blob: 8cabe4a0d38f49f139bcfcf9d57e28e961fcb328 [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 | \
Pavel Begunkov5562a8d2021-11-10 15:49:34 +0000109 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Pavel Begunkov68fe2562021-09-15 12:03:38 +0100110
Pavel Begunkov5562a8d2021-11-10 15:49:34 +0000111#define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \
112 IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS)
Pavel Begunkov68fe2562021-09-15 12:03:38 +0100113
Pavel Begunkovc8543572021-06-17 18:14:04 +0100114#define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
Pavel Begunkovd886e182021-10-04 20:02:56 +0100115 REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \
116 REQ_F_ASYNC_DATA)
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000117
Pavel Begunkov09899b12021-06-14 02:36:22 +0100118#define IO_TCTX_REFS_CACHE_NR (1U << 10)
119
Jens Axboe2b188cc2019-01-07 10:46:33 -0700120struct io_uring {
121 u32 head ____cacheline_aligned_in_smp;
122 u32 tail ____cacheline_aligned_in_smp;
123};
124
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 * This data is shared with the application through the mmap at offsets
127 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 *
129 * The offsets to the member fields are published through struct
130 * io_sqring_offsets when calling io_uring_setup.
131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200133 /*
134 * Head and tail offsets into the ring; the offsets need to be
135 * masked to get valid indices.
136 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 * The kernel controls head of the sq ring and the tail of the cq ring,
138 * and the application controls tail of the sq ring and the head of the
139 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200140 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000141 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200142 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 * ring_entries - 1)
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_ring_mask, cq_ring_mask;
147 /* Ring sizes (constant, power of 2) */
148 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 /*
150 * Number of invalid entries dropped by the kernel due to
151 * invalid index stored in array
152 *
153 * Written by the kernel, shouldn't be modified by the
154 * application (i.e. get number of "new events" by comparing to
155 * cached value).
156 *
157 * After a new SQ head value was read by the application this
158 * counter includes all submissions that were dropped reaching
159 * the new SQ head (and possibly more).
160 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000161 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200163 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200164 *
165 * Written by the kernel, shouldn't be modified by the
166 * application.
167 *
168 * The application needs a full memory barrier before checking
169 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
170 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000171 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200172 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200173 * Runtime CQ flags
174 *
175 * Written by the application, shouldn't be modified by the
176 * kernel.
177 */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100178 u32 cq_flags;
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200179 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200180 * Number of completion events lost because the queue was full;
181 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800182 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200183 * the completion queue.
184 *
185 * Written by the kernel, shouldn't be modified by the
186 * application (i.e. get number of "new events" by comparing to
187 * cached value).
188 *
189 * As completion events come in out of order this counter is not
190 * ordered with any other data.
191 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000192 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200193 /*
194 * Ring buffer of completion events.
195 *
196 * The kernel writes completion events fresh every time they are
197 * produced, so the application is allowed to modify pending
198 * entries.
199 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000200 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700201};
202
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000203enum io_uring_cmd_flags {
Pavel Begunkov51aac422021-10-14 16:10:17 +0100204 IO_URING_F_COMPLETE_DEFER = 1,
Hao Xu3b44b372021-10-18 21:34:31 +0800205 IO_URING_F_UNLOCKED = 2,
Pavel Begunkov51aac422021-10-14 16:10:17 +0100206 /* int's last bit, sign checks are usually faster than a bit test */
207 IO_URING_F_NONBLOCK = INT_MIN,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000208};
209
Jens Axboeedafcce2019-01-09 09:16:05 -0700210struct io_mapped_ubuf {
211 u64 ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +0100212 u64 ubuf_end;
Jens Axboeedafcce2019-01-09 09:16:05 -0700213 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600214 unsigned long acct_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100215 struct bio_vec bvec[];
Jens Axboeedafcce2019-01-09 09:16:05 -0700216};
217
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000218struct io_ring_ctx;
219
Pavel Begunkov6c2450a2021-02-23 12:40:22 +0000220struct io_overflow_cqe {
221 struct io_uring_cqe cqe;
222 struct list_head list;
223};
224
Pavel Begunkova04b0ac2021-04-01 15:44:04 +0100225struct io_fixed_file {
226 /* file * with additional FFS_* flags */
227 unsigned long file_ptr;
228};
229
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000230struct io_rsrc_put {
231 struct list_head list;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100232 u64 tag;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000233 union {
234 void *rsrc;
235 struct file *file;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +0100236 struct io_mapped_ubuf *buf;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000237 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000238};
239
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100240struct io_file_table {
Pavel Begunkov042b0d82021-08-09 13:04:01 +0100241 struct io_fixed_file *files;
Jens Axboe31b51512019-01-18 22:56:34 -0700242};
243
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100244struct io_rsrc_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800245 struct percpu_ref refs;
246 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000247 struct list_head rsrc_list;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100248 struct io_rsrc_data *rsrc_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600249 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000250 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800251};
252
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100253typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
254
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100255struct io_rsrc_data {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700256 struct io_ring_ctx *ctx;
257
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100258 u64 **tags;
259 unsigned int nr;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100260 rsrc_put_fn *do_put;
Pavel Begunkov3e942492021-04-11 01:46:34 +0100261 atomic_t refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700262 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800263 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700264};
265
Jens Axboe5a2e7452020-02-23 16:23:11 -0700266struct io_buffer {
267 struct list_head list;
268 __u64 addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -0300269 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700270 __u16 bid;
271};
272
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200273struct io_restriction {
274 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
275 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
276 u8 sqe_flags_allowed;
277 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200278 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200279};
280
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700281enum {
282 IO_SQ_THREAD_SHOULD_STOP = 0,
283 IO_SQ_THREAD_SHOULD_PARK,
284};
285
Jens Axboe534ca6d2020-09-02 13:52:19 -0600286struct io_sq_data {
287 refcount_t refs;
Pavel Begunkov9e138a42021-03-14 20:57:12 +0000288 atomic_t park_pending;
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +0000289 struct mutex lock;
Jens Axboe69fb2132020-09-14 11:16:23 -0600290
291 /* ctx's that are using this sqd */
292 struct list_head ctx_list;
Jens Axboe69fb2132020-09-14 11:16:23 -0600293
Jens Axboe534ca6d2020-09-02 13:52:19 -0600294 struct task_struct *thread;
295 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800296
297 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700298 int sq_cpu;
299 pid_t task_pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -0700300 pid_t task_tgid;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700301
302 unsigned long state;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700303 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600304};
305
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000306#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000307#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000308#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000309
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000310struct io_submit_link {
311 struct io_kiocb *head;
312 struct io_kiocb *last;
313};
314
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000315struct io_submit_state {
Pavel Begunkov5a158c62021-10-06 16:06:48 +0100316 /* inline/task_work completion list, under ->uring_lock */
317 struct io_wq_work_node free_list;
318 /* batch completion logic */
319 struct io_wq_work_list compl_reqs;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000320 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000321
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000322 bool plug_started;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +0100323 bool need_plug;
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +0000324 bool flush_cqes;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -0600325 unsigned short submit_nr;
Pavel Begunkov5a158c62021-10-06 16:06:48 +0100326 struct blk_plug plug;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000327};
328
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329struct io_ring_ctx {
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100330 /* const or read-mostly hot data */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331 struct {
332 struct percpu_ref refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700333
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100334 struct io_rings *rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800336 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800337 unsigned int drain_next: 1;
338 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200339 unsigned int restricted: 1;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +0100340 unsigned int off_timeout_used: 1;
Pavel Begunkov10c66902021-06-15 16:47:56 +0100341 unsigned int drain_active: 1;
Pavel Begunkov5562a8d2021-11-10 15:49:34 +0000342 unsigned int drain_disabled: 1;
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100343 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100345 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100346 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100347 struct mutex uring_lock;
348
Hristo Venev75b28af2019-08-26 17:23:46 +0000349 /*
350 * Ring buffer of indices into array of io_uring_sqe, which is
351 * mmapped by the application using the IORING_OFF_SQES offset.
352 *
353 * This indirection could e.g. be used to assign fixed
354 * io_uring_sqe entries to operations and only submit them to
355 * the queue when needed.
356 *
357 * The kernel modifies neither the indices array nor the entries
358 * array.
359 */
360 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100361 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700362 unsigned cached_sq_head;
363 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600364 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100365
366 /*
367 * Fixed resources fast path, should be accessed only under
368 * uring_lock, and updated through io_uring_register(2)
369 */
370 struct io_rsrc_node *rsrc_node;
Pavel Begunkovab409402021-10-09 23:14:41 +0100371 int rsrc_cached_refs;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100372 struct io_file_table file_table;
373 unsigned nr_user_files;
374 unsigned nr_user_bufs;
375 struct io_mapped_ubuf **user_bufs;
376
377 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600378 struct list_head timeout_list;
Pavel Begunkovef9dd632021-08-28 19:54:38 -0600379 struct list_head ltimeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700380 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100381 struct xarray io_buffers;
382 struct xarray personalities;
383 u32 pers_next;
384 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700385 } ____cacheline_aligned_in_smp;
386
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100387 /* IRQ completion list, under ->completion_lock */
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +0100388 struct io_wq_work_list locked_free_list;
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100389 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700390
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100391 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600392 struct io_sq_data *sq_data; /* if using sq thread polling */
393
Jens Axboe90554202020-09-03 12:12:41 -0600394 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600395 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000396
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100397 unsigned long check_cq_overflow;
398
Jens Axboe206aefd2019-11-07 18:27:42 -0700399 struct {
400 unsigned cached_cq_tail;
401 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700402 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100403 struct wait_queue_head cq_wait;
404 unsigned cq_extra;
405 atomic_t cq_timeouts;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100406 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700407 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700408
409 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700411
Jens Axboe89850fc2021-08-10 15:11:51 -0600412 spinlock_t timeout_lock;
413
Jens Axboedef596e2019-01-09 08:59:42 -0700414 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300415 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700416 * io_uring instances that don't use IORING_SETUP_SQPOLL.
417 * For SQPOLL, only the single threaded io_sq_thread() will
418 * manipulate the list, hence no extra locking is needed there.
419 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +0100420 struct io_wq_work_list iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700421 struct hlist_head *cancel_hash;
422 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800423 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700424 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600425
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200426 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700427
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100428 /* slow path rsrc auxilary data, used by update/register */
429 struct {
430 struct io_rsrc_node *rsrc_backup_node;
431 struct io_mapped_ubuf *dummy_ubuf;
432 struct io_rsrc_data *file_data;
433 struct io_rsrc_data *buf_data;
434
435 struct delayed_work rsrc_put_work;
436 struct llist_head rsrc_put_llist;
437 struct list_head rsrc_ref_list;
438 spinlock_t rsrc_ref_lock;
439 };
440
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700441 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100442 struct {
443 #if defined(CONFIG_UNIX)
444 struct socket *ring_sock;
445 #endif
446 /* hashed buffered write serialization */
447 struct io_wq_hash *hash_map;
448
449 /* Only used for accounting purposes */
450 struct user_struct *user;
451 struct mm_struct *mm_account;
452
453 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100454 struct llist_head fallback_llist;
455 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100456 struct work_struct exit_work;
457 struct list_head tctx_list;
458 struct completion ref_comp;
Pavel Begunkove139a1e2021-10-19 23:43:46 +0100459 u32 iowq_limits[2];
460 bool iowq_limits_set;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100461 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700462};
463
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100464struct io_uring_task {
465 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100466 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100467 struct xarray xa;
468 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100469 const struct io_ring_ctx *last;
470 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100471 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100472 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100473 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100474
475 spinlock_t task_lock;
476 struct io_wq_work_list task_list;
Hao Xu4813c372021-12-07 17:39:48 +0800477 struct io_wq_work_list prior_task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100478 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100479 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100480};
481
Jens Axboe09bb8392019-03-13 12:39:28 -0600482/*
483 * First field must be the file pointer in all the
484 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
485 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700486struct io_poll_iocb {
487 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000488 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700489 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600490 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700491 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700492 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700493};
494
Pavel Begunkov9d805892021-04-13 02:58:40 +0100495struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000496 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100497 u64 old_user_data;
498 u64 new_user_data;
499 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600500 bool update_events;
501 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000502};
503
Jens Axboeb5dba592019-12-11 14:02:38 -0700504struct io_close {
505 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700506 int fd;
Pavel Begunkov7df778b2021-09-24 20:04:29 +0100507 u32 file_slot;
Jens Axboeb5dba592019-12-11 14:02:38 -0700508};
509
Jens Axboead8a48a2019-11-15 08:49:11 -0700510struct io_timeout_data {
511 struct io_kiocb *req;
512 struct hrtimer timer;
513 struct timespec64 ts;
514 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600515 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700516};
517
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700518struct io_accept {
519 struct file *file;
520 struct sockaddr __user *addr;
521 int __user *addr_len;
522 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100523 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600524 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700525};
526
527struct io_sync {
528 struct file *file;
529 loff_t len;
530 loff_t off;
531 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700532 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700533};
534
Jens Axboefbf23842019-12-17 18:45:56 -0700535struct io_cancel {
536 struct file *file;
537 u64 addr;
538};
539
Jens Axboeb29472e2019-12-17 18:50:29 -0700540struct io_timeout {
541 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300542 u32 off;
543 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300544 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000545 /* head of the link, used by linked timeouts only */
546 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600547 /* for linked completions */
548 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700549};
550
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100551struct io_timeout_rem {
552 struct file *file;
553 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000554
555 /* timeout update */
556 struct timespec64 ts;
557 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600558 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100559};
560
Jens Axboe9adbd452019-12-20 08:45:55 -0700561struct io_rw {
562 /* NOTE: kiocb has the file as the first member, so don't do it here */
563 struct kiocb kiocb;
564 u64 addr;
565 u64 len;
566};
567
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700568struct io_connect {
569 struct file *file;
570 struct sockaddr __user *addr;
571 int addr_len;
572};
573
Jens Axboee47293f2019-12-20 08:58:21 -0700574struct io_sr_msg {
575 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700576 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100577 struct compat_msghdr __user *umsg_compat;
578 struct user_msghdr __user *umsg;
579 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700580 };
Jens Axboee47293f2019-12-20 08:58:21 -0700581 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700582 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700583 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700584};
585
Jens Axboe15b71ab2019-12-11 11:20:36 -0700586struct io_open {
587 struct file *file;
588 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100589 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700590 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700591 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600592 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700593};
594
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000595struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700596 struct file *file;
597 u64 arg;
598 u32 nr_args;
599 u32 offset;
600};
601
Jens Axboe4840e412019-12-25 22:03:45 -0700602struct io_fadvise {
603 struct file *file;
604 u64 offset;
605 u32 len;
606 u32 advice;
607};
608
Jens Axboec1ca7572019-12-25 22:18:28 -0700609struct io_madvise {
610 struct file *file;
611 u64 addr;
612 u32 len;
613 u32 advice;
614};
615
Jens Axboe3e4827b2020-01-08 15:18:09 -0700616struct io_epoll {
617 struct file *file;
618 int epfd;
619 int op;
620 int fd;
621 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700622};
623
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300624struct io_splice {
625 struct file *file_out;
626 struct file *file_in;
627 loff_t off_out;
628 loff_t off_in;
629 u64 len;
630 unsigned int flags;
631};
632
Jens Axboeddf0322d2020-02-23 16:41:33 -0700633struct io_provide_buf {
634 struct file *file;
635 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100636 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700637 __u32 bgid;
638 __u16 nbufs;
639 __u16 bid;
640};
641
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700642struct io_statx {
643 struct file *file;
644 int dfd;
645 unsigned int mask;
646 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700647 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700648 struct statx __user *buffer;
649};
650
Jens Axboe36f4fa62020-09-05 11:14:22 -0600651struct io_shutdown {
652 struct file *file;
653 int how;
654};
655
Jens Axboe80a261f2020-09-28 14:23:58 -0600656struct io_rename {
657 struct file *file;
658 int old_dfd;
659 int new_dfd;
660 struct filename *oldpath;
661 struct filename *newpath;
662 int flags;
663};
664
Jens Axboe14a11432020-09-28 14:27:37 -0600665struct io_unlink {
666 struct file *file;
667 int dfd;
668 int flags;
669 struct filename *filename;
670};
671
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700672struct io_mkdir {
673 struct file *file;
674 int dfd;
675 umode_t mode;
676 struct filename *filename;
677};
678
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700679struct io_symlink {
680 struct file *file;
681 int new_dfd;
682 struct filename *oldpath;
683 struct filename *newpath;
684};
685
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700686struct io_hardlink {
687 struct file *file;
688 int old_dfd;
689 int new_dfd;
690 struct filename *oldpath;
691 struct filename *newpath;
692 int flags;
693};
694
Jens Axboef499a022019-12-02 16:28:46 -0700695struct io_async_connect {
696 struct sockaddr_storage address;
697};
698
Jens Axboe03b12302019-12-02 18:50:25 -0700699struct io_async_msghdr {
700 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000701 /* points to an allocated iov, if NULL we use fast_iov instead */
702 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700703 struct sockaddr __user *uaddr;
704 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700705 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700706};
707
Pavel Begunkov538941e2021-10-14 16:10:15 +0100708struct io_rw_state {
Jens Axboeff6165b2020-08-13 09:47:43 -0600709 struct iov_iter iter;
Jens Axboecd658692021-09-10 11:19:14 -0600710 struct iov_iter_state iter_state;
Pavel Begunkovc88598a2021-10-14 16:10:16 +0100711 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov538941e2021-10-14 16:10:15 +0100712};
713
714struct io_async_rw {
715 struct io_rw_state s;
716 const struct iovec *free_iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -0600717 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600718 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700719};
720
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300721enum {
722 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
723 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
724 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
725 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
726 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700727 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000728 REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300729
Pavel Begunkovdddca222021-04-27 16:13:52 +0100730 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100731 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300732 REQ_F_INFLIGHT_BIT,
733 REQ_F_CUR_POS_BIT,
734 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300735 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300736 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700737 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700738 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000739 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600740 REQ_F_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100741 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100742 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100743 REQ_F_ARM_LTIMEOUT_BIT,
Pavel Begunkovd886e182021-10-04 20:02:56 +0100744 REQ_F_ASYNC_DATA_BIT,
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000745 REQ_F_SKIP_LINK_CQES_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700746 /* keep async read/write and isreg together and in order */
Pavel Begunkov35645ac2021-10-17 00:07:09 +0100747 REQ_F_SUPPORT_NOWAIT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700748 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700749
750 /* not a real bit, just to check we're not overflowing the space */
751 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300752};
753
754enum {
755 /* ctx owns file */
756 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
757 /* drain existing IO first */
758 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
759 /* linked sqes */
760 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
761 /* doesn't sever on completion < 0 */
762 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
763 /* IOSQE_ASYNC */
764 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700765 /* IOSQE_BUFFER_SELECT */
766 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000767 /* IOSQE_CQE_SKIP_SUCCESS */
768 REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300769
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300770 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100771 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000772 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300773 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
774 /* read/write uses file position */
775 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
776 /* must not punt to workers */
777 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100778 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300779 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300780 /* needs cleanup */
781 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700782 /* already went through poll handler */
783 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700784 /* buffer already selected */
785 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000786 /* completion is deferred through io_comp_state */
787 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600788 /* caller should reissue async */
789 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov35645ac2021-10-17 00:07:09 +0100790 /* supports async reads/writes */
791 REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700792 /* regular file */
793 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100794 /* has creds assigned */
795 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100796 /* skip refcounting if not set */
797 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100798 /* there is a linked timeout that has to be armed */
799 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Pavel Begunkovd886e182021-10-04 20:02:56 +0100800 /* ->async_data allocated */
801 REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT),
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000802 /* don't post CQEs while failing linked requests */
803 REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700804};
805
806struct async_poll {
807 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600808 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300809};
810
Pavel Begunkovf237c302021-08-18 12:42:46 +0100811typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100812
Jens Axboe7cbf1722021-02-10 00:03:20 +0000813struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100814 union {
815 struct io_wq_work_node node;
816 struct llist_node fallback_node;
817 };
818 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000819};
820
Pavel Begunkov992da012021-06-10 16:37:37 +0100821enum {
822 IORING_RSRC_FILE = 0,
823 IORING_RSRC_BUFFER = 1,
824};
825
Jens Axboe09bb8392019-03-13 12:39:28 -0600826/*
827 * NOTE! Each of the iocb union members has the file pointer
828 * as the first entry in their struct definition. So you can
829 * access the file pointer through any of the sub-structs,
830 * or directly as just 'ki_filp' in this struct.
831 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700832struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700833 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600834 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700835 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700836 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100837 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700838 struct io_accept accept;
839 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700840 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700841 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100842 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700843 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700844 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700845 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700846 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000847 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700848 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700849 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700850 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300851 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700852 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700853 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600854 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600855 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600856 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700857 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700858 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700859 struct io_hardlink hardlink;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700860 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861
Jens Axboed625c6e2019-12-17 19:53:05 -0700862 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800863 /* polled IO has completed */
864 u8 iopoll_completed;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700865 u16 buf_index;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100866 unsigned int flags;
867
868 u64 user_data;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300869 u32 result;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100870 u32 cflags;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700871
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300872 struct io_ring_ctx *ctx;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300873 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700874
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000875 struct percpu_ref *fixed_rsrc_refs;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100876 /* store used ubuf, so we can prevent reloading */
877 struct io_mapped_ubuf *imu;
Jens Axboed7718a92020-02-14 22:23:12 -0700878
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100879 /* used by request caches, completion batching and iopoll */
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100880 struct io_wq_work_node comp_list;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100881 atomic_t refs;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100882 struct io_kiocb *link;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100883 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300884 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
885 struct hlist_node hash_node;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100886 /* internal polling, see IORING_FEAT_FAST_POLL */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300887 struct async_poll *apoll;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100888 /* opcode allocated if it needs to store data for async defer */
889 void *async_data;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300890 struct io_wq_work work;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100891 /* custom credentials, valid IFF REQ_F_CREDS is set */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100892 const struct cred *creds;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100893 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
Pavel Begunkov30d51dd2021-10-01 18:07:03 +0100894 struct io_buffer *kbuf;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700895};
896
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000897struct io_tctx_node {
898 struct list_head ctx_node;
899 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000900 struct io_ring_ctx *ctx;
901};
902
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300903struct io_defer_entry {
904 struct list_head list;
905 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300906 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300907};
908
Jens Axboed3656342019-12-18 09:50:26 -0700909struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700910 /* needs req->file assigned */
911 unsigned needs_file : 1;
Pavel Begunkov6d634162021-10-06 16:06:46 +0100912 /* should block plug */
913 unsigned plug : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700914 /* hash wq insertion if file is a regular file */
915 unsigned hash_reg_file : 1;
916 /* unbound wq insertion if file is a non-regular file */
917 unsigned unbound_nonreg_file : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700918 /* set if opcode supports polled "wait" */
919 unsigned pollin : 1;
920 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700921 /* op supports buffer selection */
922 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000923 /* do prep async if is going to be punted */
924 unsigned needs_async_setup : 1;
Pavel Begunkov6d634162021-10-06 16:06:46 +0100925 /* opcode is not supported by this kernel */
926 unsigned not_supported : 1;
Paul Moore5bd21822021-02-16 19:46:48 -0500927 /* skip auditing */
928 unsigned audit_skip : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700929 /* size of async data needed, if any */
930 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700931};
932
Jens Axboe09186822020-10-13 15:01:40 -0600933static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300934 [IORING_OP_NOP] = {},
935 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700936 .needs_file = 1,
937 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700938 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700939 .buffer_select = 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_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700946 .needs_file = 1,
947 .hash_reg_file = 1,
948 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700949 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000950 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600951 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500952 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700953 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700954 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300955 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700956 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500957 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700958 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300959 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700960 .needs_file = 1,
961 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700962 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600963 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500964 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700965 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700966 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300967 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700968 .needs_file = 1,
969 .hash_reg_file = 1,
970 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700971 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600972 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500973 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700974 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700975 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300976 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700977 .needs_file = 1,
978 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500979 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700980 },
Paul Moore5bd21822021-02-16 19:46:48 -0500981 [IORING_OP_POLL_REMOVE] = {
982 .audit_skip = 1,
983 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300984 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700985 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500986 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700987 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300988 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700989 .needs_file = 1,
990 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700991 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000992 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700993 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700994 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300995 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700996 .needs_file = 1,
997 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700998 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700999 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +00001000 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001001 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -07001002 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001003 [IORING_OP_TIMEOUT] = {
Paul Moore5bd21822021-02-16 19:46:48 -05001004 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001005 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -07001006 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00001007 [IORING_OP_TIMEOUT_REMOVE] = {
1008 /* used by timeout updates' prep() */
Paul Moore5bd21822021-02-16 19:46:48 -05001009 .audit_skip = 1,
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00001010 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001011 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001012 .needs_file = 1,
1013 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001014 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -07001015 },
Paul Moore5bd21822021-02-16 19:46:48 -05001016 [IORING_OP_ASYNC_CANCEL] = {
1017 .audit_skip = 1,
1018 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001019 [IORING_OP_LINK_TIMEOUT] = {
Paul Moore5bd21822021-02-16 19:46:48 -05001020 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001021 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -07001022 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001023 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001024 .needs_file = 1,
1025 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001026 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +00001027 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001028 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -07001029 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001030 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -07001031 .needs_file = 1,
1032 },
Jens Axboe44526be2021-02-15 13:32:18 -07001033 [IORING_OP_OPENAT] = {},
1034 [IORING_OP_CLOSE] = {},
Paul Moore5bd21822021-02-16 19:46:48 -05001035 [IORING_OP_FILES_UPDATE] = {
1036 .audit_skip = 1,
1037 },
1038 [IORING_OP_STATX] = {
1039 .audit_skip = 1,
1040 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001041 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001042 .needs_file = 1,
1043 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001044 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001045 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001046 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001047 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001048 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001049 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001050 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001051 .needs_file = 1,
Jens Axboe7b3188e2021-08-30 19:37:41 -06001052 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -07001053 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001054 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001055 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001056 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001057 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001058 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001059 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001060 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001061 .audit_skip = 1,
Jens Axboe4840e412019-12-25 22:03:45 -07001062 },
Jens Axboe44526be2021-02-15 13:32:18 -07001063 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001064 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001065 .needs_file = 1,
1066 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001067 .pollout = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001068 .audit_skip = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001069 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001070 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001071 .needs_file = 1,
1072 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001073 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001074 .buffer_select = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001075 .audit_skip = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001076 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001077 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001078 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001079 [IORING_OP_EPOLL_CTL] = {
1080 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001081 .audit_skip = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001082 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001083 [IORING_OP_SPLICE] = {
1084 .needs_file = 1,
1085 .hash_reg_file = 1,
1086 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001087 .audit_skip = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001088 },
Paul Moore5bd21822021-02-16 19:46:48 -05001089 [IORING_OP_PROVIDE_BUFFERS] = {
1090 .audit_skip = 1,
1091 },
1092 [IORING_OP_REMOVE_BUFFERS] = {
1093 .audit_skip = 1,
1094 },
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001095 [IORING_OP_TEE] = {
1096 .needs_file = 1,
1097 .hash_reg_file = 1,
1098 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001099 .audit_skip = 1,
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001100 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001101 [IORING_OP_SHUTDOWN] = {
1102 .needs_file = 1,
1103 },
Jens Axboe44526be2021-02-15 13:32:18 -07001104 [IORING_OP_RENAMEAT] = {},
1105 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001106 [IORING_OP_MKDIRAT] = {},
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07001107 [IORING_OP_SYMLINKAT] = {},
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07001108 [IORING_OP_LINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001109};
1110
Pavel Begunkov0756a862021-08-15 10:40:25 +01001111/* requests with any of those set should undergo io_disarm_next() */
1112#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1113
Pavel Begunkov7a612352021-03-09 00:37:59 +00001114static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001115static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001116static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1117 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001118 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001119static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001120
Pavel Begunkov913a5712021-11-10 15:49:31 +00001121static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags);
1122
Jackie Liuec9c02a2019-11-08 23:50:36 +08001123static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001124static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001125static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001126static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001127static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001128 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001129 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001130static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001131static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001132 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001133static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001134static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001135
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001136static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkovc4501782021-09-08 16:40:52 +01001137static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001138static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001139
Pavel Begunkovb9445592021-08-25 12:25:45 +01001140static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1141 unsigned int issue_flags, u32 slot_index);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01001142static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags);
1143
Pavel Begunkovf1042b62021-08-28 19:54:39 -06001144static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
Pavel Begunkovb9445592021-08-25 12:25:45 +01001145
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146static struct kmem_cache *req_cachep;
1147
Jens Axboe09186822020-10-13 15:01:40 -06001148static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001149
1150struct sock *io_uring_get_socket(struct file *file)
1151{
1152#if defined(CONFIG_UNIX)
1153 if (file->f_op == &io_uring_fops) {
1154 struct io_ring_ctx *ctx = file->private_data;
1155
1156 return ctx->ring_sock->sk;
1157 }
1158#endif
1159 return NULL;
1160}
1161EXPORT_SYMBOL(io_uring_get_socket);
1162
Pavel Begunkovf237c302021-08-18 12:42:46 +01001163static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1164{
1165 if (!*locked) {
1166 mutex_lock(&ctx->uring_lock);
1167 *locked = true;
1168 }
1169}
1170
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001171#define io_for_each_link(pos, head) \
1172 for (pos = (head); pos; pos = pos->link)
1173
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001174/*
1175 * Shamelessly stolen from the mm implementation of page reference checking,
1176 * see commit f958d7b528b1 for details.
1177 */
1178#define req_ref_zero_or_close_to_overflow(req) \
1179 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1180
1181static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1182{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001183 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001184 return atomic_inc_not_zero(&req->refs);
1185}
1186
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001187static inline bool req_ref_put_and_test(struct io_kiocb *req)
1188{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001189 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1190 return true;
1191
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001192 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1193 return atomic_dec_and_test(&req->refs);
1194}
1195
1196static inline void req_ref_put(struct io_kiocb *req)
1197{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001198 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001199 WARN_ON_ONCE(req_ref_put_and_test(req));
1200}
1201
1202static inline void req_ref_get(struct io_kiocb *req)
1203{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001204 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001205 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1206 atomic_inc(&req->refs);
1207}
1208
Pavel Begunkovc4501782021-09-08 16:40:52 +01001209static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
1210{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001211 if (!wq_list_empty(&ctx->submit_state.compl_reqs))
Pavel Begunkovc4501782021-09-08 16:40:52 +01001212 __io_submit_flush_completions(ctx);
1213}
1214
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001215static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001216{
1217 if (!(req->flags & REQ_F_REFCOUNT)) {
1218 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001219 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001220 }
1221}
1222
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001223static inline void io_req_set_refcount(struct io_kiocb *req)
1224{
1225 __io_req_set_refcount(req, 1);
1226}
1227
Pavel Begunkovab409402021-10-09 23:14:41 +01001228#define IO_RSRC_REF_BATCH 100
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001229
Pavel Begunkovab409402021-10-09 23:14:41 +01001230static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
1231 struct io_ring_ctx *ctx)
1232 __must_hold(&ctx->uring_lock)
1233{
1234 struct percpu_ref *ref = req->fixed_rsrc_refs;
1235
1236 if (ref) {
1237 if (ref == &ctx->rsrc_node->refs)
1238 ctx->rsrc_cached_refs++;
1239 else
1240 percpu_ref_put(ref);
1241 }
1242}
1243
1244static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx)
1245{
1246 if (req->fixed_rsrc_refs)
1247 percpu_ref_put(req->fixed_rsrc_refs);
1248}
1249
1250static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
1251 __must_hold(&ctx->uring_lock)
1252{
1253 if (ctx->rsrc_cached_refs) {
1254 percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs);
1255 ctx->rsrc_cached_refs = 0;
1256 }
1257}
1258
1259static void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
1260 __must_hold(&ctx->uring_lock)
1261{
1262 ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
1263 percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH);
1264}
1265
Pavel Begunkova46be972021-10-09 23:14:40 +01001266static inline void io_req_set_rsrc_node(struct io_kiocb *req,
1267 struct io_ring_ctx *ctx)
Jens Axboec40f6372020-06-25 15:39:59 -06001268{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001269 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001270 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Pavel Begunkovab409402021-10-09 23:14:41 +01001271 ctx->rsrc_cached_refs--;
1272 if (unlikely(ctx->rsrc_cached_refs < 0))
1273 io_rsrc_refs_refill(ctx);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001274 }
1275}
1276
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00001277static unsigned int __io_put_kbuf(struct io_kiocb *req)
Hao Xu3648e522021-12-05 14:37:57 +00001278{
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00001279 struct io_buffer *kbuf = req->kbuf;
Hao Xu3648e522021-12-05 14:37:57 +00001280 unsigned int cflags;
1281
1282 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1283 cflags |= IORING_CQE_F_BUFFER;
1284 req->flags &= ~REQ_F_BUFFER_SELECTED;
1285 kfree(kbuf);
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00001286 req->kbuf = NULL;
Hao Xu3648e522021-12-05 14:37:57 +00001287 return cflags;
1288}
1289
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00001290static inline unsigned int io_put_kbuf(struct io_kiocb *req)
Hao Xu3648e522021-12-05 14:37:57 +00001291{
1292 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
1293 return 0;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00001294 return __io_put_kbuf(req);
Hao Xu3648e522021-12-05 14:37:57 +00001295}
1296
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001297static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1298{
1299 bool got = percpu_ref_tryget(ref);
1300
1301 /* already at zero, wait for ->release() */
1302 if (!got)
1303 wait_for_completion(compl);
1304 percpu_ref_resurrect(ref);
1305 if (got)
1306 percpu_ref_put(ref);
1307}
1308
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001309static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1310 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001311{
1312 struct io_kiocb *req;
1313
Pavel Begunkov68207682021-03-22 01:58:25 +00001314 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001315 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001316 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001317 return true;
1318
1319 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001320 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001321 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001322 }
1323 return false;
1324}
1325
Pavel Begunkovd886e182021-10-04 20:02:56 +01001326static inline bool req_has_async_data(struct io_kiocb *req)
1327{
1328 return req->flags & REQ_F_ASYNC_DATA;
1329}
1330
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001331static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001332{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001333 req->flags |= REQ_F_FAIL;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001334 if (req->flags & REQ_F_CQE_SKIP) {
1335 req->flags &= ~REQ_F_CQE_SKIP;
1336 req->flags |= REQ_F_SKIP_LINK_CQES;
1337 }
Jens Axboec40f6372020-06-25 15:39:59 -06001338}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001339
Hao Xua8295b92021-08-27 17:46:09 +08001340static inline void req_fail_link_node(struct io_kiocb *req, int res)
1341{
1342 req_set_fail(req);
1343 req->result = res;
1344}
1345
Pavel Begunkovc0724812021-10-04 20:02:54 +01001346static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001347{
1348 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1349
Jens Axboe0f158b42020-05-14 17:18:39 -06001350 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001351}
1352
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001353static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1354{
1355 return !req->timeout.off;
1356}
1357
Pavel Begunkovc0724812021-10-04 20:02:54 +01001358static __cold void io_fallback_req_func(struct work_struct *work)
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001359{
1360 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1361 fallback_work.work);
1362 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1363 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001364 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001365
1366 percpu_ref_get(&ctx->refs);
1367 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001368 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001369
Pavel Begunkovf237c302021-08-18 12:42:46 +01001370 if (locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01001371 io_submit_flush_completions(ctx);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001372 mutex_unlock(&ctx->uring_lock);
1373 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001374 percpu_ref_put(&ctx->refs);
1375}
1376
Pavel Begunkovc0724812021-10-04 20:02:54 +01001377static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001378{
1379 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001380 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001381
1382 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1383 if (!ctx)
1384 return NULL;
1385
Jens Axboe78076bb2019-12-04 19:56:40 -07001386 /*
1387 * Use 5 bits less than the max cq entries, that should give us around
1388 * 32 entries per hash list if totally full and uniformly spread.
1389 */
1390 hash_bits = ilog2(p->cq_entries);
1391 hash_bits -= 5;
1392 if (hash_bits <= 0)
1393 hash_bits = 1;
1394 ctx->cancel_hash_bits = hash_bits;
1395 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1396 GFP_KERNEL);
1397 if (!ctx->cancel_hash)
1398 goto err;
1399 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1400
Pavel Begunkov62248432021-04-28 13:11:29 +01001401 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1402 if (!ctx->dummy_ubuf)
1403 goto err;
1404 /* set invalid range, so io_import_fixed() fails meeting it */
1405 ctx->dummy_ubuf->ubuf = -1UL;
1406
Roman Gushchin21482892019-05-07 10:01:48 -07001407 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001408 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1409 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001410
1411 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001412 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001413 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001414 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001415 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001416 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001417 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001418 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001419 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001421 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01001422 INIT_WQ_LIST(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001423 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001424 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001425 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001426 spin_lock_init(&ctx->rsrc_ref_lock);
1427 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001428 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1429 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001430 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001431 ctx->submit_state.free_list.next = NULL;
1432 INIT_WQ_LIST(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001433 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001434 INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001435 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001436err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001437 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001438 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001439 kfree(ctx);
1440 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001441}
1442
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001443static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1444{
1445 struct io_rings *r = ctx->rings;
1446
1447 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1448 ctx->cq_extra--;
1449}
1450
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001451static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001452{
Jens Axboe2bc99302020-07-09 09:43:27 -06001453 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1454 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001455
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001456 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001457 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001458
Bob Liu9d858b22019-11-13 18:06:25 +08001459 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001460}
1461
Pavel Begunkov35645ac2021-10-17 00:07:09 +01001462#define FFS_NOWAIT 0x1UL
1463#define FFS_ISREG 0x2UL
1464#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG)
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001465
1466static inline bool io_req_ffs_set(struct io_kiocb *req)
1467{
Pavel Begunkov35645ac2021-10-17 00:07:09 +01001468 return req->flags & REQ_F_FIXED_FILE;
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001469}
1470
Pavel Begunkovc0724812021-10-04 20:02:54 +01001471static inline void io_req_track_inflight(struct io_kiocb *req)
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001472{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001473 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001474 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001475 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001476 }
1477}
1478
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001479static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1480{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001481 if (WARN_ON_ONCE(!req->link))
1482 return NULL;
1483
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001484 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1485 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001486
1487 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001488 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001489 __io_req_set_refcount(req->link, 2);
1490 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001491}
1492
1493static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1494{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001495 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001496 return NULL;
1497 return __io_prep_linked_timeout(req);
1498}
1499
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001500static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001501{
Jens Axboed3656342019-12-18 09:50:26 -07001502 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001503 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001504
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001505 if (!(req->flags & REQ_F_CREDS)) {
1506 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001507 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001508 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001509
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001510 req->work.list.next = NULL;
1511 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001512 if (req->flags & REQ_F_FORCE_ASYNC)
1513 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1514
Jens Axboed3656342019-12-18 09:50:26 -07001515 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001516 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001517 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001518 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001519 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001520 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001521 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001522
1523 switch (req->opcode) {
1524 case IORING_OP_SPLICE:
1525 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001526 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1527 req->work.flags |= IO_WQ_WORK_UNBOUND;
1528 break;
1529 }
Jens Axboe561fb042019-10-24 07:25:42 -06001530}
1531
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001532static void io_prep_async_link(struct io_kiocb *req)
1533{
1534 struct io_kiocb *cur;
1535
Pavel Begunkov44eff402021-07-26 14:14:31 +01001536 if (req->flags & REQ_F_LINK_TIMEOUT) {
1537 struct io_ring_ctx *ctx = req->ctx;
1538
Jens Axboe79ebeae2021-08-10 15:18:27 -06001539 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001540 io_for_each_link(cur, req)
1541 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001542 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001543 } else {
1544 io_for_each_link(cur, req)
1545 io_prep_async_work(cur);
1546 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001547}
1548
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001549static inline void io_req_add_compl_list(struct io_kiocb *req)
1550{
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00001551 struct io_ring_ctx *ctx = req->ctx;
Hao Xu33ce2af2021-12-14 13:59:04 +08001552 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001553
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00001554 if (!(req->flags & REQ_F_CQE_SKIP))
1555 ctx->submit_state.flush_cqes = true;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001556 wq_list_add_tail(&req->comp_list, &state->compl_reqs);
1557}
1558
Arnd Bergmann00169242021-10-19 17:34:53 +02001559static void io_queue_async_work(struct io_kiocb *req, bool *dont_use)
Jens Axboe561fb042019-10-24 07:25:42 -06001560{
Jackie Liua197f662019-11-08 08:09:12 -07001561 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001562 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001563 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001564
Jens Axboe3bfe6102021-02-16 14:15:30 -07001565 BUG_ON(!tctx);
1566 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001567
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001568 /* init ->work of the whole link before punting */
1569 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001570
1571 /*
1572 * Not expected to happen, but if we do have a bug where this _can_
1573 * happen, catch it here and ensure the request is marked as
1574 * canceled. That will make io-wq go through the usual work cancel
1575 * procedure rather than attempt to run this request (or create a new
1576 * worker for it).
1577 */
1578 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1579 req->work.flags |= IO_WQ_WORK_CANCEL;
1580
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001581 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1582 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001583 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001584 if (link)
1585 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001586}
1587
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001588static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001589 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001590 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001591{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001592 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001593
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001594 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001595 if (status)
1596 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001597 atomic_set(&req->ctx->cq_timeouts,
1598 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001599 list_del_init(&req->timeout.list);
Pavel Begunkov913a5712021-11-10 15:49:31 +00001600 io_fill_cqe_req(req, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001601 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001602 }
1603}
1604
Pavel Begunkovc0724812021-10-04 20:02:54 +01001605static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001606{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001607 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001608 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1609 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001610
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001611 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001612 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001613 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001614 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001615 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001616 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001617}
1618
Pavel Begunkovc0724812021-10-04 20:02:54 +01001619static __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001620 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001621{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001622 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001623
Jens Axboe79ebeae2021-08-10 15:18:27 -06001624 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001625 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001626 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001627 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001628 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001629
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001630 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001631 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001632
1633 /*
1634 * Since seq can easily wrap around over time, subtract
1635 * the last seq at which timeouts were flushed before comparing.
1636 * Assuming not more than 2^31-1 events have happened since,
1637 * these subtractions won't have wrapped, so we can check if
1638 * target is in [last_seq, current_seq] by comparing the two.
1639 */
1640 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1641 events_got = seq - ctx->cq_last_tm_flush;
1642 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001643 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001644
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001645 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001646 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001647 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001648 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001649 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001650}
1651
Pavel Begunkovc0724812021-10-04 20:02:54 +01001652static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001653{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001654 if (ctx->off_timeout_used)
1655 io_flush_timeouts(ctx);
1656 if (ctx->drain_active)
1657 io_queue_deferred(ctx);
1658}
1659
1660static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1661{
1662 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1663 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001664 /* order cqe stores with ring update */
1665 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001666}
1667
Jens Axboe90554202020-09-03 12:12:41 -06001668static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1669{
1670 struct io_rings *r = ctx->rings;
1671
Pavel Begunkova566c552021-05-16 22:58:08 +01001672 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001673}
1674
Pavel Begunkov888aae22021-01-19 13:32:39 +00001675static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1676{
1677 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1678}
1679
Pavel Begunkovd068b502021-05-16 22:58:11 +01001680static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681{
Hristo Venev75b28af2019-08-26 17:23:46 +00001682 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001683 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001684
Stefan Bühler115e12e2019-04-24 23:54:18 +02001685 /*
1686 * writes to the cq entry need to come after reading head; the
1687 * control dependency is enough as we're using WRITE_ONCE to
1688 * fill the cq entry
1689 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001690 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001691 return NULL;
1692
Pavel Begunkov888aae22021-01-19 13:32:39 +00001693 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001694 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001695}
1696
Jens Axboef2842ab2020-01-08 11:04:00 -07001697static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1698{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001699 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001700 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001701 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1702 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001703 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001704}
1705
Jens Axboe2c5d7632021-08-21 07:21:19 -06001706/*
1707 * This should only get called when at least one event has been posted.
1708 * Some applications rely on the eventfd notification count only changing
1709 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1710 * 1:1 relationship between how many times this function is called (and
1711 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1712 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001713static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001714{
Jens Axboe5fd46172021-08-06 14:04:31 -06001715 /*
1716 * wake_up_all() may seem excessive, but io_wake_function() and
1717 * io_should_wake() handle the termination of the loop and only
1718 * wake as many waiters as we need to.
1719 */
1720 if (wq_has_sleeper(&ctx->cq_wait))
1721 wake_up_all(&ctx->cq_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001722 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001723 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001724}
1725
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001726static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1727{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001728 /* see waitqueue_active() comment */
1729 smp_mb();
1730
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001731 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001732 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001733 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001734 }
1735 if (io_should_trigger_evfd(ctx))
1736 eventfd_signal(ctx->cq_ev_fd, 1);
1737}
1738
Jens Axboec4a2ed72019-11-21 21:01:26 -07001739/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001740static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741{
Jens Axboeb18032b2021-01-24 16:58:56 -07001742 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001743
Pavel Begunkova566c552021-05-16 22:58:08 +01001744 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001745 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001746
Jens Axboeb18032b2021-01-24 16:58:56 -07001747 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001748 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001749 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001750 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001751 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001752
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001753 if (!cqe && !force)
1754 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001755 ocqe = list_first_entry(&ctx->cq_overflow_list,
1756 struct io_overflow_cqe, list);
1757 if (cqe)
1758 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1759 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001760 io_account_cq_overflow(ctx);
1761
Jens Axboeb18032b2021-01-24 16:58:56 -07001762 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001763 list_del(&ocqe->list);
1764 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001765 }
1766
Pavel Begunkov09e88402020-12-17 00:24:38 +00001767 all_flushed = list_empty(&ctx->cq_overflow_list);
1768 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001769 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001770 WRITE_ONCE(ctx->rings->sq_flags,
1771 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001772 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001773
Jens Axboeb18032b2021-01-24 16:58:56 -07001774 if (posted)
1775 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001776 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001777 if (posted)
1778 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001779 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001780}
1781
Pavel Begunkov90f67362021-08-09 20:18:12 +01001782static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001783{
Jens Axboeca0a2652021-03-04 17:15:48 -07001784 bool ret = true;
1785
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001786 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001787 /* iopoll syncs against uring_lock, not completion_lock */
1788 if (ctx->flags & IORING_SETUP_IOPOLL)
1789 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001790 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001791 if (ctx->flags & IORING_SETUP_IOPOLL)
1792 mutex_unlock(&ctx->uring_lock);
1793 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001794
1795 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001796}
1797
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001798/* must to be called somewhat shortly after putting a request */
1799static inline void io_put_task(struct task_struct *task, int nr)
1800{
1801 struct io_uring_task *tctx = task->io_uring;
1802
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001803 if (likely(task == current)) {
1804 tctx->cached_refs += nr;
1805 } else {
1806 percpu_counter_sub(&tctx->inflight, nr);
1807 if (unlikely(atomic_read(&tctx->in_idle)))
1808 wake_up(&tctx->wait);
1809 put_task_struct_many(task, nr);
1810 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001811}
1812
Pavel Begunkov9a108672021-08-27 11:55:01 +01001813static void io_task_refs_refill(struct io_uring_task *tctx)
1814{
1815 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1816
1817 percpu_counter_add(&tctx->inflight, refill);
1818 refcount_add(refill, &current->usage);
1819 tctx->cached_refs += refill;
1820}
1821
1822static inline void io_get_task_refs(int nr)
1823{
1824 struct io_uring_task *tctx = current->io_uring;
1825
1826 tctx->cached_refs -= nr;
1827 if (unlikely(tctx->cached_refs < 0))
1828 io_task_refs_refill(tctx);
1829}
1830
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001831static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001832 s32 res, u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001833{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001834 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001835
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001836 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1837 if (!ocqe) {
1838 /*
1839 * If we're in ring overflow flush mode, or in task cancel mode,
1840 * or cannot allocate an overflow entry, then we need to drop it
1841 * on the floor.
1842 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001843 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001844 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001845 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001846 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001847 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001848 WRITE_ONCE(ctx->rings->sq_flags,
1849 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1850
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001851 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001852 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001853 ocqe->cqe.res = res;
1854 ocqe->cqe.flags = cflags;
1855 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1856 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001857}
1858
Pavel Begunkov913a5712021-11-10 15:49:31 +00001859static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data,
1860 s32 res, u32 cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001861{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001862 struct io_uring_cqe *cqe;
1863
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001864 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001865
1866 /*
1867 * If we can't get a cq entry, userspace overflowed the
1868 * submission (by quite a lot). Increment the overflow count in
1869 * the ring.
1870 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001871 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001872 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001873 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001874 WRITE_ONCE(cqe->res, res);
1875 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001876 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001877 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001878 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001879}
1880
Pavel Begunkov913a5712021-11-10 15:49:31 +00001881static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001882{
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001883 if (!(req->flags & REQ_F_CQE_SKIP))
1884 __io_fill_cqe(req->ctx, req->user_data, res, cflags);
Pavel Begunkov913a5712021-11-10 15:49:31 +00001885}
1886
1887static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data,
1888 s32 res, u32 cflags)
1889{
1890 ctx->cq_extra++;
1891 return __io_fill_cqe(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001892}
1893
Hao Xua37fae82021-12-07 17:39:50 +08001894static void __io_req_complete_post(struct io_kiocb *req, s32 res,
1895 u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001896{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001897 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001899 if (!(req->flags & REQ_F_CQE_SKIP))
1900 __io_fill_cqe(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001901 /*
1902 * If we're the last reference to this request, add to our locked
1903 * free_list cache.
1904 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001905 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001906 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001907 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001908 io_disarm_next(req);
1909 if (req->link) {
1910 io_req_task_queue(req->link);
1911 req->link = NULL;
1912 }
1913 }
Pavel Begunkovab409402021-10-09 23:14:41 +01001914 io_req_put_rsrc(req, ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001915 io_dismantle_req(req);
1916 io_put_task(req->task, 1);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001917 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001918 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001919 }
Hao Xua37fae82021-12-07 17:39:50 +08001920}
1921
1922static void io_req_complete_post(struct io_kiocb *req, s32 res,
1923 u32 cflags)
1924{
1925 struct io_ring_ctx *ctx = req->ctx;
1926
1927 spin_lock(&ctx->completion_lock);
1928 __io_req_complete_post(req, res, cflags);
Pavel Begunkov7a612352021-03-09 00:37:59 +00001929 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001930 spin_unlock(&ctx->completion_lock);
Pavel Begunkova3f349072021-09-15 12:04:20 +01001931 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001932}
1933
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001934static inline void io_req_complete_state(struct io_kiocb *req, s32 res,
1935 u32 cflags)
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001936{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001937 req->result = res;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +01001938 req->cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001939 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001940}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001941
Pavel Begunkov889fca72021-02-10 00:03:09 +00001942static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001943 s32 res, u32 cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001944{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001945 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1946 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001947 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001948 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001949}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001950
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001951static inline void io_req_complete(struct io_kiocb *req, s32 res)
Jens Axboee1e16092020-06-22 09:17:17 -06001952{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001953 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001954}
1955
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001956static void io_req_complete_failed(struct io_kiocb *req, s32 res)
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001957{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001958 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001959 io_req_complete_post(req, res, 0);
1960}
1961
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01001962static void io_req_complete_fail_submit(struct io_kiocb *req)
1963{
1964 /*
1965 * We don't submit, fail them all, for that replace hardlinks with
1966 * normal links. Extra REQ_F_LINK is tolerated.
1967 */
1968 req->flags &= ~REQ_F_HARDLINK;
1969 req->flags |= REQ_F_LINK;
1970 io_req_complete_failed(req, req->result);
1971}
1972
Pavel Begunkov864ea922021-08-09 13:04:08 +01001973/*
1974 * Don't initialise the fields below on every allocation, but do that in
1975 * advance and keep them valid across allocations.
1976 */
1977static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1978{
1979 req->ctx = ctx;
1980 req->link = NULL;
1981 req->async_data = NULL;
1982 /* not necessary, but safer to zero */
1983 req->result = 0;
1984}
1985
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001986static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001987 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001988{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001989 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001990 wq_list_splice(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001991 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001992 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001993}
1994
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001995/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001996static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001997{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001998 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001999
Jens Axboec7dae4b2021-02-09 19:53:37 -07002000 /*
2001 * If we have more than a batch's worth of requests in our IRQ side
2002 * locked cache, grab the lock and move them over to our submission
2003 * side cache.
2004 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01002005 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002006 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002007 return !!state->free_list.next;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002008}
2009
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01002010/*
2011 * A request might get retired back into the request caches even before opcode
2012 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
2013 * Because of that, io_alloc_req() should be called only under ->uring_lock
2014 * and with extra caution to not get a request that is still worked on.
2015 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01002016static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01002017 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002018{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002019 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01002020 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002021 void *reqs[IO_REQ_ALLOC_BATCH];
2022 struct io_kiocb *req;
Pavel Begunkov864ea922021-08-09 13:04:08 +01002023 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002024
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002025 if (likely(state->free_list.next || io_flush_cached_reqs(ctx)))
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01002026 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002027
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002028 ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002029
Pavel Begunkov864ea922021-08-09 13:04:08 +01002030 /*
2031 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2032 * retry single alloc to be on the safe side.
2033 */
2034 if (unlikely(ret <= 0)) {
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002035 reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2036 if (!reqs[0])
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01002037 return false;
Pavel Begunkov864ea922021-08-09 13:04:08 +01002038 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002039 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01002040
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002041 percpu_ref_get_many(&ctx->refs, ret);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002042 for (i = 0; i < ret; i++) {
2043 req = reqs[i];
2044
2045 io_preinit_req(req, ctx);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002046 wq_stack_add_head(&req->comp_list, &state->free_list);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002047 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01002048 return true;
2049}
2050
2051static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
2052{
2053 if (unlikely(!ctx->submit_state.free_list.next))
2054 return __io_alloc_req_refill(ctx);
2055 return true;
2056}
2057
2058static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
2059{
2060 struct io_wq_work_node *node;
2061
2062 node = wq_stack_extract(&ctx->submit_state.free_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002063 return container_of(node, struct io_kiocb, comp_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002064}
2065
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002066static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002067{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002068 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002069 fput(file);
2070}
2071
Pavel Begunkov6b639522021-09-08 16:40:50 +01002072static inline void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002073{
Pavel Begunkov094bae42021-03-19 17:22:42 +00002074 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002075
Pavel Begunkov867f8fa2021-10-04 20:02:58 +01002076 if (unlikely(flags & IO_REQ_CLEAN_FLAGS))
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01002077 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002078 if (!(flags & REQ_F_FIXED_FILE))
2079 io_put_file(req->file);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002080}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002081
Pavel Begunkovc0724812021-10-04 20:02:54 +01002082static __cold void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002083{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002084 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002085
Pavel Begunkovab409402021-10-09 23:14:41 +01002086 io_req_put_rsrc(req, ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002087 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002088 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002089
Jens Axboe79ebeae2021-08-10 15:18:27 -06002090 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002091 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01002092 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06002093 spin_unlock(&ctx->completion_lock);
Jens Axboee65ef562019-03-12 10:16:44 -06002094}
2095
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002096static inline void io_remove_next_linked(struct io_kiocb *req)
2097{
2098 struct io_kiocb *nxt = req->link;
2099
2100 req->link = nxt->link;
2101 nxt->link = NULL;
2102}
2103
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002104static bool io_kill_linked_timeout(struct io_kiocb *req)
2105 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06002106 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002107{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002108 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002109
Pavel Begunkovb97e7362021-08-15 10:40:23 +01002110 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002111 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002112
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002113 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002114 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01002115 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06002116 list_del(&link->timeout.list);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002117 /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */
Pavel Begunkov913a5712021-11-10 15:49:31 +00002118 io_fill_cqe_req(link, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002119 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002120 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002121 }
2122 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002123 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002124}
2125
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002126static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002127 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002128{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002129 struct io_kiocb *nxt, *link = req->link;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002130 bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES;
Jens Axboe9e645e112019-05-10 16:07:28 -06002131
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002132 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002133 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002134 long res = -ECANCELED;
2135
2136 if (link->flags & REQ_F_FAIL)
2137 res = link->result;
2138
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002139 nxt = link->link;
2140 link->link = NULL;
2141
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002142 trace_io_uring_fail_link(req, link);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002143 if (!ignore_cqes) {
2144 link->flags &= ~REQ_F_CQE_SKIP;
2145 io_fill_cqe_req(link, res, 0);
2146 }
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002147 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002148 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002149 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002150}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002151
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002152static bool io_disarm_next(struct io_kiocb *req)
2153 __must_hold(&req->ctx->completion_lock)
2154{
2155 bool posted = false;
2156
Pavel Begunkov0756a862021-08-15 10:40:25 +01002157 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2158 struct io_kiocb *link = req->link;
2159
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002160 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002161 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2162 io_remove_next_linked(req);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002163 /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */
Pavel Begunkov913a5712021-11-10 15:49:31 +00002164 io_fill_cqe_req(link, -ECANCELED, 0);
Pavel Begunkov0756a862021-08-15 10:40:25 +01002165 io_put_req_deferred(link);
2166 posted = true;
2167 }
2168 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002169 struct io_ring_ctx *ctx = req->ctx;
2170
2171 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002172 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002173 spin_unlock_irq(&ctx->timeout_lock);
2174 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002175 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002176 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002177 posted |= (req->link != NULL);
2178 io_fail_links(req);
2179 }
2180 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002181}
2182
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002183static void __io_req_find_next_prep(struct io_kiocb *req)
2184{
2185 struct io_ring_ctx *ctx = req->ctx;
2186 bool posted;
2187
2188 spin_lock(&ctx->completion_lock);
2189 posted = io_disarm_next(req);
2190 if (posted)
Hao Xu33ce2af2021-12-14 13:59:04 +08002191 io_commit_cqring(ctx);
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002192 spin_unlock(&ctx->completion_lock);
2193 if (posted)
2194 io_cqring_ev_posted(ctx);
2195}
2196
2197static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002198{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002199 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002200
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002201 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2202 return NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002203 /*
2204 * If LINK is set, we have dependent requests in this chain. If we
2205 * didn't fail this request, queue the first one up, moving any other
2206 * dependencies to the next request. In case of failure, fail the rest
2207 * of the chain.
2208 */
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002209 if (unlikely(req->flags & IO_DISARM_MASK))
2210 __io_req_find_next_prep(req);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002211 nxt = req->link;
2212 req->link = NULL;
2213 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002214}
Jens Axboe2665abf2019-11-05 12:40:47 -07002215
Pavel Begunkovf237c302021-08-18 12:42:46 +01002216static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002217{
2218 if (!ctx)
2219 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002220 if (*locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01002221 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002222 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002223 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002224 }
2225 percpu_ref_put(&ctx->refs);
2226}
2227
Hao Xuf28c240e2021-12-08 13:21:25 +08002228static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx)
2229{
2230 io_commit_cqring(ctx);
2231 spin_unlock(&ctx->completion_lock);
2232 io_cqring_ev_posted(ctx);
2233}
2234
2235static void handle_prev_tw_list(struct io_wq_work_node *node,
2236 struct io_ring_ctx **ctx, bool *uring_locked)
2237{
2238 if (*ctx && !*uring_locked)
2239 spin_lock(&(*ctx)->completion_lock);
2240
2241 do {
2242 struct io_wq_work_node *next = node->next;
2243 struct io_kiocb *req = container_of(node, struct io_kiocb,
2244 io_task_work.node);
2245
2246 if (req->ctx != *ctx) {
2247 if (unlikely(!*uring_locked && *ctx))
2248 ctx_commit_and_unlock(*ctx);
2249
2250 ctx_flush_and_put(*ctx, uring_locked);
2251 *ctx = req->ctx;
2252 /* if not contended, grab and improve batching */
2253 *uring_locked = mutex_trylock(&(*ctx)->uring_lock);
2254 percpu_ref_get(&(*ctx)->refs);
2255 if (unlikely(!*uring_locked))
2256 spin_lock(&(*ctx)->completion_lock);
2257 }
2258 if (likely(*uring_locked))
2259 req->io_task_work.func(req, uring_locked);
2260 else
2261 __io_req_complete_post(req, req->result, io_put_kbuf(req));
2262 node = next;
2263 } while (node);
2264
2265 if (unlikely(!*uring_locked))
2266 ctx_commit_and_unlock(*ctx);
2267}
2268
2269static void handle_tw_list(struct io_wq_work_node *node,
2270 struct io_ring_ctx **ctx, bool *locked)
Hao Xu9f8d0322021-12-07 17:39:49 +08002271{
2272 do {
2273 struct io_wq_work_node *next = node->next;
2274 struct io_kiocb *req = container_of(node, struct io_kiocb,
2275 io_task_work.node);
2276
2277 if (req->ctx != *ctx) {
2278 ctx_flush_and_put(*ctx, locked);
2279 *ctx = req->ctx;
2280 /* if not contended, grab and improve batching */
2281 *locked = mutex_trylock(&(*ctx)->uring_lock);
2282 percpu_ref_get(&(*ctx)->refs);
2283 }
2284 req->io_task_work.func(req, locked);
2285 node = next;
2286 } while (node);
2287}
2288
Jens Axboe7cbf1722021-02-10 00:03:20 +00002289static void tctx_task_work(struct callback_head *cb)
2290{
Hao Xuf28c240e2021-12-08 13:21:25 +08002291 bool uring_locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002292 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002293 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2294 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002295
Pavel Begunkov16f72072021-06-17 18:14:09 +01002296 while (1) {
Hao Xuf28c240e2021-12-08 13:21:25 +08002297 struct io_wq_work_node *node1, *node2;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002298
Hao Xuf28c240e2021-12-08 13:21:25 +08002299 if (!tctx->task_list.first &&
2300 !tctx->prior_task_list.first && uring_locked)
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002301 io_submit_flush_completions(ctx);
2302
Pavel Begunkov3f184072021-06-17 18:14:06 +01002303 spin_lock_irq(&tctx->task_lock);
Hao Xuf28c240e2021-12-08 13:21:25 +08002304 node1 = tctx->prior_task_list.first;
2305 node2 = tctx->task_list.first;
2306 INIT_WQ_LIST(&tctx->task_list);
2307 INIT_WQ_LIST(&tctx->prior_task_list);
2308 if (!node2 && !node1)
Pavel Begunkov6294f362021-08-10 17:53:55 +01002309 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002310 spin_unlock_irq(&tctx->task_lock);
Hao Xuf28c240e2021-12-08 13:21:25 +08002311 if (!node2 && !node1)
Pavel Begunkov6294f362021-08-10 17:53:55 +01002312 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002313
Hao Xuf28c240e2021-12-08 13:21:25 +08002314 if (node1)
2315 handle_prev_tw_list(node1, &ctx, &uring_locked);
2316
2317 if (node2)
2318 handle_tw_list(node2, &ctx, &uring_locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002319 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002320 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002321
Hao Xuf28c240e2021-12-08 13:21:25 +08002322 ctx_flush_and_put(ctx, &uring_locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002323}
2324
Hao Xu4813c372021-12-07 17:39:48 +08002325static void io_req_task_work_add(struct io_kiocb *req, bool priority)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002326{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002327 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002328 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002329 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002330 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002331 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002332 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002333
2334 WARN_ON_ONCE(!tctx);
2335
Jens Axboe0b81e802021-02-16 10:33:53 -07002336 spin_lock_irqsave(&tctx->task_lock, flags);
Hao Xu4813c372021-12-07 17:39:48 +08002337 if (priority)
2338 wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list);
2339 else
2340 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002341 running = tctx->task_running;
2342 if (!running)
2343 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002344 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002345
2346 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002347 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002348 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002349
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002350 /*
2351 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2352 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2353 * processing task_work. There's no reliable way to tell if TWA_RESUME
2354 * will do the job.
2355 */
2356 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovd97ec622021-09-08 16:40:53 +01002357 if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2358 if (notify == TWA_NONE)
2359 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002360 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002361 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002362
Pavel Begunkove09ee512021-07-01 13:26:05 +01002363 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002364 tctx->task_running = false;
Hao Xu4813c372021-12-07 17:39:48 +08002365 node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002366 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002367
Pavel Begunkove09ee512021-07-01 13:26:05 +01002368 while (node) {
2369 req = container_of(node, struct io_kiocb, io_task_work.node);
2370 node = node->next;
2371 if (llist_add(&req->io_task_work.fallback_node,
2372 &req->ctx->fallback_llist))
2373 schedule_delayed_work(&req->ctx->fallback_work, 1);
2374 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002375}
2376
Pavel Begunkovf237c302021-08-18 12:42:46 +01002377static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002378{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002379 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002380
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002381 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002382 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002383 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002384}
2385
Pavel Begunkovf237c302021-08-18 12:42:46 +01002386static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002387{
2388 struct io_ring_ctx *ctx = req->ctx;
2389
Pavel Begunkovf237c302021-08-18 12:42:46 +01002390 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002391 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002392 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002393 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002394 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002395 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002396}
2397
Pavel Begunkova3df76982021-02-18 22:32:52 +00002398static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2399{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002400 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002401 req->io_task_work.func = io_req_task_cancel;
Hao Xu4813c372021-12-07 17:39:48 +08002402 io_req_task_work_add(req, false);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002403}
2404
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002405static void io_req_task_queue(struct io_kiocb *req)
2406{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002407 req->io_task_work.func = io_req_task_submit;
Hao Xu4813c372021-12-07 17:39:48 +08002408 io_req_task_work_add(req, false);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002409}
2410
Jens Axboe773af692021-07-27 10:25:55 -06002411static void io_req_task_queue_reissue(struct io_kiocb *req)
2412{
2413 req->io_task_work.func = io_queue_async_work;
Hao Xu4813c372021-12-07 17:39:48 +08002414 io_req_task_work_add(req, false);
Jens Axboe773af692021-07-27 10:25:55 -06002415}
2416
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002417static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002418{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002419 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002420
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002421 if (nxt)
2422 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002423}
2424
Jens Axboe9e645e112019-05-10 16:07:28 -06002425static void io_free_req(struct io_kiocb *req)
2426{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002427 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002428 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002429}
2430
Pavel Begunkovf237c302021-08-18 12:42:46 +01002431static void io_free_req_work(struct io_kiocb *req, bool *locked)
2432{
2433 io_free_req(req);
2434}
2435
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002436static void io_free_batch_list(struct io_ring_ctx *ctx,
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002437 struct io_wq_work_node *node)
Jens Axboea141dd82021-08-12 12:48:34 -06002438 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002439{
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002440 struct task_struct *task = NULL;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002441 int task_refs = 0;
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002442
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002443 do {
2444 struct io_kiocb *req = container_of(node, struct io_kiocb,
2445 comp_list);
2446
Pavel Begunkovdef77ac2021-10-12 15:02:14 +01002447 if (unlikely(req->flags & REQ_F_REFCOUNT)) {
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002448 node = req->comp_list.next;
Pavel Begunkovdef77ac2021-10-12 15:02:14 +01002449 if (!req_ref_put_and_test(req))
2450 continue;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002451 }
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002452
Pavel Begunkovab409402021-10-09 23:14:41 +01002453 io_req_put_rsrc_locked(req, ctx);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002454 io_queue_next(req);
2455 io_dismantle_req(req);
2456
2457 if (req->task != task) {
2458 if (task)
2459 io_put_task(task, task_refs);
2460 task = req->task;
2461 task_refs = 0;
2462 }
2463 task_refs++;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002464 node = req->comp_list.next;
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002465 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002466 } while (node);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002467
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002468 if (task)
2469 io_put_task(task, task_refs);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002470}
2471
Pavel Begunkovc4501782021-09-08 16:40:52 +01002472static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002473 __must_hold(&ctx->uring_lock)
2474{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002475 struct io_wq_work_node *node, *prev;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002476 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002477
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00002478 if (state->flush_cqes) {
2479 spin_lock(&ctx->completion_lock);
2480 wq_list_for_each(node, prev, &state->compl_reqs) {
2481 struct io_kiocb *req = container_of(node, struct io_kiocb,
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002482 comp_list);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002483
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00002484 if (!(req->flags & REQ_F_CQE_SKIP))
2485 __io_fill_cqe(ctx, req->user_data, req->result,
2486 req->cflags);
2487 }
2488
2489 io_commit_cqring(ctx);
2490 spin_unlock(&ctx->completion_lock);
2491 io_cqring_ev_posted(ctx);
2492 state->flush_cqes = false;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002493 }
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002494
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002495 io_free_batch_list(ctx, state->compl_reqs.first);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002496 INIT_WQ_LIST(&state->compl_reqs);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002497}
2498
Jens Axboeba816ad2019-09-28 11:36:45 -06002499/*
2500 * Drop reference to request, return next in chain (if there is one) if this
2501 * was the last reference to this request.
2502 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002503static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002504{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002505 struct io_kiocb *nxt = NULL;
2506
Jens Axboede9b4cc2021-02-24 13:28:27 -07002507 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002508 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002509 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002510 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002511 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002512}
2513
Pavel Begunkov0d850352021-03-19 17:22:37 +00002514static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002515{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002516 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002517 io_free_req(req);
2518}
2519
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002520static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002521{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002522 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002523 req->io_task_work.func = io_free_req_work;
Hao Xu4813c372021-12-07 17:39:48 +08002524 io_req_task_work_add(req, false);
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002525 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002526}
2527
Pavel Begunkov6c503152021-01-04 20:36:36 +00002528static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002529{
2530 /* See comment at the top of this file */
2531 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002532 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002533}
2534
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002535static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2536{
2537 struct io_rings *rings = ctx->rings;
2538
2539 /* make sure SQ entry isn't read before tail */
2540 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2541}
2542
Jens Axboe4c6e2772020-07-01 11:29:10 -06002543static inline bool io_run_task_work(void)
2544{
Nadav Amitef98eb02021-08-07 17:13:41 -07002545 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002546 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002547 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002548 return true;
2549 }
2550
2551 return false;
2552}
2553
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002554static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
Jens Axboedef596e2019-01-09 08:59:42 -07002555{
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002556 struct io_wq_work_node *pos, *start, *prev;
Christoph Hellwigd729cf92021-10-12 13:12:20 +02002557 unsigned int poll_flags = BLK_POLL_NOSLEEP;
Jens Axboeb688f112021-10-12 09:28:46 -06002558 DEFINE_IO_COMP_BATCH(iob);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002559 int nr_events = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002560
2561 /*
2562 * Only spin for completions if we don't have multiple devices hanging
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002563 * off our complete list.
Jens Axboedef596e2019-01-09 08:59:42 -07002564 */
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002565 if (ctx->poll_multi_queue || force_nonspin)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002566 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002567
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002568 wq_list_for_each(pos, start, &ctx->iopoll_list) {
2569 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002570 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002571 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002572
2573 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002574 * Move completed and retryable entries to our local lists.
2575 * If we find a request that requires polling, break out
2576 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002577 */
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002578 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002579 break;
2580
Jens Axboeb688f112021-10-12 09:28:46 -06002581 ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002582 if (unlikely(ret < 0))
2583 return ret;
2584 else if (ret)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002585 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002586
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002587 /* iopoll may have completed current req */
Jens Axboeb688f112021-10-12 09:28:46 -06002588 if (!rq_list_empty(iob.req_list) ||
2589 READ_ONCE(req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002590 break;
Jens Axboedef596e2019-01-09 08:59:42 -07002591 }
2592
Jens Axboeb688f112021-10-12 09:28:46 -06002593 if (!rq_list_empty(iob.req_list))
2594 iob.complete(&iob);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002595 else if (!pos)
2596 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002597
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002598 prev = start;
2599 wq_list_for_each_resume(pos, prev) {
2600 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
2601
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002602 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
2603 if (!smp_load_acquire(&req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002604 break;
Pavel Begunkov83a13a42021-12-05 14:37:59 +00002605 if (unlikely(req->flags & REQ_F_CQE_SKIP))
2606 continue;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00002607
Pavel Begunkov83a13a42021-12-05 14:37:59 +00002608 __io_fill_cqe(ctx, req->user_data, req->result, io_put_kbuf(req));
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002609 nr_events++;
2610 }
Jens Axboedef596e2019-01-09 08:59:42 -07002611
Pavel Begunkovf5ed3bc2021-09-24 21:59:52 +01002612 if (unlikely(!nr_events))
2613 return 0;
2614
2615 io_commit_cqring(ctx);
2616 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002617 pos = start ? start->next : ctx->iopoll_list.first;
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002618 wq_list_cut(&ctx->iopoll_list, prev, start);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002619 io_free_batch_list(ctx, pos);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002620 return nr_events;
Jens Axboedef596e2019-01-09 08:59:42 -07002621}
2622
2623/*
Jens Axboedef596e2019-01-09 08:59:42 -07002624 * We can't just wait for polled events to come to us, we have to actively
2625 * find and complete them.
2626 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01002627static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002628{
2629 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2630 return;
2631
2632 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002633 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002634 /* let it sleep and repeat later if can't complete a request */
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002635 if (io_do_iopoll(ctx, true) == 0)
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002636 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002637 /*
2638 * Ensure we allow local-to-the-cpu processing to take place,
2639 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002640 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002641 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002642 if (need_resched()) {
2643 mutex_unlock(&ctx->uring_lock);
2644 cond_resched();
2645 mutex_lock(&ctx->uring_lock);
2646 }
Jens Axboedef596e2019-01-09 08:59:42 -07002647 }
2648 mutex_unlock(&ctx->uring_lock);
2649}
2650
Pavel Begunkov7668b922020-07-07 16:36:21 +03002651static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002652{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002653 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002654 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002655
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002656 /*
2657 * We disallow the app entering submit/complete with polling, but we
2658 * still need to lock the ring to prevent racing with polled issue
2659 * that got punted to a workqueue.
2660 */
2661 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002662 /*
2663 * Don't enter poll loop if we already have events pending.
2664 * If we do, we can potentially be spinning for commands that
2665 * already triggered a CQE (eg in error).
2666 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002667 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002668 __io_cqring_overflow_flush(ctx, false);
2669 if (io_cqring_events(ctx))
2670 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002671 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002672 /*
2673 * If a submit got punted to a workqueue, we can have the
2674 * application entering polling for a command before it gets
2675 * issued. That app will hold the uring_lock for the duration
2676 * of the poll right here, so we need to take a breather every
2677 * now and then to ensure that the issue has a chance to add
2678 * the poll to the issued list. Otherwise we can spin here
2679 * forever, while the workqueue is stuck trying to acquire the
2680 * very same mutex.
2681 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002682 if (wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002683 u32 tail = ctx->cached_cq_tail;
2684
Jens Axboe500f9fb2019-08-19 12:15:59 -06002685 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002686 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002687 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002688
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002689 /* some requests don't go through iopoll_list */
2690 if (tail != ctx->cached_cq_tail ||
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002691 wq_list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002692 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002693 }
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002694 ret = io_do_iopoll(ctx, !min);
2695 if (ret < 0)
2696 break;
2697 nr_events += ret;
2698 ret = 0;
2699 } while (nr_events < min && !need_resched());
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002700out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002701 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002702 return ret;
2703}
2704
Jens Axboe491381ce2019-10-17 09:20:46 -06002705static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002706{
Jens Axboe491381ce2019-10-17 09:20:46 -06002707 /*
2708 * Tell lockdep we inherited freeze protection from submission
2709 * thread.
2710 */
2711 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002712 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002713
Pavel Begunkov1c986792021-03-22 01:58:31 +00002714 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2715 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002716 }
2717}
2718
Jens Axboeb63534c2020-06-04 11:28:00 -06002719#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002720static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002721{
Pavel Begunkovab454432021-03-22 01:58:33 +00002722 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002723
Pavel Begunkovd886e182021-10-04 20:02:56 +01002724 if (!req_has_async_data(req))
Pavel Begunkovab454432021-03-22 01:58:33 +00002725 return !io_req_prep_async(req);
Pavel Begunkov538941e2021-10-14 16:10:15 +01002726 iov_iter_restore(&rw->s.iter, &rw->s.iter_state);
Pavel Begunkovab454432021-03-22 01:58:33 +00002727 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002728}
Jens Axboeb63534c2020-06-04 11:28:00 -06002729
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002730static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002731{
Jens Axboe355afae2020-09-02 09:30:31 -06002732 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002733 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002734
Jens Axboe355afae2020-09-02 09:30:31 -06002735 if (!S_ISBLK(mode) && !S_ISREG(mode))
2736 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002737 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2738 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002739 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002740 /*
2741 * If ref is dying, we might be running poll reap from the exit work.
2742 * Don't attempt to reissue from that path, just let it fail with
2743 * -EAGAIN.
2744 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002745 if (percpu_ref_is_dying(&ctx->refs))
2746 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002747 /*
2748 * Play it safe and assume not safe to re-import and reissue if we're
2749 * not in the original thread group (or in task context).
2750 */
2751 if (!same_thread_group(req->task, current) || !in_task())
2752 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002753 return true;
2754}
Jens Axboee82ad482021-04-02 19:45:34 -06002755#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002756static bool io_resubmit_prep(struct io_kiocb *req)
2757{
2758 return false;
2759}
Jens Axboee82ad482021-04-02 19:45:34 -06002760static bool io_rw_should_reissue(struct io_kiocb *req)
2761{
2762 return false;
2763}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002764#endif
2765
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002766static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002767{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002768 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2769 kiocb_end_write(req);
Pavel Begunkov258f3a72021-10-14 16:10:14 +01002770 if (unlikely(res != req->result)) {
Pavel Begunkov9532b992021-03-22 01:58:34 +00002771 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2772 io_rw_should_reissue(req)) {
2773 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002774 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002775 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002776 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002777 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002778 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002779 return false;
2780}
2781
Pavel Begunkovf237c302021-08-18 12:42:46 +01002782static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002783{
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00002784 unsigned int cflags = io_put_kbuf(req);
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01002785 int res = req->result;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002786
2787 if (*locked) {
Pavel Begunkov126180b2021-08-18 12:42:47 +01002788 io_req_complete_state(req, res, cflags);
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002789 io_req_add_compl_list(req);
Pavel Begunkov126180b2021-08-18 12:42:47 +01002790 } else {
2791 io_req_complete_post(req, res, cflags);
2792 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002793}
2794
2795static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2796 unsigned int issue_flags)
2797{
2798 if (__io_complete_rw_common(req, res))
2799 return;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00002800 __io_req_complete(req, issue_flags, req->result, io_put_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002801}
2802
Jens Axboe6b19b762021-10-21 09:22:35 -06002803static void io_complete_rw(struct kiocb *kiocb, long res)
Jens Axboeba816ad2019-09-28 11:36:45 -06002804{
Jens Axboe9adbd452019-12-20 08:45:55 -07002805 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002806
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002807 if (__io_complete_rw_common(req, res))
2808 return;
2809 req->result = res;
2810 req->io_task_work.func = io_req_task_complete;
Hao Xuf28c240e2021-12-08 13:21:25 +08002811 io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL));
Jens Axboe2b188cc2019-01-07 10:46:33 -07002812}
2813
Jens Axboe6b19b762021-10-21 09:22:35 -06002814static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
Jens Axboedef596e2019-01-09 08:59:42 -07002815{
Jens Axboe9adbd452019-12-20 08:45:55 -07002816 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002817
Jens Axboe491381ce2019-10-17 09:20:46 -06002818 if (kiocb->ki_flags & IOCB_WRITE)
2819 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002820 if (unlikely(res != req->result)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002821 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2822 req->flags |= REQ_F_REISSUE;
2823 return;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002824 }
Pavel Begunkov258f3a72021-10-14 16:10:14 +01002825 req->result = res;
Pavel Begunkov8c130822021-03-22 01:58:32 +00002826 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002827
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002828 /* order with io_iopoll_complete() checking ->iopoll_completed */
2829 smp_store_release(&req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002830}
2831
2832/*
2833 * After the iocb has been issued, it's safe to be found on the poll list.
2834 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002835 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002836 * accessing the kiocb cookie.
2837 */
Pavel Begunkov98821312021-10-15 17:09:12 +01002838static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboedef596e2019-01-09 08:59:42 -07002839{
2840 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08002841 const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002842
2843 /* workqueue context doesn't hold uring_lock, grab it now */
Hao Xu3b44b372021-10-18 21:34:31 +08002844 if (unlikely(needs_lock))
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002845 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002846
2847 /*
2848 * Track whether we have multiple files in our lists. This will impact
2849 * how we do polling eventually, not spinning if we're on potentially
2850 * different devices.
2851 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002852 if (wq_list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002853 ctx->poll_multi_queue = false;
2854 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002855 struct io_kiocb *list_req;
2856
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002857 list_req = container_of(ctx->iopoll_list.first, struct io_kiocb,
2858 comp_list);
Christoph Hellwig30da1b42021-10-12 13:12:14 +02002859 if (list_req->file != req->file)
Hao Xu915b3dd2021-06-28 05:37:30 +08002860 ctx->poll_multi_queue = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002861 }
2862
2863 /*
2864 * For fast devices, IO may have already completed. If it has, add
2865 * it to the front so we find it first.
2866 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002867 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002868 wq_list_add_head(&req->comp_list, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002869 else
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002870 wq_list_add_tail(&req->comp_list, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002871
Hao Xu3b44b372021-10-18 21:34:31 +08002872 if (unlikely(needs_lock)) {
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002873 /*
2874 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2875 * in sq thread task context or in io worker task context. If
2876 * current task context is sq thread, we don't need to check
2877 * whether should wake up sq thread.
2878 */
2879 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2880 wq_has_sleeper(&ctx->sq_data->wait))
2881 wake_up(&ctx->sq_data->wait);
2882
2883 mutex_unlock(&ctx->uring_lock);
2884 }
Jens Axboedef596e2019-01-09 08:59:42 -07002885}
2886
Jens Axboe4503b762020-06-01 10:00:27 -06002887static bool io_bdev_nowait(struct block_device *bdev)
2888{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002889 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002890}
2891
Jens Axboe2b188cc2019-01-07 10:46:33 -07002892/*
2893 * If we tracked the file through the SCM inflight mechanism, we could support
2894 * any file. For now, just ensure that anything potentially problematic is done
2895 * inline.
2896 */
Pavel Begunkov88459b52021-10-17 00:07:10 +01002897static bool __io_file_supports_nowait(struct file *file, umode_t mode)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002898{
Jens Axboe4503b762020-06-01 10:00:27 -06002899 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002900 if (IS_ENABLED(CONFIG_BLOCK) &&
2901 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002902 return true;
2903 return false;
2904 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002905 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002906 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002907 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002908 if (IS_ENABLED(CONFIG_BLOCK) &&
2909 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002910 file->f_op != &io_uring_fops)
2911 return true;
2912 return false;
2913 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002914
Jens Axboec5b85622020-06-09 19:23:05 -06002915 /* any ->read/write should understand O_NONBLOCK */
2916 if (file->f_flags & O_NONBLOCK)
2917 return true;
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002918 return file->f_mode & FMODE_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002919}
2920
Pavel Begunkov88459b52021-10-17 00:07:10 +01002921/*
2922 * If we tracked the file through the SCM inflight mechanism, we could support
2923 * any file. For now, just ensure that anything potentially problematic is done
2924 * inline.
2925 */
2926static unsigned int io_file_get_flags(struct file *file)
Jens Axboe7b29f922021-03-12 08:30:14 -07002927{
Pavel Begunkov88459b52021-10-17 00:07:10 +01002928 umode_t mode = file_inode(file)->i_mode;
2929 unsigned int res = 0;
Jens Axboe7b29f922021-03-12 08:30:14 -07002930
Pavel Begunkov88459b52021-10-17 00:07:10 +01002931 if (S_ISREG(mode))
2932 res |= FFS_ISREG;
2933 if (__io_file_supports_nowait(file, mode))
2934 res |= FFS_NOWAIT;
2935 return res;
Jens Axboe7b29f922021-03-12 08:30:14 -07002936}
2937
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002938static inline bool io_file_supports_nowait(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939{
Pavel Begunkov88459b52021-10-17 00:07:10 +01002940 return req->flags & REQ_F_SUPPORT_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002941}
2942
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01002943static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002944{
Jens Axboedef596e2019-01-09 08:59:42 -07002945 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002946 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002947 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002948 unsigned ioprio;
2949 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002950
Pavel Begunkov88459b52021-10-17 00:07:10 +01002951 if (!io_req_ffs_set(req))
2952 req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT;
Jens Axboe491381ce2019-10-17 09:20:46 -06002953
Jens Axboe2b188cc2019-01-07 10:46:33 -07002954 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002955 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002956 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002957 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002958 }
Pavel Begunkov5cb03d62021-10-15 17:09:16 +01002959 kiocb->ki_flags = iocb_flags(file);
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002960 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2961 if (unlikely(ret))
2962 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002963
Jens Axboe5d329e12021-09-14 11:08:37 -06002964 /*
2965 * If the file is marked O_NONBLOCK, still allow retry for it if it
2966 * supports async. Otherwise it's impossible to use O_NONBLOCK files
2967 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2968 */
2969 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002970 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req)))
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002971 req->flags |= REQ_F_NOWAIT;
2972
Jens Axboedef596e2019-01-09 08:59:42 -07002973 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov5cb03d62021-10-15 17:09:16 +01002974 if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002975 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002976
Jens Axboe394918e2021-03-08 11:40:23 -07002977 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
Jens Axboedef596e2019-01-09 08:59:42 -07002978 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002979 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002980 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002981 if (kiocb->ki_flags & IOCB_HIPRI)
2982 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002983 kiocb->ki_complete = io_complete_rw;
2984 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002985
Pavel Begunkovfb272742021-10-23 12:14:02 +01002986 ioprio = READ_ONCE(sqe->ioprio);
2987 if (ioprio) {
2988 ret = ioprio_check_cap(ioprio);
2989 if (ret)
2990 return ret;
2991
2992 kiocb->ki_ioprio = ioprio;
2993 } else {
2994 kiocb->ki_ioprio = get_current_ioprio();
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002995 }
2996
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01002997 req->imu = NULL;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002998 req->rw.addr = READ_ONCE(sqe->addr);
2999 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003000 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003001 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003002}
3003
3004static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
3005{
3006 switch (ret) {
3007 case -EIOCBQUEUED:
3008 break;
3009 case -ERESTARTSYS:
3010 case -ERESTARTNOINTR:
3011 case -ERESTARTNOHAND:
3012 case -ERESTART_RESTARTBLOCK:
3013 /*
3014 * We can't just restart the syscall, since previously
3015 * submitted sqes may already be in progress. Just fail this
3016 * IO with EINTR.
3017 */
3018 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003019 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003020 default:
Jens Axboe6b19b762021-10-21 09:22:35 -06003021 kiocb->ki_complete(kiocb, ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003022 }
3023}
3024
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003025static void kiocb_done(struct io_kiocb *req, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00003026 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06003027{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003028 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07003029
Jens Axboe227c0c92020-08-13 11:51:40 -06003030 /* add previously done IO, if any */
Pavel Begunkovd886e182021-10-04 20:02:56 +01003031 if (req_has_async_data(req) && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06003032 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07003033 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003034 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07003035 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003036 }
3037
Jens Axboeba042912019-12-25 16:33:42 -07003038 if (req->flags & REQ_F_CUR_POS)
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003039 req->file->f_pos = req->rw.kiocb.ki_pos;
3040 if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw))
Pavel Begunkov889fca72021-02-10 00:03:09 +00003041 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06003042 else
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003043 io_rw_done(&req->rw.kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01003044
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01003045 if (req->flags & REQ_F_REISSUE) {
Pavel Begunkov97284632021-04-08 19:28:03 +01003046 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06003047 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06003048 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00003049 } else {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003050 req_set_fail(req);
Pavel Begunkov06bdea22021-11-23 00:07:46 +00003051 req->result = ret;
3052 req->io_task_work.func = io_req_task_complete;
Hao Xu4813c372021-12-07 17:39:48 +08003053 io_req_task_work_add(req, false);
Pavel Begunkov97284632021-04-08 19:28:03 +01003054 }
3055 }
Jens Axboeba816ad2019-09-28 11:36:45 -06003056}
3057
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003058static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
3059 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07003060{
Jens Axboe9adbd452019-12-20 08:45:55 -07003061 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01003062 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003063 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07003064
Pavel Begunkov75769e32021-04-01 15:43:54 +01003065 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07003066 return -EFAULT;
3067 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01003068 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07003069 return -EFAULT;
3070
3071 /*
3072 * May not be a start of buffer, set size appropriately
3073 * and advance us to the beginning.
3074 */
3075 offset = buf_addr - imu->ubuf;
3076 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003077
3078 if (offset) {
3079 /*
3080 * Don't use iov_iter_advance() here, as it's really slow for
3081 * using the latter parts of a big fixed buffer - it iterates
3082 * over each segment manually. We can cheat a bit here, because
3083 * we know that:
3084 *
3085 * 1) it's a BVEC iter, we set it up
3086 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3087 * first and last bvec
3088 *
3089 * So just find our index, and adjust the iterator afterwards.
3090 * If the offset is within the first bvec (or the whole first
3091 * bvec, just use iov_iter_advance(). This makes it easier
3092 * since we can just skip the first segment, which may not
3093 * be PAGE_SIZE aligned.
3094 */
3095 const struct bio_vec *bvec = imu->bvec;
3096
3097 if (offset <= bvec->bv_len) {
3098 iov_iter_advance(iter, offset);
3099 } else {
3100 unsigned long seg_skip;
3101
3102 /* skip first vec */
3103 offset -= bvec->bv_len;
3104 seg_skip = 1 + (offset >> PAGE_SHIFT);
3105
3106 iter->bvec = bvec + seg_skip;
3107 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003108 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003109 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003110 }
3111 }
3112
Pavel Begunkov847595d2021-02-04 13:52:06 +00003113 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003114}
3115
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003116static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3117{
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003118 struct io_mapped_ubuf *imu = req->imu;
3119 u16 index, buf_index = req->buf_index;
3120
3121 if (likely(!imu)) {
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003122 struct io_ring_ctx *ctx = req->ctx;
3123
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003124 if (unlikely(buf_index >= ctx->nr_user_bufs))
3125 return -EFAULT;
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003126 io_req_set_rsrc_node(req, ctx);
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003127 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3128 imu = READ_ONCE(ctx->user_bufs[index]);
3129 req->imu = imu;
3130 }
3131 return __io_import_fixed(req, rw, iter, imu);
3132}
3133
Jens Axboebcda7ba2020-02-23 16:42:51 -07003134static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3135{
3136 if (needs_lock)
3137 mutex_unlock(&ctx->uring_lock);
3138}
3139
3140static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3141{
3142 /*
3143 * "Normal" inline submissions always hold the uring_lock, since we
3144 * grab it from the system call. Same is true for the SQPOLL offload.
3145 * The only exception is when we've detached the request and issue it
3146 * from an async worker thread, grab the lock for that case.
3147 */
3148 if (needs_lock)
3149 mutex_lock(&ctx->uring_lock);
3150}
3151
3152static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003153 int bgid, unsigned int issue_flags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07003154{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003155 struct io_buffer *kbuf = req->kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003156 struct io_buffer *head;
Hao Xu3b44b372021-10-18 21:34:31 +08003157 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003158
3159 if (req->flags & REQ_F_BUFFER_SELECTED)
3160 return kbuf;
3161
3162 io_ring_submit_lock(req->ctx, needs_lock);
3163
3164 lockdep_assert_held(&req->ctx->uring_lock);
3165
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003166 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003167 if (head) {
3168 if (!list_empty(&head->list)) {
3169 kbuf = list_last_entry(&head->list, struct io_buffer,
3170 list);
3171 list_del(&kbuf->list);
3172 } else {
3173 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003174 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003175 }
3176 if (*len > kbuf->len)
3177 *len = kbuf->len;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003178 req->flags |= REQ_F_BUFFER_SELECTED;
3179 req->kbuf = kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003180 } else {
3181 kbuf = ERR_PTR(-ENOBUFS);
3182 }
3183
3184 io_ring_submit_unlock(req->ctx, needs_lock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003185 return kbuf;
3186}
3187
Jens Axboe4d954c22020-02-27 07:31:19 -07003188static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003189 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003190{
3191 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003192 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003193
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003194 bgid = req->buf_index;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003195 kbuf = io_buffer_select(req, len, bgid, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003196 if (IS_ERR(kbuf))
3197 return kbuf;
Jens Axboe4d954c22020-02-27 07:31:19 -07003198 return u64_to_user_ptr(kbuf->addr);
3199}
3200
3201#ifdef CONFIG_COMPAT
3202static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003203 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003204{
3205 struct compat_iovec __user *uiov;
3206 compat_ssize_t clen;
3207 void __user *buf;
3208 ssize_t len;
3209
3210 uiov = u64_to_user_ptr(req->rw.addr);
3211 if (!access_ok(uiov, sizeof(*uiov)))
3212 return -EFAULT;
3213 if (__get_user(clen, &uiov->iov_len))
3214 return -EFAULT;
3215 if (clen < 0)
3216 return -EINVAL;
3217
3218 len = clen;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003219 buf = io_rw_buffer_select(req, &len, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003220 if (IS_ERR(buf))
3221 return PTR_ERR(buf);
3222 iov[0].iov_base = buf;
3223 iov[0].iov_len = (compat_size_t) len;
3224 return 0;
3225}
3226#endif
3227
3228static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003229 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003230{
3231 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3232 void __user *buf;
3233 ssize_t len;
3234
3235 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3236 return -EFAULT;
3237
3238 len = iov[0].iov_len;
3239 if (len < 0)
3240 return -EINVAL;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003241 buf = io_rw_buffer_select(req, &len, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003242 if (IS_ERR(buf))
3243 return PTR_ERR(buf);
3244 iov[0].iov_base = buf;
3245 iov[0].iov_len = len;
3246 return 0;
3247}
3248
3249static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003250 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003251{
Jens Axboedddb3e22020-06-04 11:27:01 -06003252 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003253 struct io_buffer *kbuf = req->kbuf;
Jens Axboedddb3e22020-06-04 11:27:01 -06003254
Jens Axboedddb3e22020-06-04 11:27:01 -06003255 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3256 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003257 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003258 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003259 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003260 return -EINVAL;
3261
3262#ifdef CONFIG_COMPAT
3263 if (req->ctx->compat)
Pavel Begunkov51aac422021-10-14 16:10:17 +01003264 return io_compat_import(req, iov, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003265#endif
3266
Pavel Begunkov51aac422021-10-14 16:10:17 +01003267 return __io_iov_buffer_select(req, iov, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003268}
3269
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003270static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req,
3271 struct io_rw_state *s,
3272 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003273{
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003274 struct iov_iter *iter = &s->iter;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003275 u8 opcode = req->opcode;
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003276 struct iovec *iovec;
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003277 void __user *buf;
3278 size_t sqe_len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003279 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003280
Pavel Begunkovf3251182021-11-23 00:07:48 +00003281 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
3282 ret = io_import_fixed(req, rw, iter);
3283 if (ret)
3284 return ERR_PTR(ret);
3285 return NULL;
3286 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003287
Jens Axboebcda7ba2020-02-23 16:42:51 -07003288 /* buffer index only valid with fixed read/write, or buffer select */
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003289 if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)))
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003290 return ERR_PTR(-EINVAL);
Jens Axboe9adbd452019-12-20 08:45:55 -07003291
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003292 buf = u64_to_user_ptr(req->rw.addr);
3293 sqe_len = req->rw.len;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003294
Jens Axboe3a6820f2019-12-22 15:19:35 -07003295 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003296 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01003297 buf = io_rw_buffer_select(req, &sqe_len, issue_flags);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003298 if (IS_ERR(buf))
Changcheng Deng898df242021-10-20 08:49:48 +00003299 return ERR_CAST(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003300 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003301 }
3302
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003303 ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter);
Pavel Begunkovf3251182021-11-23 00:07:48 +00003304 if (ret)
3305 return ERR_PTR(ret);
3306 return NULL;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003307 }
3308
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003309 iovec = s->fast_iov;
Jens Axboe4d954c22020-02-27 07:31:19 -07003310 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003311 ret = io_iov_buffer_select(req, iovec, issue_flags);
Pavel Begunkovf3251182021-11-23 00:07:48 +00003312 if (ret)
3313 return ERR_PTR(ret);
3314 iov_iter_init(iter, rw, iovec, 1, iovec->iov_len);
3315 return NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07003316 }
3317
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003318 ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003319 req->ctx->compat);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003320 if (unlikely(ret < 0))
3321 return ERR_PTR(ret);
3322 return iovec;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003323}
3324
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003325static inline int io_import_iovec(int rw, struct io_kiocb *req,
3326 struct iovec **iovec, struct io_rw_state *s,
3327 unsigned int issue_flags)
3328{
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003329 *iovec = __io_import_iovec(rw, req, s, issue_flags);
3330 if (unlikely(IS_ERR(*iovec)))
3331 return PTR_ERR(*iovec);
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003332
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003333 iov_iter_save_state(&s->iter, &s->iter_state);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003334 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003335}
3336
Jens Axboe0fef9482020-08-26 10:36:20 -06003337static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3338{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003339 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003340}
3341
Jens Axboe32960612019-09-23 11:05:34 -06003342/*
3343 * For files that don't have ->read_iter() and ->write_iter(), handle them
3344 * by looping over ->read() or ->write() manually.
3345 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003346static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003347{
Jens Axboe4017eb92020-10-22 14:14:12 -06003348 struct kiocb *kiocb = &req->rw.kiocb;
3349 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003350 ssize_t ret = 0;
3351
3352 /*
3353 * Don't support polled IO through this interface, and we can't
3354 * support non-blocking either. For the latter, this just causes
3355 * the kiocb to be handled from an async context.
3356 */
3357 if (kiocb->ki_flags & IOCB_HIPRI)
3358 return -EOPNOTSUPP;
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003359 if ((kiocb->ki_flags & IOCB_NOWAIT) &&
3360 !(kiocb->ki_filp->f_flags & O_NONBLOCK))
Jens Axboe32960612019-09-23 11:05:34 -06003361 return -EAGAIN;
3362
3363 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003364 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003365 ssize_t nr;
3366
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003367 if (!iov_iter_is_bvec(iter)) {
3368 iovec = iov_iter_iovec(iter);
3369 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003370 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3371 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003372 }
3373
Jens Axboe32960612019-09-23 11:05:34 -06003374 if (rw == READ) {
3375 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003376 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003377 } else {
3378 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003379 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003380 }
3381
3382 if (nr < 0) {
3383 if (!ret)
3384 ret = nr;
3385 break;
3386 }
Jens Axboe16c8d2d2021-09-12 06:45:07 -06003387 if (!iov_iter_is_bvec(iter)) {
3388 iov_iter_advance(iter, nr);
3389 } else {
3390 req->rw.len -= nr;
3391 req->rw.addr += nr;
3392 }
Jens Axboe32960612019-09-23 11:05:34 -06003393 ret += nr;
3394 if (nr != iovec.iov_len)
3395 break;
Jens Axboe32960612019-09-23 11:05:34 -06003396 }
3397
3398 return ret;
3399}
3400
Jens Axboeff6165b2020-08-13 09:47:43 -06003401static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3402 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003403{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003404 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003405
Pavel Begunkov538941e2021-10-14 16:10:15 +01003406 memcpy(&rw->s.iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003407 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003408 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003409 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003410 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003411 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003412 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003413 unsigned iov_off = 0;
3414
Pavel Begunkov538941e2021-10-14 16:10:15 +01003415 rw->s.iter.iov = rw->s.fast_iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003416 if (iter->iov != fast_iov) {
3417 iov_off = iter->iov - fast_iov;
Pavel Begunkov538941e2021-10-14 16:10:15 +01003418 rw->s.iter.iov += iov_off;
Jens Axboeff6165b2020-08-13 09:47:43 -06003419 }
Pavel Begunkov538941e2021-10-14 16:10:15 +01003420 if (rw->s.fast_iov != fast_iov)
3421 memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003422 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003423 } else {
3424 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003425 }
3426}
3427
Hao Xu8d4af682021-09-22 18:15:22 +08003428static inline bool io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003429{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003430 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3431 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
Pavel Begunkovd886e182021-10-04 20:02:56 +01003432 if (req->async_data) {
3433 req->flags |= REQ_F_ASYNC_DATA;
3434 return false;
3435 }
3436 return true;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003437}
3438
Jens Axboeff6165b2020-08-13 09:47:43 -06003439static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003440 struct io_rw_state *s, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003441{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003442 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003443 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003444 if (!req_has_async_data(req)) {
Jens Axboecd658692021-09-10 11:19:14 -06003445 struct io_async_rw *iorw;
3446
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003447 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003448 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003449 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003450 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003451
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003452 io_req_map_rw(req, iovec, s->fast_iov, &s->iter);
Jens Axboecd658692021-09-10 11:19:14 -06003453 iorw = req->async_data;
3454 /* we've copied and mapped the iter, ensure state is saved */
Pavel Begunkov538941e2021-10-14 16:10:15 +01003455 iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003456 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003457 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003458}
3459
Pavel Begunkov73debe62020-09-30 22:57:54 +03003460static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003461{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003462 struct io_async_rw *iorw = req->async_data;
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003463 struct iovec *iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003464 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003465
Pavel Begunkov51aac422021-10-14 16:10:17 +01003466 /* submission path, ->uring_lock should already be taken */
Hao Xu3b44b372021-10-18 21:34:31 +08003467 ret = io_import_iovec(rw, req, &iov, &iorw->s, 0);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003468 if (unlikely(ret < 0))
3469 return ret;
3470
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003471 iorw->bytes_done = 0;
3472 iorw->free_iovec = iov;
3473 if (iov)
3474 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003475 return 0;
3476}
3477
Pavel Begunkov73debe62020-09-30 22:57:54 +03003478static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003479{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003480 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3481 return -EBADF;
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01003482 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003483}
3484
Jens Axboec1dd91d2020-08-03 16:43:59 -06003485/*
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05003486 * This is our waitqueue callback handler, registered through __folio_lock_async()
Jens Axboec1dd91d2020-08-03 16:43:59 -06003487 * when we initially tried to do the IO with the iocb armed our waitqueue.
3488 * This gets called when the page is unlocked, and we generally expect that to
3489 * happen when the page IO is completed and the page is now uptodate. This will
3490 * queue a task_work based retry of the operation, attempting to copy the data
3491 * again. If the latter fails because the page was NOT uptodate, then we will
3492 * do a thread based blocking retry of the operation. That's the unexpected
3493 * slow path.
3494 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003495static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3496 int sync, void *arg)
3497{
3498 struct wait_page_queue *wpq;
3499 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003500 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003501
3502 wpq = container_of(wait, struct wait_page_queue, wait);
3503
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003504 if (!wake_page_match(wpq, key))
3505 return 0;
3506
Hao Xuc8d317a2020-09-29 20:00:45 +08003507 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003508 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003509 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003510 return 1;
3511}
3512
Jens Axboec1dd91d2020-08-03 16:43:59 -06003513/*
3514 * This controls whether a given IO request should be armed for async page
3515 * based retry. If we return false here, the request is handed to the async
3516 * worker threads for retry. If we're doing buffered reads on a regular file,
3517 * we prepare a private wait_page_queue entry and retry the operation. This
3518 * will either succeed because the page is now uptodate and unlocked, or it
3519 * will register a callback when the page is unlocked at IO completion. Through
3520 * that callback, io_uring uses task_work to setup a retry of the operation.
3521 * That retry will attempt the buffered read again. The retry will generally
3522 * succeed, or in rare cases where it fails, we then fall back to using the
3523 * async worker threads for a blocking retry.
3524 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003525static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003526{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003527 struct io_async_rw *rw = req->async_data;
3528 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003529 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003530
3531 /* never retry for NOWAIT, we just complete with -EAGAIN */
3532 if (req->flags & REQ_F_NOWAIT)
3533 return false;
3534
Jens Axboe227c0c92020-08-13 11:51:40 -06003535 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003536 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003537 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003538
Jens Axboebcf5a062020-05-22 09:24:42 -06003539 /*
3540 * just use poll if we can, and don't attempt if the fs doesn't
3541 * support callback based unlocks
3542 */
3543 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3544 return false;
3545
Jens Axboe3b2a4432020-08-16 10:58:43 -07003546 wait->wait.func = io_async_buf_func;
3547 wait->wait.private = req;
3548 wait->wait.flags = 0;
3549 INIT_LIST_HEAD(&wait->wait.entry);
3550 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003551 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003552 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003553 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003554}
3555
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003556static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003557{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003558 if (likely(req->file->f_op->read_iter))
Jens Axboebcf5a062020-05-22 09:24:42 -06003559 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003560 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003561 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003562 else
3563 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003564}
3565
Ming Lei7db30432021-08-21 23:07:51 +08003566static bool need_read_all(struct io_kiocb *req)
3567{
3568 return req->flags & REQ_F_ISREG ||
3569 S_ISBLK(file_inode(req->file)->i_mode);
3570}
3571
Pavel Begunkov889fca72021-02-10 00:03:09 +00003572static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003573{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003574 struct io_rw_state __s, *s = &__s;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003575 struct iovec *iovec;
Jens Axboe9adbd452019-12-20 08:45:55 -07003576 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003577 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003578 struct io_async_rw *rw;
Jens Axboecd658692021-09-10 11:19:14 -06003579 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003580
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003581 if (!req_has_async_data(req)) {
3582 ret = io_import_iovec(READ, req, &iovec, s, issue_flags);
3583 if (unlikely(ret < 0))
3584 return ret;
3585 } else {
Pavel Begunkovd886e182021-10-04 20:02:56 +01003586 rw = req->async_data;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003587 s = &rw->s;
Jens Axboecd658692021-09-10 11:19:14 -06003588 /*
3589 * We come here from an earlier attempt, restore our state to
3590 * match in case it doesn't. It's cheap enough that we don't
3591 * need to make this conditional.
3592 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003593 iov_iter_restore(&s->iter, &s->iter_state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003594 iovec = NULL;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003595 }
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003596 req->result = iov_iter_count(&s->iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003597
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003598 if (force_nonblock) {
3599 /* If the file doesn't support async, just async punt */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003600 if (unlikely(!io_file_supports_nowait(req))) {
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003601 ret = io_setup_async_rw(req, iovec, s, true);
3602 return ret ?: -EAGAIN;
3603 }
Pavel Begunkova88fc402020-09-30 22:57:53 +03003604 kiocb->ki_flags |= IOCB_NOWAIT;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003605 } else {
3606 /* Ensure we clear previously set non-block flag */
3607 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003608 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003609
Jens Axboecd658692021-09-10 11:19:14 -06003610 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003611 if (unlikely(ret)) {
3612 kfree(iovec);
3613 return ret;
3614 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003615
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003616 ret = io_iter_do_read(req, &s->iter);
Jens Axboe32960612019-09-23 11:05:34 -06003617
Jens Axboe230d50d2021-04-01 20:41:15 -06003618 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003619 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003620 /* IOPOLL retry should happen for io-wq threads */
3621 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003622 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003623 /* no retry on NONBLOCK nor RWF_NOWAIT */
3624 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003625 goto done;
Jens Axboef38c7e32020-09-25 15:23:43 -06003626 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003627 } else if (ret == -EIOCBQUEUED) {
3628 goto out_free;
Pavel Begunkovf80a50a2021-10-14 16:10:13 +01003629 } else if (ret == req->result || ret <= 0 || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003630 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003631 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003632 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003633 }
3634
Jens Axboecd658692021-09-10 11:19:14 -06003635 /*
3636 * Don't depend on the iter state matching what was consumed, or being
3637 * untouched in case of error. Restore it and we'll advance it
3638 * manually if we need to.
3639 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003640 iov_iter_restore(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003641
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003642 ret2 = io_setup_async_rw(req, iovec, s, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003643 if (ret2)
3644 return ret2;
3645
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003646 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003647 rw = req->async_data;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003648 s = &rw->s;
Jens Axboecd658692021-09-10 11:19:14 -06003649 /*
3650 * Now use our persistent iterator and state, if we aren't already.
3651 * We've restored and mapped the iter to match.
3652 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003653
Pavel Begunkovb23df912021-02-04 13:52:04 +00003654 do {
Jens Axboecd658692021-09-10 11:19:14 -06003655 /*
3656 * We end up here because of a partial read, either from
3657 * above or inside this loop. Advance the iter by the bytes
3658 * that were consumed.
3659 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003660 iov_iter_advance(&s->iter, ret);
3661 if (!iov_iter_count(&s->iter))
Jens Axboecd658692021-09-10 11:19:14 -06003662 break;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003663 rw->bytes_done += ret;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003664 iov_iter_save_state(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003665
Pavel Begunkovb23df912021-02-04 13:52:04 +00003666 /* if we can retry, do so with the callbacks armed */
3667 if (!io_rw_should_retry(req)) {
3668 kiocb->ki_flags &= ~IOCB_WAITQ;
3669 return -EAGAIN;
3670 }
3671
3672 /*
3673 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3674 * we get -EIOCBQUEUED, then we'll get a notification when the
3675 * desired page gets unlocked. We can also get a partial read
3676 * here, and if we do, then just retry at the new offset.
3677 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003678 ret = io_iter_do_read(req, &s->iter);
Pavel Begunkovb23df912021-02-04 13:52:04 +00003679 if (ret == -EIOCBQUEUED)
3680 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003681 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003682 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003683 iov_iter_restore(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003684 } while (ret > 0);
Jens Axboe227c0c92020-08-13 11:51:40 -06003685done:
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003686 kiocb_done(req, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003687out_free:
3688 /* it's faster to check here then delegate to kfree */
3689 if (iovec)
3690 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003691 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003692}
3693
Pavel Begunkov73debe62020-09-30 22:57:54 +03003694static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003695{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003696 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3697 return -EBADF;
Jens Axboe3884b832021-10-25 13:45:12 -06003698 req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file));
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01003699 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003700}
3701
Pavel Begunkov889fca72021-02-10 00:03:09 +00003702static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003703{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003704 struct io_rw_state __s, *s = &__s;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003705 struct iovec *iovec;
Jens Axboe9adbd452019-12-20 08:45:55 -07003706 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003707 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003708 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003709
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003710 if (!req_has_async_data(req)) {
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003711 ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags);
3712 if (unlikely(ret < 0))
Pavel Begunkov2846c482020-11-07 13:16:27 +00003713 return ret;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003714 } else {
3715 struct io_async_rw *rw = req->async_data;
3716
3717 s = &rw->s;
3718 iov_iter_restore(&s->iter, &s->iter_state);
3719 iovec = NULL;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003720 }
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003721 req->result = iov_iter_count(&s->iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003722
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003723 if (force_nonblock) {
3724 /* If the file doesn't support async, just async punt */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003725 if (unlikely(!io_file_supports_nowait(req)))
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003726 goto copy_iov;
3727
3728 /* file path doesn't support NOWAIT for non-direct_IO */
3729 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3730 (req->flags & REQ_F_ISREG))
3731 goto copy_iov;
3732
Pavel Begunkova88fc402020-09-30 22:57:53 +03003733 kiocb->ki_flags |= IOCB_NOWAIT;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003734 } else {
3735 /* Ensure we clear previously set non-block flag */
3736 kiocb->ki_flags &= ~IOCB_NOWAIT;
3737 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003738
Jens Axboecd658692021-09-10 11:19:14 -06003739 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003740 if (unlikely(ret))
3741 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003742
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003743 /*
3744 * Open-code file_start_write here to grab freeze protection,
3745 * which will be released by another thread in
3746 * io_complete_rw(). Fool lockdep by telling it the lock got
3747 * released so that it doesn't complain about the held lock when
3748 * we return to userspace.
3749 */
3750 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003751 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003752 __sb_writers_release(file_inode(req->file)->i_sb,
3753 SB_FREEZE_WRITE);
3754 }
3755 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003756
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003757 if (likely(req->file->f_op->write_iter))
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003758 ret2 = call_write_iter(req->file, kiocb, &s->iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003759 else if (req->file->f_op->write)
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003760 ret2 = loop_rw_iter(WRITE, req, &s->iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003761 else
3762 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003763
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003764 if (req->flags & REQ_F_REISSUE) {
3765 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003766 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003767 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003768
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003769 /*
3770 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3771 * retry them without IOCB_NOWAIT.
3772 */
3773 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3774 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003775 /* no retry on NONBLOCK nor RWF_NOWAIT */
3776 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003777 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003778 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003779 /* IOPOLL retry should happen for io-wq threads */
Noah Goldsteinb10841c2021-10-16 20:32:29 -05003780 if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboeeefdf302020-08-27 16:40:19 -06003781 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003782done:
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003783 kiocb_done(req, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003784 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003785copy_iov:
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003786 iov_iter_restore(&s->iter, &s->iter_state);
3787 ret = io_setup_async_rw(req, iovec, s, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003788 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003789 }
Jens Axboe31b51512019-01-18 22:56:34 -07003790out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003791 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003792 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003793 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003794 return ret;
3795}
3796
Jens Axboe80a261f2020-09-28 14:23:58 -06003797static int io_renameat_prep(struct io_kiocb *req,
3798 const struct io_uring_sqe *sqe)
3799{
3800 struct io_rename *ren = &req->rename;
3801 const char __user *oldf, *newf;
3802
Jens Axboeed7eb252021-06-23 09:04:13 -06003803 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3804 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003805 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003806 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003807 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3808 return -EBADF;
3809
3810 ren->old_dfd = READ_ONCE(sqe->fd);
3811 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3812 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3813 ren->new_dfd = READ_ONCE(sqe->len);
3814 ren->flags = READ_ONCE(sqe->rename_flags);
3815
3816 ren->oldpath = getname(oldf);
3817 if (IS_ERR(ren->oldpath))
3818 return PTR_ERR(ren->oldpath);
3819
3820 ren->newpath = getname(newf);
3821 if (IS_ERR(ren->newpath)) {
3822 putname(ren->oldpath);
3823 return PTR_ERR(ren->newpath);
3824 }
3825
3826 req->flags |= REQ_F_NEED_CLEANUP;
3827 return 0;
3828}
3829
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003830static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003831{
3832 struct io_rename *ren = &req->rename;
3833 int ret;
3834
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003835 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003836 return -EAGAIN;
3837
3838 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3839 ren->newpath, ren->flags);
3840
3841 req->flags &= ~REQ_F_NEED_CLEANUP;
3842 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003843 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003844 io_req_complete(req, ret);
3845 return 0;
3846}
3847
Jens Axboe14a11432020-09-28 14:27:37 -06003848static int io_unlinkat_prep(struct io_kiocb *req,
3849 const struct io_uring_sqe *sqe)
3850{
3851 struct io_unlink *un = &req->unlink;
3852 const char __user *fname;
3853
Jens Axboe22634bc2021-06-23 09:07:45 -06003854 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3855 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003856 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3857 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003858 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003859 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3860 return -EBADF;
3861
3862 un->dfd = READ_ONCE(sqe->fd);
3863
3864 un->flags = READ_ONCE(sqe->unlink_flags);
3865 if (un->flags & ~AT_REMOVEDIR)
3866 return -EINVAL;
3867
3868 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3869 un->filename = getname(fname);
3870 if (IS_ERR(un->filename))
3871 return PTR_ERR(un->filename);
3872
3873 req->flags |= REQ_F_NEED_CLEANUP;
3874 return 0;
3875}
3876
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003877static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003878{
3879 struct io_unlink *un = &req->unlink;
3880 int ret;
3881
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003882 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003883 return -EAGAIN;
3884
3885 if (un->flags & AT_REMOVEDIR)
3886 ret = do_rmdir(un->dfd, un->filename);
3887 else
3888 ret = do_unlinkat(un->dfd, un->filename);
3889
3890 req->flags &= ~REQ_F_NEED_CLEANUP;
3891 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003892 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003893 io_req_complete(req, ret);
3894 return 0;
3895}
3896
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003897static int io_mkdirat_prep(struct io_kiocb *req,
3898 const struct io_uring_sqe *sqe)
3899{
3900 struct io_mkdir *mkd = &req->mkdir;
3901 const char __user *fname;
3902
3903 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3904 return -EINVAL;
3905 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3906 sqe->splice_fd_in)
3907 return -EINVAL;
3908 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3909 return -EBADF;
3910
3911 mkd->dfd = READ_ONCE(sqe->fd);
3912 mkd->mode = READ_ONCE(sqe->len);
3913
3914 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3915 mkd->filename = getname(fname);
3916 if (IS_ERR(mkd->filename))
3917 return PTR_ERR(mkd->filename);
3918
3919 req->flags |= REQ_F_NEED_CLEANUP;
3920 return 0;
3921}
3922
Pavel Begunkov04f34082021-10-14 16:10:12 +01003923static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003924{
3925 struct io_mkdir *mkd = &req->mkdir;
3926 int ret;
3927
3928 if (issue_flags & IO_URING_F_NONBLOCK)
3929 return -EAGAIN;
3930
3931 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3932
3933 req->flags &= ~REQ_F_NEED_CLEANUP;
3934 if (ret < 0)
3935 req_set_fail(req);
3936 io_req_complete(req, ret);
3937 return 0;
3938}
3939
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003940static int io_symlinkat_prep(struct io_kiocb *req,
3941 const struct io_uring_sqe *sqe)
3942{
3943 struct io_symlink *sl = &req->symlink;
3944 const char __user *oldpath, *newpath;
3945
3946 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3947 return -EINVAL;
3948 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3949 sqe->splice_fd_in)
3950 return -EINVAL;
3951 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3952 return -EBADF;
3953
3954 sl->new_dfd = READ_ONCE(sqe->fd);
3955 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3956 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3957
3958 sl->oldpath = getname(oldpath);
3959 if (IS_ERR(sl->oldpath))
3960 return PTR_ERR(sl->oldpath);
3961
3962 sl->newpath = getname(newpath);
3963 if (IS_ERR(sl->newpath)) {
3964 putname(sl->oldpath);
3965 return PTR_ERR(sl->newpath);
3966 }
3967
3968 req->flags |= REQ_F_NEED_CLEANUP;
3969 return 0;
3970}
3971
Pavel Begunkov04f34082021-10-14 16:10:12 +01003972static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003973{
3974 struct io_symlink *sl = &req->symlink;
3975 int ret;
3976
3977 if (issue_flags & IO_URING_F_NONBLOCK)
3978 return -EAGAIN;
3979
3980 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3981
3982 req->flags &= ~REQ_F_NEED_CLEANUP;
3983 if (ret < 0)
3984 req_set_fail(req);
3985 io_req_complete(req, ret);
3986 return 0;
3987}
3988
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003989static int io_linkat_prep(struct io_kiocb *req,
3990 const struct io_uring_sqe *sqe)
3991{
3992 struct io_hardlink *lnk = &req->hardlink;
3993 const char __user *oldf, *newf;
3994
3995 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3996 return -EINVAL;
3997 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3998 return -EINVAL;
3999 if (unlikely(req->flags & REQ_F_FIXED_FILE))
4000 return -EBADF;
4001
4002 lnk->old_dfd = READ_ONCE(sqe->fd);
4003 lnk->new_dfd = READ_ONCE(sqe->len);
4004 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
4005 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4006 lnk->flags = READ_ONCE(sqe->hardlink_flags);
4007
4008 lnk->oldpath = getname(oldf);
4009 if (IS_ERR(lnk->oldpath))
4010 return PTR_ERR(lnk->oldpath);
4011
4012 lnk->newpath = getname(newf);
4013 if (IS_ERR(lnk->newpath)) {
4014 putname(lnk->oldpath);
4015 return PTR_ERR(lnk->newpath);
4016 }
4017
4018 req->flags |= REQ_F_NEED_CLEANUP;
4019 return 0;
4020}
4021
Pavel Begunkov04f34082021-10-14 16:10:12 +01004022static int io_linkat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07004023{
4024 struct io_hardlink *lnk = &req->hardlink;
4025 int ret;
4026
4027 if (issue_flags & IO_URING_F_NONBLOCK)
4028 return -EAGAIN;
4029
4030 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
4031 lnk->newpath, lnk->flags);
4032
4033 req->flags &= ~REQ_F_NEED_CLEANUP;
4034 if (ret < 0)
4035 req_set_fail(req);
4036 io_req_complete(req, ret);
4037 return 0;
4038}
4039
Jens Axboe36f4fa62020-09-05 11:14:22 -06004040static int io_shutdown_prep(struct io_kiocb *req,
4041 const struct io_uring_sqe *sqe)
4042{
4043#if defined(CONFIG_NET)
4044 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4045 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004046 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
4047 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06004048 return -EINVAL;
4049
4050 req->shutdown.how = READ_ONCE(sqe->len);
4051 return 0;
4052#else
4053 return -EOPNOTSUPP;
4054#endif
4055}
4056
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004057static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06004058{
4059#if defined(CONFIG_NET)
4060 struct socket *sock;
4061 int ret;
4062
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004063 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06004064 return -EAGAIN;
4065
Linus Torvalds48aba792020-12-16 12:44:05 -08004066 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06004067 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08004068 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06004069
4070 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07004071 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004072 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06004073 io_req_complete(req, ret);
4074 return 0;
4075#else
4076 return -EOPNOTSUPP;
4077#endif
4078}
4079
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004080static int __io_splice_prep(struct io_kiocb *req,
4081 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004082{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004083 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004084 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004085
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004086 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4087 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004088
4089 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004090 sp->len = READ_ONCE(sqe->len);
4091 sp->flags = READ_ONCE(sqe->splice_flags);
4092
4093 if (unlikely(sp->flags & ~valid_flags))
4094 return -EINVAL;
4095
Pavel Begunkov62906e82021-08-10 14:52:47 +01004096 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01004097 (sp->flags & SPLICE_F_FD_IN_FIXED));
4098 if (!sp->file_in)
4099 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004100 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004101 return 0;
4102}
4103
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004104static int io_tee_prep(struct io_kiocb *req,
4105 const struct io_uring_sqe *sqe)
4106{
4107 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
4108 return -EINVAL;
4109 return __io_splice_prep(req, sqe);
4110}
4111
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004112static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004113{
4114 struct io_splice *sp = &req->splice;
4115 struct file *in = sp->file_in;
4116 struct file *out = sp->file_out;
4117 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4118 long ret = 0;
4119
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004120 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004121 return -EAGAIN;
4122 if (sp->len)
4123 ret = do_tee(in, out, sp->len, flags);
4124
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004125 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4126 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004127 req->flags &= ~REQ_F_NEED_CLEANUP;
4128
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004129 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004130 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004131 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004132 return 0;
4133}
4134
4135static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4136{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004137 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004138
4139 sp->off_in = READ_ONCE(sqe->splice_off_in);
4140 sp->off_out = READ_ONCE(sqe->off);
4141 return __io_splice_prep(req, sqe);
4142}
4143
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004144static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004145{
4146 struct io_splice *sp = &req->splice;
4147 struct file *in = sp->file_in;
4148 struct file *out = sp->file_out;
4149 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4150 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004151 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004152
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004153 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004154 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004155
4156 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4157 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004158
Jens Axboe948a7742020-05-17 14:21:38 -06004159 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004160 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004161
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004162 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4163 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004164 req->flags &= ~REQ_F_NEED_CLEANUP;
4165
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004166 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004167 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004168 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004169 return 0;
4170}
4171
Jens Axboe2b188cc2019-01-07 10:46:33 -07004172/*
4173 * IORING_OP_NOP just posts a completion event, nothing else.
4174 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004175static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004176{
4177 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004178
Jens Axboedef596e2019-01-09 08:59:42 -07004179 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4180 return -EINVAL;
4181
Pavel Begunkov889fca72021-02-10 00:03:09 +00004182 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004183 return 0;
4184}
4185
Pavel Begunkov1155c762021-02-18 18:29:38 +00004186static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004187{
Jens Axboe6b063142019-01-10 22:13:58 -07004188 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004189
Jens Axboe09bb8392019-03-13 12:39:28 -06004190 if (!req->file)
4191 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004192
Jens Axboe6b063142019-01-10 22:13:58 -07004193 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004194 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004195 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4196 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004197 return -EINVAL;
4198
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004199 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4200 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4201 return -EINVAL;
4202
4203 req->sync.off = READ_ONCE(sqe->off);
4204 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004205 return 0;
4206}
4207
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004208static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004209{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004210 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004211 int ret;
4212
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004213 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004214 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004215 return -EAGAIN;
4216
Jens Axboe9adbd452019-12-20 08:45:55 -07004217 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004218 end > 0 ? end : LLONG_MAX,
4219 req->sync.flags & IORING_FSYNC_DATASYNC);
4220 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004221 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004222 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004223 return 0;
4224}
4225
Jens Axboed63d1b52019-12-10 10:38:56 -07004226static int io_fallocate_prep(struct io_kiocb *req,
4227 const struct io_uring_sqe *sqe)
4228{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004229 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4230 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004231 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004232 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4233 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004234
4235 req->sync.off = READ_ONCE(sqe->off);
4236 req->sync.len = READ_ONCE(sqe->addr);
4237 req->sync.mode = READ_ONCE(sqe->len);
4238 return 0;
4239}
4240
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004241static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004242{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004243 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004244
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004245 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004246 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004247 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004248 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4249 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004250 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004251 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004252 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004253 return 0;
4254}
4255
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004256static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004257{
Jens Axboef8748882020-01-08 17:47:02 -07004258 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004259 int ret;
4260
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004261 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4262 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004263 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004264 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004265 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004266 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004267
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004268 /* open.how should be already initialised */
4269 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004270 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004271
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004272 req->open.dfd = READ_ONCE(sqe->fd);
4273 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004274 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004275 if (IS_ERR(req->open.filename)) {
4276 ret = PTR_ERR(req->open.filename);
4277 req->open.filename = NULL;
4278 return ret;
4279 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004280
4281 req->open.file_slot = READ_ONCE(sqe->file_index);
4282 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4283 return -EINVAL;
4284
Jens Axboe4022e7a2020-03-19 19:23:18 -06004285 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004286 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004287 return 0;
4288}
4289
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004290static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4291{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004292 u64 mode = READ_ONCE(sqe->len);
4293 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004294
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004295 req->open.how = build_open_how(flags, mode);
4296 return __io_openat_prep(req, sqe);
4297}
4298
Jens Axboecebdb982020-01-08 17:59:24 -07004299static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4300{
4301 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004302 size_t len;
4303 int ret;
4304
Jens Axboecebdb982020-01-08 17:59:24 -07004305 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4306 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004307 if (len < OPEN_HOW_SIZE_VER0)
4308 return -EINVAL;
4309
4310 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4311 len);
4312 if (ret)
4313 return ret;
4314
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004315 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004316}
4317
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004318static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004319{
4320 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004321 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004322 bool resolve_nonblock, nonblock_set;
4323 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004324 int ret;
4325
Jens Axboecebdb982020-01-08 17:59:24 -07004326 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004327 if (ret)
4328 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004329 nonblock_set = op.open_flag & O_NONBLOCK;
4330 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004331 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004332 /*
4333 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4334 * it'll always -EAGAIN
4335 */
4336 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4337 return -EAGAIN;
4338 op.lookup_flags |= LOOKUP_CACHED;
4339 op.open_flag |= O_NONBLOCK;
4340 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004341
Pavel Begunkovb9445592021-08-25 12:25:45 +01004342 if (!fixed) {
4343 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4344 if (ret < 0)
4345 goto err;
4346 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004347
4348 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004349 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004350 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004351 * We could hang on to this 'fd' on retrying, but seems like
4352 * marginal gain for something that is now known to be a slower
4353 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004354 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004355 if (!fixed)
4356 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004357
4358 ret = PTR_ERR(file);
4359 /* only retry if RESOLVE_CACHED wasn't already set by application */
4360 if (ret == -EAGAIN &&
4361 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4362 return -EAGAIN;
4363 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004364 }
4365
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004366 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4367 file->f_flags &= ~O_NONBLOCK;
4368 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004369
4370 if (!fixed)
4371 fd_install(ret, file);
4372 else
4373 ret = io_install_fixed_file(req, file, issue_flags,
4374 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004375err:
4376 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004377 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004378 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004379 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004380 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004381 return 0;
4382}
4383
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004384static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004385{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004386 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004387}
4388
Jens Axboe067524e2020-03-02 16:32:28 -07004389static int io_remove_buffers_prep(struct io_kiocb *req,
4390 const struct io_uring_sqe *sqe)
4391{
4392 struct io_provide_buf *p = &req->pbuf;
4393 u64 tmp;
4394
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004395 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4396 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004397 return -EINVAL;
4398
4399 tmp = READ_ONCE(sqe->fd);
4400 if (!tmp || tmp > USHRT_MAX)
4401 return -EINVAL;
4402
4403 memset(p, 0, sizeof(*p));
4404 p->nbufs = tmp;
4405 p->bgid = READ_ONCE(sqe->buf_group);
4406 return 0;
4407}
4408
4409static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4410 int bgid, unsigned nbufs)
4411{
4412 unsigned i = 0;
4413
4414 /* shouldn't happen */
4415 if (!nbufs)
4416 return 0;
4417
4418 /* the head kbuf is the list itself */
4419 while (!list_empty(&buf->list)) {
4420 struct io_buffer *nxt;
4421
4422 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4423 list_del(&nxt->list);
4424 kfree(nxt);
4425 if (++i == nbufs)
4426 return i;
4427 }
4428 i++;
4429 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004430 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004431
4432 return i;
4433}
4434
Pavel Begunkov889fca72021-02-10 00:03:09 +00004435static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004436{
4437 struct io_provide_buf *p = &req->pbuf;
4438 struct io_ring_ctx *ctx = req->ctx;
4439 struct io_buffer *head;
4440 int ret = 0;
Hao Xu3b44b372021-10-18 21:34:31 +08004441 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboe067524e2020-03-02 16:32:28 -07004442
Hao Xu3b44b372021-10-18 21:34:31 +08004443 io_ring_submit_lock(ctx, needs_lock);
Jens Axboe067524e2020-03-02 16:32:28 -07004444
4445 lockdep_assert_held(&ctx->uring_lock);
4446
4447 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004448 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004449 if (head)
4450 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004451 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004452 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004453
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004454 /* complete before unlock, IOPOLL may need the lock */
4455 __io_req_complete(req, issue_flags, ret, 0);
Hao Xu3b44b372021-10-18 21:34:31 +08004456 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboe067524e2020-03-02 16:32:28 -07004457 return 0;
4458}
4459
Jens Axboeddf0322d2020-02-23 16:41:33 -07004460static int io_provide_buffers_prep(struct io_kiocb *req,
4461 const struct io_uring_sqe *sqe)
4462{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004463 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004464 struct io_provide_buf *p = &req->pbuf;
4465 u64 tmp;
4466
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004467 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004468 return -EINVAL;
4469
4470 tmp = READ_ONCE(sqe->fd);
4471 if (!tmp || tmp > USHRT_MAX)
4472 return -E2BIG;
4473 p->nbufs = tmp;
4474 p->addr = READ_ONCE(sqe->addr);
4475 p->len = READ_ONCE(sqe->len);
4476
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004477 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4478 &size))
4479 return -EOVERFLOW;
4480 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4481 return -EOVERFLOW;
4482
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004483 size = (unsigned long)p->len * p->nbufs;
4484 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004485 return -EFAULT;
4486
4487 p->bgid = READ_ONCE(sqe->buf_group);
4488 tmp = READ_ONCE(sqe->off);
4489 if (tmp > USHRT_MAX)
4490 return -E2BIG;
4491 p->bid = tmp;
4492 return 0;
4493}
4494
4495static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4496{
4497 struct io_buffer *buf;
4498 u64 addr = pbuf->addr;
4499 int i, bid = pbuf->bid;
4500
4501 for (i = 0; i < pbuf->nbufs; i++) {
Jens Axboe9990da92021-09-24 07:39:08 -06004502 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004503 if (!buf)
4504 break;
4505
4506 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004507 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004508 buf->bid = bid;
4509 addr += pbuf->len;
4510 bid++;
4511 if (!*head) {
4512 INIT_LIST_HEAD(&buf->list);
4513 *head = buf;
4514 } else {
4515 list_add_tail(&buf->list, &(*head)->list);
4516 }
4517 }
4518
4519 return i ? i : -ENOMEM;
4520}
4521
Pavel Begunkov889fca72021-02-10 00:03:09 +00004522static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004523{
4524 struct io_provide_buf *p = &req->pbuf;
4525 struct io_ring_ctx *ctx = req->ctx;
4526 struct io_buffer *head, *list;
4527 int ret = 0;
Hao Xu3b44b372021-10-18 21:34:31 +08004528 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004529
Hao Xu3b44b372021-10-18 21:34:31 +08004530 io_ring_submit_lock(ctx, needs_lock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004531
4532 lockdep_assert_held(&ctx->uring_lock);
4533
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004534 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004535
4536 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004537 if (ret >= 0 && !list) {
4538 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4539 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004540 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004541 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004542 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004543 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004544 /* complete before unlock, IOPOLL may need the lock */
4545 __io_req_complete(req, issue_flags, ret, 0);
Hao Xu3b44b372021-10-18 21:34:31 +08004546 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004547 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004548}
4549
Jens Axboe3e4827b2020-01-08 15:18:09 -07004550static int io_epoll_ctl_prep(struct io_kiocb *req,
4551 const struct io_uring_sqe *sqe)
4552{
4553#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004554 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004555 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004556 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004557 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004558
4559 req->epoll.epfd = READ_ONCE(sqe->fd);
4560 req->epoll.op = READ_ONCE(sqe->len);
4561 req->epoll.fd = READ_ONCE(sqe->off);
4562
4563 if (ep_op_has_event(req->epoll.op)) {
4564 struct epoll_event __user *ev;
4565
4566 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4567 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4568 return -EFAULT;
4569 }
4570
4571 return 0;
4572#else
4573 return -EOPNOTSUPP;
4574#endif
4575}
4576
Pavel Begunkov889fca72021-02-10 00:03:09 +00004577static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004578{
4579#if defined(CONFIG_EPOLL)
4580 struct io_epoll *ie = &req->epoll;
4581 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004582 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004583
4584 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4585 if (force_nonblock && ret == -EAGAIN)
4586 return -EAGAIN;
4587
4588 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004589 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004590 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004591 return 0;
4592#else
4593 return -EOPNOTSUPP;
4594#endif
4595}
4596
Jens Axboec1ca7572019-12-25 22:18:28 -07004597static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4598{
4599#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004600 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004601 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004602 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4603 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004604
4605 req->madvise.addr = READ_ONCE(sqe->addr);
4606 req->madvise.len = READ_ONCE(sqe->len);
4607 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4608 return 0;
4609#else
4610 return -EOPNOTSUPP;
4611#endif
4612}
4613
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004614static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004615{
4616#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4617 struct io_madvise *ma = &req->madvise;
4618 int ret;
4619
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004620 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004621 return -EAGAIN;
4622
Minchan Kim0726b012020-10-17 16:14:50 -07004623 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004624 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004625 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004626 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004627 return 0;
4628#else
4629 return -EOPNOTSUPP;
4630#endif
4631}
4632
Jens Axboe4840e412019-12-25 22:03:45 -07004633static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4634{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004635 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004636 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004637 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4638 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004639
4640 req->fadvise.offset = READ_ONCE(sqe->off);
4641 req->fadvise.len = READ_ONCE(sqe->len);
4642 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4643 return 0;
4644}
4645
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004646static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004647{
4648 struct io_fadvise *fa = &req->fadvise;
4649 int ret;
4650
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004651 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004652 switch (fa->advice) {
4653 case POSIX_FADV_NORMAL:
4654 case POSIX_FADV_RANDOM:
4655 case POSIX_FADV_SEQUENTIAL:
4656 break;
4657 default:
4658 return -EAGAIN;
4659 }
4660 }
Jens Axboe4840e412019-12-25 22:03:45 -07004661
4662 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4663 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004664 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004665 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004666 return 0;
4667}
4668
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004669static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4670{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004671 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004672 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004673 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004674 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004675 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004676 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004677
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004678 req->statx.dfd = READ_ONCE(sqe->fd);
4679 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004680 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004681 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4682 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004683
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004684 return 0;
4685}
4686
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004687static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004688{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004689 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004690 int ret;
4691
Pavel Begunkov59d70012021-03-22 01:58:30 +00004692 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004693 return -EAGAIN;
4694
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004695 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4696 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004697
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004698 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004699 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004700 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004701 return 0;
4702}
4703
Jens Axboeb5dba592019-12-11 14:02:38 -07004704static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4705{
Jens Axboe14587a462020-09-05 11:36:08 -06004706 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004707 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004708 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004709 sqe->rw_flags || sqe->buf_index)
Jens Axboeb5dba592019-12-11 14:02:38 -07004710 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004711 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004712 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004713
4714 req->close.fd = READ_ONCE(sqe->fd);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004715 req->close.file_slot = READ_ONCE(sqe->file_index);
4716 if (req->close.file_slot && req->close.fd)
4717 return -EINVAL;
4718
Jens Axboeb5dba592019-12-11 14:02:38 -07004719 return 0;
4720}
4721
Pavel Begunkov889fca72021-02-10 00:03:09 +00004722static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004723{
Jens Axboe9eac1902021-01-19 15:50:37 -07004724 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004725 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004726 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004727 struct file *file = NULL;
4728 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004729
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004730 if (req->close.file_slot) {
4731 ret = io_close_fixed(req, issue_flags);
4732 goto err;
4733 }
4734
Jens Axboe9eac1902021-01-19 15:50:37 -07004735 spin_lock(&files->file_lock);
4736 fdt = files_fdtable(files);
4737 if (close->fd >= fdt->max_fds) {
4738 spin_unlock(&files->file_lock);
4739 goto err;
4740 }
4741 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004742 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004743 spin_unlock(&files->file_lock);
4744 file = NULL;
4745 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004746 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004747
4748 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004749 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004750 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004751 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004752 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004753
Jens Axboe9eac1902021-01-19 15:50:37 -07004754 ret = __close_fd_get_file(close->fd, &file);
4755 spin_unlock(&files->file_lock);
4756 if (ret < 0) {
4757 if (ret == -ENOENT)
4758 ret = -EBADF;
4759 goto err;
4760 }
4761
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004762 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004763 ret = filp_close(file, current->files);
4764err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004765 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004766 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004767 if (file)
4768 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004769 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004770 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004771}
4772
Pavel Begunkov1155c762021-02-18 18:29:38 +00004773static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004774{
4775 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004776
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004777 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4778 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004779 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4780 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004781 return -EINVAL;
4782
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004783 req->sync.off = READ_ONCE(sqe->off);
4784 req->sync.len = READ_ONCE(sqe->len);
4785 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004786 return 0;
4787}
4788
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004789static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004790{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004791 int ret;
4792
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004793 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004794 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004795 return -EAGAIN;
4796
Jens Axboe9adbd452019-12-20 08:45:55 -07004797 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004798 req->sync.flags);
4799 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004800 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004801 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004802 return 0;
4803}
4804
YueHaibing469956e2020-03-04 15:53:52 +08004805#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004806static int io_setup_async_msg(struct io_kiocb *req,
4807 struct io_async_msghdr *kmsg)
4808{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004809 struct io_async_msghdr *async_msg = req->async_data;
4810
4811 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004812 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004813 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004814 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004815 return -ENOMEM;
4816 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004817 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004818 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004819 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004820 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004821 /* if were using fast_iov, set it to the new one */
4822 if (!async_msg->free_iov)
4823 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4824
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004825 return -EAGAIN;
4826}
4827
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004828static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4829 struct io_async_msghdr *iomsg)
4830{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004831 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004832 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004833 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004834 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004835}
4836
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004837static int io_sendmsg_prep_async(struct io_kiocb *req)
4838{
4839 int ret;
4840
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004841 ret = io_sendmsg_copy_hdr(req, req->async_data);
4842 if (!ret)
4843 req->flags |= REQ_F_NEED_CLEANUP;
4844 return ret;
4845}
4846
Jens Axboe3529d8c2019-12-19 18:24:38 -07004847static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004848{
Jens Axboee47293f2019-12-20 08:58:21 -07004849 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004850
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004851 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4852 return -EINVAL;
4853
Pavel Begunkov270a5942020-07-12 20:41:04 +03004854 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004855 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004856 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4857 if (sr->msg_flags & MSG_DONTWAIT)
4858 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004859
Jens Axboed8768362020-02-27 14:17:49 -07004860#ifdef CONFIG_COMPAT
4861 if (req->ctx->compat)
4862 sr->msg_flags |= MSG_CMSG_COMPAT;
4863#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004864 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004865}
4866
Pavel Begunkov889fca72021-02-10 00:03:09 +00004867static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004868{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004869 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004870 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004871 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004872 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004873 int ret;
4874
Florent Revestdba4a922020-12-04 12:36:04 +01004875 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004876 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004877 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004878
Pavel Begunkovd886e182021-10-04 20:02:56 +01004879 if (req_has_async_data(req)) {
4880 kmsg = req->async_data;
4881 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004882 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004883 if (ret)
4884 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004885 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004886 }
4887
Pavel Begunkov04411802021-04-01 15:44:00 +01004888 flags = req->sr_msg.msg_flags;
4889 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004890 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004891 if (flags & MSG_WAITALL)
4892 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4893
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004894 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004895
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004896 if (ret < min_ret) {
4897 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
4898 return io_setup_async_msg(req, kmsg);
4899 if (ret == -ERESTARTSYS)
4900 ret = -EINTR;
4901 req_set_fail(req);
4902 }
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004903 /* fast path, check for non-NULL to avoid function call */
4904 if (kmsg->free_iov)
4905 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004906 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov889fca72021-02-10 00:03:09 +00004907 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004908 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004909}
4910
Pavel Begunkov889fca72021-02-10 00:03:09 +00004911static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004912{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004913 struct io_sr_msg *sr = &req->sr_msg;
4914 struct msghdr msg;
4915 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004916 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004917 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004918 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004919 int ret;
4920
Florent Revestdba4a922020-12-04 12:36:04 +01004921 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004922 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004923 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004924
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004925 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4926 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004927 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004928
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004929 msg.msg_name = NULL;
4930 msg.msg_control = NULL;
4931 msg.msg_controllen = 0;
4932 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004933
Pavel Begunkov04411802021-04-01 15:44:00 +01004934 flags = req->sr_msg.msg_flags;
4935 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004936 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004937 if (flags & MSG_WAITALL)
4938 min_ret = iov_iter_count(&msg.msg_iter);
4939
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004940 msg.msg_flags = flags;
4941 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004942 if (ret < min_ret) {
4943 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
4944 return -EAGAIN;
4945 if (ret == -ERESTARTSYS)
4946 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004947 req_set_fail(req);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004948 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004949 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004950 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004951}
4952
Pavel Begunkov1400e692020-07-12 20:41:05 +03004953static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4954 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004955{
4956 struct io_sr_msg *sr = &req->sr_msg;
4957 struct iovec __user *uiov;
4958 size_t iov_len;
4959 int ret;
4960
Pavel Begunkov1400e692020-07-12 20:41:05 +03004961 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4962 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004963 if (ret)
4964 return ret;
4965
4966 if (req->flags & REQ_F_BUFFER_SELECT) {
4967 if (iov_len > 1)
4968 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004969 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004970 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004971 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004972 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004973 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004974 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004975 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004976 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004977 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004978 if (ret > 0)
4979 ret = 0;
4980 }
4981
4982 return ret;
4983}
4984
4985#ifdef CONFIG_COMPAT
4986static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004987 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004988{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004989 struct io_sr_msg *sr = &req->sr_msg;
4990 struct compat_iovec __user *uiov;
4991 compat_uptr_t ptr;
4992 compat_size_t len;
4993 int ret;
4994
Pavel Begunkov4af34172021-04-11 01:46:30 +01004995 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4996 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004997 if (ret)
4998 return ret;
4999
5000 uiov = compat_ptr(ptr);
5001 if (req->flags & REQ_F_BUFFER_SELECT) {
5002 compat_ssize_t clen;
5003
5004 if (len > 1)
5005 return -EINVAL;
5006 if (!access_ok(uiov, sizeof(*uiov)))
5007 return -EFAULT;
5008 if (__get_user(clen, &uiov->iov_len))
5009 return -EFAULT;
5010 if (clen < 0)
5011 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00005012 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005013 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07005014 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005015 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02005016 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005017 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02005018 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07005019 if (ret < 0)
5020 return ret;
5021 }
5022
5023 return 0;
5024}
Jens Axboe03b12302019-12-02 18:50:25 -07005025#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07005026
Pavel Begunkov1400e692020-07-12 20:41:05 +03005027static int io_recvmsg_copy_hdr(struct io_kiocb *req,
5028 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07005029{
Pavel Begunkov1400e692020-07-12 20:41:05 +03005030 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07005031
5032#ifdef CONFIG_COMPAT
5033 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03005034 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07005035#endif
5036
Pavel Begunkov1400e692020-07-12 20:41:05 +03005037 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07005038}
5039
Jens Axboebcda7ba2020-02-23 16:42:51 -07005040static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov51aac422021-10-14 16:10:17 +01005041 unsigned int issue_flags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07005042{
5043 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005044
Pavel Begunkov51aac422021-10-14 16:10:17 +01005045 return io_buffer_select(req, &sr->len, sr->bgid, issue_flags);
Jens Axboe03b12302019-12-02 18:50:25 -07005046}
5047
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005048static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07005049{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005050 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07005051
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005052 ret = io_recvmsg_copy_hdr(req, req->async_data);
5053 if (!ret)
5054 req->flags |= REQ_F_NEED_CLEANUP;
5055 return ret;
5056}
5057
5058static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5059{
5060 struct io_sr_msg *sr = &req->sr_msg;
5061
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03005062 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5063 return -EINVAL;
5064
Pavel Begunkov270a5942020-07-12 20:41:04 +03005065 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07005066 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005067 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01005068 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
5069 if (sr->msg_flags & MSG_DONTWAIT)
5070 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005071
Jens Axboed8768362020-02-27 14:17:49 -07005072#ifdef CONFIG_COMPAT
5073 if (req->ctx->compat)
5074 sr->msg_flags |= MSG_CMSG_COMPAT;
5075#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005076 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07005077}
5078
Pavel Begunkov889fca72021-02-10 00:03:09 +00005079static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07005080{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005081 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005082 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005083 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005084 unsigned flags;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00005085 int ret, min_ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005086 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005087
Florent Revestdba4a922020-12-04 12:36:04 +01005088 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005089 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005090 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005091
Pavel Begunkovd886e182021-10-04 20:02:56 +01005092 if (req_has_async_data(req)) {
5093 kmsg = req->async_data;
5094 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005095 ret = io_recvmsg_copy_hdr(req, &iomsg);
5096 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03005097 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005098 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005099 }
5100
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005101 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01005102 kbuf = io_recv_buffer_select(req, issue_flags);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005103 if (IS_ERR(kbuf))
5104 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005105 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00005106 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
5107 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005108 1, req->sr_msg.len);
5109 }
5110
Pavel Begunkov04411802021-04-01 15:44:00 +01005111 flags = req->sr_msg.msg_flags;
5112 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005113 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005114 if (flags & MSG_WAITALL)
5115 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
5116
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005117 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
5118 kmsg->uaddr, flags);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005119 if (ret < min_ret) {
5120 if (ret == -EAGAIN && force_nonblock)
5121 return io_setup_async_msg(req, kmsg);
5122 if (ret == -ERESTARTSYS)
5123 ret = -EINTR;
5124 req_set_fail(req);
5125 } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
5126 req_set_fail(req);
5127 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005128
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005129 /* fast path, check for non-NULL to avoid function call */
5130 if (kmsg->free_iov)
5131 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005132 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00005133 __io_req_complete(req, issue_flags, ret, io_put_kbuf(req));
Jens Axboe0fa03c62019-04-19 13:34:07 -06005134 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005135}
5136
Pavel Begunkov889fca72021-02-10 00:03:09 +00005137static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07005138{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005139 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005140 struct io_sr_msg *sr = &req->sr_msg;
5141 struct msghdr msg;
5142 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005143 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005144 struct iovec iov;
5145 unsigned flags;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00005146 int ret, min_ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005147 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005148
Florent Revestdba4a922020-12-04 12:36:04 +01005149 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005150 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005151 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005152
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005153 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01005154 kbuf = io_recv_buffer_select(req, issue_flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005155 if (IS_ERR(kbuf))
5156 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005157 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005158 }
5159
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005160 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005161 if (unlikely(ret))
5162 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005163
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005164 msg.msg_name = NULL;
5165 msg.msg_control = NULL;
5166 msg.msg_controllen = 0;
5167 msg.msg_namelen = 0;
5168 msg.msg_iocb = NULL;
5169 msg.msg_flags = 0;
5170
Pavel Begunkov04411802021-04-01 15:44:00 +01005171 flags = req->sr_msg.msg_flags;
5172 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005173 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005174 if (flags & MSG_WAITALL)
5175 min_ret = iov_iter_count(&msg.msg_iter);
5176
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005177 ret = sock_recvmsg(sock, &msg, flags);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005178out_free:
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005179 if (ret < min_ret) {
5180 if (ret == -EAGAIN && force_nonblock)
5181 return -EAGAIN;
5182 if (ret == -ERESTARTSYS)
5183 ret = -EINTR;
5184 req_set_fail(req);
5185 } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
5186 req_set_fail(req);
5187 }
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00005188
5189 __io_req_complete(req, issue_flags, ret, io_put_kbuf(req));
Jens Axboefddafac2020-01-04 20:19:44 -07005190 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005191}
5192
Jens Axboe3529d8c2019-12-19 18:24:38 -07005193static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005194{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005195 struct io_accept *accept = &req->accept;
5196
Jens Axboe14587a462020-09-05 11:36:08 -06005197 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005198 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005199 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005200 return -EINVAL;
5201
Jens Axboed55e5f52019-12-11 16:12:15 -07005202 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5203 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005204 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005205 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005206
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005207 accept->file_slot = READ_ONCE(sqe->file_index);
5208 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5209 (accept->flags & SOCK_CLOEXEC)))
5210 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005211 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5212 return -EINVAL;
5213 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5214 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005215 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005216}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005217
Pavel Begunkov889fca72021-02-10 00:03:09 +00005218static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005219{
5220 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005221 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005222 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005223 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005224 struct file *file;
5225 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005226
Jiufei Xuee697dee2020-06-10 13:41:59 +08005227 if (req->file->f_flags & O_NONBLOCK)
5228 req->flags |= REQ_F_NOWAIT;
5229
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005230 if (!fixed) {
5231 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5232 if (unlikely(fd < 0))
5233 return fd;
5234 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005235 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5236 accept->flags);
5237 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005238 if (!fixed)
5239 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005240 ret = PTR_ERR(file);
5241 if (ret == -EAGAIN && force_nonblock)
5242 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005243 if (ret == -ERESTARTSYS)
5244 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005245 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005246 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005247 fd_install(fd, file);
5248 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005249 } else {
5250 ret = io_install_fixed_file(req, file, issue_flags,
5251 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005252 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005253 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005254 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005255}
5256
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005257static int io_connect_prep_async(struct io_kiocb *req)
5258{
5259 struct io_async_connect *io = req->async_data;
5260 struct io_connect *conn = &req->connect;
5261
5262 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5263}
5264
Jens Axboe3529d8c2019-12-19 18:24:38 -07005265static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005266{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005267 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005268
Jens Axboe14587a462020-09-05 11:36:08 -06005269 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005270 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005271 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5272 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005273 return -EINVAL;
5274
Jens Axboe3529d8c2019-12-19 18:24:38 -07005275 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5276 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005277 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005278}
5279
Pavel Begunkov889fca72021-02-10 00:03:09 +00005280static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005281{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005282 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005283 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005284 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005285 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005286
Pavel Begunkovd886e182021-10-04 20:02:56 +01005287 if (req_has_async_data(req)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005288 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005289 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005290 ret = move_addr_to_kernel(req->connect.addr,
5291 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005292 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005293 if (ret)
5294 goto out;
5295 io = &__io;
5296 }
5297
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005298 file_flags = force_nonblock ? O_NONBLOCK : 0;
5299
Jens Axboee8c2bc12020-08-15 18:44:09 -07005300 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005301 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005302 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Pavel Begunkovd886e182021-10-04 20:02:56 +01005303 if (req_has_async_data(req))
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005304 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005305 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005306 ret = -ENOMEM;
5307 goto out;
5308 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005309 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005310 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005311 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005312 if (ret == -ERESTARTSYS)
5313 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005314out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005315 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005316 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005317 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005318 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005319}
YueHaibing469956e2020-03-04 15:53:52 +08005320#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005321#define IO_NETOP_FN(op) \
5322static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5323{ \
5324 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005325}
5326
Jens Axboe99a10082021-02-19 09:35:19 -07005327#define IO_NETOP_PREP(op) \
5328IO_NETOP_FN(op) \
5329static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5330{ \
5331 return -EOPNOTSUPP; \
5332} \
5333
5334#define IO_NETOP_PREP_ASYNC(op) \
5335IO_NETOP_PREP(op) \
5336static int io_##op##_prep_async(struct io_kiocb *req) \
5337{ \
5338 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005339}
5340
Jens Axboe99a10082021-02-19 09:35:19 -07005341IO_NETOP_PREP_ASYNC(sendmsg);
5342IO_NETOP_PREP_ASYNC(recvmsg);
5343IO_NETOP_PREP_ASYNC(connect);
5344IO_NETOP_PREP(accept);
5345IO_NETOP_FN(send);
5346IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005347#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005348
Jens Axboed7718a92020-02-14 22:23:12 -07005349struct io_poll_table {
5350 struct poll_table_struct pt;
5351 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005352 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005353 int error;
5354};
5355
Pavel Begunkov56418972021-12-15 22:08:46 +00005356static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
5357{
5358 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
5359 if (req->opcode == IORING_OP_POLL_ADD)
5360 return req->async_data;
5361 return req->apoll->double_poll;
5362}
5363
5364static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5365{
5366 if (req->opcode == IORING_OP_POLL_ADD)
5367 return &req->poll;
5368 return &req->apoll->poll;
5369}
5370
5371static void io_poll_req_insert(struct io_kiocb *req)
5372{
5373 struct io_ring_ctx *ctx = req->ctx;
5374 struct hlist_head *list;
5375
5376 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5377 hlist_add_head(&req->hash_node, list);
5378}
5379
5380static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5381 wait_queue_func_t wake_func)
5382{
5383 poll->head = NULL;
5384 poll->done = false;
5385 poll->canceled = false;
5386#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5387 /* mask in events that we always want/need */
5388 poll->events = events | IO_POLL_UNMASK;
5389 INIT_LIST_HEAD(&poll->wait.entry);
5390 init_waitqueue_func_entry(&poll->wait, wake_func);
5391}
5392
Jens Axboed7718a92020-02-14 22:23:12 -07005393static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005394 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07005395{
Jens Axboed7718a92020-02-14 22:23:12 -07005396 /* for instances that support it check for an event match first: */
5397 if (mask && !(mask & poll->events))
5398 return 0;
5399
5400 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5401
5402 list_del_init(&poll->wait.entry);
5403
Jens Axboed7718a92020-02-14 22:23:12 -07005404 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005405 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005406
Jens Axboed7718a92020-02-14 22:23:12 -07005407 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005408 * If this fails, then the task is exiting. When a task exits, the
5409 * work gets canceled, so just cancel this request as well instead
5410 * of executing it. We can't safely execute it anyway, as we may not
5411 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005412 */
Hao Xu4813c372021-12-07 17:39:48 +08005413 io_req_task_work_add(req, false);
Jens Axboed7718a92020-02-14 22:23:12 -07005414 return 1;
5415}
5416
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005417static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5418 __acquires(&req->ctx->completion_lock)
5419{
5420 struct io_ring_ctx *ctx = req->ctx;
5421
Jens Axboe316319e2021-08-19 09:41:42 -06005422 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005423 if (unlikely(req->task->flags & PF_EXITING))
5424 WRITE_ONCE(poll->canceled, true);
5425
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005426 if (!req->result && !READ_ONCE(poll->canceled)) {
5427 struct poll_table_struct pt = { ._key = poll->events };
5428
5429 req->result = vfs_poll(req->file, &pt) & poll->events;
5430 }
5431
Jens Axboe79ebeae2021-08-10 15:18:27 -06005432 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005433 if (!req->result && !READ_ONCE(poll->canceled)) {
5434 add_wait_queue(poll->head, &poll->wait);
5435 return true;
5436 }
5437
5438 return false;
5439}
5440
Jens Axboed4e7cd32020-08-15 11:44:50 -07005441static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005442 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005443{
5444 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005445
5446 lockdep_assert_held(&req->ctx->completion_lock);
5447
5448 if (poll && poll->head) {
5449 struct wait_queue_head *head = poll->head;
5450
Jens Axboe79ebeae2021-08-10 15:18:27 -06005451 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005452 list_del_init(&poll->wait.entry);
5453 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005454 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005455 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005456 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005457 }
5458}
5459
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005460static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005461 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005462{
5463 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005464 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005465 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005466
Pavel Begunkove27414b2021-04-09 09:13:20 +01005467 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005468 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005469 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005470 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005471 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005472 }
Jens Axboeb69de282021-03-17 08:37:41 -06005473 if (req->poll.events & EPOLLONESHOT)
5474 flags = 0;
Pavel Begunkov913a5712021-11-10 15:49:31 +00005475
5476 if (!(flags & IORING_CQE_F_MORE)) {
5477 io_fill_cqe_req(req, error, flags);
5478 } else if (!io_fill_cqe_aux(ctx, req->user_data, error, flags)) {
Hao Xua62682f2021-09-22 18:12:37 +08005479 req->poll.events |= EPOLLONESHOT;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005480 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005481 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005482 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005483}
5484
Pavel Begunkovf237c302021-08-18 12:42:46 +01005485static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005486{
Jens Axboe6d816e02020-08-11 08:04:14 -06005487 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005488 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005489
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005490 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005491 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005492 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005493 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005494
Hao Xu5b7aa382021-09-22 18:12:38 +08005495 if (req->poll.done) {
5496 spin_unlock(&ctx->completion_lock);
5497 return;
5498 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005499 done = __io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005500 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005501 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005502 hash_del(&req->hash_node);
Hao Xubd99c712021-09-22 18:12:36 +08005503 req->poll.done = true;
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005504 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005505 req->result = 0;
5506 add_wait_queue(req->poll.head, &req->poll.wait);
5507 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005508 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005509 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005510 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005511
Jens Axboe88e41cf2021-02-22 22:08:01 -07005512 if (done) {
5513 nxt = io_put_req_find_next(req);
5514 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005515 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005516 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005517 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005518}
5519
5520static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5521 int sync, void *key)
5522{
5523 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005524 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005525 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005526 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005527
5528 /* for instances that support it check for an event match first: */
5529 if (mask && !(mask & poll->events))
5530 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005531 if (!(poll->events & EPOLLONESHOT))
5532 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005533
Jens Axboe8706e042020-09-28 08:38:54 -06005534 list_del_init(&wait->entry);
5535
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005536 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005537 bool done;
5538
Jens Axboe79ebeae2021-08-10 15:18:27 -06005539 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005540 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005541 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005542 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005543 /* make sure double remove sees this as being gone */
5544 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005545 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005546 if (!done) {
5547 /* use wait func handler, so it matches the rq type */
5548 poll->wait.func(&poll->wait, mode, sync, key);
5549 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005550 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005551 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005552 return 1;
5553}
5554
Jens Axboe18bceab2020-05-15 11:56:54 -06005555static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005556 struct wait_queue_head *head,
5557 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005558{
5559 struct io_kiocb *req = pt->req;
5560
5561 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005562 * The file being polled uses multiple waitqueues for poll handling
5563 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5564 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005565 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005566 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005567 struct io_poll_iocb *poll_one = poll;
5568
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005569 /* double add on the same waitqueue head, ignore */
5570 if (poll_one->head == head)
5571 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005572 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005573 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005574 if ((*poll_ptr)->head == head)
5575 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005576 pt->error = -EINVAL;
5577 return;
5578 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005579 /*
5580 * Can't handle multishot for double wait for now, turn it
5581 * into one-shot mode.
5582 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005583 if (!(poll_one->events & EPOLLONESHOT))
5584 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005585 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5586 if (!poll) {
5587 pt->error = -ENOMEM;
5588 return;
5589 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005590 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005591 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005592 poll->wait.private = req;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005593
Jens Axboe807abcb2020-07-17 17:09:27 -06005594 *poll_ptr = poll;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005595 if (req->opcode == IORING_OP_POLL_ADD)
5596 req->flags |= REQ_F_ASYNC_DATA;
Jens Axboe18bceab2020-05-15 11:56:54 -06005597 }
5598
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005599 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005600 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005601
5602 if (poll->events & EPOLLEXCLUSIVE)
5603 add_wait_queue_exclusive(head, &poll->wait);
5604 else
5605 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005606}
5607
5608static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5609 struct poll_table_struct *p)
5610{
5611 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005612 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005613
Jens Axboe807abcb2020-07-17 17:09:27 -06005614 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005615}
5616
Pavel Begunkovf237c302021-08-18 12:42:46 +01005617static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005618{
Jens Axboed7718a92020-02-14 22:23:12 -07005619 struct async_poll *apoll = req->apoll;
5620 struct io_ring_ctx *ctx = req->ctx;
5621
Olivier Langlois236daeae2021-05-31 02:36:37 -04005622 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005623
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005624 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005625 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005626 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005627 }
5628
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005629 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005630 io_poll_remove_double(req);
Hao Xubd99c712021-09-22 18:12:36 +08005631 apoll->poll.done = true;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005632 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005633
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005634 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005635 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005636 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005637 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005638}
5639
5640static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5641 void *key)
5642{
5643 struct io_kiocb *req = wait->private;
5644 struct io_poll_iocb *poll = &req->apoll->poll;
5645
5646 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5647 key_to_poll(key));
5648
5649 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5650}
5651
Jens Axboed7718a92020-02-14 22:23:12 -07005652static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5653 struct io_poll_iocb *poll,
5654 struct io_poll_table *ipt, __poll_t mask,
5655 wait_queue_func_t wake_func)
5656 __acquires(&ctx->completion_lock)
5657{
5658 struct io_ring_ctx *ctx = req->ctx;
5659 bool cancel = false;
5660
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005661 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005662 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005663 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005664 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005665
5666 ipt->pt._key = mask;
5667 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005668 ipt->error = 0;
5669 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005670
Jens Axboed7718a92020-02-14 22:23:12 -07005671 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005672 if (unlikely(!ipt->nr_entries) && !ipt->error)
5673 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005674
Jens Axboe79ebeae2021-08-10 15:18:27 -06005675 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005676 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005677 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005678 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005679 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005680 if (unlikely(list_empty(&poll->wait.entry))) {
5681 if (ipt->error)
5682 cancel = true;
5683 ipt->error = 0;
5684 mask = 0;
5685 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005686 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005687 list_del_init(&poll->wait.entry);
5688 else if (cancel)
5689 WRITE_ONCE(poll->canceled, true);
5690 else if (!poll->done) /* actually waiting for an event */
5691 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005692 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005693 }
5694
5695 return mask;
5696}
5697
Olivier Langlois59b735a2021-06-22 05:17:39 -07005698enum {
5699 IO_APOLL_OK,
5700 IO_APOLL_ABORTED,
5701 IO_APOLL_READY
5702};
5703
5704static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005705{
5706 const struct io_op_def *def = &io_op_defs[req->opcode];
5707 struct io_ring_ctx *ctx = req->ctx;
5708 struct async_poll *apoll;
5709 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005710 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboed7718a92020-02-14 22:23:12 -07005711
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005712 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005713 return IO_APOLL_ABORTED;
Pavel Begunkov658d0a42021-10-23 12:13:58 +01005714 if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED))
5715 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005716
5717 if (def->pollin) {
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005718 mask |= POLLIN | POLLRDNORM;
5719
5720 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5721 if ((req->opcode == IORING_OP_RECVMSG) &&
5722 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5723 mask &= ~POLLIN;
5724 } else {
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005725 mask |= POLLOUT | POLLWRNORM;
5726 }
5727
Jens Axboed7718a92020-02-14 22:23:12 -07005728 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5729 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005730 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005731 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005732 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005733 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005734 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005735 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005736
5737 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5738 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005739 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005740 if (ret || ipt.error)
5741 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5742
Olivier Langlois236daeae2021-05-31 02:36:37 -04005743 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5744 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005745 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005746}
5747
5748static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005749 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005750 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005751{
Jens Axboeb41e9852020-02-17 09:52:41 -07005752 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005753
Jens Axboe50826202021-02-23 09:02:26 -07005754 if (!poll->head)
5755 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005756 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005757 if (do_cancel)
5758 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005759 if (!list_empty(&poll->wait.entry)) {
5760 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005761 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005762 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005763 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005764 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005765 return do_complete;
5766}
5767
Pavel Begunkov5d709042021-08-09 20:18:13 +01005768static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005769 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005770{
5771 bool do_complete;
5772
Jens Axboed4e7cd32020-08-15 11:44:50 -07005773 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005774 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005775
Jens Axboeb41e9852020-02-17 09:52:41 -07005776 if (do_complete) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005777 req_set_fail(req);
Pavel Begunkov913a5712021-11-10 15:49:31 +00005778 io_fill_cqe_req(req, -ECANCELED, 0);
5779 io_commit_cqring(req->ctx);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005780 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005781 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005782 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005783}
5784
Jens Axboe76e1b642020-09-26 15:05:03 -06005785/*
5786 * Returns true if we found and killed one or more poll requests
5787 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01005788static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx,
5789 struct task_struct *tsk, bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005790{
Jens Axboe78076bb2019-12-04 19:56:40 -07005791 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005792 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005793 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005794
Jens Axboe79ebeae2021-08-10 15:18:27 -06005795 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005796 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5797 struct hlist_head *list;
5798
5799 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005800 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005801 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005802 posted += io_poll_remove_one(req);
5803 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005804 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005805 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005806
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005807 if (posted)
5808 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005809
5810 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005811}
5812
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005813static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5814 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005815 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005816{
Jens Axboe78076bb2019-12-04 19:56:40 -07005817 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005818 struct io_kiocb *req;
5819
Jens Axboe78076bb2019-12-04 19:56:40 -07005820 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5821 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005822 if (sqe_addr != req->user_data)
5823 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005824 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5825 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005826 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005827 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005828 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005829}
5830
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005831static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5832 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005833 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005834{
5835 struct io_kiocb *req;
5836
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005837 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005838 if (!req)
5839 return -ENOENT;
5840 if (io_poll_remove_one(req))
5841 return 0;
5842
5843 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005844}
5845
Pavel Begunkov9096af32021-04-14 13:38:36 +01005846static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5847 unsigned int flags)
5848{
5849 u32 events;
5850
5851 events = READ_ONCE(sqe->poll32_events);
5852#ifdef __BIG_ENDIAN
5853 events = swahw32(events);
5854#endif
5855 if (!(flags & IORING_POLL_ADD_MULTI))
5856 events |= EPOLLONESHOT;
5857 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5858}
5859
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005860static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005861 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005862{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005863 struct io_poll_update *upd = &req->poll_update;
5864 u32 flags;
5865
Jens Axboe221c5eb2019-01-17 09:41:58 -07005866 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5867 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005868 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005869 return -EINVAL;
5870 flags = READ_ONCE(sqe->len);
5871 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5872 IORING_POLL_ADD_MULTI))
5873 return -EINVAL;
5874 /* meaningless without update */
5875 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005876 return -EINVAL;
5877
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005878 upd->old_user_data = READ_ONCE(sqe->addr);
5879 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5880 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005881
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005882 upd->new_user_data = READ_ONCE(sqe->off);
5883 if (!upd->update_user_data && upd->new_user_data)
5884 return -EINVAL;
5885 if (upd->update_events)
5886 upd->events = io_poll_parse_events(sqe, flags);
5887 else if (sqe->poll32_events)
5888 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005889
Jens Axboe221c5eb2019-01-17 09:41:58 -07005890 return 0;
5891}
5892
Jens Axboe221c5eb2019-01-17 09:41:58 -07005893static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5894 void *key)
5895{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005896 struct io_kiocb *req = wait->private;
5897 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005898
Jens Axboed7718a92020-02-14 22:23:12 -07005899 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005900}
5901
Jens Axboe221c5eb2019-01-17 09:41:58 -07005902static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5903 struct poll_table_struct *p)
5904{
5905 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5906
Jens Axboee8c2bc12020-08-15 18:44:09 -07005907 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005908}
5909
Jens Axboe3529d8c2019-12-19 18:24:38 -07005910static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005911{
5912 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005913 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005914
5915 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5916 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005917 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005918 return -EINVAL;
5919 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005920 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005921 return -EINVAL;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00005922 if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
5923 return -EINVAL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005924
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005925 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005926 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005927 return 0;
5928}
5929
Pavel Begunkov61e98202021-02-10 00:03:08 +00005930static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005931{
5932 struct io_poll_iocb *poll = &req->poll;
5933 struct io_ring_ctx *ctx = req->ctx;
5934 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005935 __poll_t mask;
Hao Xu5b7aa382021-09-22 18:12:38 +08005936 bool done;
Jens Axboe0969e782019-12-17 18:40:57 -07005937
Jens Axboed7718a92020-02-14 22:23:12 -07005938 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005939
Jens Axboed7718a92020-02-14 22:23:12 -07005940 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5941 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005942
Jens Axboe8c838782019-03-12 15:48:16 -06005943 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005944 ipt.error = 0;
Pavel Begunkoveb6e6f02021-10-04 20:02:59 +01005945 done = __io_poll_complete(req, mask);
5946 io_commit_cqring(req->ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06005947 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005948 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005949
Jens Axboe8c838782019-03-12 15:48:16 -06005950 if (mask) {
5951 io_cqring_ev_posted(ctx);
Hao Xu5b7aa382021-09-22 18:12:38 +08005952 if (done)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005953 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005954 }
Jens Axboe8c838782019-03-12 15:48:16 -06005955 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005956}
5957
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005958static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005959{
5960 struct io_ring_ctx *ctx = req->ctx;
5961 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005962 bool completing;
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00005963 int ret2, ret = 0;
Jens Axboeb69de282021-03-17 08:37:41 -06005964
Jens Axboe79ebeae2021-08-10 15:18:27 -06005965 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005966 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005967 if (!preq) {
5968 ret = -ENOENT;
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00005969fail:
5970 spin_unlock(&ctx->completion_lock);
5971 goto out;
Jens Axboeb69de282021-03-17 08:37:41 -06005972 }
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00005973 io_poll_remove_double(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005974 /*
5975 * Don't allow racy completion with singleshot, as we cannot safely
5976 * update those. For multishot, if we're racing with completion, just
5977 * let completion re-add it.
5978 */
5979 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5980 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5981 ret = -EALREADY;
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00005982 goto fail;
Jens Axboeb69de282021-03-17 08:37:41 -06005983 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005984 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005985
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00005986 if (req->poll_update.update_events || req->poll_update.update_user_data) {
5987 /* only mask one event flags, keep behavior flags */
5988 if (req->poll_update.update_events) {
5989 preq->poll.events &= ~0xffff;
5990 preq->poll.events |= req->poll_update.events & 0xffff;
5991 preq->poll.events |= IO_POLL_UNMASK;
5992 }
5993 if (req->poll_update.update_user_data)
5994 preq->user_data = req->poll_update.new_user_data;
5995
5996 ret2 = io_poll_add(preq, issue_flags);
5997 /* successfully updated, don't complete poll request */
5998 if (!ret2)
5999 goto out;
6000 }
6001 req_set_fail(preq);
6002 io_req_complete(preq, -ECANCELED);
6003out:
6004 if (ret < 0)
6005 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06006006 /* complete update request, we're done with it */
6007 io_req_complete(req, ret);
Jens Axboeb69de282021-03-17 08:37:41 -06006008 return 0;
6009}
6010
Jens Axboe5262f562019-09-17 12:26:57 -06006011static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
6012{
Jens Axboead8a48a2019-11-15 08:49:11 -07006013 struct io_timeout_data *data = container_of(timer,
6014 struct io_timeout_data, timer);
6015 struct io_kiocb *req = data->req;
6016 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06006017 unsigned long flags;
6018
Jens Axboe89850fc2021-08-10 15:11:51 -06006019 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01006020 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03006021 atomic_set(&req->ctx->cq_timeouts,
6022 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06006023 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03006024
Pavel Begunkova90c8bf2021-12-05 14:38:00 +00006025 if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
6026 req_set_fail(req);
6027
6028 req->result = -ETIME;
6029 req->io_task_work.func = io_req_task_complete;
Hao Xu4813c372021-12-07 17:39:48 +08006030 io_req_task_work_add(req, false);
Jens Axboe5262f562019-09-17 12:26:57 -06006031 return HRTIMER_NORESTART;
6032}
6033
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006034static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
6035 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06006036 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07006037{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006038 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06006039 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006040 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06006041
6042 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006043 found = user_data == req->user_data;
6044 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06006045 break;
Jens Axboef254ac02020-08-12 17:33:30 -06006046 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006047 if (!found)
6048 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06006049
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006050 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006051 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006052 return ERR_PTR(-EALREADY);
6053 list_del_init(&req->timeout.list);
6054 return req;
6055}
6056
6057static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006058 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06006059 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006060{
6061 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6062
6063 if (IS_ERR(req))
6064 return PTR_ERR(req);
6065
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006066 req_set_fail(req);
Pavel Begunkov913a5712021-11-10 15:49:31 +00006067 io_fill_cqe_req(req, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01006068 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006069 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06006070}
6071
Jens Axboe50c1df22021-08-27 17:11:06 -06006072static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
6073{
6074 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
6075 case IORING_TIMEOUT_BOOTTIME:
6076 return CLOCK_BOOTTIME;
6077 case IORING_TIMEOUT_REALTIME:
6078 return CLOCK_REALTIME;
6079 default:
6080 /* can't happen, vetted at prep time */
6081 WARN_ON_ONCE(1);
6082 fallthrough;
6083 case 0:
6084 return CLOCK_MONOTONIC;
6085 }
6086}
6087
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006088static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6089 struct timespec64 *ts, enum hrtimer_mode mode)
6090 __must_hold(&ctx->timeout_lock)
6091{
6092 struct io_timeout_data *io;
6093 struct io_kiocb *req;
6094 bool found = false;
6095
6096 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
6097 found = user_data == req->user_data;
6098 if (found)
6099 break;
6100 }
6101 if (!found)
6102 return -ENOENT;
6103
6104 io = req->async_data;
6105 if (hrtimer_try_to_cancel(&io->timer) == -1)
6106 return -EALREADY;
6107 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
6108 io->timer.function = io_link_timeout_fn;
6109 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
6110 return 0;
6111}
6112
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006113static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6114 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06006115 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006116{
6117 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6118 struct io_timeout_data *data;
6119
6120 if (IS_ERR(req))
6121 return PTR_ERR(req);
6122
6123 req->timeout.off = 0; /* noseq */
6124 data = req->async_data;
6125 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06006126 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006127 data->timer.function = io_timeout_fn;
6128 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
6129 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07006130}
6131
Jens Axboe3529d8c2019-12-19 18:24:38 -07006132static int io_timeout_remove_prep(struct io_kiocb *req,
6133 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07006134{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006135 struct io_timeout_rem *tr = &req->timeout_rem;
6136
Jens Axboeb29472e2019-12-17 18:50:29 -07006137 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6138 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006139 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6140 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006141 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07006142 return -EINVAL;
6143
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006144 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006145 tr->addr = READ_ONCE(sqe->addr);
6146 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006147 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6148 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6149 return -EINVAL;
6150 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6151 tr->ltimeout = true;
6152 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006153 return -EINVAL;
6154 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6155 return -EFAULT;
Ye Bin20870092021-11-29 12:15:37 +08006156 if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0)
6157 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006158 } else if (tr->flags) {
6159 /* timeout removal doesn't support flags */
6160 return -EINVAL;
6161 }
6162
Jens Axboeb29472e2019-12-17 18:50:29 -07006163 return 0;
6164}
6165
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006166static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6167{
6168 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6169 : HRTIMER_MODE_REL;
6170}
6171
Jens Axboe11365042019-10-16 09:08:32 -06006172/*
6173 * Remove or update an existing timeout command
6174 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006175static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006176{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006177 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006178 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006179 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006180
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006181 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6182 spin_lock(&ctx->completion_lock);
6183 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006184 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006185 spin_unlock_irq(&ctx->timeout_lock);
6186 spin_unlock(&ctx->completion_lock);
6187 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006188 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6189
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006190 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006191 if (tr->ltimeout)
6192 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6193 else
6194 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006195 spin_unlock_irq(&ctx->timeout_lock);
6196 }
Jens Axboe11365042019-10-16 09:08:32 -06006197
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006198 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006199 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006200 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006201 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006202}
6203
Jens Axboe3529d8c2019-12-19 18:24:38 -07006204static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006205 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006206{
Jens Axboead8a48a2019-11-15 08:49:11 -07006207 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006208 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006209 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006210
Jens Axboead8a48a2019-11-15 08:49:11 -07006211 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006212 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006213 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6214 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006215 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006216 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006217 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006218 flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkov62245902021-10-02 19:36:14 +01006219 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
6220 IORING_TIMEOUT_ETIME_SUCCESS))
Jens Axboe50c1df22021-08-27 17:11:06 -06006221 return -EINVAL;
6222 /* more than one clock specified is invalid, obviously */
6223 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006224 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006225
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006226 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006227 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006228 if (unlikely(off && !req->ctx->off_timeout_used))
6229 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006230
Pavel Begunkovd6a644a2021-10-23 12:14:00 +01006231 if (WARN_ON_ONCE(req_has_async_data(req)))
6232 return -EFAULT;
6233 if (io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006234 return -ENOMEM;
6235
Jens Axboee8c2bc12020-08-15 18:44:09 -07006236 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006237 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006238 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006239
6240 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006241 return -EFAULT;
6242
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006243 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006244 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006245
6246 if (is_timeout_link) {
6247 struct io_submit_link *link = &req->ctx->submit_state.link;
6248
6249 if (!link->head)
6250 return -EINVAL;
6251 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6252 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006253 req->timeout.head = link->last;
6254 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006255 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006256 return 0;
6257}
6258
Pavel Begunkov61e98202021-02-10 00:03:08 +00006259static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006260{
Jens Axboead8a48a2019-11-15 08:49:11 -07006261 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006262 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006263 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006264 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006265
Jens Axboe89850fc2021-08-10 15:11:51 -06006266 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006267
Jens Axboe5262f562019-09-17 12:26:57 -06006268 /*
6269 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006270 * timeout event to be satisfied. If it isn't set, then this is
6271 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006272 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006273 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006274 entry = ctx->timeout_list.prev;
6275 goto add;
6276 }
Jens Axboe5262f562019-09-17 12:26:57 -06006277
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006278 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6279 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006280
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006281 /* Update the last seq here in case io_flush_timeouts() hasn't.
6282 * This is safe because ->completion_lock is held, and submissions
6283 * and completions are never mixed in the same ->completion_lock section.
6284 */
6285 ctx->cq_last_tm_flush = tail;
6286
Jens Axboe5262f562019-09-17 12:26:57 -06006287 /*
6288 * Insertion sort, ensuring the first entry in the list is always
6289 * the one we need first.
6290 */
Jens Axboe5262f562019-09-17 12:26:57 -06006291 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006292 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6293 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006294
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006295 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006296 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006297 /* nxt.seq is behind @tail, otherwise would've been completed */
6298 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006299 break;
6300 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006301add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006302 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006303 data->timer.function = io_timeout_fn;
6304 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006305 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006306 return 0;
6307}
6308
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006309struct io_cancel_data {
6310 struct io_ring_ctx *ctx;
6311 u64 user_data;
6312};
6313
Jens Axboe62755e32019-10-28 21:49:21 -06006314static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006315{
Jens Axboe62755e32019-10-28 21:49:21 -06006316 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006317 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006318
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006319 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006320}
6321
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006322static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6323 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006324{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006325 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006326 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006327 int ret = 0;
6328
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006329 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006330 return -ENOENT;
6331
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006332 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006333 switch (cancel_ret) {
6334 case IO_WQ_CANCEL_OK:
6335 ret = 0;
6336 break;
6337 case IO_WQ_CANCEL_RUNNING:
6338 ret = -EALREADY;
6339 break;
6340 case IO_WQ_CANCEL_NOTFOUND:
6341 ret = -ENOENT;
6342 break;
6343 }
6344
Jens Axboee977d6d2019-11-05 12:39:45 -07006345 return ret;
6346}
6347
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006348static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006349{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006350 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006351 int ret;
6352
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006353 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006354
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006355 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01006356 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006357 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006358
6359 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006360 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006361 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006362 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006363 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01006364 goto out;
6365 ret = io_poll_cancel(ctx, sqe_addr, false);
6366out:
6367 spin_unlock(&ctx->completion_lock);
6368 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006369}
6370
Jens Axboe3529d8c2019-12-19 18:24:38 -07006371static int io_async_cancel_prep(struct io_kiocb *req,
6372 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006373{
Jens Axboefbf23842019-12-17 18:45:56 -07006374 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006375 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006376 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6377 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006378 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6379 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006380 return -EINVAL;
6381
Jens Axboefbf23842019-12-17 18:45:56 -07006382 req->cancel.addr = READ_ONCE(sqe->addr);
6383 return 0;
6384}
6385
Pavel Begunkov61e98202021-02-10 00:03:08 +00006386static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006387{
6388 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006389 u64 sqe_addr = req->cancel.addr;
Hao Xu3b44b372021-10-18 21:34:31 +08006390 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006391 struct io_tctx_node *node;
6392 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006393
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006394 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006395 if (ret != -ENOENT)
6396 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006397
6398 /* slow path, try all io-wq's */
Hao Xu3b44b372021-10-18 21:34:31 +08006399 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006400 ret = -ENOENT;
6401 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6402 struct io_uring_task *tctx = node->task->io_uring;
6403
Pavel Begunkov58f99372021-03-12 16:25:55 +00006404 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6405 if (ret != -ENOENT)
6406 break;
6407 }
Hao Xu3b44b372021-10-18 21:34:31 +08006408 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006409done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006410 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006411 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006412 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006413 return 0;
6414}
6415
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006416static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006417 const struct io_uring_sqe *sqe)
6418{
Daniele Albano61710e42020-07-18 14:15:16 -06006419 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6420 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006421 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006422 return -EINVAL;
6423
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006424 req->rsrc_update.offset = READ_ONCE(sqe->off);
6425 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6426 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006427 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006428 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006429 return 0;
6430}
6431
Pavel Begunkov889fca72021-02-10 00:03:09 +00006432static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006433{
6434 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08006435 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006436 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006437 int ret;
6438
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006439 up.offset = req->rsrc_update.offset;
6440 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006441 up.nr = 0;
6442 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006443 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006444
Hao Xu3b44b372021-10-18 21:34:31 +08006445 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006446 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006447 &up, req->rsrc_update.nr_args);
Hao Xu3b44b372021-10-18 21:34:31 +08006448 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006449
6450 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006451 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006452 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006453 return 0;
6454}
6455
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006456static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006457{
Jens Axboed625c6e2019-12-17 19:53:05 -07006458 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006459 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006460 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006461 case IORING_OP_READV:
6462 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006463 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006464 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006465 case IORING_OP_WRITEV:
6466 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006467 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006468 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006469 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006470 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006471 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006472 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006473 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006474 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006475 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006476 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006477 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006478 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006479 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006480 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006481 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006482 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006483 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006484 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006485 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006486 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006487 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006488 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006489 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006490 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006491 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006492 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006493 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006494 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006495 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006496 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006497 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006498 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006499 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006500 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006501 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006502 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006503 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006504 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006505 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006506 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006507 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006508 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006509 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006510 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006511 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006512 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006513 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006514 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006515 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006516 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006517 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006518 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006519 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006520 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006521 case IORING_OP_SHUTDOWN:
6522 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006523 case IORING_OP_RENAMEAT:
6524 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006525 case IORING_OP_UNLINKAT:
6526 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006527 case IORING_OP_MKDIRAT:
6528 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006529 case IORING_OP_SYMLINKAT:
6530 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006531 case IORING_OP_LINKAT:
6532 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006533 }
6534
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006535 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6536 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006537 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006538}
6539
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006540static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006541{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006542 if (!io_op_defs[req->opcode].needs_async_setup)
6543 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01006544 if (WARN_ON_ONCE(req_has_async_data(req)))
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006545 return -EFAULT;
6546 if (io_alloc_async_data(req))
6547 return -EAGAIN;
6548
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006549 switch (req->opcode) {
6550 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006551 return io_rw_prep_async(req, READ);
6552 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006553 return io_rw_prep_async(req, WRITE);
6554 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006555 return io_sendmsg_prep_async(req);
6556 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006557 return io_recvmsg_prep_async(req);
6558 case IORING_OP_CONNECT:
6559 return io_connect_prep_async(req);
6560 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006561 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6562 req->opcode);
6563 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006564}
6565
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006566static u32 io_get_sequence(struct io_kiocb *req)
6567{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006568 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006569
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006570 /* need original cached_sq_head, but it was increased for each req */
6571 io_for_each_link(req, req)
6572 seq--;
6573 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006574}
6575
Pavel Begunkovc0724812021-10-04 20:02:54 +01006576static __cold void io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006577{
6578 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006579 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006580 int ret;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006581 u32 seq = io_get_sequence(req);
Pavel Begunkov3c199662021-06-15 16:47:57 +01006582
Jens Axboedef596e2019-01-09 08:59:42 -07006583 /* Still need defer if there is pending req in defer list. */
Hao Xue302f102021-11-25 17:21:02 +08006584 spin_lock(&ctx->completion_lock);
Pavel Begunkov5e371262021-09-24 22:00:04 +01006585 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) {
Hao Xue302f102021-11-25 17:21:02 +08006586 spin_unlock(&ctx->completion_lock);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006587queue:
Pavel Begunkov10c66902021-06-15 16:47:56 +01006588 ctx->drain_active = false;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006589 io_req_task_queue(req);
6590 return;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006591 }
Hao Xue302f102021-11-25 17:21:02 +08006592 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006593
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006594 ret = io_req_prep_async(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006595 if (ret) {
6596fail:
6597 io_req_complete_failed(req, ret);
6598 return;
6599 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006600 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006601 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006602 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006603 ret = -ENOMEM;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006604 goto fail;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006605 }
Jens Axboe31b51512019-01-18 22:56:34 -07006606
Jens Axboe79ebeae2021-08-10 15:18:27 -06006607 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006608 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006609 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006610 kfree(de);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006611 goto queue;
Jens Axboe31b51512019-01-18 22:56:34 -07006612 }
6613
6614 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006615 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006616 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006617 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006618 spin_unlock(&ctx->completion_lock);
Jens Axboe31b51512019-01-18 22:56:34 -07006619}
6620
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006621static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006622{
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00006623 if (req->flags & REQ_F_BUFFER_SELECTED)
6624 io_put_kbuf(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006625
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006626 if (req->flags & REQ_F_NEED_CLEANUP) {
6627 switch (req->opcode) {
6628 case IORING_OP_READV:
6629 case IORING_OP_READ_FIXED:
6630 case IORING_OP_READ:
6631 case IORING_OP_WRITEV:
6632 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006633 case IORING_OP_WRITE: {
6634 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006635
6636 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006637 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006638 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006639 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006640 case IORING_OP_SENDMSG: {
6641 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006642
6643 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006644 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006645 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006646 case IORING_OP_SPLICE:
6647 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006648 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6649 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006650 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006651 case IORING_OP_OPENAT:
6652 case IORING_OP_OPENAT2:
6653 if (req->open.filename)
6654 putname(req->open.filename);
6655 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006656 case IORING_OP_RENAMEAT:
6657 putname(req->rename.oldpath);
6658 putname(req->rename.newpath);
6659 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006660 case IORING_OP_UNLINKAT:
6661 putname(req->unlink.filename);
6662 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006663 case IORING_OP_MKDIRAT:
6664 putname(req->mkdir.filename);
6665 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006666 case IORING_OP_SYMLINKAT:
6667 putname(req->symlink.oldpath);
6668 putname(req->symlink.newpath);
6669 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006670 case IORING_OP_LINKAT:
6671 putname(req->hardlink.oldpath);
6672 putname(req->hardlink.newpath);
6673 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006674 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006675 }
Jens Axboe75652a302021-04-15 09:52:40 -06006676 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6677 kfree(req->apoll->double_poll);
6678 kfree(req->apoll);
6679 req->apoll = NULL;
6680 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006681 if (req->flags & REQ_F_INFLIGHT) {
6682 struct io_uring_task *tctx = req->task->io_uring;
6683
6684 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006685 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006686 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006687 put_cred(req->creds);
Pavel Begunkovd886e182021-10-04 20:02:56 +01006688 if (req->flags & REQ_F_ASYNC_DATA) {
6689 kfree(req->async_data);
6690 req->async_data = NULL;
6691 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006692 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006693}
6694
Pavel Begunkov889fca72021-02-10 00:03:09 +00006695static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006696{
Jens Axboe5730b272021-02-27 15:57:30 -07006697 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006698 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006699
Pavel Begunkov6878b402021-09-24 21:59:41 +01006700 if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006701 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006702
Paul Moore5bd21822021-02-16 19:46:48 -05006703 if (!io_op_defs[req->opcode].audit_skip)
6704 audit_uring_entry(req->opcode);
6705
Jens Axboed625c6e2019-12-17 19:53:05 -07006706 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006707 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006708 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006709 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006710 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006711 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006712 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006713 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006714 break;
6715 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006716 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006717 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006718 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006719 break;
6720 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006721 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006722 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006723 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006724 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006725 break;
6726 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006727 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006728 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006729 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006730 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006731 break;
6732 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006733 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006734 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006735 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006736 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006737 break;
6738 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006739 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006740 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006741 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006742 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006743 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006744 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006745 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006746 break;
6747 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006748 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006749 break;
6750 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006751 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006752 break;
6753 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006754 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006755 break;
6756 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006757 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006758 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006759 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006760 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006761 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006762 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006763 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006764 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006765 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006766 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006767 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006768 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006769 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006770 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006771 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006772 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006773 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006774 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006775 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006776 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006777 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006778 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006779 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006780 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006781 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006782 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006783 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006784 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006785 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006786 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006787 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006788 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006789 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006790 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006791 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006792 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006793 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006794 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006795 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006796 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006797 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006798 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006799 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006800 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006801 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006802 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006803 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006804 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006805 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006806 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006807 case IORING_OP_MKDIRAT:
6808 ret = io_mkdirat(req, issue_flags);
6809 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006810 case IORING_OP_SYMLINKAT:
6811 ret = io_symlinkat(req, issue_flags);
6812 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006813 case IORING_OP_LINKAT:
6814 ret = io_linkat(req, issue_flags);
6815 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006816 default:
6817 ret = -EINVAL;
6818 break;
6819 }
Jens Axboe31b51512019-01-18 22:56:34 -07006820
Paul Moore5bd21822021-02-16 19:46:48 -05006821 if (!io_op_defs[req->opcode].audit_skip)
6822 audit_uring_exit(!ret, ret);
6823
Jens Axboe5730b272021-02-27 15:57:30 -07006824 if (creds)
6825 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006826 if (ret)
6827 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006828 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkov99830282021-10-15 17:09:11 +01006829 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file)
Pavel Begunkov98821312021-10-15 17:09:12 +01006830 io_iopoll_req_issued(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006831
6832 return 0;
6833}
6834
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006835static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6836{
6837 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6838
6839 req = io_put_req_find_next(req);
6840 return req ? &req->work : NULL;
6841}
6842
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006843static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006844{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006845 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006846 unsigned int issue_flags = IO_URING_F_UNLOCKED;
6847 bool needs_poll = false;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006848 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006849 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006850
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006851 /* one will be dropped by ->io_free_work() after returning to io-wq */
6852 if (!(req->flags & REQ_F_REFCOUNT))
6853 __io_req_set_refcount(req, 2);
6854 else
6855 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006856
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006857 timeout = io_prep_linked_timeout(req);
6858 if (timeout)
6859 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006860
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006861 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006862 if (work->flags & IO_WQ_WORK_CANCEL) {
6863 io_req_task_queue_fail(req, -ECANCELED);
6864 return;
Jens Axboe561fb042019-10-24 07:25:42 -06006865 }
Jens Axboe31b51512019-01-18 22:56:34 -07006866
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006867 if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovafb7f56f2021-10-23 12:13:59 +01006868 const struct io_op_def *def = &io_op_defs[req->opcode];
6869 bool opcode_poll = def->pollin || def->pollout;
6870
6871 if (opcode_poll && file_can_poll(req->file)) {
6872 needs_poll = true;
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006873 issue_flags |= IO_URING_F_NONBLOCK;
Pavel Begunkovafb7f56f2021-10-23 12:13:59 +01006874 }
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006875 }
Hao Xu90fa0282021-10-18 21:34:45 +08006876
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006877 do {
6878 ret = io_issue_sqe(req, issue_flags);
6879 if (ret != -EAGAIN)
6880 break;
6881 /*
6882 * We can get EAGAIN for iopolled IO even though we're
6883 * forcing a sync submission from here, since we can't
6884 * wait for request slots on the block side.
6885 */
6886 if (!needs_poll) {
6887 cond_resched();
6888 continue;
Hao Xu90fa0282021-10-18 21:34:45 +08006889 }
6890
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006891 if (io_arm_poll_handler(req) == IO_APOLL_OK)
6892 return;
6893 /* aborted or ready, in either case retry blocking */
6894 needs_poll = false;
6895 issue_flags &= ~IO_URING_F_NONBLOCK;
6896 } while (1);
Jens Axboe31b51512019-01-18 22:56:34 -07006897
Pavel Begunkova3df76982021-02-18 22:32:52 +00006898 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006899 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006900 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006901}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006902
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006903static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006904 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006905{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006906 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006907}
6908
Jens Axboe09bb8392019-03-13 12:39:28 -06006909static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6910 int index)
6911{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006912 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006913
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006914 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006915}
6916
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006917static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006918{
6919 unsigned long file_ptr = (unsigned long) file;
6920
Pavel Begunkov88459b52021-10-17 00:07:10 +01006921 file_ptr |= io_file_get_flags(file);
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006922 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006923}
6924
Pavel Begunkovac177052021-08-09 13:04:02 +01006925static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6926 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006927{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006928 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006929 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006930
Pavel Begunkovac177052021-08-09 13:04:02 +01006931 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6932 return NULL;
6933 fd = array_index_nospec(fd, ctx->nr_user_files);
6934 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6935 file = (struct file *) (file_ptr & FFS_MASK);
6936 file_ptr &= ~FFS_MASK;
6937 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01006938 req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
Pavel Begunkova46be972021-10-09 23:14:40 +01006939 io_req_set_rsrc_node(req, ctx);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006940 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006941}
6942
Pavel Begunkovac177052021-08-09 13:04:02 +01006943static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006944 struct io_kiocb *req, int fd)
6945{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006946 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006947
6948 trace_io_uring_file_get(ctx, fd);
6949
6950 /* we don't allow fixed io_uring files */
6951 if (file && unlikely(file->f_op == &io_uring_fops))
6952 io_req_track_inflight(req);
6953 return file;
6954}
6955
6956static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006957 struct io_kiocb *req, int fd, bool fixed)
6958{
6959 if (fixed)
6960 return io_file_get_fixed(ctx, req, fd);
6961 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006962 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006963}
6964
Pavel Begunkovf237c302021-08-18 12:42:46 +01006965static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006966{
6967 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006968 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006969
6970 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006971 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006972 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006973 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006974 } else {
6975 io_req_complete_post(req, -ETIME, 0);
6976 }
6977}
6978
Jens Axboe2665abf2019-11-05 12:40:47 -07006979static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6980{
Jens Axboead8a48a2019-11-15 08:49:11 -07006981 struct io_timeout_data *data = container_of(timer,
6982 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006983 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006984 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006985 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006986
Jens Axboe89b263f2021-08-10 15:14:18 -06006987 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006988 prev = req->timeout.head;
6989 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006990
6991 /*
6992 * We don't expect the list to be empty, that will only happen if we
6993 * race with the completion of the linked work.
6994 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006995 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006996 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006997 if (!req_ref_inc_not_zero(prev))
6998 prev = NULL;
6999 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06007000 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06007001 req->timeout.prev = prev;
7002 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07007003
Jens Axboe89b263f2021-08-10 15:14:18 -06007004 req->io_task_work.func = io_req_task_link_timeout;
Hao Xu4813c372021-12-07 17:39:48 +08007005 io_req_task_work_add(req, false);
Jens Axboe2665abf2019-11-05 12:40:47 -07007006 return HRTIMER_NORESTART;
7007}
7008
Pavel Begunkovde968c12021-03-19 17:22:33 +00007009static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07007010{
Pavel Begunkovde968c12021-03-19 17:22:33 +00007011 struct io_ring_ctx *ctx = req->ctx;
7012
Jens Axboe89b263f2021-08-10 15:14:18 -06007013 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07007014 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00007015 * If the back reference is NULL, then our linked request finished
7016 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07007017 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00007018 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07007019 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07007020
Jens Axboead8a48a2019-11-15 08:49:11 -07007021 data->timer.function = io_link_timeout_fn;
7022 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
7023 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06007024 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07007025 }
Jens Axboe89b263f2021-08-10 15:14:18 -06007026 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07007027 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07007028 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07007029}
7030
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01007031static void io_queue_sqe_arm_apoll(struct io_kiocb *req)
7032 __must_hold(&req->ctx->uring_lock)
7033{
7034 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
7035
7036 switch (io_arm_poll_handler(req)) {
7037 case IO_APOLL_READY:
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01007038 io_req_task_queue(req);
7039 break;
7040 case IO_APOLL_ABORTED:
7041 /*
7042 * Queued up for async execution, worker will release
7043 * submit reference when the iocb is actually submitted.
7044 */
7045 io_queue_async_work(req, NULL);
7046 break;
7047 }
7048
7049 if (linked_timeout)
7050 io_queue_linked_timeout(linked_timeout);
7051}
7052
7053static inline void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007054 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007055{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01007056 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06007057 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007058
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00007059 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06007060
Pavel Begunkovfff4e402021-10-04 20:02:48 +01007061 if (req->flags & REQ_F_COMPLETE_INLINE) {
7062 io_req_add_compl_list(req);
Pavel Begunkovd9f9d282021-09-24 22:00:00 +01007063 return;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01007064 }
Jens Axboe491381ce2019-10-17 09:20:46 -06007065 /*
7066 * We async punt it if the file wasn't marked NOWAIT, or if the file
7067 * doesn't support non-blocking read/write attempts
7068 */
Pavel Begunkov18400382021-03-19 17:22:34 +00007069 if (likely(!ret)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01007070 linked_timeout = io_prep_linked_timeout(req);
7071 if (linked_timeout)
7072 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00007073 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01007074 io_queue_sqe_arm_apoll(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01007075 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00007076 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06007077 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007078}
7079
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007080static void io_queue_sqe_fallback(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007081 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08007082{
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007083 if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01007084 io_req_complete_fail_submit(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01007085 } else if (unlikely(req->ctx->drain_active)) {
7086 io_drain_req(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01007087 } else {
7088 int ret = io_req_prep_async(req);
7089
7090 if (unlikely(ret))
7091 io_req_complete_failed(req, ret);
7092 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01007093 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07007094 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007095}
7096
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007097static inline void io_queue_sqe(struct io_kiocb *req)
7098 __must_hold(&req->ctx->uring_lock)
7099{
7100 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))))
7101 __io_queue_sqe(req);
7102 else
7103 io_queue_sqe_fallback(req);
7104}
7105
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007106/*
7107 * Check SQE restrictions (opcode and flags).
7108 *
7109 * Returns 'true' if SQE is allowed, 'false' otherwise.
7110 */
7111static inline bool io_check_restriction(struct io_ring_ctx *ctx,
7112 struct io_kiocb *req,
7113 unsigned int sqe_flags)
7114{
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007115 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
7116 return false;
7117
7118 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
7119 ctx->restrictions.sqe_flags_required)
7120 return false;
7121
7122 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
7123 ctx->restrictions.sqe_flags_required))
7124 return false;
7125
7126 return true;
7127}
7128
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007129static void io_init_req_drain(struct io_kiocb *req)
7130{
7131 struct io_ring_ctx *ctx = req->ctx;
7132 struct io_kiocb *head = ctx->submit_state.link.head;
7133
7134 ctx->drain_active = true;
7135 if (head) {
7136 /*
7137 * If we need to drain a request in the middle of a link, drain
7138 * the head request and the next request/link after the current
7139 * link. Considering sequential execution of links,
Hao Xub6c7db32021-11-25 17:21:03 +08007140 * REQ_F_IO_DRAIN will be maintained for every request of our
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007141 * link.
7142 */
Hao Xub6c7db32021-11-25 17:21:03 +08007143 head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007144 ctx->drain_next = true;
7145 }
7146}
7147
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007148static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007149 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007150 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007151{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007152 unsigned int sqe_flags;
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007153 int personality;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007154 u8 opcode;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007155
Pavel Begunkov864ea922021-08-09 13:04:08 +01007156 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007157 req->opcode = opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007158 /* same numerical values with corresponding REQ_F_*, safe to copy */
7159 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007160 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007161 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007162 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03007163 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007164
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007165 if (unlikely(opcode >= IORING_OP_LAST)) {
7166 req->opcode = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007167 return -EINVAL;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007168 }
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007169 if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
7170 /* enforce forwards compatibility on users */
7171 if (sqe_flags & ~SQE_VALID_FLAGS)
7172 return -EINVAL;
7173 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007174 !io_op_defs[opcode].buffer_select)
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007175 return -EOPNOTSUPP;
Pavel Begunkov5562a8d2021-11-10 15:49:34 +00007176 if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
7177 ctx->drain_disabled = true;
7178 if (sqe_flags & IOSQE_IO_DRAIN) {
7179 if (ctx->drain_disabled)
7180 return -EOPNOTSUPP;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007181 io_init_req_drain(req);
Pavel Begunkov5562a8d2021-11-10 15:49:34 +00007182 }
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007183 }
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007184 if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) {
7185 if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags))
7186 return -EACCES;
7187 /* knock it to the slow queue path, will be drained there */
7188 if (ctx->drain_active)
7189 req->flags |= REQ_F_FORCE_ASYNC;
7190 /* if there is no link, we're at "next" request and need to drain */
7191 if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
7192 ctx->drain_next = false;
7193 ctx->drain_active = true;
Hao Xub6c7db32021-11-25 17:21:03 +08007194 req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007195 }
7196 }
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007197
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007198 if (io_op_defs[opcode].needs_file) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007199 struct io_submit_state *state = &ctx->submit_state;
7200
7201 /*
7202 * Plug now if we have more than 2 IO left after this, and the
7203 * target is potentially a read/write to block based storage.
7204 */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007205 if (state->need_plug && io_op_defs[opcode].plug) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007206 state->plug_started = true;
7207 state->need_plug = false;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -06007208 blk_start_plug_nr_ios(&state->plug, state->submit_nr);
Pavel Begunkov6d634162021-10-06 16:06:46 +01007209 }
7210
Pavel Begunkov62906e82021-08-10 14:52:47 +01007211 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007212 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007213 if (unlikely(!req->file))
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007214 return -EBADF;
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007215 }
Pavel Begunkovc11368a52020-05-17 14:13:42 +03007216
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007217 personality = READ_ONCE(sqe->personality);
Jens Axboe63ff8222020-05-07 14:56:15 -06007218 if (personality) {
Linus Torvaldscdab10b2021-11-01 21:06:18 -07007219 int ret;
7220
Jens Axboe63ff8222020-05-07 14:56:15 -06007221 req->creds = xa_load(&ctx->personalities, personality);
7222 if (!req->creds)
Jens Axboe27926b62020-10-28 09:33:23 -06007223 return -EINVAL;
Jens Axboe63ff8222020-05-07 14:56:15 -06007224 get_cred(req->creds);
Paul Moorecdc14042021-02-01 19:56:49 -05007225 ret = security_uring_override_creds(req->creds);
7226 if (ret) {
7227 put_cred(req->creds);
7228 return ret;
7229 }
Jens Axboe63ff8222020-05-07 14:56:15 -06007230 req->flags |= REQ_F_CREDS;
7231 }
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007232
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007233 return io_req_prep(req, sqe);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007234}
7235
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007236static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007237 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007238 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007239{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007240 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007241 int ret;
7242
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007243 ret = io_init_req(ctx, req, sqe);
7244 if (unlikely(ret)) {
Jens Axboea87acfd2021-09-11 16:04:50 -06007245 trace_io_uring_req_failed(sqe, ret);
7246
Hao Xua8295b92021-08-27 17:46:09 +08007247 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007248 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007249 /*
7250 * we can judge a link req is failed or cancelled by if
7251 * REQ_F_FAIL is set, but the head is an exception since
7252 * it may be set REQ_F_FAIL because of other req's failure
7253 * so let's leverage req->result to distinguish if a head
7254 * is set REQ_F_FAIL because of its failure or other req's
7255 * failure so that we can set the correct ret code for it.
7256 * init result here to avoid affecting the normal path.
7257 */
7258 if (!(link->head->flags & REQ_F_FAIL))
7259 req_fail_link_node(link->head, -ECANCELED);
7260 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7261 /*
7262 * the current req is a normal req, we should return
7263 * error and thus break the submittion loop.
7264 */
7265 io_req_complete_failed(req, ret);
7266 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007267 }
Hao Xua8295b92021-08-27 17:46:09 +08007268 req_fail_link_node(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007269 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007270
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007271 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007272 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7273 req->flags, true,
7274 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007275
Jens Axboe6c271ce2019-01-10 11:22:30 -07007276 /*
7277 * If we already have a head request, queue this one for async
7278 * submittal once the head completes. If we don't have a head but
7279 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7280 * submitted sync once the chain is complete. If none of those
7281 * conditions are true (normal request), then just queue it.
7282 */
7283 if (link->head) {
7284 struct io_kiocb *head = link->head;
7285
Hao Xua8295b92021-08-27 17:46:09 +08007286 if (!(req->flags & REQ_F_FAIL)) {
7287 ret = io_req_prep_async(req);
7288 if (unlikely(ret)) {
7289 req_fail_link_node(req, ret);
7290 if (!(head->flags & REQ_F_FAIL))
7291 req_fail_link_node(head, -ECANCELED);
7292 }
7293 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007294 trace_io_uring_link(ctx, req, head);
7295 link->last->link = req;
7296 link->last = req;
7297
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007298 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK))
7299 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007300 /* last request of a link, enqueue the link */
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007301 link->head = NULL;
7302 req = head;
7303 } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
7304 link->head = req;
7305 link->last = req;
7306 return 0;
Jackie Liu4fe2c962019-09-09 20:50:40 +08007307 }
7308
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007309 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007310 return 0;
7311}
7312
7313/*
7314 * Batched submission is done, ensure local IO is flushed out.
7315 */
Pavel Begunkov553deff2021-09-24 21:59:55 +01007316static void io_submit_state_end(struct io_ring_ctx *ctx)
Jens Axboe3529d8c2019-12-19 18:24:38 -07007317{
Pavel Begunkov553deff2021-09-24 21:59:55 +01007318 struct io_submit_state *state = &ctx->submit_state;
7319
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007320 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007321 io_queue_sqe(state->link.head);
Pavel Begunkov553deff2021-09-24 21:59:55 +01007322 /* flush only after queuing links as they can generate completions */
Pavel Begunkovc4501782021-09-08 16:40:52 +01007323 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007324 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007325 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007326}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007327
Jens Axboe9e645e112019-05-10 16:07:28 -06007328/*
7329 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007330 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007331static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007332 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007333{
7334 state->plug_started = false;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007335 state->need_plug = max_ios > 2;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -06007336 state->submit_nr = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007337 /* set only head, no need to init link_last in advance */
7338 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007339}
7340
Jens Axboe193155c2020-02-22 23:22:19 -07007341static void io_commit_sqring(struct io_ring_ctx *ctx)
7342{
Jens Axboe75c6a032020-01-28 10:15:23 -07007343 struct io_rings *rings = ctx->rings;
7344
7345 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007346 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007347 * since once we write the new head, the application could
7348 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007349 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007350 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007351}
7352
Jens Axboe9e645e112019-05-10 16:07:28 -06007353/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007354 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007355 * that is mapped by userspace. This means that care needs to be taken to
7356 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007357 * being a good citizen. If members of the sqe are validated and then later
7358 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007359 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007360 */
7361static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007362{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007363 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007364 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007365
7366 /*
7367 * The cached sq head (or cq tail) serves two purposes:
7368 *
7369 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007370 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007371 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007372 * though the application is the one updating it.
7373 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007374 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007375 if (likely(head < ctx->sq_entries))
7376 return &ctx->sq_sqes[head];
7377
7378 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007379 ctx->cq_extra--;
7380 WRITE_ONCE(ctx->rings->sq_dropped,
7381 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007382 return NULL;
7383}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007384
Jens Axboe0f212202020-09-13 13:09:39 -06007385static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007386 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007387{
Pavel Begunkov69629802021-09-24 22:00:01 +01007388 unsigned int entries = io_sqring_entries(ctx);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007389 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007390
Pavel Begunkov51d48da2021-10-04 20:02:47 +01007391 if (unlikely(!entries))
Pavel Begunkov69629802021-09-24 22:00:01 +01007392 return 0;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03007393 /* make sure SQ entry isn't read before tail */
Pavel Begunkov69629802021-09-24 22:00:01 +01007394 nr = min3(nr, ctx->sq_entries, entries);
Pavel Begunkov9a108672021-08-27 11:55:01 +01007395 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007396
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007397 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov69629802021-09-24 22:00:01 +01007398 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007399 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007400 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007401
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007402 if (unlikely(!io_alloc_req_refill(ctx))) {
Pavel Begunkov196be952019-11-07 01:41:06 +03007403 if (!submitted)
7404 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007405 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007406 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007407 req = io_alloc_req(ctx);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007408 sqe = io_get_sqe(ctx);
7409 if (unlikely(!sqe)) {
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01007410 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007411 break;
7412 }
Jens Axboed3656342019-12-18 09:50:26 -07007413 /* will complete beyond this point, count as submitted */
7414 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007415 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007416 break;
Pavel Begunkov69629802021-09-24 22:00:01 +01007417 } while (submitted < nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007418
Pavel Begunkov9466f432020-01-25 22:34:01 +03007419 if (unlikely(submitted != nr)) {
7420 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007421 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007422
Pavel Begunkov09899b12021-06-14 02:36:22 +01007423 current->io_uring->cached_refs += unused;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007424 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007425
Pavel Begunkov553deff2021-09-24 21:59:55 +01007426 io_submit_state_end(ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007427 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7428 io_commit_sqring(ctx);
7429
Jens Axboe6c271ce2019-01-10 11:22:30 -07007430 return submitted;
7431}
7432
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007433static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7434{
7435 return READ_ONCE(sqd->state);
7436}
7437
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007438static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7439{
7440 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007441 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007442 WRITE_ONCE(ctx->rings->sq_flags,
7443 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007444 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007445}
7446
7447static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7448{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007449 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007450 WRITE_ONCE(ctx->rings->sq_flags,
7451 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007452 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007453}
7454
Xiaoguang Wang08369242020-11-03 14:15:59 +08007455static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007456{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007457 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007458 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007459
Jens Axboec8d1ba52020-09-14 11:07:26 -06007460 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007461 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007462 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7463 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007464
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007465 if (!wq_list_empty(&ctx->iopoll_list) || to_submit) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007466 const struct cred *creds = NULL;
7467
7468 if (ctx->sq_creds != current_cred())
7469 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007470
Xiaoguang Wang08369242020-11-03 14:15:59 +08007471 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007472 if (!wq_list_empty(&ctx->iopoll_list))
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01007473 io_do_iopoll(ctx, true);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007474
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007475 /*
7476 * Don't submit if refs are dying, good for io_uring_register(),
7477 * but also it is relied upon by io_ring_exit_work()
7478 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007479 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7480 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007481 ret = io_submit_sqes(ctx, to_submit);
7482 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007483
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007484 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7485 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007486 if (creds)
7487 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007488 }
Jens Axboe90554202020-09-03 12:12:41 -06007489
Xiaoguang Wang08369242020-11-03 14:15:59 +08007490 return ret;
7491}
7492
Pavel Begunkovc0724812021-10-04 20:02:54 +01007493static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd)
Xiaoguang Wang08369242020-11-03 14:15:59 +08007494{
7495 struct io_ring_ctx *ctx;
7496 unsigned sq_thread_idle = 0;
7497
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007498 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7499 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007500 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007501}
7502
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007503static bool io_sqd_handle_event(struct io_sq_data *sqd)
7504{
7505 bool did_sig = false;
7506 struct ksignal ksig;
7507
7508 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7509 signal_pending(current)) {
7510 mutex_unlock(&sqd->lock);
7511 if (signal_pending(current))
7512 did_sig = get_signal(&ksig);
7513 cond_resched();
7514 mutex_lock(&sqd->lock);
7515 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007516 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7517}
7518
Jens Axboe6c271ce2019-01-10 11:22:30 -07007519static int io_sq_thread(void *data)
7520{
Jens Axboe69fb2132020-09-14 11:16:23 -06007521 struct io_sq_data *sqd = data;
7522 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007523 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007524 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007525 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007526
Pavel Begunkov696ee882021-04-01 09:55:04 +01007527 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007528 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007529
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007530 if (sqd->sq_cpu != -1)
7531 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7532 else
7533 set_cpus_allowed_ptr(current, cpu_online_mask);
7534 current->flags |= PF_NO_SETAFFINITY;
7535
Paul Moore5bd21822021-02-16 19:46:48 -05007536 audit_alloc_kernel(current);
7537
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007538 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007539 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007540 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007541
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007542 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7543 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007544 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007545 timeout = jiffies + sqd->sq_thread_idle;
7546 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007547
Jens Axboee95eee22020-09-08 09:11:32 -06007548 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007549 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007550 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007551
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007552 if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007553 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007554 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007555 if (io_run_task_work())
7556 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007557
Xiaoguang Wang08369242020-11-03 14:15:59 +08007558 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007559 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007560 if (sqt_spin)
7561 timeout = jiffies + sqd->sq_thread_idle;
7562 continue;
7563 }
7564
Xiaoguang Wang08369242020-11-03 14:15:59 +08007565 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007566 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007567 bool needs_sched = true;
7568
Hao Xu724cb4f2021-04-21 23:19:11 +08007569 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007570 io_ring_set_wakeup_flag(ctx);
7571
Hao Xu724cb4f2021-04-21 23:19:11 +08007572 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007573 !wq_list_empty(&ctx->iopoll_list)) {
Hao Xu724cb4f2021-04-21 23:19:11 +08007574 needs_sched = false;
7575 break;
7576 }
7577 if (io_sqring_entries(ctx)) {
7578 needs_sched = false;
7579 break;
7580 }
7581 }
7582
7583 if (needs_sched) {
7584 mutex_unlock(&sqd->lock);
7585 schedule();
7586 mutex_lock(&sqd->lock);
7587 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007588 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7589 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007590 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007591
7592 finish_wait(&sqd->wait, &wait);
7593 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007594 }
7595
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007596 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007597 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007598 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007599 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007600 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007601 mutex_unlock(&sqd->lock);
7602
Paul Moore5bd21822021-02-16 19:46:48 -05007603 audit_free(current);
7604
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007605 complete(&sqd->exited);
7606 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007607}
7608
Jens Axboebda52162019-09-24 13:47:15 -06007609struct io_wait_queue {
7610 struct wait_queue_entry wq;
7611 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007612 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007613 unsigned nr_timeouts;
7614};
7615
Pavel Begunkov6c503152021-01-04 20:36:36 +00007616static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007617{
7618 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007619 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007620
7621 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007622 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007623 * started waiting. For timeouts, we always want to return to userspace,
7624 * regardless of event count.
7625 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007626 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007627}
7628
7629static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7630 int wake_flags, void *key)
7631{
7632 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7633 wq);
7634
Pavel Begunkov6c503152021-01-04 20:36:36 +00007635 /*
7636 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7637 * the task, and the next invocation will do it.
7638 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007639 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007640 return autoremove_wake_function(curr, mode, wake_flags, key);
7641 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007642}
7643
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007644static int io_run_task_work_sig(void)
7645{
7646 if (io_run_task_work())
7647 return 1;
7648 if (!signal_pending(current))
7649 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007650 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007651 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007652 return -EINTR;
7653}
7654
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007655/* when returns >0, the caller should retry */
7656static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7657 struct io_wait_queue *iowq,
7658 signed long *timeout)
7659{
7660 int ret;
7661
7662 /* make sure we run task_work before checking for signals */
7663 ret = io_run_task_work_sig();
7664 if (ret || io_should_wake(iowq))
7665 return ret;
7666 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007667 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007668 return 1;
7669
7670 *timeout = schedule_timeout(*timeout);
7671 return !*timeout ? -ETIME : 1;
7672}
7673
Jens Axboe2b188cc2019-01-07 10:46:33 -07007674/*
7675 * Wait until events become available, if we don't already have some. The
7676 * application must reap them itself, as they reside on the shared cq ring.
7677 */
7678static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007679 const sigset_t __user *sig, size_t sigsz,
7680 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007681{
Pavel Begunkov902910992021-08-09 09:07:32 -06007682 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007683 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007684 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7685 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007686
Jens Axboeb41e9852020-02-17 09:52:41 -07007687 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007688 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007689 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007690 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007691 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007692 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007693 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007694
Xiaoguang Wang44df58d2021-09-14 22:38:52 +08007695 if (uts) {
7696 struct timespec64 ts;
7697
7698 if (get_timespec64(&ts, uts))
7699 return -EFAULT;
7700 timeout = timespec64_to_jiffies(&ts);
7701 }
7702
Jens Axboe2b188cc2019-01-07 10:46:33 -07007703 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007704#ifdef CONFIG_COMPAT
7705 if (in_compat_syscall())
7706 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007707 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007708 else
7709#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007710 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007711
Jens Axboe2b188cc2019-01-07 10:46:33 -07007712 if (ret)
7713 return ret;
7714 }
7715
Pavel Begunkov902910992021-08-09 09:07:32 -06007716 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7717 iowq.wq.private = current;
7718 INIT_LIST_HEAD(&iowq.wq.entry);
7719 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007720 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007721 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007722
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007723 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007724 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007725 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007726 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007727 ret = -EBUSY;
7728 break;
7729 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007730 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007731 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007732 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007733 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007734 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007735 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007736
Jens Axboeb7db41c2020-07-04 08:55:50 -06007737 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007738
Hristo Venev75b28af2019-08-26 17:23:46 +00007739 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007740}
7741
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007742static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007743{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007744 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007745
7746 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007747 kfree(table[i]);
7748 kfree(table);
7749}
7750
Pavel Begunkovc0724812021-10-04 20:02:54 +01007751static __cold void **io_alloc_page_table(size_t size)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007752{
7753 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7754 size_t init_size = size;
7755 void **table;
7756
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007757 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007758 if (!table)
7759 return NULL;
7760
7761 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007762 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007763
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007764 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007765 if (!table[i]) {
7766 io_free_page_table(table, init_size);
7767 return NULL;
7768 }
7769 size -= this_size;
7770 }
7771 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007772}
7773
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007774static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7775{
7776 percpu_ref_exit(&ref_node->refs);
7777 kfree(ref_node);
7778}
7779
Pavel Begunkovc0724812021-10-04 20:02:54 +01007780static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007781{
7782 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7783 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7784 unsigned long flags;
7785 bool first_add = false;
7786
7787 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7788 node->done = true;
7789
7790 while (!list_empty(&ctx->rsrc_ref_list)) {
7791 node = list_first_entry(&ctx->rsrc_ref_list,
7792 struct io_rsrc_node, node);
7793 /* recycle ref nodes in order */
7794 if (!node->done)
7795 break;
7796 list_del(&node->node);
7797 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7798 }
7799 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7800
7801 if (first_add)
7802 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7803}
7804
7805static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7806{
7807 struct io_rsrc_node *ref_node;
7808
7809 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7810 if (!ref_node)
7811 return NULL;
7812
7813 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7814 0, GFP_KERNEL)) {
7815 kfree(ref_node);
7816 return NULL;
7817 }
7818 INIT_LIST_HEAD(&ref_node->node);
7819 INIT_LIST_HEAD(&ref_node->rsrc_list);
7820 ref_node->done = false;
7821 return ref_node;
7822}
7823
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007824static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7825 struct io_rsrc_data *data_to_kill)
Pavel Begunkovab409402021-10-09 23:14:41 +01007826 __must_hold(&ctx->uring_lock)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007827{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007828 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7829 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007830
Pavel Begunkovab409402021-10-09 23:14:41 +01007831 io_rsrc_refs_drop(ctx);
7832
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007833 if (data_to_kill) {
7834 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007835
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007836 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007837 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007838 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007839 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007840
Pavel Begunkov3e942492021-04-11 01:46:34 +01007841 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007842 percpu_ref_kill(&rsrc_node->refs);
7843 ctx->rsrc_node = NULL;
7844 }
7845
7846 if (!ctx->rsrc_node) {
7847 ctx->rsrc_node = ctx->rsrc_backup_node;
7848 ctx->rsrc_backup_node = NULL;
7849 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007850}
7851
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007852static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007853{
7854 if (ctx->rsrc_backup_node)
7855 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007856 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007857 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7858}
7859
Pavel Begunkovc0724812021-10-04 20:02:54 +01007860static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
7861 struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007862{
7863 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007864
Pavel Begunkov215c3902021-04-01 15:43:48 +01007865 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007866 if (data->quiesce)
7867 return -ENXIO;
7868
7869 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007870 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007871 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007872 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007873 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007874 io_rsrc_node_switch(ctx, data);
7875
Pavel Begunkov3e942492021-04-11 01:46:34 +01007876 /* kill initial ref, already quiesced if zero */
7877 if (atomic_dec_and_test(&data->refs))
7878 break;
Jens Axboec018db42021-08-09 08:15:50 -06007879 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007880 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007881 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007882 if (!ret) {
7883 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007884 break;
Jens Axboec018db42021-08-09 08:15:50 -06007885 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007886
Pavel Begunkov3e942492021-04-11 01:46:34 +01007887 atomic_inc(&data->refs);
7888 /* wait for all works potentially completing data->done */
7889 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007890 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007891
Hao Xu8bad28d2021-02-19 17:19:36 +08007892 ret = io_run_task_work_sig();
7893 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007894 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007895 data->quiesce = false;
7896
Hao Xu8bad28d2021-02-19 17:19:36 +08007897 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007898}
7899
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007900static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7901{
7902 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7903 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7904
7905 return &data->tags[table_idx][off];
7906}
7907
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007908static void io_rsrc_data_free(struct io_rsrc_data *data)
7909{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007910 size_t size = data->nr * sizeof(data->tags[0][0]);
7911
7912 if (data->tags)
7913 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007914 kfree(data);
7915}
7916
Pavel Begunkovc0724812021-10-04 20:02:54 +01007917static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7918 u64 __user *utags, unsigned nr,
7919 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007920{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007921 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007922 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007923 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007924
7925 data = kzalloc(sizeof(*data), GFP_KERNEL);
7926 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007927 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007928 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007929 if (!data->tags) {
7930 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007931 return -ENOMEM;
7932 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007933
7934 data->nr = nr;
7935 data->ctx = ctx;
7936 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007937 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007938 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007939 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007940 u64 *tag_slot = io_get_tag_slot(data, i);
7941
7942 if (copy_from_user(tag_slot, &utags[i],
7943 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007944 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007945 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007946 }
7947
Pavel Begunkov3e942492021-04-11 01:46:34 +01007948 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007949 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007950 *pdata = data;
7951 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007952fail:
7953 io_rsrc_data_free(data);
7954 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007955}
7956
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007957static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7958{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007959 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7960 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007961 return !!table->files;
7962}
7963
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007964static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007965{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007966 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007967 table->files = NULL;
7968}
7969
Jens Axboe2b188cc2019-01-07 10:46:33 -07007970static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7971{
7972#if defined(CONFIG_UNIX)
7973 if (ctx->ring_sock) {
7974 struct sock *sock = ctx->ring_sock->sk;
7975 struct sk_buff *skb;
7976
7977 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7978 kfree_skb(skb);
7979 }
7980#else
7981 int i;
7982
7983 for (i = 0; i < ctx->nr_user_files; i++) {
7984 struct file *file;
7985
7986 file = io_file_from_index(ctx, i);
7987 if (file)
7988 fput(file);
7989 }
7990#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007991 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007992 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007993 ctx->file_data = NULL;
7994 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007995}
7996
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007997static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7998{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007999 int ret;
8000
Pavel Begunkov08480402021-04-13 02:58:38 +01008001 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00008002 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01008003 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
8004 if (!ret)
8005 __io_sqe_files_unregister(ctx);
8006 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07008007}
8008
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008009static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008010 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008011{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008012 WARN_ON_ONCE(sqd->thread == current);
8013
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008014 /*
8015 * Do the dance but not conditional clear_bit() because it'd race with
8016 * other threads incrementing park_pending and setting the bit.
8017 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008018 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008019 if (atomic_dec_return(&sqd->park_pending))
8020 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008021 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008022}
8023
Jens Axboe86e0d672021-03-05 08:44:39 -07008024static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008025 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008026{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008027 WARN_ON_ONCE(sqd->thread == current);
8028
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008029 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008030 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008031 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07008032 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07008033 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008034}
8035
8036static void io_sq_thread_stop(struct io_sq_data *sqd)
8037{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008038 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01008039 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008040
Jens Axboe05962f92021-03-06 13:58:48 -07008041 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01008042 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07008043 if (sqd->thread)
8044 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008045 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07008046 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008047}
8048
Jens Axboe534ca6d2020-09-02 13:52:19 -06008049static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07008050{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008051 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008052 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
8053
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008054 io_sq_thread_stop(sqd);
8055 kfree(sqd);
8056 }
8057}
8058
8059static void io_sq_thread_finish(struct io_ring_ctx *ctx)
8060{
8061 struct io_sq_data *sqd = ctx->sq_data;
8062
8063 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07008064 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008065 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008066 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07008067 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008068
8069 io_put_sq_data(sqd);
8070 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008071 }
8072}
8073
Jens Axboeaa061652020-09-02 14:50:27 -06008074static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
8075{
8076 struct io_ring_ctx *ctx_attach;
8077 struct io_sq_data *sqd;
8078 struct fd f;
8079
8080 f = fdget(p->wq_fd);
8081 if (!f.file)
8082 return ERR_PTR(-ENXIO);
8083 if (f.file->f_op != &io_uring_fops) {
8084 fdput(f);
8085 return ERR_PTR(-EINVAL);
8086 }
8087
8088 ctx_attach = f.file->private_data;
8089 sqd = ctx_attach->sq_data;
8090 if (!sqd) {
8091 fdput(f);
8092 return ERR_PTR(-EINVAL);
8093 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07008094 if (sqd->task_tgid != current->tgid) {
8095 fdput(f);
8096 return ERR_PTR(-EPERM);
8097 }
Jens Axboeaa061652020-09-02 14:50:27 -06008098
8099 refcount_inc(&sqd->refs);
8100 fdput(f);
8101 return sqd;
8102}
8103
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008104static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
8105 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008106{
8107 struct io_sq_data *sqd;
8108
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008109 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008110 if (p->flags & IORING_SETUP_ATTACH_WQ) {
8111 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008112 if (!IS_ERR(sqd)) {
8113 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008114 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008115 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07008116 /* fall through for EPERM case, setup new sqd/task */
8117 if (PTR_ERR(sqd) != -EPERM)
8118 return sqd;
8119 }
Jens Axboeaa061652020-09-02 14:50:27 -06008120
Jens Axboe534ca6d2020-09-02 13:52:19 -06008121 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
8122 if (!sqd)
8123 return ERR_PTR(-ENOMEM);
8124
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008125 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008126 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06008127 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008128 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008129 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008130 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008131 return sqd;
8132}
8133
Jens Axboe6b063142019-01-10 22:13:58 -07008134#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07008135/*
8136 * Ensure the UNIX gc is aware of our file set, so we are certain that
8137 * the io_uring can be safely unregistered on process exit, even if we have
8138 * loops in the file referencing.
8139 */
8140static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
8141{
8142 struct sock *sk = ctx->ring_sock->sk;
8143 struct scm_fp_list *fpl;
8144 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06008145 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07008146
Jens Axboe6b063142019-01-10 22:13:58 -07008147 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
8148 if (!fpl)
8149 return -ENOMEM;
8150
8151 skb = alloc_skb(0, GFP_KERNEL);
8152 if (!skb) {
8153 kfree(fpl);
8154 return -ENOMEM;
8155 }
8156
8157 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07008158
Jens Axboe08a45172019-10-03 08:11:03 -06008159 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07008160 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07008161 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008162 struct file *file = io_file_from_index(ctx, i + offset);
8163
8164 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06008165 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06008166 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06008167 unix_inflight(fpl->user, fpl->fp[nr_files]);
8168 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07008169 }
8170
Jens Axboe08a45172019-10-03 08:11:03 -06008171 if (nr_files) {
8172 fpl->max = SCM_MAX_FD;
8173 fpl->count = nr_files;
8174 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008175 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06008176 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
8177 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07008178
Jens Axboe08a45172019-10-03 08:11:03 -06008179 for (i = 0; i < nr_files; i++)
8180 fput(fpl->fp[i]);
8181 } else {
8182 kfree_skb(skb);
8183 kfree(fpl);
8184 }
Jens Axboe6b063142019-01-10 22:13:58 -07008185
8186 return 0;
8187}
8188
8189/*
8190 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8191 * causes regular reference counting to break down. We rely on the UNIX
8192 * garbage collection to take care of this problem for us.
8193 */
8194static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8195{
8196 unsigned left, total;
8197 int ret = 0;
8198
8199 total = 0;
8200 left = ctx->nr_user_files;
8201 while (left) {
8202 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008203
8204 ret = __io_sqe_files_scm(ctx, this_files, total);
8205 if (ret)
8206 break;
8207 left -= this_files;
8208 total += this_files;
8209 }
8210
8211 if (!ret)
8212 return 0;
8213
8214 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008215 struct file *file = io_file_from_index(ctx, total);
8216
8217 if (file)
8218 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008219 total++;
8220 }
8221
8222 return ret;
8223}
8224#else
8225static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8226{
8227 return 0;
8228}
8229#endif
8230
Pavel Begunkov47e90392021-04-01 15:43:56 +01008231static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008232{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008233 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008234#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008235 struct sock *sock = ctx->ring_sock->sk;
8236 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8237 struct sk_buff *skb;
8238 int i;
8239
8240 __skb_queue_head_init(&list);
8241
8242 /*
8243 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8244 * remove this entry and rearrange the file array.
8245 */
8246 skb = skb_dequeue(head);
8247 while (skb) {
8248 struct scm_fp_list *fp;
8249
8250 fp = UNIXCB(skb).fp;
8251 for (i = 0; i < fp->count; i++) {
8252 int left;
8253
8254 if (fp->fp[i] != file)
8255 continue;
8256
8257 unix_notinflight(fp->user, fp->fp[i]);
8258 left = fp->count - 1 - i;
8259 if (left) {
8260 memmove(&fp->fp[i], &fp->fp[i + 1],
8261 left * sizeof(struct file *));
8262 }
8263 fp->count--;
8264 if (!fp->count) {
8265 kfree_skb(skb);
8266 skb = NULL;
8267 } else {
8268 __skb_queue_tail(&list, skb);
8269 }
8270 fput(file);
8271 file = NULL;
8272 break;
8273 }
8274
8275 if (!file)
8276 break;
8277
8278 __skb_queue_tail(&list, skb);
8279
8280 skb = skb_dequeue(head);
8281 }
8282
8283 if (skb_peek(&list)) {
8284 spin_lock_irq(&head->lock);
8285 while ((skb = __skb_dequeue(&list)) != NULL)
8286 __skb_queue_tail(head, skb);
8287 spin_unlock_irq(&head->lock);
8288 }
8289#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008290 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008291#endif
8292}
8293
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008294static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008295{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008296 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008297 struct io_ring_ctx *ctx = rsrc_data->ctx;
8298 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008299
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008300 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8301 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008302
8303 if (prsrc->tag) {
8304 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008305
8306 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008307 spin_lock(&ctx->completion_lock);
Pavel Begunkov913a5712021-11-10 15:49:31 +00008308 io_fill_cqe_aux(ctx, prsrc->tag, 0, 0);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008309 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008310 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008311 io_cqring_ev_posted(ctx);
8312 io_ring_submit_unlock(ctx, lock_ring);
8313 }
8314
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008315 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008316 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008317 }
8318
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008319 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008320 if (atomic_dec_and_test(&rsrc_data->refs))
8321 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008322}
8323
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008324static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008325{
8326 struct io_ring_ctx *ctx;
8327 struct llist_node *node;
8328
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008329 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8330 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008331
8332 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008333 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008334 struct llist_node *next = node->next;
8335
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008336 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008337 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008338 node = next;
8339 }
8340}
8341
Jens Axboe05f3fb32019-12-09 11:22:50 -07008342static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008343 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008344{
8345 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008346 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008347 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008348 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008349
8350 if (ctx->file_data)
8351 return -EBUSY;
8352 if (!nr_args)
8353 return -EINVAL;
8354 if (nr_args > IORING_MAX_FIXED_FILES)
8355 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008356 if (nr_args > rlimit(RLIMIT_NOFILE))
8357 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008358 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008359 if (ret)
8360 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008361 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8362 &ctx->file_data);
8363 if (ret)
8364 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008365
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008366 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008367 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008368 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008369
Jens Axboe05f3fb32019-12-09 11:22:50 -07008370 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008371 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008372 ret = -EFAULT;
8373 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008374 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008375 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008376 if (fd == -1) {
8377 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008378 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008379 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008380 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008381 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008382
Jens Axboe05f3fb32019-12-09 11:22:50 -07008383 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008384 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008385 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008386 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008387
8388 /*
8389 * Don't allow io_uring instances to be registered. If UNIX
8390 * isn't enabled, then this causes a reference cycle and this
8391 * instance can never get freed. If UNIX is enabled we'll
8392 * handle it just fine, but there's still no point in allowing
8393 * a ring fd as it doesn't support regular read/write anyway.
8394 */
8395 if (file->f_op == &io_uring_fops) {
8396 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008397 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008398 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008399 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008400 }
8401
Jens Axboe05f3fb32019-12-09 11:22:50 -07008402 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008403 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008404 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008405 return ret;
8406 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008407
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008408 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008409 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008410out_fput:
8411 for (i = 0; i < ctx->nr_user_files; i++) {
8412 file = io_file_from_index(ctx, i);
8413 if (file)
8414 fput(file);
8415 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008416 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008417 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008418out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008419 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008420 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008421 return ret;
8422}
8423
Jens Axboec3a31e62019-10-03 13:59:56 -06008424static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8425 int index)
8426{
8427#if defined(CONFIG_UNIX)
8428 struct sock *sock = ctx->ring_sock->sk;
8429 struct sk_buff_head *head = &sock->sk_receive_queue;
8430 struct sk_buff *skb;
8431
8432 /*
8433 * See if we can merge this file into an existing skb SCM_RIGHTS
8434 * file set. If there's no room, fall back to allocating a new skb
8435 * and filling it in.
8436 */
8437 spin_lock_irq(&head->lock);
8438 skb = skb_peek(head);
8439 if (skb) {
8440 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8441
8442 if (fpl->count < SCM_MAX_FD) {
8443 __skb_unlink(skb, head);
8444 spin_unlock_irq(&head->lock);
8445 fpl->fp[fpl->count] = get_file(file);
8446 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8447 fpl->count++;
8448 spin_lock_irq(&head->lock);
8449 __skb_queue_head(head, skb);
8450 } else {
8451 skb = NULL;
8452 }
8453 }
8454 spin_unlock_irq(&head->lock);
8455
8456 if (skb) {
8457 fput(file);
8458 return 0;
8459 }
8460
8461 return __io_sqe_files_scm(ctx, 1, index);
8462#else
8463 return 0;
8464#endif
8465}
8466
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008467static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
8468 struct io_rsrc_node *node, void *rsrc)
8469{
8470 struct io_rsrc_put *prsrc;
8471
8472 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8473 if (!prsrc)
8474 return -ENOMEM;
8475
8476 prsrc->tag = *io_get_tag_slot(data, idx);
8477 prsrc->rsrc = rsrc;
8478 list_add(&prsrc->list, &node->rsrc_list);
8479 return 0;
8480}
8481
Pavel Begunkovb9445592021-08-25 12:25:45 +01008482static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8483 unsigned int issue_flags, u32 slot_index)
8484{
8485 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08008486 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008487 bool needs_switch = false;
Pavel Begunkovb9445592021-08-25 12:25:45 +01008488 struct io_fixed_file *file_slot;
8489 int ret = -EBADF;
8490
Hao Xu3b44b372021-10-18 21:34:31 +08008491 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008492 if (file->f_op == &io_uring_fops)
8493 goto err;
8494 ret = -ENXIO;
8495 if (!ctx->file_data)
8496 goto err;
8497 ret = -EINVAL;
8498 if (slot_index >= ctx->nr_user_files)
8499 goto err;
8500
8501 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8502 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008503
8504 if (file_slot->file_ptr) {
8505 struct file *old_file;
8506
8507 ret = io_rsrc_node_switch_start(ctx);
8508 if (ret)
8509 goto err;
8510
8511 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8512 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
8513 ctx->rsrc_node, old_file);
8514 if (ret)
8515 goto err;
8516 file_slot->file_ptr = 0;
8517 needs_switch = true;
8518 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01008519
8520 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8521 io_fixed_file_set(file_slot, file);
8522 ret = io_sqe_file_register(ctx, file, slot_index);
8523 if (ret) {
8524 file_slot->file_ptr = 0;
8525 goto err;
8526 }
8527
8528 ret = 0;
8529err:
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008530 if (needs_switch)
8531 io_rsrc_node_switch(ctx, ctx->file_data);
Hao Xu3b44b372021-10-18 21:34:31 +08008532 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008533 if (ret)
8534 fput(file);
8535 return ret;
8536}
8537
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008538static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
8539{
8540 unsigned int offset = req->close.file_slot - 1;
8541 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08008542 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008543 struct io_fixed_file *file_slot;
8544 struct file *file;
8545 int ret, i;
8546
Hao Xu3b44b372021-10-18 21:34:31 +08008547 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008548 ret = -ENXIO;
8549 if (unlikely(!ctx->file_data))
8550 goto out;
8551 ret = -EINVAL;
8552 if (offset >= ctx->nr_user_files)
8553 goto out;
8554 ret = io_rsrc_node_switch_start(ctx);
8555 if (ret)
8556 goto out;
8557
8558 i = array_index_nospec(offset, ctx->nr_user_files);
8559 file_slot = io_fixed_file_slot(&ctx->file_table, i);
8560 ret = -EBADF;
8561 if (!file_slot->file_ptr)
8562 goto out;
8563
8564 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8565 ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
8566 if (ret)
8567 goto out;
8568
8569 file_slot->file_ptr = 0;
8570 io_rsrc_node_switch(ctx, ctx->file_data);
8571 ret = 0;
8572out:
Hao Xu3b44b372021-10-18 21:34:31 +08008573 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008574 return ret;
8575}
8576
Jens Axboe05f3fb32019-12-09 11:22:50 -07008577static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008578 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008579 unsigned nr_args)
8580{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008581 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008582 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008583 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008584 struct io_fixed_file *file_slot;
8585 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008586 int fd, i, err = 0;
8587 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008588 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008589
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008590 if (!ctx->file_data)
8591 return -ENXIO;
8592 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008593 return -EINVAL;
8594
Pavel Begunkov67973b92021-01-26 13:51:09 +00008595 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008596 u64 tag = 0;
8597
8598 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8599 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008600 err = -EFAULT;
8601 break;
8602 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008603 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8604 err = -EINVAL;
8605 break;
8606 }
noah4e0377a2021-01-26 15:23:28 -05008607 if (fd == IORING_REGISTER_FILES_SKIP)
8608 continue;
8609
Pavel Begunkov67973b92021-01-26 13:51:09 +00008610 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008611 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008612
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008613 if (file_slot->file_ptr) {
8614 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008615 err = io_queue_rsrc_removal(data, up->offset + done,
8616 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008617 if (err)
8618 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008619 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008620 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008621 }
8622 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008623 file = fget(fd);
8624 if (!file) {
8625 err = -EBADF;
8626 break;
8627 }
8628 /*
8629 * Don't allow io_uring instances to be registered. If
8630 * UNIX isn't enabled, then this causes a reference
8631 * cycle and this instance can never get freed. If UNIX
8632 * is enabled we'll handle it just fine, but there's
8633 * still no point in allowing a ring fd as it doesn't
8634 * support regular read/write anyway.
8635 */
8636 if (file->f_op == &io_uring_fops) {
8637 fput(file);
8638 err = -EBADF;
8639 break;
8640 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008641 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008642 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008643 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008644 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008645 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008646 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008647 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008648 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008649 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008650 }
8651
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008652 if (needs_switch)
8653 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008654 return done ? done : err;
8655}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008656
Jens Axboe685fe7f2021-03-08 09:37:51 -07008657static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8658 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008659{
Jens Axboee9418942021-02-19 12:33:30 -07008660 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008661 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008662 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008663
Yang Yingliang362a9e62021-07-20 16:38:05 +08008664 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008665 hash = ctx->hash_map;
8666 if (!hash) {
8667 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008668 if (!hash) {
8669 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008670 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008671 }
Jens Axboee9418942021-02-19 12:33:30 -07008672 refcount_set(&hash->refs, 1);
8673 init_waitqueue_head(&hash->wait);
8674 ctx->hash_map = hash;
8675 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008676 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008677
8678 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008679 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008680 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008681 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008682
Jens Axboed25e3a32021-02-16 11:41:41 -07008683 /* Do QD, or 4 * CPUS, whatever is smallest */
8684 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008685
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008686 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008687}
8688
Pavel Begunkovc0724812021-10-04 20:02:54 +01008689static __cold int io_uring_alloc_task_context(struct task_struct *task,
8690 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008691{
8692 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008693 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008694
Pavel Begunkov09899b12021-06-14 02:36:22 +01008695 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008696 if (unlikely(!tctx))
8697 return -ENOMEM;
8698
Jens Axboed8a6df12020-10-15 16:24:45 -06008699 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8700 if (unlikely(ret)) {
8701 kfree(tctx);
8702 return ret;
8703 }
8704
Jens Axboe685fe7f2021-03-08 09:37:51 -07008705 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008706 if (IS_ERR(tctx->io_wq)) {
8707 ret = PTR_ERR(tctx->io_wq);
8708 percpu_counter_destroy(&tctx->inflight);
8709 kfree(tctx);
8710 return ret;
8711 }
8712
Jens Axboe0f212202020-09-13 13:09:39 -06008713 xa_init(&tctx->xa);
8714 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008715 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008716 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008717 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008718 spin_lock_init(&tctx->task_lock);
8719 INIT_WQ_LIST(&tctx->task_list);
Hao Xu4813c372021-12-07 17:39:48 +08008720 INIT_WQ_LIST(&tctx->prior_task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008721 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008722 return 0;
8723}
8724
8725void __io_uring_free(struct task_struct *tsk)
8726{
8727 struct io_uring_task *tctx = tsk->io_uring;
8728
8729 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008730 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008731 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008732
Jens Axboed8a6df12020-10-15 16:24:45 -06008733 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008734 kfree(tctx);
8735 tsk->io_uring = NULL;
8736}
8737
Pavel Begunkovc0724812021-10-04 20:02:54 +01008738static __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
8739 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008740{
8741 int ret;
8742
Jens Axboed25e3a32021-02-16 11:41:41 -07008743 /* Retain compatibility with failing for an invalid attach attempt */
8744 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8745 IORING_SETUP_ATTACH_WQ) {
8746 struct fd f;
8747
8748 f = fdget(p->wq_fd);
8749 if (!f.file)
8750 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008751 if (f.file->f_op != &io_uring_fops) {
8752 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008753 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008754 }
8755 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008756 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008757 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008758 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008759 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008760 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008761
Paul Moorecdc14042021-02-01 19:56:49 -05008762 ret = security_uring_sqpoll();
8763 if (ret)
8764 return ret;
8765
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008766 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008767 if (IS_ERR(sqd)) {
8768 ret = PTR_ERR(sqd);
8769 goto err;
8770 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008771
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008772 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008773 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008774 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8775 if (!ctx->sq_thread_idle)
8776 ctx->sq_thread_idle = HZ;
8777
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008778 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008779 list_add(&ctx->sqd_list, &sqd->ctx_list);
8780 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008781 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008782 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008783 io_sq_thread_unpark(sqd);
8784
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008785 if (ret < 0)
8786 goto err;
8787 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008788 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008789
Jens Axboe6c271ce2019-01-10 11:22:30 -07008790 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008791 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008792
Jens Axboe917257d2019-04-13 09:28:55 -06008793 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008794 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008795 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008796 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008797 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008798 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008799 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008800
8801 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008802 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008803 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8804 if (IS_ERR(tsk)) {
8805 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008806 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008807 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008808
Jens Axboe46fe18b2021-03-04 12:39:36 -07008809 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008810 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008811 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008812 if (ret)
8813 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008814 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8815 /* Can't have SQ_AFF without SQPOLL */
8816 ret = -EINVAL;
8817 goto err;
8818 }
8819
Jens Axboe2b188cc2019-01-07 10:46:33 -07008820 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008821err_sqpoll:
8822 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008823err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008824 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008825 return ret;
8826}
8827
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008828static inline void __io_unaccount_mem(struct user_struct *user,
8829 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008830{
8831 atomic_long_sub(nr_pages, &user->locked_vm);
8832}
8833
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008834static inline int __io_account_mem(struct user_struct *user,
8835 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008836{
8837 unsigned long page_limit, cur_pages, new_pages;
8838
8839 /* Don't allow more pages than we can safely lock */
8840 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8841
8842 do {
8843 cur_pages = atomic_long_read(&user->locked_vm);
8844 new_pages = cur_pages + nr_pages;
8845 if (new_pages > page_limit)
8846 return -ENOMEM;
8847 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8848 new_pages) != cur_pages);
8849
8850 return 0;
8851}
8852
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008853static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008854{
Jens Axboe62e398b2021-02-21 16:19:37 -07008855 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008856 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008857
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008858 if (ctx->mm_account)
8859 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008860}
8861
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008862static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008863{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008864 int ret;
8865
Jens Axboe62e398b2021-02-21 16:19:37 -07008866 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008867 ret = __io_account_mem(ctx->user, nr_pages);
8868 if (ret)
8869 return ret;
8870 }
8871
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008872 if (ctx->mm_account)
8873 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008874
8875 return 0;
8876}
8877
Jens Axboe2b188cc2019-01-07 10:46:33 -07008878static void io_mem_free(void *ptr)
8879{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008880 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008881
Mark Rutland52e04ef2019-04-30 17:30:21 +01008882 if (!ptr)
8883 return;
8884
8885 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008886 if (put_page_testzero(page))
8887 free_compound_page(page);
8888}
8889
8890static void *io_mem_alloc(size_t size)
8891{
8892 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008893 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008894
8895 return (void *) __get_free_pages(gfp_flags, get_order(size));
8896}
8897
Hristo Venev75b28af2019-08-26 17:23:46 +00008898static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8899 size_t *sq_offset)
8900{
8901 struct io_rings *rings;
8902 size_t off, sq_array_size;
8903
8904 off = struct_size(rings, cqes, cq_entries);
8905 if (off == SIZE_MAX)
8906 return SIZE_MAX;
8907
8908#ifdef CONFIG_SMP
8909 off = ALIGN(off, SMP_CACHE_BYTES);
8910 if (off == 0)
8911 return SIZE_MAX;
8912#endif
8913
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008914 if (sq_offset)
8915 *sq_offset = off;
8916
Hristo Venev75b28af2019-08-26 17:23:46 +00008917 sq_array_size = array_size(sizeof(u32), sq_entries);
8918 if (sq_array_size == SIZE_MAX)
8919 return SIZE_MAX;
8920
8921 if (check_add_overflow(off, sq_array_size, &off))
8922 return SIZE_MAX;
8923
Hristo Venev75b28af2019-08-26 17:23:46 +00008924 return off;
8925}
8926
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008927static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008928{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008929 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008930 unsigned int i;
8931
Pavel Begunkov62248432021-04-28 13:11:29 +01008932 if (imu != ctx->dummy_ubuf) {
8933 for (i = 0; i < imu->nr_bvecs; i++)
8934 unpin_user_page(imu->bvec[i].bv_page);
8935 if (imu->acct_pages)
8936 io_unaccount_mem(ctx, imu->acct_pages);
8937 kvfree(imu);
8938 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008939 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008940}
8941
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008942static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8943{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008944 io_buffer_unmap(ctx, &prsrc->buf);
8945 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008946}
8947
8948static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008949{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008950 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008951
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008952 for (i = 0; i < ctx->nr_user_bufs; i++)
8953 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008954 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008955 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008956 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008957 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008958 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008959}
8960
Jens Axboeedafcce2019-01-09 09:16:05 -07008961static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8962{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008963 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008964
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008965 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008966 return -ENXIO;
8967
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008968 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8969 if (!ret)
8970 __io_sqe_buffers_unregister(ctx);
8971 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008972}
8973
8974static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8975 void __user *arg, unsigned index)
8976{
8977 struct iovec __user *src;
8978
8979#ifdef CONFIG_COMPAT
8980 if (ctx->compat) {
8981 struct compat_iovec __user *ciovs;
8982 struct compat_iovec ciov;
8983
8984 ciovs = (struct compat_iovec __user *) arg;
8985 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8986 return -EFAULT;
8987
Jens Axboed55e5f52019-12-11 16:12:15 -07008988 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008989 dst->iov_len = ciov.iov_len;
8990 return 0;
8991 }
8992#endif
8993 src = (struct iovec __user *) arg;
8994 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8995 return -EFAULT;
8996 return 0;
8997}
8998
Jens Axboede293932020-09-17 16:19:16 -06008999/*
9000 * Not super efficient, but this is just a registration time. And we do cache
9001 * the last compound head, so generally we'll only do a full search if we don't
9002 * match that one.
9003 *
9004 * We check if the given compound head page has already been accounted, to
9005 * avoid double accounting it. This allows us to account the full size of the
9006 * page, not just the constituent pages of a huge page.
9007 */
9008static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
9009 int nr_pages, struct page *hpage)
9010{
9011 int i, j;
9012
9013 /* check current page array */
9014 for (i = 0; i < nr_pages; i++) {
9015 if (!PageCompound(pages[i]))
9016 continue;
9017 if (compound_head(pages[i]) == hpage)
9018 return true;
9019 }
9020
9021 /* check previously registered pages */
9022 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009023 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06009024
9025 for (j = 0; j < imu->nr_bvecs; j++) {
9026 if (!PageCompound(imu->bvec[j].bv_page))
9027 continue;
9028 if (compound_head(imu->bvec[j].bv_page) == hpage)
9029 return true;
9030 }
9031 }
9032
9033 return false;
9034}
9035
9036static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
9037 int nr_pages, struct io_mapped_ubuf *imu,
9038 struct page **last_hpage)
9039{
9040 int i, ret;
9041
Pavel Begunkov216e5832021-05-29 12:01:02 +01009042 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06009043 for (i = 0; i < nr_pages; i++) {
9044 if (!PageCompound(pages[i])) {
9045 imu->acct_pages++;
9046 } else {
9047 struct page *hpage;
9048
9049 hpage = compound_head(pages[i]);
9050 if (hpage == *last_hpage)
9051 continue;
9052 *last_hpage = hpage;
9053 if (headpage_already_acct(ctx, pages, i, hpage))
9054 continue;
9055 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
9056 }
9057 }
9058
9059 if (!imu->acct_pages)
9060 return 0;
9061
Jens Axboe26bfa89e2021-02-09 20:14:12 -07009062 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06009063 if (ret)
9064 imu->acct_pages = 0;
9065 return ret;
9066}
9067
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009068static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009069 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009070 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07009071{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009072 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07009073 struct vm_area_struct **vmas = NULL;
9074 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009075 unsigned long off, start, end, ubuf;
9076 size_t size;
9077 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07009078
Pavel Begunkov62248432021-04-28 13:11:29 +01009079 if (!iov->iov_base) {
9080 *pimu = ctx->dummy_ubuf;
9081 return 0;
9082 }
9083
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009084 ubuf = (unsigned long) iov->iov_base;
9085 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
9086 start = ubuf >> PAGE_SHIFT;
9087 nr_pages = end - start;
9088
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009089 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009090 ret = -ENOMEM;
9091
9092 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
9093 if (!pages)
9094 goto done;
9095
9096 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
9097 GFP_KERNEL);
9098 if (!vmas)
9099 goto done;
9100
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009101 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01009102 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009103 goto done;
9104
9105 ret = 0;
9106 mmap_read_lock(current->mm);
9107 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
9108 pages, vmas);
9109 if (pret == nr_pages) {
9110 /* don't support file backed memory */
9111 for (i = 0; i < nr_pages; i++) {
9112 struct vm_area_struct *vma = vmas[i];
9113
Pavel Begunkov40dad762021-06-09 15:26:54 +01009114 if (vma_is_shmem(vma))
9115 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009116 if (vma->vm_file &&
9117 !is_file_hugepages(vma->vm_file)) {
9118 ret = -EOPNOTSUPP;
9119 break;
9120 }
9121 }
9122 } else {
9123 ret = pret < 0 ? pret : -EFAULT;
9124 }
9125 mmap_read_unlock(current->mm);
9126 if (ret) {
9127 /*
9128 * if we did partial map, or found file backed vmas,
9129 * release any pages we did get
9130 */
9131 if (pret > 0)
9132 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009133 goto done;
9134 }
9135
9136 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
9137 if (ret) {
9138 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009139 goto done;
9140 }
9141
9142 off = ubuf & ~PAGE_MASK;
9143 size = iov->iov_len;
9144 for (i = 0; i < nr_pages; i++) {
9145 size_t vec_len;
9146
9147 vec_len = min_t(size_t, size, PAGE_SIZE - off);
9148 imu->bvec[i].bv_page = pages[i];
9149 imu->bvec[i].bv_len = vec_len;
9150 imu->bvec[i].bv_offset = off;
9151 off = 0;
9152 size -= vec_len;
9153 }
9154 /* store original address for later verification */
9155 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01009156 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009157 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009158 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009159 ret = 0;
9160done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009161 if (ret)
9162 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009163 kvfree(pages);
9164 kvfree(vmas);
9165 return ret;
9166}
9167
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009168static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009169{
Pavel Begunkov87094462021-04-11 01:46:36 +01009170 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
9171 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009172}
9173
9174static int io_buffer_validate(struct iovec *iov)
9175{
Pavel Begunkov50e96982021-03-24 22:59:01 +00009176 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
9177
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009178 /*
9179 * Don't impose further limits on the size and buffer
9180 * constraints here, we'll -EINVAL later when IO is
9181 * submitted if they are wrong.
9182 */
Pavel Begunkov62248432021-04-28 13:11:29 +01009183 if (!iov->iov_base)
9184 return iov->iov_len ? -EFAULT : 0;
9185 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009186 return -EFAULT;
9187
9188 /* arbitrary limit, but we need something */
9189 if (iov->iov_len > SZ_1G)
9190 return -EFAULT;
9191
Pavel Begunkov50e96982021-03-24 22:59:01 +00009192 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
9193 return -EOVERFLOW;
9194
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009195 return 0;
9196}
9197
9198static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009199 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009200{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009201 struct page *last_hpage = NULL;
9202 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009203 int i, ret;
9204 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009205
Pavel Begunkov87094462021-04-11 01:46:36 +01009206 if (ctx->user_bufs)
9207 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01009208 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01009209 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009210 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009211 if (ret)
9212 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01009213 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9214 if (ret)
9215 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009216 ret = io_buffers_map_alloc(ctx, nr_args);
9217 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08009218 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009219 return ret;
9220 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009221
Pavel Begunkov87094462021-04-11 01:46:36 +01009222 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07009223 ret = io_copy_iov(ctx, &iov, arg, i);
9224 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009225 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009226 ret = io_buffer_validate(&iov);
9227 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009228 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009229 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009230 ret = -EINVAL;
9231 break;
9232 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009233
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009234 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9235 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009236 if (ret)
9237 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009238 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009239
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009240 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009241
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009242 ctx->buf_data = data;
9243 if (ret)
9244 __io_sqe_buffers_unregister(ctx);
9245 else
9246 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009247 return ret;
9248}
9249
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009250static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9251 struct io_uring_rsrc_update2 *up,
9252 unsigned int nr_args)
9253{
9254 u64 __user *tags = u64_to_user_ptr(up->tags);
9255 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009256 struct page *last_hpage = NULL;
9257 bool needs_switch = false;
9258 __u32 done;
9259 int i, err;
9260
9261 if (!ctx->buf_data)
9262 return -ENXIO;
9263 if (up->offset + nr_args > ctx->nr_user_bufs)
9264 return -EINVAL;
9265
9266 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009267 struct io_mapped_ubuf *imu;
9268 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009269 u64 tag = 0;
9270
9271 err = io_copy_iov(ctx, &iov, iovs, done);
9272 if (err)
9273 break;
9274 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9275 err = -EFAULT;
9276 break;
9277 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009278 err = io_buffer_validate(&iov);
9279 if (err)
9280 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009281 if (!iov.iov_base && tag) {
9282 err = -EINVAL;
9283 break;
9284 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009285 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9286 if (err)
9287 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009288
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009289 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009290 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009291 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9292 ctx->rsrc_node, ctx->user_bufs[i]);
9293 if (unlikely(err)) {
9294 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009295 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009296 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009297 ctx->user_bufs[i] = NULL;
9298 needs_switch = true;
9299 }
9300
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009301 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009302 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009303 }
9304
9305 if (needs_switch)
9306 io_rsrc_node_switch(ctx, ctx->buf_data);
9307 return done ? done : err;
9308}
9309
Jens Axboe9b402842019-04-11 11:45:41 -06009310static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9311{
9312 __s32 __user *fds = arg;
9313 int fd;
9314
9315 if (ctx->cq_ev_fd)
9316 return -EBUSY;
9317
9318 if (copy_from_user(&fd, fds, sizeof(*fds)))
9319 return -EFAULT;
9320
9321 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9322 if (IS_ERR(ctx->cq_ev_fd)) {
9323 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009324
Jens Axboe9b402842019-04-11 11:45:41 -06009325 ctx->cq_ev_fd = NULL;
9326 return ret;
9327 }
9328
9329 return 0;
9330}
9331
9332static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9333{
9334 if (ctx->cq_ev_fd) {
9335 eventfd_ctx_put(ctx->cq_ev_fd);
9336 ctx->cq_ev_fd = NULL;
9337 return 0;
9338 }
9339
9340 return -ENXIO;
9341}
9342
Jens Axboe5a2e7452020-02-23 16:23:11 -07009343static void io_destroy_buffers(struct io_ring_ctx *ctx)
9344{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009345 struct io_buffer *buf;
9346 unsigned long index;
9347
Jens Axboe8bab4c02021-09-24 07:12:27 -06009348 xa_for_each(&ctx->io_buffers, index, buf) {
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009349 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009350 cond_resched();
9351 }
Jens Axboe5a2e7452020-02-23 16:23:11 -07009352}
9353
Jens Axboe4010fec2021-02-27 15:04:18 -07009354static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009355{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009356 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009357 int nr = 0;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009358
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009359 mutex_lock(&ctx->uring_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009360 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009361
9362 while (state->free_list.next) {
9363 struct io_wq_work_node *node;
9364 struct io_kiocb *req;
9365
9366 node = wq_stack_extract(&state->free_list);
9367 req = container_of(node, struct io_kiocb, comp_list);
9368 kmem_cache_free(req_cachep, req);
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009369 nr++;
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009370 }
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009371 if (nr)
9372 percpu_ref_put_many(&ctx->refs, nr);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009373 mutex_unlock(&ctx->uring_lock);
9374}
9375
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009376static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009377{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009378 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009379 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009380}
9381
Pavel Begunkovc0724812021-10-04 20:02:54 +01009382static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009383{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009384 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009385
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009386 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009387 mmdrop(ctx->mm_account);
9388 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009389 }
Jens Axboedef596e2019-01-09 08:59:42 -07009390
Pavel Begunkovab409402021-10-09 23:14:41 +01009391 io_rsrc_refs_drop(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009392 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9393 io_wait_rsrc_data(ctx->buf_data);
9394 io_wait_rsrc_data(ctx->file_data);
9395
Hao Xu8bad28d2021-02-19 17:19:36 +08009396 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009397 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009398 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009399 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009400 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009401 if (ctx->rings)
9402 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009403 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009404 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009405 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009406 if (ctx->sq_creds)
9407 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009408
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009409 /* there are no registered resources left, nobody uses it */
9410 if (ctx->rsrc_node)
9411 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009412 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009413 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009414 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov756ab7c2021-10-06 16:06:47 +01009415 flush_delayed_work(&ctx->fallback_work);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009416
9417 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9418 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009419
9420#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07009421 if (ctx->ring_sock) {
9422 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009423 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07009424 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009425#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009426 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009427
Hristo Venev75b28af2019-08-26 17:23:46 +00009428 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009429 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009430
9431 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009432 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009433 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009434 if (ctx->hash_map)
9435 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009436 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009437 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009438 kfree(ctx);
9439}
9440
9441static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9442{
9443 struct io_ring_ctx *ctx = file->private_data;
9444 __poll_t mask = 0;
9445
Pavel Begunkovd60aa652021-10-04 20:02:52 +01009446 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009447 /*
9448 * synchronizes with barrier from wq_has_sleeper call in
9449 * io_commit_cqring
9450 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009451 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009452 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009453 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009454
9455 /*
9456 * Don't flush cqring overflow list here, just do a simple check.
9457 * Otherwise there could possible be ABBA deadlock:
9458 * CPU0 CPU1
9459 * ---- ----
9460 * lock(&ctx->uring_lock);
9461 * lock(&ep->mtx);
9462 * lock(&ctx->uring_lock);
9463 * lock(&ep->mtx);
9464 *
9465 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9466 * pushs them to do the flush.
9467 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009468 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009469 mask |= EPOLLIN | EPOLLRDNORM;
9470
9471 return mask;
9472}
9473
Yejune Deng0bead8c2020-12-24 11:02:20 +08009474static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009475{
Jens Axboe4379bf82021-02-15 13:40:22 -07009476 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009477
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009478 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009479 if (creds) {
9480 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009481 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009482 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009483
9484 return -EINVAL;
9485}
9486
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009487struct io_tctx_exit {
9488 struct callback_head task_work;
9489 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009490 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009491};
9492
Pavel Begunkovc0724812021-10-04 20:02:54 +01009493static __cold void io_tctx_exit_cb(struct callback_head *cb)
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009494{
9495 struct io_uring_task *tctx = current->io_uring;
9496 struct io_tctx_exit *work;
9497
9498 work = container_of(cb, struct io_tctx_exit, task_work);
9499 /*
9500 * When @in_idle, we're in cancellation and it's racy to remove the
9501 * node. It'll be removed by the end of cancellation, just ignore it.
9502 */
9503 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009504 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009505 complete(&work->completion);
9506}
9507
Pavel Begunkovc0724812021-10-04 20:02:54 +01009508static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
Pavel Begunkov28090c12021-04-25 23:34:45 +01009509{
9510 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9511
9512 return req->ctx == data;
9513}
9514
Pavel Begunkovc0724812021-10-04 20:02:54 +01009515static __cold void io_ring_exit_work(struct work_struct *work)
Jens Axboe85faa7b2020-04-09 18:14:00 -06009516{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009517 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009518 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009519 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009520 struct io_tctx_exit exit;
9521 struct io_tctx_node *node;
9522 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009523
Jens Axboe56952e92020-06-17 15:00:04 -06009524 /*
9525 * If we're doing polled IO and end up having requests being
9526 * submitted async (out-of-line), then completions can come in while
9527 * we're waiting for refs to drop. We need to reap these manually,
9528 * as nobody else will be looking for them.
9529 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009530 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009531 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009532 if (ctx->sq_data) {
9533 struct io_sq_data *sqd = ctx->sq_data;
9534 struct task_struct *tsk;
9535
9536 io_sq_thread_park(sqd);
9537 tsk = sqd->thread;
9538 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9539 io_wq_cancel_cb(tsk->io_uring->io_wq,
9540 io_cancel_ctx_cb, ctx, true);
9541 io_sq_thread_unpark(sqd);
9542 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009543
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009544 io_req_caches_free(ctx);
9545
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009546 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9547 /* there is little hope left, don't run it too often */
9548 interval = HZ * 60;
9549 }
9550 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009551
Pavel Begunkov7f006512021-04-14 13:38:34 +01009552 init_completion(&exit.completion);
9553 init_task_work(&exit.task_work, io_tctx_exit_cb);
9554 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009555 /*
9556 * Some may use context even when all refs and requests have been put,
9557 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009558 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009559 * this lock/unlock section also waits them to finish.
9560 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009561 mutex_lock(&ctx->uring_lock);
9562 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009563 WARN_ON_ONCE(time_after(jiffies, timeout));
9564
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009565 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9566 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009567 /* don't spin on a single task if cancellation failed */
9568 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009569 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9570 if (WARN_ON_ONCE(ret))
9571 continue;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009572
9573 mutex_unlock(&ctx->uring_lock);
9574 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009575 mutex_lock(&ctx->uring_lock);
9576 }
9577 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009578 spin_lock(&ctx->completion_lock);
9579 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009580
Jens Axboe85faa7b2020-04-09 18:14:00 -06009581 io_ring_ctx_free(ctx);
9582}
9583
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009584/* Returns true if we found and killed one or more timeouts */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009585static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx,
9586 struct task_struct *tsk, bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009587{
9588 struct io_kiocb *req, *tmp;
9589 int canceled = 0;
9590
Jens Axboe79ebeae2021-08-10 15:18:27 -06009591 spin_lock(&ctx->completion_lock);
9592 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009593 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009594 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009595 io_kill_timeout(req, -ECANCELED);
9596 canceled++;
9597 }
9598 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009599 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009600 if (canceled != 0)
9601 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009602 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009603 if (canceled != 0)
9604 io_cqring_ev_posted(ctx);
9605 return canceled != 0;
9606}
9607
Pavel Begunkovc0724812021-10-04 20:02:54 +01009608static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009609{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009610 unsigned long index;
9611 struct creds *creds;
9612
Jens Axboe2b188cc2019-01-07 10:46:33 -07009613 mutex_lock(&ctx->uring_lock);
9614 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009615 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009616 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009617 xa_for_each(&ctx->personalities, index, creds)
9618 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009619 mutex_unlock(&ctx->uring_lock);
9620
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009621 io_kill_timeouts(ctx, NULL, true);
9622 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009623
Jens Axboe15dff282019-11-13 09:09:23 -07009624 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009625 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009626
Jens Axboe85faa7b2020-04-09 18:14:00 -06009627 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009628 /*
9629 * Use system_unbound_wq to avoid spawning tons of event kworkers
9630 * if we're exiting a ton of rings at the same time. It just adds
9631 * noise and overhead, there's no discernable change in runtime
9632 * over using system_wq.
9633 */
9634 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009635}
9636
9637static int io_uring_release(struct inode *inode, struct file *file)
9638{
9639 struct io_ring_ctx *ctx = file->private_data;
9640
9641 file->private_data = NULL;
9642 io_ring_ctx_wait_and_kill(ctx);
9643 return 0;
9644}
9645
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009646struct io_task_cancel {
9647 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009648 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009649};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009650
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009651static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009652{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009653 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009654 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009655 bool ret;
9656
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009657 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009658 struct io_ring_ctx *ctx = req->ctx;
9659
9660 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009661 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009662 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009663 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009664 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009665 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009666 }
9667 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009668}
9669
Pavel Begunkovc0724812021-10-04 20:02:54 +01009670static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
9671 struct task_struct *task,
9672 bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009673{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009674 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009675 LIST_HEAD(list);
9676
Jens Axboe79ebeae2021-08-10 15:18:27 -06009677 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009678 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009679 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009680 list_cut_position(&list, &ctx->defer_list, &de->list);
9681 break;
9682 }
9683 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009684 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009685 if (list_empty(&list))
9686 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009687
9688 while (!list_empty(&list)) {
9689 de = list_first_entry(&list, struct io_defer_entry, list);
9690 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009691 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009692 kfree(de);
9693 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009694 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009695}
9696
Pavel Begunkovc0724812021-10-04 20:02:54 +01009697static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
Pavel Begunkov1b007642021-03-06 11:02:17 +00009698{
9699 struct io_tctx_node *node;
9700 enum io_wq_cancel cret;
9701 bool ret = false;
9702
9703 mutex_lock(&ctx->uring_lock);
9704 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9705 struct io_uring_task *tctx = node->task->io_uring;
9706
9707 /*
9708 * io_wq will stay alive while we hold uring_lock, because it's
9709 * killed after ctx nodes, which requires to take the lock.
9710 */
9711 if (!tctx || !tctx->io_wq)
9712 continue;
9713 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9714 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9715 }
9716 mutex_unlock(&ctx->uring_lock);
9717
9718 return ret;
9719}
9720
Pavel Begunkovc0724812021-10-04 20:02:54 +01009721static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9722 struct task_struct *task,
9723 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009724{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009725 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009726 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009727
9728 while (1) {
9729 enum io_wq_cancel cret;
9730 bool ret = false;
9731
Pavel Begunkov1b007642021-03-06 11:02:17 +00009732 if (!task) {
9733 ret |= io_uring_try_cancel_iowq(ctx);
9734 } else if (tctx && tctx->io_wq) {
9735 /*
9736 * Cancels requests of all rings, not only @ctx, but
9737 * it's fine as the task is in exit/exec.
9738 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009739 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009740 &cancel, true);
9741 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9742 }
9743
9744 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009745 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009746 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01009747 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009748 io_iopoll_try_reap_events(ctx);
9749 ret = true;
9750 }
9751 }
9752
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009753 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9754 ret |= io_poll_remove_all(ctx, task, cancel_all);
9755 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009756 if (task)
9757 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009758 if (!ret)
9759 break;
9760 cond_resched();
9761 }
9762}
9763
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009764static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009765{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009766 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009767 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009768 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009769
9770 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009771 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009772 if (unlikely(ret))
9773 return ret;
Pavel Begunkove139a1e2021-10-19 23:43:46 +01009774
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009775 tctx = current->io_uring;
Pavel Begunkove139a1e2021-10-19 23:43:46 +01009776 if (ctx->iowq_limits_set) {
9777 unsigned int limits[2] = { ctx->iowq_limits[0],
9778 ctx->iowq_limits[1], };
9779
9780 ret = io_wq_max_workers(tctx->io_wq, limits);
9781 if (ret)
9782 return ret;
9783 }
Jens Axboe0f212202020-09-13 13:09:39 -06009784 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009785 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9786 node = kmalloc(sizeof(*node), GFP_KERNEL);
9787 if (!node)
9788 return -ENOMEM;
9789 node->ctx = ctx;
9790 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009791
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009792 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9793 node, GFP_KERNEL));
9794 if (ret) {
9795 kfree(node);
9796 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009797 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009798
9799 mutex_lock(&ctx->uring_lock);
9800 list_add(&node->ctx_node, &ctx->tctx_list);
9801 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009802 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009803 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009804 return 0;
9805}
9806
9807/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009808 * Note that this task has used io_uring. We use it for cancelation purposes.
9809 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009810static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009811{
9812 struct io_uring_task *tctx = current->io_uring;
9813
9814 if (likely(tctx && tctx->last == ctx))
9815 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009816 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009817}
9818
9819/*
Jens Axboe0f212202020-09-13 13:09:39 -06009820 * Remove this io_uring_file -> task mapping.
9821 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009822static __cold void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009823{
9824 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009825 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009826
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009827 if (!tctx)
9828 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009829 node = xa_erase(&tctx->xa, index);
9830 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009831 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009832
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009833 WARN_ON_ONCE(current != node->task);
9834 WARN_ON_ONCE(list_empty(&node->ctx_node));
9835
9836 mutex_lock(&node->ctx->uring_lock);
9837 list_del(&node->ctx_node);
9838 mutex_unlock(&node->ctx->uring_lock);
9839
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009840 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009841 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009842 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009843}
9844
Pavel Begunkovc0724812021-10-04 20:02:54 +01009845static __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009846{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009847 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009848 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009849 unsigned long index;
9850
Jens Axboe8bab4c02021-09-24 07:12:27 -06009851 xa_for_each(&tctx->xa, index, node) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009852 io_uring_del_tctx_node(index);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009853 cond_resched();
9854 }
Marco Elverb16ef422021-05-27 11:25:48 +02009855 if (wq) {
9856 /*
9857 * Must be after io_uring_del_task_file() (removes nodes under
9858 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9859 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009860 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009861 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009862 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009863}
9864
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009865static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009866{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009867 if (tracked)
9868 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009869 return percpu_counter_sum(&tctx->inflight);
9870}
9871
Pavel Begunkovc0724812021-10-04 20:02:54 +01009872static __cold void io_uring_drop_tctx_refs(struct task_struct *task)
Pavel Begunkov09899b12021-06-14 02:36:22 +01009873{
9874 struct io_uring_task *tctx = task->io_uring;
9875 unsigned int refs = tctx->cached_refs;
9876
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009877 if (refs) {
9878 tctx->cached_refs = 0;
9879 percpu_counter_sub(&tctx->inflight, refs);
9880 put_task_struct_many(task, refs);
9881 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009882}
9883
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009884/*
9885 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9886 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9887 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009888static __cold void io_uring_cancel_generic(bool cancel_all,
9889 struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009890{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009891 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009892 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009893 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009894 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009895
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009896 WARN_ON_ONCE(sqd && sqd->thread != current);
9897
Palash Oswal6d042ff2021-04-27 18:21:49 +05309898 if (!current->io_uring)
9899 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009900 if (tctx->io_wq)
9901 io_wq_exit_start(tctx->io_wq);
9902
Jens Axboefdaf0832020-10-30 09:37:30 -06009903 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009904 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009905 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009906 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009907 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009908 if (!inflight)
9909 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009910
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009911 if (!sqd) {
9912 struct io_tctx_node *node;
9913 unsigned long index;
9914
9915 xa_for_each(&tctx->xa, index, node) {
9916 /* sqpoll task will cancel all its requests */
9917 if (node->ctx->sq_data)
9918 continue;
9919 io_uring_try_cancel_requests(node->ctx, current,
9920 cancel_all);
9921 }
9922 } else {
9923 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9924 io_uring_try_cancel_requests(ctx, current,
9925 cancel_all);
9926 }
9927
9928 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009929 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009930 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009931 * If we've seen completions, retry without waiting. This
9932 * avoids a race where a completion comes in before we did
9933 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009934 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009935 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009936 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009937 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009938 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009939 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009940
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009941 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009942 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009943 /* for exec all current's requests should be gone, kill tctx */
9944 __io_uring_free(current);
9945 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009946}
9947
Hao Xuf552a272021-08-12 12:14:35 +08009948void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009949{
Hao Xuf552a272021-08-12 12:14:35 +08009950 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009951}
9952
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009953static void *io_uring_validate_mmap_request(struct file *file,
9954 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009955{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009956 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009957 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009958 struct page *page;
9959 void *ptr;
9960
9961 switch (offset) {
9962 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009963 case IORING_OFF_CQ_RING:
9964 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009965 break;
9966 case IORING_OFF_SQES:
9967 ptr = ctx->sq_sqes;
9968 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009969 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009970 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009971 }
9972
9973 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009974 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009975 return ERR_PTR(-EINVAL);
9976
9977 return ptr;
9978}
9979
9980#ifdef CONFIG_MMU
9981
Pavel Begunkovc0724812021-10-04 20:02:54 +01009982static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009983{
9984 size_t sz = vma->vm_end - vma->vm_start;
9985 unsigned long pfn;
9986 void *ptr;
9987
9988 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9989 if (IS_ERR(ptr))
9990 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009991
9992 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9993 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9994}
9995
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009996#else /* !CONFIG_MMU */
9997
9998static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9999{
10000 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
10001}
10002
10003static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
10004{
10005 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
10006}
10007
10008static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
10009 unsigned long addr, unsigned long len,
10010 unsigned long pgoff, unsigned long flags)
10011{
10012 void *ptr;
10013
10014 ptr = io_uring_validate_mmap_request(file, pgoff, len);
10015 if (IS_ERR(ptr))
10016 return PTR_ERR(ptr);
10017
10018 return (unsigned long) ptr;
10019}
10020
10021#endif /* !CONFIG_MMU */
10022
Pavel Begunkovd9d05212021-01-08 20:57:25 +000010023static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -060010024{
10025 DEFINE_WAIT(wait);
10026
10027 do {
10028 if (!io_sqring_full(ctx))
10029 break;
Jens Axboe90554202020-09-03 12:12:41 -060010030 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
10031
10032 if (!io_sqring_full(ctx))
10033 break;
Jens Axboe90554202020-09-03 12:12:41 -060010034 schedule();
10035 } while (!signal_pending(current));
10036
10037 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +080010038 return 0;
Jens Axboe90554202020-09-03 12:12:41 -060010039}
10040
Hao Xuc73ebb62020-11-03 10:54:37 +080010041static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
10042 struct __kernel_timespec __user **ts,
10043 const sigset_t __user **sig)
10044{
10045 struct io_uring_getevents_arg arg;
10046
10047 /*
10048 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
10049 * is just a pointer to the sigset_t.
10050 */
10051 if (!(flags & IORING_ENTER_EXT_ARG)) {
10052 *sig = (const sigset_t __user *) argp;
10053 *ts = NULL;
10054 return 0;
10055 }
10056
10057 /*
10058 * EXT_ARG is set - ensure we agree on the size of it and copy in our
10059 * timespec and sigset_t pointers if good.
10060 */
10061 if (*argsz != sizeof(arg))
10062 return -EINVAL;
10063 if (copy_from_user(&arg, argp, sizeof(arg)))
10064 return -EFAULT;
10065 *sig = u64_to_user_ptr(arg.sigmask);
10066 *argsz = arg.sigmask_sz;
10067 *ts = u64_to_user_ptr(arg.ts);
10068 return 0;
10069}
10070
Jens Axboe2b188cc2019-01-07 10:46:33 -070010071SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +080010072 u32, min_complete, u32, flags, const void __user *, argp,
10073 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010074{
10075 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010076 int submitted = 0;
10077 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010078 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010079
Jens Axboe4c6e2772020-07-01 11:29:10 -060010080 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -070010081
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010082 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
10083 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010084 return -EINVAL;
10085
10086 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010087 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010088 return -EBADF;
10089
10090 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010091 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010092 goto out_fput;
10093
10094 ret = -ENXIO;
10095 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010096 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010097 goto out_fput;
10098
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010099 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010100 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010101 goto out;
10102
Jens Axboe6c271ce2019-01-10 11:22:30 -070010103 /*
10104 * For SQ polling, the thread will do all submissions and completions.
10105 * Just return the requested submit count, and wake the thread if
10106 * we were asked to.
10107 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -060010108 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -070010109 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +010010110 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +000010111
Jens Axboe21f96522021-08-14 09:04:40 -060010112 if (unlikely(ctx->sq_data->thread == NULL)) {
10113 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +010010114 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -060010115 }
Jens Axboe6c271ce2019-01-10 11:22:30 -070010116 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -060010117 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +000010118 if (flags & IORING_ENTER_SQ_WAIT) {
10119 ret = io_sqpoll_wait_sq(ctx);
10120 if (ret)
10121 goto out;
10122 }
Jens Axboe6c271ce2019-01-10 11:22:30 -070010123 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -060010124 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010125 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -060010126 if (unlikely(ret))
10127 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010128 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -060010129 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010130 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010131
10132 if (submitted != to_submit)
10133 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010134 }
10135 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +080010136 const sigset_t __user *sig;
10137 struct __kernel_timespec __user *ts;
10138
10139 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
10140 if (unlikely(ret))
10141 goto out;
10142
Jens Axboe2b188cc2019-01-07 10:46:33 -070010143 min_complete = min(min_complete, ctx->cq_entries);
10144
Xiaoguang Wang32b22442020-03-11 09:26:09 +080010145 /*
10146 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
10147 * space applications don't need to do io completion events
10148 * polling again, they can rely on io_sq_thread to do polling
10149 * work, which can reduce cpu usage and uring_lock contention.
10150 */
10151 if (ctx->flags & IORING_SETUP_IOPOLL &&
10152 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +030010153 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -070010154 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +080010155 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -070010156 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010157 }
10158
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010159out:
Pavel Begunkov6805b322019-10-08 02:18:42 +030010160 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010161out_fput:
10162 fdput(f);
10163 return submitted ? submitted : ret;
10164}
10165
Tobias Klauserbebdb652020-02-26 18:38:32 +010010166#ifdef CONFIG_PROC_FS
Pavel Begunkovc0724812021-10-04 20:02:54 +010010167static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010168 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -070010169{
Jens Axboe87ce9552020-01-30 08:25:34 -070010170 struct user_namespace *uns = seq_user_ns(m);
10171 struct group_info *gi;
10172 kernel_cap_t cap;
10173 unsigned __capi;
10174 int g;
10175
10176 seq_printf(m, "%5d\n", id);
10177 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
10178 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
10179 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
10180 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
10181 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
10182 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
10183 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
10184 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
10185 seq_puts(m, "\n\tGroups:\t");
10186 gi = cred->group_info;
10187 for (g = 0; g < gi->ngroups; g++) {
10188 seq_put_decimal_ull(m, g ? " " : "",
10189 from_kgid_munged(uns, gi->gid[g]));
10190 }
10191 seq_puts(m, "\n\tCapEff:\t");
10192 cap = cred->cap_effective;
10193 CAP_FOR_EACH_U32(__capi)
10194 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
10195 seq_putc(m, '\n');
10196 return 0;
10197}
10198
Pavel Begunkovc0724812021-10-04 20:02:54 +010010199static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx,
10200 struct seq_file *m)
Jens Axboe87ce9552020-01-30 08:25:34 -070010201{
Joseph Qidbbe9c62020-09-29 09:01:22 -060010202 struct io_sq_data *sq = NULL;
Hao Xu83f84352021-09-13 21:08:54 +080010203 struct io_overflow_cqe *ocqe;
10204 struct io_rings *r = ctx->rings;
10205 unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
Hao Xu83f84352021-09-13 21:08:54 +080010206 unsigned int sq_head = READ_ONCE(r->sq.head);
10207 unsigned int sq_tail = READ_ONCE(r->sq.tail);
10208 unsigned int cq_head = READ_ONCE(r->cq.head);
10209 unsigned int cq_tail = READ_ONCE(r->cq.tail);
Jens Axboef75d1182021-10-29 06:36:45 -060010210 unsigned int sq_entries, cq_entries;
Jens Axboefad8e0d2020-09-28 08:57:48 -060010211 bool has_lock;
Hao Xu83f84352021-09-13 21:08:54 +080010212 unsigned int i;
10213
10214 /*
10215 * we may get imprecise sqe and cqe info if uring is actively running
10216 * since we get cached_sq_head and cached_cq_tail without uring_lock
10217 * and sq_tail and cq_head are changed by userspace. But it's ok since
10218 * we usually use these info when it is stuck.
10219 */
Jens Axboef75d1182021-10-29 06:36:45 -060010220 seq_printf(m, "SqMask:\t\t0x%x\n", sq_mask);
10221 seq_printf(m, "SqHead:\t%u\n", sq_head);
10222 seq_printf(m, "SqTail:\t%u\n", sq_tail);
10223 seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head);
10224 seq_printf(m, "CqMask:\t0x%x\n", cq_mask);
10225 seq_printf(m, "CqHead:\t%u\n", cq_head);
10226 seq_printf(m, "CqTail:\t%u\n", cq_tail);
10227 seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail);
10228 seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head);
10229 sq_entries = min(sq_tail - sq_head, ctx->sq_entries);
10230 for (i = 0; i < sq_entries; i++) {
10231 unsigned int entry = i + sq_head;
10232 unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]);
Jens Axboea1957782021-11-05 09:31:05 -060010233 struct io_uring_sqe *sqe;
Hao Xu83f84352021-09-13 21:08:54 +080010234
Jens Axboef75d1182021-10-29 06:36:45 -060010235 if (sq_idx > sq_mask)
10236 continue;
10237 sqe = &ctx->sq_sqes[sq_idx];
10238 seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n",
10239 sq_idx, sqe->opcode, sqe->fd, sqe->flags,
10240 sqe->user_data);
Hao Xu83f84352021-09-13 21:08:54 +080010241 }
Jens Axboef75d1182021-10-29 06:36:45 -060010242 seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head);
10243 cq_entries = min(cq_tail - cq_head, ctx->cq_entries);
10244 for (i = 0; i < cq_entries; i++) {
10245 unsigned int entry = i + cq_head;
10246 struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask];
Hao Xu83f84352021-09-13 21:08:54 +080010247
10248 seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n",
Jens Axboef75d1182021-10-29 06:36:45 -060010249 entry & cq_mask, cqe->user_data, cqe->res,
10250 cqe->flags);
Hao Xu83f84352021-09-13 21:08:54 +080010251 }
Jens Axboe87ce9552020-01-30 08:25:34 -070010252
Jens Axboefad8e0d2020-09-28 08:57:48 -060010253 /*
10254 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10255 * since fdinfo case grabs it in the opposite direction of normal use
10256 * cases. If we fail to get the lock, we just don't iterate any
10257 * structures that could be going away outside the io_uring mutex.
10258 */
10259 has_lock = mutex_trylock(&ctx->uring_lock);
10260
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010261 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -060010262 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010263 if (!sq->thread)
10264 sq = NULL;
10265 }
Joseph Qidbbe9c62020-09-29 09:01:22 -060010266
10267 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10268 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -070010269 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010270 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -070010271 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -070010272
Jens Axboe87ce9552020-01-30 08:25:34 -070010273 if (f)
10274 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10275 else
10276 seq_printf(m, "%5u: <none>\n", i);
10277 }
10278 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010279 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +010010280 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +010010281 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -070010282
Pavel Begunkov4751f532021-04-01 15:43:55 +010010283 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -070010284 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010285 if (has_lock && !xa_empty(&ctx->personalities)) {
10286 unsigned long index;
10287 const struct cred *cred;
10288
Jens Axboe87ce9552020-01-30 08:25:34 -070010289 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010290 xa_for_each(&ctx->personalities, index, cred)
10291 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010292 }
Hao Xu83f84352021-09-13 21:08:54 +080010293 if (has_lock)
10294 mutex_unlock(&ctx->uring_lock);
10295
10296 seq_puts(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010297 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010298 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10299 struct hlist_head *list = &ctx->cancel_hash[i];
10300 struct io_kiocb *req;
10301
10302 hlist_for_each_entry(req, list, hash_node)
10303 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10304 req->task->task_works != NULL);
10305 }
Hao Xu83f84352021-09-13 21:08:54 +080010306
10307 seq_puts(m, "CqOverflowList:\n");
10308 list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
10309 struct io_uring_cqe *cqe = &ocqe->cqe;
10310
10311 seq_printf(m, " user_data=%llu, res=%d, flags=%x\n",
10312 cqe->user_data, cqe->res, cqe->flags);
10313
10314 }
10315
Jens Axboe79ebeae2021-08-10 15:18:27 -060010316 spin_unlock(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010317}
10318
Pavel Begunkovc0724812021-10-04 20:02:54 +010010319static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
Jens Axboe87ce9552020-01-30 08:25:34 -070010320{
10321 struct io_ring_ctx *ctx = f->private_data;
10322
10323 if (percpu_ref_tryget(&ctx->refs)) {
10324 __io_uring_show_fdinfo(ctx, m);
10325 percpu_ref_put(&ctx->refs);
10326 }
10327}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010328#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010329
Jens Axboe2b188cc2019-01-07 10:46:33 -070010330static const struct file_operations io_uring_fops = {
10331 .release = io_uring_release,
10332 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010333#ifndef CONFIG_MMU
10334 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10335 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10336#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010337 .poll = io_uring_poll,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010338#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010339 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010340#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010341};
10342
Pavel Begunkovc0724812021-10-04 20:02:54 +010010343static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10344 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010345{
Hristo Venev75b28af2019-08-26 17:23:46 +000010346 struct io_rings *rings;
10347 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010348
Jens Axboebd740482020-08-05 12:58:23 -060010349 /* make sure these are sane, as we already accounted them */
10350 ctx->sq_entries = p->sq_entries;
10351 ctx->cq_entries = p->cq_entries;
10352
Hristo Venev75b28af2019-08-26 17:23:46 +000010353 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10354 if (size == SIZE_MAX)
10355 return -EOVERFLOW;
10356
10357 rings = io_mem_alloc(size);
10358 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010359 return -ENOMEM;
10360
Hristo Venev75b28af2019-08-26 17:23:46 +000010361 ctx->rings = rings;
10362 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10363 rings->sq_ring_mask = p->sq_entries - 1;
10364 rings->cq_ring_mask = p->cq_entries - 1;
10365 rings->sq_ring_entries = p->sq_entries;
10366 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010367
10368 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010369 if (size == SIZE_MAX) {
10370 io_mem_free(ctx->rings);
10371 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010372 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010373 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010374
10375 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010376 if (!ctx->sq_sqes) {
10377 io_mem_free(ctx->rings);
10378 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010379 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010380 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010381
Jens Axboe2b188cc2019-01-07 10:46:33 -070010382 return 0;
10383}
10384
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010385static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10386{
10387 int ret, fd;
10388
10389 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10390 if (fd < 0)
10391 return fd;
10392
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010393 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010394 if (ret) {
10395 put_unused_fd(fd);
10396 return ret;
10397 }
10398 fd_install(fd, file);
10399 return fd;
10400}
10401
Jens Axboe2b188cc2019-01-07 10:46:33 -070010402/*
10403 * Allocate an anonymous fd, this is what constitutes the application
10404 * visible backing of an io_uring instance. The application mmaps this
10405 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10406 * we have to tie this fd to a socket for file garbage collection purposes.
10407 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010408static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010409{
10410 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010411#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010412 int ret;
10413
Jens Axboe2b188cc2019-01-07 10:46:33 -070010414 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10415 &ctx->ring_sock);
10416 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010417 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010418#endif
10419
Paul Moore91a9ab72021-02-01 19:33:52 -050010420 file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx,
10421 O_RDWR | O_CLOEXEC, NULL);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010422#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010423 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010424 sock_release(ctx->ring_sock);
10425 ctx->ring_sock = NULL;
10426 } else {
10427 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010428 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010429#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010430 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010431}
10432
Pavel Begunkovc0724812021-10-04 20:02:54 +010010433static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
10434 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010435{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010436 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010437 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010438 int ret;
10439
Jens Axboe8110c1a2019-12-28 15:39:54 -070010440 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010441 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010442 if (entries > IORING_MAX_ENTRIES) {
10443 if (!(p->flags & IORING_SETUP_CLAMP))
10444 return -EINVAL;
10445 entries = IORING_MAX_ENTRIES;
10446 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010447
10448 /*
10449 * Use twice as many entries for the CQ ring. It's possible for the
10450 * application to drive a higher depth than the size of the SQ ring,
10451 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010452 * some flexibility in overcommitting a bit. If the application has
10453 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10454 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010455 */
10456 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010457 if (p->flags & IORING_SETUP_CQSIZE) {
10458 /*
10459 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10460 * to a power-of-two, if it isn't already. We do NOT impose
10461 * any cq vs sq ring sizing.
10462 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010463 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010464 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010465 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10466 if (!(p->flags & IORING_SETUP_CLAMP))
10467 return -EINVAL;
10468 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10469 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010470 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10471 if (p->cq_entries < p->sq_entries)
10472 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010473 } else {
10474 p->cq_entries = 2 * p->sq_entries;
10475 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010476
Jens Axboe2b188cc2019-01-07 10:46:33 -070010477 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010478 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010479 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010480 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010481 if (!capable(CAP_IPC_LOCK))
10482 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010483
10484 /*
10485 * This is just grabbed for accounting purposes. When a process exits,
10486 * the mm is exited and dropped before the files, hence we need to hang
10487 * on to this mm purely for the purposes of being able to unaccount
10488 * memory (locked/pinned vm). It's not used for anything else.
10489 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010490 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010491 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010492
Jens Axboe2b188cc2019-01-07 10:46:33 -070010493 ret = io_allocate_scq_urings(ctx, p);
10494 if (ret)
10495 goto err;
10496
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010497 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010498 if (ret)
10499 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010500 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010501 ret = io_rsrc_node_switch_start(ctx);
10502 if (ret)
10503 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010504 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010505
Jens Axboe2b188cc2019-01-07 10:46:33 -070010506 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010507 p->sq_off.head = offsetof(struct io_rings, sq.head);
10508 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10509 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10510 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10511 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10512 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10513 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010514
10515 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010516 p->cq_off.head = offsetof(struct io_rings, cq.head);
10517 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10518 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10519 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10520 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10521 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010522 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010523
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010524 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10525 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010526 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010527 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010528 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
Pavel Begunkov04c76b42021-11-10 15:49:32 +000010529 IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010530
10531 if (copy_to_user(params, p, sizeof(*p))) {
10532 ret = -EFAULT;
10533 goto err;
10534 }
Jens Axboed1719f72020-07-30 13:43:53 -060010535
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010536 file = io_uring_get_file(ctx);
10537 if (IS_ERR(file)) {
10538 ret = PTR_ERR(file);
10539 goto err;
10540 }
10541
Jens Axboed1719f72020-07-30 13:43:53 -060010542 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010543 * Install ring fd as the very last thing, so we don't risk someone
10544 * having closed it before we finish setup
10545 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010546 ret = io_uring_install_fd(ctx, file);
10547 if (ret < 0) {
10548 /* fput will clean it up */
10549 fput(file);
10550 return ret;
10551 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010552
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010553 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010554 return ret;
10555err:
10556 io_ring_ctx_wait_and_kill(ctx);
10557 return ret;
10558}
10559
10560/*
10561 * Sets up an aio uring context, and returns the fd. Applications asks for a
10562 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10563 * params structure passed in.
10564 */
10565static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10566{
10567 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010568 int i;
10569
10570 if (copy_from_user(&p, params, sizeof(p)))
10571 return -EFAULT;
10572 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10573 if (p.resv[i])
10574 return -EINVAL;
10575 }
10576
Jens Axboe6c271ce2019-01-10 11:22:30 -070010577 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010578 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010579 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10580 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010581 return -EINVAL;
10582
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010583 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010584}
10585
10586SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10587 struct io_uring_params __user *, params)
10588{
10589 return io_uring_setup(entries, params);
10590}
10591
Pavel Begunkovc0724812021-10-04 20:02:54 +010010592static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg,
10593 unsigned nr_args)
Jens Axboe66f4af92020-01-16 15:36:52 -070010594{
10595 struct io_uring_probe *p;
10596 size_t size;
10597 int i, ret;
10598
10599 size = struct_size(p, ops, nr_args);
10600 if (size == SIZE_MAX)
10601 return -EOVERFLOW;
10602 p = kzalloc(size, GFP_KERNEL);
10603 if (!p)
10604 return -ENOMEM;
10605
10606 ret = -EFAULT;
10607 if (copy_from_user(p, arg, size))
10608 goto out;
10609 ret = -EINVAL;
10610 if (memchr_inv(p, 0, size))
10611 goto out;
10612
10613 p->last_op = IORING_OP_LAST - 1;
10614 if (nr_args > IORING_OP_LAST)
10615 nr_args = IORING_OP_LAST;
10616
10617 for (i = 0; i < nr_args; i++) {
10618 p->ops[i].op = i;
10619 if (!io_op_defs[i].not_supported)
10620 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10621 }
10622 p->ops_len = i;
10623
10624 ret = 0;
10625 if (copy_to_user(arg, p, size))
10626 ret = -EFAULT;
10627out:
10628 kfree(p);
10629 return ret;
10630}
10631
Jens Axboe071698e2020-01-28 10:04:42 -070010632static int io_register_personality(struct io_ring_ctx *ctx)
10633{
Jens Axboe4379bf82021-02-15 13:40:22 -070010634 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010635 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010636 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010637
Jens Axboe4379bf82021-02-15 13:40:22 -070010638 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010639
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010640 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10641 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010642 if (ret < 0) {
10643 put_cred(creds);
10644 return ret;
10645 }
10646 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010647}
10648
Pavel Begunkovc0724812021-10-04 20:02:54 +010010649static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
10650 void __user *arg, unsigned int nr_args)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010651{
10652 struct io_uring_restriction *res;
10653 size_t size;
10654 int i, ret;
10655
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010656 /* Restrictions allowed only if rings started disabled */
10657 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10658 return -EBADFD;
10659
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010660 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010661 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010662 return -EBUSY;
10663
10664 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10665 return -EINVAL;
10666
10667 size = array_size(nr_args, sizeof(*res));
10668 if (size == SIZE_MAX)
10669 return -EOVERFLOW;
10670
10671 res = memdup_user(arg, size);
10672 if (IS_ERR(res))
10673 return PTR_ERR(res);
10674
10675 ret = 0;
10676
10677 for (i = 0; i < nr_args; i++) {
10678 switch (res[i].opcode) {
10679 case IORING_RESTRICTION_REGISTER_OP:
10680 if (res[i].register_op >= IORING_REGISTER_LAST) {
10681 ret = -EINVAL;
10682 goto out;
10683 }
10684
10685 __set_bit(res[i].register_op,
10686 ctx->restrictions.register_op);
10687 break;
10688 case IORING_RESTRICTION_SQE_OP:
10689 if (res[i].sqe_op >= IORING_OP_LAST) {
10690 ret = -EINVAL;
10691 goto out;
10692 }
10693
10694 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10695 break;
10696 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10697 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10698 break;
10699 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10700 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10701 break;
10702 default:
10703 ret = -EINVAL;
10704 goto out;
10705 }
10706 }
10707
10708out:
10709 /* Reset all restrictions if an error happened */
10710 if (ret != 0)
10711 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10712 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010713 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010714
10715 kfree(res);
10716 return ret;
10717}
10718
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010719static int io_register_enable_rings(struct io_ring_ctx *ctx)
10720{
10721 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10722 return -EBADFD;
10723
10724 if (ctx->restrictions.registered)
10725 ctx->restricted = 1;
10726
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010727 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10728 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10729 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010730 return 0;
10731}
10732
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010733static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010734 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010735 unsigned nr_args)
10736{
10737 __u32 tmp;
10738 int err;
10739
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010740 if (up->resv)
10741 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010742 if (check_add_overflow(up->offset, nr_args, &tmp))
10743 return -EOVERFLOW;
10744 err = io_rsrc_node_switch_start(ctx);
10745 if (err)
10746 return err;
10747
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010748 switch (type) {
10749 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010750 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010751 case IORING_RSRC_BUFFER:
10752 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010753 }
10754 return -EINVAL;
10755}
10756
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010757static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10758 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010759{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010760 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010761
10762 if (!nr_args)
10763 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010764 memset(&up, 0, sizeof(up));
10765 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10766 return -EFAULT;
10767 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10768}
10769
10770static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010771 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010772{
10773 struct io_uring_rsrc_update2 up;
10774
10775 if (size != sizeof(up))
10776 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010777 if (copy_from_user(&up, arg, sizeof(up)))
10778 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010779 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010780 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010781 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010782}
10783
Pavel Begunkovc0724812021-10-04 20:02:54 +010010784static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010785 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010786{
10787 struct io_uring_rsrc_register rr;
10788
10789 /* keep it extendible */
10790 if (size != sizeof(rr))
10791 return -EINVAL;
10792
10793 memset(&rr, 0, sizeof(rr));
10794 if (copy_from_user(&rr, arg, size))
10795 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010796 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010797 return -EINVAL;
10798
Pavel Begunkov992da012021-06-10 16:37:37 +010010799 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010800 case IORING_RSRC_FILE:
10801 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10802 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010803 case IORING_RSRC_BUFFER:
10804 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10805 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010806 }
10807 return -EINVAL;
10808}
10809
Pavel Begunkovc0724812021-10-04 20:02:54 +010010810static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
10811 void __user *arg, unsigned len)
Jens Axboefe764212021-06-17 10:19:54 -060010812{
10813 struct io_uring_task *tctx = current->io_uring;
10814 cpumask_var_t new_mask;
10815 int ret;
10816
10817 if (!tctx || !tctx->io_wq)
10818 return -EINVAL;
10819
10820 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10821 return -ENOMEM;
10822
10823 cpumask_clear(new_mask);
10824 if (len > cpumask_size())
10825 len = cpumask_size();
10826
10827 if (copy_from_user(new_mask, arg, len)) {
10828 free_cpumask_var(new_mask);
10829 return -EFAULT;
10830 }
10831
10832 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10833 free_cpumask_var(new_mask);
10834 return ret;
10835}
10836
Pavel Begunkovc0724812021-10-04 20:02:54 +010010837static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
Jens Axboefe764212021-06-17 10:19:54 -060010838{
10839 struct io_uring_task *tctx = current->io_uring;
10840
10841 if (!tctx || !tctx->io_wq)
10842 return -EINVAL;
10843
10844 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10845}
10846
Pavel Begunkovc0724812021-10-04 20:02:54 +010010847static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10848 void __user *arg)
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010849 __must_hold(&ctx->uring_lock)
Jens Axboe2e480052021-08-27 11:33:19 -060010850{
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010851 struct io_tctx_node *node;
Jens Axboefa846932021-09-01 14:15:59 -060010852 struct io_uring_task *tctx = NULL;
10853 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010854 __u32 new_count[2];
10855 int i, ret;
10856
Jens Axboe2e480052021-08-27 11:33:19 -060010857 if (copy_from_user(new_count, arg, sizeof(new_count)))
10858 return -EFAULT;
10859 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10860 if (new_count[i] > INT_MAX)
10861 return -EINVAL;
10862
Jens Axboefa846932021-09-01 14:15:59 -060010863 if (ctx->flags & IORING_SETUP_SQPOLL) {
10864 sqd = ctx->sq_data;
10865 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010866 /*
10867 * Observe the correct sqd->lock -> ctx->uring_lock
10868 * ordering. Fine to drop uring_lock here, we hold
10869 * a ref to the ctx.
10870 */
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010871 refcount_inc(&sqd->refs);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010872 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010873 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010874 mutex_lock(&ctx->uring_lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010875 if (sqd->thread)
10876 tctx = sqd->thread->io_uring;
Jens Axboefa846932021-09-01 14:15:59 -060010877 }
10878 } else {
10879 tctx = current->io_uring;
10880 }
10881
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010882 BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits));
Jens Axboefa846932021-09-01 14:15:59 -060010883
Pavel Begunkovbad119b2021-11-08 15:10:03 +000010884 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10885 if (new_count[i])
10886 ctx->iowq_limits[i] = new_count[i];
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010887 ctx->iowq_limits_set = true;
10888
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010889 if (tctx && tctx->io_wq) {
10890 ret = io_wq_max_workers(tctx->io_wq, new_count);
10891 if (ret)
10892 goto err;
10893 } else {
10894 memset(new_count, 0, sizeof(new_count));
10895 }
Jens Axboefa846932021-09-01 14:15:59 -060010896
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010897 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010898 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010899 io_put_sq_data(sqd);
10900 }
Jens Axboe2e480052021-08-27 11:33:19 -060010901
10902 if (copy_to_user(arg, new_count, sizeof(new_count)))
10903 return -EFAULT;
10904
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010905 /* that's it for SQPOLL, only the SQPOLL task creates requests */
10906 if (sqd)
10907 return 0;
10908
10909 /* now propagate the restriction to all registered users */
10910 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
10911 struct io_uring_task *tctx = node->task->io_uring;
10912
10913 if (WARN_ON_ONCE(!tctx->io_wq))
10914 continue;
10915
10916 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10917 new_count[i] = ctx->iowq_limits[i];
10918 /* ignore errors, it always returns zero anyway */
10919 (void)io_wq_max_workers(tctx->io_wq, new_count);
10920 }
Jens Axboe2e480052021-08-27 11:33:19 -060010921 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010922err:
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010923 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010924 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010925 io_put_sq_data(sqd);
10926 }
Jens Axboefa846932021-09-01 14:15:59 -060010927 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010928}
10929
Jens Axboe071698e2020-01-28 10:04:42 -070010930static bool io_register_op_must_quiesce(int op)
10931{
10932 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010933 case IORING_REGISTER_BUFFERS:
10934 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010935 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010936 case IORING_UNREGISTER_FILES:
10937 case IORING_REGISTER_FILES_UPDATE:
10938 case IORING_REGISTER_PROBE:
10939 case IORING_REGISTER_PERSONALITY:
10940 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010941 case IORING_REGISTER_FILES2:
10942 case IORING_REGISTER_FILES_UPDATE2:
10943 case IORING_REGISTER_BUFFERS2:
10944 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010945 case IORING_REGISTER_IOWQ_AFF:
10946 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010947 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010948 return false;
10949 default:
10950 return true;
10951 }
10952}
10953
Pavel Begunkovc0724812021-10-04 20:02:54 +010010954static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx)
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010955{
10956 long ret;
10957
10958 percpu_ref_kill(&ctx->refs);
10959
10960 /*
10961 * Drop uring mutex before waiting for references to exit. If another
10962 * thread is currently inside io_uring_enter() it might need to grab the
10963 * uring_lock to make progress. If we hold it here across the drain
10964 * wait, then we can deadlock. It's safe to drop the mutex here, since
10965 * no new references will come in after we've killed the percpu ref.
10966 */
10967 mutex_unlock(&ctx->uring_lock);
10968 do {
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010969 ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ);
10970 if (ret) {
10971 ret = min(0L, ret);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010972 break;
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010973 }
10974
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010975 ret = io_run_task_work_sig();
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010976 io_req_caches_free(ctx);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010977 } while (ret >= 0);
10978 mutex_lock(&ctx->uring_lock);
10979
10980 if (ret)
10981 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10982 return ret;
10983}
10984
Jens Axboeedafcce2019-01-09 09:16:05 -070010985static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10986 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010987 __releases(ctx->uring_lock)
10988 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010989{
10990 int ret;
10991
Jens Axboe35fa71a2019-04-22 10:23:23 -060010992 /*
10993 * We're inside the ring mutex, if the ref is already dying, then
10994 * someone else killed the ctx or is already going through
10995 * io_uring_register().
10996 */
10997 if (percpu_ref_is_dying(&ctx->refs))
10998 return -ENXIO;
10999
Pavel Begunkov75c40212021-04-15 13:07:40 +010011000 if (ctx->restricted) {
11001 if (opcode >= IORING_REGISTER_LAST)
11002 return -EINVAL;
11003 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
11004 if (!test_bit(opcode, ctx->restrictions.register_op))
11005 return -EACCES;
11006 }
11007
Jens Axboe071698e2020-01-28 10:04:42 -070011008 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010011009 ret = io_ctx_quiesce(ctx);
11010 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010011011 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070011012 }
Jens Axboeedafcce2019-01-09 09:16:05 -070011013
11014 switch (opcode) {
11015 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010011016 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070011017 break;
11018 case IORING_UNREGISTER_BUFFERS:
11019 ret = -EINVAL;
11020 if (arg || nr_args)
11021 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080011022 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070011023 break;
Jens Axboe6b063142019-01-10 22:13:58 -070011024 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010011025 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070011026 break;
11027 case IORING_UNREGISTER_FILES:
11028 ret = -EINVAL;
11029 if (arg || nr_args)
11030 break;
11031 ret = io_sqe_files_unregister(ctx);
11032 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060011033 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010011034 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060011035 break;
Jens Axboe9b402842019-04-11 11:45:41 -060011036 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070011037 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060011038 ret = -EINVAL;
11039 if (nr_args != 1)
11040 break;
11041 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070011042 if (ret)
11043 break;
11044 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
11045 ctx->eventfd_async = 1;
11046 else
11047 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060011048 break;
11049 case IORING_UNREGISTER_EVENTFD:
11050 ret = -EINVAL;
11051 if (arg || nr_args)
11052 break;
11053 ret = io_eventfd_unregister(ctx);
11054 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070011055 case IORING_REGISTER_PROBE:
11056 ret = -EINVAL;
11057 if (!arg || nr_args > 256)
11058 break;
11059 ret = io_probe(ctx, arg, nr_args);
11060 break;
Jens Axboe071698e2020-01-28 10:04:42 -070011061 case IORING_REGISTER_PERSONALITY:
11062 ret = -EINVAL;
11063 if (arg || nr_args)
11064 break;
11065 ret = io_register_personality(ctx);
11066 break;
11067 case IORING_UNREGISTER_PERSONALITY:
11068 ret = -EINVAL;
11069 if (arg)
11070 break;
11071 ret = io_unregister_personality(ctx, nr_args);
11072 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020011073 case IORING_REGISTER_ENABLE_RINGS:
11074 ret = -EINVAL;
11075 if (arg || nr_args)
11076 break;
11077 ret = io_register_enable_rings(ctx);
11078 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020011079 case IORING_REGISTER_RESTRICTIONS:
11080 ret = io_register_restrictions(ctx, arg, nr_args);
11081 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010011082 case IORING_REGISTER_FILES2:
11083 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010011084 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010011085 case IORING_REGISTER_FILES_UPDATE2:
11086 ret = io_register_rsrc_update(ctx, arg, nr_args,
11087 IORING_RSRC_FILE);
11088 break;
11089 case IORING_REGISTER_BUFFERS2:
11090 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
11091 break;
11092 case IORING_REGISTER_BUFFERS_UPDATE:
11093 ret = io_register_rsrc_update(ctx, arg, nr_args,
11094 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010011095 break;
Jens Axboefe764212021-06-17 10:19:54 -060011096 case IORING_REGISTER_IOWQ_AFF:
11097 ret = -EINVAL;
11098 if (!arg || !nr_args)
11099 break;
11100 ret = io_register_iowq_aff(ctx, arg, nr_args);
11101 break;
11102 case IORING_UNREGISTER_IOWQ_AFF:
11103 ret = -EINVAL;
11104 if (arg || nr_args)
11105 break;
11106 ret = io_unregister_iowq_aff(ctx);
11107 break;
Jens Axboe2e480052021-08-27 11:33:19 -060011108 case IORING_REGISTER_IOWQ_MAX_WORKERS:
11109 ret = -EINVAL;
11110 if (!arg || nr_args != 2)
11111 break;
11112 ret = io_register_iowq_max_workers(ctx, arg);
11113 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070011114 default:
11115 ret = -EINVAL;
11116 break;
11117 }
11118
Jens Axboe071698e2020-01-28 10:04:42 -070011119 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070011120 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070011121 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060011122 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070011123 }
Jens Axboeedafcce2019-01-09 09:16:05 -070011124 return ret;
11125}
11126
11127SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
11128 void __user *, arg, unsigned int, nr_args)
11129{
11130 struct io_ring_ctx *ctx;
11131 long ret = -EBADF;
11132 struct fd f;
11133
11134 f = fdget(fd);
11135 if (!f.file)
11136 return -EBADF;
11137
11138 ret = -EOPNOTSUPP;
11139 if (f.file->f_op != &io_uring_fops)
11140 goto out_fput;
11141
11142 ctx = f.file->private_data;
11143
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000011144 io_run_task_work();
11145
Jens Axboeedafcce2019-01-09 09:16:05 -070011146 mutex_lock(&ctx->uring_lock);
11147 ret = __io_uring_register(ctx, opcode, arg, nr_args);
11148 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020011149 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
11150 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070011151out_fput:
11152 fdput(f);
11153 return ret;
11154}
11155
Jens Axboe2b188cc2019-01-07 10:46:33 -070011156static int __init io_uring_init(void)
11157{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011158#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
11159 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
11160 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
11161} while (0)
11162
11163#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
11164 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
11165 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
11166 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
11167 BUILD_BUG_SQE_ELEM(1, __u8, flags);
11168 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
11169 BUILD_BUG_SQE_ELEM(4, __s32, fd);
11170 BUILD_BUG_SQE_ELEM(8, __u64, off);
11171 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
11172 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011173 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011174 BUILD_BUG_SQE_ELEM(24, __u32, len);
11175 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
11176 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
11177 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
11178 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080011179 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
11180 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011181 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
11182 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
11183 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
11184 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
11185 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
11186 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
11187 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
11188 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011189 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011190 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
11191 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011192 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011193 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011194 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010011195 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011196
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011197 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
11198 sizeof(struct io_uring_rsrc_update));
11199 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
11200 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010011201
11202 /* ->buf_index is u16 */
11203 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
11204
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011205 /* should fit into one byte */
11206 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
Pavel Begunkov68fe2562021-09-15 12:03:38 +010011207 BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
11208 BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011209
Jens Axboed3656342019-12-18 09:50:26 -070011210 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080011211 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011212
Jens Axboe91f245d2021-02-09 13:48:50 -070011213 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
11214 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070011215 return 0;
11216};
11217__initcall(io_uring_init);