blob: b07196b4511c421a81ddda68a6cde64dbbf717f3 [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>
Paul Moore5bd21822021-02-16 19:46:48 -050082#include <linux/audit.h>
Paul Moorecdc14042021-02-01 19:56:49 -050083#include <linux/security.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020085#define CREATE_TRACE_POINTS
86#include <trace/events/io_uring.h>
87
Jens Axboe2b188cc2019-01-07 10:46:33 -070088#include <uapi/linux/io_uring.h>
89
90#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060091#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
Daniel Xu5277dea2019-09-14 14:23:45 -070093#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060094#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Olivier Langlois4ce8ad92021-06-23 11:50:18 -070095#define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
Jens Axboe65e19f52019-10-26 07:20:21 -060096
wangyangbo187f08c2021-08-19 13:56:57 +080097/* only define max */
Pavel Begunkov042b0d82021-08-09 13:04:01 +010098#define IORING_MAX_FIXED_FILES (1U << 15)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020099#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
100 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
wangyangbo187f08c2021-08-19 13:56:57 +0800102#define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100103#define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT)
104#define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
105
Pavel Begunkov489809e2021-05-14 12:06:44 +0100106#define IORING_MAX_REG_BUFFERS (1U << 14)
107
Pavel Begunkov68fe2562021-09-15 12:03:38 +0100108#define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \
109 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
110
111#define SQE_VALID_FLAGS (SQE_COMMON_FLAGS|IOSQE_BUFFER_SELECT|IOSQE_IO_DRAIN)
112
Pavel Begunkovc8543572021-06-17 18:14:04 +0100113#define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
Pavel Begunkovd886e182021-10-04 20:02:56 +0100114 REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \
115 REQ_F_ASYNC_DATA)
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000116
Pavel Begunkov09899b12021-06-14 02:36:22 +0100117#define IO_TCTX_REFS_CACHE_NR (1U << 10)
118
Jens Axboe2b188cc2019-01-07 10:46:33 -0700119struct io_uring {
120 u32 head ____cacheline_aligned_in_smp;
121 u32 tail ____cacheline_aligned_in_smp;
122};
123
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 * This data is shared with the application through the mmap at offsets
126 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 *
128 * The offsets to the member fields are published through struct
129 * io_sqring_offsets when calling io_uring_setup.
130 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000131struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200132 /*
133 * Head and tail offsets into the ring; the offsets need to be
134 * masked to get valid indices.
135 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000136 * The kernel controls head of the sq ring and the tail of the cq ring,
137 * and the application controls tail of the sq ring and the head of the
138 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200139 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200141 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000142 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200143 * ring_entries - 1)
144 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000145 u32 sq_ring_mask, cq_ring_mask;
146 /* Ring sizes (constant, power of 2) */
147 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200148 /*
149 * Number of invalid entries dropped by the kernel due to
150 * invalid index stored in array
151 *
152 * Written by the kernel, shouldn't be modified by the
153 * application (i.e. get number of "new events" by comparing to
154 * cached value).
155 *
156 * After a new SQ head value was read by the application this
157 * counter includes all submissions that were dropped reaching
158 * the new SQ head (and possibly more).
159 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000160 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200161 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200162 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200163 *
164 * Written by the kernel, shouldn't be modified by the
165 * application.
166 *
167 * The application needs a full memory barrier before checking
168 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
169 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000170 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200171 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200172 * Runtime CQ flags
173 *
174 * Written by the application, shouldn't be modified by the
175 * kernel.
176 */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100177 u32 cq_flags;
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200178 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200179 * Number of completion events lost because the queue was full;
180 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800181 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200182 * the completion queue.
183 *
184 * Written by the kernel, shouldn't be modified by the
185 * application (i.e. get number of "new events" by comparing to
186 * cached value).
187 *
188 * As completion events come in out of order this counter is not
189 * ordered with any other data.
190 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000191 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200192 /*
193 * Ring buffer of completion events.
194 *
195 * The kernel writes completion events fresh every time they are
196 * produced, so the application is allowed to modify pending
197 * entries.
198 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000199 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700200};
201
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000202enum io_uring_cmd_flags {
Pavel Begunkov51aac422021-10-14 16:10:17 +0100203 IO_URING_F_COMPLETE_DEFER = 1,
Hao Xu3b44b372021-10-18 21:34:31 +0800204 IO_URING_F_UNLOCKED = 2,
Pavel Begunkov51aac422021-10-14 16:10:17 +0100205 /* int's last bit, sign checks are usually faster than a bit test */
206 IO_URING_F_NONBLOCK = INT_MIN,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000207};
208
Jens Axboeedafcce2019-01-09 09:16:05 -0700209struct io_mapped_ubuf {
210 u64 ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +0100211 u64 ubuf_end;
Jens Axboeedafcce2019-01-09 09:16:05 -0700212 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600213 unsigned long acct_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100214 struct bio_vec bvec[];
Jens Axboeedafcce2019-01-09 09:16:05 -0700215};
216
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000217struct io_ring_ctx;
218
Pavel Begunkov6c2450a2021-02-23 12:40:22 +0000219struct io_overflow_cqe {
220 struct io_uring_cqe cqe;
221 struct list_head list;
222};
223
Pavel Begunkova04b0ac2021-04-01 15:44:04 +0100224struct io_fixed_file {
225 /* file * with additional FFS_* flags */
226 unsigned long file_ptr;
227};
228
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000229struct io_rsrc_put {
230 struct list_head list;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100231 u64 tag;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000232 union {
233 void *rsrc;
234 struct file *file;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +0100235 struct io_mapped_ubuf *buf;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000236 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000237};
238
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100239struct io_file_table {
Pavel Begunkov042b0d82021-08-09 13:04:01 +0100240 struct io_fixed_file *files;
Jens Axboe31b51512019-01-18 22:56:34 -0700241};
242
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100243struct io_rsrc_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800244 struct percpu_ref refs;
245 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000246 struct list_head rsrc_list;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100247 struct io_rsrc_data *rsrc_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600248 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000249 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800250};
251
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100252typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
253
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100254struct io_rsrc_data {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700255 struct io_ring_ctx *ctx;
256
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100257 u64 **tags;
258 unsigned int nr;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100259 rsrc_put_fn *do_put;
Pavel Begunkov3e942492021-04-11 01:46:34 +0100260 atomic_t refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700261 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800262 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700263};
264
Jens Axboe5a2e7452020-02-23 16:23:11 -0700265struct io_buffer {
266 struct list_head list;
267 __u64 addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -0300268 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700269 __u16 bid;
270};
271
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200272struct io_restriction {
273 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
274 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
275 u8 sqe_flags_allowed;
276 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200277 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200278};
279
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700280enum {
281 IO_SQ_THREAD_SHOULD_STOP = 0,
282 IO_SQ_THREAD_SHOULD_PARK,
283};
284
Jens Axboe534ca6d2020-09-02 13:52:19 -0600285struct io_sq_data {
286 refcount_t refs;
Pavel Begunkov9e138a42021-03-14 20:57:12 +0000287 atomic_t park_pending;
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +0000288 struct mutex lock;
Jens Axboe69fb2132020-09-14 11:16:23 -0600289
290 /* ctx's that are using this sqd */
291 struct list_head ctx_list;
Jens Axboe69fb2132020-09-14 11:16:23 -0600292
Jens Axboe534ca6d2020-09-02 13:52:19 -0600293 struct task_struct *thread;
294 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800295
296 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700297 int sq_cpu;
298 pid_t task_pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -0700299 pid_t task_tgid;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700300
301 unsigned long state;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700302 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600303};
304
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000305#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000306#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000307#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000308
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000309struct io_submit_link {
310 struct io_kiocb *head;
311 struct io_kiocb *last;
312};
313
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000314struct io_submit_state {
Pavel Begunkov5a158c62021-10-06 16:06:48 +0100315 /* inline/task_work completion list, under ->uring_lock */
316 struct io_wq_work_node free_list;
317 /* batch completion logic */
318 struct io_wq_work_list compl_reqs;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000319 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000320
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000321 bool plug_started;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +0100322 bool need_plug;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -0600323 unsigned short submit_nr;
Pavel Begunkov5a158c62021-10-06 16:06:48 +0100324 struct blk_plug plug;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000325};
326
Jens Axboe2b188cc2019-01-07 10:46:33 -0700327struct io_ring_ctx {
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100328 /* const or read-mostly hot data */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329 struct {
330 struct percpu_ref refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100332 struct io_rings *rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700333 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800334 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800335 unsigned int drain_next: 1;
336 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200337 unsigned int restricted: 1;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +0100338 unsigned int off_timeout_used: 1;
Pavel Begunkov10c66902021-06-15 16:47:56 +0100339 unsigned int drain_active: 1;
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100340 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100342 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100343 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100344 struct mutex uring_lock;
345
Hristo Venev75b28af2019-08-26 17:23:46 +0000346 /*
347 * Ring buffer of indices into array of io_uring_sqe, which is
348 * mmapped by the application using the IORING_OFF_SQES offset.
349 *
350 * This indirection could e.g. be used to assign fixed
351 * io_uring_sqe entries to operations and only submit them to
352 * the queue when needed.
353 *
354 * The kernel modifies neither the indices array nor the entries
355 * array.
356 */
357 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100358 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700359 unsigned cached_sq_head;
360 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600361 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100362
363 /*
364 * Fixed resources fast path, should be accessed only under
365 * uring_lock, and updated through io_uring_register(2)
366 */
367 struct io_rsrc_node *rsrc_node;
Pavel Begunkovab409402021-10-09 23:14:41 +0100368 int rsrc_cached_refs;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100369 struct io_file_table file_table;
370 unsigned nr_user_files;
371 unsigned nr_user_bufs;
372 struct io_mapped_ubuf **user_bufs;
373
374 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600375 struct list_head timeout_list;
Pavel Begunkovef9dd632021-08-28 19:54:38 -0600376 struct list_head ltimeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700377 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100378 struct xarray io_buffers;
379 struct xarray personalities;
380 u32 pers_next;
381 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700382 } ____cacheline_aligned_in_smp;
383
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100384 /* IRQ completion list, under ->completion_lock */
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +0100385 struct io_wq_work_list locked_free_list;
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100386 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700387
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100388 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600389 struct io_sq_data *sq_data; /* if using sq thread polling */
390
Jens Axboe90554202020-09-03 12:12:41 -0600391 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600392 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000393
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100394 unsigned long check_cq_overflow;
395
Jens Axboe206aefd2019-11-07 18:27:42 -0700396 struct {
397 unsigned cached_cq_tail;
398 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700399 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100400 struct wait_queue_head cq_wait;
401 unsigned cq_extra;
402 atomic_t cq_timeouts;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100403 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700404 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700405
406 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700407 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700408
Jens Axboe89850fc2021-08-10 15:11:51 -0600409 spinlock_t timeout_lock;
410
Jens Axboedef596e2019-01-09 08:59:42 -0700411 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300412 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700413 * io_uring instances that don't use IORING_SETUP_SQPOLL.
414 * For SQPOLL, only the single threaded io_sq_thread() will
415 * manipulate the list, hence no extra locking is needed there.
416 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +0100417 struct io_wq_work_list iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700418 struct hlist_head *cancel_hash;
419 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800420 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700421 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600422
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200423 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700424
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100425 /* slow path rsrc auxilary data, used by update/register */
426 struct {
427 struct io_rsrc_node *rsrc_backup_node;
428 struct io_mapped_ubuf *dummy_ubuf;
429 struct io_rsrc_data *file_data;
430 struct io_rsrc_data *buf_data;
431
432 struct delayed_work rsrc_put_work;
433 struct llist_head rsrc_put_llist;
434 struct list_head rsrc_ref_list;
435 spinlock_t rsrc_ref_lock;
436 };
437
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700438 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100439 struct {
440 #if defined(CONFIG_UNIX)
441 struct socket *ring_sock;
442 #endif
443 /* hashed buffered write serialization */
444 struct io_wq_hash *hash_map;
445
446 /* Only used for accounting purposes */
447 struct user_struct *user;
448 struct mm_struct *mm_account;
449
450 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100451 struct llist_head fallback_llist;
452 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100453 struct work_struct exit_work;
454 struct list_head tctx_list;
455 struct completion ref_comp;
Pavel Begunkove139a1e2021-10-19 23:43:46 +0100456 u32 iowq_limits[2];
457 bool iowq_limits_set;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100458 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700459};
460
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100461struct io_uring_task {
462 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100463 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100464 struct xarray xa;
465 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100466 const struct io_ring_ctx *last;
467 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100468 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100469 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100470 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100471
472 spinlock_t task_lock;
473 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100474 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100475 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100476};
477
Jens Axboe09bb8392019-03-13 12:39:28 -0600478/*
479 * First field must be the file pointer in all the
480 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
481 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700482struct io_poll_iocb {
483 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000484 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700485 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600486 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700487 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700488 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700489};
490
Pavel Begunkov9d805892021-04-13 02:58:40 +0100491struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000492 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100493 u64 old_user_data;
494 u64 new_user_data;
495 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600496 bool update_events;
497 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000498};
499
Jens Axboeb5dba592019-12-11 14:02:38 -0700500struct io_close {
501 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700502 int fd;
Pavel Begunkov7df778b2021-09-24 20:04:29 +0100503 u32 file_slot;
Jens Axboeb5dba592019-12-11 14:02:38 -0700504};
505
Jens Axboead8a48a2019-11-15 08:49:11 -0700506struct io_timeout_data {
507 struct io_kiocb *req;
508 struct hrtimer timer;
509 struct timespec64 ts;
510 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600511 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700512};
513
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700514struct io_accept {
515 struct file *file;
516 struct sockaddr __user *addr;
517 int __user *addr_len;
518 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100519 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600520 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700521};
522
523struct io_sync {
524 struct file *file;
525 loff_t len;
526 loff_t off;
527 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700528 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700529};
530
Jens Axboefbf23842019-12-17 18:45:56 -0700531struct io_cancel {
532 struct file *file;
533 u64 addr;
534};
535
Jens Axboeb29472e2019-12-17 18:50:29 -0700536struct io_timeout {
537 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300538 u32 off;
539 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300540 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000541 /* head of the link, used by linked timeouts only */
542 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600543 /* for linked completions */
544 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700545};
546
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100547struct io_timeout_rem {
548 struct file *file;
549 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000550
551 /* timeout update */
552 struct timespec64 ts;
553 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600554 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100555};
556
Jens Axboe9adbd452019-12-20 08:45:55 -0700557struct io_rw {
558 /* NOTE: kiocb has the file as the first member, so don't do it here */
559 struct kiocb kiocb;
560 u64 addr;
561 u64 len;
562};
563
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700564struct io_connect {
565 struct file *file;
566 struct sockaddr __user *addr;
567 int addr_len;
568};
569
Jens Axboee47293f2019-12-20 08:58:21 -0700570struct io_sr_msg {
571 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700572 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100573 struct compat_msghdr __user *umsg_compat;
574 struct user_msghdr __user *umsg;
575 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700576 };
Jens Axboee47293f2019-12-20 08:58:21 -0700577 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700578 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700579 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700580};
581
Jens Axboe15b71ab2019-12-11 11:20:36 -0700582struct io_open {
583 struct file *file;
584 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100585 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700586 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700587 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600588 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700589};
590
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000591struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700592 struct file *file;
593 u64 arg;
594 u32 nr_args;
595 u32 offset;
596};
597
Jens Axboe4840e412019-12-25 22:03:45 -0700598struct io_fadvise {
599 struct file *file;
600 u64 offset;
601 u32 len;
602 u32 advice;
603};
604
Jens Axboec1ca7572019-12-25 22:18:28 -0700605struct io_madvise {
606 struct file *file;
607 u64 addr;
608 u32 len;
609 u32 advice;
610};
611
Jens Axboe3e4827b2020-01-08 15:18:09 -0700612struct io_epoll {
613 struct file *file;
614 int epfd;
615 int op;
616 int fd;
617 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700618};
619
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300620struct io_splice {
621 struct file *file_out;
622 struct file *file_in;
623 loff_t off_out;
624 loff_t off_in;
625 u64 len;
626 unsigned int flags;
627};
628
Jens Axboeddf0322d2020-02-23 16:41:33 -0700629struct io_provide_buf {
630 struct file *file;
631 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100632 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700633 __u32 bgid;
634 __u16 nbufs;
635 __u16 bid;
636};
637
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700638struct io_statx {
639 struct file *file;
640 int dfd;
641 unsigned int mask;
642 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700643 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700644 struct statx __user *buffer;
645};
646
Jens Axboe36f4fa62020-09-05 11:14:22 -0600647struct io_shutdown {
648 struct file *file;
649 int how;
650};
651
Jens Axboe80a261f2020-09-28 14:23:58 -0600652struct io_rename {
653 struct file *file;
654 int old_dfd;
655 int new_dfd;
656 struct filename *oldpath;
657 struct filename *newpath;
658 int flags;
659};
660
Jens Axboe14a11432020-09-28 14:27:37 -0600661struct io_unlink {
662 struct file *file;
663 int dfd;
664 int flags;
665 struct filename *filename;
666};
667
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700668struct io_mkdir {
669 struct file *file;
670 int dfd;
671 umode_t mode;
672 struct filename *filename;
673};
674
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700675struct io_symlink {
676 struct file *file;
677 int new_dfd;
678 struct filename *oldpath;
679 struct filename *newpath;
680};
681
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700682struct io_hardlink {
683 struct file *file;
684 int old_dfd;
685 int new_dfd;
686 struct filename *oldpath;
687 struct filename *newpath;
688 int flags;
689};
690
Jens Axboef499a022019-12-02 16:28:46 -0700691struct io_async_connect {
692 struct sockaddr_storage address;
693};
694
Jens Axboe03b12302019-12-02 18:50:25 -0700695struct io_async_msghdr {
696 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000697 /* points to an allocated iov, if NULL we use fast_iov instead */
698 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700699 struct sockaddr __user *uaddr;
700 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700701 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700702};
703
Pavel Begunkov538941e2021-10-14 16:10:15 +0100704struct io_rw_state {
Jens Axboeff6165b2020-08-13 09:47:43 -0600705 struct iov_iter iter;
Jens Axboecd658692021-09-10 11:19:14 -0600706 struct iov_iter_state iter_state;
Pavel Begunkovc88598a2021-10-14 16:10:16 +0100707 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov538941e2021-10-14 16:10:15 +0100708};
709
710struct io_async_rw {
711 struct io_rw_state s;
712 const struct iovec *free_iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -0600713 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600714 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700715};
716
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300717enum {
718 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
719 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
720 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
721 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
722 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700723 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300724
Pavel Begunkovdddca222021-04-27 16:13:52 +0100725 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100726 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300727 REQ_F_INFLIGHT_BIT,
728 REQ_F_CUR_POS_BIT,
729 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300730 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300731 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700732 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700733 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000734 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600735 REQ_F_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100736 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100737 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100738 REQ_F_ARM_LTIMEOUT_BIT,
Pavel Begunkovd886e182021-10-04 20:02:56 +0100739 REQ_F_ASYNC_DATA_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700740 /* keep async read/write and isreg together and in order */
Pavel Begunkov35645ac2021-10-17 00:07:09 +0100741 REQ_F_SUPPORT_NOWAIT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700742 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700743
744 /* not a real bit, just to check we're not overflowing the space */
745 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300746};
747
748enum {
749 /* ctx owns file */
750 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
751 /* drain existing IO first */
752 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
753 /* linked sqes */
754 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
755 /* doesn't sever on completion < 0 */
756 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
757 /* IOSQE_ASYNC */
758 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700759 /* IOSQE_BUFFER_SELECT */
760 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300761
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300762 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100763 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000764 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300765 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
766 /* read/write uses file position */
767 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
768 /* must not punt to workers */
769 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100770 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300771 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300772 /* needs cleanup */
773 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700774 /* already went through poll handler */
775 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700776 /* buffer already selected */
777 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000778 /* completion is deferred through io_comp_state */
779 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600780 /* caller should reissue async */
781 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov35645ac2021-10-17 00:07:09 +0100782 /* supports async reads/writes */
783 REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700784 /* regular file */
785 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100786 /* has creds assigned */
787 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100788 /* skip refcounting if not set */
789 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100790 /* there is a linked timeout that has to be armed */
791 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Pavel Begunkovd886e182021-10-04 20:02:56 +0100792 /* ->async_data allocated */
793 REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700794};
795
796struct async_poll {
797 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600798 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300799};
800
Pavel Begunkovf237c302021-08-18 12:42:46 +0100801typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100802
Jens Axboe7cbf1722021-02-10 00:03:20 +0000803struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100804 union {
805 struct io_wq_work_node node;
806 struct llist_node fallback_node;
807 };
808 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000809};
810
Pavel Begunkov992da012021-06-10 16:37:37 +0100811enum {
812 IORING_RSRC_FILE = 0,
813 IORING_RSRC_BUFFER = 1,
814};
815
Jens Axboe09bb8392019-03-13 12:39:28 -0600816/*
817 * NOTE! Each of the iocb union members has the file pointer
818 * as the first entry in their struct definition. So you can
819 * access the file pointer through any of the sub-structs,
820 * or directly as just 'ki_filp' in this struct.
821 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700822struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700823 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600824 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700825 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700826 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100827 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700828 struct io_accept accept;
829 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700830 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700831 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100832 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700833 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700834 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700835 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700836 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000837 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700838 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700839 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700840 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300841 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700842 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700843 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600844 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600845 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600846 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700847 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700848 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700849 struct io_hardlink hardlink;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700850 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700851
Jens Axboed625c6e2019-12-17 19:53:05 -0700852 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800853 /* polled IO has completed */
854 u8 iopoll_completed;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700855 u16 buf_index;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100856 unsigned int flags;
857
858 u64 user_data;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300859 u32 result;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100860 u32 cflags;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700861
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300862 struct io_ring_ctx *ctx;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300863 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700864
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000865 struct percpu_ref *fixed_rsrc_refs;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100866 /* store used ubuf, so we can prevent reloading */
867 struct io_mapped_ubuf *imu;
Jens Axboed7718a92020-02-14 22:23:12 -0700868
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100869 /* used by request caches, completion batching and iopoll */
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100870 struct io_wq_work_node comp_list;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100871 atomic_t refs;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100872 struct io_kiocb *link;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100873 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300874 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
875 struct hlist_node hash_node;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100876 /* internal polling, see IORING_FEAT_FAST_POLL */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300877 struct async_poll *apoll;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100878 /* opcode allocated if it needs to store data for async defer */
879 void *async_data;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300880 struct io_wq_work work;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100881 /* custom credentials, valid IFF REQ_F_CREDS is set */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100882 const struct cred *creds;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100883 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
Pavel Begunkov30d51dd2021-10-01 18:07:03 +0100884 struct io_buffer *kbuf;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700885};
886
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000887struct io_tctx_node {
888 struct list_head ctx_node;
889 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000890 struct io_ring_ctx *ctx;
891};
892
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300893struct io_defer_entry {
894 struct list_head list;
895 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300896 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300897};
898
Jens Axboed3656342019-12-18 09:50:26 -0700899struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700900 /* needs req->file assigned */
901 unsigned needs_file : 1;
Pavel Begunkov6d634162021-10-06 16:06:46 +0100902 /* should block plug */
903 unsigned plug : 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 Axboe8a727582020-02-20 09:59:44 -0700908 /* set if opcode supports polled "wait" */
909 unsigned pollin : 1;
910 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700911 /* op supports buffer selection */
912 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000913 /* do prep async if is going to be punted */
914 unsigned needs_async_setup : 1;
Pavel Begunkov6d634162021-10-06 16:06:46 +0100915 /* opcode is not supported by this kernel */
916 unsigned not_supported : 1;
Paul Moore5bd21822021-02-16 19:46:48 -0500917 /* skip auditing */
918 unsigned audit_skip : 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,
Paul Moore5bd21822021-02-16 19:46:48 -0500932 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700933 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700934 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300935 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700936 .needs_file = 1,
937 .hash_reg_file = 1,
938 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700939 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000940 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600941 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500942 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700943 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700944 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300945 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700946 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500947 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700948 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300949 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700950 .needs_file = 1,
951 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700952 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600953 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500954 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700955 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700958 .needs_file = 1,
959 .hash_reg_file = 1,
960 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700961 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600962 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500963 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700964 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700965 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300966 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700967 .needs_file = 1,
968 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500969 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700970 },
Paul Moore5bd21822021-02-16 19:46:48 -0500971 [IORING_OP_POLL_REMOVE] = {
972 .audit_skip = 1,
973 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300974 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700975 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500976 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700977 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300978 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700979 .needs_file = 1,
980 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700981 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000982 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700983 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700984 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300985 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700986 .needs_file = 1,
987 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700988 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700989 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000990 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700991 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700992 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300993 [IORING_OP_TIMEOUT] = {
Paul Moore5bd21822021-02-16 19:46:48 -0500994 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700995 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700996 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000997 [IORING_OP_TIMEOUT_REMOVE] = {
998 /* used by timeout updates' prep() */
Paul Moore5bd21822021-02-16 19:46:48 -0500999 .audit_skip = 1,
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00001000 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001001 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001002 .needs_file = 1,
1003 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001004 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -07001005 },
Paul Moore5bd21822021-02-16 19:46:48 -05001006 [IORING_OP_ASYNC_CANCEL] = {
1007 .audit_skip = 1,
1008 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001009 [IORING_OP_LINK_TIMEOUT] = {
Paul Moore5bd21822021-02-16 19:46:48 -05001010 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001011 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -07001012 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001013 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001014 .needs_file = 1,
1015 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001016 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +00001017 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001018 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -07001019 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001020 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -07001021 .needs_file = 1,
1022 },
Jens Axboe44526be2021-02-15 13:32:18 -07001023 [IORING_OP_OPENAT] = {},
1024 [IORING_OP_CLOSE] = {},
Paul Moore5bd21822021-02-16 19:46:48 -05001025 [IORING_OP_FILES_UPDATE] = {
1026 .audit_skip = 1,
1027 },
1028 [IORING_OP_STATX] = {
1029 .audit_skip = 1,
1030 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001031 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001032 .needs_file = 1,
1033 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001034 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001035 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001036 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001037 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001038 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001039 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001040 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001041 .needs_file = 1,
Jens Axboe7b3188e2021-08-30 19:37:41 -06001042 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -07001043 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001044 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001045 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001046 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001047 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001048 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001049 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001050 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001051 .audit_skip = 1,
Jens Axboe4840e412019-12-25 22:03:45 -07001052 },
Jens Axboe44526be2021-02-15 13:32:18 -07001053 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001054 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001055 .needs_file = 1,
1056 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001057 .pollout = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001058 .audit_skip = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001059 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001060 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001061 .needs_file = 1,
1062 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001063 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001064 .buffer_select = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001065 .audit_skip = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001066 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001067 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001068 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001069 [IORING_OP_EPOLL_CTL] = {
1070 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001071 .audit_skip = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001072 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001073 [IORING_OP_SPLICE] = {
1074 .needs_file = 1,
1075 .hash_reg_file = 1,
1076 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001077 .audit_skip = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001078 },
Paul Moore5bd21822021-02-16 19:46:48 -05001079 [IORING_OP_PROVIDE_BUFFERS] = {
1080 .audit_skip = 1,
1081 },
1082 [IORING_OP_REMOVE_BUFFERS] = {
1083 .audit_skip = 1,
1084 },
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001085 [IORING_OP_TEE] = {
1086 .needs_file = 1,
1087 .hash_reg_file = 1,
1088 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001089 .audit_skip = 1,
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001090 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001091 [IORING_OP_SHUTDOWN] = {
1092 .needs_file = 1,
1093 },
Jens Axboe44526be2021-02-15 13:32:18 -07001094 [IORING_OP_RENAMEAT] = {},
1095 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001096 [IORING_OP_MKDIRAT] = {},
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07001097 [IORING_OP_SYMLINKAT] = {},
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07001098 [IORING_OP_LINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001099};
1100
Pavel Begunkov0756a862021-08-15 10:40:25 +01001101/* requests with any of those set should undergo io_disarm_next() */
1102#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1103
Pavel Begunkov7a612352021-03-09 00:37:59 +00001104static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001105static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001106static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1107 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001108 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001109static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001110
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001111static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001112 s32 res, u32 cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001113static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001114static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001115static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001116static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001117static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001118 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001119 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001120static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001121static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001122 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001123static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001124static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001125
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001126static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkovc4501782021-09-08 16:40:52 +01001127static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001128static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001129
Pavel Begunkovb9445592021-08-25 12:25:45 +01001130static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1131 unsigned int issue_flags, u32 slot_index);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01001132static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags);
1133
Pavel Begunkovf1042b62021-08-28 19:54:39 -06001134static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
Pavel Begunkovb9445592021-08-25 12:25:45 +01001135
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136static struct kmem_cache *req_cachep;
1137
Jens Axboe09186822020-10-13 15:01:40 -06001138static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001139
1140struct sock *io_uring_get_socket(struct file *file)
1141{
1142#if defined(CONFIG_UNIX)
1143 if (file->f_op == &io_uring_fops) {
1144 struct io_ring_ctx *ctx = file->private_data;
1145
1146 return ctx->ring_sock->sk;
1147 }
1148#endif
1149 return NULL;
1150}
1151EXPORT_SYMBOL(io_uring_get_socket);
1152
Pavel Begunkovf237c302021-08-18 12:42:46 +01001153static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1154{
1155 if (!*locked) {
1156 mutex_lock(&ctx->uring_lock);
1157 *locked = true;
1158 }
1159}
1160
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001161#define io_for_each_link(pos, head) \
1162 for (pos = (head); pos; pos = pos->link)
1163
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001164/*
1165 * Shamelessly stolen from the mm implementation of page reference checking,
1166 * see commit f958d7b528b1 for details.
1167 */
1168#define req_ref_zero_or_close_to_overflow(req) \
1169 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1170
1171static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1172{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001173 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001174 return atomic_inc_not_zero(&req->refs);
1175}
1176
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001177static inline bool req_ref_put_and_test(struct io_kiocb *req)
1178{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001179 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1180 return true;
1181
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001182 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1183 return atomic_dec_and_test(&req->refs);
1184}
1185
1186static inline void req_ref_put(struct io_kiocb *req)
1187{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001188 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001189 WARN_ON_ONCE(req_ref_put_and_test(req));
1190}
1191
1192static inline void req_ref_get(struct io_kiocb *req)
1193{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001194 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001195 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1196 atomic_inc(&req->refs);
1197}
1198
Pavel Begunkovc4501782021-09-08 16:40:52 +01001199static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
1200{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001201 if (!wq_list_empty(&ctx->submit_state.compl_reqs))
Pavel Begunkovc4501782021-09-08 16:40:52 +01001202 __io_submit_flush_completions(ctx);
1203}
1204
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001205static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001206{
1207 if (!(req->flags & REQ_F_REFCOUNT)) {
1208 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001209 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001210 }
1211}
1212
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001213static inline void io_req_set_refcount(struct io_kiocb *req)
1214{
1215 __io_req_set_refcount(req, 1);
1216}
1217
Pavel Begunkovab409402021-10-09 23:14:41 +01001218#define IO_RSRC_REF_BATCH 100
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001219
Pavel Begunkovab409402021-10-09 23:14:41 +01001220static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
1221 struct io_ring_ctx *ctx)
1222 __must_hold(&ctx->uring_lock)
1223{
1224 struct percpu_ref *ref = req->fixed_rsrc_refs;
1225
1226 if (ref) {
1227 if (ref == &ctx->rsrc_node->refs)
1228 ctx->rsrc_cached_refs++;
1229 else
1230 percpu_ref_put(ref);
1231 }
1232}
1233
1234static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx)
1235{
1236 if (req->fixed_rsrc_refs)
1237 percpu_ref_put(req->fixed_rsrc_refs);
1238}
1239
1240static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
1241 __must_hold(&ctx->uring_lock)
1242{
1243 if (ctx->rsrc_cached_refs) {
1244 percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs);
1245 ctx->rsrc_cached_refs = 0;
1246 }
1247}
1248
1249static void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
1250 __must_hold(&ctx->uring_lock)
1251{
1252 ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
1253 percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH);
1254}
1255
Pavel Begunkova46be972021-10-09 23:14:40 +01001256static inline void io_req_set_rsrc_node(struct io_kiocb *req,
1257 struct io_ring_ctx *ctx)
Jens Axboec40f6372020-06-25 15:39:59 -06001258{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001259 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001260 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Pavel Begunkovab409402021-10-09 23:14:41 +01001261 ctx->rsrc_cached_refs--;
1262 if (unlikely(ctx->rsrc_cached_refs < 0))
1263 io_rsrc_refs_refill(ctx);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001264 }
1265}
1266
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001267static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1268{
1269 bool got = percpu_ref_tryget(ref);
1270
1271 /* already at zero, wait for ->release() */
1272 if (!got)
1273 wait_for_completion(compl);
1274 percpu_ref_resurrect(ref);
1275 if (got)
1276 percpu_ref_put(ref);
1277}
1278
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001279static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1280 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001281{
1282 struct io_kiocb *req;
1283
Pavel Begunkov68207682021-03-22 01:58:25 +00001284 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001285 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001286 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001287 return true;
1288
1289 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001290 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001291 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001292 }
1293 return false;
1294}
1295
Pavel Begunkovd886e182021-10-04 20:02:56 +01001296static inline bool req_has_async_data(struct io_kiocb *req)
1297{
1298 return req->flags & REQ_F_ASYNC_DATA;
1299}
1300
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001301static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001302{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001303 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001304}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001305
Hao Xua8295b92021-08-27 17:46:09 +08001306static inline void req_fail_link_node(struct io_kiocb *req, int res)
1307{
1308 req_set_fail(req);
1309 req->result = res;
1310}
1311
Pavel Begunkovc0724812021-10-04 20:02:54 +01001312static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001313{
1314 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1315
Jens Axboe0f158b42020-05-14 17:18:39 -06001316 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001317}
1318
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001319static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1320{
1321 return !req->timeout.off;
1322}
1323
Pavel Begunkovc0724812021-10-04 20:02:54 +01001324static __cold void io_fallback_req_func(struct work_struct *work)
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001325{
1326 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1327 fallback_work.work);
1328 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1329 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001330 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001331
1332 percpu_ref_get(&ctx->refs);
1333 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001334 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001335
Pavel Begunkovf237c302021-08-18 12:42:46 +01001336 if (locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01001337 io_submit_flush_completions(ctx);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001338 mutex_unlock(&ctx->uring_lock);
1339 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001340 percpu_ref_put(&ctx->refs);
1341}
1342
Pavel Begunkovc0724812021-10-04 20:02:54 +01001343static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001344{
1345 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001346 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001347
1348 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1349 if (!ctx)
1350 return NULL;
1351
Jens Axboe78076bb2019-12-04 19:56:40 -07001352 /*
1353 * Use 5 bits less than the max cq entries, that should give us around
1354 * 32 entries per hash list if totally full and uniformly spread.
1355 */
1356 hash_bits = ilog2(p->cq_entries);
1357 hash_bits -= 5;
1358 if (hash_bits <= 0)
1359 hash_bits = 1;
1360 ctx->cancel_hash_bits = hash_bits;
1361 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1362 GFP_KERNEL);
1363 if (!ctx->cancel_hash)
1364 goto err;
1365 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1366
Pavel Begunkov62248432021-04-28 13:11:29 +01001367 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1368 if (!ctx->dummy_ubuf)
1369 goto err;
1370 /* set invalid range, so io_import_fixed() fails meeting it */
1371 ctx->dummy_ubuf->ubuf = -1UL;
1372
Roman Gushchin21482892019-05-07 10:01:48 -07001373 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001374 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1375 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001376
1377 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001378 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001379 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001380 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001381 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001382 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001383 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001384 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001385 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001386 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001387 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01001388 INIT_WQ_LIST(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001389 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001390 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001391 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001392 spin_lock_init(&ctx->rsrc_ref_lock);
1393 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001394 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1395 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001396 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001397 ctx->submit_state.free_list.next = NULL;
1398 INIT_WQ_LIST(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001399 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001400 INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001401 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001402err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001403 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001404 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001405 kfree(ctx);
1406 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001407}
1408
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001409static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1410{
1411 struct io_rings *r = ctx->rings;
1412
1413 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1414 ctx->cq_extra--;
1415}
1416
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001417static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001418{
Jens Axboe2bc99302020-07-09 09:43:27 -06001419 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1420 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001421
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001422 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001423 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001424
Bob Liu9d858b22019-11-13 18:06:25 +08001425 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001426}
1427
Pavel Begunkov35645ac2021-10-17 00:07:09 +01001428#define FFS_NOWAIT 0x1UL
1429#define FFS_ISREG 0x2UL
1430#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG)
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001431
1432static inline bool io_req_ffs_set(struct io_kiocb *req)
1433{
Pavel Begunkov35645ac2021-10-17 00:07:09 +01001434 return req->flags & REQ_F_FIXED_FILE;
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001435}
1436
Pavel Begunkovc0724812021-10-04 20:02:54 +01001437static inline void io_req_track_inflight(struct io_kiocb *req)
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001438{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001439 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001440 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001441 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001442 }
1443}
1444
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001445static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1446{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001447 if (WARN_ON_ONCE(!req->link))
1448 return NULL;
1449
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001450 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1451 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001452
1453 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001454 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001455 __io_req_set_refcount(req->link, 2);
1456 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001457}
1458
1459static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1460{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001461 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001462 return NULL;
1463 return __io_prep_linked_timeout(req);
1464}
1465
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001466static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001467{
Jens Axboed3656342019-12-18 09:50:26 -07001468 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001469 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001470
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001471 if (!(req->flags & REQ_F_CREDS)) {
1472 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001473 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001474 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001475
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001476 req->work.list.next = NULL;
1477 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001478 if (req->flags & REQ_F_FORCE_ASYNC)
1479 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1480
Jens Axboed3656342019-12-18 09:50:26 -07001481 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001482 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001483 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001484 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001485 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001486 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001487 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001488
1489 switch (req->opcode) {
1490 case IORING_OP_SPLICE:
1491 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001492 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1493 req->work.flags |= IO_WQ_WORK_UNBOUND;
1494 break;
1495 }
Jens Axboe561fb042019-10-24 07:25:42 -06001496}
1497
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001498static void io_prep_async_link(struct io_kiocb *req)
1499{
1500 struct io_kiocb *cur;
1501
Pavel Begunkov44eff402021-07-26 14:14:31 +01001502 if (req->flags & REQ_F_LINK_TIMEOUT) {
1503 struct io_ring_ctx *ctx = req->ctx;
1504
Jens Axboe79ebeae2021-08-10 15:18:27 -06001505 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001506 io_for_each_link(cur, req)
1507 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001508 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001509 } else {
1510 io_for_each_link(cur, req)
1511 io_prep_async_work(cur);
1512 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001513}
1514
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001515static inline void io_req_add_compl_list(struct io_kiocb *req)
1516{
1517 struct io_submit_state *state = &req->ctx->submit_state;
1518
1519 wq_list_add_tail(&req->comp_list, &state->compl_reqs);
1520}
1521
Arnd Bergmann00169242021-10-19 17:34:53 +02001522static void io_queue_async_work(struct io_kiocb *req, bool *dont_use)
Jens Axboe561fb042019-10-24 07:25:42 -06001523{
Jackie Liua197f662019-11-08 08:09:12 -07001524 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001525 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001526 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001527
Jens Axboe3bfe6102021-02-16 14:15:30 -07001528 BUG_ON(!tctx);
1529 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001530
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001531 /* init ->work of the whole link before punting */
1532 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001533
1534 /*
1535 * Not expected to happen, but if we do have a bug where this _can_
1536 * happen, catch it here and ensure the request is marked as
1537 * canceled. That will make io-wq go through the usual work cancel
1538 * procedure rather than attempt to run this request (or create a new
1539 * worker for it).
1540 */
1541 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1542 req->work.flags |= IO_WQ_WORK_CANCEL;
1543
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001544 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1545 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001546 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001547 if (link)
1548 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001549}
1550
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001551static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001552 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001553 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001554{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001555 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001556
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001557 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001558 if (status)
1559 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001560 atomic_set(&req->ctx->cq_timeouts,
1561 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001562 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001563 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001564 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001565 }
1566}
1567
Pavel Begunkovc0724812021-10-04 20:02:54 +01001568static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001569{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001570 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001571 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1572 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001573
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001574 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001575 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001576 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001577 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001578 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001579 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001580}
1581
Pavel Begunkovc0724812021-10-04 20:02:54 +01001582static __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001583 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001584{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001585 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001586
Jens Axboe79ebeae2021-08-10 15:18:27 -06001587 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001588 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001589 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001590 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001591 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001592
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001593 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001594 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001595
1596 /*
1597 * Since seq can easily wrap around over time, subtract
1598 * the last seq at which timeouts were flushed before comparing.
1599 * Assuming not more than 2^31-1 events have happened since,
1600 * these subtractions won't have wrapped, so we can check if
1601 * target is in [last_seq, current_seq] by comparing the two.
1602 */
1603 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1604 events_got = seq - ctx->cq_last_tm_flush;
1605 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001606 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001607
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001608 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001609 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001610 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001611 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001612 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001613}
1614
Pavel Begunkovc0724812021-10-04 20:02:54 +01001615static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001616{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001617 if (ctx->off_timeout_used)
1618 io_flush_timeouts(ctx);
1619 if (ctx->drain_active)
1620 io_queue_deferred(ctx);
1621}
1622
1623static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1624{
1625 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1626 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001627 /* order cqe stores with ring update */
1628 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001629}
1630
Jens Axboe90554202020-09-03 12:12:41 -06001631static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1632{
1633 struct io_rings *r = ctx->rings;
1634
Pavel Begunkova566c552021-05-16 22:58:08 +01001635 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001636}
1637
Pavel Begunkov888aae22021-01-19 13:32:39 +00001638static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1639{
1640 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1641}
1642
Pavel Begunkovd068b502021-05-16 22:58:11 +01001643static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001644{
Hristo Venev75b28af2019-08-26 17:23:46 +00001645 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001646 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001647
Stefan Bühler115e12e2019-04-24 23:54:18 +02001648 /*
1649 * writes to the cq entry need to come after reading head; the
1650 * control dependency is enough as we're using WRITE_ONCE to
1651 * fill the cq entry
1652 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001653 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001654 return NULL;
1655
Pavel Begunkov888aae22021-01-19 13:32:39 +00001656 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001657 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001658}
1659
Jens Axboef2842ab2020-01-08 11:04:00 -07001660static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1661{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001662 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001663 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001664 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1665 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001666 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001667}
1668
Jens Axboe2c5d7632021-08-21 07:21:19 -06001669/*
1670 * This should only get called when at least one event has been posted.
1671 * Some applications rely on the eventfd notification count only changing
1672 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1673 * 1:1 relationship between how many times this function is called (and
1674 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1675 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001676static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001677{
Jens Axboe5fd46172021-08-06 14:04:31 -06001678 /*
1679 * wake_up_all() may seem excessive, but io_wake_function() and
1680 * io_should_wake() handle the termination of the loop and only
1681 * wake as many waiters as we need to.
1682 */
1683 if (wq_has_sleeper(&ctx->cq_wait))
1684 wake_up_all(&ctx->cq_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001685 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001686 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001687}
1688
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001689static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1690{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001691 /* see waitqueue_active() comment */
1692 smp_mb();
1693
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001694 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001695 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001696 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001697 }
1698 if (io_should_trigger_evfd(ctx))
1699 eventfd_signal(ctx->cq_ev_fd, 1);
1700}
1701
Jens Axboec4a2ed72019-11-21 21:01:26 -07001702/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001703static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001704{
Jens Axboeb18032b2021-01-24 16:58:56 -07001705 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001706
Pavel Begunkova566c552021-05-16 22:58:08 +01001707 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001708 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001709
Jens Axboeb18032b2021-01-24 16:58:56 -07001710 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001711 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001712 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001713 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001714 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001715
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001716 if (!cqe && !force)
1717 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001718 ocqe = list_first_entry(&ctx->cq_overflow_list,
1719 struct io_overflow_cqe, list);
1720 if (cqe)
1721 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1722 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001723 io_account_cq_overflow(ctx);
1724
Jens Axboeb18032b2021-01-24 16:58:56 -07001725 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001726 list_del(&ocqe->list);
1727 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001728 }
1729
Pavel Begunkov09e88402020-12-17 00:24:38 +00001730 all_flushed = list_empty(&ctx->cq_overflow_list);
1731 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001732 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001733 WRITE_ONCE(ctx->rings->sq_flags,
1734 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001735 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001736
Jens Axboeb18032b2021-01-24 16:58:56 -07001737 if (posted)
1738 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001739 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001740 if (posted)
1741 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001742 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001743}
1744
Pavel Begunkov90f67362021-08-09 20:18:12 +01001745static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001746{
Jens Axboeca0a2652021-03-04 17:15:48 -07001747 bool ret = true;
1748
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001749 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001750 /* iopoll syncs against uring_lock, not completion_lock */
1751 if (ctx->flags & IORING_SETUP_IOPOLL)
1752 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001753 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001754 if (ctx->flags & IORING_SETUP_IOPOLL)
1755 mutex_unlock(&ctx->uring_lock);
1756 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001757
1758 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001759}
1760
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001761/* must to be called somewhat shortly after putting a request */
1762static inline void io_put_task(struct task_struct *task, int nr)
1763{
1764 struct io_uring_task *tctx = task->io_uring;
1765
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001766 if (likely(task == current)) {
1767 tctx->cached_refs += nr;
1768 } else {
1769 percpu_counter_sub(&tctx->inflight, nr);
1770 if (unlikely(atomic_read(&tctx->in_idle)))
1771 wake_up(&tctx->wait);
1772 put_task_struct_many(task, nr);
1773 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001774}
1775
Pavel Begunkov9a108672021-08-27 11:55:01 +01001776static void io_task_refs_refill(struct io_uring_task *tctx)
1777{
1778 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1779
1780 percpu_counter_add(&tctx->inflight, refill);
1781 refcount_add(refill, &current->usage);
1782 tctx->cached_refs += refill;
1783}
1784
1785static inline void io_get_task_refs(int nr)
1786{
1787 struct io_uring_task *tctx = current->io_uring;
1788
1789 tctx->cached_refs -= nr;
1790 if (unlikely(tctx->cached_refs < 0))
1791 io_task_refs_refill(tctx);
1792}
1793
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001794static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001795 s32 res, u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001796{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001797 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001798
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001799 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1800 if (!ocqe) {
1801 /*
1802 * If we're in ring overflow flush mode, or in task cancel mode,
1803 * or cannot allocate an overflow entry, then we need to drop it
1804 * on the floor.
1805 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001806 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001807 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001808 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001809 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001810 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001811 WRITE_ONCE(ctx->rings->sq_flags,
1812 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1813
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001814 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001815 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001816 ocqe->cqe.res = res;
1817 ocqe->cqe.flags = cflags;
1818 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1819 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001820}
1821
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001822static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001823 s32 res, u32 cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001824{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001825 struct io_uring_cqe *cqe;
1826
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001827 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001828
1829 /*
1830 * If we can't get a cq entry, userspace overflowed the
1831 * submission (by quite a lot). Increment the overflow count in
1832 * the ring.
1833 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001834 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001835 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001836 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001837 WRITE_ONCE(cqe->res, res);
1838 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001839 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001840 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001841 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001842}
1843
Pavel Begunkov8d133262021-04-11 01:46:33 +01001844/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001845static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001846 s32 res, u32 cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001847{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001848 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001849}
1850
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001851static void io_req_complete_post(struct io_kiocb *req, s32 res,
1852 u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001853{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001854 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001855
Jens Axboe79ebeae2021-08-10 15:18:27 -06001856 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001857 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001858 /*
1859 * If we're the last reference to this request, add to our locked
1860 * free_list cache.
1861 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001862 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001863 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001864 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001865 io_disarm_next(req);
1866 if (req->link) {
1867 io_req_task_queue(req->link);
1868 req->link = NULL;
1869 }
1870 }
Pavel Begunkovab409402021-10-09 23:14:41 +01001871 io_req_put_rsrc(req, ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001872 io_dismantle_req(req);
1873 io_put_task(req->task, 1);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001874 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001875 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001876 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001877 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001878 spin_unlock(&ctx->completion_lock);
Pavel Begunkova3f349072021-09-15 12:04:20 +01001879 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001880}
1881
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001882static inline void io_req_complete_state(struct io_kiocb *req, s32 res,
1883 u32 cflags)
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001884{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001885 req->result = res;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +01001886 req->cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001887 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001888}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889
Pavel Begunkov889fca72021-02-10 00:03:09 +00001890static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001891 s32 res, u32 cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001892{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001893 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1894 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001895 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001896 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001897}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001898
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001899static inline void io_req_complete(struct io_kiocb *req, s32 res)
Jens Axboee1e16092020-06-22 09:17:17 -06001900{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001901 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001902}
1903
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001904static void io_req_complete_failed(struct io_kiocb *req, s32 res)
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001905{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001906 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001907 io_req_complete_post(req, res, 0);
1908}
1909
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01001910static void io_req_complete_fail_submit(struct io_kiocb *req)
1911{
1912 /*
1913 * We don't submit, fail them all, for that replace hardlinks with
1914 * normal links. Extra REQ_F_LINK is tolerated.
1915 */
1916 req->flags &= ~REQ_F_HARDLINK;
1917 req->flags |= REQ_F_LINK;
1918 io_req_complete_failed(req, req->result);
1919}
1920
Pavel Begunkov864ea922021-08-09 13:04:08 +01001921/*
1922 * Don't initialise the fields below on every allocation, but do that in
1923 * advance and keep them valid across allocations.
1924 */
1925static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1926{
1927 req->ctx = ctx;
1928 req->link = NULL;
1929 req->async_data = NULL;
1930 /* not necessary, but safer to zero */
1931 req->result = 0;
1932}
1933
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001934static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001935 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001936{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001937 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001938 wq_list_splice(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001939 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001940 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001941}
1942
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001943/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001944static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001945{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001946 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001947
Jens Axboec7dae4b2021-02-09 19:53:37 -07001948 /*
1949 * If we have more than a batch's worth of requests in our IRQ side
1950 * locked cache, grab the lock and move them over to our submission
1951 * side cache.
1952 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001953 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001954 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001955 return !!state->free_list.next;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001956}
1957
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001958/*
1959 * A request might get retired back into the request caches even before opcode
1960 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1961 * Because of that, io_alloc_req() should be called only under ->uring_lock
1962 * and with extra caution to not get a request that is still worked on.
1963 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01001964static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001965 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001966{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001967 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001968 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001969 void *reqs[IO_REQ_ALLOC_BATCH];
1970 struct io_kiocb *req;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001971 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001972
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001973 if (likely(state->free_list.next || io_flush_cached_reqs(ctx)))
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001974 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001975
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001976 ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001977
Pavel Begunkov864ea922021-08-09 13:04:08 +01001978 /*
1979 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1980 * retry single alloc to be on the safe side.
1981 */
1982 if (unlikely(ret <= 0)) {
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001983 reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1984 if (!reqs[0])
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001985 return false;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001986 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001987 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001988
Pavel Begunkov37f0e762021-10-04 20:02:53 +01001989 percpu_ref_get_many(&ctx->refs, ret);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001990 for (i = 0; i < ret; i++) {
1991 req = reqs[i];
1992
1993 io_preinit_req(req, ctx);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001994 wq_stack_add_head(&req->comp_list, &state->free_list);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001995 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001996 return true;
1997}
1998
1999static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
2000{
2001 if (unlikely(!ctx->submit_state.free_list.next))
2002 return __io_alloc_req_refill(ctx);
2003 return true;
2004}
2005
2006static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
2007{
2008 struct io_wq_work_node *node;
2009
2010 node = wq_stack_extract(&ctx->submit_state.free_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002011 return container_of(node, struct io_kiocb, comp_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002012}
2013
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002014static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002015{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002016 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002017 fput(file);
2018}
2019
Pavel Begunkov6b639522021-09-08 16:40:50 +01002020static inline void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002021{
Pavel Begunkov094bae42021-03-19 17:22:42 +00002022 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002023
Pavel Begunkov867f8fa2021-10-04 20:02:58 +01002024 if (unlikely(flags & IO_REQ_CLEAN_FLAGS))
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01002025 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002026 if (!(flags & REQ_F_FIXED_FILE))
2027 io_put_file(req->file);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002028}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002029
Pavel Begunkovc0724812021-10-04 20:02:54 +01002030static __cold void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002031{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002032 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002033
Pavel Begunkovab409402021-10-09 23:14:41 +01002034 io_req_put_rsrc(req, ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002035 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002036 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002037
Jens Axboe79ebeae2021-08-10 15:18:27 -06002038 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002039 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01002040 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06002041 spin_unlock(&ctx->completion_lock);
Jens Axboee65ef562019-03-12 10:16:44 -06002042}
2043
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002044static inline void io_remove_next_linked(struct io_kiocb *req)
2045{
2046 struct io_kiocb *nxt = req->link;
2047
2048 req->link = nxt->link;
2049 nxt->link = NULL;
2050}
2051
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002052static bool io_kill_linked_timeout(struct io_kiocb *req)
2053 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06002054 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002055{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002056 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002057
Pavel Begunkovb97e7362021-08-15 10:40:23 +01002058 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002059 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002060
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002061 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002062 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01002063 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06002064 list_del(&link->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002065 io_cqring_fill_event(link->ctx, link->user_data,
2066 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002067 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002068 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002069 }
2070 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002071 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002072}
2073
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002074static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002075 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002076{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002077 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06002078
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002079 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002080 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002081 long res = -ECANCELED;
2082
2083 if (link->flags & REQ_F_FAIL)
2084 res = link->result;
2085
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002086 nxt = link->link;
2087 link->link = NULL;
2088
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002089 trace_io_uring_fail_link(req, link);
Hao Xua8295b92021-08-27 17:46:09 +08002090 io_cqring_fill_event(link->ctx, link->user_data, res, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002091 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002092 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002093 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002094}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002095
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002096static bool io_disarm_next(struct io_kiocb *req)
2097 __must_hold(&req->ctx->completion_lock)
2098{
2099 bool posted = false;
2100
Pavel Begunkov0756a862021-08-15 10:40:25 +01002101 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2102 struct io_kiocb *link = req->link;
2103
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002104 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002105 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2106 io_remove_next_linked(req);
2107 io_cqring_fill_event(link->ctx, link->user_data,
2108 -ECANCELED, 0);
2109 io_put_req_deferred(link);
2110 posted = true;
2111 }
2112 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002113 struct io_ring_ctx *ctx = req->ctx;
2114
2115 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002116 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002117 spin_unlock_irq(&ctx->timeout_lock);
2118 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002119 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002120 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002121 posted |= (req->link != NULL);
2122 io_fail_links(req);
2123 }
2124 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002125}
2126
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002127static void __io_req_find_next_prep(struct io_kiocb *req)
2128{
2129 struct io_ring_ctx *ctx = req->ctx;
2130 bool posted;
2131
2132 spin_lock(&ctx->completion_lock);
2133 posted = io_disarm_next(req);
2134 if (posted)
2135 io_commit_cqring(req->ctx);
2136 spin_unlock(&ctx->completion_lock);
2137 if (posted)
2138 io_cqring_ev_posted(ctx);
2139}
2140
2141static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002142{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002143 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002144
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002145 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2146 return NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002147 /*
2148 * If LINK is set, we have dependent requests in this chain. If we
2149 * didn't fail this request, queue the first one up, moving any other
2150 * dependencies to the next request. In case of failure, fail the rest
2151 * of the chain.
2152 */
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002153 if (unlikely(req->flags & IO_DISARM_MASK))
2154 __io_req_find_next_prep(req);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002155 nxt = req->link;
2156 req->link = NULL;
2157 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002158}
Jens Axboe2665abf2019-11-05 12:40:47 -07002159
Pavel Begunkovf237c302021-08-18 12:42:46 +01002160static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002161{
2162 if (!ctx)
2163 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002164 if (*locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01002165 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002166 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002167 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002168 }
2169 percpu_ref_put(&ctx->refs);
2170}
2171
Jens Axboe7cbf1722021-02-10 00:03:20 +00002172static void tctx_task_work(struct callback_head *cb)
2173{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002174 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002175 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002176 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2177 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002178
Pavel Begunkov16f72072021-06-17 18:14:09 +01002179 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002180 struct io_wq_work_node *node;
2181
Pavel Begunkovc4501782021-09-08 16:40:52 +01002182 if (!tctx->task_list.first && locked)
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002183 io_submit_flush_completions(ctx);
2184
Pavel Begunkov3f184072021-06-17 18:14:06 +01002185 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002186 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002187 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002188 if (!node)
2189 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002190 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002191 if (!node)
2192 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002193
Pavel Begunkov6294f362021-08-10 17:53:55 +01002194 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002195 struct io_wq_work_node *next = node->next;
2196 struct io_kiocb *req = container_of(node, struct io_kiocb,
2197 io_task_work.node);
2198
2199 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002200 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002201 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002202 /* if not contended, grab and improve batching */
2203 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002204 percpu_ref_get(&ctx->refs);
2205 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002206 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002207 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002208 } while (node);
2209
Jens Axboe7cbf1722021-02-10 00:03:20 +00002210 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002211 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002212
Pavel Begunkovf237c302021-08-18 12:42:46 +01002213 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002214}
2215
Pavel Begunkove09ee512021-07-01 13:26:05 +01002216static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002217{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002218 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002219 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002220 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002221 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002222 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002223 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002224
2225 WARN_ON_ONCE(!tctx);
2226
Jens Axboe0b81e802021-02-16 10:33:53 -07002227 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002228 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002229 running = tctx->task_running;
2230 if (!running)
2231 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002232 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002233
2234 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002235 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002236 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002237
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002238 /*
2239 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2240 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2241 * processing task_work. There's no reliable way to tell if TWA_RESUME
2242 * will do the job.
2243 */
2244 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovd97ec622021-09-08 16:40:53 +01002245 if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2246 if (notify == TWA_NONE)
2247 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002248 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002249 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002250
Pavel Begunkove09ee512021-07-01 13:26:05 +01002251 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002252 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002253 node = tctx->task_list.first;
2254 INIT_WQ_LIST(&tctx->task_list);
2255 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002256
Pavel Begunkove09ee512021-07-01 13:26:05 +01002257 while (node) {
2258 req = container_of(node, struct io_kiocb, io_task_work.node);
2259 node = node->next;
2260 if (llist_add(&req->io_task_work.fallback_node,
2261 &req->ctx->fallback_llist))
2262 schedule_delayed_work(&req->ctx->fallback_work, 1);
2263 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002264}
2265
Pavel Begunkovf237c302021-08-18 12:42:46 +01002266static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002267{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002268 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002269
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002270 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002271 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002272 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002273}
2274
Pavel Begunkovf237c302021-08-18 12:42:46 +01002275static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002276{
2277 struct io_ring_ctx *ctx = req->ctx;
2278
Pavel Begunkovf237c302021-08-18 12:42:46 +01002279 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002280 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002281 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002282 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002283 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002284 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002285}
2286
Pavel Begunkova3df76982021-02-18 22:32:52 +00002287static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2288{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002289 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002290 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002291 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002292}
2293
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002294static void io_req_task_queue(struct io_kiocb *req)
2295{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002296 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002297 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002298}
2299
Jens Axboe773af692021-07-27 10:25:55 -06002300static void io_req_task_queue_reissue(struct io_kiocb *req)
2301{
2302 req->io_task_work.func = io_queue_async_work;
2303 io_req_task_work_add(req);
2304}
2305
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002306static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002307{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002308 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002309
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002310 if (nxt)
2311 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002312}
2313
Jens Axboe9e645e112019-05-10 16:07:28 -06002314static void io_free_req(struct io_kiocb *req)
2315{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002316 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002317 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002318}
2319
Pavel Begunkovf237c302021-08-18 12:42:46 +01002320static void io_free_req_work(struct io_kiocb *req, bool *locked)
2321{
2322 io_free_req(req);
2323}
2324
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002325static void io_free_batch_list(struct io_ring_ctx *ctx,
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002326 struct io_wq_work_node *node)
Jens Axboea141dd82021-08-12 12:48:34 -06002327 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002328{
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002329 struct task_struct *task = NULL;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002330 int task_refs = 0;
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002331
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002332 do {
2333 struct io_kiocb *req = container_of(node, struct io_kiocb,
2334 comp_list);
2335
Pavel Begunkovdef77ac2021-10-12 15:02:14 +01002336 if (unlikely(req->flags & REQ_F_REFCOUNT)) {
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002337 node = req->comp_list.next;
Pavel Begunkovdef77ac2021-10-12 15:02:14 +01002338 if (!req_ref_put_and_test(req))
2339 continue;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002340 }
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002341
Pavel Begunkovab409402021-10-09 23:14:41 +01002342 io_req_put_rsrc_locked(req, ctx);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002343 io_queue_next(req);
2344 io_dismantle_req(req);
2345
2346 if (req->task != task) {
2347 if (task)
2348 io_put_task(task, task_refs);
2349 task = req->task;
2350 task_refs = 0;
2351 }
2352 task_refs++;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002353 node = req->comp_list.next;
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002354 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002355 } while (node);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002356
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002357 if (task)
2358 io_put_task(task, task_refs);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002359}
2360
Pavel Begunkovc4501782021-09-08 16:40:52 +01002361static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002362 __must_hold(&ctx->uring_lock)
2363{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002364 struct io_wq_work_node *node, *prev;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002365 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002366
Jens Axboe79ebeae2021-08-10 15:18:27 -06002367 spin_lock(&ctx->completion_lock);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002368 wq_list_for_each(node, prev, &state->compl_reqs) {
2369 struct io_kiocb *req = container_of(node, struct io_kiocb,
2370 comp_list);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002371
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002372 __io_cqring_fill_event(ctx, req->user_data, req->result,
Pavel Begunkovd17e56e2021-10-04 20:02:57 +01002373 req->cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002374 }
2375 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002376 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002377 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002378
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002379 io_free_batch_list(ctx, state->compl_reqs.first);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002380 INIT_WQ_LIST(&state->compl_reqs);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002381}
2382
Jens Axboeba816ad2019-09-28 11:36:45 -06002383/*
2384 * Drop reference to request, return next in chain (if there is one) if this
2385 * was the last reference to this request.
2386 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002387static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002388{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002389 struct io_kiocb *nxt = NULL;
2390
Jens Axboede9b4cc2021-02-24 13:28:27 -07002391 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002392 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002393 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002394 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002395 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002396}
2397
Pavel Begunkov0d850352021-03-19 17:22:37 +00002398static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002399{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002400 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002401 io_free_req(req);
2402}
2403
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002404static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002405{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002406 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002407 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002408 io_req_task_work_add(req);
2409 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002410}
2411
Pavel Begunkov6c503152021-01-04 20:36:36 +00002412static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002413{
2414 /* See comment at the top of this file */
2415 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002416 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002417}
2418
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002419static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2420{
2421 struct io_rings *rings = ctx->rings;
2422
2423 /* make sure SQ entry isn't read before tail */
2424 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2425}
2426
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002427static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002428{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002429 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002430
Jens Axboebcda7ba2020-02-23 16:42:51 -07002431 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2432 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002433 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002434 kfree(kbuf);
2435 return cflags;
2436}
2437
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002438static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2439{
Pavel Begunkovae421d92021-08-17 20:28:08 +01002440 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2441 return 0;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01002442 return io_put_kbuf(req, req->kbuf);
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002443}
2444
Jens Axboe4c6e2772020-07-01 11:29:10 -06002445static inline bool io_run_task_work(void)
2446{
Nadav Amitef98eb02021-08-07 17:13:41 -07002447 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002448 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002449 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002450 return true;
2451 }
2452
2453 return false;
2454}
2455
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002456static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
Jens Axboedef596e2019-01-09 08:59:42 -07002457{
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002458 struct io_wq_work_node *pos, *start, *prev;
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);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002461 int nr_events = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002462
2463 /*
2464 * Only spin for completions if we don't have multiple devices hanging
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002465 * off our complete list.
Jens Axboedef596e2019-01-09 08:59:42 -07002466 */
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002467 if (ctx->poll_multi_queue || force_nonspin)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002468 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002469
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002470 wq_list_for_each(pos, start, &ctx->iopoll_list) {
2471 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002472 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002473 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002474
2475 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002476 * Move completed and retryable entries to our local lists.
2477 * If we find a request that requires polling, break out
2478 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002479 */
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002480 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002481 break;
2482
Jens Axboeb688f112021-10-12 09:28:46 -06002483 ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002484 if (unlikely(ret < 0))
2485 return ret;
2486 else if (ret)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002487 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002488
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002489 /* iopoll may have completed current req */
Jens Axboeb688f112021-10-12 09:28:46 -06002490 if (!rq_list_empty(iob.req_list) ||
2491 READ_ONCE(req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002492 break;
Jens Axboedef596e2019-01-09 08:59:42 -07002493 }
2494
Jens Axboeb688f112021-10-12 09:28:46 -06002495 if (!rq_list_empty(iob.req_list))
2496 iob.complete(&iob);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002497 else if (!pos)
2498 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002499
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002500 prev = start;
2501 wq_list_for_each_resume(pos, prev) {
2502 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
2503
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002504 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
2505 if (!smp_load_acquire(&req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002506 break;
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002507 __io_cqring_fill_event(ctx, req->user_data, req->result,
Pavel Begunkovf5ed3bc2021-09-24 21:59:52 +01002508 io_put_rw_kbuf(req));
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002509 nr_events++;
2510 }
Jens Axboedef596e2019-01-09 08:59:42 -07002511
Pavel Begunkovf5ed3bc2021-09-24 21:59:52 +01002512 if (unlikely(!nr_events))
2513 return 0;
2514
2515 io_commit_cqring(ctx);
2516 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002517 pos = start ? start->next : ctx->iopoll_list.first;
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002518 wq_list_cut(&ctx->iopoll_list, prev, start);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002519 io_free_batch_list(ctx, pos);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002520 return nr_events;
Jens Axboedef596e2019-01-09 08:59:42 -07002521}
2522
2523/*
Jens Axboedef596e2019-01-09 08:59:42 -07002524 * We can't just wait for polled events to come to us, we have to actively
2525 * find and complete them.
2526 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01002527static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002528{
2529 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2530 return;
2531
2532 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002533 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002534 /* let it sleep and repeat later if can't complete a request */
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002535 if (io_do_iopoll(ctx, true) == 0)
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002536 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002537 /*
2538 * Ensure we allow local-to-the-cpu processing to take place,
2539 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002540 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002541 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002542 if (need_resched()) {
2543 mutex_unlock(&ctx->uring_lock);
2544 cond_resched();
2545 mutex_lock(&ctx->uring_lock);
2546 }
Jens Axboedef596e2019-01-09 08:59:42 -07002547 }
2548 mutex_unlock(&ctx->uring_lock);
2549}
2550
Pavel Begunkov7668b922020-07-07 16:36:21 +03002551static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002552{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002553 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002554 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002555
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002556 /*
2557 * We disallow the app entering submit/complete with polling, but we
2558 * still need to lock the ring to prevent racing with polled issue
2559 * that got punted to a workqueue.
2560 */
2561 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002562 /*
2563 * Don't enter poll loop if we already have events pending.
2564 * If we do, we can potentially be spinning for commands that
2565 * already triggered a CQE (eg in error).
2566 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002567 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002568 __io_cqring_overflow_flush(ctx, false);
2569 if (io_cqring_events(ctx))
2570 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002571 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002572 /*
2573 * If a submit got punted to a workqueue, we can have the
2574 * application entering polling for a command before it gets
2575 * issued. That app will hold the uring_lock for the duration
2576 * of the poll right here, so we need to take a breather every
2577 * now and then to ensure that the issue has a chance to add
2578 * the poll to the issued list. Otherwise we can spin here
2579 * forever, while the workqueue is stuck trying to acquire the
2580 * very same mutex.
2581 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002582 if (wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002583 u32 tail = ctx->cached_cq_tail;
2584
Jens Axboe500f9fb2019-08-19 12:15:59 -06002585 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002586 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002587 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002588
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002589 /* some requests don't go through iopoll_list */
2590 if (tail != ctx->cached_cq_tail ||
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002591 wq_list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002592 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002593 }
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002594 ret = io_do_iopoll(ctx, !min);
2595 if (ret < 0)
2596 break;
2597 nr_events += ret;
2598 ret = 0;
2599 } while (nr_events < min && !need_resched());
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002600out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002601 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002602 return ret;
2603}
2604
Jens Axboe491381ce2019-10-17 09:20:46 -06002605static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002606{
Jens Axboe491381ce2019-10-17 09:20:46 -06002607 /*
2608 * Tell lockdep we inherited freeze protection from submission
2609 * thread.
2610 */
2611 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002612 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002613
Pavel Begunkov1c986792021-03-22 01:58:31 +00002614 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2615 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002616 }
2617}
2618
Jens Axboeb63534c2020-06-04 11:28:00 -06002619#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002620static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002621{
Pavel Begunkovab454432021-03-22 01:58:33 +00002622 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002623
Pavel Begunkovd886e182021-10-04 20:02:56 +01002624 if (!req_has_async_data(req))
Pavel Begunkovab454432021-03-22 01:58:33 +00002625 return !io_req_prep_async(req);
Pavel Begunkov538941e2021-10-14 16:10:15 +01002626 iov_iter_restore(&rw->s.iter, &rw->s.iter_state);
Pavel Begunkovab454432021-03-22 01:58:33 +00002627 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002628}
Jens Axboeb63534c2020-06-04 11:28:00 -06002629
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002630static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002631{
Jens Axboe355afae2020-09-02 09:30:31 -06002632 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002633 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002634
Jens Axboe355afae2020-09-02 09:30:31 -06002635 if (!S_ISBLK(mode) && !S_ISREG(mode))
2636 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002637 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2638 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002639 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002640 /*
2641 * If ref is dying, we might be running poll reap from the exit work.
2642 * Don't attempt to reissue from that path, just let it fail with
2643 * -EAGAIN.
2644 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002645 if (percpu_ref_is_dying(&ctx->refs))
2646 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002647 /*
2648 * Play it safe and assume not safe to re-import and reissue if we're
2649 * not in the original thread group (or in task context).
2650 */
2651 if (!same_thread_group(req->task, current) || !in_task())
2652 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002653 return true;
2654}
Jens Axboee82ad482021-04-02 19:45:34 -06002655#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002656static bool io_resubmit_prep(struct io_kiocb *req)
2657{
2658 return false;
2659}
Jens Axboee82ad482021-04-02 19:45:34 -06002660static bool io_rw_should_reissue(struct io_kiocb *req)
2661{
2662 return false;
2663}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002664#endif
2665
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002666static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002667{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002668 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2669 kiocb_end_write(req);
Pavel Begunkov258f3a72021-10-14 16:10:14 +01002670 if (unlikely(res != req->result)) {
Pavel Begunkov9532b992021-03-22 01:58:34 +00002671 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2672 io_rw_should_reissue(req)) {
2673 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002674 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002675 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002676 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002677 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002678 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002679 return false;
2680}
2681
Pavel Begunkovf237c302021-08-18 12:42:46 +01002682static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002683{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002684 unsigned int cflags = io_put_rw_kbuf(req);
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01002685 int res = req->result;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002686
2687 if (*locked) {
Pavel Begunkov126180b2021-08-18 12:42:47 +01002688 io_req_complete_state(req, res, cflags);
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002689 io_req_add_compl_list(req);
Pavel Begunkov126180b2021-08-18 12:42:47 +01002690 } else {
2691 io_req_complete_post(req, res, cflags);
2692 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002693}
2694
2695static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2696 unsigned int issue_flags)
2697{
2698 if (__io_complete_rw_common(req, res))
2699 return;
Pavel Begunkov63637852021-09-02 00:38:22 +01002700 __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002701}
2702
Jens Axboe6b19b762021-10-21 09:22:35 -06002703static void io_complete_rw(struct kiocb *kiocb, long res)
Jens Axboeba816ad2019-09-28 11:36:45 -06002704{
Jens Axboe9adbd452019-12-20 08:45:55 -07002705 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002706
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002707 if (__io_complete_rw_common(req, res))
2708 return;
2709 req->result = res;
2710 req->io_task_work.func = io_req_task_complete;
2711 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002712}
2713
Jens Axboe6b19b762021-10-21 09:22:35 -06002714static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
Jens Axboedef596e2019-01-09 08:59:42 -07002715{
Jens Axboe9adbd452019-12-20 08:45:55 -07002716 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002717
Jens Axboe491381ce2019-10-17 09:20:46 -06002718 if (kiocb->ki_flags & IOCB_WRITE)
2719 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002720 if (unlikely(res != req->result)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002721 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2722 req->flags |= REQ_F_REISSUE;
2723 return;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002724 }
Pavel Begunkov258f3a72021-10-14 16:10:14 +01002725 req->result = res;
Pavel Begunkov8c130822021-03-22 01:58:32 +00002726 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002727
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002728 /* order with io_iopoll_complete() checking ->iopoll_completed */
2729 smp_store_release(&req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002730}
2731
2732/*
2733 * After the iocb has been issued, it's safe to be found on the poll list.
2734 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002735 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002736 * accessing the kiocb cookie.
2737 */
Pavel Begunkov98821312021-10-15 17:09:12 +01002738static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboedef596e2019-01-09 08:59:42 -07002739{
2740 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08002741 const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002742
2743 /* workqueue context doesn't hold uring_lock, grab it now */
Hao Xu3b44b372021-10-18 21:34:31 +08002744 if (unlikely(needs_lock))
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002745 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002746
2747 /*
2748 * Track whether we have multiple files in our lists. This will impact
2749 * how we do polling eventually, not spinning if we're on potentially
2750 * different devices.
2751 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002752 if (wq_list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002753 ctx->poll_multi_queue = false;
2754 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002755 struct io_kiocb *list_req;
2756
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002757 list_req = container_of(ctx->iopoll_list.first, struct io_kiocb,
2758 comp_list);
Christoph Hellwig30da1b42021-10-12 13:12:14 +02002759 if (list_req->file != req->file)
Hao Xu915b3dd2021-06-28 05:37:30 +08002760 ctx->poll_multi_queue = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002761 }
2762
2763 /*
2764 * For fast devices, IO may have already completed. If it has, add
2765 * it to the front so we find it first.
2766 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002767 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002768 wq_list_add_head(&req->comp_list, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002769 else
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002770 wq_list_add_tail(&req->comp_list, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002771
Hao Xu3b44b372021-10-18 21:34:31 +08002772 if (unlikely(needs_lock)) {
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002773 /*
2774 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2775 * in sq thread task context or in io worker task context. If
2776 * current task context is sq thread, we don't need to check
2777 * whether should wake up sq thread.
2778 */
2779 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2780 wq_has_sleeper(&ctx->sq_data->wait))
2781 wake_up(&ctx->sq_data->wait);
2782
2783 mutex_unlock(&ctx->uring_lock);
2784 }
Jens Axboedef596e2019-01-09 08:59:42 -07002785}
2786
Jens Axboe4503b762020-06-01 10:00:27 -06002787static bool io_bdev_nowait(struct block_device *bdev)
2788{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002789 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002790}
2791
Jens Axboe2b188cc2019-01-07 10:46:33 -07002792/*
2793 * If we tracked the file through the SCM inflight mechanism, we could support
2794 * any file. For now, just ensure that anything potentially problematic is done
2795 * inline.
2796 */
Pavel Begunkov88459b52021-10-17 00:07:10 +01002797static bool __io_file_supports_nowait(struct file *file, umode_t mode)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002798{
Jens Axboe4503b762020-06-01 10:00:27 -06002799 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002800 if (IS_ENABLED(CONFIG_BLOCK) &&
2801 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002802 return true;
2803 return false;
2804 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002805 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002807 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002808 if (IS_ENABLED(CONFIG_BLOCK) &&
2809 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002810 file->f_op != &io_uring_fops)
2811 return true;
2812 return false;
2813 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002814
Jens Axboec5b85622020-06-09 19:23:05 -06002815 /* any ->read/write should understand O_NONBLOCK */
2816 if (file->f_flags & O_NONBLOCK)
2817 return true;
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002818 return file->f_mode & FMODE_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002819}
2820
Pavel Begunkov88459b52021-10-17 00:07:10 +01002821/*
2822 * If we tracked the file through the SCM inflight mechanism, we could support
2823 * any file. For now, just ensure that anything potentially problematic is done
2824 * inline.
2825 */
2826static unsigned int io_file_get_flags(struct file *file)
Jens Axboe7b29f922021-03-12 08:30:14 -07002827{
Pavel Begunkov88459b52021-10-17 00:07:10 +01002828 umode_t mode = file_inode(file)->i_mode;
2829 unsigned int res = 0;
Jens Axboe7b29f922021-03-12 08:30:14 -07002830
Pavel Begunkov88459b52021-10-17 00:07:10 +01002831 if (S_ISREG(mode))
2832 res |= FFS_ISREG;
2833 if (__io_file_supports_nowait(file, mode))
2834 res |= FFS_NOWAIT;
2835 return res;
Jens Axboe7b29f922021-03-12 08:30:14 -07002836}
2837
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002838static inline bool io_file_supports_nowait(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002839{
Pavel Begunkov88459b52021-10-17 00:07:10 +01002840 return req->flags & REQ_F_SUPPORT_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002841}
2842
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01002843static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002844{
Jens Axboedef596e2019-01-09 08:59:42 -07002845 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002846 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002847 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002848 unsigned ioprio;
2849 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002850
Pavel Begunkov88459b52021-10-17 00:07:10 +01002851 if (!io_req_ffs_set(req))
2852 req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT;
Jens Axboe491381ce2019-10-17 09:20:46 -06002853
Jens Axboe2b188cc2019-01-07 10:46:33 -07002854 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002855 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002856 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002857 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002858 }
Pavel Begunkov5cb03d62021-10-15 17:09:16 +01002859 kiocb->ki_flags = iocb_flags(file);
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002860 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2861 if (unlikely(ret))
2862 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002863
Jens Axboe5d329e12021-09-14 11:08:37 -06002864 /*
2865 * If the file is marked O_NONBLOCK, still allow retry for it if it
2866 * supports async. Otherwise it's impossible to use O_NONBLOCK files
2867 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2868 */
2869 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002870 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req)))
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002871 req->flags |= REQ_F_NOWAIT;
2872
Jens Axboedef596e2019-01-09 08:59:42 -07002873 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov5cb03d62021-10-15 17:09:16 +01002874 if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->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 Begunkovfb272742021-10-23 12:14:02 +01002886 ioprio = READ_ONCE(sqe->ioprio);
2887 if (ioprio) {
2888 ret = ioprio_check_cap(ioprio);
2889 if (ret)
2890 return ret;
2891
2892 kiocb->ki_ioprio = ioprio;
2893 } else {
2894 kiocb->ki_ioprio = get_current_ioprio();
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002895 }
2896
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01002897 req->imu = NULL;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002898 req->rw.addr = READ_ONCE(sqe->addr);
2899 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002900 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002901 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002902}
2903
2904static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2905{
2906 switch (ret) {
2907 case -EIOCBQUEUED:
2908 break;
2909 case -ERESTARTSYS:
2910 case -ERESTARTNOINTR:
2911 case -ERESTARTNOHAND:
2912 case -ERESTART_RESTARTBLOCK:
2913 /*
2914 * We can't just restart the syscall, since previously
2915 * submitted sqes may already be in progress. Just fail this
2916 * IO with EINTR.
2917 */
2918 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002919 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002920 default:
Jens Axboe6b19b762021-10-21 09:22:35 -06002921 kiocb->ki_complete(kiocb, ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002922 }
2923}
2924
Jens Axboea1d7c392020-06-22 11:09:46 -06002925static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002926 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002927{
Jens Axboeba042912019-12-25 16:33:42 -07002928 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002929 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002930
Jens Axboe227c0c92020-08-13 11:51:40 -06002931 /* add previously done IO, if any */
Pavel Begunkovd886e182021-10-04 20:02:56 +01002932 if (req_has_async_data(req) && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002933 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002934 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002935 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002936 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002937 }
2938
Jens Axboeba042912019-12-25 16:33:42 -07002939 if (req->flags & REQ_F_CUR_POS)
2940 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002941 if (ret >= 0 && (kiocb->ki_complete == io_complete_rw))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002942 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002943 else
2944 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002945
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002946 if (req->flags & REQ_F_REISSUE) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002947 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002948 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002949 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002950 } else {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002951 unsigned int cflags = io_put_rw_kbuf(req);
2952 struct io_ring_ctx *ctx = req->ctx;
2953
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002954 req_set_fail(req);
Hao Xu3b44b372021-10-18 21:34:31 +08002955 if (issue_flags & IO_URING_F_UNLOCKED) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002956 mutex_lock(&ctx->uring_lock);
2957 __io_req_complete(req, issue_flags, ret, cflags);
2958 mutex_unlock(&ctx->uring_lock);
2959 } else {
2960 __io_req_complete(req, issue_flags, ret, cflags);
2961 }
Pavel Begunkov97284632021-04-08 19:28:03 +01002962 }
2963 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002964}
2965
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002966static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2967 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002968{
Jens Axboe9adbd452019-12-20 08:45:55 -07002969 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002970 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002971 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002972
Pavel Begunkov75769e32021-04-01 15:43:54 +01002973 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002974 return -EFAULT;
2975 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002976 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002977 return -EFAULT;
2978
2979 /*
2980 * May not be a start of buffer, set size appropriately
2981 * and advance us to the beginning.
2982 */
2983 offset = buf_addr - imu->ubuf;
2984 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002985
2986 if (offset) {
2987 /*
2988 * Don't use iov_iter_advance() here, as it's really slow for
2989 * using the latter parts of a big fixed buffer - it iterates
2990 * over each segment manually. We can cheat a bit here, because
2991 * we know that:
2992 *
2993 * 1) it's a BVEC iter, we set it up
2994 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2995 * first and last bvec
2996 *
2997 * So just find our index, and adjust the iterator afterwards.
2998 * If the offset is within the first bvec (or the whole first
2999 * bvec, just use iov_iter_advance(). This makes it easier
3000 * since we can just skip the first segment, which may not
3001 * be PAGE_SIZE aligned.
3002 */
3003 const struct bio_vec *bvec = imu->bvec;
3004
3005 if (offset <= bvec->bv_len) {
3006 iov_iter_advance(iter, offset);
3007 } else {
3008 unsigned long seg_skip;
3009
3010 /* skip first vec */
3011 offset -= bvec->bv_len;
3012 seg_skip = 1 + (offset >> PAGE_SHIFT);
3013
3014 iter->bvec = bvec + seg_skip;
3015 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003016 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003017 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003018 }
3019 }
3020
Pavel Begunkov847595d2021-02-04 13:52:06 +00003021 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003022}
3023
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003024static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3025{
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003026 struct io_mapped_ubuf *imu = req->imu;
3027 u16 index, buf_index = req->buf_index;
3028
3029 if (likely(!imu)) {
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003030 struct io_ring_ctx *ctx = req->ctx;
3031
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003032 if (unlikely(buf_index >= ctx->nr_user_bufs))
3033 return -EFAULT;
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003034 io_req_set_rsrc_node(req, ctx);
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003035 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3036 imu = READ_ONCE(ctx->user_bufs[index]);
3037 req->imu = imu;
3038 }
3039 return __io_import_fixed(req, rw, iter, imu);
3040}
3041
Jens Axboebcda7ba2020-02-23 16:42:51 -07003042static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3043{
3044 if (needs_lock)
3045 mutex_unlock(&ctx->uring_lock);
3046}
3047
3048static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3049{
3050 /*
3051 * "Normal" inline submissions always hold the uring_lock, since we
3052 * grab it from the system call. Same is true for the SQPOLL offload.
3053 * The only exception is when we've detached the request and issue it
3054 * from an async worker thread, grab the lock for that case.
3055 */
3056 if (needs_lock)
3057 mutex_lock(&ctx->uring_lock);
3058}
3059
3060static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003061 int bgid, unsigned int issue_flags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07003062{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003063 struct io_buffer *kbuf = req->kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003064 struct io_buffer *head;
Hao Xu3b44b372021-10-18 21:34:31 +08003065 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003066
3067 if (req->flags & REQ_F_BUFFER_SELECTED)
3068 return kbuf;
3069
3070 io_ring_submit_lock(req->ctx, needs_lock);
3071
3072 lockdep_assert_held(&req->ctx->uring_lock);
3073
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003074 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003075 if (head) {
3076 if (!list_empty(&head->list)) {
3077 kbuf = list_last_entry(&head->list, struct io_buffer,
3078 list);
3079 list_del(&kbuf->list);
3080 } else {
3081 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003082 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003083 }
3084 if (*len > kbuf->len)
3085 *len = kbuf->len;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003086 req->flags |= REQ_F_BUFFER_SELECTED;
3087 req->kbuf = kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003088 } else {
3089 kbuf = ERR_PTR(-ENOBUFS);
3090 }
3091
3092 io_ring_submit_unlock(req->ctx, needs_lock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003093 return kbuf;
3094}
3095
Jens Axboe4d954c22020-02-27 07:31:19 -07003096static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003097 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003098{
3099 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003100 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003101
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003102 bgid = req->buf_index;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003103 kbuf = io_buffer_select(req, len, bgid, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003104 if (IS_ERR(kbuf))
3105 return kbuf;
Jens Axboe4d954c22020-02-27 07:31:19 -07003106 return u64_to_user_ptr(kbuf->addr);
3107}
3108
3109#ifdef CONFIG_COMPAT
3110static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003111 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003112{
3113 struct compat_iovec __user *uiov;
3114 compat_ssize_t clen;
3115 void __user *buf;
3116 ssize_t len;
3117
3118 uiov = u64_to_user_ptr(req->rw.addr);
3119 if (!access_ok(uiov, sizeof(*uiov)))
3120 return -EFAULT;
3121 if (__get_user(clen, &uiov->iov_len))
3122 return -EFAULT;
3123 if (clen < 0)
3124 return -EINVAL;
3125
3126 len = clen;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003127 buf = io_rw_buffer_select(req, &len, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003128 if (IS_ERR(buf))
3129 return PTR_ERR(buf);
3130 iov[0].iov_base = buf;
3131 iov[0].iov_len = (compat_size_t) len;
3132 return 0;
3133}
3134#endif
3135
3136static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003137 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003138{
3139 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3140 void __user *buf;
3141 ssize_t len;
3142
3143 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3144 return -EFAULT;
3145
3146 len = iov[0].iov_len;
3147 if (len < 0)
3148 return -EINVAL;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003149 buf = io_rw_buffer_select(req, &len, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003150 if (IS_ERR(buf))
3151 return PTR_ERR(buf);
3152 iov[0].iov_base = buf;
3153 iov[0].iov_len = len;
3154 return 0;
3155}
3156
3157static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003158 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003159{
Jens Axboedddb3e22020-06-04 11:27:01 -06003160 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003161 struct io_buffer *kbuf = req->kbuf;
Jens Axboedddb3e22020-06-04 11:27:01 -06003162
Jens Axboedddb3e22020-06-04 11:27:01 -06003163 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3164 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003165 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003166 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003167 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003168 return -EINVAL;
3169
3170#ifdef CONFIG_COMPAT
3171 if (req->ctx->compat)
Pavel Begunkov51aac422021-10-14 16:10:17 +01003172 return io_compat_import(req, iov, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003173#endif
3174
Pavel Begunkov51aac422021-10-14 16:10:17 +01003175 return __io_iov_buffer_select(req, iov, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003176}
3177
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003178static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req,
3179 struct io_rw_state *s,
3180 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003181{
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003182 struct iov_iter *iter = &s->iter;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003183 u8 opcode = req->opcode;
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003184 struct iovec *iovec;
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003185 void __user *buf;
3186 size_t sqe_len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003187 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003188
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003189 BUILD_BUG_ON(ERR_PTR(0) != NULL);
3190
3191 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED)
3192 return ERR_PTR(io_import_fixed(req, rw, iter));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003193
Jens Axboebcda7ba2020-02-23 16:42:51 -07003194 /* buffer index only valid with fixed read/write, or buffer select */
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003195 if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)))
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003196 return ERR_PTR(-EINVAL);
Jens Axboe9adbd452019-12-20 08:45:55 -07003197
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003198 buf = u64_to_user_ptr(req->rw.addr);
3199 sqe_len = req->rw.len;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003200
Jens Axboe3a6820f2019-12-22 15:19:35 -07003201 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003202 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01003203 buf = io_rw_buffer_select(req, &sqe_len, issue_flags);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003204 if (IS_ERR(buf))
Changcheng Deng898df242021-10-20 08:49:48 +00003205 return ERR_CAST(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003206 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003207 }
3208
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003209 ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003210 return ERR_PTR(ret);
Jens Axboe3a6820f2019-12-22 15:19:35 -07003211 }
3212
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003213 iovec = s->fast_iov;
Jens Axboe4d954c22020-02-27 07:31:19 -07003214 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003215 ret = io_iov_buffer_select(req, iovec, issue_flags);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003216 if (!ret)
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003217 iov_iter_init(iter, rw, iovec, 1, iovec->iov_len);
3218 return ERR_PTR(ret);
Jens Axboe4d954c22020-02-27 07:31:19 -07003219 }
3220
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003221 ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003222 req->ctx->compat);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003223 if (unlikely(ret < 0))
3224 return ERR_PTR(ret);
3225 return iovec;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003226}
3227
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003228static inline int io_import_iovec(int rw, struct io_kiocb *req,
3229 struct iovec **iovec, struct io_rw_state *s,
3230 unsigned int issue_flags)
3231{
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003232 *iovec = __io_import_iovec(rw, req, s, issue_flags);
3233 if (unlikely(IS_ERR(*iovec)))
3234 return PTR_ERR(*iovec);
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003235
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003236 iov_iter_save_state(&s->iter, &s->iter_state);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003237 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003238}
3239
Jens Axboe0fef9482020-08-26 10:36:20 -06003240static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3241{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003242 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003243}
3244
Jens Axboe32960612019-09-23 11:05:34 -06003245/*
3246 * For files that don't have ->read_iter() and ->write_iter(), handle them
3247 * by looping over ->read() or ->write() manually.
3248 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003249static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003250{
Jens Axboe4017eb92020-10-22 14:14:12 -06003251 struct kiocb *kiocb = &req->rw.kiocb;
3252 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003253 ssize_t ret = 0;
3254
3255 /*
3256 * Don't support polled IO through this interface, and we can't
3257 * support non-blocking either. For the latter, this just causes
3258 * the kiocb to be handled from an async context.
3259 */
3260 if (kiocb->ki_flags & IOCB_HIPRI)
3261 return -EOPNOTSUPP;
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003262 if ((kiocb->ki_flags & IOCB_NOWAIT) &&
3263 !(kiocb->ki_filp->f_flags & O_NONBLOCK))
Jens Axboe32960612019-09-23 11:05:34 -06003264 return -EAGAIN;
3265
3266 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003267 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003268 ssize_t nr;
3269
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003270 if (!iov_iter_is_bvec(iter)) {
3271 iovec = iov_iter_iovec(iter);
3272 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003273 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3274 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003275 }
3276
Jens Axboe32960612019-09-23 11:05:34 -06003277 if (rw == READ) {
3278 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003279 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003280 } else {
3281 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003282 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003283 }
3284
3285 if (nr < 0) {
3286 if (!ret)
3287 ret = nr;
3288 break;
3289 }
Jens Axboe16c8d2d2021-09-12 06:45:07 -06003290 if (!iov_iter_is_bvec(iter)) {
3291 iov_iter_advance(iter, nr);
3292 } else {
3293 req->rw.len -= nr;
3294 req->rw.addr += nr;
3295 }
Jens Axboe32960612019-09-23 11:05:34 -06003296 ret += nr;
3297 if (nr != iovec.iov_len)
3298 break;
Jens Axboe32960612019-09-23 11:05:34 -06003299 }
3300
3301 return ret;
3302}
3303
Jens Axboeff6165b2020-08-13 09:47:43 -06003304static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3305 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003306{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003307 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003308
Pavel Begunkov538941e2021-10-14 16:10:15 +01003309 memcpy(&rw->s.iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003310 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003311 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003312 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003313 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003314 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003315 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003316 unsigned iov_off = 0;
3317
Pavel Begunkov538941e2021-10-14 16:10:15 +01003318 rw->s.iter.iov = rw->s.fast_iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003319 if (iter->iov != fast_iov) {
3320 iov_off = iter->iov - fast_iov;
Pavel Begunkov538941e2021-10-14 16:10:15 +01003321 rw->s.iter.iov += iov_off;
Jens Axboeff6165b2020-08-13 09:47:43 -06003322 }
Pavel Begunkov538941e2021-10-14 16:10:15 +01003323 if (rw->s.fast_iov != fast_iov)
3324 memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003325 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003326 } else {
3327 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003328 }
3329}
3330
Hao Xu8d4af682021-09-22 18:15:22 +08003331static inline bool io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003332{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003333 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3334 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
Pavel Begunkovd886e182021-10-04 20:02:56 +01003335 if (req->async_data) {
3336 req->flags |= REQ_F_ASYNC_DATA;
3337 return false;
3338 }
3339 return true;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003340}
3341
Jens Axboeff6165b2020-08-13 09:47:43 -06003342static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003343 struct io_rw_state *s, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003344{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003345 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003346 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003347 if (!req_has_async_data(req)) {
Jens Axboecd658692021-09-10 11:19:14 -06003348 struct io_async_rw *iorw;
3349
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003350 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003351 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003352 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003353 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003354
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003355 io_req_map_rw(req, iovec, s->fast_iov, &s->iter);
Jens Axboecd658692021-09-10 11:19:14 -06003356 iorw = req->async_data;
3357 /* we've copied and mapped the iter, ensure state is saved */
Pavel Begunkov538941e2021-10-14 16:10:15 +01003358 iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003359 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003360 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003361}
3362
Pavel Begunkov73debe62020-09-30 22:57:54 +03003363static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003364{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003365 struct io_async_rw *iorw = req->async_data;
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003366 struct iovec *iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003367 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003368
Pavel Begunkov51aac422021-10-14 16:10:17 +01003369 /* submission path, ->uring_lock should already be taken */
Hao Xu3b44b372021-10-18 21:34:31 +08003370 ret = io_import_iovec(rw, req, &iov, &iorw->s, 0);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003371 if (unlikely(ret < 0))
3372 return ret;
3373
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003374 iorw->bytes_done = 0;
3375 iorw->free_iovec = iov;
3376 if (iov)
3377 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003378 return 0;
3379}
3380
Pavel Begunkov73debe62020-09-30 22:57:54 +03003381static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003382{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003383 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3384 return -EBADF;
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01003385 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003386}
3387
Jens Axboec1dd91d2020-08-03 16:43:59 -06003388/*
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05003389 * This is our waitqueue callback handler, registered through __folio_lock_async()
Jens Axboec1dd91d2020-08-03 16:43:59 -06003390 * when we initially tried to do the IO with the iocb armed our waitqueue.
3391 * This gets called when the page is unlocked, and we generally expect that to
3392 * happen when the page IO is completed and the page is now uptodate. This will
3393 * queue a task_work based retry of the operation, attempting to copy the data
3394 * again. If the latter fails because the page was NOT uptodate, then we will
3395 * do a thread based blocking retry of the operation. That's the unexpected
3396 * slow path.
3397 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003398static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3399 int sync, void *arg)
3400{
3401 struct wait_page_queue *wpq;
3402 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003403 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003404
3405 wpq = container_of(wait, struct wait_page_queue, wait);
3406
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003407 if (!wake_page_match(wpq, key))
3408 return 0;
3409
Hao Xuc8d317a2020-09-29 20:00:45 +08003410 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003411 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003412 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003413 return 1;
3414}
3415
Jens Axboec1dd91d2020-08-03 16:43:59 -06003416/*
3417 * This controls whether a given IO request should be armed for async page
3418 * based retry. If we return false here, the request is handed to the async
3419 * worker threads for retry. If we're doing buffered reads on a regular file,
3420 * we prepare a private wait_page_queue entry and retry the operation. This
3421 * will either succeed because the page is now uptodate and unlocked, or it
3422 * will register a callback when the page is unlocked at IO completion. Through
3423 * that callback, io_uring uses task_work to setup a retry of the operation.
3424 * That retry will attempt the buffered read again. The retry will generally
3425 * succeed, or in rare cases where it fails, we then fall back to using the
3426 * async worker threads for a blocking retry.
3427 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003428static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003429{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003430 struct io_async_rw *rw = req->async_data;
3431 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003432 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003433
3434 /* never retry for NOWAIT, we just complete with -EAGAIN */
3435 if (req->flags & REQ_F_NOWAIT)
3436 return false;
3437
Jens Axboe227c0c92020-08-13 11:51:40 -06003438 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003439 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003440 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003441
Jens Axboebcf5a062020-05-22 09:24:42 -06003442 /*
3443 * just use poll if we can, and don't attempt if the fs doesn't
3444 * support callback based unlocks
3445 */
3446 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3447 return false;
3448
Jens Axboe3b2a4432020-08-16 10:58:43 -07003449 wait->wait.func = io_async_buf_func;
3450 wait->wait.private = req;
3451 wait->wait.flags = 0;
3452 INIT_LIST_HEAD(&wait->wait.entry);
3453 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003454 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003455 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003456 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003457}
3458
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003459static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003460{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003461 if (likely(req->file->f_op->read_iter))
Jens Axboebcf5a062020-05-22 09:24:42 -06003462 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003463 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003464 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003465 else
3466 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003467}
3468
Ming Lei7db30432021-08-21 23:07:51 +08003469static bool need_read_all(struct io_kiocb *req)
3470{
3471 return req->flags & REQ_F_ISREG ||
3472 S_ISBLK(file_inode(req->file)->i_mode);
3473}
3474
Pavel Begunkov889fca72021-02-10 00:03:09 +00003475static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003476{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003477 struct io_rw_state __s, *s = &__s;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003478 struct iovec *iovec;
Jens Axboe9adbd452019-12-20 08:45:55 -07003479 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003480 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003481 struct io_async_rw *rw;
Jens Axboecd658692021-09-10 11:19:14 -06003482 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003483
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003484 if (!req_has_async_data(req)) {
3485 ret = io_import_iovec(READ, req, &iovec, s, issue_flags);
3486 if (unlikely(ret < 0))
3487 return ret;
3488 } else {
Pavel Begunkovd886e182021-10-04 20:02:56 +01003489 rw = req->async_data;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003490 s = &rw->s;
Jens Axboecd658692021-09-10 11:19:14 -06003491 /*
3492 * We come here from an earlier attempt, restore our state to
3493 * match in case it doesn't. It's cheap enough that we don't
3494 * need to make this conditional.
3495 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003496 iov_iter_restore(&s->iter, &s->iter_state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003497 iovec = NULL;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003498 }
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003499 req->result = iov_iter_count(&s->iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003500
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003501 if (force_nonblock) {
3502 /* If the file doesn't support async, just async punt */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003503 if (unlikely(!io_file_supports_nowait(req))) {
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003504 ret = io_setup_async_rw(req, iovec, s, true);
3505 return ret ?: -EAGAIN;
3506 }
Pavel Begunkova88fc402020-09-30 22:57:53 +03003507 kiocb->ki_flags |= IOCB_NOWAIT;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003508 } else {
3509 /* Ensure we clear previously set non-block flag */
3510 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003511 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003512
Jens Axboecd658692021-09-10 11:19:14 -06003513 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003514 if (unlikely(ret)) {
3515 kfree(iovec);
3516 return ret;
3517 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003518
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003519 ret = io_iter_do_read(req, &s->iter);
Jens Axboe32960612019-09-23 11:05:34 -06003520
Jens Axboe230d50d2021-04-01 20:41:15 -06003521 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003522 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003523 /* IOPOLL retry should happen for io-wq threads */
3524 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003525 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003526 /* no retry on NONBLOCK nor RWF_NOWAIT */
3527 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003528 goto done;
Jens Axboef38c7e32020-09-25 15:23:43 -06003529 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003530 } else if (ret == -EIOCBQUEUED) {
3531 goto out_free;
Pavel Begunkovf80a50a2021-10-14 16:10:13 +01003532 } else if (ret == req->result || ret <= 0 || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003533 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003534 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003535 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003536 }
3537
Jens Axboecd658692021-09-10 11:19:14 -06003538 /*
3539 * Don't depend on the iter state matching what was consumed, or being
3540 * untouched in case of error. Restore it and we'll advance it
3541 * manually if we need to.
3542 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003543 iov_iter_restore(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003544
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003545 ret2 = io_setup_async_rw(req, iovec, s, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003546 if (ret2)
3547 return ret2;
3548
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003549 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003550 rw = req->async_data;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003551 s = &rw->s;
Jens Axboecd658692021-09-10 11:19:14 -06003552 /*
3553 * Now use our persistent iterator and state, if we aren't already.
3554 * We've restored and mapped the iter to match.
3555 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003556
Pavel Begunkovb23df912021-02-04 13:52:04 +00003557 do {
Jens Axboecd658692021-09-10 11:19:14 -06003558 /*
3559 * We end up here because of a partial read, either from
3560 * above or inside this loop. Advance the iter by the bytes
3561 * that were consumed.
3562 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003563 iov_iter_advance(&s->iter, ret);
3564 if (!iov_iter_count(&s->iter))
Jens Axboecd658692021-09-10 11:19:14 -06003565 break;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003566 rw->bytes_done += ret;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003567 iov_iter_save_state(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003568
Pavel Begunkovb23df912021-02-04 13:52:04 +00003569 /* if we can retry, do so with the callbacks armed */
3570 if (!io_rw_should_retry(req)) {
3571 kiocb->ki_flags &= ~IOCB_WAITQ;
3572 return -EAGAIN;
3573 }
3574
3575 /*
3576 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3577 * we get -EIOCBQUEUED, then we'll get a notification when the
3578 * desired page gets unlocked. We can also get a partial read
3579 * here, and if we do, then just retry at the new offset.
3580 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003581 ret = io_iter_do_read(req, &s->iter);
Pavel Begunkovb23df912021-02-04 13:52:04 +00003582 if (ret == -EIOCBQUEUED)
3583 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003584 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003585 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003586 iov_iter_restore(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003587 } while (ret > 0);
Jens Axboe227c0c92020-08-13 11:51:40 -06003588done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003589 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003590out_free:
3591 /* it's faster to check here then delegate to kfree */
3592 if (iovec)
3593 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003594 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003595}
3596
Pavel Begunkov73debe62020-09-30 22:57:54 +03003597static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003598{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003599 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3600 return -EBADF;
Jens Axboe3884b832021-10-25 13:45:12 -06003601 req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file));
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01003602 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003603}
3604
Pavel Begunkov889fca72021-02-10 00:03:09 +00003605static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003606{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003607 struct io_rw_state __s, *s = &__s;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003608 struct iovec *iovec;
Jens Axboe9adbd452019-12-20 08:45:55 -07003609 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003610 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003611 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003612
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003613 if (!req_has_async_data(req)) {
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003614 ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags);
3615 if (unlikely(ret < 0))
Pavel Begunkov2846c482020-11-07 13:16:27 +00003616 return ret;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003617 } else {
3618 struct io_async_rw *rw = req->async_data;
3619
3620 s = &rw->s;
3621 iov_iter_restore(&s->iter, &s->iter_state);
3622 iovec = NULL;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003623 }
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003624 req->result = iov_iter_count(&s->iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003625
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003626 if (force_nonblock) {
3627 /* If the file doesn't support async, just async punt */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003628 if (unlikely(!io_file_supports_nowait(req)))
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003629 goto copy_iov;
3630
3631 /* file path doesn't support NOWAIT for non-direct_IO */
3632 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3633 (req->flags & REQ_F_ISREG))
3634 goto copy_iov;
3635
Pavel Begunkova88fc402020-09-30 22:57:53 +03003636 kiocb->ki_flags |= IOCB_NOWAIT;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003637 } else {
3638 /* Ensure we clear previously set non-block flag */
3639 kiocb->ki_flags &= ~IOCB_NOWAIT;
3640 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003641
Jens Axboecd658692021-09-10 11:19:14 -06003642 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003643 if (unlikely(ret))
3644 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003645
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003646 /*
3647 * Open-code file_start_write here to grab freeze protection,
3648 * which will be released by another thread in
3649 * io_complete_rw(). Fool lockdep by telling it the lock got
3650 * released so that it doesn't complain about the held lock when
3651 * we return to userspace.
3652 */
3653 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003654 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003655 __sb_writers_release(file_inode(req->file)->i_sb,
3656 SB_FREEZE_WRITE);
3657 }
3658 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003659
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003660 if (likely(req->file->f_op->write_iter))
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003661 ret2 = call_write_iter(req->file, kiocb, &s->iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003662 else if (req->file->f_op->write)
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003663 ret2 = loop_rw_iter(WRITE, req, &s->iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003664 else
3665 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003666
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003667 if (req->flags & REQ_F_REISSUE) {
3668 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003669 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003670 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003671
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003672 /*
3673 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3674 * retry them without IOCB_NOWAIT.
3675 */
3676 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3677 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003678 /* no retry on NONBLOCK nor RWF_NOWAIT */
3679 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003680 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003681 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003682 /* IOPOLL retry should happen for io-wq threads */
Noah Goldsteinb10841c2021-10-16 20:32:29 -05003683 if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboeeefdf302020-08-27 16:40:19 -06003684 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003685done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003686 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003687 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003688copy_iov:
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003689 iov_iter_restore(&s->iter, &s->iter_state);
3690 ret = io_setup_async_rw(req, iovec, s, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003691 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003692 }
Jens Axboe31b51512019-01-18 22:56:34 -07003693out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003694 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003695 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003696 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003697 return ret;
3698}
3699
Jens Axboe80a261f2020-09-28 14:23:58 -06003700static int io_renameat_prep(struct io_kiocb *req,
3701 const struct io_uring_sqe *sqe)
3702{
3703 struct io_rename *ren = &req->rename;
3704 const char __user *oldf, *newf;
3705
Jens Axboeed7eb252021-06-23 09:04:13 -06003706 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3707 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003708 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003709 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003710 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3711 return -EBADF;
3712
3713 ren->old_dfd = READ_ONCE(sqe->fd);
3714 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3715 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3716 ren->new_dfd = READ_ONCE(sqe->len);
3717 ren->flags = READ_ONCE(sqe->rename_flags);
3718
3719 ren->oldpath = getname(oldf);
3720 if (IS_ERR(ren->oldpath))
3721 return PTR_ERR(ren->oldpath);
3722
3723 ren->newpath = getname(newf);
3724 if (IS_ERR(ren->newpath)) {
3725 putname(ren->oldpath);
3726 return PTR_ERR(ren->newpath);
3727 }
3728
3729 req->flags |= REQ_F_NEED_CLEANUP;
3730 return 0;
3731}
3732
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003733static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003734{
3735 struct io_rename *ren = &req->rename;
3736 int ret;
3737
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003738 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003739 return -EAGAIN;
3740
3741 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3742 ren->newpath, ren->flags);
3743
3744 req->flags &= ~REQ_F_NEED_CLEANUP;
3745 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003746 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003747 io_req_complete(req, ret);
3748 return 0;
3749}
3750
Jens Axboe14a11432020-09-28 14:27:37 -06003751static int io_unlinkat_prep(struct io_kiocb *req,
3752 const struct io_uring_sqe *sqe)
3753{
3754 struct io_unlink *un = &req->unlink;
3755 const char __user *fname;
3756
Jens Axboe22634bc2021-06-23 09:07:45 -06003757 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3758 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003759 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3760 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003761 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003762 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3763 return -EBADF;
3764
3765 un->dfd = READ_ONCE(sqe->fd);
3766
3767 un->flags = READ_ONCE(sqe->unlink_flags);
3768 if (un->flags & ~AT_REMOVEDIR)
3769 return -EINVAL;
3770
3771 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3772 un->filename = getname(fname);
3773 if (IS_ERR(un->filename))
3774 return PTR_ERR(un->filename);
3775
3776 req->flags |= REQ_F_NEED_CLEANUP;
3777 return 0;
3778}
3779
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003780static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003781{
3782 struct io_unlink *un = &req->unlink;
3783 int ret;
3784
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003785 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003786 return -EAGAIN;
3787
3788 if (un->flags & AT_REMOVEDIR)
3789 ret = do_rmdir(un->dfd, un->filename);
3790 else
3791 ret = do_unlinkat(un->dfd, un->filename);
3792
3793 req->flags &= ~REQ_F_NEED_CLEANUP;
3794 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003795 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003796 io_req_complete(req, ret);
3797 return 0;
3798}
3799
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003800static int io_mkdirat_prep(struct io_kiocb *req,
3801 const struct io_uring_sqe *sqe)
3802{
3803 struct io_mkdir *mkd = &req->mkdir;
3804 const char __user *fname;
3805
3806 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3807 return -EINVAL;
3808 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3809 sqe->splice_fd_in)
3810 return -EINVAL;
3811 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3812 return -EBADF;
3813
3814 mkd->dfd = READ_ONCE(sqe->fd);
3815 mkd->mode = READ_ONCE(sqe->len);
3816
3817 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3818 mkd->filename = getname(fname);
3819 if (IS_ERR(mkd->filename))
3820 return PTR_ERR(mkd->filename);
3821
3822 req->flags |= REQ_F_NEED_CLEANUP;
3823 return 0;
3824}
3825
Pavel Begunkov04f34082021-10-14 16:10:12 +01003826static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003827{
3828 struct io_mkdir *mkd = &req->mkdir;
3829 int ret;
3830
3831 if (issue_flags & IO_URING_F_NONBLOCK)
3832 return -EAGAIN;
3833
3834 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3835
3836 req->flags &= ~REQ_F_NEED_CLEANUP;
3837 if (ret < 0)
3838 req_set_fail(req);
3839 io_req_complete(req, ret);
3840 return 0;
3841}
3842
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003843static int io_symlinkat_prep(struct io_kiocb *req,
3844 const struct io_uring_sqe *sqe)
3845{
3846 struct io_symlink *sl = &req->symlink;
3847 const char __user *oldpath, *newpath;
3848
3849 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3850 return -EINVAL;
3851 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3852 sqe->splice_fd_in)
3853 return -EINVAL;
3854 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3855 return -EBADF;
3856
3857 sl->new_dfd = READ_ONCE(sqe->fd);
3858 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3859 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3860
3861 sl->oldpath = getname(oldpath);
3862 if (IS_ERR(sl->oldpath))
3863 return PTR_ERR(sl->oldpath);
3864
3865 sl->newpath = getname(newpath);
3866 if (IS_ERR(sl->newpath)) {
3867 putname(sl->oldpath);
3868 return PTR_ERR(sl->newpath);
3869 }
3870
3871 req->flags |= REQ_F_NEED_CLEANUP;
3872 return 0;
3873}
3874
Pavel Begunkov04f34082021-10-14 16:10:12 +01003875static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003876{
3877 struct io_symlink *sl = &req->symlink;
3878 int ret;
3879
3880 if (issue_flags & IO_URING_F_NONBLOCK)
3881 return -EAGAIN;
3882
3883 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3884
3885 req->flags &= ~REQ_F_NEED_CLEANUP;
3886 if (ret < 0)
3887 req_set_fail(req);
3888 io_req_complete(req, ret);
3889 return 0;
3890}
3891
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003892static int io_linkat_prep(struct io_kiocb *req,
3893 const struct io_uring_sqe *sqe)
3894{
3895 struct io_hardlink *lnk = &req->hardlink;
3896 const char __user *oldf, *newf;
3897
3898 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3899 return -EINVAL;
3900 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3901 return -EINVAL;
3902 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3903 return -EBADF;
3904
3905 lnk->old_dfd = READ_ONCE(sqe->fd);
3906 lnk->new_dfd = READ_ONCE(sqe->len);
3907 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3908 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3909 lnk->flags = READ_ONCE(sqe->hardlink_flags);
3910
3911 lnk->oldpath = getname(oldf);
3912 if (IS_ERR(lnk->oldpath))
3913 return PTR_ERR(lnk->oldpath);
3914
3915 lnk->newpath = getname(newf);
3916 if (IS_ERR(lnk->newpath)) {
3917 putname(lnk->oldpath);
3918 return PTR_ERR(lnk->newpath);
3919 }
3920
3921 req->flags |= REQ_F_NEED_CLEANUP;
3922 return 0;
3923}
3924
Pavel Begunkov04f34082021-10-14 16:10:12 +01003925static int io_linkat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003926{
3927 struct io_hardlink *lnk = &req->hardlink;
3928 int ret;
3929
3930 if (issue_flags & IO_URING_F_NONBLOCK)
3931 return -EAGAIN;
3932
3933 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
3934 lnk->newpath, lnk->flags);
3935
3936 req->flags &= ~REQ_F_NEED_CLEANUP;
3937 if (ret < 0)
3938 req_set_fail(req);
3939 io_req_complete(req, ret);
3940 return 0;
3941}
3942
Jens Axboe36f4fa62020-09-05 11:14:22 -06003943static int io_shutdown_prep(struct io_kiocb *req,
3944 const struct io_uring_sqe *sqe)
3945{
3946#if defined(CONFIG_NET)
3947 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3948 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003949 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3950 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003951 return -EINVAL;
3952
3953 req->shutdown.how = READ_ONCE(sqe->len);
3954 return 0;
3955#else
3956 return -EOPNOTSUPP;
3957#endif
3958}
3959
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003960static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003961{
3962#if defined(CONFIG_NET)
3963 struct socket *sock;
3964 int ret;
3965
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003966 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003967 return -EAGAIN;
3968
Linus Torvalds48aba792020-12-16 12:44:05 -08003969 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003970 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003971 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003972
3973 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003974 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003975 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003976 io_req_complete(req, ret);
3977 return 0;
3978#else
3979 return -EOPNOTSUPP;
3980#endif
3981}
3982
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003983static int __io_splice_prep(struct io_kiocb *req,
3984 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003985{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003986 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003987 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003988
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003989 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3990 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003991
3992 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003993 sp->len = READ_ONCE(sqe->len);
3994 sp->flags = READ_ONCE(sqe->splice_flags);
3995
3996 if (unlikely(sp->flags & ~valid_flags))
3997 return -EINVAL;
3998
Pavel Begunkov62906e82021-08-10 14:52:47 +01003999 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01004000 (sp->flags & SPLICE_F_FD_IN_FIXED));
4001 if (!sp->file_in)
4002 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004003 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004004 return 0;
4005}
4006
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004007static int io_tee_prep(struct io_kiocb *req,
4008 const struct io_uring_sqe *sqe)
4009{
4010 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
4011 return -EINVAL;
4012 return __io_splice_prep(req, sqe);
4013}
4014
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004015static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004016{
4017 struct io_splice *sp = &req->splice;
4018 struct file *in = sp->file_in;
4019 struct file *out = sp->file_out;
4020 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4021 long ret = 0;
4022
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004023 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004024 return -EAGAIN;
4025 if (sp->len)
4026 ret = do_tee(in, out, sp->len, flags);
4027
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004028 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4029 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004030 req->flags &= ~REQ_F_NEED_CLEANUP;
4031
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004032 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004033 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004034 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004035 return 0;
4036}
4037
4038static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4039{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004040 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004041
4042 sp->off_in = READ_ONCE(sqe->splice_off_in);
4043 sp->off_out = READ_ONCE(sqe->off);
4044 return __io_splice_prep(req, sqe);
4045}
4046
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004047static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004048{
4049 struct io_splice *sp = &req->splice;
4050 struct file *in = sp->file_in;
4051 struct file *out = sp->file_out;
4052 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4053 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004054 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004055
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004056 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004057 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004058
4059 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4060 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004061
Jens Axboe948a7742020-05-17 14:21:38 -06004062 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004063 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004064
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004065 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4066 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004067 req->flags &= ~REQ_F_NEED_CLEANUP;
4068
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004069 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004070 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004071 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004072 return 0;
4073}
4074
Jens Axboe2b188cc2019-01-07 10:46:33 -07004075/*
4076 * IORING_OP_NOP just posts a completion event, nothing else.
4077 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004078static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004079{
4080 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004081
Jens Axboedef596e2019-01-09 08:59:42 -07004082 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4083 return -EINVAL;
4084
Pavel Begunkov889fca72021-02-10 00:03:09 +00004085 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004086 return 0;
4087}
4088
Pavel Begunkov1155c762021-02-18 18:29:38 +00004089static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004090{
Jens Axboe6b063142019-01-10 22:13:58 -07004091 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004092
Jens Axboe09bb8392019-03-13 12:39:28 -06004093 if (!req->file)
4094 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004095
Jens Axboe6b063142019-01-10 22:13:58 -07004096 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004097 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004098 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4099 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004100 return -EINVAL;
4101
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004102 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4103 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4104 return -EINVAL;
4105
4106 req->sync.off = READ_ONCE(sqe->off);
4107 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004108 return 0;
4109}
4110
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004111static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004112{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004113 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004114 int ret;
4115
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004116 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004117 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004118 return -EAGAIN;
4119
Jens Axboe9adbd452019-12-20 08:45:55 -07004120 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004121 end > 0 ? end : LLONG_MAX,
4122 req->sync.flags & IORING_FSYNC_DATASYNC);
4123 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004124 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004125 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004126 return 0;
4127}
4128
Jens Axboed63d1b52019-12-10 10:38:56 -07004129static int io_fallocate_prep(struct io_kiocb *req,
4130 const struct io_uring_sqe *sqe)
4131{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004132 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4133 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004134 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004135 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4136 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004137
4138 req->sync.off = READ_ONCE(sqe->off);
4139 req->sync.len = READ_ONCE(sqe->addr);
4140 req->sync.mode = READ_ONCE(sqe->len);
4141 return 0;
4142}
4143
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004144static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004145{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004146 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004147
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004148 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004149 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004150 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004151 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4152 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004153 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004154 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004155 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004156 return 0;
4157}
4158
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004159static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004160{
Jens Axboef8748882020-01-08 17:47:02 -07004161 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004162 int ret;
4163
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004164 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4165 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004166 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004167 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004168 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004169 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004170
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004171 /* open.how should be already initialised */
4172 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004173 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004174
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004175 req->open.dfd = READ_ONCE(sqe->fd);
4176 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004177 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004178 if (IS_ERR(req->open.filename)) {
4179 ret = PTR_ERR(req->open.filename);
4180 req->open.filename = NULL;
4181 return ret;
4182 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004183
4184 req->open.file_slot = READ_ONCE(sqe->file_index);
4185 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4186 return -EINVAL;
4187
Jens Axboe4022e7a2020-03-19 19:23:18 -06004188 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004189 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004190 return 0;
4191}
4192
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004193static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4194{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004195 u64 mode = READ_ONCE(sqe->len);
4196 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004197
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004198 req->open.how = build_open_how(flags, mode);
4199 return __io_openat_prep(req, sqe);
4200}
4201
Jens Axboecebdb982020-01-08 17:59:24 -07004202static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4203{
4204 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004205 size_t len;
4206 int ret;
4207
Jens Axboecebdb982020-01-08 17:59:24 -07004208 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4209 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004210 if (len < OPEN_HOW_SIZE_VER0)
4211 return -EINVAL;
4212
4213 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4214 len);
4215 if (ret)
4216 return ret;
4217
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004218 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004219}
4220
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004221static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004222{
4223 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004224 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004225 bool resolve_nonblock, nonblock_set;
4226 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004227 int ret;
4228
Jens Axboecebdb982020-01-08 17:59:24 -07004229 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004230 if (ret)
4231 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004232 nonblock_set = op.open_flag & O_NONBLOCK;
4233 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004234 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004235 /*
4236 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4237 * it'll always -EAGAIN
4238 */
4239 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4240 return -EAGAIN;
4241 op.lookup_flags |= LOOKUP_CACHED;
4242 op.open_flag |= O_NONBLOCK;
4243 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004244
Pavel Begunkovb9445592021-08-25 12:25:45 +01004245 if (!fixed) {
4246 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4247 if (ret < 0)
4248 goto err;
4249 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004250
4251 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004252 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004253 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004254 * We could hang on to this 'fd' on retrying, but seems like
4255 * marginal gain for something that is now known to be a slower
4256 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004257 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004258 if (!fixed)
4259 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004260
4261 ret = PTR_ERR(file);
4262 /* only retry if RESOLVE_CACHED wasn't already set by application */
4263 if (ret == -EAGAIN &&
4264 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4265 return -EAGAIN;
4266 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004267 }
4268
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004269 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4270 file->f_flags &= ~O_NONBLOCK;
4271 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004272
4273 if (!fixed)
4274 fd_install(ret, file);
4275 else
4276 ret = io_install_fixed_file(req, file, issue_flags,
4277 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004278err:
4279 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004280 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004281 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004282 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004283 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004284 return 0;
4285}
4286
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004287static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004288{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004289 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004290}
4291
Jens Axboe067524e2020-03-02 16:32:28 -07004292static int io_remove_buffers_prep(struct io_kiocb *req,
4293 const struct io_uring_sqe *sqe)
4294{
4295 struct io_provide_buf *p = &req->pbuf;
4296 u64 tmp;
4297
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004298 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4299 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004300 return -EINVAL;
4301
4302 tmp = READ_ONCE(sqe->fd);
4303 if (!tmp || tmp > USHRT_MAX)
4304 return -EINVAL;
4305
4306 memset(p, 0, sizeof(*p));
4307 p->nbufs = tmp;
4308 p->bgid = READ_ONCE(sqe->buf_group);
4309 return 0;
4310}
4311
4312static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4313 int bgid, unsigned nbufs)
4314{
4315 unsigned i = 0;
4316
4317 /* shouldn't happen */
4318 if (!nbufs)
4319 return 0;
4320
4321 /* the head kbuf is the list itself */
4322 while (!list_empty(&buf->list)) {
4323 struct io_buffer *nxt;
4324
4325 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4326 list_del(&nxt->list);
4327 kfree(nxt);
4328 if (++i == nbufs)
4329 return i;
4330 }
4331 i++;
4332 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004333 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004334
4335 return i;
4336}
4337
Pavel Begunkov889fca72021-02-10 00:03:09 +00004338static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004339{
4340 struct io_provide_buf *p = &req->pbuf;
4341 struct io_ring_ctx *ctx = req->ctx;
4342 struct io_buffer *head;
4343 int ret = 0;
Hao Xu3b44b372021-10-18 21:34:31 +08004344 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboe067524e2020-03-02 16:32:28 -07004345
Hao Xu3b44b372021-10-18 21:34:31 +08004346 io_ring_submit_lock(ctx, needs_lock);
Jens Axboe067524e2020-03-02 16:32:28 -07004347
4348 lockdep_assert_held(&ctx->uring_lock);
4349
4350 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004351 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004352 if (head)
4353 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004354 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004355 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004356
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004357 /* complete before unlock, IOPOLL may need the lock */
4358 __io_req_complete(req, issue_flags, ret, 0);
Hao Xu3b44b372021-10-18 21:34:31 +08004359 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboe067524e2020-03-02 16:32:28 -07004360 return 0;
4361}
4362
Jens Axboeddf0322d2020-02-23 16:41:33 -07004363static int io_provide_buffers_prep(struct io_kiocb *req,
4364 const struct io_uring_sqe *sqe)
4365{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004366 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004367 struct io_provide_buf *p = &req->pbuf;
4368 u64 tmp;
4369
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004370 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004371 return -EINVAL;
4372
4373 tmp = READ_ONCE(sqe->fd);
4374 if (!tmp || tmp > USHRT_MAX)
4375 return -E2BIG;
4376 p->nbufs = tmp;
4377 p->addr = READ_ONCE(sqe->addr);
4378 p->len = READ_ONCE(sqe->len);
4379
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004380 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4381 &size))
4382 return -EOVERFLOW;
4383 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4384 return -EOVERFLOW;
4385
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004386 size = (unsigned long)p->len * p->nbufs;
4387 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004388 return -EFAULT;
4389
4390 p->bgid = READ_ONCE(sqe->buf_group);
4391 tmp = READ_ONCE(sqe->off);
4392 if (tmp > USHRT_MAX)
4393 return -E2BIG;
4394 p->bid = tmp;
4395 return 0;
4396}
4397
4398static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4399{
4400 struct io_buffer *buf;
4401 u64 addr = pbuf->addr;
4402 int i, bid = pbuf->bid;
4403
4404 for (i = 0; i < pbuf->nbufs; i++) {
Jens Axboe9990da92021-09-24 07:39:08 -06004405 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004406 if (!buf)
4407 break;
4408
4409 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004410 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004411 buf->bid = bid;
4412 addr += pbuf->len;
4413 bid++;
4414 if (!*head) {
4415 INIT_LIST_HEAD(&buf->list);
4416 *head = buf;
4417 } else {
4418 list_add_tail(&buf->list, &(*head)->list);
4419 }
4420 }
4421
4422 return i ? i : -ENOMEM;
4423}
4424
Pavel Begunkov889fca72021-02-10 00:03:09 +00004425static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004426{
4427 struct io_provide_buf *p = &req->pbuf;
4428 struct io_ring_ctx *ctx = req->ctx;
4429 struct io_buffer *head, *list;
4430 int ret = 0;
Hao Xu3b44b372021-10-18 21:34:31 +08004431 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004432
Hao Xu3b44b372021-10-18 21:34:31 +08004433 io_ring_submit_lock(ctx, needs_lock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004434
4435 lockdep_assert_held(&ctx->uring_lock);
4436
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004437 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004438
4439 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004440 if (ret >= 0 && !list) {
4441 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4442 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004443 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004444 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004445 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004446 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004447 /* complete before unlock, IOPOLL may need the lock */
4448 __io_req_complete(req, issue_flags, ret, 0);
Hao Xu3b44b372021-10-18 21:34:31 +08004449 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004450 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004451}
4452
Jens Axboe3e4827b2020-01-08 15:18:09 -07004453static int io_epoll_ctl_prep(struct io_kiocb *req,
4454 const struct io_uring_sqe *sqe)
4455{
4456#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004457 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004458 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004459 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004460 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004461
4462 req->epoll.epfd = READ_ONCE(sqe->fd);
4463 req->epoll.op = READ_ONCE(sqe->len);
4464 req->epoll.fd = READ_ONCE(sqe->off);
4465
4466 if (ep_op_has_event(req->epoll.op)) {
4467 struct epoll_event __user *ev;
4468
4469 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4470 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4471 return -EFAULT;
4472 }
4473
4474 return 0;
4475#else
4476 return -EOPNOTSUPP;
4477#endif
4478}
4479
Pavel Begunkov889fca72021-02-10 00:03:09 +00004480static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004481{
4482#if defined(CONFIG_EPOLL)
4483 struct io_epoll *ie = &req->epoll;
4484 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004485 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004486
4487 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4488 if (force_nonblock && ret == -EAGAIN)
4489 return -EAGAIN;
4490
4491 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004492 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004493 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004494 return 0;
4495#else
4496 return -EOPNOTSUPP;
4497#endif
4498}
4499
Jens Axboec1ca7572019-12-25 22:18:28 -07004500static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4501{
4502#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004503 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004504 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004505 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4506 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004507
4508 req->madvise.addr = READ_ONCE(sqe->addr);
4509 req->madvise.len = READ_ONCE(sqe->len);
4510 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4511 return 0;
4512#else
4513 return -EOPNOTSUPP;
4514#endif
4515}
4516
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004517static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004518{
4519#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4520 struct io_madvise *ma = &req->madvise;
4521 int ret;
4522
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004523 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004524 return -EAGAIN;
4525
Minchan Kim0726b012020-10-17 16:14:50 -07004526 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004527 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004528 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004529 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004530 return 0;
4531#else
4532 return -EOPNOTSUPP;
4533#endif
4534}
4535
Jens Axboe4840e412019-12-25 22:03:45 -07004536static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4537{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004538 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004539 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004540 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4541 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004542
4543 req->fadvise.offset = READ_ONCE(sqe->off);
4544 req->fadvise.len = READ_ONCE(sqe->len);
4545 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4546 return 0;
4547}
4548
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004549static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004550{
4551 struct io_fadvise *fa = &req->fadvise;
4552 int ret;
4553
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004554 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004555 switch (fa->advice) {
4556 case POSIX_FADV_NORMAL:
4557 case POSIX_FADV_RANDOM:
4558 case POSIX_FADV_SEQUENTIAL:
4559 break;
4560 default:
4561 return -EAGAIN;
4562 }
4563 }
Jens Axboe4840e412019-12-25 22:03:45 -07004564
4565 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4566 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004567 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004568 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004569 return 0;
4570}
4571
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004572static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4573{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004574 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004575 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004576 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004577 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004578 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004579 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004580
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004581 req->statx.dfd = READ_ONCE(sqe->fd);
4582 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004583 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004584 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4585 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004586
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004587 return 0;
4588}
4589
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004590static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004591{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004592 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004593 int ret;
4594
Pavel Begunkov59d70012021-03-22 01:58:30 +00004595 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004596 return -EAGAIN;
4597
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004598 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4599 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004600
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004601 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004602 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004603 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004604 return 0;
4605}
4606
Jens Axboeb5dba592019-12-11 14:02:38 -07004607static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4608{
Jens Axboe14587a462020-09-05 11:36:08 -06004609 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004610 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004611 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004612 sqe->rw_flags || sqe->buf_index)
Jens Axboeb5dba592019-12-11 14:02:38 -07004613 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004614 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004615 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004616
4617 req->close.fd = READ_ONCE(sqe->fd);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004618 req->close.file_slot = READ_ONCE(sqe->file_index);
4619 if (req->close.file_slot && req->close.fd)
4620 return -EINVAL;
4621
Jens Axboeb5dba592019-12-11 14:02:38 -07004622 return 0;
4623}
4624
Pavel Begunkov889fca72021-02-10 00:03:09 +00004625static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004626{
Jens Axboe9eac1902021-01-19 15:50:37 -07004627 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004628 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004629 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004630 struct file *file = NULL;
4631 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004632
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004633 if (req->close.file_slot) {
4634 ret = io_close_fixed(req, issue_flags);
4635 goto err;
4636 }
4637
Jens Axboe9eac1902021-01-19 15:50:37 -07004638 spin_lock(&files->file_lock);
4639 fdt = files_fdtable(files);
4640 if (close->fd >= fdt->max_fds) {
4641 spin_unlock(&files->file_lock);
4642 goto err;
4643 }
4644 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004645 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004646 spin_unlock(&files->file_lock);
4647 file = NULL;
4648 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004649 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004650
4651 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004652 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004653 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004654 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004655 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004656
Jens Axboe9eac1902021-01-19 15:50:37 -07004657 ret = __close_fd_get_file(close->fd, &file);
4658 spin_unlock(&files->file_lock);
4659 if (ret < 0) {
4660 if (ret == -ENOENT)
4661 ret = -EBADF;
4662 goto err;
4663 }
4664
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004665 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004666 ret = filp_close(file, current->files);
4667err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004668 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004669 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004670 if (file)
4671 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004672 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004673 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004674}
4675
Pavel Begunkov1155c762021-02-18 18:29:38 +00004676static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004677{
4678 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004679
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004680 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4681 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004682 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4683 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004684 return -EINVAL;
4685
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004686 req->sync.off = READ_ONCE(sqe->off);
4687 req->sync.len = READ_ONCE(sqe->len);
4688 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004689 return 0;
4690}
4691
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004692static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004693{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004694 int ret;
4695
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004696 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004697 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004698 return -EAGAIN;
4699
Jens Axboe9adbd452019-12-20 08:45:55 -07004700 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004701 req->sync.flags);
4702 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004703 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004704 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004705 return 0;
4706}
4707
YueHaibing469956e2020-03-04 15:53:52 +08004708#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004709static int io_setup_async_msg(struct io_kiocb *req,
4710 struct io_async_msghdr *kmsg)
4711{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004712 struct io_async_msghdr *async_msg = req->async_data;
4713
4714 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004715 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004716 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004717 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004718 return -ENOMEM;
4719 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004720 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004721 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004722 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004723 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004724 /* if were using fast_iov, set it to the new one */
4725 if (!async_msg->free_iov)
4726 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4727
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004728 return -EAGAIN;
4729}
4730
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004731static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4732 struct io_async_msghdr *iomsg)
4733{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004734 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004735 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004736 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004737 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004738}
4739
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004740static int io_sendmsg_prep_async(struct io_kiocb *req)
4741{
4742 int ret;
4743
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004744 ret = io_sendmsg_copy_hdr(req, req->async_data);
4745 if (!ret)
4746 req->flags |= REQ_F_NEED_CLEANUP;
4747 return ret;
4748}
4749
Jens Axboe3529d8c2019-12-19 18:24:38 -07004750static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004751{
Jens Axboee47293f2019-12-20 08:58:21 -07004752 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004753
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004754 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4755 return -EINVAL;
4756
Pavel Begunkov270a5942020-07-12 20:41:04 +03004757 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004758 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004759 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4760 if (sr->msg_flags & MSG_DONTWAIT)
4761 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004762
Jens Axboed8768362020-02-27 14:17:49 -07004763#ifdef CONFIG_COMPAT
4764 if (req->ctx->compat)
4765 sr->msg_flags |= MSG_CMSG_COMPAT;
4766#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004767 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004768}
4769
Pavel Begunkov889fca72021-02-10 00:03:09 +00004770static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004771{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004772 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004773 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004774 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004775 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004776 int ret;
4777
Florent Revestdba4a922020-12-04 12:36:04 +01004778 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004779 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004780 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004781
Pavel Begunkovd886e182021-10-04 20:02:56 +01004782 if (req_has_async_data(req)) {
4783 kmsg = req->async_data;
4784 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004785 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004786 if (ret)
4787 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004788 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004789 }
4790
Pavel Begunkov04411802021-04-01 15:44:00 +01004791 flags = req->sr_msg.msg_flags;
4792 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004793 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004794 if (flags & MSG_WAITALL)
4795 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4796
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004797 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004798 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004799 return io_setup_async_msg(req, kmsg);
4800 if (ret == -ERESTARTSYS)
4801 ret = -EINTR;
4802
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004803 /* fast path, check for non-NULL to avoid function call */
4804 if (kmsg->free_iov)
4805 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004806 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004807 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004808 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004809 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004810 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004811}
4812
Pavel Begunkov889fca72021-02-10 00:03:09 +00004813static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004814{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004815 struct io_sr_msg *sr = &req->sr_msg;
4816 struct msghdr msg;
4817 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004818 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004819 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004820 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004821 int ret;
4822
Florent Revestdba4a922020-12-04 12:36:04 +01004823 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004824 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004825 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004826
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004827 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4828 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004829 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004830
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004831 msg.msg_name = NULL;
4832 msg.msg_control = NULL;
4833 msg.msg_controllen = 0;
4834 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004835
Pavel Begunkov04411802021-04-01 15:44:00 +01004836 flags = req->sr_msg.msg_flags;
4837 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004838 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004839 if (flags & MSG_WAITALL)
4840 min_ret = iov_iter_count(&msg.msg_iter);
4841
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004842 msg.msg_flags = flags;
4843 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004844 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004845 return -EAGAIN;
4846 if (ret == -ERESTARTSYS)
4847 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004848
Stefan Metzmacher00312752021-03-20 20:33:36 +01004849 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004850 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004851 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004852 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004853}
4854
Pavel Begunkov1400e692020-07-12 20:41:05 +03004855static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4856 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004857{
4858 struct io_sr_msg *sr = &req->sr_msg;
4859 struct iovec __user *uiov;
4860 size_t iov_len;
4861 int ret;
4862
Pavel Begunkov1400e692020-07-12 20:41:05 +03004863 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4864 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004865 if (ret)
4866 return ret;
4867
4868 if (req->flags & REQ_F_BUFFER_SELECT) {
4869 if (iov_len > 1)
4870 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004871 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004872 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004873 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004874 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004875 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004876 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004877 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004878 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004879 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004880 if (ret > 0)
4881 ret = 0;
4882 }
4883
4884 return ret;
4885}
4886
4887#ifdef CONFIG_COMPAT
4888static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004889 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004890{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004891 struct io_sr_msg *sr = &req->sr_msg;
4892 struct compat_iovec __user *uiov;
4893 compat_uptr_t ptr;
4894 compat_size_t len;
4895 int ret;
4896
Pavel Begunkov4af34172021-04-11 01:46:30 +01004897 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4898 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004899 if (ret)
4900 return ret;
4901
4902 uiov = compat_ptr(ptr);
4903 if (req->flags & REQ_F_BUFFER_SELECT) {
4904 compat_ssize_t clen;
4905
4906 if (len > 1)
4907 return -EINVAL;
4908 if (!access_ok(uiov, sizeof(*uiov)))
4909 return -EFAULT;
4910 if (__get_user(clen, &uiov->iov_len))
4911 return -EFAULT;
4912 if (clen < 0)
4913 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004914 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004915 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004916 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004917 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004918 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004919 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004920 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004921 if (ret < 0)
4922 return ret;
4923 }
4924
4925 return 0;
4926}
Jens Axboe03b12302019-12-02 18:50:25 -07004927#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004928
Pavel Begunkov1400e692020-07-12 20:41:05 +03004929static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4930 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004931{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004932 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004933
4934#ifdef CONFIG_COMPAT
4935 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004936 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004937#endif
4938
Pavel Begunkov1400e692020-07-12 20:41:05 +03004939 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004940}
4941
Jens Axboebcda7ba2020-02-23 16:42:51 -07004942static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov51aac422021-10-14 16:10:17 +01004943 unsigned int issue_flags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004944{
4945 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004946
Pavel Begunkov51aac422021-10-14 16:10:17 +01004947 return io_buffer_select(req, &sr->len, sr->bgid, issue_flags);
Jens Axboe03b12302019-12-02 18:50:25 -07004948}
4949
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004950static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4951{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01004952 return io_put_kbuf(req, req->kbuf);
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004953}
4954
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004955static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004956{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004957 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004958
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004959 ret = io_recvmsg_copy_hdr(req, req->async_data);
4960 if (!ret)
4961 req->flags |= REQ_F_NEED_CLEANUP;
4962 return ret;
4963}
4964
4965static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4966{
4967 struct io_sr_msg *sr = &req->sr_msg;
4968
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004969 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4970 return -EINVAL;
4971
Pavel Begunkov270a5942020-07-12 20:41:04 +03004972 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004973 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004974 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004975 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4976 if (sr->msg_flags & MSG_DONTWAIT)
4977 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004978
Jens Axboed8768362020-02-27 14:17:49 -07004979#ifdef CONFIG_COMPAT
4980 if (req->ctx->compat)
4981 sr->msg_flags |= MSG_CMSG_COMPAT;
4982#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004983 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004984}
4985
Pavel Begunkov889fca72021-02-10 00:03:09 +00004986static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004987{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004988 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004989 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004990 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004991 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004992 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004993 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004994 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004995
Florent Revestdba4a922020-12-04 12:36:04 +01004996 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004997 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004998 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004999
Pavel Begunkovd886e182021-10-04 20:02:56 +01005000 if (req_has_async_data(req)) {
5001 kmsg = req->async_data;
5002 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005003 ret = io_recvmsg_copy_hdr(req, &iomsg);
5004 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03005005 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005006 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005007 }
5008
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005009 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01005010 kbuf = io_recv_buffer_select(req, issue_flags);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005011 if (IS_ERR(kbuf))
5012 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005013 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00005014 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
5015 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005016 1, req->sr_msg.len);
5017 }
5018
Pavel Begunkov04411802021-04-01 15:44:00 +01005019 flags = req->sr_msg.msg_flags;
5020 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005021 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005022 if (flags & MSG_WAITALL)
5023 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
5024
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005025 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
5026 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005027 if (force_nonblock && ret == -EAGAIN)
5028 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005029 if (ret == -ERESTARTSYS)
5030 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005031
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005032 if (req->flags & REQ_F_BUFFER_SELECTED)
5033 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005034 /* fast path, check for non-NULL to avoid function call */
5035 if (kmsg->free_iov)
5036 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005037 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005038 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005039 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005040 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005041 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005042}
5043
Pavel Begunkov889fca72021-02-10 00:03:09 +00005044static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07005045{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005046 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005047 struct io_sr_msg *sr = &req->sr_msg;
5048 struct msghdr msg;
5049 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005050 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005051 struct iovec iov;
5052 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005053 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005054 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005055 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005056
Florent Revestdba4a922020-12-04 12:36:04 +01005057 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005058 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005059 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005060
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005061 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01005062 kbuf = io_recv_buffer_select(req, issue_flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005063 if (IS_ERR(kbuf))
5064 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005065 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005066 }
5067
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005068 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005069 if (unlikely(ret))
5070 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005071
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005072 msg.msg_name = NULL;
5073 msg.msg_control = NULL;
5074 msg.msg_controllen = 0;
5075 msg.msg_namelen = 0;
5076 msg.msg_iocb = NULL;
5077 msg.msg_flags = 0;
5078
Pavel Begunkov04411802021-04-01 15:44:00 +01005079 flags = req->sr_msg.msg_flags;
5080 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005081 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005082 if (flags & MSG_WAITALL)
5083 min_ret = iov_iter_count(&msg.msg_iter);
5084
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005085 ret = sock_recvmsg(sock, &msg, flags);
5086 if (force_nonblock && ret == -EAGAIN)
5087 return -EAGAIN;
5088 if (ret == -ERESTARTSYS)
5089 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005090out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005091 if (req->flags & REQ_F_BUFFER_SELECTED)
5092 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01005093 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005094 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005095 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005096 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005097}
5098
Jens Axboe3529d8c2019-12-19 18:24:38 -07005099static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005100{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005101 struct io_accept *accept = &req->accept;
5102
Jens Axboe14587a462020-09-05 11:36:08 -06005103 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005104 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005105 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005106 return -EINVAL;
5107
Jens Axboed55e5f52019-12-11 16:12:15 -07005108 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5109 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005110 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005111 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005112
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005113 accept->file_slot = READ_ONCE(sqe->file_index);
5114 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5115 (accept->flags & SOCK_CLOEXEC)))
5116 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005117 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5118 return -EINVAL;
5119 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5120 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005121 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005122}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005123
Pavel Begunkov889fca72021-02-10 00:03:09 +00005124static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005125{
5126 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005127 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005128 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005129 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005130 struct file *file;
5131 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005132
Jiufei Xuee697dee2020-06-10 13:41:59 +08005133 if (req->file->f_flags & O_NONBLOCK)
5134 req->flags |= REQ_F_NOWAIT;
5135
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005136 if (!fixed) {
5137 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5138 if (unlikely(fd < 0))
5139 return fd;
5140 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005141 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5142 accept->flags);
5143 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005144 if (!fixed)
5145 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005146 ret = PTR_ERR(file);
5147 if (ret == -EAGAIN && force_nonblock)
5148 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005149 if (ret == -ERESTARTSYS)
5150 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005151 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005152 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005153 fd_install(fd, file);
5154 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005155 } else {
5156 ret = io_install_fixed_file(req, file, issue_flags,
5157 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005158 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005159 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005160 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005161}
5162
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005163static int io_connect_prep_async(struct io_kiocb *req)
5164{
5165 struct io_async_connect *io = req->async_data;
5166 struct io_connect *conn = &req->connect;
5167
5168 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5169}
5170
Jens Axboe3529d8c2019-12-19 18:24:38 -07005171static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005172{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005173 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005174
Jens Axboe14587a462020-09-05 11:36:08 -06005175 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005176 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005177 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5178 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005179 return -EINVAL;
5180
Jens Axboe3529d8c2019-12-19 18:24:38 -07005181 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5182 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005183 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005184}
5185
Pavel Begunkov889fca72021-02-10 00:03:09 +00005186static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005187{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005188 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005189 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005190 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005191 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005192
Pavel Begunkovd886e182021-10-04 20:02:56 +01005193 if (req_has_async_data(req)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005194 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005195 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005196 ret = move_addr_to_kernel(req->connect.addr,
5197 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005198 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005199 if (ret)
5200 goto out;
5201 io = &__io;
5202 }
5203
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005204 file_flags = force_nonblock ? O_NONBLOCK : 0;
5205
Jens Axboee8c2bc12020-08-15 18:44:09 -07005206 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005207 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005208 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Pavel Begunkovd886e182021-10-04 20:02:56 +01005209 if (req_has_async_data(req))
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005210 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005211 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005212 ret = -ENOMEM;
5213 goto out;
5214 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005215 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005216 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005217 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005218 if (ret == -ERESTARTSYS)
5219 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005220out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005221 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005222 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005223 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005224 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005225}
YueHaibing469956e2020-03-04 15:53:52 +08005226#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005227#define IO_NETOP_FN(op) \
5228static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5229{ \
5230 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005231}
5232
Jens Axboe99a10082021-02-19 09:35:19 -07005233#define IO_NETOP_PREP(op) \
5234IO_NETOP_FN(op) \
5235static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5236{ \
5237 return -EOPNOTSUPP; \
5238} \
5239
5240#define IO_NETOP_PREP_ASYNC(op) \
5241IO_NETOP_PREP(op) \
5242static int io_##op##_prep_async(struct io_kiocb *req) \
5243{ \
5244 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005245}
5246
Jens Axboe99a10082021-02-19 09:35:19 -07005247IO_NETOP_PREP_ASYNC(sendmsg);
5248IO_NETOP_PREP_ASYNC(recvmsg);
5249IO_NETOP_PREP_ASYNC(connect);
5250IO_NETOP_PREP(accept);
5251IO_NETOP_FN(send);
5252IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005253#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005254
Jens Axboed7718a92020-02-14 22:23:12 -07005255struct io_poll_table {
5256 struct poll_table_struct pt;
5257 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005258 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005259 int error;
5260};
5261
Jens Axboed7718a92020-02-14 22:23:12 -07005262static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005263 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07005264{
Jens Axboed7718a92020-02-14 22:23:12 -07005265 /* for instances that support it check for an event match first: */
5266 if (mask && !(mask & poll->events))
5267 return 0;
5268
5269 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5270
5271 list_del_init(&poll->wait.entry);
5272
Jens Axboed7718a92020-02-14 22:23:12 -07005273 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005274 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005275
Jens Axboed7718a92020-02-14 22:23:12 -07005276 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005277 * If this fails, then the task is exiting. When a task exits, the
5278 * work gets canceled, so just cancel this request as well instead
5279 * of executing it. We can't safely execute it anyway, as we may not
5280 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005281 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005282 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005283 return 1;
5284}
5285
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005286static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5287 __acquires(&req->ctx->completion_lock)
5288{
5289 struct io_ring_ctx *ctx = req->ctx;
5290
Jens Axboe316319e2021-08-19 09:41:42 -06005291 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005292 if (unlikely(req->task->flags & PF_EXITING))
5293 WRITE_ONCE(poll->canceled, true);
5294
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005295 if (!req->result && !READ_ONCE(poll->canceled)) {
5296 struct poll_table_struct pt = { ._key = poll->events };
5297
5298 req->result = vfs_poll(req->file, &pt) & poll->events;
5299 }
5300
Jens Axboe79ebeae2021-08-10 15:18:27 -06005301 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005302 if (!req->result && !READ_ONCE(poll->canceled)) {
5303 add_wait_queue(poll->head, &poll->wait);
5304 return true;
5305 }
5306
5307 return false;
5308}
5309
Jens Axboed4e7cd32020-08-15 11:44:50 -07005310static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005311{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005312 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005313 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005314 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005315 return req->apoll->double_poll;
5316}
5317
5318static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5319{
5320 if (req->opcode == IORING_OP_POLL_ADD)
5321 return &req->poll;
5322 return &req->apoll->poll;
5323}
5324
5325static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005326 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005327{
5328 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005329
5330 lockdep_assert_held(&req->ctx->completion_lock);
5331
5332 if (poll && poll->head) {
5333 struct wait_queue_head *head = poll->head;
5334
Jens Axboe79ebeae2021-08-10 15:18:27 -06005335 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005336 list_del_init(&poll->wait.entry);
5337 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005338 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005339 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005340 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005341 }
5342}
5343
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005344static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005345 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005346{
5347 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005348 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005349 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005350
Pavel Begunkove27414b2021-04-09 09:13:20 +01005351 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005352 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005353 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005354 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005355 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005356 }
Jens Axboeb69de282021-03-17 08:37:41 -06005357 if (req->poll.events & EPOLLONESHOT)
5358 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005359 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
5360 req->poll.events |= EPOLLONESHOT;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005361 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005362 }
Hao Xu7b289c32021-04-13 15:20:39 +08005363 if (flags & IORING_CQE_F_MORE)
5364 ctx->cq_extra++;
5365
Jens Axboe88e41cf2021-02-22 22:08:01 -07005366 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005367}
5368
Pavel Begunkovf237c302021-08-18 12:42:46 +01005369static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005370{
Jens Axboe6d816e02020-08-11 08:04:14 -06005371 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005372 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005373
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005374 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005375 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005376 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005377 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005378
Hao Xu5b7aa382021-09-22 18:12:38 +08005379 if (req->poll.done) {
5380 spin_unlock(&ctx->completion_lock);
5381 return;
5382 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005383 done = __io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005384 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005385 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005386 hash_del(&req->hash_node);
Hao Xubd99c712021-09-22 18:12:36 +08005387 req->poll.done = true;
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005388 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005389 req->result = 0;
5390 add_wait_queue(req->poll.head, &req->poll.wait);
5391 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005392 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005393 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005394 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005395
Jens Axboe88e41cf2021-02-22 22:08:01 -07005396 if (done) {
5397 nxt = io_put_req_find_next(req);
5398 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005399 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005400 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005401 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005402}
5403
5404static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5405 int sync, void *key)
5406{
5407 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005408 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005409 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005410 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005411
5412 /* for instances that support it check for an event match first: */
5413 if (mask && !(mask & poll->events))
5414 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005415 if (!(poll->events & EPOLLONESHOT))
5416 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005417
Jens Axboe8706e042020-09-28 08:38:54 -06005418 list_del_init(&wait->entry);
5419
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005420 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005421 bool done;
5422
Jens Axboe79ebeae2021-08-10 15:18:27 -06005423 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005424 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005425 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005426 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005427 /* make sure double remove sees this as being gone */
5428 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005429 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005430 if (!done) {
5431 /* use wait func handler, so it matches the rq type */
5432 poll->wait.func(&poll->wait, mode, sync, key);
5433 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005434 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005435 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005436 return 1;
5437}
5438
5439static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5440 wait_queue_func_t wake_func)
5441{
5442 poll->head = NULL;
5443 poll->done = false;
5444 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005445#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5446 /* mask in events that we always want/need */
5447 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005448 INIT_LIST_HEAD(&poll->wait.entry);
5449 init_waitqueue_func_entry(&poll->wait, wake_func);
5450}
5451
5452static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005453 struct wait_queue_head *head,
5454 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005455{
5456 struct io_kiocb *req = pt->req;
5457
5458 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005459 * The file being polled uses multiple waitqueues for poll handling
5460 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5461 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005462 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005463 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005464 struct io_poll_iocb *poll_one = poll;
5465
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005466 /* double add on the same waitqueue head, ignore */
5467 if (poll_one->head == head)
5468 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005469 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005470 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005471 if ((*poll_ptr)->head == head)
5472 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005473 pt->error = -EINVAL;
5474 return;
5475 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005476 /*
5477 * Can't handle multishot for double wait for now, turn it
5478 * into one-shot mode.
5479 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005480 if (!(poll_one->events & EPOLLONESHOT))
5481 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005482 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5483 if (!poll) {
5484 pt->error = -ENOMEM;
5485 return;
5486 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005487 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005488 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005489 poll->wait.private = req;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005490
Jens Axboe807abcb2020-07-17 17:09:27 -06005491 *poll_ptr = poll;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005492 if (req->opcode == IORING_OP_POLL_ADD)
5493 req->flags |= REQ_F_ASYNC_DATA;
Jens Axboe18bceab2020-05-15 11:56:54 -06005494 }
5495
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005496 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005497 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005498
5499 if (poll->events & EPOLLEXCLUSIVE)
5500 add_wait_queue_exclusive(head, &poll->wait);
5501 else
5502 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005503}
5504
5505static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5506 struct poll_table_struct *p)
5507{
5508 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005509 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005510
Jens Axboe807abcb2020-07-17 17:09:27 -06005511 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005512}
5513
Pavel Begunkovf237c302021-08-18 12:42:46 +01005514static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005515{
Jens Axboed7718a92020-02-14 22:23:12 -07005516 struct async_poll *apoll = req->apoll;
5517 struct io_ring_ctx *ctx = req->ctx;
5518
Olivier Langlois236daeae2021-05-31 02:36:37 -04005519 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005520
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005521 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005522 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005523 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005524 }
5525
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005526 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005527 io_poll_remove_double(req);
Hao Xubd99c712021-09-22 18:12:36 +08005528 apoll->poll.done = true;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005529 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005530
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005531 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005532 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005533 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005534 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005535}
5536
5537static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5538 void *key)
5539{
5540 struct io_kiocb *req = wait->private;
5541 struct io_poll_iocb *poll = &req->apoll->poll;
5542
5543 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5544 key_to_poll(key));
5545
5546 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5547}
5548
5549static void io_poll_req_insert(struct io_kiocb *req)
5550{
5551 struct io_ring_ctx *ctx = req->ctx;
5552 struct hlist_head *list;
5553
5554 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5555 hlist_add_head(&req->hash_node, list);
5556}
5557
5558static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5559 struct io_poll_iocb *poll,
5560 struct io_poll_table *ipt, __poll_t mask,
5561 wait_queue_func_t wake_func)
5562 __acquires(&ctx->completion_lock)
5563{
5564 struct io_ring_ctx *ctx = req->ctx;
5565 bool cancel = false;
5566
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005567 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005568 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005569 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005570 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005571
5572 ipt->pt._key = mask;
5573 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005574 ipt->error = 0;
5575 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005576
Jens Axboed7718a92020-02-14 22:23:12 -07005577 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005578 if (unlikely(!ipt->nr_entries) && !ipt->error)
5579 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005580
Jens Axboe79ebeae2021-08-10 15:18:27 -06005581 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005582 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005583 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005584 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005585 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005586 if (unlikely(list_empty(&poll->wait.entry))) {
5587 if (ipt->error)
5588 cancel = true;
5589 ipt->error = 0;
5590 mask = 0;
5591 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005592 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005593 list_del_init(&poll->wait.entry);
5594 else if (cancel)
5595 WRITE_ONCE(poll->canceled, true);
5596 else if (!poll->done) /* actually waiting for an event */
5597 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005598 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005599 }
5600
5601 return mask;
5602}
5603
Olivier Langlois59b735a2021-06-22 05:17:39 -07005604enum {
5605 IO_APOLL_OK,
5606 IO_APOLL_ABORTED,
5607 IO_APOLL_READY
5608};
5609
5610static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005611{
5612 const struct io_op_def *def = &io_op_defs[req->opcode];
5613 struct io_ring_ctx *ctx = req->ctx;
5614 struct async_poll *apoll;
5615 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005616 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboed7718a92020-02-14 22:23:12 -07005617
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005618 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005619 return IO_APOLL_ABORTED;
Pavel Begunkov658d0a42021-10-23 12:13:58 +01005620 if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED))
5621 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005622
5623 if (def->pollin) {
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005624 mask |= POLLIN | POLLRDNORM;
5625
5626 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5627 if ((req->opcode == IORING_OP_RECVMSG) &&
5628 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5629 mask &= ~POLLIN;
5630 } else {
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005631 mask |= POLLOUT | POLLWRNORM;
5632 }
5633
Jens Axboed7718a92020-02-14 22:23:12 -07005634 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5635 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005636 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005637 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005638 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005639 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005640 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005641 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005642
5643 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5644 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005645 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005646 if (ret || ipt.error)
5647 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5648
Olivier Langlois236daeae2021-05-31 02:36:37 -04005649 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5650 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005651 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005652}
5653
5654static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005655 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005656 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005657{
Jens Axboeb41e9852020-02-17 09:52:41 -07005658 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005659
Jens Axboe50826202021-02-23 09:02:26 -07005660 if (!poll->head)
5661 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005662 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005663 if (do_cancel)
5664 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005665 if (!list_empty(&poll->wait.entry)) {
5666 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005667 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005668 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005669 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005670 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005671 return do_complete;
5672}
5673
Pavel Begunkov5d709042021-08-09 20:18:13 +01005674static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005675 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005676{
5677 bool do_complete;
5678
Jens Axboed4e7cd32020-08-15 11:44:50 -07005679 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005680 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005681
Jens Axboeb41e9852020-02-17 09:52:41 -07005682 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005683 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005684 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005685 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005686 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005687 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005688 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005689}
5690
Jens Axboe76e1b642020-09-26 15:05:03 -06005691/*
5692 * Returns true if we found and killed one or more poll requests
5693 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01005694static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx,
5695 struct task_struct *tsk, bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005696{
Jens Axboe78076bb2019-12-04 19:56:40 -07005697 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005698 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005699 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005700
Jens Axboe79ebeae2021-08-10 15:18:27 -06005701 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005702 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5703 struct hlist_head *list;
5704
5705 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005706 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005707 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005708 posted += io_poll_remove_one(req);
5709 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005710 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005711 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005712
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005713 if (posted)
5714 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005715
5716 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005717}
5718
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005719static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5720 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005721 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005722{
Jens Axboe78076bb2019-12-04 19:56:40 -07005723 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005724 struct io_kiocb *req;
5725
Jens Axboe78076bb2019-12-04 19:56:40 -07005726 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5727 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005728 if (sqe_addr != req->user_data)
5729 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005730 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5731 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005732 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005733 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005734 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005735}
5736
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005737static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5738 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005739 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005740{
5741 struct io_kiocb *req;
5742
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005743 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005744 if (!req)
5745 return -ENOENT;
5746 if (io_poll_remove_one(req))
5747 return 0;
5748
5749 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005750}
5751
Pavel Begunkov9096af32021-04-14 13:38:36 +01005752static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5753 unsigned int flags)
5754{
5755 u32 events;
5756
5757 events = READ_ONCE(sqe->poll32_events);
5758#ifdef __BIG_ENDIAN
5759 events = swahw32(events);
5760#endif
5761 if (!(flags & IORING_POLL_ADD_MULTI))
5762 events |= EPOLLONESHOT;
5763 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5764}
5765
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005766static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005767 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005768{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005769 struct io_poll_update *upd = &req->poll_update;
5770 u32 flags;
5771
Jens Axboe221c5eb2019-01-17 09:41:58 -07005772 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5773 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005774 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005775 return -EINVAL;
5776 flags = READ_ONCE(sqe->len);
5777 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5778 IORING_POLL_ADD_MULTI))
5779 return -EINVAL;
5780 /* meaningless without update */
5781 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005782 return -EINVAL;
5783
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005784 upd->old_user_data = READ_ONCE(sqe->addr);
5785 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5786 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005787
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005788 upd->new_user_data = READ_ONCE(sqe->off);
5789 if (!upd->update_user_data && upd->new_user_data)
5790 return -EINVAL;
5791 if (upd->update_events)
5792 upd->events = io_poll_parse_events(sqe, flags);
5793 else if (sqe->poll32_events)
5794 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005795
Jens Axboe221c5eb2019-01-17 09:41:58 -07005796 return 0;
5797}
5798
Jens Axboe221c5eb2019-01-17 09:41:58 -07005799static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5800 void *key)
5801{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005802 struct io_kiocb *req = wait->private;
5803 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005804
Jens Axboed7718a92020-02-14 22:23:12 -07005805 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005806}
5807
Jens Axboe221c5eb2019-01-17 09:41:58 -07005808static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5809 struct poll_table_struct *p)
5810{
5811 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5812
Jens Axboee8c2bc12020-08-15 18:44:09 -07005813 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005814}
5815
Jens Axboe3529d8c2019-12-19 18:24:38 -07005816static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005817{
5818 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005819 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005820
5821 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5822 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005823 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005824 return -EINVAL;
5825 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005826 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005827 return -EINVAL;
5828
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005829 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005830 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005831 return 0;
5832}
5833
Pavel Begunkov61e98202021-02-10 00:03:08 +00005834static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005835{
5836 struct io_poll_iocb *poll = &req->poll;
5837 struct io_ring_ctx *ctx = req->ctx;
5838 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005839 __poll_t mask;
Hao Xu5b7aa382021-09-22 18:12:38 +08005840 bool done;
Jens Axboe0969e782019-12-17 18:40:57 -07005841
Jens Axboed7718a92020-02-14 22:23:12 -07005842 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005843
Jens Axboed7718a92020-02-14 22:23:12 -07005844 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5845 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005846
Jens Axboe8c838782019-03-12 15:48:16 -06005847 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005848 ipt.error = 0;
Pavel Begunkoveb6e6f02021-10-04 20:02:59 +01005849 done = __io_poll_complete(req, mask);
5850 io_commit_cqring(req->ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06005851 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005852 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005853
Jens Axboe8c838782019-03-12 15:48:16 -06005854 if (mask) {
5855 io_cqring_ev_posted(ctx);
Hao Xu5b7aa382021-09-22 18:12:38 +08005856 if (done)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005857 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005858 }
Jens Axboe8c838782019-03-12 15:48:16 -06005859 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005860}
5861
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005862static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005863{
5864 struct io_ring_ctx *ctx = req->ctx;
5865 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005866 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005867 int ret;
5868
Jens Axboe79ebeae2021-08-10 15:18:27 -06005869 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005870 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005871 if (!preq) {
5872 ret = -ENOENT;
5873 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005874 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005875
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005876 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5877 completing = true;
5878 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5879 goto err;
5880 }
5881
Jens Axboecb3b200e2021-04-06 09:49:31 -06005882 /*
5883 * Don't allow racy completion with singleshot, as we cannot safely
5884 * update those. For multishot, if we're racing with completion, just
5885 * let completion re-add it.
5886 */
5887 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5888 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5889 ret = -EALREADY;
5890 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005891 }
5892 /* we now have a detached poll request. reissue. */
5893 ret = 0;
5894err:
Jens Axboeb69de282021-03-17 08:37:41 -06005895 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005896 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005897 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005898 io_req_complete(req, ret);
5899 return 0;
5900 }
5901 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005902 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005903 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005904 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005905 preq->poll.events |= IO_POLL_UNMASK;
5906 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005907 if (req->poll_update.update_user_data)
5908 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005909 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005910
Jens Axboeb69de282021-03-17 08:37:41 -06005911 /* complete update request, we're done with it */
5912 io_req_complete(req, ret);
5913
Jens Axboecb3b200e2021-04-06 09:49:31 -06005914 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005915 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005916 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005917 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005918 io_req_complete(preq, ret);
5919 }
Jens Axboeb69de282021-03-17 08:37:41 -06005920 }
5921 return 0;
5922}
5923
Pavel Begunkovf237c302021-08-18 12:42:46 +01005924static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005925{
Pavel Begunkov62245902021-10-02 19:36:14 +01005926 struct io_timeout_data *data = req->async_data;
5927
5928 if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
5929 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005930 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005931}
5932
Jens Axboe5262f562019-09-17 12:26:57 -06005933static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5934{
Jens Axboead8a48a2019-11-15 08:49:11 -07005935 struct io_timeout_data *data = container_of(timer,
5936 struct io_timeout_data, timer);
5937 struct io_kiocb *req = data->req;
5938 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005939 unsigned long flags;
5940
Jens Axboe89850fc2021-08-10 15:11:51 -06005941 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005942 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005943 atomic_set(&req->ctx->cq_timeouts,
5944 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005945 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005946
Jens Axboe89850fc2021-08-10 15:11:51 -06005947 req->io_task_work.func = io_req_task_timeout;
5948 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005949 return HRTIMER_NORESTART;
5950}
5951
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005952static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5953 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005954 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005955{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005956 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005957 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005958 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005959
5960 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005961 found = user_data == req->user_data;
5962 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005963 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005964 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005965 if (!found)
5966 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005967
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005968 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005969 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005970 return ERR_PTR(-EALREADY);
5971 list_del_init(&req->timeout.list);
5972 return req;
5973}
5974
5975static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005976 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005977 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005978{
5979 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5980
5981 if (IS_ERR(req))
5982 return PTR_ERR(req);
5983
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005984 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005985 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005986 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005987 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005988}
5989
Jens Axboe50c1df22021-08-27 17:11:06 -06005990static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
5991{
5992 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
5993 case IORING_TIMEOUT_BOOTTIME:
5994 return CLOCK_BOOTTIME;
5995 case IORING_TIMEOUT_REALTIME:
5996 return CLOCK_REALTIME;
5997 default:
5998 /* can't happen, vetted at prep time */
5999 WARN_ON_ONCE(1);
6000 fallthrough;
6001 case 0:
6002 return CLOCK_MONOTONIC;
6003 }
6004}
6005
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006006static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6007 struct timespec64 *ts, enum hrtimer_mode mode)
6008 __must_hold(&ctx->timeout_lock)
6009{
6010 struct io_timeout_data *io;
6011 struct io_kiocb *req;
6012 bool found = false;
6013
6014 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
6015 found = user_data == req->user_data;
6016 if (found)
6017 break;
6018 }
6019 if (!found)
6020 return -ENOENT;
6021
6022 io = req->async_data;
6023 if (hrtimer_try_to_cancel(&io->timer) == -1)
6024 return -EALREADY;
6025 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
6026 io->timer.function = io_link_timeout_fn;
6027 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
6028 return 0;
6029}
6030
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006031static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6032 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06006033 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006034{
6035 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6036 struct io_timeout_data *data;
6037
6038 if (IS_ERR(req))
6039 return PTR_ERR(req);
6040
6041 req->timeout.off = 0; /* noseq */
6042 data = req->async_data;
6043 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06006044 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006045 data->timer.function = io_timeout_fn;
6046 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
6047 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07006048}
6049
Jens Axboe3529d8c2019-12-19 18:24:38 -07006050static int io_timeout_remove_prep(struct io_kiocb *req,
6051 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07006052{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006053 struct io_timeout_rem *tr = &req->timeout_rem;
6054
Jens Axboeb29472e2019-12-17 18:50:29 -07006055 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6056 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006057 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6058 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006059 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07006060 return -EINVAL;
6061
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006062 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006063 tr->addr = READ_ONCE(sqe->addr);
6064 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006065 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6066 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6067 return -EINVAL;
6068 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6069 tr->ltimeout = true;
6070 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006071 return -EINVAL;
6072 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6073 return -EFAULT;
6074 } else if (tr->flags) {
6075 /* timeout removal doesn't support flags */
6076 return -EINVAL;
6077 }
6078
Jens Axboeb29472e2019-12-17 18:50:29 -07006079 return 0;
6080}
6081
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006082static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6083{
6084 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6085 : HRTIMER_MODE_REL;
6086}
6087
Jens Axboe11365042019-10-16 09:08:32 -06006088/*
6089 * Remove or update an existing timeout command
6090 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006091static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006092{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006093 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006094 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006095 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006096
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006097 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6098 spin_lock(&ctx->completion_lock);
6099 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006100 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006101 spin_unlock_irq(&ctx->timeout_lock);
6102 spin_unlock(&ctx->completion_lock);
6103 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006104 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6105
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006106 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006107 if (tr->ltimeout)
6108 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6109 else
6110 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006111 spin_unlock_irq(&ctx->timeout_lock);
6112 }
Jens Axboe11365042019-10-16 09:08:32 -06006113
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006114 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006115 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006116 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006117 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006118}
6119
Jens Axboe3529d8c2019-12-19 18:24:38 -07006120static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006121 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006122{
Jens Axboead8a48a2019-11-15 08:49:11 -07006123 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006124 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006125 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006126
Jens Axboead8a48a2019-11-15 08:49:11 -07006127 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006128 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006129 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6130 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006131 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006132 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006133 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006134 flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkov62245902021-10-02 19:36:14 +01006135 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
6136 IORING_TIMEOUT_ETIME_SUCCESS))
Jens Axboe50c1df22021-08-27 17:11:06 -06006137 return -EINVAL;
6138 /* more than one clock specified is invalid, obviously */
6139 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006140 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006141
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006142 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006143 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006144 if (unlikely(off && !req->ctx->off_timeout_used))
6145 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006146
Pavel Begunkovd6a644a2021-10-23 12:14:00 +01006147 if (WARN_ON_ONCE(req_has_async_data(req)))
6148 return -EFAULT;
6149 if (io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006150 return -ENOMEM;
6151
Jens Axboee8c2bc12020-08-15 18:44:09 -07006152 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006153 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006154 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006155
6156 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006157 return -EFAULT;
6158
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006159 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006160 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006161
6162 if (is_timeout_link) {
6163 struct io_submit_link *link = &req->ctx->submit_state.link;
6164
6165 if (!link->head)
6166 return -EINVAL;
6167 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6168 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006169 req->timeout.head = link->last;
6170 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006171 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006172 return 0;
6173}
6174
Pavel Begunkov61e98202021-02-10 00:03:08 +00006175static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006176{
Jens Axboead8a48a2019-11-15 08:49:11 -07006177 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006178 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006179 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006180 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006181
Jens Axboe89850fc2021-08-10 15:11:51 -06006182 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006183
Jens Axboe5262f562019-09-17 12:26:57 -06006184 /*
6185 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006186 * timeout event to be satisfied. If it isn't set, then this is
6187 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006188 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006189 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006190 entry = ctx->timeout_list.prev;
6191 goto add;
6192 }
Jens Axboe5262f562019-09-17 12:26:57 -06006193
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006194 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6195 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006196
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006197 /* Update the last seq here in case io_flush_timeouts() hasn't.
6198 * This is safe because ->completion_lock is held, and submissions
6199 * and completions are never mixed in the same ->completion_lock section.
6200 */
6201 ctx->cq_last_tm_flush = tail;
6202
Jens Axboe5262f562019-09-17 12:26:57 -06006203 /*
6204 * Insertion sort, ensuring the first entry in the list is always
6205 * the one we need first.
6206 */
Jens Axboe5262f562019-09-17 12:26:57 -06006207 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006208 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6209 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006210
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006211 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006212 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006213 /* nxt.seq is behind @tail, otherwise would've been completed */
6214 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006215 break;
6216 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006217add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006218 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006219 data->timer.function = io_timeout_fn;
6220 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006221 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006222 return 0;
6223}
6224
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006225struct io_cancel_data {
6226 struct io_ring_ctx *ctx;
6227 u64 user_data;
6228};
6229
Jens Axboe62755e32019-10-28 21:49:21 -06006230static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006231{
Jens Axboe62755e32019-10-28 21:49:21 -06006232 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006233 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006234
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006235 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006236}
6237
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006238static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6239 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006240{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006241 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006242 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006243 int ret = 0;
6244
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006245 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006246 return -ENOENT;
6247
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006248 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006249 switch (cancel_ret) {
6250 case IO_WQ_CANCEL_OK:
6251 ret = 0;
6252 break;
6253 case IO_WQ_CANCEL_RUNNING:
6254 ret = -EALREADY;
6255 break;
6256 case IO_WQ_CANCEL_NOTFOUND:
6257 ret = -ENOENT;
6258 break;
6259 }
6260
Jens Axboee977d6d2019-11-05 12:39:45 -07006261 return ret;
6262}
6263
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006264static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006265{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006266 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006267 int ret;
6268
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006269 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006270
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006271 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01006272 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006273 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006274
6275 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006276 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006277 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006278 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006279 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01006280 goto out;
6281 ret = io_poll_cancel(ctx, sqe_addr, false);
6282out:
6283 spin_unlock(&ctx->completion_lock);
6284 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006285}
6286
Jens Axboe3529d8c2019-12-19 18:24:38 -07006287static int io_async_cancel_prep(struct io_kiocb *req,
6288 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006289{
Jens Axboefbf23842019-12-17 18:45:56 -07006290 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006291 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006292 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6293 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006294 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6295 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006296 return -EINVAL;
6297
Jens Axboefbf23842019-12-17 18:45:56 -07006298 req->cancel.addr = READ_ONCE(sqe->addr);
6299 return 0;
6300}
6301
Pavel Begunkov61e98202021-02-10 00:03:08 +00006302static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006303{
6304 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006305 u64 sqe_addr = req->cancel.addr;
Hao Xu3b44b372021-10-18 21:34:31 +08006306 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006307 struct io_tctx_node *node;
6308 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006309
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006310 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006311 if (ret != -ENOENT)
6312 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006313
6314 /* slow path, try all io-wq's */
Hao Xu3b44b372021-10-18 21:34:31 +08006315 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006316 ret = -ENOENT;
6317 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6318 struct io_uring_task *tctx = node->task->io_uring;
6319
Pavel Begunkov58f99372021-03-12 16:25:55 +00006320 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6321 if (ret != -ENOENT)
6322 break;
6323 }
Hao Xu3b44b372021-10-18 21:34:31 +08006324 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006325done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006326 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006327 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006328 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006329 return 0;
6330}
6331
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006332static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006333 const struct io_uring_sqe *sqe)
6334{
Daniele Albano61710e42020-07-18 14:15:16 -06006335 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6336 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006337 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006338 return -EINVAL;
6339
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006340 req->rsrc_update.offset = READ_ONCE(sqe->off);
6341 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6342 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006343 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006344 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006345 return 0;
6346}
6347
Pavel Begunkov889fca72021-02-10 00:03:09 +00006348static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006349{
6350 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08006351 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006352 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006353 int ret;
6354
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006355 up.offset = req->rsrc_update.offset;
6356 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006357 up.nr = 0;
6358 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006359 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006360
Hao Xu3b44b372021-10-18 21:34:31 +08006361 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006362 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006363 &up, req->rsrc_update.nr_args);
Hao Xu3b44b372021-10-18 21:34:31 +08006364 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006365
6366 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006367 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006368 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006369 return 0;
6370}
6371
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006372static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006373{
Jens Axboed625c6e2019-12-17 19:53:05 -07006374 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006375 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006376 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006377 case IORING_OP_READV:
6378 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006379 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006380 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006381 case IORING_OP_WRITEV:
6382 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006383 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006384 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006385 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006386 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006387 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006388 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006389 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006390 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006391 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006392 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006393 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006394 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006395 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006396 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006397 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006398 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006399 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006400 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006401 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006402 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006403 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006404 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006405 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006406 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006407 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006408 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006409 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006410 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006411 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006412 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006413 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006414 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006415 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006416 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006417 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006418 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006419 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006420 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006421 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006422 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006423 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006424 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006425 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006426 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006427 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006428 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006429 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006430 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006431 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006432 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006433 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006434 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006435 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006436 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006437 case IORING_OP_SHUTDOWN:
6438 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006439 case IORING_OP_RENAMEAT:
6440 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006441 case IORING_OP_UNLINKAT:
6442 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006443 case IORING_OP_MKDIRAT:
6444 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006445 case IORING_OP_SYMLINKAT:
6446 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006447 case IORING_OP_LINKAT:
6448 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006449 }
6450
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006451 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6452 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006453 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006454}
6455
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006456static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006457{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006458 if (!io_op_defs[req->opcode].needs_async_setup)
6459 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01006460 if (WARN_ON_ONCE(req_has_async_data(req)))
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006461 return -EFAULT;
6462 if (io_alloc_async_data(req))
6463 return -EAGAIN;
6464
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006465 switch (req->opcode) {
6466 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006467 return io_rw_prep_async(req, READ);
6468 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006469 return io_rw_prep_async(req, WRITE);
6470 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006471 return io_sendmsg_prep_async(req);
6472 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006473 return io_recvmsg_prep_async(req);
6474 case IORING_OP_CONNECT:
6475 return io_connect_prep_async(req);
6476 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006477 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6478 req->opcode);
6479 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006480}
6481
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006482static u32 io_get_sequence(struct io_kiocb *req)
6483{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006484 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006485
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006486 /* need original cached_sq_head, but it was increased for each req */
6487 io_for_each_link(req, req)
6488 seq--;
6489 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006490}
6491
Pavel Begunkovc0724812021-10-04 20:02:54 +01006492static __cold void io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006493{
6494 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006495 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006496 int ret;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006497 u32 seq = io_get_sequence(req);
Pavel Begunkov3c199662021-06-15 16:47:57 +01006498
Jens Axboedef596e2019-01-09 08:59:42 -07006499 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov5e371262021-09-24 22:00:04 +01006500 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) {
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006501queue:
Pavel Begunkov10c66902021-06-15 16:47:56 +01006502 ctx->drain_active = false;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006503 io_req_task_queue(req);
6504 return;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006505 }
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006506
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006507 ret = io_req_prep_async(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006508 if (ret) {
6509fail:
6510 io_req_complete_failed(req, ret);
6511 return;
6512 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006513 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006514 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006515 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006516 ret = -ENOMEM;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006517 goto fail;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006518 }
Jens Axboe31b51512019-01-18 22:56:34 -07006519
Jens Axboe79ebeae2021-08-10 15:18:27 -06006520 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006521 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006522 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006523 kfree(de);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006524 goto queue;
Jens Axboe31b51512019-01-18 22:56:34 -07006525 }
6526
6527 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006528 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006529 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006530 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006531 spin_unlock(&ctx->completion_lock);
Jens Axboe31b51512019-01-18 22:56:34 -07006532}
6533
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006534static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006535{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006536 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01006537 kfree(req->kbuf);
6538 req->kbuf = NULL;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006539 }
6540
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006541 if (req->flags & REQ_F_NEED_CLEANUP) {
6542 switch (req->opcode) {
6543 case IORING_OP_READV:
6544 case IORING_OP_READ_FIXED:
6545 case IORING_OP_READ:
6546 case IORING_OP_WRITEV:
6547 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006548 case IORING_OP_WRITE: {
6549 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006550
6551 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006552 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006553 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006554 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006555 case IORING_OP_SENDMSG: {
6556 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006557
6558 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006559 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006560 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006561 case IORING_OP_SPLICE:
6562 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006563 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6564 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006565 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006566 case IORING_OP_OPENAT:
6567 case IORING_OP_OPENAT2:
6568 if (req->open.filename)
6569 putname(req->open.filename);
6570 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006571 case IORING_OP_RENAMEAT:
6572 putname(req->rename.oldpath);
6573 putname(req->rename.newpath);
6574 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006575 case IORING_OP_UNLINKAT:
6576 putname(req->unlink.filename);
6577 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006578 case IORING_OP_MKDIRAT:
6579 putname(req->mkdir.filename);
6580 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006581 case IORING_OP_SYMLINKAT:
6582 putname(req->symlink.oldpath);
6583 putname(req->symlink.newpath);
6584 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006585 case IORING_OP_LINKAT:
6586 putname(req->hardlink.oldpath);
6587 putname(req->hardlink.newpath);
6588 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006589 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006590 }
Jens Axboe75652a302021-04-15 09:52:40 -06006591 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6592 kfree(req->apoll->double_poll);
6593 kfree(req->apoll);
6594 req->apoll = NULL;
6595 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006596 if (req->flags & REQ_F_INFLIGHT) {
6597 struct io_uring_task *tctx = req->task->io_uring;
6598
6599 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006600 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006601 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006602 put_cred(req->creds);
Pavel Begunkovd886e182021-10-04 20:02:56 +01006603 if (req->flags & REQ_F_ASYNC_DATA) {
6604 kfree(req->async_data);
6605 req->async_data = NULL;
6606 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006607 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006608}
6609
Pavel Begunkov889fca72021-02-10 00:03:09 +00006610static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006611{
Jens Axboe5730b272021-02-27 15:57:30 -07006612 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006613 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006614
Pavel Begunkov6878b402021-09-24 21:59:41 +01006615 if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006616 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006617
Paul Moore5bd21822021-02-16 19:46:48 -05006618 if (!io_op_defs[req->opcode].audit_skip)
6619 audit_uring_entry(req->opcode);
6620
Jens Axboed625c6e2019-12-17 19:53:05 -07006621 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006622 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006623 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006624 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006625 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006626 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006627 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006628 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006629 break;
6630 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006631 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006632 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006633 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006634 break;
6635 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006636 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006637 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006638 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006639 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006640 break;
6641 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006642 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006643 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006644 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006645 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006646 break;
6647 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006648 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006649 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006650 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006651 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006652 break;
6653 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006654 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006655 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006656 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006657 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006658 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006659 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006660 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006661 break;
6662 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006663 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006664 break;
6665 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006666 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006667 break;
6668 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006669 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006670 break;
6671 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006672 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006673 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006674 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006675 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006676 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006677 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006678 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006679 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006680 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006681 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006682 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006683 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006684 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006685 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006686 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006687 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006688 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006689 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006690 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006691 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006692 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006693 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006694 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006695 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006696 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006697 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006698 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006699 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006700 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006701 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006702 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006703 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006704 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006705 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006706 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006707 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006708 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006709 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006710 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006711 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006712 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006713 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006714 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006715 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006716 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006717 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006718 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006719 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006720 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006721 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006722 case IORING_OP_MKDIRAT:
6723 ret = io_mkdirat(req, issue_flags);
6724 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006725 case IORING_OP_SYMLINKAT:
6726 ret = io_symlinkat(req, issue_flags);
6727 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006728 case IORING_OP_LINKAT:
6729 ret = io_linkat(req, issue_flags);
6730 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006731 default:
6732 ret = -EINVAL;
6733 break;
6734 }
Jens Axboe31b51512019-01-18 22:56:34 -07006735
Paul Moore5bd21822021-02-16 19:46:48 -05006736 if (!io_op_defs[req->opcode].audit_skip)
6737 audit_uring_exit(!ret, ret);
6738
Jens Axboe5730b272021-02-27 15:57:30 -07006739 if (creds)
6740 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006741 if (ret)
6742 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006743 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkov99830282021-10-15 17:09:11 +01006744 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file)
Pavel Begunkov98821312021-10-15 17:09:12 +01006745 io_iopoll_req_issued(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006746
6747 return 0;
6748}
6749
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006750static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6751{
6752 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6753
6754 req = io_put_req_find_next(req);
6755 return req ? &req->work : NULL;
6756}
6757
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006758static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006759{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006760 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006761 unsigned int issue_flags = IO_URING_F_UNLOCKED;
6762 bool needs_poll = false;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006763 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006764 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006765
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006766 /* one will be dropped by ->io_free_work() after returning to io-wq */
6767 if (!(req->flags & REQ_F_REFCOUNT))
6768 __io_req_set_refcount(req, 2);
6769 else
6770 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006771
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006772 timeout = io_prep_linked_timeout(req);
6773 if (timeout)
6774 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006775
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006776 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006777 if (work->flags & IO_WQ_WORK_CANCEL) {
6778 io_req_task_queue_fail(req, -ECANCELED);
6779 return;
Jens Axboe561fb042019-10-24 07:25:42 -06006780 }
Jens Axboe31b51512019-01-18 22:56:34 -07006781
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006782 if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovafb7f56f2021-10-23 12:13:59 +01006783 const struct io_op_def *def = &io_op_defs[req->opcode];
6784 bool opcode_poll = def->pollin || def->pollout;
6785
6786 if (opcode_poll && file_can_poll(req->file)) {
6787 needs_poll = true;
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006788 issue_flags |= IO_URING_F_NONBLOCK;
Pavel Begunkovafb7f56f2021-10-23 12:13:59 +01006789 }
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006790 }
Hao Xu90fa0282021-10-18 21:34:45 +08006791
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006792 do {
6793 ret = io_issue_sqe(req, issue_flags);
6794 if (ret != -EAGAIN)
6795 break;
6796 /*
6797 * We can get EAGAIN for iopolled IO even though we're
6798 * forcing a sync submission from here, since we can't
6799 * wait for request slots on the block side.
6800 */
6801 if (!needs_poll) {
6802 cond_resched();
6803 continue;
Hao Xu90fa0282021-10-18 21:34:45 +08006804 }
6805
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006806 if (io_arm_poll_handler(req) == IO_APOLL_OK)
6807 return;
6808 /* aborted or ready, in either case retry blocking */
6809 needs_poll = false;
6810 issue_flags &= ~IO_URING_F_NONBLOCK;
6811 } while (1);
Jens Axboe31b51512019-01-18 22:56:34 -07006812
Pavel Begunkova3df76982021-02-18 22:32:52 +00006813 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006814 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006815 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006816}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006817
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006818static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006819 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006820{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006821 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006822}
6823
Jens Axboe09bb8392019-03-13 12:39:28 -06006824static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6825 int index)
6826{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006827 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006828
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006829 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006830}
6831
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006832static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006833{
6834 unsigned long file_ptr = (unsigned long) file;
6835
Pavel Begunkov88459b52021-10-17 00:07:10 +01006836 file_ptr |= io_file_get_flags(file);
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006837 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006838}
6839
Pavel Begunkovac177052021-08-09 13:04:02 +01006840static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6841 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006842{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006843 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006844 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006845
Pavel Begunkovac177052021-08-09 13:04:02 +01006846 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6847 return NULL;
6848 fd = array_index_nospec(fd, ctx->nr_user_files);
6849 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6850 file = (struct file *) (file_ptr & FFS_MASK);
6851 file_ptr &= ~FFS_MASK;
6852 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01006853 req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
Pavel Begunkova46be972021-10-09 23:14:40 +01006854 io_req_set_rsrc_node(req, ctx);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006855 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006856}
6857
Pavel Begunkovac177052021-08-09 13:04:02 +01006858static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006859 struct io_kiocb *req, int fd)
6860{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006861 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006862
6863 trace_io_uring_file_get(ctx, fd);
6864
6865 /* we don't allow fixed io_uring files */
6866 if (file && unlikely(file->f_op == &io_uring_fops))
6867 io_req_track_inflight(req);
6868 return file;
6869}
6870
6871static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006872 struct io_kiocb *req, int fd, bool fixed)
6873{
6874 if (fixed)
6875 return io_file_get_fixed(ctx, req, fd);
6876 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006877 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006878}
6879
Pavel Begunkovf237c302021-08-18 12:42:46 +01006880static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006881{
6882 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006883 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006884
6885 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006886 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006887 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006888 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006889 } else {
6890 io_req_complete_post(req, -ETIME, 0);
6891 }
6892}
6893
Jens Axboe2665abf2019-11-05 12:40:47 -07006894static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6895{
Jens Axboead8a48a2019-11-15 08:49:11 -07006896 struct io_timeout_data *data = container_of(timer,
6897 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006898 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006899 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006900 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006901
Jens Axboe89b263f2021-08-10 15:14:18 -06006902 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006903 prev = req->timeout.head;
6904 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006905
6906 /*
6907 * We don't expect the list to be empty, that will only happen if we
6908 * race with the completion of the linked work.
6909 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006910 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006911 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006912 if (!req_ref_inc_not_zero(prev))
6913 prev = NULL;
6914 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006915 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06006916 req->timeout.prev = prev;
6917 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006918
Jens Axboe89b263f2021-08-10 15:14:18 -06006919 req->io_task_work.func = io_req_task_link_timeout;
6920 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006921 return HRTIMER_NORESTART;
6922}
6923
Pavel Begunkovde968c12021-03-19 17:22:33 +00006924static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006925{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006926 struct io_ring_ctx *ctx = req->ctx;
6927
Jens Axboe89b263f2021-08-10 15:14:18 -06006928 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006929 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006930 * If the back reference is NULL, then our linked request finished
6931 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006932 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006933 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006934 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006935
Jens Axboead8a48a2019-11-15 08:49:11 -07006936 data->timer.function = io_link_timeout_fn;
6937 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6938 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006939 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07006940 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006941 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006942 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006943 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006944}
6945
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006946static void io_queue_sqe_arm_apoll(struct io_kiocb *req)
6947 __must_hold(&req->ctx->uring_lock)
6948{
6949 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
6950
6951 switch (io_arm_poll_handler(req)) {
6952 case IO_APOLL_READY:
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006953 io_req_task_queue(req);
6954 break;
6955 case IO_APOLL_ABORTED:
6956 /*
6957 * Queued up for async execution, worker will release
6958 * submit reference when the iocb is actually submitted.
6959 */
6960 io_queue_async_work(req, NULL);
6961 break;
6962 }
6963
6964 if (linked_timeout)
6965 io_queue_linked_timeout(linked_timeout);
6966}
6967
6968static inline void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006969 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006970{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006971 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006972 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006973
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006974 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006975
Pavel Begunkovfff4e402021-10-04 20:02:48 +01006976 if (req->flags & REQ_F_COMPLETE_INLINE) {
6977 io_req_add_compl_list(req);
Pavel Begunkovd9f9d282021-09-24 22:00:00 +01006978 return;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01006979 }
Jens Axboe491381ce2019-10-17 09:20:46 -06006980 /*
6981 * We async punt it if the file wasn't marked NOWAIT, or if the file
6982 * doesn't support non-blocking read/write attempts
6983 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006984 if (likely(!ret)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006985 linked_timeout = io_prep_linked_timeout(req);
6986 if (linked_timeout)
6987 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006988 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006989 io_queue_sqe_arm_apoll(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006990 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006991 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006992 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006993}
6994
Pavel Begunkov4652fe32021-09-24 21:59:58 +01006995static void io_queue_sqe_fallback(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006996 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006997{
Pavel Begunkov4652fe32021-09-24 21:59:58 +01006998 if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01006999 io_req_complete_fail_submit(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01007000 } else if (unlikely(req->ctx->drain_active)) {
7001 io_drain_req(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01007002 } else {
7003 int ret = io_req_prep_async(req);
7004
7005 if (unlikely(ret))
7006 io_req_complete_failed(req, ret);
7007 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01007008 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07007009 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007010}
7011
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007012static inline void io_queue_sqe(struct io_kiocb *req)
7013 __must_hold(&req->ctx->uring_lock)
7014{
7015 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))))
7016 __io_queue_sqe(req);
7017 else
7018 io_queue_sqe_fallback(req);
7019}
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{
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007030 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
7031 return false;
7032
7033 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
7034 ctx->restrictions.sqe_flags_required)
7035 return false;
7036
7037 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
7038 ctx->restrictions.sqe_flags_required))
7039 return false;
7040
7041 return true;
7042}
7043
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007044static void io_init_req_drain(struct io_kiocb *req)
7045{
7046 struct io_ring_ctx *ctx = req->ctx;
7047 struct io_kiocb *head = ctx->submit_state.link.head;
7048
7049 ctx->drain_active = true;
7050 if (head) {
7051 /*
7052 * If we need to drain a request in the middle of a link, drain
7053 * the head request and the next request/link after the current
7054 * link. Considering sequential execution of links,
7055 * IOSQE_IO_DRAIN will be maintained for every request of our
7056 * link.
7057 */
7058 head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC;
7059 ctx->drain_next = true;
7060 }
7061}
7062
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007063static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007064 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007065 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007066{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007067 unsigned int sqe_flags;
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007068 int personality;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007069 u8 opcode;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007070
Pavel Begunkov864ea922021-08-09 13:04:08 +01007071 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007072 req->opcode = opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007073 /* same numerical values with corresponding REQ_F_*, safe to copy */
7074 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007075 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007076 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007077 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03007078 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007079
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007080 if (unlikely(opcode >= IORING_OP_LAST)) {
7081 req->opcode = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007082 return -EINVAL;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007083 }
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007084 if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
7085 /* enforce forwards compatibility on users */
7086 if (sqe_flags & ~SQE_VALID_FLAGS)
7087 return -EINVAL;
7088 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007089 !io_op_defs[opcode].buffer_select)
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007090 return -EOPNOTSUPP;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007091 if (sqe_flags & IOSQE_IO_DRAIN)
7092 io_init_req_drain(req);
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007093 }
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007094 if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) {
7095 if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags))
7096 return -EACCES;
7097 /* knock it to the slow queue path, will be drained there */
7098 if (ctx->drain_active)
7099 req->flags |= REQ_F_FORCE_ASYNC;
7100 /* if there is no link, we're at "next" request and need to drain */
7101 if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
7102 ctx->drain_next = false;
7103 ctx->drain_active = true;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007104 req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC;
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007105 }
7106 }
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007107
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007108 if (io_op_defs[opcode].needs_file) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007109 struct io_submit_state *state = &ctx->submit_state;
7110
7111 /*
7112 * Plug now if we have more than 2 IO left after this, and the
7113 * target is potentially a read/write to block based storage.
7114 */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007115 if (state->need_plug && io_op_defs[opcode].plug) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007116 state->plug_started = true;
7117 state->need_plug = false;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -06007118 blk_start_plug_nr_ios(&state->plug, state->submit_nr);
Pavel Begunkov6d634162021-10-06 16:06:46 +01007119 }
7120
Pavel Begunkov62906e82021-08-10 14:52:47 +01007121 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007122 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007123 if (unlikely(!req->file))
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007124 return -EBADF;
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007125 }
Pavel Begunkovc11368a52020-05-17 14:13:42 +03007126
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007127 personality = READ_ONCE(sqe->personality);
Jens Axboe63ff8222020-05-07 14:56:15 -06007128 if (personality) {
Linus Torvaldscdab10b2021-11-01 21:06:18 -07007129 int ret;
7130
Jens Axboe63ff8222020-05-07 14:56:15 -06007131 req->creds = xa_load(&ctx->personalities, personality);
7132 if (!req->creds)
Jens Axboe27926b62020-10-28 09:33:23 -06007133 return -EINVAL;
Jens Axboe63ff8222020-05-07 14:56:15 -06007134 get_cred(req->creds);
Paul Moorecdc14042021-02-01 19:56:49 -05007135 ret = security_uring_override_creds(req->creds);
7136 if (ret) {
7137 put_cred(req->creds);
7138 return ret;
7139 }
Jens Axboe63ff8222020-05-07 14:56:15 -06007140 req->flags |= REQ_F_CREDS;
7141 }
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007142
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007143 return io_req_prep(req, sqe);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007144}
7145
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007146static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007147 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007148 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007149{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007150 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007151 int ret;
7152
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007153 ret = io_init_req(ctx, req, sqe);
7154 if (unlikely(ret)) {
Jens Axboea87acfd2021-09-11 16:04:50 -06007155 trace_io_uring_req_failed(sqe, ret);
7156
Hao Xua8295b92021-08-27 17:46:09 +08007157 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007158 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007159 /*
7160 * we can judge a link req is failed or cancelled by if
7161 * REQ_F_FAIL is set, but the head is an exception since
7162 * it may be set REQ_F_FAIL because of other req's failure
7163 * so let's leverage req->result to distinguish if a head
7164 * is set REQ_F_FAIL because of its failure or other req's
7165 * failure so that we can set the correct ret code for it.
7166 * init result here to avoid affecting the normal path.
7167 */
7168 if (!(link->head->flags & REQ_F_FAIL))
7169 req_fail_link_node(link->head, -ECANCELED);
7170 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7171 /*
7172 * the current req is a normal req, we should return
7173 * error and thus break the submittion loop.
7174 */
7175 io_req_complete_failed(req, ret);
7176 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007177 }
Hao Xua8295b92021-08-27 17:46:09 +08007178 req_fail_link_node(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007179 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007180
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007181 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007182 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7183 req->flags, true,
7184 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007185
Jens Axboe6c271ce2019-01-10 11:22:30 -07007186 /*
7187 * If we already have a head request, queue this one for async
7188 * submittal once the head completes. If we don't have a head but
7189 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7190 * submitted sync once the chain is complete. If none of those
7191 * conditions are true (normal request), then just queue it.
7192 */
7193 if (link->head) {
7194 struct io_kiocb *head = link->head;
7195
Hao Xua8295b92021-08-27 17:46:09 +08007196 if (!(req->flags & REQ_F_FAIL)) {
7197 ret = io_req_prep_async(req);
7198 if (unlikely(ret)) {
7199 req_fail_link_node(req, ret);
7200 if (!(head->flags & REQ_F_FAIL))
7201 req_fail_link_node(head, -ECANCELED);
7202 }
7203 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007204 trace_io_uring_link(ctx, req, head);
7205 link->last->link = req;
7206 link->last = req;
7207
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007208 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK))
7209 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007210 /* last request of a link, enqueue the link */
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007211 link->head = NULL;
7212 req = head;
7213 } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
7214 link->head = req;
7215 link->last = req;
7216 return 0;
Jackie Liu4fe2c962019-09-09 20:50:40 +08007217 }
7218
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007219 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007220 return 0;
7221}
7222
7223/*
7224 * Batched submission is done, ensure local IO is flushed out.
7225 */
Pavel Begunkov553deff2021-09-24 21:59:55 +01007226static void io_submit_state_end(struct io_ring_ctx *ctx)
Jens Axboe3529d8c2019-12-19 18:24:38 -07007227{
Pavel Begunkov553deff2021-09-24 21:59:55 +01007228 struct io_submit_state *state = &ctx->submit_state;
7229
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007230 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007231 io_queue_sqe(state->link.head);
Pavel Begunkov553deff2021-09-24 21:59:55 +01007232 /* flush only after queuing links as they can generate completions */
Pavel Begunkovc4501782021-09-08 16:40:52 +01007233 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007234 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007235 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007236}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007237
Jens Axboe9e645e112019-05-10 16:07:28 -06007238/*
7239 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007240 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007241static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007242 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007243{
7244 state->plug_started = false;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007245 state->need_plug = max_ios > 2;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -06007246 state->submit_nr = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007247 /* set only head, no need to init link_last in advance */
7248 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007249}
7250
Jens Axboe193155c2020-02-22 23:22:19 -07007251static void io_commit_sqring(struct io_ring_ctx *ctx)
7252{
Jens Axboe75c6a032020-01-28 10:15:23 -07007253 struct io_rings *rings = ctx->rings;
7254
7255 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007256 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007257 * since once we write the new head, the application could
7258 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007259 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007260 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007261}
7262
Jens Axboe9e645e112019-05-10 16:07:28 -06007263/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007264 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007265 * that is mapped by userspace. This means that care needs to be taken to
7266 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007267 * being a good citizen. If members of the sqe are validated and then later
7268 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007269 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007270 */
7271static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007272{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007273 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007274 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007275
7276 /*
7277 * The cached sq head (or cq tail) serves two purposes:
7278 *
7279 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007280 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007281 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007282 * though the application is the one updating it.
7283 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007284 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007285 if (likely(head < ctx->sq_entries))
7286 return &ctx->sq_sqes[head];
7287
7288 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007289 ctx->cq_extra--;
7290 WRITE_ONCE(ctx->rings->sq_dropped,
7291 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007292 return NULL;
7293}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007294
Jens Axboe0f212202020-09-13 13:09:39 -06007295static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007296 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007297{
Pavel Begunkov69629802021-09-24 22:00:01 +01007298 unsigned int entries = io_sqring_entries(ctx);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007299 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007300
Pavel Begunkov51d48da2021-10-04 20:02:47 +01007301 if (unlikely(!entries))
Pavel Begunkov69629802021-09-24 22:00:01 +01007302 return 0;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03007303 /* make sure SQ entry isn't read before tail */
Pavel Begunkov69629802021-09-24 22:00:01 +01007304 nr = min3(nr, ctx->sq_entries, entries);
Pavel Begunkov9a108672021-08-27 11:55:01 +01007305 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007306
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007307 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov69629802021-09-24 22:00:01 +01007308 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007309 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007310 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007311
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007312 if (unlikely(!io_alloc_req_refill(ctx))) {
Pavel Begunkov196be952019-11-07 01:41:06 +03007313 if (!submitted)
7314 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007315 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007316 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007317 req = io_alloc_req(ctx);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007318 sqe = io_get_sqe(ctx);
7319 if (unlikely(!sqe)) {
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01007320 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007321 break;
7322 }
Jens Axboed3656342019-12-18 09:50:26 -07007323 /* will complete beyond this point, count as submitted */
7324 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007325 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007326 break;
Pavel Begunkov69629802021-09-24 22:00:01 +01007327 } while (submitted < nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007328
Pavel Begunkov9466f432020-01-25 22:34:01 +03007329 if (unlikely(submitted != nr)) {
7330 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007331 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007332
Pavel Begunkov09899b12021-06-14 02:36:22 +01007333 current->io_uring->cached_refs += unused;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007334 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007335
Pavel Begunkov553deff2021-09-24 21:59:55 +01007336 io_submit_state_end(ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007337 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7338 io_commit_sqring(ctx);
7339
Jens Axboe6c271ce2019-01-10 11:22:30 -07007340 return submitted;
7341}
7342
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007343static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7344{
7345 return READ_ONCE(sqd->state);
7346}
7347
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007348static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7349{
7350 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007351 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007352 WRITE_ONCE(ctx->rings->sq_flags,
7353 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007354 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007355}
7356
7357static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7358{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007359 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007360 WRITE_ONCE(ctx->rings->sq_flags,
7361 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007362 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007363}
7364
Xiaoguang Wang08369242020-11-03 14:15:59 +08007365static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007366{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007367 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007368 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007369
Jens Axboec8d1ba52020-09-14 11:07:26 -06007370 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007371 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007372 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7373 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007374
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007375 if (!wq_list_empty(&ctx->iopoll_list) || to_submit) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007376 const struct cred *creds = NULL;
7377
7378 if (ctx->sq_creds != current_cred())
7379 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007380
Xiaoguang Wang08369242020-11-03 14:15:59 +08007381 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007382 if (!wq_list_empty(&ctx->iopoll_list))
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01007383 io_do_iopoll(ctx, true);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007384
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007385 /*
7386 * Don't submit if refs are dying, good for io_uring_register(),
7387 * but also it is relied upon by io_ring_exit_work()
7388 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007389 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7390 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007391 ret = io_submit_sqes(ctx, to_submit);
7392 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007393
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007394 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7395 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007396 if (creds)
7397 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007398 }
Jens Axboe90554202020-09-03 12:12:41 -06007399
Xiaoguang Wang08369242020-11-03 14:15:59 +08007400 return ret;
7401}
7402
Pavel Begunkovc0724812021-10-04 20:02:54 +01007403static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd)
Xiaoguang Wang08369242020-11-03 14:15:59 +08007404{
7405 struct io_ring_ctx *ctx;
7406 unsigned sq_thread_idle = 0;
7407
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007408 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7409 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007410 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007411}
7412
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007413static bool io_sqd_handle_event(struct io_sq_data *sqd)
7414{
7415 bool did_sig = false;
7416 struct ksignal ksig;
7417
7418 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7419 signal_pending(current)) {
7420 mutex_unlock(&sqd->lock);
7421 if (signal_pending(current))
7422 did_sig = get_signal(&ksig);
7423 cond_resched();
7424 mutex_lock(&sqd->lock);
7425 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007426 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7427}
7428
Jens Axboe6c271ce2019-01-10 11:22:30 -07007429static int io_sq_thread(void *data)
7430{
Jens Axboe69fb2132020-09-14 11:16:23 -06007431 struct io_sq_data *sqd = data;
7432 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007433 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007434 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007435 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007436
Pavel Begunkov696ee882021-04-01 09:55:04 +01007437 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007438 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007439
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007440 if (sqd->sq_cpu != -1)
7441 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7442 else
7443 set_cpus_allowed_ptr(current, cpu_online_mask);
7444 current->flags |= PF_NO_SETAFFINITY;
7445
Paul Moore5bd21822021-02-16 19:46:48 -05007446 audit_alloc_kernel(current);
7447
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007448 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007449 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007450 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007451
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007452 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7453 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007454 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007455 timeout = jiffies + sqd->sq_thread_idle;
7456 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007457
Jens Axboee95eee22020-09-08 09:11:32 -06007458 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007459 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007460 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007461
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007462 if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007463 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007464 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007465 if (io_run_task_work())
7466 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007467
Xiaoguang Wang08369242020-11-03 14:15:59 +08007468 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007469 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007470 if (sqt_spin)
7471 timeout = jiffies + sqd->sq_thread_idle;
7472 continue;
7473 }
7474
Xiaoguang Wang08369242020-11-03 14:15:59 +08007475 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007476 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007477 bool needs_sched = true;
7478
Hao Xu724cb4f2021-04-21 23:19:11 +08007479 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007480 io_ring_set_wakeup_flag(ctx);
7481
Hao Xu724cb4f2021-04-21 23:19:11 +08007482 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007483 !wq_list_empty(&ctx->iopoll_list)) {
Hao Xu724cb4f2021-04-21 23:19:11 +08007484 needs_sched = false;
7485 break;
7486 }
7487 if (io_sqring_entries(ctx)) {
7488 needs_sched = false;
7489 break;
7490 }
7491 }
7492
7493 if (needs_sched) {
7494 mutex_unlock(&sqd->lock);
7495 schedule();
7496 mutex_lock(&sqd->lock);
7497 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007498 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7499 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007500 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007501
7502 finish_wait(&sqd->wait, &wait);
7503 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007504 }
7505
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007506 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007507 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007508 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007509 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007510 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007511 mutex_unlock(&sqd->lock);
7512
Paul Moore5bd21822021-02-16 19:46:48 -05007513 audit_free(current);
7514
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007515 complete(&sqd->exited);
7516 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007517}
7518
Jens Axboebda52162019-09-24 13:47:15 -06007519struct io_wait_queue {
7520 struct wait_queue_entry wq;
7521 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007522 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007523 unsigned nr_timeouts;
7524};
7525
Pavel Begunkov6c503152021-01-04 20:36:36 +00007526static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007527{
7528 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007529 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007530
7531 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007532 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007533 * started waiting. For timeouts, we always want to return to userspace,
7534 * regardless of event count.
7535 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007536 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007537}
7538
7539static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7540 int wake_flags, void *key)
7541{
7542 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7543 wq);
7544
Pavel Begunkov6c503152021-01-04 20:36:36 +00007545 /*
7546 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7547 * the task, and the next invocation will do it.
7548 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007549 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007550 return autoremove_wake_function(curr, mode, wake_flags, key);
7551 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007552}
7553
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007554static int io_run_task_work_sig(void)
7555{
7556 if (io_run_task_work())
7557 return 1;
7558 if (!signal_pending(current))
7559 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007560 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007561 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007562 return -EINTR;
7563}
7564
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007565/* when returns >0, the caller should retry */
7566static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7567 struct io_wait_queue *iowq,
7568 signed long *timeout)
7569{
7570 int ret;
7571
7572 /* make sure we run task_work before checking for signals */
7573 ret = io_run_task_work_sig();
7574 if (ret || io_should_wake(iowq))
7575 return ret;
7576 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007577 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007578 return 1;
7579
7580 *timeout = schedule_timeout(*timeout);
7581 return !*timeout ? -ETIME : 1;
7582}
7583
Jens Axboe2b188cc2019-01-07 10:46:33 -07007584/*
7585 * Wait until events become available, if we don't already have some. The
7586 * application must reap them itself, as they reside on the shared cq ring.
7587 */
7588static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007589 const sigset_t __user *sig, size_t sigsz,
7590 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007591{
Pavel Begunkov902910992021-08-09 09:07:32 -06007592 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007593 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007594 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7595 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007596
Jens Axboeb41e9852020-02-17 09:52:41 -07007597 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007598 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007599 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007600 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007601 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007602 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007603 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007604
Xiaoguang Wang44df58d2021-09-14 22:38:52 +08007605 if (uts) {
7606 struct timespec64 ts;
7607
7608 if (get_timespec64(&ts, uts))
7609 return -EFAULT;
7610 timeout = timespec64_to_jiffies(&ts);
7611 }
7612
Jens Axboe2b188cc2019-01-07 10:46:33 -07007613 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007614#ifdef CONFIG_COMPAT
7615 if (in_compat_syscall())
7616 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007617 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007618 else
7619#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007620 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007621
Jens Axboe2b188cc2019-01-07 10:46:33 -07007622 if (ret)
7623 return ret;
7624 }
7625
Pavel Begunkov902910992021-08-09 09:07:32 -06007626 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7627 iowq.wq.private = current;
7628 INIT_LIST_HEAD(&iowq.wq.entry);
7629 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007630 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007631 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007632
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007633 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007634 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007635 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007636 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007637 ret = -EBUSY;
7638 break;
7639 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007640 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007641 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007642 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007643 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007644 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007645 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007646
Jens Axboeb7db41c2020-07-04 08:55:50 -06007647 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007648
Hristo Venev75b28af2019-08-26 17:23:46 +00007649 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007650}
7651
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007652static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007653{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007654 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007655
7656 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007657 kfree(table[i]);
7658 kfree(table);
7659}
7660
Pavel Begunkovc0724812021-10-04 20:02:54 +01007661static __cold void **io_alloc_page_table(size_t size)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007662{
7663 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7664 size_t init_size = size;
7665 void **table;
7666
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007667 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007668 if (!table)
7669 return NULL;
7670
7671 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007672 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007673
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007674 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007675 if (!table[i]) {
7676 io_free_page_table(table, init_size);
7677 return NULL;
7678 }
7679 size -= this_size;
7680 }
7681 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007682}
7683
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007684static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7685{
7686 percpu_ref_exit(&ref_node->refs);
7687 kfree(ref_node);
7688}
7689
Pavel Begunkovc0724812021-10-04 20:02:54 +01007690static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007691{
7692 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7693 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7694 unsigned long flags;
7695 bool first_add = false;
7696
7697 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7698 node->done = true;
7699
7700 while (!list_empty(&ctx->rsrc_ref_list)) {
7701 node = list_first_entry(&ctx->rsrc_ref_list,
7702 struct io_rsrc_node, node);
7703 /* recycle ref nodes in order */
7704 if (!node->done)
7705 break;
7706 list_del(&node->node);
7707 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7708 }
7709 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7710
7711 if (first_add)
7712 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7713}
7714
7715static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7716{
7717 struct io_rsrc_node *ref_node;
7718
7719 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7720 if (!ref_node)
7721 return NULL;
7722
7723 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7724 0, GFP_KERNEL)) {
7725 kfree(ref_node);
7726 return NULL;
7727 }
7728 INIT_LIST_HEAD(&ref_node->node);
7729 INIT_LIST_HEAD(&ref_node->rsrc_list);
7730 ref_node->done = false;
7731 return ref_node;
7732}
7733
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007734static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7735 struct io_rsrc_data *data_to_kill)
Pavel Begunkovab409402021-10-09 23:14:41 +01007736 __must_hold(&ctx->uring_lock)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007737{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007738 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7739 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007740
Pavel Begunkovab409402021-10-09 23:14:41 +01007741 io_rsrc_refs_drop(ctx);
7742
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007743 if (data_to_kill) {
7744 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007745
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007746 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007747 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007748 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007749 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007750
Pavel Begunkov3e942492021-04-11 01:46:34 +01007751 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007752 percpu_ref_kill(&rsrc_node->refs);
7753 ctx->rsrc_node = NULL;
7754 }
7755
7756 if (!ctx->rsrc_node) {
7757 ctx->rsrc_node = ctx->rsrc_backup_node;
7758 ctx->rsrc_backup_node = NULL;
7759 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007760}
7761
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007762static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007763{
7764 if (ctx->rsrc_backup_node)
7765 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007766 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007767 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7768}
7769
Pavel Begunkovc0724812021-10-04 20:02:54 +01007770static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
7771 struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007772{
7773 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007774
Pavel Begunkov215c3902021-04-01 15:43:48 +01007775 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007776 if (data->quiesce)
7777 return -ENXIO;
7778
7779 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007780 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007781 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007782 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007783 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007784 io_rsrc_node_switch(ctx, data);
7785
Pavel Begunkov3e942492021-04-11 01:46:34 +01007786 /* kill initial ref, already quiesced if zero */
7787 if (atomic_dec_and_test(&data->refs))
7788 break;
Jens Axboec018db42021-08-09 08:15:50 -06007789 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007790 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007791 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007792 if (!ret) {
7793 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007794 break;
Jens Axboec018db42021-08-09 08:15:50 -06007795 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007796
Pavel Begunkov3e942492021-04-11 01:46:34 +01007797 atomic_inc(&data->refs);
7798 /* wait for all works potentially completing data->done */
7799 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007800 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007801
Hao Xu8bad28d2021-02-19 17:19:36 +08007802 ret = io_run_task_work_sig();
7803 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007804 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007805 data->quiesce = false;
7806
Hao Xu8bad28d2021-02-19 17:19:36 +08007807 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007808}
7809
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007810static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7811{
7812 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7813 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7814
7815 return &data->tags[table_idx][off];
7816}
7817
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007818static void io_rsrc_data_free(struct io_rsrc_data *data)
7819{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007820 size_t size = data->nr * sizeof(data->tags[0][0]);
7821
7822 if (data->tags)
7823 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007824 kfree(data);
7825}
7826
Pavel Begunkovc0724812021-10-04 20:02:54 +01007827static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7828 u64 __user *utags, unsigned nr,
7829 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007830{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007831 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007832 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007833 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007834
7835 data = kzalloc(sizeof(*data), GFP_KERNEL);
7836 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007837 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007838 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007839 if (!data->tags) {
7840 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007841 return -ENOMEM;
7842 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007843
7844 data->nr = nr;
7845 data->ctx = ctx;
7846 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007847 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007848 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007849 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007850 u64 *tag_slot = io_get_tag_slot(data, i);
7851
7852 if (copy_from_user(tag_slot, &utags[i],
7853 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007854 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007855 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007856 }
7857
Pavel Begunkov3e942492021-04-11 01:46:34 +01007858 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007859 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007860 *pdata = data;
7861 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007862fail:
7863 io_rsrc_data_free(data);
7864 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007865}
7866
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007867static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7868{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007869 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7870 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007871 return !!table->files;
7872}
7873
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007874static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007875{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007876 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007877 table->files = NULL;
7878}
7879
Jens Axboe2b188cc2019-01-07 10:46:33 -07007880static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7881{
7882#if defined(CONFIG_UNIX)
7883 if (ctx->ring_sock) {
7884 struct sock *sock = ctx->ring_sock->sk;
7885 struct sk_buff *skb;
7886
7887 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7888 kfree_skb(skb);
7889 }
7890#else
7891 int i;
7892
7893 for (i = 0; i < ctx->nr_user_files; i++) {
7894 struct file *file;
7895
7896 file = io_file_from_index(ctx, i);
7897 if (file)
7898 fput(file);
7899 }
7900#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007901 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007902 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007903 ctx->file_data = NULL;
7904 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007905}
7906
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007907static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7908{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007909 int ret;
7910
Pavel Begunkov08480402021-04-13 02:58:38 +01007911 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007912 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007913 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7914 if (!ret)
7915 __io_sqe_files_unregister(ctx);
7916 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007917}
7918
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007919static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007920 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007921{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007922 WARN_ON_ONCE(sqd->thread == current);
7923
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007924 /*
7925 * Do the dance but not conditional clear_bit() because it'd race with
7926 * other threads incrementing park_pending and setting the bit.
7927 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007928 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007929 if (atomic_dec_return(&sqd->park_pending))
7930 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007931 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007932}
7933
Jens Axboe86e0d672021-03-05 08:44:39 -07007934static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007935 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007936{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007937 WARN_ON_ONCE(sqd->thread == current);
7938
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007939 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007940 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007941 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007942 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007943 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007944}
7945
7946static void io_sq_thread_stop(struct io_sq_data *sqd)
7947{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007948 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007949 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007950
Jens Axboe05962f92021-03-06 13:58:48 -07007951 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007952 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007953 if (sqd->thread)
7954 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007955 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007956 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007957}
7958
Jens Axboe534ca6d2020-09-02 13:52:19 -06007959static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007960{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007961 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007962 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7963
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007964 io_sq_thread_stop(sqd);
7965 kfree(sqd);
7966 }
7967}
7968
7969static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7970{
7971 struct io_sq_data *sqd = ctx->sq_data;
7972
7973 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007974 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007975 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007976 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007977 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007978
7979 io_put_sq_data(sqd);
7980 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007981 }
7982}
7983
Jens Axboeaa061652020-09-02 14:50:27 -06007984static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7985{
7986 struct io_ring_ctx *ctx_attach;
7987 struct io_sq_data *sqd;
7988 struct fd f;
7989
7990 f = fdget(p->wq_fd);
7991 if (!f.file)
7992 return ERR_PTR(-ENXIO);
7993 if (f.file->f_op != &io_uring_fops) {
7994 fdput(f);
7995 return ERR_PTR(-EINVAL);
7996 }
7997
7998 ctx_attach = f.file->private_data;
7999 sqd = ctx_attach->sq_data;
8000 if (!sqd) {
8001 fdput(f);
8002 return ERR_PTR(-EINVAL);
8003 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07008004 if (sqd->task_tgid != current->tgid) {
8005 fdput(f);
8006 return ERR_PTR(-EPERM);
8007 }
Jens Axboeaa061652020-09-02 14:50:27 -06008008
8009 refcount_inc(&sqd->refs);
8010 fdput(f);
8011 return sqd;
8012}
8013
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008014static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
8015 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008016{
8017 struct io_sq_data *sqd;
8018
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008019 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008020 if (p->flags & IORING_SETUP_ATTACH_WQ) {
8021 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008022 if (!IS_ERR(sqd)) {
8023 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008024 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008025 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07008026 /* fall through for EPERM case, setup new sqd/task */
8027 if (PTR_ERR(sqd) != -EPERM)
8028 return sqd;
8029 }
Jens Axboeaa061652020-09-02 14:50:27 -06008030
Jens Axboe534ca6d2020-09-02 13:52:19 -06008031 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
8032 if (!sqd)
8033 return ERR_PTR(-ENOMEM);
8034
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008035 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008036 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06008037 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008038 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008039 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008040 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008041 return sqd;
8042}
8043
Jens Axboe6b063142019-01-10 22:13:58 -07008044#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07008045/*
8046 * Ensure the UNIX gc is aware of our file set, so we are certain that
8047 * the io_uring can be safely unregistered on process exit, even if we have
8048 * loops in the file referencing.
8049 */
8050static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
8051{
8052 struct sock *sk = ctx->ring_sock->sk;
8053 struct scm_fp_list *fpl;
8054 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06008055 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07008056
Jens Axboe6b063142019-01-10 22:13:58 -07008057 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
8058 if (!fpl)
8059 return -ENOMEM;
8060
8061 skb = alloc_skb(0, GFP_KERNEL);
8062 if (!skb) {
8063 kfree(fpl);
8064 return -ENOMEM;
8065 }
8066
8067 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07008068
Jens Axboe08a45172019-10-03 08:11:03 -06008069 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07008070 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07008071 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008072 struct file *file = io_file_from_index(ctx, i + offset);
8073
8074 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06008075 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06008076 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06008077 unix_inflight(fpl->user, fpl->fp[nr_files]);
8078 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07008079 }
8080
Jens Axboe08a45172019-10-03 08:11:03 -06008081 if (nr_files) {
8082 fpl->max = SCM_MAX_FD;
8083 fpl->count = nr_files;
8084 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008085 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06008086 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
8087 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07008088
Jens Axboe08a45172019-10-03 08:11:03 -06008089 for (i = 0; i < nr_files; i++)
8090 fput(fpl->fp[i]);
8091 } else {
8092 kfree_skb(skb);
8093 kfree(fpl);
8094 }
Jens Axboe6b063142019-01-10 22:13:58 -07008095
8096 return 0;
8097}
8098
8099/*
8100 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8101 * causes regular reference counting to break down. We rely on the UNIX
8102 * garbage collection to take care of this problem for us.
8103 */
8104static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8105{
8106 unsigned left, total;
8107 int ret = 0;
8108
8109 total = 0;
8110 left = ctx->nr_user_files;
8111 while (left) {
8112 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008113
8114 ret = __io_sqe_files_scm(ctx, this_files, total);
8115 if (ret)
8116 break;
8117 left -= this_files;
8118 total += this_files;
8119 }
8120
8121 if (!ret)
8122 return 0;
8123
8124 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008125 struct file *file = io_file_from_index(ctx, total);
8126
8127 if (file)
8128 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008129 total++;
8130 }
8131
8132 return ret;
8133}
8134#else
8135static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8136{
8137 return 0;
8138}
8139#endif
8140
Pavel Begunkov47e90392021-04-01 15:43:56 +01008141static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008142{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008143 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008144#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008145 struct sock *sock = ctx->ring_sock->sk;
8146 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8147 struct sk_buff *skb;
8148 int i;
8149
8150 __skb_queue_head_init(&list);
8151
8152 /*
8153 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8154 * remove this entry and rearrange the file array.
8155 */
8156 skb = skb_dequeue(head);
8157 while (skb) {
8158 struct scm_fp_list *fp;
8159
8160 fp = UNIXCB(skb).fp;
8161 for (i = 0; i < fp->count; i++) {
8162 int left;
8163
8164 if (fp->fp[i] != file)
8165 continue;
8166
8167 unix_notinflight(fp->user, fp->fp[i]);
8168 left = fp->count - 1 - i;
8169 if (left) {
8170 memmove(&fp->fp[i], &fp->fp[i + 1],
8171 left * sizeof(struct file *));
8172 }
8173 fp->count--;
8174 if (!fp->count) {
8175 kfree_skb(skb);
8176 skb = NULL;
8177 } else {
8178 __skb_queue_tail(&list, skb);
8179 }
8180 fput(file);
8181 file = NULL;
8182 break;
8183 }
8184
8185 if (!file)
8186 break;
8187
8188 __skb_queue_tail(&list, skb);
8189
8190 skb = skb_dequeue(head);
8191 }
8192
8193 if (skb_peek(&list)) {
8194 spin_lock_irq(&head->lock);
8195 while ((skb = __skb_dequeue(&list)) != NULL)
8196 __skb_queue_tail(head, skb);
8197 spin_unlock_irq(&head->lock);
8198 }
8199#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008200 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008201#endif
8202}
8203
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008204static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008205{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008206 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008207 struct io_ring_ctx *ctx = rsrc_data->ctx;
8208 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008209
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008210 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8211 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008212
8213 if (prsrc->tag) {
8214 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008215
8216 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008217 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008218 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01008219 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008220 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008221 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008222 io_cqring_ev_posted(ctx);
8223 io_ring_submit_unlock(ctx, lock_ring);
8224 }
8225
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008226 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008227 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008228 }
8229
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008230 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008231 if (atomic_dec_and_test(&rsrc_data->refs))
8232 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008233}
8234
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008235static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008236{
8237 struct io_ring_ctx *ctx;
8238 struct llist_node *node;
8239
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008240 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8241 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008242
8243 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008244 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008245 struct llist_node *next = node->next;
8246
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008247 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008248 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008249 node = next;
8250 }
8251}
8252
Jens Axboe05f3fb32019-12-09 11:22:50 -07008253static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008254 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008255{
8256 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008257 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008258 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008259 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008260
8261 if (ctx->file_data)
8262 return -EBUSY;
8263 if (!nr_args)
8264 return -EINVAL;
8265 if (nr_args > IORING_MAX_FIXED_FILES)
8266 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008267 if (nr_args > rlimit(RLIMIT_NOFILE))
8268 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008269 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008270 if (ret)
8271 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008272 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8273 &ctx->file_data);
8274 if (ret)
8275 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008276
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008277 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008278 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008279 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008280
Jens Axboe05f3fb32019-12-09 11:22:50 -07008281 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008282 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008283 ret = -EFAULT;
8284 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008285 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008286 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008287 if (fd == -1) {
8288 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008289 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008290 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008291 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008292 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008293
Jens Axboe05f3fb32019-12-09 11:22:50 -07008294 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008295 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008296 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008297 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008298
8299 /*
8300 * Don't allow io_uring instances to be registered. If UNIX
8301 * isn't enabled, then this causes a reference cycle and this
8302 * instance can never get freed. If UNIX is enabled we'll
8303 * handle it just fine, but there's still no point in allowing
8304 * a ring fd as it doesn't support regular read/write anyway.
8305 */
8306 if (file->f_op == &io_uring_fops) {
8307 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008308 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008309 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008310 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008311 }
8312
Jens Axboe05f3fb32019-12-09 11:22:50 -07008313 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008314 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008315 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008316 return ret;
8317 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008318
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008319 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008320 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008321out_fput:
8322 for (i = 0; i < ctx->nr_user_files; i++) {
8323 file = io_file_from_index(ctx, i);
8324 if (file)
8325 fput(file);
8326 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008327 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008328 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008329out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008330 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008331 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008332 return ret;
8333}
8334
Jens Axboec3a31e62019-10-03 13:59:56 -06008335static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8336 int index)
8337{
8338#if defined(CONFIG_UNIX)
8339 struct sock *sock = ctx->ring_sock->sk;
8340 struct sk_buff_head *head = &sock->sk_receive_queue;
8341 struct sk_buff *skb;
8342
8343 /*
8344 * See if we can merge this file into an existing skb SCM_RIGHTS
8345 * file set. If there's no room, fall back to allocating a new skb
8346 * and filling it in.
8347 */
8348 spin_lock_irq(&head->lock);
8349 skb = skb_peek(head);
8350 if (skb) {
8351 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8352
8353 if (fpl->count < SCM_MAX_FD) {
8354 __skb_unlink(skb, head);
8355 spin_unlock_irq(&head->lock);
8356 fpl->fp[fpl->count] = get_file(file);
8357 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8358 fpl->count++;
8359 spin_lock_irq(&head->lock);
8360 __skb_queue_head(head, skb);
8361 } else {
8362 skb = NULL;
8363 }
8364 }
8365 spin_unlock_irq(&head->lock);
8366
8367 if (skb) {
8368 fput(file);
8369 return 0;
8370 }
8371
8372 return __io_sqe_files_scm(ctx, 1, index);
8373#else
8374 return 0;
8375#endif
8376}
8377
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008378static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
8379 struct io_rsrc_node *node, void *rsrc)
8380{
8381 struct io_rsrc_put *prsrc;
8382
8383 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8384 if (!prsrc)
8385 return -ENOMEM;
8386
8387 prsrc->tag = *io_get_tag_slot(data, idx);
8388 prsrc->rsrc = rsrc;
8389 list_add(&prsrc->list, &node->rsrc_list);
8390 return 0;
8391}
8392
Pavel Begunkovb9445592021-08-25 12:25:45 +01008393static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8394 unsigned int issue_flags, u32 slot_index)
8395{
8396 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08008397 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008398 bool needs_switch = false;
Pavel Begunkovb9445592021-08-25 12:25:45 +01008399 struct io_fixed_file *file_slot;
8400 int ret = -EBADF;
8401
Hao Xu3b44b372021-10-18 21:34:31 +08008402 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008403 if (file->f_op == &io_uring_fops)
8404 goto err;
8405 ret = -ENXIO;
8406 if (!ctx->file_data)
8407 goto err;
8408 ret = -EINVAL;
8409 if (slot_index >= ctx->nr_user_files)
8410 goto err;
8411
8412 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8413 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008414
8415 if (file_slot->file_ptr) {
8416 struct file *old_file;
8417
8418 ret = io_rsrc_node_switch_start(ctx);
8419 if (ret)
8420 goto err;
8421
8422 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8423 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
8424 ctx->rsrc_node, old_file);
8425 if (ret)
8426 goto err;
8427 file_slot->file_ptr = 0;
8428 needs_switch = true;
8429 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01008430
8431 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8432 io_fixed_file_set(file_slot, file);
8433 ret = io_sqe_file_register(ctx, file, slot_index);
8434 if (ret) {
8435 file_slot->file_ptr = 0;
8436 goto err;
8437 }
8438
8439 ret = 0;
8440err:
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008441 if (needs_switch)
8442 io_rsrc_node_switch(ctx, ctx->file_data);
Hao Xu3b44b372021-10-18 21:34:31 +08008443 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008444 if (ret)
8445 fput(file);
8446 return ret;
8447}
8448
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008449static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
8450{
8451 unsigned int offset = req->close.file_slot - 1;
8452 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08008453 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008454 struct io_fixed_file *file_slot;
8455 struct file *file;
8456 int ret, i;
8457
Hao Xu3b44b372021-10-18 21:34:31 +08008458 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008459 ret = -ENXIO;
8460 if (unlikely(!ctx->file_data))
8461 goto out;
8462 ret = -EINVAL;
8463 if (offset >= ctx->nr_user_files)
8464 goto out;
8465 ret = io_rsrc_node_switch_start(ctx);
8466 if (ret)
8467 goto out;
8468
8469 i = array_index_nospec(offset, ctx->nr_user_files);
8470 file_slot = io_fixed_file_slot(&ctx->file_table, i);
8471 ret = -EBADF;
8472 if (!file_slot->file_ptr)
8473 goto out;
8474
8475 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8476 ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
8477 if (ret)
8478 goto out;
8479
8480 file_slot->file_ptr = 0;
8481 io_rsrc_node_switch(ctx, ctx->file_data);
8482 ret = 0;
8483out:
Hao Xu3b44b372021-10-18 21:34:31 +08008484 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008485 return ret;
8486}
8487
Jens Axboe05f3fb32019-12-09 11:22:50 -07008488static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008489 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008490 unsigned nr_args)
8491{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008492 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008493 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008494 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008495 struct io_fixed_file *file_slot;
8496 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008497 int fd, i, err = 0;
8498 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008499 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008500
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008501 if (!ctx->file_data)
8502 return -ENXIO;
8503 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008504 return -EINVAL;
8505
Pavel Begunkov67973b92021-01-26 13:51:09 +00008506 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008507 u64 tag = 0;
8508
8509 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8510 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008511 err = -EFAULT;
8512 break;
8513 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008514 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8515 err = -EINVAL;
8516 break;
8517 }
noah4e0377a2021-01-26 15:23:28 -05008518 if (fd == IORING_REGISTER_FILES_SKIP)
8519 continue;
8520
Pavel Begunkov67973b92021-01-26 13:51:09 +00008521 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008522 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008523
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008524 if (file_slot->file_ptr) {
8525 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008526 err = io_queue_rsrc_removal(data, up->offset + done,
8527 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008528 if (err)
8529 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008530 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008531 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008532 }
8533 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008534 file = fget(fd);
8535 if (!file) {
8536 err = -EBADF;
8537 break;
8538 }
8539 /*
8540 * Don't allow io_uring instances to be registered. If
8541 * UNIX isn't enabled, then this causes a reference
8542 * cycle and this instance can never get freed. If UNIX
8543 * is enabled we'll handle it just fine, but there's
8544 * still no point in allowing a ring fd as it doesn't
8545 * support regular read/write anyway.
8546 */
8547 if (file->f_op == &io_uring_fops) {
8548 fput(file);
8549 err = -EBADF;
8550 break;
8551 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008552 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008553 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008554 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008555 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008556 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008557 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008558 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008559 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008560 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008561 }
8562
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008563 if (needs_switch)
8564 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008565 return done ? done : err;
8566}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008567
Jens Axboe685fe7f2021-03-08 09:37:51 -07008568static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8569 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008570{
Jens Axboee9418942021-02-19 12:33:30 -07008571 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008572 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008573 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008574
Yang Yingliang362a9e62021-07-20 16:38:05 +08008575 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008576 hash = ctx->hash_map;
8577 if (!hash) {
8578 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008579 if (!hash) {
8580 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008581 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008582 }
Jens Axboee9418942021-02-19 12:33:30 -07008583 refcount_set(&hash->refs, 1);
8584 init_waitqueue_head(&hash->wait);
8585 ctx->hash_map = hash;
8586 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008587 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008588
8589 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008590 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008591 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008592 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008593
Jens Axboed25e3a32021-02-16 11:41:41 -07008594 /* Do QD, or 4 * CPUS, whatever is smallest */
8595 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008596
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008597 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008598}
8599
Pavel Begunkovc0724812021-10-04 20:02:54 +01008600static __cold int io_uring_alloc_task_context(struct task_struct *task,
8601 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008602{
8603 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008604 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008605
Pavel Begunkov09899b12021-06-14 02:36:22 +01008606 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008607 if (unlikely(!tctx))
8608 return -ENOMEM;
8609
Jens Axboed8a6df12020-10-15 16:24:45 -06008610 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8611 if (unlikely(ret)) {
8612 kfree(tctx);
8613 return ret;
8614 }
8615
Jens Axboe685fe7f2021-03-08 09:37:51 -07008616 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008617 if (IS_ERR(tctx->io_wq)) {
8618 ret = PTR_ERR(tctx->io_wq);
8619 percpu_counter_destroy(&tctx->inflight);
8620 kfree(tctx);
8621 return ret;
8622 }
8623
Jens Axboe0f212202020-09-13 13:09:39 -06008624 xa_init(&tctx->xa);
8625 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008626 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008627 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008628 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008629 spin_lock_init(&tctx->task_lock);
8630 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008631 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008632 return 0;
8633}
8634
8635void __io_uring_free(struct task_struct *tsk)
8636{
8637 struct io_uring_task *tctx = tsk->io_uring;
8638
8639 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008640 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008641 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008642
Jens Axboed8a6df12020-10-15 16:24:45 -06008643 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008644 kfree(tctx);
8645 tsk->io_uring = NULL;
8646}
8647
Pavel Begunkovc0724812021-10-04 20:02:54 +01008648static __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
8649 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008650{
8651 int ret;
8652
Jens Axboed25e3a32021-02-16 11:41:41 -07008653 /* Retain compatibility with failing for an invalid attach attempt */
8654 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8655 IORING_SETUP_ATTACH_WQ) {
8656 struct fd f;
8657
8658 f = fdget(p->wq_fd);
8659 if (!f.file)
8660 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008661 if (f.file->f_op != &io_uring_fops) {
8662 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008663 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008664 }
8665 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008666 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008667 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008668 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008669 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008670 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008671
Paul Moorecdc14042021-02-01 19:56:49 -05008672 ret = security_uring_sqpoll();
8673 if (ret)
8674 return ret;
8675
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008676 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008677 if (IS_ERR(sqd)) {
8678 ret = PTR_ERR(sqd);
8679 goto err;
8680 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008681
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008682 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008683 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008684 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8685 if (!ctx->sq_thread_idle)
8686 ctx->sq_thread_idle = HZ;
8687
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008688 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008689 list_add(&ctx->sqd_list, &sqd->ctx_list);
8690 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008691 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008692 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008693 io_sq_thread_unpark(sqd);
8694
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008695 if (ret < 0)
8696 goto err;
8697 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008698 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008699
Jens Axboe6c271ce2019-01-10 11:22:30 -07008700 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008701 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008702
Jens Axboe917257d2019-04-13 09:28:55 -06008703 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008704 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008705 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008706 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008707 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008708 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008709 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008710
8711 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008712 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008713 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8714 if (IS_ERR(tsk)) {
8715 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008716 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008717 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008718
Jens Axboe46fe18b2021-03-04 12:39:36 -07008719 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008720 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008721 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008722 if (ret)
8723 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008724 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8725 /* Can't have SQ_AFF without SQPOLL */
8726 ret = -EINVAL;
8727 goto err;
8728 }
8729
Jens Axboe2b188cc2019-01-07 10:46:33 -07008730 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008731err_sqpoll:
8732 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008733err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008734 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008735 return ret;
8736}
8737
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008738static inline void __io_unaccount_mem(struct user_struct *user,
8739 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008740{
8741 atomic_long_sub(nr_pages, &user->locked_vm);
8742}
8743
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008744static inline int __io_account_mem(struct user_struct *user,
8745 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008746{
8747 unsigned long page_limit, cur_pages, new_pages;
8748
8749 /* Don't allow more pages than we can safely lock */
8750 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8751
8752 do {
8753 cur_pages = atomic_long_read(&user->locked_vm);
8754 new_pages = cur_pages + nr_pages;
8755 if (new_pages > page_limit)
8756 return -ENOMEM;
8757 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8758 new_pages) != cur_pages);
8759
8760 return 0;
8761}
8762
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008763static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008764{
Jens Axboe62e398b2021-02-21 16:19:37 -07008765 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008766 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008767
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008768 if (ctx->mm_account)
8769 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008770}
8771
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008772static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008773{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008774 int ret;
8775
Jens Axboe62e398b2021-02-21 16:19:37 -07008776 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008777 ret = __io_account_mem(ctx->user, nr_pages);
8778 if (ret)
8779 return ret;
8780 }
8781
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008782 if (ctx->mm_account)
8783 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008784
8785 return 0;
8786}
8787
Jens Axboe2b188cc2019-01-07 10:46:33 -07008788static void io_mem_free(void *ptr)
8789{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008790 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008791
Mark Rutland52e04ef2019-04-30 17:30:21 +01008792 if (!ptr)
8793 return;
8794
8795 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008796 if (put_page_testzero(page))
8797 free_compound_page(page);
8798}
8799
8800static void *io_mem_alloc(size_t size)
8801{
8802 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008803 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008804
8805 return (void *) __get_free_pages(gfp_flags, get_order(size));
8806}
8807
Hristo Venev75b28af2019-08-26 17:23:46 +00008808static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8809 size_t *sq_offset)
8810{
8811 struct io_rings *rings;
8812 size_t off, sq_array_size;
8813
8814 off = struct_size(rings, cqes, cq_entries);
8815 if (off == SIZE_MAX)
8816 return SIZE_MAX;
8817
8818#ifdef CONFIG_SMP
8819 off = ALIGN(off, SMP_CACHE_BYTES);
8820 if (off == 0)
8821 return SIZE_MAX;
8822#endif
8823
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008824 if (sq_offset)
8825 *sq_offset = off;
8826
Hristo Venev75b28af2019-08-26 17:23:46 +00008827 sq_array_size = array_size(sizeof(u32), sq_entries);
8828 if (sq_array_size == SIZE_MAX)
8829 return SIZE_MAX;
8830
8831 if (check_add_overflow(off, sq_array_size, &off))
8832 return SIZE_MAX;
8833
Hristo Venev75b28af2019-08-26 17:23:46 +00008834 return off;
8835}
8836
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008837static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008838{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008839 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008840 unsigned int i;
8841
Pavel Begunkov62248432021-04-28 13:11:29 +01008842 if (imu != ctx->dummy_ubuf) {
8843 for (i = 0; i < imu->nr_bvecs; i++)
8844 unpin_user_page(imu->bvec[i].bv_page);
8845 if (imu->acct_pages)
8846 io_unaccount_mem(ctx, imu->acct_pages);
8847 kvfree(imu);
8848 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008849 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008850}
8851
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008852static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8853{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008854 io_buffer_unmap(ctx, &prsrc->buf);
8855 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008856}
8857
8858static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008859{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008860 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008861
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008862 for (i = 0; i < ctx->nr_user_bufs; i++)
8863 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008864 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008865 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008866 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008867 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008868 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008869}
8870
Jens Axboeedafcce2019-01-09 09:16:05 -07008871static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8872{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008873 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008874
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008875 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008876 return -ENXIO;
8877
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008878 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8879 if (!ret)
8880 __io_sqe_buffers_unregister(ctx);
8881 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008882}
8883
8884static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8885 void __user *arg, unsigned index)
8886{
8887 struct iovec __user *src;
8888
8889#ifdef CONFIG_COMPAT
8890 if (ctx->compat) {
8891 struct compat_iovec __user *ciovs;
8892 struct compat_iovec ciov;
8893
8894 ciovs = (struct compat_iovec __user *) arg;
8895 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8896 return -EFAULT;
8897
Jens Axboed55e5f52019-12-11 16:12:15 -07008898 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008899 dst->iov_len = ciov.iov_len;
8900 return 0;
8901 }
8902#endif
8903 src = (struct iovec __user *) arg;
8904 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8905 return -EFAULT;
8906 return 0;
8907}
8908
Jens Axboede293932020-09-17 16:19:16 -06008909/*
8910 * Not super efficient, but this is just a registration time. And we do cache
8911 * the last compound head, so generally we'll only do a full search if we don't
8912 * match that one.
8913 *
8914 * We check if the given compound head page has already been accounted, to
8915 * avoid double accounting it. This allows us to account the full size of the
8916 * page, not just the constituent pages of a huge page.
8917 */
8918static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8919 int nr_pages, struct page *hpage)
8920{
8921 int i, j;
8922
8923 /* check current page array */
8924 for (i = 0; i < nr_pages; i++) {
8925 if (!PageCompound(pages[i]))
8926 continue;
8927 if (compound_head(pages[i]) == hpage)
8928 return true;
8929 }
8930
8931 /* check previously registered pages */
8932 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008933 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008934
8935 for (j = 0; j < imu->nr_bvecs; j++) {
8936 if (!PageCompound(imu->bvec[j].bv_page))
8937 continue;
8938 if (compound_head(imu->bvec[j].bv_page) == hpage)
8939 return true;
8940 }
8941 }
8942
8943 return false;
8944}
8945
8946static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8947 int nr_pages, struct io_mapped_ubuf *imu,
8948 struct page **last_hpage)
8949{
8950 int i, ret;
8951
Pavel Begunkov216e5832021-05-29 12:01:02 +01008952 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008953 for (i = 0; i < nr_pages; i++) {
8954 if (!PageCompound(pages[i])) {
8955 imu->acct_pages++;
8956 } else {
8957 struct page *hpage;
8958
8959 hpage = compound_head(pages[i]);
8960 if (hpage == *last_hpage)
8961 continue;
8962 *last_hpage = hpage;
8963 if (headpage_already_acct(ctx, pages, i, hpage))
8964 continue;
8965 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8966 }
8967 }
8968
8969 if (!imu->acct_pages)
8970 return 0;
8971
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008972 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008973 if (ret)
8974 imu->acct_pages = 0;
8975 return ret;
8976}
8977
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008978static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008979 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008980 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008981{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008982 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008983 struct vm_area_struct **vmas = NULL;
8984 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008985 unsigned long off, start, end, ubuf;
8986 size_t size;
8987 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008988
Pavel Begunkov62248432021-04-28 13:11:29 +01008989 if (!iov->iov_base) {
8990 *pimu = ctx->dummy_ubuf;
8991 return 0;
8992 }
8993
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008994 ubuf = (unsigned long) iov->iov_base;
8995 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8996 start = ubuf >> PAGE_SHIFT;
8997 nr_pages = end - start;
8998
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008999 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009000 ret = -ENOMEM;
9001
9002 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
9003 if (!pages)
9004 goto done;
9005
9006 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
9007 GFP_KERNEL);
9008 if (!vmas)
9009 goto done;
9010
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009011 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01009012 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009013 goto done;
9014
9015 ret = 0;
9016 mmap_read_lock(current->mm);
9017 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
9018 pages, vmas);
9019 if (pret == nr_pages) {
9020 /* don't support file backed memory */
9021 for (i = 0; i < nr_pages; i++) {
9022 struct vm_area_struct *vma = vmas[i];
9023
Pavel Begunkov40dad762021-06-09 15:26:54 +01009024 if (vma_is_shmem(vma))
9025 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009026 if (vma->vm_file &&
9027 !is_file_hugepages(vma->vm_file)) {
9028 ret = -EOPNOTSUPP;
9029 break;
9030 }
9031 }
9032 } else {
9033 ret = pret < 0 ? pret : -EFAULT;
9034 }
9035 mmap_read_unlock(current->mm);
9036 if (ret) {
9037 /*
9038 * if we did partial map, or found file backed vmas,
9039 * release any pages we did get
9040 */
9041 if (pret > 0)
9042 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009043 goto done;
9044 }
9045
9046 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
9047 if (ret) {
9048 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009049 goto done;
9050 }
9051
9052 off = ubuf & ~PAGE_MASK;
9053 size = iov->iov_len;
9054 for (i = 0; i < nr_pages; i++) {
9055 size_t vec_len;
9056
9057 vec_len = min_t(size_t, size, PAGE_SIZE - off);
9058 imu->bvec[i].bv_page = pages[i];
9059 imu->bvec[i].bv_len = vec_len;
9060 imu->bvec[i].bv_offset = off;
9061 off = 0;
9062 size -= vec_len;
9063 }
9064 /* store original address for later verification */
9065 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01009066 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009067 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009068 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009069 ret = 0;
9070done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009071 if (ret)
9072 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009073 kvfree(pages);
9074 kvfree(vmas);
9075 return ret;
9076}
9077
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009078static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009079{
Pavel Begunkov87094462021-04-11 01:46:36 +01009080 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
9081 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009082}
9083
9084static int io_buffer_validate(struct iovec *iov)
9085{
Pavel Begunkov50e96982021-03-24 22:59:01 +00009086 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
9087
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009088 /*
9089 * Don't impose further limits on the size and buffer
9090 * constraints here, we'll -EINVAL later when IO is
9091 * submitted if they are wrong.
9092 */
Pavel Begunkov62248432021-04-28 13:11:29 +01009093 if (!iov->iov_base)
9094 return iov->iov_len ? -EFAULT : 0;
9095 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009096 return -EFAULT;
9097
9098 /* arbitrary limit, but we need something */
9099 if (iov->iov_len > SZ_1G)
9100 return -EFAULT;
9101
Pavel Begunkov50e96982021-03-24 22:59:01 +00009102 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
9103 return -EOVERFLOW;
9104
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009105 return 0;
9106}
9107
9108static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009109 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009110{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009111 struct page *last_hpage = NULL;
9112 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009113 int i, ret;
9114 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009115
Pavel Begunkov87094462021-04-11 01:46:36 +01009116 if (ctx->user_bufs)
9117 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01009118 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01009119 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009120 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009121 if (ret)
9122 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01009123 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9124 if (ret)
9125 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009126 ret = io_buffers_map_alloc(ctx, nr_args);
9127 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08009128 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009129 return ret;
9130 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009131
Pavel Begunkov87094462021-04-11 01:46:36 +01009132 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07009133 ret = io_copy_iov(ctx, &iov, arg, i);
9134 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009135 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009136 ret = io_buffer_validate(&iov);
9137 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009138 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009139 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009140 ret = -EINVAL;
9141 break;
9142 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009143
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009144 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9145 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009146 if (ret)
9147 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009148 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009149
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009150 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009151
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009152 ctx->buf_data = data;
9153 if (ret)
9154 __io_sqe_buffers_unregister(ctx);
9155 else
9156 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009157 return ret;
9158}
9159
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009160static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9161 struct io_uring_rsrc_update2 *up,
9162 unsigned int nr_args)
9163{
9164 u64 __user *tags = u64_to_user_ptr(up->tags);
9165 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009166 struct page *last_hpage = NULL;
9167 bool needs_switch = false;
9168 __u32 done;
9169 int i, err;
9170
9171 if (!ctx->buf_data)
9172 return -ENXIO;
9173 if (up->offset + nr_args > ctx->nr_user_bufs)
9174 return -EINVAL;
9175
9176 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009177 struct io_mapped_ubuf *imu;
9178 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009179 u64 tag = 0;
9180
9181 err = io_copy_iov(ctx, &iov, iovs, done);
9182 if (err)
9183 break;
9184 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9185 err = -EFAULT;
9186 break;
9187 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009188 err = io_buffer_validate(&iov);
9189 if (err)
9190 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009191 if (!iov.iov_base && tag) {
9192 err = -EINVAL;
9193 break;
9194 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009195 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9196 if (err)
9197 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009198
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009199 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009200 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009201 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9202 ctx->rsrc_node, ctx->user_bufs[i]);
9203 if (unlikely(err)) {
9204 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009205 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009206 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009207 ctx->user_bufs[i] = NULL;
9208 needs_switch = true;
9209 }
9210
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009211 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009212 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009213 }
9214
9215 if (needs_switch)
9216 io_rsrc_node_switch(ctx, ctx->buf_data);
9217 return done ? done : err;
9218}
9219
Jens Axboe9b402842019-04-11 11:45:41 -06009220static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9221{
9222 __s32 __user *fds = arg;
9223 int fd;
9224
9225 if (ctx->cq_ev_fd)
9226 return -EBUSY;
9227
9228 if (copy_from_user(&fd, fds, sizeof(*fds)))
9229 return -EFAULT;
9230
9231 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9232 if (IS_ERR(ctx->cq_ev_fd)) {
9233 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009234
Jens Axboe9b402842019-04-11 11:45:41 -06009235 ctx->cq_ev_fd = NULL;
9236 return ret;
9237 }
9238
9239 return 0;
9240}
9241
9242static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9243{
9244 if (ctx->cq_ev_fd) {
9245 eventfd_ctx_put(ctx->cq_ev_fd);
9246 ctx->cq_ev_fd = NULL;
9247 return 0;
9248 }
9249
9250 return -ENXIO;
9251}
9252
Jens Axboe5a2e7452020-02-23 16:23:11 -07009253static void io_destroy_buffers(struct io_ring_ctx *ctx)
9254{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009255 struct io_buffer *buf;
9256 unsigned long index;
9257
Jens Axboe8bab4c02021-09-24 07:12:27 -06009258 xa_for_each(&ctx->io_buffers, index, buf) {
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009259 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009260 cond_resched();
9261 }
Jens Axboe5a2e7452020-02-23 16:23:11 -07009262}
9263
Jens Axboe4010fec2021-02-27 15:04:18 -07009264static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009265{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009266 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009267 int nr = 0;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009268
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009269 mutex_lock(&ctx->uring_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009270 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009271
9272 while (state->free_list.next) {
9273 struct io_wq_work_node *node;
9274 struct io_kiocb *req;
9275
9276 node = wq_stack_extract(&state->free_list);
9277 req = container_of(node, struct io_kiocb, comp_list);
9278 kmem_cache_free(req_cachep, req);
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009279 nr++;
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009280 }
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009281 if (nr)
9282 percpu_ref_put_many(&ctx->refs, nr);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009283 mutex_unlock(&ctx->uring_lock);
9284}
9285
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009286static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009287{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009288 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009289 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009290}
9291
Pavel Begunkovc0724812021-10-04 20:02:54 +01009292static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009293{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009294 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009295
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009296 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009297 mmdrop(ctx->mm_account);
9298 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009299 }
Jens Axboedef596e2019-01-09 08:59:42 -07009300
Pavel Begunkovab409402021-10-09 23:14:41 +01009301 io_rsrc_refs_drop(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009302 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9303 io_wait_rsrc_data(ctx->buf_data);
9304 io_wait_rsrc_data(ctx->file_data);
9305
Hao Xu8bad28d2021-02-19 17:19:36 +08009306 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009307 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009308 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009309 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009310 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009311 if (ctx->rings)
9312 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009313 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009314 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009315 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009316 if (ctx->sq_creds)
9317 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009318
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009319 /* there are no registered resources left, nobody uses it */
9320 if (ctx->rsrc_node)
9321 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009322 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009323 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009324 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov756ab7c2021-10-06 16:06:47 +01009325 flush_delayed_work(&ctx->fallback_work);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009326
9327 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9328 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009329
9330#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07009331 if (ctx->ring_sock) {
9332 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009333 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07009334 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009335#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009336 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009337
Hristo Venev75b28af2019-08-26 17:23:46 +00009338 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009339 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009340
9341 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009342 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009343 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009344 if (ctx->hash_map)
9345 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009346 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009347 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009348 kfree(ctx);
9349}
9350
9351static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9352{
9353 struct io_ring_ctx *ctx = file->private_data;
9354 __poll_t mask = 0;
9355
Pavel Begunkovd60aa652021-10-04 20:02:52 +01009356 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009357 /*
9358 * synchronizes with barrier from wq_has_sleeper call in
9359 * io_commit_cqring
9360 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009361 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009362 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009363 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009364
9365 /*
9366 * Don't flush cqring overflow list here, just do a simple check.
9367 * Otherwise there could possible be ABBA deadlock:
9368 * CPU0 CPU1
9369 * ---- ----
9370 * lock(&ctx->uring_lock);
9371 * lock(&ep->mtx);
9372 * lock(&ctx->uring_lock);
9373 * lock(&ep->mtx);
9374 *
9375 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9376 * pushs them to do the flush.
9377 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009378 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009379 mask |= EPOLLIN | EPOLLRDNORM;
9380
9381 return mask;
9382}
9383
Yejune Deng0bead8c2020-12-24 11:02:20 +08009384static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009385{
Jens Axboe4379bf82021-02-15 13:40:22 -07009386 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009387
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009388 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009389 if (creds) {
9390 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009391 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009392 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009393
9394 return -EINVAL;
9395}
9396
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009397struct io_tctx_exit {
9398 struct callback_head task_work;
9399 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009400 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009401};
9402
Pavel Begunkovc0724812021-10-04 20:02:54 +01009403static __cold void io_tctx_exit_cb(struct callback_head *cb)
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009404{
9405 struct io_uring_task *tctx = current->io_uring;
9406 struct io_tctx_exit *work;
9407
9408 work = container_of(cb, struct io_tctx_exit, task_work);
9409 /*
9410 * When @in_idle, we're in cancellation and it's racy to remove the
9411 * node. It'll be removed by the end of cancellation, just ignore it.
9412 */
9413 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009414 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009415 complete(&work->completion);
9416}
9417
Pavel Begunkovc0724812021-10-04 20:02:54 +01009418static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
Pavel Begunkov28090c12021-04-25 23:34:45 +01009419{
9420 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9421
9422 return req->ctx == data;
9423}
9424
Pavel Begunkovc0724812021-10-04 20:02:54 +01009425static __cold void io_ring_exit_work(struct work_struct *work)
Jens Axboe85faa7b2020-04-09 18:14:00 -06009426{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009427 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009428 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009429 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009430 struct io_tctx_exit exit;
9431 struct io_tctx_node *node;
9432 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009433
Jens Axboe56952e92020-06-17 15:00:04 -06009434 /*
9435 * If we're doing polled IO and end up having requests being
9436 * submitted async (out-of-line), then completions can come in while
9437 * we're waiting for refs to drop. We need to reap these manually,
9438 * as nobody else will be looking for them.
9439 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009440 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009441 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009442 if (ctx->sq_data) {
9443 struct io_sq_data *sqd = ctx->sq_data;
9444 struct task_struct *tsk;
9445
9446 io_sq_thread_park(sqd);
9447 tsk = sqd->thread;
9448 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9449 io_wq_cancel_cb(tsk->io_uring->io_wq,
9450 io_cancel_ctx_cb, ctx, true);
9451 io_sq_thread_unpark(sqd);
9452 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009453
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009454 io_req_caches_free(ctx);
9455
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009456 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9457 /* there is little hope left, don't run it too often */
9458 interval = HZ * 60;
9459 }
9460 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009461
Pavel Begunkov7f006512021-04-14 13:38:34 +01009462 init_completion(&exit.completion);
9463 init_task_work(&exit.task_work, io_tctx_exit_cb);
9464 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009465 /*
9466 * Some may use context even when all refs and requests have been put,
9467 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009468 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009469 * this lock/unlock section also waits them to finish.
9470 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009471 mutex_lock(&ctx->uring_lock);
9472 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009473 WARN_ON_ONCE(time_after(jiffies, timeout));
9474
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009475 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9476 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009477 /* don't spin on a single task if cancellation failed */
9478 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009479 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9480 if (WARN_ON_ONCE(ret))
9481 continue;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009482
9483 mutex_unlock(&ctx->uring_lock);
9484 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009485 mutex_lock(&ctx->uring_lock);
9486 }
9487 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009488 spin_lock(&ctx->completion_lock);
9489 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009490
Jens Axboe85faa7b2020-04-09 18:14:00 -06009491 io_ring_ctx_free(ctx);
9492}
9493
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009494/* Returns true if we found and killed one or more timeouts */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009495static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx,
9496 struct task_struct *tsk, bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009497{
9498 struct io_kiocb *req, *tmp;
9499 int canceled = 0;
9500
Jens Axboe79ebeae2021-08-10 15:18:27 -06009501 spin_lock(&ctx->completion_lock);
9502 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009503 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009504 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009505 io_kill_timeout(req, -ECANCELED);
9506 canceled++;
9507 }
9508 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009509 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009510 if (canceled != 0)
9511 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009512 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009513 if (canceled != 0)
9514 io_cqring_ev_posted(ctx);
9515 return canceled != 0;
9516}
9517
Pavel Begunkovc0724812021-10-04 20:02:54 +01009518static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009519{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009520 unsigned long index;
9521 struct creds *creds;
9522
Jens Axboe2b188cc2019-01-07 10:46:33 -07009523 mutex_lock(&ctx->uring_lock);
9524 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009525 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009526 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009527 xa_for_each(&ctx->personalities, index, creds)
9528 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009529 mutex_unlock(&ctx->uring_lock);
9530
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009531 io_kill_timeouts(ctx, NULL, true);
9532 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009533
Jens Axboe15dff282019-11-13 09:09:23 -07009534 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009535 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009536
Jens Axboe85faa7b2020-04-09 18:14:00 -06009537 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009538 /*
9539 * Use system_unbound_wq to avoid spawning tons of event kworkers
9540 * if we're exiting a ton of rings at the same time. It just adds
9541 * noise and overhead, there's no discernable change in runtime
9542 * over using system_wq.
9543 */
9544 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009545}
9546
9547static int io_uring_release(struct inode *inode, struct file *file)
9548{
9549 struct io_ring_ctx *ctx = file->private_data;
9550
9551 file->private_data = NULL;
9552 io_ring_ctx_wait_and_kill(ctx);
9553 return 0;
9554}
9555
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009556struct io_task_cancel {
9557 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009558 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009559};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009560
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009561static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009562{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009563 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009564 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009565 bool ret;
9566
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009567 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009568 struct io_ring_ctx *ctx = req->ctx;
9569
9570 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009571 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009572 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009573 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009574 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009575 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009576 }
9577 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009578}
9579
Pavel Begunkovc0724812021-10-04 20:02:54 +01009580static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
9581 struct task_struct *task,
9582 bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009583{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009584 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009585 LIST_HEAD(list);
9586
Jens Axboe79ebeae2021-08-10 15:18:27 -06009587 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009588 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009589 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009590 list_cut_position(&list, &ctx->defer_list, &de->list);
9591 break;
9592 }
9593 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009594 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009595 if (list_empty(&list))
9596 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009597
9598 while (!list_empty(&list)) {
9599 de = list_first_entry(&list, struct io_defer_entry, list);
9600 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009601 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009602 kfree(de);
9603 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009604 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009605}
9606
Pavel Begunkovc0724812021-10-04 20:02:54 +01009607static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
Pavel Begunkov1b007642021-03-06 11:02:17 +00009608{
9609 struct io_tctx_node *node;
9610 enum io_wq_cancel cret;
9611 bool ret = false;
9612
9613 mutex_lock(&ctx->uring_lock);
9614 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9615 struct io_uring_task *tctx = node->task->io_uring;
9616
9617 /*
9618 * io_wq will stay alive while we hold uring_lock, because it's
9619 * killed after ctx nodes, which requires to take the lock.
9620 */
9621 if (!tctx || !tctx->io_wq)
9622 continue;
9623 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9624 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9625 }
9626 mutex_unlock(&ctx->uring_lock);
9627
9628 return ret;
9629}
9630
Pavel Begunkovc0724812021-10-04 20:02:54 +01009631static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9632 struct task_struct *task,
9633 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009634{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009635 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009636 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009637
9638 while (1) {
9639 enum io_wq_cancel cret;
9640 bool ret = false;
9641
Pavel Begunkov1b007642021-03-06 11:02:17 +00009642 if (!task) {
9643 ret |= io_uring_try_cancel_iowq(ctx);
9644 } else if (tctx && tctx->io_wq) {
9645 /*
9646 * Cancels requests of all rings, not only @ctx, but
9647 * it's fine as the task is in exit/exec.
9648 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009649 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009650 &cancel, true);
9651 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9652 }
9653
9654 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009655 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009656 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01009657 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009658 io_iopoll_try_reap_events(ctx);
9659 ret = true;
9660 }
9661 }
9662
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009663 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9664 ret |= io_poll_remove_all(ctx, task, cancel_all);
9665 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009666 if (task)
9667 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009668 if (!ret)
9669 break;
9670 cond_resched();
9671 }
9672}
9673
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009674static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009675{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009676 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009677 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009678 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009679
9680 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009681 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009682 if (unlikely(ret))
9683 return ret;
Pavel Begunkove139a1e2021-10-19 23:43:46 +01009684
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009685 tctx = current->io_uring;
Pavel Begunkove139a1e2021-10-19 23:43:46 +01009686 if (ctx->iowq_limits_set) {
9687 unsigned int limits[2] = { ctx->iowq_limits[0],
9688 ctx->iowq_limits[1], };
9689
9690 ret = io_wq_max_workers(tctx->io_wq, limits);
9691 if (ret)
9692 return ret;
9693 }
Jens Axboe0f212202020-09-13 13:09:39 -06009694 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009695 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9696 node = kmalloc(sizeof(*node), GFP_KERNEL);
9697 if (!node)
9698 return -ENOMEM;
9699 node->ctx = ctx;
9700 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009701
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009702 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9703 node, GFP_KERNEL));
9704 if (ret) {
9705 kfree(node);
9706 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009707 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009708
9709 mutex_lock(&ctx->uring_lock);
9710 list_add(&node->ctx_node, &ctx->tctx_list);
9711 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009712 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009713 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009714 return 0;
9715}
9716
9717/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009718 * Note that this task has used io_uring. We use it for cancelation purposes.
9719 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009720static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009721{
9722 struct io_uring_task *tctx = current->io_uring;
9723
9724 if (likely(tctx && tctx->last == ctx))
9725 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009726 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009727}
9728
9729/*
Jens Axboe0f212202020-09-13 13:09:39 -06009730 * Remove this io_uring_file -> task mapping.
9731 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009732static __cold void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009733{
9734 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009735 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009736
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009737 if (!tctx)
9738 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009739 node = xa_erase(&tctx->xa, index);
9740 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009741 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009742
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009743 WARN_ON_ONCE(current != node->task);
9744 WARN_ON_ONCE(list_empty(&node->ctx_node));
9745
9746 mutex_lock(&node->ctx->uring_lock);
9747 list_del(&node->ctx_node);
9748 mutex_unlock(&node->ctx->uring_lock);
9749
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009750 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009751 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009752 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009753}
9754
Pavel Begunkovc0724812021-10-04 20:02:54 +01009755static __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009756{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009757 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009758 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009759 unsigned long index;
9760
Jens Axboe8bab4c02021-09-24 07:12:27 -06009761 xa_for_each(&tctx->xa, index, node) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009762 io_uring_del_tctx_node(index);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009763 cond_resched();
9764 }
Marco Elverb16ef422021-05-27 11:25:48 +02009765 if (wq) {
9766 /*
9767 * Must be after io_uring_del_task_file() (removes nodes under
9768 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9769 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009770 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009771 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009772 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009773}
9774
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009775static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009776{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009777 if (tracked)
9778 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009779 return percpu_counter_sum(&tctx->inflight);
9780}
9781
Pavel Begunkovc0724812021-10-04 20:02:54 +01009782static __cold void io_uring_drop_tctx_refs(struct task_struct *task)
Pavel Begunkov09899b12021-06-14 02:36:22 +01009783{
9784 struct io_uring_task *tctx = task->io_uring;
9785 unsigned int refs = tctx->cached_refs;
9786
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009787 if (refs) {
9788 tctx->cached_refs = 0;
9789 percpu_counter_sub(&tctx->inflight, refs);
9790 put_task_struct_many(task, refs);
9791 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009792}
9793
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009794/*
9795 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9796 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9797 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009798static __cold void io_uring_cancel_generic(bool cancel_all,
9799 struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009800{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009801 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009802 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009803 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009804 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009805
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009806 WARN_ON_ONCE(sqd && sqd->thread != current);
9807
Palash Oswal6d042ff2021-04-27 18:21:49 +05309808 if (!current->io_uring)
9809 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009810 if (tctx->io_wq)
9811 io_wq_exit_start(tctx->io_wq);
9812
Jens Axboefdaf0832020-10-30 09:37:30 -06009813 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009814 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009815 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009816 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009817 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009818 if (!inflight)
9819 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009820
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009821 if (!sqd) {
9822 struct io_tctx_node *node;
9823 unsigned long index;
9824
9825 xa_for_each(&tctx->xa, index, node) {
9826 /* sqpoll task will cancel all its requests */
9827 if (node->ctx->sq_data)
9828 continue;
9829 io_uring_try_cancel_requests(node->ctx, current,
9830 cancel_all);
9831 }
9832 } else {
9833 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9834 io_uring_try_cancel_requests(ctx, current,
9835 cancel_all);
9836 }
9837
9838 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009839 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009840 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009841 * If we've seen completions, retry without waiting. This
9842 * avoids a race where a completion comes in before we did
9843 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009844 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009845 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009846 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009847 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009848 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009849 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009850
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009851 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009852 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009853 /* for exec all current's requests should be gone, kill tctx */
9854 __io_uring_free(current);
9855 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009856}
9857
Hao Xuf552a272021-08-12 12:14:35 +08009858void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009859{
Hao Xuf552a272021-08-12 12:14:35 +08009860 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009861}
9862
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009863static void *io_uring_validate_mmap_request(struct file *file,
9864 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009865{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009866 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009867 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009868 struct page *page;
9869 void *ptr;
9870
9871 switch (offset) {
9872 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009873 case IORING_OFF_CQ_RING:
9874 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009875 break;
9876 case IORING_OFF_SQES:
9877 ptr = ctx->sq_sqes;
9878 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009879 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009880 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009881 }
9882
9883 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009884 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009885 return ERR_PTR(-EINVAL);
9886
9887 return ptr;
9888}
9889
9890#ifdef CONFIG_MMU
9891
Pavel Begunkovc0724812021-10-04 20:02:54 +01009892static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009893{
9894 size_t sz = vma->vm_end - vma->vm_start;
9895 unsigned long pfn;
9896 void *ptr;
9897
9898 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9899 if (IS_ERR(ptr))
9900 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009901
9902 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9903 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9904}
9905
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009906#else /* !CONFIG_MMU */
9907
9908static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9909{
9910 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9911}
9912
9913static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9914{
9915 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9916}
9917
9918static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9919 unsigned long addr, unsigned long len,
9920 unsigned long pgoff, unsigned long flags)
9921{
9922 void *ptr;
9923
9924 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9925 if (IS_ERR(ptr))
9926 return PTR_ERR(ptr);
9927
9928 return (unsigned long) ptr;
9929}
9930
9931#endif /* !CONFIG_MMU */
9932
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009933static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009934{
9935 DEFINE_WAIT(wait);
9936
9937 do {
9938 if (!io_sqring_full(ctx))
9939 break;
Jens Axboe90554202020-09-03 12:12:41 -06009940 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9941
9942 if (!io_sqring_full(ctx))
9943 break;
Jens Axboe90554202020-09-03 12:12:41 -06009944 schedule();
9945 } while (!signal_pending(current));
9946
9947 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009948 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009949}
9950
Hao Xuc73ebb62020-11-03 10:54:37 +08009951static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9952 struct __kernel_timespec __user **ts,
9953 const sigset_t __user **sig)
9954{
9955 struct io_uring_getevents_arg arg;
9956
9957 /*
9958 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9959 * is just a pointer to the sigset_t.
9960 */
9961 if (!(flags & IORING_ENTER_EXT_ARG)) {
9962 *sig = (const sigset_t __user *) argp;
9963 *ts = NULL;
9964 return 0;
9965 }
9966
9967 /*
9968 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9969 * timespec and sigset_t pointers if good.
9970 */
9971 if (*argsz != sizeof(arg))
9972 return -EINVAL;
9973 if (copy_from_user(&arg, argp, sizeof(arg)))
9974 return -EFAULT;
9975 *sig = u64_to_user_ptr(arg.sigmask);
9976 *argsz = arg.sigmask_sz;
9977 *ts = u64_to_user_ptr(arg.ts);
9978 return 0;
9979}
9980
Jens Axboe2b188cc2019-01-07 10:46:33 -07009981SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009982 u32, min_complete, u32, flags, const void __user *, argp,
9983 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009984{
9985 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009986 int submitted = 0;
9987 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009988 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009989
Jens Axboe4c6e2772020-07-01 11:29:10 -06009990 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009991
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009992 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9993 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009994 return -EINVAL;
9995
9996 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009997 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009998 return -EBADF;
9999
10000 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010001 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010002 goto out_fput;
10003
10004 ret = -ENXIO;
10005 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010006 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010007 goto out_fput;
10008
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010009 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010010 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010011 goto out;
10012
Jens Axboe6c271ce2019-01-10 11:22:30 -070010013 /*
10014 * For SQ polling, the thread will do all submissions and completions.
10015 * Just return the requested submit count, and wake the thread if
10016 * we were asked to.
10017 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -060010018 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -070010019 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +010010020 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +000010021
Jens Axboe21f96522021-08-14 09:04:40 -060010022 if (unlikely(ctx->sq_data->thread == NULL)) {
10023 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +010010024 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -060010025 }
Jens Axboe6c271ce2019-01-10 11:22:30 -070010026 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -060010027 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +000010028 if (flags & IORING_ENTER_SQ_WAIT) {
10029 ret = io_sqpoll_wait_sq(ctx);
10030 if (ret)
10031 goto out;
10032 }
Jens Axboe6c271ce2019-01-10 11:22:30 -070010033 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -060010034 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010035 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -060010036 if (unlikely(ret))
10037 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010038 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -060010039 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010040 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010041
10042 if (submitted != to_submit)
10043 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010044 }
10045 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +080010046 const sigset_t __user *sig;
10047 struct __kernel_timespec __user *ts;
10048
10049 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
10050 if (unlikely(ret))
10051 goto out;
10052
Jens Axboe2b188cc2019-01-07 10:46:33 -070010053 min_complete = min(min_complete, ctx->cq_entries);
10054
Xiaoguang Wang32b22442020-03-11 09:26:09 +080010055 /*
10056 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
10057 * space applications don't need to do io completion events
10058 * polling again, they can rely on io_sq_thread to do polling
10059 * work, which can reduce cpu usage and uring_lock contention.
10060 */
10061 if (ctx->flags & IORING_SETUP_IOPOLL &&
10062 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +030010063 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -070010064 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +080010065 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -070010066 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010067 }
10068
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010069out:
Pavel Begunkov6805b322019-10-08 02:18:42 +030010070 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010071out_fput:
10072 fdput(f);
10073 return submitted ? submitted : ret;
10074}
10075
Tobias Klauserbebdb652020-02-26 18:38:32 +010010076#ifdef CONFIG_PROC_FS
Pavel Begunkovc0724812021-10-04 20:02:54 +010010077static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010078 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -070010079{
Jens Axboe87ce9552020-01-30 08:25:34 -070010080 struct user_namespace *uns = seq_user_ns(m);
10081 struct group_info *gi;
10082 kernel_cap_t cap;
10083 unsigned __capi;
10084 int g;
10085
10086 seq_printf(m, "%5d\n", id);
10087 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
10088 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
10089 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
10090 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
10091 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
10092 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
10093 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
10094 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
10095 seq_puts(m, "\n\tGroups:\t");
10096 gi = cred->group_info;
10097 for (g = 0; g < gi->ngroups; g++) {
10098 seq_put_decimal_ull(m, g ? " " : "",
10099 from_kgid_munged(uns, gi->gid[g]));
10100 }
10101 seq_puts(m, "\n\tCapEff:\t");
10102 cap = cred->cap_effective;
10103 CAP_FOR_EACH_U32(__capi)
10104 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
10105 seq_putc(m, '\n');
10106 return 0;
10107}
10108
Pavel Begunkovc0724812021-10-04 20:02:54 +010010109static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx,
10110 struct seq_file *m)
Jens Axboe87ce9552020-01-30 08:25:34 -070010111{
Joseph Qidbbe9c62020-09-29 09:01:22 -060010112 struct io_sq_data *sq = NULL;
Hao Xu83f84352021-09-13 21:08:54 +080010113 struct io_overflow_cqe *ocqe;
10114 struct io_rings *r = ctx->rings;
10115 unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
Hao Xu83f84352021-09-13 21:08:54 +080010116 unsigned int sq_head = READ_ONCE(r->sq.head);
10117 unsigned int sq_tail = READ_ONCE(r->sq.tail);
10118 unsigned int cq_head = READ_ONCE(r->cq.head);
10119 unsigned int cq_tail = READ_ONCE(r->cq.tail);
Jens Axboef75d1182021-10-29 06:36:45 -060010120 unsigned int sq_entries, cq_entries;
Jens Axboefad8e0d2020-09-28 08:57:48 -060010121 bool has_lock;
Hao Xu83f84352021-09-13 21:08:54 +080010122 unsigned int i;
10123
10124 /*
10125 * we may get imprecise sqe and cqe info if uring is actively running
10126 * since we get cached_sq_head and cached_cq_tail without uring_lock
10127 * and sq_tail and cq_head are changed by userspace. But it's ok since
10128 * we usually use these info when it is stuck.
10129 */
Jens Axboef75d1182021-10-29 06:36:45 -060010130 seq_printf(m, "SqMask:\t\t0x%x\n", sq_mask);
10131 seq_printf(m, "SqHead:\t%u\n", sq_head);
10132 seq_printf(m, "SqTail:\t%u\n", sq_tail);
10133 seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head);
10134 seq_printf(m, "CqMask:\t0x%x\n", cq_mask);
10135 seq_printf(m, "CqHead:\t%u\n", cq_head);
10136 seq_printf(m, "CqTail:\t%u\n", cq_tail);
10137 seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail);
10138 seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head);
10139 sq_entries = min(sq_tail - sq_head, ctx->sq_entries);
10140 for (i = 0; i < sq_entries; i++) {
10141 unsigned int entry = i + sq_head;
10142 unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]);
Jens Axboea1957782021-11-05 09:31:05 -060010143 struct io_uring_sqe *sqe;
Hao Xu83f84352021-09-13 21:08:54 +080010144
Jens Axboef75d1182021-10-29 06:36:45 -060010145 if (sq_idx > sq_mask)
10146 continue;
10147 sqe = &ctx->sq_sqes[sq_idx];
10148 seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n",
10149 sq_idx, sqe->opcode, sqe->fd, sqe->flags,
10150 sqe->user_data);
Hao Xu83f84352021-09-13 21:08:54 +080010151 }
Jens Axboef75d1182021-10-29 06:36:45 -060010152 seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head);
10153 cq_entries = min(cq_tail - cq_head, ctx->cq_entries);
10154 for (i = 0; i < cq_entries; i++) {
10155 unsigned int entry = i + cq_head;
10156 struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask];
Hao Xu83f84352021-09-13 21:08:54 +080010157
10158 seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n",
Jens Axboef75d1182021-10-29 06:36:45 -060010159 entry & cq_mask, cqe->user_data, cqe->res,
10160 cqe->flags);
Hao Xu83f84352021-09-13 21:08:54 +080010161 }
Jens Axboe87ce9552020-01-30 08:25:34 -070010162
Jens Axboefad8e0d2020-09-28 08:57:48 -060010163 /*
10164 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10165 * since fdinfo case grabs it in the opposite direction of normal use
10166 * cases. If we fail to get the lock, we just don't iterate any
10167 * structures that could be going away outside the io_uring mutex.
10168 */
10169 has_lock = mutex_trylock(&ctx->uring_lock);
10170
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010171 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -060010172 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010173 if (!sq->thread)
10174 sq = NULL;
10175 }
Joseph Qidbbe9c62020-09-29 09:01:22 -060010176
10177 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10178 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -070010179 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010180 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -070010181 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -070010182
Jens Axboe87ce9552020-01-30 08:25:34 -070010183 if (f)
10184 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10185 else
10186 seq_printf(m, "%5u: <none>\n", i);
10187 }
10188 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010189 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +010010190 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +010010191 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -070010192
Pavel Begunkov4751f532021-04-01 15:43:55 +010010193 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -070010194 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010195 if (has_lock && !xa_empty(&ctx->personalities)) {
10196 unsigned long index;
10197 const struct cred *cred;
10198
Jens Axboe87ce9552020-01-30 08:25:34 -070010199 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010200 xa_for_each(&ctx->personalities, index, cred)
10201 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010202 }
Hao Xu83f84352021-09-13 21:08:54 +080010203 if (has_lock)
10204 mutex_unlock(&ctx->uring_lock);
10205
10206 seq_puts(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010207 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010208 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10209 struct hlist_head *list = &ctx->cancel_hash[i];
10210 struct io_kiocb *req;
10211
10212 hlist_for_each_entry(req, list, hash_node)
10213 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10214 req->task->task_works != NULL);
10215 }
Hao Xu83f84352021-09-13 21:08:54 +080010216
10217 seq_puts(m, "CqOverflowList:\n");
10218 list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
10219 struct io_uring_cqe *cqe = &ocqe->cqe;
10220
10221 seq_printf(m, " user_data=%llu, res=%d, flags=%x\n",
10222 cqe->user_data, cqe->res, cqe->flags);
10223
10224 }
10225
Jens Axboe79ebeae2021-08-10 15:18:27 -060010226 spin_unlock(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010227}
10228
Pavel Begunkovc0724812021-10-04 20:02:54 +010010229static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
Jens Axboe87ce9552020-01-30 08:25:34 -070010230{
10231 struct io_ring_ctx *ctx = f->private_data;
10232
10233 if (percpu_ref_tryget(&ctx->refs)) {
10234 __io_uring_show_fdinfo(ctx, m);
10235 percpu_ref_put(&ctx->refs);
10236 }
10237}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010238#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010239
Jens Axboe2b188cc2019-01-07 10:46:33 -070010240static const struct file_operations io_uring_fops = {
10241 .release = io_uring_release,
10242 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010243#ifndef CONFIG_MMU
10244 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10245 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10246#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010247 .poll = io_uring_poll,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010248#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010249 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010250#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010251};
10252
Pavel Begunkovc0724812021-10-04 20:02:54 +010010253static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10254 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010255{
Hristo Venev75b28af2019-08-26 17:23:46 +000010256 struct io_rings *rings;
10257 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010258
Jens Axboebd740482020-08-05 12:58:23 -060010259 /* make sure these are sane, as we already accounted them */
10260 ctx->sq_entries = p->sq_entries;
10261 ctx->cq_entries = p->cq_entries;
10262
Hristo Venev75b28af2019-08-26 17:23:46 +000010263 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10264 if (size == SIZE_MAX)
10265 return -EOVERFLOW;
10266
10267 rings = io_mem_alloc(size);
10268 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010269 return -ENOMEM;
10270
Hristo Venev75b28af2019-08-26 17:23:46 +000010271 ctx->rings = rings;
10272 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10273 rings->sq_ring_mask = p->sq_entries - 1;
10274 rings->cq_ring_mask = p->cq_entries - 1;
10275 rings->sq_ring_entries = p->sq_entries;
10276 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010277
10278 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010279 if (size == SIZE_MAX) {
10280 io_mem_free(ctx->rings);
10281 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010282 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010283 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010284
10285 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010286 if (!ctx->sq_sqes) {
10287 io_mem_free(ctx->rings);
10288 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010289 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010290 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010291
Jens Axboe2b188cc2019-01-07 10:46:33 -070010292 return 0;
10293}
10294
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010295static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10296{
10297 int ret, fd;
10298
10299 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10300 if (fd < 0)
10301 return fd;
10302
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010303 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010304 if (ret) {
10305 put_unused_fd(fd);
10306 return ret;
10307 }
10308 fd_install(fd, file);
10309 return fd;
10310}
10311
Jens Axboe2b188cc2019-01-07 10:46:33 -070010312/*
10313 * Allocate an anonymous fd, this is what constitutes the application
10314 * visible backing of an io_uring instance. The application mmaps this
10315 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10316 * we have to tie this fd to a socket for file garbage collection purposes.
10317 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010318static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010319{
10320 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010321#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010322 int ret;
10323
Jens Axboe2b188cc2019-01-07 10:46:33 -070010324 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10325 &ctx->ring_sock);
10326 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010327 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010328#endif
10329
Paul Moore91a9ab72021-02-01 19:33:52 -050010330 file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx,
10331 O_RDWR | O_CLOEXEC, NULL);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010332#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010333 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010334 sock_release(ctx->ring_sock);
10335 ctx->ring_sock = NULL;
10336 } else {
10337 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010338 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010339#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010340 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010341}
10342
Pavel Begunkovc0724812021-10-04 20:02:54 +010010343static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
10344 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010345{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010346 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010347 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010348 int ret;
10349
Jens Axboe8110c1a2019-12-28 15:39:54 -070010350 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010351 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010352 if (entries > IORING_MAX_ENTRIES) {
10353 if (!(p->flags & IORING_SETUP_CLAMP))
10354 return -EINVAL;
10355 entries = IORING_MAX_ENTRIES;
10356 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010357
10358 /*
10359 * Use twice as many entries for the CQ ring. It's possible for the
10360 * application to drive a higher depth than the size of the SQ ring,
10361 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010362 * some flexibility in overcommitting a bit. If the application has
10363 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10364 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010365 */
10366 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010367 if (p->flags & IORING_SETUP_CQSIZE) {
10368 /*
10369 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10370 * to a power-of-two, if it isn't already. We do NOT impose
10371 * any cq vs sq ring sizing.
10372 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010373 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010374 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010375 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10376 if (!(p->flags & IORING_SETUP_CLAMP))
10377 return -EINVAL;
10378 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10379 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010380 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10381 if (p->cq_entries < p->sq_entries)
10382 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010383 } else {
10384 p->cq_entries = 2 * p->sq_entries;
10385 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010386
Jens Axboe2b188cc2019-01-07 10:46:33 -070010387 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010388 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010389 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010390 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010391 if (!capable(CAP_IPC_LOCK))
10392 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010393
10394 /*
10395 * This is just grabbed for accounting purposes. When a process exits,
10396 * the mm is exited and dropped before the files, hence we need to hang
10397 * on to this mm purely for the purposes of being able to unaccount
10398 * memory (locked/pinned vm). It's not used for anything else.
10399 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010400 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010401 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010402
Jens Axboe2b188cc2019-01-07 10:46:33 -070010403 ret = io_allocate_scq_urings(ctx, p);
10404 if (ret)
10405 goto err;
10406
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010407 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010408 if (ret)
10409 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010410 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010411 ret = io_rsrc_node_switch_start(ctx);
10412 if (ret)
10413 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010414 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010415
Jens Axboe2b188cc2019-01-07 10:46:33 -070010416 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010417 p->sq_off.head = offsetof(struct io_rings, sq.head);
10418 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10419 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10420 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10421 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10422 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10423 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010424
10425 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010426 p->cq_off.head = offsetof(struct io_rings, cq.head);
10427 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10428 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10429 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10430 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10431 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010432 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010433
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010434 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10435 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010436 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010437 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010438 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
10439 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010440
10441 if (copy_to_user(params, p, sizeof(*p))) {
10442 ret = -EFAULT;
10443 goto err;
10444 }
Jens Axboed1719f72020-07-30 13:43:53 -060010445
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010446 file = io_uring_get_file(ctx);
10447 if (IS_ERR(file)) {
10448 ret = PTR_ERR(file);
10449 goto err;
10450 }
10451
Jens Axboed1719f72020-07-30 13:43:53 -060010452 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010453 * Install ring fd as the very last thing, so we don't risk someone
10454 * having closed it before we finish setup
10455 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010456 ret = io_uring_install_fd(ctx, file);
10457 if (ret < 0) {
10458 /* fput will clean it up */
10459 fput(file);
10460 return ret;
10461 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010462
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010463 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010464 return ret;
10465err:
10466 io_ring_ctx_wait_and_kill(ctx);
10467 return ret;
10468}
10469
10470/*
10471 * Sets up an aio uring context, and returns the fd. Applications asks for a
10472 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10473 * params structure passed in.
10474 */
10475static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10476{
10477 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010478 int i;
10479
10480 if (copy_from_user(&p, params, sizeof(p)))
10481 return -EFAULT;
10482 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10483 if (p.resv[i])
10484 return -EINVAL;
10485 }
10486
Jens Axboe6c271ce2019-01-10 11:22:30 -070010487 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010488 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010489 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10490 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010491 return -EINVAL;
10492
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010493 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010494}
10495
10496SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10497 struct io_uring_params __user *, params)
10498{
10499 return io_uring_setup(entries, params);
10500}
10501
Pavel Begunkovc0724812021-10-04 20:02:54 +010010502static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg,
10503 unsigned nr_args)
Jens Axboe66f4af92020-01-16 15:36:52 -070010504{
10505 struct io_uring_probe *p;
10506 size_t size;
10507 int i, ret;
10508
10509 size = struct_size(p, ops, nr_args);
10510 if (size == SIZE_MAX)
10511 return -EOVERFLOW;
10512 p = kzalloc(size, GFP_KERNEL);
10513 if (!p)
10514 return -ENOMEM;
10515
10516 ret = -EFAULT;
10517 if (copy_from_user(p, arg, size))
10518 goto out;
10519 ret = -EINVAL;
10520 if (memchr_inv(p, 0, size))
10521 goto out;
10522
10523 p->last_op = IORING_OP_LAST - 1;
10524 if (nr_args > IORING_OP_LAST)
10525 nr_args = IORING_OP_LAST;
10526
10527 for (i = 0; i < nr_args; i++) {
10528 p->ops[i].op = i;
10529 if (!io_op_defs[i].not_supported)
10530 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10531 }
10532 p->ops_len = i;
10533
10534 ret = 0;
10535 if (copy_to_user(arg, p, size))
10536 ret = -EFAULT;
10537out:
10538 kfree(p);
10539 return ret;
10540}
10541
Jens Axboe071698e2020-01-28 10:04:42 -070010542static int io_register_personality(struct io_ring_ctx *ctx)
10543{
Jens Axboe4379bf82021-02-15 13:40:22 -070010544 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010545 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010546 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010547
Jens Axboe4379bf82021-02-15 13:40:22 -070010548 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010549
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010550 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10551 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010552 if (ret < 0) {
10553 put_cred(creds);
10554 return ret;
10555 }
10556 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010557}
10558
Pavel Begunkovc0724812021-10-04 20:02:54 +010010559static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
10560 void __user *arg, unsigned int nr_args)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010561{
10562 struct io_uring_restriction *res;
10563 size_t size;
10564 int i, ret;
10565
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010566 /* Restrictions allowed only if rings started disabled */
10567 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10568 return -EBADFD;
10569
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010570 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010571 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010572 return -EBUSY;
10573
10574 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10575 return -EINVAL;
10576
10577 size = array_size(nr_args, sizeof(*res));
10578 if (size == SIZE_MAX)
10579 return -EOVERFLOW;
10580
10581 res = memdup_user(arg, size);
10582 if (IS_ERR(res))
10583 return PTR_ERR(res);
10584
10585 ret = 0;
10586
10587 for (i = 0; i < nr_args; i++) {
10588 switch (res[i].opcode) {
10589 case IORING_RESTRICTION_REGISTER_OP:
10590 if (res[i].register_op >= IORING_REGISTER_LAST) {
10591 ret = -EINVAL;
10592 goto out;
10593 }
10594
10595 __set_bit(res[i].register_op,
10596 ctx->restrictions.register_op);
10597 break;
10598 case IORING_RESTRICTION_SQE_OP:
10599 if (res[i].sqe_op >= IORING_OP_LAST) {
10600 ret = -EINVAL;
10601 goto out;
10602 }
10603
10604 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10605 break;
10606 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10607 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10608 break;
10609 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10610 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10611 break;
10612 default:
10613 ret = -EINVAL;
10614 goto out;
10615 }
10616 }
10617
10618out:
10619 /* Reset all restrictions if an error happened */
10620 if (ret != 0)
10621 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10622 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010623 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010624
10625 kfree(res);
10626 return ret;
10627}
10628
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010629static int io_register_enable_rings(struct io_ring_ctx *ctx)
10630{
10631 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10632 return -EBADFD;
10633
10634 if (ctx->restrictions.registered)
10635 ctx->restricted = 1;
10636
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010637 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10638 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10639 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010640 return 0;
10641}
10642
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010643static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010644 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010645 unsigned nr_args)
10646{
10647 __u32 tmp;
10648 int err;
10649
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010650 if (up->resv)
10651 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010652 if (check_add_overflow(up->offset, nr_args, &tmp))
10653 return -EOVERFLOW;
10654 err = io_rsrc_node_switch_start(ctx);
10655 if (err)
10656 return err;
10657
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010658 switch (type) {
10659 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010660 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010661 case IORING_RSRC_BUFFER:
10662 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010663 }
10664 return -EINVAL;
10665}
10666
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010667static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10668 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010669{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010670 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010671
10672 if (!nr_args)
10673 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010674 memset(&up, 0, sizeof(up));
10675 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10676 return -EFAULT;
10677 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10678}
10679
10680static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010681 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010682{
10683 struct io_uring_rsrc_update2 up;
10684
10685 if (size != sizeof(up))
10686 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010687 if (copy_from_user(&up, arg, sizeof(up)))
10688 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010689 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010690 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010691 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010692}
10693
Pavel Begunkovc0724812021-10-04 20:02:54 +010010694static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010695 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010696{
10697 struct io_uring_rsrc_register rr;
10698
10699 /* keep it extendible */
10700 if (size != sizeof(rr))
10701 return -EINVAL;
10702
10703 memset(&rr, 0, sizeof(rr));
10704 if (copy_from_user(&rr, arg, size))
10705 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010706 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010707 return -EINVAL;
10708
Pavel Begunkov992da012021-06-10 16:37:37 +010010709 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010710 case IORING_RSRC_FILE:
10711 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10712 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010713 case IORING_RSRC_BUFFER:
10714 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10715 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010716 }
10717 return -EINVAL;
10718}
10719
Pavel Begunkovc0724812021-10-04 20:02:54 +010010720static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
10721 void __user *arg, unsigned len)
Jens Axboefe764212021-06-17 10:19:54 -060010722{
10723 struct io_uring_task *tctx = current->io_uring;
10724 cpumask_var_t new_mask;
10725 int ret;
10726
10727 if (!tctx || !tctx->io_wq)
10728 return -EINVAL;
10729
10730 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10731 return -ENOMEM;
10732
10733 cpumask_clear(new_mask);
10734 if (len > cpumask_size())
10735 len = cpumask_size();
10736
10737 if (copy_from_user(new_mask, arg, len)) {
10738 free_cpumask_var(new_mask);
10739 return -EFAULT;
10740 }
10741
10742 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10743 free_cpumask_var(new_mask);
10744 return ret;
10745}
10746
Pavel Begunkovc0724812021-10-04 20:02:54 +010010747static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
Jens Axboefe764212021-06-17 10:19:54 -060010748{
10749 struct io_uring_task *tctx = current->io_uring;
10750
10751 if (!tctx || !tctx->io_wq)
10752 return -EINVAL;
10753
10754 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10755}
10756
Pavel Begunkovc0724812021-10-04 20:02:54 +010010757static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10758 void __user *arg)
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010759 __must_hold(&ctx->uring_lock)
Jens Axboe2e480052021-08-27 11:33:19 -060010760{
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010761 struct io_tctx_node *node;
Jens Axboefa846932021-09-01 14:15:59 -060010762 struct io_uring_task *tctx = NULL;
10763 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010764 __u32 new_count[2];
10765 int i, ret;
10766
Jens Axboe2e480052021-08-27 11:33:19 -060010767 if (copy_from_user(new_count, arg, sizeof(new_count)))
10768 return -EFAULT;
10769 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10770 if (new_count[i] > INT_MAX)
10771 return -EINVAL;
10772
Jens Axboefa846932021-09-01 14:15:59 -060010773 if (ctx->flags & IORING_SETUP_SQPOLL) {
10774 sqd = ctx->sq_data;
10775 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010776 /*
10777 * Observe the correct sqd->lock -> ctx->uring_lock
10778 * ordering. Fine to drop uring_lock here, we hold
10779 * a ref to the ctx.
10780 */
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010781 refcount_inc(&sqd->refs);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010782 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010783 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010784 mutex_lock(&ctx->uring_lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010785 if (sqd->thread)
10786 tctx = sqd->thread->io_uring;
Jens Axboefa846932021-09-01 14:15:59 -060010787 }
10788 } else {
10789 tctx = current->io_uring;
10790 }
10791
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010792 BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits));
Jens Axboefa846932021-09-01 14:15:59 -060010793
Pavel Begunkovbad119b2021-11-08 15:10:03 +000010794 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10795 if (new_count[i])
10796 ctx->iowq_limits[i] = new_count[i];
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010797 ctx->iowq_limits_set = true;
10798
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010799 if (tctx && tctx->io_wq) {
10800 ret = io_wq_max_workers(tctx->io_wq, new_count);
10801 if (ret)
10802 goto err;
10803 } else {
10804 memset(new_count, 0, sizeof(new_count));
10805 }
Jens Axboefa846932021-09-01 14:15:59 -060010806
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010807 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010808 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010809 io_put_sq_data(sqd);
10810 }
Jens Axboe2e480052021-08-27 11:33:19 -060010811
10812 if (copy_to_user(arg, new_count, sizeof(new_count)))
10813 return -EFAULT;
10814
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010815 /* that's it for SQPOLL, only the SQPOLL task creates requests */
10816 if (sqd)
10817 return 0;
10818
10819 /* now propagate the restriction to all registered users */
10820 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
10821 struct io_uring_task *tctx = node->task->io_uring;
10822
10823 if (WARN_ON_ONCE(!tctx->io_wq))
10824 continue;
10825
10826 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10827 new_count[i] = ctx->iowq_limits[i];
10828 /* ignore errors, it always returns zero anyway */
10829 (void)io_wq_max_workers(tctx->io_wq, new_count);
10830 }
Jens Axboe2e480052021-08-27 11:33:19 -060010831 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010832err:
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010833 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010834 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010835 io_put_sq_data(sqd);
10836 }
Jens Axboefa846932021-09-01 14:15:59 -060010837 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010838}
10839
Jens Axboe071698e2020-01-28 10:04:42 -070010840static bool io_register_op_must_quiesce(int op)
10841{
10842 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010843 case IORING_REGISTER_BUFFERS:
10844 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010845 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010846 case IORING_UNREGISTER_FILES:
10847 case IORING_REGISTER_FILES_UPDATE:
10848 case IORING_REGISTER_PROBE:
10849 case IORING_REGISTER_PERSONALITY:
10850 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010851 case IORING_REGISTER_FILES2:
10852 case IORING_REGISTER_FILES_UPDATE2:
10853 case IORING_REGISTER_BUFFERS2:
10854 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010855 case IORING_REGISTER_IOWQ_AFF:
10856 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010857 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010858 return false;
10859 default:
10860 return true;
10861 }
10862}
10863
Pavel Begunkovc0724812021-10-04 20:02:54 +010010864static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx)
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010865{
10866 long ret;
10867
10868 percpu_ref_kill(&ctx->refs);
10869
10870 /*
10871 * Drop uring mutex before waiting for references to exit. If another
10872 * thread is currently inside io_uring_enter() it might need to grab the
10873 * uring_lock to make progress. If we hold it here across the drain
10874 * wait, then we can deadlock. It's safe to drop the mutex here, since
10875 * no new references will come in after we've killed the percpu ref.
10876 */
10877 mutex_unlock(&ctx->uring_lock);
10878 do {
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010879 ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ);
10880 if (ret) {
10881 ret = min(0L, ret);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010882 break;
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010883 }
10884
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010885 ret = io_run_task_work_sig();
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010886 io_req_caches_free(ctx);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010887 } while (ret >= 0);
10888 mutex_lock(&ctx->uring_lock);
10889
10890 if (ret)
10891 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10892 return ret;
10893}
10894
Jens Axboeedafcce2019-01-09 09:16:05 -070010895static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10896 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010897 __releases(ctx->uring_lock)
10898 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010899{
10900 int ret;
10901
Jens Axboe35fa71a2019-04-22 10:23:23 -060010902 /*
10903 * We're inside the ring mutex, if the ref is already dying, then
10904 * someone else killed the ctx or is already going through
10905 * io_uring_register().
10906 */
10907 if (percpu_ref_is_dying(&ctx->refs))
10908 return -ENXIO;
10909
Pavel Begunkov75c40212021-04-15 13:07:40 +010010910 if (ctx->restricted) {
10911 if (opcode >= IORING_REGISTER_LAST)
10912 return -EINVAL;
10913 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10914 if (!test_bit(opcode, ctx->restrictions.register_op))
10915 return -EACCES;
10916 }
10917
Jens Axboe071698e2020-01-28 10:04:42 -070010918 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010919 ret = io_ctx_quiesce(ctx);
10920 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010921 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010922 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010923
10924 switch (opcode) {
10925 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010926 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010927 break;
10928 case IORING_UNREGISTER_BUFFERS:
10929 ret = -EINVAL;
10930 if (arg || nr_args)
10931 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010932 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010933 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010934 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010935 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010936 break;
10937 case IORING_UNREGISTER_FILES:
10938 ret = -EINVAL;
10939 if (arg || nr_args)
10940 break;
10941 ret = io_sqe_files_unregister(ctx);
10942 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010943 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010944 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010945 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010946 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010947 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010948 ret = -EINVAL;
10949 if (nr_args != 1)
10950 break;
10951 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010952 if (ret)
10953 break;
10954 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10955 ctx->eventfd_async = 1;
10956 else
10957 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010958 break;
10959 case IORING_UNREGISTER_EVENTFD:
10960 ret = -EINVAL;
10961 if (arg || nr_args)
10962 break;
10963 ret = io_eventfd_unregister(ctx);
10964 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010965 case IORING_REGISTER_PROBE:
10966 ret = -EINVAL;
10967 if (!arg || nr_args > 256)
10968 break;
10969 ret = io_probe(ctx, arg, nr_args);
10970 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010971 case IORING_REGISTER_PERSONALITY:
10972 ret = -EINVAL;
10973 if (arg || nr_args)
10974 break;
10975 ret = io_register_personality(ctx);
10976 break;
10977 case IORING_UNREGISTER_PERSONALITY:
10978 ret = -EINVAL;
10979 if (arg)
10980 break;
10981 ret = io_unregister_personality(ctx, nr_args);
10982 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010983 case IORING_REGISTER_ENABLE_RINGS:
10984 ret = -EINVAL;
10985 if (arg || nr_args)
10986 break;
10987 ret = io_register_enable_rings(ctx);
10988 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010989 case IORING_REGISTER_RESTRICTIONS:
10990 ret = io_register_restrictions(ctx, arg, nr_args);
10991 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010992 case IORING_REGISTER_FILES2:
10993 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010994 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010995 case IORING_REGISTER_FILES_UPDATE2:
10996 ret = io_register_rsrc_update(ctx, arg, nr_args,
10997 IORING_RSRC_FILE);
10998 break;
10999 case IORING_REGISTER_BUFFERS2:
11000 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
11001 break;
11002 case IORING_REGISTER_BUFFERS_UPDATE:
11003 ret = io_register_rsrc_update(ctx, arg, nr_args,
11004 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010011005 break;
Jens Axboefe764212021-06-17 10:19:54 -060011006 case IORING_REGISTER_IOWQ_AFF:
11007 ret = -EINVAL;
11008 if (!arg || !nr_args)
11009 break;
11010 ret = io_register_iowq_aff(ctx, arg, nr_args);
11011 break;
11012 case IORING_UNREGISTER_IOWQ_AFF:
11013 ret = -EINVAL;
11014 if (arg || nr_args)
11015 break;
11016 ret = io_unregister_iowq_aff(ctx);
11017 break;
Jens Axboe2e480052021-08-27 11:33:19 -060011018 case IORING_REGISTER_IOWQ_MAX_WORKERS:
11019 ret = -EINVAL;
11020 if (!arg || nr_args != 2)
11021 break;
11022 ret = io_register_iowq_max_workers(ctx, arg);
11023 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070011024 default:
11025 ret = -EINVAL;
11026 break;
11027 }
11028
Jens Axboe071698e2020-01-28 10:04:42 -070011029 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070011030 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070011031 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060011032 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070011033 }
Jens Axboeedafcce2019-01-09 09:16:05 -070011034 return ret;
11035}
11036
11037SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
11038 void __user *, arg, unsigned int, nr_args)
11039{
11040 struct io_ring_ctx *ctx;
11041 long ret = -EBADF;
11042 struct fd f;
11043
11044 f = fdget(fd);
11045 if (!f.file)
11046 return -EBADF;
11047
11048 ret = -EOPNOTSUPP;
11049 if (f.file->f_op != &io_uring_fops)
11050 goto out_fput;
11051
11052 ctx = f.file->private_data;
11053
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000011054 io_run_task_work();
11055
Jens Axboeedafcce2019-01-09 09:16:05 -070011056 mutex_lock(&ctx->uring_lock);
11057 ret = __io_uring_register(ctx, opcode, arg, nr_args);
11058 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020011059 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
11060 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070011061out_fput:
11062 fdput(f);
11063 return ret;
11064}
11065
Jens Axboe2b188cc2019-01-07 10:46:33 -070011066static int __init io_uring_init(void)
11067{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011068#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
11069 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
11070 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
11071} while (0)
11072
11073#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
11074 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
11075 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
11076 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
11077 BUILD_BUG_SQE_ELEM(1, __u8, flags);
11078 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
11079 BUILD_BUG_SQE_ELEM(4, __s32, fd);
11080 BUILD_BUG_SQE_ELEM(8, __u64, off);
11081 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
11082 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011083 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011084 BUILD_BUG_SQE_ELEM(24, __u32, len);
11085 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
11086 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
11087 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
11088 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080011089 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
11090 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011091 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
11092 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
11093 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
11094 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
11095 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
11096 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
11097 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
11098 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011099 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011100 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
11101 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011102 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011103 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011104 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010011105 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011106
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011107 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
11108 sizeof(struct io_uring_rsrc_update));
11109 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
11110 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010011111
11112 /* ->buf_index is u16 */
11113 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
11114
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011115 /* should fit into one byte */
11116 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
Pavel Begunkov68fe2562021-09-15 12:03:38 +010011117 BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
11118 BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011119
Jens Axboed3656342019-12-18 09:50:26 -070011120 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080011121 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011122
Jens Axboe91f245d2021-02-09 13:48:50 -070011123 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
11124 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070011125 return 0;
11126};
11127__initcall(io_uring_init);