blob: 77b9c7e4793bf3332a0229ed74603d195b761756 [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>
Keith Buschedce22e2022-01-05 09:05:15 -080060#include <linux/blk-mq.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070075#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070076#include <linux/eventpoll.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030077#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070078#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060079#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060080#include <linux/io_uring.h>
Nadav Amitef98eb02021-08-07 17:13:41 -070081#include <linux/tracehook.h>
Paul Moore5bd21822021-02-16 19:46:48 -050082#include <linux/audit.h>
Paul Moorecdc14042021-02-01 19:56:49 -050083#include <linux/security.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020085#define CREATE_TRACE_POINTS
86#include <trace/events/io_uring.h>
87
Jens Axboe2b188cc2019-01-07 10:46:33 -070088#include <uapi/linux/io_uring.h>
89
90#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060091#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
Daniel Xu5277dea2019-09-14 14:23:45 -070093#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060094#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Olivier Langlois4ce8ad92021-06-23 11:50:18 -070095#define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
Jens Axboe65e19f52019-10-26 07:20:21 -060096
wangyangbo187f08c2021-08-19 13:56:57 +080097/* only define max */
Pavel Begunkov042b0d82021-08-09 13:04:01 +010098#define IORING_MAX_FIXED_FILES (1U << 15)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020099#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
100 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
wangyangbo187f08c2021-08-19 13:56:57 +0800102#define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100103#define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT)
104#define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
105
Pavel Begunkov489809e2021-05-14 12:06:44 +0100106#define IORING_MAX_REG_BUFFERS (1U << 14)
107
Pavel Begunkov68fe2562021-09-15 12:03:38 +0100108#define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \
109 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
110
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 Axboe392edb42019-12-09 17:52:20 -0700490 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700491};
492
Pavel Begunkov9d805892021-04-13 02:58:40 +0100493struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000494 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100495 u64 old_user_data;
496 u64 new_user_data;
497 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600498 bool update_events;
499 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000500};
501
Jens Axboeb5dba592019-12-11 14:02:38 -0700502struct io_close {
503 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700504 int fd;
Pavel Begunkov7df778b2021-09-24 20:04:29 +0100505 u32 file_slot;
Jens Axboeb5dba592019-12-11 14:02:38 -0700506};
507
Jens Axboead8a48a2019-11-15 08:49:11 -0700508struct io_timeout_data {
509 struct io_kiocb *req;
510 struct hrtimer timer;
511 struct timespec64 ts;
512 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600513 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700514};
515
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700516struct io_accept {
517 struct file *file;
518 struct sockaddr __user *addr;
519 int __user *addr_len;
520 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100521 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600522 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700523};
524
525struct io_sync {
526 struct file *file;
527 loff_t len;
528 loff_t off;
529 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700530 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700531};
532
Jens Axboefbf23842019-12-17 18:45:56 -0700533struct io_cancel {
534 struct file *file;
535 u64 addr;
536};
537
Jens Axboeb29472e2019-12-17 18:50:29 -0700538struct io_timeout {
539 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300540 u32 off;
541 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300542 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000543 /* head of the link, used by linked timeouts only */
544 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600545 /* for linked completions */
546 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700547};
548
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100549struct io_timeout_rem {
550 struct file *file;
551 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000552
553 /* timeout update */
554 struct timespec64 ts;
555 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600556 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100557};
558
Jens Axboe9adbd452019-12-20 08:45:55 -0700559struct io_rw {
560 /* NOTE: kiocb has the file as the first member, so don't do it here */
561 struct kiocb kiocb;
562 u64 addr;
563 u64 len;
564};
565
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700566struct io_connect {
567 struct file *file;
568 struct sockaddr __user *addr;
569 int addr_len;
570};
571
Jens Axboee47293f2019-12-20 08:58:21 -0700572struct io_sr_msg {
573 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700574 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100575 struct compat_msghdr __user *umsg_compat;
576 struct user_msghdr __user *umsg;
577 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700578 };
Jens Axboee47293f2019-12-20 08:58:21 -0700579 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700580 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700581 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700582};
583
Jens Axboe15b71ab2019-12-11 11:20:36 -0700584struct io_open {
585 struct file *file;
586 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100587 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700588 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700589 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600590 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700591};
592
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000593struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700594 struct file *file;
595 u64 arg;
596 u32 nr_args;
597 u32 offset;
598};
599
Jens Axboe4840e412019-12-25 22:03:45 -0700600struct io_fadvise {
601 struct file *file;
602 u64 offset;
603 u32 len;
604 u32 advice;
605};
606
Jens Axboec1ca7572019-12-25 22:18:28 -0700607struct io_madvise {
608 struct file *file;
609 u64 addr;
610 u32 len;
611 u32 advice;
612};
613
Jens Axboe3e4827b2020-01-08 15:18:09 -0700614struct io_epoll {
615 struct file *file;
616 int epfd;
617 int op;
618 int fd;
619 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700620};
621
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300622struct io_splice {
623 struct file *file_out;
624 struct file *file_in;
625 loff_t off_out;
626 loff_t off_in;
627 u64 len;
628 unsigned int flags;
629};
630
Jens Axboeddf0322d2020-02-23 16:41:33 -0700631struct io_provide_buf {
632 struct file *file;
633 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100634 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700635 __u32 bgid;
636 __u16 nbufs;
637 __u16 bid;
638};
639
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700640struct io_statx {
641 struct file *file;
642 int dfd;
643 unsigned int mask;
644 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700645 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700646 struct statx __user *buffer;
647};
648
Jens Axboe36f4fa62020-09-05 11:14:22 -0600649struct io_shutdown {
650 struct file *file;
651 int how;
652};
653
Jens Axboe80a261f2020-09-28 14:23:58 -0600654struct io_rename {
655 struct file *file;
656 int old_dfd;
657 int new_dfd;
658 struct filename *oldpath;
659 struct filename *newpath;
660 int flags;
661};
662
Jens Axboe14a11432020-09-28 14:27:37 -0600663struct io_unlink {
664 struct file *file;
665 int dfd;
666 int flags;
667 struct filename *filename;
668};
669
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700670struct io_mkdir {
671 struct file *file;
672 int dfd;
673 umode_t mode;
674 struct filename *filename;
675};
676
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700677struct io_symlink {
678 struct file *file;
679 int new_dfd;
680 struct filename *oldpath;
681 struct filename *newpath;
682};
683
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700684struct io_hardlink {
685 struct file *file;
686 int old_dfd;
687 int new_dfd;
688 struct filename *oldpath;
689 struct filename *newpath;
690 int flags;
691};
692
Jens Axboef499a022019-12-02 16:28:46 -0700693struct io_async_connect {
694 struct sockaddr_storage address;
695};
696
Jens Axboe03b12302019-12-02 18:50:25 -0700697struct io_async_msghdr {
698 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000699 /* points to an allocated iov, if NULL we use fast_iov instead */
700 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700701 struct sockaddr __user *uaddr;
702 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700703 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700704};
705
Pavel Begunkov538941e2021-10-14 16:10:15 +0100706struct io_rw_state {
Jens Axboeff6165b2020-08-13 09:47:43 -0600707 struct iov_iter iter;
Jens Axboecd658692021-09-10 11:19:14 -0600708 struct iov_iter_state iter_state;
Pavel Begunkovc88598a2021-10-14 16:10:16 +0100709 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov538941e2021-10-14 16:10:15 +0100710};
711
712struct io_async_rw {
713 struct io_rw_state s;
714 const struct iovec *free_iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -0600715 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600716 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700717};
718
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300719enum {
720 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
721 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
722 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
723 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
724 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700725 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000726 REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300727
Pavel Begunkovdddca222021-04-27 16:13:52 +0100728 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100729 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300730 REQ_F_INFLIGHT_BIT,
731 REQ_F_CUR_POS_BIT,
732 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300733 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300734 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700735 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700736 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000737 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600738 REQ_F_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100739 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100740 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100741 REQ_F_ARM_LTIMEOUT_BIT,
Pavel Begunkovd886e182021-10-04 20:02:56 +0100742 REQ_F_ASYNC_DATA_BIT,
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000743 REQ_F_SKIP_LINK_CQES_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700744 /* keep async read/write and isreg together and in order */
Pavel Begunkov35645ac2021-10-17 00:07:09 +0100745 REQ_F_SUPPORT_NOWAIT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700746 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700747
748 /* not a real bit, just to check we're not overflowing the space */
749 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300750};
751
752enum {
753 /* ctx owns file */
754 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
755 /* drain existing IO first */
756 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
757 /* linked sqes */
758 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
759 /* doesn't sever on completion < 0 */
760 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
761 /* IOSQE_ASYNC */
762 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700763 /* IOSQE_BUFFER_SELECT */
764 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000765 /* IOSQE_CQE_SKIP_SUCCESS */
766 REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300767
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300768 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100769 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000770 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300771 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
772 /* read/write uses file position */
773 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
774 /* must not punt to workers */
775 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100776 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300777 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300778 /* needs cleanup */
779 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700780 /* already went through poll handler */
781 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700782 /* buffer already selected */
783 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000784 /* completion is deferred through io_comp_state */
785 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600786 /* caller should reissue async */
787 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov35645ac2021-10-17 00:07:09 +0100788 /* supports async reads/writes */
789 REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700790 /* regular file */
791 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100792 /* has creds assigned */
793 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100794 /* skip refcounting if not set */
795 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100796 /* there is a linked timeout that has to be armed */
797 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Pavel Begunkovd886e182021-10-04 20:02:56 +0100798 /* ->async_data allocated */
799 REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT),
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000800 /* don't post CQEs while failing linked requests */
801 REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700802};
803
804struct async_poll {
805 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600806 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300807};
808
Pavel Begunkovf237c302021-08-18 12:42:46 +0100809typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100810
Jens Axboe7cbf1722021-02-10 00:03:20 +0000811struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100812 union {
813 struct io_wq_work_node node;
814 struct llist_node fallback_node;
815 };
816 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000817};
818
Pavel Begunkov992da012021-06-10 16:37:37 +0100819enum {
820 IORING_RSRC_FILE = 0,
821 IORING_RSRC_BUFFER = 1,
822};
823
Jens Axboe09bb8392019-03-13 12:39:28 -0600824/*
825 * NOTE! Each of the iocb union members has the file pointer
826 * as the first entry in their struct definition. So you can
827 * access the file pointer through any of the sub-structs,
828 * or directly as just 'ki_filp' in this struct.
829 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700830struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700831 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600832 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700833 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700834 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100835 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700836 struct io_accept accept;
837 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700838 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700839 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100840 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700841 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700842 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700843 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700844 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000845 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700846 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700847 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700848 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300849 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700850 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700851 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600852 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600853 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600854 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700855 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700856 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700857 struct io_hardlink hardlink;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700858 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859
Jens Axboed625c6e2019-12-17 19:53:05 -0700860 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800861 /* polled IO has completed */
862 u8 iopoll_completed;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700863 u16 buf_index;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100864 unsigned int flags;
865
866 u64 user_data;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300867 u32 result;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100868 u32 cflags;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700869
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300870 struct io_ring_ctx *ctx;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300871 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700872
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000873 struct percpu_ref *fixed_rsrc_refs;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100874 /* store used ubuf, so we can prevent reloading */
875 struct io_mapped_ubuf *imu;
Jens Axboed7718a92020-02-14 22:23:12 -0700876
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100877 /* used by request caches, completion batching and iopoll */
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100878 struct io_wq_work_node comp_list;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100879 atomic_t refs;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100880 struct io_kiocb *link;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100881 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300882 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
883 struct hlist_node hash_node;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100884 /* internal polling, see IORING_FEAT_FAST_POLL */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300885 struct async_poll *apoll;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100886 /* opcode allocated if it needs to store data for async defer */
887 void *async_data;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300888 struct io_wq_work work;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100889 /* custom credentials, valid IFF REQ_F_CREDS is set */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100890 const struct cred *creds;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100891 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
Pavel Begunkov30d51dd2021-10-01 18:07:03 +0100892 struct io_buffer *kbuf;
Pavel Begunkovaa434772021-12-15 22:08:48 +0000893 atomic_t poll_refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700894};
895
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000896struct io_tctx_node {
897 struct list_head ctx_node;
898 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000899 struct io_ring_ctx *ctx;
900};
901
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300902struct io_defer_entry {
903 struct list_head list;
904 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300905 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300906};
907
Jens Axboed3656342019-12-18 09:50:26 -0700908struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700909 /* needs req->file assigned */
910 unsigned needs_file : 1;
Pavel Begunkov6d634162021-10-06 16:06:46 +0100911 /* should block plug */
912 unsigned plug : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700913 /* hash wq insertion if file is a regular file */
914 unsigned hash_reg_file : 1;
915 /* unbound wq insertion if file is a non-regular file */
916 unsigned unbound_nonreg_file : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700917 /* set if opcode supports polled "wait" */
918 unsigned pollin : 1;
919 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700920 /* op supports buffer selection */
921 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000922 /* do prep async if is going to be punted */
923 unsigned needs_async_setup : 1;
Pavel Begunkov6d634162021-10-06 16:06:46 +0100924 /* opcode is not supported by this kernel */
925 unsigned not_supported : 1;
Paul Moore5bd21822021-02-16 19:46:48 -0500926 /* skip auditing */
927 unsigned audit_skip : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700928 /* size of async data needed, if any */
929 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700930};
931
Jens Axboe09186822020-10-13 15:01:40 -0600932static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300933 [IORING_OP_NOP] = {},
934 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700935 .needs_file = 1,
936 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700937 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700938 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000939 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600940 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500941 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700942 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700943 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300944 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700945 .needs_file = 1,
946 .hash_reg_file = 1,
947 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700948 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000949 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600950 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500951 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700952 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700953 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300954 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700955 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500956 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700957 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300958 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700959 .needs_file = 1,
960 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700961 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600962 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500963 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700964 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700965 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300966 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700967 .needs_file = 1,
968 .hash_reg_file = 1,
969 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700970 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600971 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500972 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700973 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700974 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300975 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700976 .needs_file = 1,
977 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500978 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700979 },
Paul Moore5bd21822021-02-16 19:46:48 -0500980 [IORING_OP_POLL_REMOVE] = {
981 .audit_skip = 1,
982 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300983 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700984 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500985 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700986 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300987 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700988 .needs_file = 1,
989 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700990 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000991 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700992 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700993 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300994 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700995 .needs_file = 1,
996 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700997 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700998 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000999 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001000 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -07001001 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001002 [IORING_OP_TIMEOUT] = {
Paul Moore5bd21822021-02-16 19:46:48 -05001003 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001004 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -07001005 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00001006 [IORING_OP_TIMEOUT_REMOVE] = {
1007 /* used by timeout updates' prep() */
Paul Moore5bd21822021-02-16 19:46:48 -05001008 .audit_skip = 1,
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00001009 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001010 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001011 .needs_file = 1,
1012 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001013 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -07001014 },
Paul Moore5bd21822021-02-16 19:46:48 -05001015 [IORING_OP_ASYNC_CANCEL] = {
1016 .audit_skip = 1,
1017 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001018 [IORING_OP_LINK_TIMEOUT] = {
Paul Moore5bd21822021-02-16 19:46:48 -05001019 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001020 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -07001021 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001022 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001023 .needs_file = 1,
1024 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001025 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +00001026 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001027 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -07001028 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001029 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -07001030 .needs_file = 1,
1031 },
Jens Axboe44526be2021-02-15 13:32:18 -07001032 [IORING_OP_OPENAT] = {},
1033 [IORING_OP_CLOSE] = {},
Paul Moore5bd21822021-02-16 19:46:48 -05001034 [IORING_OP_FILES_UPDATE] = {
1035 .audit_skip = 1,
1036 },
1037 [IORING_OP_STATX] = {
1038 .audit_skip = 1,
1039 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001040 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001041 .needs_file = 1,
1042 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001043 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001044 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001045 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001046 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001047 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001048 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001049 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001050 .needs_file = 1,
Jens Axboe7b3188e2021-08-30 19:37:41 -06001051 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -07001052 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001053 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001054 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001055 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001056 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001057 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001058 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001059 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001060 .audit_skip = 1,
Jens Axboe4840e412019-12-25 22:03:45 -07001061 },
Jens Axboe44526be2021-02-15 13:32:18 -07001062 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001063 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001064 .needs_file = 1,
1065 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001066 .pollout = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001067 .audit_skip = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001068 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001069 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001070 .needs_file = 1,
1071 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001072 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001073 .buffer_select = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001074 .audit_skip = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001075 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001076 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001077 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001078 [IORING_OP_EPOLL_CTL] = {
1079 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001080 .audit_skip = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001081 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001082 [IORING_OP_SPLICE] = {
1083 .needs_file = 1,
1084 .hash_reg_file = 1,
1085 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001086 .audit_skip = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001087 },
Paul Moore5bd21822021-02-16 19:46:48 -05001088 [IORING_OP_PROVIDE_BUFFERS] = {
1089 .audit_skip = 1,
1090 },
1091 [IORING_OP_REMOVE_BUFFERS] = {
1092 .audit_skip = 1,
1093 },
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001094 [IORING_OP_TEE] = {
1095 .needs_file = 1,
1096 .hash_reg_file = 1,
1097 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001098 .audit_skip = 1,
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001099 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001100 [IORING_OP_SHUTDOWN] = {
1101 .needs_file = 1,
1102 },
Jens Axboe44526be2021-02-15 13:32:18 -07001103 [IORING_OP_RENAMEAT] = {},
1104 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001105 [IORING_OP_MKDIRAT] = {},
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07001106 [IORING_OP_SYMLINKAT] = {},
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07001107 [IORING_OP_LINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001108};
1109
Pavel Begunkov0756a862021-08-15 10:40:25 +01001110/* requests with any of those set should undergo io_disarm_next() */
1111#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1112
Pavel Begunkov7a612352021-03-09 00:37:59 +00001113static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001114static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001115static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1116 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001117 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001118static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001119
Pavel Begunkov913a5712021-11-10 15:49:31 +00001120static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags);
1121
Jackie Liuec9c02a2019-11-08 23:50:36 +08001122static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001123static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001124static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001125static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001126static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001127 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001128 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001129static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001130static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001131 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001132static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001133static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001134
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001135static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkovc4501782021-09-08 16:40:52 +01001136static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001137static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001138
Pavel Begunkovb9445592021-08-25 12:25:45 +01001139static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1140 unsigned int issue_flags, u32 slot_index);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01001141static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags);
1142
Pavel Begunkovf1042b62021-08-28 19:54:39 -06001143static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
Pavel Begunkovb9445592021-08-25 12:25:45 +01001144
Jens Axboe2b188cc2019-01-07 10:46:33 -07001145static struct kmem_cache *req_cachep;
1146
Jens Axboe09186822020-10-13 15:01:40 -06001147static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001148
1149struct sock *io_uring_get_socket(struct file *file)
1150{
1151#if defined(CONFIG_UNIX)
1152 if (file->f_op == &io_uring_fops) {
1153 struct io_ring_ctx *ctx = file->private_data;
1154
1155 return ctx->ring_sock->sk;
1156 }
1157#endif
1158 return NULL;
1159}
1160EXPORT_SYMBOL(io_uring_get_socket);
1161
Pavel Begunkovf237c302021-08-18 12:42:46 +01001162static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1163{
1164 if (!*locked) {
1165 mutex_lock(&ctx->uring_lock);
1166 *locked = true;
1167 }
1168}
1169
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001170#define io_for_each_link(pos, head) \
1171 for (pos = (head); pos; pos = pos->link)
1172
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001173/*
1174 * Shamelessly stolen from the mm implementation of page reference checking,
1175 * see commit f958d7b528b1 for details.
1176 */
1177#define req_ref_zero_or_close_to_overflow(req) \
1178 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1179
1180static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1181{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001182 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001183 return atomic_inc_not_zero(&req->refs);
1184}
1185
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001186static inline bool req_ref_put_and_test(struct io_kiocb *req)
1187{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001188 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1189 return true;
1190
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001191 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1192 return atomic_dec_and_test(&req->refs);
1193}
1194
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001195static inline void req_ref_get(struct io_kiocb *req)
1196{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001197 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001198 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1199 atomic_inc(&req->refs);
1200}
1201
Pavel Begunkovc4501782021-09-08 16:40:52 +01001202static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
1203{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001204 if (!wq_list_empty(&ctx->submit_state.compl_reqs))
Pavel Begunkovc4501782021-09-08 16:40:52 +01001205 __io_submit_flush_completions(ctx);
1206}
1207
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001208static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001209{
1210 if (!(req->flags & REQ_F_REFCOUNT)) {
1211 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001212 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001213 }
1214}
1215
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001216static inline void io_req_set_refcount(struct io_kiocb *req)
1217{
1218 __io_req_set_refcount(req, 1);
1219}
1220
Pavel Begunkovab409402021-10-09 23:14:41 +01001221#define IO_RSRC_REF_BATCH 100
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001222
Pavel Begunkovab409402021-10-09 23:14:41 +01001223static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
1224 struct io_ring_ctx *ctx)
1225 __must_hold(&ctx->uring_lock)
1226{
1227 struct percpu_ref *ref = req->fixed_rsrc_refs;
1228
1229 if (ref) {
1230 if (ref == &ctx->rsrc_node->refs)
1231 ctx->rsrc_cached_refs++;
1232 else
1233 percpu_ref_put(ref);
1234 }
1235}
1236
1237static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx)
1238{
1239 if (req->fixed_rsrc_refs)
1240 percpu_ref_put(req->fixed_rsrc_refs);
1241}
1242
1243static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
1244 __must_hold(&ctx->uring_lock)
1245{
1246 if (ctx->rsrc_cached_refs) {
1247 percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs);
1248 ctx->rsrc_cached_refs = 0;
1249 }
1250}
1251
1252static void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
1253 __must_hold(&ctx->uring_lock)
1254{
1255 ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
1256 percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH);
1257}
1258
Pavel Begunkova46be972021-10-09 23:14:40 +01001259static inline void io_req_set_rsrc_node(struct io_kiocb *req,
1260 struct io_ring_ctx *ctx)
Jens Axboec40f6372020-06-25 15:39:59 -06001261{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001262 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001263 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Pavel Begunkovab409402021-10-09 23:14:41 +01001264 ctx->rsrc_cached_refs--;
1265 if (unlikely(ctx->rsrc_cached_refs < 0))
1266 io_rsrc_refs_refill(ctx);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001267 }
1268}
1269
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00001270static unsigned int __io_put_kbuf(struct io_kiocb *req)
Hao Xu3648e522021-12-05 14:37:57 +00001271{
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00001272 struct io_buffer *kbuf = req->kbuf;
Hao Xu3648e522021-12-05 14:37:57 +00001273 unsigned int cflags;
1274
1275 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1276 cflags |= IORING_CQE_F_BUFFER;
1277 req->flags &= ~REQ_F_BUFFER_SELECTED;
1278 kfree(kbuf);
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00001279 req->kbuf = NULL;
Hao Xu3648e522021-12-05 14:37:57 +00001280 return cflags;
1281}
1282
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00001283static inline unsigned int io_put_kbuf(struct io_kiocb *req)
Hao Xu3648e522021-12-05 14:37:57 +00001284{
1285 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
1286 return 0;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00001287 return __io_put_kbuf(req);
Hao Xu3648e522021-12-05 14:37:57 +00001288}
1289
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001290static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1291{
1292 bool got = percpu_ref_tryget(ref);
1293
1294 /* already at zero, wait for ->release() */
1295 if (!got)
1296 wait_for_completion(compl);
1297 percpu_ref_resurrect(ref);
1298 if (got)
1299 percpu_ref_put(ref);
1300}
1301
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001302static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1303 bool cancel_all)
Pavel Begunkov6af3f482021-11-26 14:38:15 +00001304 __must_hold(&req->ctx->timeout_lock)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001305{
1306 struct io_kiocb *req;
1307
Pavel Begunkov68207682021-03-22 01:58:25 +00001308 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001309 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001310 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001311 return true;
1312
1313 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001314 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001315 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001316 }
1317 return false;
1318}
1319
Pavel Begunkov6af3f482021-11-26 14:38:15 +00001320static bool io_match_linked(struct io_kiocb *head)
1321{
1322 struct io_kiocb *req;
1323
1324 io_for_each_link(req, head) {
1325 if (req->flags & REQ_F_INFLIGHT)
1326 return true;
1327 }
1328 return false;
1329}
1330
1331/*
1332 * As io_match_task() but protected against racing with linked timeouts.
1333 * User must not hold timeout_lock.
1334 */
1335static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
1336 bool cancel_all)
1337{
1338 bool matched;
1339
1340 if (task && head->task != task)
1341 return false;
1342 if (cancel_all)
1343 return true;
1344
1345 if (head->flags & REQ_F_LINK_TIMEOUT) {
1346 struct io_ring_ctx *ctx = head->ctx;
1347
1348 /* protect against races with linked timeouts */
1349 spin_lock_irq(&ctx->timeout_lock);
1350 matched = io_match_linked(head);
1351 spin_unlock_irq(&ctx->timeout_lock);
1352 } else {
1353 matched = io_match_linked(head);
1354 }
1355 return matched;
1356}
1357
Pavel Begunkovd886e182021-10-04 20:02:56 +01001358static inline bool req_has_async_data(struct io_kiocb *req)
1359{
1360 return req->flags & REQ_F_ASYNC_DATA;
1361}
1362
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001363static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001364{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001365 req->flags |= REQ_F_FAIL;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001366 if (req->flags & REQ_F_CQE_SKIP) {
1367 req->flags &= ~REQ_F_CQE_SKIP;
1368 req->flags |= REQ_F_SKIP_LINK_CQES;
1369 }
Jens Axboec40f6372020-06-25 15:39:59 -06001370}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001371
Hao Xua8295b92021-08-27 17:46:09 +08001372static inline void req_fail_link_node(struct io_kiocb *req, int res)
1373{
1374 req_set_fail(req);
1375 req->result = res;
1376}
1377
Pavel Begunkovc0724812021-10-04 20:02:54 +01001378static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001379{
1380 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1381
Jens Axboe0f158b42020-05-14 17:18:39 -06001382 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001383}
1384
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001385static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1386{
1387 return !req->timeout.off;
1388}
1389
Pavel Begunkovc0724812021-10-04 20:02:54 +01001390static __cold void io_fallback_req_func(struct work_struct *work)
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001391{
1392 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1393 fallback_work.work);
1394 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1395 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001396 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001397
1398 percpu_ref_get(&ctx->refs);
1399 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001400 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001401
Pavel Begunkovf237c302021-08-18 12:42:46 +01001402 if (locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01001403 io_submit_flush_completions(ctx);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001404 mutex_unlock(&ctx->uring_lock);
1405 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001406 percpu_ref_put(&ctx->refs);
1407}
1408
Pavel Begunkovc0724812021-10-04 20:02:54 +01001409static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001410{
1411 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001412 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001413
1414 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1415 if (!ctx)
1416 return NULL;
1417
Jens Axboe78076bb2019-12-04 19:56:40 -07001418 /*
1419 * Use 5 bits less than the max cq entries, that should give us around
1420 * 32 entries per hash list if totally full and uniformly spread.
1421 */
1422 hash_bits = ilog2(p->cq_entries);
1423 hash_bits -= 5;
1424 if (hash_bits <= 0)
1425 hash_bits = 1;
1426 ctx->cancel_hash_bits = hash_bits;
1427 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1428 GFP_KERNEL);
1429 if (!ctx->cancel_hash)
1430 goto err;
1431 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1432
Pavel Begunkov62248432021-04-28 13:11:29 +01001433 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1434 if (!ctx->dummy_ubuf)
1435 goto err;
1436 /* set invalid range, so io_import_fixed() fails meeting it */
1437 ctx->dummy_ubuf->ubuf = -1UL;
1438
Roman Gushchin21482892019-05-07 10:01:48 -07001439 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001440 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1441 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001442
1443 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001444 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001445 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001446 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001447 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001448 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001449 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001450 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001451 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001452 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001453 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01001454 INIT_WQ_LIST(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001455 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001456 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001457 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001458 spin_lock_init(&ctx->rsrc_ref_lock);
1459 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001460 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1461 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001462 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001463 ctx->submit_state.free_list.next = NULL;
1464 INIT_WQ_LIST(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001465 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001466 INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001467 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001468err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001469 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001470 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001471 kfree(ctx);
1472 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001473}
1474
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001475static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1476{
1477 struct io_rings *r = ctx->rings;
1478
1479 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1480 ctx->cq_extra--;
1481}
1482
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001483static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001484{
Jens Axboe2bc99302020-07-09 09:43:27 -06001485 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1486 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001487
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001488 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001489 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001490
Bob Liu9d858b22019-11-13 18:06:25 +08001491 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001492}
1493
Pavel Begunkov35645ac2021-10-17 00:07:09 +01001494#define FFS_NOWAIT 0x1UL
1495#define FFS_ISREG 0x2UL
1496#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG)
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001497
1498static inline bool io_req_ffs_set(struct io_kiocb *req)
1499{
Pavel Begunkov35645ac2021-10-17 00:07:09 +01001500 return req->flags & REQ_F_FIXED_FILE;
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001501}
1502
Pavel Begunkovc0724812021-10-04 20:02:54 +01001503static inline void io_req_track_inflight(struct io_kiocb *req)
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001504{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001505 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001506 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001507 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001508 }
1509}
1510
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001511static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1512{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001513 if (WARN_ON_ONCE(!req->link))
1514 return NULL;
1515
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001516 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1517 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001518
1519 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001520 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001521 __io_req_set_refcount(req->link, 2);
1522 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001523}
1524
1525static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1526{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001527 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001528 return NULL;
1529 return __io_prep_linked_timeout(req);
1530}
1531
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001532static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001533{
Jens Axboed3656342019-12-18 09:50:26 -07001534 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001535 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001536
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001537 if (!(req->flags & REQ_F_CREDS)) {
1538 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001539 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001540 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001541
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001542 req->work.list.next = NULL;
1543 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001544 if (req->flags & REQ_F_FORCE_ASYNC)
1545 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1546
Jens Axboed3656342019-12-18 09:50:26 -07001547 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001548 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001549 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001550 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001551 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001552 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001553 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001554
1555 switch (req->opcode) {
1556 case IORING_OP_SPLICE:
1557 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001558 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1559 req->work.flags |= IO_WQ_WORK_UNBOUND;
1560 break;
1561 }
Jens Axboe561fb042019-10-24 07:25:42 -06001562}
1563
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001564static void io_prep_async_link(struct io_kiocb *req)
1565{
1566 struct io_kiocb *cur;
1567
Pavel Begunkov44eff402021-07-26 14:14:31 +01001568 if (req->flags & REQ_F_LINK_TIMEOUT) {
1569 struct io_ring_ctx *ctx = req->ctx;
1570
Pavel Begunkov674ee8e2021-11-23 01:45:35 +00001571 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001572 io_for_each_link(cur, req)
1573 io_prep_async_work(cur);
Pavel Begunkov674ee8e2021-11-23 01:45:35 +00001574 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001575 } else {
1576 io_for_each_link(cur, req)
1577 io_prep_async_work(cur);
1578 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001579}
1580
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001581static inline void io_req_add_compl_list(struct io_kiocb *req)
1582{
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00001583 struct io_ring_ctx *ctx = req->ctx;
Hao Xu33ce2af2021-12-14 13:59:04 +08001584 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001585
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00001586 if (!(req->flags & REQ_F_CQE_SKIP))
1587 ctx->submit_state.flush_cqes = true;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001588 wq_list_add_tail(&req->comp_list, &state->compl_reqs);
1589}
1590
Arnd Bergmann00169242021-10-19 17:34:53 +02001591static void io_queue_async_work(struct io_kiocb *req, bool *dont_use)
Jens Axboe561fb042019-10-24 07:25:42 -06001592{
Jackie Liua197f662019-11-08 08:09:12 -07001593 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001594 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001595 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001596
Jens Axboe3bfe6102021-02-16 14:15:30 -07001597 BUG_ON(!tctx);
1598 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001599
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001600 /* init ->work of the whole link before punting */
1601 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001602
1603 /*
1604 * Not expected to happen, but if we do have a bug where this _can_
1605 * happen, catch it here and ensure the request is marked as
1606 * canceled. That will make io-wq go through the usual work cancel
1607 * procedure rather than attempt to run this request (or create a new
1608 * worker for it).
1609 */
1610 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1611 req->work.flags |= IO_WQ_WORK_CANCEL;
1612
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001613 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1614 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001615 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001616 if (link)
1617 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001618}
1619
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001620static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001621 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001622 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001623{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001624 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001625
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001626 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001627 if (status)
1628 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001629 atomic_set(&req->ctx->cq_timeouts,
1630 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001631 list_del_init(&req->timeout.list);
Pavel Begunkov913a5712021-11-10 15:49:31 +00001632 io_fill_cqe_req(req, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001633 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001634 }
1635}
1636
Pavel Begunkovc0724812021-10-04 20:02:54 +01001637static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001638{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001639 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001640 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1641 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001642
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001643 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001644 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001645 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001646 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001647 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001648 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001649}
1650
Pavel Begunkovc0724812021-10-04 20:02:54 +01001651static __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001652 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001653{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001654 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001655
Jens Axboe79ebeae2021-08-10 15:18:27 -06001656 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001657 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001658 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001659 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001660 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001661
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001662 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001663 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001664
1665 /*
1666 * Since seq can easily wrap around over time, subtract
1667 * the last seq at which timeouts were flushed before comparing.
1668 * Assuming not more than 2^31-1 events have happened since,
1669 * these subtractions won't have wrapped, so we can check if
1670 * target is in [last_seq, current_seq] by comparing the two.
1671 */
1672 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1673 events_got = seq - ctx->cq_last_tm_flush;
1674 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001675 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001676
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001677 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001678 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001679 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001680 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001681 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001682}
1683
Pavel Begunkovc0724812021-10-04 20:02:54 +01001684static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001685{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001686 if (ctx->off_timeout_used)
1687 io_flush_timeouts(ctx);
1688 if (ctx->drain_active)
1689 io_queue_deferred(ctx);
1690}
1691
1692static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1693{
1694 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1695 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001696 /* order cqe stores with ring update */
1697 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001698}
1699
Jens Axboe90554202020-09-03 12:12:41 -06001700static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1701{
1702 struct io_rings *r = ctx->rings;
1703
Pavel Begunkova566c552021-05-16 22:58:08 +01001704 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001705}
1706
Pavel Begunkov888aae22021-01-19 13:32:39 +00001707static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1708{
1709 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1710}
1711
Pavel Begunkovd068b502021-05-16 22:58:11 +01001712static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001713{
Hristo Venev75b28af2019-08-26 17:23:46 +00001714 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001715 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001716
Stefan Bühler115e12e2019-04-24 23:54:18 +02001717 /*
1718 * writes to the cq entry need to come after reading head; the
1719 * control dependency is enough as we're using WRITE_ONCE to
1720 * fill the cq entry
1721 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001722 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001723 return NULL;
1724
Pavel Begunkov888aae22021-01-19 13:32:39 +00001725 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001726 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727}
1728
Jens Axboef2842ab2020-01-08 11:04:00 -07001729static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1730{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001731 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001732 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001733 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1734 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001735 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001736}
1737
Jens Axboe2c5d7632021-08-21 07:21:19 -06001738/*
1739 * This should only get called when at least one event has been posted.
1740 * Some applications rely on the eventfd notification count only changing
1741 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1742 * 1:1 relationship between how many times this function is called (and
1743 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1744 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001745static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001746{
Jens Axboe5fd46172021-08-06 14:04:31 -06001747 /*
1748 * wake_up_all() may seem excessive, but io_wake_function() and
1749 * io_should_wake() handle the termination of the loop and only
1750 * wake as many waiters as we need to.
1751 */
1752 if (wq_has_sleeper(&ctx->cq_wait))
1753 wake_up_all(&ctx->cq_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001754 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001755 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001756}
1757
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001758static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1759{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001760 /* see waitqueue_active() comment */
1761 smp_mb();
1762
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001763 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001764 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001765 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001766 }
1767 if (io_should_trigger_evfd(ctx))
1768 eventfd_signal(ctx->cq_ev_fd, 1);
1769}
1770
Jens Axboec4a2ed72019-11-21 21:01:26 -07001771/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001772static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001773{
Jens Axboeb18032b2021-01-24 16:58:56 -07001774 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001775
Pavel Begunkova566c552021-05-16 22:58:08 +01001776 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001777 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001778
Jens Axboeb18032b2021-01-24 16:58:56 -07001779 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001780 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001781 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001782 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001783 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001784
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001785 if (!cqe && !force)
1786 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001787 ocqe = list_first_entry(&ctx->cq_overflow_list,
1788 struct io_overflow_cqe, list);
1789 if (cqe)
1790 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1791 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001792 io_account_cq_overflow(ctx);
1793
Jens Axboeb18032b2021-01-24 16:58:56 -07001794 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001795 list_del(&ocqe->list);
1796 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001797 }
1798
Pavel Begunkov09e88402020-12-17 00:24:38 +00001799 all_flushed = list_empty(&ctx->cq_overflow_list);
1800 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001801 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001802 WRITE_ONCE(ctx->rings->sq_flags,
1803 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001804 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001805
Jens Axboeb18032b2021-01-24 16:58:56 -07001806 if (posted)
1807 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001808 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001809 if (posted)
1810 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001811 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001812}
1813
Pavel Begunkov90f67362021-08-09 20:18:12 +01001814static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001815{
Jens Axboeca0a2652021-03-04 17:15:48 -07001816 bool ret = true;
1817
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001818 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001819 /* iopoll syncs against uring_lock, not completion_lock */
1820 if (ctx->flags & IORING_SETUP_IOPOLL)
1821 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001822 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001823 if (ctx->flags & IORING_SETUP_IOPOLL)
1824 mutex_unlock(&ctx->uring_lock);
1825 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001826
1827 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001828}
1829
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001830/* must to be called somewhat shortly after putting a request */
1831static inline void io_put_task(struct task_struct *task, int nr)
1832{
1833 struct io_uring_task *tctx = task->io_uring;
1834
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001835 if (likely(task == current)) {
1836 tctx->cached_refs += nr;
1837 } else {
1838 percpu_counter_sub(&tctx->inflight, nr);
1839 if (unlikely(atomic_read(&tctx->in_idle)))
1840 wake_up(&tctx->wait);
1841 put_task_struct_many(task, nr);
1842 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001843}
1844
Pavel Begunkov9a108672021-08-27 11:55:01 +01001845static void io_task_refs_refill(struct io_uring_task *tctx)
1846{
1847 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1848
1849 percpu_counter_add(&tctx->inflight, refill);
1850 refcount_add(refill, &current->usage);
1851 tctx->cached_refs += refill;
1852}
1853
1854static inline void io_get_task_refs(int nr)
1855{
1856 struct io_uring_task *tctx = current->io_uring;
1857
1858 tctx->cached_refs -= nr;
1859 if (unlikely(tctx->cached_refs < 0))
1860 io_task_refs_refill(tctx);
1861}
1862
Pavel Begunkov3cc7fdb2022-01-09 00:53:22 +00001863static __cold void io_uring_drop_tctx_refs(struct task_struct *task)
1864{
1865 struct io_uring_task *tctx = task->io_uring;
1866 unsigned int refs = tctx->cached_refs;
1867
1868 if (refs) {
1869 tctx->cached_refs = 0;
1870 percpu_counter_sub(&tctx->inflight, refs);
1871 put_task_struct_many(task, refs);
1872 }
1873}
1874
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001875static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001876 s32 res, u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001877{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001878 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001879
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001880 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1881 if (!ocqe) {
1882 /*
1883 * If we're in ring overflow flush mode, or in task cancel mode,
1884 * or cannot allocate an overflow entry, then we need to drop it
1885 * on the floor.
1886 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001887 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001888 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001890 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001891 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001892 WRITE_ONCE(ctx->rings->sq_flags,
1893 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1894
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001895 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001896 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001897 ocqe->cqe.res = res;
1898 ocqe->cqe.flags = cflags;
1899 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1900 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901}
1902
Pavel Begunkov913a5712021-11-10 15:49:31 +00001903static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data,
1904 s32 res, u32 cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001905{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001906 struct io_uring_cqe *cqe;
1907
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001908 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909
1910 /*
1911 * If we can't get a cq entry, userspace overflowed the
1912 * submission (by quite a lot). Increment the overflow count in
1913 * the ring.
1914 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001915 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001917 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001918 WRITE_ONCE(cqe->res, res);
1919 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001920 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001921 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001922 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001923}
1924
Pavel Begunkov913a5712021-11-10 15:49:31 +00001925static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001926{
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001927 if (!(req->flags & REQ_F_CQE_SKIP))
1928 __io_fill_cqe(req->ctx, req->user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001929}
1930
Pavel Begunkov913a5712021-11-10 15:49:31 +00001931static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data,
1932 s32 res, u32 cflags)
1933{
1934 ctx->cq_extra++;
1935 return __io_fill_cqe(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001936}
1937
Hao Xua37fae82021-12-07 17:39:50 +08001938static void __io_req_complete_post(struct io_kiocb *req, s32 res,
1939 u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001940{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001941 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001943 if (!(req->flags & REQ_F_CQE_SKIP))
1944 __io_fill_cqe(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001945 /*
1946 * If we're the last reference to this request, add to our locked
1947 * free_list cache.
1948 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001949 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001950 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001951 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001952 io_disarm_next(req);
1953 if (req->link) {
1954 io_req_task_queue(req->link);
1955 req->link = NULL;
1956 }
1957 }
Pavel Begunkovab409402021-10-09 23:14:41 +01001958 io_req_put_rsrc(req, ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001959 io_dismantle_req(req);
1960 io_put_task(req->task, 1);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001961 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001962 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001963 }
Hao Xua37fae82021-12-07 17:39:50 +08001964}
1965
1966static void io_req_complete_post(struct io_kiocb *req, s32 res,
1967 u32 cflags)
1968{
1969 struct io_ring_ctx *ctx = req->ctx;
1970
1971 spin_lock(&ctx->completion_lock);
1972 __io_req_complete_post(req, res, cflags);
Pavel Begunkov7a612352021-03-09 00:37:59 +00001973 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001974 spin_unlock(&ctx->completion_lock);
Pavel Begunkova3f349072021-09-15 12:04:20 +01001975 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001976}
1977
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001978static inline void io_req_complete_state(struct io_kiocb *req, s32 res,
1979 u32 cflags)
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001980{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001981 req->result = res;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +01001982 req->cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001983 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001984}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001985
Pavel Begunkov889fca72021-02-10 00:03:09 +00001986static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001987 s32 res, u32 cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001988{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001989 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1990 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001991 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001992 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001993}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001994
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001995static inline void io_req_complete(struct io_kiocb *req, s32 res)
Jens Axboee1e16092020-06-22 09:17:17 -06001996{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001997 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001998}
1999
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01002000static void io_req_complete_failed(struct io_kiocb *req, s32 res)
Pavel Begunkovf41db2732021-02-28 22:35:12 +00002001{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002002 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00002003 io_req_complete_post(req, res, 0);
2004}
2005
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01002006static void io_req_complete_fail_submit(struct io_kiocb *req)
2007{
2008 /*
2009 * We don't submit, fail them all, for that replace hardlinks with
2010 * normal links. Extra REQ_F_LINK is tolerated.
2011 */
2012 req->flags &= ~REQ_F_HARDLINK;
2013 req->flags |= REQ_F_LINK;
2014 io_req_complete_failed(req, req->result);
2015}
2016
Pavel Begunkov864ea922021-08-09 13:04:08 +01002017/*
2018 * Don't initialise the fields below on every allocation, but do that in
2019 * advance and keep them valid across allocations.
2020 */
2021static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
2022{
2023 req->ctx = ctx;
2024 req->link = NULL;
2025 req->async_data = NULL;
2026 /* not necessary, but safer to zero */
2027 req->result = 0;
2028}
2029
Pavel Begunkovdac7a092021-03-19 17:22:39 +00002030static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002031 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00002032{
Jens Axboe79ebeae2021-08-10 15:18:27 -06002033 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002034 wq_list_splice(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01002035 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06002036 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00002037}
2038
Pavel Begunkovdd78f492021-03-19 17:22:35 +00002039/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07002040static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002041{
Jens Axboec7dae4b2021-02-09 19:53:37 -07002042 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002043
Jens Axboec7dae4b2021-02-09 19:53:37 -07002044 /*
2045 * If we have more than a batch's worth of requests in our IRQ side
2046 * locked cache, grab the lock and move them over to our submission
2047 * side cache.
2048 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01002049 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002050 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002051 return !!state->free_list.next;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002052}
2053
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01002054/*
2055 * A request might get retired back into the request caches even before opcode
2056 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
2057 * Because of that, io_alloc_req() should be called only under ->uring_lock
2058 * and with extra caution to not get a request that is still worked on.
2059 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01002060static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01002061 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002062{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002063 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01002064 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002065 void *reqs[IO_REQ_ALLOC_BATCH];
2066 struct io_kiocb *req;
Pavel Begunkov864ea922021-08-09 13:04:08 +01002067 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002068
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002069 if (likely(state->free_list.next || io_flush_cached_reqs(ctx)))
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01002070 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002071
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002072 ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002073
Pavel Begunkov864ea922021-08-09 13:04:08 +01002074 /*
2075 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2076 * retry single alloc to be on the safe side.
2077 */
2078 if (unlikely(ret <= 0)) {
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002079 reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2080 if (!reqs[0])
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01002081 return false;
Pavel Begunkov864ea922021-08-09 13:04:08 +01002082 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002083 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01002084
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002085 percpu_ref_get_many(&ctx->refs, ret);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002086 for (i = 0; i < ret; i++) {
2087 req = reqs[i];
2088
2089 io_preinit_req(req, ctx);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002090 wq_stack_add_head(&req->comp_list, &state->free_list);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002091 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01002092 return true;
2093}
2094
2095static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
2096{
2097 if (unlikely(!ctx->submit_state.free_list.next))
2098 return __io_alloc_req_refill(ctx);
2099 return true;
2100}
2101
2102static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
2103{
2104 struct io_wq_work_node *node;
2105
2106 node = wq_stack_extract(&ctx->submit_state.free_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002107 return container_of(node, struct io_kiocb, comp_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002108}
2109
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002110static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002111{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002112 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002113 fput(file);
2114}
2115
Pavel Begunkov6b639522021-09-08 16:40:50 +01002116static inline void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002117{
Pavel Begunkov094bae42021-03-19 17:22:42 +00002118 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002119
Pavel Begunkov867f8fa2021-10-04 20:02:58 +01002120 if (unlikely(flags & IO_REQ_CLEAN_FLAGS))
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01002121 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002122 if (!(flags & REQ_F_FIXED_FILE))
2123 io_put_file(req->file);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002124}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002125
Pavel Begunkovc0724812021-10-04 20:02:54 +01002126static __cold void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002127{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002128 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002129
Pavel Begunkovab409402021-10-09 23:14:41 +01002130 io_req_put_rsrc(req, ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002131 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002132 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002133
Jens Axboe79ebeae2021-08-10 15:18:27 -06002134 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002135 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01002136 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06002137 spin_unlock(&ctx->completion_lock);
Jens Axboee65ef562019-03-12 10:16:44 -06002138}
2139
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002140static inline void io_remove_next_linked(struct io_kiocb *req)
2141{
2142 struct io_kiocb *nxt = req->link;
2143
2144 req->link = nxt->link;
2145 nxt->link = NULL;
2146}
2147
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002148static bool io_kill_linked_timeout(struct io_kiocb *req)
2149 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06002150 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002151{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002152 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002153
Pavel Begunkovb97e7362021-08-15 10:40:23 +01002154 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002155 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002156
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002157 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002158 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01002159 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06002160 list_del(&link->timeout.list);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002161 /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */
Pavel Begunkov913a5712021-11-10 15:49:31 +00002162 io_fill_cqe_req(link, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002163 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002164 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002165 }
2166 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002167 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002168}
2169
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002170static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002171 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002172{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002173 struct io_kiocb *nxt, *link = req->link;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002174 bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES;
Jens Axboe9e645e112019-05-10 16:07:28 -06002175
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002176 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002177 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002178 long res = -ECANCELED;
2179
2180 if (link->flags & REQ_F_FAIL)
2181 res = link->result;
2182
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002183 nxt = link->link;
2184 link->link = NULL;
2185
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002186 trace_io_uring_fail_link(req, link);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002187 if (!ignore_cqes) {
2188 link->flags &= ~REQ_F_CQE_SKIP;
2189 io_fill_cqe_req(link, res, 0);
2190 }
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002191 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002192 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002193 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002194}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002195
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002196static bool io_disarm_next(struct io_kiocb *req)
2197 __must_hold(&req->ctx->completion_lock)
2198{
2199 bool posted = false;
2200
Pavel Begunkov0756a862021-08-15 10:40:25 +01002201 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2202 struct io_kiocb *link = req->link;
2203
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002204 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002205 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2206 io_remove_next_linked(req);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002207 /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */
Pavel Begunkov913a5712021-11-10 15:49:31 +00002208 io_fill_cqe_req(link, -ECANCELED, 0);
Pavel Begunkov0756a862021-08-15 10:40:25 +01002209 io_put_req_deferred(link);
2210 posted = true;
2211 }
2212 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002213 struct io_ring_ctx *ctx = req->ctx;
2214
2215 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002216 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002217 spin_unlock_irq(&ctx->timeout_lock);
2218 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002219 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002220 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002221 posted |= (req->link != NULL);
2222 io_fail_links(req);
2223 }
2224 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002225}
2226
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002227static void __io_req_find_next_prep(struct io_kiocb *req)
2228{
2229 struct io_ring_ctx *ctx = req->ctx;
2230 bool posted;
2231
2232 spin_lock(&ctx->completion_lock);
2233 posted = io_disarm_next(req);
2234 if (posted)
Hao Xu33ce2af2021-12-14 13:59:04 +08002235 io_commit_cqring(ctx);
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002236 spin_unlock(&ctx->completion_lock);
2237 if (posted)
2238 io_cqring_ev_posted(ctx);
2239}
2240
2241static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002242{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002243 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002244
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002245 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2246 return NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002247 /*
2248 * If LINK is set, we have dependent requests in this chain. If we
2249 * didn't fail this request, queue the first one up, moving any other
2250 * dependencies to the next request. In case of failure, fail the rest
2251 * of the chain.
2252 */
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002253 if (unlikely(req->flags & IO_DISARM_MASK))
2254 __io_req_find_next_prep(req);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002255 nxt = req->link;
2256 req->link = NULL;
2257 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002258}
Jens Axboe2665abf2019-11-05 12:40:47 -07002259
Pavel Begunkovf237c302021-08-18 12:42:46 +01002260static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002261{
2262 if (!ctx)
2263 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002264 if (*locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01002265 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002266 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002267 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002268 }
2269 percpu_ref_put(&ctx->refs);
2270}
2271
Hao Xuf28c240e2021-12-08 13:21:25 +08002272static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx)
2273{
2274 io_commit_cqring(ctx);
2275 spin_unlock(&ctx->completion_lock);
2276 io_cqring_ev_posted(ctx);
2277}
2278
2279static void handle_prev_tw_list(struct io_wq_work_node *node,
2280 struct io_ring_ctx **ctx, bool *uring_locked)
2281{
2282 if (*ctx && !*uring_locked)
2283 spin_lock(&(*ctx)->completion_lock);
2284
2285 do {
2286 struct io_wq_work_node *next = node->next;
2287 struct io_kiocb *req = container_of(node, struct io_kiocb,
2288 io_task_work.node);
2289
2290 if (req->ctx != *ctx) {
2291 if (unlikely(!*uring_locked && *ctx))
2292 ctx_commit_and_unlock(*ctx);
2293
2294 ctx_flush_and_put(*ctx, uring_locked);
2295 *ctx = req->ctx;
2296 /* if not contended, grab and improve batching */
2297 *uring_locked = mutex_trylock(&(*ctx)->uring_lock);
2298 percpu_ref_get(&(*ctx)->refs);
2299 if (unlikely(!*uring_locked))
2300 spin_lock(&(*ctx)->completion_lock);
2301 }
2302 if (likely(*uring_locked))
2303 req->io_task_work.func(req, uring_locked);
2304 else
2305 __io_req_complete_post(req, req->result, io_put_kbuf(req));
2306 node = next;
2307 } while (node);
2308
2309 if (unlikely(!*uring_locked))
2310 ctx_commit_and_unlock(*ctx);
2311}
2312
2313static void handle_tw_list(struct io_wq_work_node *node,
2314 struct io_ring_ctx **ctx, bool *locked)
Hao Xu9f8d0322021-12-07 17:39:49 +08002315{
2316 do {
2317 struct io_wq_work_node *next = node->next;
2318 struct io_kiocb *req = container_of(node, struct io_kiocb,
2319 io_task_work.node);
2320
2321 if (req->ctx != *ctx) {
2322 ctx_flush_and_put(*ctx, locked);
2323 *ctx = req->ctx;
2324 /* if not contended, grab and improve batching */
2325 *locked = mutex_trylock(&(*ctx)->uring_lock);
2326 percpu_ref_get(&(*ctx)->refs);
2327 }
2328 req->io_task_work.func(req, locked);
2329 node = next;
2330 } while (node);
2331}
2332
Jens Axboe7cbf1722021-02-10 00:03:20 +00002333static void tctx_task_work(struct callback_head *cb)
2334{
Hao Xuf28c240e2021-12-08 13:21:25 +08002335 bool uring_locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002336 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002337 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2338 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002339
Pavel Begunkov16f72072021-06-17 18:14:09 +01002340 while (1) {
Hao Xuf28c240e2021-12-08 13:21:25 +08002341 struct io_wq_work_node *node1, *node2;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002342
Hao Xuf28c240e2021-12-08 13:21:25 +08002343 if (!tctx->task_list.first &&
2344 !tctx->prior_task_list.first && uring_locked)
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002345 io_submit_flush_completions(ctx);
2346
Pavel Begunkov3f184072021-06-17 18:14:06 +01002347 spin_lock_irq(&tctx->task_lock);
Hao Xuf28c240e2021-12-08 13:21:25 +08002348 node1 = tctx->prior_task_list.first;
2349 node2 = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002350 INIT_WQ_LIST(&tctx->task_list);
Hao Xuf28c240e2021-12-08 13:21:25 +08002351 INIT_WQ_LIST(&tctx->prior_task_list);
2352 if (!node2 && !node1)
Pavel Begunkov6294f362021-08-10 17:53:55 +01002353 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002354 spin_unlock_irq(&tctx->task_lock);
Hao Xuf28c240e2021-12-08 13:21:25 +08002355 if (!node2 && !node1)
Pavel Begunkov6294f362021-08-10 17:53:55 +01002356 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002357
Hao Xuf28c240e2021-12-08 13:21:25 +08002358 if (node1)
2359 handle_prev_tw_list(node1, &ctx, &uring_locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002360
Hao Xuf28c240e2021-12-08 13:21:25 +08002361 if (node2)
2362 handle_tw_list(node2, &ctx, &uring_locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002363 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002364 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002365
Hao Xuf28c240e2021-12-08 13:21:25 +08002366 ctx_flush_and_put(ctx, &uring_locked);
Pavel Begunkov3cc7fdb2022-01-09 00:53:22 +00002367
2368 /* relaxed read is enough as only the task itself sets ->in_idle */
2369 if (unlikely(atomic_read(&tctx->in_idle)))
2370 io_uring_drop_tctx_refs(current);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002371}
2372
Hao Xu4813c372021-12-07 17:39:48 +08002373static void io_req_task_work_add(struct io_kiocb *req, bool priority)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002374{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002375 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002376 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002377 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002378 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002379 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002380 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002381
2382 WARN_ON_ONCE(!tctx);
2383
Jens Axboe0b81e802021-02-16 10:33:53 -07002384 spin_lock_irqsave(&tctx->task_lock, flags);
Hao Xu4813c372021-12-07 17:39:48 +08002385 if (priority)
2386 wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list);
2387 else
2388 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002389 running = tctx->task_running;
2390 if (!running)
2391 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002392 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002393
2394 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002395 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002396 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002397
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002398 /*
2399 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2400 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2401 * processing task_work. There's no reliable way to tell if TWA_RESUME
2402 * will do the job.
2403 */
2404 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovd97ec622021-09-08 16:40:53 +01002405 if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2406 if (notify == TWA_NONE)
2407 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002408 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002409 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002410
Pavel Begunkove09ee512021-07-01 13:26:05 +01002411 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002412 tctx->task_running = false;
Hao Xu4813c372021-12-07 17:39:48 +08002413 node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002414 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002415
Pavel Begunkove09ee512021-07-01 13:26:05 +01002416 while (node) {
2417 req = container_of(node, struct io_kiocb, io_task_work.node);
2418 node = node->next;
2419 if (llist_add(&req->io_task_work.fallback_node,
2420 &req->ctx->fallback_llist))
2421 schedule_delayed_work(&req->ctx->fallback_work, 1);
2422 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002423}
2424
Pavel Begunkovf237c302021-08-18 12:42:46 +01002425static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002426{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002427 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002428
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002429 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002430 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002431 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002432}
2433
Pavel Begunkovf237c302021-08-18 12:42:46 +01002434static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002435{
2436 struct io_ring_ctx *ctx = req->ctx;
2437
Pavel Begunkovf237c302021-08-18 12:42:46 +01002438 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002439 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002440 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002441 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002442 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002443 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002444}
2445
Pavel Begunkova3df76982021-02-18 22:32:52 +00002446static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2447{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002448 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002449 req->io_task_work.func = io_req_task_cancel;
Hao Xu4813c372021-12-07 17:39:48 +08002450 io_req_task_work_add(req, false);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002451}
2452
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002453static void io_req_task_queue(struct io_kiocb *req)
2454{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002455 req->io_task_work.func = io_req_task_submit;
Hao Xu4813c372021-12-07 17:39:48 +08002456 io_req_task_work_add(req, false);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002457}
2458
Jens Axboe773af692021-07-27 10:25:55 -06002459static void io_req_task_queue_reissue(struct io_kiocb *req)
2460{
2461 req->io_task_work.func = io_queue_async_work;
Hao Xu4813c372021-12-07 17:39:48 +08002462 io_req_task_work_add(req, false);
Jens Axboe773af692021-07-27 10:25:55 -06002463}
2464
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002465static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002466{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002467 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002468
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002469 if (nxt)
2470 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002471}
2472
Jens Axboe9e645e112019-05-10 16:07:28 -06002473static void io_free_req(struct io_kiocb *req)
2474{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002475 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002476 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002477}
2478
Pavel Begunkovf237c302021-08-18 12:42:46 +01002479static void io_free_req_work(struct io_kiocb *req, bool *locked)
2480{
2481 io_free_req(req);
2482}
2483
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002484static void io_free_batch_list(struct io_ring_ctx *ctx,
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002485 struct io_wq_work_node *node)
Jens Axboea141dd82021-08-12 12:48:34 -06002486 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002487{
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002488 struct task_struct *task = NULL;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002489 int task_refs = 0;
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002490
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002491 do {
2492 struct io_kiocb *req = container_of(node, struct io_kiocb,
2493 comp_list);
2494
Pavel Begunkovdef77ac2021-10-12 15:02:14 +01002495 if (unlikely(req->flags & REQ_F_REFCOUNT)) {
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002496 node = req->comp_list.next;
Pavel Begunkovdef77ac2021-10-12 15:02:14 +01002497 if (!req_ref_put_and_test(req))
2498 continue;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002499 }
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002500
Pavel Begunkovab409402021-10-09 23:14:41 +01002501 io_req_put_rsrc_locked(req, ctx);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002502 io_queue_next(req);
2503 io_dismantle_req(req);
2504
2505 if (req->task != task) {
2506 if (task)
2507 io_put_task(task, task_refs);
2508 task = req->task;
2509 task_refs = 0;
2510 }
2511 task_refs++;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002512 node = req->comp_list.next;
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002513 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002514 } while (node);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002515
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002516 if (task)
2517 io_put_task(task, task_refs);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002518}
2519
Pavel Begunkovc4501782021-09-08 16:40:52 +01002520static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002521 __must_hold(&ctx->uring_lock)
2522{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002523 struct io_wq_work_node *node, *prev;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002524 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002525
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00002526 if (state->flush_cqes) {
2527 spin_lock(&ctx->completion_lock);
2528 wq_list_for_each(node, prev, &state->compl_reqs) {
2529 struct io_kiocb *req = container_of(node, struct io_kiocb,
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002530 comp_list);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002531
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00002532 if (!(req->flags & REQ_F_CQE_SKIP))
2533 __io_fill_cqe(ctx, req->user_data, req->result,
2534 req->cflags);
2535 }
2536
2537 io_commit_cqring(ctx);
2538 spin_unlock(&ctx->completion_lock);
2539 io_cqring_ev_posted(ctx);
2540 state->flush_cqes = false;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002541 }
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002542
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002543 io_free_batch_list(ctx, state->compl_reqs.first);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002544 INIT_WQ_LIST(&state->compl_reqs);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002545}
2546
Jens Axboeba816ad2019-09-28 11:36:45 -06002547/*
2548 * Drop reference to request, return next in chain (if there is one) if this
2549 * was the last reference to this request.
2550 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002551static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002552{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002553 struct io_kiocb *nxt = NULL;
2554
Jens Axboede9b4cc2021-02-24 13:28:27 -07002555 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002556 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002557 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002558 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002559 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002560}
2561
Pavel Begunkov0d850352021-03-19 17:22:37 +00002562static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002563{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002564 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002565 io_free_req(req);
2566}
2567
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002568static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002569{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002570 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002571 req->io_task_work.func = io_free_req_work;
Hao Xu4813c372021-12-07 17:39:48 +08002572 io_req_task_work_add(req, false);
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002573 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002574}
2575
Pavel Begunkov6c503152021-01-04 20:36:36 +00002576static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002577{
2578 /* See comment at the top of this file */
2579 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002580 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002581}
2582
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002583static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2584{
2585 struct io_rings *rings = ctx->rings;
2586
2587 /* make sure SQ entry isn't read before tail */
2588 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2589}
2590
Jens Axboe4c6e2772020-07-01 11:29:10 -06002591static inline bool io_run_task_work(void)
2592{
Nadav Amitef98eb02021-08-07 17:13:41 -07002593 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002594 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002595 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002596 return true;
2597 }
2598
2599 return false;
2600}
2601
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002602static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
Jens Axboedef596e2019-01-09 08:59:42 -07002603{
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002604 struct io_wq_work_node *pos, *start, *prev;
Christoph Hellwigd729cf92021-10-12 13:12:20 +02002605 unsigned int poll_flags = BLK_POLL_NOSLEEP;
Jens Axboeb688f112021-10-12 09:28:46 -06002606 DEFINE_IO_COMP_BATCH(iob);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002607 int nr_events = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002608
2609 /*
2610 * Only spin for completions if we don't have multiple devices hanging
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002611 * off our complete list.
Jens Axboedef596e2019-01-09 08:59:42 -07002612 */
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002613 if (ctx->poll_multi_queue || force_nonspin)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002614 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002615
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002616 wq_list_for_each(pos, start, &ctx->iopoll_list) {
2617 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002618 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002619 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002620
2621 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002622 * Move completed and retryable entries to our local lists.
2623 * If we find a request that requires polling, break out
2624 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002625 */
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002626 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002627 break;
2628
Jens Axboeb688f112021-10-12 09:28:46 -06002629 ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002630 if (unlikely(ret < 0))
2631 return ret;
2632 else if (ret)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002633 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002634
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002635 /* iopoll may have completed current req */
Jens Axboeb688f112021-10-12 09:28:46 -06002636 if (!rq_list_empty(iob.req_list) ||
2637 READ_ONCE(req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002638 break;
Jens Axboedef596e2019-01-09 08:59:42 -07002639 }
2640
Jens Axboeb688f112021-10-12 09:28:46 -06002641 if (!rq_list_empty(iob.req_list))
2642 iob.complete(&iob);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002643 else if (!pos)
2644 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002645
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002646 prev = start;
2647 wq_list_for_each_resume(pos, prev) {
2648 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
2649
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002650 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
2651 if (!smp_load_acquire(&req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002652 break;
Pavel Begunkov83a13a42021-12-05 14:37:59 +00002653 if (unlikely(req->flags & REQ_F_CQE_SKIP))
2654 continue;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00002655
Pavel Begunkov83a13a42021-12-05 14:37:59 +00002656 __io_fill_cqe(ctx, req->user_data, req->result, io_put_kbuf(req));
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002657 nr_events++;
2658 }
Jens Axboedef596e2019-01-09 08:59:42 -07002659
Pavel Begunkovf5ed3bc2021-09-24 21:59:52 +01002660 if (unlikely(!nr_events))
2661 return 0;
2662
2663 io_commit_cqring(ctx);
2664 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002665 pos = start ? start->next : ctx->iopoll_list.first;
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002666 wq_list_cut(&ctx->iopoll_list, prev, start);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002667 io_free_batch_list(ctx, pos);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002668 return nr_events;
Jens Axboedef596e2019-01-09 08:59:42 -07002669}
2670
2671/*
Jens Axboedef596e2019-01-09 08:59:42 -07002672 * We can't just wait for polled events to come to us, we have to actively
2673 * find and complete them.
2674 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01002675static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002676{
2677 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2678 return;
2679
2680 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002681 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002682 /* let it sleep and repeat later if can't complete a request */
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002683 if (io_do_iopoll(ctx, true) == 0)
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002684 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002685 /*
2686 * Ensure we allow local-to-the-cpu processing to take place,
2687 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002688 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002689 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002690 if (need_resched()) {
2691 mutex_unlock(&ctx->uring_lock);
2692 cond_resched();
2693 mutex_lock(&ctx->uring_lock);
2694 }
Jens Axboedef596e2019-01-09 08:59:42 -07002695 }
2696 mutex_unlock(&ctx->uring_lock);
2697}
2698
Pavel Begunkov7668b922020-07-07 16:36:21 +03002699static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002700{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002701 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002702 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002703
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002704 /*
2705 * We disallow the app entering submit/complete with polling, but we
2706 * still need to lock the ring to prevent racing with polled issue
2707 * that got punted to a workqueue.
2708 */
2709 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002710 /*
2711 * Don't enter poll loop if we already have events pending.
2712 * If we do, we can potentially be spinning for commands that
2713 * already triggered a CQE (eg in error).
2714 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002715 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002716 __io_cqring_overflow_flush(ctx, false);
2717 if (io_cqring_events(ctx))
2718 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002719 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002720 /*
2721 * If a submit got punted to a workqueue, we can have the
2722 * application entering polling for a command before it gets
2723 * issued. That app will hold the uring_lock for the duration
2724 * of the poll right here, so we need to take a breather every
2725 * now and then to ensure that the issue has a chance to add
2726 * the poll to the issued list. Otherwise we can spin here
2727 * forever, while the workqueue is stuck trying to acquire the
2728 * very same mutex.
2729 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002730 if (wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002731 u32 tail = ctx->cached_cq_tail;
2732
Jens Axboe500f9fb2019-08-19 12:15:59 -06002733 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002734 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002735 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002736
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002737 /* some requests don't go through iopoll_list */
2738 if (tail != ctx->cached_cq_tail ||
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002739 wq_list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002740 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002741 }
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002742 ret = io_do_iopoll(ctx, !min);
2743 if (ret < 0)
2744 break;
2745 nr_events += ret;
2746 ret = 0;
2747 } while (nr_events < min && !need_resched());
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002748out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002749 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002750 return ret;
2751}
2752
Jens Axboe491381ce2019-10-17 09:20:46 -06002753static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002754{
Jens Axboe491381ce2019-10-17 09:20:46 -06002755 /*
2756 * Tell lockdep we inherited freeze protection from submission
2757 * thread.
2758 */
2759 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002760 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002761
Pavel Begunkov1c986792021-03-22 01:58:31 +00002762 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2763 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002764 }
2765}
2766
Jens Axboeb63534c2020-06-04 11:28:00 -06002767#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002768static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002769{
Pavel Begunkovab454432021-03-22 01:58:33 +00002770 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002771
Pavel Begunkovd886e182021-10-04 20:02:56 +01002772 if (!req_has_async_data(req))
Pavel Begunkovab454432021-03-22 01:58:33 +00002773 return !io_req_prep_async(req);
Pavel Begunkov538941e2021-10-14 16:10:15 +01002774 iov_iter_restore(&rw->s.iter, &rw->s.iter_state);
Pavel Begunkovab454432021-03-22 01:58:33 +00002775 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002776}
Jens Axboeb63534c2020-06-04 11:28:00 -06002777
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002778static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002779{
Jens Axboe355afae2020-09-02 09:30:31 -06002780 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002781 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002782
Jens Axboe355afae2020-09-02 09:30:31 -06002783 if (!S_ISBLK(mode) && !S_ISREG(mode))
2784 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002785 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2786 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002787 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002788 /*
2789 * If ref is dying, we might be running poll reap from the exit work.
2790 * Don't attempt to reissue from that path, just let it fail with
2791 * -EAGAIN.
2792 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002793 if (percpu_ref_is_dying(&ctx->refs))
2794 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002795 /*
2796 * Play it safe and assume not safe to re-import and reissue if we're
2797 * not in the original thread group (or in task context).
2798 */
2799 if (!same_thread_group(req->task, current) || !in_task())
2800 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002801 return true;
2802}
Jens Axboee82ad482021-04-02 19:45:34 -06002803#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002804static bool io_resubmit_prep(struct io_kiocb *req)
2805{
2806 return false;
2807}
Jens Axboee82ad482021-04-02 19:45:34 -06002808static bool io_rw_should_reissue(struct io_kiocb *req)
2809{
2810 return false;
2811}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002812#endif
2813
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002814static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002815{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002816 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2817 kiocb_end_write(req);
Pavel Begunkov258f3a72021-10-14 16:10:14 +01002818 if (unlikely(res != req->result)) {
Pavel Begunkov9532b992021-03-22 01:58:34 +00002819 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2820 io_rw_should_reissue(req)) {
2821 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002822 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002823 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002824 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002825 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002826 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002827 return false;
2828}
2829
Pavel Begunkovcc8e9ba2021-12-15 22:08:50 +00002830static inline void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002831{
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00002832 unsigned int cflags = io_put_kbuf(req);
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01002833 int res = req->result;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002834
2835 if (*locked) {
Pavel Begunkov126180b2021-08-18 12:42:47 +01002836 io_req_complete_state(req, res, cflags);
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002837 io_req_add_compl_list(req);
Pavel Begunkov126180b2021-08-18 12:42:47 +01002838 } else {
2839 io_req_complete_post(req, res, cflags);
2840 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002841}
2842
GuoYong Zheng00f6e682022-01-05 18:12:02 +08002843static void __io_complete_rw(struct io_kiocb *req, long res,
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002844 unsigned int issue_flags)
2845{
2846 if (__io_complete_rw_common(req, res))
2847 return;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00002848 __io_req_complete(req, issue_flags, req->result, io_put_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002849}
2850
Jens Axboe6b19b762021-10-21 09:22:35 -06002851static void io_complete_rw(struct kiocb *kiocb, long res)
Jens Axboeba816ad2019-09-28 11:36:45 -06002852{
Jens Axboe9adbd452019-12-20 08:45:55 -07002853 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002854
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002855 if (__io_complete_rw_common(req, res))
2856 return;
2857 req->result = res;
2858 req->io_task_work.func = io_req_task_complete;
Hao Xuf28c240e2021-12-08 13:21:25 +08002859 io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL));
Jens Axboe2b188cc2019-01-07 10:46:33 -07002860}
2861
Jens Axboe6b19b762021-10-21 09:22:35 -06002862static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
Jens Axboedef596e2019-01-09 08:59:42 -07002863{
Jens Axboe9adbd452019-12-20 08:45:55 -07002864 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002865
Jens Axboe491381ce2019-10-17 09:20:46 -06002866 if (kiocb->ki_flags & IOCB_WRITE)
2867 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002868 if (unlikely(res != req->result)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002869 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2870 req->flags |= REQ_F_REISSUE;
2871 return;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002872 }
Pavel Begunkov258f3a72021-10-14 16:10:14 +01002873 req->result = res;
Pavel Begunkov8c130822021-03-22 01:58:32 +00002874 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002875
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002876 /* order with io_iopoll_complete() checking ->iopoll_completed */
2877 smp_store_release(&req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002878}
2879
2880/*
2881 * After the iocb has been issued, it's safe to be found on the poll list.
2882 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002883 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002884 * accessing the kiocb cookie.
2885 */
Pavel Begunkov98821312021-10-15 17:09:12 +01002886static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboedef596e2019-01-09 08:59:42 -07002887{
2888 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08002889 const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002890
2891 /* workqueue context doesn't hold uring_lock, grab it now */
Hao Xu3b44b372021-10-18 21:34:31 +08002892 if (unlikely(needs_lock))
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002893 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002894
2895 /*
2896 * Track whether we have multiple files in our lists. This will impact
2897 * how we do polling eventually, not spinning if we're on potentially
2898 * different devices.
2899 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002900 if (wq_list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002901 ctx->poll_multi_queue = false;
2902 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002903 struct io_kiocb *list_req;
2904
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002905 list_req = container_of(ctx->iopoll_list.first, struct io_kiocb,
2906 comp_list);
Christoph Hellwig30da1b42021-10-12 13:12:14 +02002907 if (list_req->file != req->file)
Hao Xu915b3dd2021-06-28 05:37:30 +08002908 ctx->poll_multi_queue = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002909 }
2910
2911 /*
2912 * For fast devices, IO may have already completed. If it has, add
2913 * it to the front so we find it first.
2914 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002915 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002916 wq_list_add_head(&req->comp_list, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002917 else
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002918 wq_list_add_tail(&req->comp_list, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002919
Hao Xu3b44b372021-10-18 21:34:31 +08002920 if (unlikely(needs_lock)) {
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002921 /*
2922 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2923 * in sq thread task context or in io worker task context. If
2924 * current task context is sq thread, we don't need to check
2925 * whether should wake up sq thread.
2926 */
2927 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2928 wq_has_sleeper(&ctx->sq_data->wait))
2929 wake_up(&ctx->sq_data->wait);
2930
2931 mutex_unlock(&ctx->uring_lock);
2932 }
Jens Axboedef596e2019-01-09 08:59:42 -07002933}
2934
Jens Axboe4503b762020-06-01 10:00:27 -06002935static bool io_bdev_nowait(struct block_device *bdev)
2936{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002937 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002938}
2939
Jens Axboe2b188cc2019-01-07 10:46:33 -07002940/*
2941 * If we tracked the file through the SCM inflight mechanism, we could support
2942 * any file. For now, just ensure that anything potentially problematic is done
2943 * inline.
2944 */
Pavel Begunkov88459b52021-10-17 00:07:10 +01002945static bool __io_file_supports_nowait(struct file *file, umode_t mode)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002946{
Jens Axboe4503b762020-06-01 10:00:27 -06002947 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002948 if (IS_ENABLED(CONFIG_BLOCK) &&
2949 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002950 return true;
2951 return false;
2952 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002953 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002954 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002955 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002956 if (IS_ENABLED(CONFIG_BLOCK) &&
2957 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002958 file->f_op != &io_uring_fops)
2959 return true;
2960 return false;
2961 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002962
Jens Axboec5b85622020-06-09 19:23:05 -06002963 /* any ->read/write should understand O_NONBLOCK */
2964 if (file->f_flags & O_NONBLOCK)
2965 return true;
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002966 return file->f_mode & FMODE_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002967}
2968
Pavel Begunkov88459b52021-10-17 00:07:10 +01002969/*
2970 * If we tracked the file through the SCM inflight mechanism, we could support
2971 * any file. For now, just ensure that anything potentially problematic is done
2972 * inline.
2973 */
2974static unsigned int io_file_get_flags(struct file *file)
Jens Axboe7b29f922021-03-12 08:30:14 -07002975{
Pavel Begunkov88459b52021-10-17 00:07:10 +01002976 umode_t mode = file_inode(file)->i_mode;
2977 unsigned int res = 0;
Jens Axboe7b29f922021-03-12 08:30:14 -07002978
Pavel Begunkov88459b52021-10-17 00:07:10 +01002979 if (S_ISREG(mode))
2980 res |= FFS_ISREG;
2981 if (__io_file_supports_nowait(file, mode))
2982 res |= FFS_NOWAIT;
2983 return res;
Jens Axboe7b29f922021-03-12 08:30:14 -07002984}
2985
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002986static inline bool io_file_supports_nowait(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002987{
Pavel Begunkov88459b52021-10-17 00:07:10 +01002988 return req->flags & REQ_F_SUPPORT_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002989}
2990
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01002991static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002992{
Jens Axboedef596e2019-01-09 08:59:42 -07002993 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002994 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002995 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002996 unsigned ioprio;
2997 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002998
Pavel Begunkov88459b52021-10-17 00:07:10 +01002999 if (!io_req_ffs_set(req))
3000 req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT;
Jens Axboe491381ce2019-10-17 09:20:46 -06003001
Jens Axboe2b188cc2019-01-07 10:46:33 -07003002 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboe7b9762a2021-12-22 20:26:56 -07003003 if (kiocb->ki_pos == -1) {
3004 if (!(file->f_mode & FMODE_STREAM)) {
3005 req->flags |= REQ_F_CUR_POS;
3006 kiocb->ki_pos = file->f_pos;
3007 } else {
3008 kiocb->ki_pos = 0;
3009 }
Jens Axboeba042912019-12-25 16:33:42 -07003010 }
Pavel Begunkov5cb03d62021-10-15 17:09:16 +01003011 kiocb->ki_flags = iocb_flags(file);
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03003012 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
3013 if (unlikely(ret))
3014 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003015
Jens Axboe5d329e12021-09-14 11:08:37 -06003016 /*
3017 * If the file is marked O_NONBLOCK, still allow retry for it if it
3018 * supports async. Otherwise it's impossible to use O_NONBLOCK files
3019 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
3020 */
3021 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003022 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req)))
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003023 req->flags |= REQ_F_NOWAIT;
3024
Jens Axboedef596e2019-01-09 08:59:42 -07003025 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov5cb03d62021-10-15 17:09:16 +01003026 if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06003027 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003028
Jens Axboe394918e2021-03-08 11:40:23 -07003029 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
Jens Axboedef596e2019-01-09 08:59:42 -07003030 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08003031 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07003032 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06003033 if (kiocb->ki_flags & IOCB_HIPRI)
3034 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07003035 kiocb->ki_complete = io_complete_rw;
3036 }
Jens Axboe9adbd452019-12-20 08:45:55 -07003037
Pavel Begunkovfb272742021-10-23 12:14:02 +01003038 ioprio = READ_ONCE(sqe->ioprio);
3039 if (ioprio) {
3040 ret = ioprio_check_cap(ioprio);
3041 if (ret)
3042 return ret;
3043
3044 kiocb->ki_ioprio = ioprio;
3045 } else {
3046 kiocb->ki_ioprio = get_current_ioprio();
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003047 }
3048
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003049 req->imu = NULL;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003050 req->rw.addr = READ_ONCE(sqe->addr);
3051 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003052 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003053 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003054}
3055
3056static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
3057{
3058 switch (ret) {
3059 case -EIOCBQUEUED:
3060 break;
3061 case -ERESTARTSYS:
3062 case -ERESTARTNOINTR:
3063 case -ERESTARTNOHAND:
3064 case -ERESTART_RESTARTBLOCK:
3065 /*
3066 * We can't just restart the syscall, since previously
3067 * submitted sqes may already be in progress. Just fail this
3068 * IO with EINTR.
3069 */
3070 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003071 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003072 default:
Jens Axboe6b19b762021-10-21 09:22:35 -06003073 kiocb->ki_complete(kiocb, ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003074 }
3075}
3076
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003077static void kiocb_done(struct io_kiocb *req, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00003078 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06003079{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003080 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07003081
Jens Axboe227c0c92020-08-13 11:51:40 -06003082 /* add previously done IO, if any */
Pavel Begunkovd886e182021-10-04 20:02:56 +01003083 if (req_has_async_data(req) && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06003084 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07003085 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003086 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07003087 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003088 }
3089
Jens Axboeba042912019-12-25 16:33:42 -07003090 if (req->flags & REQ_F_CUR_POS)
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003091 req->file->f_pos = req->rw.kiocb.ki_pos;
3092 if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw))
GuoYong Zheng00f6e682022-01-05 18:12:02 +08003093 __io_complete_rw(req, ret, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06003094 else
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003095 io_rw_done(&req->rw.kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01003096
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01003097 if (req->flags & REQ_F_REISSUE) {
Pavel Begunkov97284632021-04-08 19:28:03 +01003098 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06003099 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06003100 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00003101 } else {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003102 req_set_fail(req);
Pavel Begunkov06bdea22021-11-23 00:07:46 +00003103 req->result = ret;
3104 req->io_task_work.func = io_req_task_complete;
Hao Xu4813c372021-12-07 17:39:48 +08003105 io_req_task_work_add(req, false);
Pavel Begunkov97284632021-04-08 19:28:03 +01003106 }
3107 }
Jens Axboeba816ad2019-09-28 11:36:45 -06003108}
3109
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003110static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
3111 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07003112{
Jens Axboe9adbd452019-12-20 08:45:55 -07003113 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01003114 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003115 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07003116
Pavel Begunkov75769e32021-04-01 15:43:54 +01003117 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07003118 return -EFAULT;
3119 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01003120 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07003121 return -EFAULT;
3122
3123 /*
3124 * May not be a start of buffer, set size appropriately
3125 * and advance us to the beginning.
3126 */
3127 offset = buf_addr - imu->ubuf;
3128 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003129
3130 if (offset) {
3131 /*
3132 * Don't use iov_iter_advance() here, as it's really slow for
3133 * using the latter parts of a big fixed buffer - it iterates
3134 * over each segment manually. We can cheat a bit here, because
3135 * we know that:
3136 *
3137 * 1) it's a BVEC iter, we set it up
3138 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3139 * first and last bvec
3140 *
3141 * So just find our index, and adjust the iterator afterwards.
3142 * If the offset is within the first bvec (or the whole first
3143 * bvec, just use iov_iter_advance(). This makes it easier
3144 * since we can just skip the first segment, which may not
3145 * be PAGE_SIZE aligned.
3146 */
3147 const struct bio_vec *bvec = imu->bvec;
3148
3149 if (offset <= bvec->bv_len) {
3150 iov_iter_advance(iter, offset);
3151 } else {
3152 unsigned long seg_skip;
3153
3154 /* skip first vec */
3155 offset -= bvec->bv_len;
3156 seg_skip = 1 + (offset >> PAGE_SHIFT);
3157
3158 iter->bvec = bvec + seg_skip;
3159 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003160 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003161 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003162 }
3163 }
3164
Pavel Begunkov847595d2021-02-04 13:52:06 +00003165 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003166}
3167
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003168static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3169{
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003170 struct io_mapped_ubuf *imu = req->imu;
3171 u16 index, buf_index = req->buf_index;
3172
3173 if (likely(!imu)) {
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003174 struct io_ring_ctx *ctx = req->ctx;
3175
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003176 if (unlikely(buf_index >= ctx->nr_user_bufs))
3177 return -EFAULT;
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003178 io_req_set_rsrc_node(req, ctx);
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003179 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3180 imu = READ_ONCE(ctx->user_bufs[index]);
3181 req->imu = imu;
3182 }
3183 return __io_import_fixed(req, rw, iter, imu);
3184}
3185
Jens Axboebcda7ba2020-02-23 16:42:51 -07003186static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3187{
3188 if (needs_lock)
3189 mutex_unlock(&ctx->uring_lock);
3190}
3191
3192static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3193{
3194 /*
3195 * "Normal" inline submissions always hold the uring_lock, since we
3196 * grab it from the system call. Same is true for the SQPOLL offload.
3197 * The only exception is when we've detached the request and issue it
3198 * from an async worker thread, grab the lock for that case.
3199 */
3200 if (needs_lock)
3201 mutex_lock(&ctx->uring_lock);
3202}
3203
3204static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003205 int bgid, unsigned int issue_flags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07003206{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003207 struct io_buffer *kbuf = req->kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003208 struct io_buffer *head;
Hao Xu3b44b372021-10-18 21:34:31 +08003209 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003210
3211 if (req->flags & REQ_F_BUFFER_SELECTED)
3212 return kbuf;
3213
3214 io_ring_submit_lock(req->ctx, needs_lock);
3215
3216 lockdep_assert_held(&req->ctx->uring_lock);
3217
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003218 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003219 if (head) {
3220 if (!list_empty(&head->list)) {
3221 kbuf = list_last_entry(&head->list, struct io_buffer,
3222 list);
3223 list_del(&kbuf->list);
3224 } else {
3225 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003226 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003227 }
3228 if (*len > kbuf->len)
3229 *len = kbuf->len;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003230 req->flags |= REQ_F_BUFFER_SELECTED;
3231 req->kbuf = kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003232 } else {
3233 kbuf = ERR_PTR(-ENOBUFS);
3234 }
3235
3236 io_ring_submit_unlock(req->ctx, needs_lock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003237 return kbuf;
3238}
3239
Jens Axboe4d954c22020-02-27 07:31:19 -07003240static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003241 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003242{
3243 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003244 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003245
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003246 bgid = req->buf_index;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003247 kbuf = io_buffer_select(req, len, bgid, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003248 if (IS_ERR(kbuf))
3249 return kbuf;
Jens Axboe4d954c22020-02-27 07:31:19 -07003250 return u64_to_user_ptr(kbuf->addr);
3251}
3252
3253#ifdef CONFIG_COMPAT
3254static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003255 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003256{
3257 struct compat_iovec __user *uiov;
3258 compat_ssize_t clen;
3259 void __user *buf;
3260 ssize_t len;
3261
3262 uiov = u64_to_user_ptr(req->rw.addr);
3263 if (!access_ok(uiov, sizeof(*uiov)))
3264 return -EFAULT;
3265 if (__get_user(clen, &uiov->iov_len))
3266 return -EFAULT;
3267 if (clen < 0)
3268 return -EINVAL;
3269
3270 len = clen;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003271 buf = io_rw_buffer_select(req, &len, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003272 if (IS_ERR(buf))
3273 return PTR_ERR(buf);
3274 iov[0].iov_base = buf;
3275 iov[0].iov_len = (compat_size_t) len;
3276 return 0;
3277}
3278#endif
3279
3280static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003281 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003282{
3283 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3284 void __user *buf;
3285 ssize_t len;
3286
3287 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3288 return -EFAULT;
3289
3290 len = iov[0].iov_len;
3291 if (len < 0)
3292 return -EINVAL;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003293 buf = io_rw_buffer_select(req, &len, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003294 if (IS_ERR(buf))
3295 return PTR_ERR(buf);
3296 iov[0].iov_base = buf;
3297 iov[0].iov_len = len;
3298 return 0;
3299}
3300
3301static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003302 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003303{
Jens Axboedddb3e22020-06-04 11:27:01 -06003304 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003305 struct io_buffer *kbuf = req->kbuf;
Jens Axboedddb3e22020-06-04 11:27:01 -06003306
Jens Axboedddb3e22020-06-04 11:27:01 -06003307 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3308 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003309 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003310 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003311 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003312 return -EINVAL;
3313
3314#ifdef CONFIG_COMPAT
3315 if (req->ctx->compat)
Pavel Begunkov51aac422021-10-14 16:10:17 +01003316 return io_compat_import(req, iov, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003317#endif
3318
Pavel Begunkov51aac422021-10-14 16:10:17 +01003319 return __io_iov_buffer_select(req, iov, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003320}
3321
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003322static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req,
3323 struct io_rw_state *s,
3324 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003325{
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003326 struct iov_iter *iter = &s->iter;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003327 u8 opcode = req->opcode;
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003328 struct iovec *iovec;
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003329 void __user *buf;
3330 size_t sqe_len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003331 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003332
Pavel Begunkovf3251182021-11-23 00:07:48 +00003333 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
3334 ret = io_import_fixed(req, rw, iter);
3335 if (ret)
3336 return ERR_PTR(ret);
3337 return NULL;
3338 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003339
Jens Axboebcda7ba2020-02-23 16:42:51 -07003340 /* buffer index only valid with fixed read/write, or buffer select */
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003341 if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)))
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003342 return ERR_PTR(-EINVAL);
Jens Axboe9adbd452019-12-20 08:45:55 -07003343
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003344 buf = u64_to_user_ptr(req->rw.addr);
3345 sqe_len = req->rw.len;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003346
Jens Axboe3a6820f2019-12-22 15:19:35 -07003347 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003348 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01003349 buf = io_rw_buffer_select(req, &sqe_len, issue_flags);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003350 if (IS_ERR(buf))
Changcheng Deng898df242021-10-20 08:49:48 +00003351 return ERR_CAST(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003352 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003353 }
3354
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003355 ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter);
Pavel Begunkovf3251182021-11-23 00:07:48 +00003356 if (ret)
3357 return ERR_PTR(ret);
3358 return NULL;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003359 }
3360
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003361 iovec = s->fast_iov;
Jens Axboe4d954c22020-02-27 07:31:19 -07003362 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003363 ret = io_iov_buffer_select(req, iovec, issue_flags);
Pavel Begunkovf3251182021-11-23 00:07:48 +00003364 if (ret)
3365 return ERR_PTR(ret);
3366 iov_iter_init(iter, rw, iovec, 1, iovec->iov_len);
3367 return NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07003368 }
3369
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003370 ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003371 req->ctx->compat);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003372 if (unlikely(ret < 0))
3373 return ERR_PTR(ret);
3374 return iovec;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003375}
3376
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003377static inline int io_import_iovec(int rw, struct io_kiocb *req,
3378 struct iovec **iovec, struct io_rw_state *s,
3379 unsigned int issue_flags)
3380{
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003381 *iovec = __io_import_iovec(rw, req, s, issue_flags);
3382 if (unlikely(IS_ERR(*iovec)))
3383 return PTR_ERR(*iovec);
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003384
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003385 iov_iter_save_state(&s->iter, &s->iter_state);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003386 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003387}
3388
Jens Axboe0fef9482020-08-26 10:36:20 -06003389static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3390{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003391 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003392}
3393
Jens Axboe32960612019-09-23 11:05:34 -06003394/*
3395 * For files that don't have ->read_iter() and ->write_iter(), handle them
3396 * by looping over ->read() or ->write() manually.
3397 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003398static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003399{
Jens Axboe4017eb92020-10-22 14:14:12 -06003400 struct kiocb *kiocb = &req->rw.kiocb;
3401 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003402 ssize_t ret = 0;
3403
3404 /*
3405 * Don't support polled IO through this interface, and we can't
3406 * support non-blocking either. For the latter, this just causes
3407 * the kiocb to be handled from an async context.
3408 */
3409 if (kiocb->ki_flags & IOCB_HIPRI)
3410 return -EOPNOTSUPP;
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003411 if ((kiocb->ki_flags & IOCB_NOWAIT) &&
3412 !(kiocb->ki_filp->f_flags & O_NONBLOCK))
Jens Axboe32960612019-09-23 11:05:34 -06003413 return -EAGAIN;
3414
3415 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003416 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003417 ssize_t nr;
3418
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003419 if (!iov_iter_is_bvec(iter)) {
3420 iovec = iov_iter_iovec(iter);
3421 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003422 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3423 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003424 }
3425
Jens Axboe32960612019-09-23 11:05:34 -06003426 if (rw == READ) {
3427 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003428 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003429 } else {
3430 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003431 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003432 }
3433
3434 if (nr < 0) {
3435 if (!ret)
3436 ret = nr;
3437 break;
3438 }
Jens Axboe16c8d2d2021-09-12 06:45:07 -06003439 if (!iov_iter_is_bvec(iter)) {
3440 iov_iter_advance(iter, nr);
3441 } else {
3442 req->rw.len -= nr;
3443 req->rw.addr += nr;
3444 }
Jens Axboe32960612019-09-23 11:05:34 -06003445 ret += nr;
3446 if (nr != iovec.iov_len)
3447 break;
Jens Axboe32960612019-09-23 11:05:34 -06003448 }
3449
3450 return ret;
3451}
3452
Jens Axboeff6165b2020-08-13 09:47:43 -06003453static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3454 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003455{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003456 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003457
Pavel Begunkov538941e2021-10-14 16:10:15 +01003458 memcpy(&rw->s.iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003459 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003460 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003461 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003462 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003463 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003464 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003465 unsigned iov_off = 0;
3466
Pavel Begunkov538941e2021-10-14 16:10:15 +01003467 rw->s.iter.iov = rw->s.fast_iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003468 if (iter->iov != fast_iov) {
3469 iov_off = iter->iov - fast_iov;
Pavel Begunkov538941e2021-10-14 16:10:15 +01003470 rw->s.iter.iov += iov_off;
Jens Axboeff6165b2020-08-13 09:47:43 -06003471 }
Pavel Begunkov538941e2021-10-14 16:10:15 +01003472 if (rw->s.fast_iov != fast_iov)
3473 memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003474 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003475 } else {
3476 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003477 }
3478}
3479
Hao Xu8d4af682021-09-22 18:15:22 +08003480static inline bool io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003481{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003482 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3483 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
Pavel Begunkovd886e182021-10-04 20:02:56 +01003484 if (req->async_data) {
3485 req->flags |= REQ_F_ASYNC_DATA;
3486 return false;
3487 }
3488 return true;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003489}
3490
Jens Axboeff6165b2020-08-13 09:47:43 -06003491static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003492 struct io_rw_state *s, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003493{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003494 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003495 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003496 if (!req_has_async_data(req)) {
Jens Axboecd658692021-09-10 11:19:14 -06003497 struct io_async_rw *iorw;
3498
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003499 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003500 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003501 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003502 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003503
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003504 io_req_map_rw(req, iovec, s->fast_iov, &s->iter);
Jens Axboecd658692021-09-10 11:19:14 -06003505 iorw = req->async_data;
3506 /* we've copied and mapped the iter, ensure state is saved */
Pavel Begunkov538941e2021-10-14 16:10:15 +01003507 iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003508 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003509 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003510}
3511
Pavel Begunkov73debe62020-09-30 22:57:54 +03003512static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003513{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003514 struct io_async_rw *iorw = req->async_data;
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003515 struct iovec *iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003516 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003517
Pavel Begunkov51aac422021-10-14 16:10:17 +01003518 /* submission path, ->uring_lock should already be taken */
Hao Xu3b44b372021-10-18 21:34:31 +08003519 ret = io_import_iovec(rw, req, &iov, &iorw->s, 0);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003520 if (unlikely(ret < 0))
3521 return ret;
3522
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003523 iorw->bytes_done = 0;
3524 iorw->free_iovec = iov;
3525 if (iov)
3526 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003527 return 0;
3528}
3529
Pavel Begunkov73debe62020-09-30 22:57:54 +03003530static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003531{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003532 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3533 return -EBADF;
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01003534 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003535}
3536
Jens Axboec1dd91d2020-08-03 16:43:59 -06003537/*
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05003538 * This is our waitqueue callback handler, registered through __folio_lock_async()
Jens Axboec1dd91d2020-08-03 16:43:59 -06003539 * when we initially tried to do the IO with the iocb armed our waitqueue.
3540 * This gets called when the page is unlocked, and we generally expect that to
3541 * happen when the page IO is completed and the page is now uptodate. This will
3542 * queue a task_work based retry of the operation, attempting to copy the data
3543 * again. If the latter fails because the page was NOT uptodate, then we will
3544 * do a thread based blocking retry of the operation. That's the unexpected
3545 * slow path.
3546 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003547static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3548 int sync, void *arg)
3549{
3550 struct wait_page_queue *wpq;
3551 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003552 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003553
3554 wpq = container_of(wait, struct wait_page_queue, wait);
3555
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003556 if (!wake_page_match(wpq, key))
3557 return 0;
3558
Hao Xuc8d317a2020-09-29 20:00:45 +08003559 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003560 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003561 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003562 return 1;
3563}
3564
Jens Axboec1dd91d2020-08-03 16:43:59 -06003565/*
3566 * This controls whether a given IO request should be armed for async page
3567 * based retry. If we return false here, the request is handed to the async
3568 * worker threads for retry. If we're doing buffered reads on a regular file,
3569 * we prepare a private wait_page_queue entry and retry the operation. This
3570 * will either succeed because the page is now uptodate and unlocked, or it
3571 * will register a callback when the page is unlocked at IO completion. Through
3572 * that callback, io_uring uses task_work to setup a retry of the operation.
3573 * That retry will attempt the buffered read again. The retry will generally
3574 * succeed, or in rare cases where it fails, we then fall back to using the
3575 * async worker threads for a blocking retry.
3576 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003577static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003578{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003579 struct io_async_rw *rw = req->async_data;
3580 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003581 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003582
3583 /* never retry for NOWAIT, we just complete with -EAGAIN */
3584 if (req->flags & REQ_F_NOWAIT)
3585 return false;
3586
Jens Axboe227c0c92020-08-13 11:51:40 -06003587 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003588 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003589 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003590
Jens Axboebcf5a062020-05-22 09:24:42 -06003591 /*
3592 * just use poll if we can, and don't attempt if the fs doesn't
3593 * support callback based unlocks
3594 */
3595 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3596 return false;
3597
Jens Axboe3b2a4432020-08-16 10:58:43 -07003598 wait->wait.func = io_async_buf_func;
3599 wait->wait.private = req;
3600 wait->wait.flags = 0;
3601 INIT_LIST_HEAD(&wait->wait.entry);
3602 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003603 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003604 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003605 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003606}
3607
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003608static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003609{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003610 if (likely(req->file->f_op->read_iter))
Jens Axboebcf5a062020-05-22 09:24:42 -06003611 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003612 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003613 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003614 else
3615 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003616}
3617
Ming Lei7db30432021-08-21 23:07:51 +08003618static bool need_read_all(struct io_kiocb *req)
3619{
3620 return req->flags & REQ_F_ISREG ||
3621 S_ISBLK(file_inode(req->file)->i_mode);
3622}
3623
Pavel Begunkov889fca72021-02-10 00:03:09 +00003624static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003625{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003626 struct io_rw_state __s, *s = &__s;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003627 struct iovec *iovec;
Jens Axboe9adbd452019-12-20 08:45:55 -07003628 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003629 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003630 struct io_async_rw *rw;
Jens Axboecd658692021-09-10 11:19:14 -06003631 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003632
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003633 if (!req_has_async_data(req)) {
3634 ret = io_import_iovec(READ, req, &iovec, s, issue_flags);
3635 if (unlikely(ret < 0))
3636 return ret;
3637 } else {
Pavel Begunkovd886e182021-10-04 20:02:56 +01003638 rw = req->async_data;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003639 s = &rw->s;
Jens Axboecd658692021-09-10 11:19:14 -06003640 /*
3641 * We come here from an earlier attempt, restore our state to
3642 * match in case it doesn't. It's cheap enough that we don't
3643 * need to make this conditional.
3644 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003645 iov_iter_restore(&s->iter, &s->iter_state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003646 iovec = NULL;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003647 }
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003648 req->result = iov_iter_count(&s->iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003649
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003650 if (force_nonblock) {
3651 /* If the file doesn't support async, just async punt */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003652 if (unlikely(!io_file_supports_nowait(req))) {
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003653 ret = io_setup_async_rw(req, iovec, s, true);
3654 return ret ?: -EAGAIN;
3655 }
Pavel Begunkova88fc402020-09-30 22:57:53 +03003656 kiocb->ki_flags |= IOCB_NOWAIT;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003657 } else {
3658 /* Ensure we clear previously set non-block flag */
3659 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003660 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003661
Jens Axboecd658692021-09-10 11:19:14 -06003662 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003663 if (unlikely(ret)) {
3664 kfree(iovec);
3665 return ret;
3666 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003667
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003668 ret = io_iter_do_read(req, &s->iter);
Jens Axboe32960612019-09-23 11:05:34 -06003669
Jens Axboe230d50d2021-04-01 20:41:15 -06003670 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003671 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003672 /* IOPOLL retry should happen for io-wq threads */
3673 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003674 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003675 /* no retry on NONBLOCK nor RWF_NOWAIT */
3676 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003677 goto done;
Jens Axboef38c7e32020-09-25 15:23:43 -06003678 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003679 } else if (ret == -EIOCBQUEUED) {
3680 goto out_free;
Pavel Begunkovf80a50a2021-10-14 16:10:13 +01003681 } else if (ret == req->result || ret <= 0 || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003682 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003683 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003684 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003685 }
3686
Jens Axboecd658692021-09-10 11:19:14 -06003687 /*
3688 * Don't depend on the iter state matching what was consumed, or being
3689 * untouched in case of error. Restore it and we'll advance it
3690 * manually if we need to.
3691 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003692 iov_iter_restore(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003693
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003694 ret2 = io_setup_async_rw(req, iovec, s, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003695 if (ret2)
3696 return ret2;
3697
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003698 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003699 rw = req->async_data;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003700 s = &rw->s;
Jens Axboecd658692021-09-10 11:19:14 -06003701 /*
3702 * Now use our persistent iterator and state, if we aren't already.
3703 * We've restored and mapped the iter to match.
3704 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003705
Pavel Begunkovb23df912021-02-04 13:52:04 +00003706 do {
Jens Axboecd658692021-09-10 11:19:14 -06003707 /*
3708 * We end up here because of a partial read, either from
3709 * above or inside this loop. Advance the iter by the bytes
3710 * that were consumed.
3711 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003712 iov_iter_advance(&s->iter, ret);
3713 if (!iov_iter_count(&s->iter))
Jens Axboecd658692021-09-10 11:19:14 -06003714 break;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003715 rw->bytes_done += ret;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003716 iov_iter_save_state(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003717
Pavel Begunkovb23df912021-02-04 13:52:04 +00003718 /* if we can retry, do so with the callbacks armed */
3719 if (!io_rw_should_retry(req)) {
3720 kiocb->ki_flags &= ~IOCB_WAITQ;
3721 return -EAGAIN;
3722 }
3723
3724 /*
3725 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3726 * we get -EIOCBQUEUED, then we'll get a notification when the
3727 * desired page gets unlocked. We can also get a partial read
3728 * here, and if we do, then just retry at the new offset.
3729 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003730 ret = io_iter_do_read(req, &s->iter);
Pavel Begunkovb23df912021-02-04 13:52:04 +00003731 if (ret == -EIOCBQUEUED)
3732 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003733 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003734 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003735 iov_iter_restore(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003736 } while (ret > 0);
Jens Axboe227c0c92020-08-13 11:51:40 -06003737done:
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003738 kiocb_done(req, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003739out_free:
3740 /* it's faster to check here then delegate to kfree */
3741 if (iovec)
3742 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003743 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003744}
3745
Pavel Begunkov73debe62020-09-30 22:57:54 +03003746static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003747{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003748 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3749 return -EBADF;
Jens Axboe3884b832021-10-25 13:45:12 -06003750 req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file));
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01003751 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003752}
3753
Pavel Begunkov889fca72021-02-10 00:03:09 +00003754static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003755{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003756 struct io_rw_state __s, *s = &__s;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003757 struct iovec *iovec;
Jens Axboe9adbd452019-12-20 08:45:55 -07003758 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003759 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003760 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003761
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003762 if (!req_has_async_data(req)) {
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003763 ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags);
3764 if (unlikely(ret < 0))
Pavel Begunkov2846c482020-11-07 13:16:27 +00003765 return ret;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003766 } else {
3767 struct io_async_rw *rw = req->async_data;
3768
3769 s = &rw->s;
3770 iov_iter_restore(&s->iter, &s->iter_state);
3771 iovec = NULL;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003772 }
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003773 req->result = iov_iter_count(&s->iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003774
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003775 if (force_nonblock) {
3776 /* If the file doesn't support async, just async punt */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003777 if (unlikely(!io_file_supports_nowait(req)))
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003778 goto copy_iov;
3779
3780 /* file path doesn't support NOWAIT for non-direct_IO */
3781 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3782 (req->flags & REQ_F_ISREG))
3783 goto copy_iov;
3784
Pavel Begunkova88fc402020-09-30 22:57:53 +03003785 kiocb->ki_flags |= IOCB_NOWAIT;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003786 } else {
3787 /* Ensure we clear previously set non-block flag */
3788 kiocb->ki_flags &= ~IOCB_NOWAIT;
3789 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003790
Jens Axboecd658692021-09-10 11:19:14 -06003791 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003792 if (unlikely(ret))
3793 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003794
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003795 /*
3796 * Open-code file_start_write here to grab freeze protection,
3797 * which will be released by another thread in
3798 * io_complete_rw(). Fool lockdep by telling it the lock got
3799 * released so that it doesn't complain about the held lock when
3800 * we return to userspace.
3801 */
3802 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003803 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003804 __sb_writers_release(file_inode(req->file)->i_sb,
3805 SB_FREEZE_WRITE);
3806 }
3807 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003808
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003809 if (likely(req->file->f_op->write_iter))
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003810 ret2 = call_write_iter(req->file, kiocb, &s->iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003811 else if (req->file->f_op->write)
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003812 ret2 = loop_rw_iter(WRITE, req, &s->iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003813 else
3814 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003815
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003816 if (req->flags & REQ_F_REISSUE) {
3817 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003818 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003819 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003820
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003821 /*
3822 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3823 * retry them without IOCB_NOWAIT.
3824 */
3825 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3826 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003827 /* no retry on NONBLOCK nor RWF_NOWAIT */
3828 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003829 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003830 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003831 /* IOPOLL retry should happen for io-wq threads */
Noah Goldsteinb10841c2021-10-16 20:32:29 -05003832 if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboeeefdf302020-08-27 16:40:19 -06003833 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003834done:
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003835 kiocb_done(req, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003836 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003837copy_iov:
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003838 iov_iter_restore(&s->iter, &s->iter_state);
3839 ret = io_setup_async_rw(req, iovec, s, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003840 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003841 }
Jens Axboe31b51512019-01-18 22:56:34 -07003842out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003843 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003844 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003845 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003846 return ret;
3847}
3848
Jens Axboe80a261f2020-09-28 14:23:58 -06003849static int io_renameat_prep(struct io_kiocb *req,
3850 const struct io_uring_sqe *sqe)
3851{
3852 struct io_rename *ren = &req->rename;
3853 const char __user *oldf, *newf;
3854
Jens Axboeed7eb252021-06-23 09:04:13 -06003855 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3856 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003857 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003858 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003859 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3860 return -EBADF;
3861
3862 ren->old_dfd = READ_ONCE(sqe->fd);
3863 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3864 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3865 ren->new_dfd = READ_ONCE(sqe->len);
3866 ren->flags = READ_ONCE(sqe->rename_flags);
3867
3868 ren->oldpath = getname(oldf);
3869 if (IS_ERR(ren->oldpath))
3870 return PTR_ERR(ren->oldpath);
3871
3872 ren->newpath = getname(newf);
3873 if (IS_ERR(ren->newpath)) {
3874 putname(ren->oldpath);
3875 return PTR_ERR(ren->newpath);
3876 }
3877
3878 req->flags |= REQ_F_NEED_CLEANUP;
3879 return 0;
3880}
3881
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003882static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003883{
3884 struct io_rename *ren = &req->rename;
3885 int ret;
3886
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003887 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003888 return -EAGAIN;
3889
3890 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3891 ren->newpath, ren->flags);
3892
3893 req->flags &= ~REQ_F_NEED_CLEANUP;
3894 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003895 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003896 io_req_complete(req, ret);
3897 return 0;
3898}
3899
Jens Axboe14a11432020-09-28 14:27:37 -06003900static int io_unlinkat_prep(struct io_kiocb *req,
3901 const struct io_uring_sqe *sqe)
3902{
3903 struct io_unlink *un = &req->unlink;
3904 const char __user *fname;
3905
Jens Axboe22634bc2021-06-23 09:07:45 -06003906 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3907 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003908 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3909 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003910 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003911 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3912 return -EBADF;
3913
3914 un->dfd = READ_ONCE(sqe->fd);
3915
3916 un->flags = READ_ONCE(sqe->unlink_flags);
3917 if (un->flags & ~AT_REMOVEDIR)
3918 return -EINVAL;
3919
3920 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3921 un->filename = getname(fname);
3922 if (IS_ERR(un->filename))
3923 return PTR_ERR(un->filename);
3924
3925 req->flags |= REQ_F_NEED_CLEANUP;
3926 return 0;
3927}
3928
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003929static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003930{
3931 struct io_unlink *un = &req->unlink;
3932 int ret;
3933
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003934 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003935 return -EAGAIN;
3936
3937 if (un->flags & AT_REMOVEDIR)
3938 ret = do_rmdir(un->dfd, un->filename);
3939 else
3940 ret = do_unlinkat(un->dfd, un->filename);
3941
3942 req->flags &= ~REQ_F_NEED_CLEANUP;
3943 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003944 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003945 io_req_complete(req, ret);
3946 return 0;
3947}
3948
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003949static int io_mkdirat_prep(struct io_kiocb *req,
3950 const struct io_uring_sqe *sqe)
3951{
3952 struct io_mkdir *mkd = &req->mkdir;
3953 const char __user *fname;
3954
3955 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3956 return -EINVAL;
3957 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3958 sqe->splice_fd_in)
3959 return -EINVAL;
3960 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3961 return -EBADF;
3962
3963 mkd->dfd = READ_ONCE(sqe->fd);
3964 mkd->mode = READ_ONCE(sqe->len);
3965
3966 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3967 mkd->filename = getname(fname);
3968 if (IS_ERR(mkd->filename))
3969 return PTR_ERR(mkd->filename);
3970
3971 req->flags |= REQ_F_NEED_CLEANUP;
3972 return 0;
3973}
3974
Pavel Begunkov04f34082021-10-14 16:10:12 +01003975static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003976{
3977 struct io_mkdir *mkd = &req->mkdir;
3978 int ret;
3979
3980 if (issue_flags & IO_URING_F_NONBLOCK)
3981 return -EAGAIN;
3982
3983 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3984
3985 req->flags &= ~REQ_F_NEED_CLEANUP;
3986 if (ret < 0)
3987 req_set_fail(req);
3988 io_req_complete(req, ret);
3989 return 0;
3990}
3991
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003992static int io_symlinkat_prep(struct io_kiocb *req,
3993 const struct io_uring_sqe *sqe)
3994{
3995 struct io_symlink *sl = &req->symlink;
3996 const char __user *oldpath, *newpath;
3997
3998 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3999 return -EINVAL;
4000 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
4001 sqe->splice_fd_in)
4002 return -EINVAL;
4003 if (unlikely(req->flags & REQ_F_FIXED_FILE))
4004 return -EBADF;
4005
4006 sl->new_dfd = READ_ONCE(sqe->fd);
4007 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
4008 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4009
4010 sl->oldpath = getname(oldpath);
4011 if (IS_ERR(sl->oldpath))
4012 return PTR_ERR(sl->oldpath);
4013
4014 sl->newpath = getname(newpath);
4015 if (IS_ERR(sl->newpath)) {
4016 putname(sl->oldpath);
4017 return PTR_ERR(sl->newpath);
4018 }
4019
4020 req->flags |= REQ_F_NEED_CLEANUP;
4021 return 0;
4022}
4023
Pavel Begunkov04f34082021-10-14 16:10:12 +01004024static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07004025{
4026 struct io_symlink *sl = &req->symlink;
4027 int ret;
4028
4029 if (issue_flags & IO_URING_F_NONBLOCK)
4030 return -EAGAIN;
4031
4032 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
4033
4034 req->flags &= ~REQ_F_NEED_CLEANUP;
4035 if (ret < 0)
4036 req_set_fail(req);
4037 io_req_complete(req, ret);
4038 return 0;
4039}
4040
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07004041static int io_linkat_prep(struct io_kiocb *req,
4042 const struct io_uring_sqe *sqe)
4043{
4044 struct io_hardlink *lnk = &req->hardlink;
4045 const char __user *oldf, *newf;
4046
4047 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4048 return -EINVAL;
4049 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
4050 return -EINVAL;
4051 if (unlikely(req->flags & REQ_F_FIXED_FILE))
4052 return -EBADF;
4053
4054 lnk->old_dfd = READ_ONCE(sqe->fd);
4055 lnk->new_dfd = READ_ONCE(sqe->len);
4056 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
4057 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4058 lnk->flags = READ_ONCE(sqe->hardlink_flags);
4059
4060 lnk->oldpath = getname(oldf);
4061 if (IS_ERR(lnk->oldpath))
4062 return PTR_ERR(lnk->oldpath);
4063
4064 lnk->newpath = getname(newf);
4065 if (IS_ERR(lnk->newpath)) {
4066 putname(lnk->oldpath);
4067 return PTR_ERR(lnk->newpath);
4068 }
4069
4070 req->flags |= REQ_F_NEED_CLEANUP;
4071 return 0;
4072}
4073
Pavel Begunkov04f34082021-10-14 16:10:12 +01004074static int io_linkat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07004075{
4076 struct io_hardlink *lnk = &req->hardlink;
4077 int ret;
4078
4079 if (issue_flags & IO_URING_F_NONBLOCK)
4080 return -EAGAIN;
4081
4082 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
4083 lnk->newpath, lnk->flags);
4084
4085 req->flags &= ~REQ_F_NEED_CLEANUP;
4086 if (ret < 0)
4087 req_set_fail(req);
4088 io_req_complete(req, ret);
4089 return 0;
4090}
4091
Jens Axboe36f4fa62020-09-05 11:14:22 -06004092static int io_shutdown_prep(struct io_kiocb *req,
4093 const struct io_uring_sqe *sqe)
4094{
4095#if defined(CONFIG_NET)
4096 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4097 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004098 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
4099 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06004100 return -EINVAL;
4101
4102 req->shutdown.how = READ_ONCE(sqe->len);
4103 return 0;
4104#else
4105 return -EOPNOTSUPP;
4106#endif
4107}
4108
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004109static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06004110{
4111#if defined(CONFIG_NET)
4112 struct socket *sock;
4113 int ret;
4114
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004115 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06004116 return -EAGAIN;
4117
Linus Torvalds48aba792020-12-16 12:44:05 -08004118 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06004119 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08004120 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06004121
4122 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07004123 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004124 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06004125 io_req_complete(req, ret);
4126 return 0;
4127#else
4128 return -EOPNOTSUPP;
4129#endif
4130}
4131
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004132static int __io_splice_prep(struct io_kiocb *req,
4133 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004134{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004135 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004136 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004137
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004138 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4139 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004140
4141 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004142 sp->len = READ_ONCE(sqe->len);
4143 sp->flags = READ_ONCE(sqe->splice_flags);
4144
4145 if (unlikely(sp->flags & ~valid_flags))
4146 return -EINVAL;
4147
Pavel Begunkov62906e82021-08-10 14:52:47 +01004148 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01004149 (sp->flags & SPLICE_F_FD_IN_FIXED));
4150 if (!sp->file_in)
4151 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004152 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004153 return 0;
4154}
4155
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004156static int io_tee_prep(struct io_kiocb *req,
4157 const struct io_uring_sqe *sqe)
4158{
4159 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
4160 return -EINVAL;
4161 return __io_splice_prep(req, sqe);
4162}
4163
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004164static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004165{
4166 struct io_splice *sp = &req->splice;
4167 struct file *in = sp->file_in;
4168 struct file *out = sp->file_out;
4169 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4170 long ret = 0;
4171
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004172 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004173 return -EAGAIN;
4174 if (sp->len)
4175 ret = do_tee(in, out, sp->len, flags);
4176
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004177 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4178 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004179 req->flags &= ~REQ_F_NEED_CLEANUP;
4180
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004181 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004182 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004183 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004184 return 0;
4185}
4186
4187static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4188{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004189 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004190
4191 sp->off_in = READ_ONCE(sqe->splice_off_in);
4192 sp->off_out = READ_ONCE(sqe->off);
4193 return __io_splice_prep(req, sqe);
4194}
4195
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004196static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004197{
4198 struct io_splice *sp = &req->splice;
4199 struct file *in = sp->file_in;
4200 struct file *out = sp->file_out;
4201 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4202 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004203 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004204
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004205 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004206 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004207
4208 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4209 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004210
Jens Axboe948a7742020-05-17 14:21:38 -06004211 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004212 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004213
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004214 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4215 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004216 req->flags &= ~REQ_F_NEED_CLEANUP;
4217
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004218 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004219 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004220 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004221 return 0;
4222}
4223
Jens Axboe2b188cc2019-01-07 10:46:33 -07004224/*
4225 * IORING_OP_NOP just posts a completion event, nothing else.
4226 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004227static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004228{
4229 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004230
Jens Axboedef596e2019-01-09 08:59:42 -07004231 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4232 return -EINVAL;
4233
Pavel Begunkov889fca72021-02-10 00:03:09 +00004234 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004235 return 0;
4236}
4237
Pavel Begunkov1155c762021-02-18 18:29:38 +00004238static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004239{
Jens Axboe6b063142019-01-10 22:13:58 -07004240 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004241
Jens Axboe09bb8392019-03-13 12:39:28 -06004242 if (!req->file)
4243 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004244
Jens Axboe6b063142019-01-10 22:13:58 -07004245 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004246 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004247 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4248 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004249 return -EINVAL;
4250
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004251 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4252 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4253 return -EINVAL;
4254
4255 req->sync.off = READ_ONCE(sqe->off);
4256 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004257 return 0;
4258}
4259
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004260static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004261{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004262 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004263 int ret;
4264
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004265 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004266 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004267 return -EAGAIN;
4268
Jens Axboe9adbd452019-12-20 08:45:55 -07004269 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004270 end > 0 ? end : LLONG_MAX,
4271 req->sync.flags & IORING_FSYNC_DATASYNC);
4272 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004273 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004274 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004275 return 0;
4276}
4277
Jens Axboed63d1b52019-12-10 10:38:56 -07004278static int io_fallocate_prep(struct io_kiocb *req,
4279 const struct io_uring_sqe *sqe)
4280{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004281 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4282 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004283 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004284 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4285 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004286
4287 req->sync.off = READ_ONCE(sqe->off);
4288 req->sync.len = READ_ONCE(sqe->addr);
4289 req->sync.mode = READ_ONCE(sqe->len);
4290 return 0;
4291}
4292
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004293static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004294{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004295 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004296
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004297 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004298 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004299 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004300 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4301 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004302 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004303 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004304 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004305 return 0;
4306}
4307
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004308static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004309{
Jens Axboef8748882020-01-08 17:47:02 -07004310 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004311 int ret;
4312
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004313 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4314 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004315 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004316 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004317 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004318 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004319
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004320 /* open.how should be already initialised */
4321 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004322 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004323
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004324 req->open.dfd = READ_ONCE(sqe->fd);
4325 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004326 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004327 if (IS_ERR(req->open.filename)) {
4328 ret = PTR_ERR(req->open.filename);
4329 req->open.filename = NULL;
4330 return ret;
4331 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004332
4333 req->open.file_slot = READ_ONCE(sqe->file_index);
4334 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4335 return -EINVAL;
4336
Jens Axboe4022e7a2020-03-19 19:23:18 -06004337 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004338 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004339 return 0;
4340}
4341
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004342static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4343{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004344 u64 mode = READ_ONCE(sqe->len);
4345 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004346
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004347 req->open.how = build_open_how(flags, mode);
4348 return __io_openat_prep(req, sqe);
4349}
4350
Jens Axboecebdb982020-01-08 17:59:24 -07004351static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4352{
4353 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004354 size_t len;
4355 int ret;
4356
Jens Axboecebdb982020-01-08 17:59:24 -07004357 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4358 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004359 if (len < OPEN_HOW_SIZE_VER0)
4360 return -EINVAL;
4361
4362 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4363 len);
4364 if (ret)
4365 return ret;
4366
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004367 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004368}
4369
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004370static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004371{
4372 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004373 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004374 bool resolve_nonblock, nonblock_set;
4375 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004376 int ret;
4377
Jens Axboecebdb982020-01-08 17:59:24 -07004378 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004379 if (ret)
4380 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004381 nonblock_set = op.open_flag & O_NONBLOCK;
4382 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004383 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004384 /*
4385 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4386 * it'll always -EAGAIN
4387 */
4388 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4389 return -EAGAIN;
4390 op.lookup_flags |= LOOKUP_CACHED;
4391 op.open_flag |= O_NONBLOCK;
4392 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004393
Pavel Begunkovb9445592021-08-25 12:25:45 +01004394 if (!fixed) {
4395 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4396 if (ret < 0)
4397 goto err;
4398 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004399
4400 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004401 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004402 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004403 * We could hang on to this 'fd' on retrying, but seems like
4404 * marginal gain for something that is now known to be a slower
4405 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004406 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004407 if (!fixed)
4408 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004409
4410 ret = PTR_ERR(file);
4411 /* only retry if RESOLVE_CACHED wasn't already set by application */
4412 if (ret == -EAGAIN &&
4413 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4414 return -EAGAIN;
4415 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004416 }
4417
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004418 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4419 file->f_flags &= ~O_NONBLOCK;
4420 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004421
4422 if (!fixed)
4423 fd_install(ret, file);
4424 else
4425 ret = io_install_fixed_file(req, file, issue_flags,
4426 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004427err:
4428 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004429 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004430 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004431 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004432 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004433 return 0;
4434}
4435
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004436static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004437{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004438 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004439}
4440
Jens Axboe067524e2020-03-02 16:32:28 -07004441static int io_remove_buffers_prep(struct io_kiocb *req,
4442 const struct io_uring_sqe *sqe)
4443{
4444 struct io_provide_buf *p = &req->pbuf;
4445 u64 tmp;
4446
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004447 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4448 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004449 return -EINVAL;
4450
4451 tmp = READ_ONCE(sqe->fd);
4452 if (!tmp || tmp > USHRT_MAX)
4453 return -EINVAL;
4454
4455 memset(p, 0, sizeof(*p));
4456 p->nbufs = tmp;
4457 p->bgid = READ_ONCE(sqe->buf_group);
4458 return 0;
4459}
4460
4461static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4462 int bgid, unsigned nbufs)
4463{
4464 unsigned i = 0;
4465
4466 /* shouldn't happen */
4467 if (!nbufs)
4468 return 0;
4469
4470 /* the head kbuf is the list itself */
4471 while (!list_empty(&buf->list)) {
4472 struct io_buffer *nxt;
4473
4474 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4475 list_del(&nxt->list);
4476 kfree(nxt);
4477 if (++i == nbufs)
4478 return i;
Ye Bin1d0254e2021-11-22 10:47:37 +08004479 cond_resched();
Jens Axboe067524e2020-03-02 16:32:28 -07004480 }
4481 i++;
4482 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004483 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004484
4485 return i;
4486}
4487
Pavel Begunkov889fca72021-02-10 00:03:09 +00004488static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004489{
4490 struct io_provide_buf *p = &req->pbuf;
4491 struct io_ring_ctx *ctx = req->ctx;
4492 struct io_buffer *head;
4493 int ret = 0;
Hao Xu3b44b372021-10-18 21:34:31 +08004494 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboe067524e2020-03-02 16:32:28 -07004495
Hao Xu3b44b372021-10-18 21:34:31 +08004496 io_ring_submit_lock(ctx, needs_lock);
Jens Axboe067524e2020-03-02 16:32:28 -07004497
4498 lockdep_assert_held(&ctx->uring_lock);
4499
4500 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004501 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004502 if (head)
4503 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004504 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004505 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004506
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004507 /* complete before unlock, IOPOLL may need the lock */
4508 __io_req_complete(req, issue_flags, ret, 0);
Hao Xu3b44b372021-10-18 21:34:31 +08004509 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboe067524e2020-03-02 16:32:28 -07004510 return 0;
4511}
4512
Jens Axboeddf0322d2020-02-23 16:41:33 -07004513static int io_provide_buffers_prep(struct io_kiocb *req,
4514 const struct io_uring_sqe *sqe)
4515{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004516 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004517 struct io_provide_buf *p = &req->pbuf;
4518 u64 tmp;
4519
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004520 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004521 return -EINVAL;
4522
4523 tmp = READ_ONCE(sqe->fd);
4524 if (!tmp || tmp > USHRT_MAX)
4525 return -E2BIG;
4526 p->nbufs = tmp;
4527 p->addr = READ_ONCE(sqe->addr);
4528 p->len = READ_ONCE(sqe->len);
4529
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004530 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4531 &size))
4532 return -EOVERFLOW;
4533 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4534 return -EOVERFLOW;
4535
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004536 size = (unsigned long)p->len * p->nbufs;
4537 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004538 return -EFAULT;
4539
4540 p->bgid = READ_ONCE(sqe->buf_group);
4541 tmp = READ_ONCE(sqe->off);
4542 if (tmp > USHRT_MAX)
4543 return -E2BIG;
4544 p->bid = tmp;
4545 return 0;
4546}
4547
4548static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4549{
4550 struct io_buffer *buf;
4551 u64 addr = pbuf->addr;
4552 int i, bid = pbuf->bid;
4553
4554 for (i = 0; i < pbuf->nbufs; i++) {
Jens Axboe9990da92021-09-24 07:39:08 -06004555 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004556 if (!buf)
4557 break;
4558
4559 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004560 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004561 buf->bid = bid;
4562 addr += pbuf->len;
4563 bid++;
4564 if (!*head) {
4565 INIT_LIST_HEAD(&buf->list);
4566 *head = buf;
4567 } else {
4568 list_add_tail(&buf->list, &(*head)->list);
4569 }
4570 }
4571
4572 return i ? i : -ENOMEM;
4573}
4574
Pavel Begunkov889fca72021-02-10 00:03:09 +00004575static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004576{
4577 struct io_provide_buf *p = &req->pbuf;
4578 struct io_ring_ctx *ctx = req->ctx;
4579 struct io_buffer *head, *list;
4580 int ret = 0;
Hao Xu3b44b372021-10-18 21:34:31 +08004581 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004582
Hao Xu3b44b372021-10-18 21:34:31 +08004583 io_ring_submit_lock(ctx, needs_lock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004584
4585 lockdep_assert_held(&ctx->uring_lock);
4586
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004587 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004588
4589 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004590 if (ret >= 0 && !list) {
4591 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4592 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004593 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004594 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004595 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004596 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004597 /* complete before unlock, IOPOLL may need the lock */
4598 __io_req_complete(req, issue_flags, ret, 0);
Hao Xu3b44b372021-10-18 21:34:31 +08004599 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004600 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004601}
4602
Jens Axboe3e4827b2020-01-08 15:18:09 -07004603static int io_epoll_ctl_prep(struct io_kiocb *req,
4604 const struct io_uring_sqe *sqe)
4605{
4606#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004607 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004608 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004609 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004610 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004611
4612 req->epoll.epfd = READ_ONCE(sqe->fd);
4613 req->epoll.op = READ_ONCE(sqe->len);
4614 req->epoll.fd = READ_ONCE(sqe->off);
4615
4616 if (ep_op_has_event(req->epoll.op)) {
4617 struct epoll_event __user *ev;
4618
4619 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4620 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4621 return -EFAULT;
4622 }
4623
4624 return 0;
4625#else
4626 return -EOPNOTSUPP;
4627#endif
4628}
4629
Pavel Begunkov889fca72021-02-10 00:03:09 +00004630static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004631{
4632#if defined(CONFIG_EPOLL)
4633 struct io_epoll *ie = &req->epoll;
4634 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004635 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004636
4637 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4638 if (force_nonblock && ret == -EAGAIN)
4639 return -EAGAIN;
4640
4641 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004642 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004643 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004644 return 0;
4645#else
4646 return -EOPNOTSUPP;
4647#endif
4648}
4649
Jens Axboec1ca7572019-12-25 22:18:28 -07004650static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4651{
4652#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004653 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004654 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004655 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4656 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004657
4658 req->madvise.addr = READ_ONCE(sqe->addr);
4659 req->madvise.len = READ_ONCE(sqe->len);
4660 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4661 return 0;
4662#else
4663 return -EOPNOTSUPP;
4664#endif
4665}
4666
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004667static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004668{
4669#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4670 struct io_madvise *ma = &req->madvise;
4671 int ret;
4672
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004673 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004674 return -EAGAIN;
4675
Minchan Kim0726b012020-10-17 16:14:50 -07004676 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004677 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004678 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004679 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004680 return 0;
4681#else
4682 return -EOPNOTSUPP;
4683#endif
4684}
4685
Jens Axboe4840e412019-12-25 22:03:45 -07004686static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4687{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004688 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004689 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004690 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4691 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004692
4693 req->fadvise.offset = READ_ONCE(sqe->off);
4694 req->fadvise.len = READ_ONCE(sqe->len);
4695 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4696 return 0;
4697}
4698
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004699static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004700{
4701 struct io_fadvise *fa = &req->fadvise;
4702 int ret;
4703
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004704 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004705 switch (fa->advice) {
4706 case POSIX_FADV_NORMAL:
4707 case POSIX_FADV_RANDOM:
4708 case POSIX_FADV_SEQUENTIAL:
4709 break;
4710 default:
4711 return -EAGAIN;
4712 }
4713 }
Jens Axboe4840e412019-12-25 22:03:45 -07004714
4715 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4716 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004717 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004718 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004719 return 0;
4720}
4721
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004722static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4723{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004724 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004725 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004726 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004727 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004728 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004729 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004730
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004731 req->statx.dfd = READ_ONCE(sqe->fd);
4732 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004733 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004734 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4735 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004736
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004737 return 0;
4738}
4739
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004740static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004741{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004742 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004743 int ret;
4744
Pavel Begunkov59d70012021-03-22 01:58:30 +00004745 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004746 return -EAGAIN;
4747
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004748 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4749 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004750
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004751 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004752 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004753 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004754 return 0;
4755}
4756
Jens Axboeb5dba592019-12-11 14:02:38 -07004757static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4758{
Jens Axboe14587a462020-09-05 11:36:08 -06004759 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004760 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004761 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004762 sqe->rw_flags || sqe->buf_index)
Jens Axboeb5dba592019-12-11 14:02:38 -07004763 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004764 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004765 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004766
4767 req->close.fd = READ_ONCE(sqe->fd);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004768 req->close.file_slot = READ_ONCE(sqe->file_index);
4769 if (req->close.file_slot && req->close.fd)
4770 return -EINVAL;
4771
Jens Axboeb5dba592019-12-11 14:02:38 -07004772 return 0;
4773}
4774
Pavel Begunkov889fca72021-02-10 00:03:09 +00004775static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004776{
Jens Axboe9eac1902021-01-19 15:50:37 -07004777 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004778 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004779 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004780 struct file *file = NULL;
4781 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004782
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004783 if (req->close.file_slot) {
4784 ret = io_close_fixed(req, issue_flags);
4785 goto err;
4786 }
4787
Jens Axboe9eac1902021-01-19 15:50:37 -07004788 spin_lock(&files->file_lock);
4789 fdt = files_fdtable(files);
4790 if (close->fd >= fdt->max_fds) {
4791 spin_unlock(&files->file_lock);
4792 goto err;
4793 }
4794 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004795 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004796 spin_unlock(&files->file_lock);
4797 file = NULL;
4798 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004799 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004800
4801 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004802 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004803 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004804 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004805 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004806
Jens Axboe9eac1902021-01-19 15:50:37 -07004807 ret = __close_fd_get_file(close->fd, &file);
4808 spin_unlock(&files->file_lock);
4809 if (ret < 0) {
4810 if (ret == -ENOENT)
4811 ret = -EBADF;
4812 goto err;
4813 }
4814
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004815 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004816 ret = filp_close(file, current->files);
4817err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004818 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004819 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004820 if (file)
4821 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004822 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004823 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004824}
4825
Pavel Begunkov1155c762021-02-18 18:29:38 +00004826static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004827{
4828 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004829
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004830 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4831 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004832 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4833 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004834 return -EINVAL;
4835
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004836 req->sync.off = READ_ONCE(sqe->off);
4837 req->sync.len = READ_ONCE(sqe->len);
4838 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004839 return 0;
4840}
4841
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004842static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004843{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004844 int ret;
4845
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004846 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004847 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004848 return -EAGAIN;
4849
Jens Axboe9adbd452019-12-20 08:45:55 -07004850 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004851 req->sync.flags);
4852 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004853 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004854 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004855 return 0;
4856}
4857
YueHaibing469956e2020-03-04 15:53:52 +08004858#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004859static int io_setup_async_msg(struct io_kiocb *req,
4860 struct io_async_msghdr *kmsg)
4861{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004862 struct io_async_msghdr *async_msg = req->async_data;
4863
4864 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004865 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004866 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004867 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004868 return -ENOMEM;
4869 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004870 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004871 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004872 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004873 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004874 /* if were using fast_iov, set it to the new one */
4875 if (!async_msg->free_iov)
4876 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4877
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004878 return -EAGAIN;
4879}
4880
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004881static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4882 struct io_async_msghdr *iomsg)
4883{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004884 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004885 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004886 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004887 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004888}
4889
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004890static int io_sendmsg_prep_async(struct io_kiocb *req)
4891{
4892 int ret;
4893
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004894 ret = io_sendmsg_copy_hdr(req, req->async_data);
4895 if (!ret)
4896 req->flags |= REQ_F_NEED_CLEANUP;
4897 return ret;
4898}
4899
Jens Axboe3529d8c2019-12-19 18:24:38 -07004900static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004901{
Jens Axboee47293f2019-12-20 08:58:21 -07004902 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004903
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004904 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4905 return -EINVAL;
4906
Pavel Begunkov270a5942020-07-12 20:41:04 +03004907 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004908 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004909 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4910 if (sr->msg_flags & MSG_DONTWAIT)
4911 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004912
Jens Axboed8768362020-02-27 14:17:49 -07004913#ifdef CONFIG_COMPAT
4914 if (req->ctx->compat)
4915 sr->msg_flags |= MSG_CMSG_COMPAT;
4916#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004917 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004918}
4919
Pavel Begunkov889fca72021-02-10 00:03:09 +00004920static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004921{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004922 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004923 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004924 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004925 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004926 int ret;
4927
Florent Revestdba4a922020-12-04 12:36:04 +01004928 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004929 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004930 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004931
Pavel Begunkovd886e182021-10-04 20:02:56 +01004932 if (req_has_async_data(req)) {
4933 kmsg = req->async_data;
4934 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004935 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004936 if (ret)
4937 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004938 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004939 }
4940
Pavel Begunkov04411802021-04-01 15:44:00 +01004941 flags = req->sr_msg.msg_flags;
4942 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004943 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004944 if (flags & MSG_WAITALL)
4945 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4946
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004947 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004948
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004949 if (ret < min_ret) {
4950 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
4951 return io_setup_async_msg(req, kmsg);
4952 if (ret == -ERESTARTSYS)
4953 ret = -EINTR;
4954 req_set_fail(req);
4955 }
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004956 /* fast path, check for non-NULL to avoid function call */
4957 if (kmsg->free_iov)
4958 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004959 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov889fca72021-02-10 00:03:09 +00004960 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004961 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004962}
4963
Pavel Begunkov889fca72021-02-10 00:03:09 +00004964static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004965{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004966 struct io_sr_msg *sr = &req->sr_msg;
4967 struct msghdr msg;
4968 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004969 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004970 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004971 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004972 int ret;
4973
Florent Revestdba4a922020-12-04 12:36:04 +01004974 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004975 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004976 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004977
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004978 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4979 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004980 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004981
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004982 msg.msg_name = NULL;
4983 msg.msg_control = NULL;
4984 msg.msg_controllen = 0;
4985 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004986
Pavel Begunkov04411802021-04-01 15:44:00 +01004987 flags = req->sr_msg.msg_flags;
4988 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004989 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004990 if (flags & MSG_WAITALL)
4991 min_ret = iov_iter_count(&msg.msg_iter);
4992
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004993 msg.msg_flags = flags;
4994 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004995 if (ret < min_ret) {
4996 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
4997 return -EAGAIN;
4998 if (ret == -ERESTARTSYS)
4999 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005000 req_set_fail(req);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005001 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005002 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07005003 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07005004}
5005
Pavel Begunkov1400e692020-07-12 20:41:05 +03005006static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
5007 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07005008{
5009 struct io_sr_msg *sr = &req->sr_msg;
5010 struct iovec __user *uiov;
5011 size_t iov_len;
5012 int ret;
5013
Pavel Begunkov1400e692020-07-12 20:41:05 +03005014 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
5015 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07005016 if (ret)
5017 return ret;
5018
5019 if (req->flags & REQ_F_BUFFER_SELECT) {
5020 if (iov_len > 1)
5021 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00005022 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07005023 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00005024 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005025 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07005026 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005027 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02005028 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005029 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02005030 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07005031 if (ret > 0)
5032 ret = 0;
5033 }
5034
5035 return ret;
5036}
5037
5038#ifdef CONFIG_COMPAT
5039static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03005040 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07005041{
Jens Axboe52de1fe2020-02-27 10:15:42 -07005042 struct io_sr_msg *sr = &req->sr_msg;
5043 struct compat_iovec __user *uiov;
5044 compat_uptr_t ptr;
5045 compat_size_t len;
5046 int ret;
5047
Pavel Begunkov4af34172021-04-11 01:46:30 +01005048 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
5049 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07005050 if (ret)
5051 return ret;
5052
5053 uiov = compat_ptr(ptr);
5054 if (req->flags & REQ_F_BUFFER_SELECT) {
5055 compat_ssize_t clen;
5056
5057 if (len > 1)
5058 return -EINVAL;
5059 if (!access_ok(uiov, sizeof(*uiov)))
5060 return -EFAULT;
5061 if (__get_user(clen, &uiov->iov_len))
5062 return -EFAULT;
5063 if (clen < 0)
5064 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00005065 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005066 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07005067 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005068 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02005069 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005070 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02005071 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07005072 if (ret < 0)
5073 return ret;
5074 }
5075
5076 return 0;
5077}
Jens Axboe03b12302019-12-02 18:50:25 -07005078#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07005079
Pavel Begunkov1400e692020-07-12 20:41:05 +03005080static int io_recvmsg_copy_hdr(struct io_kiocb *req,
5081 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07005082{
Pavel Begunkov1400e692020-07-12 20:41:05 +03005083 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07005084
5085#ifdef CONFIG_COMPAT
5086 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03005087 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07005088#endif
5089
Pavel Begunkov1400e692020-07-12 20:41:05 +03005090 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07005091}
5092
Jens Axboebcda7ba2020-02-23 16:42:51 -07005093static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov51aac422021-10-14 16:10:17 +01005094 unsigned int issue_flags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07005095{
5096 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005097
Pavel Begunkov51aac422021-10-14 16:10:17 +01005098 return io_buffer_select(req, &sr->len, sr->bgid, issue_flags);
Jens Axboe03b12302019-12-02 18:50:25 -07005099}
5100
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005101static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07005102{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005103 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07005104
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005105 ret = io_recvmsg_copy_hdr(req, req->async_data);
5106 if (!ret)
5107 req->flags |= REQ_F_NEED_CLEANUP;
5108 return ret;
5109}
5110
5111static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5112{
5113 struct io_sr_msg *sr = &req->sr_msg;
5114
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03005115 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5116 return -EINVAL;
5117
Pavel Begunkov270a5942020-07-12 20:41:04 +03005118 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07005119 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005120 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01005121 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
5122 if (sr->msg_flags & MSG_DONTWAIT)
5123 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005124
Jens Axboed8768362020-02-27 14:17:49 -07005125#ifdef CONFIG_COMPAT
5126 if (req->ctx->compat)
5127 sr->msg_flags |= MSG_CMSG_COMPAT;
5128#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005129 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07005130}
5131
Pavel Begunkov889fca72021-02-10 00:03:09 +00005132static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07005133{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005134 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005135 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005136 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005137 unsigned flags;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00005138 int ret, min_ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005139 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005140
Florent Revestdba4a922020-12-04 12:36:04 +01005141 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005142 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005143 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005144
Pavel Begunkovd886e182021-10-04 20:02:56 +01005145 if (req_has_async_data(req)) {
5146 kmsg = req->async_data;
5147 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005148 ret = io_recvmsg_copy_hdr(req, &iomsg);
5149 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03005150 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005151 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005152 }
5153
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005154 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01005155 kbuf = io_recv_buffer_select(req, issue_flags);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005156 if (IS_ERR(kbuf))
5157 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005158 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00005159 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
5160 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005161 1, req->sr_msg.len);
5162 }
5163
Pavel Begunkov04411802021-04-01 15:44:00 +01005164 flags = req->sr_msg.msg_flags;
5165 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005166 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005167 if (flags & MSG_WAITALL)
5168 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
5169
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005170 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
5171 kmsg->uaddr, flags);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005172 if (ret < min_ret) {
5173 if (ret == -EAGAIN && force_nonblock)
5174 return io_setup_async_msg(req, kmsg);
5175 if (ret == -ERESTARTSYS)
5176 ret = -EINTR;
5177 req_set_fail(req);
5178 } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
5179 req_set_fail(req);
5180 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005181
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005182 /* fast path, check for non-NULL to avoid function call */
5183 if (kmsg->free_iov)
5184 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005185 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00005186 __io_req_complete(req, issue_flags, ret, io_put_kbuf(req));
Jens Axboe0fa03c62019-04-19 13:34:07 -06005187 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005188}
5189
Pavel Begunkov889fca72021-02-10 00:03:09 +00005190static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07005191{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005192 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005193 struct io_sr_msg *sr = &req->sr_msg;
5194 struct msghdr msg;
5195 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005196 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005197 struct iovec iov;
5198 unsigned flags;
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00005199 int ret, min_ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005200 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005201
Florent Revestdba4a922020-12-04 12:36:04 +01005202 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005203 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005204 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005205
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005206 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01005207 kbuf = io_recv_buffer_select(req, issue_flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005208 if (IS_ERR(kbuf))
5209 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005210 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005211 }
5212
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005213 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005214 if (unlikely(ret))
5215 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005216
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005217 msg.msg_name = NULL;
5218 msg.msg_control = NULL;
5219 msg.msg_controllen = 0;
5220 msg.msg_namelen = 0;
5221 msg.msg_iocb = NULL;
5222 msg.msg_flags = 0;
5223
Pavel Begunkov04411802021-04-01 15:44:00 +01005224 flags = req->sr_msg.msg_flags;
5225 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005226 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005227 if (flags & MSG_WAITALL)
5228 min_ret = iov_iter_count(&msg.msg_iter);
5229
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005230 ret = sock_recvmsg(sock, &msg, flags);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005231 if (ret < min_ret) {
5232 if (ret == -EAGAIN && force_nonblock)
5233 return -EAGAIN;
5234 if (ret == -ERESTARTSYS)
5235 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005236 req_set_fail(req);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005237 } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
Alviro Iskandar Setiawan0d7c1152022-02-07 21:05:33 +07005238out_free:
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005239 req_set_fail(req);
5240 }
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00005241 __io_req_complete(req, issue_flags, ret, io_put_kbuf(req));
Jens Axboefddafac2020-01-04 20:19:44 -07005242 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005243}
5244
Jens Axboe3529d8c2019-12-19 18:24:38 -07005245static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005246{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005247 struct io_accept *accept = &req->accept;
5248
Jens Axboe14587a462020-09-05 11:36:08 -06005249 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005250 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005251 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005252 return -EINVAL;
5253
Jens Axboed55e5f52019-12-11 16:12:15 -07005254 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5255 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005256 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005257 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005258
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005259 accept->file_slot = READ_ONCE(sqe->file_index);
5260 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5261 (accept->flags & SOCK_CLOEXEC)))
5262 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005263 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5264 return -EINVAL;
5265 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5266 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005267 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005268}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005269
Pavel Begunkov889fca72021-02-10 00:03:09 +00005270static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005271{
5272 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005273 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005274 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005275 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005276 struct file *file;
5277 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005278
Jiufei Xuee697dee2020-06-10 13:41:59 +08005279 if (req->file->f_flags & O_NONBLOCK)
5280 req->flags |= REQ_F_NOWAIT;
5281
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005282 if (!fixed) {
5283 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5284 if (unlikely(fd < 0))
5285 return fd;
5286 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005287 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5288 accept->flags);
5289 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005290 if (!fixed)
5291 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005292 ret = PTR_ERR(file);
5293 if (ret == -EAGAIN && force_nonblock)
5294 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005295 if (ret == -ERESTARTSYS)
5296 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005297 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005298 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005299 fd_install(fd, file);
5300 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005301 } else {
5302 ret = io_install_fixed_file(req, file, issue_flags,
5303 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005304 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005305 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005306 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005307}
5308
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005309static int io_connect_prep_async(struct io_kiocb *req)
5310{
5311 struct io_async_connect *io = req->async_data;
5312 struct io_connect *conn = &req->connect;
5313
5314 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5315}
5316
Jens Axboe3529d8c2019-12-19 18:24:38 -07005317static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005318{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005319 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005320
Jens Axboe14587a462020-09-05 11:36:08 -06005321 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005322 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005323 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5324 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005325 return -EINVAL;
5326
Jens Axboe3529d8c2019-12-19 18:24:38 -07005327 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5328 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005329 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005330}
5331
Pavel Begunkov889fca72021-02-10 00:03:09 +00005332static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005333{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005334 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005335 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005336 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005337 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005338
Pavel Begunkovd886e182021-10-04 20:02:56 +01005339 if (req_has_async_data(req)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005340 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005341 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005342 ret = move_addr_to_kernel(req->connect.addr,
5343 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005344 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005345 if (ret)
5346 goto out;
5347 io = &__io;
5348 }
5349
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005350 file_flags = force_nonblock ? O_NONBLOCK : 0;
5351
Jens Axboee8c2bc12020-08-15 18:44:09 -07005352 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005353 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005354 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Pavel Begunkovd886e182021-10-04 20:02:56 +01005355 if (req_has_async_data(req))
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005356 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005357 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005358 ret = -ENOMEM;
5359 goto out;
5360 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005361 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005362 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005363 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005364 if (ret == -ERESTARTSYS)
5365 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005366out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005367 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005368 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005369 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005370 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005371}
YueHaibing469956e2020-03-04 15:53:52 +08005372#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005373#define IO_NETOP_FN(op) \
5374static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5375{ \
5376 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005377}
5378
Jens Axboe99a10082021-02-19 09:35:19 -07005379#define IO_NETOP_PREP(op) \
5380IO_NETOP_FN(op) \
5381static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5382{ \
5383 return -EOPNOTSUPP; \
5384} \
5385
5386#define IO_NETOP_PREP_ASYNC(op) \
5387IO_NETOP_PREP(op) \
5388static int io_##op##_prep_async(struct io_kiocb *req) \
5389{ \
5390 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005391}
5392
Jens Axboe99a10082021-02-19 09:35:19 -07005393IO_NETOP_PREP_ASYNC(sendmsg);
5394IO_NETOP_PREP_ASYNC(recvmsg);
5395IO_NETOP_PREP_ASYNC(connect);
5396IO_NETOP_PREP(accept);
5397IO_NETOP_FN(send);
5398IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005399#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005400
Jens Axboed7718a92020-02-14 22:23:12 -07005401struct io_poll_table {
5402 struct poll_table_struct pt;
5403 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005404 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005405 int error;
5406};
5407
Pavel Begunkovaa434772021-12-15 22:08:48 +00005408#define IO_POLL_CANCEL_FLAG BIT(31)
5409#define IO_POLL_REF_MASK ((1u << 20)-1)
5410
5411/*
5412 * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can
5413 * bump it and acquire ownership. It's disallowed to modify requests while not
5414 * owning it, that prevents from races for enqueueing task_work's and b/w
5415 * arming poll and wakeups.
5416 */
5417static inline bool io_poll_get_ownership(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005418{
Pavel Begunkovaa434772021-12-15 22:08:48 +00005419 return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
Jens Axboed7718a92020-02-14 22:23:12 -07005420}
5421
Pavel Begunkovaa434772021-12-15 22:08:48 +00005422static void io_poll_mark_cancelled(struct io_kiocb *req)
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005423{
Pavel Begunkovaa434772021-12-15 22:08:48 +00005424 atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005425}
5426
Jens Axboed4e7cd32020-08-15 11:44:50 -07005427static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005428{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005429 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005430 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005431 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005432 return req->apoll->double_poll;
5433}
5434
5435static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5436{
5437 if (req->opcode == IORING_OP_POLL_ADD)
5438 return &req->poll;
5439 return &req->apoll->poll;
5440}
5441
Pavel Begunkov56418972021-12-15 22:08:46 +00005442static void io_poll_req_insert(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005443{
5444 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov56418972021-12-15 22:08:46 +00005445 struct hlist_head *list;
Jens Axboe18bceab2020-05-15 11:56:54 -06005446
Pavel Begunkov56418972021-12-15 22:08:46 +00005447 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5448 hlist_add_head(&req->hash_node, list);
Jens Axboe18bceab2020-05-15 11:56:54 -06005449}
5450
5451static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5452 wait_queue_func_t wake_func)
5453{
5454 poll->head = NULL;
Jens Axboe464dca62021-03-19 14:06:24 -06005455#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5456 /* mask in events that we always want/need */
5457 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005458 INIT_LIST_HEAD(&poll->wait.entry);
5459 init_waitqueue_func_entry(&poll->wait, wake_func);
5460}
5461
Pavel Begunkovaa434772021-12-15 22:08:48 +00005462static inline void io_poll_remove_entry(struct io_poll_iocb *poll)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005463{
Pavel Begunkov791f3462022-01-14 11:59:10 +00005464 struct wait_queue_head *head = smp_load_acquire(&poll->head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005465
Pavel Begunkov791f3462022-01-14 11:59:10 +00005466 if (head) {
5467 spin_lock_irq(&head->lock);
5468 list_del_init(&poll->wait.entry);
5469 poll->head = NULL;
5470 spin_unlock_irq(&head->lock);
5471 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005472}
5473
Pavel Begunkovaa434772021-12-15 22:08:48 +00005474static void io_poll_remove_entries(struct io_kiocb *req)
5475{
5476 struct io_poll_iocb *poll = io_poll_get_single(req);
5477 struct io_poll_iocb *poll_double = io_poll_get_double(req);
5478
Pavel Begunkov791f3462022-01-14 11:59:10 +00005479 /*
5480 * While we hold the waitqueue lock and the waitqueue is nonempty,
5481 * wake_up_pollfree() will wait for us. However, taking the waitqueue
5482 * lock in the first place can race with the waitqueue being freed.
5483 *
5484 * We solve this as eventpoll does: by taking advantage of the fact that
5485 * all users of wake_up_pollfree() will RCU-delay the actual free. If
5486 * we enter rcu_read_lock() and see that the pointer to the queue is
5487 * non-NULL, we can then lock it without the memory being freed out from
5488 * under us.
5489 *
5490 * Keep holding rcu_read_lock() as long as we hold the queue lock, in
5491 * case the caller deletes the entry from the queue, leaving it empty.
5492 * In that case, only RCU prevents the queue memory from being freed.
5493 */
5494 rcu_read_lock();
5495 io_poll_remove_entry(poll);
5496 if (poll_double)
Pavel Begunkovaa434772021-12-15 22:08:48 +00005497 io_poll_remove_entry(poll_double);
Pavel Begunkov791f3462022-01-14 11:59:10 +00005498 rcu_read_unlock();
Pavel Begunkovaa434772021-12-15 22:08:48 +00005499}
5500
5501/*
5502 * All poll tw should go through this. Checks for poll events, manages
5503 * references, does rewait, etc.
5504 *
5505 * Returns a negative error on failure. >0 when no action require, which is
5506 * either spurious wakeup or multishot CQE is served. 0 when it's done with
5507 * the request, then the mask is stored in req->result.
5508 */
5509static int io_poll_check_events(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005510{
5511 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovaa434772021-12-15 22:08:48 +00005512 struct io_poll_iocb *poll = io_poll_get_single(req);
5513 int v;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005514
5515 /* req->task == current here, checking PF_EXITING is safe */
5516 if (unlikely(req->task->flags & PF_EXITING))
Pavel Begunkovaa434772021-12-15 22:08:48 +00005517 io_poll_mark_cancelled(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005518
Pavel Begunkovaa434772021-12-15 22:08:48 +00005519 do {
5520 v = atomic_read(&req->poll_refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005521
Pavel Begunkovaa434772021-12-15 22:08:48 +00005522 /* tw handler should be the owner, and so have some references */
5523 if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK)))
5524 return 0;
5525 if (v & IO_POLL_CANCEL_FLAG)
5526 return -ECANCELED;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005527
Pavel Begunkovaa434772021-12-15 22:08:48 +00005528 if (!req->result) {
5529 struct poll_table_struct pt = { ._key = poll->events };
Jens Axboed7718a92020-02-14 22:23:12 -07005530
Pavel Begunkovaa434772021-12-15 22:08:48 +00005531 req->result = vfs_poll(req->file, &pt) & poll->events;
5532 }
Jens Axboed7718a92020-02-14 22:23:12 -07005533
Pavel Begunkovaa434772021-12-15 22:08:48 +00005534 /* multishot, just fill an CQE and proceed */
5535 if (req->result && !(poll->events & EPOLLONESHOT)) {
5536 __poll_t mask = mangle_poll(req->result & poll->events);
5537 bool filled;
Jens Axboed7718a92020-02-14 22:23:12 -07005538
Pavel Begunkovaa434772021-12-15 22:08:48 +00005539 spin_lock(&ctx->completion_lock);
5540 filled = io_fill_cqe_aux(ctx, req->user_data, mask,
5541 IORING_CQE_F_MORE);
5542 io_commit_cqring(ctx);
5543 spin_unlock(&ctx->completion_lock);
5544 if (unlikely(!filled))
5545 return -ECANCELED;
5546 io_cqring_ev_posted(ctx);
5547 } else if (req->result) {
5548 return 0;
5549 }
Jens Axboed7718a92020-02-14 22:23:12 -07005550
Pavel Begunkovaa434772021-12-15 22:08:48 +00005551 /*
5552 * Release all references, retry if someone tried to restart
5553 * task_work while we were executing it.
5554 */
5555 } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs));
Jens Axboed7718a92020-02-14 22:23:12 -07005556
Pavel Begunkovaa434772021-12-15 22:08:48 +00005557 return 1;
Jens Axboe18bceab2020-05-15 11:56:54 -06005558}
5559
5560static void io_poll_task_func(struct io_kiocb *req, bool *locked)
5561{
5562 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovaa434772021-12-15 22:08:48 +00005563 int ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06005564
Pavel Begunkovaa434772021-12-15 22:08:48 +00005565 ret = io_poll_check_events(req);
5566 if (ret > 0)
5567 return;
5568
5569 if (!ret) {
5570 req->result = mangle_poll(req->result & req->poll.events);
Jens Axboe18bceab2020-05-15 11:56:54 -06005571 } else {
Pavel Begunkovaa434772021-12-15 22:08:48 +00005572 req->result = ret;
5573 req_set_fail(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005574 }
Pavel Begunkovaa434772021-12-15 22:08:48 +00005575
5576 io_poll_remove_entries(req);
5577 spin_lock(&ctx->completion_lock);
5578 hash_del(&req->hash_node);
5579 __io_req_complete_post(req, req->result, 0);
5580 io_commit_cqring(ctx);
5581 spin_unlock(&ctx->completion_lock);
5582 io_cqring_ev_posted(ctx);
Jens Axboe18bceab2020-05-15 11:56:54 -06005583}
5584
Pavel Begunkovaa434772021-12-15 22:08:48 +00005585static void io_apoll_task_func(struct io_kiocb *req, bool *locked)
5586{
5587 struct io_ring_ctx *ctx = req->ctx;
5588 int ret;
5589
5590 ret = io_poll_check_events(req);
5591 if (ret > 0)
5592 return;
5593
5594 io_poll_remove_entries(req);
5595 spin_lock(&ctx->completion_lock);
5596 hash_del(&req->hash_node);
5597 spin_unlock(&ctx->completion_lock);
5598
5599 if (!ret)
5600 io_req_task_submit(req, locked);
5601 else
5602 io_req_complete_failed(req, ret);
5603}
5604
5605static void __io_poll_execute(struct io_kiocb *req, int mask)
5606{
5607 req->result = mask;
5608 if (req->opcode == IORING_OP_POLL_ADD)
5609 req->io_task_work.func = io_poll_task_func;
5610 else
5611 req->io_task_work.func = io_apoll_task_func;
5612
5613 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5614 io_req_task_work_add(req, false);
5615}
5616
5617static inline void io_poll_execute(struct io_kiocb *req, int res)
5618{
5619 if (io_poll_get_ownership(req))
5620 __io_poll_execute(req, res);
5621}
5622
5623static void io_poll_cancel_req(struct io_kiocb *req)
5624{
5625 io_poll_mark_cancelled(req);
5626 /* kick tw, which should complete the request */
5627 io_poll_execute(req, 0);
5628}
5629
5630static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5631 void *key)
Jens Axboe18bceab2020-05-15 11:56:54 -06005632{
5633 struct io_kiocb *req = wait->private;
Pavel Begunkovaa434772021-12-15 22:08:48 +00005634 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
5635 wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005636 __poll_t mask = key_to_poll(key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005637
Pavel Begunkov791f3462022-01-14 11:59:10 +00005638 if (unlikely(mask & POLLFREE)) {
5639 io_poll_mark_cancelled(req);
5640 /* we have to kick tw in case it's not already */
5641 io_poll_execute(req, 0);
5642
5643 /*
5644 * If the waitqueue is being freed early but someone is already
5645 * holds ownership over it, we have to tear down the request as
5646 * best we can. That means immediately removing the request from
5647 * its waitqueue and preventing all further accesses to the
5648 * waitqueue via the request.
5649 */
5650 list_del_init(&poll->wait.entry);
5651
5652 /*
5653 * Careful: this *must* be the last step, since as soon
5654 * as req->head is NULL'ed out, the request can be
5655 * completed and freed, since aio_poll_complete_work()
5656 * will no longer need to take the waitqueue lock.
5657 */
5658 smp_store_release(&poll->head, NULL);
5659 return 1;
5660 }
5661
Pavel Begunkovaa434772021-12-15 22:08:48 +00005662 /* for instances that support it check for an event match first */
Jens Axboe18bceab2020-05-15 11:56:54 -06005663 if (mask && !(mask & poll->events))
5664 return 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06005665
Pavel Begunkoveb0089d2021-12-15 22:08:49 +00005666 if (io_poll_get_ownership(req)) {
5667 /* optional, saves extra locking for removal in tw handler */
5668 if (mask && poll->events & EPOLLONESHOT) {
5669 list_del_init(&poll->wait.entry);
5670 poll->head = NULL;
5671 }
Pavel Begunkovaa434772021-12-15 22:08:48 +00005672 __io_poll_execute(req, mask);
Pavel Begunkoveb0089d2021-12-15 22:08:49 +00005673 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005674 return 1;
5675}
5676
Jens Axboe18bceab2020-05-15 11:56:54 -06005677static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005678 struct wait_queue_head *head,
5679 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005680{
5681 struct io_kiocb *req = pt->req;
5682
5683 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005684 * The file being polled uses multiple waitqueues for poll handling
5685 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5686 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005687 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005688 if (unlikely(pt->nr_entries)) {
Pavel Begunkovaa434772021-12-15 22:08:48 +00005689 struct io_poll_iocb *first = poll;
Pavel Begunkov58852d42020-10-16 20:55:56 +01005690
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005691 /* double add on the same waitqueue head, ignore */
Pavel Begunkovaa434772021-12-15 22:08:48 +00005692 if (first->head == head)
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005693 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005694 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005695 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005696 if ((*poll_ptr)->head == head)
5697 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005698 pt->error = -EINVAL;
5699 return;
5700 }
Pavel Begunkovaa434772021-12-15 22:08:48 +00005701
Jens Axboe18bceab2020-05-15 11:56:54 -06005702 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5703 if (!poll) {
5704 pt->error = -ENOMEM;
5705 return;
5706 }
Pavel Begunkovaa434772021-12-15 22:08:48 +00005707 io_init_poll_iocb(poll, first->events, first->wait.func);
Jens Axboe807abcb2020-07-17 17:09:27 -06005708 *poll_ptr = poll;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005709 if (req->opcode == IORING_OP_POLL_ADD)
5710 req->flags |= REQ_F_ASYNC_DATA;
Jens Axboe18bceab2020-05-15 11:56:54 -06005711 }
5712
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005713 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005714 poll->head = head;
Pavel Begunkovaa434772021-12-15 22:08:48 +00005715 poll->wait.private = req;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005716
5717 if (poll->events & EPOLLEXCLUSIVE)
5718 add_wait_queue_exclusive(head, &poll->wait);
5719 else
5720 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005721}
5722
Pavel Begunkovaa434772021-12-15 22:08:48 +00005723static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5724 struct poll_table_struct *p)
5725{
5726 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5727
5728 __io_queue_proc(&pt->req->poll, pt, head,
5729 (struct io_poll_iocb **) &pt->req->async_data);
5730}
5731
5732static int __io_arm_poll_handler(struct io_kiocb *req,
5733 struct io_poll_iocb *poll,
5734 struct io_poll_table *ipt, __poll_t mask)
5735{
5736 struct io_ring_ctx *ctx = req->ctx;
5737 int v;
5738
5739 INIT_HLIST_NODE(&req->hash_node);
5740 io_init_poll_iocb(poll, mask, io_poll_wake);
5741 poll->file = req->file;
5742 poll->wait.private = req;
5743
5744 ipt->pt._key = mask;
5745 ipt->req = req;
5746 ipt->error = 0;
5747 ipt->nr_entries = 0;
5748
5749 /*
5750 * Take the ownership to delay any tw execution up until we're done
5751 * with poll arming. see io_poll_get_ownership().
5752 */
5753 atomic_set(&req->poll_refs, 1);
5754 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5755
5756 if (mask && (poll->events & EPOLLONESHOT)) {
5757 io_poll_remove_entries(req);
5758 /* no one else has access to the req, forget about the ref */
5759 return mask;
5760 }
5761 if (!mask && unlikely(ipt->error || !ipt->nr_entries)) {
5762 io_poll_remove_entries(req);
5763 if (!ipt->error)
5764 ipt->error = -EINVAL;
5765 return 0;
5766 }
5767
5768 spin_lock(&ctx->completion_lock);
5769 io_poll_req_insert(req);
5770 spin_unlock(&ctx->completion_lock);
5771
5772 if (mask) {
5773 /* can't multishot if failed, just queue the event we've got */
5774 if (unlikely(ipt->error || !ipt->nr_entries))
5775 poll->events |= EPOLLONESHOT;
5776 __io_poll_execute(req, mask);
5777 return 0;
5778 }
5779
5780 /*
5781 * Release ownership. If someone tried to queue a tw while it was
5782 * locked, kick it off for them.
5783 */
5784 v = atomic_dec_return(&req->poll_refs);
5785 if (unlikely(v & IO_POLL_REF_MASK))
5786 __io_poll_execute(req, 0);
5787 return 0;
5788}
5789
Jens Axboe18bceab2020-05-15 11:56:54 -06005790static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5791 struct poll_table_struct *p)
5792{
5793 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005794 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005795
Jens Axboe807abcb2020-07-17 17:09:27 -06005796 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005797}
5798
Olivier Langlois59b735a2021-06-22 05:17:39 -07005799enum {
5800 IO_APOLL_OK,
5801 IO_APOLL_ABORTED,
5802 IO_APOLL_READY
5803};
5804
5805static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005806{
5807 const struct io_op_def *def = &io_op_defs[req->opcode];
5808 struct io_ring_ctx *ctx = req->ctx;
5809 struct async_poll *apoll;
5810 struct io_poll_table ipt;
Pavel Begunkovaa434772021-12-15 22:08:48 +00005811 __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI;
5812 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005813
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005814 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005815 return IO_APOLL_ABORTED;
Pavel Begunkov658d0a42021-10-23 12:13:58 +01005816 if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED))
5817 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005818
5819 if (def->pollin) {
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005820 mask |= POLLIN | POLLRDNORM;
5821
5822 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5823 if ((req->opcode == IORING_OP_RECVMSG) &&
5824 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5825 mask &= ~POLLIN;
5826 } else {
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005827 mask |= POLLOUT | POLLWRNORM;
5828 }
5829
Jens Axboed7718a92020-02-14 22:23:12 -07005830 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5831 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005832 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005833 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005834 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005835 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005836 ipt.pt._qproc = io_async_queue_proc;
5837
Pavel Begunkovaa434772021-12-15 22:08:48 +00005838 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask);
Hao Xu41a51692021-08-12 15:47:02 +08005839 if (ret || ipt.error)
5840 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5841
Olivier Langlois236daeae2021-05-31 02:36:37 -04005842 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5843 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005844 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005845}
5846
Jens Axboe76e1b642020-09-26 15:05:03 -06005847/*
5848 * Returns true if we found and killed one or more poll requests
5849 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01005850static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx,
5851 struct task_struct *tsk, bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005852{
Jens Axboe78076bb2019-12-04 19:56:40 -07005853 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005854 struct io_kiocb *req;
Pavel Begunkovaa434772021-12-15 22:08:48 +00005855 bool found = false;
5856 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005857
Jens Axboe79ebeae2021-08-10 15:18:27 -06005858 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005859 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5860 struct hlist_head *list;
5861
5862 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005863 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Linus Torvalds42a7b4e2022-01-12 10:20:35 -08005864 if (io_match_task_safe(req, tsk, cancel_all)) {
Pavel Begunkovaa434772021-12-15 22:08:48 +00005865 io_poll_cancel_req(req);
5866 found = true;
5867 }
Jens Axboef3606e32020-09-22 08:18:24 -06005868 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005869 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005870 spin_unlock(&ctx->completion_lock);
Pavel Begunkovaa434772021-12-15 22:08:48 +00005871 return found;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005872}
5873
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005874static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5875 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005876 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005877{
Jens Axboe78076bb2019-12-04 19:56:40 -07005878 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005879 struct io_kiocb *req;
5880
Jens Axboe78076bb2019-12-04 19:56:40 -07005881 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5882 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005883 if (sqe_addr != req->user_data)
5884 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005885 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5886 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005887 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005888 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005889 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005890}
5891
Pavel Begunkovaa434772021-12-15 22:08:48 +00005892static bool io_poll_disarm(struct io_kiocb *req)
5893 __must_hold(&ctx->completion_lock)
5894{
5895 if (!io_poll_get_ownership(req))
5896 return false;
5897 io_poll_remove_entries(req);
5898 hash_del(&req->hash_node);
5899 return true;
5900}
5901
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005902static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5903 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005904 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005905{
Pavel Begunkovaa434772021-12-15 22:08:48 +00005906 struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005907
Jens Axboeb2cb8052021-03-17 08:17:19 -06005908 if (!req)
5909 return -ENOENT;
Pavel Begunkovaa434772021-12-15 22:08:48 +00005910 io_poll_cancel_req(req);
5911 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005912}
5913
Pavel Begunkov9096af32021-04-14 13:38:36 +01005914static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5915 unsigned int flags)
5916{
5917 u32 events;
5918
5919 events = READ_ONCE(sqe->poll32_events);
5920#ifdef __BIG_ENDIAN
5921 events = swahw32(events);
5922#endif
5923 if (!(flags & IORING_POLL_ADD_MULTI))
5924 events |= EPOLLONESHOT;
5925 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5926}
5927
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005928static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005929 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005930{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005931 struct io_poll_update *upd = &req->poll_update;
5932 u32 flags;
5933
Jens Axboe221c5eb2019-01-17 09:41:58 -07005934 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5935 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005936 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005937 return -EINVAL;
5938 flags = READ_ONCE(sqe->len);
5939 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5940 IORING_POLL_ADD_MULTI))
5941 return -EINVAL;
5942 /* meaningless without update */
5943 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005944 return -EINVAL;
5945
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005946 upd->old_user_data = READ_ONCE(sqe->addr);
5947 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5948 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005949
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005950 upd->new_user_data = READ_ONCE(sqe->off);
5951 if (!upd->update_user_data && upd->new_user_data)
5952 return -EINVAL;
5953 if (upd->update_events)
5954 upd->events = io_poll_parse_events(sqe, flags);
5955 else if (sqe->poll32_events)
5956 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005957
Jens Axboe221c5eb2019-01-17 09:41:58 -07005958 return 0;
5959}
5960
Jens Axboe3529d8c2019-12-19 18:24:38 -07005961static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005962{
5963 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005964 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005965
5966 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5967 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005968 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005969 return -EINVAL;
5970 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005971 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005972 return -EINVAL;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00005973 if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
5974 return -EINVAL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005975
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005976 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005977 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005978 return 0;
5979}
5980
Pavel Begunkov61e98202021-02-10 00:03:08 +00005981static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005982{
5983 struct io_poll_iocb *poll = &req->poll;
Jens Axboe0969e782019-12-17 18:40:57 -07005984 struct io_poll_table ipt;
Pavel Begunkovaa434772021-12-15 22:08:48 +00005985 int ret;
Jens Axboe0969e782019-12-17 18:40:57 -07005986
Jens Axboed7718a92020-02-14 22:23:12 -07005987 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005988
Pavel Begunkovaa434772021-12-15 22:08:48 +00005989 ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events);
5990 ret = ret ?: ipt.error;
5991 if (ret)
5992 __io_req_complete(req, issue_flags, ret, 0);
5993 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005994}
5995
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005996static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005997{
5998 struct io_ring_ctx *ctx = req->ctx;
5999 struct io_kiocb *preq;
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00006000 int ret2, ret = 0;
Pavel Begunkovcc8e9ba2021-12-15 22:08:50 +00006001 bool locked;
Jens Axboeb69de282021-03-17 08:37:41 -06006002
Jens Axboe79ebeae2021-08-10 15:18:27 -06006003 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01006004 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Pavel Begunkovaa434772021-12-15 22:08:48 +00006005 if (!preq || !io_poll_disarm(preq)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006006 spin_unlock(&ctx->completion_lock);
Pavel Begunkovaa434772021-12-15 22:08:48 +00006007 ret = preq ? -EALREADY : -ENOENT;
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00006008 goto out;
Jens Axboeb69de282021-03-17 08:37:41 -06006009 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06006010 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06006011
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00006012 if (req->poll_update.update_events || req->poll_update.update_user_data) {
6013 /* only mask one event flags, keep behavior flags */
6014 if (req->poll_update.update_events) {
6015 preq->poll.events &= ~0xffff;
6016 preq->poll.events |= req->poll_update.events & 0xffff;
6017 preq->poll.events |= IO_POLL_UNMASK;
Jens Axboecb3b200e2021-04-06 09:49:31 -06006018 }
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00006019 if (req->poll_update.update_user_data)
6020 preq->user_data = req->poll_update.new_user_data;
6021
6022 ret2 = io_poll_add(preq, issue_flags);
6023 /* successfully updated, don't complete poll request */
6024 if (!ret2)
6025 goto out;
Jens Axboeb69de282021-03-17 08:37:41 -06006026 }
Jens Axboeb69de282021-03-17 08:37:41 -06006027
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00006028 req_set_fail(preq);
Pavel Begunkovcc8e9ba2021-12-15 22:08:50 +00006029 preq->result = -ECANCELED;
6030 locked = !(issue_flags & IO_URING_F_UNLOCKED);
6031 io_req_task_complete(preq, &locked);
Pavel Begunkov2bbb1462021-12-15 22:08:45 +00006032out:
6033 if (ret < 0)
Pavel Begunkov62245902021-10-02 19:36:14 +01006034 req_set_fail(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006035 /* complete update request, we're done with it */
Pavel Begunkovcc8e9ba2021-12-15 22:08:50 +00006036 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006037 return 0;
Jens Axboe89850fc2021-08-10 15:11:51 -06006038}
6039
Jens Axboe5262f562019-09-17 12:26:57 -06006040static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
6041{
Jens Axboead8a48a2019-11-15 08:49:11 -07006042 struct io_timeout_data *data = container_of(timer,
6043 struct io_timeout_data, timer);
6044 struct io_kiocb *req = data->req;
6045 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06006046 unsigned long flags;
6047
Jens Axboe89850fc2021-08-10 15:11:51 -06006048 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01006049 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03006050 atomic_set(&req->ctx->cq_timeouts,
6051 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06006052 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03006053
Pavel Begunkova90c8bf2021-12-05 14:38:00 +00006054 if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
6055 req_set_fail(req);
6056
6057 req->result = -ETIME;
6058 req->io_task_work.func = io_req_task_complete;
Hao Xu4813c372021-12-07 17:39:48 +08006059 io_req_task_work_add(req, false);
Jens Axboe5262f562019-09-17 12:26:57 -06006060 return HRTIMER_NORESTART;
6061}
6062
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006063static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
6064 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06006065 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07006066{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006067 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06006068 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006069 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06006070
6071 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006072 found = user_data == req->user_data;
6073 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06006074 break;
Jens Axboef254ac02020-08-12 17:33:30 -06006075 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006076 if (!found)
6077 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06006078
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006079 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006080 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006081 return ERR_PTR(-EALREADY);
6082 list_del_init(&req->timeout.list);
6083 return req;
6084}
6085
6086static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006087 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06006088 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006089{
6090 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6091
6092 if (IS_ERR(req))
6093 return PTR_ERR(req);
6094
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006095 req_set_fail(req);
Pavel Begunkov913a5712021-11-10 15:49:31 +00006096 io_fill_cqe_req(req, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01006097 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006098 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06006099}
6100
Jens Axboe50c1df22021-08-27 17:11:06 -06006101static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
6102{
6103 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
6104 case IORING_TIMEOUT_BOOTTIME:
6105 return CLOCK_BOOTTIME;
6106 case IORING_TIMEOUT_REALTIME:
6107 return CLOCK_REALTIME;
6108 default:
6109 /* can't happen, vetted at prep time */
6110 WARN_ON_ONCE(1);
6111 fallthrough;
6112 case 0:
6113 return CLOCK_MONOTONIC;
6114 }
6115}
6116
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006117static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6118 struct timespec64 *ts, enum hrtimer_mode mode)
6119 __must_hold(&ctx->timeout_lock)
6120{
6121 struct io_timeout_data *io;
6122 struct io_kiocb *req;
6123 bool found = false;
6124
6125 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
6126 found = user_data == req->user_data;
6127 if (found)
6128 break;
6129 }
6130 if (!found)
6131 return -ENOENT;
6132
6133 io = req->async_data;
6134 if (hrtimer_try_to_cancel(&io->timer) == -1)
6135 return -EALREADY;
6136 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
6137 io->timer.function = io_link_timeout_fn;
6138 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
6139 return 0;
6140}
6141
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006142static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6143 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06006144 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006145{
6146 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6147 struct io_timeout_data *data;
6148
6149 if (IS_ERR(req))
6150 return PTR_ERR(req);
6151
6152 req->timeout.off = 0; /* noseq */
6153 data = req->async_data;
6154 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06006155 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006156 data->timer.function = io_timeout_fn;
6157 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
6158 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07006159}
6160
Jens Axboe3529d8c2019-12-19 18:24:38 -07006161static int io_timeout_remove_prep(struct io_kiocb *req,
6162 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07006163{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006164 struct io_timeout_rem *tr = &req->timeout_rem;
6165
Jens Axboeb29472e2019-12-17 18:50:29 -07006166 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6167 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006168 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6169 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006170 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07006171 return -EINVAL;
6172
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006173 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006174 tr->addr = READ_ONCE(sqe->addr);
6175 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006176 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6177 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6178 return -EINVAL;
6179 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6180 tr->ltimeout = true;
6181 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006182 return -EINVAL;
6183 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6184 return -EFAULT;
Ye Bin20870092021-11-29 12:15:37 +08006185 if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0)
6186 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006187 } else if (tr->flags) {
6188 /* timeout removal doesn't support flags */
6189 return -EINVAL;
6190 }
6191
Jens Axboeb29472e2019-12-17 18:50:29 -07006192 return 0;
6193}
6194
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006195static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6196{
6197 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6198 : HRTIMER_MODE_REL;
6199}
6200
Jens Axboe11365042019-10-16 09:08:32 -06006201/*
6202 * Remove or update an existing timeout command
6203 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006204static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006205{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006206 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006207 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006208 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006209
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006210 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6211 spin_lock(&ctx->completion_lock);
6212 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006213 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006214 spin_unlock_irq(&ctx->timeout_lock);
6215 spin_unlock(&ctx->completion_lock);
6216 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006217 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6218
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006219 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006220 if (tr->ltimeout)
6221 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6222 else
6223 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006224 spin_unlock_irq(&ctx->timeout_lock);
6225 }
Jens Axboe11365042019-10-16 09:08:32 -06006226
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006227 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006228 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006229 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006230 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006231}
6232
Jens Axboe3529d8c2019-12-19 18:24:38 -07006233static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006234 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006235{
Jens Axboead8a48a2019-11-15 08:49:11 -07006236 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006237 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006238 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006239
Jens Axboead8a48a2019-11-15 08:49:11 -07006240 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006241 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006242 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6243 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006244 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006245 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006246 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006247 flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkov62245902021-10-02 19:36:14 +01006248 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
6249 IORING_TIMEOUT_ETIME_SUCCESS))
Jens Axboe50c1df22021-08-27 17:11:06 -06006250 return -EINVAL;
6251 /* more than one clock specified is invalid, obviously */
6252 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006253 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006254
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006255 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006256 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006257 if (unlikely(off && !req->ctx->off_timeout_used))
6258 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006259
Pavel Begunkovd6a644a2021-10-23 12:14:00 +01006260 if (WARN_ON_ONCE(req_has_async_data(req)))
6261 return -EFAULT;
6262 if (io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006263 return -ENOMEM;
6264
Jens Axboee8c2bc12020-08-15 18:44:09 -07006265 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006266 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006267 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006268
6269 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006270 return -EFAULT;
6271
Ye Binf6223ff2021-11-18 09:59:07 +08006272 if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0)
6273 return -EINVAL;
6274
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006275 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006276 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006277
6278 if (is_timeout_link) {
6279 struct io_submit_link *link = &req->ctx->submit_state.link;
6280
6281 if (!link->head)
6282 return -EINVAL;
6283 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6284 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006285 req->timeout.head = link->last;
6286 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006287 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006288 return 0;
6289}
6290
Pavel Begunkov61e98202021-02-10 00:03:08 +00006291static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006292{
Jens Axboead8a48a2019-11-15 08:49:11 -07006293 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006294 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006295 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006296 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006297
Jens Axboe89850fc2021-08-10 15:11:51 -06006298 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006299
Jens Axboe5262f562019-09-17 12:26:57 -06006300 /*
6301 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006302 * timeout event to be satisfied. If it isn't set, then this is
6303 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006304 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006305 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006306 entry = ctx->timeout_list.prev;
6307 goto add;
6308 }
Jens Axboe5262f562019-09-17 12:26:57 -06006309
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006310 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6311 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006312
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006313 /* Update the last seq here in case io_flush_timeouts() hasn't.
6314 * This is safe because ->completion_lock is held, and submissions
6315 * and completions are never mixed in the same ->completion_lock section.
6316 */
6317 ctx->cq_last_tm_flush = tail;
6318
Jens Axboe5262f562019-09-17 12:26:57 -06006319 /*
6320 * Insertion sort, ensuring the first entry in the list is always
6321 * the one we need first.
6322 */
Jens Axboe5262f562019-09-17 12:26:57 -06006323 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006324 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6325 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006326
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006327 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006328 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006329 /* nxt.seq is behind @tail, otherwise would've been completed */
6330 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006331 break;
6332 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006333add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006334 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006335 data->timer.function = io_timeout_fn;
6336 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006337 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006338 return 0;
6339}
6340
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006341struct io_cancel_data {
6342 struct io_ring_ctx *ctx;
6343 u64 user_data;
6344};
6345
Jens Axboe62755e32019-10-28 21:49:21 -06006346static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006347{
Jens Axboe62755e32019-10-28 21:49:21 -06006348 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006349 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006350
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006351 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006352}
6353
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006354static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6355 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006356{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006357 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006358 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006359 int ret = 0;
6360
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006361 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006362 return -ENOENT;
6363
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006364 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006365 switch (cancel_ret) {
6366 case IO_WQ_CANCEL_OK:
6367 ret = 0;
6368 break;
6369 case IO_WQ_CANCEL_RUNNING:
6370 ret = -EALREADY;
6371 break;
6372 case IO_WQ_CANCEL_NOTFOUND:
6373 ret = -ENOENT;
6374 break;
6375 }
6376
Jens Axboee977d6d2019-11-05 12:39:45 -07006377 return ret;
6378}
6379
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006380static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006381{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006382 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006383 int ret;
6384
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006385 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006386
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006387 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Jens Axboeccbf7262022-01-18 19:11:11 -07006388 /*
6389 * Fall-through even for -EALREADY, as we may have poll armed
6390 * that need unarming.
6391 */
6392 if (!ret)
6393 return 0;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006394
6395 spin_lock(&ctx->completion_lock);
Jens Axboeccbf7262022-01-18 19:11:11 -07006396 ret = io_poll_cancel(ctx, sqe_addr, false);
6397 if (ret != -ENOENT)
6398 goto out;
6399
Jens Axboe79ebeae2021-08-10 15:18:27 -06006400 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006401 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006402 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006403out:
6404 spin_unlock(&ctx->completion_lock);
6405 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006406}
6407
Jens Axboe3529d8c2019-12-19 18:24:38 -07006408static int io_async_cancel_prep(struct io_kiocb *req,
6409 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006410{
Jens Axboefbf23842019-12-17 18:45:56 -07006411 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006412 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006413 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6414 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006415 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6416 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006417 return -EINVAL;
6418
Jens Axboefbf23842019-12-17 18:45:56 -07006419 req->cancel.addr = READ_ONCE(sqe->addr);
6420 return 0;
6421}
6422
Pavel Begunkov61e98202021-02-10 00:03:08 +00006423static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006424{
6425 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006426 u64 sqe_addr = req->cancel.addr;
Hao Xu3b44b372021-10-18 21:34:31 +08006427 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006428 struct io_tctx_node *node;
6429 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006430
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006431 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006432 if (ret != -ENOENT)
6433 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006434
6435 /* slow path, try all io-wq's */
Hao Xu3b44b372021-10-18 21:34:31 +08006436 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006437 ret = -ENOENT;
6438 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6439 struct io_uring_task *tctx = node->task->io_uring;
6440
Pavel Begunkov58f99372021-03-12 16:25:55 +00006441 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6442 if (ret != -ENOENT)
6443 break;
6444 }
Hao Xu3b44b372021-10-18 21:34:31 +08006445 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006446done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006447 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006448 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006449 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006450 return 0;
6451}
6452
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006453static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006454 const struct io_uring_sqe *sqe)
6455{
Daniele Albano61710e42020-07-18 14:15:16 -06006456 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6457 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006458 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006459 return -EINVAL;
6460
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006461 req->rsrc_update.offset = READ_ONCE(sqe->off);
6462 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6463 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006464 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006465 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006466 return 0;
6467}
6468
Pavel Begunkov889fca72021-02-10 00:03:09 +00006469static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006470{
6471 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08006472 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006473 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006474 int ret;
6475
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006476 up.offset = req->rsrc_update.offset;
6477 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006478 up.nr = 0;
6479 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006480 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006481
Hao Xu3b44b372021-10-18 21:34:31 +08006482 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006483 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006484 &up, req->rsrc_update.nr_args);
Hao Xu3b44b372021-10-18 21:34:31 +08006485 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006486
6487 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006488 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006489 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006490 return 0;
6491}
6492
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006493static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006494{
Jens Axboed625c6e2019-12-17 19:53:05 -07006495 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006496 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006497 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006498 case IORING_OP_READV:
6499 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006500 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006501 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006502 case IORING_OP_WRITEV:
6503 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006504 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006505 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006506 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006507 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006508 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006509 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006510 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006511 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006512 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006513 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006514 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006515 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006516 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006517 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006518 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006519 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006520 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006521 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006522 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006523 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006524 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006525 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006526 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006527 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006528 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006529 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006530 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006531 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006532 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006533 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006534 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006535 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006536 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006537 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006538 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006539 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006540 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006541 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006542 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006543 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006544 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006545 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006546 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006547 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006548 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006549 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006550 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006551 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006552 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006553 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006554 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006555 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006556 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006557 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006558 case IORING_OP_SHUTDOWN:
6559 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006560 case IORING_OP_RENAMEAT:
6561 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006562 case IORING_OP_UNLINKAT:
6563 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006564 case IORING_OP_MKDIRAT:
6565 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006566 case IORING_OP_SYMLINKAT:
6567 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006568 case IORING_OP_LINKAT:
6569 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006570 }
6571
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006572 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6573 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006574 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006575}
6576
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006577static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006578{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006579 if (!io_op_defs[req->opcode].needs_async_setup)
6580 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01006581 if (WARN_ON_ONCE(req_has_async_data(req)))
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006582 return -EFAULT;
6583 if (io_alloc_async_data(req))
6584 return -EAGAIN;
6585
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006586 switch (req->opcode) {
6587 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006588 return io_rw_prep_async(req, READ);
6589 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006590 return io_rw_prep_async(req, WRITE);
6591 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006592 return io_sendmsg_prep_async(req);
6593 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006594 return io_recvmsg_prep_async(req);
6595 case IORING_OP_CONNECT:
6596 return io_connect_prep_async(req);
6597 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006598 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6599 req->opcode);
6600 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006601}
6602
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006603static u32 io_get_sequence(struct io_kiocb *req)
6604{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006605 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006606
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006607 /* need original cached_sq_head, but it was increased for each req */
6608 io_for_each_link(req, req)
6609 seq--;
6610 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006611}
6612
Pavel Begunkovc0724812021-10-04 20:02:54 +01006613static __cold void io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006614{
6615 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006616 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006617 int ret;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006618 u32 seq = io_get_sequence(req);
Pavel Begunkov3c199662021-06-15 16:47:57 +01006619
Jens Axboedef596e2019-01-09 08:59:42 -07006620 /* Still need defer if there is pending req in defer list. */
Hao Xue302f102021-11-25 17:21:02 +08006621 spin_lock(&ctx->completion_lock);
Pavel Begunkov5e371262021-09-24 22:00:04 +01006622 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) {
Hao Xue302f102021-11-25 17:21:02 +08006623 spin_unlock(&ctx->completion_lock);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006624queue:
Pavel Begunkov10c66902021-06-15 16:47:56 +01006625 ctx->drain_active = false;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006626 io_req_task_queue(req);
6627 return;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006628 }
Hao Xue302f102021-11-25 17:21:02 +08006629 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006630
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006631 ret = io_req_prep_async(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006632 if (ret) {
6633fail:
6634 io_req_complete_failed(req, ret);
6635 return;
6636 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006637 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006638 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006639 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006640 ret = -ENOMEM;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006641 goto fail;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006642 }
Jens Axboe31b51512019-01-18 22:56:34 -07006643
Jens Axboe79ebeae2021-08-10 15:18:27 -06006644 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006645 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006646 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006647 kfree(de);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006648 goto queue;
Jens Axboe31b51512019-01-18 22:56:34 -07006649 }
6650
6651 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006652 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006653 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006654 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006655 spin_unlock(&ctx->completion_lock);
Jens Axboe31b51512019-01-18 22:56:34 -07006656}
6657
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006658static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006659{
Pavel Begunkovd1fd1c22021-12-05 14:37:58 +00006660 if (req->flags & REQ_F_BUFFER_SELECTED)
6661 io_put_kbuf(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006662
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006663 if (req->flags & REQ_F_NEED_CLEANUP) {
6664 switch (req->opcode) {
6665 case IORING_OP_READV:
6666 case IORING_OP_READ_FIXED:
6667 case IORING_OP_READ:
6668 case IORING_OP_WRITEV:
6669 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006670 case IORING_OP_WRITE: {
6671 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006672
6673 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006674 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006675 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006676 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006677 case IORING_OP_SENDMSG: {
6678 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006679
6680 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006681 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006682 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006683 case IORING_OP_SPLICE:
6684 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006685 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6686 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006687 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006688 case IORING_OP_OPENAT:
6689 case IORING_OP_OPENAT2:
6690 if (req->open.filename)
6691 putname(req->open.filename);
6692 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006693 case IORING_OP_RENAMEAT:
6694 putname(req->rename.oldpath);
6695 putname(req->rename.newpath);
6696 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006697 case IORING_OP_UNLINKAT:
6698 putname(req->unlink.filename);
6699 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006700 case IORING_OP_MKDIRAT:
6701 putname(req->mkdir.filename);
6702 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006703 case IORING_OP_SYMLINKAT:
6704 putname(req->symlink.oldpath);
6705 putname(req->symlink.newpath);
6706 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006707 case IORING_OP_LINKAT:
6708 putname(req->hardlink.oldpath);
6709 putname(req->hardlink.newpath);
6710 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006711 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006712 }
Jens Axboe75652a302021-04-15 09:52:40 -06006713 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6714 kfree(req->apoll->double_poll);
6715 kfree(req->apoll);
6716 req->apoll = NULL;
6717 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006718 if (req->flags & REQ_F_INFLIGHT) {
6719 struct io_uring_task *tctx = req->task->io_uring;
6720
6721 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006722 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006723 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006724 put_cred(req->creds);
Pavel Begunkovd886e182021-10-04 20:02:56 +01006725 if (req->flags & REQ_F_ASYNC_DATA) {
6726 kfree(req->async_data);
6727 req->async_data = NULL;
6728 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006729 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006730}
6731
Pavel Begunkov889fca72021-02-10 00:03:09 +00006732static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006733{
Jens Axboe5730b272021-02-27 15:57:30 -07006734 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006735 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006736
Pavel Begunkov6878b402021-09-24 21:59:41 +01006737 if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006738 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006739
Paul Moore5bd21822021-02-16 19:46:48 -05006740 if (!io_op_defs[req->opcode].audit_skip)
6741 audit_uring_entry(req->opcode);
6742
Jens Axboed625c6e2019-12-17 19:53:05 -07006743 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006744 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006745 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006746 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006747 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006748 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006749 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006750 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006751 break;
6752 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006753 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006754 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006755 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006756 break;
6757 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006758 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006759 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006760 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006761 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006762 break;
6763 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006764 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006765 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006766 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006767 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006768 break;
6769 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006770 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006771 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006772 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006773 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006774 break;
6775 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006776 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006777 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006778 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006779 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006780 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006781 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006782 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006783 break;
6784 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006785 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006786 break;
6787 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006788 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006789 break;
6790 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006791 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006792 break;
6793 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006794 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006795 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006796 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006797 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006798 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006799 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006800 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006801 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006802 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006803 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006804 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006805 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006806 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006807 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006808 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006809 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006810 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006811 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006812 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006813 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006814 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006815 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006816 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006817 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006818 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006819 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006820 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006821 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006822 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006823 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006824 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006825 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006826 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006827 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006828 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006829 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006830 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006831 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006832 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006833 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006834 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006835 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006836 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006837 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006838 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006839 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006840 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006841 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006842 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006843 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006844 case IORING_OP_MKDIRAT:
6845 ret = io_mkdirat(req, issue_flags);
6846 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006847 case IORING_OP_SYMLINKAT:
6848 ret = io_symlinkat(req, issue_flags);
6849 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006850 case IORING_OP_LINKAT:
6851 ret = io_linkat(req, issue_flags);
6852 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006853 default:
6854 ret = -EINVAL;
6855 break;
6856 }
Jens Axboe31b51512019-01-18 22:56:34 -07006857
Paul Moore5bd21822021-02-16 19:46:48 -05006858 if (!io_op_defs[req->opcode].audit_skip)
6859 audit_uring_exit(!ret, ret);
6860
Jens Axboe5730b272021-02-27 15:57:30 -07006861 if (creds)
6862 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006863 if (ret)
6864 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006865 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkov99830282021-10-15 17:09:11 +01006866 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file)
Pavel Begunkov98821312021-10-15 17:09:12 +01006867 io_iopoll_req_issued(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006868
6869 return 0;
6870}
6871
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006872static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6873{
6874 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6875
6876 req = io_put_req_find_next(req);
6877 return req ? &req->work : NULL;
6878}
6879
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006880static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006881{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006882 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006883 unsigned int issue_flags = IO_URING_F_UNLOCKED;
6884 bool needs_poll = false;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006885 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006886 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006887
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006888 /* one will be dropped by ->io_free_work() after returning to io-wq */
6889 if (!(req->flags & REQ_F_REFCOUNT))
6890 __io_req_set_refcount(req, 2);
6891 else
6892 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006893
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006894 timeout = io_prep_linked_timeout(req);
6895 if (timeout)
6896 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006897
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006898 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006899 if (work->flags & IO_WQ_WORK_CANCEL) {
6900 io_req_task_queue_fail(req, -ECANCELED);
6901 return;
Jens Axboe561fb042019-10-24 07:25:42 -06006902 }
Jens Axboe31b51512019-01-18 22:56:34 -07006903
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006904 if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovafb7f56f2021-10-23 12:13:59 +01006905 const struct io_op_def *def = &io_op_defs[req->opcode];
6906 bool opcode_poll = def->pollin || def->pollout;
6907
6908 if (opcode_poll && file_can_poll(req->file)) {
6909 needs_poll = true;
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006910 issue_flags |= IO_URING_F_NONBLOCK;
Pavel Begunkovafb7f56f2021-10-23 12:13:59 +01006911 }
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006912 }
Hao Xu90fa0282021-10-18 21:34:45 +08006913
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006914 do {
6915 ret = io_issue_sqe(req, issue_flags);
6916 if (ret != -EAGAIN)
6917 break;
6918 /*
6919 * We can get EAGAIN for iopolled IO even though we're
6920 * forcing a sync submission from here, since we can't
6921 * wait for request slots on the block side.
6922 */
6923 if (!needs_poll) {
6924 cond_resched();
6925 continue;
Hao Xu90fa0282021-10-18 21:34:45 +08006926 }
6927
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006928 if (io_arm_poll_handler(req) == IO_APOLL_OK)
6929 return;
6930 /* aborted or ready, in either case retry blocking */
6931 needs_poll = false;
6932 issue_flags &= ~IO_URING_F_NONBLOCK;
6933 } while (1);
Jens Axboe31b51512019-01-18 22:56:34 -07006934
Pavel Begunkova3df76982021-02-18 22:32:52 +00006935 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006936 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006937 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006938}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006939
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006940static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006941 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006942{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006943 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006944}
6945
Jens Axboe09bb8392019-03-13 12:39:28 -06006946static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6947 int index)
6948{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006949 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006950
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006951 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006952}
6953
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006954static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006955{
6956 unsigned long file_ptr = (unsigned long) file;
6957
Pavel Begunkov88459b52021-10-17 00:07:10 +01006958 file_ptr |= io_file_get_flags(file);
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006959 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006960}
6961
Pavel Begunkovac177052021-08-09 13:04:02 +01006962static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6963 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006964{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006965 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006966 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006967
Pavel Begunkovac177052021-08-09 13:04:02 +01006968 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6969 return NULL;
6970 fd = array_index_nospec(fd, ctx->nr_user_files);
6971 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6972 file = (struct file *) (file_ptr & FFS_MASK);
6973 file_ptr &= ~FFS_MASK;
6974 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01006975 req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
Pavel Begunkova46be972021-10-09 23:14:40 +01006976 io_req_set_rsrc_node(req, ctx);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006977 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006978}
6979
Pavel Begunkovac177052021-08-09 13:04:02 +01006980static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006981 struct io_kiocb *req, int fd)
6982{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006983 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006984
6985 trace_io_uring_file_get(ctx, fd);
6986
6987 /* we don't allow fixed io_uring files */
6988 if (file && unlikely(file->f_op == &io_uring_fops))
6989 io_req_track_inflight(req);
6990 return file;
6991}
6992
6993static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006994 struct io_kiocb *req, int fd, bool fixed)
6995{
6996 if (fixed)
6997 return io_file_get_fixed(ctx, req, fd);
6998 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006999 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01007000}
7001
Pavel Begunkovf237c302021-08-18 12:42:46 +01007002static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06007003{
7004 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov617a8942021-11-26 14:38:14 +00007005 int ret = -ENOENT;
Jens Axboe89b263f2021-08-10 15:14:18 -06007006
7007 if (prev) {
Pavel Begunkov617a8942021-11-26 14:38:14 +00007008 if (!(req->task->flags & PF_EXITING))
7009 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01007010 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06007011 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06007012 } else {
7013 io_req_complete_post(req, -ETIME, 0);
7014 }
7015}
7016
Jens Axboe2665abf2019-11-05 12:40:47 -07007017static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
7018{
Jens Axboead8a48a2019-11-15 08:49:11 -07007019 struct io_timeout_data *data = container_of(timer,
7020 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00007021 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07007022 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07007023 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07007024
Jens Axboe89b263f2021-08-10 15:14:18 -06007025 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00007026 prev = req->timeout.head;
7027 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07007028
7029 /*
7030 * We don't expect the list to be empty, that will only happen if we
7031 * race with the completion of the linked work.
7032 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01007033 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00007034 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01007035 if (!req_ref_inc_not_zero(prev))
7036 prev = NULL;
7037 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06007038 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06007039 req->timeout.prev = prev;
7040 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07007041
Jens Axboe89b263f2021-08-10 15:14:18 -06007042 req->io_task_work.func = io_req_task_link_timeout;
Hao Xu4813c372021-12-07 17:39:48 +08007043 io_req_task_work_add(req, false);
Jens Axboe2665abf2019-11-05 12:40:47 -07007044 return HRTIMER_NORESTART;
7045}
7046
Pavel Begunkovde968c12021-03-19 17:22:33 +00007047static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07007048{
Pavel Begunkovde968c12021-03-19 17:22:33 +00007049 struct io_ring_ctx *ctx = req->ctx;
7050
Jens Axboe89b263f2021-08-10 15:14:18 -06007051 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07007052 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00007053 * If the back reference is NULL, then our linked request finished
7054 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07007055 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00007056 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07007057 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07007058
Jens Axboead8a48a2019-11-15 08:49:11 -07007059 data->timer.function = io_link_timeout_fn;
7060 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
7061 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06007062 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07007063 }
Jens Axboe89b263f2021-08-10 15:14:18 -06007064 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07007065 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07007066 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07007067}
7068
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01007069static void io_queue_sqe_arm_apoll(struct io_kiocb *req)
7070 __must_hold(&req->ctx->uring_lock)
7071{
7072 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
7073
7074 switch (io_arm_poll_handler(req)) {
7075 case IO_APOLL_READY:
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01007076 io_req_task_queue(req);
7077 break;
7078 case IO_APOLL_ABORTED:
7079 /*
7080 * Queued up for async execution, worker will release
7081 * submit reference when the iocb is actually submitted.
7082 */
7083 io_queue_async_work(req, NULL);
7084 break;
7085 }
7086
7087 if (linked_timeout)
7088 io_queue_linked_timeout(linked_timeout);
7089}
7090
7091static inline void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007092 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007093{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01007094 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06007095 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007096
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00007097 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06007098
Pavel Begunkovfff4e402021-10-04 20:02:48 +01007099 if (req->flags & REQ_F_COMPLETE_INLINE) {
7100 io_req_add_compl_list(req);
Pavel Begunkovd9f9d282021-09-24 22:00:00 +01007101 return;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01007102 }
Jens Axboe491381ce2019-10-17 09:20:46 -06007103 /*
7104 * We async punt it if the file wasn't marked NOWAIT, or if the file
7105 * doesn't support non-blocking read/write attempts
7106 */
Pavel Begunkov18400382021-03-19 17:22:34 +00007107 if (likely(!ret)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01007108 linked_timeout = io_prep_linked_timeout(req);
7109 if (linked_timeout)
7110 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00007111 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01007112 io_queue_sqe_arm_apoll(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01007113 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00007114 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06007115 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007116}
7117
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007118static void io_queue_sqe_fallback(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007119 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08007120{
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007121 if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01007122 io_req_complete_fail_submit(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01007123 } else if (unlikely(req->ctx->drain_active)) {
7124 io_drain_req(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01007125 } else {
7126 int ret = io_req_prep_async(req);
7127
7128 if (unlikely(ret))
7129 io_req_complete_failed(req, ret);
7130 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01007131 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07007132 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007133}
7134
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007135static inline void io_queue_sqe(struct io_kiocb *req)
7136 __must_hold(&req->ctx->uring_lock)
7137{
7138 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))))
7139 __io_queue_sqe(req);
7140 else
7141 io_queue_sqe_fallback(req);
7142}
7143
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007144/*
7145 * Check SQE restrictions (opcode and flags).
7146 *
7147 * Returns 'true' if SQE is allowed, 'false' otherwise.
7148 */
7149static inline bool io_check_restriction(struct io_ring_ctx *ctx,
7150 struct io_kiocb *req,
7151 unsigned int sqe_flags)
7152{
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007153 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
7154 return false;
7155
7156 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
7157 ctx->restrictions.sqe_flags_required)
7158 return false;
7159
7160 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
7161 ctx->restrictions.sqe_flags_required))
7162 return false;
7163
7164 return true;
7165}
7166
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007167static void io_init_req_drain(struct io_kiocb *req)
7168{
7169 struct io_ring_ctx *ctx = req->ctx;
7170 struct io_kiocb *head = ctx->submit_state.link.head;
7171
7172 ctx->drain_active = true;
7173 if (head) {
7174 /*
7175 * If we need to drain a request in the middle of a link, drain
7176 * the head request and the next request/link after the current
7177 * link. Considering sequential execution of links,
Hao Xub6c7db32021-11-25 17:21:03 +08007178 * REQ_F_IO_DRAIN will be maintained for every request of our
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007179 * link.
7180 */
Hao Xub6c7db32021-11-25 17:21:03 +08007181 head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007182 ctx->drain_next = true;
7183 }
7184}
7185
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007186static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007187 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007188 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007189{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007190 unsigned int sqe_flags;
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007191 int personality;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007192 u8 opcode;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007193
Pavel Begunkov864ea922021-08-09 13:04:08 +01007194 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007195 req->opcode = opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007196 /* same numerical values with corresponding REQ_F_*, safe to copy */
7197 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007198 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007199 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007200 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03007201 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007202
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007203 if (unlikely(opcode >= IORING_OP_LAST)) {
7204 req->opcode = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007205 return -EINVAL;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007206 }
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007207 if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
7208 /* enforce forwards compatibility on users */
7209 if (sqe_flags & ~SQE_VALID_FLAGS)
7210 return -EINVAL;
7211 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007212 !io_op_defs[opcode].buffer_select)
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007213 return -EOPNOTSUPP;
Pavel Begunkov5562a8d2021-11-10 15:49:34 +00007214 if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
7215 ctx->drain_disabled = true;
7216 if (sqe_flags & IOSQE_IO_DRAIN) {
7217 if (ctx->drain_disabled)
7218 return -EOPNOTSUPP;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007219 io_init_req_drain(req);
Pavel Begunkov5562a8d2021-11-10 15:49:34 +00007220 }
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007221 }
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007222 if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) {
7223 if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags))
7224 return -EACCES;
7225 /* knock it to the slow queue path, will be drained there */
7226 if (ctx->drain_active)
7227 req->flags |= REQ_F_FORCE_ASYNC;
7228 /* if there is no link, we're at "next" request and need to drain */
7229 if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
7230 ctx->drain_next = false;
7231 ctx->drain_active = true;
Hao Xub6c7db32021-11-25 17:21:03 +08007232 req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007233 }
7234 }
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007235
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007236 if (io_op_defs[opcode].needs_file) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007237 struct io_submit_state *state = &ctx->submit_state;
7238
7239 /*
7240 * Plug now if we have more than 2 IO left after this, and the
7241 * target is potentially a read/write to block based storage.
7242 */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007243 if (state->need_plug && io_op_defs[opcode].plug) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007244 state->plug_started = true;
7245 state->need_plug = false;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -06007246 blk_start_plug_nr_ios(&state->plug, state->submit_nr);
Pavel Begunkov6d634162021-10-06 16:06:46 +01007247 }
7248
Pavel Begunkov62906e82021-08-10 14:52:47 +01007249 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007250 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007251 if (unlikely(!req->file))
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007252 return -EBADF;
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007253 }
Pavel Begunkovc11368a52020-05-17 14:13:42 +03007254
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007255 personality = READ_ONCE(sqe->personality);
Jens Axboe63ff8222020-05-07 14:56:15 -06007256 if (personality) {
Linus Torvaldscdab10b2021-11-01 21:06:18 -07007257 int ret;
7258
Jens Axboe63ff8222020-05-07 14:56:15 -06007259 req->creds = xa_load(&ctx->personalities, personality);
7260 if (!req->creds)
Jens Axboe27926b62020-10-28 09:33:23 -06007261 return -EINVAL;
Jens Axboe63ff8222020-05-07 14:56:15 -06007262 get_cred(req->creds);
Paul Moorecdc14042021-02-01 19:56:49 -05007263 ret = security_uring_override_creds(req->creds);
7264 if (ret) {
7265 put_cred(req->creds);
7266 return ret;
7267 }
Jens Axboe63ff8222020-05-07 14:56:15 -06007268 req->flags |= REQ_F_CREDS;
7269 }
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007270
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007271 return io_req_prep(req, sqe);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007272}
7273
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007274static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007275 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007276 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007277{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007278 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007279 int ret;
7280
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007281 ret = io_init_req(ctx, req, sqe);
7282 if (unlikely(ret)) {
Jens Axboea87acfd2021-09-11 16:04:50 -06007283 trace_io_uring_req_failed(sqe, ret);
7284
Hao Xua8295b92021-08-27 17:46:09 +08007285 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007286 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007287 /*
7288 * we can judge a link req is failed or cancelled by if
7289 * REQ_F_FAIL is set, but the head is an exception since
7290 * it may be set REQ_F_FAIL because of other req's failure
7291 * so let's leverage req->result to distinguish if a head
7292 * is set REQ_F_FAIL because of its failure or other req's
7293 * failure so that we can set the correct ret code for it.
7294 * init result here to avoid affecting the normal path.
7295 */
7296 if (!(link->head->flags & REQ_F_FAIL))
7297 req_fail_link_node(link->head, -ECANCELED);
7298 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7299 /*
7300 * the current req is a normal req, we should return
7301 * error and thus break the submittion loop.
7302 */
7303 io_req_complete_failed(req, ret);
7304 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007305 }
Hao Xua8295b92021-08-27 17:46:09 +08007306 req_fail_link_node(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007307 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007308
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007309 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007310 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7311 req->flags, true,
7312 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007313
Jens Axboe6c271ce2019-01-10 11:22:30 -07007314 /*
7315 * If we already have a head request, queue this one for async
7316 * submittal once the head completes. If we don't have a head but
7317 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7318 * submitted sync once the chain is complete. If none of those
7319 * conditions are true (normal request), then just queue it.
7320 */
7321 if (link->head) {
7322 struct io_kiocb *head = link->head;
7323
Hao Xua8295b92021-08-27 17:46:09 +08007324 if (!(req->flags & REQ_F_FAIL)) {
7325 ret = io_req_prep_async(req);
7326 if (unlikely(ret)) {
7327 req_fail_link_node(req, ret);
7328 if (!(head->flags & REQ_F_FAIL))
7329 req_fail_link_node(head, -ECANCELED);
7330 }
7331 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007332 trace_io_uring_link(ctx, req, head);
7333 link->last->link = req;
7334 link->last = req;
7335
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007336 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK))
7337 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007338 /* last request of a link, enqueue the link */
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007339 link->head = NULL;
7340 req = head;
7341 } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
7342 link->head = req;
7343 link->last = req;
7344 return 0;
Jackie Liu4fe2c962019-09-09 20:50:40 +08007345 }
7346
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007347 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007348 return 0;
7349}
7350
7351/*
7352 * Batched submission is done, ensure local IO is flushed out.
7353 */
Pavel Begunkov553deff2021-09-24 21:59:55 +01007354static void io_submit_state_end(struct io_ring_ctx *ctx)
Jens Axboe3529d8c2019-12-19 18:24:38 -07007355{
Pavel Begunkov553deff2021-09-24 21:59:55 +01007356 struct io_submit_state *state = &ctx->submit_state;
7357
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007358 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007359 io_queue_sqe(state->link.head);
Pavel Begunkov553deff2021-09-24 21:59:55 +01007360 /* flush only after queuing links as they can generate completions */
Pavel Begunkovc4501782021-09-08 16:40:52 +01007361 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007362 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007363 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007364}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007365
Jens Axboe9e645e112019-05-10 16:07:28 -06007366/*
7367 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007368 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007369static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007370 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007371{
7372 state->plug_started = false;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007373 state->need_plug = max_ios > 2;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -06007374 state->submit_nr = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007375 /* set only head, no need to init link_last in advance */
7376 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007377}
7378
Jens Axboe193155c2020-02-22 23:22:19 -07007379static void io_commit_sqring(struct io_ring_ctx *ctx)
7380{
Jens Axboe75c6a032020-01-28 10:15:23 -07007381 struct io_rings *rings = ctx->rings;
7382
7383 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007384 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007385 * since once we write the new head, the application could
7386 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007387 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007388 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007389}
7390
Jens Axboe9e645e112019-05-10 16:07:28 -06007391/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007392 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007393 * that is mapped by userspace. This means that care needs to be taken to
7394 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007395 * being a good citizen. If members of the sqe are validated and then later
7396 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007397 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007398 */
7399static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007400{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007401 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007402 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007403
7404 /*
7405 * The cached sq head (or cq tail) serves two purposes:
7406 *
7407 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007408 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007409 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007410 * though the application is the one updating it.
7411 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007412 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007413 if (likely(head < ctx->sq_entries))
7414 return &ctx->sq_sqes[head];
7415
7416 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007417 ctx->cq_extra--;
7418 WRITE_ONCE(ctx->rings->sq_dropped,
7419 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007420 return NULL;
7421}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007422
Jens Axboe0f212202020-09-13 13:09:39 -06007423static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007424 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007425{
Pavel Begunkov69629802021-09-24 22:00:01 +01007426 unsigned int entries = io_sqring_entries(ctx);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007427 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007428
Pavel Begunkov51d48da2021-10-04 20:02:47 +01007429 if (unlikely(!entries))
Pavel Begunkov69629802021-09-24 22:00:01 +01007430 return 0;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03007431 /* make sure SQ entry isn't read before tail */
Pavel Begunkov69629802021-09-24 22:00:01 +01007432 nr = min3(nr, ctx->sq_entries, entries);
Pavel Begunkov9a108672021-08-27 11:55:01 +01007433 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007434
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007435 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov69629802021-09-24 22:00:01 +01007436 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007437 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007438 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007439
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007440 if (unlikely(!io_alloc_req_refill(ctx))) {
Pavel Begunkov196be952019-11-07 01:41:06 +03007441 if (!submitted)
7442 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007443 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007444 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007445 req = io_alloc_req(ctx);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007446 sqe = io_get_sqe(ctx);
7447 if (unlikely(!sqe)) {
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01007448 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007449 break;
7450 }
Jens Axboed3656342019-12-18 09:50:26 -07007451 /* will complete beyond this point, count as submitted */
7452 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007453 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007454 break;
Pavel Begunkov69629802021-09-24 22:00:01 +01007455 } while (submitted < nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007456
Pavel Begunkov9466f432020-01-25 22:34:01 +03007457 if (unlikely(submitted != nr)) {
7458 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007459 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007460
Pavel Begunkov09899b12021-06-14 02:36:22 +01007461 current->io_uring->cached_refs += unused;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007462 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007463
Pavel Begunkov553deff2021-09-24 21:59:55 +01007464 io_submit_state_end(ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007465 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7466 io_commit_sqring(ctx);
7467
Jens Axboe6c271ce2019-01-10 11:22:30 -07007468 return submitted;
7469}
7470
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007471static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7472{
7473 return READ_ONCE(sqd->state);
7474}
7475
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007476static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7477{
7478 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007479 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007480 WRITE_ONCE(ctx->rings->sq_flags,
7481 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007482 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007483}
7484
7485static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7486{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007487 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007488 WRITE_ONCE(ctx->rings->sq_flags,
7489 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007490 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007491}
7492
Xiaoguang Wang08369242020-11-03 14:15:59 +08007493static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007494{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007495 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007496 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007497
Jens Axboec8d1ba52020-09-14 11:07:26 -06007498 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007499 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007500 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7501 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007502
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007503 if (!wq_list_empty(&ctx->iopoll_list) || to_submit) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007504 const struct cred *creds = NULL;
7505
7506 if (ctx->sq_creds != current_cred())
7507 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007508
Xiaoguang Wang08369242020-11-03 14:15:59 +08007509 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007510 if (!wq_list_empty(&ctx->iopoll_list))
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01007511 io_do_iopoll(ctx, true);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007512
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007513 /*
7514 * Don't submit if refs are dying, good for io_uring_register(),
7515 * but also it is relied upon by io_ring_exit_work()
7516 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007517 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7518 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007519 ret = io_submit_sqes(ctx, to_submit);
7520 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007521
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007522 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7523 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007524 if (creds)
7525 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007526 }
Jens Axboe90554202020-09-03 12:12:41 -06007527
Xiaoguang Wang08369242020-11-03 14:15:59 +08007528 return ret;
7529}
7530
Pavel Begunkovc0724812021-10-04 20:02:54 +01007531static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd)
Xiaoguang Wang08369242020-11-03 14:15:59 +08007532{
7533 struct io_ring_ctx *ctx;
7534 unsigned sq_thread_idle = 0;
7535
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007536 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7537 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007538 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007539}
7540
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007541static bool io_sqd_handle_event(struct io_sq_data *sqd)
7542{
7543 bool did_sig = false;
7544 struct ksignal ksig;
7545
7546 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7547 signal_pending(current)) {
7548 mutex_unlock(&sqd->lock);
7549 if (signal_pending(current))
7550 did_sig = get_signal(&ksig);
7551 cond_resched();
7552 mutex_lock(&sqd->lock);
7553 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007554 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7555}
7556
Jens Axboe6c271ce2019-01-10 11:22:30 -07007557static int io_sq_thread(void *data)
7558{
Jens Axboe69fb2132020-09-14 11:16:23 -06007559 struct io_sq_data *sqd = data;
7560 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007561 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007562 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007563 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007564
Pavel Begunkov696ee882021-04-01 09:55:04 +01007565 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007566 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007567
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007568 if (sqd->sq_cpu != -1)
7569 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7570 else
7571 set_cpus_allowed_ptr(current, cpu_online_mask);
7572 current->flags |= PF_NO_SETAFFINITY;
7573
Paul Moore5bd21822021-02-16 19:46:48 -05007574 audit_alloc_kernel(current);
7575
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007576 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007577 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007578 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007579
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007580 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7581 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007582 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007583 timeout = jiffies + sqd->sq_thread_idle;
7584 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007585
Jens Axboee95eee22020-09-08 09:11:32 -06007586 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007587 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007588 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007589
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007590 if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007591 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007592 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007593 if (io_run_task_work())
7594 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007595
Xiaoguang Wang08369242020-11-03 14:15:59 +08007596 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007597 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007598 if (sqt_spin)
7599 timeout = jiffies + sqd->sq_thread_idle;
7600 continue;
7601 }
7602
Xiaoguang Wang08369242020-11-03 14:15:59 +08007603 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007604 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007605 bool needs_sched = true;
7606
Hao Xu724cb4f2021-04-21 23:19:11 +08007607 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007608 io_ring_set_wakeup_flag(ctx);
7609
Hao Xu724cb4f2021-04-21 23:19:11 +08007610 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007611 !wq_list_empty(&ctx->iopoll_list)) {
Hao Xu724cb4f2021-04-21 23:19:11 +08007612 needs_sched = false;
7613 break;
7614 }
7615 if (io_sqring_entries(ctx)) {
7616 needs_sched = false;
7617 break;
7618 }
7619 }
7620
7621 if (needs_sched) {
7622 mutex_unlock(&sqd->lock);
7623 schedule();
7624 mutex_lock(&sqd->lock);
7625 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007626 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7627 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007628 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007629
7630 finish_wait(&sqd->wait, &wait);
7631 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007632 }
7633
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007634 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007635 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007636 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007637 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007638 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007639 mutex_unlock(&sqd->lock);
7640
Paul Moore5bd21822021-02-16 19:46:48 -05007641 audit_free(current);
7642
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007643 complete(&sqd->exited);
7644 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007645}
7646
Jens Axboebda52162019-09-24 13:47:15 -06007647struct io_wait_queue {
7648 struct wait_queue_entry wq;
7649 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007650 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007651 unsigned nr_timeouts;
7652};
7653
Pavel Begunkov6c503152021-01-04 20:36:36 +00007654static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007655{
7656 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007657 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007658
7659 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007660 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007661 * started waiting. For timeouts, we always want to return to userspace,
7662 * regardless of event count.
7663 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007664 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007665}
7666
7667static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7668 int wake_flags, void *key)
7669{
7670 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7671 wq);
7672
Pavel Begunkov6c503152021-01-04 20:36:36 +00007673 /*
7674 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7675 * the task, and the next invocation will do it.
7676 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007677 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007678 return autoremove_wake_function(curr, mode, wake_flags, key);
7679 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007680}
7681
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007682static int io_run_task_work_sig(void)
7683{
7684 if (io_run_task_work())
7685 return 1;
7686 if (!signal_pending(current))
7687 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007688 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007689 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007690 return -EINTR;
7691}
7692
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007693/* when returns >0, the caller should retry */
7694static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7695 struct io_wait_queue *iowq,
7696 signed long *timeout)
7697{
7698 int ret;
7699
7700 /* make sure we run task_work before checking for signals */
7701 ret = io_run_task_work_sig();
7702 if (ret || io_should_wake(iowq))
7703 return ret;
7704 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007705 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007706 return 1;
7707
7708 *timeout = schedule_timeout(*timeout);
7709 return !*timeout ? -ETIME : 1;
7710}
7711
Jens Axboe2b188cc2019-01-07 10:46:33 -07007712/*
7713 * Wait until events become available, if we don't already have some. The
7714 * application must reap them itself, as they reside on the shared cq ring.
7715 */
7716static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007717 const sigset_t __user *sig, size_t sigsz,
7718 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007719{
Pavel Begunkov902910992021-08-09 09:07:32 -06007720 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007721 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007722 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7723 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007724
Jens Axboeb41e9852020-02-17 09:52:41 -07007725 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007726 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007727 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007728 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007729 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007730 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007731 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007732
Xiaoguang Wang44df58d2021-09-14 22:38:52 +08007733 if (uts) {
7734 struct timespec64 ts;
7735
7736 if (get_timespec64(&ts, uts))
7737 return -EFAULT;
7738 timeout = timespec64_to_jiffies(&ts);
7739 }
7740
Jens Axboe2b188cc2019-01-07 10:46:33 -07007741 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007742#ifdef CONFIG_COMPAT
7743 if (in_compat_syscall())
7744 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007745 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007746 else
7747#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007748 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007749
Jens Axboe2b188cc2019-01-07 10:46:33 -07007750 if (ret)
7751 return ret;
7752 }
7753
Pavel Begunkov902910992021-08-09 09:07:32 -06007754 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7755 iowq.wq.private = current;
7756 INIT_LIST_HEAD(&iowq.wq.entry);
7757 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007758 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007759 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007760
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007761 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007762 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007763 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007764 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007765 ret = -EBUSY;
7766 break;
7767 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007768 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007769 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007770 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007771 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007772 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007773 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007774
Jens Axboeb7db41c2020-07-04 08:55:50 -06007775 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007776
Hristo Venev75b28af2019-08-26 17:23:46 +00007777 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007778}
7779
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007780static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007781{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007782 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007783
7784 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007785 kfree(table[i]);
7786 kfree(table);
7787}
7788
Pavel Begunkovc0724812021-10-04 20:02:54 +01007789static __cold void **io_alloc_page_table(size_t size)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007790{
7791 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7792 size_t init_size = size;
7793 void **table;
7794
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007795 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007796 if (!table)
7797 return NULL;
7798
7799 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007800 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007801
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007802 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007803 if (!table[i]) {
7804 io_free_page_table(table, init_size);
7805 return NULL;
7806 }
7807 size -= this_size;
7808 }
7809 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007810}
7811
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007812static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7813{
7814 percpu_ref_exit(&ref_node->refs);
7815 kfree(ref_node);
7816}
7817
Pavel Begunkovc0724812021-10-04 20:02:54 +01007818static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007819{
7820 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7821 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7822 unsigned long flags;
7823 bool first_add = false;
Dylan Yudakenb36a2052022-01-21 04:38:56 -08007824 unsigned long delay = HZ;
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007825
7826 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7827 node->done = true;
7828
Dylan Yudakenb36a2052022-01-21 04:38:56 -08007829 /* if we are mid-quiesce then do not delay */
7830 if (node->rsrc_data->quiesce)
7831 delay = 0;
7832
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007833 while (!list_empty(&ctx->rsrc_ref_list)) {
7834 node = list_first_entry(&ctx->rsrc_ref_list,
7835 struct io_rsrc_node, node);
7836 /* recycle ref nodes in order */
7837 if (!node->done)
7838 break;
7839 list_del(&node->node);
7840 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7841 }
7842 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7843
7844 if (first_add)
Dylan Yudakenb36a2052022-01-21 04:38:56 -08007845 mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007846}
7847
Usama Ariff6133fb2022-01-27 14:04:44 +00007848static struct io_rsrc_node *io_rsrc_node_alloc(void)
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007849{
7850 struct io_rsrc_node *ref_node;
7851
7852 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7853 if (!ref_node)
7854 return NULL;
7855
7856 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7857 0, GFP_KERNEL)) {
7858 kfree(ref_node);
7859 return NULL;
7860 }
7861 INIT_LIST_HEAD(&ref_node->node);
7862 INIT_LIST_HEAD(&ref_node->rsrc_list);
7863 ref_node->done = false;
7864 return ref_node;
7865}
7866
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007867static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7868 struct io_rsrc_data *data_to_kill)
Pavel Begunkovab409402021-10-09 23:14:41 +01007869 __must_hold(&ctx->uring_lock)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007870{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007871 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7872 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007873
Pavel Begunkovab409402021-10-09 23:14:41 +01007874 io_rsrc_refs_drop(ctx);
7875
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007876 if (data_to_kill) {
7877 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007878
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007879 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007880 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007881 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007882 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007883
Pavel Begunkov3e942492021-04-11 01:46:34 +01007884 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007885 percpu_ref_kill(&rsrc_node->refs);
7886 ctx->rsrc_node = NULL;
7887 }
7888
7889 if (!ctx->rsrc_node) {
7890 ctx->rsrc_node = ctx->rsrc_backup_node;
7891 ctx->rsrc_backup_node = NULL;
7892 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007893}
7894
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007895static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007896{
7897 if (ctx->rsrc_backup_node)
7898 return 0;
Usama Ariff6133fb2022-01-27 14:04:44 +00007899 ctx->rsrc_backup_node = io_rsrc_node_alloc();
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007900 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7901}
7902
Pavel Begunkovc0724812021-10-04 20:02:54 +01007903static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
7904 struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007905{
7906 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007907
Pavel Begunkov215c3902021-04-01 15:43:48 +01007908 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007909 if (data->quiesce)
7910 return -ENXIO;
7911
7912 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007913 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007914 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007915 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007916 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007917 io_rsrc_node_switch(ctx, data);
7918
Pavel Begunkov3e942492021-04-11 01:46:34 +01007919 /* kill initial ref, already quiesced if zero */
7920 if (atomic_dec_and_test(&data->refs))
7921 break;
Jens Axboec018db42021-08-09 08:15:50 -06007922 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007923 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007924 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007925 if (!ret) {
7926 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007927 break;
Jens Axboec018db42021-08-09 08:15:50 -06007928 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007929
Pavel Begunkov3e942492021-04-11 01:46:34 +01007930 atomic_inc(&data->refs);
7931 /* wait for all works potentially completing data->done */
7932 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007933 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007934
Hao Xu8bad28d2021-02-19 17:19:36 +08007935 ret = io_run_task_work_sig();
7936 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007937 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007938 data->quiesce = false;
7939
Hao Xu8bad28d2021-02-19 17:19:36 +08007940 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007941}
7942
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007943static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7944{
7945 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7946 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7947
7948 return &data->tags[table_idx][off];
7949}
7950
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007951static void io_rsrc_data_free(struct io_rsrc_data *data)
7952{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007953 size_t size = data->nr * sizeof(data->tags[0][0]);
7954
7955 if (data->tags)
7956 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007957 kfree(data);
7958}
7959
Pavel Begunkovc0724812021-10-04 20:02:54 +01007960static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7961 u64 __user *utags, unsigned nr,
7962 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007963{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007964 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007965 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007966 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007967
7968 data = kzalloc(sizeof(*data), GFP_KERNEL);
7969 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007970 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007971 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007972 if (!data->tags) {
7973 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007974 return -ENOMEM;
7975 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007976
7977 data->nr = nr;
7978 data->ctx = ctx;
7979 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007980 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007981 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007982 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007983 u64 *tag_slot = io_get_tag_slot(data, i);
7984
7985 if (copy_from_user(tag_slot, &utags[i],
7986 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007987 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007988 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007989 }
7990
Pavel Begunkov3e942492021-04-11 01:46:34 +01007991 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007992 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007993 *pdata = data;
7994 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007995fail:
7996 io_rsrc_data_free(data);
7997 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007998}
7999
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01008000static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
8001{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01008002 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
8003 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01008004 return !!table->files;
8005}
8006
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008007static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01008008{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008009 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01008010 table->files = NULL;
8011}
8012
Jens Axboe2b188cc2019-01-07 10:46:33 -07008013static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
8014{
8015#if defined(CONFIG_UNIX)
8016 if (ctx->ring_sock) {
8017 struct sock *sock = ctx->ring_sock->sk;
8018 struct sk_buff *skb;
8019
8020 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
8021 kfree_skb(skb);
8022 }
8023#else
8024 int i;
8025
8026 for (i = 0; i < ctx->nr_user_files; i++) {
8027 struct file *file;
8028
8029 file = io_file_from_index(ctx, i);
8030 if (file)
8031 fput(file);
8032 }
8033#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008034 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008035 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01008036 ctx->file_data = NULL;
8037 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00008038}
8039
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00008040static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
8041{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00008042 int ret;
8043
Pavel Begunkov08480402021-04-13 02:58:38 +01008044 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00008045 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01008046 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
8047 if (!ret)
8048 __io_sqe_files_unregister(ctx);
8049 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07008050}
8051
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008052static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008053 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008054{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008055 WARN_ON_ONCE(sqd->thread == current);
8056
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008057 /*
8058 * Do the dance but not conditional clear_bit() because it'd race with
8059 * other threads incrementing park_pending and setting the bit.
8060 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008061 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008062 if (atomic_dec_return(&sqd->park_pending))
8063 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008064 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008065}
8066
Jens Axboe86e0d672021-03-05 08:44:39 -07008067static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008068 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008069{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008070 WARN_ON_ONCE(sqd->thread == current);
8071
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008072 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008073 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008074 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07008075 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07008076 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008077}
8078
8079static void io_sq_thread_stop(struct io_sq_data *sqd)
8080{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008081 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01008082 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008083
Jens Axboe05962f92021-03-06 13:58:48 -07008084 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01008085 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07008086 if (sqd->thread)
8087 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008088 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07008089 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008090}
8091
Jens Axboe534ca6d2020-09-02 13:52:19 -06008092static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07008093{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008094 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008095 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
8096
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008097 io_sq_thread_stop(sqd);
8098 kfree(sqd);
8099 }
8100}
8101
8102static void io_sq_thread_finish(struct io_ring_ctx *ctx)
8103{
8104 struct io_sq_data *sqd = ctx->sq_data;
8105
8106 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07008107 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008108 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008109 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07008110 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008111
8112 io_put_sq_data(sqd);
8113 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008114 }
8115}
8116
Jens Axboeaa061652020-09-02 14:50:27 -06008117static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
8118{
8119 struct io_ring_ctx *ctx_attach;
8120 struct io_sq_data *sqd;
8121 struct fd f;
8122
8123 f = fdget(p->wq_fd);
8124 if (!f.file)
8125 return ERR_PTR(-ENXIO);
8126 if (f.file->f_op != &io_uring_fops) {
8127 fdput(f);
8128 return ERR_PTR(-EINVAL);
8129 }
8130
8131 ctx_attach = f.file->private_data;
8132 sqd = ctx_attach->sq_data;
8133 if (!sqd) {
8134 fdput(f);
8135 return ERR_PTR(-EINVAL);
8136 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07008137 if (sqd->task_tgid != current->tgid) {
8138 fdput(f);
8139 return ERR_PTR(-EPERM);
8140 }
Jens Axboeaa061652020-09-02 14:50:27 -06008141
8142 refcount_inc(&sqd->refs);
8143 fdput(f);
8144 return sqd;
8145}
8146
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008147static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
8148 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008149{
8150 struct io_sq_data *sqd;
8151
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008152 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008153 if (p->flags & IORING_SETUP_ATTACH_WQ) {
8154 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008155 if (!IS_ERR(sqd)) {
8156 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008157 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008158 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07008159 /* fall through for EPERM case, setup new sqd/task */
8160 if (PTR_ERR(sqd) != -EPERM)
8161 return sqd;
8162 }
Jens Axboeaa061652020-09-02 14:50:27 -06008163
Jens Axboe534ca6d2020-09-02 13:52:19 -06008164 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
8165 if (!sqd)
8166 return ERR_PTR(-ENOMEM);
8167
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008168 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008169 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06008170 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008171 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008172 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008173 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008174 return sqd;
8175}
8176
Jens Axboe6b063142019-01-10 22:13:58 -07008177#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07008178/*
8179 * Ensure the UNIX gc is aware of our file set, so we are certain that
8180 * the io_uring can be safely unregistered on process exit, even if we have
8181 * loops in the file referencing.
8182 */
8183static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
8184{
8185 struct sock *sk = ctx->ring_sock->sk;
8186 struct scm_fp_list *fpl;
8187 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06008188 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07008189
Jens Axboe6b063142019-01-10 22:13:58 -07008190 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
8191 if (!fpl)
8192 return -ENOMEM;
8193
8194 skb = alloc_skb(0, GFP_KERNEL);
8195 if (!skb) {
8196 kfree(fpl);
8197 return -ENOMEM;
8198 }
8199
8200 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07008201
Jens Axboe08a45172019-10-03 08:11:03 -06008202 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07008203 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07008204 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008205 struct file *file = io_file_from_index(ctx, i + offset);
8206
8207 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06008208 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06008209 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06008210 unix_inflight(fpl->user, fpl->fp[nr_files]);
8211 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07008212 }
8213
Jens Axboe08a45172019-10-03 08:11:03 -06008214 if (nr_files) {
8215 fpl->max = SCM_MAX_FD;
8216 fpl->count = nr_files;
8217 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008218 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06008219 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
8220 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07008221
Jens Axboe08a45172019-10-03 08:11:03 -06008222 for (i = 0; i < nr_files; i++)
8223 fput(fpl->fp[i]);
8224 } else {
8225 kfree_skb(skb);
8226 kfree(fpl);
8227 }
Jens Axboe6b063142019-01-10 22:13:58 -07008228
8229 return 0;
8230}
8231
8232/*
8233 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8234 * causes regular reference counting to break down. We rely on the UNIX
8235 * garbage collection to take care of this problem for us.
8236 */
8237static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8238{
8239 unsigned left, total;
8240 int ret = 0;
8241
8242 total = 0;
8243 left = ctx->nr_user_files;
8244 while (left) {
8245 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008246
8247 ret = __io_sqe_files_scm(ctx, this_files, total);
8248 if (ret)
8249 break;
8250 left -= this_files;
8251 total += this_files;
8252 }
8253
8254 if (!ret)
8255 return 0;
8256
8257 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008258 struct file *file = io_file_from_index(ctx, total);
8259
8260 if (file)
8261 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008262 total++;
8263 }
8264
8265 return ret;
8266}
8267#else
8268static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8269{
8270 return 0;
8271}
8272#endif
8273
Pavel Begunkov47e90392021-04-01 15:43:56 +01008274static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008275{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008276 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008277#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008278 struct sock *sock = ctx->ring_sock->sk;
8279 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8280 struct sk_buff *skb;
8281 int i;
8282
8283 __skb_queue_head_init(&list);
8284
8285 /*
8286 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8287 * remove this entry and rearrange the file array.
8288 */
8289 skb = skb_dequeue(head);
8290 while (skb) {
8291 struct scm_fp_list *fp;
8292
8293 fp = UNIXCB(skb).fp;
8294 for (i = 0; i < fp->count; i++) {
8295 int left;
8296
8297 if (fp->fp[i] != file)
8298 continue;
8299
8300 unix_notinflight(fp->user, fp->fp[i]);
8301 left = fp->count - 1 - i;
8302 if (left) {
8303 memmove(&fp->fp[i], &fp->fp[i + 1],
8304 left * sizeof(struct file *));
8305 }
8306 fp->count--;
8307 if (!fp->count) {
8308 kfree_skb(skb);
8309 skb = NULL;
8310 } else {
8311 __skb_queue_tail(&list, skb);
8312 }
8313 fput(file);
8314 file = NULL;
8315 break;
8316 }
8317
8318 if (!file)
8319 break;
8320
8321 __skb_queue_tail(&list, skb);
8322
8323 skb = skb_dequeue(head);
8324 }
8325
8326 if (skb_peek(&list)) {
8327 spin_lock_irq(&head->lock);
8328 while ((skb = __skb_dequeue(&list)) != NULL)
8329 __skb_queue_tail(head, skb);
8330 spin_unlock_irq(&head->lock);
8331 }
8332#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008333 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008334#endif
8335}
8336
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008337static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008338{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008339 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008340 struct io_ring_ctx *ctx = rsrc_data->ctx;
8341 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008342
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008343 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8344 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008345
8346 if (prsrc->tag) {
8347 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008348
8349 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008350 spin_lock(&ctx->completion_lock);
Pavel Begunkov913a5712021-11-10 15:49:31 +00008351 io_fill_cqe_aux(ctx, prsrc->tag, 0, 0);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008352 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008353 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008354 io_cqring_ev_posted(ctx);
8355 io_ring_submit_unlock(ctx, lock_ring);
8356 }
8357
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008358 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008359 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008360 }
8361
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008362 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008363 if (atomic_dec_and_test(&rsrc_data->refs))
8364 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008365}
8366
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008367static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008368{
8369 struct io_ring_ctx *ctx;
8370 struct llist_node *node;
8371
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008372 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8373 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008374
8375 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008376 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008377 struct llist_node *next = node->next;
8378
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008379 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008380 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008381 node = next;
8382 }
8383}
8384
Jens Axboe05f3fb32019-12-09 11:22:50 -07008385static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008386 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008387{
8388 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008389 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008390 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008391 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008392
8393 if (ctx->file_data)
8394 return -EBUSY;
8395 if (!nr_args)
8396 return -EINVAL;
8397 if (nr_args > IORING_MAX_FIXED_FILES)
8398 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008399 if (nr_args > rlimit(RLIMIT_NOFILE))
8400 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008401 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008402 if (ret)
8403 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008404 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8405 &ctx->file_data);
8406 if (ret)
8407 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008408
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008409 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008410 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008411 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008412
Jens Axboe05f3fb32019-12-09 11:22:50 -07008413 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008414 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008415 ret = -EFAULT;
8416 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008417 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008418 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008419 if (fd == -1) {
8420 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008421 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008422 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008423 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008424 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008425
Jens Axboe05f3fb32019-12-09 11:22:50 -07008426 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008427 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008428 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008429 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008430
8431 /*
8432 * Don't allow io_uring instances to be registered. If UNIX
8433 * isn't enabled, then this causes a reference cycle and this
8434 * instance can never get freed. If UNIX is enabled we'll
8435 * handle it just fine, but there's still no point in allowing
8436 * a ring fd as it doesn't support regular read/write anyway.
8437 */
8438 if (file->f_op == &io_uring_fops) {
8439 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008440 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008441 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008442 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008443 }
8444
Jens Axboe05f3fb32019-12-09 11:22:50 -07008445 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008446 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008447 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008448 return ret;
8449 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008450
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008451 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008452 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008453out_fput:
8454 for (i = 0; i < ctx->nr_user_files; i++) {
8455 file = io_file_from_index(ctx, i);
8456 if (file)
8457 fput(file);
8458 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008459 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008460 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008461out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008462 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008463 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008464 return ret;
8465}
8466
Jens Axboec3a31e62019-10-03 13:59:56 -06008467static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8468 int index)
8469{
8470#if defined(CONFIG_UNIX)
8471 struct sock *sock = ctx->ring_sock->sk;
8472 struct sk_buff_head *head = &sock->sk_receive_queue;
8473 struct sk_buff *skb;
8474
8475 /*
8476 * See if we can merge this file into an existing skb SCM_RIGHTS
8477 * file set. If there's no room, fall back to allocating a new skb
8478 * and filling it in.
8479 */
8480 spin_lock_irq(&head->lock);
8481 skb = skb_peek(head);
8482 if (skb) {
8483 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8484
8485 if (fpl->count < SCM_MAX_FD) {
8486 __skb_unlink(skb, head);
8487 spin_unlock_irq(&head->lock);
8488 fpl->fp[fpl->count] = get_file(file);
8489 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8490 fpl->count++;
8491 spin_lock_irq(&head->lock);
8492 __skb_queue_head(head, skb);
8493 } else {
8494 skb = NULL;
8495 }
8496 }
8497 spin_unlock_irq(&head->lock);
8498
8499 if (skb) {
8500 fput(file);
8501 return 0;
8502 }
8503
8504 return __io_sqe_files_scm(ctx, 1, index);
8505#else
8506 return 0;
8507#endif
8508}
8509
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008510static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
8511 struct io_rsrc_node *node, void *rsrc)
8512{
8513 struct io_rsrc_put *prsrc;
8514
8515 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8516 if (!prsrc)
8517 return -ENOMEM;
8518
8519 prsrc->tag = *io_get_tag_slot(data, idx);
8520 prsrc->rsrc = rsrc;
8521 list_add(&prsrc->list, &node->rsrc_list);
8522 return 0;
8523}
8524
Pavel Begunkovb9445592021-08-25 12:25:45 +01008525static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8526 unsigned int issue_flags, u32 slot_index)
8527{
8528 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08008529 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008530 bool needs_switch = false;
Pavel Begunkovb9445592021-08-25 12:25:45 +01008531 struct io_fixed_file *file_slot;
8532 int ret = -EBADF;
8533
Hao Xu3b44b372021-10-18 21:34:31 +08008534 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008535 if (file->f_op == &io_uring_fops)
8536 goto err;
8537 ret = -ENXIO;
8538 if (!ctx->file_data)
8539 goto err;
8540 ret = -EINVAL;
8541 if (slot_index >= ctx->nr_user_files)
8542 goto err;
8543
8544 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8545 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008546
8547 if (file_slot->file_ptr) {
8548 struct file *old_file;
8549
8550 ret = io_rsrc_node_switch_start(ctx);
8551 if (ret)
8552 goto err;
8553
8554 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8555 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
8556 ctx->rsrc_node, old_file);
8557 if (ret)
8558 goto err;
8559 file_slot->file_ptr = 0;
8560 needs_switch = true;
8561 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01008562
8563 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8564 io_fixed_file_set(file_slot, file);
8565 ret = io_sqe_file_register(ctx, file, slot_index);
8566 if (ret) {
8567 file_slot->file_ptr = 0;
8568 goto err;
8569 }
8570
8571 ret = 0;
8572err:
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008573 if (needs_switch)
8574 io_rsrc_node_switch(ctx, ctx->file_data);
Hao Xu3b44b372021-10-18 21:34:31 +08008575 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008576 if (ret)
8577 fput(file);
8578 return ret;
8579}
8580
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008581static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
8582{
8583 unsigned int offset = req->close.file_slot - 1;
8584 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08008585 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008586 struct io_fixed_file *file_slot;
8587 struct file *file;
8588 int ret, i;
8589
Hao Xu3b44b372021-10-18 21:34:31 +08008590 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008591 ret = -ENXIO;
8592 if (unlikely(!ctx->file_data))
8593 goto out;
8594 ret = -EINVAL;
8595 if (offset >= ctx->nr_user_files)
8596 goto out;
8597 ret = io_rsrc_node_switch_start(ctx);
8598 if (ret)
8599 goto out;
8600
8601 i = array_index_nospec(offset, ctx->nr_user_files);
8602 file_slot = io_fixed_file_slot(&ctx->file_table, i);
8603 ret = -EBADF;
8604 if (!file_slot->file_ptr)
8605 goto out;
8606
8607 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8608 ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
8609 if (ret)
8610 goto out;
8611
8612 file_slot->file_ptr = 0;
8613 io_rsrc_node_switch(ctx, ctx->file_data);
8614 ret = 0;
8615out:
Hao Xu3b44b372021-10-18 21:34:31 +08008616 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008617 return ret;
8618}
8619
Jens Axboe05f3fb32019-12-09 11:22:50 -07008620static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008621 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008622 unsigned nr_args)
8623{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008624 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008625 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008626 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008627 struct io_fixed_file *file_slot;
8628 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008629 int fd, i, err = 0;
8630 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008631 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008632
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008633 if (!ctx->file_data)
8634 return -ENXIO;
8635 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008636 return -EINVAL;
8637
Pavel Begunkov67973b92021-01-26 13:51:09 +00008638 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008639 u64 tag = 0;
8640
8641 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8642 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008643 err = -EFAULT;
8644 break;
8645 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008646 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8647 err = -EINVAL;
8648 break;
8649 }
noah4e0377a2021-01-26 15:23:28 -05008650 if (fd == IORING_REGISTER_FILES_SKIP)
8651 continue;
8652
Pavel Begunkov67973b92021-01-26 13:51:09 +00008653 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008654 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008655
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008656 if (file_slot->file_ptr) {
8657 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008658 err = io_queue_rsrc_removal(data, up->offset + done,
8659 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008660 if (err)
8661 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008662 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008663 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008664 }
8665 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008666 file = fget(fd);
8667 if (!file) {
8668 err = -EBADF;
8669 break;
8670 }
8671 /*
8672 * Don't allow io_uring instances to be registered. If
8673 * UNIX isn't enabled, then this causes a reference
8674 * cycle and this instance can never get freed. If UNIX
8675 * is enabled we'll handle it just fine, but there's
8676 * still no point in allowing a ring fd as it doesn't
8677 * support regular read/write anyway.
8678 */
8679 if (file->f_op == &io_uring_fops) {
8680 fput(file);
8681 err = -EBADF;
8682 break;
8683 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008684 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008685 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008686 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008687 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008688 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008689 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008690 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008691 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008692 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008693 }
8694
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008695 if (needs_switch)
8696 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008697 return done ? done : err;
8698}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008699
Jens Axboe685fe7f2021-03-08 09:37:51 -07008700static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8701 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008702{
Jens Axboee9418942021-02-19 12:33:30 -07008703 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008704 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008705 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008706
Yang Yingliang362a9e62021-07-20 16:38:05 +08008707 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008708 hash = ctx->hash_map;
8709 if (!hash) {
8710 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008711 if (!hash) {
8712 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008713 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008714 }
Jens Axboee9418942021-02-19 12:33:30 -07008715 refcount_set(&hash->refs, 1);
8716 init_waitqueue_head(&hash->wait);
8717 ctx->hash_map = hash;
8718 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008719 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008720
8721 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008722 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008723 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008724 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008725
Jens Axboed25e3a32021-02-16 11:41:41 -07008726 /* Do QD, or 4 * CPUS, whatever is smallest */
8727 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008728
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008729 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008730}
8731
Pavel Begunkovc0724812021-10-04 20:02:54 +01008732static __cold int io_uring_alloc_task_context(struct task_struct *task,
8733 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008734{
8735 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008736 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008737
Pavel Begunkov09899b12021-06-14 02:36:22 +01008738 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008739 if (unlikely(!tctx))
8740 return -ENOMEM;
8741
Jens Axboed8a6df12020-10-15 16:24:45 -06008742 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8743 if (unlikely(ret)) {
8744 kfree(tctx);
8745 return ret;
8746 }
8747
Jens Axboe685fe7f2021-03-08 09:37:51 -07008748 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008749 if (IS_ERR(tctx->io_wq)) {
8750 ret = PTR_ERR(tctx->io_wq);
8751 percpu_counter_destroy(&tctx->inflight);
8752 kfree(tctx);
8753 return ret;
8754 }
8755
Jens Axboe0f212202020-09-13 13:09:39 -06008756 xa_init(&tctx->xa);
8757 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008758 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008759 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008760 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008761 spin_lock_init(&tctx->task_lock);
8762 INIT_WQ_LIST(&tctx->task_list);
Hao Xu4813c372021-12-07 17:39:48 +08008763 INIT_WQ_LIST(&tctx->prior_task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008764 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008765 return 0;
8766}
8767
8768void __io_uring_free(struct task_struct *tsk)
8769{
8770 struct io_uring_task *tctx = tsk->io_uring;
8771
8772 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008773 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008774 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008775
Jens Axboed8a6df12020-10-15 16:24:45 -06008776 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008777 kfree(tctx);
8778 tsk->io_uring = NULL;
8779}
8780
Pavel Begunkovc0724812021-10-04 20:02:54 +01008781static __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
8782 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008783{
8784 int ret;
8785
Jens Axboed25e3a32021-02-16 11:41:41 -07008786 /* Retain compatibility with failing for an invalid attach attempt */
8787 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8788 IORING_SETUP_ATTACH_WQ) {
8789 struct fd f;
8790
8791 f = fdget(p->wq_fd);
8792 if (!f.file)
8793 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008794 if (f.file->f_op != &io_uring_fops) {
8795 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008796 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008797 }
8798 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008799 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008800 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008801 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008802 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008803 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008804
Paul Moorecdc14042021-02-01 19:56:49 -05008805 ret = security_uring_sqpoll();
8806 if (ret)
8807 return ret;
8808
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008809 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008810 if (IS_ERR(sqd)) {
8811 ret = PTR_ERR(sqd);
8812 goto err;
8813 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008814
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008815 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008816 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008817 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8818 if (!ctx->sq_thread_idle)
8819 ctx->sq_thread_idle = HZ;
8820
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008821 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008822 list_add(&ctx->sqd_list, &sqd->ctx_list);
8823 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008824 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008825 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008826 io_sq_thread_unpark(sqd);
8827
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008828 if (ret < 0)
8829 goto err;
8830 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008831 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008832
Jens Axboe6c271ce2019-01-10 11:22:30 -07008833 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008834 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008835
Jens Axboe917257d2019-04-13 09:28:55 -06008836 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008837 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008838 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008839 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008840 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008841 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008842 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008843
8844 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008845 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008846 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8847 if (IS_ERR(tsk)) {
8848 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008849 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008850 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008851
Jens Axboe46fe18b2021-03-04 12:39:36 -07008852 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008853 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008854 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008855 if (ret)
8856 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008857 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8858 /* Can't have SQ_AFF without SQPOLL */
8859 ret = -EINVAL;
8860 goto err;
8861 }
8862
Jens Axboe2b188cc2019-01-07 10:46:33 -07008863 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008864err_sqpoll:
8865 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008866err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008867 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008868 return ret;
8869}
8870
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008871static inline void __io_unaccount_mem(struct user_struct *user,
8872 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008873{
8874 atomic_long_sub(nr_pages, &user->locked_vm);
8875}
8876
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008877static inline int __io_account_mem(struct user_struct *user,
8878 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008879{
8880 unsigned long page_limit, cur_pages, new_pages;
8881
8882 /* Don't allow more pages than we can safely lock */
8883 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8884
8885 do {
8886 cur_pages = atomic_long_read(&user->locked_vm);
8887 new_pages = cur_pages + nr_pages;
8888 if (new_pages > page_limit)
8889 return -ENOMEM;
8890 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8891 new_pages) != cur_pages);
8892
8893 return 0;
8894}
8895
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008896static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008897{
Jens Axboe62e398b2021-02-21 16:19:37 -07008898 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008899 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008900
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008901 if (ctx->mm_account)
8902 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008903}
8904
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008905static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008906{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008907 int ret;
8908
Jens Axboe62e398b2021-02-21 16:19:37 -07008909 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008910 ret = __io_account_mem(ctx->user, nr_pages);
8911 if (ret)
8912 return ret;
8913 }
8914
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008915 if (ctx->mm_account)
8916 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008917
8918 return 0;
8919}
8920
Jens Axboe2b188cc2019-01-07 10:46:33 -07008921static void io_mem_free(void *ptr)
8922{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008923 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008924
Mark Rutland52e04ef2019-04-30 17:30:21 +01008925 if (!ptr)
8926 return;
8927
8928 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008929 if (put_page_testzero(page))
8930 free_compound_page(page);
8931}
8932
8933static void *io_mem_alloc(size_t size)
8934{
Shakeel Butt0a3f1e02022-01-24 21:17:36 -08008935 gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008936
Shakeel Butt0a3f1e02022-01-24 21:17:36 -08008937 return (void *) __get_free_pages(gfp, get_order(size));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008938}
8939
Hristo Venev75b28af2019-08-26 17:23:46 +00008940static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8941 size_t *sq_offset)
8942{
8943 struct io_rings *rings;
8944 size_t off, sq_array_size;
8945
8946 off = struct_size(rings, cqes, cq_entries);
8947 if (off == SIZE_MAX)
8948 return SIZE_MAX;
8949
8950#ifdef CONFIG_SMP
8951 off = ALIGN(off, SMP_CACHE_BYTES);
8952 if (off == 0)
8953 return SIZE_MAX;
8954#endif
8955
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008956 if (sq_offset)
8957 *sq_offset = off;
8958
Hristo Venev75b28af2019-08-26 17:23:46 +00008959 sq_array_size = array_size(sizeof(u32), sq_entries);
8960 if (sq_array_size == SIZE_MAX)
8961 return SIZE_MAX;
8962
8963 if (check_add_overflow(off, sq_array_size, &off))
8964 return SIZE_MAX;
8965
Hristo Venev75b28af2019-08-26 17:23:46 +00008966 return off;
8967}
8968
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008969static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008970{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008971 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008972 unsigned int i;
8973
Pavel Begunkov62248432021-04-28 13:11:29 +01008974 if (imu != ctx->dummy_ubuf) {
8975 for (i = 0; i < imu->nr_bvecs; i++)
8976 unpin_user_page(imu->bvec[i].bv_page);
8977 if (imu->acct_pages)
8978 io_unaccount_mem(ctx, imu->acct_pages);
8979 kvfree(imu);
8980 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008981 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008982}
8983
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008984static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8985{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008986 io_buffer_unmap(ctx, &prsrc->buf);
8987 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008988}
8989
8990static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008991{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008992 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008993
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008994 for (i = 0; i < ctx->nr_user_bufs; i++)
8995 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008996 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008997 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008998 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008999 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07009000 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009001}
9002
Jens Axboeedafcce2019-01-09 09:16:05 -07009003static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
9004{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009005 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07009006
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009007 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07009008 return -ENXIO;
9009
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009010 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
9011 if (!ret)
9012 __io_sqe_buffers_unregister(ctx);
9013 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07009014}
9015
9016static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
9017 void __user *arg, unsigned index)
9018{
9019 struct iovec __user *src;
9020
9021#ifdef CONFIG_COMPAT
9022 if (ctx->compat) {
9023 struct compat_iovec __user *ciovs;
9024 struct compat_iovec ciov;
9025
9026 ciovs = (struct compat_iovec __user *) arg;
9027 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
9028 return -EFAULT;
9029
Jens Axboed55e5f52019-12-11 16:12:15 -07009030 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07009031 dst->iov_len = ciov.iov_len;
9032 return 0;
9033 }
9034#endif
9035 src = (struct iovec __user *) arg;
9036 if (copy_from_user(dst, &src[index], sizeof(*dst)))
9037 return -EFAULT;
9038 return 0;
9039}
9040
Jens Axboede293932020-09-17 16:19:16 -06009041/*
9042 * Not super efficient, but this is just a registration time. And we do cache
9043 * the last compound head, so generally we'll only do a full search if we don't
9044 * match that one.
9045 *
9046 * We check if the given compound head page has already been accounted, to
9047 * avoid double accounting it. This allows us to account the full size of the
9048 * page, not just the constituent pages of a huge page.
9049 */
9050static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
9051 int nr_pages, struct page *hpage)
9052{
9053 int i, j;
9054
9055 /* check current page array */
9056 for (i = 0; i < nr_pages; i++) {
9057 if (!PageCompound(pages[i]))
9058 continue;
9059 if (compound_head(pages[i]) == hpage)
9060 return true;
9061 }
9062
9063 /* check previously registered pages */
9064 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009065 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06009066
9067 for (j = 0; j < imu->nr_bvecs; j++) {
9068 if (!PageCompound(imu->bvec[j].bv_page))
9069 continue;
9070 if (compound_head(imu->bvec[j].bv_page) == hpage)
9071 return true;
9072 }
9073 }
9074
9075 return false;
9076}
9077
9078static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
9079 int nr_pages, struct io_mapped_ubuf *imu,
9080 struct page **last_hpage)
9081{
9082 int i, ret;
9083
Pavel Begunkov216e5832021-05-29 12:01:02 +01009084 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06009085 for (i = 0; i < nr_pages; i++) {
9086 if (!PageCompound(pages[i])) {
9087 imu->acct_pages++;
9088 } else {
9089 struct page *hpage;
9090
9091 hpage = compound_head(pages[i]);
9092 if (hpage == *last_hpage)
9093 continue;
9094 *last_hpage = hpage;
9095 if (headpage_already_acct(ctx, pages, i, hpage))
9096 continue;
9097 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
9098 }
9099 }
9100
9101 if (!imu->acct_pages)
9102 return 0;
9103
Jens Axboe26bfa89e2021-02-09 20:14:12 -07009104 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06009105 if (ret)
9106 imu->acct_pages = 0;
9107 return ret;
9108}
9109
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009110static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009111 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009112 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07009113{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009114 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07009115 struct vm_area_struct **vmas = NULL;
9116 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009117 unsigned long off, start, end, ubuf;
9118 size_t size;
9119 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07009120
Pavel Begunkov62248432021-04-28 13:11:29 +01009121 if (!iov->iov_base) {
9122 *pimu = ctx->dummy_ubuf;
9123 return 0;
9124 }
9125
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009126 ubuf = (unsigned long) iov->iov_base;
9127 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
9128 start = ubuf >> PAGE_SHIFT;
9129 nr_pages = end - start;
9130
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009131 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009132 ret = -ENOMEM;
9133
9134 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
9135 if (!pages)
9136 goto done;
9137
9138 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
9139 GFP_KERNEL);
9140 if (!vmas)
9141 goto done;
9142
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009143 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01009144 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009145 goto done;
9146
9147 ret = 0;
9148 mmap_read_lock(current->mm);
9149 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
9150 pages, vmas);
9151 if (pret == nr_pages) {
9152 /* don't support file backed memory */
9153 for (i = 0; i < nr_pages; i++) {
9154 struct vm_area_struct *vma = vmas[i];
9155
Pavel Begunkov40dad762021-06-09 15:26:54 +01009156 if (vma_is_shmem(vma))
9157 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009158 if (vma->vm_file &&
9159 !is_file_hugepages(vma->vm_file)) {
9160 ret = -EOPNOTSUPP;
9161 break;
9162 }
9163 }
9164 } else {
9165 ret = pret < 0 ? pret : -EFAULT;
9166 }
9167 mmap_read_unlock(current->mm);
9168 if (ret) {
9169 /*
9170 * if we did partial map, or found file backed vmas,
9171 * release any pages we did get
9172 */
9173 if (pret > 0)
9174 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009175 goto done;
9176 }
9177
9178 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
9179 if (ret) {
9180 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009181 goto done;
9182 }
9183
9184 off = ubuf & ~PAGE_MASK;
9185 size = iov->iov_len;
9186 for (i = 0; i < nr_pages; i++) {
9187 size_t vec_len;
9188
9189 vec_len = min_t(size_t, size, PAGE_SIZE - off);
9190 imu->bvec[i].bv_page = pages[i];
9191 imu->bvec[i].bv_len = vec_len;
9192 imu->bvec[i].bv_offset = off;
9193 off = 0;
9194 size -= vec_len;
9195 }
9196 /* store original address for later verification */
9197 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01009198 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009199 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009200 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009201 ret = 0;
9202done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009203 if (ret)
9204 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009205 kvfree(pages);
9206 kvfree(vmas);
9207 return ret;
9208}
9209
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009210static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009211{
Pavel Begunkov87094462021-04-11 01:46:36 +01009212 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
9213 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009214}
9215
9216static int io_buffer_validate(struct iovec *iov)
9217{
Pavel Begunkov50e96982021-03-24 22:59:01 +00009218 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
9219
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009220 /*
9221 * Don't impose further limits on the size and buffer
9222 * constraints here, we'll -EINVAL later when IO is
9223 * submitted if they are wrong.
9224 */
Pavel Begunkov62248432021-04-28 13:11:29 +01009225 if (!iov->iov_base)
9226 return iov->iov_len ? -EFAULT : 0;
9227 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009228 return -EFAULT;
9229
9230 /* arbitrary limit, but we need something */
9231 if (iov->iov_len > SZ_1G)
9232 return -EFAULT;
9233
Pavel Begunkov50e96982021-03-24 22:59:01 +00009234 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
9235 return -EOVERFLOW;
9236
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009237 return 0;
9238}
9239
9240static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009241 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009242{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009243 struct page *last_hpage = NULL;
9244 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009245 int i, ret;
9246 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009247
Pavel Begunkov87094462021-04-11 01:46:36 +01009248 if (ctx->user_bufs)
9249 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01009250 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01009251 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009252 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009253 if (ret)
9254 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01009255 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9256 if (ret)
9257 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009258 ret = io_buffers_map_alloc(ctx, nr_args);
9259 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08009260 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009261 return ret;
9262 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009263
Pavel Begunkov87094462021-04-11 01:46:36 +01009264 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07009265 ret = io_copy_iov(ctx, &iov, arg, i);
9266 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009267 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009268 ret = io_buffer_validate(&iov);
9269 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009270 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009271 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009272 ret = -EINVAL;
9273 break;
9274 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009275
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009276 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9277 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009278 if (ret)
9279 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009280 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009281
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009282 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009283
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009284 ctx->buf_data = data;
9285 if (ret)
9286 __io_sqe_buffers_unregister(ctx);
9287 else
9288 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009289 return ret;
9290}
9291
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009292static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9293 struct io_uring_rsrc_update2 *up,
9294 unsigned int nr_args)
9295{
9296 u64 __user *tags = u64_to_user_ptr(up->tags);
9297 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009298 struct page *last_hpage = NULL;
9299 bool needs_switch = false;
9300 __u32 done;
9301 int i, err;
9302
9303 if (!ctx->buf_data)
9304 return -ENXIO;
9305 if (up->offset + nr_args > ctx->nr_user_bufs)
9306 return -EINVAL;
9307
9308 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009309 struct io_mapped_ubuf *imu;
9310 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009311 u64 tag = 0;
9312
9313 err = io_copy_iov(ctx, &iov, iovs, done);
9314 if (err)
9315 break;
9316 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9317 err = -EFAULT;
9318 break;
9319 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009320 err = io_buffer_validate(&iov);
9321 if (err)
9322 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009323 if (!iov.iov_base && tag) {
9324 err = -EINVAL;
9325 break;
9326 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009327 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9328 if (err)
9329 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009330
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009331 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009332 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009333 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9334 ctx->rsrc_node, ctx->user_bufs[i]);
9335 if (unlikely(err)) {
9336 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009337 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009338 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009339 ctx->user_bufs[i] = NULL;
9340 needs_switch = true;
9341 }
9342
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009343 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009344 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009345 }
9346
9347 if (needs_switch)
9348 io_rsrc_node_switch(ctx, ctx->buf_data);
9349 return done ? done : err;
9350}
9351
Jens Axboe9b402842019-04-11 11:45:41 -06009352static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9353{
9354 __s32 __user *fds = arg;
9355 int fd;
9356
9357 if (ctx->cq_ev_fd)
9358 return -EBUSY;
9359
9360 if (copy_from_user(&fd, fds, sizeof(*fds)))
9361 return -EFAULT;
9362
9363 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9364 if (IS_ERR(ctx->cq_ev_fd)) {
9365 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009366
Jens Axboe9b402842019-04-11 11:45:41 -06009367 ctx->cq_ev_fd = NULL;
9368 return ret;
9369 }
9370
9371 return 0;
9372}
9373
9374static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9375{
9376 if (ctx->cq_ev_fd) {
9377 eventfd_ctx_put(ctx->cq_ev_fd);
9378 ctx->cq_ev_fd = NULL;
9379 return 0;
9380 }
9381
9382 return -ENXIO;
9383}
9384
Jens Axboe5a2e7452020-02-23 16:23:11 -07009385static void io_destroy_buffers(struct io_ring_ctx *ctx)
9386{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009387 struct io_buffer *buf;
9388 unsigned long index;
9389
Ye Bin1d0254e2021-11-22 10:47:37 +08009390 xa_for_each(&ctx->io_buffers, index, buf)
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009391 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009392}
9393
Jens Axboe4010fec2021-02-27 15:04:18 -07009394static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009395{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009396 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009397 int nr = 0;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009398
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009399 mutex_lock(&ctx->uring_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009400 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009401
9402 while (state->free_list.next) {
9403 struct io_wq_work_node *node;
9404 struct io_kiocb *req;
9405
9406 node = wq_stack_extract(&state->free_list);
9407 req = container_of(node, struct io_kiocb, comp_list);
9408 kmem_cache_free(req_cachep, req);
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009409 nr++;
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009410 }
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009411 if (nr)
9412 percpu_ref_put_many(&ctx->refs, nr);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009413 mutex_unlock(&ctx->uring_lock);
9414}
9415
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009416static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009417{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009418 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009419 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009420}
9421
Pavel Begunkovc0724812021-10-04 20:02:54 +01009422static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009423{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009424 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009425
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009426 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009427 mmdrop(ctx->mm_account);
9428 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009429 }
Jens Axboedef596e2019-01-09 08:59:42 -07009430
Pavel Begunkovab409402021-10-09 23:14:41 +01009431 io_rsrc_refs_drop(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009432 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9433 io_wait_rsrc_data(ctx->buf_data);
9434 io_wait_rsrc_data(ctx->file_data);
9435
Hao Xu8bad28d2021-02-19 17:19:36 +08009436 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009437 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009438 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009439 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009440 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009441 if (ctx->rings)
9442 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009443 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009444 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009445 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009446 if (ctx->sq_creds)
9447 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009448
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009449 /* there are no registered resources left, nobody uses it */
9450 if (ctx->rsrc_node)
9451 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009452 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009453 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009454 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov756ab7c2021-10-06 16:06:47 +01009455 flush_delayed_work(&ctx->fallback_work);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009456
9457 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9458 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009459
9460#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07009461 if (ctx->ring_sock) {
9462 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009463 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07009464 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009465#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009466 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009467
Hristo Venev75b28af2019-08-26 17:23:46 +00009468 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009469 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009470
9471 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009472 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009473 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009474 if (ctx->hash_map)
9475 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009476 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009477 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009478 kfree(ctx);
9479}
9480
9481static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9482{
9483 struct io_ring_ctx *ctx = file->private_data;
9484 __poll_t mask = 0;
9485
Pavel Begunkovd60aa652021-10-04 20:02:52 +01009486 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009487 /*
9488 * synchronizes with barrier from wq_has_sleeper call in
9489 * io_commit_cqring
9490 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009491 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009492 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009493 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009494
9495 /*
9496 * Don't flush cqring overflow list here, just do a simple check.
9497 * Otherwise there could possible be ABBA deadlock:
9498 * CPU0 CPU1
9499 * ---- ----
9500 * lock(&ctx->uring_lock);
9501 * lock(&ep->mtx);
9502 * lock(&ctx->uring_lock);
9503 * lock(&ep->mtx);
9504 *
9505 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9506 * pushs them to do the flush.
9507 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009508 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009509 mask |= EPOLLIN | EPOLLRDNORM;
9510
9511 return mask;
9512}
9513
Yejune Deng0bead8c2020-12-24 11:02:20 +08009514static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009515{
Jens Axboe4379bf82021-02-15 13:40:22 -07009516 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009517
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009518 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009519 if (creds) {
9520 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009521 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009522 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009523
9524 return -EINVAL;
9525}
9526
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009527struct io_tctx_exit {
9528 struct callback_head task_work;
9529 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009530 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009531};
9532
Pavel Begunkovc0724812021-10-04 20:02:54 +01009533static __cold void io_tctx_exit_cb(struct callback_head *cb)
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009534{
9535 struct io_uring_task *tctx = current->io_uring;
9536 struct io_tctx_exit *work;
9537
9538 work = container_of(cb, struct io_tctx_exit, task_work);
9539 /*
9540 * When @in_idle, we're in cancellation and it's racy to remove the
9541 * node. It'll be removed by the end of cancellation, just ignore it.
9542 */
9543 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009544 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009545 complete(&work->completion);
9546}
9547
Pavel Begunkovc0724812021-10-04 20:02:54 +01009548static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
Pavel Begunkov28090c12021-04-25 23:34:45 +01009549{
9550 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9551
9552 return req->ctx == data;
9553}
9554
Pavel Begunkovc0724812021-10-04 20:02:54 +01009555static __cold void io_ring_exit_work(struct work_struct *work)
Jens Axboe85faa7b2020-04-09 18:14:00 -06009556{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009557 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009558 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009559 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009560 struct io_tctx_exit exit;
9561 struct io_tctx_node *node;
9562 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009563
Jens Axboe56952e92020-06-17 15:00:04 -06009564 /*
9565 * If we're doing polled IO and end up having requests being
9566 * submitted async (out-of-line), then completions can come in while
9567 * we're waiting for refs to drop. We need to reap these manually,
9568 * as nobody else will be looking for them.
9569 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009570 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009571 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009572 if (ctx->sq_data) {
9573 struct io_sq_data *sqd = ctx->sq_data;
9574 struct task_struct *tsk;
9575
9576 io_sq_thread_park(sqd);
9577 tsk = sqd->thread;
9578 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9579 io_wq_cancel_cb(tsk->io_uring->io_wq,
9580 io_cancel_ctx_cb, ctx, true);
9581 io_sq_thread_unpark(sqd);
9582 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009583
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009584 io_req_caches_free(ctx);
9585
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009586 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9587 /* there is little hope left, don't run it too often */
9588 interval = HZ * 60;
9589 }
9590 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009591
Pavel Begunkov7f006512021-04-14 13:38:34 +01009592 init_completion(&exit.completion);
9593 init_task_work(&exit.task_work, io_tctx_exit_cb);
9594 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009595 /*
9596 * Some may use context even when all refs and requests have been put,
9597 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009598 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009599 * this lock/unlock section also waits them to finish.
9600 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009601 mutex_lock(&ctx->uring_lock);
9602 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009603 WARN_ON_ONCE(time_after(jiffies, timeout));
9604
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009605 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9606 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009607 /* don't spin on a single task if cancellation failed */
9608 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009609 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9610 if (WARN_ON_ONCE(ret))
9611 continue;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009612
9613 mutex_unlock(&ctx->uring_lock);
9614 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009615 mutex_lock(&ctx->uring_lock);
9616 }
9617 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009618 spin_lock(&ctx->completion_lock);
9619 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009620
Jens Axboe85faa7b2020-04-09 18:14:00 -06009621 io_ring_ctx_free(ctx);
9622}
9623
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009624/* Returns true if we found and killed one or more timeouts */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009625static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx,
9626 struct task_struct *tsk, bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009627{
9628 struct io_kiocb *req, *tmp;
9629 int canceled = 0;
9630
Jens Axboe79ebeae2021-08-10 15:18:27 -06009631 spin_lock(&ctx->completion_lock);
9632 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009633 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009634 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009635 io_kill_timeout(req, -ECANCELED);
9636 canceled++;
9637 }
9638 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009639 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009640 if (canceled != 0)
9641 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009642 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009643 if (canceled != 0)
9644 io_cqring_ev_posted(ctx);
9645 return canceled != 0;
9646}
9647
Pavel Begunkovc0724812021-10-04 20:02:54 +01009648static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009649{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009650 unsigned long index;
9651 struct creds *creds;
9652
Jens Axboe2b188cc2019-01-07 10:46:33 -07009653 mutex_lock(&ctx->uring_lock);
9654 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009655 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009656 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009657 xa_for_each(&ctx->personalities, index, creds)
9658 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009659 mutex_unlock(&ctx->uring_lock);
9660
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009661 io_kill_timeouts(ctx, NULL, true);
9662 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009663
Jens Axboe15dff282019-11-13 09:09:23 -07009664 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009665 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009666
Jens Axboe85faa7b2020-04-09 18:14:00 -06009667 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009668 /*
9669 * Use system_unbound_wq to avoid spawning tons of event kworkers
9670 * if we're exiting a ton of rings at the same time. It just adds
9671 * noise and overhead, there's no discernable change in runtime
9672 * over using system_wq.
9673 */
9674 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009675}
9676
9677static int io_uring_release(struct inode *inode, struct file *file)
9678{
9679 struct io_ring_ctx *ctx = file->private_data;
9680
9681 file->private_data = NULL;
9682 io_ring_ctx_wait_and_kill(ctx);
9683 return 0;
9684}
9685
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009686struct io_task_cancel {
9687 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009688 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009689};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009690
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009691static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009692{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009693 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009694 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009695
Pavel Begunkov6af3f482021-11-26 14:38:15 +00009696 return io_match_task_safe(req, cancel->task, cancel->all);
Jens Axboeb711d4e2020-08-16 08:23:05 -07009697}
9698
Pavel Begunkovc0724812021-10-04 20:02:54 +01009699static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
9700 struct task_struct *task,
9701 bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009702{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009703 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009704 LIST_HEAD(list);
9705
Jens Axboe79ebeae2021-08-10 15:18:27 -06009706 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009707 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov6af3f482021-11-26 14:38:15 +00009708 if (io_match_task_safe(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009709 list_cut_position(&list, &ctx->defer_list, &de->list);
9710 break;
9711 }
9712 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009713 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009714 if (list_empty(&list))
9715 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009716
9717 while (!list_empty(&list)) {
9718 de = list_first_entry(&list, struct io_defer_entry, list);
9719 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009720 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009721 kfree(de);
9722 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009723 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009724}
9725
Pavel Begunkovc0724812021-10-04 20:02:54 +01009726static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
Pavel Begunkov1b007642021-03-06 11:02:17 +00009727{
9728 struct io_tctx_node *node;
9729 enum io_wq_cancel cret;
9730 bool ret = false;
9731
9732 mutex_lock(&ctx->uring_lock);
9733 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9734 struct io_uring_task *tctx = node->task->io_uring;
9735
9736 /*
9737 * io_wq will stay alive while we hold uring_lock, because it's
9738 * killed after ctx nodes, which requires to take the lock.
9739 */
9740 if (!tctx || !tctx->io_wq)
9741 continue;
9742 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9743 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9744 }
9745 mutex_unlock(&ctx->uring_lock);
9746
9747 return ret;
9748}
9749
Pavel Begunkovc0724812021-10-04 20:02:54 +01009750static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9751 struct task_struct *task,
9752 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009753{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009754 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009755 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009756
9757 while (1) {
9758 enum io_wq_cancel cret;
9759 bool ret = false;
9760
Pavel Begunkov1b007642021-03-06 11:02:17 +00009761 if (!task) {
9762 ret |= io_uring_try_cancel_iowq(ctx);
9763 } else if (tctx && tctx->io_wq) {
9764 /*
9765 * Cancels requests of all rings, not only @ctx, but
9766 * it's fine as the task is in exit/exec.
9767 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009768 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009769 &cancel, true);
9770 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9771 }
9772
9773 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009774 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009775 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01009776 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009777 io_iopoll_try_reap_events(ctx);
9778 ret = true;
9779 }
9780 }
9781
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009782 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9783 ret |= io_poll_remove_all(ctx, task, cancel_all);
9784 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009785 if (task)
9786 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009787 if (!ret)
9788 break;
9789 cond_resched();
9790 }
9791}
9792
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009793static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009794{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009795 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009796 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009797 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009798
9799 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009800 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009801 if (unlikely(ret))
9802 return ret;
Pavel Begunkove139a1e2021-10-19 23:43:46 +01009803
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009804 tctx = current->io_uring;
Pavel Begunkove139a1e2021-10-19 23:43:46 +01009805 if (ctx->iowq_limits_set) {
9806 unsigned int limits[2] = { ctx->iowq_limits[0],
9807 ctx->iowq_limits[1], };
9808
9809 ret = io_wq_max_workers(tctx->io_wq, limits);
9810 if (ret)
9811 return ret;
9812 }
Jens Axboe0f212202020-09-13 13:09:39 -06009813 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009814 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9815 node = kmalloc(sizeof(*node), GFP_KERNEL);
9816 if (!node)
9817 return -ENOMEM;
9818 node->ctx = ctx;
9819 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009820
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009821 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9822 node, GFP_KERNEL));
9823 if (ret) {
9824 kfree(node);
9825 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009826 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009827
9828 mutex_lock(&ctx->uring_lock);
9829 list_add(&node->ctx_node, &ctx->tctx_list);
9830 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009831 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009832 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009833 return 0;
9834}
9835
9836/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009837 * Note that this task has used io_uring. We use it for cancelation purposes.
9838 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009839static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009840{
9841 struct io_uring_task *tctx = current->io_uring;
9842
9843 if (likely(tctx && tctx->last == ctx))
9844 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009845 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009846}
9847
9848/*
Jens Axboe0f212202020-09-13 13:09:39 -06009849 * Remove this io_uring_file -> task mapping.
9850 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009851static __cold void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009852{
9853 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009854 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009855
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009856 if (!tctx)
9857 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009858 node = xa_erase(&tctx->xa, index);
9859 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009860 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009861
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009862 WARN_ON_ONCE(current != node->task);
9863 WARN_ON_ONCE(list_empty(&node->ctx_node));
9864
9865 mutex_lock(&node->ctx->uring_lock);
9866 list_del(&node->ctx_node);
9867 mutex_unlock(&node->ctx->uring_lock);
9868
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009869 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009870 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009871 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009872}
9873
Pavel Begunkovc0724812021-10-04 20:02:54 +01009874static __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009875{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009876 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009877 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009878 unsigned long index;
9879
Jens Axboe8bab4c02021-09-24 07:12:27 -06009880 xa_for_each(&tctx->xa, index, node) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009881 io_uring_del_tctx_node(index);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009882 cond_resched();
9883 }
Marco Elverb16ef422021-05-27 11:25:48 +02009884 if (wq) {
9885 /*
Kamal Mostafaf6f9b272021-11-16 09:55:30 -08009886 * Must be after io_uring_del_tctx_node() (removes nodes under
Marco Elverb16ef422021-05-27 11:25:48 +02009887 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9888 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009889 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009890 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009891 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009892}
9893
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009894static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009895{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009896 if (tracked)
9897 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009898 return percpu_counter_sum(&tctx->inflight);
9899}
9900
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009901/*
9902 * Find any io_uring ctx that this task has registered or done IO on, and cancel
Jens Axboe78a78062021-12-09 08:54:29 -07009903 * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation.
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009904 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009905static __cold void io_uring_cancel_generic(bool cancel_all,
9906 struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009907{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009908 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009909 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009910 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009911 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009912
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009913 WARN_ON_ONCE(sqd && sqd->thread != current);
9914
Palash Oswal6d042ff2021-04-27 18:21:49 +05309915 if (!current->io_uring)
9916 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009917 if (tctx->io_wq)
9918 io_wq_exit_start(tctx->io_wq);
9919
Jens Axboefdaf0832020-10-30 09:37:30 -06009920 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009921 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009922 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009923 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009924 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009925 if (!inflight)
9926 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009927
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009928 if (!sqd) {
9929 struct io_tctx_node *node;
9930 unsigned long index;
9931
9932 xa_for_each(&tctx->xa, index, node) {
9933 /* sqpoll task will cancel all its requests */
9934 if (node->ctx->sq_data)
9935 continue;
9936 io_uring_try_cancel_requests(node->ctx, current,
9937 cancel_all);
9938 }
9939 } else {
9940 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9941 io_uring_try_cancel_requests(ctx, current,
9942 cancel_all);
9943 }
9944
Jens Axboe78a78062021-12-09 08:54:29 -07009945 prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE);
9946 io_run_task_work();
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009947 io_uring_drop_tctx_refs(current);
Jens Axboe78a78062021-12-09 08:54:29 -07009948
Jens Axboe0f212202020-09-13 13:09:39 -06009949 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009950 * If we've seen completions, retry without waiting. This
9951 * avoids a race where a completion comes in before we did
9952 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009953 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009954 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009955 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009956 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009957 } while (1);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009958
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009959 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009960 if (cancel_all) {
Pavel Begunkov3cc7fdb2022-01-09 00:53:22 +00009961 /*
9962 * We shouldn't run task_works after cancel, so just leave
9963 * ->in_idle set for normal exit.
9964 */
9965 atomic_dec(&tctx->in_idle);
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009966 /* for exec all current's requests should be gone, kill tctx */
9967 __io_uring_free(current);
9968 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009969}
9970
Hao Xuf552a272021-08-12 12:14:35 +08009971void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009972{
Hao Xuf552a272021-08-12 12:14:35 +08009973 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009974}
9975
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009976static void *io_uring_validate_mmap_request(struct file *file,
9977 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009978{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009979 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009980 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009981 struct page *page;
9982 void *ptr;
9983
9984 switch (offset) {
9985 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009986 case IORING_OFF_CQ_RING:
9987 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009988 break;
9989 case IORING_OFF_SQES:
9990 ptr = ctx->sq_sqes;
9991 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009992 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009993 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009994 }
9995
9996 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009997 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009998 return ERR_PTR(-EINVAL);
9999
10000 return ptr;
10001}
10002
10003#ifdef CONFIG_MMU
10004
Pavel Begunkovc0724812021-10-04 20:02:54 +010010005static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010006{
10007 size_t sz = vma->vm_end - vma->vm_start;
10008 unsigned long pfn;
10009 void *ptr;
10010
10011 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
10012 if (IS_ERR(ptr))
10013 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010014
10015 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
10016 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
10017}
10018
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010019#else /* !CONFIG_MMU */
10020
10021static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
10022{
10023 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
10024}
10025
10026static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
10027{
10028 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
10029}
10030
10031static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
10032 unsigned long addr, unsigned long len,
10033 unsigned long pgoff, unsigned long flags)
10034{
10035 void *ptr;
10036
10037 ptr = io_uring_validate_mmap_request(file, pgoff, len);
10038 if (IS_ERR(ptr))
10039 return PTR_ERR(ptr);
10040
10041 return (unsigned long) ptr;
10042}
10043
10044#endif /* !CONFIG_MMU */
10045
Pavel Begunkovd9d05212021-01-08 20:57:25 +000010046static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -060010047{
10048 DEFINE_WAIT(wait);
10049
10050 do {
10051 if (!io_sqring_full(ctx))
10052 break;
Jens Axboe90554202020-09-03 12:12:41 -060010053 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
10054
10055 if (!io_sqring_full(ctx))
10056 break;
Jens Axboe90554202020-09-03 12:12:41 -060010057 schedule();
10058 } while (!signal_pending(current));
10059
10060 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +080010061 return 0;
Jens Axboe90554202020-09-03 12:12:41 -060010062}
10063
Hao Xuc73ebb62020-11-03 10:54:37 +080010064static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
10065 struct __kernel_timespec __user **ts,
10066 const sigset_t __user **sig)
10067{
10068 struct io_uring_getevents_arg arg;
10069
10070 /*
10071 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
10072 * is just a pointer to the sigset_t.
10073 */
10074 if (!(flags & IORING_ENTER_EXT_ARG)) {
10075 *sig = (const sigset_t __user *) argp;
10076 *ts = NULL;
10077 return 0;
10078 }
10079
10080 /*
10081 * EXT_ARG is set - ensure we agree on the size of it and copy in our
10082 * timespec and sigset_t pointers if good.
10083 */
10084 if (*argsz != sizeof(arg))
10085 return -EINVAL;
10086 if (copy_from_user(&arg, argp, sizeof(arg)))
10087 return -EFAULT;
10088 *sig = u64_to_user_ptr(arg.sigmask);
10089 *argsz = arg.sigmask_sz;
10090 *ts = u64_to_user_ptr(arg.ts);
10091 return 0;
10092}
10093
Jens Axboe2b188cc2019-01-07 10:46:33 -070010094SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +080010095 u32, min_complete, u32, flags, const void __user *, argp,
10096 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010097{
10098 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010099 int submitted = 0;
10100 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010101 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010102
Jens Axboe4c6e2772020-07-01 11:29:10 -060010103 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -070010104
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010105 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
10106 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010107 return -EINVAL;
10108
10109 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010110 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010111 return -EBADF;
10112
10113 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010114 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010115 goto out_fput;
10116
10117 ret = -ENXIO;
10118 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010119 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010120 goto out_fput;
10121
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010122 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010123 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010124 goto out;
10125
Jens Axboe6c271ce2019-01-10 11:22:30 -070010126 /*
10127 * For SQ polling, the thread will do all submissions and completions.
10128 * Just return the requested submit count, and wake the thread if
10129 * we were asked to.
10130 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -060010131 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -070010132 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +010010133 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +000010134
Jens Axboe21f96522021-08-14 09:04:40 -060010135 if (unlikely(ctx->sq_data->thread == NULL)) {
10136 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +010010137 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -060010138 }
Jens Axboe6c271ce2019-01-10 11:22:30 -070010139 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -060010140 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +000010141 if (flags & IORING_ENTER_SQ_WAIT) {
10142 ret = io_sqpoll_wait_sq(ctx);
10143 if (ret)
10144 goto out;
10145 }
Jens Axboe6c271ce2019-01-10 11:22:30 -070010146 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -060010147 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010148 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -060010149 if (unlikely(ret))
10150 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010151 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -060010152 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010153 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010154
10155 if (submitted != to_submit)
10156 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010157 }
10158 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +080010159 const sigset_t __user *sig;
10160 struct __kernel_timespec __user *ts;
10161
10162 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
10163 if (unlikely(ret))
10164 goto out;
10165
Jens Axboe2b188cc2019-01-07 10:46:33 -070010166 min_complete = min(min_complete, ctx->cq_entries);
10167
Xiaoguang Wang32b22442020-03-11 09:26:09 +080010168 /*
10169 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
10170 * space applications don't need to do io completion events
10171 * polling again, they can rely on io_sq_thread to do polling
10172 * work, which can reduce cpu usage and uring_lock contention.
10173 */
10174 if (ctx->flags & IORING_SETUP_IOPOLL &&
10175 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +030010176 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -070010177 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +080010178 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -070010179 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010180 }
10181
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010182out:
Pavel Begunkov6805b322019-10-08 02:18:42 +030010183 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010184out_fput:
10185 fdput(f);
10186 return submitted ? submitted : ret;
10187}
10188
Tobias Klauserbebdb652020-02-26 18:38:32 +010010189#ifdef CONFIG_PROC_FS
Pavel Begunkovc0724812021-10-04 20:02:54 +010010190static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010191 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -070010192{
Jens Axboe87ce9552020-01-30 08:25:34 -070010193 struct user_namespace *uns = seq_user_ns(m);
10194 struct group_info *gi;
10195 kernel_cap_t cap;
10196 unsigned __capi;
10197 int g;
10198
10199 seq_printf(m, "%5d\n", id);
10200 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
10201 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
10202 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
10203 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
10204 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
10205 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
10206 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
10207 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
10208 seq_puts(m, "\n\tGroups:\t");
10209 gi = cred->group_info;
10210 for (g = 0; g < gi->ngroups; g++) {
10211 seq_put_decimal_ull(m, g ? " " : "",
10212 from_kgid_munged(uns, gi->gid[g]));
10213 }
10214 seq_puts(m, "\n\tCapEff:\t");
10215 cap = cred->cap_effective;
10216 CAP_FOR_EACH_U32(__capi)
10217 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
10218 seq_putc(m, '\n');
10219 return 0;
10220}
10221
Pavel Begunkovc0724812021-10-04 20:02:54 +010010222static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx,
10223 struct seq_file *m)
Jens Axboe87ce9552020-01-30 08:25:34 -070010224{
Joseph Qidbbe9c62020-09-29 09:01:22 -060010225 struct io_sq_data *sq = NULL;
Hao Xu83f84352021-09-13 21:08:54 +080010226 struct io_overflow_cqe *ocqe;
10227 struct io_rings *r = ctx->rings;
10228 unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
Hao Xu83f84352021-09-13 21:08:54 +080010229 unsigned int sq_head = READ_ONCE(r->sq.head);
10230 unsigned int sq_tail = READ_ONCE(r->sq.tail);
10231 unsigned int cq_head = READ_ONCE(r->cq.head);
10232 unsigned int cq_tail = READ_ONCE(r->cq.tail);
Jens Axboef75d1182021-10-29 06:36:45 -060010233 unsigned int sq_entries, cq_entries;
Jens Axboefad8e0d2020-09-28 08:57:48 -060010234 bool has_lock;
Hao Xu83f84352021-09-13 21:08:54 +080010235 unsigned int i;
10236
10237 /*
10238 * we may get imprecise sqe and cqe info if uring is actively running
10239 * since we get cached_sq_head and cached_cq_tail without uring_lock
10240 * and sq_tail and cq_head are changed by userspace. But it's ok since
10241 * we usually use these info when it is stuck.
10242 */
GuoYong Zhengc0235652022-01-05 18:13:05 +080010243 seq_printf(m, "SqMask:\t0x%x\n", sq_mask);
Jens Axboef75d1182021-10-29 06:36:45 -060010244 seq_printf(m, "SqHead:\t%u\n", sq_head);
10245 seq_printf(m, "SqTail:\t%u\n", sq_tail);
10246 seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head);
10247 seq_printf(m, "CqMask:\t0x%x\n", cq_mask);
10248 seq_printf(m, "CqHead:\t%u\n", cq_head);
10249 seq_printf(m, "CqTail:\t%u\n", cq_tail);
10250 seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail);
10251 seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head);
10252 sq_entries = min(sq_tail - sq_head, ctx->sq_entries);
10253 for (i = 0; i < sq_entries; i++) {
10254 unsigned int entry = i + sq_head;
10255 unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]);
Jens Axboea1957782021-11-05 09:31:05 -060010256 struct io_uring_sqe *sqe;
Hao Xu83f84352021-09-13 21:08:54 +080010257
Jens Axboef75d1182021-10-29 06:36:45 -060010258 if (sq_idx > sq_mask)
10259 continue;
10260 sqe = &ctx->sq_sqes[sq_idx];
10261 seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n",
10262 sq_idx, sqe->opcode, sqe->fd, sqe->flags,
10263 sqe->user_data);
Hao Xu83f84352021-09-13 21:08:54 +080010264 }
Jens Axboef75d1182021-10-29 06:36:45 -060010265 seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head);
10266 cq_entries = min(cq_tail - cq_head, ctx->cq_entries);
10267 for (i = 0; i < cq_entries; i++) {
10268 unsigned int entry = i + cq_head;
10269 struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask];
Hao Xu83f84352021-09-13 21:08:54 +080010270
10271 seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n",
Jens Axboef75d1182021-10-29 06:36:45 -060010272 entry & cq_mask, cqe->user_data, cqe->res,
10273 cqe->flags);
Hao Xu83f84352021-09-13 21:08:54 +080010274 }
Jens Axboe87ce9552020-01-30 08:25:34 -070010275
Jens Axboefad8e0d2020-09-28 08:57:48 -060010276 /*
10277 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10278 * since fdinfo case grabs it in the opposite direction of normal use
10279 * cases. If we fail to get the lock, we just don't iterate any
10280 * structures that could be going away outside the io_uring mutex.
10281 */
10282 has_lock = mutex_trylock(&ctx->uring_lock);
10283
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010284 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -060010285 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010286 if (!sq->thread)
10287 sq = NULL;
10288 }
Joseph Qidbbe9c62020-09-29 09:01:22 -060010289
10290 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10291 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -070010292 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010293 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -070010294 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -070010295
Jens Axboe87ce9552020-01-30 08:25:34 -070010296 if (f)
10297 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10298 else
10299 seq_printf(m, "%5u: <none>\n", i);
10300 }
10301 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010302 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +010010303 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +010010304 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -070010305
Pavel Begunkov4751f532021-04-01 15:43:55 +010010306 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -070010307 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010308 if (has_lock && !xa_empty(&ctx->personalities)) {
10309 unsigned long index;
10310 const struct cred *cred;
10311
Jens Axboe87ce9552020-01-30 08:25:34 -070010312 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010313 xa_for_each(&ctx->personalities, index, cred)
10314 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010315 }
Hao Xu83f84352021-09-13 21:08:54 +080010316 if (has_lock)
10317 mutex_unlock(&ctx->uring_lock);
10318
10319 seq_puts(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010320 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010321 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10322 struct hlist_head *list = &ctx->cancel_hash[i];
10323 struct io_kiocb *req;
10324
10325 hlist_for_each_entry(req, list, hash_node)
10326 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10327 req->task->task_works != NULL);
10328 }
Hao Xu83f84352021-09-13 21:08:54 +080010329
10330 seq_puts(m, "CqOverflowList:\n");
10331 list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
10332 struct io_uring_cqe *cqe = &ocqe->cqe;
10333
10334 seq_printf(m, " user_data=%llu, res=%d, flags=%x\n",
10335 cqe->user_data, cqe->res, cqe->flags);
10336
10337 }
10338
Jens Axboe79ebeae2021-08-10 15:18:27 -060010339 spin_unlock(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010340}
10341
Pavel Begunkovc0724812021-10-04 20:02:54 +010010342static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
Jens Axboe87ce9552020-01-30 08:25:34 -070010343{
10344 struct io_ring_ctx *ctx = f->private_data;
10345
10346 if (percpu_ref_tryget(&ctx->refs)) {
10347 __io_uring_show_fdinfo(ctx, m);
10348 percpu_ref_put(&ctx->refs);
10349 }
10350}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010351#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010352
Jens Axboe2b188cc2019-01-07 10:46:33 -070010353static const struct file_operations io_uring_fops = {
10354 .release = io_uring_release,
10355 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010356#ifndef CONFIG_MMU
10357 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10358 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10359#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010360 .poll = io_uring_poll,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010361#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010362 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010363#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010364};
10365
Pavel Begunkovc0724812021-10-04 20:02:54 +010010366static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10367 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010368{
Hristo Venev75b28af2019-08-26 17:23:46 +000010369 struct io_rings *rings;
10370 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010371
Jens Axboebd740482020-08-05 12:58:23 -060010372 /* make sure these are sane, as we already accounted them */
10373 ctx->sq_entries = p->sq_entries;
10374 ctx->cq_entries = p->cq_entries;
10375
Hristo Venev75b28af2019-08-26 17:23:46 +000010376 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10377 if (size == SIZE_MAX)
10378 return -EOVERFLOW;
10379
10380 rings = io_mem_alloc(size);
10381 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010382 return -ENOMEM;
10383
Hristo Venev75b28af2019-08-26 17:23:46 +000010384 ctx->rings = rings;
10385 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10386 rings->sq_ring_mask = p->sq_entries - 1;
10387 rings->cq_ring_mask = p->cq_entries - 1;
10388 rings->sq_ring_entries = p->sq_entries;
10389 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010390
10391 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010392 if (size == SIZE_MAX) {
10393 io_mem_free(ctx->rings);
10394 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010395 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010396 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010397
10398 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010399 if (!ctx->sq_sqes) {
10400 io_mem_free(ctx->rings);
10401 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010402 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010403 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010404
Jens Axboe2b188cc2019-01-07 10:46:33 -070010405 return 0;
10406}
10407
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010408static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10409{
10410 int ret, fd;
10411
10412 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10413 if (fd < 0)
10414 return fd;
10415
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010416 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010417 if (ret) {
10418 put_unused_fd(fd);
10419 return ret;
10420 }
10421 fd_install(fd, file);
10422 return fd;
10423}
10424
Jens Axboe2b188cc2019-01-07 10:46:33 -070010425/*
10426 * Allocate an anonymous fd, this is what constitutes the application
10427 * visible backing of an io_uring instance. The application mmaps this
10428 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10429 * we have to tie this fd to a socket for file garbage collection purposes.
10430 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010431static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010432{
10433 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010434#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010435 int ret;
10436
Jens Axboe2b188cc2019-01-07 10:46:33 -070010437 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10438 &ctx->ring_sock);
10439 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010440 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010441#endif
10442
Paul Moore91a9ab72021-02-01 19:33:52 -050010443 file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx,
10444 O_RDWR | O_CLOEXEC, NULL);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010445#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010446 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010447 sock_release(ctx->ring_sock);
10448 ctx->ring_sock = NULL;
10449 } else {
10450 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010451 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010452#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010453 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010454}
10455
Pavel Begunkovc0724812021-10-04 20:02:54 +010010456static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
10457 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010458{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010459 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010460 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010461 int ret;
10462
Jens Axboe8110c1a2019-12-28 15:39:54 -070010463 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010464 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010465 if (entries > IORING_MAX_ENTRIES) {
10466 if (!(p->flags & IORING_SETUP_CLAMP))
10467 return -EINVAL;
10468 entries = IORING_MAX_ENTRIES;
10469 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010470
10471 /*
10472 * Use twice as many entries for the CQ ring. It's possible for the
10473 * application to drive a higher depth than the size of the SQ ring,
10474 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010475 * some flexibility in overcommitting a bit. If the application has
10476 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10477 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010478 */
10479 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010480 if (p->flags & IORING_SETUP_CQSIZE) {
10481 /*
10482 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10483 * to a power-of-two, if it isn't already. We do NOT impose
10484 * any cq vs sq ring sizing.
10485 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010486 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010487 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010488 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10489 if (!(p->flags & IORING_SETUP_CLAMP))
10490 return -EINVAL;
10491 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10492 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010493 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10494 if (p->cq_entries < p->sq_entries)
10495 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010496 } else {
10497 p->cq_entries = 2 * p->sq_entries;
10498 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010499
Jens Axboe2b188cc2019-01-07 10:46:33 -070010500 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010501 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010502 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010503 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010504 if (!capable(CAP_IPC_LOCK))
10505 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010506
10507 /*
10508 * This is just grabbed for accounting purposes. When a process exits,
10509 * the mm is exited and dropped before the files, hence we need to hang
10510 * on to this mm purely for the purposes of being able to unaccount
10511 * memory (locked/pinned vm). It's not used for anything else.
10512 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010513 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010514 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010515
Jens Axboe2b188cc2019-01-07 10:46:33 -070010516 ret = io_allocate_scq_urings(ctx, p);
10517 if (ret)
10518 goto err;
10519
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010520 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010521 if (ret)
10522 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010523 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010524 ret = io_rsrc_node_switch_start(ctx);
10525 if (ret)
10526 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010527 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010528
Jens Axboe2b188cc2019-01-07 10:46:33 -070010529 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010530 p->sq_off.head = offsetof(struct io_rings, sq.head);
10531 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10532 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10533 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10534 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10535 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10536 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010537
10538 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010539 p->cq_off.head = offsetof(struct io_rings, cq.head);
10540 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10541 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10542 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10543 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10544 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010545 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010546
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010547 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10548 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010549 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010550 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010551 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
Pavel Begunkov04c76b42021-11-10 15:49:32 +000010552 IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010553
10554 if (copy_to_user(params, p, sizeof(*p))) {
10555 ret = -EFAULT;
10556 goto err;
10557 }
Jens Axboed1719f72020-07-30 13:43:53 -060010558
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010559 file = io_uring_get_file(ctx);
10560 if (IS_ERR(file)) {
10561 ret = PTR_ERR(file);
10562 goto err;
10563 }
10564
Jens Axboed1719f72020-07-30 13:43:53 -060010565 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010566 * Install ring fd as the very last thing, so we don't risk someone
10567 * having closed it before we finish setup
10568 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010569 ret = io_uring_install_fd(ctx, file);
10570 if (ret < 0) {
10571 /* fput will clean it up */
10572 fput(file);
10573 return ret;
10574 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010575
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010576 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010577 return ret;
10578err:
10579 io_ring_ctx_wait_and_kill(ctx);
10580 return ret;
10581}
10582
10583/*
10584 * Sets up an aio uring context, and returns the fd. Applications asks for a
10585 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10586 * params structure passed in.
10587 */
10588static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10589{
10590 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010591 int i;
10592
10593 if (copy_from_user(&p, params, sizeof(p)))
10594 return -EFAULT;
10595 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10596 if (p.resv[i])
10597 return -EINVAL;
10598 }
10599
Jens Axboe6c271ce2019-01-10 11:22:30 -070010600 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010601 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010602 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10603 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010604 return -EINVAL;
10605
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010606 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010607}
10608
10609SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10610 struct io_uring_params __user *, params)
10611{
10612 return io_uring_setup(entries, params);
10613}
10614
Pavel Begunkovc0724812021-10-04 20:02:54 +010010615static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg,
10616 unsigned nr_args)
Jens Axboe66f4af92020-01-16 15:36:52 -070010617{
10618 struct io_uring_probe *p;
10619 size_t size;
10620 int i, ret;
10621
10622 size = struct_size(p, ops, nr_args);
10623 if (size == SIZE_MAX)
10624 return -EOVERFLOW;
10625 p = kzalloc(size, GFP_KERNEL);
10626 if (!p)
10627 return -ENOMEM;
10628
10629 ret = -EFAULT;
10630 if (copy_from_user(p, arg, size))
10631 goto out;
10632 ret = -EINVAL;
10633 if (memchr_inv(p, 0, size))
10634 goto out;
10635
10636 p->last_op = IORING_OP_LAST - 1;
10637 if (nr_args > IORING_OP_LAST)
10638 nr_args = IORING_OP_LAST;
10639
10640 for (i = 0; i < nr_args; i++) {
10641 p->ops[i].op = i;
10642 if (!io_op_defs[i].not_supported)
10643 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10644 }
10645 p->ops_len = i;
10646
10647 ret = 0;
10648 if (copy_to_user(arg, p, size))
10649 ret = -EFAULT;
10650out:
10651 kfree(p);
10652 return ret;
10653}
10654
Jens Axboe071698e2020-01-28 10:04:42 -070010655static int io_register_personality(struct io_ring_ctx *ctx)
10656{
Jens Axboe4379bf82021-02-15 13:40:22 -070010657 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010658 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010659 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010660
Jens Axboe4379bf82021-02-15 13:40:22 -070010661 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010662
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010663 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10664 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010665 if (ret < 0) {
10666 put_cred(creds);
10667 return ret;
10668 }
10669 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010670}
10671
Pavel Begunkovc0724812021-10-04 20:02:54 +010010672static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
10673 void __user *arg, unsigned int nr_args)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010674{
10675 struct io_uring_restriction *res;
10676 size_t size;
10677 int i, ret;
10678
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010679 /* Restrictions allowed only if rings started disabled */
10680 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10681 return -EBADFD;
10682
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010683 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010684 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010685 return -EBUSY;
10686
10687 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10688 return -EINVAL;
10689
10690 size = array_size(nr_args, sizeof(*res));
10691 if (size == SIZE_MAX)
10692 return -EOVERFLOW;
10693
10694 res = memdup_user(arg, size);
10695 if (IS_ERR(res))
10696 return PTR_ERR(res);
10697
10698 ret = 0;
10699
10700 for (i = 0; i < nr_args; i++) {
10701 switch (res[i].opcode) {
10702 case IORING_RESTRICTION_REGISTER_OP:
10703 if (res[i].register_op >= IORING_REGISTER_LAST) {
10704 ret = -EINVAL;
10705 goto out;
10706 }
10707
10708 __set_bit(res[i].register_op,
10709 ctx->restrictions.register_op);
10710 break;
10711 case IORING_RESTRICTION_SQE_OP:
10712 if (res[i].sqe_op >= IORING_OP_LAST) {
10713 ret = -EINVAL;
10714 goto out;
10715 }
10716
10717 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10718 break;
10719 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10720 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10721 break;
10722 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10723 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10724 break;
10725 default:
10726 ret = -EINVAL;
10727 goto out;
10728 }
10729 }
10730
10731out:
10732 /* Reset all restrictions if an error happened */
10733 if (ret != 0)
10734 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10735 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010736 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010737
10738 kfree(res);
10739 return ret;
10740}
10741
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010742static int io_register_enable_rings(struct io_ring_ctx *ctx)
10743{
10744 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10745 return -EBADFD;
10746
10747 if (ctx->restrictions.registered)
10748 ctx->restricted = 1;
10749
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010750 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10751 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10752 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010753 return 0;
10754}
10755
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010756static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010757 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010758 unsigned nr_args)
10759{
10760 __u32 tmp;
10761 int err;
10762
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010763 if (up->resv)
10764 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010765 if (check_add_overflow(up->offset, nr_args, &tmp))
10766 return -EOVERFLOW;
10767 err = io_rsrc_node_switch_start(ctx);
10768 if (err)
10769 return err;
10770
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010771 switch (type) {
10772 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010773 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010774 case IORING_RSRC_BUFFER:
10775 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010776 }
10777 return -EINVAL;
10778}
10779
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010780static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10781 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010782{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010783 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010784
10785 if (!nr_args)
10786 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010787 memset(&up, 0, sizeof(up));
10788 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10789 return -EFAULT;
10790 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10791}
10792
10793static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010794 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010795{
10796 struct io_uring_rsrc_update2 up;
10797
10798 if (size != sizeof(up))
10799 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010800 if (copy_from_user(&up, arg, sizeof(up)))
10801 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010802 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010803 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010804 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010805}
10806
Pavel Begunkovc0724812021-10-04 20:02:54 +010010807static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010808 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010809{
10810 struct io_uring_rsrc_register rr;
10811
10812 /* keep it extendible */
10813 if (size != sizeof(rr))
10814 return -EINVAL;
10815
10816 memset(&rr, 0, sizeof(rr));
10817 if (copy_from_user(&rr, arg, size))
10818 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010819 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010820 return -EINVAL;
10821
Pavel Begunkov992da012021-06-10 16:37:37 +010010822 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010823 case IORING_RSRC_FILE:
10824 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10825 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010826 case IORING_RSRC_BUFFER:
10827 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10828 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010829 }
10830 return -EINVAL;
10831}
10832
Pavel Begunkovc0724812021-10-04 20:02:54 +010010833static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
10834 void __user *arg, unsigned len)
Jens Axboefe764212021-06-17 10:19:54 -060010835{
10836 struct io_uring_task *tctx = current->io_uring;
10837 cpumask_var_t new_mask;
10838 int ret;
10839
10840 if (!tctx || !tctx->io_wq)
10841 return -EINVAL;
10842
10843 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10844 return -ENOMEM;
10845
10846 cpumask_clear(new_mask);
10847 if (len > cpumask_size())
10848 len = cpumask_size();
10849
10850 if (copy_from_user(new_mask, arg, len)) {
10851 free_cpumask_var(new_mask);
10852 return -EFAULT;
10853 }
10854
10855 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10856 free_cpumask_var(new_mask);
10857 return ret;
10858}
10859
Pavel Begunkovc0724812021-10-04 20:02:54 +010010860static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
Jens Axboefe764212021-06-17 10:19:54 -060010861{
10862 struct io_uring_task *tctx = current->io_uring;
10863
10864 if (!tctx || !tctx->io_wq)
10865 return -EINVAL;
10866
10867 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10868}
10869
Pavel Begunkovc0724812021-10-04 20:02:54 +010010870static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10871 void __user *arg)
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010872 __must_hold(&ctx->uring_lock)
Jens Axboe2e480052021-08-27 11:33:19 -060010873{
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010874 struct io_tctx_node *node;
Jens Axboefa846932021-09-01 14:15:59 -060010875 struct io_uring_task *tctx = NULL;
10876 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010877 __u32 new_count[2];
10878 int i, ret;
10879
Jens Axboe2e480052021-08-27 11:33:19 -060010880 if (copy_from_user(new_count, arg, sizeof(new_count)))
10881 return -EFAULT;
10882 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10883 if (new_count[i] > INT_MAX)
10884 return -EINVAL;
10885
Jens Axboefa846932021-09-01 14:15:59 -060010886 if (ctx->flags & IORING_SETUP_SQPOLL) {
10887 sqd = ctx->sq_data;
10888 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010889 /*
10890 * Observe the correct sqd->lock -> ctx->uring_lock
10891 * ordering. Fine to drop uring_lock here, we hold
10892 * a ref to the ctx.
10893 */
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010894 refcount_inc(&sqd->refs);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010895 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010896 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010897 mutex_lock(&ctx->uring_lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010898 if (sqd->thread)
10899 tctx = sqd->thread->io_uring;
Jens Axboefa846932021-09-01 14:15:59 -060010900 }
10901 } else {
10902 tctx = current->io_uring;
10903 }
10904
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010905 BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits));
Jens Axboefa846932021-09-01 14:15:59 -060010906
Pavel Begunkovbad119b2021-11-08 15:10:03 +000010907 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10908 if (new_count[i])
10909 ctx->iowq_limits[i] = new_count[i];
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010910 ctx->iowq_limits_set = true;
10911
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010912 if (tctx && tctx->io_wq) {
10913 ret = io_wq_max_workers(tctx->io_wq, new_count);
10914 if (ret)
10915 goto err;
10916 } else {
10917 memset(new_count, 0, sizeof(new_count));
10918 }
Jens Axboefa846932021-09-01 14:15:59 -060010919
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010920 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010921 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010922 io_put_sq_data(sqd);
10923 }
Jens Axboe2e480052021-08-27 11:33:19 -060010924
10925 if (copy_to_user(arg, new_count, sizeof(new_count)))
10926 return -EFAULT;
10927
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010928 /* that's it for SQPOLL, only the SQPOLL task creates requests */
10929 if (sqd)
10930 return 0;
10931
10932 /* now propagate the restriction to all registered users */
10933 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
10934 struct io_uring_task *tctx = node->task->io_uring;
10935
10936 if (WARN_ON_ONCE(!tctx->io_wq))
10937 continue;
10938
10939 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10940 new_count[i] = ctx->iowq_limits[i];
10941 /* ignore errors, it always returns zero anyway */
10942 (void)io_wq_max_workers(tctx->io_wq, new_count);
10943 }
Jens Axboe2e480052021-08-27 11:33:19 -060010944 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010945err:
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010946 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010947 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010948 io_put_sq_data(sqd);
10949 }
Jens Axboefa846932021-09-01 14:15:59 -060010950 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010951}
10952
Jens Axboe071698e2020-01-28 10:04:42 -070010953static bool io_register_op_must_quiesce(int op)
10954{
10955 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010956 case IORING_REGISTER_BUFFERS:
10957 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010958 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010959 case IORING_UNREGISTER_FILES:
10960 case IORING_REGISTER_FILES_UPDATE:
10961 case IORING_REGISTER_PROBE:
10962 case IORING_REGISTER_PERSONALITY:
10963 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010964 case IORING_REGISTER_FILES2:
10965 case IORING_REGISTER_FILES_UPDATE2:
10966 case IORING_REGISTER_BUFFERS2:
10967 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010968 case IORING_REGISTER_IOWQ_AFF:
10969 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010970 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010971 return false;
10972 default:
10973 return true;
10974 }
10975}
10976
Pavel Begunkovc0724812021-10-04 20:02:54 +010010977static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx)
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010978{
10979 long ret;
10980
10981 percpu_ref_kill(&ctx->refs);
10982
10983 /*
10984 * Drop uring mutex before waiting for references to exit. If another
10985 * thread is currently inside io_uring_enter() it might need to grab the
10986 * uring_lock to make progress. If we hold it here across the drain
10987 * wait, then we can deadlock. It's safe to drop the mutex here, since
10988 * no new references will come in after we've killed the percpu ref.
10989 */
10990 mutex_unlock(&ctx->uring_lock);
10991 do {
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010992 ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ);
10993 if (ret) {
10994 ret = min(0L, ret);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010995 break;
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010996 }
10997
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010998 ret = io_run_task_work_sig();
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010999 io_req_caches_free(ctx);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010011000 } while (ret >= 0);
11001 mutex_lock(&ctx->uring_lock);
11002
11003 if (ret)
11004 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
11005 return ret;
11006}
11007
Jens Axboeedafcce2019-01-09 09:16:05 -070011008static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
11009 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060011010 __releases(ctx->uring_lock)
11011 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070011012{
11013 int ret;
11014
Jens Axboe35fa71a2019-04-22 10:23:23 -060011015 /*
11016 * We're inside the ring mutex, if the ref is already dying, then
11017 * someone else killed the ctx or is already going through
11018 * io_uring_register().
11019 */
11020 if (percpu_ref_is_dying(&ctx->refs))
11021 return -ENXIO;
11022
Pavel Begunkov75c40212021-04-15 13:07:40 +010011023 if (ctx->restricted) {
11024 if (opcode >= IORING_REGISTER_LAST)
11025 return -EINVAL;
11026 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
11027 if (!test_bit(opcode, ctx->restrictions.register_op))
11028 return -EACCES;
11029 }
11030
Jens Axboe071698e2020-01-28 10:04:42 -070011031 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010011032 ret = io_ctx_quiesce(ctx);
11033 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010011034 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070011035 }
Jens Axboeedafcce2019-01-09 09:16:05 -070011036
11037 switch (opcode) {
11038 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010011039 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070011040 break;
11041 case IORING_UNREGISTER_BUFFERS:
11042 ret = -EINVAL;
11043 if (arg || nr_args)
11044 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080011045 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070011046 break;
Jens Axboe6b063142019-01-10 22:13:58 -070011047 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010011048 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070011049 break;
11050 case IORING_UNREGISTER_FILES:
11051 ret = -EINVAL;
11052 if (arg || nr_args)
11053 break;
11054 ret = io_sqe_files_unregister(ctx);
11055 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060011056 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010011057 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060011058 break;
Jens Axboe9b402842019-04-11 11:45:41 -060011059 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070011060 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060011061 ret = -EINVAL;
11062 if (nr_args != 1)
11063 break;
11064 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070011065 if (ret)
11066 break;
11067 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
11068 ctx->eventfd_async = 1;
11069 else
11070 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060011071 break;
11072 case IORING_UNREGISTER_EVENTFD:
11073 ret = -EINVAL;
11074 if (arg || nr_args)
11075 break;
11076 ret = io_eventfd_unregister(ctx);
11077 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070011078 case IORING_REGISTER_PROBE:
11079 ret = -EINVAL;
11080 if (!arg || nr_args > 256)
11081 break;
11082 ret = io_probe(ctx, arg, nr_args);
11083 break;
Jens Axboe071698e2020-01-28 10:04:42 -070011084 case IORING_REGISTER_PERSONALITY:
11085 ret = -EINVAL;
11086 if (arg || nr_args)
11087 break;
11088 ret = io_register_personality(ctx);
11089 break;
11090 case IORING_UNREGISTER_PERSONALITY:
11091 ret = -EINVAL;
11092 if (arg)
11093 break;
11094 ret = io_unregister_personality(ctx, nr_args);
11095 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020011096 case IORING_REGISTER_ENABLE_RINGS:
11097 ret = -EINVAL;
11098 if (arg || nr_args)
11099 break;
11100 ret = io_register_enable_rings(ctx);
11101 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020011102 case IORING_REGISTER_RESTRICTIONS:
11103 ret = io_register_restrictions(ctx, arg, nr_args);
11104 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010011105 case IORING_REGISTER_FILES2:
11106 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010011107 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010011108 case IORING_REGISTER_FILES_UPDATE2:
11109 ret = io_register_rsrc_update(ctx, arg, nr_args,
11110 IORING_RSRC_FILE);
11111 break;
11112 case IORING_REGISTER_BUFFERS2:
11113 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
11114 break;
11115 case IORING_REGISTER_BUFFERS_UPDATE:
11116 ret = io_register_rsrc_update(ctx, arg, nr_args,
11117 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010011118 break;
Jens Axboefe764212021-06-17 10:19:54 -060011119 case IORING_REGISTER_IOWQ_AFF:
11120 ret = -EINVAL;
11121 if (!arg || !nr_args)
11122 break;
11123 ret = io_register_iowq_aff(ctx, arg, nr_args);
11124 break;
11125 case IORING_UNREGISTER_IOWQ_AFF:
11126 ret = -EINVAL;
11127 if (arg || nr_args)
11128 break;
11129 ret = io_unregister_iowq_aff(ctx);
11130 break;
Jens Axboe2e480052021-08-27 11:33:19 -060011131 case IORING_REGISTER_IOWQ_MAX_WORKERS:
11132 ret = -EINVAL;
11133 if (!arg || nr_args != 2)
11134 break;
11135 ret = io_register_iowq_max_workers(ctx, arg);
11136 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070011137 default:
11138 ret = -EINVAL;
11139 break;
11140 }
11141
Jens Axboe071698e2020-01-28 10:04:42 -070011142 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070011143 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070011144 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060011145 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070011146 }
Jens Axboeedafcce2019-01-09 09:16:05 -070011147 return ret;
11148}
11149
11150SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
11151 void __user *, arg, unsigned int, nr_args)
11152{
11153 struct io_ring_ctx *ctx;
11154 long ret = -EBADF;
11155 struct fd f;
11156
11157 f = fdget(fd);
11158 if (!f.file)
11159 return -EBADF;
11160
11161 ret = -EOPNOTSUPP;
11162 if (f.file->f_op != &io_uring_fops)
11163 goto out_fput;
11164
11165 ctx = f.file->private_data;
11166
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000011167 io_run_task_work();
11168
Jens Axboeedafcce2019-01-09 09:16:05 -070011169 mutex_lock(&ctx->uring_lock);
11170 ret = __io_uring_register(ctx, opcode, arg, nr_args);
11171 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020011172 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
11173 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070011174out_fput:
11175 fdput(f);
11176 return ret;
11177}
11178
Jens Axboe2b188cc2019-01-07 10:46:33 -070011179static int __init io_uring_init(void)
11180{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011181#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
11182 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
11183 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
11184} while (0)
11185
11186#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
11187 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
11188 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
11189 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
11190 BUILD_BUG_SQE_ELEM(1, __u8, flags);
11191 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
11192 BUILD_BUG_SQE_ELEM(4, __s32, fd);
11193 BUILD_BUG_SQE_ELEM(8, __u64, off);
11194 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
11195 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011196 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011197 BUILD_BUG_SQE_ELEM(24, __u32, len);
11198 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
11199 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
11200 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
11201 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080011202 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
11203 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011204 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
11205 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
11206 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
11207 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
11208 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
11209 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
11210 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
11211 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011212 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011213 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
11214 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011215 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011216 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011217 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010011218 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011219
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011220 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
11221 sizeof(struct io_uring_rsrc_update));
11222 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
11223 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010011224
11225 /* ->buf_index is u16 */
11226 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
11227
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011228 /* should fit into one byte */
11229 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
Pavel Begunkov68fe2562021-09-15 12:03:38 +010011230 BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
11231 BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011232
Jens Axboed3656342019-12-18 09:50:26 -070011233 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080011234 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011235
Jens Axboe91f245d2021-02-09 13:48:50 -070011236 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
11237 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070011238 return 0;
11239};
11240__initcall(io_uring_init);