blob: 9ec86e35668f5ad1c03226717ac9ed657e7ef52e [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 */
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +0100322 struct io_wq_work_node 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 */
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +0100382 struct io_wq_work_list locked_free_list;
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100383 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 cq_wait;
398 unsigned cq_extra;
399 atomic_t cq_timeouts;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100400 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700401 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700402
403 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700404 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700405
Jens Axboe89850fc2021-08-10 15:11:51 -0600406 spinlock_t timeout_lock;
407
Jens Axboedef596e2019-01-09 08:59:42 -0700408 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300409 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700410 * io_uring instances that don't use IORING_SETUP_SQPOLL.
411 * For SQPOLL, only the single threaded io_sq_thread() will
412 * manipulate the list, hence no extra locking is needed there.
413 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +0100414 struct io_wq_work_list iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700415 struct hlist_head *cancel_hash;
416 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800417 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700418 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600419
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200420 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700421
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100422 /* slow path rsrc auxilary data, used by update/register */
423 struct {
424 struct io_rsrc_node *rsrc_backup_node;
425 struct io_mapped_ubuf *dummy_ubuf;
426 struct io_rsrc_data *file_data;
427 struct io_rsrc_data *buf_data;
428
429 struct delayed_work rsrc_put_work;
430 struct llist_head rsrc_put_llist;
431 struct list_head rsrc_ref_list;
432 spinlock_t rsrc_ref_lock;
433 };
434
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700435 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100436 struct {
437 #if defined(CONFIG_UNIX)
438 struct socket *ring_sock;
439 #endif
440 /* hashed buffered write serialization */
441 struct io_wq_hash *hash_map;
442
443 /* Only used for accounting purposes */
444 struct user_struct *user;
445 struct mm_struct *mm_account;
446
447 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100448 struct llist_head fallback_llist;
449 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100450 struct work_struct exit_work;
451 struct list_head tctx_list;
452 struct completion ref_comp;
453 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700454};
455
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100456struct io_uring_task {
457 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100458 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100459 struct xarray xa;
460 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100461 const struct io_ring_ctx *last;
462 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100463 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100464 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100465 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100466
467 spinlock_t task_lock;
468 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100469 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100470 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100471};
472
Jens Axboe09bb8392019-03-13 12:39:28 -0600473/*
474 * First field must be the file pointer in all the
475 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
476 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700477struct io_poll_iocb {
478 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000479 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700480 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600481 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700482 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700483 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700484};
485
Pavel Begunkov9d805892021-04-13 02:58:40 +0100486struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000487 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100488 u64 old_user_data;
489 u64 new_user_data;
490 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600491 bool update_events;
492 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000493};
494
Jens Axboeb5dba592019-12-11 14:02:38 -0700495struct io_close {
496 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700497 int fd;
Pavel Begunkov7df778b2021-09-24 20:04:29 +0100498 u32 file_slot;
Jens Axboeb5dba592019-12-11 14:02:38 -0700499};
500
Jens Axboead8a48a2019-11-15 08:49:11 -0700501struct io_timeout_data {
502 struct io_kiocb *req;
503 struct hrtimer timer;
504 struct timespec64 ts;
505 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600506 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700507};
508
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700509struct io_accept {
510 struct file *file;
511 struct sockaddr __user *addr;
512 int __user *addr_len;
513 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100514 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600515 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700516};
517
518struct io_sync {
519 struct file *file;
520 loff_t len;
521 loff_t off;
522 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700523 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700524};
525
Jens Axboefbf23842019-12-17 18:45:56 -0700526struct io_cancel {
527 struct file *file;
528 u64 addr;
529};
530
Jens Axboeb29472e2019-12-17 18:50:29 -0700531struct io_timeout {
532 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300533 u32 off;
534 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300535 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000536 /* head of the link, used by linked timeouts only */
537 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600538 /* for linked completions */
539 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700540};
541
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100542struct io_timeout_rem {
543 struct file *file;
544 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000545
546 /* timeout update */
547 struct timespec64 ts;
548 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600549 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100550};
551
Jens Axboe9adbd452019-12-20 08:45:55 -0700552struct io_rw {
553 /* NOTE: kiocb has the file as the first member, so don't do it here */
554 struct kiocb kiocb;
555 u64 addr;
556 u64 len;
557};
558
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700559struct io_connect {
560 struct file *file;
561 struct sockaddr __user *addr;
562 int addr_len;
563};
564
Jens Axboee47293f2019-12-20 08:58:21 -0700565struct io_sr_msg {
566 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700567 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100568 struct compat_msghdr __user *umsg_compat;
569 struct user_msghdr __user *umsg;
570 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700571 };
Jens Axboee47293f2019-12-20 08:58:21 -0700572 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700573 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700574 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700575};
576
Jens Axboe15b71ab2019-12-11 11:20:36 -0700577struct io_open {
578 struct file *file;
579 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100580 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700581 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700582 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600583 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700584};
585
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000586struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700587 struct file *file;
588 u64 arg;
589 u32 nr_args;
590 u32 offset;
591};
592
Jens Axboe4840e412019-12-25 22:03:45 -0700593struct io_fadvise {
594 struct file *file;
595 u64 offset;
596 u32 len;
597 u32 advice;
598};
599
Jens Axboec1ca7572019-12-25 22:18:28 -0700600struct io_madvise {
601 struct file *file;
602 u64 addr;
603 u32 len;
604 u32 advice;
605};
606
Jens Axboe3e4827b2020-01-08 15:18:09 -0700607struct io_epoll {
608 struct file *file;
609 int epfd;
610 int op;
611 int fd;
612 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700613};
614
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300615struct io_splice {
616 struct file *file_out;
617 struct file *file_in;
618 loff_t off_out;
619 loff_t off_in;
620 u64 len;
621 unsigned int flags;
622};
623
Jens Axboeddf0322d2020-02-23 16:41:33 -0700624struct io_provide_buf {
625 struct file *file;
626 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100627 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700628 __u32 bgid;
629 __u16 nbufs;
630 __u16 bid;
631};
632
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700633struct io_statx {
634 struct file *file;
635 int dfd;
636 unsigned int mask;
637 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700638 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700639 struct statx __user *buffer;
640};
641
Jens Axboe36f4fa62020-09-05 11:14:22 -0600642struct io_shutdown {
643 struct file *file;
644 int how;
645};
646
Jens Axboe80a261f2020-09-28 14:23:58 -0600647struct io_rename {
648 struct file *file;
649 int old_dfd;
650 int new_dfd;
651 struct filename *oldpath;
652 struct filename *newpath;
653 int flags;
654};
655
Jens Axboe14a11432020-09-28 14:27:37 -0600656struct io_unlink {
657 struct file *file;
658 int dfd;
659 int flags;
660 struct filename *filename;
661};
662
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700663struct io_mkdir {
664 struct file *file;
665 int dfd;
666 umode_t mode;
667 struct filename *filename;
668};
669
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700670struct io_symlink {
671 struct file *file;
672 int new_dfd;
673 struct filename *oldpath;
674 struct filename *newpath;
675};
676
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700677struct io_hardlink {
678 struct file *file;
679 int old_dfd;
680 int new_dfd;
681 struct filename *oldpath;
682 struct filename *newpath;
683 int flags;
684};
685
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300686struct io_completion {
687 struct file *file;
Pavel Begunkov8c3f9cd2021-02-28 22:35:15 +0000688 u32 cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300689};
690
Jens Axboef499a022019-12-02 16:28:46 -0700691struct io_async_connect {
692 struct sockaddr_storage address;
693};
694
Jens Axboe03b12302019-12-02 18:50:25 -0700695struct io_async_msghdr {
696 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000697 /* points to an allocated iov, if NULL we use fast_iov instead */
698 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700699 struct sockaddr __user *uaddr;
700 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700701 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700702};
703
Jens Axboef67676d2019-12-02 11:03:47 -0700704struct io_async_rw {
705 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600706 const struct iovec *free_iovec;
707 struct iov_iter iter;
Jens Axboecd658692021-09-10 11:19:14 -0600708 struct iov_iter_state iter_state;
Jens Axboe227c0c92020-08-13 11:51:40 -0600709 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600710 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700711};
712
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300713enum {
714 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
715 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
716 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
717 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
718 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700719 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300720
Pavel Begunkovdddca222021-04-27 16:13:52 +0100721 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100722 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300723 REQ_F_INFLIGHT_BIT,
724 REQ_F_CUR_POS_BIT,
725 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300726 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300727 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700728 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700729 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000730 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600731 REQ_F_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100732 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100733 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100734 REQ_F_ARM_LTIMEOUT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700735 /* keep async read/write and isreg together and in order */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100736 REQ_F_NOWAIT_READ_BIT,
737 REQ_F_NOWAIT_WRITE_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700738 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700739
740 /* not a real bit, just to check we're not overflowing the space */
741 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300742};
743
744enum {
745 /* ctx owns file */
746 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
747 /* drain existing IO first */
748 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
749 /* linked sqes */
750 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
751 /* doesn't sever on completion < 0 */
752 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
753 /* IOSQE_ASYNC */
754 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700755 /* IOSQE_BUFFER_SELECT */
756 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300757
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300758 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100759 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000760 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300761 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
762 /* read/write uses file position */
763 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
764 /* must not punt to workers */
765 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100766 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300767 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300768 /* needs cleanup */
769 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700770 /* already went through poll handler */
771 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700772 /* buffer already selected */
773 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000774 /* completion is deferred through io_comp_state */
775 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600776 /* caller should reissue async */
777 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700778 /* supports async reads */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100779 REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700780 /* supports async writes */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100781 REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700782 /* regular file */
783 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100784 /* has creds assigned */
785 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100786 /* skip refcounting if not set */
787 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100788 /* there is a linked timeout that has to be armed */
789 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700790};
791
792struct async_poll {
793 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600794 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300795};
796
Pavel Begunkovf237c302021-08-18 12:42:46 +0100797typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100798
Jens Axboe7cbf1722021-02-10 00:03:20 +0000799struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100800 union {
801 struct io_wq_work_node node;
802 struct llist_node fallback_node;
803 };
804 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000805};
806
Pavel Begunkov992da012021-06-10 16:37:37 +0100807enum {
808 IORING_RSRC_FILE = 0,
809 IORING_RSRC_BUFFER = 1,
810};
811
Jens Axboe09bb8392019-03-13 12:39:28 -0600812/*
813 * NOTE! Each of the iocb union members has the file pointer
814 * as the first entry in their struct definition. So you can
815 * access the file pointer through any of the sub-structs,
816 * or directly as just 'ki_filp' in this struct.
817 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700818struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700819 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600820 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700821 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700822 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100823 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700824 struct io_accept accept;
825 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700826 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700827 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100828 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700829 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700830 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700831 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700832 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000833 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700834 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700835 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700836 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300837 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700838 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700839 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600840 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600841 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600842 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700843 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700844 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700845 struct io_hardlink hardlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300846 /* use only after cleaning per-op data, see io_clean_op() */
847 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700848 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700849
Jens Axboee8c2bc12020-08-15 18:44:09 -0700850 /* opcode allocated if it needs to store data for async defer */
851 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700852 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800853 /* polled IO has completed */
854 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700855
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700856 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300857 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700858
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300859 struct io_ring_ctx *ctx;
860 unsigned int flags;
Jens Axboeabc54d62021-02-24 13:32:30 -0700861 atomic_t refs;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300862 struct task_struct *task;
863 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700864
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000865 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700866
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100867 /* used by request caches, completion batching and iopoll */
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100868 struct io_wq_work_node comp_list;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100869 struct io_kiocb *link;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100870 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300871 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
872 struct hlist_node hash_node;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100873 /* internal polling, see IORING_FEAT_FAST_POLL */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300874 struct async_poll *apoll;
Pavel Begunkoveae071c2021-04-25 14:32:24 +0100875 /* store used ubuf, so we can prevent reloading */
876 struct io_mapped_ubuf *imu;
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100877 struct io_wq_work work;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100878 /* custom credentials, valid IFF REQ_F_CREDS is set */
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100879 const struct cred *creds;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100880 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
Pavel Begunkov30d51dd2021-10-01 18:07:03 +0100881 struct io_buffer *kbuf;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700882};
883
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000884struct io_tctx_node {
885 struct list_head ctx_node;
886 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000887 struct io_ring_ctx *ctx;
888};
889
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300890struct io_defer_entry {
891 struct list_head list;
892 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300893 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300894};
895
Jens Axboed3656342019-12-18 09:50:26 -0700896struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700897 /* needs req->file assigned */
898 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700899 /* hash wq insertion if file is a regular file */
900 unsigned hash_reg_file : 1;
901 /* unbound wq insertion if file is a non-regular file */
902 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700903 /* opcode is not supported by this kernel */
904 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700905 /* set if opcode supports polled "wait" */
906 unsigned pollin : 1;
907 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700908 /* op supports buffer selection */
909 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000910 /* do prep async if is going to be punted */
911 unsigned needs_async_setup : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600912 /* should block plug */
913 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700914 /* size of async data needed, if any */
915 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700916};
917
Jens Axboe09186822020-10-13 15:01:40 -0600918static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300919 [IORING_OP_NOP] = {},
920 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700921 .needs_file = 1,
922 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700923 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700924 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000925 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600926 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700927 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700928 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300929 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700930 .needs_file = 1,
931 .hash_reg_file = 1,
932 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700933 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000934 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600935 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700936 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700937 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300938 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700939 .needs_file = 1,
940 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300941 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700942 .needs_file = 1,
943 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700944 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600945 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700946 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700947 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300948 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700949 .needs_file = 1,
950 .hash_reg_file = 1,
951 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700952 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600953 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700954 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700955 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300956 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700957 .needs_file = 1,
958 .unbound_nonreg_file = 1,
959 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300960 [IORING_OP_POLL_REMOVE] = {},
961 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700962 .needs_file = 1,
963 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300964 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700965 .needs_file = 1,
966 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700967 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000968 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700969 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700970 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300971 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700972 .needs_file = 1,
973 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700974 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700975 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000976 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700977 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700978 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300979 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700980 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700981 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000982 [IORING_OP_TIMEOUT_REMOVE] = {
983 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000984 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300985 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700986 .needs_file = 1,
987 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700988 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700989 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300990 [IORING_OP_ASYNC_CANCEL] = {},
991 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700992 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700993 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300994 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700995 .needs_file = 1,
996 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700997 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000998 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700999 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -07001000 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001001 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -07001002 .needs_file = 1,
1003 },
Jens Axboe44526be2021-02-15 13:32:18 -07001004 [IORING_OP_OPENAT] = {},
1005 [IORING_OP_CLOSE] = {},
1006 [IORING_OP_FILES_UPDATE] = {},
1007 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001008 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001009 .needs_file = 1,
1010 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001011 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001012 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001013 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001014 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001015 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001016 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001017 .needs_file = 1,
Jens Axboe7b3188e2021-08-30 19:37:41 -06001018 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -07001019 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001020 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001021 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001022 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001023 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001024 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001025 .needs_file = 1,
1026 },
Jens Axboe44526be2021-02-15 13:32:18 -07001027 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001028 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001029 .needs_file = 1,
1030 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001031 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001032 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001033 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001034 .needs_file = 1,
1035 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001036 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001037 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001038 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001039 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001040 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001041 [IORING_OP_EPOLL_CTL] = {
1042 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001043 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001044 [IORING_OP_SPLICE] = {
1045 .needs_file = 1,
1046 .hash_reg_file = 1,
1047 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001048 },
1049 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001050 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001051 [IORING_OP_TEE] = {
1052 .needs_file = 1,
1053 .hash_reg_file = 1,
1054 .unbound_nonreg_file = 1,
1055 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001056 [IORING_OP_SHUTDOWN] = {
1057 .needs_file = 1,
1058 },
Jens Axboe44526be2021-02-15 13:32:18 -07001059 [IORING_OP_RENAMEAT] = {},
1060 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001061 [IORING_OP_MKDIRAT] = {},
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07001062 [IORING_OP_SYMLINKAT] = {},
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07001063 [IORING_OP_LINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001064};
1065
Pavel Begunkov0756a862021-08-15 10:40:25 +01001066/* requests with any of those set should undergo io_disarm_next() */
1067#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1068
Pavel Begunkov7a612352021-03-09 00:37:59 +00001069static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001070static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001071static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1072 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001073 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001074static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001075
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001076static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1077 long res, unsigned int cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001078static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001079static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001080static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001081static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001082static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001083 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001084 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001085static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001086static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001087 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001088static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001089static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001090
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001091static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkovc4501782021-09-08 16:40:52 +01001092static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001093static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001094
Pavel Begunkovb9445592021-08-25 12:25:45 +01001095static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1096 unsigned int issue_flags, u32 slot_index);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01001097static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags);
1098
Pavel Begunkovf1042b62021-08-28 19:54:39 -06001099static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
Pavel Begunkovb9445592021-08-25 12:25:45 +01001100
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101static struct kmem_cache *req_cachep;
1102
Jens Axboe09186822020-10-13 15:01:40 -06001103static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104
1105struct sock *io_uring_get_socket(struct file *file)
1106{
1107#if defined(CONFIG_UNIX)
1108 if (file->f_op == &io_uring_fops) {
1109 struct io_ring_ctx *ctx = file->private_data;
1110
1111 return ctx->ring_sock->sk;
1112 }
1113#endif
1114 return NULL;
1115}
1116EXPORT_SYMBOL(io_uring_get_socket);
1117
Pavel Begunkovf237c302021-08-18 12:42:46 +01001118static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1119{
1120 if (!*locked) {
1121 mutex_lock(&ctx->uring_lock);
1122 *locked = true;
1123 }
1124}
1125
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001126#define io_for_each_link(pos, head) \
1127 for (pos = (head); pos; pos = pos->link)
1128
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001129/*
1130 * Shamelessly stolen from the mm implementation of page reference checking,
1131 * see commit f958d7b528b1 for details.
1132 */
1133#define req_ref_zero_or_close_to_overflow(req) \
1134 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1135
1136static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1137{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001138 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001139 return atomic_inc_not_zero(&req->refs);
1140}
1141
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001142static inline bool req_ref_put_and_test(struct io_kiocb *req)
1143{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001144 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1145 return true;
1146
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001147 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1148 return atomic_dec_and_test(&req->refs);
1149}
1150
1151static inline void req_ref_put(struct io_kiocb *req)
1152{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001153 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001154 WARN_ON_ONCE(req_ref_put_and_test(req));
1155}
1156
1157static inline void req_ref_get(struct io_kiocb *req)
1158{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001159 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001160 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1161 atomic_inc(&req->refs);
1162}
1163
Pavel Begunkovc4501782021-09-08 16:40:52 +01001164static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
1165{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001166 if (!wq_list_empty(&ctx->submit_state.compl_reqs))
Pavel Begunkovc4501782021-09-08 16:40:52 +01001167 __io_submit_flush_completions(ctx);
1168}
1169
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001170static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001171{
1172 if (!(req->flags & REQ_F_REFCOUNT)) {
1173 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001174 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001175 }
1176}
1177
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001178static inline void io_req_set_refcount(struct io_kiocb *req)
1179{
1180 __io_req_set_refcount(req, 1);
1181}
1182
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001183static inline void io_req_set_rsrc_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001184{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001185 struct io_ring_ctx *ctx = req->ctx;
1186
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001187 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001188 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001189 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001190 }
1191}
1192
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001193static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1194{
1195 bool got = percpu_ref_tryget(ref);
1196
1197 /* already at zero, wait for ->release() */
1198 if (!got)
1199 wait_for_completion(compl);
1200 percpu_ref_resurrect(ref);
1201 if (got)
1202 percpu_ref_put(ref);
1203}
1204
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001205static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1206 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001207{
1208 struct io_kiocb *req;
1209
Pavel Begunkov68207682021-03-22 01:58:25 +00001210 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001211 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001212 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001213 return true;
1214
1215 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001216 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001217 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001218 }
1219 return false;
1220}
1221
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001222static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001223{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001224 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001225}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001226
Hao Xua8295b92021-08-27 17:46:09 +08001227static inline void req_fail_link_node(struct io_kiocb *req, int res)
1228{
1229 req_set_fail(req);
1230 req->result = res;
1231}
1232
Jens Axboe2b188cc2019-01-07 10:46:33 -07001233static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1234{
1235 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1236
Jens Axboe0f158b42020-05-14 17:18:39 -06001237 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001238}
1239
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001240static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1241{
1242 return !req->timeout.off;
1243}
1244
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001245static void io_fallback_req_func(struct work_struct *work)
1246{
1247 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1248 fallback_work.work);
1249 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1250 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001251 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001252
1253 percpu_ref_get(&ctx->refs);
1254 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001255 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001256
Pavel Begunkovf237c302021-08-18 12:42:46 +01001257 if (locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01001258 io_submit_flush_completions(ctx);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001259 mutex_unlock(&ctx->uring_lock);
1260 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001261 percpu_ref_put(&ctx->refs);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001262
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001263}
1264
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1266{
1267 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001268 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269
1270 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1271 if (!ctx)
1272 return NULL;
1273
Jens Axboe78076bb2019-12-04 19:56:40 -07001274 /*
1275 * Use 5 bits less than the max cq entries, that should give us around
1276 * 32 entries per hash list if totally full and uniformly spread.
1277 */
1278 hash_bits = ilog2(p->cq_entries);
1279 hash_bits -= 5;
1280 if (hash_bits <= 0)
1281 hash_bits = 1;
1282 ctx->cancel_hash_bits = hash_bits;
1283 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1284 GFP_KERNEL);
1285 if (!ctx->cancel_hash)
1286 goto err;
1287 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1288
Pavel Begunkov62248432021-04-28 13:11:29 +01001289 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1290 if (!ctx->dummy_ubuf)
1291 goto err;
1292 /* set invalid range, so io_import_fixed() fails meeting it */
1293 ctx->dummy_ubuf->ubuf = -1UL;
1294
Roman Gushchin21482892019-05-07 10:01:48 -07001295 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001296 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1297 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001298
1299 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001300 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001301 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001302 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001303 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001304 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001305 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001306 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001307 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001309 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01001310 INIT_WQ_LIST(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001311 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001312 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001313 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001314 spin_lock_init(&ctx->rsrc_ref_lock);
1315 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001316 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1317 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001318 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001319 ctx->submit_state.free_list.next = NULL;
1320 INIT_WQ_LIST(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001321 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001322 INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001323 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001324err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001325 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001326 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001327 kfree(ctx);
1328 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001329}
1330
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001331static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1332{
1333 struct io_rings *r = ctx->rings;
1334
1335 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1336 ctx->cq_extra--;
1337}
1338
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001339static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001340{
Jens Axboe2bc99302020-07-09 09:43:27 -06001341 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1342 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001343
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001344 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001345 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001346
Bob Liu9d858b22019-11-13 18:06:25 +08001347 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001348}
1349
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001350#define FFS_ASYNC_READ 0x1UL
1351#define FFS_ASYNC_WRITE 0x2UL
1352#ifdef CONFIG_64BIT
1353#define FFS_ISREG 0x4UL
1354#else
1355#define FFS_ISREG 0x0UL
1356#endif
1357#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
1358
1359static inline bool io_req_ffs_set(struct io_kiocb *req)
1360{
1361 return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE);
1362}
1363
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001364static void io_req_track_inflight(struct io_kiocb *req)
1365{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001366 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001367 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001368 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001369 }
1370}
1371
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001372static inline void io_unprep_linked_timeout(struct io_kiocb *req)
1373{
1374 req->flags &= ~REQ_F_LINK_TIMEOUT;
1375}
1376
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001377static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1378{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001379 if (WARN_ON_ONCE(!req->link))
1380 return NULL;
1381
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001382 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1383 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001384
1385 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001386 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001387 __io_req_set_refcount(req->link, 2);
1388 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001389}
1390
1391static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1392{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001393 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001394 return NULL;
1395 return __io_prep_linked_timeout(req);
1396}
1397
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001398static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001399{
Jens Axboed3656342019-12-18 09:50:26 -07001400 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001401 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001402
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001403 if (!(req->flags & REQ_F_CREDS)) {
1404 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001405 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001406 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001407
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001408 req->work.list.next = NULL;
1409 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001410 if (req->flags & REQ_F_FORCE_ASYNC)
1411 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1412
Jens Axboed3656342019-12-18 09:50:26 -07001413 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001414 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001415 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001416 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001417 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001418 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001419 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001420
1421 switch (req->opcode) {
1422 case IORING_OP_SPLICE:
1423 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001424 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1425 req->work.flags |= IO_WQ_WORK_UNBOUND;
1426 break;
1427 }
Jens Axboe561fb042019-10-24 07:25:42 -06001428}
1429
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001430static void io_prep_async_link(struct io_kiocb *req)
1431{
1432 struct io_kiocb *cur;
1433
Pavel Begunkov44eff402021-07-26 14:14:31 +01001434 if (req->flags & REQ_F_LINK_TIMEOUT) {
1435 struct io_ring_ctx *ctx = req->ctx;
1436
Jens Axboe79ebeae2021-08-10 15:18:27 -06001437 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001438 io_for_each_link(cur, req)
1439 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001440 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001441 } else {
1442 io_for_each_link(cur, req)
1443 io_prep_async_work(cur);
1444 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001445}
1446
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001447static inline void io_req_add_compl_list(struct io_kiocb *req)
1448{
1449 struct io_submit_state *state = &req->ctx->submit_state;
1450
1451 wq_list_add_tail(&req->comp_list, &state->compl_reqs);
1452}
1453
Pavel Begunkovf237c302021-08-18 12:42:46 +01001454static void io_queue_async_work(struct io_kiocb *req, bool *locked)
Jens Axboe561fb042019-10-24 07:25:42 -06001455{
Jackie Liua197f662019-11-08 08:09:12 -07001456 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001457 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001458 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001459
Pavel Begunkovf237c302021-08-18 12:42:46 +01001460 /* must not take the lock, NULL it as a precaution */
1461 locked = NULL;
1462
Jens Axboe3bfe6102021-02-16 14:15:30 -07001463 BUG_ON(!tctx);
1464 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001465
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001466 /* init ->work of the whole link before punting */
1467 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001468
1469 /*
1470 * Not expected to happen, but if we do have a bug where this _can_
1471 * happen, catch it here and ensure the request is marked as
1472 * canceled. That will make io-wq go through the usual work cancel
1473 * procedure rather than attempt to run this request (or create a new
1474 * worker for it).
1475 */
1476 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1477 req->work.flags |= IO_WQ_WORK_CANCEL;
1478
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001479 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1480 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001481 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001482 if (link)
1483 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001484}
1485
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001486static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001487 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001488 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001489{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001490 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001491
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001492 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001493 if (status)
1494 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001495 atomic_set(&req->ctx->cq_timeouts,
1496 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001497 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001498 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001499 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001500 }
1501}
1502
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001503static void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001504{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001505 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001506 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1507 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001508
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001509 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001510 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001511 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001512 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001513 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001514 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001515}
1516
Pavel Begunkov360428f2020-05-30 14:54:17 +03001517static void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001518 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001519{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001520 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001521
Jens Axboe79ebeae2021-08-10 15:18:27 -06001522 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001523 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001524 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001525 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001526 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001527
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001528 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001529 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001530
1531 /*
1532 * Since seq can easily wrap around over time, subtract
1533 * the last seq at which timeouts were flushed before comparing.
1534 * Assuming not more than 2^31-1 events have happened since,
1535 * these subtractions won't have wrapped, so we can check if
1536 * target is in [last_seq, current_seq] by comparing the two.
1537 */
1538 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1539 events_got = seq - ctx->cq_last_tm_flush;
1540 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001541 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001542
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001543 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001544 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001545 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001546 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001547 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001548}
1549
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001550static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001551{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001552 if (ctx->off_timeout_used)
1553 io_flush_timeouts(ctx);
1554 if (ctx->drain_active)
1555 io_queue_deferred(ctx);
1556}
1557
1558static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1559{
1560 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1561 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001562 /* order cqe stores with ring update */
1563 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001564}
1565
Jens Axboe90554202020-09-03 12:12:41 -06001566static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1567{
1568 struct io_rings *r = ctx->rings;
1569
Pavel Begunkova566c552021-05-16 22:58:08 +01001570 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001571}
1572
Pavel Begunkov888aae22021-01-19 13:32:39 +00001573static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1574{
1575 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1576}
1577
Pavel Begunkovd068b502021-05-16 22:58:11 +01001578static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001579{
Hristo Venev75b28af2019-08-26 17:23:46 +00001580 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001581 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001582
Stefan Bühler115e12e2019-04-24 23:54:18 +02001583 /*
1584 * writes to the cq entry need to come after reading head; the
1585 * control dependency is enough as we're using WRITE_ONCE to
1586 * fill the cq entry
1587 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001588 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001589 return NULL;
1590
Pavel Begunkov888aae22021-01-19 13:32:39 +00001591 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001592 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001593}
1594
Jens Axboef2842ab2020-01-08 11:04:00 -07001595static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1596{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001597 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001598 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001599 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1600 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001601 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001602}
1603
Jens Axboe2c5d7632021-08-21 07:21:19 -06001604/*
1605 * This should only get called when at least one event has been posted.
1606 * Some applications rely on the eventfd notification count only changing
1607 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1608 * 1:1 relationship between how many times this function is called (and
1609 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1610 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001611static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001612{
Jens Axboe5fd46172021-08-06 14:04:31 -06001613 /*
1614 * wake_up_all() may seem excessive, but io_wake_function() and
1615 * io_should_wake() handle the termination of the loop and only
1616 * wake as many waiters as we need to.
1617 */
1618 if (wq_has_sleeper(&ctx->cq_wait))
1619 wake_up_all(&ctx->cq_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001620 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001621 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001622}
1623
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001624static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1625{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001626 /* see waitqueue_active() comment */
1627 smp_mb();
1628
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001629 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001630 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001631 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001632 }
1633 if (io_should_trigger_evfd(ctx))
1634 eventfd_signal(ctx->cq_ev_fd, 1);
1635}
1636
Jens Axboec4a2ed72019-11-21 21:01:26 -07001637/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001638static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001639{
Jens Axboeb18032b2021-01-24 16:58:56 -07001640 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001641
Pavel Begunkova566c552021-05-16 22:58:08 +01001642 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001643 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001644
Jens Axboeb18032b2021-01-24 16:58:56 -07001645 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001646 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001647 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001648 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001649 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001650
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001651 if (!cqe && !force)
1652 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001653 ocqe = list_first_entry(&ctx->cq_overflow_list,
1654 struct io_overflow_cqe, list);
1655 if (cqe)
1656 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1657 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001658 io_account_cq_overflow(ctx);
1659
Jens Axboeb18032b2021-01-24 16:58:56 -07001660 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001661 list_del(&ocqe->list);
1662 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001663 }
1664
Pavel Begunkov09e88402020-12-17 00:24:38 +00001665 all_flushed = list_empty(&ctx->cq_overflow_list);
1666 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001667 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001668 WRITE_ONCE(ctx->rings->sq_flags,
1669 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001670 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001671
Jens Axboeb18032b2021-01-24 16:58:56 -07001672 if (posted)
1673 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001674 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001675 if (posted)
1676 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001677 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001678}
1679
Pavel Begunkov90f67362021-08-09 20:18:12 +01001680static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001681{
Jens Axboeca0a2652021-03-04 17:15:48 -07001682 bool ret = true;
1683
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001684 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001685 /* iopoll syncs against uring_lock, not completion_lock */
1686 if (ctx->flags & IORING_SETUP_IOPOLL)
1687 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001688 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001689 if (ctx->flags & IORING_SETUP_IOPOLL)
1690 mutex_unlock(&ctx->uring_lock);
1691 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001692
1693 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001694}
1695
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001696/* must to be called somewhat shortly after putting a request */
1697static inline void io_put_task(struct task_struct *task, int nr)
1698{
1699 struct io_uring_task *tctx = task->io_uring;
1700
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001701 if (likely(task == current)) {
1702 tctx->cached_refs += nr;
1703 } else {
1704 percpu_counter_sub(&tctx->inflight, nr);
1705 if (unlikely(atomic_read(&tctx->in_idle)))
1706 wake_up(&tctx->wait);
1707 put_task_struct_many(task, nr);
1708 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001709}
1710
Pavel Begunkov9a108672021-08-27 11:55:01 +01001711static void io_task_refs_refill(struct io_uring_task *tctx)
1712{
1713 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1714
1715 percpu_counter_add(&tctx->inflight, refill);
1716 refcount_add(refill, &current->usage);
1717 tctx->cached_refs += refill;
1718}
1719
1720static inline void io_get_task_refs(int nr)
1721{
1722 struct io_uring_task *tctx = current->io_uring;
1723
1724 tctx->cached_refs -= nr;
1725 if (unlikely(tctx->cached_refs < 0))
1726 io_task_refs_refill(tctx);
1727}
1728
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001729static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1730 long res, unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001732 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001734 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1735 if (!ocqe) {
1736 /*
1737 * If we're in ring overflow flush mode, or in task cancel mode,
1738 * or cannot allocate an overflow entry, then we need to drop it
1739 * on the floor.
1740 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001741 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001742 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001744 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001745 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001746 WRITE_ONCE(ctx->rings->sq_flags,
1747 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1748
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001749 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001750 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001751 ocqe->cqe.res = res;
1752 ocqe->cqe.flags = cflags;
1753 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1754 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001755}
1756
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001757static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1758 long res, unsigned int cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001759{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001760 struct io_uring_cqe *cqe;
1761
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001762 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001763
1764 /*
1765 * If we can't get a cq entry, userspace overflowed the
1766 * submission (by quite a lot). Increment the overflow count in
1767 * the ring.
1768 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001769 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001770 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001771 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001772 WRITE_ONCE(cqe->res, res);
1773 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001774 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001775 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001776 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777}
1778
Pavel Begunkov8d133262021-04-11 01:46:33 +01001779/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001780static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1781 long res, unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001782{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001783 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001784}
1785
Pavel Begunkov7a612352021-03-09 00:37:59 +00001786static void io_req_complete_post(struct io_kiocb *req, long res,
1787 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001789 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790
Jens Axboe79ebeae2021-08-10 15:18:27 -06001791 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001792 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001793 /*
1794 * If we're the last reference to this request, add to our locked
1795 * free_list cache.
1796 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001797 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001798 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001799 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001800 io_disarm_next(req);
1801 if (req->link) {
1802 io_req_task_queue(req->link);
1803 req->link = NULL;
1804 }
1805 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001806 io_dismantle_req(req);
1807 io_put_task(req->task, 1);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001808 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001809 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001810 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001811 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001812 spin_unlock(&ctx->completion_lock);
Pavel Begunkova3f349072021-09-15 12:04:20 +01001813 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001814}
1815
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001816static inline bool io_req_needs_clean(struct io_kiocb *req)
1817{
Pavel Begunkovc8543572021-06-17 18:14:04 +01001818 return req->flags & IO_REQ_CLEAN_FLAGS;
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001819}
1820
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001821static inline void io_req_complete_state(struct io_kiocb *req, long res,
1822 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001823{
Pavel Begunkova1cdbb42021-09-24 22:00:03 +01001824 /* clean per-opcode space, because req->compl is aliased with it */
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001825 if (io_req_needs_clean(req))
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001826 io_clean_op(req);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001827 req->result = res;
1828 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001829 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001830}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001831
Pavel Begunkov889fca72021-02-10 00:03:09 +00001832static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1833 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001834{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001835 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1836 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001837 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001838 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001839}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001840
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001841static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001842{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001843 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001844}
1845
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001846static void io_req_complete_failed(struct io_kiocb *req, long res)
1847{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001848 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001849 io_req_complete_post(req, res, 0);
1850}
1851
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01001852static void io_req_complete_fail_submit(struct io_kiocb *req)
1853{
1854 /*
1855 * We don't submit, fail them all, for that replace hardlinks with
1856 * normal links. Extra REQ_F_LINK is tolerated.
1857 */
1858 req->flags &= ~REQ_F_HARDLINK;
1859 req->flags |= REQ_F_LINK;
1860 io_req_complete_failed(req, req->result);
1861}
1862
Pavel Begunkov864ea922021-08-09 13:04:08 +01001863/*
1864 * Don't initialise the fields below on every allocation, but do that in
1865 * advance and keep them valid across allocations.
1866 */
1867static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1868{
1869 req->ctx = ctx;
1870 req->link = NULL;
1871 req->async_data = NULL;
1872 /* not necessary, but safer to zero */
1873 req->result = 0;
1874}
1875
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001876static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001877 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001878{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001879 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001880 wq_list_splice(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001881 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001882 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001883}
1884
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001885/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001886static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001887{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001888 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001889
Jens Axboec7dae4b2021-02-09 19:53:37 -07001890 /*
1891 * If we have more than a batch's worth of requests in our IRQ side
1892 * locked cache, grab the lock and move them over to our submission
1893 * side cache.
1894 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001895 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001896 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001897 return !!state->free_list.next;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898}
1899
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001900/*
1901 * A request might get retired back into the request caches even before opcode
1902 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1903 * Because of that, io_alloc_req() should be called only under ->uring_lock
1904 * and with extra caution to not get a request that is still worked on.
1905 */
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001906static bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001907 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001909 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001910 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001911 void *reqs[IO_REQ_ALLOC_BATCH];
1912 struct io_kiocb *req;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001913 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001914
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001915 if (likely(state->free_list.next || io_flush_cached_reqs(ctx)))
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001916 return true;
Jens Axboe2579f912019-01-09 09:10:43 -07001917
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001918 ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001919
Pavel Begunkov864ea922021-08-09 13:04:08 +01001920 /*
1921 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1922 * retry single alloc to be on the safe side.
1923 */
1924 if (unlikely(ret <= 0)) {
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001925 reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1926 if (!reqs[0])
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001927 return false;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001928 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001929 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001930
Pavel Begunkov37f0e762021-10-04 20:02:53 +01001931 percpu_ref_get_many(&ctx->refs, ret);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001932 for (i = 0; i < ret; i++) {
1933 req = reqs[i];
1934
1935 io_preinit_req(req, ctx);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001936 wq_stack_add_head(&req->comp_list, &state->free_list);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001937 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001938 return true;
1939}
1940
1941static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
1942{
1943 if (unlikely(!ctx->submit_state.free_list.next))
1944 return __io_alloc_req_refill(ctx);
1945 return true;
1946}
1947
1948static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
1949{
1950 struct io_wq_work_node *node;
1951
1952 node = wq_stack_extract(&ctx->submit_state.free_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001953 return container_of(node, struct io_kiocb, comp_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001954}
1955
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001956static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001957{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001958 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001959 fput(file);
1960}
1961
Pavel Begunkov6b639522021-09-08 16:40:50 +01001962static inline void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001963{
Pavel Begunkov094bae42021-03-19 17:22:42 +00001964 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001965
Pavel Begunkov51d48da2021-10-04 20:02:47 +01001966 if (unlikely(io_req_needs_clean(req)))
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001967 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001968 if (!(flags & REQ_F_FIXED_FILE))
1969 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001970 if (req->fixed_rsrc_refs)
1971 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001972 if (req->async_data) {
Pavel Begunkov094bae42021-03-19 17:22:42 +00001973 kfree(req->async_data);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001974 req->async_data = NULL;
1975 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001976}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001977
Pavel Begunkov216578e2020-10-13 09:44:00 +01001978static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001979{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001980 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001981
Pavel Begunkov216578e2020-10-13 09:44:00 +01001982 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001983 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001984
Jens Axboe79ebeae2021-08-10 15:18:27 -06001985 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001986 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001987 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001988 spin_unlock(&ctx->completion_lock);
Jens Axboee65ef562019-03-12 10:16:44 -06001989}
1990
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001991static inline void io_remove_next_linked(struct io_kiocb *req)
1992{
1993 struct io_kiocb *nxt = req->link;
1994
1995 req->link = nxt->link;
1996 nxt->link = NULL;
1997}
1998
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001999static bool io_kill_linked_timeout(struct io_kiocb *req)
2000 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06002001 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002002{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002003 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002004
Pavel Begunkovb97e7362021-08-15 10:40:23 +01002005 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002006 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002007
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002008 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002009 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01002010 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06002011 list_del(&link->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002012 io_cqring_fill_event(link->ctx, link->user_data,
2013 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002014 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002015 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002016 }
2017 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002018 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002019}
2020
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002021static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002022 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002023{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002024 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06002025
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002026 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002027 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002028 long res = -ECANCELED;
2029
2030 if (link->flags & REQ_F_FAIL)
2031 res = link->result;
2032
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002033 nxt = link->link;
2034 link->link = NULL;
2035
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002036 trace_io_uring_fail_link(req, link);
Hao Xua8295b92021-08-27 17:46:09 +08002037 io_cqring_fill_event(link->ctx, link->user_data, res, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002038 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002039 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002040 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002041}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002042
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002043static bool io_disarm_next(struct io_kiocb *req)
2044 __must_hold(&req->ctx->completion_lock)
2045{
2046 bool posted = false;
2047
Pavel Begunkov0756a862021-08-15 10:40:25 +01002048 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2049 struct io_kiocb *link = req->link;
2050
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002051 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002052 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2053 io_remove_next_linked(req);
2054 io_cqring_fill_event(link->ctx, link->user_data,
2055 -ECANCELED, 0);
2056 io_put_req_deferred(link);
2057 posted = true;
2058 }
2059 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002060 struct io_ring_ctx *ctx = req->ctx;
2061
2062 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002063 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002064 spin_unlock_irq(&ctx->timeout_lock);
2065 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002066 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002067 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002068 posted |= (req->link != NULL);
2069 io_fail_links(req);
2070 }
2071 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002072}
2073
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002074static void __io_req_find_next_prep(struct io_kiocb *req)
2075{
2076 struct io_ring_ctx *ctx = req->ctx;
2077 bool posted;
2078
2079 spin_lock(&ctx->completion_lock);
2080 posted = io_disarm_next(req);
2081 if (posted)
2082 io_commit_cqring(req->ctx);
2083 spin_unlock(&ctx->completion_lock);
2084 if (posted)
2085 io_cqring_ev_posted(ctx);
2086}
2087
2088static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002089{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002090 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002091
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002092 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2093 return NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002094 /*
2095 * If LINK is set, we have dependent requests in this chain. If we
2096 * didn't fail this request, queue the first one up, moving any other
2097 * dependencies to the next request. In case of failure, fail the rest
2098 * of the chain.
2099 */
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002100 if (unlikely(req->flags & IO_DISARM_MASK))
2101 __io_req_find_next_prep(req);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002102 nxt = req->link;
2103 req->link = NULL;
2104 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002105}
Jens Axboe2665abf2019-11-05 12:40:47 -07002106
Pavel Begunkovf237c302021-08-18 12:42:46 +01002107static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002108{
2109 if (!ctx)
2110 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002111 if (*locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01002112 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002113 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002114 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002115 }
2116 percpu_ref_put(&ctx->refs);
2117}
2118
Jens Axboe7cbf1722021-02-10 00:03:20 +00002119static void tctx_task_work(struct callback_head *cb)
2120{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002121 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002122 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002123 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2124 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002125
Pavel Begunkov16f72072021-06-17 18:14:09 +01002126 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002127 struct io_wq_work_node *node;
2128
Pavel Begunkovc4501782021-09-08 16:40:52 +01002129 if (!tctx->task_list.first && locked)
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002130 io_submit_flush_completions(ctx);
2131
Pavel Begunkov3f184072021-06-17 18:14:06 +01002132 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002133 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002134 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002135 if (!node)
2136 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002137 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002138 if (!node)
2139 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002140
Pavel Begunkov6294f362021-08-10 17:53:55 +01002141 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002142 struct io_wq_work_node *next = node->next;
2143 struct io_kiocb *req = container_of(node, struct io_kiocb,
2144 io_task_work.node);
2145
2146 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002147 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002148 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002149 /* if not contended, grab and improve batching */
2150 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002151 percpu_ref_get(&ctx->refs);
2152 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002153 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002154 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002155 } while (node);
2156
Jens Axboe7cbf1722021-02-10 00:03:20 +00002157 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002158 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002159
Pavel Begunkovf237c302021-08-18 12:42:46 +01002160 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002161}
2162
Pavel Begunkove09ee512021-07-01 13:26:05 +01002163static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002164{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002165 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002166 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002167 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002168 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002169 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002170 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002171
2172 WARN_ON_ONCE(!tctx);
2173
Jens Axboe0b81e802021-02-16 10:33:53 -07002174 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002175 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002176 running = tctx->task_running;
2177 if (!running)
2178 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002179 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002180
2181 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002182 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002183 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002184
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002185 /*
2186 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2187 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2188 * processing task_work. There's no reliable way to tell if TWA_RESUME
2189 * will do the job.
2190 */
2191 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovd97ec622021-09-08 16:40:53 +01002192 if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2193 if (notify == TWA_NONE)
2194 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002195 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002196 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002197
Pavel Begunkove09ee512021-07-01 13:26:05 +01002198 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002199 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002200 node = tctx->task_list.first;
2201 INIT_WQ_LIST(&tctx->task_list);
2202 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002203
Pavel Begunkove09ee512021-07-01 13:26:05 +01002204 while (node) {
2205 req = container_of(node, struct io_kiocb, io_task_work.node);
2206 node = node->next;
2207 if (llist_add(&req->io_task_work.fallback_node,
2208 &req->ctx->fallback_llist))
2209 schedule_delayed_work(&req->ctx->fallback_work, 1);
2210 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002211}
2212
Pavel Begunkovf237c302021-08-18 12:42:46 +01002213static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002214{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002215 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002216
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002217 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002218 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002219 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002220}
2221
Pavel Begunkovf237c302021-08-18 12:42:46 +01002222static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002223{
2224 struct io_ring_ctx *ctx = req->ctx;
2225
Pavel Begunkovf237c302021-08-18 12:42:46 +01002226 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002227 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002228 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002229 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002230 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002231 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002232}
2233
Pavel Begunkova3df76982021-02-18 22:32:52 +00002234static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2235{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002236 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002237 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002238 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002239}
2240
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002241static void io_req_task_queue(struct io_kiocb *req)
2242{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002243 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002244 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002245}
2246
Jens Axboe773af692021-07-27 10:25:55 -06002247static void io_req_task_queue_reissue(struct io_kiocb *req)
2248{
2249 req->io_task_work.func = io_queue_async_work;
2250 io_req_task_work_add(req);
2251}
2252
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002253static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002254{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002255 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002256
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002257 if (nxt)
2258 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002259}
2260
Jens Axboe9e645e112019-05-10 16:07:28 -06002261static void io_free_req(struct io_kiocb *req)
2262{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002263 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002264 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002265}
2266
Pavel Begunkovf237c302021-08-18 12:42:46 +01002267static void io_free_req_work(struct io_kiocb *req, bool *locked)
2268{
2269 io_free_req(req);
2270}
2271
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002272static void io_free_batch_list(struct io_ring_ctx *ctx,
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002273 struct io_wq_work_node *node)
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002274 __must_hold(&ctx->uring_lock)
2275{
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002276 struct task_struct *task = NULL;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002277 int task_refs = 0;
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002278
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002279 do {
2280 struct io_kiocb *req = container_of(node, struct io_kiocb,
2281 comp_list);
2282
2283 node = req->comp_list.next;
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002284 if (!req_ref_put_and_test(req))
2285 continue;
2286
2287 io_queue_next(req);
2288 io_dismantle_req(req);
2289
2290 if (req->task != task) {
2291 if (task)
2292 io_put_task(task, task_refs);
2293 task = req->task;
2294 task_refs = 0;
2295 }
2296 task_refs++;
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002297 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002298 } while (node);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002299
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002300 if (task)
2301 io_put_task(task, task_refs);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002302}
2303
Pavel Begunkovc4501782021-09-08 16:40:52 +01002304static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
Jens Axboea141dd82021-08-12 12:48:34 -06002305 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002306{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002307 struct io_wq_work_node *node, *prev;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002308 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002309
Jens Axboe79ebeae2021-08-10 15:18:27 -06002310 spin_lock(&ctx->completion_lock);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002311 wq_list_for_each(node, prev, &state->compl_reqs) {
2312 struct io_kiocb *req = container_of(node, struct io_kiocb,
2313 comp_list);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002314
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002315 __io_cqring_fill_event(ctx, req->user_data, req->result,
2316 req->compl.cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002317 }
2318 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002319 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002320 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002321
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002322 io_free_batch_list(ctx, state->compl_reqs.first);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002323 INIT_WQ_LIST(&state->compl_reqs);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002324}
2325
Jens Axboeba816ad2019-09-28 11:36:45 -06002326/*
2327 * Drop reference to request, return next in chain (if there is one) if this
2328 * was the last reference to this request.
2329 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002330static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002331{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002332 struct io_kiocb *nxt = NULL;
2333
Jens Axboede9b4cc2021-02-24 13:28:27 -07002334 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002335 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002336 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002337 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002338 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002339}
2340
Pavel Begunkov0d850352021-03-19 17:22:37 +00002341static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002342{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002343 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002344 io_free_req(req);
2345}
2346
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002347static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002348{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002349 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002350 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002351 io_req_task_work_add(req);
2352 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002353}
2354
Pavel Begunkov6c503152021-01-04 20:36:36 +00002355static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002356{
2357 /* See comment at the top of this file */
2358 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002359 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002360}
2361
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002362static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2363{
2364 struct io_rings *rings = ctx->rings;
2365
2366 /* make sure SQ entry isn't read before tail */
2367 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2368}
2369
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002370static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002371{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002372 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002373
Jens Axboebcda7ba2020-02-23 16:42:51 -07002374 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2375 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002376 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002377 kfree(kbuf);
2378 return cflags;
2379}
2380
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002381static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2382{
Pavel Begunkovae421d92021-08-17 20:28:08 +01002383 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2384 return 0;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01002385 return io_put_kbuf(req, req->kbuf);
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002386}
2387
Jens Axboe4c6e2772020-07-01 11:29:10 -06002388static inline bool io_run_task_work(void)
2389{
Nadav Amitef98eb02021-08-07 17:13:41 -07002390 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002391 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002392 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002393 return true;
2394 }
2395
2396 return false;
2397}
2398
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002399static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
Jens Axboedef596e2019-01-09 08:59:42 -07002400{
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002401 struct io_wq_work_node *pos, *start, *prev;
Christoph Hellwigd729cf92021-10-12 13:12:20 +02002402 unsigned int poll_flags = BLK_POLL_NOSLEEP;
Jens Axboeb688f112021-10-12 09:28:46 -06002403 DEFINE_IO_COMP_BATCH(iob);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002404 int nr_events = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002405
2406 /*
2407 * Only spin for completions if we don't have multiple devices hanging
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002408 * off our complete list.
Jens Axboedef596e2019-01-09 08:59:42 -07002409 */
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002410 if (ctx->poll_multi_queue || force_nonspin)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002411 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002412
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002413 wq_list_for_each(pos, start, &ctx->iopoll_list) {
2414 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002415 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002416 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002417
2418 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002419 * Move completed and retryable entries to our local lists.
2420 * If we find a request that requires polling, break out
2421 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002422 */
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002423 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002424 break;
2425
Jens Axboeb688f112021-10-12 09:28:46 -06002426 ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002427 if (unlikely(ret < 0))
2428 return ret;
2429 else if (ret)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002430 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002431
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002432 /* iopoll may have completed current req */
Jens Axboeb688f112021-10-12 09:28:46 -06002433 if (!rq_list_empty(iob.req_list) ||
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002434 READ_ONCE(req->iopoll_completed))
2435 break;
Jens Axboedef596e2019-01-09 08:59:42 -07002436 }
2437
Jens Axboeb688f112021-10-12 09:28:46 -06002438 if (!rq_list_empty(iob.req_list))
2439 iob.complete(&iob);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002440 else if (!pos)
2441 return 0;
2442
2443 prev = start;
2444 wq_list_for_each_resume(pos, prev) {
2445 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
2446
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002447 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
2448 if (!smp_load_acquire(&req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002449 break;
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002450 __io_cqring_fill_event(ctx, req->user_data, req->result,
Pavel Begunkovf5ed3bc2021-09-24 21:59:52 +01002451 io_put_rw_kbuf(req));
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002452 nr_events++;
2453 }
Jens Axboedef596e2019-01-09 08:59:42 -07002454
Pavel Begunkovf5ed3bc2021-09-24 21:59:52 +01002455 if (unlikely(!nr_events))
2456 return 0;
2457
2458 io_commit_cqring(ctx);
2459 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002460 pos = start ? start->next : ctx->iopoll_list.first;
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002461 wq_list_cut(&ctx->iopoll_list, prev, start);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002462 io_free_batch_list(ctx, pos);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002463 return nr_events;
Jens Axboedef596e2019-01-09 08:59:42 -07002464}
2465
2466/*
Jens Axboedef596e2019-01-09 08:59:42 -07002467 * We can't just wait for polled events to come to us, we have to actively
2468 * find and complete them.
2469 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002470static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002471{
2472 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2473 return;
2474
2475 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002476 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002477 /* let it sleep and repeat later if can't complete a request */
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002478 if (io_do_iopoll(ctx, true) == 0)
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002479 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002480 /*
2481 * Ensure we allow local-to-the-cpu processing to take place,
2482 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002483 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002484 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002485 if (need_resched()) {
2486 mutex_unlock(&ctx->uring_lock);
2487 cond_resched();
2488 mutex_lock(&ctx->uring_lock);
2489 }
Jens Axboedef596e2019-01-09 08:59:42 -07002490 }
2491 mutex_unlock(&ctx->uring_lock);
2492}
2493
Pavel Begunkov7668b922020-07-07 16:36:21 +03002494static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002495{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002496 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002497 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002498
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002499 /*
2500 * We disallow the app entering submit/complete with polling, but we
2501 * still need to lock the ring to prevent racing with polled issue
2502 * that got punted to a workqueue.
2503 */
2504 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002505 /*
2506 * Don't enter poll loop if we already have events pending.
2507 * If we do, we can potentially be spinning for commands that
2508 * already triggered a CQE (eg in error).
2509 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002510 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002511 __io_cqring_overflow_flush(ctx, false);
2512 if (io_cqring_events(ctx))
2513 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002514 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002515 /*
2516 * If a submit got punted to a workqueue, we can have the
2517 * application entering polling for a command before it gets
2518 * issued. That app will hold the uring_lock for the duration
2519 * of the poll right here, so we need to take a breather every
2520 * now and then to ensure that the issue has a chance to add
2521 * the poll to the issued list. Otherwise we can spin here
2522 * forever, while the workqueue is stuck trying to acquire the
2523 * very same mutex.
2524 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002525 if (wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002526 u32 tail = ctx->cached_cq_tail;
2527
Jens Axboe500f9fb2019-08-19 12:15:59 -06002528 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002529 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002530 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002531
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002532 /* some requests don't go through iopoll_list */
2533 if (tail != ctx->cached_cq_tail ||
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002534 wq_list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002535 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002536 }
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002537 ret = io_do_iopoll(ctx, !min);
2538 if (ret < 0)
2539 break;
2540 nr_events += ret;
2541 ret = 0;
2542 } while (nr_events < min && !need_resched());
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002543out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002544 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002545 return ret;
2546}
2547
Jens Axboe491381ce2019-10-17 09:20:46 -06002548static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002549{
Jens Axboe491381ce2019-10-17 09:20:46 -06002550 /*
2551 * Tell lockdep we inherited freeze protection from submission
2552 * thread.
2553 */
2554 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002555 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002556
Pavel Begunkov1c986792021-03-22 01:58:31 +00002557 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2558 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002559 }
2560}
2561
Jens Axboeb63534c2020-06-04 11:28:00 -06002562#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002563static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002564{
Pavel Begunkovab454432021-03-22 01:58:33 +00002565 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002566
Pavel Begunkovab454432021-03-22 01:58:33 +00002567 if (!rw)
2568 return !io_req_prep_async(req);
Jens Axboecd658692021-09-10 11:19:14 -06002569 iov_iter_restore(&rw->iter, &rw->iter_state);
Pavel Begunkovab454432021-03-22 01:58:33 +00002570 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002571}
Jens Axboeb63534c2020-06-04 11:28:00 -06002572
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002573static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002574{
Jens Axboe355afae2020-09-02 09:30:31 -06002575 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002576 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002577
Jens Axboe355afae2020-09-02 09:30:31 -06002578 if (!S_ISBLK(mode) && !S_ISREG(mode))
2579 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002580 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2581 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002582 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002583 /*
2584 * If ref is dying, we might be running poll reap from the exit work.
2585 * Don't attempt to reissue from that path, just let it fail with
2586 * -EAGAIN.
2587 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002588 if (percpu_ref_is_dying(&ctx->refs))
2589 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002590 /*
2591 * Play it safe and assume not safe to re-import and reissue if we're
2592 * not in the original thread group (or in task context).
2593 */
2594 if (!same_thread_group(req->task, current) || !in_task())
2595 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002596 return true;
2597}
Jens Axboee82ad482021-04-02 19:45:34 -06002598#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002599static bool io_resubmit_prep(struct io_kiocb *req)
2600{
2601 return false;
2602}
Jens Axboee82ad482021-04-02 19:45:34 -06002603static bool io_rw_should_reissue(struct io_kiocb *req)
2604{
2605 return false;
2606}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002607#endif
2608
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002609static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002610{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002611 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2612 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002613 if (res != req->result) {
2614 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2615 io_rw_should_reissue(req)) {
2616 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002617 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002618 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002619 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002620 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002621 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002622 return false;
2623}
2624
Pavel Begunkovf237c302021-08-18 12:42:46 +01002625static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002626{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002627 unsigned int cflags = io_put_rw_kbuf(req);
2628 long res = req->result;
2629
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002630 if (*locked) {
Pavel Begunkov126180b2021-08-18 12:42:47 +01002631 io_req_complete_state(req, res, cflags);
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002632 io_req_add_compl_list(req);
2633 } else {
Pavel Begunkov126180b2021-08-18 12:42:47 +01002634 io_req_complete_post(req, res, cflags);
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002635 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002636}
2637
2638static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2639 unsigned int issue_flags)
2640{
2641 if (__io_complete_rw_common(req, res))
2642 return;
Pavel Begunkov63637852021-09-02 00:38:22 +01002643 __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002644}
2645
2646static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2647{
Jens Axboe9adbd452019-12-20 08:45:55 -07002648 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002649
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002650 if (__io_complete_rw_common(req, res))
2651 return;
2652 req->result = res;
2653 req->io_task_work.func = io_req_task_complete;
2654 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002655}
2656
Jens Axboedef596e2019-01-09 08:59:42 -07002657static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2658{
Jens Axboe9adbd452019-12-20 08:45:55 -07002659 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002660
Jens Axboe491381ce2019-10-17 09:20:46 -06002661 if (kiocb->ki_flags & IOCB_WRITE)
2662 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002663 if (unlikely(res != req->result)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002664 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2665 req->flags |= REQ_F_REISSUE;
2666 return;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002667 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002668 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002669
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002670 req->result = res;
2671 /* order with io_iopoll_complete() checking ->iopoll_completed */
2672 smp_store_release(&req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002673}
2674
2675/*
2676 * After the iocb has been issued, it's safe to be found on the poll list.
2677 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002678 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002679 * accessing the kiocb cookie.
2680 */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002681static void io_iopoll_req_issued(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07002682{
2683 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002684 const bool in_async = io_wq_current_is_worker();
2685
2686 /* workqueue context doesn't hold uring_lock, grab it now */
2687 if (unlikely(in_async))
2688 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002689
2690 /*
2691 * Track whether we have multiple files in our lists. This will impact
2692 * how we do polling eventually, not spinning if we're on potentially
2693 * different devices.
2694 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002695 if (wq_list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002696 ctx->poll_multi_queue = false;
2697 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002698 struct io_kiocb *list_req;
2699
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002700 list_req = container_of(ctx->iopoll_list.first, struct io_kiocb,
2701 comp_list);
Christoph Hellwig30da1b42021-10-12 13:12:14 +02002702 if (list_req->file != req->file)
Hao Xu915b3dd2021-06-28 05:37:30 +08002703 ctx->poll_multi_queue = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002704 }
2705
2706 /*
2707 * For fast devices, IO may have already completed. If it has, add
2708 * it to the front so we find it first.
2709 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002710 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002711 wq_list_add_head(&req->comp_list, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002712 else
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002713 wq_list_add_tail(&req->comp_list, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002714
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002715 if (unlikely(in_async)) {
2716 /*
2717 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2718 * in sq thread task context or in io worker task context. If
2719 * current task context is sq thread, we don't need to check
2720 * whether should wake up sq thread.
2721 */
2722 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2723 wq_has_sleeper(&ctx->sq_data->wait))
2724 wake_up(&ctx->sq_data->wait);
2725
2726 mutex_unlock(&ctx->uring_lock);
2727 }
Jens Axboedef596e2019-01-09 08:59:42 -07002728}
2729
Jens Axboe4503b762020-06-01 10:00:27 -06002730static bool io_bdev_nowait(struct block_device *bdev)
2731{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002732 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002733}
2734
Jens Axboe2b188cc2019-01-07 10:46:33 -07002735/*
2736 * If we tracked the file through the SCM inflight mechanism, we could support
2737 * any file. For now, just ensure that anything potentially problematic is done
2738 * inline.
2739 */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002740static bool __io_file_supports_nowait(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002741{
2742 umode_t mode = file_inode(file)->i_mode;
2743
Jens Axboe4503b762020-06-01 10:00:27 -06002744 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002745 if (IS_ENABLED(CONFIG_BLOCK) &&
2746 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002747 return true;
2748 return false;
2749 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002750 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002751 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002752 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002753 if (IS_ENABLED(CONFIG_BLOCK) &&
2754 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002755 file->f_op != &io_uring_fops)
2756 return true;
2757 return false;
2758 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002759
Jens Axboec5b85622020-06-09 19:23:05 -06002760 /* any ->read/write should understand O_NONBLOCK */
2761 if (file->f_flags & O_NONBLOCK)
2762 return true;
2763
Jens Axboeaf197f52020-04-28 13:15:06 -06002764 if (!(file->f_mode & FMODE_NOWAIT))
2765 return false;
2766
2767 if (rw == READ)
2768 return file->f_op->read_iter != NULL;
2769
2770 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002771}
2772
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002773static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
Jens Axboe7b29f922021-03-12 08:30:14 -07002774{
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002775 if (rw == READ && (req->flags & REQ_F_NOWAIT_READ))
Jens Axboe7b29f922021-03-12 08:30:14 -07002776 return true;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002777 else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE))
Jens Axboe7b29f922021-03-12 08:30:14 -07002778 return true;
2779
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002780 return __io_file_supports_nowait(req->file, rw);
Jens Axboe7b29f922021-03-12 08:30:14 -07002781}
2782
Jens Axboe5d329e12021-09-14 11:08:37 -06002783static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2784 int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002785{
Jens Axboedef596e2019-01-09 08:59:42 -07002786 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002787 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002788 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002789 unsigned ioprio;
2790 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002791
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01002792 if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002793 req->flags |= REQ_F_ISREG;
2794
Jens Axboe2b188cc2019-01-07 10:46:33 -07002795 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002796 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002797 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002798 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002799 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002800 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002801 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2802 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2803 if (unlikely(ret))
2804 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002805
Jens Axboe5d329e12021-09-14 11:08:37 -06002806 /*
2807 * If the file is marked O_NONBLOCK, still allow retry for it if it
2808 * supports async. Otherwise it's impossible to use O_NONBLOCK files
2809 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2810 */
2811 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2812 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw)))
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002813 req->flags |= REQ_F_NOWAIT;
2814
Jens Axboe2b188cc2019-01-07 10:46:33 -07002815 ioprio = READ_ONCE(sqe->ioprio);
2816 if (ioprio) {
2817 ret = ioprio_check_cap(ioprio);
2818 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002819 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002820
2821 kiocb->ki_ioprio = ioprio;
2822 } else
2823 kiocb->ki_ioprio = get_current_ioprio();
2824
Jens Axboedef596e2019-01-09 08:59:42 -07002825 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002826 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2827 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002828 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002829
Jens Axboe394918e2021-03-08 11:40:23 -07002830 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
Jens Axboedef596e2019-01-09 08:59:42 -07002831 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002832 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002833 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002834 if (kiocb->ki_flags & IOCB_HIPRI)
2835 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002836 kiocb->ki_complete = io_complete_rw;
2837 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002838
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002839 if (req->opcode == IORING_OP_READ_FIXED ||
2840 req->opcode == IORING_OP_WRITE_FIXED) {
2841 req->imu = NULL;
2842 io_req_set_rsrc_node(req);
2843 }
2844
Jens Axboe3529d8c2019-12-19 18:24:38 -07002845 req->rw.addr = READ_ONCE(sqe->addr);
2846 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002847 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002849}
2850
2851static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2852{
2853 switch (ret) {
2854 case -EIOCBQUEUED:
2855 break;
2856 case -ERESTARTSYS:
2857 case -ERESTARTNOINTR:
2858 case -ERESTARTNOHAND:
2859 case -ERESTART_RESTARTBLOCK:
2860 /*
2861 * We can't just restart the syscall, since previously
2862 * submitted sqes may already be in progress. Just fail this
2863 * IO with EINTR.
2864 */
2865 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002866 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002867 default:
2868 kiocb->ki_complete(kiocb, ret, 0);
2869 }
2870}
2871
Jens Axboea1d7c392020-06-22 11:09:46 -06002872static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002873 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002874{
Jens Axboeba042912019-12-25 16:33:42 -07002875 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002876 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002877
Jens Axboe227c0c92020-08-13 11:51:40 -06002878 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002879 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002880 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002881 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002882 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002883 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002884 }
2885
Jens Axboeba042912019-12-25 16:33:42 -07002886 if (req->flags & REQ_F_CUR_POS)
2887 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002888 if (ret >= 0 && (kiocb->ki_complete == io_complete_rw))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002889 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002890 else
2891 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002892
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002893 if (req->flags & REQ_F_REISSUE) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002894 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002895 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002896 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002897 } else {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002898 unsigned int cflags = io_put_rw_kbuf(req);
2899 struct io_ring_ctx *ctx = req->ctx;
2900
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002901 req_set_fail(req);
Hao Xu14cfbb72021-10-14 22:04:00 +08002902 if (!(issue_flags & IO_URING_F_NONBLOCK)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002903 mutex_lock(&ctx->uring_lock);
2904 __io_req_complete(req, issue_flags, ret, cflags);
2905 mutex_unlock(&ctx->uring_lock);
2906 } else {
2907 __io_req_complete(req, issue_flags, ret, cflags);
2908 }
Pavel Begunkov97284632021-04-08 19:28:03 +01002909 }
2910 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002911}
2912
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002913static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2914 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002915{
Jens Axboe9adbd452019-12-20 08:45:55 -07002916 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002917 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002918 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002919
Pavel Begunkov75769e32021-04-01 15:43:54 +01002920 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002921 return -EFAULT;
2922 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002923 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002924 return -EFAULT;
2925
2926 /*
2927 * May not be a start of buffer, set size appropriately
2928 * and advance us to the beginning.
2929 */
2930 offset = buf_addr - imu->ubuf;
2931 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002932
2933 if (offset) {
2934 /*
2935 * Don't use iov_iter_advance() here, as it's really slow for
2936 * using the latter parts of a big fixed buffer - it iterates
2937 * over each segment manually. We can cheat a bit here, because
2938 * we know that:
2939 *
2940 * 1) it's a BVEC iter, we set it up
2941 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2942 * first and last bvec
2943 *
2944 * So just find our index, and adjust the iterator afterwards.
2945 * If the offset is within the first bvec (or the whole first
2946 * bvec, just use iov_iter_advance(). This makes it easier
2947 * since we can just skip the first segment, which may not
2948 * be PAGE_SIZE aligned.
2949 */
2950 const struct bio_vec *bvec = imu->bvec;
2951
2952 if (offset <= bvec->bv_len) {
2953 iov_iter_advance(iter, offset);
2954 } else {
2955 unsigned long seg_skip;
2956
2957 /* skip first vec */
2958 offset -= bvec->bv_len;
2959 seg_skip = 1 + (offset >> PAGE_SHIFT);
2960
2961 iter->bvec = bvec + seg_skip;
2962 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002963 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002964 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002965 }
2966 }
2967
Pavel Begunkov847595d2021-02-04 13:52:06 +00002968 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002969}
2970
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002971static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
2972{
2973 struct io_ring_ctx *ctx = req->ctx;
2974 struct io_mapped_ubuf *imu = req->imu;
2975 u16 index, buf_index = req->buf_index;
2976
2977 if (likely(!imu)) {
2978 if (unlikely(buf_index >= ctx->nr_user_bufs))
2979 return -EFAULT;
2980 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2981 imu = READ_ONCE(ctx->user_bufs[index]);
2982 req->imu = imu;
2983 }
2984 return __io_import_fixed(req, rw, iter, imu);
2985}
2986
Jens Axboebcda7ba2020-02-23 16:42:51 -07002987static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2988{
2989 if (needs_lock)
2990 mutex_unlock(&ctx->uring_lock);
2991}
2992
2993static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2994{
2995 /*
2996 * "Normal" inline submissions always hold the uring_lock, since we
2997 * grab it from the system call. Same is true for the SQPOLL offload.
2998 * The only exception is when we've detached the request and issue it
2999 * from an async worker thread, grab the lock for that case.
3000 */
3001 if (needs_lock)
3002 mutex_lock(&ctx->uring_lock);
3003}
3004
3005static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003006 int bgid, bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07003007{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003008 struct io_buffer *kbuf = req->kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003009 struct io_buffer *head;
3010
3011 if (req->flags & REQ_F_BUFFER_SELECTED)
3012 return kbuf;
3013
3014 io_ring_submit_lock(req->ctx, needs_lock);
3015
3016 lockdep_assert_held(&req->ctx->uring_lock);
3017
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003018 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003019 if (head) {
3020 if (!list_empty(&head->list)) {
3021 kbuf = list_last_entry(&head->list, struct io_buffer,
3022 list);
3023 list_del(&kbuf->list);
3024 } else {
3025 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003026 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003027 }
3028 if (*len > kbuf->len)
3029 *len = kbuf->len;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003030 req->flags |= REQ_F_BUFFER_SELECTED;
3031 req->kbuf = kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003032 } else {
3033 kbuf = ERR_PTR(-ENOBUFS);
3034 }
3035
3036 io_ring_submit_unlock(req->ctx, needs_lock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003037 return kbuf;
3038}
3039
Jens Axboe4d954c22020-02-27 07:31:19 -07003040static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3041 bool needs_lock)
3042{
3043 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003044 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003045
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003046 bgid = req->buf_index;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003047 kbuf = io_buffer_select(req, len, bgid, needs_lock);
Jens Axboe4d954c22020-02-27 07:31:19 -07003048 if (IS_ERR(kbuf))
3049 return kbuf;
Jens Axboe4d954c22020-02-27 07:31:19 -07003050 return u64_to_user_ptr(kbuf->addr);
3051}
3052
3053#ifdef CONFIG_COMPAT
3054static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3055 bool needs_lock)
3056{
3057 struct compat_iovec __user *uiov;
3058 compat_ssize_t clen;
3059 void __user *buf;
3060 ssize_t len;
3061
3062 uiov = u64_to_user_ptr(req->rw.addr);
3063 if (!access_ok(uiov, sizeof(*uiov)))
3064 return -EFAULT;
3065 if (__get_user(clen, &uiov->iov_len))
3066 return -EFAULT;
3067 if (clen < 0)
3068 return -EINVAL;
3069
3070 len = clen;
3071 buf = io_rw_buffer_select(req, &len, needs_lock);
3072 if (IS_ERR(buf))
3073 return PTR_ERR(buf);
3074 iov[0].iov_base = buf;
3075 iov[0].iov_len = (compat_size_t) len;
3076 return 0;
3077}
3078#endif
3079
3080static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3081 bool needs_lock)
3082{
3083 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3084 void __user *buf;
3085 ssize_t len;
3086
3087 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3088 return -EFAULT;
3089
3090 len = iov[0].iov_len;
3091 if (len < 0)
3092 return -EINVAL;
3093 buf = io_rw_buffer_select(req, &len, needs_lock);
3094 if (IS_ERR(buf))
3095 return PTR_ERR(buf);
3096 iov[0].iov_base = buf;
3097 iov[0].iov_len = len;
3098 return 0;
3099}
3100
3101static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3102 bool needs_lock)
3103{
Jens Axboedddb3e22020-06-04 11:27:01 -06003104 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003105 struct io_buffer *kbuf = req->kbuf;
Jens Axboedddb3e22020-06-04 11:27:01 -06003106
Jens Axboedddb3e22020-06-04 11:27:01 -06003107 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3108 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003109 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003110 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003111 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003112 return -EINVAL;
3113
3114#ifdef CONFIG_COMPAT
3115 if (req->ctx->compat)
3116 return io_compat_import(req, iov, needs_lock);
3117#endif
3118
3119 return __io_iov_buffer_select(req, iov, needs_lock);
3120}
3121
Pavel Begunkov847595d2021-02-04 13:52:06 +00003122static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3123 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003124{
Jens Axboe9adbd452019-12-20 08:45:55 -07003125 void __user *buf = u64_to_user_ptr(req->rw.addr);
3126 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003127 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003128 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003129
Pavel Begunkov7d009162019-11-25 23:14:40 +03003130 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003131 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003132 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003133 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003134
Jens Axboebcda7ba2020-02-23 16:42:51 -07003135 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003136 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003137 return -EINVAL;
3138
Jens Axboe3a6820f2019-12-22 15:19:35 -07003139 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003140 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003141 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003142 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003143 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003144 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003145 }
3146
Jens Axboe3a6820f2019-12-22 15:19:35 -07003147 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3148 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003149 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003150 }
3151
Jens Axboe4d954c22020-02-27 07:31:19 -07003152 if (req->flags & REQ_F_BUFFER_SELECT) {
3153 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003154 if (!ret)
3155 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003156 *iovec = NULL;
3157 return ret;
3158 }
3159
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003160 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3161 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003162}
3163
Jens Axboe0fef9482020-08-26 10:36:20 -06003164static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3165{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003166 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003167}
3168
Jens Axboe32960612019-09-23 11:05:34 -06003169/*
3170 * For files that don't have ->read_iter() and ->write_iter(), handle them
3171 * by looping over ->read() or ->write() manually.
3172 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003173static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003174{
Jens Axboe4017eb92020-10-22 14:14:12 -06003175 struct kiocb *kiocb = &req->rw.kiocb;
3176 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003177 ssize_t ret = 0;
3178
3179 /*
3180 * Don't support polled IO through this interface, and we can't
3181 * support non-blocking either. For the latter, this just causes
3182 * the kiocb to be handled from an async context.
3183 */
3184 if (kiocb->ki_flags & IOCB_HIPRI)
3185 return -EOPNOTSUPP;
3186 if (kiocb->ki_flags & IOCB_NOWAIT)
3187 return -EAGAIN;
3188
3189 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003190 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003191 ssize_t nr;
3192
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003193 if (!iov_iter_is_bvec(iter)) {
3194 iovec = iov_iter_iovec(iter);
3195 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003196 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3197 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003198 }
3199
Jens Axboe32960612019-09-23 11:05:34 -06003200 if (rw == READ) {
3201 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003202 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003203 } else {
3204 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003205 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003206 }
3207
3208 if (nr < 0) {
3209 if (!ret)
3210 ret = nr;
3211 break;
3212 }
Jens Axboe16c8d2d2021-09-12 06:45:07 -06003213 if (!iov_iter_is_bvec(iter)) {
3214 iov_iter_advance(iter, nr);
3215 } else {
3216 req->rw.len -= nr;
3217 req->rw.addr += nr;
3218 }
Jens Axboe32960612019-09-23 11:05:34 -06003219 ret += nr;
3220 if (nr != iovec.iov_len)
3221 break;
Jens Axboe32960612019-09-23 11:05:34 -06003222 }
3223
3224 return ret;
3225}
3226
Jens Axboeff6165b2020-08-13 09:47:43 -06003227static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3228 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003229{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003230 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003231
Jens Axboeff6165b2020-08-13 09:47:43 -06003232 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003233 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003234 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003235 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003236 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003237 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003238 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003239 unsigned iov_off = 0;
3240
3241 rw->iter.iov = rw->fast_iov;
3242 if (iter->iov != fast_iov) {
3243 iov_off = iter->iov - fast_iov;
3244 rw->iter.iov += iov_off;
3245 }
3246 if (rw->fast_iov != fast_iov)
3247 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003248 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003249 } else {
3250 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003251 }
3252}
3253
Hao Xu8d4af682021-09-22 18:15:22 +08003254static inline bool io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003255{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003256 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3257 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3258 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003259}
3260
Jens Axboeff6165b2020-08-13 09:47:43 -06003261static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3262 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003263 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003264{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003265 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003266 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003267 if (!req->async_data) {
Jens Axboecd658692021-09-10 11:19:14 -06003268 struct io_async_rw *iorw;
3269
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003270 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003271 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003272 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003273 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003274
Jens Axboeff6165b2020-08-13 09:47:43 -06003275 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboecd658692021-09-10 11:19:14 -06003276 iorw = req->async_data;
3277 /* we've copied and mapped the iter, ensure state is saved */
3278 iov_iter_save_state(&iorw->iter, &iorw->iter_state);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003279 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003280 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003281}
3282
Pavel Begunkov73debe62020-09-30 22:57:54 +03003283static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003284{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003285 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003286 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003287 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003288
Pavel Begunkov2846c482020-11-07 13:16:27 +00003289 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003290 if (unlikely(ret < 0))
3291 return ret;
3292
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003293 iorw->bytes_done = 0;
3294 iorw->free_iovec = iov;
3295 if (iov)
3296 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecd658692021-09-10 11:19:14 -06003297 iov_iter_save_state(&iorw->iter, &iorw->iter_state);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003298 return 0;
3299}
3300
Pavel Begunkov73debe62020-09-30 22:57:54 +03003301static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003302{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003303 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3304 return -EBADF;
Jens Axboe5d329e12021-09-14 11:08:37 -06003305 return io_prep_rw(req, sqe, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003306}
3307
Jens Axboec1dd91d2020-08-03 16:43:59 -06003308/*
3309 * This is our waitqueue callback handler, registered through lock_page_async()
3310 * when we initially tried to do the IO with the iocb armed our waitqueue.
3311 * This gets called when the page is unlocked, and we generally expect that to
3312 * happen when the page IO is completed and the page is now uptodate. This will
3313 * queue a task_work based retry of the operation, attempting to copy the data
3314 * again. If the latter fails because the page was NOT uptodate, then we will
3315 * do a thread based blocking retry of the operation. That's the unexpected
3316 * slow path.
3317 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003318static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3319 int sync, void *arg)
3320{
3321 struct wait_page_queue *wpq;
3322 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003323 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003324
3325 wpq = container_of(wait, struct wait_page_queue, wait);
3326
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003327 if (!wake_page_match(wpq, key))
3328 return 0;
3329
Hao Xuc8d317a2020-09-29 20:00:45 +08003330 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003331 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003332 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003333 return 1;
3334}
3335
Jens Axboec1dd91d2020-08-03 16:43:59 -06003336/*
3337 * This controls whether a given IO request should be armed for async page
3338 * based retry. If we return false here, the request is handed to the async
3339 * worker threads for retry. If we're doing buffered reads on a regular file,
3340 * we prepare a private wait_page_queue entry and retry the operation. This
3341 * will either succeed because the page is now uptodate and unlocked, or it
3342 * will register a callback when the page is unlocked at IO completion. Through
3343 * that callback, io_uring uses task_work to setup a retry of the operation.
3344 * That retry will attempt the buffered read again. The retry will generally
3345 * succeed, or in rare cases where it fails, we then fall back to using the
3346 * async worker threads for a blocking retry.
3347 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003348static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003349{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003350 struct io_async_rw *rw = req->async_data;
3351 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003352 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003353
3354 /* never retry for NOWAIT, we just complete with -EAGAIN */
3355 if (req->flags & REQ_F_NOWAIT)
3356 return false;
3357
Jens Axboe227c0c92020-08-13 11:51:40 -06003358 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003359 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003360 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003361
Jens Axboebcf5a062020-05-22 09:24:42 -06003362 /*
3363 * just use poll if we can, and don't attempt if the fs doesn't
3364 * support callback based unlocks
3365 */
3366 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3367 return false;
3368
Jens Axboe3b2a4432020-08-16 10:58:43 -07003369 wait->wait.func = io_async_buf_func;
3370 wait->wait.private = req;
3371 wait->wait.flags = 0;
3372 INIT_LIST_HEAD(&wait->wait.entry);
3373 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003374 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003375 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003376 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003377}
3378
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003379static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003380{
3381 if (req->file->f_op->read_iter)
3382 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003383 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003384 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003385 else
3386 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003387}
3388
Ming Lei7db30432021-08-21 23:07:51 +08003389static bool need_read_all(struct io_kiocb *req)
3390{
3391 return req->flags & REQ_F_ISREG ||
3392 S_ISBLK(file_inode(req->file)->i_mode);
3393}
3394
Pavel Begunkov889fca72021-02-10 00:03:09 +00003395static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003396{
3397 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003398 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003399 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003400 struct io_async_rw *rw = req->async_data;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003401 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003402 struct iov_iter_state __state, *state;
3403 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003404
Pavel Begunkov2846c482020-11-07 13:16:27 +00003405 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003406 iter = &rw->iter;
Jens Axboecd658692021-09-10 11:19:14 -06003407 state = &rw->iter_state;
3408 /*
3409 * We come here from an earlier attempt, restore our state to
3410 * match in case it doesn't. It's cheap enough that we don't
3411 * need to make this conditional.
3412 */
3413 iov_iter_restore(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003414 iovec = NULL;
3415 } else {
3416 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3417 if (ret < 0)
3418 return ret;
Jens Axboecd658692021-09-10 11:19:14 -06003419 state = &__state;
3420 iov_iter_save_state(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003421 }
Jens Axboecd658692021-09-10 11:19:14 -06003422 req->result = iov_iter_count(iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003423
Jens Axboefd6c2e42019-12-18 12:19:41 -07003424 /* Ensure we clear previously set non-block flag */
3425 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003426 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003427 else
3428 kiocb->ki_flags |= IOCB_NOWAIT;
3429
Pavel Begunkov24c74672020-06-21 13:09:51 +03003430 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003431 if (force_nonblock && !io_file_supports_nowait(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003432 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003433 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003434 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003435
Jens Axboecd658692021-09-10 11:19:14 -06003436 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003437 if (unlikely(ret)) {
3438 kfree(iovec);
3439 return ret;
3440 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003441
Jens Axboe227c0c92020-08-13 11:51:40 -06003442 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003443
Jens Axboe230d50d2021-04-01 20:41:15 -06003444 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003445 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003446 /* IOPOLL retry should happen for io-wq threads */
3447 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003448 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003449 /* no retry on NONBLOCK nor RWF_NOWAIT */
3450 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003451 goto done;
Jens Axboef38c7e32020-09-25 15:23:43 -06003452 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003453 } else if (ret == -EIOCBQUEUED) {
3454 goto out_free;
Jens Axboecd658692021-09-10 11:19:14 -06003455 } else if (ret <= 0 || ret == req->result || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003456 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003457 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003458 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003459 }
3460
Jens Axboecd658692021-09-10 11:19:14 -06003461 /*
3462 * Don't depend on the iter state matching what was consumed, or being
3463 * untouched in case of error. Restore it and we'll advance it
3464 * manually if we need to.
3465 */
3466 iov_iter_restore(iter, state);
3467
Jens Axboe227c0c92020-08-13 11:51:40 -06003468 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003469 if (ret2)
3470 return ret2;
3471
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003472 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003473 rw = req->async_data;
Jens Axboecd658692021-09-10 11:19:14 -06003474 /*
3475 * Now use our persistent iterator and state, if we aren't already.
3476 * We've restored and mapped the iter to match.
3477 */
3478 if (iter != &rw->iter) {
3479 iter = &rw->iter;
3480 state = &rw->iter_state;
3481 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003482
Pavel Begunkovb23df912021-02-04 13:52:04 +00003483 do {
Jens Axboecd658692021-09-10 11:19:14 -06003484 /*
3485 * We end up here because of a partial read, either from
3486 * above or inside this loop. Advance the iter by the bytes
3487 * that were consumed.
3488 */
3489 iov_iter_advance(iter, ret);
3490 if (!iov_iter_count(iter))
3491 break;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003492 rw->bytes_done += ret;
Jens Axboecd658692021-09-10 11:19:14 -06003493 iov_iter_save_state(iter, state);
3494
Pavel Begunkovb23df912021-02-04 13:52:04 +00003495 /* if we can retry, do so with the callbacks armed */
3496 if (!io_rw_should_retry(req)) {
3497 kiocb->ki_flags &= ~IOCB_WAITQ;
3498 return -EAGAIN;
3499 }
3500
3501 /*
3502 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3503 * we get -EIOCBQUEUED, then we'll get a notification when the
3504 * desired page gets unlocked. We can also get a partial read
3505 * here, and if we do, then just retry at the new offset.
3506 */
3507 ret = io_iter_do_read(req, iter);
3508 if (ret == -EIOCBQUEUED)
3509 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003510 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003511 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboecd658692021-09-10 11:19:14 -06003512 iov_iter_restore(iter, state);
3513 } while (ret > 0);
Jens Axboe227c0c92020-08-13 11:51:40 -06003514done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003515 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003516out_free:
3517 /* it's faster to check here then delegate to kfree */
3518 if (iovec)
3519 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003520 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003521}
3522
Pavel Begunkov73debe62020-09-30 22:57:54 +03003523static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003524{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003525 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3526 return -EBADF;
Jens Axboe5d329e12021-09-14 11:08:37 -06003527 return io_prep_rw(req, sqe, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003528}
3529
Pavel Begunkov889fca72021-02-10 00:03:09 +00003530static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003531{
3532 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003533 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003534 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003535 struct io_async_rw *rw = req->async_data;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003536 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003537 struct iov_iter_state __state, *state;
3538 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003539
Pavel Begunkov2846c482020-11-07 13:16:27 +00003540 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003541 iter = &rw->iter;
Jens Axboecd658692021-09-10 11:19:14 -06003542 state = &rw->iter_state;
3543 iov_iter_restore(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003544 iovec = NULL;
3545 } else {
3546 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3547 if (ret < 0)
3548 return ret;
Jens Axboecd658692021-09-10 11:19:14 -06003549 state = &__state;
3550 iov_iter_save_state(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003551 }
Jens Axboecd658692021-09-10 11:19:14 -06003552 req->result = iov_iter_count(iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003553
Jens Axboefd6c2e42019-12-18 12:19:41 -07003554 /* Ensure we clear previously set non-block flag */
3555 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003556 kiocb->ki_flags &= ~IOCB_NOWAIT;
3557 else
3558 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003559
Pavel Begunkov24c74672020-06-21 13:09:51 +03003560 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003561 if (force_nonblock && !io_file_supports_nowait(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003562 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003563
Jens Axboe10d59342019-12-09 20:16:22 -07003564 /* file path doesn't support NOWAIT for non-direct_IO */
3565 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3566 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003567 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003568
Jens Axboecd658692021-09-10 11:19:14 -06003569 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003570 if (unlikely(ret))
3571 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003572
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003573 /*
3574 * Open-code file_start_write here to grab freeze protection,
3575 * which will be released by another thread in
3576 * io_complete_rw(). Fool lockdep by telling it the lock got
3577 * released so that it doesn't complain about the held lock when
3578 * we return to userspace.
3579 */
3580 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003581 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003582 __sb_writers_release(file_inode(req->file)->i_sb,
3583 SB_FREEZE_WRITE);
3584 }
3585 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003586
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003587 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003588 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003589 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003590 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003591 else
3592 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003593
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003594 if (req->flags & REQ_F_REISSUE) {
3595 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003596 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003597 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003598
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003599 /*
3600 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3601 * retry them without IOCB_NOWAIT.
3602 */
3603 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3604 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003605 /* no retry on NONBLOCK nor RWF_NOWAIT */
3606 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003607 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003608 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003609 /* IOPOLL retry should happen for io-wq threads */
3610 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3611 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003612done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003613 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003614 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003615copy_iov:
Jens Axboecd658692021-09-10 11:19:14 -06003616 iov_iter_restore(iter, state);
Jens Axboe227c0c92020-08-13 11:51:40 -06003617 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003618 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003619 }
Jens Axboe31b51512019-01-18 22:56:34 -07003620out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003621 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003622 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003623 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003624 return ret;
3625}
3626
Jens Axboe80a261f2020-09-28 14:23:58 -06003627static int io_renameat_prep(struct io_kiocb *req,
3628 const struct io_uring_sqe *sqe)
3629{
3630 struct io_rename *ren = &req->rename;
3631 const char __user *oldf, *newf;
3632
Jens Axboeed7eb252021-06-23 09:04:13 -06003633 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3634 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003635 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003636 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003637 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3638 return -EBADF;
3639
3640 ren->old_dfd = READ_ONCE(sqe->fd);
3641 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3642 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3643 ren->new_dfd = READ_ONCE(sqe->len);
3644 ren->flags = READ_ONCE(sqe->rename_flags);
3645
3646 ren->oldpath = getname(oldf);
3647 if (IS_ERR(ren->oldpath))
3648 return PTR_ERR(ren->oldpath);
3649
3650 ren->newpath = getname(newf);
3651 if (IS_ERR(ren->newpath)) {
3652 putname(ren->oldpath);
3653 return PTR_ERR(ren->newpath);
3654 }
3655
3656 req->flags |= REQ_F_NEED_CLEANUP;
3657 return 0;
3658}
3659
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003660static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003661{
3662 struct io_rename *ren = &req->rename;
3663 int ret;
3664
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003665 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003666 return -EAGAIN;
3667
3668 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3669 ren->newpath, ren->flags);
3670
3671 req->flags &= ~REQ_F_NEED_CLEANUP;
3672 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003673 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003674 io_req_complete(req, ret);
3675 return 0;
3676}
3677
Jens Axboe14a11432020-09-28 14:27:37 -06003678static int io_unlinkat_prep(struct io_kiocb *req,
3679 const struct io_uring_sqe *sqe)
3680{
3681 struct io_unlink *un = &req->unlink;
3682 const char __user *fname;
3683
Jens Axboe22634bc2021-06-23 09:07:45 -06003684 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3685 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003686 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3687 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003688 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003689 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3690 return -EBADF;
3691
3692 un->dfd = READ_ONCE(sqe->fd);
3693
3694 un->flags = READ_ONCE(sqe->unlink_flags);
3695 if (un->flags & ~AT_REMOVEDIR)
3696 return -EINVAL;
3697
3698 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3699 un->filename = getname(fname);
3700 if (IS_ERR(un->filename))
3701 return PTR_ERR(un->filename);
3702
3703 req->flags |= REQ_F_NEED_CLEANUP;
3704 return 0;
3705}
3706
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003707static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003708{
3709 struct io_unlink *un = &req->unlink;
3710 int ret;
3711
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003712 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003713 return -EAGAIN;
3714
3715 if (un->flags & AT_REMOVEDIR)
3716 ret = do_rmdir(un->dfd, un->filename);
3717 else
3718 ret = do_unlinkat(un->dfd, un->filename);
3719
3720 req->flags &= ~REQ_F_NEED_CLEANUP;
3721 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003722 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003723 io_req_complete(req, ret);
3724 return 0;
3725}
3726
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003727static int io_mkdirat_prep(struct io_kiocb *req,
3728 const struct io_uring_sqe *sqe)
3729{
3730 struct io_mkdir *mkd = &req->mkdir;
3731 const char __user *fname;
3732
3733 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3734 return -EINVAL;
3735 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3736 sqe->splice_fd_in)
3737 return -EINVAL;
3738 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3739 return -EBADF;
3740
3741 mkd->dfd = READ_ONCE(sqe->fd);
3742 mkd->mode = READ_ONCE(sqe->len);
3743
3744 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3745 mkd->filename = getname(fname);
3746 if (IS_ERR(mkd->filename))
3747 return PTR_ERR(mkd->filename);
3748
3749 req->flags |= REQ_F_NEED_CLEANUP;
3750 return 0;
3751}
3752
3753static int io_mkdirat(struct io_kiocb *req, int issue_flags)
3754{
3755 struct io_mkdir *mkd = &req->mkdir;
3756 int ret;
3757
3758 if (issue_flags & IO_URING_F_NONBLOCK)
3759 return -EAGAIN;
3760
3761 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3762
3763 req->flags &= ~REQ_F_NEED_CLEANUP;
3764 if (ret < 0)
3765 req_set_fail(req);
3766 io_req_complete(req, ret);
3767 return 0;
3768}
3769
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003770static int io_symlinkat_prep(struct io_kiocb *req,
3771 const struct io_uring_sqe *sqe)
3772{
3773 struct io_symlink *sl = &req->symlink;
3774 const char __user *oldpath, *newpath;
3775
3776 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3777 return -EINVAL;
3778 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3779 sqe->splice_fd_in)
3780 return -EINVAL;
3781 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3782 return -EBADF;
3783
3784 sl->new_dfd = READ_ONCE(sqe->fd);
3785 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3786 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3787
3788 sl->oldpath = getname(oldpath);
3789 if (IS_ERR(sl->oldpath))
3790 return PTR_ERR(sl->oldpath);
3791
3792 sl->newpath = getname(newpath);
3793 if (IS_ERR(sl->newpath)) {
3794 putname(sl->oldpath);
3795 return PTR_ERR(sl->newpath);
3796 }
3797
3798 req->flags |= REQ_F_NEED_CLEANUP;
3799 return 0;
3800}
3801
3802static int io_symlinkat(struct io_kiocb *req, int issue_flags)
3803{
3804 struct io_symlink *sl = &req->symlink;
3805 int ret;
3806
3807 if (issue_flags & IO_URING_F_NONBLOCK)
3808 return -EAGAIN;
3809
3810 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3811
3812 req->flags &= ~REQ_F_NEED_CLEANUP;
3813 if (ret < 0)
3814 req_set_fail(req);
3815 io_req_complete(req, ret);
3816 return 0;
3817}
3818
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003819static int io_linkat_prep(struct io_kiocb *req,
3820 const struct io_uring_sqe *sqe)
3821{
3822 struct io_hardlink *lnk = &req->hardlink;
3823 const char __user *oldf, *newf;
3824
3825 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3826 return -EINVAL;
3827 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3828 return -EINVAL;
3829 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3830 return -EBADF;
3831
3832 lnk->old_dfd = READ_ONCE(sqe->fd);
3833 lnk->new_dfd = READ_ONCE(sqe->len);
3834 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3835 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3836 lnk->flags = READ_ONCE(sqe->hardlink_flags);
3837
3838 lnk->oldpath = getname(oldf);
3839 if (IS_ERR(lnk->oldpath))
3840 return PTR_ERR(lnk->oldpath);
3841
3842 lnk->newpath = getname(newf);
3843 if (IS_ERR(lnk->newpath)) {
3844 putname(lnk->oldpath);
3845 return PTR_ERR(lnk->newpath);
3846 }
3847
3848 req->flags |= REQ_F_NEED_CLEANUP;
3849 return 0;
3850}
3851
3852static int io_linkat(struct io_kiocb *req, int issue_flags)
3853{
3854 struct io_hardlink *lnk = &req->hardlink;
3855 int ret;
3856
3857 if (issue_flags & IO_URING_F_NONBLOCK)
3858 return -EAGAIN;
3859
3860 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
3861 lnk->newpath, lnk->flags);
3862
3863 req->flags &= ~REQ_F_NEED_CLEANUP;
3864 if (ret < 0)
3865 req_set_fail(req);
3866 io_req_complete(req, ret);
3867 return 0;
3868}
3869
Jens Axboe36f4fa62020-09-05 11:14:22 -06003870static int io_shutdown_prep(struct io_kiocb *req,
3871 const struct io_uring_sqe *sqe)
3872{
3873#if defined(CONFIG_NET)
3874 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3875 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003876 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3877 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003878 return -EINVAL;
3879
3880 req->shutdown.how = READ_ONCE(sqe->len);
3881 return 0;
3882#else
3883 return -EOPNOTSUPP;
3884#endif
3885}
3886
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003887static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003888{
3889#if defined(CONFIG_NET)
3890 struct socket *sock;
3891 int ret;
3892
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003893 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003894 return -EAGAIN;
3895
Linus Torvalds48aba792020-12-16 12:44:05 -08003896 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003897 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003898 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003899
3900 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003901 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003902 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003903 io_req_complete(req, ret);
3904 return 0;
3905#else
3906 return -EOPNOTSUPP;
3907#endif
3908}
3909
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003910static int __io_splice_prep(struct io_kiocb *req,
3911 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003912{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003913 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003914 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003915
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003916 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3917 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003918
3919 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003920 sp->len = READ_ONCE(sqe->len);
3921 sp->flags = READ_ONCE(sqe->splice_flags);
3922
3923 if (unlikely(sp->flags & ~valid_flags))
3924 return -EINVAL;
3925
Pavel Begunkov62906e82021-08-10 14:52:47 +01003926 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003927 (sp->flags & SPLICE_F_FD_IN_FIXED));
3928 if (!sp->file_in)
3929 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003930 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003931 return 0;
3932}
3933
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003934static int io_tee_prep(struct io_kiocb *req,
3935 const struct io_uring_sqe *sqe)
3936{
3937 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3938 return -EINVAL;
3939 return __io_splice_prep(req, sqe);
3940}
3941
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003942static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003943{
3944 struct io_splice *sp = &req->splice;
3945 struct file *in = sp->file_in;
3946 struct file *out = sp->file_out;
3947 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3948 long ret = 0;
3949
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003950 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003951 return -EAGAIN;
3952 if (sp->len)
3953 ret = do_tee(in, out, sp->len, flags);
3954
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003955 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3956 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003957 req->flags &= ~REQ_F_NEED_CLEANUP;
3958
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003959 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003960 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003961 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003962 return 0;
3963}
3964
3965static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3966{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003967 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003968
3969 sp->off_in = READ_ONCE(sqe->splice_off_in);
3970 sp->off_out = READ_ONCE(sqe->off);
3971 return __io_splice_prep(req, sqe);
3972}
3973
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003974static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003975{
3976 struct io_splice *sp = &req->splice;
3977 struct file *in = sp->file_in;
3978 struct file *out = sp->file_out;
3979 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3980 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003981 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003982
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003983 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003984 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003985
3986 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3987 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003988
Jens Axboe948a7742020-05-17 14:21:38 -06003989 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003990 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003991
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003992 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3993 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003994 req->flags &= ~REQ_F_NEED_CLEANUP;
3995
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003996 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003997 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003998 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003999 return 0;
4000}
4001
Jens Axboe2b188cc2019-01-07 10:46:33 -07004002/*
4003 * IORING_OP_NOP just posts a completion event, nothing else.
4004 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004005static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004006{
4007 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004008
Jens Axboedef596e2019-01-09 08:59:42 -07004009 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4010 return -EINVAL;
4011
Pavel Begunkov889fca72021-02-10 00:03:09 +00004012 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004013 return 0;
4014}
4015
Pavel Begunkov1155c762021-02-18 18:29:38 +00004016static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004017{
Jens Axboe6b063142019-01-10 22:13:58 -07004018 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004019
Jens Axboe09bb8392019-03-13 12:39:28 -06004020 if (!req->file)
4021 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004022
Jens Axboe6b063142019-01-10 22:13:58 -07004023 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004024 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004025 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4026 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004027 return -EINVAL;
4028
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004029 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4030 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4031 return -EINVAL;
4032
4033 req->sync.off = READ_ONCE(sqe->off);
4034 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004035 return 0;
4036}
4037
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004038static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004039{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004040 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004041 int ret;
4042
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004043 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004044 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004045 return -EAGAIN;
4046
Jens Axboe9adbd452019-12-20 08:45:55 -07004047 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004048 end > 0 ? end : LLONG_MAX,
4049 req->sync.flags & IORING_FSYNC_DATASYNC);
4050 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004051 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004052 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004053 return 0;
4054}
4055
Jens Axboed63d1b52019-12-10 10:38:56 -07004056static int io_fallocate_prep(struct io_kiocb *req,
4057 const struct io_uring_sqe *sqe)
4058{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004059 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4060 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004061 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004062 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4063 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004064
4065 req->sync.off = READ_ONCE(sqe->off);
4066 req->sync.len = READ_ONCE(sqe->addr);
4067 req->sync.mode = READ_ONCE(sqe->len);
4068 return 0;
4069}
4070
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004071static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004072{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004073 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004074
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004075 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004076 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004077 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004078 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4079 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004080 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004081 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004082 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004083 return 0;
4084}
4085
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004086static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004087{
Jens Axboef8748882020-01-08 17:47:02 -07004088 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004089 int ret;
4090
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004091 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4092 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004093 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004094 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004095 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004096 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004097
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004098 /* open.how should be already initialised */
4099 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004100 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004101
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004102 req->open.dfd = READ_ONCE(sqe->fd);
4103 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004104 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004105 if (IS_ERR(req->open.filename)) {
4106 ret = PTR_ERR(req->open.filename);
4107 req->open.filename = NULL;
4108 return ret;
4109 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004110
4111 req->open.file_slot = READ_ONCE(sqe->file_index);
4112 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4113 return -EINVAL;
4114
Jens Axboe4022e7a2020-03-19 19:23:18 -06004115 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004116 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004117 return 0;
4118}
4119
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004120static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4121{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004122 u64 mode = READ_ONCE(sqe->len);
4123 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004124
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004125 req->open.how = build_open_how(flags, mode);
4126 return __io_openat_prep(req, sqe);
4127}
4128
Jens Axboecebdb982020-01-08 17:59:24 -07004129static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4130{
4131 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004132 size_t len;
4133 int ret;
4134
Jens Axboecebdb982020-01-08 17:59:24 -07004135 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4136 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004137 if (len < OPEN_HOW_SIZE_VER0)
4138 return -EINVAL;
4139
4140 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4141 len);
4142 if (ret)
4143 return ret;
4144
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004145 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004146}
4147
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004148static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004149{
4150 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004151 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004152 bool resolve_nonblock, nonblock_set;
4153 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004154 int ret;
4155
Jens Axboecebdb982020-01-08 17:59:24 -07004156 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004157 if (ret)
4158 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004159 nonblock_set = op.open_flag & O_NONBLOCK;
4160 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004161 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004162 /*
4163 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4164 * it'll always -EAGAIN
4165 */
4166 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4167 return -EAGAIN;
4168 op.lookup_flags |= LOOKUP_CACHED;
4169 op.open_flag |= O_NONBLOCK;
4170 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004171
Pavel Begunkovb9445592021-08-25 12:25:45 +01004172 if (!fixed) {
4173 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4174 if (ret < 0)
4175 goto err;
4176 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004177
4178 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004179 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004180 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004181 * We could hang on to this 'fd' on retrying, but seems like
4182 * marginal gain for something that is now known to be a slower
4183 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004184 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004185 if (!fixed)
4186 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004187
4188 ret = PTR_ERR(file);
4189 /* only retry if RESOLVE_CACHED wasn't already set by application */
4190 if (ret == -EAGAIN &&
4191 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4192 return -EAGAIN;
4193 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004194 }
4195
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004196 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4197 file->f_flags &= ~O_NONBLOCK;
4198 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004199
4200 if (!fixed)
4201 fd_install(ret, file);
4202 else
4203 ret = io_install_fixed_file(req, file, issue_flags,
4204 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004205err:
4206 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004207 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004208 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004209 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004210 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004211 return 0;
4212}
4213
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004214static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004215{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004216 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004217}
4218
Jens Axboe067524e2020-03-02 16:32:28 -07004219static int io_remove_buffers_prep(struct io_kiocb *req,
4220 const struct io_uring_sqe *sqe)
4221{
4222 struct io_provide_buf *p = &req->pbuf;
4223 u64 tmp;
4224
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004225 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4226 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004227 return -EINVAL;
4228
4229 tmp = READ_ONCE(sqe->fd);
4230 if (!tmp || tmp > USHRT_MAX)
4231 return -EINVAL;
4232
4233 memset(p, 0, sizeof(*p));
4234 p->nbufs = tmp;
4235 p->bgid = READ_ONCE(sqe->buf_group);
4236 return 0;
4237}
4238
4239static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4240 int bgid, unsigned nbufs)
4241{
4242 unsigned i = 0;
4243
4244 /* shouldn't happen */
4245 if (!nbufs)
4246 return 0;
4247
4248 /* the head kbuf is the list itself */
4249 while (!list_empty(&buf->list)) {
4250 struct io_buffer *nxt;
4251
4252 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4253 list_del(&nxt->list);
4254 kfree(nxt);
4255 if (++i == nbufs)
4256 return i;
4257 }
4258 i++;
4259 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004260 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004261
4262 return i;
4263}
4264
Pavel Begunkov889fca72021-02-10 00:03:09 +00004265static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004266{
4267 struct io_provide_buf *p = &req->pbuf;
4268 struct io_ring_ctx *ctx = req->ctx;
4269 struct io_buffer *head;
4270 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004271 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004272
4273 io_ring_submit_lock(ctx, !force_nonblock);
4274
4275 lockdep_assert_held(&ctx->uring_lock);
4276
4277 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004278 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004279 if (head)
4280 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004281 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004282 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004283
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004284 /* complete before unlock, IOPOLL may need the lock */
4285 __io_req_complete(req, issue_flags, ret, 0);
4286 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07004287 return 0;
4288}
4289
Jens Axboeddf0322d2020-02-23 16:41:33 -07004290static int io_provide_buffers_prep(struct io_kiocb *req,
4291 const struct io_uring_sqe *sqe)
4292{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004293 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004294 struct io_provide_buf *p = &req->pbuf;
4295 u64 tmp;
4296
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004297 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004298 return -EINVAL;
4299
4300 tmp = READ_ONCE(sqe->fd);
4301 if (!tmp || tmp > USHRT_MAX)
4302 return -E2BIG;
4303 p->nbufs = tmp;
4304 p->addr = READ_ONCE(sqe->addr);
4305 p->len = READ_ONCE(sqe->len);
4306
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004307 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4308 &size))
4309 return -EOVERFLOW;
4310 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4311 return -EOVERFLOW;
4312
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004313 size = (unsigned long)p->len * p->nbufs;
4314 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004315 return -EFAULT;
4316
4317 p->bgid = READ_ONCE(sqe->buf_group);
4318 tmp = READ_ONCE(sqe->off);
4319 if (tmp > USHRT_MAX)
4320 return -E2BIG;
4321 p->bid = tmp;
4322 return 0;
4323}
4324
4325static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4326{
4327 struct io_buffer *buf;
4328 u64 addr = pbuf->addr;
4329 int i, bid = pbuf->bid;
4330
4331 for (i = 0; i < pbuf->nbufs; i++) {
Jens Axboe9990da92021-09-24 07:39:08 -06004332 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004333 if (!buf)
4334 break;
4335
4336 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004337 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004338 buf->bid = bid;
4339 addr += pbuf->len;
4340 bid++;
4341 if (!*head) {
4342 INIT_LIST_HEAD(&buf->list);
4343 *head = buf;
4344 } else {
4345 list_add_tail(&buf->list, &(*head)->list);
4346 }
4347 }
4348
4349 return i ? i : -ENOMEM;
4350}
4351
Pavel Begunkov889fca72021-02-10 00:03:09 +00004352static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004353{
4354 struct io_provide_buf *p = &req->pbuf;
4355 struct io_ring_ctx *ctx = req->ctx;
4356 struct io_buffer *head, *list;
4357 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004358 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004359
4360 io_ring_submit_lock(ctx, !force_nonblock);
4361
4362 lockdep_assert_held(&ctx->uring_lock);
4363
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004364 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004365
4366 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004367 if (ret >= 0 && !list) {
4368 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4369 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004370 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004371 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004372 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004373 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004374 /* complete before unlock, IOPOLL may need the lock */
4375 __io_req_complete(req, issue_flags, ret, 0);
4376 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004377 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004378}
4379
Jens Axboe3e4827b2020-01-08 15:18:09 -07004380static int io_epoll_ctl_prep(struct io_kiocb *req,
4381 const struct io_uring_sqe *sqe)
4382{
4383#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004384 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004385 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004386 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004387 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004388
4389 req->epoll.epfd = READ_ONCE(sqe->fd);
4390 req->epoll.op = READ_ONCE(sqe->len);
4391 req->epoll.fd = READ_ONCE(sqe->off);
4392
4393 if (ep_op_has_event(req->epoll.op)) {
4394 struct epoll_event __user *ev;
4395
4396 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4397 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4398 return -EFAULT;
4399 }
4400
4401 return 0;
4402#else
4403 return -EOPNOTSUPP;
4404#endif
4405}
4406
Pavel Begunkov889fca72021-02-10 00:03:09 +00004407static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004408{
4409#if defined(CONFIG_EPOLL)
4410 struct io_epoll *ie = &req->epoll;
4411 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004412 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004413
4414 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4415 if (force_nonblock && ret == -EAGAIN)
4416 return -EAGAIN;
4417
4418 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004419 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004420 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004421 return 0;
4422#else
4423 return -EOPNOTSUPP;
4424#endif
4425}
4426
Jens Axboec1ca7572019-12-25 22:18:28 -07004427static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4428{
4429#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004430 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004431 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004432 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4433 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004434
4435 req->madvise.addr = READ_ONCE(sqe->addr);
4436 req->madvise.len = READ_ONCE(sqe->len);
4437 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4438 return 0;
4439#else
4440 return -EOPNOTSUPP;
4441#endif
4442}
4443
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004444static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004445{
4446#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4447 struct io_madvise *ma = &req->madvise;
4448 int ret;
4449
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004450 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004451 return -EAGAIN;
4452
Minchan Kim0726b012020-10-17 16:14:50 -07004453 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004454 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004455 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004456 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004457 return 0;
4458#else
4459 return -EOPNOTSUPP;
4460#endif
4461}
4462
Jens Axboe4840e412019-12-25 22:03:45 -07004463static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4464{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004465 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004466 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004467 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4468 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004469
4470 req->fadvise.offset = READ_ONCE(sqe->off);
4471 req->fadvise.len = READ_ONCE(sqe->len);
4472 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4473 return 0;
4474}
4475
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004476static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004477{
4478 struct io_fadvise *fa = &req->fadvise;
4479 int ret;
4480
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004481 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004482 switch (fa->advice) {
4483 case POSIX_FADV_NORMAL:
4484 case POSIX_FADV_RANDOM:
4485 case POSIX_FADV_SEQUENTIAL:
4486 break;
4487 default:
4488 return -EAGAIN;
4489 }
4490 }
Jens Axboe4840e412019-12-25 22:03:45 -07004491
4492 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4493 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004494 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004495 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004496 return 0;
4497}
4498
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004499static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4500{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004501 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004502 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004503 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004504 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004505 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004506 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004507
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004508 req->statx.dfd = READ_ONCE(sqe->fd);
4509 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004510 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004511 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4512 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004513
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004514 return 0;
4515}
4516
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004517static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004518{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004519 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004520 int ret;
4521
Pavel Begunkov59d70012021-03-22 01:58:30 +00004522 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004523 return -EAGAIN;
4524
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004525 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4526 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004527
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004528 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004529 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004530 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004531 return 0;
4532}
4533
Jens Axboeb5dba592019-12-11 14:02:38 -07004534static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4535{
Jens Axboe14587a462020-09-05 11:36:08 -06004536 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004537 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004538 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004539 sqe->rw_flags || sqe->buf_index)
Jens Axboeb5dba592019-12-11 14:02:38 -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 Axboeb5dba592019-12-11 14:02:38 -07004543
4544 req->close.fd = READ_ONCE(sqe->fd);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004545 req->close.file_slot = READ_ONCE(sqe->file_index);
4546 if (req->close.file_slot && req->close.fd)
4547 return -EINVAL;
4548
Jens Axboeb5dba592019-12-11 14:02:38 -07004549 return 0;
4550}
4551
Pavel Begunkov889fca72021-02-10 00:03:09 +00004552static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004553{
Jens Axboe9eac1902021-01-19 15:50:37 -07004554 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004555 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004556 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004557 struct file *file = NULL;
4558 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004559
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004560 if (req->close.file_slot) {
4561 ret = io_close_fixed(req, issue_flags);
4562 goto err;
4563 }
4564
Jens Axboe9eac1902021-01-19 15:50:37 -07004565 spin_lock(&files->file_lock);
4566 fdt = files_fdtable(files);
4567 if (close->fd >= fdt->max_fds) {
4568 spin_unlock(&files->file_lock);
4569 goto err;
4570 }
4571 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004572 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004573 spin_unlock(&files->file_lock);
4574 file = NULL;
4575 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004576 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004577
4578 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004579 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004580 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004581 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004582 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004583
Jens Axboe9eac1902021-01-19 15:50:37 -07004584 ret = __close_fd_get_file(close->fd, &file);
4585 spin_unlock(&files->file_lock);
4586 if (ret < 0) {
4587 if (ret == -ENOENT)
4588 ret = -EBADF;
4589 goto err;
4590 }
4591
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004592 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004593 ret = filp_close(file, current->files);
4594err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004595 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004596 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004597 if (file)
4598 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004599 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004600 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004601}
4602
Pavel Begunkov1155c762021-02-18 18:29:38 +00004603static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004604{
4605 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004606
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004607 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4608 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004609 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4610 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004611 return -EINVAL;
4612
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004613 req->sync.off = READ_ONCE(sqe->off);
4614 req->sync.len = READ_ONCE(sqe->len);
4615 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004616 return 0;
4617}
4618
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004619static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004620{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004621 int ret;
4622
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004623 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004624 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004625 return -EAGAIN;
4626
Jens Axboe9adbd452019-12-20 08:45:55 -07004627 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004628 req->sync.flags);
4629 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004630 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004631 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004632 return 0;
4633}
4634
YueHaibing469956e2020-03-04 15:53:52 +08004635#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004636static int io_setup_async_msg(struct io_kiocb *req,
4637 struct io_async_msghdr *kmsg)
4638{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004639 struct io_async_msghdr *async_msg = req->async_data;
4640
4641 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004642 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004643 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004644 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004645 return -ENOMEM;
4646 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004647 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004648 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004649 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004650 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004651 /* if were using fast_iov, set it to the new one */
4652 if (!async_msg->free_iov)
4653 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4654
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004655 return -EAGAIN;
4656}
4657
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004658static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4659 struct io_async_msghdr *iomsg)
4660{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004661 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004662 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004663 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004664 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004665}
4666
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004667static int io_sendmsg_prep_async(struct io_kiocb *req)
4668{
4669 int ret;
4670
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004671 ret = io_sendmsg_copy_hdr(req, req->async_data);
4672 if (!ret)
4673 req->flags |= REQ_F_NEED_CLEANUP;
4674 return ret;
4675}
4676
Jens Axboe3529d8c2019-12-19 18:24:38 -07004677static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004678{
Jens Axboee47293f2019-12-20 08:58:21 -07004679 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004680
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004681 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4682 return -EINVAL;
4683
Pavel Begunkov270a5942020-07-12 20:41:04 +03004684 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004685 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004686 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4687 if (sr->msg_flags & MSG_DONTWAIT)
4688 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004689
Jens Axboed8768362020-02-27 14:17:49 -07004690#ifdef CONFIG_COMPAT
4691 if (req->ctx->compat)
4692 sr->msg_flags |= MSG_CMSG_COMPAT;
4693#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004694 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004695}
4696
Pavel Begunkov889fca72021-02-10 00:03:09 +00004697static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004698{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004699 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004700 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004701 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004702 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004703 int ret;
4704
Florent Revestdba4a922020-12-04 12:36:04 +01004705 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004706 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004707 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004708
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004709 kmsg = req->async_data;
4710 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004711 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004712 if (ret)
4713 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004714 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004715 }
4716
Pavel Begunkov04411802021-04-01 15:44:00 +01004717 flags = req->sr_msg.msg_flags;
4718 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004719 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004720 if (flags & MSG_WAITALL)
4721 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4722
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004723 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004724 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004725 return io_setup_async_msg(req, kmsg);
4726 if (ret == -ERESTARTSYS)
4727 ret = -EINTR;
4728
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004729 /* fast path, check for non-NULL to avoid function call */
4730 if (kmsg->free_iov)
4731 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004732 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004733 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004734 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004735 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004736 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004737}
4738
Pavel Begunkov889fca72021-02-10 00:03:09 +00004739static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004740{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004741 struct io_sr_msg *sr = &req->sr_msg;
4742 struct msghdr msg;
4743 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004744 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004745 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004746 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004747 int ret;
4748
Florent Revestdba4a922020-12-04 12:36:04 +01004749 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004750 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004751 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004752
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004753 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4754 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004755 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004756
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004757 msg.msg_name = NULL;
4758 msg.msg_control = NULL;
4759 msg.msg_controllen = 0;
4760 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004761
Pavel Begunkov04411802021-04-01 15:44:00 +01004762 flags = req->sr_msg.msg_flags;
4763 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004764 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004765 if (flags & MSG_WAITALL)
4766 min_ret = iov_iter_count(&msg.msg_iter);
4767
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004768 msg.msg_flags = flags;
4769 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004770 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004771 return -EAGAIN;
4772 if (ret == -ERESTARTSYS)
4773 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004774
Stefan Metzmacher00312752021-03-20 20:33:36 +01004775 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004776 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004777 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004778 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004779}
4780
Pavel Begunkov1400e692020-07-12 20:41:05 +03004781static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4782 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004783{
4784 struct io_sr_msg *sr = &req->sr_msg;
4785 struct iovec __user *uiov;
4786 size_t iov_len;
4787 int ret;
4788
Pavel Begunkov1400e692020-07-12 20:41:05 +03004789 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4790 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004791 if (ret)
4792 return ret;
4793
4794 if (req->flags & REQ_F_BUFFER_SELECT) {
4795 if (iov_len > 1)
4796 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004797 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004798 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004799 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004800 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004801 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004802 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004803 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004804 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004805 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004806 if (ret > 0)
4807 ret = 0;
4808 }
4809
4810 return ret;
4811}
4812
4813#ifdef CONFIG_COMPAT
4814static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004815 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004816{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004817 struct io_sr_msg *sr = &req->sr_msg;
4818 struct compat_iovec __user *uiov;
4819 compat_uptr_t ptr;
4820 compat_size_t len;
4821 int ret;
4822
Pavel Begunkov4af34172021-04-11 01:46:30 +01004823 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4824 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004825 if (ret)
4826 return ret;
4827
4828 uiov = compat_ptr(ptr);
4829 if (req->flags & REQ_F_BUFFER_SELECT) {
4830 compat_ssize_t clen;
4831
4832 if (len > 1)
4833 return -EINVAL;
4834 if (!access_ok(uiov, sizeof(*uiov)))
4835 return -EFAULT;
4836 if (__get_user(clen, &uiov->iov_len))
4837 return -EFAULT;
4838 if (clen < 0)
4839 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004840 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004841 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004842 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004843 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004844 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004845 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004846 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004847 if (ret < 0)
4848 return ret;
4849 }
4850
4851 return 0;
4852}
Jens Axboe03b12302019-12-02 18:50:25 -07004853#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004854
Pavel Begunkov1400e692020-07-12 20:41:05 +03004855static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4856 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004857{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004858 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004859
4860#ifdef CONFIG_COMPAT
4861 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004862 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004863#endif
4864
Pavel Begunkov1400e692020-07-12 20:41:05 +03004865 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004866}
4867
Jens Axboebcda7ba2020-02-23 16:42:51 -07004868static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004869 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004870{
4871 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004872
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01004873 return io_buffer_select(req, &sr->len, sr->bgid, needs_lock);
Jens Axboe03b12302019-12-02 18:50:25 -07004874}
4875
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004876static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4877{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01004878 return io_put_kbuf(req, req->kbuf);
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004879}
4880
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004881static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004882{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004883 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004884
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004885 ret = io_recvmsg_copy_hdr(req, req->async_data);
4886 if (!ret)
4887 req->flags |= REQ_F_NEED_CLEANUP;
4888 return ret;
4889}
4890
4891static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4892{
4893 struct io_sr_msg *sr = &req->sr_msg;
4894
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004895 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4896 return -EINVAL;
4897
Pavel Begunkov270a5942020-07-12 20:41:04 +03004898 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004899 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004900 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004901 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4902 if (sr->msg_flags & MSG_DONTWAIT)
4903 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004904
Jens Axboed8768362020-02-27 14:17:49 -07004905#ifdef CONFIG_COMPAT
4906 if (req->ctx->compat)
4907 sr->msg_flags |= MSG_CMSG_COMPAT;
4908#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004909 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004910}
4911
Pavel Begunkov889fca72021-02-10 00:03:09 +00004912static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004913{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004914 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004915 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004916 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004917 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004918 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004919 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004920 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004921
Florent Revestdba4a922020-12-04 12:36:04 +01004922 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004923 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004924 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004925
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004926 kmsg = req->async_data;
4927 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004928 ret = io_recvmsg_copy_hdr(req, &iomsg);
4929 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004930 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004931 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004932 }
4933
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004934 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004935 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004936 if (IS_ERR(kbuf))
4937 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004938 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004939 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4940 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004941 1, req->sr_msg.len);
4942 }
4943
Pavel Begunkov04411802021-04-01 15:44:00 +01004944 flags = req->sr_msg.msg_flags;
4945 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004946 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004947 if (flags & MSG_WAITALL)
4948 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4949
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004950 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4951 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004952 if (force_nonblock && ret == -EAGAIN)
4953 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004954 if (ret == -ERESTARTSYS)
4955 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004956
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004957 if (req->flags & REQ_F_BUFFER_SELECTED)
4958 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004959 /* fast path, check for non-NULL to avoid function call */
4960 if (kmsg->free_iov)
4961 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004962 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004963 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004964 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004965 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004966 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004967}
4968
Pavel Begunkov889fca72021-02-10 00:03:09 +00004969static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004970{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004971 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004972 struct io_sr_msg *sr = &req->sr_msg;
4973 struct msghdr msg;
4974 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004975 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004976 struct iovec iov;
4977 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004978 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004979 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004980 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004981
Florent Revestdba4a922020-12-04 12:36:04 +01004982 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004983 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004984 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004985
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004986 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004987 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004988 if (IS_ERR(kbuf))
4989 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004990 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004991 }
4992
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004993 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004994 if (unlikely(ret))
4995 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004996
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004997 msg.msg_name = NULL;
4998 msg.msg_control = NULL;
4999 msg.msg_controllen = 0;
5000 msg.msg_namelen = 0;
5001 msg.msg_iocb = NULL;
5002 msg.msg_flags = 0;
5003
Pavel Begunkov04411802021-04-01 15:44:00 +01005004 flags = req->sr_msg.msg_flags;
5005 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005006 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005007 if (flags & MSG_WAITALL)
5008 min_ret = iov_iter_count(&msg.msg_iter);
5009
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005010 ret = sock_recvmsg(sock, &msg, flags);
5011 if (force_nonblock && ret == -EAGAIN)
5012 return -EAGAIN;
5013 if (ret == -ERESTARTSYS)
5014 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005015out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005016 if (req->flags & REQ_F_BUFFER_SELECTED)
5017 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01005018 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005019 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005020 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005021 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005022}
5023
Jens Axboe3529d8c2019-12-19 18:24:38 -07005024static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005025{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005026 struct io_accept *accept = &req->accept;
5027
Jens Axboe14587a462020-09-05 11:36:08 -06005028 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005029 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005030 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005031 return -EINVAL;
5032
Jens Axboed55e5f52019-12-11 16:12:15 -07005033 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5034 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005035 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005036 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005037
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005038 accept->file_slot = READ_ONCE(sqe->file_index);
5039 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5040 (accept->flags & SOCK_CLOEXEC)))
5041 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005042 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5043 return -EINVAL;
5044 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5045 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005046 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005047}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005048
Pavel Begunkov889fca72021-02-10 00:03:09 +00005049static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005050{
5051 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005052 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005053 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005054 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005055 struct file *file;
5056 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005057
Jiufei Xuee697dee2020-06-10 13:41:59 +08005058 if (req->file->f_flags & O_NONBLOCK)
5059 req->flags |= REQ_F_NOWAIT;
5060
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005061 if (!fixed) {
5062 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5063 if (unlikely(fd < 0))
5064 return fd;
5065 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005066 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5067 accept->flags);
5068 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005069 if (!fixed)
5070 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005071 ret = PTR_ERR(file);
5072 if (ret == -EAGAIN && force_nonblock)
5073 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005074 if (ret == -ERESTARTSYS)
5075 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005076 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005077 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005078 fd_install(fd, file);
5079 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005080 } else {
5081 ret = io_install_fixed_file(req, file, issue_flags,
5082 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005083 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005084 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005085 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005086}
5087
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005088static int io_connect_prep_async(struct io_kiocb *req)
5089{
5090 struct io_async_connect *io = req->async_data;
5091 struct io_connect *conn = &req->connect;
5092
5093 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5094}
5095
Jens Axboe3529d8c2019-12-19 18:24:38 -07005096static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005097{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005098 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005099
Jens Axboe14587a462020-09-05 11:36:08 -06005100 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005101 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005102 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5103 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005104 return -EINVAL;
5105
Jens Axboe3529d8c2019-12-19 18:24:38 -07005106 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5107 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005108 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005109}
5110
Pavel Begunkov889fca72021-02-10 00:03:09 +00005111static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005112{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005113 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005114 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005115 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005116 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005117
Jens Axboee8c2bc12020-08-15 18:44:09 -07005118 if (req->async_data) {
5119 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005120 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005121 ret = move_addr_to_kernel(req->connect.addr,
5122 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005123 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005124 if (ret)
5125 goto out;
5126 io = &__io;
5127 }
5128
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005129 file_flags = force_nonblock ? O_NONBLOCK : 0;
5130
Jens Axboee8c2bc12020-08-15 18:44:09 -07005131 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005132 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005133 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005134 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005135 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005136 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005137 ret = -ENOMEM;
5138 goto out;
5139 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005140 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005141 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005142 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005143 if (ret == -ERESTARTSYS)
5144 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005145out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005146 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005147 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005148 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005149 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005150}
YueHaibing469956e2020-03-04 15:53:52 +08005151#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005152#define IO_NETOP_FN(op) \
5153static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5154{ \
5155 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005156}
5157
Jens Axboe99a10082021-02-19 09:35:19 -07005158#define IO_NETOP_PREP(op) \
5159IO_NETOP_FN(op) \
5160static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5161{ \
5162 return -EOPNOTSUPP; \
5163} \
5164
5165#define IO_NETOP_PREP_ASYNC(op) \
5166IO_NETOP_PREP(op) \
5167static int io_##op##_prep_async(struct io_kiocb *req) \
5168{ \
5169 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005170}
5171
Jens Axboe99a10082021-02-19 09:35:19 -07005172IO_NETOP_PREP_ASYNC(sendmsg);
5173IO_NETOP_PREP_ASYNC(recvmsg);
5174IO_NETOP_PREP_ASYNC(connect);
5175IO_NETOP_PREP(accept);
5176IO_NETOP_FN(send);
5177IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005178#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005179
Jens Axboed7718a92020-02-14 22:23:12 -07005180struct io_poll_table {
5181 struct poll_table_struct pt;
5182 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005183 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005184 int error;
5185};
5186
Jens Axboed7718a92020-02-14 22:23:12 -07005187static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005188 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07005189{
Jens Axboed7718a92020-02-14 22:23:12 -07005190 /* for instances that support it check for an event match first: */
5191 if (mask && !(mask & poll->events))
5192 return 0;
5193
5194 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5195
5196 list_del_init(&poll->wait.entry);
5197
Jens Axboed7718a92020-02-14 22:23:12 -07005198 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005199 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005200
Jens Axboed7718a92020-02-14 22:23:12 -07005201 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005202 * If this fails, then the task is exiting. When a task exits, the
5203 * work gets canceled, so just cancel this request as well instead
5204 * of executing it. We can't safely execute it anyway, as we may not
5205 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005206 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005207 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005208 return 1;
5209}
5210
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005211static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5212 __acquires(&req->ctx->completion_lock)
5213{
5214 struct io_ring_ctx *ctx = req->ctx;
5215
Jens Axboe316319e2021-08-19 09:41:42 -06005216 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005217 if (unlikely(req->task->flags & PF_EXITING))
5218 WRITE_ONCE(poll->canceled, true);
5219
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005220 if (!req->result && !READ_ONCE(poll->canceled)) {
5221 struct poll_table_struct pt = { ._key = poll->events };
5222
5223 req->result = vfs_poll(req->file, &pt) & poll->events;
5224 }
5225
Jens Axboe79ebeae2021-08-10 15:18:27 -06005226 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005227 if (!req->result && !READ_ONCE(poll->canceled)) {
5228 add_wait_queue(poll->head, &poll->wait);
5229 return true;
5230 }
5231
5232 return false;
5233}
5234
Jens Axboed4e7cd32020-08-15 11:44:50 -07005235static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005236{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005237 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005238 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005239 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005240 return req->apoll->double_poll;
5241}
5242
5243static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5244{
5245 if (req->opcode == IORING_OP_POLL_ADD)
5246 return &req->poll;
5247 return &req->apoll->poll;
5248}
5249
5250static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005251 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005252{
5253 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005254
5255 lockdep_assert_held(&req->ctx->completion_lock);
5256
5257 if (poll && poll->head) {
5258 struct wait_queue_head *head = poll->head;
5259
Jens Axboe79ebeae2021-08-10 15:18:27 -06005260 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005261 list_del_init(&poll->wait.entry);
5262 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005263 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005264 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005265 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005266 }
5267}
5268
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005269static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005270 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005271{
5272 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005273 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005274 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005275
Pavel Begunkove27414b2021-04-09 09:13:20 +01005276 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005277 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005278 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005279 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005280 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005281 }
Jens Axboeb69de282021-03-17 08:37:41 -06005282 if (req->poll.events & EPOLLONESHOT)
5283 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005284 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
5285 req->poll.events |= EPOLLONESHOT;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005286 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005287 }
Hao Xu7b289c32021-04-13 15:20:39 +08005288 if (flags & IORING_CQE_F_MORE)
5289 ctx->cq_extra++;
5290
Jens Axboe88e41cf2021-02-22 22:08:01 -07005291 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005292}
5293
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005294static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
5295 __must_hold(&req->ctx->completion_lock)
5296{
5297 bool done;
5298
5299 done = __io_poll_complete(req, mask);
5300 io_commit_cqring(req->ctx);
5301 return done;
5302}
5303
Pavel Begunkovf237c302021-08-18 12:42:46 +01005304static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005305{
Jens Axboe6d816e02020-08-11 08:04:14 -06005306 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005307 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005308
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005309 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005310 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005311 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005312 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005313
Hao Xu5b7aa382021-09-22 18:12:38 +08005314 if (req->poll.done) {
5315 spin_unlock(&ctx->completion_lock);
5316 return;
5317 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005318 done = __io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005319 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005320 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005321 hash_del(&req->hash_node);
Hao Xubd99c712021-09-22 18:12:36 +08005322 req->poll.done = true;
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005323 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005324 req->result = 0;
5325 add_wait_queue(req->poll.head, &req->poll.wait);
5326 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005327 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005328 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005329 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005330
Jens Axboe88e41cf2021-02-22 22:08:01 -07005331 if (done) {
5332 nxt = io_put_req_find_next(req);
5333 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005334 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005335 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005336 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005337}
5338
5339static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5340 int sync, void *key)
5341{
5342 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005343 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005344 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005345 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005346
5347 /* for instances that support it check for an event match first: */
5348 if (mask && !(mask & poll->events))
5349 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005350 if (!(poll->events & EPOLLONESHOT))
5351 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005352
Jens Axboe8706e042020-09-28 08:38:54 -06005353 list_del_init(&wait->entry);
5354
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005355 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005356 bool done;
5357
Jens Axboe79ebeae2021-08-10 15:18:27 -06005358 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005359 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005360 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005361 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005362 /* make sure double remove sees this as being gone */
5363 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005364 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005365 if (!done) {
5366 /* use wait func handler, so it matches the rq type */
5367 poll->wait.func(&poll->wait, mode, sync, key);
5368 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005369 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005370 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005371 return 1;
5372}
5373
5374static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5375 wait_queue_func_t wake_func)
5376{
5377 poll->head = NULL;
5378 poll->done = false;
5379 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005380#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5381 /* mask in events that we always want/need */
5382 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005383 INIT_LIST_HEAD(&poll->wait.entry);
5384 init_waitqueue_func_entry(&poll->wait, wake_func);
5385}
5386
5387static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005388 struct wait_queue_head *head,
5389 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005390{
5391 struct io_kiocb *req = pt->req;
5392
5393 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005394 * The file being polled uses multiple waitqueues for poll handling
5395 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5396 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005397 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005398 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005399 struct io_poll_iocb *poll_one = poll;
5400
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005401 /* double add on the same waitqueue head, ignore */
5402 if (poll_one->head == head)
5403 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005404 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005405 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005406 if ((*poll_ptr)->head == head)
5407 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005408 pt->error = -EINVAL;
5409 return;
5410 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005411 /*
5412 * Can't handle multishot for double wait for now, turn it
5413 * into one-shot mode.
5414 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005415 if (!(poll_one->events & EPOLLONESHOT))
5416 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005417 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5418 if (!poll) {
5419 pt->error = -ENOMEM;
5420 return;
5421 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005422 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005423 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005424 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005425 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005426 }
5427
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005428 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005429 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005430
5431 if (poll->events & EPOLLEXCLUSIVE)
5432 add_wait_queue_exclusive(head, &poll->wait);
5433 else
5434 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005435}
5436
5437static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5438 struct poll_table_struct *p)
5439{
5440 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005441 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005442
Jens Axboe807abcb2020-07-17 17:09:27 -06005443 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005444}
5445
Pavel Begunkovf237c302021-08-18 12:42:46 +01005446static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005447{
Jens Axboed7718a92020-02-14 22:23:12 -07005448 struct async_poll *apoll = req->apoll;
5449 struct io_ring_ctx *ctx = req->ctx;
5450
Olivier Langlois236daeae2021-05-31 02:36:37 -04005451 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005452
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005453 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005454 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005455 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005456 }
5457
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005458 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005459 io_poll_remove_double(req);
Hao Xubd99c712021-09-22 18:12:36 +08005460 apoll->poll.done = true;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005461 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005462
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005463 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005464 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005465 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005466 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005467}
5468
5469static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5470 void *key)
5471{
5472 struct io_kiocb *req = wait->private;
5473 struct io_poll_iocb *poll = &req->apoll->poll;
5474
5475 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5476 key_to_poll(key));
5477
5478 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5479}
5480
5481static void io_poll_req_insert(struct io_kiocb *req)
5482{
5483 struct io_ring_ctx *ctx = req->ctx;
5484 struct hlist_head *list;
5485
5486 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5487 hlist_add_head(&req->hash_node, list);
5488}
5489
5490static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5491 struct io_poll_iocb *poll,
5492 struct io_poll_table *ipt, __poll_t mask,
5493 wait_queue_func_t wake_func)
5494 __acquires(&ctx->completion_lock)
5495{
5496 struct io_ring_ctx *ctx = req->ctx;
5497 bool cancel = false;
5498
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005499 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005500 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005501 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005502 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005503
5504 ipt->pt._key = mask;
5505 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005506 ipt->error = 0;
5507 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005508
Jens Axboed7718a92020-02-14 22:23:12 -07005509 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005510 if (unlikely(!ipt->nr_entries) && !ipt->error)
5511 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005512
Jens Axboe79ebeae2021-08-10 15:18:27 -06005513 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005514 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005515 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005516 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005517 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005518 if (unlikely(list_empty(&poll->wait.entry))) {
5519 if (ipt->error)
5520 cancel = true;
5521 ipt->error = 0;
5522 mask = 0;
5523 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005524 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005525 list_del_init(&poll->wait.entry);
5526 else if (cancel)
5527 WRITE_ONCE(poll->canceled, true);
5528 else if (!poll->done) /* actually waiting for an event */
5529 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005530 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005531 }
5532
5533 return mask;
5534}
5535
Olivier Langlois59b735a2021-06-22 05:17:39 -07005536enum {
5537 IO_APOLL_OK,
5538 IO_APOLL_ABORTED,
5539 IO_APOLL_READY
5540};
5541
5542static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005543{
5544 const struct io_op_def *def = &io_op_defs[req->opcode];
5545 struct io_ring_ctx *ctx = req->ctx;
5546 struct async_poll *apoll;
5547 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005548 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005549 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005550
5551 if (!req->file || !file_can_poll(req->file))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005552 return IO_APOLL_ABORTED;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005553 if (req->flags & REQ_F_POLLED)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005554 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005555 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005556 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005557
5558 if (def->pollin) {
5559 rw = READ;
5560 mask |= POLLIN | POLLRDNORM;
5561
5562 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5563 if ((req->opcode == IORING_OP_RECVMSG) &&
5564 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5565 mask &= ~POLLIN;
5566 } else {
5567 rw = WRITE;
5568 mask |= POLLOUT | POLLWRNORM;
5569 }
5570
Jens Axboe9dab14b2020-08-25 12:27:50 -06005571 /* if we can't nonblock try, then no point in arming a poll handler */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01005572 if (!io_file_supports_nowait(req, rw))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005573 return IO_APOLL_ABORTED;
Jens Axboed7718a92020-02-14 22:23:12 -07005574
5575 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5576 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005577 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005578 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005579 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005580 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005581 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005582 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005583
5584 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5585 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005586 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005587 if (ret || ipt.error)
5588 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5589
Olivier Langlois236daeae2021-05-31 02:36:37 -04005590 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5591 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005592 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005593}
5594
5595static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005596 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005597 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005598{
Jens Axboeb41e9852020-02-17 09:52:41 -07005599 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005600
Jens Axboe50826202021-02-23 09:02:26 -07005601 if (!poll->head)
5602 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005603 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005604 if (do_cancel)
5605 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005606 if (!list_empty(&poll->wait.entry)) {
5607 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005608 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005609 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005610 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005611 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005612 return do_complete;
5613}
5614
Pavel Begunkov5d709042021-08-09 20:18:13 +01005615static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005616 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005617{
5618 bool do_complete;
5619
Jens Axboed4e7cd32020-08-15 11:44:50 -07005620 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005621 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005622
Jens Axboeb41e9852020-02-17 09:52:41 -07005623 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005624 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005625 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005626 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005627 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005628 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005629 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005630}
5631
Jens Axboe76e1b642020-09-26 15:05:03 -06005632/*
5633 * Returns true if we found and killed one or more poll requests
5634 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005635static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005636 bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005637{
Jens Axboe78076bb2019-12-04 19:56:40 -07005638 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005639 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005640 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005641
Jens Axboe79ebeae2021-08-10 15:18:27 -06005642 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005643 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5644 struct hlist_head *list;
5645
5646 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005647 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005648 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005649 posted += io_poll_remove_one(req);
5650 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005651 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005652 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005653
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005654 if (posted)
5655 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005656
5657 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005658}
5659
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005660static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5661 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005662 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005663{
Jens Axboe78076bb2019-12-04 19:56:40 -07005664 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005665 struct io_kiocb *req;
5666
Jens Axboe78076bb2019-12-04 19:56:40 -07005667 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5668 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005669 if (sqe_addr != req->user_data)
5670 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005671 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5672 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005673 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005674 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005675 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005676}
5677
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005678static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5679 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005680 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005681{
5682 struct io_kiocb *req;
5683
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005684 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005685 if (!req)
5686 return -ENOENT;
5687 if (io_poll_remove_one(req))
5688 return 0;
5689
5690 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005691}
5692
Pavel Begunkov9096af32021-04-14 13:38:36 +01005693static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5694 unsigned int flags)
5695{
5696 u32 events;
5697
5698 events = READ_ONCE(sqe->poll32_events);
5699#ifdef __BIG_ENDIAN
5700 events = swahw32(events);
5701#endif
5702 if (!(flags & IORING_POLL_ADD_MULTI))
5703 events |= EPOLLONESHOT;
5704 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5705}
5706
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005707static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005708 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005709{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005710 struct io_poll_update *upd = &req->poll_update;
5711 u32 flags;
5712
Jens Axboe221c5eb2019-01-17 09:41:58 -07005713 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5714 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005715 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005716 return -EINVAL;
5717 flags = READ_ONCE(sqe->len);
5718 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5719 IORING_POLL_ADD_MULTI))
5720 return -EINVAL;
5721 /* meaningless without update */
5722 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005723 return -EINVAL;
5724
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005725 upd->old_user_data = READ_ONCE(sqe->addr);
5726 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5727 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005728
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005729 upd->new_user_data = READ_ONCE(sqe->off);
5730 if (!upd->update_user_data && upd->new_user_data)
5731 return -EINVAL;
5732 if (upd->update_events)
5733 upd->events = io_poll_parse_events(sqe, flags);
5734 else if (sqe->poll32_events)
5735 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005736
Jens Axboe221c5eb2019-01-17 09:41:58 -07005737 return 0;
5738}
5739
Jens Axboe221c5eb2019-01-17 09:41:58 -07005740static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5741 void *key)
5742{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005743 struct io_kiocb *req = wait->private;
5744 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005745
Jens Axboed7718a92020-02-14 22:23:12 -07005746 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005747}
5748
Jens Axboe221c5eb2019-01-17 09:41:58 -07005749static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5750 struct poll_table_struct *p)
5751{
5752 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5753
Jens Axboee8c2bc12020-08-15 18:44:09 -07005754 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005755}
5756
Jens Axboe3529d8c2019-12-19 18:24:38 -07005757static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005758{
5759 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005760 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005761
5762 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5763 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005764 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005765 return -EINVAL;
5766 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005767 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005768 return -EINVAL;
5769
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005770 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005771 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005772 return 0;
5773}
5774
Pavel Begunkov61e98202021-02-10 00:03:08 +00005775static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005776{
5777 struct io_poll_iocb *poll = &req->poll;
5778 struct io_ring_ctx *ctx = req->ctx;
5779 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005780 __poll_t mask;
Hao Xu5b7aa382021-09-22 18:12:38 +08005781 bool done;
Jens Axboe0969e782019-12-17 18:40:57 -07005782
Jens Axboed7718a92020-02-14 22:23:12 -07005783 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005784
Jens Axboed7718a92020-02-14 22:23:12 -07005785 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5786 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005787
Jens Axboe8c838782019-03-12 15:48:16 -06005788 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005789 ipt.error = 0;
Hao Xu5b7aa382021-09-22 18:12:38 +08005790 done = io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06005791 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005792 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005793
Jens Axboe8c838782019-03-12 15:48:16 -06005794 if (mask) {
5795 io_cqring_ev_posted(ctx);
Hao Xu5b7aa382021-09-22 18:12:38 +08005796 if (done)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005797 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005798 }
Jens Axboe8c838782019-03-12 15:48:16 -06005799 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005800}
5801
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005802static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005803{
5804 struct io_ring_ctx *ctx = req->ctx;
5805 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005806 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005807 int ret;
5808
Jens Axboe79ebeae2021-08-10 15:18:27 -06005809 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005810 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005811 if (!preq) {
5812 ret = -ENOENT;
5813 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005814 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005815
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005816 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5817 completing = true;
5818 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5819 goto err;
5820 }
5821
Jens Axboecb3b200e2021-04-06 09:49:31 -06005822 /*
5823 * Don't allow racy completion with singleshot, as we cannot safely
5824 * update those. For multishot, if we're racing with completion, just
5825 * let completion re-add it.
5826 */
5827 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5828 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5829 ret = -EALREADY;
5830 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005831 }
5832 /* we now have a detached poll request. reissue. */
5833 ret = 0;
5834err:
Jens Axboeb69de282021-03-17 08:37:41 -06005835 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005836 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005837 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005838 io_req_complete(req, ret);
5839 return 0;
5840 }
5841 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005842 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005843 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005844 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005845 preq->poll.events |= IO_POLL_UNMASK;
5846 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005847 if (req->poll_update.update_user_data)
5848 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005849 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005850
Jens Axboeb69de282021-03-17 08:37:41 -06005851 /* complete update request, we're done with it */
5852 io_req_complete(req, ret);
5853
Jens Axboecb3b200e2021-04-06 09:49:31 -06005854 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005855 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005856 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005857 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005858 io_req_complete(preq, ret);
5859 }
Jens Axboeb69de282021-03-17 08:37:41 -06005860 }
5861 return 0;
5862}
5863
Pavel Begunkovf237c302021-08-18 12:42:46 +01005864static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005865{
Pavel Begunkov62245902021-10-02 19:36:14 +01005866 struct io_timeout_data *data = req->async_data;
5867
5868 if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
5869 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005870 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005871}
5872
Jens Axboe5262f562019-09-17 12:26:57 -06005873static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5874{
Jens Axboead8a48a2019-11-15 08:49:11 -07005875 struct io_timeout_data *data = container_of(timer,
5876 struct io_timeout_data, timer);
5877 struct io_kiocb *req = data->req;
5878 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005879 unsigned long flags;
5880
Jens Axboe89850fc2021-08-10 15:11:51 -06005881 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005882 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005883 atomic_set(&req->ctx->cq_timeouts,
5884 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005885 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005886
Jens Axboe89850fc2021-08-10 15:11:51 -06005887 req->io_task_work.func = io_req_task_timeout;
5888 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005889 return HRTIMER_NORESTART;
5890}
5891
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005892static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5893 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005894 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005895{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005896 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005897 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005898 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005899
5900 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005901 found = user_data == req->user_data;
5902 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005903 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005904 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005905 if (!found)
5906 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005907
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005908 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005909 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005910 return ERR_PTR(-EALREADY);
5911 list_del_init(&req->timeout.list);
5912 return req;
5913}
5914
5915static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005916 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005917 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005918{
5919 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5920
5921 if (IS_ERR(req))
5922 return PTR_ERR(req);
5923
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005924 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005925 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005926 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005927 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005928}
5929
Jens Axboe50c1df22021-08-27 17:11:06 -06005930static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
5931{
5932 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
5933 case IORING_TIMEOUT_BOOTTIME:
5934 return CLOCK_BOOTTIME;
5935 case IORING_TIMEOUT_REALTIME:
5936 return CLOCK_REALTIME;
5937 default:
5938 /* can't happen, vetted at prep time */
5939 WARN_ON_ONCE(1);
5940 fallthrough;
5941 case 0:
5942 return CLOCK_MONOTONIC;
5943 }
5944}
5945
Pavel Begunkovf1042b62021-08-28 19:54:39 -06005946static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5947 struct timespec64 *ts, enum hrtimer_mode mode)
5948 __must_hold(&ctx->timeout_lock)
5949{
5950 struct io_timeout_data *io;
5951 struct io_kiocb *req;
5952 bool found = false;
5953
5954 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
5955 found = user_data == req->user_data;
5956 if (found)
5957 break;
5958 }
5959 if (!found)
5960 return -ENOENT;
5961
5962 io = req->async_data;
5963 if (hrtimer_try_to_cancel(&io->timer) == -1)
5964 return -EALREADY;
5965 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
5966 io->timer.function = io_link_timeout_fn;
5967 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
5968 return 0;
5969}
5970
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005971static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5972 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06005973 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005974{
5975 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5976 struct io_timeout_data *data;
5977
5978 if (IS_ERR(req))
5979 return PTR_ERR(req);
5980
5981 req->timeout.off = 0; /* noseq */
5982 data = req->async_data;
5983 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06005984 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005985 data->timer.function = io_timeout_fn;
5986 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5987 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005988}
5989
Jens Axboe3529d8c2019-12-19 18:24:38 -07005990static int io_timeout_remove_prep(struct io_kiocb *req,
5991 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005992{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005993 struct io_timeout_rem *tr = &req->timeout_rem;
5994
Jens Axboeb29472e2019-12-17 18:50:29 -07005995 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5996 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005997 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5998 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005999 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07006000 return -EINVAL;
6001
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006002 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006003 tr->addr = READ_ONCE(sqe->addr);
6004 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006005 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6006 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6007 return -EINVAL;
6008 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6009 tr->ltimeout = true;
6010 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006011 return -EINVAL;
6012 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6013 return -EFAULT;
6014 } else if (tr->flags) {
6015 /* timeout removal doesn't support flags */
6016 return -EINVAL;
6017 }
6018
Jens Axboeb29472e2019-12-17 18:50:29 -07006019 return 0;
6020}
6021
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006022static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6023{
6024 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6025 : HRTIMER_MODE_REL;
6026}
6027
Jens Axboe11365042019-10-16 09:08:32 -06006028/*
6029 * Remove or update an existing timeout command
6030 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006031static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006032{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006033 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006034 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006035 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006036
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006037 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6038 spin_lock(&ctx->completion_lock);
6039 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006040 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006041 spin_unlock_irq(&ctx->timeout_lock);
6042 spin_unlock(&ctx->completion_lock);
6043 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006044 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6045
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006046 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006047 if (tr->ltimeout)
6048 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6049 else
6050 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006051 spin_unlock_irq(&ctx->timeout_lock);
6052 }
Jens Axboe11365042019-10-16 09:08:32 -06006053
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006054 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006055 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006056 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006057 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006058}
6059
Jens Axboe3529d8c2019-12-19 18:24:38 -07006060static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006061 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006062{
Jens Axboead8a48a2019-11-15 08:49:11 -07006063 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006064 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006065 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006066
Jens Axboead8a48a2019-11-15 08:49:11 -07006067 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006068 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006069 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6070 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006071 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006072 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006073 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006074 flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkov62245902021-10-02 19:36:14 +01006075 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
6076 IORING_TIMEOUT_ETIME_SUCCESS))
Jens Axboe50c1df22021-08-27 17:11:06 -06006077 return -EINVAL;
6078 /* more than one clock specified is invalid, obviously */
6079 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006080 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006081
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006082 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006083 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006084 if (unlikely(off && !req->ctx->off_timeout_used))
6085 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006086
Jens Axboee8c2bc12020-08-15 18:44:09 -07006087 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006088 return -ENOMEM;
6089
Jens Axboee8c2bc12020-08-15 18:44:09 -07006090 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006091 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006092 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006093
6094 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006095 return -EFAULT;
6096
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006097 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006098 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006099
6100 if (is_timeout_link) {
6101 struct io_submit_link *link = &req->ctx->submit_state.link;
6102
6103 if (!link->head)
6104 return -EINVAL;
6105 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6106 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006107 req->timeout.head = link->last;
6108 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006109 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006110 return 0;
6111}
6112
Pavel Begunkov61e98202021-02-10 00:03:08 +00006113static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006114{
Jens Axboead8a48a2019-11-15 08:49:11 -07006115 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006116 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006117 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006118 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006119
Jens Axboe89850fc2021-08-10 15:11:51 -06006120 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006121
Jens Axboe5262f562019-09-17 12:26:57 -06006122 /*
6123 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006124 * timeout event to be satisfied. If it isn't set, then this is
6125 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006126 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006127 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006128 entry = ctx->timeout_list.prev;
6129 goto add;
6130 }
Jens Axboe5262f562019-09-17 12:26:57 -06006131
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006132 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6133 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006134
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006135 /* Update the last seq here in case io_flush_timeouts() hasn't.
6136 * This is safe because ->completion_lock is held, and submissions
6137 * and completions are never mixed in the same ->completion_lock section.
6138 */
6139 ctx->cq_last_tm_flush = tail;
6140
Jens Axboe5262f562019-09-17 12:26:57 -06006141 /*
6142 * Insertion sort, ensuring the first entry in the list is always
6143 * the one we need first.
6144 */
Jens Axboe5262f562019-09-17 12:26:57 -06006145 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006146 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6147 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006148
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006149 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006150 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006151 /* nxt.seq is behind @tail, otherwise would've been completed */
6152 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006153 break;
6154 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006155add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006156 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006157 data->timer.function = io_timeout_fn;
6158 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006159 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006160 return 0;
6161}
6162
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006163struct io_cancel_data {
6164 struct io_ring_ctx *ctx;
6165 u64 user_data;
6166};
6167
Jens Axboe62755e32019-10-28 21:49:21 -06006168static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006169{
Jens Axboe62755e32019-10-28 21:49:21 -06006170 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006171 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006172
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006173 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006174}
6175
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006176static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6177 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006178{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006179 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006180 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006181 int ret = 0;
6182
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006183 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006184 return -ENOENT;
6185
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006186 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006187 switch (cancel_ret) {
6188 case IO_WQ_CANCEL_OK:
6189 ret = 0;
6190 break;
6191 case IO_WQ_CANCEL_RUNNING:
6192 ret = -EALREADY;
6193 break;
6194 case IO_WQ_CANCEL_NOTFOUND:
6195 ret = -ENOENT;
6196 break;
6197 }
6198
Jens Axboee977d6d2019-11-05 12:39:45 -07006199 return ret;
6200}
6201
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006202static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006203{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006204 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006205 int ret;
6206
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006207 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006208
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006209 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01006210 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006211 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006212
6213 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006214 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006215 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006216 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006217 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01006218 goto out;
6219 ret = io_poll_cancel(ctx, sqe_addr, false);
6220out:
6221 spin_unlock(&ctx->completion_lock);
6222 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006223}
6224
Jens Axboe3529d8c2019-12-19 18:24:38 -07006225static int io_async_cancel_prep(struct io_kiocb *req,
6226 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006227{
Jens Axboefbf23842019-12-17 18:45:56 -07006228 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006229 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006230 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6231 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006232 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6233 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006234 return -EINVAL;
6235
Jens Axboefbf23842019-12-17 18:45:56 -07006236 req->cancel.addr = READ_ONCE(sqe->addr);
6237 return 0;
6238}
6239
Pavel Begunkov61e98202021-02-10 00:03:08 +00006240static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006241{
6242 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006243 u64 sqe_addr = req->cancel.addr;
6244 struct io_tctx_node *node;
6245 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006246
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006247 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006248 if (ret != -ENOENT)
6249 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006250
6251 /* slow path, try all io-wq's */
6252 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
6253 ret = -ENOENT;
6254 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6255 struct io_uring_task *tctx = node->task->io_uring;
6256
Pavel Begunkov58f99372021-03-12 16:25:55 +00006257 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6258 if (ret != -ENOENT)
6259 break;
6260 }
6261 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkov58f99372021-03-12 16:25:55 +00006262done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006263 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006264 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006265 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006266 return 0;
6267}
6268
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006269static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006270 const struct io_uring_sqe *sqe)
6271{
Daniele Albano61710e42020-07-18 14:15:16 -06006272 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6273 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006274 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006275 return -EINVAL;
6276
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006277 req->rsrc_update.offset = READ_ONCE(sqe->off);
6278 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6279 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006280 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006281 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006282 return 0;
6283}
6284
Pavel Begunkov889fca72021-02-10 00:03:09 +00006285static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006286{
6287 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006288 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006289 int ret;
6290
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006291 up.offset = req->rsrc_update.offset;
6292 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006293 up.nr = 0;
6294 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006295 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006296
Jens Axboecdb31c22021-09-24 08:43:54 -06006297 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006298 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006299 &up, req->rsrc_update.nr_args);
Jens Axboecdb31c22021-09-24 08:43:54 -06006300 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Jens Axboe05f3fb32019-12-09 11:22:50 -07006301
6302 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006303 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006304 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006305 return 0;
6306}
6307
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006308static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006309{
Jens Axboed625c6e2019-12-17 19:53:05 -07006310 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006311 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006312 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006313 case IORING_OP_READV:
6314 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006315 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006316 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006317 case IORING_OP_WRITEV:
6318 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006319 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006320 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006321 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006322 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006323 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006324 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006325 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006326 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006327 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006328 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006329 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006330 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006331 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006332 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006333 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006334 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006335 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006336 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006337 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006338 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006339 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006340 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006341 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006342 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006343 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006344 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006345 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006346 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006347 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006348 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006349 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006350 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006351 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006352 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006353 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006354 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006355 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006356 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006357 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006358 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006359 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006360 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006361 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006362 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006363 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006364 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006365 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006366 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006367 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006368 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006369 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006370 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006371 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006372 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006373 case IORING_OP_SHUTDOWN:
6374 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006375 case IORING_OP_RENAMEAT:
6376 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006377 case IORING_OP_UNLINKAT:
6378 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006379 case IORING_OP_MKDIRAT:
6380 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006381 case IORING_OP_SYMLINKAT:
6382 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006383 case IORING_OP_LINKAT:
6384 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006385 }
6386
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006387 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6388 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006389 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006390}
6391
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006392static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006393{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006394 if (!io_op_defs[req->opcode].needs_async_setup)
6395 return 0;
6396 if (WARN_ON_ONCE(req->async_data))
6397 return -EFAULT;
6398 if (io_alloc_async_data(req))
6399 return -EAGAIN;
6400
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006401 switch (req->opcode) {
6402 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006403 return io_rw_prep_async(req, READ);
6404 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006405 return io_rw_prep_async(req, WRITE);
6406 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006407 return io_sendmsg_prep_async(req);
6408 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006409 return io_recvmsg_prep_async(req);
6410 case IORING_OP_CONNECT:
6411 return io_connect_prep_async(req);
6412 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006413 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6414 req->opcode);
6415 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006416}
6417
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006418static u32 io_get_sequence(struct io_kiocb *req)
6419{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006420 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006421
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006422 /* need original cached_sq_head, but it was increased for each req */
6423 io_for_each_link(req, req)
6424 seq--;
6425 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006426}
6427
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006428static void io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006429{
6430 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006431 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006432 int ret;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006433 u32 seq = io_get_sequence(req);
Jens Axboedef596e2019-01-09 08:59:42 -07006434
6435 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov5e371262021-09-24 22:00:04 +01006436 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) {
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006437queue:
Pavel Begunkov5e371262021-09-24 22:00:04 +01006438 ctx->drain_active = false;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006439 io_req_task_queue(req);
6440 return;
Pavel Begunkov5e371262021-09-24 22:00:04 +01006441 }
Jens Axboedef596e2019-01-09 08:59:42 -07006442
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006443 ret = io_req_prep_async(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006444 if (ret) {
6445fail:
6446 io_req_complete_failed(req, ret);
6447 return;
6448 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006449 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006450 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006451 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006452 ret = -ENOMEM;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006453 goto fail;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006454 }
Jens Axboe31b51512019-01-18 22:56:34 -07006455
Jens Axboe79ebeae2021-08-10 15:18:27 -06006456 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006457 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006458 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006459 kfree(de);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006460 goto queue;
Jens Axboe31b51512019-01-18 22:56:34 -07006461 }
6462
6463 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006464 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006465 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006466 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006467 spin_unlock(&ctx->completion_lock);
Jens Axboe31b51512019-01-18 22:56:34 -07006468}
6469
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006470static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006471{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006472 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01006473 kfree(req->kbuf);
6474 req->kbuf = NULL;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006475 }
6476
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006477 if (req->flags & REQ_F_NEED_CLEANUP) {
6478 switch (req->opcode) {
6479 case IORING_OP_READV:
6480 case IORING_OP_READ_FIXED:
6481 case IORING_OP_READ:
6482 case IORING_OP_WRITEV:
6483 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006484 case IORING_OP_WRITE: {
6485 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006486
6487 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006488 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006489 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006490 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006491 case IORING_OP_SENDMSG: {
6492 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006493
6494 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006495 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006496 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006497 case IORING_OP_SPLICE:
6498 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006499 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6500 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006501 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006502 case IORING_OP_OPENAT:
6503 case IORING_OP_OPENAT2:
6504 if (req->open.filename)
6505 putname(req->open.filename);
6506 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006507 case IORING_OP_RENAMEAT:
6508 putname(req->rename.oldpath);
6509 putname(req->rename.newpath);
6510 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006511 case IORING_OP_UNLINKAT:
6512 putname(req->unlink.filename);
6513 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006514 case IORING_OP_MKDIRAT:
6515 putname(req->mkdir.filename);
6516 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006517 case IORING_OP_SYMLINKAT:
6518 putname(req->symlink.oldpath);
6519 putname(req->symlink.newpath);
6520 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006521 case IORING_OP_LINKAT:
6522 putname(req->hardlink.oldpath);
6523 putname(req->hardlink.newpath);
6524 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006525 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006526 }
Jens Axboe75652a302021-04-15 09:52:40 -06006527 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6528 kfree(req->apoll->double_poll);
6529 kfree(req->apoll);
6530 req->apoll = NULL;
6531 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006532 if (req->flags & REQ_F_INFLIGHT) {
6533 struct io_uring_task *tctx = req->task->io_uring;
6534
6535 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006536 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006537 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006538 put_cred(req->creds);
Pavel Begunkovc8543572021-06-17 18:14:04 +01006539
6540 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006541}
6542
Pavel Begunkov889fca72021-02-10 00:03:09 +00006543static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006544{
Jens Axboeedafcce2019-01-09 09:16:05 -07006545 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006546 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006547 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006548
Pavel Begunkov6878b402021-09-24 21:59:41 +01006549 if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006550 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006551
Jens Axboed625c6e2019-12-17 19:53:05 -07006552 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006553 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006554 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006555 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006556 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006557 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006558 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006559 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006560 break;
6561 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006562 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006563 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006564 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006565 break;
6566 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006567 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006568 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006569 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006570 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006571 break;
6572 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006573 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006574 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006575 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006576 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006577 break;
6578 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006579 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006580 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006581 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006582 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006583 break;
6584 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006585 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006586 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006587 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006588 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006589 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006590 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006591 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006592 break;
6593 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006594 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006595 break;
6596 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006597 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006598 break;
6599 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006600 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006601 break;
6602 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006603 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006604 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006605 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006606 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006607 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006608 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006609 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006610 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006611 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006612 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006613 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006614 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006615 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006616 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006617 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006618 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006619 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006620 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006621 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006622 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006623 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006624 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006625 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006626 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006627 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006628 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006629 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006630 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006631 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006632 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006633 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006634 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006635 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006636 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006637 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006638 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006639 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006640 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006641 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006642 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006643 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006644 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006645 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006646 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006647 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006648 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006649 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006650 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006651 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006652 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006653 case IORING_OP_MKDIRAT:
6654 ret = io_mkdirat(req, issue_flags);
6655 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006656 case IORING_OP_SYMLINKAT:
6657 ret = io_symlinkat(req, issue_flags);
6658 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006659 case IORING_OP_LINKAT:
6660 ret = io_linkat(req, issue_flags);
6661 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006662 default:
6663 ret = -EINVAL;
6664 break;
6665 }
Jens Axboe31b51512019-01-18 22:56:34 -07006666
Jens Axboe5730b272021-02-27 15:57:30 -07006667 if (creds)
6668 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006669 if (ret)
6670 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006671 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01006672 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6673 io_iopoll_req_issued(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006674
6675 return 0;
6676}
6677
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006678static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6679{
6680 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6681
6682 req = io_put_req_find_next(req);
6683 return req ? &req->work : NULL;
6684}
6685
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006686static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006687{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006688 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006689 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006690 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006691
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006692 /* one will be dropped by ->io_free_work() after returning to io-wq */
6693 if (!(req->flags & REQ_F_REFCOUNT))
6694 __io_req_set_refcount(req, 2);
6695 else
6696 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006697
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006698 timeout = io_prep_linked_timeout(req);
6699 if (timeout)
6700 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006701
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006702 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Jens Axboe4014d942021-01-19 15:53:54 -07006703 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006704 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006705
Jens Axboe561fb042019-10-24 07:25:42 -06006706 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006707 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006708 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006709 /*
6710 * We can get EAGAIN for polled IO even though we're
6711 * forcing a sync submission from here, since we can't
6712 * wait for request slots on the block side.
6713 */
6714 if (ret != -EAGAIN)
6715 break;
6716 cond_resched();
6717 } while (1);
6718 }
Jens Axboe31b51512019-01-18 22:56:34 -07006719
Pavel Begunkova3df76982021-02-18 22:32:52 +00006720 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006721 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006722 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006723}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006724
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006725static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006726 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006727{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006728 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006729}
6730
Jens Axboe09bb8392019-03-13 12:39:28 -06006731static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6732 int index)
6733{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006734 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006735
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006736 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006737}
6738
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006739static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006740{
6741 unsigned long file_ptr = (unsigned long) file;
6742
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006743 if (__io_file_supports_nowait(file, READ))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006744 file_ptr |= FFS_ASYNC_READ;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006745 if (__io_file_supports_nowait(file, WRITE))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006746 file_ptr |= FFS_ASYNC_WRITE;
6747 if (S_ISREG(file_inode(file)->i_mode))
6748 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006749 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006750}
6751
Pavel Begunkovac177052021-08-09 13:04:02 +01006752static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6753 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006754{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006755 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006756 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006757
Pavel Begunkovac177052021-08-09 13:04:02 +01006758 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6759 return NULL;
6760 fd = array_index_nospec(fd, ctx->nr_user_files);
6761 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6762 file = (struct file *) (file_ptr & FFS_MASK);
6763 file_ptr &= ~FFS_MASK;
6764 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006765 req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT);
Pavel Begunkovac177052021-08-09 13:04:02 +01006766 io_req_set_rsrc_node(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006767 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006768}
6769
Pavel Begunkovac177052021-08-09 13:04:02 +01006770static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006771 struct io_kiocb *req, int fd)
6772{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006773 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006774
6775 trace_io_uring_file_get(ctx, fd);
6776
6777 /* we don't allow fixed io_uring files */
6778 if (file && unlikely(file->f_op == &io_uring_fops))
6779 io_req_track_inflight(req);
6780 return file;
6781}
6782
6783static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006784 struct io_kiocb *req, int fd, bool fixed)
6785{
6786 if (fixed)
6787 return io_file_get_fixed(ctx, req, fd);
6788 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006789 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006790}
6791
Pavel Begunkovf237c302021-08-18 12:42:46 +01006792static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006793{
6794 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006795 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006796
6797 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006798 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006799 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006800 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006801 } else {
6802 io_req_complete_post(req, -ETIME, 0);
6803 }
6804}
6805
Jens Axboe2665abf2019-11-05 12:40:47 -07006806static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6807{
Jens Axboead8a48a2019-11-15 08:49:11 -07006808 struct io_timeout_data *data = container_of(timer,
6809 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006810 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006811 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006812 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006813
Jens Axboe89b263f2021-08-10 15:14:18 -06006814 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006815 prev = req->timeout.head;
6816 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006817
6818 /*
6819 * We don't expect the list to be empty, that will only happen if we
6820 * race with the completion of the linked work.
6821 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006822 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006823 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006824 if (!req_ref_inc_not_zero(prev))
6825 prev = NULL;
6826 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006827 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06006828 req->timeout.prev = prev;
6829 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006830
Jens Axboe89b263f2021-08-10 15:14:18 -06006831 req->io_task_work.func = io_req_task_link_timeout;
6832 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006833 return HRTIMER_NORESTART;
6834}
6835
Pavel Begunkovde968c12021-03-19 17:22:33 +00006836static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006837{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006838 struct io_ring_ctx *ctx = req->ctx;
6839
Jens Axboe89b263f2021-08-10 15:14:18 -06006840 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006841 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006842 * If the back reference is NULL, then our linked request finished
6843 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006844 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006845 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006846 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006847
Jens Axboead8a48a2019-11-15 08:49:11 -07006848 data->timer.function = io_link_timeout_fn;
6849 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6850 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006851 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07006852 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006853 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006854 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006855 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006856}
6857
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006858static void io_queue_sqe_arm_apoll(struct io_kiocb *req)
6859 __must_hold(&req->ctx->uring_lock)
6860{
6861 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
6862
6863 switch (io_arm_poll_handler(req)) {
6864 case IO_APOLL_READY:
6865 if (linked_timeout) {
6866 io_unprep_linked_timeout(req);
6867 linked_timeout = NULL;
6868 }
6869 io_req_task_queue(req);
6870 break;
6871 case IO_APOLL_ABORTED:
6872 /*
6873 * Queued up for async execution, worker will release
6874 * submit reference when the iocb is actually submitted.
6875 */
6876 io_queue_async_work(req, NULL);
6877 break;
6878 }
6879
6880 if (linked_timeout)
6881 io_queue_linked_timeout(linked_timeout);
6882}
6883
6884static inline void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006885 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006886{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006887 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006888 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006889
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006890 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006891
Pavel Begunkovfff4e402021-10-04 20:02:48 +01006892 if (req->flags & REQ_F_COMPLETE_INLINE) {
6893 io_req_add_compl_list(req);
Pavel Begunkovd9f9d282021-09-24 22:00:00 +01006894 return;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01006895 }
Jens Axboe491381ce2019-10-17 09:20:46 -06006896 /*
6897 * We async punt it if the file wasn't marked NOWAIT, or if the file
6898 * doesn't support non-blocking read/write attempts
6899 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006900 if (likely(!ret)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006901 linked_timeout = io_prep_linked_timeout(req);
6902 if (linked_timeout)
6903 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006904 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006905 io_queue_sqe_arm_apoll(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006906 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006907 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006908 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006909}
6910
Pavel Begunkov4652fe32021-09-24 21:59:58 +01006911static void io_queue_sqe_fallback(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006912 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006913{
Pavel Begunkov4652fe32021-09-24 21:59:58 +01006914 if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01006915 io_req_complete_fail_submit(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006916 } else if (unlikely(req->ctx->drain_active)) {
6917 io_drain_req(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006918 } else {
6919 int ret = io_req_prep_async(req);
6920
6921 if (unlikely(ret))
6922 io_req_complete_failed(req, ret);
6923 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01006924 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07006925 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006926}
6927
Pavel Begunkov4652fe32021-09-24 21:59:58 +01006928static inline void io_queue_sqe(struct io_kiocb *req)
6929 __must_hold(&req->ctx->uring_lock)
6930{
6931 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))))
6932 __io_queue_sqe(req);
6933 else
6934 io_queue_sqe_fallback(req);
6935}
6936
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006937/*
6938 * Check SQE restrictions (opcode and flags).
6939 *
6940 * Returns 'true' if SQE is allowed, 'false' otherwise.
6941 */
6942static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6943 struct io_kiocb *req,
6944 unsigned int sqe_flags)
6945{
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006946 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6947 return false;
6948
6949 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6950 ctx->restrictions.sqe_flags_required)
6951 return false;
6952
6953 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6954 ctx->restrictions.sqe_flags_required))
6955 return false;
6956
6957 return true;
6958}
6959
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01006960static void io_init_req_drain(struct io_kiocb *req)
6961{
6962 struct io_ring_ctx *ctx = req->ctx;
6963 struct io_kiocb *head = ctx->submit_state.link.head;
6964
6965 ctx->drain_active = true;
6966 if (head) {
6967 /*
6968 * If we need to drain a request in the middle of a link, drain
6969 * the head request and the next request/link after the current
6970 * link. Considering sequential execution of links,
6971 * IOSQE_IO_DRAIN will be maintained for every request of our
6972 * link.
6973 */
6974 head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC;
6975 ctx->drain_next = true;
6976 }
6977}
6978
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006979static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006980 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006981 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006982{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006983 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006984 unsigned int sqe_flags;
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01006985 int personality;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006986
Pavel Begunkov864ea922021-08-09 13:04:08 +01006987 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006988 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006989 /* same numerical values with corresponding REQ_F_*, safe to copy */
6990 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006991 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006992 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006993 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006994 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006995
6996 if (unlikely(req->opcode >= IORING_OP_LAST))
6997 return -EINVAL;
Pavel Begunkov68fe2562021-09-15 12:03:38 +01006998 if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
6999 /* enforce forwards compatibility on users */
7000 if (sqe_flags & ~SQE_VALID_FLAGS)
7001 return -EINVAL;
7002 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
7003 !io_op_defs[req->opcode].buffer_select)
7004 return -EOPNOTSUPP;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007005 if (sqe_flags & IOSQE_IO_DRAIN)
7006 io_init_req_drain(req);
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007007 }
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007008 if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) {
7009 if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags))
7010 return -EACCES;
7011 /* knock it to the slow queue path, will be drained there */
7012 if (ctx->drain_active)
7013 req->flags |= REQ_F_FORCE_ASYNC;
7014 /* if there is no link, we're at "next" request and need to drain */
7015 if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
7016 ctx->drain_next = false;
7017 ctx->drain_active = true;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007018 req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC;
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007019 }
7020 }
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007021
Jens Axboe003e8dc2021-03-06 09:22:27 -07007022 personality = READ_ONCE(sqe->personality);
7023 if (personality) {
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01007024 req->creds = xa_load(&ctx->personalities, personality);
7025 if (!req->creds)
Jens Axboe003e8dc2021-03-06 09:22:27 -07007026 return -EINVAL;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01007027 get_cred(req->creds);
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01007028 req->flags |= REQ_F_CREDS;
Jens Axboe003e8dc2021-03-06 09:22:27 -07007029 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007030 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007031
Jens Axboe27926b62020-10-28 09:33:23 -06007032 /*
7033 * Plug now if we have more than 1 IO left after this, and the target
7034 * is potentially a read/write to block based storage.
7035 */
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007036 if (state->need_plug && io_op_defs[req->opcode].plug) {
Jens Axboe27926b62020-10-28 09:33:23 -06007037 blk_start_plug(&state->plug);
7038 state->plug_started = true;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007039 state->need_plug = false;
Jens Axboe27926b62020-10-28 09:33:23 -06007040 }
Jens Axboe63ff8222020-05-07 14:56:15 -06007041
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007042 if (io_op_defs[req->opcode].needs_file) {
Pavel Begunkov62906e82021-08-10 14:52:47 +01007043 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007044 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007045 if (unlikely(!req->file))
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007046 return -EBADF;
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007047 }
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007048
7049 return io_req_prep(req, sqe);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007050}
7051
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007052static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007053 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007054 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007055{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007056 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007057 int ret;
7058
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007059 ret = io_init_req(ctx, req, sqe);
7060 if (unlikely(ret)) {
Jens Axboea87acfd2021-09-11 16:04:50 -06007061 trace_io_uring_req_failed(sqe, ret);
7062
Hao Xua8295b92021-08-27 17:46:09 +08007063 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007064 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007065 /*
7066 * we can judge a link req is failed or cancelled by if
7067 * REQ_F_FAIL is set, but the head is an exception since
7068 * it may be set REQ_F_FAIL because of other req's failure
7069 * so let's leverage req->result to distinguish if a head
7070 * is set REQ_F_FAIL because of its failure or other req's
7071 * failure so that we can set the correct ret code for it.
7072 * init result here to avoid affecting the normal path.
7073 */
7074 if (!(link->head->flags & REQ_F_FAIL))
7075 req_fail_link_node(link->head, -ECANCELED);
7076 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7077 /*
7078 * the current req is a normal req, we should return
7079 * error and thus break the submittion loop.
7080 */
7081 io_req_complete_failed(req, ret);
7082 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007083 }
Hao Xua8295b92021-08-27 17:46:09 +08007084 req_fail_link_node(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007085 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007086
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007087 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007088 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7089 req->flags, true,
7090 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007091
Jens Axboe6c271ce2019-01-10 11:22:30 -07007092 /*
7093 * If we already have a head request, queue this one for async
7094 * submittal once the head completes. If we don't have a head but
7095 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7096 * submitted sync once the chain is complete. If none of those
7097 * conditions are true (normal request), then just queue it.
7098 */
7099 if (link->head) {
7100 struct io_kiocb *head = link->head;
7101
Hao Xua8295b92021-08-27 17:46:09 +08007102 if (!(req->flags & REQ_F_FAIL)) {
7103 ret = io_req_prep_async(req);
7104 if (unlikely(ret)) {
7105 req_fail_link_node(req, ret);
7106 if (!(head->flags & REQ_F_FAIL))
7107 req_fail_link_node(head, -ECANCELED);
7108 }
7109 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007110 trace_io_uring_link(ctx, req, head);
7111 link->last->link = req;
7112 link->last = req;
7113
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007114 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK))
7115 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007116 /* last request of a link, enqueue the link */
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007117 link->head = NULL;
7118 req = head;
7119 } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
7120 link->head = req;
7121 link->last = req;
7122 return 0;
Jackie Liu4fe2c962019-09-09 20:50:40 +08007123 }
7124
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007125 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007126 return 0;
7127}
7128
7129/*
7130 * Batched submission is done, ensure local IO is flushed out.
7131 */
Pavel Begunkov553deff2021-09-24 21:59:55 +01007132static void io_submit_state_end(struct io_ring_ctx *ctx)
Jens Axboe3529d8c2019-12-19 18:24:38 -07007133{
Pavel Begunkov553deff2021-09-24 21:59:55 +01007134 struct io_submit_state *state = &ctx->submit_state;
7135
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007136 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007137 io_queue_sqe(state->link.head);
Pavel Begunkov553deff2021-09-24 21:59:55 +01007138 /* flush only after queuing links as they can generate completions */
Pavel Begunkovc4501782021-09-08 16:40:52 +01007139 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007140 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007141 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007142}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007143
Jens Axboe9e645e112019-05-10 16:07:28 -06007144/*
7145 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007146 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007147static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007148 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007149{
7150 state->plug_started = false;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007151 state->need_plug = max_ios > 2;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007152 /* set only head, no need to init link_last in advance */
7153 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007154}
7155
Jens Axboe193155c2020-02-22 23:22:19 -07007156static void io_commit_sqring(struct io_ring_ctx *ctx)
7157{
Jens Axboe75c6a032020-01-28 10:15:23 -07007158 struct io_rings *rings = ctx->rings;
7159
7160 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007161 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007162 * since once we write the new head, the application could
7163 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007164 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007165 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007166}
7167
Jens Axboe9e645e112019-05-10 16:07:28 -06007168/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007169 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007170 * that is mapped by userspace. This means that care needs to be taken to
7171 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007172 * being a good citizen. If members of the sqe are validated and then later
7173 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007174 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007175 */
7176static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007177{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007178 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007179 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007180
7181 /*
7182 * The cached sq head (or cq tail) serves two purposes:
7183 *
7184 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007185 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007186 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007187 * though the application is the one updating it.
7188 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007189 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007190 if (likely(head < ctx->sq_entries))
7191 return &ctx->sq_sqes[head];
7192
7193 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007194 ctx->cq_extra--;
7195 WRITE_ONCE(ctx->rings->sq_dropped,
7196 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007197 return NULL;
7198}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007199
Jens Axboe0f212202020-09-13 13:09:39 -06007200static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007201 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007202{
Pavel Begunkov69629802021-09-24 22:00:01 +01007203 unsigned int entries = io_sqring_entries(ctx);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007204 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007205
Pavel Begunkov51d48da2021-10-04 20:02:47 +01007206 if (unlikely(!entries))
Pavel Begunkov69629802021-09-24 22:00:01 +01007207 return 0;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03007208 /* make sure SQ entry isn't read before tail */
Pavel Begunkov69629802021-09-24 22:00:01 +01007209 nr = min3(nr, ctx->sq_entries, entries);
Pavel Begunkov9a108672021-08-27 11:55:01 +01007210 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007211
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007212 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov69629802021-09-24 22:00:01 +01007213 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007214 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007215 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007216
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007217 if (unlikely(!io_alloc_req_refill(ctx))) {
Pavel Begunkov196be952019-11-07 01:41:06 +03007218 if (!submitted)
7219 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007220 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007221 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007222 req = io_alloc_req(ctx);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007223 sqe = io_get_sqe(ctx);
7224 if (unlikely(!sqe)) {
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01007225 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007226 break;
7227 }
Jens Axboed3656342019-12-18 09:50:26 -07007228 /* will complete beyond this point, count as submitted */
7229 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007230 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007231 break;
Pavel Begunkov69629802021-09-24 22:00:01 +01007232 } while (submitted < nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007233
Pavel Begunkov9466f432020-01-25 22:34:01 +03007234 if (unlikely(submitted != nr)) {
7235 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007236 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007237
Pavel Begunkov09899b12021-06-14 02:36:22 +01007238 current->io_uring->cached_refs += unused;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007239 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007240
Pavel Begunkov553deff2021-09-24 21:59:55 +01007241 io_submit_state_end(ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007242 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7243 io_commit_sqring(ctx);
7244
Jens Axboe6c271ce2019-01-10 11:22:30 -07007245 return submitted;
7246}
7247
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007248static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7249{
7250 return READ_ONCE(sqd->state);
7251}
7252
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007253static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7254{
7255 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007256 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007257 WRITE_ONCE(ctx->rings->sq_flags,
7258 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007259 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007260}
7261
7262static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7263{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007264 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007265 WRITE_ONCE(ctx->rings->sq_flags,
7266 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007267 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007268}
7269
Xiaoguang Wang08369242020-11-03 14:15:59 +08007270static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007271{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007272 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007273 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007274
Jens Axboec8d1ba52020-09-14 11:07:26 -06007275 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007276 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007277 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7278 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007279
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007280 if (!wq_list_empty(&ctx->iopoll_list) || to_submit) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007281 const struct cred *creds = NULL;
7282
7283 if (ctx->sq_creds != current_cred())
7284 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007285
Xiaoguang Wang08369242020-11-03 14:15:59 +08007286 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007287 if (!wq_list_empty(&ctx->iopoll_list))
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01007288 io_do_iopoll(ctx, true);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007289
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007290 /*
7291 * Don't submit if refs are dying, good for io_uring_register(),
7292 * but also it is relied upon by io_ring_exit_work()
7293 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007294 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7295 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007296 ret = io_submit_sqes(ctx, to_submit);
7297 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007298
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007299 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7300 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007301 if (creds)
7302 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007303 }
Jens Axboe90554202020-09-03 12:12:41 -06007304
Xiaoguang Wang08369242020-11-03 14:15:59 +08007305 return ret;
7306}
7307
7308static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7309{
7310 struct io_ring_ctx *ctx;
7311 unsigned sq_thread_idle = 0;
7312
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007313 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7314 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007315 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007316}
7317
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007318static bool io_sqd_handle_event(struct io_sq_data *sqd)
7319{
7320 bool did_sig = false;
7321 struct ksignal ksig;
7322
7323 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7324 signal_pending(current)) {
7325 mutex_unlock(&sqd->lock);
7326 if (signal_pending(current))
7327 did_sig = get_signal(&ksig);
7328 cond_resched();
7329 mutex_lock(&sqd->lock);
7330 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007331 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7332}
7333
Jens Axboe6c271ce2019-01-10 11:22:30 -07007334static int io_sq_thread(void *data)
7335{
Jens Axboe69fb2132020-09-14 11:16:23 -06007336 struct io_sq_data *sqd = data;
7337 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007338 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007339 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007340 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007341
Pavel Begunkov696ee882021-04-01 09:55:04 +01007342 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007343 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007344
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007345 if (sqd->sq_cpu != -1)
7346 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7347 else
7348 set_cpus_allowed_ptr(current, cpu_online_mask);
7349 current->flags |= PF_NO_SETAFFINITY;
7350
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007351 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007352 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007353 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007354
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007355 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7356 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007357 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007358 timeout = jiffies + sqd->sq_thread_idle;
7359 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007360
Jens Axboee95eee22020-09-08 09:11:32 -06007361 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007362 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007363 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007364
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007365 if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007366 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007367 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007368 if (io_run_task_work())
7369 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007370
Xiaoguang Wang08369242020-11-03 14:15:59 +08007371 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007372 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007373 if (sqt_spin)
7374 timeout = jiffies + sqd->sq_thread_idle;
7375 continue;
7376 }
7377
Xiaoguang Wang08369242020-11-03 14:15:59 +08007378 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007379 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007380 bool needs_sched = true;
7381
Hao Xu724cb4f2021-04-21 23:19:11 +08007382 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007383 io_ring_set_wakeup_flag(ctx);
7384
Hao Xu724cb4f2021-04-21 23:19:11 +08007385 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007386 !wq_list_empty(&ctx->iopoll_list)) {
Hao Xu724cb4f2021-04-21 23:19:11 +08007387 needs_sched = false;
7388 break;
7389 }
7390 if (io_sqring_entries(ctx)) {
7391 needs_sched = false;
7392 break;
7393 }
7394 }
7395
7396 if (needs_sched) {
7397 mutex_unlock(&sqd->lock);
7398 schedule();
7399 mutex_lock(&sqd->lock);
7400 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007401 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7402 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007403 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007404
7405 finish_wait(&sqd->wait, &wait);
7406 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007407 }
7408
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007409 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007410 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007411 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007412 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007413 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007414 mutex_unlock(&sqd->lock);
7415
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007416 complete(&sqd->exited);
7417 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007418}
7419
Jens Axboebda52162019-09-24 13:47:15 -06007420struct io_wait_queue {
7421 struct wait_queue_entry wq;
7422 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007423 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007424 unsigned nr_timeouts;
7425};
7426
Pavel Begunkov6c503152021-01-04 20:36:36 +00007427static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007428{
7429 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007430 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007431
7432 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007433 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007434 * started waiting. For timeouts, we always want to return to userspace,
7435 * regardless of event count.
7436 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007437 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007438}
7439
7440static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7441 int wake_flags, void *key)
7442{
7443 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7444 wq);
7445
Pavel Begunkov6c503152021-01-04 20:36:36 +00007446 /*
7447 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7448 * the task, and the next invocation will do it.
7449 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007450 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007451 return autoremove_wake_function(curr, mode, wake_flags, key);
7452 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007453}
7454
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007455static int io_run_task_work_sig(void)
7456{
7457 if (io_run_task_work())
7458 return 1;
7459 if (!signal_pending(current))
7460 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007461 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007462 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007463 return -EINTR;
7464}
7465
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007466/* when returns >0, the caller should retry */
7467static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7468 struct io_wait_queue *iowq,
7469 signed long *timeout)
7470{
7471 int ret;
7472
7473 /* make sure we run task_work before checking for signals */
7474 ret = io_run_task_work_sig();
7475 if (ret || io_should_wake(iowq))
7476 return ret;
7477 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007478 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007479 return 1;
7480
7481 *timeout = schedule_timeout(*timeout);
7482 return !*timeout ? -ETIME : 1;
7483}
7484
Jens Axboe2b188cc2019-01-07 10:46:33 -07007485/*
7486 * Wait until events become available, if we don't already have some. The
7487 * application must reap them itself, as they reside on the shared cq ring.
7488 */
7489static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007490 const sigset_t __user *sig, size_t sigsz,
7491 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007492{
Pavel Begunkov902910992021-08-09 09:07:32 -06007493 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007494 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007495 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7496 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007497
Jens Axboeb41e9852020-02-17 09:52:41 -07007498 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007499 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007500 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007501 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007502 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007503 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007504 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007505
Xiaoguang Wang44df58d2021-09-14 22:38:52 +08007506 if (uts) {
7507 struct timespec64 ts;
7508
7509 if (get_timespec64(&ts, uts))
7510 return -EFAULT;
7511 timeout = timespec64_to_jiffies(&ts);
7512 }
7513
Jens Axboe2b188cc2019-01-07 10:46:33 -07007514 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007515#ifdef CONFIG_COMPAT
7516 if (in_compat_syscall())
7517 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007518 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007519 else
7520#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007521 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007522
Jens Axboe2b188cc2019-01-07 10:46:33 -07007523 if (ret)
7524 return ret;
7525 }
7526
Pavel Begunkov902910992021-08-09 09:07:32 -06007527 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7528 iowq.wq.private = current;
7529 INIT_LIST_HEAD(&iowq.wq.entry);
7530 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007531 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007532 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007533
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007534 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007535 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007536 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007537 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007538 ret = -EBUSY;
7539 break;
7540 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007541 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007542 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007543 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007544 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007545 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007546 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007547
Jens Axboeb7db41c2020-07-04 08:55:50 -06007548 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007549
Hristo Venev75b28af2019-08-26 17:23:46 +00007550 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007551}
7552
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007553static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007554{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007555 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007556
7557 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007558 kfree(table[i]);
7559 kfree(table);
7560}
7561
7562static void **io_alloc_page_table(size_t size)
7563{
7564 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7565 size_t init_size = size;
7566 void **table;
7567
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007568 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007569 if (!table)
7570 return NULL;
7571
7572 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007573 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007574
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007575 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007576 if (!table[i]) {
7577 io_free_page_table(table, init_size);
7578 return NULL;
7579 }
7580 size -= this_size;
7581 }
7582 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007583}
7584
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007585static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7586{
7587 percpu_ref_exit(&ref_node->refs);
7588 kfree(ref_node);
7589}
7590
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007591static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
7592{
7593 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7594 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7595 unsigned long flags;
7596 bool first_add = false;
7597
7598 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7599 node->done = true;
7600
7601 while (!list_empty(&ctx->rsrc_ref_list)) {
7602 node = list_first_entry(&ctx->rsrc_ref_list,
7603 struct io_rsrc_node, node);
7604 /* recycle ref nodes in order */
7605 if (!node->done)
7606 break;
7607 list_del(&node->node);
7608 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7609 }
7610 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7611
7612 if (first_add)
7613 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7614}
7615
7616static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7617{
7618 struct io_rsrc_node *ref_node;
7619
7620 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7621 if (!ref_node)
7622 return NULL;
7623
7624 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7625 0, GFP_KERNEL)) {
7626 kfree(ref_node);
7627 return NULL;
7628 }
7629 INIT_LIST_HEAD(&ref_node->node);
7630 INIT_LIST_HEAD(&ref_node->rsrc_list);
7631 ref_node->done = false;
7632 return ref_node;
7633}
7634
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007635static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7636 struct io_rsrc_data *data_to_kill)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007637{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007638 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7639 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007640
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007641 if (data_to_kill) {
7642 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007643
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007644 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007645 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007646 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007647 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007648
Pavel Begunkov3e942492021-04-11 01:46:34 +01007649 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007650 percpu_ref_kill(&rsrc_node->refs);
7651 ctx->rsrc_node = NULL;
7652 }
7653
7654 if (!ctx->rsrc_node) {
7655 ctx->rsrc_node = ctx->rsrc_backup_node;
7656 ctx->rsrc_backup_node = NULL;
7657 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007658}
7659
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007660static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007661{
7662 if (ctx->rsrc_backup_node)
7663 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007664 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007665 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7666}
7667
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007668static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007669{
7670 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007671
Pavel Begunkov215c3902021-04-01 15:43:48 +01007672 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007673 if (data->quiesce)
7674 return -ENXIO;
7675
7676 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007677 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007678 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007679 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007680 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007681 io_rsrc_node_switch(ctx, data);
7682
Pavel Begunkov3e942492021-04-11 01:46:34 +01007683 /* kill initial ref, already quiesced if zero */
7684 if (atomic_dec_and_test(&data->refs))
7685 break;
Jens Axboec018db42021-08-09 08:15:50 -06007686 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007687 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007688 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007689 if (!ret) {
7690 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007691 break;
Jens Axboec018db42021-08-09 08:15:50 -06007692 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007693
Pavel Begunkov3e942492021-04-11 01:46:34 +01007694 atomic_inc(&data->refs);
7695 /* wait for all works potentially completing data->done */
7696 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007697 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007698
Hao Xu8bad28d2021-02-19 17:19:36 +08007699 ret = io_run_task_work_sig();
7700 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007701 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007702 data->quiesce = false;
7703
Hao Xu8bad28d2021-02-19 17:19:36 +08007704 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007705}
7706
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007707static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7708{
7709 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7710 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7711
7712 return &data->tags[table_idx][off];
7713}
7714
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007715static void io_rsrc_data_free(struct io_rsrc_data *data)
7716{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007717 size_t size = data->nr * sizeof(data->tags[0][0]);
7718
7719 if (data->tags)
7720 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007721 kfree(data);
7722}
7723
Pavel Begunkovd878c812021-06-14 02:36:18 +01007724static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7725 u64 __user *utags, unsigned nr,
7726 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007727{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007728 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007729 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007730 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007731
7732 data = kzalloc(sizeof(*data), GFP_KERNEL);
7733 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007734 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007735 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007736 if (!data->tags) {
7737 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007738 return -ENOMEM;
7739 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007740
7741 data->nr = nr;
7742 data->ctx = ctx;
7743 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007744 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007745 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007746 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007747 u64 *tag_slot = io_get_tag_slot(data, i);
7748
7749 if (copy_from_user(tag_slot, &utags[i],
7750 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007751 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007752 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007753 }
7754
Pavel Begunkov3e942492021-04-11 01:46:34 +01007755 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007756 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007757 *pdata = data;
7758 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007759fail:
7760 io_rsrc_data_free(data);
7761 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007762}
7763
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007764static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7765{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007766 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7767 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007768 return !!table->files;
7769}
7770
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007771static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007772{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007773 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007774 table->files = NULL;
7775}
7776
Jens Axboe2b188cc2019-01-07 10:46:33 -07007777static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7778{
7779#if defined(CONFIG_UNIX)
7780 if (ctx->ring_sock) {
7781 struct sock *sock = ctx->ring_sock->sk;
7782 struct sk_buff *skb;
7783
7784 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7785 kfree_skb(skb);
7786 }
7787#else
7788 int i;
7789
7790 for (i = 0; i < ctx->nr_user_files; i++) {
7791 struct file *file;
7792
7793 file = io_file_from_index(ctx, i);
7794 if (file)
7795 fput(file);
7796 }
7797#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007798 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007799 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007800 ctx->file_data = NULL;
7801 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007802}
7803
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007804static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7805{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007806 int ret;
7807
Pavel Begunkov08480402021-04-13 02:58:38 +01007808 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007809 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007810 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7811 if (!ret)
7812 __io_sqe_files_unregister(ctx);
7813 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007814}
7815
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007816static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007817 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007818{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007819 WARN_ON_ONCE(sqd->thread == current);
7820
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007821 /*
7822 * Do the dance but not conditional clear_bit() because it'd race with
7823 * other threads incrementing park_pending and setting the bit.
7824 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007825 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007826 if (atomic_dec_return(&sqd->park_pending))
7827 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007828 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007829}
7830
Jens Axboe86e0d672021-03-05 08:44:39 -07007831static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007832 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007833{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007834 WARN_ON_ONCE(sqd->thread == current);
7835
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007836 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007837 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007838 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007839 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007840 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007841}
7842
7843static void io_sq_thread_stop(struct io_sq_data *sqd)
7844{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007845 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007846 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007847
Jens Axboe05962f92021-03-06 13:58:48 -07007848 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007849 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007850 if (sqd->thread)
7851 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007852 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007853 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007854}
7855
Jens Axboe534ca6d2020-09-02 13:52:19 -06007856static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007857{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007858 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007859 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7860
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007861 io_sq_thread_stop(sqd);
7862 kfree(sqd);
7863 }
7864}
7865
7866static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7867{
7868 struct io_sq_data *sqd = ctx->sq_data;
7869
7870 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007871 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007872 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007873 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007874 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007875
7876 io_put_sq_data(sqd);
7877 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007878 }
7879}
7880
Jens Axboeaa061652020-09-02 14:50:27 -06007881static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7882{
7883 struct io_ring_ctx *ctx_attach;
7884 struct io_sq_data *sqd;
7885 struct fd f;
7886
7887 f = fdget(p->wq_fd);
7888 if (!f.file)
7889 return ERR_PTR(-ENXIO);
7890 if (f.file->f_op != &io_uring_fops) {
7891 fdput(f);
7892 return ERR_PTR(-EINVAL);
7893 }
7894
7895 ctx_attach = f.file->private_data;
7896 sqd = ctx_attach->sq_data;
7897 if (!sqd) {
7898 fdput(f);
7899 return ERR_PTR(-EINVAL);
7900 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007901 if (sqd->task_tgid != current->tgid) {
7902 fdput(f);
7903 return ERR_PTR(-EPERM);
7904 }
Jens Axboeaa061652020-09-02 14:50:27 -06007905
7906 refcount_inc(&sqd->refs);
7907 fdput(f);
7908 return sqd;
7909}
7910
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007911static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7912 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007913{
7914 struct io_sq_data *sqd;
7915
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007916 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007917 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7918 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007919 if (!IS_ERR(sqd)) {
7920 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007921 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007922 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007923 /* fall through for EPERM case, setup new sqd/task */
7924 if (PTR_ERR(sqd) != -EPERM)
7925 return sqd;
7926 }
Jens Axboeaa061652020-09-02 14:50:27 -06007927
Jens Axboe534ca6d2020-09-02 13:52:19 -06007928 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7929 if (!sqd)
7930 return ERR_PTR(-ENOMEM);
7931
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007932 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007933 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007934 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007935 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007936 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007937 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007938 return sqd;
7939}
7940
Jens Axboe6b063142019-01-10 22:13:58 -07007941#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007942/*
7943 * Ensure the UNIX gc is aware of our file set, so we are certain that
7944 * the io_uring can be safely unregistered on process exit, even if we have
7945 * loops in the file referencing.
7946 */
7947static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7948{
7949 struct sock *sk = ctx->ring_sock->sk;
7950 struct scm_fp_list *fpl;
7951 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007952 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007953
Jens Axboe6b063142019-01-10 22:13:58 -07007954 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7955 if (!fpl)
7956 return -ENOMEM;
7957
7958 skb = alloc_skb(0, GFP_KERNEL);
7959 if (!skb) {
7960 kfree(fpl);
7961 return -ENOMEM;
7962 }
7963
7964 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007965
Jens Axboe08a45172019-10-03 08:11:03 -06007966 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007967 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007968 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007969 struct file *file = io_file_from_index(ctx, i + offset);
7970
7971 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007972 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007973 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007974 unix_inflight(fpl->user, fpl->fp[nr_files]);
7975 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007976 }
7977
Jens Axboe08a45172019-10-03 08:11:03 -06007978 if (nr_files) {
7979 fpl->max = SCM_MAX_FD;
7980 fpl->count = nr_files;
7981 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007982 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007983 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7984 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007985
Jens Axboe08a45172019-10-03 08:11:03 -06007986 for (i = 0; i < nr_files; i++)
7987 fput(fpl->fp[i]);
7988 } else {
7989 kfree_skb(skb);
7990 kfree(fpl);
7991 }
Jens Axboe6b063142019-01-10 22:13:58 -07007992
7993 return 0;
7994}
7995
7996/*
7997 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7998 * causes regular reference counting to break down. We rely on the UNIX
7999 * garbage collection to take care of this problem for us.
8000 */
8001static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8002{
8003 unsigned left, total;
8004 int ret = 0;
8005
8006 total = 0;
8007 left = ctx->nr_user_files;
8008 while (left) {
8009 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008010
8011 ret = __io_sqe_files_scm(ctx, this_files, total);
8012 if (ret)
8013 break;
8014 left -= this_files;
8015 total += this_files;
8016 }
8017
8018 if (!ret)
8019 return 0;
8020
8021 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008022 struct file *file = io_file_from_index(ctx, total);
8023
8024 if (file)
8025 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008026 total++;
8027 }
8028
8029 return ret;
8030}
8031#else
8032static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8033{
8034 return 0;
8035}
8036#endif
8037
Pavel Begunkov47e90392021-04-01 15:43:56 +01008038static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008039{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008040 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008041#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008042 struct sock *sock = ctx->ring_sock->sk;
8043 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8044 struct sk_buff *skb;
8045 int i;
8046
8047 __skb_queue_head_init(&list);
8048
8049 /*
8050 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8051 * remove this entry and rearrange the file array.
8052 */
8053 skb = skb_dequeue(head);
8054 while (skb) {
8055 struct scm_fp_list *fp;
8056
8057 fp = UNIXCB(skb).fp;
8058 for (i = 0; i < fp->count; i++) {
8059 int left;
8060
8061 if (fp->fp[i] != file)
8062 continue;
8063
8064 unix_notinflight(fp->user, fp->fp[i]);
8065 left = fp->count - 1 - i;
8066 if (left) {
8067 memmove(&fp->fp[i], &fp->fp[i + 1],
8068 left * sizeof(struct file *));
8069 }
8070 fp->count--;
8071 if (!fp->count) {
8072 kfree_skb(skb);
8073 skb = NULL;
8074 } else {
8075 __skb_queue_tail(&list, skb);
8076 }
8077 fput(file);
8078 file = NULL;
8079 break;
8080 }
8081
8082 if (!file)
8083 break;
8084
8085 __skb_queue_tail(&list, skb);
8086
8087 skb = skb_dequeue(head);
8088 }
8089
8090 if (skb_peek(&list)) {
8091 spin_lock_irq(&head->lock);
8092 while ((skb = __skb_dequeue(&list)) != NULL)
8093 __skb_queue_tail(head, skb);
8094 spin_unlock_irq(&head->lock);
8095 }
8096#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008097 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008098#endif
8099}
8100
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008101static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008102{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008103 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008104 struct io_ring_ctx *ctx = rsrc_data->ctx;
8105 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008106
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008107 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8108 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008109
8110 if (prsrc->tag) {
8111 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008112
8113 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008114 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008115 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01008116 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008117 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008118 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008119 io_cqring_ev_posted(ctx);
8120 io_ring_submit_unlock(ctx, lock_ring);
8121 }
8122
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008123 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008124 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008125 }
8126
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008127 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008128 if (atomic_dec_and_test(&rsrc_data->refs))
8129 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008130}
8131
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008132static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008133{
8134 struct io_ring_ctx *ctx;
8135 struct llist_node *node;
8136
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008137 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8138 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008139
8140 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008141 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008142 struct llist_node *next = node->next;
8143
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008144 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008145 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008146 node = next;
8147 }
8148}
8149
Jens Axboe05f3fb32019-12-09 11:22:50 -07008150static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008151 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008152{
8153 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008154 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008155 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008156 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008157
8158 if (ctx->file_data)
8159 return -EBUSY;
8160 if (!nr_args)
8161 return -EINVAL;
8162 if (nr_args > IORING_MAX_FIXED_FILES)
8163 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008164 if (nr_args > rlimit(RLIMIT_NOFILE))
8165 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008166 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008167 if (ret)
8168 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008169 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8170 &ctx->file_data);
8171 if (ret)
8172 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008173
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008174 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008175 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008176 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008177
Jens Axboe05f3fb32019-12-09 11:22:50 -07008178 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008179 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008180 ret = -EFAULT;
8181 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008182 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008183 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008184 if (fd == -1) {
8185 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008186 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008187 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008188 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008189 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008190
Jens Axboe05f3fb32019-12-09 11:22:50 -07008191 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008192 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008193 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008194 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008195
8196 /*
8197 * Don't allow io_uring instances to be registered. If UNIX
8198 * isn't enabled, then this causes a reference cycle and this
8199 * instance can never get freed. If UNIX is enabled we'll
8200 * handle it just fine, but there's still no point in allowing
8201 * a ring fd as it doesn't support regular read/write anyway.
8202 */
8203 if (file->f_op == &io_uring_fops) {
8204 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008205 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008206 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008207 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008208 }
8209
Jens Axboe05f3fb32019-12-09 11:22:50 -07008210 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008211 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008212 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008213 return ret;
8214 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008215
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008216 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008217 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008218out_fput:
8219 for (i = 0; i < ctx->nr_user_files; i++) {
8220 file = io_file_from_index(ctx, i);
8221 if (file)
8222 fput(file);
8223 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008224 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008225 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008226out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008227 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008228 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008229 return ret;
8230}
8231
Jens Axboec3a31e62019-10-03 13:59:56 -06008232static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8233 int index)
8234{
8235#if defined(CONFIG_UNIX)
8236 struct sock *sock = ctx->ring_sock->sk;
8237 struct sk_buff_head *head = &sock->sk_receive_queue;
8238 struct sk_buff *skb;
8239
8240 /*
8241 * See if we can merge this file into an existing skb SCM_RIGHTS
8242 * file set. If there's no room, fall back to allocating a new skb
8243 * and filling it in.
8244 */
8245 spin_lock_irq(&head->lock);
8246 skb = skb_peek(head);
8247 if (skb) {
8248 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8249
8250 if (fpl->count < SCM_MAX_FD) {
8251 __skb_unlink(skb, head);
8252 spin_unlock_irq(&head->lock);
8253 fpl->fp[fpl->count] = get_file(file);
8254 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8255 fpl->count++;
8256 spin_lock_irq(&head->lock);
8257 __skb_queue_head(head, skb);
8258 } else {
8259 skb = NULL;
8260 }
8261 }
8262 spin_unlock_irq(&head->lock);
8263
8264 if (skb) {
8265 fput(file);
8266 return 0;
8267 }
8268
8269 return __io_sqe_files_scm(ctx, 1, index);
8270#else
8271 return 0;
8272#endif
8273}
8274
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008275static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
8276 struct io_rsrc_node *node, void *rsrc)
8277{
8278 struct io_rsrc_put *prsrc;
8279
8280 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8281 if (!prsrc)
8282 return -ENOMEM;
8283
8284 prsrc->tag = *io_get_tag_slot(data, idx);
8285 prsrc->rsrc = rsrc;
8286 list_add(&prsrc->list, &node->rsrc_list);
8287 return 0;
8288}
8289
Pavel Begunkovb9445592021-08-25 12:25:45 +01008290static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8291 unsigned int issue_flags, u32 slot_index)
8292{
8293 struct io_ring_ctx *ctx = req->ctx;
8294 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008295 bool needs_switch = false;
Pavel Begunkovb9445592021-08-25 12:25:45 +01008296 struct io_fixed_file *file_slot;
8297 int ret = -EBADF;
8298
8299 io_ring_submit_lock(ctx, !force_nonblock);
8300 if (file->f_op == &io_uring_fops)
8301 goto err;
8302 ret = -ENXIO;
8303 if (!ctx->file_data)
8304 goto err;
8305 ret = -EINVAL;
8306 if (slot_index >= ctx->nr_user_files)
8307 goto err;
8308
8309 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8310 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008311
8312 if (file_slot->file_ptr) {
8313 struct file *old_file;
8314
8315 ret = io_rsrc_node_switch_start(ctx);
8316 if (ret)
8317 goto err;
8318
8319 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8320 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
8321 ctx->rsrc_node, old_file);
8322 if (ret)
8323 goto err;
8324 file_slot->file_ptr = 0;
8325 needs_switch = true;
8326 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01008327
8328 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8329 io_fixed_file_set(file_slot, file);
8330 ret = io_sqe_file_register(ctx, file, slot_index);
8331 if (ret) {
8332 file_slot->file_ptr = 0;
8333 goto err;
8334 }
8335
8336 ret = 0;
8337err:
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008338 if (needs_switch)
8339 io_rsrc_node_switch(ctx, ctx->file_data);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008340 io_ring_submit_unlock(ctx, !force_nonblock);
8341 if (ret)
8342 fput(file);
8343 return ret;
8344}
8345
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008346static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
8347{
8348 unsigned int offset = req->close.file_slot - 1;
8349 struct io_ring_ctx *ctx = req->ctx;
8350 struct io_fixed_file *file_slot;
8351 struct file *file;
8352 int ret, i;
8353
8354 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
8355 ret = -ENXIO;
8356 if (unlikely(!ctx->file_data))
8357 goto out;
8358 ret = -EINVAL;
8359 if (offset >= ctx->nr_user_files)
8360 goto out;
8361 ret = io_rsrc_node_switch_start(ctx);
8362 if (ret)
8363 goto out;
8364
8365 i = array_index_nospec(offset, ctx->nr_user_files);
8366 file_slot = io_fixed_file_slot(&ctx->file_table, i);
8367 ret = -EBADF;
8368 if (!file_slot->file_ptr)
8369 goto out;
8370
8371 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8372 ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
8373 if (ret)
8374 goto out;
8375
8376 file_slot->file_ptr = 0;
8377 io_rsrc_node_switch(ctx, ctx->file_data);
8378 ret = 0;
8379out:
8380 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
8381 return ret;
8382}
8383
Jens Axboe05f3fb32019-12-09 11:22:50 -07008384static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008385 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008386 unsigned nr_args)
8387{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008388 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008389 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008390 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008391 struct io_fixed_file *file_slot;
8392 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008393 int fd, i, err = 0;
8394 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008395 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008396
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008397 if (!ctx->file_data)
8398 return -ENXIO;
8399 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008400 return -EINVAL;
8401
Pavel Begunkov67973b92021-01-26 13:51:09 +00008402 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008403 u64 tag = 0;
8404
8405 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8406 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008407 err = -EFAULT;
8408 break;
8409 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008410 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8411 err = -EINVAL;
8412 break;
8413 }
noah4e0377a2021-01-26 15:23:28 -05008414 if (fd == IORING_REGISTER_FILES_SKIP)
8415 continue;
8416
Pavel Begunkov67973b92021-01-26 13:51:09 +00008417 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008418 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008419
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008420 if (file_slot->file_ptr) {
8421 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008422 err = io_queue_rsrc_removal(data, up->offset + done,
8423 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008424 if (err)
8425 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008426 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008427 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008428 }
8429 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008430 file = fget(fd);
8431 if (!file) {
8432 err = -EBADF;
8433 break;
8434 }
8435 /*
8436 * Don't allow io_uring instances to be registered. If
8437 * UNIX isn't enabled, then this causes a reference
8438 * cycle and this instance can never get freed. If UNIX
8439 * is enabled we'll handle it just fine, but there's
8440 * still no point in allowing a ring fd as it doesn't
8441 * support regular read/write anyway.
8442 */
8443 if (file->f_op == &io_uring_fops) {
8444 fput(file);
8445 err = -EBADF;
8446 break;
8447 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008448 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008449 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008450 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008451 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008452 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008453 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008454 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008455 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008456 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008457 }
8458
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008459 if (needs_switch)
8460 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008461 return done ? done : err;
8462}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008463
Jens Axboe685fe7f2021-03-08 09:37:51 -07008464static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8465 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008466{
Jens Axboee9418942021-02-19 12:33:30 -07008467 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008468 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008469 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008470
Yang Yingliang362a9e62021-07-20 16:38:05 +08008471 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008472 hash = ctx->hash_map;
8473 if (!hash) {
8474 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008475 if (!hash) {
8476 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008477 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008478 }
Jens Axboee9418942021-02-19 12:33:30 -07008479 refcount_set(&hash->refs, 1);
8480 init_waitqueue_head(&hash->wait);
8481 ctx->hash_map = hash;
8482 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008483 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008484
8485 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008486 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008487 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008488 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008489
Jens Axboed25e3a32021-02-16 11:41:41 -07008490 /* Do QD, or 4 * CPUS, whatever is smallest */
8491 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008492
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008493 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008494}
8495
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008496static int io_uring_alloc_task_context(struct task_struct *task,
8497 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008498{
8499 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008500 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008501
Pavel Begunkov09899b12021-06-14 02:36:22 +01008502 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008503 if (unlikely(!tctx))
8504 return -ENOMEM;
8505
Jens Axboed8a6df12020-10-15 16:24:45 -06008506 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8507 if (unlikely(ret)) {
8508 kfree(tctx);
8509 return ret;
8510 }
8511
Jens Axboe685fe7f2021-03-08 09:37:51 -07008512 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008513 if (IS_ERR(tctx->io_wq)) {
8514 ret = PTR_ERR(tctx->io_wq);
8515 percpu_counter_destroy(&tctx->inflight);
8516 kfree(tctx);
8517 return ret;
8518 }
8519
Jens Axboe0f212202020-09-13 13:09:39 -06008520 xa_init(&tctx->xa);
8521 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008522 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008523 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008524 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008525 spin_lock_init(&tctx->task_lock);
8526 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008527 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008528 return 0;
8529}
8530
8531void __io_uring_free(struct task_struct *tsk)
8532{
8533 struct io_uring_task *tctx = tsk->io_uring;
8534
8535 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008536 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008537 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008538
Jens Axboed8a6df12020-10-15 16:24:45 -06008539 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008540 kfree(tctx);
8541 tsk->io_uring = NULL;
8542}
8543
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008544static int io_sq_offload_create(struct io_ring_ctx *ctx,
8545 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008546{
8547 int ret;
8548
Jens Axboed25e3a32021-02-16 11:41:41 -07008549 /* Retain compatibility with failing for an invalid attach attempt */
8550 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8551 IORING_SETUP_ATTACH_WQ) {
8552 struct fd f;
8553
8554 f = fdget(p->wq_fd);
8555 if (!f.file)
8556 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008557 if (f.file->f_op != &io_uring_fops) {
8558 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008559 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008560 }
8561 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008562 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008563 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008564 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008565 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008566 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008567
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008568 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008569 if (IS_ERR(sqd)) {
8570 ret = PTR_ERR(sqd);
8571 goto err;
8572 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008573
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008574 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008575 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008576 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8577 if (!ctx->sq_thread_idle)
8578 ctx->sq_thread_idle = HZ;
8579
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008580 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008581 list_add(&ctx->sqd_list, &sqd->ctx_list);
8582 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008583 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008584 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008585 io_sq_thread_unpark(sqd);
8586
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008587 if (ret < 0)
8588 goto err;
8589 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008590 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008591
Jens Axboe6c271ce2019-01-10 11:22:30 -07008592 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008593 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008594
Jens Axboe917257d2019-04-13 09:28:55 -06008595 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008596 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008597 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008598 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008599 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008600 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008601 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008602
8603 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008604 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008605 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8606 if (IS_ERR(tsk)) {
8607 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008608 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008609 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008610
Jens Axboe46fe18b2021-03-04 12:39:36 -07008611 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008612 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008613 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008614 if (ret)
8615 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008616 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8617 /* Can't have SQ_AFF without SQPOLL */
8618 ret = -EINVAL;
8619 goto err;
8620 }
8621
Jens Axboe2b188cc2019-01-07 10:46:33 -07008622 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008623err_sqpoll:
8624 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008625err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008626 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008627 return ret;
8628}
8629
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008630static inline void __io_unaccount_mem(struct user_struct *user,
8631 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008632{
8633 atomic_long_sub(nr_pages, &user->locked_vm);
8634}
8635
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008636static inline int __io_account_mem(struct user_struct *user,
8637 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008638{
8639 unsigned long page_limit, cur_pages, new_pages;
8640
8641 /* Don't allow more pages than we can safely lock */
8642 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8643
8644 do {
8645 cur_pages = atomic_long_read(&user->locked_vm);
8646 new_pages = cur_pages + nr_pages;
8647 if (new_pages > page_limit)
8648 return -ENOMEM;
8649 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8650 new_pages) != cur_pages);
8651
8652 return 0;
8653}
8654
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008655static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008656{
Jens Axboe62e398b2021-02-21 16:19:37 -07008657 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008658 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008659
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008660 if (ctx->mm_account)
8661 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008662}
8663
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008664static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008665{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008666 int ret;
8667
Jens Axboe62e398b2021-02-21 16:19:37 -07008668 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008669 ret = __io_account_mem(ctx->user, nr_pages);
8670 if (ret)
8671 return ret;
8672 }
8673
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008674 if (ctx->mm_account)
8675 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008676
8677 return 0;
8678}
8679
Jens Axboe2b188cc2019-01-07 10:46:33 -07008680static void io_mem_free(void *ptr)
8681{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008682 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008683
Mark Rutland52e04ef2019-04-30 17:30:21 +01008684 if (!ptr)
8685 return;
8686
8687 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008688 if (put_page_testzero(page))
8689 free_compound_page(page);
8690}
8691
8692static void *io_mem_alloc(size_t size)
8693{
8694 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008695 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008696
8697 return (void *) __get_free_pages(gfp_flags, get_order(size));
8698}
8699
Hristo Venev75b28af2019-08-26 17:23:46 +00008700static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8701 size_t *sq_offset)
8702{
8703 struct io_rings *rings;
8704 size_t off, sq_array_size;
8705
8706 off = struct_size(rings, cqes, cq_entries);
8707 if (off == SIZE_MAX)
8708 return SIZE_MAX;
8709
8710#ifdef CONFIG_SMP
8711 off = ALIGN(off, SMP_CACHE_BYTES);
8712 if (off == 0)
8713 return SIZE_MAX;
8714#endif
8715
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008716 if (sq_offset)
8717 *sq_offset = off;
8718
Hristo Venev75b28af2019-08-26 17:23:46 +00008719 sq_array_size = array_size(sizeof(u32), sq_entries);
8720 if (sq_array_size == SIZE_MAX)
8721 return SIZE_MAX;
8722
8723 if (check_add_overflow(off, sq_array_size, &off))
8724 return SIZE_MAX;
8725
Hristo Venev75b28af2019-08-26 17:23:46 +00008726 return off;
8727}
8728
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008729static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008730{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008731 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008732 unsigned int i;
8733
Pavel Begunkov62248432021-04-28 13:11:29 +01008734 if (imu != ctx->dummy_ubuf) {
8735 for (i = 0; i < imu->nr_bvecs; i++)
8736 unpin_user_page(imu->bvec[i].bv_page);
8737 if (imu->acct_pages)
8738 io_unaccount_mem(ctx, imu->acct_pages);
8739 kvfree(imu);
8740 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008741 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008742}
8743
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008744static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8745{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008746 io_buffer_unmap(ctx, &prsrc->buf);
8747 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008748}
8749
8750static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008751{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008752 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008753
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008754 for (i = 0; i < ctx->nr_user_bufs; i++)
8755 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008756 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008757 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008758 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008759 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008760 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008761}
8762
Jens Axboeedafcce2019-01-09 09:16:05 -07008763static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8764{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008765 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008766
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008767 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008768 return -ENXIO;
8769
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008770 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8771 if (!ret)
8772 __io_sqe_buffers_unregister(ctx);
8773 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008774}
8775
8776static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8777 void __user *arg, unsigned index)
8778{
8779 struct iovec __user *src;
8780
8781#ifdef CONFIG_COMPAT
8782 if (ctx->compat) {
8783 struct compat_iovec __user *ciovs;
8784 struct compat_iovec ciov;
8785
8786 ciovs = (struct compat_iovec __user *) arg;
8787 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8788 return -EFAULT;
8789
Jens Axboed55e5f52019-12-11 16:12:15 -07008790 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008791 dst->iov_len = ciov.iov_len;
8792 return 0;
8793 }
8794#endif
8795 src = (struct iovec __user *) arg;
8796 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8797 return -EFAULT;
8798 return 0;
8799}
8800
Jens Axboede293932020-09-17 16:19:16 -06008801/*
8802 * Not super efficient, but this is just a registration time. And we do cache
8803 * the last compound head, so generally we'll only do a full search if we don't
8804 * match that one.
8805 *
8806 * We check if the given compound head page has already been accounted, to
8807 * avoid double accounting it. This allows us to account the full size of the
8808 * page, not just the constituent pages of a huge page.
8809 */
8810static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8811 int nr_pages, struct page *hpage)
8812{
8813 int i, j;
8814
8815 /* check current page array */
8816 for (i = 0; i < nr_pages; i++) {
8817 if (!PageCompound(pages[i]))
8818 continue;
8819 if (compound_head(pages[i]) == hpage)
8820 return true;
8821 }
8822
8823 /* check previously registered pages */
8824 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008825 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008826
8827 for (j = 0; j < imu->nr_bvecs; j++) {
8828 if (!PageCompound(imu->bvec[j].bv_page))
8829 continue;
8830 if (compound_head(imu->bvec[j].bv_page) == hpage)
8831 return true;
8832 }
8833 }
8834
8835 return false;
8836}
8837
8838static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8839 int nr_pages, struct io_mapped_ubuf *imu,
8840 struct page **last_hpage)
8841{
8842 int i, ret;
8843
Pavel Begunkov216e5832021-05-29 12:01:02 +01008844 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008845 for (i = 0; i < nr_pages; i++) {
8846 if (!PageCompound(pages[i])) {
8847 imu->acct_pages++;
8848 } else {
8849 struct page *hpage;
8850
8851 hpage = compound_head(pages[i]);
8852 if (hpage == *last_hpage)
8853 continue;
8854 *last_hpage = hpage;
8855 if (headpage_already_acct(ctx, pages, i, hpage))
8856 continue;
8857 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8858 }
8859 }
8860
8861 if (!imu->acct_pages)
8862 return 0;
8863
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008864 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008865 if (ret)
8866 imu->acct_pages = 0;
8867 return ret;
8868}
8869
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008870static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008871 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008872 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008873{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008874 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008875 struct vm_area_struct **vmas = NULL;
8876 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008877 unsigned long off, start, end, ubuf;
8878 size_t size;
8879 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008880
Pavel Begunkov62248432021-04-28 13:11:29 +01008881 if (!iov->iov_base) {
8882 *pimu = ctx->dummy_ubuf;
8883 return 0;
8884 }
8885
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008886 ubuf = (unsigned long) iov->iov_base;
8887 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8888 start = ubuf >> PAGE_SHIFT;
8889 nr_pages = end - start;
8890
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008891 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008892 ret = -ENOMEM;
8893
8894 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8895 if (!pages)
8896 goto done;
8897
8898 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8899 GFP_KERNEL);
8900 if (!vmas)
8901 goto done;
8902
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008903 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008904 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008905 goto done;
8906
8907 ret = 0;
8908 mmap_read_lock(current->mm);
8909 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8910 pages, vmas);
8911 if (pret == nr_pages) {
8912 /* don't support file backed memory */
8913 for (i = 0; i < nr_pages; i++) {
8914 struct vm_area_struct *vma = vmas[i];
8915
Pavel Begunkov40dad762021-06-09 15:26:54 +01008916 if (vma_is_shmem(vma))
8917 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008918 if (vma->vm_file &&
8919 !is_file_hugepages(vma->vm_file)) {
8920 ret = -EOPNOTSUPP;
8921 break;
8922 }
8923 }
8924 } else {
8925 ret = pret < 0 ? pret : -EFAULT;
8926 }
8927 mmap_read_unlock(current->mm);
8928 if (ret) {
8929 /*
8930 * if we did partial map, or found file backed vmas,
8931 * release any pages we did get
8932 */
8933 if (pret > 0)
8934 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008935 goto done;
8936 }
8937
8938 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8939 if (ret) {
8940 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008941 goto done;
8942 }
8943
8944 off = ubuf & ~PAGE_MASK;
8945 size = iov->iov_len;
8946 for (i = 0; i < nr_pages; i++) {
8947 size_t vec_len;
8948
8949 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8950 imu->bvec[i].bv_page = pages[i];
8951 imu->bvec[i].bv_len = vec_len;
8952 imu->bvec[i].bv_offset = off;
8953 off = 0;
8954 size -= vec_len;
8955 }
8956 /* store original address for later verification */
8957 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01008958 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008959 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008960 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008961 ret = 0;
8962done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008963 if (ret)
8964 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008965 kvfree(pages);
8966 kvfree(vmas);
8967 return ret;
8968}
8969
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008970static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008971{
Pavel Begunkov87094462021-04-11 01:46:36 +01008972 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
8973 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008974}
8975
8976static int io_buffer_validate(struct iovec *iov)
8977{
Pavel Begunkov50e96982021-03-24 22:59:01 +00008978 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
8979
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008980 /*
8981 * Don't impose further limits on the size and buffer
8982 * constraints here, we'll -EINVAL later when IO is
8983 * submitted if they are wrong.
8984 */
Pavel Begunkov62248432021-04-28 13:11:29 +01008985 if (!iov->iov_base)
8986 return iov->iov_len ? -EFAULT : 0;
8987 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008988 return -EFAULT;
8989
8990 /* arbitrary limit, but we need something */
8991 if (iov->iov_len > SZ_1G)
8992 return -EFAULT;
8993
Pavel Begunkov50e96982021-03-24 22:59:01 +00008994 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
8995 return -EOVERFLOW;
8996
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008997 return 0;
8998}
8999
9000static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009001 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009002{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009003 struct page *last_hpage = NULL;
9004 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009005 int i, ret;
9006 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009007
Pavel Begunkov87094462021-04-11 01:46:36 +01009008 if (ctx->user_bufs)
9009 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01009010 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01009011 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009012 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009013 if (ret)
9014 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01009015 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9016 if (ret)
9017 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009018 ret = io_buffers_map_alloc(ctx, nr_args);
9019 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08009020 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009021 return ret;
9022 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009023
Pavel Begunkov87094462021-04-11 01:46:36 +01009024 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07009025 ret = io_copy_iov(ctx, &iov, arg, i);
9026 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009027 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009028 ret = io_buffer_validate(&iov);
9029 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009030 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009031 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009032 ret = -EINVAL;
9033 break;
9034 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009035
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009036 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9037 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009038 if (ret)
9039 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009040 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009041
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009042 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009043
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009044 ctx->buf_data = data;
9045 if (ret)
9046 __io_sqe_buffers_unregister(ctx);
9047 else
9048 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009049 return ret;
9050}
9051
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009052static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9053 struct io_uring_rsrc_update2 *up,
9054 unsigned int nr_args)
9055{
9056 u64 __user *tags = u64_to_user_ptr(up->tags);
9057 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009058 struct page *last_hpage = NULL;
9059 bool needs_switch = false;
9060 __u32 done;
9061 int i, err;
9062
9063 if (!ctx->buf_data)
9064 return -ENXIO;
9065 if (up->offset + nr_args > ctx->nr_user_bufs)
9066 return -EINVAL;
9067
9068 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009069 struct io_mapped_ubuf *imu;
9070 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009071 u64 tag = 0;
9072
9073 err = io_copy_iov(ctx, &iov, iovs, done);
9074 if (err)
9075 break;
9076 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9077 err = -EFAULT;
9078 break;
9079 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009080 err = io_buffer_validate(&iov);
9081 if (err)
9082 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009083 if (!iov.iov_base && tag) {
9084 err = -EINVAL;
9085 break;
9086 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009087 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9088 if (err)
9089 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009090
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009091 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009092 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009093 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9094 ctx->rsrc_node, ctx->user_bufs[i]);
9095 if (unlikely(err)) {
9096 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009097 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009098 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009099 ctx->user_bufs[i] = NULL;
9100 needs_switch = true;
9101 }
9102
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009103 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009104 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009105 }
9106
9107 if (needs_switch)
9108 io_rsrc_node_switch(ctx, ctx->buf_data);
9109 return done ? done : err;
9110}
9111
Jens Axboe9b402842019-04-11 11:45:41 -06009112static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9113{
9114 __s32 __user *fds = arg;
9115 int fd;
9116
9117 if (ctx->cq_ev_fd)
9118 return -EBUSY;
9119
9120 if (copy_from_user(&fd, fds, sizeof(*fds)))
9121 return -EFAULT;
9122
9123 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9124 if (IS_ERR(ctx->cq_ev_fd)) {
9125 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009126
Jens Axboe9b402842019-04-11 11:45:41 -06009127 ctx->cq_ev_fd = NULL;
9128 return ret;
9129 }
9130
9131 return 0;
9132}
9133
9134static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9135{
9136 if (ctx->cq_ev_fd) {
9137 eventfd_ctx_put(ctx->cq_ev_fd);
9138 ctx->cq_ev_fd = NULL;
9139 return 0;
9140 }
9141
9142 return -ENXIO;
9143}
9144
Jens Axboe5a2e7452020-02-23 16:23:11 -07009145static void io_destroy_buffers(struct io_ring_ctx *ctx)
9146{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009147 struct io_buffer *buf;
9148 unsigned long index;
9149
Jens Axboe8bab4c02021-09-24 07:12:27 -06009150 xa_for_each(&ctx->io_buffers, index, buf) {
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009151 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009152 cond_resched();
9153 }
Jens Axboe5a2e7452020-02-23 16:23:11 -07009154}
9155
Jens Axboe4010fec2021-02-27 15:04:18 -07009156static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009157{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009158 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009159 int nr = 0;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009160
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009161 mutex_lock(&ctx->uring_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009162 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009163
9164 while (state->free_list.next) {
9165 struct io_wq_work_node *node;
9166 struct io_kiocb *req;
9167
9168 node = wq_stack_extract(&state->free_list);
9169 req = container_of(node, struct io_kiocb, comp_list);
9170 kmem_cache_free(req_cachep, req);
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009171 nr++;
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009172 }
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009173 if (nr)
9174 percpu_ref_put_many(&ctx->refs, nr);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009175 mutex_unlock(&ctx->uring_lock);
9176}
9177
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009178static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009179{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009180 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009181 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009182}
9183
Jens Axboe2b188cc2019-01-07 10:46:33 -07009184static void io_ring_ctx_free(struct io_ring_ctx *ctx)
9185{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009186 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009187
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009188 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009189 mmdrop(ctx->mm_account);
9190 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009191 }
Jens Axboedef596e2019-01-09 08:59:42 -07009192
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009193 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9194 io_wait_rsrc_data(ctx->buf_data);
9195 io_wait_rsrc_data(ctx->file_data);
9196
Hao Xu8bad28d2021-02-19 17:19:36 +08009197 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009198 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009199 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009200 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009201 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009202 if (ctx->rings)
9203 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009204 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009205 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009206 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009207 if (ctx->sq_creds)
9208 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009209
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009210 /* there are no registered resources left, nobody uses it */
9211 if (ctx->rsrc_node)
9212 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009213 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009214 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009215 flush_delayed_work(&ctx->rsrc_put_work);
9216
9217 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9218 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009219
9220#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07009221 if (ctx->ring_sock) {
9222 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009223 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07009224 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009225#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009226 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009227
Hristo Venev75b28af2019-08-26 17:23:46 +00009228 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009229 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009230
9231 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009232 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009233 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009234 if (ctx->hash_map)
9235 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009236 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009237 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009238 kfree(ctx);
9239}
9240
9241static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9242{
9243 struct io_ring_ctx *ctx = file->private_data;
9244 __poll_t mask = 0;
9245
Pavel Begunkovd60aa652021-10-04 20:02:52 +01009246 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009247 /*
9248 * synchronizes with barrier from wq_has_sleeper call in
9249 * io_commit_cqring
9250 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009251 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009252 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009253 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009254
9255 /*
9256 * Don't flush cqring overflow list here, just do a simple check.
9257 * Otherwise there could possible be ABBA deadlock:
9258 * CPU0 CPU1
9259 * ---- ----
9260 * lock(&ctx->uring_lock);
9261 * lock(&ep->mtx);
9262 * lock(&ctx->uring_lock);
9263 * lock(&ep->mtx);
9264 *
9265 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9266 * pushs them to do the flush.
9267 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009268 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009269 mask |= EPOLLIN | EPOLLRDNORM;
9270
9271 return mask;
9272}
9273
Yejune Deng0bead8c2020-12-24 11:02:20 +08009274static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009275{
Jens Axboe4379bf82021-02-15 13:40:22 -07009276 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009277
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009278 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009279 if (creds) {
9280 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009281 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009282 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009283
9284 return -EINVAL;
9285}
9286
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009287struct io_tctx_exit {
9288 struct callback_head task_work;
9289 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009290 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009291};
9292
9293static void io_tctx_exit_cb(struct callback_head *cb)
9294{
9295 struct io_uring_task *tctx = current->io_uring;
9296 struct io_tctx_exit *work;
9297
9298 work = container_of(cb, struct io_tctx_exit, task_work);
9299 /*
9300 * When @in_idle, we're in cancellation and it's racy to remove the
9301 * node. It'll be removed by the end of cancellation, just ignore it.
9302 */
9303 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009304 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009305 complete(&work->completion);
9306}
9307
Pavel Begunkov28090c12021-04-25 23:34:45 +01009308static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
9309{
9310 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9311
9312 return req->ctx == data;
9313}
9314
Jens Axboe85faa7b2020-04-09 18:14:00 -06009315static void io_ring_exit_work(struct work_struct *work)
9316{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009317 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009318 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009319 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009320 struct io_tctx_exit exit;
9321 struct io_tctx_node *node;
9322 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009323
Jens Axboe56952e92020-06-17 15:00:04 -06009324 /*
9325 * If we're doing polled IO and end up having requests being
9326 * submitted async (out-of-line), then completions can come in while
9327 * we're waiting for refs to drop. We need to reap these manually,
9328 * as nobody else will be looking for them.
9329 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009330 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009331 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009332 if (ctx->sq_data) {
9333 struct io_sq_data *sqd = ctx->sq_data;
9334 struct task_struct *tsk;
9335
9336 io_sq_thread_park(sqd);
9337 tsk = sqd->thread;
9338 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9339 io_wq_cancel_cb(tsk->io_uring->io_wq,
9340 io_cancel_ctx_cb, ctx, true);
9341 io_sq_thread_unpark(sqd);
9342 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009343
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009344 io_req_caches_free(ctx);
9345
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009346 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9347 /* there is little hope left, don't run it too often */
9348 interval = HZ * 60;
9349 }
9350 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009351
Pavel Begunkov7f006512021-04-14 13:38:34 +01009352 init_completion(&exit.completion);
9353 init_task_work(&exit.task_work, io_tctx_exit_cb);
9354 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009355 /*
9356 * Some may use context even when all refs and requests have been put,
9357 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009358 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009359 * this lock/unlock section also waits them to finish.
9360 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009361 mutex_lock(&ctx->uring_lock);
9362 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009363 WARN_ON_ONCE(time_after(jiffies, timeout));
9364
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009365 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9366 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009367 /* don't spin on a single task if cancellation failed */
9368 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009369 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9370 if (WARN_ON_ONCE(ret))
9371 continue;
9372 wake_up_process(node->task);
9373
9374 mutex_unlock(&ctx->uring_lock);
9375 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009376 mutex_lock(&ctx->uring_lock);
9377 }
9378 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009379 spin_lock(&ctx->completion_lock);
9380 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009381
Jens Axboe85faa7b2020-04-09 18:14:00 -06009382 io_ring_ctx_free(ctx);
9383}
9384
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009385/* Returns true if we found and killed one or more timeouts */
9386static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009387 bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009388{
9389 struct io_kiocb *req, *tmp;
9390 int canceled = 0;
9391
Jens Axboe79ebeae2021-08-10 15:18:27 -06009392 spin_lock(&ctx->completion_lock);
9393 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009394 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009395 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009396 io_kill_timeout(req, -ECANCELED);
9397 canceled++;
9398 }
9399 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009400 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009401 if (canceled != 0)
9402 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009403 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009404 if (canceled != 0)
9405 io_cqring_ev_posted(ctx);
9406 return canceled != 0;
9407}
9408
Jens Axboe2b188cc2019-01-07 10:46:33 -07009409static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
9410{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009411 unsigned long index;
9412 struct creds *creds;
9413
Jens Axboe2b188cc2019-01-07 10:46:33 -07009414 mutex_lock(&ctx->uring_lock);
9415 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009416 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009417 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009418 xa_for_each(&ctx->personalities, index, creds)
9419 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009420 mutex_unlock(&ctx->uring_lock);
9421
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009422 io_kill_timeouts(ctx, NULL, true);
9423 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009424
Jens Axboe15dff282019-11-13 09:09:23 -07009425 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009426 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009427
Jens Axboe85faa7b2020-04-09 18:14:00 -06009428 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009429 /*
9430 * Use system_unbound_wq to avoid spawning tons of event kworkers
9431 * if we're exiting a ton of rings at the same time. It just adds
9432 * noise and overhead, there's no discernable change in runtime
9433 * over using system_wq.
9434 */
9435 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009436}
9437
9438static int io_uring_release(struct inode *inode, struct file *file)
9439{
9440 struct io_ring_ctx *ctx = file->private_data;
9441
9442 file->private_data = NULL;
9443 io_ring_ctx_wait_and_kill(ctx);
9444 return 0;
9445}
9446
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009447struct io_task_cancel {
9448 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009449 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009450};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009451
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009452static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009453{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009454 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009455 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009456 bool ret;
9457
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009458 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009459 struct io_ring_ctx *ctx = req->ctx;
9460
9461 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009462 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009463 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009464 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009465 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009466 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009467 }
9468 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009469}
9470
Pavel Begunkove1915f72021-03-11 23:29:35 +00009471static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009472 struct task_struct *task, bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009473{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009474 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009475 LIST_HEAD(list);
9476
Jens Axboe79ebeae2021-08-10 15:18:27 -06009477 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009478 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009479 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009480 list_cut_position(&list, &ctx->defer_list, &de->list);
9481 break;
9482 }
9483 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009484 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009485 if (list_empty(&list))
9486 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009487
9488 while (!list_empty(&list)) {
9489 de = list_first_entry(&list, struct io_defer_entry, list);
9490 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009491 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009492 kfree(de);
9493 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009494 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009495}
9496
Pavel Begunkov1b007642021-03-06 11:02:17 +00009497static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
9498{
9499 struct io_tctx_node *node;
9500 enum io_wq_cancel cret;
9501 bool ret = false;
9502
9503 mutex_lock(&ctx->uring_lock);
9504 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9505 struct io_uring_task *tctx = node->task->io_uring;
9506
9507 /*
9508 * io_wq will stay alive while we hold uring_lock, because it's
9509 * killed after ctx nodes, which requires to take the lock.
9510 */
9511 if (!tctx || !tctx->io_wq)
9512 continue;
9513 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9514 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9515 }
9516 mutex_unlock(&ctx->uring_lock);
9517
9518 return ret;
9519}
9520
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009521static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9522 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009523 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009524{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009525 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009526 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009527
9528 while (1) {
9529 enum io_wq_cancel cret;
9530 bool ret = false;
9531
Pavel Begunkov1b007642021-03-06 11:02:17 +00009532 if (!task) {
9533 ret |= io_uring_try_cancel_iowq(ctx);
9534 } else if (tctx && tctx->io_wq) {
9535 /*
9536 * Cancels requests of all rings, not only @ctx, but
9537 * it's fine as the task is in exit/exec.
9538 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009539 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009540 &cancel, true);
9541 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9542 }
9543
9544 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009545 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009546 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01009547 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009548 io_iopoll_try_reap_events(ctx);
9549 ret = true;
9550 }
9551 }
9552
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009553 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9554 ret |= io_poll_remove_all(ctx, task, cancel_all);
9555 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009556 if (task)
9557 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009558 if (!ret)
9559 break;
9560 cond_resched();
9561 }
9562}
9563
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009564static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009565{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009566 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009567 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009568 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009569
9570 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009571 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009572 if (unlikely(ret))
9573 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009574 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009575 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009576 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9577 node = kmalloc(sizeof(*node), GFP_KERNEL);
9578 if (!node)
9579 return -ENOMEM;
9580 node->ctx = ctx;
9581 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009582
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009583 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9584 node, GFP_KERNEL));
9585 if (ret) {
9586 kfree(node);
9587 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009588 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009589
9590 mutex_lock(&ctx->uring_lock);
9591 list_add(&node->ctx_node, &ctx->tctx_list);
9592 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009593 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009594 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009595 return 0;
9596}
9597
9598/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009599 * Note that this task has used io_uring. We use it for cancelation purposes.
9600 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009601static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009602{
9603 struct io_uring_task *tctx = current->io_uring;
9604
9605 if (likely(tctx && tctx->last == ctx))
9606 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009607 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009608}
9609
9610/*
Jens Axboe0f212202020-09-13 13:09:39 -06009611 * Remove this io_uring_file -> task mapping.
9612 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009613static void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009614{
9615 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009616 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009617
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009618 if (!tctx)
9619 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009620 node = xa_erase(&tctx->xa, index);
9621 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009622 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009623
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009624 WARN_ON_ONCE(current != node->task);
9625 WARN_ON_ONCE(list_empty(&node->ctx_node));
9626
9627 mutex_lock(&node->ctx->uring_lock);
9628 list_del(&node->ctx_node);
9629 mutex_unlock(&node->ctx->uring_lock);
9630
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009631 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009632 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009633 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009634}
9635
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009636static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009637{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009638 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009639 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009640 unsigned long index;
9641
Jens Axboe8bab4c02021-09-24 07:12:27 -06009642 xa_for_each(&tctx->xa, index, node) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009643 io_uring_del_tctx_node(index);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009644 cond_resched();
9645 }
Marco Elverb16ef422021-05-27 11:25:48 +02009646 if (wq) {
9647 /*
9648 * Must be after io_uring_del_task_file() (removes nodes under
9649 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9650 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009651 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009652 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009653 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009654}
9655
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009656static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009657{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009658 if (tracked)
9659 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009660 return percpu_counter_sum(&tctx->inflight);
9661}
9662
Pavel Begunkov09899b12021-06-14 02:36:22 +01009663static void io_uring_drop_tctx_refs(struct task_struct *task)
9664{
9665 struct io_uring_task *tctx = task->io_uring;
9666 unsigned int refs = tctx->cached_refs;
9667
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009668 if (refs) {
9669 tctx->cached_refs = 0;
9670 percpu_counter_sub(&tctx->inflight, refs);
9671 put_task_struct_many(task, refs);
9672 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009673}
9674
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009675/*
9676 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9677 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9678 */
9679static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009680{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009681 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009682 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009683 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009684 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009685
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009686 WARN_ON_ONCE(sqd && sqd->thread != current);
9687
Palash Oswal6d042ff2021-04-27 18:21:49 +05309688 if (!current->io_uring)
9689 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009690 if (tctx->io_wq)
9691 io_wq_exit_start(tctx->io_wq);
9692
Jens Axboefdaf0832020-10-30 09:37:30 -06009693 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009694 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009695 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009696 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009697 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009698 if (!inflight)
9699 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009700
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009701 if (!sqd) {
9702 struct io_tctx_node *node;
9703 unsigned long index;
9704
9705 xa_for_each(&tctx->xa, index, node) {
9706 /* sqpoll task will cancel all its requests */
9707 if (node->ctx->sq_data)
9708 continue;
9709 io_uring_try_cancel_requests(node->ctx, current,
9710 cancel_all);
9711 }
9712 } else {
9713 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9714 io_uring_try_cancel_requests(ctx, current,
9715 cancel_all);
9716 }
9717
9718 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009719 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009720 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009721 * If we've seen completions, retry without waiting. This
9722 * avoids a race where a completion comes in before we did
9723 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009724 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009725 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009726 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009727 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009728 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009729 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009730
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009731 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009732 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009733 /* for exec all current's requests should be gone, kill tctx */
9734 __io_uring_free(current);
9735 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009736}
9737
Hao Xuf552a272021-08-12 12:14:35 +08009738void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009739{
Hao Xuf552a272021-08-12 12:14:35 +08009740 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009741}
9742
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009743static void *io_uring_validate_mmap_request(struct file *file,
9744 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009745{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009746 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009747 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009748 struct page *page;
9749 void *ptr;
9750
9751 switch (offset) {
9752 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009753 case IORING_OFF_CQ_RING:
9754 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009755 break;
9756 case IORING_OFF_SQES:
9757 ptr = ctx->sq_sqes;
9758 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009759 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009760 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009761 }
9762
9763 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009764 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009765 return ERR_PTR(-EINVAL);
9766
9767 return ptr;
9768}
9769
9770#ifdef CONFIG_MMU
9771
9772static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9773{
9774 size_t sz = vma->vm_end - vma->vm_start;
9775 unsigned long pfn;
9776 void *ptr;
9777
9778 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9779 if (IS_ERR(ptr))
9780 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009781
9782 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9783 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9784}
9785
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009786#else /* !CONFIG_MMU */
9787
9788static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9789{
9790 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9791}
9792
9793static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9794{
9795 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9796}
9797
9798static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9799 unsigned long addr, unsigned long len,
9800 unsigned long pgoff, unsigned long flags)
9801{
9802 void *ptr;
9803
9804 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9805 if (IS_ERR(ptr))
9806 return PTR_ERR(ptr);
9807
9808 return (unsigned long) ptr;
9809}
9810
9811#endif /* !CONFIG_MMU */
9812
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009813static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009814{
9815 DEFINE_WAIT(wait);
9816
9817 do {
9818 if (!io_sqring_full(ctx))
9819 break;
Jens Axboe90554202020-09-03 12:12:41 -06009820 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9821
9822 if (!io_sqring_full(ctx))
9823 break;
Jens Axboe90554202020-09-03 12:12:41 -06009824 schedule();
9825 } while (!signal_pending(current));
9826
9827 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009828 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009829}
9830
Hao Xuc73ebb62020-11-03 10:54:37 +08009831static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9832 struct __kernel_timespec __user **ts,
9833 const sigset_t __user **sig)
9834{
9835 struct io_uring_getevents_arg arg;
9836
9837 /*
9838 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9839 * is just a pointer to the sigset_t.
9840 */
9841 if (!(flags & IORING_ENTER_EXT_ARG)) {
9842 *sig = (const sigset_t __user *) argp;
9843 *ts = NULL;
9844 return 0;
9845 }
9846
9847 /*
9848 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9849 * timespec and sigset_t pointers if good.
9850 */
9851 if (*argsz != sizeof(arg))
9852 return -EINVAL;
9853 if (copy_from_user(&arg, argp, sizeof(arg)))
9854 return -EFAULT;
9855 *sig = u64_to_user_ptr(arg.sigmask);
9856 *argsz = arg.sigmask_sz;
9857 *ts = u64_to_user_ptr(arg.ts);
9858 return 0;
9859}
9860
Jens Axboe2b188cc2019-01-07 10:46:33 -07009861SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009862 u32, min_complete, u32, flags, const void __user *, argp,
9863 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009864{
9865 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009866 int submitted = 0;
9867 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009868 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009869
Jens Axboe4c6e2772020-07-01 11:29:10 -06009870 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009871
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009872 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9873 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009874 return -EINVAL;
9875
9876 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009877 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009878 return -EBADF;
9879
9880 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009881 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009882 goto out_fput;
9883
9884 ret = -ENXIO;
9885 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009886 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009887 goto out_fput;
9888
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009889 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009890 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009891 goto out;
9892
Jens Axboe6c271ce2019-01-10 11:22:30 -07009893 /*
9894 * For SQ polling, the thread will do all submissions and completions.
9895 * Just return the requested submit count, and wake the thread if
9896 * we were asked to.
9897 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009898 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009899 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +01009900 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009901
Jens Axboe21f96522021-08-14 09:04:40 -06009902 if (unlikely(ctx->sq_data->thread == NULL)) {
9903 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009904 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -06009905 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009906 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009907 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009908 if (flags & IORING_ENTER_SQ_WAIT) {
9909 ret = io_sqpoll_wait_sq(ctx);
9910 if (ret)
9911 goto out;
9912 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009913 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009914 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009915 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009916 if (unlikely(ret))
9917 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009918 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009919 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009920 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009921
9922 if (submitted != to_submit)
9923 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009924 }
9925 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009926 const sigset_t __user *sig;
9927 struct __kernel_timespec __user *ts;
9928
9929 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9930 if (unlikely(ret))
9931 goto out;
9932
Jens Axboe2b188cc2019-01-07 10:46:33 -07009933 min_complete = min(min_complete, ctx->cq_entries);
9934
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009935 /*
9936 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9937 * space applications don't need to do io completion events
9938 * polling again, they can rely on io_sq_thread to do polling
9939 * work, which can reduce cpu usage and uring_lock contention.
9940 */
9941 if (ctx->flags & IORING_SETUP_IOPOLL &&
9942 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009943 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009944 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009945 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009946 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009947 }
9948
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009949out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009950 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009951out_fput:
9952 fdput(f);
9953 return submitted ? submitted : ret;
9954}
9955
Tobias Klauserbebdb652020-02-26 18:38:32 +01009956#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009957static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9958 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -07009959{
Jens Axboe87ce9552020-01-30 08:25:34 -07009960 struct user_namespace *uns = seq_user_ns(m);
9961 struct group_info *gi;
9962 kernel_cap_t cap;
9963 unsigned __capi;
9964 int g;
9965
9966 seq_printf(m, "%5d\n", id);
9967 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9968 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9969 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9970 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9971 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9972 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9973 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9974 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9975 seq_puts(m, "\n\tGroups:\t");
9976 gi = cred->group_info;
9977 for (g = 0; g < gi->ngroups; g++) {
9978 seq_put_decimal_ull(m, g ? " " : "",
9979 from_kgid_munged(uns, gi->gid[g]));
9980 }
9981 seq_puts(m, "\n\tCapEff:\t");
9982 cap = cred->cap_effective;
9983 CAP_FOR_EACH_U32(__capi)
9984 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9985 seq_putc(m, '\n');
9986 return 0;
9987}
9988
9989static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9990{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009991 struct io_sq_data *sq = NULL;
Hao Xu83f84352021-09-13 21:08:54 +08009992 struct io_overflow_cqe *ocqe;
9993 struct io_rings *r = ctx->rings;
9994 unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
9995 unsigned int cached_sq_head = ctx->cached_sq_head;
9996 unsigned int cached_cq_tail = ctx->cached_cq_tail;
9997 unsigned int sq_head = READ_ONCE(r->sq.head);
9998 unsigned int sq_tail = READ_ONCE(r->sq.tail);
9999 unsigned int cq_head = READ_ONCE(r->cq.head);
10000 unsigned int cq_tail = READ_ONCE(r->cq.tail);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010001 bool has_lock;
Hao Xu83f84352021-09-13 21:08:54 +080010002 unsigned int i;
10003
10004 /*
10005 * we may get imprecise sqe and cqe info if uring is actively running
10006 * since we get cached_sq_head and cached_cq_tail without uring_lock
10007 * and sq_tail and cq_head are changed by userspace. But it's ok since
10008 * we usually use these info when it is stuck.
10009 */
10010 seq_printf(m, "SqHead:\t%u\n", sq_head & sq_mask);
10011 seq_printf(m, "SqTail:\t%u\n", sq_tail & sq_mask);
10012 seq_printf(m, "CachedSqHead:\t%u\n", cached_sq_head & sq_mask);
10013 seq_printf(m, "CqHead:\t%u\n", cq_head & cq_mask);
10014 seq_printf(m, "CqTail:\t%u\n", cq_tail & cq_mask);
10015 seq_printf(m, "CachedCqTail:\t%u\n", cached_cq_tail & cq_mask);
10016 seq_printf(m, "SQEs:\t%u\n", sq_tail - cached_sq_head);
10017 for (i = cached_sq_head; i < sq_tail; i++) {
10018 unsigned int sq_idx = READ_ONCE(ctx->sq_array[i & sq_mask]);
10019
10020 if (likely(sq_idx <= sq_mask)) {
10021 struct io_uring_sqe *sqe = &ctx->sq_sqes[sq_idx];
10022
10023 seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n",
10024 sq_idx, sqe->opcode, sqe->fd, sqe->flags, sqe->user_data);
10025 }
10026 }
10027 seq_printf(m, "CQEs:\t%u\n", cached_cq_tail - cq_head);
10028 for (i = cq_head; i < cached_cq_tail; i++) {
10029 struct io_uring_cqe *cqe = &r->cqes[i & cq_mask];
10030
10031 seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n",
10032 i & cq_mask, cqe->user_data, cqe->res, cqe->flags);
10033 }
Jens Axboe87ce9552020-01-30 08:25:34 -070010034
Jens Axboefad8e0d2020-09-28 08:57:48 -060010035 /*
10036 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10037 * since fdinfo case grabs it in the opposite direction of normal use
10038 * cases. If we fail to get the lock, we just don't iterate any
10039 * structures that could be going away outside the io_uring mutex.
10040 */
10041 has_lock = mutex_trylock(&ctx->uring_lock);
10042
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010043 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -060010044 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010045 if (!sq->thread)
10046 sq = NULL;
10047 }
Joseph Qidbbe9c62020-09-29 09:01:22 -060010048
10049 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10050 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -070010051 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010052 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -070010053 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -070010054
Jens Axboe87ce9552020-01-30 08:25:34 -070010055 if (f)
10056 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10057 else
10058 seq_printf(m, "%5u: <none>\n", i);
10059 }
10060 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010061 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +010010062 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +010010063 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -070010064
Pavel Begunkov4751f532021-04-01 15:43:55 +010010065 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -070010066 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010067 if (has_lock && !xa_empty(&ctx->personalities)) {
10068 unsigned long index;
10069 const struct cred *cred;
10070
Jens Axboe87ce9552020-01-30 08:25:34 -070010071 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010072 xa_for_each(&ctx->personalities, index, cred)
10073 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010074 }
Hao Xu83f84352021-09-13 21:08:54 +080010075 if (has_lock)
10076 mutex_unlock(&ctx->uring_lock);
10077
10078 seq_puts(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010079 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010080 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10081 struct hlist_head *list = &ctx->cancel_hash[i];
10082 struct io_kiocb *req;
10083
10084 hlist_for_each_entry(req, list, hash_node)
10085 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10086 req->task->task_works != NULL);
10087 }
Hao Xu83f84352021-09-13 21:08:54 +080010088
10089 seq_puts(m, "CqOverflowList:\n");
10090 list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
10091 struct io_uring_cqe *cqe = &ocqe->cqe;
10092
10093 seq_printf(m, " user_data=%llu, res=%d, flags=%x\n",
10094 cqe->user_data, cqe->res, cqe->flags);
10095
10096 }
10097
Jens Axboe79ebeae2021-08-10 15:18:27 -060010098 spin_unlock(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010099}
10100
10101static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
10102{
10103 struct io_ring_ctx *ctx = f->private_data;
10104
10105 if (percpu_ref_tryget(&ctx->refs)) {
10106 __io_uring_show_fdinfo(ctx, m);
10107 percpu_ref_put(&ctx->refs);
10108 }
10109}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010110#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010111
Jens Axboe2b188cc2019-01-07 10:46:33 -070010112static const struct file_operations io_uring_fops = {
10113 .release = io_uring_release,
10114 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010115#ifndef CONFIG_MMU
10116 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10117 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10118#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010119 .poll = io_uring_poll,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010120#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010121 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010122#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010123};
10124
10125static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10126 struct io_uring_params *p)
10127{
Hristo Venev75b28af2019-08-26 17:23:46 +000010128 struct io_rings *rings;
10129 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010130
Jens Axboebd740482020-08-05 12:58:23 -060010131 /* make sure these are sane, as we already accounted them */
10132 ctx->sq_entries = p->sq_entries;
10133 ctx->cq_entries = p->cq_entries;
10134
Hristo Venev75b28af2019-08-26 17:23:46 +000010135 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10136 if (size == SIZE_MAX)
10137 return -EOVERFLOW;
10138
10139 rings = io_mem_alloc(size);
10140 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010141 return -ENOMEM;
10142
Hristo Venev75b28af2019-08-26 17:23:46 +000010143 ctx->rings = rings;
10144 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10145 rings->sq_ring_mask = p->sq_entries - 1;
10146 rings->cq_ring_mask = p->cq_entries - 1;
10147 rings->sq_ring_entries = p->sq_entries;
10148 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010149
10150 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010151 if (size == SIZE_MAX) {
10152 io_mem_free(ctx->rings);
10153 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010154 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010155 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010156
10157 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010158 if (!ctx->sq_sqes) {
10159 io_mem_free(ctx->rings);
10160 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010161 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010162 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010163
Jens Axboe2b188cc2019-01-07 10:46:33 -070010164 return 0;
10165}
10166
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010167static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10168{
10169 int ret, fd;
10170
10171 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10172 if (fd < 0)
10173 return fd;
10174
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010175 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010176 if (ret) {
10177 put_unused_fd(fd);
10178 return ret;
10179 }
10180 fd_install(fd, file);
10181 return fd;
10182}
10183
Jens Axboe2b188cc2019-01-07 10:46:33 -070010184/*
10185 * Allocate an anonymous fd, this is what constitutes the application
10186 * visible backing of an io_uring instance. The application mmaps this
10187 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10188 * we have to tie this fd to a socket for file garbage collection purposes.
10189 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010190static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010191{
10192 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010193#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010194 int ret;
10195
Jens Axboe2b188cc2019-01-07 10:46:33 -070010196 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10197 &ctx->ring_sock);
10198 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010199 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010200#endif
10201
Jens Axboe2b188cc2019-01-07 10:46:33 -070010202 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
10203 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010204#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010205 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010206 sock_release(ctx->ring_sock);
10207 ctx->ring_sock = NULL;
10208 } else {
10209 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010210 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010211#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010212 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010213}
10214
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010215static int io_uring_create(unsigned entries, struct io_uring_params *p,
10216 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010217{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010218 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010219 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010220 int ret;
10221
Jens Axboe8110c1a2019-12-28 15:39:54 -070010222 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010223 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010224 if (entries > IORING_MAX_ENTRIES) {
10225 if (!(p->flags & IORING_SETUP_CLAMP))
10226 return -EINVAL;
10227 entries = IORING_MAX_ENTRIES;
10228 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010229
10230 /*
10231 * Use twice as many entries for the CQ ring. It's possible for the
10232 * application to drive a higher depth than the size of the SQ ring,
10233 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010234 * some flexibility in overcommitting a bit. If the application has
10235 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10236 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010237 */
10238 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010239 if (p->flags & IORING_SETUP_CQSIZE) {
10240 /*
10241 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10242 * to a power-of-two, if it isn't already. We do NOT impose
10243 * any cq vs sq ring sizing.
10244 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010245 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010246 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010247 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10248 if (!(p->flags & IORING_SETUP_CLAMP))
10249 return -EINVAL;
10250 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10251 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010252 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10253 if (p->cq_entries < p->sq_entries)
10254 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010255 } else {
10256 p->cq_entries = 2 * p->sq_entries;
10257 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010258
Jens Axboe2b188cc2019-01-07 10:46:33 -070010259 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010260 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010261 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010262 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010263 if (!capable(CAP_IPC_LOCK))
10264 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010265
10266 /*
10267 * This is just grabbed for accounting purposes. When a process exits,
10268 * the mm is exited and dropped before the files, hence we need to hang
10269 * on to this mm purely for the purposes of being able to unaccount
10270 * memory (locked/pinned vm). It's not used for anything else.
10271 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010272 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010273 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010274
Jens Axboe2b188cc2019-01-07 10:46:33 -070010275 ret = io_allocate_scq_urings(ctx, p);
10276 if (ret)
10277 goto err;
10278
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010279 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010280 if (ret)
10281 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010282 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010283 ret = io_rsrc_node_switch_start(ctx);
10284 if (ret)
10285 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010286 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010287
Jens Axboe2b188cc2019-01-07 10:46:33 -070010288 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010289 p->sq_off.head = offsetof(struct io_rings, sq.head);
10290 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10291 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10292 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10293 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10294 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10295 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010296
10297 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010298 p->cq_off.head = offsetof(struct io_rings, cq.head);
10299 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10300 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10301 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10302 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10303 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010304 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010305
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010306 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10307 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010308 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010309 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010310 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
10311 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010312
10313 if (copy_to_user(params, p, sizeof(*p))) {
10314 ret = -EFAULT;
10315 goto err;
10316 }
Jens Axboed1719f72020-07-30 13:43:53 -060010317
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010318 file = io_uring_get_file(ctx);
10319 if (IS_ERR(file)) {
10320 ret = PTR_ERR(file);
10321 goto err;
10322 }
10323
Jens Axboed1719f72020-07-30 13:43:53 -060010324 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010325 * Install ring fd as the very last thing, so we don't risk someone
10326 * having closed it before we finish setup
10327 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010328 ret = io_uring_install_fd(ctx, file);
10329 if (ret < 0) {
10330 /* fput will clean it up */
10331 fput(file);
10332 return ret;
10333 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010334
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010335 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010336 return ret;
10337err:
10338 io_ring_ctx_wait_and_kill(ctx);
10339 return ret;
10340}
10341
10342/*
10343 * Sets up an aio uring context, and returns the fd. Applications asks for a
10344 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10345 * params structure passed in.
10346 */
10347static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10348{
10349 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010350 int i;
10351
10352 if (copy_from_user(&p, params, sizeof(p)))
10353 return -EFAULT;
10354 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10355 if (p.resv[i])
10356 return -EINVAL;
10357 }
10358
Jens Axboe6c271ce2019-01-10 11:22:30 -070010359 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010360 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010361 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10362 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010363 return -EINVAL;
10364
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010365 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010366}
10367
10368SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10369 struct io_uring_params __user *, params)
10370{
10371 return io_uring_setup(entries, params);
10372}
10373
Jens Axboe66f4af92020-01-16 15:36:52 -070010374static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
10375{
10376 struct io_uring_probe *p;
10377 size_t size;
10378 int i, ret;
10379
10380 size = struct_size(p, ops, nr_args);
10381 if (size == SIZE_MAX)
10382 return -EOVERFLOW;
10383 p = kzalloc(size, GFP_KERNEL);
10384 if (!p)
10385 return -ENOMEM;
10386
10387 ret = -EFAULT;
10388 if (copy_from_user(p, arg, size))
10389 goto out;
10390 ret = -EINVAL;
10391 if (memchr_inv(p, 0, size))
10392 goto out;
10393
10394 p->last_op = IORING_OP_LAST - 1;
10395 if (nr_args > IORING_OP_LAST)
10396 nr_args = IORING_OP_LAST;
10397
10398 for (i = 0; i < nr_args; i++) {
10399 p->ops[i].op = i;
10400 if (!io_op_defs[i].not_supported)
10401 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10402 }
10403 p->ops_len = i;
10404
10405 ret = 0;
10406 if (copy_to_user(arg, p, size))
10407 ret = -EFAULT;
10408out:
10409 kfree(p);
10410 return ret;
10411}
10412
Jens Axboe071698e2020-01-28 10:04:42 -070010413static int io_register_personality(struct io_ring_ctx *ctx)
10414{
Jens Axboe4379bf82021-02-15 13:40:22 -070010415 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010416 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010417 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010418
Jens Axboe4379bf82021-02-15 13:40:22 -070010419 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010420
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010421 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10422 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010423 if (ret < 0) {
10424 put_cred(creds);
10425 return ret;
10426 }
10427 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010428}
10429
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010430static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
10431 unsigned int nr_args)
10432{
10433 struct io_uring_restriction *res;
10434 size_t size;
10435 int i, ret;
10436
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010437 /* Restrictions allowed only if rings started disabled */
10438 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10439 return -EBADFD;
10440
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010441 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010442 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010443 return -EBUSY;
10444
10445 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10446 return -EINVAL;
10447
10448 size = array_size(nr_args, sizeof(*res));
10449 if (size == SIZE_MAX)
10450 return -EOVERFLOW;
10451
10452 res = memdup_user(arg, size);
10453 if (IS_ERR(res))
10454 return PTR_ERR(res);
10455
10456 ret = 0;
10457
10458 for (i = 0; i < nr_args; i++) {
10459 switch (res[i].opcode) {
10460 case IORING_RESTRICTION_REGISTER_OP:
10461 if (res[i].register_op >= IORING_REGISTER_LAST) {
10462 ret = -EINVAL;
10463 goto out;
10464 }
10465
10466 __set_bit(res[i].register_op,
10467 ctx->restrictions.register_op);
10468 break;
10469 case IORING_RESTRICTION_SQE_OP:
10470 if (res[i].sqe_op >= IORING_OP_LAST) {
10471 ret = -EINVAL;
10472 goto out;
10473 }
10474
10475 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10476 break;
10477 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10478 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10479 break;
10480 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10481 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10482 break;
10483 default:
10484 ret = -EINVAL;
10485 goto out;
10486 }
10487 }
10488
10489out:
10490 /* Reset all restrictions if an error happened */
10491 if (ret != 0)
10492 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10493 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010494 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010495
10496 kfree(res);
10497 return ret;
10498}
10499
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010500static int io_register_enable_rings(struct io_ring_ctx *ctx)
10501{
10502 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10503 return -EBADFD;
10504
10505 if (ctx->restrictions.registered)
10506 ctx->restricted = 1;
10507
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010508 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10509 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10510 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010511 return 0;
10512}
10513
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010514static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010515 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010516 unsigned nr_args)
10517{
10518 __u32 tmp;
10519 int err;
10520
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010521 if (up->resv)
10522 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010523 if (check_add_overflow(up->offset, nr_args, &tmp))
10524 return -EOVERFLOW;
10525 err = io_rsrc_node_switch_start(ctx);
10526 if (err)
10527 return err;
10528
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010529 switch (type) {
10530 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010531 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010532 case IORING_RSRC_BUFFER:
10533 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010534 }
10535 return -EINVAL;
10536}
10537
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010538static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10539 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010540{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010541 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010542
10543 if (!nr_args)
10544 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010545 memset(&up, 0, sizeof(up));
10546 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10547 return -EFAULT;
10548 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10549}
10550
10551static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010552 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010553{
10554 struct io_uring_rsrc_update2 up;
10555
10556 if (size != sizeof(up))
10557 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010558 if (copy_from_user(&up, arg, sizeof(up)))
10559 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010560 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010561 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010562 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010563}
10564
Pavel Begunkov792e3582021-04-25 14:32:21 +010010565static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010566 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010567{
10568 struct io_uring_rsrc_register rr;
10569
10570 /* keep it extendible */
10571 if (size != sizeof(rr))
10572 return -EINVAL;
10573
10574 memset(&rr, 0, sizeof(rr));
10575 if (copy_from_user(&rr, arg, size))
10576 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010577 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010578 return -EINVAL;
10579
Pavel Begunkov992da012021-06-10 16:37:37 +010010580 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010581 case IORING_RSRC_FILE:
10582 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10583 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010584 case IORING_RSRC_BUFFER:
10585 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10586 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010587 }
10588 return -EINVAL;
10589}
10590
Jens Axboefe764212021-06-17 10:19:54 -060010591static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg,
10592 unsigned len)
10593{
10594 struct io_uring_task *tctx = current->io_uring;
10595 cpumask_var_t new_mask;
10596 int ret;
10597
10598 if (!tctx || !tctx->io_wq)
10599 return -EINVAL;
10600
10601 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10602 return -ENOMEM;
10603
10604 cpumask_clear(new_mask);
10605 if (len > cpumask_size())
10606 len = cpumask_size();
10607
10608 if (copy_from_user(new_mask, arg, len)) {
10609 free_cpumask_var(new_mask);
10610 return -EFAULT;
10611 }
10612
10613 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10614 free_cpumask_var(new_mask);
10615 return ret;
10616}
10617
10618static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
10619{
10620 struct io_uring_task *tctx = current->io_uring;
10621
10622 if (!tctx || !tctx->io_wq)
10623 return -EINVAL;
10624
10625 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10626}
10627
Jens Axboe2e480052021-08-27 11:33:19 -060010628static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10629 void __user *arg)
10630{
Jens Axboefa846932021-09-01 14:15:59 -060010631 struct io_uring_task *tctx = NULL;
10632 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010633 __u32 new_count[2];
10634 int i, ret;
10635
Jens Axboe2e480052021-08-27 11:33:19 -060010636 if (copy_from_user(new_count, arg, sizeof(new_count)))
10637 return -EFAULT;
10638 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10639 if (new_count[i] > INT_MAX)
10640 return -EINVAL;
10641
Jens Axboefa846932021-09-01 14:15:59 -060010642 if (ctx->flags & IORING_SETUP_SQPOLL) {
10643 sqd = ctx->sq_data;
10644 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010645 /*
10646 * Observe the correct sqd->lock -> ctx->uring_lock
10647 * ordering. Fine to drop uring_lock here, we hold
10648 * a ref to the ctx.
10649 */
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010650 refcount_inc(&sqd->refs);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010651 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010652 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010653 mutex_lock(&ctx->uring_lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010654 if (sqd->thread)
10655 tctx = sqd->thread->io_uring;
Jens Axboefa846932021-09-01 14:15:59 -060010656 }
10657 } else {
10658 tctx = current->io_uring;
10659 }
10660
10661 ret = -EINVAL;
10662 if (!tctx || !tctx->io_wq)
10663 goto err;
10664
Jens Axboe2e480052021-08-27 11:33:19 -060010665 ret = io_wq_max_workers(tctx->io_wq, new_count);
10666 if (ret)
Jens Axboefa846932021-09-01 14:15:59 -060010667 goto err;
10668
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010669 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010670 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010671 io_put_sq_data(sqd);
10672 }
Jens Axboe2e480052021-08-27 11:33:19 -060010673
10674 if (copy_to_user(arg, new_count, sizeof(new_count)))
10675 return -EFAULT;
10676
10677 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010678err:
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010679 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010680 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010681 io_put_sq_data(sqd);
10682 }
Jens Axboefa846932021-09-01 14:15:59 -060010683 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010684}
10685
Jens Axboe071698e2020-01-28 10:04:42 -070010686static bool io_register_op_must_quiesce(int op)
10687{
10688 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010689 case IORING_REGISTER_BUFFERS:
10690 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010691 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010692 case IORING_UNREGISTER_FILES:
10693 case IORING_REGISTER_FILES_UPDATE:
10694 case IORING_REGISTER_PROBE:
10695 case IORING_REGISTER_PERSONALITY:
10696 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010697 case IORING_REGISTER_FILES2:
10698 case IORING_REGISTER_FILES_UPDATE2:
10699 case IORING_REGISTER_BUFFERS2:
10700 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010701 case IORING_REGISTER_IOWQ_AFF:
10702 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010703 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010704 return false;
10705 default:
10706 return true;
10707 }
10708}
10709
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010710static int io_ctx_quiesce(struct io_ring_ctx *ctx)
10711{
10712 long ret;
10713
10714 percpu_ref_kill(&ctx->refs);
10715
10716 /*
10717 * Drop uring mutex before waiting for references to exit. If another
10718 * thread is currently inside io_uring_enter() it might need to grab the
10719 * uring_lock to make progress. If we hold it here across the drain
10720 * wait, then we can deadlock. It's safe to drop the mutex here, since
10721 * no new references will come in after we've killed the percpu ref.
10722 */
10723 mutex_unlock(&ctx->uring_lock);
10724 do {
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010725 ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ);
10726 if (ret) {
10727 ret = min(0L, ret);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010728 break;
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010729 }
10730
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010731 ret = io_run_task_work_sig();
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010732 io_req_caches_free(ctx);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010733 } while (ret >= 0);
10734 mutex_lock(&ctx->uring_lock);
10735
10736 if (ret)
10737 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10738 return ret;
10739}
10740
Jens Axboeedafcce2019-01-09 09:16:05 -070010741static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10742 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010743 __releases(ctx->uring_lock)
10744 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010745{
10746 int ret;
10747
Jens Axboe35fa71a2019-04-22 10:23:23 -060010748 /*
10749 * We're inside the ring mutex, if the ref is already dying, then
10750 * someone else killed the ctx or is already going through
10751 * io_uring_register().
10752 */
10753 if (percpu_ref_is_dying(&ctx->refs))
10754 return -ENXIO;
10755
Pavel Begunkov75c40212021-04-15 13:07:40 +010010756 if (ctx->restricted) {
10757 if (opcode >= IORING_REGISTER_LAST)
10758 return -EINVAL;
10759 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10760 if (!test_bit(opcode, ctx->restrictions.register_op))
10761 return -EACCES;
10762 }
10763
Jens Axboe071698e2020-01-28 10:04:42 -070010764 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010765 ret = io_ctx_quiesce(ctx);
10766 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010767 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010768 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010769
10770 switch (opcode) {
10771 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010772 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010773 break;
10774 case IORING_UNREGISTER_BUFFERS:
10775 ret = -EINVAL;
10776 if (arg || nr_args)
10777 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010778 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010779 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010780 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010781 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010782 break;
10783 case IORING_UNREGISTER_FILES:
10784 ret = -EINVAL;
10785 if (arg || nr_args)
10786 break;
10787 ret = io_sqe_files_unregister(ctx);
10788 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010789 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010790 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010791 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010792 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010793 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010794 ret = -EINVAL;
10795 if (nr_args != 1)
10796 break;
10797 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010798 if (ret)
10799 break;
10800 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10801 ctx->eventfd_async = 1;
10802 else
10803 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010804 break;
10805 case IORING_UNREGISTER_EVENTFD:
10806 ret = -EINVAL;
10807 if (arg || nr_args)
10808 break;
10809 ret = io_eventfd_unregister(ctx);
10810 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010811 case IORING_REGISTER_PROBE:
10812 ret = -EINVAL;
10813 if (!arg || nr_args > 256)
10814 break;
10815 ret = io_probe(ctx, arg, nr_args);
10816 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010817 case IORING_REGISTER_PERSONALITY:
10818 ret = -EINVAL;
10819 if (arg || nr_args)
10820 break;
10821 ret = io_register_personality(ctx);
10822 break;
10823 case IORING_UNREGISTER_PERSONALITY:
10824 ret = -EINVAL;
10825 if (arg)
10826 break;
10827 ret = io_unregister_personality(ctx, nr_args);
10828 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010829 case IORING_REGISTER_ENABLE_RINGS:
10830 ret = -EINVAL;
10831 if (arg || nr_args)
10832 break;
10833 ret = io_register_enable_rings(ctx);
10834 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010835 case IORING_REGISTER_RESTRICTIONS:
10836 ret = io_register_restrictions(ctx, arg, nr_args);
10837 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010838 case IORING_REGISTER_FILES2:
10839 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010840 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010841 case IORING_REGISTER_FILES_UPDATE2:
10842 ret = io_register_rsrc_update(ctx, arg, nr_args,
10843 IORING_RSRC_FILE);
10844 break;
10845 case IORING_REGISTER_BUFFERS2:
10846 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10847 break;
10848 case IORING_REGISTER_BUFFERS_UPDATE:
10849 ret = io_register_rsrc_update(ctx, arg, nr_args,
10850 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010851 break;
Jens Axboefe764212021-06-17 10:19:54 -060010852 case IORING_REGISTER_IOWQ_AFF:
10853 ret = -EINVAL;
10854 if (!arg || !nr_args)
10855 break;
10856 ret = io_register_iowq_aff(ctx, arg, nr_args);
10857 break;
10858 case IORING_UNREGISTER_IOWQ_AFF:
10859 ret = -EINVAL;
10860 if (arg || nr_args)
10861 break;
10862 ret = io_unregister_iowq_aff(ctx);
10863 break;
Jens Axboe2e480052021-08-27 11:33:19 -060010864 case IORING_REGISTER_IOWQ_MAX_WORKERS:
10865 ret = -EINVAL;
10866 if (!arg || nr_args != 2)
10867 break;
10868 ret = io_register_iowq_max_workers(ctx, arg);
10869 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010870 default:
10871 ret = -EINVAL;
10872 break;
10873 }
10874
Jens Axboe071698e2020-01-28 10:04:42 -070010875 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010876 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010877 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010878 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010879 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010880 return ret;
10881}
10882
10883SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10884 void __user *, arg, unsigned int, nr_args)
10885{
10886 struct io_ring_ctx *ctx;
10887 long ret = -EBADF;
10888 struct fd f;
10889
10890 f = fdget(fd);
10891 if (!f.file)
10892 return -EBADF;
10893
10894 ret = -EOPNOTSUPP;
10895 if (f.file->f_op != &io_uring_fops)
10896 goto out_fput;
10897
10898 ctx = f.file->private_data;
10899
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010900 io_run_task_work();
10901
Jens Axboeedafcce2019-01-09 09:16:05 -070010902 mutex_lock(&ctx->uring_lock);
10903 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10904 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010905 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10906 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010907out_fput:
10908 fdput(f);
10909 return ret;
10910}
10911
Jens Axboe2b188cc2019-01-07 10:46:33 -070010912static int __init io_uring_init(void)
10913{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010914#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10915 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10916 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10917} while (0)
10918
10919#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10920 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10921 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10922 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10923 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10924 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10925 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10926 BUILD_BUG_SQE_ELEM(8, __u64, off);
10927 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10928 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010929 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010930 BUILD_BUG_SQE_ELEM(24, __u32, len);
10931 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10932 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10933 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10934 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010935 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10936 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010937 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10938 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10939 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10940 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10941 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10942 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10943 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10944 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010945 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010946 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10947 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010948 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010949 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010950 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010010951 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010952
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010953 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
10954 sizeof(struct io_uring_rsrc_update));
10955 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
10956 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010010957
10958 /* ->buf_index is u16 */
10959 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
10960
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010961 /* should fit into one byte */
10962 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
Pavel Begunkov68fe2562021-09-15 12:03:38 +010010963 BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
10964 BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010965
Jens Axboed3656342019-12-18 09:50:26 -070010966 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080010967 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010968
Jens Axboe91f245d2021-02-09 13:48:50 -070010969 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10970 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010971 return 0;
10972};
10973__initcall(io_uring_init);