blob: bda302bedeeff5e06137013201bc609466fb4941 [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
313 /*
314 * io_kiocb alloc cache
315 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000316 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000317 unsigned int free_reqs;
318
319 bool plug_started;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +0100320 bool need_plug;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000321
322 /*
323 * Batch completion logic
324 */
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +0100325 struct io_kiocb *compl_reqs[IO_COMPL_BATCH];
326 unsigned int compl_nr;
327 /* inline/task_work completion list, under ->uring_lock */
328 struct list_head free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000329};
330
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331struct io_ring_ctx {
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100332 /* const or read-mostly hot data */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700333 struct {
334 struct percpu_ref refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100336 struct io_rings *rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700337 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800338 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800339 unsigned int drain_next: 1;
340 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200341 unsigned int restricted: 1;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +0100342 unsigned int off_timeout_used: 1;
Pavel Begunkov10c66902021-06-15 16:47:56 +0100343 unsigned int drain_active: 1;
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100344 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700345
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100346 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100347 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100348 struct mutex uring_lock;
349
Hristo Venev75b28af2019-08-26 17:23:46 +0000350 /*
351 * Ring buffer of indices into array of io_uring_sqe, which is
352 * mmapped by the application using the IORING_OFF_SQES offset.
353 *
354 * This indirection could e.g. be used to assign fixed
355 * io_uring_sqe entries to operations and only submit them to
356 * the queue when needed.
357 *
358 * The kernel modifies neither the indices array nor the entries
359 * array.
360 */
361 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100362 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363 unsigned cached_sq_head;
364 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600365 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100366
367 /*
368 * Fixed resources fast path, should be accessed only under
369 * uring_lock, and updated through io_uring_register(2)
370 */
371 struct io_rsrc_node *rsrc_node;
372 struct io_file_table file_table;
373 unsigned nr_user_files;
374 unsigned nr_user_bufs;
375 struct io_mapped_ubuf **user_bufs;
376
377 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600378 struct list_head timeout_list;
Pavel Begunkovef9dd632021-08-28 19:54:38 -0600379 struct list_head ltimeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700380 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100381 struct xarray io_buffers;
382 struct xarray personalities;
383 u32 pers_next;
384 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700385 } ____cacheline_aligned_in_smp;
386
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100387 /* IRQ completion list, under ->completion_lock */
388 struct list_head locked_free_list;
389 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700390
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100391 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600392 struct io_sq_data *sq_data; /* if using sq thread polling */
393
Jens Axboe90554202020-09-03 12:12:41 -0600394 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600395 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000396
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100397 unsigned long check_cq_overflow;
398
Jens Axboe206aefd2019-11-07 18:27:42 -0700399 struct {
400 unsigned cached_cq_tail;
401 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700402 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100403 struct wait_queue_head poll_wait;
404 struct wait_queue_head cq_wait;
405 unsigned cq_extra;
406 atomic_t cq_timeouts;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100407 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700408 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700409
410 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700411 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700412
Jens Axboe89850fc2021-08-10 15:11:51 -0600413 spinlock_t timeout_lock;
414
Jens Axboedef596e2019-01-09 08:59:42 -0700415 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300416 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700417 * io_uring instances that don't use IORING_SETUP_SQPOLL.
418 * For SQPOLL, only the single threaded io_sq_thread() will
419 * manipulate the list, hence no extra locking is needed there.
420 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300421 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700422 struct hlist_head *cancel_hash;
423 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800424 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700425 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600426
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200427 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700428
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100429 /* slow path rsrc auxilary data, used by update/register */
430 struct {
431 struct io_rsrc_node *rsrc_backup_node;
432 struct io_mapped_ubuf *dummy_ubuf;
433 struct io_rsrc_data *file_data;
434 struct io_rsrc_data *buf_data;
435
436 struct delayed_work rsrc_put_work;
437 struct llist_head rsrc_put_llist;
438 struct list_head rsrc_ref_list;
439 spinlock_t rsrc_ref_lock;
440 };
441
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700442 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100443 struct {
444 #if defined(CONFIG_UNIX)
445 struct socket *ring_sock;
446 #endif
447 /* hashed buffered write serialization */
448 struct io_wq_hash *hash_map;
449
450 /* Only used for accounting purposes */
451 struct user_struct *user;
452 struct mm_struct *mm_account;
453
454 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100455 struct llist_head fallback_llist;
456 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100457 struct work_struct exit_work;
458 struct list_head tctx_list;
459 struct completion ref_comp;
460 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461};
462
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100463struct io_uring_task {
464 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100465 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100466 struct xarray xa;
467 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100468 const struct io_ring_ctx *last;
469 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100470 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100471 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100472 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100473
474 spinlock_t task_lock;
475 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100476 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100477 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100478};
479
Jens Axboe09bb8392019-03-13 12:39:28 -0600480/*
481 * First field must be the file pointer in all the
482 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
483 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700484struct io_poll_iocb {
485 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000486 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700487 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600488 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700489 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700490 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700491};
492
Pavel Begunkov9d805892021-04-13 02:58:40 +0100493struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000494 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100495 u64 old_user_data;
496 u64 new_user_data;
497 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600498 bool update_events;
499 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000500};
501
Jens Axboeb5dba592019-12-11 14:02:38 -0700502struct io_close {
503 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700504 int fd;
Pavel Begunkov7df778b2021-09-24 20:04:29 +0100505 u32 file_slot;
Jens Axboeb5dba592019-12-11 14:02:38 -0700506};
507
Jens Axboead8a48a2019-11-15 08:49:11 -0700508struct io_timeout_data {
509 struct io_kiocb *req;
510 struct hrtimer timer;
511 struct timespec64 ts;
512 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600513 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700514};
515
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700516struct io_accept {
517 struct file *file;
518 struct sockaddr __user *addr;
519 int __user *addr_len;
520 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100521 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600522 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700523};
524
525struct io_sync {
526 struct file *file;
527 loff_t len;
528 loff_t off;
529 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700530 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700531};
532
Jens Axboefbf23842019-12-17 18:45:56 -0700533struct io_cancel {
534 struct file *file;
535 u64 addr;
536};
537
Jens Axboeb29472e2019-12-17 18:50:29 -0700538struct io_timeout {
539 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300540 u32 off;
541 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300542 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000543 /* head of the link, used by linked timeouts only */
544 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600545 /* for linked completions */
546 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700547};
548
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100549struct io_timeout_rem {
550 struct file *file;
551 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000552
553 /* timeout update */
554 struct timespec64 ts;
555 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600556 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100557};
558
Jens Axboe9adbd452019-12-20 08:45:55 -0700559struct io_rw {
560 /* NOTE: kiocb has the file as the first member, so don't do it here */
561 struct kiocb kiocb;
562 u64 addr;
563 u64 len;
564};
565
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700566struct io_connect {
567 struct file *file;
568 struct sockaddr __user *addr;
569 int addr_len;
570};
571
Jens Axboee47293f2019-12-20 08:58:21 -0700572struct io_sr_msg {
573 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700574 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100575 struct compat_msghdr __user *umsg_compat;
576 struct user_msghdr __user *umsg;
577 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700578 };
Jens Axboee47293f2019-12-20 08:58:21 -0700579 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700580 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700581 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700582 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700583};
584
Jens Axboe15b71ab2019-12-11 11:20:36 -0700585struct io_open {
586 struct file *file;
587 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100588 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700589 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700590 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600591 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700592};
593
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000594struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700595 struct file *file;
596 u64 arg;
597 u32 nr_args;
598 u32 offset;
599};
600
Jens Axboe4840e412019-12-25 22:03:45 -0700601struct io_fadvise {
602 struct file *file;
603 u64 offset;
604 u32 len;
605 u32 advice;
606};
607
Jens Axboec1ca7572019-12-25 22:18:28 -0700608struct io_madvise {
609 struct file *file;
610 u64 addr;
611 u32 len;
612 u32 advice;
613};
614
Jens Axboe3e4827b2020-01-08 15:18:09 -0700615struct io_epoll {
616 struct file *file;
617 int epfd;
618 int op;
619 int fd;
620 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700621};
622
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300623struct io_splice {
624 struct file *file_out;
625 struct file *file_in;
626 loff_t off_out;
627 loff_t off_in;
628 u64 len;
629 unsigned int flags;
630};
631
Jens Axboeddf0322d2020-02-23 16:41:33 -0700632struct io_provide_buf {
633 struct file *file;
634 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100635 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700636 __u32 bgid;
637 __u16 nbufs;
638 __u16 bid;
639};
640
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700641struct io_statx {
642 struct file *file;
643 int dfd;
644 unsigned int mask;
645 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700646 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700647 struct statx __user *buffer;
648};
649
Jens Axboe36f4fa62020-09-05 11:14:22 -0600650struct io_shutdown {
651 struct file *file;
652 int how;
653};
654
Jens Axboe80a261f2020-09-28 14:23:58 -0600655struct io_rename {
656 struct file *file;
657 int old_dfd;
658 int new_dfd;
659 struct filename *oldpath;
660 struct filename *newpath;
661 int flags;
662};
663
Jens Axboe14a11432020-09-28 14:27:37 -0600664struct io_unlink {
665 struct file *file;
666 int dfd;
667 int flags;
668 struct filename *filename;
669};
670
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700671struct io_mkdir {
672 struct file *file;
673 int dfd;
674 umode_t mode;
675 struct filename *filename;
676};
677
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700678struct io_symlink {
679 struct file *file;
680 int new_dfd;
681 struct filename *oldpath;
682 struct filename *newpath;
683};
684
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700685struct io_hardlink {
686 struct file *file;
687 int old_dfd;
688 int new_dfd;
689 struct filename *oldpath;
690 struct filename *newpath;
691 int flags;
692};
693
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300694struct io_completion {
695 struct file *file;
Pavel Begunkov8c3f9cd2021-02-28 22:35:15 +0000696 u32 cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300697};
698
Jens Axboef499a022019-12-02 16:28:46 -0700699struct io_async_connect {
700 struct sockaddr_storage address;
701};
702
Jens Axboe03b12302019-12-02 18:50:25 -0700703struct io_async_msghdr {
704 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000705 /* points to an allocated iov, if NULL we use fast_iov instead */
706 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700707 struct sockaddr __user *uaddr;
708 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700709 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700710};
711
Jens Axboef67676d2019-12-02 11:03:47 -0700712struct io_async_rw {
713 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600714 const struct iovec *free_iovec;
715 struct iov_iter iter;
Jens Axboecd658692021-09-10 11:19:14 -0600716 struct iov_iter_state iter_state;
Jens Axboe227c0c92020-08-13 11:51:40 -0600717 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600718 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700719};
720
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300721enum {
722 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
723 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
724 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
725 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
726 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700727 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300728
Pavel Begunkovdddca222021-04-27 16:13:52 +0100729 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100730 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300731 REQ_F_INFLIGHT_BIT,
732 REQ_F_CUR_POS_BIT,
733 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300734 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300735 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700736 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700737 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000738 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600739 REQ_F_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100740 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100741 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100742 REQ_F_ARM_LTIMEOUT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700743 /* keep async read/write and isreg together and in order */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100744 REQ_F_NOWAIT_READ_BIT,
745 REQ_F_NOWAIT_WRITE_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700746 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700747
748 /* not a real bit, just to check we're not overflowing the space */
749 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300750};
751
752enum {
753 /* ctx owns file */
754 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
755 /* drain existing IO first */
756 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
757 /* linked sqes */
758 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
759 /* doesn't sever on completion < 0 */
760 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
761 /* IOSQE_ASYNC */
762 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700763 /* IOSQE_BUFFER_SELECT */
764 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300765
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300766 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100767 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000768 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300769 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
770 /* read/write uses file position */
771 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
772 /* must not punt to workers */
773 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100774 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300775 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300776 /* needs cleanup */
777 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700778 /* already went through poll handler */
779 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700780 /* buffer already selected */
781 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000782 /* completion is deferred through io_comp_state */
783 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600784 /* caller should reissue async */
785 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700786 /* supports async reads */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100787 REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700788 /* supports async writes */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100789 REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700790 /* regular file */
791 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100792 /* has creds assigned */
793 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100794 /* skip refcounting if not set */
795 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100796 /* there is a linked timeout that has to be armed */
797 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700798};
799
800struct async_poll {
801 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600802 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300803};
804
Pavel Begunkovf237c302021-08-18 12:42:46 +0100805typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100806
Jens Axboe7cbf1722021-02-10 00:03:20 +0000807struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100808 union {
809 struct io_wq_work_node node;
810 struct llist_node fallback_node;
811 };
812 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000813};
814
Pavel Begunkov992da012021-06-10 16:37:37 +0100815enum {
816 IORING_RSRC_FILE = 0,
817 IORING_RSRC_BUFFER = 1,
818};
819
Jens Axboe09bb8392019-03-13 12:39:28 -0600820/*
821 * NOTE! Each of the iocb union members has the file pointer
822 * as the first entry in their struct definition. So you can
823 * access the file pointer through any of the sub-structs,
824 * or directly as just 'ki_filp' in this struct.
825 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700826struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700827 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600828 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700829 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700830 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100831 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700832 struct io_accept accept;
833 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700834 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700835 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100836 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700837 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700838 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700839 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700840 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000841 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700842 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700843 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700844 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300845 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700846 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700847 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600848 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600849 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600850 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700851 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700852 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700853 struct io_hardlink hardlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300854 /* use only after cleaning per-op data, see io_clean_op() */
855 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700856 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700857
Jens Axboee8c2bc12020-08-15 18:44:09 -0700858 /* opcode allocated if it needs to store data for async defer */
859 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700860 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800861 /* polled IO has completed */
862 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700863
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700864 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300865 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700866
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300867 struct io_ring_ctx *ctx;
868 unsigned int flags;
Jens Axboeabc54d62021-02-24 13:32:30 -0700869 atomic_t refs;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300870 struct task_struct *task;
871 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700872
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000873 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000874 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700875
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100876 /* used with ctx->iopoll_list with reads/writes */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300877 struct list_head inflight_entry;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100878 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300879 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
880 struct hlist_node hash_node;
881 struct async_poll *apoll;
882 struct io_wq_work work;
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100883 const struct cred *creds;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +0100884
Pavel Begunkoveae071c2021-04-25 14:32:24 +0100885 /* store used ubuf, so we can prevent reloading */
886 struct io_mapped_ubuf *imu;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700887};
888
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000889struct io_tctx_node {
890 struct list_head ctx_node;
891 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000892 struct io_ring_ctx *ctx;
893};
894
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300895struct io_defer_entry {
896 struct list_head list;
897 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300898 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300899};
900
Jens Axboed3656342019-12-18 09:50:26 -0700901struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700902 /* needs req->file assigned */
903 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700904 /* hash wq insertion if file is a regular file */
905 unsigned hash_reg_file : 1;
906 /* unbound wq insertion if file is a non-regular file */
907 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700908 /* opcode is not supported by this kernel */
909 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700910 /* set if opcode supports polled "wait" */
911 unsigned pollin : 1;
912 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700913 /* op supports buffer selection */
914 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000915 /* do prep async if is going to be punted */
916 unsigned needs_async_setup : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600917 /* should block plug */
918 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700919 /* size of async data needed, if any */
920 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700921};
922
Jens Axboe09186822020-10-13 15:01:40 -0600923static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300924 [IORING_OP_NOP] = {},
925 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700926 .needs_file = 1,
927 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700928 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700929 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000930 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600931 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700932 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700933 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300934 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700935 .needs_file = 1,
936 .hash_reg_file = 1,
937 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700938 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000939 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600940 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700941 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700942 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300943 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700944 .needs_file = 1,
945 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300946 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700947 .needs_file = 1,
948 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700949 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600950 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700951 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700952 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300953 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700954 .needs_file = 1,
955 .hash_reg_file = 1,
956 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700957 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600958 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700959 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700960 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300961 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700962 .needs_file = 1,
963 .unbound_nonreg_file = 1,
964 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300965 [IORING_OP_POLL_REMOVE] = {},
966 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700967 .needs_file = 1,
968 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300969 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700970 .needs_file = 1,
971 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700972 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000973 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700974 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700975 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300976 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700977 .needs_file = 1,
978 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700979 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700980 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000981 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700982 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700983 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300984 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700985 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700986 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000987 [IORING_OP_TIMEOUT_REMOVE] = {
988 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000989 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300990 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700991 .needs_file = 1,
992 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700993 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700994 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300995 [IORING_OP_ASYNC_CANCEL] = {},
996 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700997 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700998 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300999 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001000 .needs_file = 1,
1001 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001002 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +00001003 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001004 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -07001005 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001006 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -07001007 .needs_file = 1,
1008 },
Jens Axboe44526be2021-02-15 13:32:18 -07001009 [IORING_OP_OPENAT] = {},
1010 [IORING_OP_CLOSE] = {},
1011 [IORING_OP_FILES_UPDATE] = {},
1012 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001013 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001014 .needs_file = 1,
1015 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001016 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001017 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001018 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001019 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001020 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001021 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001022 .needs_file = 1,
Jens Axboe7b3188e2021-08-30 19:37:41 -06001023 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -07001024 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001025 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001026 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001027 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001028 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001029 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001030 .needs_file = 1,
1031 },
Jens Axboe44526be2021-02-15 13:32:18 -07001032 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001033 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001034 .needs_file = 1,
1035 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001036 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001037 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001038 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001039 .needs_file = 1,
1040 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001041 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001042 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001043 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001044 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001045 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001046 [IORING_OP_EPOLL_CTL] = {
1047 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001048 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001049 [IORING_OP_SPLICE] = {
1050 .needs_file = 1,
1051 .hash_reg_file = 1,
1052 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001053 },
1054 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001055 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001056 [IORING_OP_TEE] = {
1057 .needs_file = 1,
1058 .hash_reg_file = 1,
1059 .unbound_nonreg_file = 1,
1060 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001061 [IORING_OP_SHUTDOWN] = {
1062 .needs_file = 1,
1063 },
Jens Axboe44526be2021-02-15 13:32:18 -07001064 [IORING_OP_RENAMEAT] = {},
1065 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001066 [IORING_OP_MKDIRAT] = {},
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07001067 [IORING_OP_SYMLINKAT] = {},
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07001068 [IORING_OP_LINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001069};
1070
Pavel Begunkov0756a862021-08-15 10:40:25 +01001071/* requests with any of those set should undergo io_disarm_next() */
1072#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1073
Pavel Begunkov7a612352021-03-09 00:37:59 +00001074static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001075static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001076static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1077 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001078 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001079static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001080
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001081static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1082 long res, unsigned int cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001083static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001084static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001085static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001086static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001087static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001088 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001089 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001090static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001091static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001092 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001093static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001094static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001095
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001096static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkovc4501782021-09-08 16:40:52 +01001097static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001098static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001099
Pavel Begunkovb9445592021-08-25 12:25:45 +01001100static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1101 unsigned int issue_flags, u32 slot_index);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01001102static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags);
1103
Pavel Begunkovf1042b62021-08-28 19:54:39 -06001104static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
Pavel Begunkovb9445592021-08-25 12:25:45 +01001105
Jens Axboe2b188cc2019-01-07 10:46:33 -07001106static struct kmem_cache *req_cachep;
1107
Jens Axboe09186822020-10-13 15:01:40 -06001108static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109
1110struct sock *io_uring_get_socket(struct file *file)
1111{
1112#if defined(CONFIG_UNIX)
1113 if (file->f_op == &io_uring_fops) {
1114 struct io_ring_ctx *ctx = file->private_data;
1115
1116 return ctx->ring_sock->sk;
1117 }
1118#endif
1119 return NULL;
1120}
1121EXPORT_SYMBOL(io_uring_get_socket);
1122
Pavel Begunkovf237c302021-08-18 12:42:46 +01001123static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1124{
1125 if (!*locked) {
1126 mutex_lock(&ctx->uring_lock);
1127 *locked = true;
1128 }
1129}
1130
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001131#define io_for_each_link(pos, head) \
1132 for (pos = (head); pos; pos = pos->link)
1133
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001134/*
1135 * Shamelessly stolen from the mm implementation of page reference checking,
1136 * see commit f958d7b528b1 for details.
1137 */
1138#define req_ref_zero_or_close_to_overflow(req) \
1139 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1140
1141static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1142{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001143 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001144 return atomic_inc_not_zero(&req->refs);
1145}
1146
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001147static inline bool req_ref_put_and_test(struct io_kiocb *req)
1148{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001149 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1150 return true;
1151
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001152 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1153 return atomic_dec_and_test(&req->refs);
1154}
1155
1156static inline void req_ref_put(struct io_kiocb *req)
1157{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001158 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001159 WARN_ON_ONCE(req_ref_put_and_test(req));
1160}
1161
1162static inline void req_ref_get(struct io_kiocb *req)
1163{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001164 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001165 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1166 atomic_inc(&req->refs);
1167}
1168
Pavel Begunkovc4501782021-09-08 16:40:52 +01001169static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
1170{
1171 if (ctx->submit_state.compl_nr)
1172 __io_submit_flush_completions(ctx);
1173}
1174
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001175static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001176{
1177 if (!(req->flags & REQ_F_REFCOUNT)) {
1178 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001179 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001180 }
1181}
1182
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001183static inline void io_req_set_refcount(struct io_kiocb *req)
1184{
1185 __io_req_set_refcount(req, 1);
1186}
1187
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001188static inline void io_req_set_rsrc_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001189{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001190 struct io_ring_ctx *ctx = req->ctx;
1191
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001192 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001193 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001194 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001195 }
1196}
1197
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001198static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1199{
1200 bool got = percpu_ref_tryget(ref);
1201
1202 /* already at zero, wait for ->release() */
1203 if (!got)
1204 wait_for_completion(compl);
1205 percpu_ref_resurrect(ref);
1206 if (got)
1207 percpu_ref_put(ref);
1208}
1209
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001210static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1211 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001212{
1213 struct io_kiocb *req;
1214
Pavel Begunkov68207682021-03-22 01:58:25 +00001215 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001216 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001217 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001218 return true;
1219
1220 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001221 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001222 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001223 }
1224 return false;
1225}
1226
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001227static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001228{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001229 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001230}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001231
Hao Xua8295b92021-08-27 17:46:09 +08001232static inline void req_fail_link_node(struct io_kiocb *req, int res)
1233{
1234 req_set_fail(req);
1235 req->result = res;
1236}
1237
Jens Axboe2b188cc2019-01-07 10:46:33 -07001238static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1239{
1240 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1241
Jens Axboe0f158b42020-05-14 17:18:39 -06001242 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001243}
1244
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001245static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1246{
1247 return !req->timeout.off;
1248}
1249
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001250static void io_fallback_req_func(struct work_struct *work)
1251{
1252 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1253 fallback_work.work);
1254 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1255 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001256 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001257
1258 percpu_ref_get(&ctx->refs);
1259 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001260 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001261
Pavel Begunkovf237c302021-08-18 12:42:46 +01001262 if (locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01001263 io_submit_flush_completions(ctx);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001264 mutex_unlock(&ctx->uring_lock);
1265 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001266 percpu_ref_put(&ctx->refs);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001267
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001268}
1269
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1271{
1272 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001273 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001274
1275 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1276 if (!ctx)
1277 return NULL;
1278
Jens Axboe78076bb2019-12-04 19:56:40 -07001279 /*
1280 * Use 5 bits less than the max cq entries, that should give us around
1281 * 32 entries per hash list if totally full and uniformly spread.
1282 */
1283 hash_bits = ilog2(p->cq_entries);
1284 hash_bits -= 5;
1285 if (hash_bits <= 0)
1286 hash_bits = 1;
1287 ctx->cancel_hash_bits = hash_bits;
1288 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1289 GFP_KERNEL);
1290 if (!ctx->cancel_hash)
1291 goto err;
1292 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1293
Pavel Begunkov62248432021-04-28 13:11:29 +01001294 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1295 if (!ctx->dummy_ubuf)
1296 goto err;
1297 /* set invalid range, so io_import_fixed() fails meeting it */
1298 ctx->dummy_ubuf->ubuf = -1UL;
1299
Roman Gushchin21482892019-05-07 10:01:48 -07001300 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001301 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1302 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001303
1304 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001305 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001306 INIT_LIST_HEAD(&ctx->sqd_list);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001307 init_waitqueue_head(&ctx->poll_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001308 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001309 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001310 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001311 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001312 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001313 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001314 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001315 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001316 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001317 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001318 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001319 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001320 spin_lock_init(&ctx->rsrc_ref_lock);
1321 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001322 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1323 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001324 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001325 INIT_LIST_HEAD(&ctx->submit_state.free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001326 INIT_LIST_HEAD(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001327 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001328 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001329err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001330 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001331 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001332 kfree(ctx);
1333 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001334}
1335
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001336static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1337{
1338 struct io_rings *r = ctx->rings;
1339
1340 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1341 ctx->cq_extra--;
1342}
1343
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001344static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001345{
Jens Axboe2bc99302020-07-09 09:43:27 -06001346 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1347 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001348
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001349 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001350 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001351
Bob Liu9d858b22019-11-13 18:06:25 +08001352 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001353}
1354
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001355#define FFS_ASYNC_READ 0x1UL
1356#define FFS_ASYNC_WRITE 0x2UL
1357#ifdef CONFIG_64BIT
1358#define FFS_ISREG 0x4UL
1359#else
1360#define FFS_ISREG 0x0UL
1361#endif
1362#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
1363
1364static inline bool io_req_ffs_set(struct io_kiocb *req)
1365{
1366 return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE);
1367}
1368
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001369static void io_req_track_inflight(struct io_kiocb *req)
1370{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001371 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001372 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001373 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001374 }
1375}
1376
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001377static inline void io_unprep_linked_timeout(struct io_kiocb *req)
1378{
1379 req->flags &= ~REQ_F_LINK_TIMEOUT;
1380}
1381
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001382static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1383{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001384 if (WARN_ON_ONCE(!req->link))
1385 return NULL;
1386
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001387 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1388 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001389
1390 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001391 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001392 __io_req_set_refcount(req->link, 2);
1393 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001394}
1395
1396static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1397{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001398 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001399 return NULL;
1400 return __io_prep_linked_timeout(req);
1401}
1402
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001403static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001404{
Jens Axboed3656342019-12-18 09:50:26 -07001405 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001406 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001407
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001408 if (!(req->flags & REQ_F_CREDS)) {
1409 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001410 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001411 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001412
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001413 req->work.list.next = NULL;
1414 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001415 if (req->flags & REQ_F_FORCE_ASYNC)
1416 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1417
Jens Axboed3656342019-12-18 09:50:26 -07001418 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001419 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001420 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001421 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001422 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001423 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001424 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001425
1426 switch (req->opcode) {
1427 case IORING_OP_SPLICE:
1428 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001429 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1430 req->work.flags |= IO_WQ_WORK_UNBOUND;
1431 break;
1432 }
Jens Axboe561fb042019-10-24 07:25:42 -06001433}
1434
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001435static void io_prep_async_link(struct io_kiocb *req)
1436{
1437 struct io_kiocb *cur;
1438
Pavel Begunkov44eff402021-07-26 14:14:31 +01001439 if (req->flags & REQ_F_LINK_TIMEOUT) {
1440 struct io_ring_ctx *ctx = req->ctx;
1441
Jens Axboe79ebeae2021-08-10 15:18:27 -06001442 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001443 io_for_each_link(cur, req)
1444 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001445 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001446 } else {
1447 io_for_each_link(cur, req)
1448 io_prep_async_work(cur);
1449 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001450}
1451
Pavel Begunkovf237c302021-08-18 12:42:46 +01001452static void io_queue_async_work(struct io_kiocb *req, bool *locked)
Jens Axboe561fb042019-10-24 07:25:42 -06001453{
Jackie Liua197f662019-11-08 08:09:12 -07001454 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001455 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001456 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001457
Pavel Begunkovf237c302021-08-18 12:42:46 +01001458 /* must not take the lock, NULL it as a precaution */
1459 locked = NULL;
1460
Jens Axboe3bfe6102021-02-16 14:15:30 -07001461 BUG_ON(!tctx);
1462 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001463
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001464 /* init ->work of the whole link before punting */
1465 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001466
1467 /*
1468 * Not expected to happen, but if we do have a bug where this _can_
1469 * happen, catch it here and ensure the request is marked as
1470 * canceled. That will make io-wq go through the usual work cancel
1471 * procedure rather than attempt to run this request (or create a new
1472 * worker for it).
1473 */
1474 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1475 req->work.flags |= IO_WQ_WORK_CANCEL;
1476
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001477 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1478 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001479 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001480 if (link)
1481 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001482}
1483
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001484static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001485 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001486 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001487{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001488 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001489
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001490 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001491 if (status)
1492 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001493 atomic_set(&req->ctx->cq_timeouts,
1494 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001495 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001496 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001497 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001498 }
1499}
1500
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001501static void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001502{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001503 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001504 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1505 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001506
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001507 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001508 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001509 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001510 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001511 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001512 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001513}
1514
Pavel Begunkov360428f2020-05-30 14:54:17 +03001515static void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001516 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001517{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001518 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001519
Jens Axboe79ebeae2021-08-10 15:18:27 -06001520 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001521 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001522 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001523 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001524 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001525
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001526 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001527 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001528
1529 /*
1530 * Since seq can easily wrap around over time, subtract
1531 * the last seq at which timeouts were flushed before comparing.
1532 * Assuming not more than 2^31-1 events have happened since,
1533 * these subtractions won't have wrapped, so we can check if
1534 * target is in [last_seq, current_seq] by comparing the two.
1535 */
1536 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1537 events_got = seq - ctx->cq_last_tm_flush;
1538 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001539 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001540
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001541 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001542 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001543 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001544 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001545 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001546}
1547
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001548static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001549{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001550 if (ctx->off_timeout_used)
1551 io_flush_timeouts(ctx);
1552 if (ctx->drain_active)
1553 io_queue_deferred(ctx);
1554}
1555
1556static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1557{
1558 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1559 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001560 /* order cqe stores with ring update */
1561 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001562}
1563
Jens Axboe90554202020-09-03 12:12:41 -06001564static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1565{
1566 struct io_rings *r = ctx->rings;
1567
Pavel Begunkova566c552021-05-16 22:58:08 +01001568 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001569}
1570
Pavel Begunkov888aae22021-01-19 13:32:39 +00001571static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1572{
1573 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1574}
1575
Pavel Begunkovd068b502021-05-16 22:58:11 +01001576static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001577{
Hristo Venev75b28af2019-08-26 17:23:46 +00001578 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001579 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001580
Stefan Bühler115e12e2019-04-24 23:54:18 +02001581 /*
1582 * writes to the cq entry need to come after reading head; the
1583 * control dependency is enough as we're using WRITE_ONCE to
1584 * fill the cq entry
1585 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001586 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001587 return NULL;
1588
Pavel Begunkov888aae22021-01-19 13:32:39 +00001589 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001590 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001591}
1592
Jens Axboef2842ab2020-01-08 11:04:00 -07001593static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1594{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001595 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001596 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001597 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1598 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001599 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001600}
1601
Jens Axboe2c5d7632021-08-21 07:21:19 -06001602/*
1603 * This should only get called when at least one event has been posted.
1604 * Some applications rely on the eventfd notification count only changing
1605 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1606 * 1:1 relationship between how many times this function is called (and
1607 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1608 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001609static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001610{
Jens Axboe5fd46172021-08-06 14:04:31 -06001611 /*
1612 * wake_up_all() may seem excessive, but io_wake_function() and
1613 * io_should_wake() handle the termination of the loop and only
1614 * wake as many waiters as we need to.
1615 */
1616 if (wq_has_sleeper(&ctx->cq_wait))
1617 wake_up_all(&ctx->cq_wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001618 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1619 wake_up(&ctx->sq_data->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);
Pavel Begunkov3f008382021-10-01 10:39:33 +01001622 if (waitqueue_active(&ctx->poll_wait))
Pavel Begunkov311997b2021-06-14 23:37:28 +01001623 wake_up_interruptible(&ctx->poll_wait);
Jens Axboe8c838782019-03-12 15:48:16 -06001624}
1625
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001626static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1627{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001628 /* see waitqueue_active() comment */
1629 smp_mb();
1630
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001631 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001632 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001633 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001634 }
1635 if (io_should_trigger_evfd(ctx))
1636 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov3f008382021-10-01 10:39:33 +01001637 if (waitqueue_active(&ctx->poll_wait))
Pavel Begunkov311997b2021-06-14 23:37:28 +01001638 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001639}
1640
Jens Axboec4a2ed72019-11-21 21:01:26 -07001641/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001642static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001643{
Jens Axboeb18032b2021-01-24 16:58:56 -07001644 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001645
Pavel Begunkova566c552021-05-16 22:58:08 +01001646 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001647 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001648
Jens Axboeb18032b2021-01-24 16:58:56 -07001649 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001650 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001651 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001652 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001653 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001654
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001655 if (!cqe && !force)
1656 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001657 ocqe = list_first_entry(&ctx->cq_overflow_list,
1658 struct io_overflow_cqe, list);
1659 if (cqe)
1660 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1661 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001662 io_account_cq_overflow(ctx);
1663
Jens Axboeb18032b2021-01-24 16:58:56 -07001664 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001665 list_del(&ocqe->list);
1666 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001667 }
1668
Pavel Begunkov09e88402020-12-17 00:24:38 +00001669 all_flushed = list_empty(&ctx->cq_overflow_list);
1670 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001671 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001672 WRITE_ONCE(ctx->rings->sq_flags,
1673 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001674 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001675
Jens Axboeb18032b2021-01-24 16:58:56 -07001676 if (posted)
1677 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001678 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001679 if (posted)
1680 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001681 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001682}
1683
Pavel Begunkov90f67362021-08-09 20:18:12 +01001684static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001685{
Jens Axboeca0a2652021-03-04 17:15:48 -07001686 bool ret = true;
1687
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001688 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001689 /* iopoll syncs against uring_lock, not completion_lock */
1690 if (ctx->flags & IORING_SETUP_IOPOLL)
1691 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001692 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001693 if (ctx->flags & IORING_SETUP_IOPOLL)
1694 mutex_unlock(&ctx->uring_lock);
1695 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001696
1697 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001698}
1699
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001700/* must to be called somewhat shortly after putting a request */
1701static inline void io_put_task(struct task_struct *task, int nr)
1702{
1703 struct io_uring_task *tctx = task->io_uring;
1704
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001705 if (likely(task == current)) {
1706 tctx->cached_refs += nr;
1707 } else {
1708 percpu_counter_sub(&tctx->inflight, nr);
1709 if (unlikely(atomic_read(&tctx->in_idle)))
1710 wake_up(&tctx->wait);
1711 put_task_struct_many(task, nr);
1712 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001713}
1714
Pavel Begunkov9a108672021-08-27 11:55:01 +01001715static void io_task_refs_refill(struct io_uring_task *tctx)
1716{
1717 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1718
1719 percpu_counter_add(&tctx->inflight, refill);
1720 refcount_add(refill, &current->usage);
1721 tctx->cached_refs += refill;
1722}
1723
1724static inline void io_get_task_refs(int nr)
1725{
1726 struct io_uring_task *tctx = current->io_uring;
1727
1728 tctx->cached_refs -= nr;
1729 if (unlikely(tctx->cached_refs < 0))
1730 io_task_refs_refill(tctx);
1731}
1732
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001733static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1734 long res, unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001736 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001738 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1739 if (!ocqe) {
1740 /*
1741 * If we're in ring overflow flush mode, or in task cancel mode,
1742 * or cannot allocate an overflow entry, then we need to drop it
1743 * on the floor.
1744 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001745 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001746 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001747 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001748 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001749 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001750 WRITE_ONCE(ctx->rings->sq_flags,
1751 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1752
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001753 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001754 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001755 ocqe->cqe.res = res;
1756 ocqe->cqe.flags = cflags;
1757 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1758 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001759}
1760
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001761static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1762 long res, unsigned int cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001763{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001764 struct io_uring_cqe *cqe;
1765
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001766 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001767
1768 /*
1769 * If we can't get a cq entry, userspace overflowed the
1770 * submission (by quite a lot). Increment the overflow count in
1771 * the ring.
1772 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001773 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001774 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001775 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776 WRITE_ONCE(cqe->res, res);
1777 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001778 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001780 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001781}
1782
Pavel Begunkov8d133262021-04-11 01:46:33 +01001783/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001784static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1785 long res, unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001786{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001787 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001788}
1789
Pavel Begunkov7a612352021-03-09 00:37:59 +00001790static void io_req_complete_post(struct io_kiocb *req, long res,
1791 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001792{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001793 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001794
Jens Axboe79ebeae2021-08-10 15:18:27 -06001795 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001796 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001797 /*
1798 * If we're the last reference to this request, add to our locked
1799 * free_list cache.
1800 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001801 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001802 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001803 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001804 io_disarm_next(req);
1805 if (req->link) {
1806 io_req_task_queue(req->link);
1807 req->link = NULL;
1808 }
1809 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001810 io_dismantle_req(req);
1811 io_put_task(req->task, 1);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001812 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001813 ctx->locked_free_nr++;
Pavel Begunkova3f349072021-09-15 12:04:20 +01001814 percpu_ref_put(&ctx->refs);
Pavel Begunkov180f8292021-03-14 20:57:09 +00001815 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001816 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001817 spin_unlock(&ctx->completion_lock);
Pavel Begunkova3f349072021-09-15 12:04:20 +01001818 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001819}
1820
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001821static inline bool io_req_needs_clean(struct io_kiocb *req)
1822{
Pavel Begunkovc8543572021-06-17 18:14:04 +01001823 return req->flags & IO_REQ_CLEAN_FLAGS;
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001824}
1825
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001826static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001827 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001828{
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001829 if (io_req_needs_clean(req))
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001830 io_clean_op(req);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001831 req->result = res;
1832 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001833 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001834}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001835
Pavel Begunkov889fca72021-02-10 00:03:09 +00001836static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1837 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001838{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001839 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1840 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001841 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001842 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001843}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001844
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001845static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001846{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001847 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001848}
1849
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001850static void io_req_complete_failed(struct io_kiocb *req, long res)
1851{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001852 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001853 io_req_complete_post(req, res, 0);
1854}
1855
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01001856static void io_req_complete_fail_submit(struct io_kiocb *req)
1857{
1858 /*
1859 * We don't submit, fail them all, for that replace hardlinks with
1860 * normal links. Extra REQ_F_LINK is tolerated.
1861 */
1862 req->flags &= ~REQ_F_HARDLINK;
1863 req->flags |= REQ_F_LINK;
1864 io_req_complete_failed(req, req->result);
1865}
1866
Pavel Begunkov864ea922021-08-09 13:04:08 +01001867/*
1868 * Don't initialise the fields below on every allocation, but do that in
1869 * advance and keep them valid across allocations.
1870 */
1871static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1872{
1873 req->ctx = ctx;
1874 req->link = NULL;
1875 req->async_data = NULL;
1876 /* not necessary, but safer to zero */
1877 req->result = 0;
1878}
1879
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001880static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001881 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001882{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001883 spin_lock(&ctx->completion_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001884 list_splice_init(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001885 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001886 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001887}
1888
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001889/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001890static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001891{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001892 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001893 int nr;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001894
Jens Axboec7dae4b2021-02-09 19:53:37 -07001895 /*
1896 * If we have more than a batch's worth of requests in our IRQ side
1897 * locked cache, grab the lock and move them over to our submission
1898 * side cache.
1899 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001900 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001901 io_flush_cached_locked_reqs(ctx, state);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001902
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001903 nr = state->free_reqs;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001904 while (!list_empty(&state->free_list)) {
1905 struct io_kiocb *req = list_first_entry(&state->free_list,
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001906 struct io_kiocb, inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001907
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001908 list_del(&req->inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001909 state->reqs[nr++] = req;
1910 if (nr == ARRAY_SIZE(state->reqs))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001911 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001912 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001913
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001914 state->free_reqs = nr;
1915 return nr != 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916}
1917
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001918/*
1919 * A request might get retired back into the request caches even before opcode
1920 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1921 * Because of that, io_alloc_req() should be called only under ->uring_lock
1922 * and with extra caution to not get a request that is still worked on.
1923 */
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001924static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001925 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001926{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001927 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001928 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1929 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001930
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01001931 BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001932
Pavel Begunkov864ea922021-08-09 13:04:08 +01001933 if (likely(state->free_reqs || io_flush_cached_reqs(ctx)))
1934 goto got_req;
Jens Axboe2579f912019-01-09 09:10:43 -07001935
Pavel Begunkov864ea922021-08-09 13:04:08 +01001936 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1937 state->reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001938
Pavel Begunkov864ea922021-08-09 13:04:08 +01001939 /*
1940 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1941 * retry single alloc to be on the safe side.
1942 */
1943 if (unlikely(ret <= 0)) {
1944 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1945 if (!state->reqs[0])
1946 return NULL;
1947 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001948 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001949
1950 for (i = 0; i < ret; i++)
1951 io_preinit_req(state->reqs[i], ctx);
1952 state->free_reqs = ret;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001953got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001954 state->free_reqs--;
1955 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001956}
1957
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001958static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001959{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001960 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001961 fput(file);
1962}
1963
Pavel Begunkov6b639522021-09-08 16:40:50 +01001964static inline void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001965{
Pavel Begunkov094bae42021-03-19 17:22:42 +00001966 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001967
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001968 if (io_req_needs_clean(req))
1969 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001970 if (!(flags & REQ_F_FIXED_FILE))
1971 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001972 if (req->fixed_rsrc_refs)
1973 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001974 if (req->async_data) {
Pavel Begunkov094bae42021-03-19 17:22:42 +00001975 kfree(req->async_data);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001976 req->async_data = NULL;
1977 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001978}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001979
Pavel Begunkov216578e2020-10-13 09:44:00 +01001980static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001981{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001982 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001983
Pavel Begunkov216578e2020-10-13 09:44:00 +01001984 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001985 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001986
Jens Axboe79ebeae2021-08-10 15:18:27 -06001987 spin_lock(&ctx->completion_lock);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001988 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001989 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001990 spin_unlock(&ctx->completion_lock);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001991
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001992 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001993}
1994
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001995static inline void io_remove_next_linked(struct io_kiocb *req)
1996{
1997 struct io_kiocb *nxt = req->link;
1998
1999 req->link = nxt->link;
2000 nxt->link = NULL;
2001}
2002
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002003static bool io_kill_linked_timeout(struct io_kiocb *req)
2004 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06002005 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002006{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002007 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002008
Pavel Begunkovb97e7362021-08-15 10:40:23 +01002009 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002010 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002011
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002012 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002013 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01002014 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06002015 list_del(&link->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002016 io_cqring_fill_event(link->ctx, link->user_data,
2017 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002018 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002019 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002020 }
2021 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002022 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002023}
2024
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002025static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002026 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002027{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002028 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06002029
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002030 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002031 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002032 long res = -ECANCELED;
2033
2034 if (link->flags & REQ_F_FAIL)
2035 res = link->result;
2036
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002037 nxt = link->link;
2038 link->link = NULL;
2039
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002040 trace_io_uring_fail_link(req, link);
Hao Xua8295b92021-08-27 17:46:09 +08002041 io_cqring_fill_event(link->ctx, link->user_data, res, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002042 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002043 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002044 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002045}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002046
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002047static bool io_disarm_next(struct io_kiocb *req)
2048 __must_hold(&req->ctx->completion_lock)
2049{
2050 bool posted = false;
2051
Pavel Begunkov0756a862021-08-15 10:40:25 +01002052 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2053 struct io_kiocb *link = req->link;
2054
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002055 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002056 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2057 io_remove_next_linked(req);
2058 io_cqring_fill_event(link->ctx, link->user_data,
2059 -ECANCELED, 0);
2060 io_put_req_deferred(link);
2061 posted = true;
2062 }
2063 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002064 struct io_ring_ctx *ctx = req->ctx;
2065
2066 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002067 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002068 spin_unlock_irq(&ctx->timeout_lock);
2069 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002070 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002071 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002072 posted |= (req->link != NULL);
2073 io_fail_links(req);
2074 }
2075 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002076}
2077
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002078static void __io_req_find_next_prep(struct io_kiocb *req)
2079{
2080 struct io_ring_ctx *ctx = req->ctx;
2081 bool posted;
2082
2083 spin_lock(&ctx->completion_lock);
2084 posted = io_disarm_next(req);
2085 if (posted)
2086 io_commit_cqring(req->ctx);
2087 spin_unlock(&ctx->completion_lock);
2088 if (posted)
2089 io_cqring_ev_posted(ctx);
2090}
2091
2092static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002093{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002094 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002095
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002096 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2097 return NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002098 /*
2099 * If LINK is set, we have dependent requests in this chain. If we
2100 * didn't fail this request, queue the first one up, moving any other
2101 * dependencies to the next request. In case of failure, fail the rest
2102 * of the chain.
2103 */
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002104 if (unlikely(req->flags & IO_DISARM_MASK))
2105 __io_req_find_next_prep(req);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002106 nxt = req->link;
2107 req->link = NULL;
2108 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002109}
Jens Axboe2665abf2019-11-05 12:40:47 -07002110
Pavel Begunkovf237c302021-08-18 12:42:46 +01002111static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002112{
2113 if (!ctx)
2114 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002115 if (*locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01002116 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002117 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002118 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002119 }
2120 percpu_ref_put(&ctx->refs);
2121}
2122
Jens Axboe7cbf1722021-02-10 00:03:20 +00002123static void tctx_task_work(struct callback_head *cb)
2124{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002125 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002126 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002127 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2128 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002129
Pavel Begunkov16f72072021-06-17 18:14:09 +01002130 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002131 struct io_wq_work_node *node;
2132
Pavel Begunkovc4501782021-09-08 16:40:52 +01002133 if (!tctx->task_list.first && locked)
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002134 io_submit_flush_completions(ctx);
2135
Pavel Begunkov3f184072021-06-17 18:14:06 +01002136 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002137 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002138 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002139 if (!node)
2140 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002141 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002142 if (!node)
2143 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002144
Pavel Begunkov6294f362021-08-10 17:53:55 +01002145 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002146 struct io_wq_work_node *next = node->next;
2147 struct io_kiocb *req = container_of(node, struct io_kiocb,
2148 io_task_work.node);
2149
2150 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002151 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002152 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002153 /* if not contended, grab and improve batching */
2154 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002155 percpu_ref_get(&ctx->refs);
2156 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002157 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002158 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002159 } while (node);
2160
Jens Axboe7cbf1722021-02-10 00:03:20 +00002161 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002162 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002163
Pavel Begunkovf237c302021-08-18 12:42:46 +01002164 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002165}
2166
Pavel Begunkove09ee512021-07-01 13:26:05 +01002167static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002168{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002169 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002170 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002171 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002172 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002173 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002174 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002175
2176 WARN_ON_ONCE(!tctx);
2177
Jens Axboe0b81e802021-02-16 10:33:53 -07002178 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002179 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002180 running = tctx->task_running;
2181 if (!running)
2182 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002183 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002184
2185 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002186 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002187 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002188
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002189 /*
2190 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2191 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2192 * processing task_work. There's no reliable way to tell if TWA_RESUME
2193 * will do the job.
2194 */
2195 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovd97ec622021-09-08 16:40:53 +01002196 if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2197 if (notify == TWA_NONE)
2198 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002199 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002200 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002201
Pavel Begunkove09ee512021-07-01 13:26:05 +01002202 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002203 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002204 node = tctx->task_list.first;
2205 INIT_WQ_LIST(&tctx->task_list);
2206 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002207
Pavel Begunkove09ee512021-07-01 13:26:05 +01002208 while (node) {
2209 req = container_of(node, struct io_kiocb, io_task_work.node);
2210 node = node->next;
2211 if (llist_add(&req->io_task_work.fallback_node,
2212 &req->ctx->fallback_llist))
2213 schedule_delayed_work(&req->ctx->fallback_work, 1);
2214 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002215}
2216
Pavel Begunkovf237c302021-08-18 12:42:46 +01002217static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002218{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002219 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002220
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002221 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002222 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002223 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002224}
2225
Pavel Begunkovf237c302021-08-18 12:42:46 +01002226static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002227{
2228 struct io_ring_ctx *ctx = req->ctx;
2229
Pavel Begunkovf237c302021-08-18 12:42:46 +01002230 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002231 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002232 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002233 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002234 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002235 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002236}
2237
Pavel Begunkova3df76982021-02-18 22:32:52 +00002238static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2239{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002240 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002241 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002242 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002243}
2244
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002245static void io_req_task_queue(struct io_kiocb *req)
2246{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002247 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002248 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002249}
2250
Jens Axboe773af692021-07-27 10:25:55 -06002251static void io_req_task_queue_reissue(struct io_kiocb *req)
2252{
2253 req->io_task_work.func = io_queue_async_work;
2254 io_req_task_work_add(req);
2255}
2256
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002257static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002258{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002259 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002260
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002261 if (nxt)
2262 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002263}
2264
Jens Axboe9e645e112019-05-10 16:07:28 -06002265static void io_free_req(struct io_kiocb *req)
2266{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002267 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002268 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002269}
2270
Pavel Begunkovf237c302021-08-18 12:42:46 +01002271static void io_free_req_work(struct io_kiocb *req, bool *locked)
2272{
2273 io_free_req(req);
2274}
2275
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002276struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002277 struct task_struct *task;
2278 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002279 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002280};
2281
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002282static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002283{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002284 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002285 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002286 rb->task = NULL;
2287}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002288
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002289static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2290 struct req_batch *rb)
2291{
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002292 if (rb->ctx_refs)
2293 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkove98e49b2021-08-18 17:01:43 +01002294 if (rb->task)
Pavel Begunkove9dbe222021-08-09 13:04:20 +01002295 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002296}
2297
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002298static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2299 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002300{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002301 io_queue_next(req);
Pavel Begunkov96670652021-03-19 17:22:32 +00002302 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002303
Jens Axboee3bc8e92020-09-24 08:45:57 -06002304 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002305 if (rb->task)
2306 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002307 rb->task = req->task;
2308 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002309 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002310 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002311 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002312
Pavel Begunkovbd759042021-02-12 03:23:50 +00002313 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002314 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002315 else
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002316 list_add(&req->inflight_entry, &state->free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002317}
2318
Pavel Begunkovc4501782021-09-08 16:40:52 +01002319static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
Jens Axboea141dd82021-08-12 12:48:34 -06002320 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002321{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002322 struct io_submit_state *state = &ctx->submit_state;
2323 int i, nr = state->compl_nr;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002324 struct req_batch rb;
2325
Jens Axboe79ebeae2021-08-10 15:18:27 -06002326 spin_lock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002327 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002328 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002329
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002330 __io_cqring_fill_event(ctx, req->user_data, req->result,
2331 req->compl.cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002332 }
2333 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002334 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002335 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002336
2337 io_init_req_batch(&rb);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002338 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002339 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov905c1722021-02-10 00:03:14 +00002340
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002341 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002342 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002343 }
2344
2345 io_req_free_batch_finish(ctx, &rb);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002346 state->compl_nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002347}
2348
Jens Axboeba816ad2019-09-28 11:36:45 -06002349/*
2350 * Drop reference to request, return next in chain (if there is one) if this
2351 * was the last reference to this request.
2352 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002353static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002354{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002355 struct io_kiocb *nxt = NULL;
2356
Jens Axboede9b4cc2021-02-24 13:28:27 -07002357 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002358 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002359 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002360 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002361 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002362}
2363
Pavel Begunkov0d850352021-03-19 17:22:37 +00002364static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002365{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002366 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002367 io_free_req(req);
2368}
2369
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002370static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002371{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002372 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002373 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002374 io_req_task_work_add(req);
2375 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002376}
2377
Pavel Begunkov6c503152021-01-04 20:36:36 +00002378static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002379{
2380 /* See comment at the top of this file */
2381 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002382 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002383}
2384
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002385static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2386{
2387 struct io_rings *rings = ctx->rings;
2388
2389 /* make sure SQ entry isn't read before tail */
2390 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2391}
2392
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002393static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002394{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002395 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002396
Jens Axboebcda7ba2020-02-23 16:42:51 -07002397 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2398 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002399 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002400 kfree(kbuf);
2401 return cflags;
2402}
2403
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002404static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2405{
2406 struct io_buffer *kbuf;
2407
Pavel Begunkovae421d92021-08-17 20:28:08 +01002408 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2409 return 0;
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002410 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2411 return io_put_kbuf(req, kbuf);
2412}
2413
Jens Axboe4c6e2772020-07-01 11:29:10 -06002414static inline bool io_run_task_work(void)
2415{
Nadav Amitef98eb02021-08-07 17:13:41 -07002416 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002417 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002418 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002419 return true;
2420 }
2421
2422 return false;
2423}
2424
Jens Axboedef596e2019-01-09 08:59:42 -07002425/*
2426 * Find and free completed poll iocbs
2427 */
2428static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002429 struct list_head *done)
Jens Axboedef596e2019-01-09 08:59:42 -07002430{
Jens Axboe8237e042019-12-28 10:48:22 -07002431 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002432 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002433
2434 /* order with ->result store in io_complete_rw_iopoll() */
2435 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002436
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002437 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002438 while (!list_empty(done)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002439 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002440 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002441
Pavel Begunkovae421d92021-08-17 20:28:08 +01002442 __io_cqring_fill_event(ctx, req->user_data, req->result,
2443 io_put_rw_kbuf(req));
Jens Axboedef596e2019-01-09 08:59:42 -07002444 (*nr_events)++;
2445
Jens Axboede9b4cc2021-02-24 13:28:27 -07002446 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002447 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002448 }
Jens Axboedef596e2019-01-09 08:59:42 -07002449
Jens Axboe09bb8392019-03-13 12:39:28 -06002450 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002451 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002452 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002453}
2454
Jens Axboedef596e2019-01-09 08:59:42 -07002455static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002456 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002457{
2458 struct io_kiocb *req, *tmp;
Christoph Hellwigd729cf92021-10-12 13:12:20 +02002459 unsigned int poll_flags = BLK_POLL_NOSLEEP;
Jens Axboeb688f112021-10-12 09:28:46 -06002460 DEFINE_IO_COMP_BATCH(iob);
Jens Axboedef596e2019-01-09 08:59:42 -07002461 LIST_HEAD(done);
Jens Axboedef596e2019-01-09 08:59:42 -07002462
2463 /*
2464 * Only spin for completions if we don't have multiple devices hanging
2465 * off our complete list, and we're under the requested amount.
2466 */
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002467 if (ctx->poll_multi_queue || *nr_events >= min)
2468 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002469
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002470 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002471 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002472 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002473
2474 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002475 * Move completed and retryable entries to our local lists.
2476 * If we find a request that requires polling, break out
2477 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002478 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002479 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002480 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002481 continue;
2482 }
2483 if (!list_empty(&done))
2484 break;
2485
Jens Axboeb688f112021-10-12 09:28:46 -06002486 ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002487 if (unlikely(ret < 0))
2488 return ret;
2489 else if (ret)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002490 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002491
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002492 /* iopoll may have completed current req */
Jens Axboeb688f112021-10-12 09:28:46 -06002493 if (!rq_list_empty(iob.req_list) ||
2494 READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002495 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002496 }
2497
Jens Axboeb688f112021-10-12 09:28:46 -06002498 if (!rq_list_empty(iob.req_list))
2499 iob.complete(&iob);
Jens Axboedef596e2019-01-09 08:59:42 -07002500 if (!list_empty(&done))
Pavel Begunkova8576af2021-08-15 10:40:21 +01002501 io_iopoll_complete(ctx, nr_events, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002502
Pavel Begunkova2416e12021-08-09 13:04:09 +01002503 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002504}
2505
2506/*
Jens Axboedef596e2019-01-09 08:59:42 -07002507 * We can't just wait for polled events to come to us, we have to actively
2508 * find and complete them.
2509 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002510static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002511{
2512 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2513 return;
2514
2515 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002516 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002517 unsigned int nr_events = 0;
2518
Pavel Begunkova8576af2021-08-15 10:40:21 +01002519 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002520
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002521 /* let it sleep and repeat later if can't complete a request */
2522 if (nr_events == 0)
2523 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002524 /*
2525 * Ensure we allow local-to-the-cpu processing to take place,
2526 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002527 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002528 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002529 if (need_resched()) {
2530 mutex_unlock(&ctx->uring_lock);
2531 cond_resched();
2532 mutex_lock(&ctx->uring_lock);
2533 }
Jens Axboedef596e2019-01-09 08:59:42 -07002534 }
2535 mutex_unlock(&ctx->uring_lock);
2536}
2537
Pavel Begunkov7668b922020-07-07 16:36:21 +03002538static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002539{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002540 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002541 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002542
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002543 /*
2544 * We disallow the app entering submit/complete with polling, but we
2545 * still need to lock the ring to prevent racing with polled issue
2546 * that got punted to a workqueue.
2547 */
2548 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002549 /*
2550 * Don't enter poll loop if we already have events pending.
2551 * If we do, we can potentially be spinning for commands that
2552 * already triggered a CQE (eg in error).
2553 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002554 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002555 __io_cqring_overflow_flush(ctx, false);
2556 if (io_cqring_events(ctx))
2557 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002558 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002559 /*
2560 * If a submit got punted to a workqueue, we can have the
2561 * application entering polling for a command before it gets
2562 * issued. That app will hold the uring_lock for the duration
2563 * of the poll right here, so we need to take a breather every
2564 * now and then to ensure that the issue has a chance to add
2565 * the poll to the issued list. Otherwise we can spin here
2566 * forever, while the workqueue is stuck trying to acquire the
2567 * very same mutex.
2568 */
Pavel Begunkove9979b32021-04-13 02:58:45 +01002569 if (list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002570 u32 tail = ctx->cached_cq_tail;
2571
Jens Axboe500f9fb2019-08-19 12:15:59 -06002572 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002573 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002574 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002575
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002576 /* some requests don't go through iopoll_list */
2577 if (tail != ctx->cached_cq_tail ||
2578 list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002579 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002580 }
Pavel Begunkova8576af2021-08-15 10:40:21 +01002581 ret = io_do_iopoll(ctx, &nr_events, min);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002582 } while (!ret && nr_events < min && !need_resched());
2583out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002584 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002585 return ret;
2586}
2587
Jens Axboe491381ce2019-10-17 09:20:46 -06002588static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002589{
Jens Axboe491381ce2019-10-17 09:20:46 -06002590 /*
2591 * Tell lockdep we inherited freeze protection from submission
2592 * thread.
2593 */
2594 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002595 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002596
Pavel Begunkov1c986792021-03-22 01:58:31 +00002597 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2598 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002599 }
2600}
2601
Jens Axboeb63534c2020-06-04 11:28:00 -06002602#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002603static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002604{
Pavel Begunkovab454432021-03-22 01:58:33 +00002605 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002606
Pavel Begunkovab454432021-03-22 01:58:33 +00002607 if (!rw)
2608 return !io_req_prep_async(req);
Jens Axboecd658692021-09-10 11:19:14 -06002609 iov_iter_restore(&rw->iter, &rw->iter_state);
Pavel Begunkovab454432021-03-22 01:58:33 +00002610 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002611}
Jens Axboeb63534c2020-06-04 11:28:00 -06002612
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002613static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002614{
Jens Axboe355afae2020-09-02 09:30:31 -06002615 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002616 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002617
Jens Axboe355afae2020-09-02 09:30:31 -06002618 if (!S_ISBLK(mode) && !S_ISREG(mode))
2619 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002620 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2621 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002622 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002623 /*
2624 * If ref is dying, we might be running poll reap from the exit work.
2625 * Don't attempt to reissue from that path, just let it fail with
2626 * -EAGAIN.
2627 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002628 if (percpu_ref_is_dying(&ctx->refs))
2629 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002630 /*
2631 * Play it safe and assume not safe to re-import and reissue if we're
2632 * not in the original thread group (or in task context).
2633 */
2634 if (!same_thread_group(req->task, current) || !in_task())
2635 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002636 return true;
2637}
Jens Axboee82ad482021-04-02 19:45:34 -06002638#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002639static bool io_resubmit_prep(struct io_kiocb *req)
2640{
2641 return false;
2642}
Jens Axboee82ad482021-04-02 19:45:34 -06002643static bool io_rw_should_reissue(struct io_kiocb *req)
2644{
2645 return false;
2646}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002647#endif
2648
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002649static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002650{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002651 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2652 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002653 if (res != req->result) {
2654 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2655 io_rw_should_reissue(req)) {
2656 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002657 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002658 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002659 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002660 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002661 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002662 return false;
2663}
2664
Pavel Begunkovf237c302021-08-18 12:42:46 +01002665static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002666{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002667 unsigned int cflags = io_put_rw_kbuf(req);
2668 long res = req->result;
2669
2670 if (*locked) {
2671 struct io_ring_ctx *ctx = req->ctx;
2672 struct io_submit_state *state = &ctx->submit_state;
2673
2674 io_req_complete_state(req, res, cflags);
2675 state->compl_reqs[state->compl_nr++] = req;
2676 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
2677 io_submit_flush_completions(ctx);
2678 } else {
2679 io_req_complete_post(req, res, cflags);
2680 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002681}
2682
2683static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2684 unsigned int issue_flags)
2685{
2686 if (__io_complete_rw_common(req, res))
2687 return;
Pavel Begunkov63637852021-09-02 00:38:22 +01002688 __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002689}
2690
2691static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2692{
Jens Axboe9adbd452019-12-20 08:45:55 -07002693 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002694
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002695 if (__io_complete_rw_common(req, res))
2696 return;
2697 req->result = res;
2698 req->io_task_work.func = io_req_task_complete;
2699 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002700}
2701
Jens Axboedef596e2019-01-09 08:59:42 -07002702static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2703{
Jens Axboe9adbd452019-12-20 08:45:55 -07002704 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002705
Jens Axboe491381ce2019-10-17 09:20:46 -06002706 if (kiocb->ki_flags & IOCB_WRITE)
2707 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002708 if (unlikely(res != req->result)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002709 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2710 req->flags |= REQ_F_REISSUE;
2711 return;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002712 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002713 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002714
2715 WRITE_ONCE(req->result, res);
Jens Axboeb9b0e0d2021-02-23 08:18:36 -07002716 /* order with io_iopoll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002717 smp_wmb();
2718 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002719}
2720
2721/*
2722 * After the iocb has been issued, it's safe to be found on the poll list.
2723 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002724 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002725 * accessing the kiocb cookie.
2726 */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002727static void io_iopoll_req_issued(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07002728{
2729 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002730 const bool in_async = io_wq_current_is_worker();
2731
2732 /* workqueue context doesn't hold uring_lock, grab it now */
2733 if (unlikely(in_async))
2734 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002735
2736 /*
2737 * Track whether we have multiple files in our lists. This will impact
2738 * how we do polling eventually, not spinning if we're on potentially
2739 * different devices.
2740 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002741 if (list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002742 ctx->poll_multi_queue = false;
2743 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002744 struct io_kiocb *list_req;
2745
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002746 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002747 inflight_entry);
Hao Xu915b3dd2021-06-28 05:37:30 +08002748
Christoph Hellwig30da1b42021-10-12 13:12:14 +02002749 if (list_req->file != req->file)
Hao Xu915b3dd2021-06-28 05:37:30 +08002750 ctx->poll_multi_queue = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002751 }
2752
2753 /*
2754 * For fast devices, IO may have already completed. If it has, add
2755 * it to the front so we find it first.
2756 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002757 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002758 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002759 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002760 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002761
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002762 if (unlikely(in_async)) {
2763 /*
2764 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2765 * in sq thread task context or in io worker task context. If
2766 * current task context is sq thread, we don't need to check
2767 * whether should wake up sq thread.
2768 */
2769 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2770 wq_has_sleeper(&ctx->sq_data->wait))
2771 wake_up(&ctx->sq_data->wait);
2772
2773 mutex_unlock(&ctx->uring_lock);
2774 }
Jens Axboedef596e2019-01-09 08:59:42 -07002775}
2776
Jens Axboe4503b762020-06-01 10:00:27 -06002777static bool io_bdev_nowait(struct block_device *bdev)
2778{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002779 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002780}
2781
Jens Axboe2b188cc2019-01-07 10:46:33 -07002782/*
2783 * If we tracked the file through the SCM inflight mechanism, we could support
2784 * any file. For now, just ensure that anything potentially problematic is done
2785 * inline.
2786 */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002787static bool __io_file_supports_nowait(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002788{
2789 umode_t mode = file_inode(file)->i_mode;
2790
Jens Axboe4503b762020-06-01 10:00:27 -06002791 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002792 if (IS_ENABLED(CONFIG_BLOCK) &&
2793 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002794 return true;
2795 return false;
2796 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002797 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002798 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002799 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002800 if (IS_ENABLED(CONFIG_BLOCK) &&
2801 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002802 file->f_op != &io_uring_fops)
2803 return true;
2804 return false;
2805 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806
Jens Axboec5b85622020-06-09 19:23:05 -06002807 /* any ->read/write should understand O_NONBLOCK */
2808 if (file->f_flags & O_NONBLOCK)
2809 return true;
2810
Jens Axboeaf197f52020-04-28 13:15:06 -06002811 if (!(file->f_mode & FMODE_NOWAIT))
2812 return false;
2813
2814 if (rw == READ)
2815 return file->f_op->read_iter != NULL;
2816
2817 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002818}
2819
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002820static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
Jens Axboe7b29f922021-03-12 08:30:14 -07002821{
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002822 if (rw == READ && (req->flags & REQ_F_NOWAIT_READ))
Jens Axboe7b29f922021-03-12 08:30:14 -07002823 return true;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002824 else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE))
Jens Axboe7b29f922021-03-12 08:30:14 -07002825 return true;
2826
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002827 return __io_file_supports_nowait(req->file, rw);
Jens Axboe7b29f922021-03-12 08:30:14 -07002828}
2829
Jens Axboe5d329e12021-09-14 11:08:37 -06002830static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2831 int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002832{
Jens Axboedef596e2019-01-09 08:59:42 -07002833 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002834 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002835 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002836 unsigned ioprio;
2837 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002838
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01002839 if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002840 req->flags |= REQ_F_ISREG;
2841
Jens Axboe2b188cc2019-01-07 10:46:33 -07002842 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002843 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002844 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002845 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002846 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002847 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002848 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2849 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2850 if (unlikely(ret))
2851 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002852
Jens Axboe5d329e12021-09-14 11:08:37 -06002853 /*
2854 * If the file is marked O_NONBLOCK, still allow retry for it if it
2855 * supports async. Otherwise it's impossible to use O_NONBLOCK files
2856 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2857 */
2858 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2859 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req, rw)))
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002860 req->flags |= REQ_F_NOWAIT;
2861
Jens Axboe2b188cc2019-01-07 10:46:33 -07002862 ioprio = READ_ONCE(sqe->ioprio);
2863 if (ioprio) {
2864 ret = ioprio_check_cap(ioprio);
2865 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002866 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002867
2868 kiocb->ki_ioprio = ioprio;
2869 } else
2870 kiocb->ki_ioprio = get_current_ioprio();
2871
Jens Axboedef596e2019-01-09 08:59:42 -07002872 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002873 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2874 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002875 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002876
Jens Axboe394918e2021-03-08 11:40:23 -07002877 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
Jens Axboedef596e2019-01-09 08:59:42 -07002878 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002879 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002880 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002881 if (kiocb->ki_flags & IOCB_HIPRI)
2882 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002883 kiocb->ki_complete = io_complete_rw;
2884 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002885
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002886 if (req->opcode == IORING_OP_READ_FIXED ||
2887 req->opcode == IORING_OP_WRITE_FIXED) {
2888 req->imu = NULL;
2889 io_req_set_rsrc_node(req);
2890 }
2891
Jens Axboe3529d8c2019-12-19 18:24:38 -07002892 req->rw.addr = READ_ONCE(sqe->addr);
2893 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002894 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002895 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002896}
2897
2898static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2899{
2900 switch (ret) {
2901 case -EIOCBQUEUED:
2902 break;
2903 case -ERESTARTSYS:
2904 case -ERESTARTNOINTR:
2905 case -ERESTARTNOHAND:
2906 case -ERESTART_RESTARTBLOCK:
2907 /*
2908 * We can't just restart the syscall, since previously
2909 * submitted sqes may already be in progress. Just fail this
2910 * IO with EINTR.
2911 */
2912 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002913 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002914 default:
2915 kiocb->ki_complete(kiocb, ret, 0);
2916 }
2917}
2918
Jens Axboea1d7c392020-06-22 11:09:46 -06002919static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002920 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002921{
Jens Axboeba042912019-12-25 16:33:42 -07002922 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002923 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002924
Jens Axboe227c0c92020-08-13 11:51:40 -06002925 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002926 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002927 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002928 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002929 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002930 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002931 }
2932
Jens Axboeba042912019-12-25 16:33:42 -07002933 if (req->flags & REQ_F_CUR_POS)
2934 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002935 if (ret >= 0 && (kiocb->ki_complete == io_complete_rw))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002936 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002937 else
2938 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002939
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002940 if (req->flags & REQ_F_REISSUE) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002941 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002942 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002943 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002944 } else {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002945 unsigned int cflags = io_put_rw_kbuf(req);
2946 struct io_ring_ctx *ctx = req->ctx;
2947
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002948 req_set_fail(req);
Hao Xu14cfbb72021-10-14 22:04:00 +08002949 if (!(issue_flags & IO_URING_F_NONBLOCK)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002950 mutex_lock(&ctx->uring_lock);
2951 __io_req_complete(req, issue_flags, ret, cflags);
2952 mutex_unlock(&ctx->uring_lock);
2953 } else {
2954 __io_req_complete(req, issue_flags, ret, cflags);
2955 }
Pavel Begunkov97284632021-04-08 19:28:03 +01002956 }
2957 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002958}
2959
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002960static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2961 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002962{
Jens Axboe9adbd452019-12-20 08:45:55 -07002963 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002964 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002965 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002966
Pavel Begunkov75769e32021-04-01 15:43:54 +01002967 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002968 return -EFAULT;
2969 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002970 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002971 return -EFAULT;
2972
2973 /*
2974 * May not be a start of buffer, set size appropriately
2975 * and advance us to the beginning.
2976 */
2977 offset = buf_addr - imu->ubuf;
2978 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002979
2980 if (offset) {
2981 /*
2982 * Don't use iov_iter_advance() here, as it's really slow for
2983 * using the latter parts of a big fixed buffer - it iterates
2984 * over each segment manually. We can cheat a bit here, because
2985 * we know that:
2986 *
2987 * 1) it's a BVEC iter, we set it up
2988 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2989 * first and last bvec
2990 *
2991 * So just find our index, and adjust the iterator afterwards.
2992 * If the offset is within the first bvec (or the whole first
2993 * bvec, just use iov_iter_advance(). This makes it easier
2994 * since we can just skip the first segment, which may not
2995 * be PAGE_SIZE aligned.
2996 */
2997 const struct bio_vec *bvec = imu->bvec;
2998
2999 if (offset <= bvec->bv_len) {
3000 iov_iter_advance(iter, offset);
3001 } else {
3002 unsigned long seg_skip;
3003
3004 /* skip first vec */
3005 offset -= bvec->bv_len;
3006 seg_skip = 1 + (offset >> PAGE_SHIFT);
3007
3008 iter->bvec = bvec + seg_skip;
3009 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003010 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003011 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003012 }
3013 }
3014
Pavel Begunkov847595d2021-02-04 13:52:06 +00003015 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003016}
3017
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003018static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3019{
3020 struct io_ring_ctx *ctx = req->ctx;
3021 struct io_mapped_ubuf *imu = req->imu;
3022 u16 index, buf_index = req->buf_index;
3023
3024 if (likely(!imu)) {
3025 if (unlikely(buf_index >= ctx->nr_user_bufs))
3026 return -EFAULT;
3027 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3028 imu = READ_ONCE(ctx->user_bufs[index]);
3029 req->imu = imu;
3030 }
3031 return __io_import_fixed(req, rw, iter, imu);
3032}
3033
Jens Axboebcda7ba2020-02-23 16:42:51 -07003034static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3035{
3036 if (needs_lock)
3037 mutex_unlock(&ctx->uring_lock);
3038}
3039
3040static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3041{
3042 /*
3043 * "Normal" inline submissions always hold the uring_lock, since we
3044 * grab it from the system call. Same is true for the SQPOLL offload.
3045 * The only exception is when we've detached the request and issue it
3046 * from an async worker thread, grab the lock for that case.
3047 */
3048 if (needs_lock)
3049 mutex_lock(&ctx->uring_lock);
3050}
3051
3052static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3053 int bgid, struct io_buffer *kbuf,
3054 bool needs_lock)
3055{
3056 struct io_buffer *head;
3057
3058 if (req->flags & REQ_F_BUFFER_SELECTED)
3059 return kbuf;
3060
3061 io_ring_submit_lock(req->ctx, needs_lock);
3062
3063 lockdep_assert_held(&req->ctx->uring_lock);
3064
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003065 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003066 if (head) {
3067 if (!list_empty(&head->list)) {
3068 kbuf = list_last_entry(&head->list, struct io_buffer,
3069 list);
3070 list_del(&kbuf->list);
3071 } else {
3072 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003073 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003074 }
3075 if (*len > kbuf->len)
3076 *len = kbuf->len;
3077 } else {
3078 kbuf = ERR_PTR(-ENOBUFS);
3079 }
3080
3081 io_ring_submit_unlock(req->ctx, needs_lock);
3082
3083 return kbuf;
3084}
3085
Jens Axboe4d954c22020-02-27 07:31:19 -07003086static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3087 bool needs_lock)
3088{
3089 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003090 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003091
3092 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003093 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003094 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3095 if (IS_ERR(kbuf))
3096 return kbuf;
3097 req->rw.addr = (u64) (unsigned long) kbuf;
3098 req->flags |= REQ_F_BUFFER_SELECTED;
3099 return u64_to_user_ptr(kbuf->addr);
3100}
3101
3102#ifdef CONFIG_COMPAT
3103static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3104 bool needs_lock)
3105{
3106 struct compat_iovec __user *uiov;
3107 compat_ssize_t clen;
3108 void __user *buf;
3109 ssize_t len;
3110
3111 uiov = u64_to_user_ptr(req->rw.addr);
3112 if (!access_ok(uiov, sizeof(*uiov)))
3113 return -EFAULT;
3114 if (__get_user(clen, &uiov->iov_len))
3115 return -EFAULT;
3116 if (clen < 0)
3117 return -EINVAL;
3118
3119 len = clen;
3120 buf = io_rw_buffer_select(req, &len, needs_lock);
3121 if (IS_ERR(buf))
3122 return PTR_ERR(buf);
3123 iov[0].iov_base = buf;
3124 iov[0].iov_len = (compat_size_t) len;
3125 return 0;
3126}
3127#endif
3128
3129static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3130 bool needs_lock)
3131{
3132 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3133 void __user *buf;
3134 ssize_t len;
3135
3136 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3137 return -EFAULT;
3138
3139 len = iov[0].iov_len;
3140 if (len < 0)
3141 return -EINVAL;
3142 buf = io_rw_buffer_select(req, &len, needs_lock);
3143 if (IS_ERR(buf))
3144 return PTR_ERR(buf);
3145 iov[0].iov_base = buf;
3146 iov[0].iov_len = len;
3147 return 0;
3148}
3149
3150static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3151 bool needs_lock)
3152{
Jens Axboedddb3e22020-06-04 11:27:01 -06003153 if (req->flags & REQ_F_BUFFER_SELECTED) {
3154 struct io_buffer *kbuf;
3155
3156 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3157 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3158 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003159 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003160 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003161 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003162 return -EINVAL;
3163
3164#ifdef CONFIG_COMPAT
3165 if (req->ctx->compat)
3166 return io_compat_import(req, iov, needs_lock);
3167#endif
3168
3169 return __io_iov_buffer_select(req, iov, needs_lock);
3170}
3171
Pavel Begunkov847595d2021-02-04 13:52:06 +00003172static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3173 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003174{
Jens Axboe9adbd452019-12-20 08:45:55 -07003175 void __user *buf = u64_to_user_ptr(req->rw.addr);
3176 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003177 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003178 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003179
Pavel Begunkov7d009162019-11-25 23:14:40 +03003180 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003181 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003182 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003183 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003184
Jens Axboebcda7ba2020-02-23 16:42:51 -07003185 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003186 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003187 return -EINVAL;
3188
Jens Axboe3a6820f2019-12-22 15:19:35 -07003189 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003190 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003191 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003192 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003193 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003194 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003195 }
3196
Jens Axboe3a6820f2019-12-22 15:19:35 -07003197 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3198 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003199 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003200 }
3201
Jens Axboe4d954c22020-02-27 07:31:19 -07003202 if (req->flags & REQ_F_BUFFER_SELECT) {
3203 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003204 if (!ret)
3205 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003206 *iovec = NULL;
3207 return ret;
3208 }
3209
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003210 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3211 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003212}
3213
Jens Axboe0fef9482020-08-26 10:36:20 -06003214static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3215{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003216 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003217}
3218
Jens Axboe32960612019-09-23 11:05:34 -06003219/*
3220 * For files that don't have ->read_iter() and ->write_iter(), handle them
3221 * by looping over ->read() or ->write() manually.
3222 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003223static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003224{
Jens Axboe4017eb92020-10-22 14:14:12 -06003225 struct kiocb *kiocb = &req->rw.kiocb;
3226 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003227 ssize_t ret = 0;
3228
3229 /*
3230 * Don't support polled IO through this interface, and we can't
3231 * support non-blocking either. For the latter, this just causes
3232 * the kiocb to be handled from an async context.
3233 */
3234 if (kiocb->ki_flags & IOCB_HIPRI)
3235 return -EOPNOTSUPP;
3236 if (kiocb->ki_flags & IOCB_NOWAIT)
3237 return -EAGAIN;
3238
3239 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003240 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003241 ssize_t nr;
3242
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003243 if (!iov_iter_is_bvec(iter)) {
3244 iovec = iov_iter_iovec(iter);
3245 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003246 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3247 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003248 }
3249
Jens Axboe32960612019-09-23 11:05:34 -06003250 if (rw == READ) {
3251 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003252 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003253 } else {
3254 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003255 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003256 }
3257
3258 if (nr < 0) {
3259 if (!ret)
3260 ret = nr;
3261 break;
3262 }
Jens Axboe16c8d2d2021-09-12 06:45:07 -06003263 if (!iov_iter_is_bvec(iter)) {
3264 iov_iter_advance(iter, nr);
3265 } else {
3266 req->rw.len -= nr;
3267 req->rw.addr += nr;
3268 }
Jens Axboe32960612019-09-23 11:05:34 -06003269 ret += nr;
3270 if (nr != iovec.iov_len)
3271 break;
Jens Axboe32960612019-09-23 11:05:34 -06003272 }
3273
3274 return ret;
3275}
3276
Jens Axboeff6165b2020-08-13 09:47:43 -06003277static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3278 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003279{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003280 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003281
Jens Axboeff6165b2020-08-13 09:47:43 -06003282 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003283 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003284 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003285 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003286 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003287 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003288 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003289 unsigned iov_off = 0;
3290
3291 rw->iter.iov = rw->fast_iov;
3292 if (iter->iov != fast_iov) {
3293 iov_off = iter->iov - fast_iov;
3294 rw->iter.iov += iov_off;
3295 }
3296 if (rw->fast_iov != fast_iov)
3297 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003298 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003299 } else {
3300 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003301 }
3302}
3303
Hao Xu8d4af682021-09-22 18:15:22 +08003304static inline bool io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003305{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003306 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3307 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3308 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003309}
3310
Jens Axboeff6165b2020-08-13 09:47:43 -06003311static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3312 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003313 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003314{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003315 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003316 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003317 if (!req->async_data) {
Jens Axboecd658692021-09-10 11:19:14 -06003318 struct io_async_rw *iorw;
3319
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003320 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003321 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003322 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003323 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003324
Jens Axboeff6165b2020-08-13 09:47:43 -06003325 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboecd658692021-09-10 11:19:14 -06003326 iorw = req->async_data;
3327 /* we've copied and mapped the iter, ensure state is saved */
3328 iov_iter_save_state(&iorw->iter, &iorw->iter_state);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003329 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003330 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003331}
3332
Pavel Begunkov73debe62020-09-30 22:57:54 +03003333static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003334{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003335 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003336 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003337 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003338
Pavel Begunkov2846c482020-11-07 13:16:27 +00003339 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003340 if (unlikely(ret < 0))
3341 return ret;
3342
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003343 iorw->bytes_done = 0;
3344 iorw->free_iovec = iov;
3345 if (iov)
3346 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecd658692021-09-10 11:19:14 -06003347 iov_iter_save_state(&iorw->iter, &iorw->iter_state);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003348 return 0;
3349}
3350
Pavel Begunkov73debe62020-09-30 22:57:54 +03003351static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003352{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003353 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3354 return -EBADF;
Jens Axboe5d329e12021-09-14 11:08:37 -06003355 return io_prep_rw(req, sqe, READ);
Jens Axboef67676d2019-12-02 11:03:47 -07003356}
3357
Jens Axboec1dd91d2020-08-03 16:43:59 -06003358/*
3359 * This is our waitqueue callback handler, registered through lock_page_async()
3360 * when we initially tried to do the IO with the iocb armed our waitqueue.
3361 * This gets called when the page is unlocked, and we generally expect that to
3362 * happen when the page IO is completed and the page is now uptodate. This will
3363 * queue a task_work based retry of the operation, attempting to copy the data
3364 * again. If the latter fails because the page was NOT uptodate, then we will
3365 * do a thread based blocking retry of the operation. That's the unexpected
3366 * slow path.
3367 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003368static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3369 int sync, void *arg)
3370{
3371 struct wait_page_queue *wpq;
3372 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003373 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003374
3375 wpq = container_of(wait, struct wait_page_queue, wait);
3376
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003377 if (!wake_page_match(wpq, key))
3378 return 0;
3379
Hao Xuc8d317a2020-09-29 20:00:45 +08003380 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003381 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003382 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003383 return 1;
3384}
3385
Jens Axboec1dd91d2020-08-03 16:43:59 -06003386/*
3387 * This controls whether a given IO request should be armed for async page
3388 * based retry. If we return false here, the request is handed to the async
3389 * worker threads for retry. If we're doing buffered reads on a regular file,
3390 * we prepare a private wait_page_queue entry and retry the operation. This
3391 * will either succeed because the page is now uptodate and unlocked, or it
3392 * will register a callback when the page is unlocked at IO completion. Through
3393 * that callback, io_uring uses task_work to setup a retry of the operation.
3394 * That retry will attempt the buffered read again. The retry will generally
3395 * succeed, or in rare cases where it fails, we then fall back to using the
3396 * async worker threads for a blocking retry.
3397 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003398static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003399{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003400 struct io_async_rw *rw = req->async_data;
3401 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003402 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003403
3404 /* never retry for NOWAIT, we just complete with -EAGAIN */
3405 if (req->flags & REQ_F_NOWAIT)
3406 return false;
3407
Jens Axboe227c0c92020-08-13 11:51:40 -06003408 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003409 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003410 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003411
Jens Axboebcf5a062020-05-22 09:24:42 -06003412 /*
3413 * just use poll if we can, and don't attempt if the fs doesn't
3414 * support callback based unlocks
3415 */
3416 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3417 return false;
3418
Jens Axboe3b2a4432020-08-16 10:58:43 -07003419 wait->wait.func = io_async_buf_func;
3420 wait->wait.private = req;
3421 wait->wait.flags = 0;
3422 INIT_LIST_HEAD(&wait->wait.entry);
3423 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003424 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003425 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003426 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003427}
3428
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003429static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003430{
3431 if (req->file->f_op->read_iter)
3432 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003433 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003434 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003435 else
3436 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003437}
3438
Ming Lei7db30432021-08-21 23:07:51 +08003439static bool need_read_all(struct io_kiocb *req)
3440{
3441 return req->flags & REQ_F_ISREG ||
3442 S_ISBLK(file_inode(req->file)->i_mode);
3443}
3444
Pavel Begunkov889fca72021-02-10 00:03:09 +00003445static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003446{
3447 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003448 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003449 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003450 struct io_async_rw *rw = req->async_data;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003451 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003452 struct iov_iter_state __state, *state;
3453 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003454
Pavel Begunkov2846c482020-11-07 13:16:27 +00003455 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003456 iter = &rw->iter;
Jens Axboecd658692021-09-10 11:19:14 -06003457 state = &rw->iter_state;
3458 /*
3459 * We come here from an earlier attempt, restore our state to
3460 * match in case it doesn't. It's cheap enough that we don't
3461 * need to make this conditional.
3462 */
3463 iov_iter_restore(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003464 iovec = NULL;
3465 } else {
3466 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3467 if (ret < 0)
3468 return ret;
Jens Axboecd658692021-09-10 11:19:14 -06003469 state = &__state;
3470 iov_iter_save_state(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003471 }
Jens Axboecd658692021-09-10 11:19:14 -06003472 req->result = iov_iter_count(iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003473
Jens Axboefd6c2e42019-12-18 12:19:41 -07003474 /* Ensure we clear previously set non-block flag */
3475 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003476 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003477 else
3478 kiocb->ki_flags |= IOCB_NOWAIT;
3479
Pavel Begunkov24c74672020-06-21 13:09:51 +03003480 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003481 if (force_nonblock && !io_file_supports_nowait(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003482 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003483 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003484 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003485
Jens Axboecd658692021-09-10 11:19:14 -06003486 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003487 if (unlikely(ret)) {
3488 kfree(iovec);
3489 return ret;
3490 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003491
Jens Axboe227c0c92020-08-13 11:51:40 -06003492 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003493
Jens Axboe230d50d2021-04-01 20:41:15 -06003494 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003495 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003496 /* IOPOLL retry should happen for io-wq threads */
3497 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003498 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003499 /* no retry on NONBLOCK nor RWF_NOWAIT */
3500 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003501 goto done;
Jens Axboef38c7e32020-09-25 15:23:43 -06003502 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003503 } else if (ret == -EIOCBQUEUED) {
3504 goto out_free;
Jens Axboecd658692021-09-10 11:19:14 -06003505 } else if (ret <= 0 || ret == req->result || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003506 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003507 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003508 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003509 }
3510
Jens Axboecd658692021-09-10 11:19:14 -06003511 /*
3512 * Don't depend on the iter state matching what was consumed, or being
3513 * untouched in case of error. Restore it and we'll advance it
3514 * manually if we need to.
3515 */
3516 iov_iter_restore(iter, state);
3517
Jens Axboe227c0c92020-08-13 11:51:40 -06003518 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003519 if (ret2)
3520 return ret2;
3521
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003522 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003523 rw = req->async_data;
Jens Axboecd658692021-09-10 11:19:14 -06003524 /*
3525 * Now use our persistent iterator and state, if we aren't already.
3526 * We've restored and mapped the iter to match.
3527 */
3528 if (iter != &rw->iter) {
3529 iter = &rw->iter;
3530 state = &rw->iter_state;
3531 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003532
Pavel Begunkovb23df912021-02-04 13:52:04 +00003533 do {
Jens Axboecd658692021-09-10 11:19:14 -06003534 /*
3535 * We end up here because of a partial read, either from
3536 * above or inside this loop. Advance the iter by the bytes
3537 * that were consumed.
3538 */
3539 iov_iter_advance(iter, ret);
3540 if (!iov_iter_count(iter))
3541 break;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003542 rw->bytes_done += ret;
Jens Axboecd658692021-09-10 11:19:14 -06003543 iov_iter_save_state(iter, state);
3544
Pavel Begunkovb23df912021-02-04 13:52:04 +00003545 /* if we can retry, do so with the callbacks armed */
3546 if (!io_rw_should_retry(req)) {
3547 kiocb->ki_flags &= ~IOCB_WAITQ;
3548 return -EAGAIN;
3549 }
3550
3551 /*
3552 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3553 * we get -EIOCBQUEUED, then we'll get a notification when the
3554 * desired page gets unlocked. We can also get a partial read
3555 * here, and if we do, then just retry at the new offset.
3556 */
3557 ret = io_iter_do_read(req, iter);
3558 if (ret == -EIOCBQUEUED)
3559 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003560 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003561 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboecd658692021-09-10 11:19:14 -06003562 iov_iter_restore(iter, state);
3563 } while (ret > 0);
Jens Axboe227c0c92020-08-13 11:51:40 -06003564done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003565 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003566out_free:
3567 /* it's faster to check here then delegate to kfree */
3568 if (iovec)
3569 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003570 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003571}
3572
Pavel Begunkov73debe62020-09-30 22:57:54 +03003573static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003574{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003575 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3576 return -EBADF;
Jens Axboe5d329e12021-09-14 11:08:37 -06003577 return io_prep_rw(req, sqe, WRITE);
Jens Axboef67676d2019-12-02 11:03:47 -07003578}
3579
Pavel Begunkov889fca72021-02-10 00:03:09 +00003580static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003581{
3582 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003583 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003584 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003585 struct io_async_rw *rw = req->async_data;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003586 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003587 struct iov_iter_state __state, *state;
3588 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003589
Pavel Begunkov2846c482020-11-07 13:16:27 +00003590 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003591 iter = &rw->iter;
Jens Axboecd658692021-09-10 11:19:14 -06003592 state = &rw->iter_state;
3593 iov_iter_restore(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003594 iovec = NULL;
3595 } else {
3596 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3597 if (ret < 0)
3598 return ret;
Jens Axboecd658692021-09-10 11:19:14 -06003599 state = &__state;
3600 iov_iter_save_state(iter, state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003601 }
Jens Axboecd658692021-09-10 11:19:14 -06003602 req->result = iov_iter_count(iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003603
Jens Axboefd6c2e42019-12-18 12:19:41 -07003604 /* Ensure we clear previously set non-block flag */
3605 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003606 kiocb->ki_flags &= ~IOCB_NOWAIT;
3607 else
3608 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003609
Pavel Begunkov24c74672020-06-21 13:09:51 +03003610 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003611 if (force_nonblock && !io_file_supports_nowait(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003612 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003613
Jens Axboe10d59342019-12-09 20:16:22 -07003614 /* file path doesn't support NOWAIT for non-direct_IO */
3615 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3616 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003617 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003618
Jens Axboecd658692021-09-10 11:19:14 -06003619 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003620 if (unlikely(ret))
3621 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003622
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003623 /*
3624 * Open-code file_start_write here to grab freeze protection,
3625 * which will be released by another thread in
3626 * io_complete_rw(). Fool lockdep by telling it the lock got
3627 * released so that it doesn't complain about the held lock when
3628 * we return to userspace.
3629 */
3630 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003631 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003632 __sb_writers_release(file_inode(req->file)->i_sb,
3633 SB_FREEZE_WRITE);
3634 }
3635 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003636
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003637 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003638 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003639 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003640 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003641 else
3642 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003643
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003644 if (req->flags & REQ_F_REISSUE) {
3645 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003646 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003647 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003648
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003649 /*
3650 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3651 * retry them without IOCB_NOWAIT.
3652 */
3653 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3654 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003655 /* no retry on NONBLOCK nor RWF_NOWAIT */
3656 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003657 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003658 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003659 /* IOPOLL retry should happen for io-wq threads */
3660 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3661 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003662done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003663 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003664 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003665copy_iov:
Jens Axboecd658692021-09-10 11:19:14 -06003666 iov_iter_restore(iter, state);
Jens Axboe227c0c92020-08-13 11:51:40 -06003667 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003668 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003669 }
Jens Axboe31b51512019-01-18 22:56:34 -07003670out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003671 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003672 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003673 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003674 return ret;
3675}
3676
Jens Axboe80a261f2020-09-28 14:23:58 -06003677static int io_renameat_prep(struct io_kiocb *req,
3678 const struct io_uring_sqe *sqe)
3679{
3680 struct io_rename *ren = &req->rename;
3681 const char __user *oldf, *newf;
3682
Jens Axboeed7eb252021-06-23 09:04:13 -06003683 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3684 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003685 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003686 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003687 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3688 return -EBADF;
3689
3690 ren->old_dfd = READ_ONCE(sqe->fd);
3691 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3692 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3693 ren->new_dfd = READ_ONCE(sqe->len);
3694 ren->flags = READ_ONCE(sqe->rename_flags);
3695
3696 ren->oldpath = getname(oldf);
3697 if (IS_ERR(ren->oldpath))
3698 return PTR_ERR(ren->oldpath);
3699
3700 ren->newpath = getname(newf);
3701 if (IS_ERR(ren->newpath)) {
3702 putname(ren->oldpath);
3703 return PTR_ERR(ren->newpath);
3704 }
3705
3706 req->flags |= REQ_F_NEED_CLEANUP;
3707 return 0;
3708}
3709
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003710static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003711{
3712 struct io_rename *ren = &req->rename;
3713 int ret;
3714
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003715 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003716 return -EAGAIN;
3717
3718 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3719 ren->newpath, ren->flags);
3720
3721 req->flags &= ~REQ_F_NEED_CLEANUP;
3722 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003723 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003724 io_req_complete(req, ret);
3725 return 0;
3726}
3727
Jens Axboe14a11432020-09-28 14:27:37 -06003728static int io_unlinkat_prep(struct io_kiocb *req,
3729 const struct io_uring_sqe *sqe)
3730{
3731 struct io_unlink *un = &req->unlink;
3732 const char __user *fname;
3733
Jens Axboe22634bc2021-06-23 09:07:45 -06003734 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3735 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003736 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3737 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003738 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003739 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3740 return -EBADF;
3741
3742 un->dfd = READ_ONCE(sqe->fd);
3743
3744 un->flags = READ_ONCE(sqe->unlink_flags);
3745 if (un->flags & ~AT_REMOVEDIR)
3746 return -EINVAL;
3747
3748 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3749 un->filename = getname(fname);
3750 if (IS_ERR(un->filename))
3751 return PTR_ERR(un->filename);
3752
3753 req->flags |= REQ_F_NEED_CLEANUP;
3754 return 0;
3755}
3756
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003757static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003758{
3759 struct io_unlink *un = &req->unlink;
3760 int ret;
3761
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003762 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003763 return -EAGAIN;
3764
3765 if (un->flags & AT_REMOVEDIR)
3766 ret = do_rmdir(un->dfd, un->filename);
3767 else
3768 ret = do_unlinkat(un->dfd, un->filename);
3769
3770 req->flags &= ~REQ_F_NEED_CLEANUP;
3771 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003772 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003773 io_req_complete(req, ret);
3774 return 0;
3775}
3776
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003777static int io_mkdirat_prep(struct io_kiocb *req,
3778 const struct io_uring_sqe *sqe)
3779{
3780 struct io_mkdir *mkd = &req->mkdir;
3781 const char __user *fname;
3782
3783 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3784 return -EINVAL;
3785 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3786 sqe->splice_fd_in)
3787 return -EINVAL;
3788 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3789 return -EBADF;
3790
3791 mkd->dfd = READ_ONCE(sqe->fd);
3792 mkd->mode = READ_ONCE(sqe->len);
3793
3794 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3795 mkd->filename = getname(fname);
3796 if (IS_ERR(mkd->filename))
3797 return PTR_ERR(mkd->filename);
3798
3799 req->flags |= REQ_F_NEED_CLEANUP;
3800 return 0;
3801}
3802
3803static int io_mkdirat(struct io_kiocb *req, int issue_flags)
3804{
3805 struct io_mkdir *mkd = &req->mkdir;
3806 int ret;
3807
3808 if (issue_flags & IO_URING_F_NONBLOCK)
3809 return -EAGAIN;
3810
3811 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3812
3813 req->flags &= ~REQ_F_NEED_CLEANUP;
3814 if (ret < 0)
3815 req_set_fail(req);
3816 io_req_complete(req, ret);
3817 return 0;
3818}
3819
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003820static int io_symlinkat_prep(struct io_kiocb *req,
3821 const struct io_uring_sqe *sqe)
3822{
3823 struct io_symlink *sl = &req->symlink;
3824 const char __user *oldpath, *newpath;
3825
3826 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3827 return -EINVAL;
3828 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3829 sqe->splice_fd_in)
3830 return -EINVAL;
3831 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3832 return -EBADF;
3833
3834 sl->new_dfd = READ_ONCE(sqe->fd);
3835 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3836 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3837
3838 sl->oldpath = getname(oldpath);
3839 if (IS_ERR(sl->oldpath))
3840 return PTR_ERR(sl->oldpath);
3841
3842 sl->newpath = getname(newpath);
3843 if (IS_ERR(sl->newpath)) {
3844 putname(sl->oldpath);
3845 return PTR_ERR(sl->newpath);
3846 }
3847
3848 req->flags |= REQ_F_NEED_CLEANUP;
3849 return 0;
3850}
3851
3852static int io_symlinkat(struct io_kiocb *req, int issue_flags)
3853{
3854 struct io_symlink *sl = &req->symlink;
3855 int ret;
3856
3857 if (issue_flags & IO_URING_F_NONBLOCK)
3858 return -EAGAIN;
3859
3860 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3861
3862 req->flags &= ~REQ_F_NEED_CLEANUP;
3863 if (ret < 0)
3864 req_set_fail(req);
3865 io_req_complete(req, ret);
3866 return 0;
3867}
3868
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003869static int io_linkat_prep(struct io_kiocb *req,
3870 const struct io_uring_sqe *sqe)
3871{
3872 struct io_hardlink *lnk = &req->hardlink;
3873 const char __user *oldf, *newf;
3874
3875 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3876 return -EINVAL;
3877 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3878 return -EINVAL;
3879 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3880 return -EBADF;
3881
3882 lnk->old_dfd = READ_ONCE(sqe->fd);
3883 lnk->new_dfd = READ_ONCE(sqe->len);
3884 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3885 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3886 lnk->flags = READ_ONCE(sqe->hardlink_flags);
3887
3888 lnk->oldpath = getname(oldf);
3889 if (IS_ERR(lnk->oldpath))
3890 return PTR_ERR(lnk->oldpath);
3891
3892 lnk->newpath = getname(newf);
3893 if (IS_ERR(lnk->newpath)) {
3894 putname(lnk->oldpath);
3895 return PTR_ERR(lnk->newpath);
3896 }
3897
3898 req->flags |= REQ_F_NEED_CLEANUP;
3899 return 0;
3900}
3901
3902static int io_linkat(struct io_kiocb *req, int issue_flags)
3903{
3904 struct io_hardlink *lnk = &req->hardlink;
3905 int ret;
3906
3907 if (issue_flags & IO_URING_F_NONBLOCK)
3908 return -EAGAIN;
3909
3910 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
3911 lnk->newpath, lnk->flags);
3912
3913 req->flags &= ~REQ_F_NEED_CLEANUP;
3914 if (ret < 0)
3915 req_set_fail(req);
3916 io_req_complete(req, ret);
3917 return 0;
3918}
3919
Jens Axboe36f4fa62020-09-05 11:14:22 -06003920static int io_shutdown_prep(struct io_kiocb *req,
3921 const struct io_uring_sqe *sqe)
3922{
3923#if defined(CONFIG_NET)
3924 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3925 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003926 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3927 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003928 return -EINVAL;
3929
3930 req->shutdown.how = READ_ONCE(sqe->len);
3931 return 0;
3932#else
3933 return -EOPNOTSUPP;
3934#endif
3935}
3936
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003937static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003938{
3939#if defined(CONFIG_NET)
3940 struct socket *sock;
3941 int ret;
3942
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003943 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003944 return -EAGAIN;
3945
Linus Torvalds48aba792020-12-16 12:44:05 -08003946 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003947 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003948 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003949
3950 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003951 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003952 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003953 io_req_complete(req, ret);
3954 return 0;
3955#else
3956 return -EOPNOTSUPP;
3957#endif
3958}
3959
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003960static int __io_splice_prep(struct io_kiocb *req,
3961 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003962{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003963 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003964 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003965
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003966 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3967 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003968
3969 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003970 sp->len = READ_ONCE(sqe->len);
3971 sp->flags = READ_ONCE(sqe->splice_flags);
3972
3973 if (unlikely(sp->flags & ~valid_flags))
3974 return -EINVAL;
3975
Pavel Begunkov62906e82021-08-10 14:52:47 +01003976 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003977 (sp->flags & SPLICE_F_FD_IN_FIXED));
3978 if (!sp->file_in)
3979 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003980 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003981 return 0;
3982}
3983
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003984static int io_tee_prep(struct io_kiocb *req,
3985 const struct io_uring_sqe *sqe)
3986{
3987 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3988 return -EINVAL;
3989 return __io_splice_prep(req, sqe);
3990}
3991
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003992static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003993{
3994 struct io_splice *sp = &req->splice;
3995 struct file *in = sp->file_in;
3996 struct file *out = sp->file_out;
3997 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3998 long ret = 0;
3999
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004000 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004001 return -EAGAIN;
4002 if (sp->len)
4003 ret = do_tee(in, out, sp->len, flags);
4004
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004005 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4006 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004007 req->flags &= ~REQ_F_NEED_CLEANUP;
4008
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004009 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004010 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004011 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004012 return 0;
4013}
4014
4015static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4016{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004017 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004018
4019 sp->off_in = READ_ONCE(sqe->splice_off_in);
4020 sp->off_out = READ_ONCE(sqe->off);
4021 return __io_splice_prep(req, sqe);
4022}
4023
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004024static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004025{
4026 struct io_splice *sp = &req->splice;
4027 struct file *in = sp->file_in;
4028 struct file *out = sp->file_out;
4029 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4030 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004031 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004032
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004033 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004034 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004035
4036 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4037 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004038
Jens Axboe948a7742020-05-17 14:21:38 -06004039 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004040 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004041
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004042 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4043 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004044 req->flags &= ~REQ_F_NEED_CLEANUP;
4045
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004046 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004047 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004048 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004049 return 0;
4050}
4051
Jens Axboe2b188cc2019-01-07 10:46:33 -07004052/*
4053 * IORING_OP_NOP just posts a completion event, nothing else.
4054 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004055static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004056{
4057 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004058
Jens Axboedef596e2019-01-09 08:59:42 -07004059 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4060 return -EINVAL;
4061
Pavel Begunkov889fca72021-02-10 00:03:09 +00004062 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004063 return 0;
4064}
4065
Pavel Begunkov1155c762021-02-18 18:29:38 +00004066static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004067{
Jens Axboe6b063142019-01-10 22:13:58 -07004068 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004069
Jens Axboe09bb8392019-03-13 12:39:28 -06004070 if (!req->file)
4071 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004072
Jens Axboe6b063142019-01-10 22:13:58 -07004073 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004074 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004075 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4076 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004077 return -EINVAL;
4078
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004079 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4080 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4081 return -EINVAL;
4082
4083 req->sync.off = READ_ONCE(sqe->off);
4084 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004085 return 0;
4086}
4087
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004088static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004089{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004090 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004091 int ret;
4092
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004093 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004094 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004095 return -EAGAIN;
4096
Jens Axboe9adbd452019-12-20 08:45:55 -07004097 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004098 end > 0 ? end : LLONG_MAX,
4099 req->sync.flags & IORING_FSYNC_DATASYNC);
4100 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004101 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004102 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004103 return 0;
4104}
4105
Jens Axboed63d1b52019-12-10 10:38:56 -07004106static int io_fallocate_prep(struct io_kiocb *req,
4107 const struct io_uring_sqe *sqe)
4108{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004109 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4110 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004111 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004112 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4113 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004114
4115 req->sync.off = READ_ONCE(sqe->off);
4116 req->sync.len = READ_ONCE(sqe->addr);
4117 req->sync.mode = READ_ONCE(sqe->len);
4118 return 0;
4119}
4120
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004121static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004122{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004123 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004124
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004125 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004126 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004127 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004128 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4129 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004130 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004131 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004132 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004133 return 0;
4134}
4135
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004136static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004137{
Jens Axboef8748882020-01-08 17:47:02 -07004138 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004139 int ret;
4140
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004141 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4142 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004143 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004144 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004145 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004146 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004147
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004148 /* open.how should be already initialised */
4149 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004150 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004151
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004152 req->open.dfd = READ_ONCE(sqe->fd);
4153 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004154 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004155 if (IS_ERR(req->open.filename)) {
4156 ret = PTR_ERR(req->open.filename);
4157 req->open.filename = NULL;
4158 return ret;
4159 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004160
4161 req->open.file_slot = READ_ONCE(sqe->file_index);
4162 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4163 return -EINVAL;
4164
Jens Axboe4022e7a2020-03-19 19:23:18 -06004165 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004166 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004167 return 0;
4168}
4169
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004170static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4171{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004172 u64 mode = READ_ONCE(sqe->len);
4173 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004174
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004175 req->open.how = build_open_how(flags, mode);
4176 return __io_openat_prep(req, sqe);
4177}
4178
Jens Axboecebdb982020-01-08 17:59:24 -07004179static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4180{
4181 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004182 size_t len;
4183 int ret;
4184
Jens Axboecebdb982020-01-08 17:59:24 -07004185 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4186 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004187 if (len < OPEN_HOW_SIZE_VER0)
4188 return -EINVAL;
4189
4190 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4191 len);
4192 if (ret)
4193 return ret;
4194
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004195 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004196}
4197
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004198static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004199{
4200 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004201 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004202 bool resolve_nonblock, nonblock_set;
4203 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004204 int ret;
4205
Jens Axboecebdb982020-01-08 17:59:24 -07004206 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004207 if (ret)
4208 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004209 nonblock_set = op.open_flag & O_NONBLOCK;
4210 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004211 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004212 /*
4213 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4214 * it'll always -EAGAIN
4215 */
4216 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4217 return -EAGAIN;
4218 op.lookup_flags |= LOOKUP_CACHED;
4219 op.open_flag |= O_NONBLOCK;
4220 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004221
Pavel Begunkovb9445592021-08-25 12:25:45 +01004222 if (!fixed) {
4223 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4224 if (ret < 0)
4225 goto err;
4226 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004227
4228 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004229 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004230 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004231 * We could hang on to this 'fd' on retrying, but seems like
4232 * marginal gain for something that is now known to be a slower
4233 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004234 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004235 if (!fixed)
4236 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004237
4238 ret = PTR_ERR(file);
4239 /* only retry if RESOLVE_CACHED wasn't already set by application */
4240 if (ret == -EAGAIN &&
4241 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4242 return -EAGAIN;
4243 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004244 }
4245
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004246 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4247 file->f_flags &= ~O_NONBLOCK;
4248 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004249
4250 if (!fixed)
4251 fd_install(ret, file);
4252 else
4253 ret = io_install_fixed_file(req, file, issue_flags,
4254 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004255err:
4256 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004257 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004258 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004259 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004260 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004261 return 0;
4262}
4263
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004264static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004265{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004266 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004267}
4268
Jens Axboe067524e2020-03-02 16:32:28 -07004269static int io_remove_buffers_prep(struct io_kiocb *req,
4270 const struct io_uring_sqe *sqe)
4271{
4272 struct io_provide_buf *p = &req->pbuf;
4273 u64 tmp;
4274
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004275 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4276 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004277 return -EINVAL;
4278
4279 tmp = READ_ONCE(sqe->fd);
4280 if (!tmp || tmp > USHRT_MAX)
4281 return -EINVAL;
4282
4283 memset(p, 0, sizeof(*p));
4284 p->nbufs = tmp;
4285 p->bgid = READ_ONCE(sqe->buf_group);
4286 return 0;
4287}
4288
4289static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4290 int bgid, unsigned nbufs)
4291{
4292 unsigned i = 0;
4293
4294 /* shouldn't happen */
4295 if (!nbufs)
4296 return 0;
4297
4298 /* the head kbuf is the list itself */
4299 while (!list_empty(&buf->list)) {
4300 struct io_buffer *nxt;
4301
4302 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4303 list_del(&nxt->list);
4304 kfree(nxt);
4305 if (++i == nbufs)
4306 return i;
4307 }
4308 i++;
4309 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004310 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004311
4312 return i;
4313}
4314
Pavel Begunkov889fca72021-02-10 00:03:09 +00004315static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004316{
4317 struct io_provide_buf *p = &req->pbuf;
4318 struct io_ring_ctx *ctx = req->ctx;
4319 struct io_buffer *head;
4320 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004321 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004322
4323 io_ring_submit_lock(ctx, !force_nonblock);
4324
4325 lockdep_assert_held(&ctx->uring_lock);
4326
4327 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004328 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004329 if (head)
4330 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004331 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004332 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004333
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004334 /* complete before unlock, IOPOLL may need the lock */
4335 __io_req_complete(req, issue_flags, ret, 0);
4336 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07004337 return 0;
4338}
4339
Jens Axboeddf0322d2020-02-23 16:41:33 -07004340static int io_provide_buffers_prep(struct io_kiocb *req,
4341 const struct io_uring_sqe *sqe)
4342{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004343 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004344 struct io_provide_buf *p = &req->pbuf;
4345 u64 tmp;
4346
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004347 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004348 return -EINVAL;
4349
4350 tmp = READ_ONCE(sqe->fd);
4351 if (!tmp || tmp > USHRT_MAX)
4352 return -E2BIG;
4353 p->nbufs = tmp;
4354 p->addr = READ_ONCE(sqe->addr);
4355 p->len = READ_ONCE(sqe->len);
4356
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004357 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4358 &size))
4359 return -EOVERFLOW;
4360 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4361 return -EOVERFLOW;
4362
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004363 size = (unsigned long)p->len * p->nbufs;
4364 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004365 return -EFAULT;
4366
4367 p->bgid = READ_ONCE(sqe->buf_group);
4368 tmp = READ_ONCE(sqe->off);
4369 if (tmp > USHRT_MAX)
4370 return -E2BIG;
4371 p->bid = tmp;
4372 return 0;
4373}
4374
4375static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4376{
4377 struct io_buffer *buf;
4378 u64 addr = pbuf->addr;
4379 int i, bid = pbuf->bid;
4380
4381 for (i = 0; i < pbuf->nbufs; i++) {
Jens Axboe9990da92021-09-24 07:39:08 -06004382 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004383 if (!buf)
4384 break;
4385
4386 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004387 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004388 buf->bid = bid;
4389 addr += pbuf->len;
4390 bid++;
4391 if (!*head) {
4392 INIT_LIST_HEAD(&buf->list);
4393 *head = buf;
4394 } else {
4395 list_add_tail(&buf->list, &(*head)->list);
4396 }
4397 }
4398
4399 return i ? i : -ENOMEM;
4400}
4401
Pavel Begunkov889fca72021-02-10 00:03:09 +00004402static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004403{
4404 struct io_provide_buf *p = &req->pbuf;
4405 struct io_ring_ctx *ctx = req->ctx;
4406 struct io_buffer *head, *list;
4407 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004408 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004409
4410 io_ring_submit_lock(ctx, !force_nonblock);
4411
4412 lockdep_assert_held(&ctx->uring_lock);
4413
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004414 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004415
4416 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004417 if (ret >= 0 && !list) {
4418 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4419 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004420 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004421 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004422 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004423 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004424 /* complete before unlock, IOPOLL may need the lock */
4425 __io_req_complete(req, issue_flags, ret, 0);
4426 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004427 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004428}
4429
Jens Axboe3e4827b2020-01-08 15:18:09 -07004430static int io_epoll_ctl_prep(struct io_kiocb *req,
4431 const struct io_uring_sqe *sqe)
4432{
4433#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004434 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004435 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004436 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004437 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004438
4439 req->epoll.epfd = READ_ONCE(sqe->fd);
4440 req->epoll.op = READ_ONCE(sqe->len);
4441 req->epoll.fd = READ_ONCE(sqe->off);
4442
4443 if (ep_op_has_event(req->epoll.op)) {
4444 struct epoll_event __user *ev;
4445
4446 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4447 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4448 return -EFAULT;
4449 }
4450
4451 return 0;
4452#else
4453 return -EOPNOTSUPP;
4454#endif
4455}
4456
Pavel Begunkov889fca72021-02-10 00:03:09 +00004457static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004458{
4459#if defined(CONFIG_EPOLL)
4460 struct io_epoll *ie = &req->epoll;
4461 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004462 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004463
4464 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4465 if (force_nonblock && ret == -EAGAIN)
4466 return -EAGAIN;
4467
4468 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004469 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004470 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004471 return 0;
4472#else
4473 return -EOPNOTSUPP;
4474#endif
4475}
4476
Jens Axboec1ca7572019-12-25 22:18:28 -07004477static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4478{
4479#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004480 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004481 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004482 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4483 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004484
4485 req->madvise.addr = READ_ONCE(sqe->addr);
4486 req->madvise.len = READ_ONCE(sqe->len);
4487 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4488 return 0;
4489#else
4490 return -EOPNOTSUPP;
4491#endif
4492}
4493
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004494static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004495{
4496#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4497 struct io_madvise *ma = &req->madvise;
4498 int ret;
4499
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004500 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004501 return -EAGAIN;
4502
Minchan Kim0726b012020-10-17 16:14:50 -07004503 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004504 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004505 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004506 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004507 return 0;
4508#else
4509 return -EOPNOTSUPP;
4510#endif
4511}
4512
Jens Axboe4840e412019-12-25 22:03:45 -07004513static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4514{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004515 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004516 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004517 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4518 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004519
4520 req->fadvise.offset = READ_ONCE(sqe->off);
4521 req->fadvise.len = READ_ONCE(sqe->len);
4522 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4523 return 0;
4524}
4525
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004526static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004527{
4528 struct io_fadvise *fa = &req->fadvise;
4529 int ret;
4530
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004531 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004532 switch (fa->advice) {
4533 case POSIX_FADV_NORMAL:
4534 case POSIX_FADV_RANDOM:
4535 case POSIX_FADV_SEQUENTIAL:
4536 break;
4537 default:
4538 return -EAGAIN;
4539 }
4540 }
Jens Axboe4840e412019-12-25 22:03:45 -07004541
4542 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4543 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004544 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004545 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004546 return 0;
4547}
4548
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004549static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4550{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004551 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004552 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004553 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004554 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004555 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004556 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004557
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004558 req->statx.dfd = READ_ONCE(sqe->fd);
4559 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004560 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004561 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4562 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004563
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004564 return 0;
4565}
4566
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004567static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004568{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004569 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004570 int ret;
4571
Pavel Begunkov59d70012021-03-22 01:58:30 +00004572 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004573 return -EAGAIN;
4574
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004575 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4576 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004577
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004578 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004579 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004580 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004581 return 0;
4582}
4583
Jens Axboeb5dba592019-12-11 14:02:38 -07004584static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4585{
Jens Axboe14587a462020-09-05 11:36:08 -06004586 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004587 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004588 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004589 sqe->rw_flags || sqe->buf_index)
Jens Axboeb5dba592019-12-11 14:02:38 -07004590 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004591 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004592 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004593
4594 req->close.fd = READ_ONCE(sqe->fd);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004595 req->close.file_slot = READ_ONCE(sqe->file_index);
4596 if (req->close.file_slot && req->close.fd)
4597 return -EINVAL;
4598
Jens Axboeb5dba592019-12-11 14:02:38 -07004599 return 0;
4600}
4601
Pavel Begunkov889fca72021-02-10 00:03:09 +00004602static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004603{
Jens Axboe9eac1902021-01-19 15:50:37 -07004604 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004605 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004606 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004607 struct file *file = NULL;
4608 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004609
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004610 if (req->close.file_slot) {
4611 ret = io_close_fixed(req, issue_flags);
4612 goto err;
4613 }
4614
Jens Axboe9eac1902021-01-19 15:50:37 -07004615 spin_lock(&files->file_lock);
4616 fdt = files_fdtable(files);
4617 if (close->fd >= fdt->max_fds) {
4618 spin_unlock(&files->file_lock);
4619 goto err;
4620 }
4621 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004622 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004623 spin_unlock(&files->file_lock);
4624 file = NULL;
4625 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004626 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004627
4628 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004629 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004630 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004631 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004632 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004633
Jens Axboe9eac1902021-01-19 15:50:37 -07004634 ret = __close_fd_get_file(close->fd, &file);
4635 spin_unlock(&files->file_lock);
4636 if (ret < 0) {
4637 if (ret == -ENOENT)
4638 ret = -EBADF;
4639 goto err;
4640 }
4641
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004642 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004643 ret = filp_close(file, current->files);
4644err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004645 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004646 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004647 if (file)
4648 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004649 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004650 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004651}
4652
Pavel Begunkov1155c762021-02-18 18:29:38 +00004653static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004654{
4655 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004656
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004657 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4658 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004659 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4660 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004661 return -EINVAL;
4662
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004663 req->sync.off = READ_ONCE(sqe->off);
4664 req->sync.len = READ_ONCE(sqe->len);
4665 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004666 return 0;
4667}
4668
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004669static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004670{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004671 int ret;
4672
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004673 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004674 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004675 return -EAGAIN;
4676
Jens Axboe9adbd452019-12-20 08:45:55 -07004677 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004678 req->sync.flags);
4679 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004680 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004681 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004682 return 0;
4683}
4684
YueHaibing469956e2020-03-04 15:53:52 +08004685#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004686static int io_setup_async_msg(struct io_kiocb *req,
4687 struct io_async_msghdr *kmsg)
4688{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004689 struct io_async_msghdr *async_msg = req->async_data;
4690
4691 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004692 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004693 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004694 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004695 return -ENOMEM;
4696 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004697 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004698 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004699 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004700 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004701 /* if were using fast_iov, set it to the new one */
4702 if (!async_msg->free_iov)
4703 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4704
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004705 return -EAGAIN;
4706}
4707
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004708static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4709 struct io_async_msghdr *iomsg)
4710{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004711 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004712 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004713 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004714 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004715}
4716
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004717static int io_sendmsg_prep_async(struct io_kiocb *req)
4718{
4719 int ret;
4720
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004721 ret = io_sendmsg_copy_hdr(req, req->async_data);
4722 if (!ret)
4723 req->flags |= REQ_F_NEED_CLEANUP;
4724 return ret;
4725}
4726
Jens Axboe3529d8c2019-12-19 18:24:38 -07004727static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004728{
Jens Axboee47293f2019-12-20 08:58:21 -07004729 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004730
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004731 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4732 return -EINVAL;
4733
Pavel Begunkov270a5942020-07-12 20:41:04 +03004734 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004735 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004736 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4737 if (sr->msg_flags & MSG_DONTWAIT)
4738 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004739
Jens Axboed8768362020-02-27 14:17:49 -07004740#ifdef CONFIG_COMPAT
4741 if (req->ctx->compat)
4742 sr->msg_flags |= MSG_CMSG_COMPAT;
4743#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004744 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004745}
4746
Pavel Begunkov889fca72021-02-10 00:03:09 +00004747static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004748{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004749 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004750 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004751 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004752 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004753 int ret;
4754
Florent Revestdba4a922020-12-04 12:36:04 +01004755 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004756 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004757 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004758
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004759 kmsg = req->async_data;
4760 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004761 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004762 if (ret)
4763 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004764 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004765 }
4766
Pavel Begunkov04411802021-04-01 15:44:00 +01004767 flags = req->sr_msg.msg_flags;
4768 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004769 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004770 if (flags & MSG_WAITALL)
4771 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4772
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004773 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004774 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004775 return io_setup_async_msg(req, kmsg);
4776 if (ret == -ERESTARTSYS)
4777 ret = -EINTR;
4778
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004779 /* fast path, check for non-NULL to avoid function call */
4780 if (kmsg->free_iov)
4781 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004782 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004783 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004784 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004785 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004786 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004787}
4788
Pavel Begunkov889fca72021-02-10 00:03:09 +00004789static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004790{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004791 struct io_sr_msg *sr = &req->sr_msg;
4792 struct msghdr msg;
4793 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004794 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004795 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004796 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004797 int ret;
4798
Florent Revestdba4a922020-12-04 12:36:04 +01004799 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004800 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004801 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004802
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004803 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4804 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004805 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004806
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004807 msg.msg_name = NULL;
4808 msg.msg_control = NULL;
4809 msg.msg_controllen = 0;
4810 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004811
Pavel Begunkov04411802021-04-01 15:44:00 +01004812 flags = req->sr_msg.msg_flags;
4813 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004814 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004815 if (flags & MSG_WAITALL)
4816 min_ret = iov_iter_count(&msg.msg_iter);
4817
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004818 msg.msg_flags = flags;
4819 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004820 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004821 return -EAGAIN;
4822 if (ret == -ERESTARTSYS)
4823 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004824
Stefan Metzmacher00312752021-03-20 20:33:36 +01004825 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004826 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004827 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004828 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004829}
4830
Pavel Begunkov1400e692020-07-12 20:41:05 +03004831static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4832 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004833{
4834 struct io_sr_msg *sr = &req->sr_msg;
4835 struct iovec __user *uiov;
4836 size_t iov_len;
4837 int ret;
4838
Pavel Begunkov1400e692020-07-12 20:41:05 +03004839 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4840 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004841 if (ret)
4842 return ret;
4843
4844 if (req->flags & REQ_F_BUFFER_SELECT) {
4845 if (iov_len > 1)
4846 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004847 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004848 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004849 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004850 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004851 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004852 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004853 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004854 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004855 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004856 if (ret > 0)
4857 ret = 0;
4858 }
4859
4860 return ret;
4861}
4862
4863#ifdef CONFIG_COMPAT
4864static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004865 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004866{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004867 struct io_sr_msg *sr = &req->sr_msg;
4868 struct compat_iovec __user *uiov;
4869 compat_uptr_t ptr;
4870 compat_size_t len;
4871 int ret;
4872
Pavel Begunkov4af34172021-04-11 01:46:30 +01004873 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4874 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004875 if (ret)
4876 return ret;
4877
4878 uiov = compat_ptr(ptr);
4879 if (req->flags & REQ_F_BUFFER_SELECT) {
4880 compat_ssize_t clen;
4881
4882 if (len > 1)
4883 return -EINVAL;
4884 if (!access_ok(uiov, sizeof(*uiov)))
4885 return -EFAULT;
4886 if (__get_user(clen, &uiov->iov_len))
4887 return -EFAULT;
4888 if (clen < 0)
4889 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004890 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004891 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004892 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004893 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004894 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004895 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004896 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004897 if (ret < 0)
4898 return ret;
4899 }
4900
4901 return 0;
4902}
Jens Axboe03b12302019-12-02 18:50:25 -07004903#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004904
Pavel Begunkov1400e692020-07-12 20:41:05 +03004905static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4906 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004907{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004908 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004909
4910#ifdef CONFIG_COMPAT
4911 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004912 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004913#endif
4914
Pavel Begunkov1400e692020-07-12 20:41:05 +03004915 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004916}
4917
Jens Axboebcda7ba2020-02-23 16:42:51 -07004918static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004919 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004920{
4921 struct io_sr_msg *sr = &req->sr_msg;
4922 struct io_buffer *kbuf;
4923
Jens Axboebcda7ba2020-02-23 16:42:51 -07004924 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4925 if (IS_ERR(kbuf))
4926 return kbuf;
4927
4928 sr->kbuf = kbuf;
4929 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004930 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004931}
4932
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004933static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4934{
4935 return io_put_kbuf(req, req->sr_msg.kbuf);
4936}
4937
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004938static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004939{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004940 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004941
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004942 ret = io_recvmsg_copy_hdr(req, req->async_data);
4943 if (!ret)
4944 req->flags |= REQ_F_NEED_CLEANUP;
4945 return ret;
4946}
4947
4948static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4949{
4950 struct io_sr_msg *sr = &req->sr_msg;
4951
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004952 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4953 return -EINVAL;
4954
Pavel Begunkov270a5942020-07-12 20:41:04 +03004955 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004956 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004957 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004958 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4959 if (sr->msg_flags & MSG_DONTWAIT)
4960 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004961
Jens Axboed8768362020-02-27 14:17:49 -07004962#ifdef CONFIG_COMPAT
4963 if (req->ctx->compat)
4964 sr->msg_flags |= MSG_CMSG_COMPAT;
4965#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004966 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004967}
4968
Pavel Begunkov889fca72021-02-10 00:03:09 +00004969static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004970{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004971 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004972 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004973 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004974 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004975 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004976 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004977 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004978
Florent Revestdba4a922020-12-04 12:36:04 +01004979 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004980 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004981 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004982
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004983 kmsg = req->async_data;
4984 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004985 ret = io_recvmsg_copy_hdr(req, &iomsg);
4986 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004987 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004988 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004989 }
4990
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004991 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004992 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004993 if (IS_ERR(kbuf))
4994 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004995 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004996 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4997 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004998 1, req->sr_msg.len);
4999 }
5000
Pavel Begunkov04411802021-04-01 15:44:00 +01005001 flags = req->sr_msg.msg_flags;
5002 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005003 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005004 if (flags & MSG_WAITALL)
5005 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
5006
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005007 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
5008 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005009 if (force_nonblock && ret == -EAGAIN)
5010 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005011 if (ret == -ERESTARTSYS)
5012 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005013
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005014 if (req->flags & REQ_F_BUFFER_SELECTED)
5015 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005016 /* fast path, check for non-NULL to avoid function call */
5017 if (kmsg->free_iov)
5018 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005019 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005020 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005021 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005022 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005023 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005024}
5025
Pavel Begunkov889fca72021-02-10 00:03:09 +00005026static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07005027{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005028 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005029 struct io_sr_msg *sr = &req->sr_msg;
5030 struct msghdr msg;
5031 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005032 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005033 struct iovec iov;
5034 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005035 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005036 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005037 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005038
Florent Revestdba4a922020-12-04 12:36:04 +01005039 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005040 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005041 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005042
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005043 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005044 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005045 if (IS_ERR(kbuf))
5046 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005047 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005048 }
5049
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005050 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005051 if (unlikely(ret))
5052 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005053
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005054 msg.msg_name = NULL;
5055 msg.msg_control = NULL;
5056 msg.msg_controllen = 0;
5057 msg.msg_namelen = 0;
5058 msg.msg_iocb = NULL;
5059 msg.msg_flags = 0;
5060
Pavel Begunkov04411802021-04-01 15:44:00 +01005061 flags = req->sr_msg.msg_flags;
5062 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005063 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005064 if (flags & MSG_WAITALL)
5065 min_ret = iov_iter_count(&msg.msg_iter);
5066
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005067 ret = sock_recvmsg(sock, &msg, flags);
5068 if (force_nonblock && ret == -EAGAIN)
5069 return -EAGAIN;
5070 if (ret == -ERESTARTSYS)
5071 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005072out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005073 if (req->flags & REQ_F_BUFFER_SELECTED)
5074 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01005075 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005076 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005077 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005078 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005079}
5080
Jens Axboe3529d8c2019-12-19 18:24:38 -07005081static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005082{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005083 struct io_accept *accept = &req->accept;
5084
Jens Axboe14587a462020-09-05 11:36:08 -06005085 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005086 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005087 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005088 return -EINVAL;
5089
Jens Axboed55e5f52019-12-11 16:12:15 -07005090 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5091 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005092 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005093 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005094
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005095 accept->file_slot = READ_ONCE(sqe->file_index);
5096 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5097 (accept->flags & SOCK_CLOEXEC)))
5098 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005099 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5100 return -EINVAL;
5101 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5102 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005103 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005104}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005105
Pavel Begunkov889fca72021-02-10 00:03:09 +00005106static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005107{
5108 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005109 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005110 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005111 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005112 struct file *file;
5113 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005114
Jiufei Xuee697dee2020-06-10 13:41:59 +08005115 if (req->file->f_flags & O_NONBLOCK)
5116 req->flags |= REQ_F_NOWAIT;
5117
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005118 if (!fixed) {
5119 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5120 if (unlikely(fd < 0))
5121 return fd;
5122 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005123 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5124 accept->flags);
5125 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005126 if (!fixed)
5127 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005128 ret = PTR_ERR(file);
5129 if (ret == -EAGAIN && force_nonblock)
5130 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005131 if (ret == -ERESTARTSYS)
5132 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005133 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005134 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005135 fd_install(fd, file);
5136 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005137 } else {
5138 ret = io_install_fixed_file(req, file, issue_flags,
5139 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005140 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005141 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005142 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005143}
5144
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005145static int io_connect_prep_async(struct io_kiocb *req)
5146{
5147 struct io_async_connect *io = req->async_data;
5148 struct io_connect *conn = &req->connect;
5149
5150 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5151}
5152
Jens Axboe3529d8c2019-12-19 18:24:38 -07005153static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005154{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005155 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005156
Jens Axboe14587a462020-09-05 11:36:08 -06005157 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005158 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005159 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5160 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005161 return -EINVAL;
5162
Jens Axboe3529d8c2019-12-19 18:24:38 -07005163 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5164 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005165 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005166}
5167
Pavel Begunkov889fca72021-02-10 00:03:09 +00005168static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005169{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005170 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005171 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005172 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005173 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005174
Jens Axboee8c2bc12020-08-15 18:44:09 -07005175 if (req->async_data) {
5176 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005177 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005178 ret = move_addr_to_kernel(req->connect.addr,
5179 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005180 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005181 if (ret)
5182 goto out;
5183 io = &__io;
5184 }
5185
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005186 file_flags = force_nonblock ? O_NONBLOCK : 0;
5187
Jens Axboee8c2bc12020-08-15 18:44:09 -07005188 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005189 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005190 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005191 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005192 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005193 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005194 ret = -ENOMEM;
5195 goto out;
5196 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005197 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005198 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005199 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005200 if (ret == -ERESTARTSYS)
5201 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005202out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005203 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005204 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005205 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005206 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005207}
YueHaibing469956e2020-03-04 15:53:52 +08005208#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005209#define IO_NETOP_FN(op) \
5210static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5211{ \
5212 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005213}
5214
Jens Axboe99a10082021-02-19 09:35:19 -07005215#define IO_NETOP_PREP(op) \
5216IO_NETOP_FN(op) \
5217static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5218{ \
5219 return -EOPNOTSUPP; \
5220} \
5221
5222#define IO_NETOP_PREP_ASYNC(op) \
5223IO_NETOP_PREP(op) \
5224static int io_##op##_prep_async(struct io_kiocb *req) \
5225{ \
5226 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005227}
5228
Jens Axboe99a10082021-02-19 09:35:19 -07005229IO_NETOP_PREP_ASYNC(sendmsg);
5230IO_NETOP_PREP_ASYNC(recvmsg);
5231IO_NETOP_PREP_ASYNC(connect);
5232IO_NETOP_PREP(accept);
5233IO_NETOP_FN(send);
5234IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005235#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005236
Jens Axboed7718a92020-02-14 22:23:12 -07005237struct io_poll_table {
5238 struct poll_table_struct pt;
5239 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005240 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005241 int error;
5242};
5243
Jens Axboed7718a92020-02-14 22:23:12 -07005244static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005245 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07005246{
Jens Axboed7718a92020-02-14 22:23:12 -07005247 /* for instances that support it check for an event match first: */
5248 if (mask && !(mask & poll->events))
5249 return 0;
5250
5251 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5252
5253 list_del_init(&poll->wait.entry);
5254
Jens Axboed7718a92020-02-14 22:23:12 -07005255 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005256 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005257
Jens Axboed7718a92020-02-14 22:23:12 -07005258 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005259 * If this fails, then the task is exiting. When a task exits, the
5260 * work gets canceled, so just cancel this request as well instead
5261 * of executing it. We can't safely execute it anyway, as we may not
5262 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005263 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005264 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005265 return 1;
5266}
5267
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005268static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5269 __acquires(&req->ctx->completion_lock)
5270{
5271 struct io_ring_ctx *ctx = req->ctx;
5272
Jens Axboe316319e2021-08-19 09:41:42 -06005273 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005274 if (unlikely(req->task->flags & PF_EXITING))
5275 WRITE_ONCE(poll->canceled, true);
5276
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005277 if (!req->result && !READ_ONCE(poll->canceled)) {
5278 struct poll_table_struct pt = { ._key = poll->events };
5279
5280 req->result = vfs_poll(req->file, &pt) & poll->events;
5281 }
5282
Jens Axboe79ebeae2021-08-10 15:18:27 -06005283 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005284 if (!req->result && !READ_ONCE(poll->canceled)) {
5285 add_wait_queue(poll->head, &poll->wait);
5286 return true;
5287 }
5288
5289 return false;
5290}
5291
Jens Axboed4e7cd32020-08-15 11:44:50 -07005292static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005293{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005294 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005295 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005296 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005297 return req->apoll->double_poll;
5298}
5299
5300static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5301{
5302 if (req->opcode == IORING_OP_POLL_ADD)
5303 return &req->poll;
5304 return &req->apoll->poll;
5305}
5306
5307static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005308 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005309{
5310 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005311
5312 lockdep_assert_held(&req->ctx->completion_lock);
5313
5314 if (poll && poll->head) {
5315 struct wait_queue_head *head = poll->head;
5316
Jens Axboe79ebeae2021-08-10 15:18:27 -06005317 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005318 list_del_init(&poll->wait.entry);
5319 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005320 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005321 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005322 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005323 }
5324}
5325
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005326static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005327 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005328{
5329 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005330 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005331 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005332
Pavel Begunkove27414b2021-04-09 09:13:20 +01005333 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005334 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005335 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005336 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005337 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005338 }
Jens Axboeb69de282021-03-17 08:37:41 -06005339 if (req->poll.events & EPOLLONESHOT)
5340 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005341 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
5342 req->poll.events |= EPOLLONESHOT;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005343 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005344 }
Hao Xu7b289c32021-04-13 15:20:39 +08005345 if (flags & IORING_CQE_F_MORE)
5346 ctx->cq_extra++;
5347
Jens Axboe88e41cf2021-02-22 22:08:01 -07005348 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005349}
5350
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005351static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
5352 __must_hold(&req->ctx->completion_lock)
5353{
5354 bool done;
5355
5356 done = __io_poll_complete(req, mask);
5357 io_commit_cqring(req->ctx);
5358 return done;
5359}
5360
Pavel Begunkovf237c302021-08-18 12:42:46 +01005361static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005362{
Jens Axboe6d816e02020-08-11 08:04:14 -06005363 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005364 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005365
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005366 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005367 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005368 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005369 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005370
Hao Xu5b7aa382021-09-22 18:12:38 +08005371 if (req->poll.done) {
5372 spin_unlock(&ctx->completion_lock);
5373 return;
5374 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005375 done = __io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005376 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005377 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005378 hash_del(&req->hash_node);
Hao Xubd99c712021-09-22 18:12:36 +08005379 req->poll.done = true;
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005380 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005381 req->result = 0;
5382 add_wait_queue(req->poll.head, &req->poll.wait);
5383 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005384 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005385 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005386 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005387
Jens Axboe88e41cf2021-02-22 22:08:01 -07005388 if (done) {
5389 nxt = io_put_req_find_next(req);
5390 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005391 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005392 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005393 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005394}
5395
5396static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5397 int sync, void *key)
5398{
5399 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005400 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005401 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005402 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005403
5404 /* for instances that support it check for an event match first: */
5405 if (mask && !(mask & poll->events))
5406 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005407 if (!(poll->events & EPOLLONESHOT))
5408 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005409
Jens Axboe8706e042020-09-28 08:38:54 -06005410 list_del_init(&wait->entry);
5411
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005412 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005413 bool done;
5414
Jens Axboe79ebeae2021-08-10 15:18:27 -06005415 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005416 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005417 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005418 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005419 /* make sure double remove sees this as being gone */
5420 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005421 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005422 if (!done) {
5423 /* use wait func handler, so it matches the rq type */
5424 poll->wait.func(&poll->wait, mode, sync, key);
5425 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005426 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005427 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005428 return 1;
5429}
5430
5431static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5432 wait_queue_func_t wake_func)
5433{
5434 poll->head = NULL;
5435 poll->done = false;
5436 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005437#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5438 /* mask in events that we always want/need */
5439 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005440 INIT_LIST_HEAD(&poll->wait.entry);
5441 init_waitqueue_func_entry(&poll->wait, wake_func);
5442}
5443
5444static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005445 struct wait_queue_head *head,
5446 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005447{
5448 struct io_kiocb *req = pt->req;
5449
5450 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005451 * The file being polled uses multiple waitqueues for poll handling
5452 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5453 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005454 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005455 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005456 struct io_poll_iocb *poll_one = poll;
5457
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005458 /* double add on the same waitqueue head, ignore */
5459 if (poll_one->head == head)
5460 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005461 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005462 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005463 if ((*poll_ptr)->head == head)
5464 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005465 pt->error = -EINVAL;
5466 return;
5467 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005468 /*
5469 * Can't handle multishot for double wait for now, turn it
5470 * into one-shot mode.
5471 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005472 if (!(poll_one->events & EPOLLONESHOT))
5473 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005474 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5475 if (!poll) {
5476 pt->error = -ENOMEM;
5477 return;
5478 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005479 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005480 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005481 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005482 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005483 }
5484
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005485 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005486 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005487
5488 if (poll->events & EPOLLEXCLUSIVE)
5489 add_wait_queue_exclusive(head, &poll->wait);
5490 else
5491 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005492}
5493
5494static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5495 struct poll_table_struct *p)
5496{
5497 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005498 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005499
Jens Axboe807abcb2020-07-17 17:09:27 -06005500 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005501}
5502
Pavel Begunkovf237c302021-08-18 12:42:46 +01005503static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005504{
Jens Axboed7718a92020-02-14 22:23:12 -07005505 struct async_poll *apoll = req->apoll;
5506 struct io_ring_ctx *ctx = req->ctx;
5507
Olivier Langlois236daeae2021-05-31 02:36:37 -04005508 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005509
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005510 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005511 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005512 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005513 }
5514
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005515 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005516 io_poll_remove_double(req);
Hao Xubd99c712021-09-22 18:12:36 +08005517 apoll->poll.done = true;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005518 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005519
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005520 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005521 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005522 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005523 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005524}
5525
5526static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5527 void *key)
5528{
5529 struct io_kiocb *req = wait->private;
5530 struct io_poll_iocb *poll = &req->apoll->poll;
5531
5532 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5533 key_to_poll(key));
5534
5535 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5536}
5537
5538static void io_poll_req_insert(struct io_kiocb *req)
5539{
5540 struct io_ring_ctx *ctx = req->ctx;
5541 struct hlist_head *list;
5542
5543 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5544 hlist_add_head(&req->hash_node, list);
5545}
5546
5547static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5548 struct io_poll_iocb *poll,
5549 struct io_poll_table *ipt, __poll_t mask,
5550 wait_queue_func_t wake_func)
5551 __acquires(&ctx->completion_lock)
5552{
5553 struct io_ring_ctx *ctx = req->ctx;
5554 bool cancel = false;
5555
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005556 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005557 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005558 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005559 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005560
5561 ipt->pt._key = mask;
5562 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005563 ipt->error = 0;
5564 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005565
Jens Axboed7718a92020-02-14 22:23:12 -07005566 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005567 if (unlikely(!ipt->nr_entries) && !ipt->error)
5568 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005569
Jens Axboe79ebeae2021-08-10 15:18:27 -06005570 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005571 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005572 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005573 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005574 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005575 if (unlikely(list_empty(&poll->wait.entry))) {
5576 if (ipt->error)
5577 cancel = true;
5578 ipt->error = 0;
5579 mask = 0;
5580 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005581 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005582 list_del_init(&poll->wait.entry);
5583 else if (cancel)
5584 WRITE_ONCE(poll->canceled, true);
5585 else if (!poll->done) /* actually waiting for an event */
5586 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005587 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005588 }
5589
5590 return mask;
5591}
5592
Olivier Langlois59b735a2021-06-22 05:17:39 -07005593enum {
5594 IO_APOLL_OK,
5595 IO_APOLL_ABORTED,
5596 IO_APOLL_READY
5597};
5598
5599static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005600{
5601 const struct io_op_def *def = &io_op_defs[req->opcode];
5602 struct io_ring_ctx *ctx = req->ctx;
5603 struct async_poll *apoll;
5604 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005605 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005606 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005607
5608 if (!req->file || !file_can_poll(req->file))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005609 return IO_APOLL_ABORTED;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005610 if (req->flags & REQ_F_POLLED)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005611 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005612 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005613 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005614
5615 if (def->pollin) {
5616 rw = READ;
5617 mask |= POLLIN | POLLRDNORM;
5618
5619 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5620 if ((req->opcode == IORING_OP_RECVMSG) &&
5621 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5622 mask &= ~POLLIN;
5623 } else {
5624 rw = WRITE;
5625 mask |= POLLOUT | POLLWRNORM;
5626 }
5627
Jens Axboe9dab14b2020-08-25 12:27:50 -06005628 /* if we can't nonblock try, then no point in arming a poll handler */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01005629 if (!io_file_supports_nowait(req, rw))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005630 return IO_APOLL_ABORTED;
Jens Axboed7718a92020-02-14 22:23:12 -07005631
5632 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5633 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005634 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005635 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005636 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005637 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005638 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005639 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005640
5641 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5642 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005643 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005644 if (ret || ipt.error)
5645 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5646
Olivier Langlois236daeae2021-05-31 02:36:37 -04005647 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5648 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005649 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005650}
5651
5652static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005653 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005654 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005655{
Jens Axboeb41e9852020-02-17 09:52:41 -07005656 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005657
Jens Axboe50826202021-02-23 09:02:26 -07005658 if (!poll->head)
5659 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005660 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005661 if (do_cancel)
5662 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005663 if (!list_empty(&poll->wait.entry)) {
5664 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005665 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005666 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005667 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005668 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005669 return do_complete;
5670}
5671
Pavel Begunkov5d709042021-08-09 20:18:13 +01005672static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005673 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005674{
5675 bool do_complete;
5676
Jens Axboed4e7cd32020-08-15 11:44:50 -07005677 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005678 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005679
Jens Axboeb41e9852020-02-17 09:52:41 -07005680 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005681 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005682 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005683 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005684 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005685 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005686 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005687}
5688
Jens Axboe76e1b642020-09-26 15:05:03 -06005689/*
5690 * Returns true if we found and killed one or more poll requests
5691 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005692static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005693 bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005694{
Jens Axboe78076bb2019-12-04 19:56:40 -07005695 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005696 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005697 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005698
Jens Axboe79ebeae2021-08-10 15:18:27 -06005699 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005700 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5701 struct hlist_head *list;
5702
5703 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005704 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005705 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005706 posted += io_poll_remove_one(req);
5707 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005708 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005709 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005710
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005711 if (posted)
5712 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005713
5714 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005715}
5716
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005717static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5718 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005719 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005720{
Jens Axboe78076bb2019-12-04 19:56:40 -07005721 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005722 struct io_kiocb *req;
5723
Jens Axboe78076bb2019-12-04 19:56:40 -07005724 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5725 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005726 if (sqe_addr != req->user_data)
5727 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005728 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5729 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005730 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005731 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005732 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005733}
5734
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005735static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5736 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005737 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005738{
5739 struct io_kiocb *req;
5740
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005741 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005742 if (!req)
5743 return -ENOENT;
5744 if (io_poll_remove_one(req))
5745 return 0;
5746
5747 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005748}
5749
Pavel Begunkov9096af32021-04-14 13:38:36 +01005750static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5751 unsigned int flags)
5752{
5753 u32 events;
5754
5755 events = READ_ONCE(sqe->poll32_events);
5756#ifdef __BIG_ENDIAN
5757 events = swahw32(events);
5758#endif
5759 if (!(flags & IORING_POLL_ADD_MULTI))
5760 events |= EPOLLONESHOT;
5761 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5762}
5763
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005764static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005765 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005766{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005767 struct io_poll_update *upd = &req->poll_update;
5768 u32 flags;
5769
Jens Axboe221c5eb2019-01-17 09:41:58 -07005770 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5771 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005772 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005773 return -EINVAL;
5774 flags = READ_ONCE(sqe->len);
5775 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5776 IORING_POLL_ADD_MULTI))
5777 return -EINVAL;
5778 /* meaningless without update */
5779 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005780 return -EINVAL;
5781
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005782 upd->old_user_data = READ_ONCE(sqe->addr);
5783 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5784 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005785
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005786 upd->new_user_data = READ_ONCE(sqe->off);
5787 if (!upd->update_user_data && upd->new_user_data)
5788 return -EINVAL;
5789 if (upd->update_events)
5790 upd->events = io_poll_parse_events(sqe, flags);
5791 else if (sqe->poll32_events)
5792 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005793
Jens Axboe221c5eb2019-01-17 09:41:58 -07005794 return 0;
5795}
5796
Jens Axboe221c5eb2019-01-17 09:41:58 -07005797static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5798 void *key)
5799{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005800 struct io_kiocb *req = wait->private;
5801 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005802
Jens Axboed7718a92020-02-14 22:23:12 -07005803 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005804}
5805
Jens Axboe221c5eb2019-01-17 09:41:58 -07005806static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5807 struct poll_table_struct *p)
5808{
5809 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5810
Jens Axboee8c2bc12020-08-15 18:44:09 -07005811 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005812}
5813
Jens Axboe3529d8c2019-12-19 18:24:38 -07005814static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005815{
5816 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005817 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005818
5819 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5820 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005821 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005822 return -EINVAL;
5823 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005824 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005825 return -EINVAL;
5826
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005827 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005828 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005829 return 0;
5830}
5831
Pavel Begunkov61e98202021-02-10 00:03:08 +00005832static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005833{
5834 struct io_poll_iocb *poll = &req->poll;
5835 struct io_ring_ctx *ctx = req->ctx;
5836 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005837 __poll_t mask;
Hao Xu5b7aa382021-09-22 18:12:38 +08005838 bool done;
Jens Axboe0969e782019-12-17 18:40:57 -07005839
Jens Axboed7718a92020-02-14 22:23:12 -07005840 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005841
Jens Axboed7718a92020-02-14 22:23:12 -07005842 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5843 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005844
Jens Axboe8c838782019-03-12 15:48:16 -06005845 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005846 ipt.error = 0;
Hao Xu5b7aa382021-09-22 18:12:38 +08005847 done = io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06005848 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005849 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005850
Jens Axboe8c838782019-03-12 15:48:16 -06005851 if (mask) {
5852 io_cqring_ev_posted(ctx);
Hao Xu5b7aa382021-09-22 18:12:38 +08005853 if (done)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005854 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005855 }
Jens Axboe8c838782019-03-12 15:48:16 -06005856 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005857}
5858
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005859static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005860{
5861 struct io_ring_ctx *ctx = req->ctx;
5862 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005863 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005864 int ret;
5865
Jens Axboe79ebeae2021-08-10 15:18:27 -06005866 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005867 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005868 if (!preq) {
5869 ret = -ENOENT;
5870 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005871 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005872
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005873 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5874 completing = true;
5875 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5876 goto err;
5877 }
5878
Jens Axboecb3b200e2021-04-06 09:49:31 -06005879 /*
5880 * Don't allow racy completion with singleshot, as we cannot safely
5881 * update those. For multishot, if we're racing with completion, just
5882 * let completion re-add it.
5883 */
5884 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5885 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5886 ret = -EALREADY;
5887 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005888 }
5889 /* we now have a detached poll request. reissue. */
5890 ret = 0;
5891err:
Jens Axboeb69de282021-03-17 08:37:41 -06005892 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005893 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005894 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005895 io_req_complete(req, ret);
5896 return 0;
5897 }
5898 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005899 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005900 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005901 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005902 preq->poll.events |= IO_POLL_UNMASK;
5903 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005904 if (req->poll_update.update_user_data)
5905 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005906 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005907
Jens Axboeb69de282021-03-17 08:37:41 -06005908 /* complete update request, we're done with it */
5909 io_req_complete(req, ret);
5910
Jens Axboecb3b200e2021-04-06 09:49:31 -06005911 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005912 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005913 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005914 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005915 io_req_complete(preq, ret);
5916 }
Jens Axboeb69de282021-03-17 08:37:41 -06005917 }
5918 return 0;
5919}
5920
Pavel Begunkovf237c302021-08-18 12:42:46 +01005921static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005922{
Jens Axboe89850fc2021-08-10 15:11:51 -06005923 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005924 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005925}
5926
Jens Axboe5262f562019-09-17 12:26:57 -06005927static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5928{
Jens Axboead8a48a2019-11-15 08:49:11 -07005929 struct io_timeout_data *data = container_of(timer,
5930 struct io_timeout_data, timer);
5931 struct io_kiocb *req = data->req;
5932 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005933 unsigned long flags;
5934
Jens Axboe89850fc2021-08-10 15:11:51 -06005935 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005936 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005937 atomic_set(&req->ctx->cq_timeouts,
5938 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005939 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005940
Jens Axboe89850fc2021-08-10 15:11:51 -06005941 req->io_task_work.func = io_req_task_timeout;
5942 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005943 return HRTIMER_NORESTART;
5944}
5945
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005946static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5947 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005948 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005949{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005950 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005951 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005952 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005953
5954 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005955 found = user_data == req->user_data;
5956 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005957 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005958 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005959 if (!found)
5960 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005961
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005962 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005963 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005964 return ERR_PTR(-EALREADY);
5965 list_del_init(&req->timeout.list);
5966 return req;
5967}
5968
5969static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005970 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005971 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005972{
5973 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5974
5975 if (IS_ERR(req))
5976 return PTR_ERR(req);
5977
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005978 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005979 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005980 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005981 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005982}
5983
Jens Axboe50c1df22021-08-27 17:11:06 -06005984static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
5985{
5986 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
5987 case IORING_TIMEOUT_BOOTTIME:
5988 return CLOCK_BOOTTIME;
5989 case IORING_TIMEOUT_REALTIME:
5990 return CLOCK_REALTIME;
5991 default:
5992 /* can't happen, vetted at prep time */
5993 WARN_ON_ONCE(1);
5994 fallthrough;
5995 case 0:
5996 return CLOCK_MONOTONIC;
5997 }
5998}
5999
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006000static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6001 struct timespec64 *ts, enum hrtimer_mode mode)
6002 __must_hold(&ctx->timeout_lock)
6003{
6004 struct io_timeout_data *io;
6005 struct io_kiocb *req;
6006 bool found = false;
6007
6008 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
6009 found = user_data == req->user_data;
6010 if (found)
6011 break;
6012 }
6013 if (!found)
6014 return -ENOENT;
6015
6016 io = req->async_data;
6017 if (hrtimer_try_to_cancel(&io->timer) == -1)
6018 return -EALREADY;
6019 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
6020 io->timer.function = io_link_timeout_fn;
6021 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
6022 return 0;
6023}
6024
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006025static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6026 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06006027 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006028{
6029 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6030 struct io_timeout_data *data;
6031
6032 if (IS_ERR(req))
6033 return PTR_ERR(req);
6034
6035 req->timeout.off = 0; /* noseq */
6036 data = req->async_data;
6037 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06006038 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006039 data->timer.function = io_timeout_fn;
6040 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
6041 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07006042}
6043
Jens Axboe3529d8c2019-12-19 18:24:38 -07006044static int io_timeout_remove_prep(struct io_kiocb *req,
6045 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07006046{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006047 struct io_timeout_rem *tr = &req->timeout_rem;
6048
Jens Axboeb29472e2019-12-17 18:50:29 -07006049 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6050 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006051 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6052 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006053 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07006054 return -EINVAL;
6055
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006056 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006057 tr->addr = READ_ONCE(sqe->addr);
6058 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006059 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6060 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6061 return -EINVAL;
6062 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6063 tr->ltimeout = true;
6064 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006065 return -EINVAL;
6066 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6067 return -EFAULT;
6068 } else if (tr->flags) {
6069 /* timeout removal doesn't support flags */
6070 return -EINVAL;
6071 }
6072
Jens Axboeb29472e2019-12-17 18:50:29 -07006073 return 0;
6074}
6075
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006076static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6077{
6078 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6079 : HRTIMER_MODE_REL;
6080}
6081
Jens Axboe11365042019-10-16 09:08:32 -06006082/*
6083 * Remove or update an existing timeout command
6084 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006085static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006086{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006087 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006088 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006089 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006090
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006091 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6092 spin_lock(&ctx->completion_lock);
6093 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006094 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006095 spin_unlock_irq(&ctx->timeout_lock);
6096 spin_unlock(&ctx->completion_lock);
6097 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006098 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6099
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006100 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006101 if (tr->ltimeout)
6102 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6103 else
6104 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006105 spin_unlock_irq(&ctx->timeout_lock);
6106 }
Jens Axboe11365042019-10-16 09:08:32 -06006107
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006108 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006109 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006110 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006111 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006112}
6113
Jens Axboe3529d8c2019-12-19 18:24:38 -07006114static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006115 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006116{
Jens Axboead8a48a2019-11-15 08:49:11 -07006117 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006118 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006119 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006120
Jens Axboead8a48a2019-11-15 08:49:11 -07006121 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006122 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006123 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6124 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006125 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006126 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006127 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006128 flags = READ_ONCE(sqe->timeout_flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006129 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK))
6130 return -EINVAL;
6131 /* more than one clock specified is invalid, obviously */
6132 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006133 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006134
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006135 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006136 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006137 if (unlikely(off && !req->ctx->off_timeout_used))
6138 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006139
Jens Axboee8c2bc12020-08-15 18:44:09 -07006140 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006141 return -ENOMEM;
6142
Jens Axboee8c2bc12020-08-15 18:44:09 -07006143 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006144 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006145 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006146
6147 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006148 return -EFAULT;
6149
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006150 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006151 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006152
6153 if (is_timeout_link) {
6154 struct io_submit_link *link = &req->ctx->submit_state.link;
6155
6156 if (!link->head)
6157 return -EINVAL;
6158 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6159 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006160 req->timeout.head = link->last;
6161 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006162 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006163 return 0;
6164}
6165
Pavel Begunkov61e98202021-02-10 00:03:08 +00006166static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006167{
Jens Axboead8a48a2019-11-15 08:49:11 -07006168 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006169 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006170 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006171 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006172
Jens Axboe89850fc2021-08-10 15:11:51 -06006173 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006174
Jens Axboe5262f562019-09-17 12:26:57 -06006175 /*
6176 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006177 * timeout event to be satisfied. If it isn't set, then this is
6178 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006179 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006180 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006181 entry = ctx->timeout_list.prev;
6182 goto add;
6183 }
Jens Axboe5262f562019-09-17 12:26:57 -06006184
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006185 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6186 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006187
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006188 /* Update the last seq here in case io_flush_timeouts() hasn't.
6189 * This is safe because ->completion_lock is held, and submissions
6190 * and completions are never mixed in the same ->completion_lock section.
6191 */
6192 ctx->cq_last_tm_flush = tail;
6193
Jens Axboe5262f562019-09-17 12:26:57 -06006194 /*
6195 * Insertion sort, ensuring the first entry in the list is always
6196 * the one we need first.
6197 */
Jens Axboe5262f562019-09-17 12:26:57 -06006198 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006199 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6200 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006201
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006202 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006203 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006204 /* nxt.seq is behind @tail, otherwise would've been completed */
6205 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006206 break;
6207 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006208add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006209 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006210 data->timer.function = io_timeout_fn;
6211 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006212 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006213 return 0;
6214}
6215
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006216struct io_cancel_data {
6217 struct io_ring_ctx *ctx;
6218 u64 user_data;
6219};
6220
Jens Axboe62755e32019-10-28 21:49:21 -06006221static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006222{
Jens Axboe62755e32019-10-28 21:49:21 -06006223 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006224 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006225
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006226 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006227}
6228
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006229static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6230 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006231{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006232 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006233 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006234 int ret = 0;
6235
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006236 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006237 return -ENOENT;
6238
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006239 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006240 switch (cancel_ret) {
6241 case IO_WQ_CANCEL_OK:
6242 ret = 0;
6243 break;
6244 case IO_WQ_CANCEL_RUNNING:
6245 ret = -EALREADY;
6246 break;
6247 case IO_WQ_CANCEL_NOTFOUND:
6248 ret = -ENOENT;
6249 break;
6250 }
6251
Jens Axboee977d6d2019-11-05 12:39:45 -07006252 return ret;
6253}
6254
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006255static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006256{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006257 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006258 int ret;
6259
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006260 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006261
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006262 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01006263 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006264 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006265
6266 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006267 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006268 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006269 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006270 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01006271 goto out;
6272 ret = io_poll_cancel(ctx, sqe_addr, false);
6273out:
6274 spin_unlock(&ctx->completion_lock);
6275 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006276}
6277
Jens Axboe3529d8c2019-12-19 18:24:38 -07006278static int io_async_cancel_prep(struct io_kiocb *req,
6279 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006280{
Jens Axboefbf23842019-12-17 18:45:56 -07006281 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006282 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006283 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6284 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006285 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6286 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006287 return -EINVAL;
6288
Jens Axboefbf23842019-12-17 18:45:56 -07006289 req->cancel.addr = READ_ONCE(sqe->addr);
6290 return 0;
6291}
6292
Pavel Begunkov61e98202021-02-10 00:03:08 +00006293static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006294{
6295 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006296 u64 sqe_addr = req->cancel.addr;
6297 struct io_tctx_node *node;
6298 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006299
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006300 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006301 if (ret != -ENOENT)
6302 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006303
6304 /* slow path, try all io-wq's */
6305 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
6306 ret = -ENOENT;
6307 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6308 struct io_uring_task *tctx = node->task->io_uring;
6309
Pavel Begunkov58f99372021-03-12 16:25:55 +00006310 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6311 if (ret != -ENOENT)
6312 break;
6313 }
6314 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkov58f99372021-03-12 16:25:55 +00006315done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006316 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006317 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006318 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006319 return 0;
6320}
6321
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006322static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006323 const struct io_uring_sqe *sqe)
6324{
Daniele Albano61710e42020-07-18 14:15:16 -06006325 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6326 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006327 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006328 return -EINVAL;
6329
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006330 req->rsrc_update.offset = READ_ONCE(sqe->off);
6331 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6332 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006333 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006334 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006335 return 0;
6336}
6337
Pavel Begunkov889fca72021-02-10 00:03:09 +00006338static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006339{
6340 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006341 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006342 int ret;
6343
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006344 up.offset = req->rsrc_update.offset;
6345 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006346 up.nr = 0;
6347 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006348 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006349
Jens Axboecdb31c22021-09-24 08:43:54 -06006350 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006351 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006352 &up, req->rsrc_update.nr_args);
Jens Axboecdb31c22021-09-24 08:43:54 -06006353 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Jens Axboe05f3fb32019-12-09 11:22:50 -07006354
6355 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006356 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006357 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006358 return 0;
6359}
6360
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006361static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006362{
Jens Axboed625c6e2019-12-17 19:53:05 -07006363 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006364 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006365 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006366 case IORING_OP_READV:
6367 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006368 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006369 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006370 case IORING_OP_WRITEV:
6371 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006372 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006373 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006374 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006375 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006376 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006377 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006378 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006379 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006380 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006381 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006382 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006383 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006384 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006385 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006386 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006387 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006388 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006389 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006390 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006391 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006392 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006393 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006394 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006395 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006396 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006397 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006398 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006399 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006400 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006401 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006402 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006403 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006404 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006405 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006406 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006407 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006408 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006409 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006410 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006411 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006412 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006413 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006414 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006415 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006416 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006417 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006418 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006419 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006420 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006421 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006422 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006423 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006424 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006425 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006426 case IORING_OP_SHUTDOWN:
6427 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006428 case IORING_OP_RENAMEAT:
6429 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006430 case IORING_OP_UNLINKAT:
6431 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006432 case IORING_OP_MKDIRAT:
6433 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006434 case IORING_OP_SYMLINKAT:
6435 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006436 case IORING_OP_LINKAT:
6437 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006438 }
6439
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006440 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6441 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006442 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006443}
6444
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006445static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006446{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006447 if (!io_op_defs[req->opcode].needs_async_setup)
6448 return 0;
6449 if (WARN_ON_ONCE(req->async_data))
6450 return -EFAULT;
6451 if (io_alloc_async_data(req))
6452 return -EAGAIN;
6453
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006454 switch (req->opcode) {
6455 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006456 return io_rw_prep_async(req, READ);
6457 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006458 return io_rw_prep_async(req, WRITE);
6459 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006460 return io_sendmsg_prep_async(req);
6461 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006462 return io_recvmsg_prep_async(req);
6463 case IORING_OP_CONNECT:
6464 return io_connect_prep_async(req);
6465 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006466 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6467 req->opcode);
6468 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006469}
6470
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006471static u32 io_get_sequence(struct io_kiocb *req)
6472{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006473 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006474
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006475 /* need original cached_sq_head, but it was increased for each req */
6476 io_for_each_link(req, req)
6477 seq--;
6478 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006479}
6480
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006481static bool io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006482{
Pavel Begunkov3c199662021-06-15 16:47:57 +01006483 struct io_kiocb *pos;
Jens Axboedef596e2019-01-09 08:59:42 -07006484 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006485 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006486 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006487 u32 seq;
Jens Axboedef596e2019-01-09 08:59:42 -07006488
Pavel Begunkovb8ce1b92021-08-31 14:13:11 +01006489 if (req->flags & REQ_F_FAIL) {
6490 io_req_complete_fail_submit(req);
6491 return true;
6492 }
6493
Pavel Begunkov3c199662021-06-15 16:47:57 +01006494 /*
6495 * If we need to drain a request in the middle of a link, drain the
6496 * head request and the next request/link after the current link.
6497 * Considering sequential execution of links, IOSQE_IO_DRAIN will be
6498 * maintained for every request of our link.
6499 */
6500 if (ctx->drain_next) {
6501 req->flags |= REQ_F_IO_DRAIN;
6502 ctx->drain_next = false;
6503 }
6504 /* not interested in head, start from the first linked */
6505 io_for_each_link(pos, req->link) {
6506 if (pos->flags & REQ_F_IO_DRAIN) {
6507 ctx->drain_next = true;
6508 req->flags |= REQ_F_IO_DRAIN;
6509 break;
6510 }
6511 }
6512
Jens Axboedef596e2019-01-09 08:59:42 -07006513 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006514 if (likely(list_empty_careful(&ctx->defer_list) &&
Pavel Begunkov10c66902021-06-15 16:47:56 +01006515 !(req->flags & REQ_F_IO_DRAIN))) {
6516 ctx->drain_active = false;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006517 return false;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006518 }
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006519
6520 seq = io_get_sequence(req);
6521 /* Still a chance to pass the sequence check */
6522 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006523 return false;
Jens Axboedef596e2019-01-09 08:59:42 -07006524
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006525 ret = io_req_prep_async(req);
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006526 if (ret)
Pavel Begunkov1b487732021-07-11 22:41:13 +01006527 goto fail;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006528 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006529 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006530 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006531 ret = -ENOMEM;
6532fail:
6533 io_req_complete_failed(req, ret);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006534 return true;
6535 }
Jens Axboe31b51512019-01-18 22:56:34 -07006536
Jens Axboe79ebeae2021-08-10 15:18:27 -06006537 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006538 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006539 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006540 kfree(de);
Pavel Begunkovf237c302021-08-18 12:42:46 +01006541 io_queue_async_work(req, NULL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006542 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006543 }
6544
6545 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006546 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006547 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006548 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006549 spin_unlock(&ctx->completion_lock);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006550 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006551}
6552
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006553static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006554{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006555 if (req->flags & REQ_F_BUFFER_SELECTED) {
6556 switch (req->opcode) {
6557 case IORING_OP_READV:
6558 case IORING_OP_READ_FIXED:
6559 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006560 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006561 break;
6562 case IORING_OP_RECVMSG:
6563 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006564 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006565 break;
6566 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006567 }
6568
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006569 if (req->flags & REQ_F_NEED_CLEANUP) {
6570 switch (req->opcode) {
6571 case IORING_OP_READV:
6572 case IORING_OP_READ_FIXED:
6573 case IORING_OP_READ:
6574 case IORING_OP_WRITEV:
6575 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006576 case IORING_OP_WRITE: {
6577 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006578
6579 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006580 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006581 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006582 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006583 case IORING_OP_SENDMSG: {
6584 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006585
6586 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006587 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006588 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006589 case IORING_OP_SPLICE:
6590 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006591 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6592 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006593 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006594 case IORING_OP_OPENAT:
6595 case IORING_OP_OPENAT2:
6596 if (req->open.filename)
6597 putname(req->open.filename);
6598 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006599 case IORING_OP_RENAMEAT:
6600 putname(req->rename.oldpath);
6601 putname(req->rename.newpath);
6602 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006603 case IORING_OP_UNLINKAT:
6604 putname(req->unlink.filename);
6605 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006606 case IORING_OP_MKDIRAT:
6607 putname(req->mkdir.filename);
6608 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006609 case IORING_OP_SYMLINKAT:
6610 putname(req->symlink.oldpath);
6611 putname(req->symlink.newpath);
6612 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006613 case IORING_OP_LINKAT:
6614 putname(req->hardlink.oldpath);
6615 putname(req->hardlink.newpath);
6616 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006617 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006618 }
Jens Axboe75652a302021-04-15 09:52:40 -06006619 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6620 kfree(req->apoll->double_poll);
6621 kfree(req->apoll);
6622 req->apoll = NULL;
6623 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006624 if (req->flags & REQ_F_INFLIGHT) {
6625 struct io_uring_task *tctx = req->task->io_uring;
6626
6627 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006628 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006629 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006630 put_cred(req->creds);
Pavel Begunkovc8543572021-06-17 18:14:04 +01006631
6632 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006633}
6634
Pavel Begunkov889fca72021-02-10 00:03:09 +00006635static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006636{
Jens Axboeedafcce2019-01-09 09:16:05 -07006637 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006638 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006639 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006640
Pavel Begunkov6878b402021-09-24 21:59:41 +01006641 if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006642 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006643
Jens Axboed625c6e2019-12-17 19:53:05 -07006644 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006645 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006646 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006647 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006648 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006649 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006650 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006651 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006652 break;
6653 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006654 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006655 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006656 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006657 break;
6658 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006659 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006660 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006661 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006662 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006663 break;
6664 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006665 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006666 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006667 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006668 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006669 break;
6670 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006671 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006672 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006673 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006674 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006675 break;
6676 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006677 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006678 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006679 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006680 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006681 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006682 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006683 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006684 break;
6685 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006686 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006687 break;
6688 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006689 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006690 break;
6691 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006692 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006693 break;
6694 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006695 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006696 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006697 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006698 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006699 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006700 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006701 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006702 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006703 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006704 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006705 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006706 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006707 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006708 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006709 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006710 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006711 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006712 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006713 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006714 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006715 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006716 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006717 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006718 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006719 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006720 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006721 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006722 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006723 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006724 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006725 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006726 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006727 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006728 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006729 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006730 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006731 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006732 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006733 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006734 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006735 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006736 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006737 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006738 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006739 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006740 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006741 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006742 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006743 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006744 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006745 case IORING_OP_MKDIRAT:
6746 ret = io_mkdirat(req, issue_flags);
6747 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006748 case IORING_OP_SYMLINKAT:
6749 ret = io_symlinkat(req, issue_flags);
6750 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006751 case IORING_OP_LINKAT:
6752 ret = io_linkat(req, issue_flags);
6753 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006754 default:
6755 ret = -EINVAL;
6756 break;
6757 }
Jens Axboe31b51512019-01-18 22:56:34 -07006758
Jens Axboe5730b272021-02-27 15:57:30 -07006759 if (creds)
6760 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006761 if (ret)
6762 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006763 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01006764 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6765 io_iopoll_req_issued(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006766
6767 return 0;
6768}
6769
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006770static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6771{
6772 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6773
6774 req = io_put_req_find_next(req);
6775 return req ? &req->work : NULL;
6776}
6777
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006778static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006779{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006780 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006781 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006782 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006783
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006784 /* one will be dropped by ->io_free_work() after returning to io-wq */
6785 if (!(req->flags & REQ_F_REFCOUNT))
6786 __io_req_set_refcount(req, 2);
6787 else
6788 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006789
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006790 timeout = io_prep_linked_timeout(req);
6791 if (timeout)
6792 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006793
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006794 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Jens Axboe4014d942021-01-19 15:53:54 -07006795 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006796 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006797
Jens Axboe561fb042019-10-24 07:25:42 -06006798 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006799 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006800 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006801 /*
6802 * We can get EAGAIN for polled IO even though we're
6803 * forcing a sync submission from here, since we can't
6804 * wait for request slots on the block side.
6805 */
6806 if (ret != -EAGAIN)
6807 break;
6808 cond_resched();
6809 } while (1);
6810 }
Jens Axboe31b51512019-01-18 22:56:34 -07006811
Pavel Begunkova3df76982021-02-18 22:32:52 +00006812 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006813 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006814 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006815}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006816
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006817static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006818 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006819{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006820 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006821}
6822
Jens Axboe09bb8392019-03-13 12:39:28 -06006823static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6824 int index)
6825{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006826 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006827
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006828 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006829}
6830
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006831static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006832{
6833 unsigned long file_ptr = (unsigned long) file;
6834
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006835 if (__io_file_supports_nowait(file, READ))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006836 file_ptr |= FFS_ASYNC_READ;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006837 if (__io_file_supports_nowait(file, WRITE))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006838 file_ptr |= FFS_ASYNC_WRITE;
6839 if (S_ISREG(file_inode(file)->i_mode))
6840 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006841 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006842}
6843
Pavel Begunkovac177052021-08-09 13:04:02 +01006844static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6845 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006846{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006847 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006848 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006849
Pavel Begunkovac177052021-08-09 13:04:02 +01006850 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6851 return NULL;
6852 fd = array_index_nospec(fd, ctx->nr_user_files);
6853 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6854 file = (struct file *) (file_ptr & FFS_MASK);
6855 file_ptr &= ~FFS_MASK;
6856 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006857 req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT);
Pavel Begunkovac177052021-08-09 13:04:02 +01006858 io_req_set_rsrc_node(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006859 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006860}
6861
Pavel Begunkovac177052021-08-09 13:04:02 +01006862static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006863 struct io_kiocb *req, int fd)
6864{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006865 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006866
6867 trace_io_uring_file_get(ctx, fd);
6868
6869 /* we don't allow fixed io_uring files */
6870 if (file && unlikely(file->f_op == &io_uring_fops))
6871 io_req_track_inflight(req);
6872 return file;
6873}
6874
6875static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006876 struct io_kiocb *req, int fd, bool fixed)
6877{
6878 if (fixed)
6879 return io_file_get_fixed(ctx, req, fd);
6880 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006881 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006882}
6883
Pavel Begunkovf237c302021-08-18 12:42:46 +01006884static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006885{
6886 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006887 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006888
6889 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006890 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006891 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006892 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006893 } else {
6894 io_req_complete_post(req, -ETIME, 0);
6895 }
6896}
6897
Jens Axboe2665abf2019-11-05 12:40:47 -07006898static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6899{
Jens Axboead8a48a2019-11-15 08:49:11 -07006900 struct io_timeout_data *data = container_of(timer,
6901 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006902 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006903 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006904 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006905
Jens Axboe89b263f2021-08-10 15:14:18 -06006906 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006907 prev = req->timeout.head;
6908 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006909
6910 /*
6911 * We don't expect the list to be empty, that will only happen if we
6912 * race with the completion of the linked work.
6913 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006914 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006915 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006916 if (!req_ref_inc_not_zero(prev))
6917 prev = NULL;
6918 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006919 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06006920 req->timeout.prev = prev;
6921 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006922
Jens Axboe89b263f2021-08-10 15:14:18 -06006923 req->io_task_work.func = io_req_task_link_timeout;
6924 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006925 return HRTIMER_NORESTART;
6926}
6927
Pavel Begunkovde968c12021-03-19 17:22:33 +00006928static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006929{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006930 struct io_ring_ctx *ctx = req->ctx;
6931
Jens Axboe89b263f2021-08-10 15:14:18 -06006932 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006933 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006934 * If the back reference is NULL, then our linked request finished
6935 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006936 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006937 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006938 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006939
Jens Axboead8a48a2019-11-15 08:49:11 -07006940 data->timer.function = io_link_timeout_fn;
6941 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6942 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006943 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07006944 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006945 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006946 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006947 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006948}
6949
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006950static void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006951 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006952{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006953 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006954 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006955
Olivier Langlois59b735a2021-06-22 05:17:39 -07006956issue_sqe:
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006957 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006958
6959 /*
6960 * We async punt it if the file wasn't marked NOWAIT, or if the file
6961 * doesn't support non-blocking read/write attempts
6962 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006963 if (likely(!ret)) {
Pavel Begunkove342c802021-01-19 13:32:47 +00006964 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006965 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006966 struct io_submit_state *state = &ctx->submit_state;
Jens Axboee65ef562019-03-12 10:16:44 -06006967
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006968 state->compl_reqs[state->compl_nr++] = req;
6969 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006970 io_submit_flush_completions(ctx);
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006971 return;
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006972 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006973
6974 linked_timeout = io_prep_linked_timeout(req);
6975 if (linked_timeout)
6976 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006977 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006978 linked_timeout = io_prep_linked_timeout(req);
6979
Olivier Langlois59b735a2021-06-22 05:17:39 -07006980 switch (io_arm_poll_handler(req)) {
6981 case IO_APOLL_READY:
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006982 if (linked_timeout)
6983 io_unprep_linked_timeout(req);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006984 goto issue_sqe;
6985 case IO_APOLL_ABORTED:
Pavel Begunkov18400382021-03-19 17:22:34 +00006986 /*
6987 * Queued up for async execution, worker will release
6988 * submit reference when the iocb is actually submitted.
6989 */
Pavel Begunkovf237c302021-08-18 12:42:46 +01006990 io_queue_async_work(req, NULL);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006991 break;
Pavel Begunkov18400382021-03-19 17:22:34 +00006992 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006993
6994 if (linked_timeout)
6995 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006996 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006997 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006998 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006999}
7000
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007001static inline void io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007002 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08007003{
Pavel Begunkov10c66902021-06-15 16:47:56 +01007004 if (unlikely(req->ctx->drain_active) && io_drain_req(req))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01007005 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08007006
Hao Xua8295b92021-08-27 17:46:09 +08007007 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00007008 __io_queue_sqe(req);
Hao Xua8295b92021-08-27 17:46:09 +08007009 } else if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01007010 io_req_complete_fail_submit(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01007011 } else {
7012 int ret = io_req_prep_async(req);
7013
7014 if (unlikely(ret))
7015 io_req_complete_failed(req, ret);
7016 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01007017 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07007018 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007019}
7020
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007021/*
7022 * Check SQE restrictions (opcode and flags).
7023 *
7024 * Returns 'true' if SQE is allowed, 'false' otherwise.
7025 */
7026static inline bool io_check_restriction(struct io_ring_ctx *ctx,
7027 struct io_kiocb *req,
7028 unsigned int sqe_flags)
7029{
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01007030 if (likely(!ctx->restricted))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007031 return true;
7032
7033 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
7034 return false;
7035
7036 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
7037 ctx->restrictions.sqe_flags_required)
7038 return false;
7039
7040 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
7041 ctx->restrictions.sqe_flags_required))
7042 return false;
7043
7044 return true;
7045}
7046
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007047static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007048 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007049 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007050{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007051 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007052 unsigned int sqe_flags;
Jens Axboe003e8dc2021-03-06 09:22:27 -07007053 int personality, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007054
Pavel Begunkov864ea922021-08-09 13:04:08 +01007055 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007056 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007057 /* same numerical values with corresponding REQ_F_*, safe to copy */
7058 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007059 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007060 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007061 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03007062 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007063
7064 if (unlikely(req->opcode >= IORING_OP_LAST))
7065 return -EINVAL;
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007066 if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
7067 /* enforce forwards compatibility on users */
7068 if (sqe_flags & ~SQE_VALID_FLAGS)
7069 return -EINVAL;
7070 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
7071 !io_op_defs[req->opcode].buffer_select)
7072 return -EOPNOTSUPP;
7073 if (sqe_flags & IOSQE_IO_DRAIN)
7074 ctx->drain_active = true;
7075 }
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01007076 if (!io_check_restriction(ctx, req, sqe_flags))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007077 return -EACCES;
7078
Jens Axboe003e8dc2021-03-06 09:22:27 -07007079 personality = READ_ONCE(sqe->personality);
7080 if (personality) {
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01007081 req->creds = xa_load(&ctx->personalities, personality);
7082 if (!req->creds)
Jens Axboe003e8dc2021-03-06 09:22:27 -07007083 return -EINVAL;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01007084 get_cred(req->creds);
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01007085 req->flags |= REQ_F_CREDS;
Jens Axboe003e8dc2021-03-06 09:22:27 -07007086 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007087 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007088
Jens Axboe27926b62020-10-28 09:33:23 -06007089 /*
7090 * Plug now if we have more than 1 IO left after this, and the target
7091 * is potentially a read/write to block based storage.
7092 */
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007093 if (state->need_plug && io_op_defs[req->opcode].plug) {
Jens Axboe27926b62020-10-28 09:33:23 -06007094 blk_start_plug(&state->plug);
7095 state->plug_started = true;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007096 state->need_plug = false;
Jens Axboe27926b62020-10-28 09:33:23 -06007097 }
Jens Axboe63ff8222020-05-07 14:56:15 -06007098
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007099 if (io_op_defs[req->opcode].needs_file) {
Pavel Begunkov62906e82021-08-10 14:52:47 +01007100 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007101 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007102 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007103 ret = -EBADF;
7104 }
Pavel Begunkov71b547c2020-10-10 18:34:09 +01007105 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007106}
7107
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007108static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007109 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007110 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007111{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007112 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007113 int ret;
7114
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007115 ret = io_init_req(ctx, req, sqe);
7116 if (unlikely(ret)) {
7117fail_req:
Jens Axboea87acfd2021-09-11 16:04:50 -06007118 trace_io_uring_req_failed(sqe, ret);
7119
Hao Xua8295b92021-08-27 17:46:09 +08007120 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007121 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007122 /*
7123 * we can judge a link req is failed or cancelled by if
7124 * REQ_F_FAIL is set, but the head is an exception since
7125 * it may be set REQ_F_FAIL because of other req's failure
7126 * so let's leverage req->result to distinguish if a head
7127 * is set REQ_F_FAIL because of its failure or other req's
7128 * failure so that we can set the correct ret code for it.
7129 * init result here to avoid affecting the normal path.
7130 */
7131 if (!(link->head->flags & REQ_F_FAIL))
7132 req_fail_link_node(link->head, -ECANCELED);
7133 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7134 /*
7135 * the current req is a normal req, we should return
7136 * error and thus break the submittion loop.
7137 */
7138 io_req_complete_failed(req, ret);
7139 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007140 }
Hao Xua8295b92021-08-27 17:46:09 +08007141 req_fail_link_node(req, ret);
7142 } else {
7143 ret = io_req_prep(req, sqe);
7144 if (unlikely(ret))
7145 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007146 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007147
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007148 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007149 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7150 req->flags, true,
7151 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007152
Jens Axboe6c271ce2019-01-10 11:22:30 -07007153 /*
7154 * If we already have a head request, queue this one for async
7155 * submittal once the head completes. If we don't have a head but
7156 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7157 * submitted sync once the chain is complete. If none of those
7158 * conditions are true (normal request), then just queue it.
7159 */
7160 if (link->head) {
7161 struct io_kiocb *head = link->head;
7162
Hao Xua8295b92021-08-27 17:46:09 +08007163 if (!(req->flags & REQ_F_FAIL)) {
7164 ret = io_req_prep_async(req);
7165 if (unlikely(ret)) {
7166 req_fail_link_node(req, ret);
7167 if (!(head->flags & REQ_F_FAIL))
7168 req_fail_link_node(head, -ECANCELED);
7169 }
7170 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007171 trace_io_uring_link(ctx, req, head);
7172 link->last->link = req;
7173 link->last = req;
7174
7175 /* last request of a link, enqueue the link */
7176 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7177 link->head = NULL;
Pavel Begunkov5e159202021-06-14 23:37:26 +01007178 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007179 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007180 } else {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007181 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08007182 link->head = req;
7183 link->last = req;
7184 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007185 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007186 }
7187 }
7188
7189 return 0;
7190}
7191
7192/*
7193 * Batched submission is done, ensure local IO is flushed out.
7194 */
7195static void io_submit_state_end(struct io_submit_state *state,
7196 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03007197{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007198 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007199 io_queue_sqe(state->link.head);
Pavel Begunkovc4501782021-09-08 16:40:52 +01007200 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007201 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007202 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007203}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007204
Jens Axboe9e645e112019-05-10 16:07:28 -06007205/*
7206 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007207 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007208static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007209 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007210{
7211 state->plug_started = false;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007212 state->need_plug = max_ios > 2;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007213 /* set only head, no need to init link_last in advance */
7214 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007215}
7216
Jens Axboe193155c2020-02-22 23:22:19 -07007217static void io_commit_sqring(struct io_ring_ctx *ctx)
7218{
Jens Axboe75c6a032020-01-28 10:15:23 -07007219 struct io_rings *rings = ctx->rings;
7220
7221 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007222 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007223 * since once we write the new head, the application could
7224 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007225 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007226 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007227}
7228
Jens Axboe9e645e112019-05-10 16:07:28 -06007229/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007230 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007231 * that is mapped by userspace. This means that care needs to be taken to
7232 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007233 * being a good citizen. If members of the sqe are validated and then later
7234 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007235 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007236 */
7237static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007238{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007239 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007240 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007241
7242 /*
7243 * The cached sq head (or cq tail) serves two purposes:
7244 *
7245 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007246 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007247 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007248 * though the application is the one updating it.
7249 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007250 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007251 if (likely(head < ctx->sq_entries))
7252 return &ctx->sq_sqes[head];
7253
7254 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007255 ctx->cq_extra--;
7256 WRITE_ONCE(ctx->rings->sq_dropped,
7257 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007258 return NULL;
7259}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007260
Jens Axboe0f212202020-09-13 13:09:39 -06007261static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007262 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007263{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007264 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007265
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03007266 /* make sure SQ entry isn't read before tail */
7267 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03007268 if (!percpu_ref_tryget_many(&ctx->refs, nr))
7269 return -EAGAIN;
Pavel Begunkov9a108672021-08-27 11:55:01 +01007270 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007271
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007272 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007273 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007274 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007275 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007276
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007277 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03007278 if (unlikely(!req)) {
7279 if (!submitted)
7280 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007281 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007282 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007283 sqe = io_get_sqe(ctx);
7284 if (unlikely(!sqe)) {
Hao Xu0c6e1d72021-08-26 01:58:56 +08007285 list_add(&req->inflight_entry, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007286 break;
7287 }
Jens Axboed3656342019-12-18 09:50:26 -07007288 /* will complete beyond this point, count as submitted */
7289 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007290 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007291 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007292 }
7293
Pavel Begunkov9466f432020-01-25 22:34:01 +03007294 if (unlikely(submitted != nr)) {
7295 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007296 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007297
Pavel Begunkov09899b12021-06-14 02:36:22 +01007298 current->io_uring->cached_refs += unused;
Jens Axboed8a6df12020-10-15 16:24:45 -06007299 percpu_ref_put_many(&ctx->refs, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03007300 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007301
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007302 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007303 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7304 io_commit_sqring(ctx);
7305
Jens Axboe6c271ce2019-01-10 11:22:30 -07007306 return submitted;
7307}
7308
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007309static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7310{
7311 return READ_ONCE(sqd->state);
7312}
7313
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007314static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7315{
7316 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007317 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007318 WRITE_ONCE(ctx->rings->sq_flags,
7319 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007320 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007321}
7322
7323static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7324{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007325 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007326 WRITE_ONCE(ctx->rings->sq_flags,
7327 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007328 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007329}
7330
Xiaoguang Wang08369242020-11-03 14:15:59 +08007331static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007332{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007333 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007334 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007335
Jens Axboec8d1ba52020-09-14 11:07:26 -06007336 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007337 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007338 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7339 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007340
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007341 if (!list_empty(&ctx->iopoll_list) || to_submit) {
7342 unsigned nr_events = 0;
Pavel Begunkov948e1942021-06-24 15:09:55 +01007343 const struct cred *creds = NULL;
7344
7345 if (ctx->sq_creds != current_cred())
7346 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007347
Xiaoguang Wang08369242020-11-03 14:15:59 +08007348 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007349 if (!list_empty(&ctx->iopoll_list))
Pavel Begunkova8576af2021-08-15 10:40:21 +01007350 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007351
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007352 /*
7353 * Don't submit if refs are dying, good for io_uring_register(),
7354 * but also it is relied upon by io_ring_exit_work()
7355 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007356 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7357 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007358 ret = io_submit_sqes(ctx, to_submit);
7359 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007360
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007361 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7362 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007363 if (creds)
7364 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007365 }
Jens Axboe90554202020-09-03 12:12:41 -06007366
Xiaoguang Wang08369242020-11-03 14:15:59 +08007367 return ret;
7368}
7369
7370static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7371{
7372 struct io_ring_ctx *ctx;
7373 unsigned sq_thread_idle = 0;
7374
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007375 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7376 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007377 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007378}
7379
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007380static bool io_sqd_handle_event(struct io_sq_data *sqd)
7381{
7382 bool did_sig = false;
7383 struct ksignal ksig;
7384
7385 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7386 signal_pending(current)) {
7387 mutex_unlock(&sqd->lock);
7388 if (signal_pending(current))
7389 did_sig = get_signal(&ksig);
7390 cond_resched();
7391 mutex_lock(&sqd->lock);
7392 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007393 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7394}
7395
Jens Axboe6c271ce2019-01-10 11:22:30 -07007396static int io_sq_thread(void *data)
7397{
Jens Axboe69fb2132020-09-14 11:16:23 -06007398 struct io_sq_data *sqd = data;
7399 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007400 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007401 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007402 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007403
Pavel Begunkov696ee882021-04-01 09:55:04 +01007404 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007405 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007406
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007407 if (sqd->sq_cpu != -1)
7408 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7409 else
7410 set_cpus_allowed_ptr(current, cpu_online_mask);
7411 current->flags |= PF_NO_SETAFFINITY;
7412
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007413 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007414 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007415 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007416
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007417 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7418 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007419 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007420 timeout = jiffies + sqd->sq_thread_idle;
7421 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007422
Jens Axboee95eee22020-09-08 09:11:32 -06007423 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007424 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007425 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007426
Xiaoguang Wang08369242020-11-03 14:15:59 +08007427 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7428 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007429 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007430 if (io_run_task_work())
7431 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007432
Xiaoguang Wang08369242020-11-03 14:15:59 +08007433 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007434 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007435 if (sqt_spin)
7436 timeout = jiffies + sqd->sq_thread_idle;
7437 continue;
7438 }
7439
Xiaoguang Wang08369242020-11-03 14:15:59 +08007440 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007441 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007442 bool needs_sched = true;
7443
Hao Xu724cb4f2021-04-21 23:19:11 +08007444 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007445 io_ring_set_wakeup_flag(ctx);
7446
Hao Xu724cb4f2021-04-21 23:19:11 +08007447 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7448 !list_empty_careful(&ctx->iopoll_list)) {
7449 needs_sched = false;
7450 break;
7451 }
7452 if (io_sqring_entries(ctx)) {
7453 needs_sched = false;
7454 break;
7455 }
7456 }
7457
7458 if (needs_sched) {
7459 mutex_unlock(&sqd->lock);
7460 schedule();
7461 mutex_lock(&sqd->lock);
7462 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007463 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7464 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007465 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007466
7467 finish_wait(&sqd->wait, &wait);
7468 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007469 }
7470
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007471 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007472 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007473 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007474 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007475 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007476 mutex_unlock(&sqd->lock);
7477
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007478 complete(&sqd->exited);
7479 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007480}
7481
Jens Axboebda52162019-09-24 13:47:15 -06007482struct io_wait_queue {
7483 struct wait_queue_entry wq;
7484 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007485 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007486 unsigned nr_timeouts;
7487};
7488
Pavel Begunkov6c503152021-01-04 20:36:36 +00007489static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007490{
7491 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007492 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007493
7494 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007495 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007496 * started waiting. For timeouts, we always want to return to userspace,
7497 * regardless of event count.
7498 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007499 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007500}
7501
7502static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7503 int wake_flags, void *key)
7504{
7505 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7506 wq);
7507
Pavel Begunkov6c503152021-01-04 20:36:36 +00007508 /*
7509 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7510 * the task, and the next invocation will do it.
7511 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007512 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007513 return autoremove_wake_function(curr, mode, wake_flags, key);
7514 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007515}
7516
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007517static int io_run_task_work_sig(void)
7518{
7519 if (io_run_task_work())
7520 return 1;
7521 if (!signal_pending(current))
7522 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007523 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007524 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007525 return -EINTR;
7526}
7527
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007528/* when returns >0, the caller should retry */
7529static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7530 struct io_wait_queue *iowq,
7531 signed long *timeout)
7532{
7533 int ret;
7534
7535 /* make sure we run task_work before checking for signals */
7536 ret = io_run_task_work_sig();
7537 if (ret || io_should_wake(iowq))
7538 return ret;
7539 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007540 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007541 return 1;
7542
7543 *timeout = schedule_timeout(*timeout);
7544 return !*timeout ? -ETIME : 1;
7545}
7546
Jens Axboe2b188cc2019-01-07 10:46:33 -07007547/*
7548 * Wait until events become available, if we don't already have some. The
7549 * application must reap them itself, as they reside on the shared cq ring.
7550 */
7551static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007552 const sigset_t __user *sig, size_t sigsz,
7553 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007554{
Pavel Begunkov902910992021-08-09 09:07:32 -06007555 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007556 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007557 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7558 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007559
Jens Axboeb41e9852020-02-17 09:52:41 -07007560 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007561 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007562 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007563 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007564 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007565 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007566 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007567
Xiaoguang Wang44df58d2021-09-14 22:38:52 +08007568 if (uts) {
7569 struct timespec64 ts;
7570
7571 if (get_timespec64(&ts, uts))
7572 return -EFAULT;
7573 timeout = timespec64_to_jiffies(&ts);
7574 }
7575
Jens Axboe2b188cc2019-01-07 10:46:33 -07007576 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007577#ifdef CONFIG_COMPAT
7578 if (in_compat_syscall())
7579 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007580 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007581 else
7582#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007583 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007584
Jens Axboe2b188cc2019-01-07 10:46:33 -07007585 if (ret)
7586 return ret;
7587 }
7588
Pavel Begunkov902910992021-08-09 09:07:32 -06007589 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7590 iowq.wq.private = current;
7591 INIT_LIST_HEAD(&iowq.wq.entry);
7592 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007593 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007594 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007595
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007596 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007597 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007598 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007599 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007600 ret = -EBUSY;
7601 break;
7602 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007603 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007604 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007605 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007606 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007607 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007608 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007609
Jens Axboeb7db41c2020-07-04 08:55:50 -06007610 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007611
Hristo Venev75b28af2019-08-26 17:23:46 +00007612 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007613}
7614
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007615static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007616{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007617 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007618
7619 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007620 kfree(table[i]);
7621 kfree(table);
7622}
7623
7624static void **io_alloc_page_table(size_t size)
7625{
7626 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7627 size_t init_size = size;
7628 void **table;
7629
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007630 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007631 if (!table)
7632 return NULL;
7633
7634 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007635 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007636
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007637 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007638 if (!table[i]) {
7639 io_free_page_table(table, init_size);
7640 return NULL;
7641 }
7642 size -= this_size;
7643 }
7644 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007645}
7646
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007647static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7648{
7649 percpu_ref_exit(&ref_node->refs);
7650 kfree(ref_node);
7651}
7652
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007653static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
7654{
7655 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7656 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7657 unsigned long flags;
7658 bool first_add = false;
7659
7660 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7661 node->done = true;
7662
7663 while (!list_empty(&ctx->rsrc_ref_list)) {
7664 node = list_first_entry(&ctx->rsrc_ref_list,
7665 struct io_rsrc_node, node);
7666 /* recycle ref nodes in order */
7667 if (!node->done)
7668 break;
7669 list_del(&node->node);
7670 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7671 }
7672 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7673
7674 if (first_add)
7675 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7676}
7677
7678static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7679{
7680 struct io_rsrc_node *ref_node;
7681
7682 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7683 if (!ref_node)
7684 return NULL;
7685
7686 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7687 0, GFP_KERNEL)) {
7688 kfree(ref_node);
7689 return NULL;
7690 }
7691 INIT_LIST_HEAD(&ref_node->node);
7692 INIT_LIST_HEAD(&ref_node->rsrc_list);
7693 ref_node->done = false;
7694 return ref_node;
7695}
7696
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007697static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7698 struct io_rsrc_data *data_to_kill)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007699{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007700 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7701 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007702
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007703 if (data_to_kill) {
7704 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007705
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007706 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007707 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007708 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007709 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007710
Pavel Begunkov3e942492021-04-11 01:46:34 +01007711 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007712 percpu_ref_kill(&rsrc_node->refs);
7713 ctx->rsrc_node = NULL;
7714 }
7715
7716 if (!ctx->rsrc_node) {
7717 ctx->rsrc_node = ctx->rsrc_backup_node;
7718 ctx->rsrc_backup_node = NULL;
7719 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007720}
7721
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007722static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007723{
7724 if (ctx->rsrc_backup_node)
7725 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007726 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007727 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7728}
7729
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007730static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007731{
7732 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007733
Pavel Begunkov215c3902021-04-01 15:43:48 +01007734 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007735 if (data->quiesce)
7736 return -ENXIO;
7737
7738 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007739 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007740 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007741 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007742 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007743 io_rsrc_node_switch(ctx, data);
7744
Pavel Begunkov3e942492021-04-11 01:46:34 +01007745 /* kill initial ref, already quiesced if zero */
7746 if (atomic_dec_and_test(&data->refs))
7747 break;
Jens Axboec018db42021-08-09 08:15:50 -06007748 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007749 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007750 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007751 if (!ret) {
7752 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007753 break;
Jens Axboec018db42021-08-09 08:15:50 -06007754 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007755
Pavel Begunkov3e942492021-04-11 01:46:34 +01007756 atomic_inc(&data->refs);
7757 /* wait for all works potentially completing data->done */
7758 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007759 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007760
Hao Xu8bad28d2021-02-19 17:19:36 +08007761 ret = io_run_task_work_sig();
7762 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007763 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007764 data->quiesce = false;
7765
Hao Xu8bad28d2021-02-19 17:19:36 +08007766 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007767}
7768
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007769static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7770{
7771 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7772 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7773
7774 return &data->tags[table_idx][off];
7775}
7776
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007777static void io_rsrc_data_free(struct io_rsrc_data *data)
7778{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007779 size_t size = data->nr * sizeof(data->tags[0][0]);
7780
7781 if (data->tags)
7782 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007783 kfree(data);
7784}
7785
Pavel Begunkovd878c812021-06-14 02:36:18 +01007786static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7787 u64 __user *utags, unsigned nr,
7788 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007789{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007790 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007791 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007792 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007793
7794 data = kzalloc(sizeof(*data), GFP_KERNEL);
7795 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007796 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007797 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007798 if (!data->tags) {
7799 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007800 return -ENOMEM;
7801 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007802
7803 data->nr = nr;
7804 data->ctx = ctx;
7805 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007806 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007807 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007808 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007809 u64 *tag_slot = io_get_tag_slot(data, i);
7810
7811 if (copy_from_user(tag_slot, &utags[i],
7812 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007813 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007814 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007815 }
7816
Pavel Begunkov3e942492021-04-11 01:46:34 +01007817 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007818 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007819 *pdata = data;
7820 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007821fail:
7822 io_rsrc_data_free(data);
7823 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007824}
7825
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007826static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7827{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007828 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7829 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007830 return !!table->files;
7831}
7832
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007833static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007834{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007835 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007836 table->files = NULL;
7837}
7838
Jens Axboe2b188cc2019-01-07 10:46:33 -07007839static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7840{
7841#if defined(CONFIG_UNIX)
7842 if (ctx->ring_sock) {
7843 struct sock *sock = ctx->ring_sock->sk;
7844 struct sk_buff *skb;
7845
7846 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7847 kfree_skb(skb);
7848 }
7849#else
7850 int i;
7851
7852 for (i = 0; i < ctx->nr_user_files; i++) {
7853 struct file *file;
7854
7855 file = io_file_from_index(ctx, i);
7856 if (file)
7857 fput(file);
7858 }
7859#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007860 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007861 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007862 ctx->file_data = NULL;
7863 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007864}
7865
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007866static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7867{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007868 int ret;
7869
Pavel Begunkov08480402021-04-13 02:58:38 +01007870 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007871 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007872 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7873 if (!ret)
7874 __io_sqe_files_unregister(ctx);
7875 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007876}
7877
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007878static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007879 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007880{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007881 WARN_ON_ONCE(sqd->thread == current);
7882
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007883 /*
7884 * Do the dance but not conditional clear_bit() because it'd race with
7885 * other threads incrementing park_pending and setting the bit.
7886 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007887 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007888 if (atomic_dec_return(&sqd->park_pending))
7889 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007890 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007891}
7892
Jens Axboe86e0d672021-03-05 08:44:39 -07007893static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007894 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007895{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007896 WARN_ON_ONCE(sqd->thread == current);
7897
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007898 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007899 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007900 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007901 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007902 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007903}
7904
7905static void io_sq_thread_stop(struct io_sq_data *sqd)
7906{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007907 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007908 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007909
Jens Axboe05962f92021-03-06 13:58:48 -07007910 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007911 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007912 if (sqd->thread)
7913 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007914 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007915 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007916}
7917
Jens Axboe534ca6d2020-09-02 13:52:19 -06007918static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007919{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007920 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007921 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7922
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007923 io_sq_thread_stop(sqd);
7924 kfree(sqd);
7925 }
7926}
7927
7928static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7929{
7930 struct io_sq_data *sqd = ctx->sq_data;
7931
7932 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007933 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007934 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007935 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007936 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007937
7938 io_put_sq_data(sqd);
7939 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007940 }
7941}
7942
Jens Axboeaa061652020-09-02 14:50:27 -06007943static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7944{
7945 struct io_ring_ctx *ctx_attach;
7946 struct io_sq_data *sqd;
7947 struct fd f;
7948
7949 f = fdget(p->wq_fd);
7950 if (!f.file)
7951 return ERR_PTR(-ENXIO);
7952 if (f.file->f_op != &io_uring_fops) {
7953 fdput(f);
7954 return ERR_PTR(-EINVAL);
7955 }
7956
7957 ctx_attach = f.file->private_data;
7958 sqd = ctx_attach->sq_data;
7959 if (!sqd) {
7960 fdput(f);
7961 return ERR_PTR(-EINVAL);
7962 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007963 if (sqd->task_tgid != current->tgid) {
7964 fdput(f);
7965 return ERR_PTR(-EPERM);
7966 }
Jens Axboeaa061652020-09-02 14:50:27 -06007967
7968 refcount_inc(&sqd->refs);
7969 fdput(f);
7970 return sqd;
7971}
7972
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007973static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7974 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007975{
7976 struct io_sq_data *sqd;
7977
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007978 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007979 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7980 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007981 if (!IS_ERR(sqd)) {
7982 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007983 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007984 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007985 /* fall through for EPERM case, setup new sqd/task */
7986 if (PTR_ERR(sqd) != -EPERM)
7987 return sqd;
7988 }
Jens Axboeaa061652020-09-02 14:50:27 -06007989
Jens Axboe534ca6d2020-09-02 13:52:19 -06007990 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7991 if (!sqd)
7992 return ERR_PTR(-ENOMEM);
7993
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007994 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007995 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007996 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007997 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007998 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007999 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008000 return sqd;
8001}
8002
Jens Axboe6b063142019-01-10 22:13:58 -07008003#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07008004/*
8005 * Ensure the UNIX gc is aware of our file set, so we are certain that
8006 * the io_uring can be safely unregistered on process exit, even if we have
8007 * loops in the file referencing.
8008 */
8009static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
8010{
8011 struct sock *sk = ctx->ring_sock->sk;
8012 struct scm_fp_list *fpl;
8013 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06008014 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07008015
Jens Axboe6b063142019-01-10 22:13:58 -07008016 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
8017 if (!fpl)
8018 return -ENOMEM;
8019
8020 skb = alloc_skb(0, GFP_KERNEL);
8021 if (!skb) {
8022 kfree(fpl);
8023 return -ENOMEM;
8024 }
8025
8026 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07008027
Jens Axboe08a45172019-10-03 08:11:03 -06008028 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07008029 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07008030 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008031 struct file *file = io_file_from_index(ctx, i + offset);
8032
8033 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06008034 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06008035 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06008036 unix_inflight(fpl->user, fpl->fp[nr_files]);
8037 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07008038 }
8039
Jens Axboe08a45172019-10-03 08:11:03 -06008040 if (nr_files) {
8041 fpl->max = SCM_MAX_FD;
8042 fpl->count = nr_files;
8043 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008044 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06008045 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
8046 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07008047
Jens Axboe08a45172019-10-03 08:11:03 -06008048 for (i = 0; i < nr_files; i++)
8049 fput(fpl->fp[i]);
8050 } else {
8051 kfree_skb(skb);
8052 kfree(fpl);
8053 }
Jens Axboe6b063142019-01-10 22:13:58 -07008054
8055 return 0;
8056}
8057
8058/*
8059 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8060 * causes regular reference counting to break down. We rely on the UNIX
8061 * garbage collection to take care of this problem for us.
8062 */
8063static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8064{
8065 unsigned left, total;
8066 int ret = 0;
8067
8068 total = 0;
8069 left = ctx->nr_user_files;
8070 while (left) {
8071 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008072
8073 ret = __io_sqe_files_scm(ctx, this_files, total);
8074 if (ret)
8075 break;
8076 left -= this_files;
8077 total += this_files;
8078 }
8079
8080 if (!ret)
8081 return 0;
8082
8083 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008084 struct file *file = io_file_from_index(ctx, total);
8085
8086 if (file)
8087 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008088 total++;
8089 }
8090
8091 return ret;
8092}
8093#else
8094static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8095{
8096 return 0;
8097}
8098#endif
8099
Pavel Begunkov47e90392021-04-01 15:43:56 +01008100static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008101{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008102 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008103#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008104 struct sock *sock = ctx->ring_sock->sk;
8105 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8106 struct sk_buff *skb;
8107 int i;
8108
8109 __skb_queue_head_init(&list);
8110
8111 /*
8112 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8113 * remove this entry and rearrange the file array.
8114 */
8115 skb = skb_dequeue(head);
8116 while (skb) {
8117 struct scm_fp_list *fp;
8118
8119 fp = UNIXCB(skb).fp;
8120 for (i = 0; i < fp->count; i++) {
8121 int left;
8122
8123 if (fp->fp[i] != file)
8124 continue;
8125
8126 unix_notinflight(fp->user, fp->fp[i]);
8127 left = fp->count - 1 - i;
8128 if (left) {
8129 memmove(&fp->fp[i], &fp->fp[i + 1],
8130 left * sizeof(struct file *));
8131 }
8132 fp->count--;
8133 if (!fp->count) {
8134 kfree_skb(skb);
8135 skb = NULL;
8136 } else {
8137 __skb_queue_tail(&list, skb);
8138 }
8139 fput(file);
8140 file = NULL;
8141 break;
8142 }
8143
8144 if (!file)
8145 break;
8146
8147 __skb_queue_tail(&list, skb);
8148
8149 skb = skb_dequeue(head);
8150 }
8151
8152 if (skb_peek(&list)) {
8153 spin_lock_irq(&head->lock);
8154 while ((skb = __skb_dequeue(&list)) != NULL)
8155 __skb_queue_tail(head, skb);
8156 spin_unlock_irq(&head->lock);
8157 }
8158#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008159 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008160#endif
8161}
8162
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008163static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008164{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008165 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008166 struct io_ring_ctx *ctx = rsrc_data->ctx;
8167 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008168
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008169 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8170 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008171
8172 if (prsrc->tag) {
8173 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008174
8175 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008176 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008177 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01008178 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008179 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008180 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008181 io_cqring_ev_posted(ctx);
8182 io_ring_submit_unlock(ctx, lock_ring);
8183 }
8184
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008185 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008186 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008187 }
8188
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008189 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008190 if (atomic_dec_and_test(&rsrc_data->refs))
8191 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008192}
8193
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008194static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008195{
8196 struct io_ring_ctx *ctx;
8197 struct llist_node *node;
8198
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008199 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8200 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008201
8202 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008203 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008204 struct llist_node *next = node->next;
8205
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008206 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008207 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008208 node = next;
8209 }
8210}
8211
Jens Axboe05f3fb32019-12-09 11:22:50 -07008212static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008213 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008214{
8215 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008216 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008217 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008218 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008219
8220 if (ctx->file_data)
8221 return -EBUSY;
8222 if (!nr_args)
8223 return -EINVAL;
8224 if (nr_args > IORING_MAX_FIXED_FILES)
8225 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008226 if (nr_args > rlimit(RLIMIT_NOFILE))
8227 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008228 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008229 if (ret)
8230 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008231 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8232 &ctx->file_data);
8233 if (ret)
8234 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008235
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008236 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008237 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008238 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008239
Jens Axboe05f3fb32019-12-09 11:22:50 -07008240 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008241 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008242 ret = -EFAULT;
8243 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008244 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008245 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008246 if (fd == -1) {
8247 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008248 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008249 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008250 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008251 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008252
Jens Axboe05f3fb32019-12-09 11:22:50 -07008253 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008254 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008255 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008256 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008257
8258 /*
8259 * Don't allow io_uring instances to be registered. If UNIX
8260 * isn't enabled, then this causes a reference cycle and this
8261 * instance can never get freed. If UNIX is enabled we'll
8262 * handle it just fine, but there's still no point in allowing
8263 * a ring fd as it doesn't support regular read/write anyway.
8264 */
8265 if (file->f_op == &io_uring_fops) {
8266 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008267 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008268 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008269 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008270 }
8271
Jens Axboe05f3fb32019-12-09 11:22:50 -07008272 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008273 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008274 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008275 return ret;
8276 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008277
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008278 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008279 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008280out_fput:
8281 for (i = 0; i < ctx->nr_user_files; i++) {
8282 file = io_file_from_index(ctx, i);
8283 if (file)
8284 fput(file);
8285 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008286 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008287 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008288out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008289 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008290 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008291 return ret;
8292}
8293
Jens Axboec3a31e62019-10-03 13:59:56 -06008294static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8295 int index)
8296{
8297#if defined(CONFIG_UNIX)
8298 struct sock *sock = ctx->ring_sock->sk;
8299 struct sk_buff_head *head = &sock->sk_receive_queue;
8300 struct sk_buff *skb;
8301
8302 /*
8303 * See if we can merge this file into an existing skb SCM_RIGHTS
8304 * file set. If there's no room, fall back to allocating a new skb
8305 * and filling it in.
8306 */
8307 spin_lock_irq(&head->lock);
8308 skb = skb_peek(head);
8309 if (skb) {
8310 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8311
8312 if (fpl->count < SCM_MAX_FD) {
8313 __skb_unlink(skb, head);
8314 spin_unlock_irq(&head->lock);
8315 fpl->fp[fpl->count] = get_file(file);
8316 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8317 fpl->count++;
8318 spin_lock_irq(&head->lock);
8319 __skb_queue_head(head, skb);
8320 } else {
8321 skb = NULL;
8322 }
8323 }
8324 spin_unlock_irq(&head->lock);
8325
8326 if (skb) {
8327 fput(file);
8328 return 0;
8329 }
8330
8331 return __io_sqe_files_scm(ctx, 1, index);
8332#else
8333 return 0;
8334#endif
8335}
8336
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008337static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
8338 struct io_rsrc_node *node, void *rsrc)
8339{
8340 struct io_rsrc_put *prsrc;
8341
8342 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8343 if (!prsrc)
8344 return -ENOMEM;
8345
8346 prsrc->tag = *io_get_tag_slot(data, idx);
8347 prsrc->rsrc = rsrc;
8348 list_add(&prsrc->list, &node->rsrc_list);
8349 return 0;
8350}
8351
Pavel Begunkovb9445592021-08-25 12:25:45 +01008352static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8353 unsigned int issue_flags, u32 slot_index)
8354{
8355 struct io_ring_ctx *ctx = req->ctx;
8356 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008357 bool needs_switch = false;
Pavel Begunkovb9445592021-08-25 12:25:45 +01008358 struct io_fixed_file *file_slot;
8359 int ret = -EBADF;
8360
8361 io_ring_submit_lock(ctx, !force_nonblock);
8362 if (file->f_op == &io_uring_fops)
8363 goto err;
8364 ret = -ENXIO;
8365 if (!ctx->file_data)
8366 goto err;
8367 ret = -EINVAL;
8368 if (slot_index >= ctx->nr_user_files)
8369 goto err;
8370
8371 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8372 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008373
8374 if (file_slot->file_ptr) {
8375 struct file *old_file;
8376
8377 ret = io_rsrc_node_switch_start(ctx);
8378 if (ret)
8379 goto err;
8380
8381 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8382 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
8383 ctx->rsrc_node, old_file);
8384 if (ret)
8385 goto err;
8386 file_slot->file_ptr = 0;
8387 needs_switch = true;
8388 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01008389
8390 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8391 io_fixed_file_set(file_slot, file);
8392 ret = io_sqe_file_register(ctx, file, slot_index);
8393 if (ret) {
8394 file_slot->file_ptr = 0;
8395 goto err;
8396 }
8397
8398 ret = 0;
8399err:
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008400 if (needs_switch)
8401 io_rsrc_node_switch(ctx, ctx->file_data);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008402 io_ring_submit_unlock(ctx, !force_nonblock);
8403 if (ret)
8404 fput(file);
8405 return ret;
8406}
8407
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008408static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
8409{
8410 unsigned int offset = req->close.file_slot - 1;
8411 struct io_ring_ctx *ctx = req->ctx;
8412 struct io_fixed_file *file_slot;
8413 struct file *file;
8414 int ret, i;
8415
8416 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
8417 ret = -ENXIO;
8418 if (unlikely(!ctx->file_data))
8419 goto out;
8420 ret = -EINVAL;
8421 if (offset >= ctx->nr_user_files)
8422 goto out;
8423 ret = io_rsrc_node_switch_start(ctx);
8424 if (ret)
8425 goto out;
8426
8427 i = array_index_nospec(offset, ctx->nr_user_files);
8428 file_slot = io_fixed_file_slot(&ctx->file_table, i);
8429 ret = -EBADF;
8430 if (!file_slot->file_ptr)
8431 goto out;
8432
8433 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8434 ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
8435 if (ret)
8436 goto out;
8437
8438 file_slot->file_ptr = 0;
8439 io_rsrc_node_switch(ctx, ctx->file_data);
8440 ret = 0;
8441out:
8442 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
8443 return ret;
8444}
8445
Jens Axboe05f3fb32019-12-09 11:22:50 -07008446static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008447 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008448 unsigned nr_args)
8449{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008450 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008451 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008452 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008453 struct io_fixed_file *file_slot;
8454 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008455 int fd, i, err = 0;
8456 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008457 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008458
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008459 if (!ctx->file_data)
8460 return -ENXIO;
8461 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008462 return -EINVAL;
8463
Pavel Begunkov67973b92021-01-26 13:51:09 +00008464 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008465 u64 tag = 0;
8466
8467 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8468 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008469 err = -EFAULT;
8470 break;
8471 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008472 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8473 err = -EINVAL;
8474 break;
8475 }
noah4e0377a2021-01-26 15:23:28 -05008476 if (fd == IORING_REGISTER_FILES_SKIP)
8477 continue;
8478
Pavel Begunkov67973b92021-01-26 13:51:09 +00008479 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008480 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008481
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008482 if (file_slot->file_ptr) {
8483 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008484 err = io_queue_rsrc_removal(data, up->offset + done,
8485 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008486 if (err)
8487 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008488 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008489 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008490 }
8491 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008492 file = fget(fd);
8493 if (!file) {
8494 err = -EBADF;
8495 break;
8496 }
8497 /*
8498 * Don't allow io_uring instances to be registered. If
8499 * UNIX isn't enabled, then this causes a reference
8500 * cycle and this instance can never get freed. If UNIX
8501 * is enabled we'll handle it just fine, but there's
8502 * still no point in allowing a ring fd as it doesn't
8503 * support regular read/write anyway.
8504 */
8505 if (file->f_op == &io_uring_fops) {
8506 fput(file);
8507 err = -EBADF;
8508 break;
8509 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008510 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008511 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008512 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008513 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008514 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008515 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008516 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008517 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008518 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008519 }
8520
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008521 if (needs_switch)
8522 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008523 return done ? done : err;
8524}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008525
Jens Axboe685fe7f2021-03-08 09:37:51 -07008526static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8527 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008528{
Jens Axboee9418942021-02-19 12:33:30 -07008529 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008530 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008531 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008532
Yang Yingliang362a9e62021-07-20 16:38:05 +08008533 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008534 hash = ctx->hash_map;
8535 if (!hash) {
8536 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008537 if (!hash) {
8538 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008539 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008540 }
Jens Axboee9418942021-02-19 12:33:30 -07008541 refcount_set(&hash->refs, 1);
8542 init_waitqueue_head(&hash->wait);
8543 ctx->hash_map = hash;
8544 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008545 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008546
8547 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008548 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008549 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008550 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008551
Jens Axboed25e3a32021-02-16 11:41:41 -07008552 /* Do QD, or 4 * CPUS, whatever is smallest */
8553 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008554
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008555 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008556}
8557
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008558static int io_uring_alloc_task_context(struct task_struct *task,
8559 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008560{
8561 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008562 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008563
Pavel Begunkov09899b12021-06-14 02:36:22 +01008564 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008565 if (unlikely(!tctx))
8566 return -ENOMEM;
8567
Jens Axboed8a6df12020-10-15 16:24:45 -06008568 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8569 if (unlikely(ret)) {
8570 kfree(tctx);
8571 return ret;
8572 }
8573
Jens Axboe685fe7f2021-03-08 09:37:51 -07008574 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008575 if (IS_ERR(tctx->io_wq)) {
8576 ret = PTR_ERR(tctx->io_wq);
8577 percpu_counter_destroy(&tctx->inflight);
8578 kfree(tctx);
8579 return ret;
8580 }
8581
Jens Axboe0f212202020-09-13 13:09:39 -06008582 xa_init(&tctx->xa);
8583 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008584 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008585 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008586 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008587 spin_lock_init(&tctx->task_lock);
8588 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008589 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008590 return 0;
8591}
8592
8593void __io_uring_free(struct task_struct *tsk)
8594{
8595 struct io_uring_task *tctx = tsk->io_uring;
8596
8597 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008598 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008599 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008600
Jens Axboed8a6df12020-10-15 16:24:45 -06008601 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008602 kfree(tctx);
8603 tsk->io_uring = NULL;
8604}
8605
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008606static int io_sq_offload_create(struct io_ring_ctx *ctx,
8607 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008608{
8609 int ret;
8610
Jens Axboed25e3a32021-02-16 11:41:41 -07008611 /* Retain compatibility with failing for an invalid attach attempt */
8612 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8613 IORING_SETUP_ATTACH_WQ) {
8614 struct fd f;
8615
8616 f = fdget(p->wq_fd);
8617 if (!f.file)
8618 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008619 if (f.file->f_op != &io_uring_fops) {
8620 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008621 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008622 }
8623 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008624 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008625 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008626 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008627 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008628 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008629
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008630 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008631 if (IS_ERR(sqd)) {
8632 ret = PTR_ERR(sqd);
8633 goto err;
8634 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008635
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008636 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008637 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008638 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8639 if (!ctx->sq_thread_idle)
8640 ctx->sq_thread_idle = HZ;
8641
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008642 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008643 list_add(&ctx->sqd_list, &sqd->ctx_list);
8644 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008645 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008646 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008647 io_sq_thread_unpark(sqd);
8648
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008649 if (ret < 0)
8650 goto err;
8651 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008652 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008653
Jens Axboe6c271ce2019-01-10 11:22:30 -07008654 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008655 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008656
Jens Axboe917257d2019-04-13 09:28:55 -06008657 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008658 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008659 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008660 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008661 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008662 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008663 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008664
8665 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008666 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008667 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8668 if (IS_ERR(tsk)) {
8669 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008670 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008671 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008672
Jens Axboe46fe18b2021-03-04 12:39:36 -07008673 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008674 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008675 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008676 if (ret)
8677 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008678 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8679 /* Can't have SQ_AFF without SQPOLL */
8680 ret = -EINVAL;
8681 goto err;
8682 }
8683
Jens Axboe2b188cc2019-01-07 10:46:33 -07008684 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008685err_sqpoll:
8686 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008687err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008688 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008689 return ret;
8690}
8691
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008692static inline void __io_unaccount_mem(struct user_struct *user,
8693 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008694{
8695 atomic_long_sub(nr_pages, &user->locked_vm);
8696}
8697
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008698static inline int __io_account_mem(struct user_struct *user,
8699 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008700{
8701 unsigned long page_limit, cur_pages, new_pages;
8702
8703 /* Don't allow more pages than we can safely lock */
8704 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8705
8706 do {
8707 cur_pages = atomic_long_read(&user->locked_vm);
8708 new_pages = cur_pages + nr_pages;
8709 if (new_pages > page_limit)
8710 return -ENOMEM;
8711 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8712 new_pages) != cur_pages);
8713
8714 return 0;
8715}
8716
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008717static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008718{
Jens Axboe62e398b2021-02-21 16:19:37 -07008719 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008720 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008721
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008722 if (ctx->mm_account)
8723 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008724}
8725
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008726static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008727{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008728 int ret;
8729
Jens Axboe62e398b2021-02-21 16:19:37 -07008730 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008731 ret = __io_account_mem(ctx->user, nr_pages);
8732 if (ret)
8733 return ret;
8734 }
8735
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008736 if (ctx->mm_account)
8737 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008738
8739 return 0;
8740}
8741
Jens Axboe2b188cc2019-01-07 10:46:33 -07008742static void io_mem_free(void *ptr)
8743{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008744 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008745
Mark Rutland52e04ef2019-04-30 17:30:21 +01008746 if (!ptr)
8747 return;
8748
8749 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008750 if (put_page_testzero(page))
8751 free_compound_page(page);
8752}
8753
8754static void *io_mem_alloc(size_t size)
8755{
8756 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008757 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008758
8759 return (void *) __get_free_pages(gfp_flags, get_order(size));
8760}
8761
Hristo Venev75b28af2019-08-26 17:23:46 +00008762static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8763 size_t *sq_offset)
8764{
8765 struct io_rings *rings;
8766 size_t off, sq_array_size;
8767
8768 off = struct_size(rings, cqes, cq_entries);
8769 if (off == SIZE_MAX)
8770 return SIZE_MAX;
8771
8772#ifdef CONFIG_SMP
8773 off = ALIGN(off, SMP_CACHE_BYTES);
8774 if (off == 0)
8775 return SIZE_MAX;
8776#endif
8777
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008778 if (sq_offset)
8779 *sq_offset = off;
8780
Hristo Venev75b28af2019-08-26 17:23:46 +00008781 sq_array_size = array_size(sizeof(u32), sq_entries);
8782 if (sq_array_size == SIZE_MAX)
8783 return SIZE_MAX;
8784
8785 if (check_add_overflow(off, sq_array_size, &off))
8786 return SIZE_MAX;
8787
Hristo Venev75b28af2019-08-26 17:23:46 +00008788 return off;
8789}
8790
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008791static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008792{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008793 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008794 unsigned int i;
8795
Pavel Begunkov62248432021-04-28 13:11:29 +01008796 if (imu != ctx->dummy_ubuf) {
8797 for (i = 0; i < imu->nr_bvecs; i++)
8798 unpin_user_page(imu->bvec[i].bv_page);
8799 if (imu->acct_pages)
8800 io_unaccount_mem(ctx, imu->acct_pages);
8801 kvfree(imu);
8802 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008803 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008804}
8805
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008806static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8807{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008808 io_buffer_unmap(ctx, &prsrc->buf);
8809 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008810}
8811
8812static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008813{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008814 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008815
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008816 for (i = 0; i < ctx->nr_user_bufs; i++)
8817 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008818 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008819 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008820 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008821 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008822 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008823}
8824
Jens Axboeedafcce2019-01-09 09:16:05 -07008825static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8826{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008827 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008828
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008829 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008830 return -ENXIO;
8831
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008832 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8833 if (!ret)
8834 __io_sqe_buffers_unregister(ctx);
8835 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008836}
8837
8838static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8839 void __user *arg, unsigned index)
8840{
8841 struct iovec __user *src;
8842
8843#ifdef CONFIG_COMPAT
8844 if (ctx->compat) {
8845 struct compat_iovec __user *ciovs;
8846 struct compat_iovec ciov;
8847
8848 ciovs = (struct compat_iovec __user *) arg;
8849 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8850 return -EFAULT;
8851
Jens Axboed55e5f52019-12-11 16:12:15 -07008852 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008853 dst->iov_len = ciov.iov_len;
8854 return 0;
8855 }
8856#endif
8857 src = (struct iovec __user *) arg;
8858 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8859 return -EFAULT;
8860 return 0;
8861}
8862
Jens Axboede293932020-09-17 16:19:16 -06008863/*
8864 * Not super efficient, but this is just a registration time. And we do cache
8865 * the last compound head, so generally we'll only do a full search if we don't
8866 * match that one.
8867 *
8868 * We check if the given compound head page has already been accounted, to
8869 * avoid double accounting it. This allows us to account the full size of the
8870 * page, not just the constituent pages of a huge page.
8871 */
8872static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8873 int nr_pages, struct page *hpage)
8874{
8875 int i, j;
8876
8877 /* check current page array */
8878 for (i = 0; i < nr_pages; i++) {
8879 if (!PageCompound(pages[i]))
8880 continue;
8881 if (compound_head(pages[i]) == hpage)
8882 return true;
8883 }
8884
8885 /* check previously registered pages */
8886 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008887 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008888
8889 for (j = 0; j < imu->nr_bvecs; j++) {
8890 if (!PageCompound(imu->bvec[j].bv_page))
8891 continue;
8892 if (compound_head(imu->bvec[j].bv_page) == hpage)
8893 return true;
8894 }
8895 }
8896
8897 return false;
8898}
8899
8900static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8901 int nr_pages, struct io_mapped_ubuf *imu,
8902 struct page **last_hpage)
8903{
8904 int i, ret;
8905
Pavel Begunkov216e5832021-05-29 12:01:02 +01008906 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008907 for (i = 0; i < nr_pages; i++) {
8908 if (!PageCompound(pages[i])) {
8909 imu->acct_pages++;
8910 } else {
8911 struct page *hpage;
8912
8913 hpage = compound_head(pages[i]);
8914 if (hpage == *last_hpage)
8915 continue;
8916 *last_hpage = hpage;
8917 if (headpage_already_acct(ctx, pages, i, hpage))
8918 continue;
8919 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8920 }
8921 }
8922
8923 if (!imu->acct_pages)
8924 return 0;
8925
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008926 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008927 if (ret)
8928 imu->acct_pages = 0;
8929 return ret;
8930}
8931
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008932static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008933 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008934 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008935{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008936 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008937 struct vm_area_struct **vmas = NULL;
8938 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008939 unsigned long off, start, end, ubuf;
8940 size_t size;
8941 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008942
Pavel Begunkov62248432021-04-28 13:11:29 +01008943 if (!iov->iov_base) {
8944 *pimu = ctx->dummy_ubuf;
8945 return 0;
8946 }
8947
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008948 ubuf = (unsigned long) iov->iov_base;
8949 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8950 start = ubuf >> PAGE_SHIFT;
8951 nr_pages = end - start;
8952
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008953 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008954 ret = -ENOMEM;
8955
8956 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8957 if (!pages)
8958 goto done;
8959
8960 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8961 GFP_KERNEL);
8962 if (!vmas)
8963 goto done;
8964
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008965 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008966 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008967 goto done;
8968
8969 ret = 0;
8970 mmap_read_lock(current->mm);
8971 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8972 pages, vmas);
8973 if (pret == nr_pages) {
8974 /* don't support file backed memory */
8975 for (i = 0; i < nr_pages; i++) {
8976 struct vm_area_struct *vma = vmas[i];
8977
Pavel Begunkov40dad762021-06-09 15:26:54 +01008978 if (vma_is_shmem(vma))
8979 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008980 if (vma->vm_file &&
8981 !is_file_hugepages(vma->vm_file)) {
8982 ret = -EOPNOTSUPP;
8983 break;
8984 }
8985 }
8986 } else {
8987 ret = pret < 0 ? pret : -EFAULT;
8988 }
8989 mmap_read_unlock(current->mm);
8990 if (ret) {
8991 /*
8992 * if we did partial map, or found file backed vmas,
8993 * release any pages we did get
8994 */
8995 if (pret > 0)
8996 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008997 goto done;
8998 }
8999
9000 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
9001 if (ret) {
9002 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009003 goto done;
9004 }
9005
9006 off = ubuf & ~PAGE_MASK;
9007 size = iov->iov_len;
9008 for (i = 0; i < nr_pages; i++) {
9009 size_t vec_len;
9010
9011 vec_len = min_t(size_t, size, PAGE_SIZE - off);
9012 imu->bvec[i].bv_page = pages[i];
9013 imu->bvec[i].bv_len = vec_len;
9014 imu->bvec[i].bv_offset = off;
9015 off = 0;
9016 size -= vec_len;
9017 }
9018 /* store original address for later verification */
9019 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01009020 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009021 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009022 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009023 ret = 0;
9024done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009025 if (ret)
9026 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009027 kvfree(pages);
9028 kvfree(vmas);
9029 return ret;
9030}
9031
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009032static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009033{
Pavel Begunkov87094462021-04-11 01:46:36 +01009034 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
9035 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009036}
9037
9038static int io_buffer_validate(struct iovec *iov)
9039{
Pavel Begunkov50e96982021-03-24 22:59:01 +00009040 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
9041
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009042 /*
9043 * Don't impose further limits on the size and buffer
9044 * constraints here, we'll -EINVAL later when IO is
9045 * submitted if they are wrong.
9046 */
Pavel Begunkov62248432021-04-28 13:11:29 +01009047 if (!iov->iov_base)
9048 return iov->iov_len ? -EFAULT : 0;
9049 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009050 return -EFAULT;
9051
9052 /* arbitrary limit, but we need something */
9053 if (iov->iov_len > SZ_1G)
9054 return -EFAULT;
9055
Pavel Begunkov50e96982021-03-24 22:59:01 +00009056 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
9057 return -EOVERFLOW;
9058
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009059 return 0;
9060}
9061
9062static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009063 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009064{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009065 struct page *last_hpage = NULL;
9066 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009067 int i, ret;
9068 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009069
Pavel Begunkov87094462021-04-11 01:46:36 +01009070 if (ctx->user_bufs)
9071 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01009072 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01009073 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009074 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009075 if (ret)
9076 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01009077 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9078 if (ret)
9079 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009080 ret = io_buffers_map_alloc(ctx, nr_args);
9081 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08009082 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009083 return ret;
9084 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009085
Pavel Begunkov87094462021-04-11 01:46:36 +01009086 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07009087 ret = io_copy_iov(ctx, &iov, arg, i);
9088 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009089 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009090 ret = io_buffer_validate(&iov);
9091 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009092 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009093 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009094 ret = -EINVAL;
9095 break;
9096 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009097
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009098 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9099 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009100 if (ret)
9101 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009102 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009103
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009104 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009105
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009106 ctx->buf_data = data;
9107 if (ret)
9108 __io_sqe_buffers_unregister(ctx);
9109 else
9110 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009111 return ret;
9112}
9113
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009114static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9115 struct io_uring_rsrc_update2 *up,
9116 unsigned int nr_args)
9117{
9118 u64 __user *tags = u64_to_user_ptr(up->tags);
9119 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009120 struct page *last_hpage = NULL;
9121 bool needs_switch = false;
9122 __u32 done;
9123 int i, err;
9124
9125 if (!ctx->buf_data)
9126 return -ENXIO;
9127 if (up->offset + nr_args > ctx->nr_user_bufs)
9128 return -EINVAL;
9129
9130 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009131 struct io_mapped_ubuf *imu;
9132 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009133 u64 tag = 0;
9134
9135 err = io_copy_iov(ctx, &iov, iovs, done);
9136 if (err)
9137 break;
9138 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9139 err = -EFAULT;
9140 break;
9141 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009142 err = io_buffer_validate(&iov);
9143 if (err)
9144 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009145 if (!iov.iov_base && tag) {
9146 err = -EINVAL;
9147 break;
9148 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009149 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9150 if (err)
9151 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009152
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009153 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009154 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009155 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9156 ctx->rsrc_node, ctx->user_bufs[i]);
9157 if (unlikely(err)) {
9158 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009159 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009160 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009161 ctx->user_bufs[i] = NULL;
9162 needs_switch = true;
9163 }
9164
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009165 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009166 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009167 }
9168
9169 if (needs_switch)
9170 io_rsrc_node_switch(ctx, ctx->buf_data);
9171 return done ? done : err;
9172}
9173
Jens Axboe9b402842019-04-11 11:45:41 -06009174static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9175{
9176 __s32 __user *fds = arg;
9177 int fd;
9178
9179 if (ctx->cq_ev_fd)
9180 return -EBUSY;
9181
9182 if (copy_from_user(&fd, fds, sizeof(*fds)))
9183 return -EFAULT;
9184
9185 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9186 if (IS_ERR(ctx->cq_ev_fd)) {
9187 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009188
Jens Axboe9b402842019-04-11 11:45:41 -06009189 ctx->cq_ev_fd = NULL;
9190 return ret;
9191 }
9192
9193 return 0;
9194}
9195
9196static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9197{
9198 if (ctx->cq_ev_fd) {
9199 eventfd_ctx_put(ctx->cq_ev_fd);
9200 ctx->cq_ev_fd = NULL;
9201 return 0;
9202 }
9203
9204 return -ENXIO;
9205}
9206
Jens Axboe5a2e7452020-02-23 16:23:11 -07009207static void io_destroy_buffers(struct io_ring_ctx *ctx)
9208{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009209 struct io_buffer *buf;
9210 unsigned long index;
9211
Jens Axboe8bab4c02021-09-24 07:12:27 -06009212 xa_for_each(&ctx->io_buffers, index, buf) {
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009213 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009214 cond_resched();
9215 }
Jens Axboe5a2e7452020-02-23 16:23:11 -07009216}
9217
Pavel Begunkov72558342021-08-09 20:18:09 +01009218static void io_req_cache_free(struct list_head *list)
Jens Axboe1b4c3512021-02-10 00:03:19 +00009219{
Jens Axboe68e68ee2021-02-13 09:00:02 -07009220 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00009221
Pavel Begunkovbb943b82021-08-09 20:18:10 +01009222 list_for_each_entry_safe(req, nxt, list, inflight_entry) {
9223 list_del(&req->inflight_entry);
Jens Axboe1b4c3512021-02-10 00:03:19 +00009224 kmem_cache_free(req_cachep, req);
9225 }
9226}
9227
Jens Axboe4010fec2021-02-27 15:04:18 -07009228static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009229{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009230 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009231
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009232 mutex_lock(&ctx->uring_lock);
9233
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009234 if (state->free_reqs) {
9235 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
9236 state->free_reqs = 0;
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00009237 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009238
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009239 io_flush_cached_locked_reqs(ctx, state);
9240 io_req_cache_free(&state->free_list);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009241 mutex_unlock(&ctx->uring_lock);
9242}
9243
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009244static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009245{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009246 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009247 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009248}
9249
Jens Axboe2b188cc2019-01-07 10:46:33 -07009250static void io_ring_ctx_free(struct io_ring_ctx *ctx)
9251{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009252 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009253
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009254 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009255 mmdrop(ctx->mm_account);
9256 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009257 }
Jens Axboedef596e2019-01-09 08:59:42 -07009258
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009259 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9260 io_wait_rsrc_data(ctx->buf_data);
9261 io_wait_rsrc_data(ctx->file_data);
9262
Hao Xu8bad28d2021-02-19 17:19:36 +08009263 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009264 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009265 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009266 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009267 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009268 if (ctx->rings)
9269 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009270 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009271 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009272 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009273 if (ctx->sq_creds)
9274 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009275
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009276 /* there are no registered resources left, nobody uses it */
9277 if (ctx->rsrc_node)
9278 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009279 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009280 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009281 flush_delayed_work(&ctx->rsrc_put_work);
9282
9283 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9284 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009285
9286#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07009287 if (ctx->ring_sock) {
9288 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009289 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07009290 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009291#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009292 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009293
Hristo Venev75b28af2019-08-26 17:23:46 +00009294 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009295 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009296
9297 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009298 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009299 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009300 if (ctx->hash_map)
9301 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009302 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009303 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009304 kfree(ctx);
9305}
9306
9307static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9308{
9309 struct io_ring_ctx *ctx = file->private_data;
9310 __poll_t mask = 0;
9311
Pavel Begunkov311997b2021-06-14 23:37:28 +01009312 poll_wait(file, &ctx->poll_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009313 /*
9314 * synchronizes with barrier from wq_has_sleeper call in
9315 * io_commit_cqring
9316 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009317 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009318 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009319 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009320
9321 /*
9322 * Don't flush cqring overflow list here, just do a simple check.
9323 * Otherwise there could possible be ABBA deadlock:
9324 * CPU0 CPU1
9325 * ---- ----
9326 * lock(&ctx->uring_lock);
9327 * lock(&ep->mtx);
9328 * lock(&ctx->uring_lock);
9329 * lock(&ep->mtx);
9330 *
9331 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9332 * pushs them to do the flush.
9333 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009334 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009335 mask |= EPOLLIN | EPOLLRDNORM;
9336
9337 return mask;
9338}
9339
Yejune Deng0bead8c2020-12-24 11:02:20 +08009340static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009341{
Jens Axboe4379bf82021-02-15 13:40:22 -07009342 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009343
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009344 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009345 if (creds) {
9346 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009347 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009348 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009349
9350 return -EINVAL;
9351}
9352
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009353struct io_tctx_exit {
9354 struct callback_head task_work;
9355 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009356 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009357};
9358
9359static void io_tctx_exit_cb(struct callback_head *cb)
9360{
9361 struct io_uring_task *tctx = current->io_uring;
9362 struct io_tctx_exit *work;
9363
9364 work = container_of(cb, struct io_tctx_exit, task_work);
9365 /*
9366 * When @in_idle, we're in cancellation and it's racy to remove the
9367 * node. It'll be removed by the end of cancellation, just ignore it.
9368 */
9369 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009370 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009371 complete(&work->completion);
9372}
9373
Pavel Begunkov28090c12021-04-25 23:34:45 +01009374static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
9375{
9376 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9377
9378 return req->ctx == data;
9379}
9380
Jens Axboe85faa7b2020-04-09 18:14:00 -06009381static void io_ring_exit_work(struct work_struct *work)
9382{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009383 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009384 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009385 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009386 struct io_tctx_exit exit;
9387 struct io_tctx_node *node;
9388 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009389
Jens Axboe56952e92020-06-17 15:00:04 -06009390 /*
9391 * If we're doing polled IO and end up having requests being
9392 * submitted async (out-of-line), then completions can come in while
9393 * we're waiting for refs to drop. We need to reap these manually,
9394 * as nobody else will be looking for them.
9395 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009396 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009397 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009398 if (ctx->sq_data) {
9399 struct io_sq_data *sqd = ctx->sq_data;
9400 struct task_struct *tsk;
9401
9402 io_sq_thread_park(sqd);
9403 tsk = sqd->thread;
9404 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9405 io_wq_cancel_cb(tsk->io_uring->io_wq,
9406 io_cancel_ctx_cb, ctx, true);
9407 io_sq_thread_unpark(sqd);
9408 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009409
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009410 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9411 /* there is little hope left, don't run it too often */
9412 interval = HZ * 60;
9413 }
9414 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009415
Pavel Begunkov7f006512021-04-14 13:38:34 +01009416 init_completion(&exit.completion);
9417 init_task_work(&exit.task_work, io_tctx_exit_cb);
9418 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009419 /*
9420 * Some may use context even when all refs and requests have been put,
9421 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009422 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009423 * this lock/unlock section also waits them to finish.
9424 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009425 mutex_lock(&ctx->uring_lock);
9426 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009427 WARN_ON_ONCE(time_after(jiffies, timeout));
9428
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009429 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9430 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009431 /* don't spin on a single task if cancellation failed */
9432 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009433 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9434 if (WARN_ON_ONCE(ret))
9435 continue;
9436 wake_up_process(node->task);
9437
9438 mutex_unlock(&ctx->uring_lock);
9439 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009440 mutex_lock(&ctx->uring_lock);
9441 }
9442 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009443 spin_lock(&ctx->completion_lock);
9444 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009445
Jens Axboe85faa7b2020-04-09 18:14:00 -06009446 io_ring_ctx_free(ctx);
9447}
9448
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009449/* Returns true if we found and killed one or more timeouts */
9450static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009451 bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009452{
9453 struct io_kiocb *req, *tmp;
9454 int canceled = 0;
9455
Jens Axboe79ebeae2021-08-10 15:18:27 -06009456 spin_lock(&ctx->completion_lock);
9457 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009458 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009459 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009460 io_kill_timeout(req, -ECANCELED);
9461 canceled++;
9462 }
9463 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009464 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009465 if (canceled != 0)
9466 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009467 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009468 if (canceled != 0)
9469 io_cqring_ev_posted(ctx);
9470 return canceled != 0;
9471}
9472
Jens Axboe2b188cc2019-01-07 10:46:33 -07009473static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
9474{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009475 unsigned long index;
9476 struct creds *creds;
9477
Jens Axboe2b188cc2019-01-07 10:46:33 -07009478 mutex_lock(&ctx->uring_lock);
9479 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009480 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009481 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009482 xa_for_each(&ctx->personalities, index, creds)
9483 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009484 mutex_unlock(&ctx->uring_lock);
9485
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009486 io_kill_timeouts(ctx, NULL, true);
9487 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009488
Jens Axboe15dff282019-11-13 09:09:23 -07009489 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009490 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009491
Jens Axboe85faa7b2020-04-09 18:14:00 -06009492 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009493 /*
9494 * Use system_unbound_wq to avoid spawning tons of event kworkers
9495 * if we're exiting a ton of rings at the same time. It just adds
9496 * noise and overhead, there's no discernable change in runtime
9497 * over using system_wq.
9498 */
9499 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009500}
9501
9502static int io_uring_release(struct inode *inode, struct file *file)
9503{
9504 struct io_ring_ctx *ctx = file->private_data;
9505
9506 file->private_data = NULL;
9507 io_ring_ctx_wait_and_kill(ctx);
9508 return 0;
9509}
9510
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009511struct io_task_cancel {
9512 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009513 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009514};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009515
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009516static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009517{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009518 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009519 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009520 bool ret;
9521
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009522 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009523 struct io_ring_ctx *ctx = req->ctx;
9524
9525 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009526 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009527 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009528 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009529 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009530 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009531 }
9532 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009533}
9534
Pavel Begunkove1915f72021-03-11 23:29:35 +00009535static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009536 struct task_struct *task, bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009537{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009538 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009539 LIST_HEAD(list);
9540
Jens Axboe79ebeae2021-08-10 15:18:27 -06009541 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009542 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009543 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009544 list_cut_position(&list, &ctx->defer_list, &de->list);
9545 break;
9546 }
9547 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009548 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009549 if (list_empty(&list))
9550 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009551
9552 while (!list_empty(&list)) {
9553 de = list_first_entry(&list, struct io_defer_entry, list);
9554 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009555 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009556 kfree(de);
9557 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009558 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009559}
9560
Pavel Begunkov1b007642021-03-06 11:02:17 +00009561static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
9562{
9563 struct io_tctx_node *node;
9564 enum io_wq_cancel cret;
9565 bool ret = false;
9566
9567 mutex_lock(&ctx->uring_lock);
9568 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9569 struct io_uring_task *tctx = node->task->io_uring;
9570
9571 /*
9572 * io_wq will stay alive while we hold uring_lock, because it's
9573 * killed after ctx nodes, which requires to take the lock.
9574 */
9575 if (!tctx || !tctx->io_wq)
9576 continue;
9577 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9578 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9579 }
9580 mutex_unlock(&ctx->uring_lock);
9581
9582 return ret;
9583}
9584
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009585static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9586 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009587 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009588{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009589 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009590 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009591
9592 while (1) {
9593 enum io_wq_cancel cret;
9594 bool ret = false;
9595
Pavel Begunkov1b007642021-03-06 11:02:17 +00009596 if (!task) {
9597 ret |= io_uring_try_cancel_iowq(ctx);
9598 } else if (tctx && tctx->io_wq) {
9599 /*
9600 * Cancels requests of all rings, not only @ctx, but
9601 * it's fine as the task is in exit/exec.
9602 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009603 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009604 &cancel, true);
9605 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9606 }
9607
9608 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009609 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009610 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009611 while (!list_empty_careful(&ctx->iopoll_list)) {
9612 io_iopoll_try_reap_events(ctx);
9613 ret = true;
9614 }
9615 }
9616
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009617 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9618 ret |= io_poll_remove_all(ctx, task, cancel_all);
9619 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009620 if (task)
9621 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009622 if (!ret)
9623 break;
9624 cond_resched();
9625 }
9626}
9627
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009628static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009629{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009630 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009631 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009632 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009633
9634 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009635 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009636 if (unlikely(ret))
9637 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009638 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009639 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009640 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9641 node = kmalloc(sizeof(*node), GFP_KERNEL);
9642 if (!node)
9643 return -ENOMEM;
9644 node->ctx = ctx;
9645 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009646
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009647 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9648 node, GFP_KERNEL));
9649 if (ret) {
9650 kfree(node);
9651 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009652 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009653
9654 mutex_lock(&ctx->uring_lock);
9655 list_add(&node->ctx_node, &ctx->tctx_list);
9656 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009657 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009658 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009659 return 0;
9660}
9661
9662/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009663 * Note that this task has used io_uring. We use it for cancelation purposes.
9664 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009665static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009666{
9667 struct io_uring_task *tctx = current->io_uring;
9668
9669 if (likely(tctx && tctx->last == ctx))
9670 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009671 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009672}
9673
9674/*
Jens Axboe0f212202020-09-13 13:09:39 -06009675 * Remove this io_uring_file -> task mapping.
9676 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009677static void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009678{
9679 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009680 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009681
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009682 if (!tctx)
9683 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009684 node = xa_erase(&tctx->xa, index);
9685 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009686 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009687
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009688 WARN_ON_ONCE(current != node->task);
9689 WARN_ON_ONCE(list_empty(&node->ctx_node));
9690
9691 mutex_lock(&node->ctx->uring_lock);
9692 list_del(&node->ctx_node);
9693 mutex_unlock(&node->ctx->uring_lock);
9694
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009695 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009696 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009697 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009698}
9699
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009700static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009701{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009702 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009703 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009704 unsigned long index;
9705
Jens Axboe8bab4c02021-09-24 07:12:27 -06009706 xa_for_each(&tctx->xa, index, node) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009707 io_uring_del_tctx_node(index);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009708 cond_resched();
9709 }
Marco Elverb16ef422021-05-27 11:25:48 +02009710 if (wq) {
9711 /*
9712 * Must be after io_uring_del_task_file() (removes nodes under
9713 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9714 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009715 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009716 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009717 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009718}
9719
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009720static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009721{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009722 if (tracked)
9723 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009724 return percpu_counter_sum(&tctx->inflight);
9725}
9726
Pavel Begunkov09899b12021-06-14 02:36:22 +01009727static void io_uring_drop_tctx_refs(struct task_struct *task)
9728{
9729 struct io_uring_task *tctx = task->io_uring;
9730 unsigned int refs = tctx->cached_refs;
9731
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009732 if (refs) {
9733 tctx->cached_refs = 0;
9734 percpu_counter_sub(&tctx->inflight, refs);
9735 put_task_struct_many(task, refs);
9736 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009737}
9738
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009739/*
9740 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9741 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9742 */
9743static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009744{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009745 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009746 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009747 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009748 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009749
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009750 WARN_ON_ONCE(sqd && sqd->thread != current);
9751
Palash Oswal6d042ff2021-04-27 18:21:49 +05309752 if (!current->io_uring)
9753 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009754 if (tctx->io_wq)
9755 io_wq_exit_start(tctx->io_wq);
9756
Jens Axboefdaf0832020-10-30 09:37:30 -06009757 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009758 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009759 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009760 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009761 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009762 if (!inflight)
9763 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009764
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009765 if (!sqd) {
9766 struct io_tctx_node *node;
9767 unsigned long index;
9768
9769 xa_for_each(&tctx->xa, index, node) {
9770 /* sqpoll task will cancel all its requests */
9771 if (node->ctx->sq_data)
9772 continue;
9773 io_uring_try_cancel_requests(node->ctx, current,
9774 cancel_all);
9775 }
9776 } else {
9777 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9778 io_uring_try_cancel_requests(ctx, current,
9779 cancel_all);
9780 }
9781
9782 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009783 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009784 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009785 * If we've seen completions, retry without waiting. This
9786 * avoids a race where a completion comes in before we did
9787 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009788 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009789 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009790 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009791 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009792 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009793 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009794
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009795 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009796 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009797 /* for exec all current's requests should be gone, kill tctx */
9798 __io_uring_free(current);
9799 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009800}
9801
Hao Xuf552a272021-08-12 12:14:35 +08009802void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009803{
Hao Xuf552a272021-08-12 12:14:35 +08009804 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009805}
9806
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009807static void *io_uring_validate_mmap_request(struct file *file,
9808 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009809{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009810 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009811 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009812 struct page *page;
9813 void *ptr;
9814
9815 switch (offset) {
9816 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009817 case IORING_OFF_CQ_RING:
9818 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009819 break;
9820 case IORING_OFF_SQES:
9821 ptr = ctx->sq_sqes;
9822 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009823 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009824 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009825 }
9826
9827 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009828 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009829 return ERR_PTR(-EINVAL);
9830
9831 return ptr;
9832}
9833
9834#ifdef CONFIG_MMU
9835
9836static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9837{
9838 size_t sz = vma->vm_end - vma->vm_start;
9839 unsigned long pfn;
9840 void *ptr;
9841
9842 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9843 if (IS_ERR(ptr))
9844 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009845
9846 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9847 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9848}
9849
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009850#else /* !CONFIG_MMU */
9851
9852static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9853{
9854 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9855}
9856
9857static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9858{
9859 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9860}
9861
9862static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9863 unsigned long addr, unsigned long len,
9864 unsigned long pgoff, unsigned long flags)
9865{
9866 void *ptr;
9867
9868 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9869 if (IS_ERR(ptr))
9870 return PTR_ERR(ptr);
9871
9872 return (unsigned long) ptr;
9873}
9874
9875#endif /* !CONFIG_MMU */
9876
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009877static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009878{
9879 DEFINE_WAIT(wait);
9880
9881 do {
9882 if (!io_sqring_full(ctx))
9883 break;
Jens Axboe90554202020-09-03 12:12:41 -06009884 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9885
9886 if (!io_sqring_full(ctx))
9887 break;
Jens Axboe90554202020-09-03 12:12:41 -06009888 schedule();
9889 } while (!signal_pending(current));
9890
9891 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009892 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009893}
9894
Hao Xuc73ebb62020-11-03 10:54:37 +08009895static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9896 struct __kernel_timespec __user **ts,
9897 const sigset_t __user **sig)
9898{
9899 struct io_uring_getevents_arg arg;
9900
9901 /*
9902 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9903 * is just a pointer to the sigset_t.
9904 */
9905 if (!(flags & IORING_ENTER_EXT_ARG)) {
9906 *sig = (const sigset_t __user *) argp;
9907 *ts = NULL;
9908 return 0;
9909 }
9910
9911 /*
9912 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9913 * timespec and sigset_t pointers if good.
9914 */
9915 if (*argsz != sizeof(arg))
9916 return -EINVAL;
9917 if (copy_from_user(&arg, argp, sizeof(arg)))
9918 return -EFAULT;
9919 *sig = u64_to_user_ptr(arg.sigmask);
9920 *argsz = arg.sigmask_sz;
9921 *ts = u64_to_user_ptr(arg.ts);
9922 return 0;
9923}
9924
Jens Axboe2b188cc2019-01-07 10:46:33 -07009925SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009926 u32, min_complete, u32, flags, const void __user *, argp,
9927 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009928{
9929 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009930 int submitted = 0;
9931 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009932 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009933
Jens Axboe4c6e2772020-07-01 11:29:10 -06009934 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009935
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009936 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9937 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009938 return -EINVAL;
9939
9940 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009941 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009942 return -EBADF;
9943
9944 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009945 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009946 goto out_fput;
9947
9948 ret = -ENXIO;
9949 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009950 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009951 goto out_fput;
9952
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009953 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009954 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009955 goto out;
9956
Jens Axboe6c271ce2019-01-10 11:22:30 -07009957 /*
9958 * For SQ polling, the thread will do all submissions and completions.
9959 * Just return the requested submit count, and wake the thread if
9960 * we were asked to.
9961 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009962 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009963 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +01009964 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009965
Jens Axboe21f96522021-08-14 09:04:40 -06009966 if (unlikely(ctx->sq_data->thread == NULL)) {
9967 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009968 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -06009969 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009970 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009971 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009972 if (flags & IORING_ENTER_SQ_WAIT) {
9973 ret = io_sqpoll_wait_sq(ctx);
9974 if (ret)
9975 goto out;
9976 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009977 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009978 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009979 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009980 if (unlikely(ret))
9981 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009982 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009983 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009984 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009985
9986 if (submitted != to_submit)
9987 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009988 }
9989 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009990 const sigset_t __user *sig;
9991 struct __kernel_timespec __user *ts;
9992
9993 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9994 if (unlikely(ret))
9995 goto out;
9996
Jens Axboe2b188cc2019-01-07 10:46:33 -07009997 min_complete = min(min_complete, ctx->cq_entries);
9998
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009999 /*
10000 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
10001 * space applications don't need to do io completion events
10002 * polling again, they can rely on io_sq_thread to do polling
10003 * work, which can reduce cpu usage and uring_lock contention.
10004 */
10005 if (ctx->flags & IORING_SETUP_IOPOLL &&
10006 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +030010007 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -070010008 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +080010009 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -070010010 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010011 }
10012
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010013out:
Pavel Begunkov6805b322019-10-08 02:18:42 +030010014 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010015out_fput:
10016 fdput(f);
10017 return submitted ? submitted : ret;
10018}
10019
Tobias Klauserbebdb652020-02-26 18:38:32 +010010020#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010021static int io_uring_show_cred(struct seq_file *m, unsigned int id,
10022 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -070010023{
Jens Axboe87ce9552020-01-30 08:25:34 -070010024 struct user_namespace *uns = seq_user_ns(m);
10025 struct group_info *gi;
10026 kernel_cap_t cap;
10027 unsigned __capi;
10028 int g;
10029
10030 seq_printf(m, "%5d\n", id);
10031 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
10032 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
10033 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
10034 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
10035 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
10036 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
10037 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
10038 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
10039 seq_puts(m, "\n\tGroups:\t");
10040 gi = cred->group_info;
10041 for (g = 0; g < gi->ngroups; g++) {
10042 seq_put_decimal_ull(m, g ? " " : "",
10043 from_kgid_munged(uns, gi->gid[g]));
10044 }
10045 seq_puts(m, "\n\tCapEff:\t");
10046 cap = cred->cap_effective;
10047 CAP_FOR_EACH_U32(__capi)
10048 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
10049 seq_putc(m, '\n');
10050 return 0;
10051}
10052
10053static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
10054{
Joseph Qidbbe9c62020-09-29 09:01:22 -060010055 struct io_sq_data *sq = NULL;
Hao Xu83f84352021-09-13 21:08:54 +080010056 struct io_overflow_cqe *ocqe;
10057 struct io_rings *r = ctx->rings;
10058 unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
10059 unsigned int cached_sq_head = ctx->cached_sq_head;
10060 unsigned int cached_cq_tail = ctx->cached_cq_tail;
10061 unsigned int sq_head = READ_ONCE(r->sq.head);
10062 unsigned int sq_tail = READ_ONCE(r->sq.tail);
10063 unsigned int cq_head = READ_ONCE(r->cq.head);
10064 unsigned int cq_tail = READ_ONCE(r->cq.tail);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010065 bool has_lock;
Hao Xu83f84352021-09-13 21:08:54 +080010066 unsigned int i;
10067
10068 /*
10069 * we may get imprecise sqe and cqe info if uring is actively running
10070 * since we get cached_sq_head and cached_cq_tail without uring_lock
10071 * and sq_tail and cq_head are changed by userspace. But it's ok since
10072 * we usually use these info when it is stuck.
10073 */
10074 seq_printf(m, "SqHead:\t%u\n", sq_head & sq_mask);
10075 seq_printf(m, "SqTail:\t%u\n", sq_tail & sq_mask);
10076 seq_printf(m, "CachedSqHead:\t%u\n", cached_sq_head & sq_mask);
10077 seq_printf(m, "CqHead:\t%u\n", cq_head & cq_mask);
10078 seq_printf(m, "CqTail:\t%u\n", cq_tail & cq_mask);
10079 seq_printf(m, "CachedCqTail:\t%u\n", cached_cq_tail & cq_mask);
10080 seq_printf(m, "SQEs:\t%u\n", sq_tail - cached_sq_head);
10081 for (i = cached_sq_head; i < sq_tail; i++) {
10082 unsigned int sq_idx = READ_ONCE(ctx->sq_array[i & sq_mask]);
10083
10084 if (likely(sq_idx <= sq_mask)) {
10085 struct io_uring_sqe *sqe = &ctx->sq_sqes[sq_idx];
10086
10087 seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n",
10088 sq_idx, sqe->opcode, sqe->fd, sqe->flags, sqe->user_data);
10089 }
10090 }
10091 seq_printf(m, "CQEs:\t%u\n", cached_cq_tail - cq_head);
10092 for (i = cq_head; i < cached_cq_tail; i++) {
10093 struct io_uring_cqe *cqe = &r->cqes[i & cq_mask];
10094
10095 seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n",
10096 i & cq_mask, cqe->user_data, cqe->res, cqe->flags);
10097 }
Jens Axboe87ce9552020-01-30 08:25:34 -070010098
Jens Axboefad8e0d2020-09-28 08:57:48 -060010099 /*
10100 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10101 * since fdinfo case grabs it in the opposite direction of normal use
10102 * cases. If we fail to get the lock, we just don't iterate any
10103 * structures that could be going away outside the io_uring mutex.
10104 */
10105 has_lock = mutex_trylock(&ctx->uring_lock);
10106
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010107 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -060010108 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010109 if (!sq->thread)
10110 sq = NULL;
10111 }
Joseph Qidbbe9c62020-09-29 09:01:22 -060010112
10113 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10114 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -070010115 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010116 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -070010117 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -070010118
Jens Axboe87ce9552020-01-30 08:25:34 -070010119 if (f)
10120 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10121 else
10122 seq_printf(m, "%5u: <none>\n", i);
10123 }
10124 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010125 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +010010126 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +010010127 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -070010128
Pavel Begunkov4751f532021-04-01 15:43:55 +010010129 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -070010130 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010131 if (has_lock && !xa_empty(&ctx->personalities)) {
10132 unsigned long index;
10133 const struct cred *cred;
10134
Jens Axboe87ce9552020-01-30 08:25:34 -070010135 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010136 xa_for_each(&ctx->personalities, index, cred)
10137 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010138 }
Hao Xu83f84352021-09-13 21:08:54 +080010139 if (has_lock)
10140 mutex_unlock(&ctx->uring_lock);
10141
10142 seq_puts(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010143 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010144 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10145 struct hlist_head *list = &ctx->cancel_hash[i];
10146 struct io_kiocb *req;
10147
10148 hlist_for_each_entry(req, list, hash_node)
10149 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10150 req->task->task_works != NULL);
10151 }
Hao Xu83f84352021-09-13 21:08:54 +080010152
10153 seq_puts(m, "CqOverflowList:\n");
10154 list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
10155 struct io_uring_cqe *cqe = &ocqe->cqe;
10156
10157 seq_printf(m, " user_data=%llu, res=%d, flags=%x\n",
10158 cqe->user_data, cqe->res, cqe->flags);
10159
10160 }
10161
Jens Axboe79ebeae2021-08-10 15:18:27 -060010162 spin_unlock(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010163}
10164
10165static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
10166{
10167 struct io_ring_ctx *ctx = f->private_data;
10168
10169 if (percpu_ref_tryget(&ctx->refs)) {
10170 __io_uring_show_fdinfo(ctx, m);
10171 percpu_ref_put(&ctx->refs);
10172 }
10173}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010174#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010175
Jens Axboe2b188cc2019-01-07 10:46:33 -070010176static const struct file_operations io_uring_fops = {
10177 .release = io_uring_release,
10178 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010179#ifndef CONFIG_MMU
10180 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10181 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10182#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010183 .poll = io_uring_poll,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010184#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010185 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010186#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010187};
10188
10189static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10190 struct io_uring_params *p)
10191{
Hristo Venev75b28af2019-08-26 17:23:46 +000010192 struct io_rings *rings;
10193 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010194
Jens Axboebd740482020-08-05 12:58:23 -060010195 /* make sure these are sane, as we already accounted them */
10196 ctx->sq_entries = p->sq_entries;
10197 ctx->cq_entries = p->cq_entries;
10198
Hristo Venev75b28af2019-08-26 17:23:46 +000010199 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10200 if (size == SIZE_MAX)
10201 return -EOVERFLOW;
10202
10203 rings = io_mem_alloc(size);
10204 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010205 return -ENOMEM;
10206
Hristo Venev75b28af2019-08-26 17:23:46 +000010207 ctx->rings = rings;
10208 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10209 rings->sq_ring_mask = p->sq_entries - 1;
10210 rings->cq_ring_mask = p->cq_entries - 1;
10211 rings->sq_ring_entries = p->sq_entries;
10212 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010213
10214 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010215 if (size == SIZE_MAX) {
10216 io_mem_free(ctx->rings);
10217 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010218 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010219 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010220
10221 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010222 if (!ctx->sq_sqes) {
10223 io_mem_free(ctx->rings);
10224 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010225 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010226 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010227
Jens Axboe2b188cc2019-01-07 10:46:33 -070010228 return 0;
10229}
10230
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010231static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10232{
10233 int ret, fd;
10234
10235 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10236 if (fd < 0)
10237 return fd;
10238
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010239 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010240 if (ret) {
10241 put_unused_fd(fd);
10242 return ret;
10243 }
10244 fd_install(fd, file);
10245 return fd;
10246}
10247
Jens Axboe2b188cc2019-01-07 10:46:33 -070010248/*
10249 * Allocate an anonymous fd, this is what constitutes the application
10250 * visible backing of an io_uring instance. The application mmaps this
10251 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10252 * we have to tie this fd to a socket for file garbage collection purposes.
10253 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010254static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010255{
10256 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010257#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010258 int ret;
10259
Jens Axboe2b188cc2019-01-07 10:46:33 -070010260 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10261 &ctx->ring_sock);
10262 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010263 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010264#endif
10265
Jens Axboe2b188cc2019-01-07 10:46:33 -070010266 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
10267 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010268#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010269 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010270 sock_release(ctx->ring_sock);
10271 ctx->ring_sock = NULL;
10272 } else {
10273 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010274 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010275#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010276 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010277}
10278
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010279static int io_uring_create(unsigned entries, struct io_uring_params *p,
10280 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010281{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010282 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010283 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010284 int ret;
10285
Jens Axboe8110c1a2019-12-28 15:39:54 -070010286 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010287 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010288 if (entries > IORING_MAX_ENTRIES) {
10289 if (!(p->flags & IORING_SETUP_CLAMP))
10290 return -EINVAL;
10291 entries = IORING_MAX_ENTRIES;
10292 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010293
10294 /*
10295 * Use twice as many entries for the CQ ring. It's possible for the
10296 * application to drive a higher depth than the size of the SQ ring,
10297 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010298 * some flexibility in overcommitting a bit. If the application has
10299 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10300 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010301 */
10302 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010303 if (p->flags & IORING_SETUP_CQSIZE) {
10304 /*
10305 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10306 * to a power-of-two, if it isn't already. We do NOT impose
10307 * any cq vs sq ring sizing.
10308 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010309 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010310 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010311 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10312 if (!(p->flags & IORING_SETUP_CLAMP))
10313 return -EINVAL;
10314 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10315 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010316 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10317 if (p->cq_entries < p->sq_entries)
10318 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010319 } else {
10320 p->cq_entries = 2 * p->sq_entries;
10321 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010322
Jens Axboe2b188cc2019-01-07 10:46:33 -070010323 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010324 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010325 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010326 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010327 if (!capable(CAP_IPC_LOCK))
10328 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010329
10330 /*
10331 * This is just grabbed for accounting purposes. When a process exits,
10332 * the mm is exited and dropped before the files, hence we need to hang
10333 * on to this mm purely for the purposes of being able to unaccount
10334 * memory (locked/pinned vm). It's not used for anything else.
10335 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010336 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010337 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010338
Jens Axboe2b188cc2019-01-07 10:46:33 -070010339 ret = io_allocate_scq_urings(ctx, p);
10340 if (ret)
10341 goto err;
10342
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010343 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010344 if (ret)
10345 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010346 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010347 ret = io_rsrc_node_switch_start(ctx);
10348 if (ret)
10349 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010350 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010351
Jens Axboe2b188cc2019-01-07 10:46:33 -070010352 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010353 p->sq_off.head = offsetof(struct io_rings, sq.head);
10354 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10355 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10356 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10357 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10358 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10359 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010360
10361 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010362 p->cq_off.head = offsetof(struct io_rings, cq.head);
10363 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10364 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10365 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10366 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10367 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010368 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010369
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010370 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10371 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010372 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010373 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010374 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
10375 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010376
10377 if (copy_to_user(params, p, sizeof(*p))) {
10378 ret = -EFAULT;
10379 goto err;
10380 }
Jens Axboed1719f72020-07-30 13:43:53 -060010381
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010382 file = io_uring_get_file(ctx);
10383 if (IS_ERR(file)) {
10384 ret = PTR_ERR(file);
10385 goto err;
10386 }
10387
Jens Axboed1719f72020-07-30 13:43:53 -060010388 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010389 * Install ring fd as the very last thing, so we don't risk someone
10390 * having closed it before we finish setup
10391 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010392 ret = io_uring_install_fd(ctx, file);
10393 if (ret < 0) {
10394 /* fput will clean it up */
10395 fput(file);
10396 return ret;
10397 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010398
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010399 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010400 return ret;
10401err:
10402 io_ring_ctx_wait_and_kill(ctx);
10403 return ret;
10404}
10405
10406/*
10407 * Sets up an aio uring context, and returns the fd. Applications asks for a
10408 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10409 * params structure passed in.
10410 */
10411static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10412{
10413 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010414 int i;
10415
10416 if (copy_from_user(&p, params, sizeof(p)))
10417 return -EFAULT;
10418 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10419 if (p.resv[i])
10420 return -EINVAL;
10421 }
10422
Jens Axboe6c271ce2019-01-10 11:22:30 -070010423 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010424 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010425 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10426 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010427 return -EINVAL;
10428
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010429 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010430}
10431
10432SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10433 struct io_uring_params __user *, params)
10434{
10435 return io_uring_setup(entries, params);
10436}
10437
Jens Axboe66f4af92020-01-16 15:36:52 -070010438static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
10439{
10440 struct io_uring_probe *p;
10441 size_t size;
10442 int i, ret;
10443
10444 size = struct_size(p, ops, nr_args);
10445 if (size == SIZE_MAX)
10446 return -EOVERFLOW;
10447 p = kzalloc(size, GFP_KERNEL);
10448 if (!p)
10449 return -ENOMEM;
10450
10451 ret = -EFAULT;
10452 if (copy_from_user(p, arg, size))
10453 goto out;
10454 ret = -EINVAL;
10455 if (memchr_inv(p, 0, size))
10456 goto out;
10457
10458 p->last_op = IORING_OP_LAST - 1;
10459 if (nr_args > IORING_OP_LAST)
10460 nr_args = IORING_OP_LAST;
10461
10462 for (i = 0; i < nr_args; i++) {
10463 p->ops[i].op = i;
10464 if (!io_op_defs[i].not_supported)
10465 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10466 }
10467 p->ops_len = i;
10468
10469 ret = 0;
10470 if (copy_to_user(arg, p, size))
10471 ret = -EFAULT;
10472out:
10473 kfree(p);
10474 return ret;
10475}
10476
Jens Axboe071698e2020-01-28 10:04:42 -070010477static int io_register_personality(struct io_ring_ctx *ctx)
10478{
Jens Axboe4379bf82021-02-15 13:40:22 -070010479 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010480 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010481 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010482
Jens Axboe4379bf82021-02-15 13:40:22 -070010483 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010484
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010485 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10486 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010487 if (ret < 0) {
10488 put_cred(creds);
10489 return ret;
10490 }
10491 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010492}
10493
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010494static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
10495 unsigned int nr_args)
10496{
10497 struct io_uring_restriction *res;
10498 size_t size;
10499 int i, ret;
10500
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010501 /* Restrictions allowed only if rings started disabled */
10502 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10503 return -EBADFD;
10504
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010505 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010506 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010507 return -EBUSY;
10508
10509 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10510 return -EINVAL;
10511
10512 size = array_size(nr_args, sizeof(*res));
10513 if (size == SIZE_MAX)
10514 return -EOVERFLOW;
10515
10516 res = memdup_user(arg, size);
10517 if (IS_ERR(res))
10518 return PTR_ERR(res);
10519
10520 ret = 0;
10521
10522 for (i = 0; i < nr_args; i++) {
10523 switch (res[i].opcode) {
10524 case IORING_RESTRICTION_REGISTER_OP:
10525 if (res[i].register_op >= IORING_REGISTER_LAST) {
10526 ret = -EINVAL;
10527 goto out;
10528 }
10529
10530 __set_bit(res[i].register_op,
10531 ctx->restrictions.register_op);
10532 break;
10533 case IORING_RESTRICTION_SQE_OP:
10534 if (res[i].sqe_op >= IORING_OP_LAST) {
10535 ret = -EINVAL;
10536 goto out;
10537 }
10538
10539 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10540 break;
10541 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10542 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10543 break;
10544 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10545 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10546 break;
10547 default:
10548 ret = -EINVAL;
10549 goto out;
10550 }
10551 }
10552
10553out:
10554 /* Reset all restrictions if an error happened */
10555 if (ret != 0)
10556 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10557 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010558 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010559
10560 kfree(res);
10561 return ret;
10562}
10563
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010564static int io_register_enable_rings(struct io_ring_ctx *ctx)
10565{
10566 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10567 return -EBADFD;
10568
10569 if (ctx->restrictions.registered)
10570 ctx->restricted = 1;
10571
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010572 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10573 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10574 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010575 return 0;
10576}
10577
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010578static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010579 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010580 unsigned nr_args)
10581{
10582 __u32 tmp;
10583 int err;
10584
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010585 if (up->resv)
10586 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010587 if (check_add_overflow(up->offset, nr_args, &tmp))
10588 return -EOVERFLOW;
10589 err = io_rsrc_node_switch_start(ctx);
10590 if (err)
10591 return err;
10592
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010593 switch (type) {
10594 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010595 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010596 case IORING_RSRC_BUFFER:
10597 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010598 }
10599 return -EINVAL;
10600}
10601
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010602static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10603 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010604{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010605 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010606
10607 if (!nr_args)
10608 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010609 memset(&up, 0, sizeof(up));
10610 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10611 return -EFAULT;
10612 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10613}
10614
10615static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010616 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010617{
10618 struct io_uring_rsrc_update2 up;
10619
10620 if (size != sizeof(up))
10621 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010622 if (copy_from_user(&up, arg, sizeof(up)))
10623 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010624 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010625 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010626 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010627}
10628
Pavel Begunkov792e3582021-04-25 14:32:21 +010010629static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010630 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010631{
10632 struct io_uring_rsrc_register rr;
10633
10634 /* keep it extendible */
10635 if (size != sizeof(rr))
10636 return -EINVAL;
10637
10638 memset(&rr, 0, sizeof(rr));
10639 if (copy_from_user(&rr, arg, size))
10640 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010641 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010642 return -EINVAL;
10643
Pavel Begunkov992da012021-06-10 16:37:37 +010010644 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010645 case IORING_RSRC_FILE:
10646 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10647 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010648 case IORING_RSRC_BUFFER:
10649 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10650 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010651 }
10652 return -EINVAL;
10653}
10654
Jens Axboefe764212021-06-17 10:19:54 -060010655static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg,
10656 unsigned len)
10657{
10658 struct io_uring_task *tctx = current->io_uring;
10659 cpumask_var_t new_mask;
10660 int ret;
10661
10662 if (!tctx || !tctx->io_wq)
10663 return -EINVAL;
10664
10665 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10666 return -ENOMEM;
10667
10668 cpumask_clear(new_mask);
10669 if (len > cpumask_size())
10670 len = cpumask_size();
10671
10672 if (copy_from_user(new_mask, arg, len)) {
10673 free_cpumask_var(new_mask);
10674 return -EFAULT;
10675 }
10676
10677 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10678 free_cpumask_var(new_mask);
10679 return ret;
10680}
10681
10682static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
10683{
10684 struct io_uring_task *tctx = current->io_uring;
10685
10686 if (!tctx || !tctx->io_wq)
10687 return -EINVAL;
10688
10689 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10690}
10691
Jens Axboe2e480052021-08-27 11:33:19 -060010692static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10693 void __user *arg)
10694{
Jens Axboefa846932021-09-01 14:15:59 -060010695 struct io_uring_task *tctx = NULL;
10696 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010697 __u32 new_count[2];
10698 int i, ret;
10699
Jens Axboe2e480052021-08-27 11:33:19 -060010700 if (copy_from_user(new_count, arg, sizeof(new_count)))
10701 return -EFAULT;
10702 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10703 if (new_count[i] > INT_MAX)
10704 return -EINVAL;
10705
Jens Axboefa846932021-09-01 14:15:59 -060010706 if (ctx->flags & IORING_SETUP_SQPOLL) {
10707 sqd = ctx->sq_data;
10708 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010709 /*
10710 * Observe the correct sqd->lock -> ctx->uring_lock
10711 * ordering. Fine to drop uring_lock here, we hold
10712 * a ref to the ctx.
10713 */
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010714 refcount_inc(&sqd->refs);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010715 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010716 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010717 mutex_lock(&ctx->uring_lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010718 if (sqd->thread)
10719 tctx = sqd->thread->io_uring;
Jens Axboefa846932021-09-01 14:15:59 -060010720 }
10721 } else {
10722 tctx = current->io_uring;
10723 }
10724
10725 ret = -EINVAL;
10726 if (!tctx || !tctx->io_wq)
10727 goto err;
10728
Jens Axboe2e480052021-08-27 11:33:19 -060010729 ret = io_wq_max_workers(tctx->io_wq, new_count);
10730 if (ret)
Jens Axboefa846932021-09-01 14:15:59 -060010731 goto err;
10732
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010733 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010734 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010735 io_put_sq_data(sqd);
10736 }
Jens Axboe2e480052021-08-27 11:33:19 -060010737
10738 if (copy_to_user(arg, new_count, sizeof(new_count)))
10739 return -EFAULT;
10740
10741 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010742err:
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010743 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010744 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010745 io_put_sq_data(sqd);
10746 }
Jens Axboefa846932021-09-01 14:15:59 -060010747 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010748}
10749
Jens Axboe071698e2020-01-28 10:04:42 -070010750static bool io_register_op_must_quiesce(int op)
10751{
10752 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010753 case IORING_REGISTER_BUFFERS:
10754 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010755 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010756 case IORING_UNREGISTER_FILES:
10757 case IORING_REGISTER_FILES_UPDATE:
10758 case IORING_REGISTER_PROBE:
10759 case IORING_REGISTER_PERSONALITY:
10760 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010761 case IORING_REGISTER_FILES2:
10762 case IORING_REGISTER_FILES_UPDATE2:
10763 case IORING_REGISTER_BUFFERS2:
10764 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010765 case IORING_REGISTER_IOWQ_AFF:
10766 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010767 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010768 return false;
10769 default:
10770 return true;
10771 }
10772}
10773
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010774static int io_ctx_quiesce(struct io_ring_ctx *ctx)
10775{
10776 long ret;
10777
10778 percpu_ref_kill(&ctx->refs);
10779
10780 /*
10781 * Drop uring mutex before waiting for references to exit. If another
10782 * thread is currently inside io_uring_enter() it might need to grab the
10783 * uring_lock to make progress. If we hold it here across the drain
10784 * wait, then we can deadlock. It's safe to drop the mutex here, since
10785 * no new references will come in after we've killed the percpu ref.
10786 */
10787 mutex_unlock(&ctx->uring_lock);
10788 do {
10789 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10790 if (!ret)
10791 break;
10792 ret = io_run_task_work_sig();
10793 } while (ret >= 0);
10794 mutex_lock(&ctx->uring_lock);
10795
10796 if (ret)
10797 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10798 return ret;
10799}
10800
Jens Axboeedafcce2019-01-09 09:16:05 -070010801static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10802 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010803 __releases(ctx->uring_lock)
10804 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010805{
10806 int ret;
10807
Jens Axboe35fa71a2019-04-22 10:23:23 -060010808 /*
10809 * We're inside the ring mutex, if the ref is already dying, then
10810 * someone else killed the ctx or is already going through
10811 * io_uring_register().
10812 */
10813 if (percpu_ref_is_dying(&ctx->refs))
10814 return -ENXIO;
10815
Pavel Begunkov75c40212021-04-15 13:07:40 +010010816 if (ctx->restricted) {
10817 if (opcode >= IORING_REGISTER_LAST)
10818 return -EINVAL;
10819 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10820 if (!test_bit(opcode, ctx->restrictions.register_op))
10821 return -EACCES;
10822 }
10823
Jens Axboe071698e2020-01-28 10:04:42 -070010824 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010825 ret = io_ctx_quiesce(ctx);
10826 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010827 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010828 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010829
10830 switch (opcode) {
10831 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010832 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010833 break;
10834 case IORING_UNREGISTER_BUFFERS:
10835 ret = -EINVAL;
10836 if (arg || nr_args)
10837 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010838 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010839 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010840 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010841 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010842 break;
10843 case IORING_UNREGISTER_FILES:
10844 ret = -EINVAL;
10845 if (arg || nr_args)
10846 break;
10847 ret = io_sqe_files_unregister(ctx);
10848 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010849 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010850 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010851 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010852 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010853 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010854 ret = -EINVAL;
10855 if (nr_args != 1)
10856 break;
10857 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010858 if (ret)
10859 break;
10860 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10861 ctx->eventfd_async = 1;
10862 else
10863 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010864 break;
10865 case IORING_UNREGISTER_EVENTFD:
10866 ret = -EINVAL;
10867 if (arg || nr_args)
10868 break;
10869 ret = io_eventfd_unregister(ctx);
10870 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010871 case IORING_REGISTER_PROBE:
10872 ret = -EINVAL;
10873 if (!arg || nr_args > 256)
10874 break;
10875 ret = io_probe(ctx, arg, nr_args);
10876 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010877 case IORING_REGISTER_PERSONALITY:
10878 ret = -EINVAL;
10879 if (arg || nr_args)
10880 break;
10881 ret = io_register_personality(ctx);
10882 break;
10883 case IORING_UNREGISTER_PERSONALITY:
10884 ret = -EINVAL;
10885 if (arg)
10886 break;
10887 ret = io_unregister_personality(ctx, nr_args);
10888 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010889 case IORING_REGISTER_ENABLE_RINGS:
10890 ret = -EINVAL;
10891 if (arg || nr_args)
10892 break;
10893 ret = io_register_enable_rings(ctx);
10894 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010895 case IORING_REGISTER_RESTRICTIONS:
10896 ret = io_register_restrictions(ctx, arg, nr_args);
10897 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010898 case IORING_REGISTER_FILES2:
10899 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010900 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010901 case IORING_REGISTER_FILES_UPDATE2:
10902 ret = io_register_rsrc_update(ctx, arg, nr_args,
10903 IORING_RSRC_FILE);
10904 break;
10905 case IORING_REGISTER_BUFFERS2:
10906 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10907 break;
10908 case IORING_REGISTER_BUFFERS_UPDATE:
10909 ret = io_register_rsrc_update(ctx, arg, nr_args,
10910 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010911 break;
Jens Axboefe764212021-06-17 10:19:54 -060010912 case IORING_REGISTER_IOWQ_AFF:
10913 ret = -EINVAL;
10914 if (!arg || !nr_args)
10915 break;
10916 ret = io_register_iowq_aff(ctx, arg, nr_args);
10917 break;
10918 case IORING_UNREGISTER_IOWQ_AFF:
10919 ret = -EINVAL;
10920 if (arg || nr_args)
10921 break;
10922 ret = io_unregister_iowq_aff(ctx);
10923 break;
Jens Axboe2e480052021-08-27 11:33:19 -060010924 case IORING_REGISTER_IOWQ_MAX_WORKERS:
10925 ret = -EINVAL;
10926 if (!arg || nr_args != 2)
10927 break;
10928 ret = io_register_iowq_max_workers(ctx, arg);
10929 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010930 default:
10931 ret = -EINVAL;
10932 break;
10933 }
10934
Jens Axboe071698e2020-01-28 10:04:42 -070010935 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010936 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010937 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010938 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010939 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010940 return ret;
10941}
10942
10943SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10944 void __user *, arg, unsigned int, nr_args)
10945{
10946 struct io_ring_ctx *ctx;
10947 long ret = -EBADF;
10948 struct fd f;
10949
10950 f = fdget(fd);
10951 if (!f.file)
10952 return -EBADF;
10953
10954 ret = -EOPNOTSUPP;
10955 if (f.file->f_op != &io_uring_fops)
10956 goto out_fput;
10957
10958 ctx = f.file->private_data;
10959
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010960 io_run_task_work();
10961
Jens Axboeedafcce2019-01-09 09:16:05 -070010962 mutex_lock(&ctx->uring_lock);
10963 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10964 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010965 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10966 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010967out_fput:
10968 fdput(f);
10969 return ret;
10970}
10971
Jens Axboe2b188cc2019-01-07 10:46:33 -070010972static int __init io_uring_init(void)
10973{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010974#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10975 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10976 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10977} while (0)
10978
10979#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10980 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10981 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10982 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10983 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10984 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10985 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10986 BUILD_BUG_SQE_ELEM(8, __u64, off);
10987 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10988 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010989 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010990 BUILD_BUG_SQE_ELEM(24, __u32, len);
10991 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10992 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10993 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10994 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010995 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10996 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010997 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10998 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10999 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
11000 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
11001 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
11002 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
11003 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
11004 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011005 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011006 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
11007 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011008 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011009 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011010 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010011011 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011012
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011013 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
11014 sizeof(struct io_uring_rsrc_update));
11015 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
11016 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010011017
11018 /* ->buf_index is u16 */
11019 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
11020
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011021 /* should fit into one byte */
11022 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
Pavel Begunkov68fe2562021-09-15 12:03:38 +010011023 BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
11024 BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011025
Jens Axboed3656342019-12-18 09:50:26 -070011026 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080011027 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011028
Jens Axboe91f245d2021-02-09 13:48:50 -070011029 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
11030 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070011031 return 0;
11032};
11033__initcall(io_uring_init);