blob: 8b6bfed16f65e7381183d6bbbda757e21c4278c5 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
Pavel Begunkovd068b502021-05-16 22:58:11 +010014 * through a control-dependency in io_get_cqe (smp_store_release to
Stefan Bühler1e84b972019-04-24 23:54:16 +020015 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070075#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070076#include <linux/eventpoll.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030077#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070078#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060079#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060080#include <linux/io_uring.h>
Nadav Amitef98eb02021-08-07 17:13:41 -070081#include <linux/tracehook.h>
Paul Moore5bd21822021-02-16 19:46:48 -050082#include <linux/audit.h>
Paul Moorecdc14042021-02-01 19:56:49 -050083#include <linux/security.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020085#define CREATE_TRACE_POINTS
86#include <trace/events/io_uring.h>
87
Jens Axboe2b188cc2019-01-07 10:46:33 -070088#include <uapi/linux/io_uring.h>
89
90#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060091#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
Daniel Xu5277dea2019-09-14 14:23:45 -070093#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060094#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Olivier Langlois4ce8ad92021-06-23 11:50:18 -070095#define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
Jens Axboe65e19f52019-10-26 07:20:21 -060096
wangyangbo187f08c2021-08-19 13:56:57 +080097/* only define max */
Pavel Begunkov042b0d82021-08-09 13:04:01 +010098#define IORING_MAX_FIXED_FILES (1U << 15)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020099#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
100 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
wangyangbo187f08c2021-08-19 13:56:57 +0800102#define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100103#define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT)
104#define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
105
Pavel Begunkov489809e2021-05-14 12:06:44 +0100106#define IORING_MAX_REG_BUFFERS (1U << 14)
107
Pavel Begunkov68fe2562021-09-15 12:03:38 +0100108#define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \
Pavel Begunkov5562a8d2021-11-10 15:49:34 +0000109 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Pavel Begunkov68fe2562021-09-15 12:03:38 +0100110
Pavel Begunkov5562a8d2021-11-10 15:49:34 +0000111#define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \
112 IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS)
Pavel Begunkov68fe2562021-09-15 12:03:38 +0100113
Pavel Begunkovc8543572021-06-17 18:14:04 +0100114#define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
Pavel Begunkovd886e182021-10-04 20:02:56 +0100115 REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \
116 REQ_F_ASYNC_DATA)
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000117
Pavel Begunkov09899b12021-06-14 02:36:22 +0100118#define IO_TCTX_REFS_CACHE_NR (1U << 10)
119
Jens Axboe2b188cc2019-01-07 10:46:33 -0700120struct io_uring {
121 u32 head ____cacheline_aligned_in_smp;
122 u32 tail ____cacheline_aligned_in_smp;
123};
124
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 * This data is shared with the application through the mmap at offsets
127 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 *
129 * The offsets to the member fields are published through struct
130 * io_sqring_offsets when calling io_uring_setup.
131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200133 /*
134 * Head and tail offsets into the ring; the offsets need to be
135 * masked to get valid indices.
136 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 * The kernel controls head of the sq ring and the tail of the cq ring,
138 * and the application controls tail of the sq ring and the head of the
139 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200140 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000141 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200142 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 * ring_entries - 1)
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_ring_mask, cq_ring_mask;
147 /* Ring sizes (constant, power of 2) */
148 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 /*
150 * Number of invalid entries dropped by the kernel due to
151 * invalid index stored in array
152 *
153 * Written by the kernel, shouldn't be modified by the
154 * application (i.e. get number of "new events" by comparing to
155 * cached value).
156 *
157 * After a new SQ head value was read by the application this
158 * counter includes all submissions that were dropped reaching
159 * the new SQ head (and possibly more).
160 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000161 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200163 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200164 *
165 * Written by the kernel, shouldn't be modified by the
166 * application.
167 *
168 * The application needs a full memory barrier before checking
169 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
170 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000171 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200172 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200173 * Runtime CQ flags
174 *
175 * Written by the application, shouldn't be modified by the
176 * kernel.
177 */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100178 u32 cq_flags;
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200179 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200180 * Number of completion events lost because the queue was full;
181 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800182 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200183 * the completion queue.
184 *
185 * Written by the kernel, shouldn't be modified by the
186 * application (i.e. get number of "new events" by comparing to
187 * cached value).
188 *
189 * As completion events come in out of order this counter is not
190 * ordered with any other data.
191 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000192 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200193 /*
194 * Ring buffer of completion events.
195 *
196 * The kernel writes completion events fresh every time they are
197 * produced, so the application is allowed to modify pending
198 * entries.
199 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000200 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700201};
202
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000203enum io_uring_cmd_flags {
Pavel Begunkov51aac422021-10-14 16:10:17 +0100204 IO_URING_F_COMPLETE_DEFER = 1,
Hao Xu3b44b372021-10-18 21:34:31 +0800205 IO_URING_F_UNLOCKED = 2,
Pavel Begunkov51aac422021-10-14 16:10:17 +0100206 /* int's last bit, sign checks are usually faster than a bit test */
207 IO_URING_F_NONBLOCK = INT_MIN,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000208};
209
Jens Axboeedafcce2019-01-09 09:16:05 -0700210struct io_mapped_ubuf {
211 u64 ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +0100212 u64 ubuf_end;
Jens Axboeedafcce2019-01-09 09:16:05 -0700213 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600214 unsigned long acct_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100215 struct bio_vec bvec[];
Jens Axboeedafcce2019-01-09 09:16:05 -0700216};
217
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000218struct io_ring_ctx;
219
Pavel Begunkov6c2450a2021-02-23 12:40:22 +0000220struct io_overflow_cqe {
221 struct io_uring_cqe cqe;
222 struct list_head list;
223};
224
Pavel Begunkova04b0ac2021-04-01 15:44:04 +0100225struct io_fixed_file {
226 /* file * with additional FFS_* flags */
227 unsigned long file_ptr;
228};
229
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000230struct io_rsrc_put {
231 struct list_head list;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100232 u64 tag;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000233 union {
234 void *rsrc;
235 struct file *file;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +0100236 struct io_mapped_ubuf *buf;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000237 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000238};
239
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100240struct io_file_table {
Pavel Begunkov042b0d82021-08-09 13:04:01 +0100241 struct io_fixed_file *files;
Jens Axboe31b51512019-01-18 22:56:34 -0700242};
243
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100244struct io_rsrc_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800245 struct percpu_ref refs;
246 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000247 struct list_head rsrc_list;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100248 struct io_rsrc_data *rsrc_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600249 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000250 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800251};
252
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100253typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
254
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100255struct io_rsrc_data {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700256 struct io_ring_ctx *ctx;
257
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100258 u64 **tags;
259 unsigned int nr;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100260 rsrc_put_fn *do_put;
Pavel Begunkov3e942492021-04-11 01:46:34 +0100261 atomic_t refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700262 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800263 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700264};
265
Jens Axboe5a2e7452020-02-23 16:23:11 -0700266struct io_buffer {
267 struct list_head list;
268 __u64 addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -0300269 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700270 __u16 bid;
271};
272
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200273struct io_restriction {
274 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
275 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
276 u8 sqe_flags_allowed;
277 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200278 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200279};
280
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700281enum {
282 IO_SQ_THREAD_SHOULD_STOP = 0,
283 IO_SQ_THREAD_SHOULD_PARK,
284};
285
Jens Axboe534ca6d2020-09-02 13:52:19 -0600286struct io_sq_data {
287 refcount_t refs;
Pavel Begunkov9e138a42021-03-14 20:57:12 +0000288 atomic_t park_pending;
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +0000289 struct mutex lock;
Jens Axboe69fb2132020-09-14 11:16:23 -0600290
291 /* ctx's that are using this sqd */
292 struct list_head ctx_list;
Jens Axboe69fb2132020-09-14 11:16:23 -0600293
Jens Axboe534ca6d2020-09-02 13:52:19 -0600294 struct task_struct *thread;
295 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800296
297 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700298 int sq_cpu;
299 pid_t task_pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -0700300 pid_t task_tgid;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700301
302 unsigned long state;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700303 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600304};
305
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000306#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000307#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000308#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000309
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000310struct io_submit_link {
311 struct io_kiocb *head;
312 struct io_kiocb *last;
313};
314
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000315struct io_submit_state {
Pavel Begunkov5a158c62021-10-06 16:06:48 +0100316 /* inline/task_work completion list, under ->uring_lock */
317 struct io_wq_work_node free_list;
318 /* batch completion logic */
319 struct io_wq_work_list compl_reqs;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000320 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000321
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000322 bool plug_started;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +0100323 bool need_plug;
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +0000324 bool flush_cqes;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -0600325 unsigned short submit_nr;
Pavel Begunkov5a158c62021-10-06 16:06:48 +0100326 struct blk_plug plug;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000327};
328
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329struct io_ring_ctx {
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100330 /* const or read-mostly hot data */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331 struct {
332 struct percpu_ref refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700333
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100334 struct io_rings *rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800336 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800337 unsigned int drain_next: 1;
338 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200339 unsigned int restricted: 1;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +0100340 unsigned int off_timeout_used: 1;
Pavel Begunkov10c66902021-06-15 16:47:56 +0100341 unsigned int drain_active: 1;
Pavel Begunkov5562a8d2021-11-10 15:49:34 +0000342 unsigned int drain_disabled: 1;
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100343 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100345 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100346 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100347 struct mutex uring_lock;
348
Hristo Venev75b28af2019-08-26 17:23:46 +0000349 /*
350 * Ring buffer of indices into array of io_uring_sqe, which is
351 * mmapped by the application using the IORING_OFF_SQES offset.
352 *
353 * This indirection could e.g. be used to assign fixed
354 * io_uring_sqe entries to operations and only submit them to
355 * the queue when needed.
356 *
357 * The kernel modifies neither the indices array nor the entries
358 * array.
359 */
360 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100361 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700362 unsigned cached_sq_head;
363 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600364 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100365
366 /*
367 * Fixed resources fast path, should be accessed only under
368 * uring_lock, and updated through io_uring_register(2)
369 */
370 struct io_rsrc_node *rsrc_node;
Pavel Begunkovab409402021-10-09 23:14:41 +0100371 int rsrc_cached_refs;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100372 struct io_file_table file_table;
373 unsigned nr_user_files;
374 unsigned nr_user_bufs;
375 struct io_mapped_ubuf **user_bufs;
376
377 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600378 struct list_head timeout_list;
Pavel Begunkovef9dd632021-08-28 19:54:38 -0600379 struct list_head ltimeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700380 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100381 struct xarray io_buffers;
382 struct xarray personalities;
383 u32 pers_next;
384 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700385 } ____cacheline_aligned_in_smp;
386
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100387 /* IRQ completion list, under ->completion_lock */
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +0100388 struct io_wq_work_list locked_free_list;
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100389 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700390
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100391 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600392 struct io_sq_data *sq_data; /* if using sq thread polling */
393
Jens Axboe90554202020-09-03 12:12:41 -0600394 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600395 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000396
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100397 unsigned long check_cq_overflow;
398
Jens Axboe206aefd2019-11-07 18:27:42 -0700399 struct {
400 unsigned cached_cq_tail;
401 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700402 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100403 struct wait_queue_head cq_wait;
404 unsigned cq_extra;
405 atomic_t cq_timeouts;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100406 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700407 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700408
409 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700411
Jens Axboe89850fc2021-08-10 15:11:51 -0600412 spinlock_t timeout_lock;
413
Jens Axboedef596e2019-01-09 08:59:42 -0700414 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300415 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700416 * io_uring instances that don't use IORING_SETUP_SQPOLL.
417 * For SQPOLL, only the single threaded io_sq_thread() will
418 * manipulate the list, hence no extra locking is needed there.
419 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +0100420 struct io_wq_work_list iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700421 struct hlist_head *cancel_hash;
422 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800423 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700424 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600425
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200426 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700427
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100428 /* slow path rsrc auxilary data, used by update/register */
429 struct {
430 struct io_rsrc_node *rsrc_backup_node;
431 struct io_mapped_ubuf *dummy_ubuf;
432 struct io_rsrc_data *file_data;
433 struct io_rsrc_data *buf_data;
434
435 struct delayed_work rsrc_put_work;
436 struct llist_head rsrc_put_llist;
437 struct list_head rsrc_ref_list;
438 spinlock_t rsrc_ref_lock;
439 };
440
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700441 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100442 struct {
443 #if defined(CONFIG_UNIX)
444 struct socket *ring_sock;
445 #endif
446 /* hashed buffered write serialization */
447 struct io_wq_hash *hash_map;
448
449 /* Only used for accounting purposes */
450 struct user_struct *user;
451 struct mm_struct *mm_account;
452
453 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100454 struct llist_head fallback_llist;
455 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100456 struct work_struct exit_work;
457 struct list_head tctx_list;
458 struct completion ref_comp;
Pavel Begunkove139a1e2021-10-19 23:43:46 +0100459 u32 iowq_limits[2];
460 bool iowq_limits_set;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100461 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700462};
463
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100464struct io_uring_task {
465 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100466 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100467 struct xarray xa;
468 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100469 const struct io_ring_ctx *last;
470 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100471 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100472 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100473 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100474
475 spinlock_t task_lock;
476 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100477 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100478 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100479};
480
Jens Axboe09bb8392019-03-13 12:39:28 -0600481/*
482 * First field must be the file pointer in all the
483 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
484 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700485struct io_poll_iocb {
486 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000487 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700488 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600489 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700490 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700491 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700492};
493
Pavel Begunkov9d805892021-04-13 02:58:40 +0100494struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000495 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100496 u64 old_user_data;
497 u64 new_user_data;
498 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600499 bool update_events;
500 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000501};
502
Jens Axboeb5dba592019-12-11 14:02:38 -0700503struct io_close {
504 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700505 int fd;
Pavel Begunkov7df778b2021-09-24 20:04:29 +0100506 u32 file_slot;
Jens Axboeb5dba592019-12-11 14:02:38 -0700507};
508
Jens Axboead8a48a2019-11-15 08:49:11 -0700509struct io_timeout_data {
510 struct io_kiocb *req;
511 struct hrtimer timer;
512 struct timespec64 ts;
513 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600514 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700515};
516
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700517struct io_accept {
518 struct file *file;
519 struct sockaddr __user *addr;
520 int __user *addr_len;
521 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100522 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600523 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700524};
525
526struct io_sync {
527 struct file *file;
528 loff_t len;
529 loff_t off;
530 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700531 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700532};
533
Jens Axboefbf23842019-12-17 18:45:56 -0700534struct io_cancel {
535 struct file *file;
536 u64 addr;
537};
538
Jens Axboeb29472e2019-12-17 18:50:29 -0700539struct io_timeout {
540 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300541 u32 off;
542 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300543 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000544 /* head of the link, used by linked timeouts only */
545 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600546 /* for linked completions */
547 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700548};
549
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100550struct io_timeout_rem {
551 struct file *file;
552 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000553
554 /* timeout update */
555 struct timespec64 ts;
556 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600557 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100558};
559
Jens Axboe9adbd452019-12-20 08:45:55 -0700560struct io_rw {
561 /* NOTE: kiocb has the file as the first member, so don't do it here */
562 struct kiocb kiocb;
563 u64 addr;
564 u64 len;
565};
566
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700567struct io_connect {
568 struct file *file;
569 struct sockaddr __user *addr;
570 int addr_len;
571};
572
Jens Axboee47293f2019-12-20 08:58:21 -0700573struct io_sr_msg {
574 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700575 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100576 struct compat_msghdr __user *umsg_compat;
577 struct user_msghdr __user *umsg;
578 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700579 };
Jens Axboee47293f2019-12-20 08:58:21 -0700580 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700581 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700582 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700583};
584
Jens Axboe15b71ab2019-12-11 11:20:36 -0700585struct io_open {
586 struct file *file;
587 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100588 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700589 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700590 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600591 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700592};
593
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000594struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700595 struct file *file;
596 u64 arg;
597 u32 nr_args;
598 u32 offset;
599};
600
Jens Axboe4840e412019-12-25 22:03:45 -0700601struct io_fadvise {
602 struct file *file;
603 u64 offset;
604 u32 len;
605 u32 advice;
606};
607
Jens Axboec1ca7572019-12-25 22:18:28 -0700608struct io_madvise {
609 struct file *file;
610 u64 addr;
611 u32 len;
612 u32 advice;
613};
614
Jens Axboe3e4827b2020-01-08 15:18:09 -0700615struct io_epoll {
616 struct file *file;
617 int epfd;
618 int op;
619 int fd;
620 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700621};
622
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300623struct io_splice {
624 struct file *file_out;
625 struct file *file_in;
626 loff_t off_out;
627 loff_t off_in;
628 u64 len;
629 unsigned int flags;
630};
631
Jens Axboeddf0322d2020-02-23 16:41:33 -0700632struct io_provide_buf {
633 struct file *file;
634 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100635 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700636 __u32 bgid;
637 __u16 nbufs;
638 __u16 bid;
639};
640
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700641struct io_statx {
642 struct file *file;
643 int dfd;
644 unsigned int mask;
645 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700646 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700647 struct statx __user *buffer;
648};
649
Jens Axboe36f4fa62020-09-05 11:14:22 -0600650struct io_shutdown {
651 struct file *file;
652 int how;
653};
654
Jens Axboe80a261f2020-09-28 14:23:58 -0600655struct io_rename {
656 struct file *file;
657 int old_dfd;
658 int new_dfd;
659 struct filename *oldpath;
660 struct filename *newpath;
661 int flags;
662};
663
Jens Axboe14a11432020-09-28 14:27:37 -0600664struct io_unlink {
665 struct file *file;
666 int dfd;
667 int flags;
668 struct filename *filename;
669};
670
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700671struct io_mkdir {
672 struct file *file;
673 int dfd;
674 umode_t mode;
675 struct filename *filename;
676};
677
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700678struct io_symlink {
679 struct file *file;
680 int new_dfd;
681 struct filename *oldpath;
682 struct filename *newpath;
683};
684
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700685struct io_hardlink {
686 struct file *file;
687 int old_dfd;
688 int new_dfd;
689 struct filename *oldpath;
690 struct filename *newpath;
691 int flags;
692};
693
Jens Axboef499a022019-12-02 16:28:46 -0700694struct io_async_connect {
695 struct sockaddr_storage address;
696};
697
Jens Axboe03b12302019-12-02 18:50:25 -0700698struct io_async_msghdr {
699 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000700 /* points to an allocated iov, if NULL we use fast_iov instead */
701 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700702 struct sockaddr __user *uaddr;
703 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700704 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700705};
706
Pavel Begunkov538941e2021-10-14 16:10:15 +0100707struct io_rw_state {
Jens Axboeff6165b2020-08-13 09:47:43 -0600708 struct iov_iter iter;
Jens Axboecd658692021-09-10 11:19:14 -0600709 struct iov_iter_state iter_state;
Pavel Begunkovc88598a2021-10-14 16:10:16 +0100710 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov538941e2021-10-14 16:10:15 +0100711};
712
713struct io_async_rw {
714 struct io_rw_state s;
715 const struct iovec *free_iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -0600716 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600717 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700718};
719
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300720enum {
721 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
722 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
723 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
724 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
725 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700726 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000727 REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300728
Pavel Begunkovdddca222021-04-27 16:13:52 +0100729 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100730 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300731 REQ_F_INFLIGHT_BIT,
732 REQ_F_CUR_POS_BIT,
733 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300734 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300735 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700736 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700737 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000738 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600739 REQ_F_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100740 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100741 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100742 REQ_F_ARM_LTIMEOUT_BIT,
Pavel Begunkovd886e182021-10-04 20:02:56 +0100743 REQ_F_ASYNC_DATA_BIT,
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000744 REQ_F_SKIP_LINK_CQES_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700745 /* keep async read/write and isreg together and in order */
Pavel Begunkov35645ac2021-10-17 00:07:09 +0100746 REQ_F_SUPPORT_NOWAIT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700747 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700748
749 /* not a real bit, just to check we're not overflowing the space */
750 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300751};
752
753enum {
754 /* ctx owns file */
755 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
756 /* drain existing IO first */
757 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
758 /* linked sqes */
759 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
760 /* doesn't sever on completion < 0 */
761 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
762 /* IOSQE_ASYNC */
763 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700764 /* IOSQE_BUFFER_SELECT */
765 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000766 /* IOSQE_CQE_SKIP_SUCCESS */
767 REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300768
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300769 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100770 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000771 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300772 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
773 /* read/write uses file position */
774 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
775 /* must not punt to workers */
776 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100777 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300778 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300779 /* needs cleanup */
780 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700781 /* already went through poll handler */
782 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700783 /* buffer already selected */
784 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000785 /* completion is deferred through io_comp_state */
786 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600787 /* caller should reissue async */
788 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov35645ac2021-10-17 00:07:09 +0100789 /* supports async reads/writes */
790 REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700791 /* regular file */
792 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100793 /* has creds assigned */
794 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100795 /* skip refcounting if not set */
796 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100797 /* there is a linked timeout that has to be armed */
798 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Pavel Begunkovd886e182021-10-04 20:02:56 +0100799 /* ->async_data allocated */
800 REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT),
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000801 /* don't post CQEs while failing linked requests */
802 REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700803};
804
805struct async_poll {
806 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600807 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300808};
809
Pavel Begunkovf237c302021-08-18 12:42:46 +0100810typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100811
Jens Axboe7cbf1722021-02-10 00:03:20 +0000812struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100813 union {
814 struct io_wq_work_node node;
815 struct llist_node fallback_node;
816 };
817 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000818};
819
Pavel Begunkov992da012021-06-10 16:37:37 +0100820enum {
821 IORING_RSRC_FILE = 0,
822 IORING_RSRC_BUFFER = 1,
823};
824
Jens Axboe09bb8392019-03-13 12:39:28 -0600825/*
826 * NOTE! Each of the iocb union members has the file pointer
827 * as the first entry in their struct definition. So you can
828 * access the file pointer through any of the sub-structs,
829 * or directly as just 'ki_filp' in this struct.
830 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700831struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700832 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600833 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700834 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700835 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100836 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700837 struct io_accept accept;
838 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700839 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700840 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100841 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700842 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700843 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700844 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700845 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000846 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700847 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700848 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700849 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300850 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700851 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700852 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600853 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600854 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600855 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700856 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700857 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700858 struct io_hardlink hardlink;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700859 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700860
Jens Axboed625c6e2019-12-17 19:53:05 -0700861 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800862 /* polled IO has completed */
863 u8 iopoll_completed;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700864 u16 buf_index;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100865 unsigned int flags;
866
867 u64 user_data;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300868 u32 result;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100869 u32 cflags;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700870
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300871 struct io_ring_ctx *ctx;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300872 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700873
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000874 struct percpu_ref *fixed_rsrc_refs;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100875 /* store used ubuf, so we can prevent reloading */
876 struct io_mapped_ubuf *imu;
Jens Axboed7718a92020-02-14 22:23:12 -0700877
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100878 /* used by request caches, completion batching and iopoll */
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100879 struct io_wq_work_node comp_list;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100880 atomic_t refs;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100881 struct io_kiocb *link;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100882 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300883 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
884 struct hlist_node hash_node;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100885 /* internal polling, see IORING_FEAT_FAST_POLL */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300886 struct async_poll *apoll;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100887 /* opcode allocated if it needs to store data for async defer */
888 void *async_data;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300889 struct io_wq_work work;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100890 /* custom credentials, valid IFF REQ_F_CREDS is set */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100891 const struct cred *creds;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100892 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
Pavel Begunkov30d51dd2021-10-01 18:07:03 +0100893 struct io_buffer *kbuf;
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
1195static inline void req_ref_put(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_put_and_test(req));
1199}
1200
1201static inline void req_ref_get(struct io_kiocb *req)
1202{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001203 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001204 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1205 atomic_inc(&req->refs);
1206}
1207
Pavel Begunkovc4501782021-09-08 16:40:52 +01001208static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
1209{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001210 if (!wq_list_empty(&ctx->submit_state.compl_reqs))
Pavel Begunkovc4501782021-09-08 16:40:52 +01001211 __io_submit_flush_completions(ctx);
1212}
1213
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001214static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001215{
1216 if (!(req->flags & REQ_F_REFCOUNT)) {
1217 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001218 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001219 }
1220}
1221
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001222static inline void io_req_set_refcount(struct io_kiocb *req)
1223{
1224 __io_req_set_refcount(req, 1);
1225}
1226
Pavel Begunkovab409402021-10-09 23:14:41 +01001227#define IO_RSRC_REF_BATCH 100
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001228
Pavel Begunkovab409402021-10-09 23:14:41 +01001229static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
1230 struct io_ring_ctx *ctx)
1231 __must_hold(&ctx->uring_lock)
1232{
1233 struct percpu_ref *ref = req->fixed_rsrc_refs;
1234
1235 if (ref) {
1236 if (ref == &ctx->rsrc_node->refs)
1237 ctx->rsrc_cached_refs++;
1238 else
1239 percpu_ref_put(ref);
1240 }
1241}
1242
1243static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx)
1244{
1245 if (req->fixed_rsrc_refs)
1246 percpu_ref_put(req->fixed_rsrc_refs);
1247}
1248
1249static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
1250 __must_hold(&ctx->uring_lock)
1251{
1252 if (ctx->rsrc_cached_refs) {
1253 percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs);
1254 ctx->rsrc_cached_refs = 0;
1255 }
1256}
1257
1258static void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
1259 __must_hold(&ctx->uring_lock)
1260{
1261 ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
1262 percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH);
1263}
1264
Pavel Begunkova46be972021-10-09 23:14:40 +01001265static inline void io_req_set_rsrc_node(struct io_kiocb *req,
1266 struct io_ring_ctx *ctx)
Jens Axboec40f6372020-06-25 15:39:59 -06001267{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001268 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001269 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Pavel Begunkovab409402021-10-09 23:14:41 +01001270 ctx->rsrc_cached_refs--;
1271 if (unlikely(ctx->rsrc_cached_refs < 0))
1272 io_rsrc_refs_refill(ctx);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001273 }
1274}
1275
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001276static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1277{
1278 bool got = percpu_ref_tryget(ref);
1279
1280 /* already at zero, wait for ->release() */
1281 if (!got)
1282 wait_for_completion(compl);
1283 percpu_ref_resurrect(ref);
1284 if (got)
1285 percpu_ref_put(ref);
1286}
1287
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001288static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1289 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001290{
1291 struct io_kiocb *req;
1292
Pavel Begunkov68207682021-03-22 01:58:25 +00001293 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001294 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001295 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001296 return true;
1297
1298 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001299 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001300 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001301 }
1302 return false;
1303}
1304
Pavel Begunkovd886e182021-10-04 20:02:56 +01001305static inline bool req_has_async_data(struct io_kiocb *req)
1306{
1307 return req->flags & REQ_F_ASYNC_DATA;
1308}
1309
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001310static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001311{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001312 req->flags |= REQ_F_FAIL;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001313 if (req->flags & REQ_F_CQE_SKIP) {
1314 req->flags &= ~REQ_F_CQE_SKIP;
1315 req->flags |= REQ_F_SKIP_LINK_CQES;
1316 }
Jens Axboec40f6372020-06-25 15:39:59 -06001317}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001318
Hao Xua8295b92021-08-27 17:46:09 +08001319static inline void req_fail_link_node(struct io_kiocb *req, int res)
1320{
1321 req_set_fail(req);
1322 req->result = res;
1323}
1324
Pavel Begunkovc0724812021-10-04 20:02:54 +01001325static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001326{
1327 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1328
Jens Axboe0f158b42020-05-14 17:18:39 -06001329 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330}
1331
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001332static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1333{
1334 return !req->timeout.off;
1335}
1336
Pavel Begunkovc0724812021-10-04 20:02:54 +01001337static __cold void io_fallback_req_func(struct work_struct *work)
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001338{
1339 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1340 fallback_work.work);
1341 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1342 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001343 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001344
1345 percpu_ref_get(&ctx->refs);
1346 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001347 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001348
Pavel Begunkovf237c302021-08-18 12:42:46 +01001349 if (locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01001350 io_submit_flush_completions(ctx);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001351 mutex_unlock(&ctx->uring_lock);
1352 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001353 percpu_ref_put(&ctx->refs);
1354}
1355
Pavel Begunkovc0724812021-10-04 20:02:54 +01001356static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001357{
1358 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001359 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001360
1361 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1362 if (!ctx)
1363 return NULL;
1364
Jens Axboe78076bb2019-12-04 19:56:40 -07001365 /*
1366 * Use 5 bits less than the max cq entries, that should give us around
1367 * 32 entries per hash list if totally full and uniformly spread.
1368 */
1369 hash_bits = ilog2(p->cq_entries);
1370 hash_bits -= 5;
1371 if (hash_bits <= 0)
1372 hash_bits = 1;
1373 ctx->cancel_hash_bits = hash_bits;
1374 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1375 GFP_KERNEL);
1376 if (!ctx->cancel_hash)
1377 goto err;
1378 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1379
Pavel Begunkov62248432021-04-28 13:11:29 +01001380 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1381 if (!ctx->dummy_ubuf)
1382 goto err;
1383 /* set invalid range, so io_import_fixed() fails meeting it */
1384 ctx->dummy_ubuf->ubuf = -1UL;
1385
Roman Gushchin21482892019-05-07 10:01:48 -07001386 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001387 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1388 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001389
1390 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001391 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001392 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001393 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001394 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001395 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001396 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001397 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001398 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001399 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001400 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01001401 INIT_WQ_LIST(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001402 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001403 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001404 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001405 spin_lock_init(&ctx->rsrc_ref_lock);
1406 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001407 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1408 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001409 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001410 ctx->submit_state.free_list.next = NULL;
1411 INIT_WQ_LIST(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001412 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001413 INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001414 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001415err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001416 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001417 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001418 kfree(ctx);
1419 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420}
1421
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001422static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1423{
1424 struct io_rings *r = ctx->rings;
1425
1426 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1427 ctx->cq_extra--;
1428}
1429
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001430static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001431{
Jens Axboe2bc99302020-07-09 09:43:27 -06001432 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1433 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001434
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001435 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001436 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001437
Bob Liu9d858b22019-11-13 18:06:25 +08001438 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001439}
1440
Pavel Begunkov35645ac2021-10-17 00:07:09 +01001441#define FFS_NOWAIT 0x1UL
1442#define FFS_ISREG 0x2UL
1443#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG)
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001444
1445static inline bool io_req_ffs_set(struct io_kiocb *req)
1446{
Pavel Begunkov35645ac2021-10-17 00:07:09 +01001447 return req->flags & REQ_F_FIXED_FILE;
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001448}
1449
Pavel Begunkovc0724812021-10-04 20:02:54 +01001450static inline void io_req_track_inflight(struct io_kiocb *req)
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001451{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001452 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001453 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001454 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001455 }
1456}
1457
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001458static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1459{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001460 if (WARN_ON_ONCE(!req->link))
1461 return NULL;
1462
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001463 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1464 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001465
1466 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001467 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001468 __io_req_set_refcount(req->link, 2);
1469 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001470}
1471
1472static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1473{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001474 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001475 return NULL;
1476 return __io_prep_linked_timeout(req);
1477}
1478
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001479static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001480{
Jens Axboed3656342019-12-18 09:50:26 -07001481 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001482 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001483
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001484 if (!(req->flags & REQ_F_CREDS)) {
1485 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001486 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001487 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001488
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001489 req->work.list.next = NULL;
1490 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001491 if (req->flags & REQ_F_FORCE_ASYNC)
1492 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1493
Jens Axboed3656342019-12-18 09:50:26 -07001494 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001495 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001496 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001497 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001498 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001499 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001500 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001501
1502 switch (req->opcode) {
1503 case IORING_OP_SPLICE:
1504 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001505 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1506 req->work.flags |= IO_WQ_WORK_UNBOUND;
1507 break;
1508 }
Jens Axboe561fb042019-10-24 07:25:42 -06001509}
1510
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001511static void io_prep_async_link(struct io_kiocb *req)
1512{
1513 struct io_kiocb *cur;
1514
Pavel Begunkov44eff402021-07-26 14:14:31 +01001515 if (req->flags & REQ_F_LINK_TIMEOUT) {
1516 struct io_ring_ctx *ctx = req->ctx;
1517
Jens Axboe79ebeae2021-08-10 15:18:27 -06001518 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001519 io_for_each_link(cur, req)
1520 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001521 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001522 } else {
1523 io_for_each_link(cur, req)
1524 io_prep_async_work(cur);
1525 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001526}
1527
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001528static inline void io_req_add_compl_list(struct io_kiocb *req)
1529{
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00001530 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001531 struct io_submit_state *state = &req->ctx->submit_state;
1532
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00001533 if (!(req->flags & REQ_F_CQE_SKIP))
1534 ctx->submit_state.flush_cqes = true;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001535 wq_list_add_tail(&req->comp_list, &state->compl_reqs);
1536}
1537
Arnd Bergmann00169242021-10-19 17:34:53 +02001538static void io_queue_async_work(struct io_kiocb *req, bool *dont_use)
Jens Axboe561fb042019-10-24 07:25:42 -06001539{
Jackie Liua197f662019-11-08 08:09:12 -07001540 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001541 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001542 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001543
Jens Axboe3bfe6102021-02-16 14:15:30 -07001544 BUG_ON(!tctx);
1545 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001546
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001547 /* init ->work of the whole link before punting */
1548 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001549
1550 /*
1551 * Not expected to happen, but if we do have a bug where this _can_
1552 * happen, catch it here and ensure the request is marked as
1553 * canceled. That will make io-wq go through the usual work cancel
1554 * procedure rather than attempt to run this request (or create a new
1555 * worker for it).
1556 */
1557 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1558 req->work.flags |= IO_WQ_WORK_CANCEL;
1559
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001560 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1561 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001562 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001563 if (link)
1564 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001565}
1566
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001567static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001568 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001569 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001570{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001571 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001572
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001573 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001574 if (status)
1575 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001576 atomic_set(&req->ctx->cq_timeouts,
1577 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001578 list_del_init(&req->timeout.list);
Pavel Begunkov913a5712021-11-10 15:49:31 +00001579 io_fill_cqe_req(req, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001580 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001581 }
1582}
1583
Pavel Begunkovc0724812021-10-04 20:02:54 +01001584static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001585{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001586 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001587 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1588 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001589
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001590 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001591 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001592 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001593 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001594 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001595 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001596}
1597
Pavel Begunkovc0724812021-10-04 20:02:54 +01001598static __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001599 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001600{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001601 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001602
Jens Axboe79ebeae2021-08-10 15:18:27 -06001603 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001604 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001605 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001606 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001607 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001608
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001609 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001610 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001611
1612 /*
1613 * Since seq can easily wrap around over time, subtract
1614 * the last seq at which timeouts were flushed before comparing.
1615 * Assuming not more than 2^31-1 events have happened since,
1616 * these subtractions won't have wrapped, so we can check if
1617 * target is in [last_seq, current_seq] by comparing the two.
1618 */
1619 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1620 events_got = seq - ctx->cq_last_tm_flush;
1621 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001622 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001623
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001624 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001625 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001626 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001627 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001628 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001629}
1630
Pavel Begunkovc0724812021-10-04 20:02:54 +01001631static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001632{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001633 if (ctx->off_timeout_used)
1634 io_flush_timeouts(ctx);
1635 if (ctx->drain_active)
1636 io_queue_deferred(ctx);
1637}
1638
1639static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1640{
1641 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1642 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001643 /* order cqe stores with ring update */
1644 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001645}
1646
Jens Axboe90554202020-09-03 12:12:41 -06001647static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1648{
1649 struct io_rings *r = ctx->rings;
1650
Pavel Begunkova566c552021-05-16 22:58:08 +01001651 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001652}
1653
Pavel Begunkov888aae22021-01-19 13:32:39 +00001654static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1655{
1656 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1657}
1658
Pavel Begunkovd068b502021-05-16 22:58:11 +01001659static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001660{
Hristo Venev75b28af2019-08-26 17:23:46 +00001661 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001662 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001663
Stefan Bühler115e12e2019-04-24 23:54:18 +02001664 /*
1665 * writes to the cq entry need to come after reading head; the
1666 * control dependency is enough as we're using WRITE_ONCE to
1667 * fill the cq entry
1668 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001669 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001670 return NULL;
1671
Pavel Begunkov888aae22021-01-19 13:32:39 +00001672 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001673 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674}
1675
Jens Axboef2842ab2020-01-08 11:04:00 -07001676static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1677{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001678 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001679 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001680 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1681 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001682 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001683}
1684
Jens Axboe2c5d7632021-08-21 07:21:19 -06001685/*
1686 * This should only get called when at least one event has been posted.
1687 * Some applications rely on the eventfd notification count only changing
1688 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1689 * 1:1 relationship between how many times this function is called (and
1690 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1691 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001692static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001693{
Jens Axboe5fd46172021-08-06 14:04:31 -06001694 /*
1695 * wake_up_all() may seem excessive, but io_wake_function() and
1696 * io_should_wake() handle the termination of the loop and only
1697 * wake as many waiters as we need to.
1698 */
1699 if (wq_has_sleeper(&ctx->cq_wait))
1700 wake_up_all(&ctx->cq_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001701 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001702 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001703}
1704
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001705static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1706{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001707 /* see waitqueue_active() comment */
1708 smp_mb();
1709
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001710 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001711 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001712 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001713 }
1714 if (io_should_trigger_evfd(ctx))
1715 eventfd_signal(ctx->cq_ev_fd, 1);
1716}
1717
Jens Axboec4a2ed72019-11-21 21:01:26 -07001718/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001719static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720{
Jens Axboeb18032b2021-01-24 16:58:56 -07001721 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001722
Pavel Begunkova566c552021-05-16 22:58:08 +01001723 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001724 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001725
Jens Axboeb18032b2021-01-24 16:58:56 -07001726 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001727 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001728 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001729 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001730 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001731
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001732 if (!cqe && !force)
1733 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001734 ocqe = list_first_entry(&ctx->cq_overflow_list,
1735 struct io_overflow_cqe, list);
1736 if (cqe)
1737 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1738 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001739 io_account_cq_overflow(ctx);
1740
Jens Axboeb18032b2021-01-24 16:58:56 -07001741 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001742 list_del(&ocqe->list);
1743 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001744 }
1745
Pavel Begunkov09e88402020-12-17 00:24:38 +00001746 all_flushed = list_empty(&ctx->cq_overflow_list);
1747 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001748 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001749 WRITE_ONCE(ctx->rings->sq_flags,
1750 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001751 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001752
Jens Axboeb18032b2021-01-24 16:58:56 -07001753 if (posted)
1754 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001755 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001756 if (posted)
1757 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001758 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001759}
1760
Pavel Begunkov90f67362021-08-09 20:18:12 +01001761static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001762{
Jens Axboeca0a2652021-03-04 17:15:48 -07001763 bool ret = true;
1764
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001765 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001766 /* iopoll syncs against uring_lock, not completion_lock */
1767 if (ctx->flags & IORING_SETUP_IOPOLL)
1768 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001769 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001770 if (ctx->flags & IORING_SETUP_IOPOLL)
1771 mutex_unlock(&ctx->uring_lock);
1772 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001773
1774 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001775}
1776
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001777/* must to be called somewhat shortly after putting a request */
1778static inline void io_put_task(struct task_struct *task, int nr)
1779{
1780 struct io_uring_task *tctx = task->io_uring;
1781
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001782 if (likely(task == current)) {
1783 tctx->cached_refs += nr;
1784 } else {
1785 percpu_counter_sub(&tctx->inflight, nr);
1786 if (unlikely(atomic_read(&tctx->in_idle)))
1787 wake_up(&tctx->wait);
1788 put_task_struct_many(task, nr);
1789 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001790}
1791
Pavel Begunkov9a108672021-08-27 11:55:01 +01001792static void io_task_refs_refill(struct io_uring_task *tctx)
1793{
1794 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1795
1796 percpu_counter_add(&tctx->inflight, refill);
1797 refcount_add(refill, &current->usage);
1798 tctx->cached_refs += refill;
1799}
1800
1801static inline void io_get_task_refs(int nr)
1802{
1803 struct io_uring_task *tctx = current->io_uring;
1804
1805 tctx->cached_refs -= nr;
1806 if (unlikely(tctx->cached_refs < 0))
1807 io_task_refs_refill(tctx);
1808}
1809
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001810static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001811 s32 res, u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001812{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001813 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001814
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001815 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1816 if (!ocqe) {
1817 /*
1818 * If we're in ring overflow flush mode, or in task cancel mode,
1819 * or cannot allocate an overflow entry, then we need to drop it
1820 * on the floor.
1821 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001822 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001823 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001824 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001825 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001826 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001827 WRITE_ONCE(ctx->rings->sq_flags,
1828 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1829
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001830 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001831 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001832 ocqe->cqe.res = res;
1833 ocqe->cqe.flags = cflags;
1834 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1835 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001836}
1837
Pavel Begunkov913a5712021-11-10 15:49:31 +00001838static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data,
1839 s32 res, u32 cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001840{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001841 struct io_uring_cqe *cqe;
1842
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001843 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001844
1845 /*
1846 * If we can't get a cq entry, userspace overflowed the
1847 * submission (by quite a lot). Increment the overflow count in
1848 * the ring.
1849 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001850 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001851 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001852 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001853 WRITE_ONCE(cqe->res, res);
1854 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001855 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001856 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001857 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001858}
1859
Pavel Begunkov913a5712021-11-10 15:49:31 +00001860static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001861{
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001862 if (!(req->flags & REQ_F_CQE_SKIP))
1863 __io_fill_cqe(req->ctx, req->user_data, res, cflags);
Pavel Begunkov913a5712021-11-10 15:49:31 +00001864}
1865
1866static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data,
1867 s32 res, u32 cflags)
1868{
1869 ctx->cq_extra++;
1870 return __io_fill_cqe(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001871}
1872
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001873static void io_req_complete_post(struct io_kiocb *req, s32 res,
1874 u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001875{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001876 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001877
Jens Axboe79ebeae2021-08-10 15:18:27 -06001878 spin_lock(&ctx->completion_lock);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001879 if (!(req->flags & REQ_F_CQE_SKIP))
1880 __io_fill_cqe(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001881 /*
1882 * If we're the last reference to this request, add to our locked
1883 * free_list cache.
1884 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001885 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001886 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001887 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001888 io_disarm_next(req);
1889 if (req->link) {
1890 io_req_task_queue(req->link);
1891 req->link = NULL;
1892 }
1893 }
Pavel Begunkovab409402021-10-09 23:14:41 +01001894 io_req_put_rsrc(req, ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001895 io_dismantle_req(req);
1896 io_put_task(req->task, 1);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001897 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001898 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001899 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001900 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001901 spin_unlock(&ctx->completion_lock);
Pavel Begunkova3f349072021-09-15 12:04:20 +01001902 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903}
1904
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001905static inline void io_req_complete_state(struct io_kiocb *req, s32 res,
1906 u32 cflags)
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001907{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001908 req->result = res;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +01001909 req->cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001910 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001911}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001912
Pavel Begunkov889fca72021-02-10 00:03:09 +00001913static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001914 s32 res, u32 cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001915{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001916 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1917 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001918 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001919 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001920}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001921
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001922static inline void io_req_complete(struct io_kiocb *req, s32 res)
Jens Axboee1e16092020-06-22 09:17:17 -06001923{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001924 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001925}
1926
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001927static void io_req_complete_failed(struct io_kiocb *req, s32 res)
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001928{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001929 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001930 io_req_complete_post(req, res, 0);
1931}
1932
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01001933static void io_req_complete_fail_submit(struct io_kiocb *req)
1934{
1935 /*
1936 * We don't submit, fail them all, for that replace hardlinks with
1937 * normal links. Extra REQ_F_LINK is tolerated.
1938 */
1939 req->flags &= ~REQ_F_HARDLINK;
1940 req->flags |= REQ_F_LINK;
1941 io_req_complete_failed(req, req->result);
1942}
1943
Pavel Begunkov864ea922021-08-09 13:04:08 +01001944/*
1945 * Don't initialise the fields below on every allocation, but do that in
1946 * advance and keep them valid across allocations.
1947 */
1948static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1949{
1950 req->ctx = ctx;
1951 req->link = NULL;
1952 req->async_data = NULL;
1953 /* not necessary, but safer to zero */
1954 req->result = 0;
1955}
1956
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001957static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001958 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001959{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001960 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001961 wq_list_splice(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001962 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001963 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001964}
1965
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001966/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001967static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001968{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001969 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001970
Jens Axboec7dae4b2021-02-09 19:53:37 -07001971 /*
1972 * If we have more than a batch's worth of requests in our IRQ side
1973 * locked cache, grab the lock and move them over to our submission
1974 * side cache.
1975 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001976 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001977 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001978 return !!state->free_list.next;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001979}
1980
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001981/*
1982 * A request might get retired back into the request caches even before opcode
1983 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1984 * Because of that, io_alloc_req() should be called only under ->uring_lock
1985 * and with extra caution to not get a request that is still worked on.
1986 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01001987static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001988 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001989{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001990 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001991 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001992 void *reqs[IO_REQ_ALLOC_BATCH];
1993 struct io_kiocb *req;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001994 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001995
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001996 if (likely(state->free_list.next || io_flush_cached_reqs(ctx)))
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001997 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001998
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001999 ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002000
Pavel Begunkov864ea922021-08-09 13:04:08 +01002001 /*
2002 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2003 * retry single alloc to be on the safe side.
2004 */
2005 if (unlikely(ret <= 0)) {
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002006 reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2007 if (!reqs[0])
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01002008 return false;
Pavel Begunkov864ea922021-08-09 13:04:08 +01002009 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002010 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01002011
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002012 percpu_ref_get_many(&ctx->refs, ret);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002013 for (i = 0; i < ret; i++) {
2014 req = reqs[i];
2015
2016 io_preinit_req(req, ctx);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002017 wq_stack_add_head(&req->comp_list, &state->free_list);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002018 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01002019 return true;
2020}
2021
2022static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
2023{
2024 if (unlikely(!ctx->submit_state.free_list.next))
2025 return __io_alloc_req_refill(ctx);
2026 return true;
2027}
2028
2029static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
2030{
2031 struct io_wq_work_node *node;
2032
2033 node = wq_stack_extract(&ctx->submit_state.free_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002034 return container_of(node, struct io_kiocb, comp_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002035}
2036
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002037static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002038{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002039 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002040 fput(file);
2041}
2042
Pavel Begunkov6b639522021-09-08 16:40:50 +01002043static inline void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002044{
Pavel Begunkov094bae42021-03-19 17:22:42 +00002045 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002046
Pavel Begunkov867f8fa2021-10-04 20:02:58 +01002047 if (unlikely(flags & IO_REQ_CLEAN_FLAGS))
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01002048 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002049 if (!(flags & REQ_F_FIXED_FILE))
2050 io_put_file(req->file);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002051}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002052
Pavel Begunkovc0724812021-10-04 20:02:54 +01002053static __cold void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002054{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002055 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002056
Pavel Begunkovab409402021-10-09 23:14:41 +01002057 io_req_put_rsrc(req, ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002058 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002059 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002060
Jens Axboe79ebeae2021-08-10 15:18:27 -06002061 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002062 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01002063 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06002064 spin_unlock(&ctx->completion_lock);
Jens Axboee65ef562019-03-12 10:16:44 -06002065}
2066
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002067static inline void io_remove_next_linked(struct io_kiocb *req)
2068{
2069 struct io_kiocb *nxt = req->link;
2070
2071 req->link = nxt->link;
2072 nxt->link = NULL;
2073}
2074
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002075static bool io_kill_linked_timeout(struct io_kiocb *req)
2076 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06002077 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002078{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002079 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002080
Pavel Begunkovb97e7362021-08-15 10:40:23 +01002081 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002082 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002083
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002084 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002085 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01002086 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06002087 list_del(&link->timeout.list);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002088 /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */
Pavel Begunkov913a5712021-11-10 15:49:31 +00002089 io_fill_cqe_req(link, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002090 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002091 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002092 }
2093 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002094 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002095}
2096
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002097static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002098 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002099{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002100 struct io_kiocb *nxt, *link = req->link;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002101 bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES;
Jens Axboe9e645e112019-05-10 16:07:28 -06002102
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002103 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002104 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002105 long res = -ECANCELED;
2106
2107 if (link->flags & REQ_F_FAIL)
2108 res = link->result;
2109
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002110 nxt = link->link;
2111 link->link = NULL;
2112
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002113 trace_io_uring_fail_link(req, link);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002114 if (!ignore_cqes) {
2115 link->flags &= ~REQ_F_CQE_SKIP;
2116 io_fill_cqe_req(link, res, 0);
2117 }
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002118 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002119 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002120 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002121}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002122
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002123static bool io_disarm_next(struct io_kiocb *req)
2124 __must_hold(&req->ctx->completion_lock)
2125{
2126 bool posted = false;
2127
Pavel Begunkov0756a862021-08-15 10:40:25 +01002128 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2129 struct io_kiocb *link = req->link;
2130
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002131 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002132 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2133 io_remove_next_linked(req);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002134 /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */
Pavel Begunkov913a5712021-11-10 15:49:31 +00002135 io_fill_cqe_req(link, -ECANCELED, 0);
Pavel Begunkov0756a862021-08-15 10:40:25 +01002136 io_put_req_deferred(link);
2137 posted = true;
2138 }
2139 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002140 struct io_ring_ctx *ctx = req->ctx;
2141
2142 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002143 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002144 spin_unlock_irq(&ctx->timeout_lock);
2145 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002146 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002147 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002148 posted |= (req->link != NULL);
2149 io_fail_links(req);
2150 }
2151 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002152}
2153
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002154static void __io_req_find_next_prep(struct io_kiocb *req)
2155{
2156 struct io_ring_ctx *ctx = req->ctx;
2157 bool posted;
2158
2159 spin_lock(&ctx->completion_lock);
2160 posted = io_disarm_next(req);
2161 if (posted)
2162 io_commit_cqring(req->ctx);
2163 spin_unlock(&ctx->completion_lock);
2164 if (posted)
2165 io_cqring_ev_posted(ctx);
2166}
2167
2168static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002169{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002170 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002171
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002172 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2173 return NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002174 /*
2175 * If LINK is set, we have dependent requests in this chain. If we
2176 * didn't fail this request, queue the first one up, moving any other
2177 * dependencies to the next request. In case of failure, fail the rest
2178 * of the chain.
2179 */
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002180 if (unlikely(req->flags & IO_DISARM_MASK))
2181 __io_req_find_next_prep(req);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002182 nxt = req->link;
2183 req->link = NULL;
2184 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002185}
Jens Axboe2665abf2019-11-05 12:40:47 -07002186
Pavel Begunkovf237c302021-08-18 12:42:46 +01002187static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002188{
2189 if (!ctx)
2190 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002191 if (*locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01002192 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002193 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002194 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002195 }
2196 percpu_ref_put(&ctx->refs);
2197}
2198
Jens Axboe7cbf1722021-02-10 00:03:20 +00002199static void tctx_task_work(struct callback_head *cb)
2200{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002201 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002202 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002203 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2204 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002205
Pavel Begunkov16f72072021-06-17 18:14:09 +01002206 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002207 struct io_wq_work_node *node;
2208
Pavel Begunkovc4501782021-09-08 16:40:52 +01002209 if (!tctx->task_list.first && locked)
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002210 io_submit_flush_completions(ctx);
2211
Pavel Begunkov3f184072021-06-17 18:14:06 +01002212 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002213 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002214 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002215 if (!node)
2216 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002217 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002218 if (!node)
2219 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002220
Pavel Begunkov6294f362021-08-10 17:53:55 +01002221 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002222 struct io_wq_work_node *next = node->next;
2223 struct io_kiocb *req = container_of(node, struct io_kiocb,
2224 io_task_work.node);
2225
2226 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002227 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002228 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002229 /* if not contended, grab and improve batching */
2230 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002231 percpu_ref_get(&ctx->refs);
2232 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002233 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002234 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002235 } while (node);
2236
Jens Axboe7cbf1722021-02-10 00:03:20 +00002237 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002238 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002239
Pavel Begunkovf237c302021-08-18 12:42:46 +01002240 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002241}
2242
Pavel Begunkove09ee512021-07-01 13:26:05 +01002243static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002244{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002245 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002246 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002247 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002248 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002249 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002250 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002251
2252 WARN_ON_ONCE(!tctx);
2253
Jens Axboe0b81e802021-02-16 10:33:53 -07002254 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002255 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002256 running = tctx->task_running;
2257 if (!running)
2258 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002259 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002260
2261 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002262 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002263 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002264
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002265 /*
2266 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2267 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2268 * processing task_work. There's no reliable way to tell if TWA_RESUME
2269 * will do the job.
2270 */
2271 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovd97ec622021-09-08 16:40:53 +01002272 if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2273 if (notify == TWA_NONE)
2274 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002275 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002276 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002277
Pavel Begunkove09ee512021-07-01 13:26:05 +01002278 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002279 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002280 node = tctx->task_list.first;
2281 INIT_WQ_LIST(&tctx->task_list);
2282 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002283
Pavel Begunkove09ee512021-07-01 13:26:05 +01002284 while (node) {
2285 req = container_of(node, struct io_kiocb, io_task_work.node);
2286 node = node->next;
2287 if (llist_add(&req->io_task_work.fallback_node,
2288 &req->ctx->fallback_llist))
2289 schedule_delayed_work(&req->ctx->fallback_work, 1);
2290 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002291}
2292
Pavel Begunkovf237c302021-08-18 12:42:46 +01002293static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002294{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002295 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002296
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002297 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002298 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002299 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002300}
2301
Pavel Begunkovf237c302021-08-18 12:42:46 +01002302static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002303{
2304 struct io_ring_ctx *ctx = req->ctx;
2305
Pavel Begunkovf237c302021-08-18 12:42:46 +01002306 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002307 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002308 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002309 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002310 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002311 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002312}
2313
Pavel Begunkova3df76982021-02-18 22:32:52 +00002314static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2315{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002316 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002317 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002318 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002319}
2320
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002321static void io_req_task_queue(struct io_kiocb *req)
2322{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002323 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002324 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002325}
2326
Jens Axboe773af692021-07-27 10:25:55 -06002327static void io_req_task_queue_reissue(struct io_kiocb *req)
2328{
2329 req->io_task_work.func = io_queue_async_work;
2330 io_req_task_work_add(req);
2331}
2332
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002333static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002334{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002335 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002336
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002337 if (nxt)
2338 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002339}
2340
Jens Axboe9e645e112019-05-10 16:07:28 -06002341static void io_free_req(struct io_kiocb *req)
2342{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002343 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002344 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002345}
2346
Pavel Begunkovf237c302021-08-18 12:42:46 +01002347static void io_free_req_work(struct io_kiocb *req, bool *locked)
2348{
2349 io_free_req(req);
2350}
2351
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002352static void io_free_batch_list(struct io_ring_ctx *ctx,
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002353 struct io_wq_work_node *node)
Jens Axboea141dd82021-08-12 12:48:34 -06002354 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002355{
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002356 struct task_struct *task = NULL;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002357 int task_refs = 0;
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002358
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002359 do {
2360 struct io_kiocb *req = container_of(node, struct io_kiocb,
2361 comp_list);
2362
Pavel Begunkovdef77ac2021-10-12 15:02:14 +01002363 if (unlikely(req->flags & REQ_F_REFCOUNT)) {
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002364 node = req->comp_list.next;
Pavel Begunkovdef77ac2021-10-12 15:02:14 +01002365 if (!req_ref_put_and_test(req))
2366 continue;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002367 }
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002368
Pavel Begunkovab409402021-10-09 23:14:41 +01002369 io_req_put_rsrc_locked(req, ctx);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002370 io_queue_next(req);
2371 io_dismantle_req(req);
2372
2373 if (req->task != task) {
2374 if (task)
2375 io_put_task(task, task_refs);
2376 task = req->task;
2377 task_refs = 0;
2378 }
2379 task_refs++;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002380 node = req->comp_list.next;
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002381 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002382 } while (node);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002383
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002384 if (task)
2385 io_put_task(task, task_refs);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002386}
2387
Pavel Begunkovc4501782021-09-08 16:40:52 +01002388static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002389 __must_hold(&ctx->uring_lock)
2390{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002391 struct io_wq_work_node *node, *prev;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002392 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002393
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00002394 if (state->flush_cqes) {
2395 spin_lock(&ctx->completion_lock);
2396 wq_list_for_each(node, prev, &state->compl_reqs) {
2397 struct io_kiocb *req = container_of(node, struct io_kiocb,
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002398 comp_list);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002399
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00002400 if (!(req->flags & REQ_F_CQE_SKIP))
2401 __io_fill_cqe(ctx, req->user_data, req->result,
2402 req->cflags);
2403 }
2404
2405 io_commit_cqring(ctx);
2406 spin_unlock(&ctx->completion_lock);
2407 io_cqring_ev_posted(ctx);
2408 state->flush_cqes = false;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002409 }
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002410
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002411 io_free_batch_list(ctx, state->compl_reqs.first);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002412 INIT_WQ_LIST(&state->compl_reqs);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002413}
2414
Jens Axboeba816ad2019-09-28 11:36:45 -06002415/*
2416 * Drop reference to request, return next in chain (if there is one) if this
2417 * was the last reference to this request.
2418 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002419static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002420{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002421 struct io_kiocb *nxt = NULL;
2422
Jens Axboede9b4cc2021-02-24 13:28:27 -07002423 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002424 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002425 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002426 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002427 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002428}
2429
Pavel Begunkov0d850352021-03-19 17:22:37 +00002430static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002431{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002432 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002433 io_free_req(req);
2434}
2435
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002436static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002437{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002438 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002439 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002440 io_req_task_work_add(req);
2441 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002442}
2443
Pavel Begunkov6c503152021-01-04 20:36:36 +00002444static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002445{
2446 /* See comment at the top of this file */
2447 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002448 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002449}
2450
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002451static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2452{
2453 struct io_rings *rings = ctx->rings;
2454
2455 /* make sure SQ entry isn't read before tail */
2456 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2457}
2458
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002459static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002460{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002461 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002462
Jens Axboebcda7ba2020-02-23 16:42:51 -07002463 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2464 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002465 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002466 kfree(kbuf);
2467 return cflags;
2468}
2469
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002470static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2471{
Pavel Begunkovae421d92021-08-17 20:28:08 +01002472 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2473 return 0;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01002474 return io_put_kbuf(req, req->kbuf);
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002475}
2476
Jens Axboe4c6e2772020-07-01 11:29:10 -06002477static inline bool io_run_task_work(void)
2478{
Nadav Amitef98eb02021-08-07 17:13:41 -07002479 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002480 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002481 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002482 return true;
2483 }
2484
2485 return false;
2486}
2487
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002488static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
Jens Axboedef596e2019-01-09 08:59:42 -07002489{
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002490 struct io_wq_work_node *pos, *start, *prev;
Christoph Hellwigd729cf92021-10-12 13:12:20 +02002491 unsigned int poll_flags = BLK_POLL_NOSLEEP;
Jens Axboeb688f112021-10-12 09:28:46 -06002492 DEFINE_IO_COMP_BATCH(iob);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002493 int nr_events = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002494
2495 /*
2496 * Only spin for completions if we don't have multiple devices hanging
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002497 * off our complete list.
Jens Axboedef596e2019-01-09 08:59:42 -07002498 */
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002499 if (ctx->poll_multi_queue || force_nonspin)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002500 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002501
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002502 wq_list_for_each(pos, start, &ctx->iopoll_list) {
2503 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002504 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002505 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002506
2507 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002508 * Move completed and retryable entries to our local lists.
2509 * If we find a request that requires polling, break out
2510 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002511 */
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002512 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002513 break;
2514
Jens Axboeb688f112021-10-12 09:28:46 -06002515 ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002516 if (unlikely(ret < 0))
2517 return ret;
2518 else if (ret)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002519 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002520
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002521 /* iopoll may have completed current req */
Jens Axboeb688f112021-10-12 09:28:46 -06002522 if (!rq_list_empty(iob.req_list) ||
2523 READ_ONCE(req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002524 break;
Jens Axboedef596e2019-01-09 08:59:42 -07002525 }
2526
Jens Axboeb688f112021-10-12 09:28:46 -06002527 if (!rq_list_empty(iob.req_list))
2528 iob.complete(&iob);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002529 else if (!pos)
2530 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002531
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002532 prev = start;
2533 wq_list_for_each_resume(pos, prev) {
2534 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002535 u32 cflags;
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002536
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002537 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
2538 if (!smp_load_acquire(&req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002539 break;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002540 cflags = io_put_rw_kbuf(req);
2541 if (!(req->flags & REQ_F_CQE_SKIP))
2542 __io_fill_cqe(ctx, req->user_data, req->result, cflags);
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002543 nr_events++;
2544 }
Jens Axboedef596e2019-01-09 08:59:42 -07002545
Pavel Begunkovf5ed3bc2021-09-24 21:59:52 +01002546 if (unlikely(!nr_events))
2547 return 0;
2548
2549 io_commit_cqring(ctx);
2550 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002551 pos = start ? start->next : ctx->iopoll_list.first;
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002552 wq_list_cut(&ctx->iopoll_list, prev, start);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002553 io_free_batch_list(ctx, pos);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002554 return nr_events;
Jens Axboedef596e2019-01-09 08:59:42 -07002555}
2556
2557/*
Jens Axboedef596e2019-01-09 08:59:42 -07002558 * We can't just wait for polled events to come to us, we have to actively
2559 * find and complete them.
2560 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01002561static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002562{
2563 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2564 return;
2565
2566 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002567 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002568 /* let it sleep and repeat later if can't complete a request */
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002569 if (io_do_iopoll(ctx, true) == 0)
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002570 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002571 /*
2572 * Ensure we allow local-to-the-cpu processing to take place,
2573 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002574 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002575 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002576 if (need_resched()) {
2577 mutex_unlock(&ctx->uring_lock);
2578 cond_resched();
2579 mutex_lock(&ctx->uring_lock);
2580 }
Jens Axboedef596e2019-01-09 08:59:42 -07002581 }
2582 mutex_unlock(&ctx->uring_lock);
2583}
2584
Pavel Begunkov7668b922020-07-07 16:36:21 +03002585static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002586{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002587 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002588 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002589
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002590 /*
2591 * We disallow the app entering submit/complete with polling, but we
2592 * still need to lock the ring to prevent racing with polled issue
2593 * that got punted to a workqueue.
2594 */
2595 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002596 /*
2597 * Don't enter poll loop if we already have events pending.
2598 * If we do, we can potentially be spinning for commands that
2599 * already triggered a CQE (eg in error).
2600 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002601 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002602 __io_cqring_overflow_flush(ctx, false);
2603 if (io_cqring_events(ctx))
2604 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002605 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002606 /*
2607 * If a submit got punted to a workqueue, we can have the
2608 * application entering polling for a command before it gets
2609 * issued. That app will hold the uring_lock for the duration
2610 * of the poll right here, so we need to take a breather every
2611 * now and then to ensure that the issue has a chance to add
2612 * the poll to the issued list. Otherwise we can spin here
2613 * forever, while the workqueue is stuck trying to acquire the
2614 * very same mutex.
2615 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002616 if (wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002617 u32 tail = ctx->cached_cq_tail;
2618
Jens Axboe500f9fb2019-08-19 12:15:59 -06002619 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002620 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002621 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002622
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002623 /* some requests don't go through iopoll_list */
2624 if (tail != ctx->cached_cq_tail ||
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002625 wq_list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002626 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002627 }
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002628 ret = io_do_iopoll(ctx, !min);
2629 if (ret < 0)
2630 break;
2631 nr_events += ret;
2632 ret = 0;
2633 } while (nr_events < min && !need_resched());
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002634out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002635 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002636 return ret;
2637}
2638
Jens Axboe491381ce2019-10-17 09:20:46 -06002639static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002640{
Jens Axboe491381ce2019-10-17 09:20:46 -06002641 /*
2642 * Tell lockdep we inherited freeze protection from submission
2643 * thread.
2644 */
2645 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002646 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002647
Pavel Begunkov1c986792021-03-22 01:58:31 +00002648 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2649 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650 }
2651}
2652
Jens Axboeb63534c2020-06-04 11:28:00 -06002653#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002654static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002655{
Pavel Begunkovab454432021-03-22 01:58:33 +00002656 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002657
Pavel Begunkovd886e182021-10-04 20:02:56 +01002658 if (!req_has_async_data(req))
Pavel Begunkovab454432021-03-22 01:58:33 +00002659 return !io_req_prep_async(req);
Pavel Begunkov538941e2021-10-14 16:10:15 +01002660 iov_iter_restore(&rw->s.iter, &rw->s.iter_state);
Pavel Begunkovab454432021-03-22 01:58:33 +00002661 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002662}
Jens Axboeb63534c2020-06-04 11:28:00 -06002663
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002664static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002665{
Jens Axboe355afae2020-09-02 09:30:31 -06002666 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002667 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002668
Jens Axboe355afae2020-09-02 09:30:31 -06002669 if (!S_ISBLK(mode) && !S_ISREG(mode))
2670 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002671 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2672 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002673 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002674 /*
2675 * If ref is dying, we might be running poll reap from the exit work.
2676 * Don't attempt to reissue from that path, just let it fail with
2677 * -EAGAIN.
2678 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002679 if (percpu_ref_is_dying(&ctx->refs))
2680 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002681 /*
2682 * Play it safe and assume not safe to re-import and reissue if we're
2683 * not in the original thread group (or in task context).
2684 */
2685 if (!same_thread_group(req->task, current) || !in_task())
2686 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002687 return true;
2688}
Jens Axboee82ad482021-04-02 19:45:34 -06002689#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002690static bool io_resubmit_prep(struct io_kiocb *req)
2691{
2692 return false;
2693}
Jens Axboee82ad482021-04-02 19:45:34 -06002694static bool io_rw_should_reissue(struct io_kiocb *req)
2695{
2696 return false;
2697}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002698#endif
2699
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002700static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002701{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002702 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2703 kiocb_end_write(req);
Pavel Begunkov258f3a72021-10-14 16:10:14 +01002704 if (unlikely(res != req->result)) {
Pavel Begunkov9532b992021-03-22 01:58:34 +00002705 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2706 io_rw_should_reissue(req)) {
2707 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002708 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002709 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002710 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002711 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002712 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002713 return false;
2714}
2715
Pavel Begunkovf237c302021-08-18 12:42:46 +01002716static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002717{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002718 unsigned int cflags = io_put_rw_kbuf(req);
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01002719 int res = req->result;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002720
2721 if (*locked) {
Pavel Begunkov126180b2021-08-18 12:42:47 +01002722 io_req_complete_state(req, res, cflags);
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002723 io_req_add_compl_list(req);
Pavel Begunkov126180b2021-08-18 12:42:47 +01002724 } else {
2725 io_req_complete_post(req, res, cflags);
2726 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002727}
2728
2729static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2730 unsigned int issue_flags)
2731{
2732 if (__io_complete_rw_common(req, res))
2733 return;
Pavel Begunkov63637852021-09-02 00:38:22 +01002734 __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002735}
2736
Jens Axboe6b19b762021-10-21 09:22:35 -06002737static void io_complete_rw(struct kiocb *kiocb, long res)
Jens Axboeba816ad2019-09-28 11:36:45 -06002738{
Jens Axboe9adbd452019-12-20 08:45:55 -07002739 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002740
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002741 if (__io_complete_rw_common(req, res))
2742 return;
2743 req->result = res;
2744 req->io_task_work.func = io_req_task_complete;
2745 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002746}
2747
Jens Axboe6b19b762021-10-21 09:22:35 -06002748static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
Jens Axboedef596e2019-01-09 08:59:42 -07002749{
Jens Axboe9adbd452019-12-20 08:45:55 -07002750 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002751
Jens Axboe491381ce2019-10-17 09:20:46 -06002752 if (kiocb->ki_flags & IOCB_WRITE)
2753 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002754 if (unlikely(res != req->result)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002755 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2756 req->flags |= REQ_F_REISSUE;
2757 return;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002758 }
Pavel Begunkov258f3a72021-10-14 16:10:14 +01002759 req->result = res;
Pavel Begunkov8c130822021-03-22 01:58:32 +00002760 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002761
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002762 /* order with io_iopoll_complete() checking ->iopoll_completed */
2763 smp_store_release(&req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002764}
2765
2766/*
2767 * After the iocb has been issued, it's safe to be found on the poll list.
2768 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002769 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002770 * accessing the kiocb cookie.
2771 */
Pavel Begunkov98821312021-10-15 17:09:12 +01002772static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboedef596e2019-01-09 08:59:42 -07002773{
2774 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08002775 const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002776
2777 /* workqueue context doesn't hold uring_lock, grab it now */
Hao Xu3b44b372021-10-18 21:34:31 +08002778 if (unlikely(needs_lock))
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002779 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002780
2781 /*
2782 * Track whether we have multiple files in our lists. This will impact
2783 * how we do polling eventually, not spinning if we're on potentially
2784 * different devices.
2785 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002786 if (wq_list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002787 ctx->poll_multi_queue = false;
2788 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002789 struct io_kiocb *list_req;
2790
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002791 list_req = container_of(ctx->iopoll_list.first, struct io_kiocb,
2792 comp_list);
Christoph Hellwig30da1b42021-10-12 13:12:14 +02002793 if (list_req->file != req->file)
Hao Xu915b3dd2021-06-28 05:37:30 +08002794 ctx->poll_multi_queue = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002795 }
2796
2797 /*
2798 * For fast devices, IO may have already completed. If it has, add
2799 * it to the front so we find it first.
2800 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002801 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002802 wq_list_add_head(&req->comp_list, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002803 else
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002804 wq_list_add_tail(&req->comp_list, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002805
Hao Xu3b44b372021-10-18 21:34:31 +08002806 if (unlikely(needs_lock)) {
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002807 /*
2808 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2809 * in sq thread task context or in io worker task context. If
2810 * current task context is sq thread, we don't need to check
2811 * whether should wake up sq thread.
2812 */
2813 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2814 wq_has_sleeper(&ctx->sq_data->wait))
2815 wake_up(&ctx->sq_data->wait);
2816
2817 mutex_unlock(&ctx->uring_lock);
2818 }
Jens Axboedef596e2019-01-09 08:59:42 -07002819}
2820
Jens Axboe4503b762020-06-01 10:00:27 -06002821static bool io_bdev_nowait(struct block_device *bdev)
2822{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002823 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002824}
2825
Jens Axboe2b188cc2019-01-07 10:46:33 -07002826/*
2827 * If we tracked the file through the SCM inflight mechanism, we could support
2828 * any file. For now, just ensure that anything potentially problematic is done
2829 * inline.
2830 */
Pavel Begunkov88459b52021-10-17 00:07:10 +01002831static bool __io_file_supports_nowait(struct file *file, umode_t mode)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002832{
Jens Axboe4503b762020-06-01 10:00:27 -06002833 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002834 if (IS_ENABLED(CONFIG_BLOCK) &&
2835 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002836 return true;
2837 return false;
2838 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002839 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002840 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002841 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002842 if (IS_ENABLED(CONFIG_BLOCK) &&
2843 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002844 file->f_op != &io_uring_fops)
2845 return true;
2846 return false;
2847 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848
Jens Axboec5b85622020-06-09 19:23:05 -06002849 /* any ->read/write should understand O_NONBLOCK */
2850 if (file->f_flags & O_NONBLOCK)
2851 return true;
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002852 return file->f_mode & FMODE_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002853}
2854
Pavel Begunkov88459b52021-10-17 00:07:10 +01002855/*
2856 * If we tracked the file through the SCM inflight mechanism, we could support
2857 * any file. For now, just ensure that anything potentially problematic is done
2858 * inline.
2859 */
2860static unsigned int io_file_get_flags(struct file *file)
Jens Axboe7b29f922021-03-12 08:30:14 -07002861{
Pavel Begunkov88459b52021-10-17 00:07:10 +01002862 umode_t mode = file_inode(file)->i_mode;
2863 unsigned int res = 0;
Jens Axboe7b29f922021-03-12 08:30:14 -07002864
Pavel Begunkov88459b52021-10-17 00:07:10 +01002865 if (S_ISREG(mode))
2866 res |= FFS_ISREG;
2867 if (__io_file_supports_nowait(file, mode))
2868 res |= FFS_NOWAIT;
2869 return res;
Jens Axboe7b29f922021-03-12 08:30:14 -07002870}
2871
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002872static inline bool io_file_supports_nowait(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002873{
Pavel Begunkov88459b52021-10-17 00:07:10 +01002874 return req->flags & REQ_F_SUPPORT_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002875}
2876
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01002877static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002878{
Jens Axboedef596e2019-01-09 08:59:42 -07002879 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002880 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002881 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002882 unsigned ioprio;
2883 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002884
Pavel Begunkov88459b52021-10-17 00:07:10 +01002885 if (!io_req_ffs_set(req))
2886 req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT;
Jens Axboe491381ce2019-10-17 09:20:46 -06002887
Jens Axboe2b188cc2019-01-07 10:46:33 -07002888 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002889 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002890 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002891 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002892 }
Pavel Begunkov5cb03d62021-10-15 17:09:16 +01002893 kiocb->ki_flags = iocb_flags(file);
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002894 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2895 if (unlikely(ret))
2896 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002897
Jens Axboe5d329e12021-09-14 11:08:37 -06002898 /*
2899 * If the file is marked O_NONBLOCK, still allow retry for it if it
2900 * supports async. Otherwise it's impossible to use O_NONBLOCK files
2901 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2902 */
2903 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002904 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req)))
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002905 req->flags |= REQ_F_NOWAIT;
2906
Jens Axboedef596e2019-01-09 08:59:42 -07002907 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov5cb03d62021-10-15 17:09:16 +01002908 if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002909 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002910
Jens Axboe394918e2021-03-08 11:40:23 -07002911 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
Jens Axboedef596e2019-01-09 08:59:42 -07002912 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002913 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002914 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002915 if (kiocb->ki_flags & IOCB_HIPRI)
2916 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002917 kiocb->ki_complete = io_complete_rw;
2918 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002919
Pavel Begunkovfb272742021-10-23 12:14:02 +01002920 ioprio = READ_ONCE(sqe->ioprio);
2921 if (ioprio) {
2922 ret = ioprio_check_cap(ioprio);
2923 if (ret)
2924 return ret;
2925
2926 kiocb->ki_ioprio = ioprio;
2927 } else {
2928 kiocb->ki_ioprio = get_current_ioprio();
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002929 }
2930
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01002931 req->imu = NULL;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002932 req->rw.addr = READ_ONCE(sqe->addr);
2933 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002934 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002935 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002936}
2937
2938static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2939{
2940 switch (ret) {
2941 case -EIOCBQUEUED:
2942 break;
2943 case -ERESTARTSYS:
2944 case -ERESTARTNOINTR:
2945 case -ERESTARTNOHAND:
2946 case -ERESTART_RESTARTBLOCK:
2947 /*
2948 * We can't just restart the syscall, since previously
2949 * submitted sqes may already be in progress. Just fail this
2950 * IO with EINTR.
2951 */
2952 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002953 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002954 default:
Jens Axboe6b19b762021-10-21 09:22:35 -06002955 kiocb->ki_complete(kiocb, ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002956 }
2957}
2958
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00002959static void kiocb_done(struct io_kiocb *req, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002960 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002961{
Jens Axboee8c2bc12020-08-15 18:44:09 -07002962 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002963
Jens Axboe227c0c92020-08-13 11:51:40 -06002964 /* add previously done IO, if any */
Pavel Begunkovd886e182021-10-04 20:02:56 +01002965 if (req_has_async_data(req) && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002966 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002967 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002968 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002969 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002970 }
2971
Jens Axboeba042912019-12-25 16:33:42 -07002972 if (req->flags & REQ_F_CUR_POS)
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00002973 req->file->f_pos = req->rw.kiocb.ki_pos;
2974 if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002975 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002976 else
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00002977 io_rw_done(&req->rw.kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002978
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002979 if (req->flags & REQ_F_REISSUE) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002980 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002981 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002982 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002983 } else {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002984 req_set_fail(req);
Pavel Begunkov06bdea22021-11-23 00:07:46 +00002985 req->result = ret;
2986 req->io_task_work.func = io_req_task_complete;
2987 io_req_task_work_add(req);
Pavel Begunkov97284632021-04-08 19:28:03 +01002988 }
2989 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002990}
2991
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002992static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2993 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002994{
Jens Axboe9adbd452019-12-20 08:45:55 -07002995 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002996 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002997 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002998
Pavel Begunkov75769e32021-04-01 15:43:54 +01002999 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07003000 return -EFAULT;
3001 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01003002 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07003003 return -EFAULT;
3004
3005 /*
3006 * May not be a start of buffer, set size appropriately
3007 * and advance us to the beginning.
3008 */
3009 offset = buf_addr - imu->ubuf;
3010 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003011
3012 if (offset) {
3013 /*
3014 * Don't use iov_iter_advance() here, as it's really slow for
3015 * using the latter parts of a big fixed buffer - it iterates
3016 * over each segment manually. We can cheat a bit here, because
3017 * we know that:
3018 *
3019 * 1) it's a BVEC iter, we set it up
3020 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3021 * first and last bvec
3022 *
3023 * So just find our index, and adjust the iterator afterwards.
3024 * If the offset is within the first bvec (or the whole first
3025 * bvec, just use iov_iter_advance(). This makes it easier
3026 * since we can just skip the first segment, which may not
3027 * be PAGE_SIZE aligned.
3028 */
3029 const struct bio_vec *bvec = imu->bvec;
3030
3031 if (offset <= bvec->bv_len) {
3032 iov_iter_advance(iter, offset);
3033 } else {
3034 unsigned long seg_skip;
3035
3036 /* skip first vec */
3037 offset -= bvec->bv_len;
3038 seg_skip = 1 + (offset >> PAGE_SHIFT);
3039
3040 iter->bvec = bvec + seg_skip;
3041 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003042 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003043 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003044 }
3045 }
3046
Pavel Begunkov847595d2021-02-04 13:52:06 +00003047 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003048}
3049
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003050static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3051{
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003052 struct io_mapped_ubuf *imu = req->imu;
3053 u16 index, buf_index = req->buf_index;
3054
3055 if (likely(!imu)) {
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003056 struct io_ring_ctx *ctx = req->ctx;
3057
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003058 if (unlikely(buf_index >= ctx->nr_user_bufs))
3059 return -EFAULT;
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003060 io_req_set_rsrc_node(req, ctx);
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003061 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3062 imu = READ_ONCE(ctx->user_bufs[index]);
3063 req->imu = imu;
3064 }
3065 return __io_import_fixed(req, rw, iter, imu);
3066}
3067
Jens Axboebcda7ba2020-02-23 16:42:51 -07003068static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3069{
3070 if (needs_lock)
3071 mutex_unlock(&ctx->uring_lock);
3072}
3073
3074static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3075{
3076 /*
3077 * "Normal" inline submissions always hold the uring_lock, since we
3078 * grab it from the system call. Same is true for the SQPOLL offload.
3079 * The only exception is when we've detached the request and issue it
3080 * from an async worker thread, grab the lock for that case.
3081 */
3082 if (needs_lock)
3083 mutex_lock(&ctx->uring_lock);
3084}
3085
3086static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003087 int bgid, unsigned int issue_flags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07003088{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003089 struct io_buffer *kbuf = req->kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003090 struct io_buffer *head;
Hao Xu3b44b372021-10-18 21:34:31 +08003091 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003092
3093 if (req->flags & REQ_F_BUFFER_SELECTED)
3094 return kbuf;
3095
3096 io_ring_submit_lock(req->ctx, needs_lock);
3097
3098 lockdep_assert_held(&req->ctx->uring_lock);
3099
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003100 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003101 if (head) {
3102 if (!list_empty(&head->list)) {
3103 kbuf = list_last_entry(&head->list, struct io_buffer,
3104 list);
3105 list_del(&kbuf->list);
3106 } else {
3107 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003108 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003109 }
3110 if (*len > kbuf->len)
3111 *len = kbuf->len;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003112 req->flags |= REQ_F_BUFFER_SELECTED;
3113 req->kbuf = kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003114 } else {
3115 kbuf = ERR_PTR(-ENOBUFS);
3116 }
3117
3118 io_ring_submit_unlock(req->ctx, needs_lock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003119 return kbuf;
3120}
3121
Jens Axboe4d954c22020-02-27 07:31:19 -07003122static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003123 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003124{
3125 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003126 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003127
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003128 bgid = req->buf_index;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003129 kbuf = io_buffer_select(req, len, bgid, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003130 if (IS_ERR(kbuf))
3131 return kbuf;
Jens Axboe4d954c22020-02-27 07:31:19 -07003132 return u64_to_user_ptr(kbuf->addr);
3133}
3134
3135#ifdef CONFIG_COMPAT
3136static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003137 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003138{
3139 struct compat_iovec __user *uiov;
3140 compat_ssize_t clen;
3141 void __user *buf;
3142 ssize_t len;
3143
3144 uiov = u64_to_user_ptr(req->rw.addr);
3145 if (!access_ok(uiov, sizeof(*uiov)))
3146 return -EFAULT;
3147 if (__get_user(clen, &uiov->iov_len))
3148 return -EFAULT;
3149 if (clen < 0)
3150 return -EINVAL;
3151
3152 len = clen;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003153 buf = io_rw_buffer_select(req, &len, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003154 if (IS_ERR(buf))
3155 return PTR_ERR(buf);
3156 iov[0].iov_base = buf;
3157 iov[0].iov_len = (compat_size_t) len;
3158 return 0;
3159}
3160#endif
3161
3162static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003163 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003164{
3165 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3166 void __user *buf;
3167 ssize_t len;
3168
3169 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3170 return -EFAULT;
3171
3172 len = iov[0].iov_len;
3173 if (len < 0)
3174 return -EINVAL;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003175 buf = io_rw_buffer_select(req, &len, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003176 if (IS_ERR(buf))
3177 return PTR_ERR(buf);
3178 iov[0].iov_base = buf;
3179 iov[0].iov_len = len;
3180 return 0;
3181}
3182
3183static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003184 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003185{
Jens Axboedddb3e22020-06-04 11:27:01 -06003186 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003187 struct io_buffer *kbuf = req->kbuf;
Jens Axboedddb3e22020-06-04 11:27:01 -06003188
Jens Axboedddb3e22020-06-04 11:27:01 -06003189 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3190 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003191 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003192 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003193 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003194 return -EINVAL;
3195
3196#ifdef CONFIG_COMPAT
3197 if (req->ctx->compat)
Pavel Begunkov51aac422021-10-14 16:10:17 +01003198 return io_compat_import(req, iov, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003199#endif
3200
Pavel Begunkov51aac422021-10-14 16:10:17 +01003201 return __io_iov_buffer_select(req, iov, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003202}
3203
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003204static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req,
3205 struct io_rw_state *s,
3206 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003207{
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003208 struct iov_iter *iter = &s->iter;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003209 u8 opcode = req->opcode;
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003210 struct iovec *iovec;
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003211 void __user *buf;
3212 size_t sqe_len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003213 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003214
Pavel Begunkovf3251182021-11-23 00:07:48 +00003215 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
3216 ret = io_import_fixed(req, rw, iter);
3217 if (ret)
3218 return ERR_PTR(ret);
3219 return NULL;
3220 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003221
Jens Axboebcda7ba2020-02-23 16:42:51 -07003222 /* buffer index only valid with fixed read/write, or buffer select */
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003223 if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)))
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003224 return ERR_PTR(-EINVAL);
Jens Axboe9adbd452019-12-20 08:45:55 -07003225
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003226 buf = u64_to_user_ptr(req->rw.addr);
3227 sqe_len = req->rw.len;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003228
Jens Axboe3a6820f2019-12-22 15:19:35 -07003229 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003230 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01003231 buf = io_rw_buffer_select(req, &sqe_len, issue_flags);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003232 if (IS_ERR(buf))
Changcheng Deng898df242021-10-20 08:49:48 +00003233 return ERR_CAST(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003234 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003235 }
3236
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003237 ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter);
Pavel Begunkovf3251182021-11-23 00:07:48 +00003238 if (ret)
3239 return ERR_PTR(ret);
3240 return NULL;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003241 }
3242
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003243 iovec = s->fast_iov;
Jens Axboe4d954c22020-02-27 07:31:19 -07003244 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003245 ret = io_iov_buffer_select(req, iovec, issue_flags);
Pavel Begunkovf3251182021-11-23 00:07:48 +00003246 if (ret)
3247 return ERR_PTR(ret);
3248 iov_iter_init(iter, rw, iovec, 1, iovec->iov_len);
3249 return NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07003250 }
3251
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003252 ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003253 req->ctx->compat);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003254 if (unlikely(ret < 0))
3255 return ERR_PTR(ret);
3256 return iovec;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003257}
3258
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003259static inline int io_import_iovec(int rw, struct io_kiocb *req,
3260 struct iovec **iovec, struct io_rw_state *s,
3261 unsigned int issue_flags)
3262{
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003263 *iovec = __io_import_iovec(rw, req, s, issue_flags);
3264 if (unlikely(IS_ERR(*iovec)))
3265 return PTR_ERR(*iovec);
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003266
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003267 iov_iter_save_state(&s->iter, &s->iter_state);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003268 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003269}
3270
Jens Axboe0fef9482020-08-26 10:36:20 -06003271static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3272{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003273 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003274}
3275
Jens Axboe32960612019-09-23 11:05:34 -06003276/*
3277 * For files that don't have ->read_iter() and ->write_iter(), handle them
3278 * by looping over ->read() or ->write() manually.
3279 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003280static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003281{
Jens Axboe4017eb92020-10-22 14:14:12 -06003282 struct kiocb *kiocb = &req->rw.kiocb;
3283 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003284 ssize_t ret = 0;
3285
3286 /*
3287 * Don't support polled IO through this interface, and we can't
3288 * support non-blocking either. For the latter, this just causes
3289 * the kiocb to be handled from an async context.
3290 */
3291 if (kiocb->ki_flags & IOCB_HIPRI)
3292 return -EOPNOTSUPP;
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003293 if ((kiocb->ki_flags & IOCB_NOWAIT) &&
3294 !(kiocb->ki_filp->f_flags & O_NONBLOCK))
Jens Axboe32960612019-09-23 11:05:34 -06003295 return -EAGAIN;
3296
3297 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003298 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003299 ssize_t nr;
3300
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003301 if (!iov_iter_is_bvec(iter)) {
3302 iovec = iov_iter_iovec(iter);
3303 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003304 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3305 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003306 }
3307
Jens Axboe32960612019-09-23 11:05:34 -06003308 if (rw == READ) {
3309 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003310 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003311 } else {
3312 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003313 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003314 }
3315
3316 if (nr < 0) {
3317 if (!ret)
3318 ret = nr;
3319 break;
3320 }
Jens Axboe16c8d2d2021-09-12 06:45:07 -06003321 if (!iov_iter_is_bvec(iter)) {
3322 iov_iter_advance(iter, nr);
3323 } else {
3324 req->rw.len -= nr;
3325 req->rw.addr += nr;
3326 }
Jens Axboe32960612019-09-23 11:05:34 -06003327 ret += nr;
3328 if (nr != iovec.iov_len)
3329 break;
Jens Axboe32960612019-09-23 11:05:34 -06003330 }
3331
3332 return ret;
3333}
3334
Jens Axboeff6165b2020-08-13 09:47:43 -06003335static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3336 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003337{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003338 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003339
Pavel Begunkov538941e2021-10-14 16:10:15 +01003340 memcpy(&rw->s.iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003341 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003342 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003343 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003344 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003345 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003346 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003347 unsigned iov_off = 0;
3348
Pavel Begunkov538941e2021-10-14 16:10:15 +01003349 rw->s.iter.iov = rw->s.fast_iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003350 if (iter->iov != fast_iov) {
3351 iov_off = iter->iov - fast_iov;
Pavel Begunkov538941e2021-10-14 16:10:15 +01003352 rw->s.iter.iov += iov_off;
Jens Axboeff6165b2020-08-13 09:47:43 -06003353 }
Pavel Begunkov538941e2021-10-14 16:10:15 +01003354 if (rw->s.fast_iov != fast_iov)
3355 memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003356 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003357 } else {
3358 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003359 }
3360}
3361
Hao Xu8d4af682021-09-22 18:15:22 +08003362static inline bool io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003363{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003364 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3365 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
Pavel Begunkovd886e182021-10-04 20:02:56 +01003366 if (req->async_data) {
3367 req->flags |= REQ_F_ASYNC_DATA;
3368 return false;
3369 }
3370 return true;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003371}
3372
Jens Axboeff6165b2020-08-13 09:47:43 -06003373static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003374 struct io_rw_state *s, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003375{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003376 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003377 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003378 if (!req_has_async_data(req)) {
Jens Axboecd658692021-09-10 11:19:14 -06003379 struct io_async_rw *iorw;
3380
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003381 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003382 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003383 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003384 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003385
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003386 io_req_map_rw(req, iovec, s->fast_iov, &s->iter);
Jens Axboecd658692021-09-10 11:19:14 -06003387 iorw = req->async_data;
3388 /* we've copied and mapped the iter, ensure state is saved */
Pavel Begunkov538941e2021-10-14 16:10:15 +01003389 iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003390 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003391 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003392}
3393
Pavel Begunkov73debe62020-09-30 22:57:54 +03003394static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003395{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003396 struct io_async_rw *iorw = req->async_data;
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003397 struct iovec *iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003398 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003399
Pavel Begunkov51aac422021-10-14 16:10:17 +01003400 /* submission path, ->uring_lock should already be taken */
Hao Xu3b44b372021-10-18 21:34:31 +08003401 ret = io_import_iovec(rw, req, &iov, &iorw->s, 0);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003402 if (unlikely(ret < 0))
3403 return ret;
3404
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003405 iorw->bytes_done = 0;
3406 iorw->free_iovec = iov;
3407 if (iov)
3408 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003409 return 0;
3410}
3411
Pavel Begunkov73debe62020-09-30 22:57:54 +03003412static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003413{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003414 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3415 return -EBADF;
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01003416 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003417}
3418
Jens Axboec1dd91d2020-08-03 16:43:59 -06003419/*
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05003420 * This is our waitqueue callback handler, registered through __folio_lock_async()
Jens Axboec1dd91d2020-08-03 16:43:59 -06003421 * when we initially tried to do the IO with the iocb armed our waitqueue.
3422 * This gets called when the page is unlocked, and we generally expect that to
3423 * happen when the page IO is completed and the page is now uptodate. This will
3424 * queue a task_work based retry of the operation, attempting to copy the data
3425 * again. If the latter fails because the page was NOT uptodate, then we will
3426 * do a thread based blocking retry of the operation. That's the unexpected
3427 * slow path.
3428 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003429static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3430 int sync, void *arg)
3431{
3432 struct wait_page_queue *wpq;
3433 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003434 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003435
3436 wpq = container_of(wait, struct wait_page_queue, wait);
3437
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003438 if (!wake_page_match(wpq, key))
3439 return 0;
3440
Hao Xuc8d317a2020-09-29 20:00:45 +08003441 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003442 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003443 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003444 return 1;
3445}
3446
Jens Axboec1dd91d2020-08-03 16:43:59 -06003447/*
3448 * This controls whether a given IO request should be armed for async page
3449 * based retry. If we return false here, the request is handed to the async
3450 * worker threads for retry. If we're doing buffered reads on a regular file,
3451 * we prepare a private wait_page_queue entry and retry the operation. This
3452 * will either succeed because the page is now uptodate and unlocked, or it
3453 * will register a callback when the page is unlocked at IO completion. Through
3454 * that callback, io_uring uses task_work to setup a retry of the operation.
3455 * That retry will attempt the buffered read again. The retry will generally
3456 * succeed, or in rare cases where it fails, we then fall back to using the
3457 * async worker threads for a blocking retry.
3458 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003459static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003460{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003461 struct io_async_rw *rw = req->async_data;
3462 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003463 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003464
3465 /* never retry for NOWAIT, we just complete with -EAGAIN */
3466 if (req->flags & REQ_F_NOWAIT)
3467 return false;
3468
Jens Axboe227c0c92020-08-13 11:51:40 -06003469 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003470 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003471 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003472
Jens Axboebcf5a062020-05-22 09:24:42 -06003473 /*
3474 * just use poll if we can, and don't attempt if the fs doesn't
3475 * support callback based unlocks
3476 */
3477 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3478 return false;
3479
Jens Axboe3b2a4432020-08-16 10:58:43 -07003480 wait->wait.func = io_async_buf_func;
3481 wait->wait.private = req;
3482 wait->wait.flags = 0;
3483 INIT_LIST_HEAD(&wait->wait.entry);
3484 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003485 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003486 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003487 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003488}
3489
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003490static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003491{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003492 if (likely(req->file->f_op->read_iter))
Jens Axboebcf5a062020-05-22 09:24:42 -06003493 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003494 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003495 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003496 else
3497 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003498}
3499
Ming Lei7db30432021-08-21 23:07:51 +08003500static bool need_read_all(struct io_kiocb *req)
3501{
3502 return req->flags & REQ_F_ISREG ||
3503 S_ISBLK(file_inode(req->file)->i_mode);
3504}
3505
Pavel Begunkov889fca72021-02-10 00:03:09 +00003506static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003507{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003508 struct io_rw_state __s, *s = &__s;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003509 struct iovec *iovec;
Jens Axboe9adbd452019-12-20 08:45:55 -07003510 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003511 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003512 struct io_async_rw *rw;
Jens Axboecd658692021-09-10 11:19:14 -06003513 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003514
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003515 if (!req_has_async_data(req)) {
3516 ret = io_import_iovec(READ, req, &iovec, s, issue_flags);
3517 if (unlikely(ret < 0))
3518 return ret;
3519 } else {
Pavel Begunkovd886e182021-10-04 20:02:56 +01003520 rw = req->async_data;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003521 s = &rw->s;
Jens Axboecd658692021-09-10 11:19:14 -06003522 /*
3523 * We come here from an earlier attempt, restore our state to
3524 * match in case it doesn't. It's cheap enough that we don't
3525 * need to make this conditional.
3526 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003527 iov_iter_restore(&s->iter, &s->iter_state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003528 iovec = NULL;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003529 }
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003530 req->result = iov_iter_count(&s->iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003531
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003532 if (force_nonblock) {
3533 /* If the file doesn't support async, just async punt */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003534 if (unlikely(!io_file_supports_nowait(req))) {
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003535 ret = io_setup_async_rw(req, iovec, s, true);
3536 return ret ?: -EAGAIN;
3537 }
Pavel Begunkova88fc402020-09-30 22:57:53 +03003538 kiocb->ki_flags |= IOCB_NOWAIT;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003539 } else {
3540 /* Ensure we clear previously set non-block flag */
3541 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003542 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003543
Jens Axboecd658692021-09-10 11:19:14 -06003544 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003545 if (unlikely(ret)) {
3546 kfree(iovec);
3547 return ret;
3548 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003549
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003550 ret = io_iter_do_read(req, &s->iter);
Jens Axboe32960612019-09-23 11:05:34 -06003551
Jens Axboe230d50d2021-04-01 20:41:15 -06003552 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003553 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003554 /* IOPOLL retry should happen for io-wq threads */
3555 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003556 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003557 /* no retry on NONBLOCK nor RWF_NOWAIT */
3558 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003559 goto done;
Jens Axboef38c7e32020-09-25 15:23:43 -06003560 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003561 } else if (ret == -EIOCBQUEUED) {
3562 goto out_free;
Pavel Begunkovf80a50a2021-10-14 16:10:13 +01003563 } else if (ret == req->result || ret <= 0 || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003564 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003565 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003566 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003567 }
3568
Jens Axboecd658692021-09-10 11:19:14 -06003569 /*
3570 * Don't depend on the iter state matching what was consumed, or being
3571 * untouched in case of error. Restore it and we'll advance it
3572 * manually if we need to.
3573 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003574 iov_iter_restore(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003575
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003576 ret2 = io_setup_async_rw(req, iovec, s, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003577 if (ret2)
3578 return ret2;
3579
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003580 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003581 rw = req->async_data;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003582 s = &rw->s;
Jens Axboecd658692021-09-10 11:19:14 -06003583 /*
3584 * Now use our persistent iterator and state, if we aren't already.
3585 * We've restored and mapped the iter to match.
3586 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003587
Pavel Begunkovb23df912021-02-04 13:52:04 +00003588 do {
Jens Axboecd658692021-09-10 11:19:14 -06003589 /*
3590 * We end up here because of a partial read, either from
3591 * above or inside this loop. Advance the iter by the bytes
3592 * that were consumed.
3593 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003594 iov_iter_advance(&s->iter, ret);
3595 if (!iov_iter_count(&s->iter))
Jens Axboecd658692021-09-10 11:19:14 -06003596 break;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003597 rw->bytes_done += ret;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003598 iov_iter_save_state(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003599
Pavel Begunkovb23df912021-02-04 13:52:04 +00003600 /* if we can retry, do so with the callbacks armed */
3601 if (!io_rw_should_retry(req)) {
3602 kiocb->ki_flags &= ~IOCB_WAITQ;
3603 return -EAGAIN;
3604 }
3605
3606 /*
3607 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3608 * we get -EIOCBQUEUED, then we'll get a notification when the
3609 * desired page gets unlocked. We can also get a partial read
3610 * here, and if we do, then just retry at the new offset.
3611 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003612 ret = io_iter_do_read(req, &s->iter);
Pavel Begunkovb23df912021-02-04 13:52:04 +00003613 if (ret == -EIOCBQUEUED)
3614 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003615 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003616 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003617 iov_iter_restore(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003618 } while (ret > 0);
Jens Axboe227c0c92020-08-13 11:51:40 -06003619done:
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003620 kiocb_done(req, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003621out_free:
3622 /* it's faster to check here then delegate to kfree */
3623 if (iovec)
3624 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003625 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003626}
3627
Pavel Begunkov73debe62020-09-30 22:57:54 +03003628static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003629{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003630 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3631 return -EBADF;
Jens Axboe3884b832021-10-25 13:45:12 -06003632 req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file));
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01003633 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003634}
3635
Pavel Begunkov889fca72021-02-10 00:03:09 +00003636static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003637{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003638 struct io_rw_state __s, *s = &__s;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003639 struct iovec *iovec;
Jens Axboe9adbd452019-12-20 08:45:55 -07003640 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003641 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003642 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003643
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003644 if (!req_has_async_data(req)) {
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003645 ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags);
3646 if (unlikely(ret < 0))
Pavel Begunkov2846c482020-11-07 13:16:27 +00003647 return ret;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003648 } else {
3649 struct io_async_rw *rw = req->async_data;
3650
3651 s = &rw->s;
3652 iov_iter_restore(&s->iter, &s->iter_state);
3653 iovec = NULL;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003654 }
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003655 req->result = iov_iter_count(&s->iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003656
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003657 if (force_nonblock) {
3658 /* If the file doesn't support async, just async punt */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003659 if (unlikely(!io_file_supports_nowait(req)))
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003660 goto copy_iov;
3661
3662 /* file path doesn't support NOWAIT for non-direct_IO */
3663 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3664 (req->flags & REQ_F_ISREG))
3665 goto copy_iov;
3666
Pavel Begunkova88fc402020-09-30 22:57:53 +03003667 kiocb->ki_flags |= IOCB_NOWAIT;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003668 } else {
3669 /* Ensure we clear previously set non-block flag */
3670 kiocb->ki_flags &= ~IOCB_NOWAIT;
3671 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003672
Jens Axboecd658692021-09-10 11:19:14 -06003673 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003674 if (unlikely(ret))
3675 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003676
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003677 /*
3678 * Open-code file_start_write here to grab freeze protection,
3679 * which will be released by another thread in
3680 * io_complete_rw(). Fool lockdep by telling it the lock got
3681 * released so that it doesn't complain about the held lock when
3682 * we return to userspace.
3683 */
3684 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003685 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003686 __sb_writers_release(file_inode(req->file)->i_sb,
3687 SB_FREEZE_WRITE);
3688 }
3689 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003690
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003691 if (likely(req->file->f_op->write_iter))
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003692 ret2 = call_write_iter(req->file, kiocb, &s->iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003693 else if (req->file->f_op->write)
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003694 ret2 = loop_rw_iter(WRITE, req, &s->iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003695 else
3696 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003697
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003698 if (req->flags & REQ_F_REISSUE) {
3699 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003700 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003701 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003702
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003703 /*
3704 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3705 * retry them without IOCB_NOWAIT.
3706 */
3707 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3708 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003709 /* no retry on NONBLOCK nor RWF_NOWAIT */
3710 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003711 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003712 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003713 /* IOPOLL retry should happen for io-wq threads */
Noah Goldsteinb10841c2021-10-16 20:32:29 -05003714 if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboeeefdf302020-08-27 16:40:19 -06003715 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003716done:
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003717 kiocb_done(req, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003718 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003719copy_iov:
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003720 iov_iter_restore(&s->iter, &s->iter_state);
3721 ret = io_setup_async_rw(req, iovec, s, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003722 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003723 }
Jens Axboe31b51512019-01-18 22:56:34 -07003724out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003725 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003726 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003727 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003728 return ret;
3729}
3730
Jens Axboe80a261f2020-09-28 14:23:58 -06003731static int io_renameat_prep(struct io_kiocb *req,
3732 const struct io_uring_sqe *sqe)
3733{
3734 struct io_rename *ren = &req->rename;
3735 const char __user *oldf, *newf;
3736
Jens Axboeed7eb252021-06-23 09:04:13 -06003737 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3738 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003739 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003740 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003741 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3742 return -EBADF;
3743
3744 ren->old_dfd = READ_ONCE(sqe->fd);
3745 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3746 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3747 ren->new_dfd = READ_ONCE(sqe->len);
3748 ren->flags = READ_ONCE(sqe->rename_flags);
3749
3750 ren->oldpath = getname(oldf);
3751 if (IS_ERR(ren->oldpath))
3752 return PTR_ERR(ren->oldpath);
3753
3754 ren->newpath = getname(newf);
3755 if (IS_ERR(ren->newpath)) {
3756 putname(ren->oldpath);
3757 return PTR_ERR(ren->newpath);
3758 }
3759
3760 req->flags |= REQ_F_NEED_CLEANUP;
3761 return 0;
3762}
3763
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003764static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003765{
3766 struct io_rename *ren = &req->rename;
3767 int ret;
3768
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003769 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003770 return -EAGAIN;
3771
3772 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3773 ren->newpath, ren->flags);
3774
3775 req->flags &= ~REQ_F_NEED_CLEANUP;
3776 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003777 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003778 io_req_complete(req, ret);
3779 return 0;
3780}
3781
Jens Axboe14a11432020-09-28 14:27:37 -06003782static int io_unlinkat_prep(struct io_kiocb *req,
3783 const struct io_uring_sqe *sqe)
3784{
3785 struct io_unlink *un = &req->unlink;
3786 const char __user *fname;
3787
Jens Axboe22634bc2021-06-23 09:07:45 -06003788 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3789 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003790 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3791 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003792 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003793 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3794 return -EBADF;
3795
3796 un->dfd = READ_ONCE(sqe->fd);
3797
3798 un->flags = READ_ONCE(sqe->unlink_flags);
3799 if (un->flags & ~AT_REMOVEDIR)
3800 return -EINVAL;
3801
3802 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3803 un->filename = getname(fname);
3804 if (IS_ERR(un->filename))
3805 return PTR_ERR(un->filename);
3806
3807 req->flags |= REQ_F_NEED_CLEANUP;
3808 return 0;
3809}
3810
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003811static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003812{
3813 struct io_unlink *un = &req->unlink;
3814 int ret;
3815
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003816 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003817 return -EAGAIN;
3818
3819 if (un->flags & AT_REMOVEDIR)
3820 ret = do_rmdir(un->dfd, un->filename);
3821 else
3822 ret = do_unlinkat(un->dfd, un->filename);
3823
3824 req->flags &= ~REQ_F_NEED_CLEANUP;
3825 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003826 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003827 io_req_complete(req, ret);
3828 return 0;
3829}
3830
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003831static int io_mkdirat_prep(struct io_kiocb *req,
3832 const struct io_uring_sqe *sqe)
3833{
3834 struct io_mkdir *mkd = &req->mkdir;
3835 const char __user *fname;
3836
3837 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3838 return -EINVAL;
3839 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3840 sqe->splice_fd_in)
3841 return -EINVAL;
3842 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3843 return -EBADF;
3844
3845 mkd->dfd = READ_ONCE(sqe->fd);
3846 mkd->mode = READ_ONCE(sqe->len);
3847
3848 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3849 mkd->filename = getname(fname);
3850 if (IS_ERR(mkd->filename))
3851 return PTR_ERR(mkd->filename);
3852
3853 req->flags |= REQ_F_NEED_CLEANUP;
3854 return 0;
3855}
3856
Pavel Begunkov04f34082021-10-14 16:10:12 +01003857static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003858{
3859 struct io_mkdir *mkd = &req->mkdir;
3860 int ret;
3861
3862 if (issue_flags & IO_URING_F_NONBLOCK)
3863 return -EAGAIN;
3864
3865 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3866
3867 req->flags &= ~REQ_F_NEED_CLEANUP;
3868 if (ret < 0)
3869 req_set_fail(req);
3870 io_req_complete(req, ret);
3871 return 0;
3872}
3873
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003874static int io_symlinkat_prep(struct io_kiocb *req,
3875 const struct io_uring_sqe *sqe)
3876{
3877 struct io_symlink *sl = &req->symlink;
3878 const char __user *oldpath, *newpath;
3879
3880 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3881 return -EINVAL;
3882 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3883 sqe->splice_fd_in)
3884 return -EINVAL;
3885 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3886 return -EBADF;
3887
3888 sl->new_dfd = READ_ONCE(sqe->fd);
3889 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3890 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3891
3892 sl->oldpath = getname(oldpath);
3893 if (IS_ERR(sl->oldpath))
3894 return PTR_ERR(sl->oldpath);
3895
3896 sl->newpath = getname(newpath);
3897 if (IS_ERR(sl->newpath)) {
3898 putname(sl->oldpath);
3899 return PTR_ERR(sl->newpath);
3900 }
3901
3902 req->flags |= REQ_F_NEED_CLEANUP;
3903 return 0;
3904}
3905
Pavel Begunkov04f34082021-10-14 16:10:12 +01003906static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003907{
3908 struct io_symlink *sl = &req->symlink;
3909 int ret;
3910
3911 if (issue_flags & IO_URING_F_NONBLOCK)
3912 return -EAGAIN;
3913
3914 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3915
3916 req->flags &= ~REQ_F_NEED_CLEANUP;
3917 if (ret < 0)
3918 req_set_fail(req);
3919 io_req_complete(req, ret);
3920 return 0;
3921}
3922
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003923static int io_linkat_prep(struct io_kiocb *req,
3924 const struct io_uring_sqe *sqe)
3925{
3926 struct io_hardlink *lnk = &req->hardlink;
3927 const char __user *oldf, *newf;
3928
3929 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3930 return -EINVAL;
3931 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3932 return -EINVAL;
3933 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3934 return -EBADF;
3935
3936 lnk->old_dfd = READ_ONCE(sqe->fd);
3937 lnk->new_dfd = READ_ONCE(sqe->len);
3938 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3939 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3940 lnk->flags = READ_ONCE(sqe->hardlink_flags);
3941
3942 lnk->oldpath = getname(oldf);
3943 if (IS_ERR(lnk->oldpath))
3944 return PTR_ERR(lnk->oldpath);
3945
3946 lnk->newpath = getname(newf);
3947 if (IS_ERR(lnk->newpath)) {
3948 putname(lnk->oldpath);
3949 return PTR_ERR(lnk->newpath);
3950 }
3951
3952 req->flags |= REQ_F_NEED_CLEANUP;
3953 return 0;
3954}
3955
Pavel Begunkov04f34082021-10-14 16:10:12 +01003956static int io_linkat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003957{
3958 struct io_hardlink *lnk = &req->hardlink;
3959 int ret;
3960
3961 if (issue_flags & IO_URING_F_NONBLOCK)
3962 return -EAGAIN;
3963
3964 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
3965 lnk->newpath, lnk->flags);
3966
3967 req->flags &= ~REQ_F_NEED_CLEANUP;
3968 if (ret < 0)
3969 req_set_fail(req);
3970 io_req_complete(req, ret);
3971 return 0;
3972}
3973
Jens Axboe36f4fa62020-09-05 11:14:22 -06003974static int io_shutdown_prep(struct io_kiocb *req,
3975 const struct io_uring_sqe *sqe)
3976{
3977#if defined(CONFIG_NET)
3978 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3979 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003980 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3981 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003982 return -EINVAL;
3983
3984 req->shutdown.how = READ_ONCE(sqe->len);
3985 return 0;
3986#else
3987 return -EOPNOTSUPP;
3988#endif
3989}
3990
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003991static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003992{
3993#if defined(CONFIG_NET)
3994 struct socket *sock;
3995 int ret;
3996
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003997 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003998 return -EAGAIN;
3999
Linus Torvalds48aba792020-12-16 12:44:05 -08004000 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06004001 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08004002 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06004003
4004 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07004005 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004006 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06004007 io_req_complete(req, ret);
4008 return 0;
4009#else
4010 return -EOPNOTSUPP;
4011#endif
4012}
4013
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004014static int __io_splice_prep(struct io_kiocb *req,
4015 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004016{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004017 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004018 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004019
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004020 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4021 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004022
4023 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004024 sp->len = READ_ONCE(sqe->len);
4025 sp->flags = READ_ONCE(sqe->splice_flags);
4026
4027 if (unlikely(sp->flags & ~valid_flags))
4028 return -EINVAL;
4029
Pavel Begunkov62906e82021-08-10 14:52:47 +01004030 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01004031 (sp->flags & SPLICE_F_FD_IN_FIXED));
4032 if (!sp->file_in)
4033 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004034 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004035 return 0;
4036}
4037
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004038static int io_tee_prep(struct io_kiocb *req,
4039 const struct io_uring_sqe *sqe)
4040{
4041 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
4042 return -EINVAL;
4043 return __io_splice_prep(req, sqe);
4044}
4045
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004046static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004047{
4048 struct io_splice *sp = &req->splice;
4049 struct file *in = sp->file_in;
4050 struct file *out = sp->file_out;
4051 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4052 long ret = 0;
4053
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004054 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004055 return -EAGAIN;
4056 if (sp->len)
4057 ret = do_tee(in, out, sp->len, flags);
4058
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004059 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4060 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004061 req->flags &= ~REQ_F_NEED_CLEANUP;
4062
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004063 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004064 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004065 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004066 return 0;
4067}
4068
4069static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4070{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004071 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004072
4073 sp->off_in = READ_ONCE(sqe->splice_off_in);
4074 sp->off_out = READ_ONCE(sqe->off);
4075 return __io_splice_prep(req, sqe);
4076}
4077
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004078static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004079{
4080 struct io_splice *sp = &req->splice;
4081 struct file *in = sp->file_in;
4082 struct file *out = sp->file_out;
4083 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4084 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004085 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004086
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004087 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004088 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004089
4090 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4091 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004092
Jens Axboe948a7742020-05-17 14:21:38 -06004093 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004094 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004095
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004096 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4097 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004098 req->flags &= ~REQ_F_NEED_CLEANUP;
4099
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004100 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004101 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004102 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004103 return 0;
4104}
4105
Jens Axboe2b188cc2019-01-07 10:46:33 -07004106/*
4107 * IORING_OP_NOP just posts a completion event, nothing else.
4108 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004109static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004110{
4111 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004112
Jens Axboedef596e2019-01-09 08:59:42 -07004113 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4114 return -EINVAL;
4115
Pavel Begunkov889fca72021-02-10 00:03:09 +00004116 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004117 return 0;
4118}
4119
Pavel Begunkov1155c762021-02-18 18:29:38 +00004120static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004121{
Jens Axboe6b063142019-01-10 22:13:58 -07004122 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004123
Jens Axboe09bb8392019-03-13 12:39:28 -06004124 if (!req->file)
4125 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004126
Jens Axboe6b063142019-01-10 22:13:58 -07004127 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004128 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004129 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4130 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004131 return -EINVAL;
4132
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004133 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4134 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4135 return -EINVAL;
4136
4137 req->sync.off = READ_ONCE(sqe->off);
4138 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004139 return 0;
4140}
4141
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004142static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004143{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004144 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004145 int ret;
4146
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004147 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004148 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004149 return -EAGAIN;
4150
Jens Axboe9adbd452019-12-20 08:45:55 -07004151 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004152 end > 0 ? end : LLONG_MAX,
4153 req->sync.flags & IORING_FSYNC_DATASYNC);
4154 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004155 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004156 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004157 return 0;
4158}
4159
Jens Axboed63d1b52019-12-10 10:38:56 -07004160static int io_fallocate_prep(struct io_kiocb *req,
4161 const struct io_uring_sqe *sqe)
4162{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004163 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4164 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004165 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004166 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4167 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004168
4169 req->sync.off = READ_ONCE(sqe->off);
4170 req->sync.len = READ_ONCE(sqe->addr);
4171 req->sync.mode = READ_ONCE(sqe->len);
4172 return 0;
4173}
4174
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004175static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004176{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004177 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004178
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004179 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004180 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004181 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004182 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4183 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004184 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004185 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004186 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004187 return 0;
4188}
4189
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004190static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004191{
Jens Axboef8748882020-01-08 17:47:02 -07004192 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004193 int ret;
4194
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004195 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4196 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004197 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004198 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004199 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004200 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004201
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004202 /* open.how should be already initialised */
4203 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004204 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004205
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004206 req->open.dfd = READ_ONCE(sqe->fd);
4207 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004208 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004209 if (IS_ERR(req->open.filename)) {
4210 ret = PTR_ERR(req->open.filename);
4211 req->open.filename = NULL;
4212 return ret;
4213 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004214
4215 req->open.file_slot = READ_ONCE(sqe->file_index);
4216 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4217 return -EINVAL;
4218
Jens Axboe4022e7a2020-03-19 19:23:18 -06004219 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004220 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004221 return 0;
4222}
4223
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004224static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4225{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004226 u64 mode = READ_ONCE(sqe->len);
4227 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004228
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004229 req->open.how = build_open_how(flags, mode);
4230 return __io_openat_prep(req, sqe);
4231}
4232
Jens Axboecebdb982020-01-08 17:59:24 -07004233static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4234{
4235 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004236 size_t len;
4237 int ret;
4238
Jens Axboecebdb982020-01-08 17:59:24 -07004239 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4240 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004241 if (len < OPEN_HOW_SIZE_VER0)
4242 return -EINVAL;
4243
4244 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4245 len);
4246 if (ret)
4247 return ret;
4248
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004249 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004250}
4251
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004252static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004253{
4254 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004255 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004256 bool resolve_nonblock, nonblock_set;
4257 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004258 int ret;
4259
Jens Axboecebdb982020-01-08 17:59:24 -07004260 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004261 if (ret)
4262 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004263 nonblock_set = op.open_flag & O_NONBLOCK;
4264 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004265 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004266 /*
4267 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4268 * it'll always -EAGAIN
4269 */
4270 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4271 return -EAGAIN;
4272 op.lookup_flags |= LOOKUP_CACHED;
4273 op.open_flag |= O_NONBLOCK;
4274 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004275
Pavel Begunkovb9445592021-08-25 12:25:45 +01004276 if (!fixed) {
4277 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4278 if (ret < 0)
4279 goto err;
4280 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004281
4282 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004283 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004284 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004285 * We could hang on to this 'fd' on retrying, but seems like
4286 * marginal gain for something that is now known to be a slower
4287 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004288 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004289 if (!fixed)
4290 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004291
4292 ret = PTR_ERR(file);
4293 /* only retry if RESOLVE_CACHED wasn't already set by application */
4294 if (ret == -EAGAIN &&
4295 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4296 return -EAGAIN;
4297 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004298 }
4299
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004300 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4301 file->f_flags &= ~O_NONBLOCK;
4302 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004303
4304 if (!fixed)
4305 fd_install(ret, file);
4306 else
4307 ret = io_install_fixed_file(req, file, issue_flags,
4308 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004309err:
4310 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004311 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004312 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004313 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004314 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004315 return 0;
4316}
4317
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004318static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004319{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004320 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004321}
4322
Jens Axboe067524e2020-03-02 16:32:28 -07004323static int io_remove_buffers_prep(struct io_kiocb *req,
4324 const struct io_uring_sqe *sqe)
4325{
4326 struct io_provide_buf *p = &req->pbuf;
4327 u64 tmp;
4328
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004329 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4330 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004331 return -EINVAL;
4332
4333 tmp = READ_ONCE(sqe->fd);
4334 if (!tmp || tmp > USHRT_MAX)
4335 return -EINVAL;
4336
4337 memset(p, 0, sizeof(*p));
4338 p->nbufs = tmp;
4339 p->bgid = READ_ONCE(sqe->buf_group);
4340 return 0;
4341}
4342
4343static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4344 int bgid, unsigned nbufs)
4345{
4346 unsigned i = 0;
4347
4348 /* shouldn't happen */
4349 if (!nbufs)
4350 return 0;
4351
4352 /* the head kbuf is the list itself */
4353 while (!list_empty(&buf->list)) {
4354 struct io_buffer *nxt;
4355
4356 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4357 list_del(&nxt->list);
4358 kfree(nxt);
4359 if (++i == nbufs)
4360 return i;
4361 }
4362 i++;
4363 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004364 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004365
4366 return i;
4367}
4368
Pavel Begunkov889fca72021-02-10 00:03:09 +00004369static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004370{
4371 struct io_provide_buf *p = &req->pbuf;
4372 struct io_ring_ctx *ctx = req->ctx;
4373 struct io_buffer *head;
4374 int ret = 0;
Hao Xu3b44b372021-10-18 21:34:31 +08004375 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboe067524e2020-03-02 16:32:28 -07004376
Hao Xu3b44b372021-10-18 21:34:31 +08004377 io_ring_submit_lock(ctx, needs_lock);
Jens Axboe067524e2020-03-02 16:32:28 -07004378
4379 lockdep_assert_held(&ctx->uring_lock);
4380
4381 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004382 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004383 if (head)
4384 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004385 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004386 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004387
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004388 /* complete before unlock, IOPOLL may need the lock */
4389 __io_req_complete(req, issue_flags, ret, 0);
Hao Xu3b44b372021-10-18 21:34:31 +08004390 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboe067524e2020-03-02 16:32:28 -07004391 return 0;
4392}
4393
Jens Axboeddf0322d2020-02-23 16:41:33 -07004394static int io_provide_buffers_prep(struct io_kiocb *req,
4395 const struct io_uring_sqe *sqe)
4396{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004397 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004398 struct io_provide_buf *p = &req->pbuf;
4399 u64 tmp;
4400
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004401 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004402 return -EINVAL;
4403
4404 tmp = READ_ONCE(sqe->fd);
4405 if (!tmp || tmp > USHRT_MAX)
4406 return -E2BIG;
4407 p->nbufs = tmp;
4408 p->addr = READ_ONCE(sqe->addr);
4409 p->len = READ_ONCE(sqe->len);
4410
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004411 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4412 &size))
4413 return -EOVERFLOW;
4414 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4415 return -EOVERFLOW;
4416
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004417 size = (unsigned long)p->len * p->nbufs;
4418 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004419 return -EFAULT;
4420
4421 p->bgid = READ_ONCE(sqe->buf_group);
4422 tmp = READ_ONCE(sqe->off);
4423 if (tmp > USHRT_MAX)
4424 return -E2BIG;
4425 p->bid = tmp;
4426 return 0;
4427}
4428
4429static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4430{
4431 struct io_buffer *buf;
4432 u64 addr = pbuf->addr;
4433 int i, bid = pbuf->bid;
4434
4435 for (i = 0; i < pbuf->nbufs; i++) {
Jens Axboe9990da92021-09-24 07:39:08 -06004436 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004437 if (!buf)
4438 break;
4439
4440 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004441 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004442 buf->bid = bid;
4443 addr += pbuf->len;
4444 bid++;
4445 if (!*head) {
4446 INIT_LIST_HEAD(&buf->list);
4447 *head = buf;
4448 } else {
4449 list_add_tail(&buf->list, &(*head)->list);
4450 }
4451 }
4452
4453 return i ? i : -ENOMEM;
4454}
4455
Pavel Begunkov889fca72021-02-10 00:03:09 +00004456static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004457{
4458 struct io_provide_buf *p = &req->pbuf;
4459 struct io_ring_ctx *ctx = req->ctx;
4460 struct io_buffer *head, *list;
4461 int ret = 0;
Hao Xu3b44b372021-10-18 21:34:31 +08004462 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004463
Hao Xu3b44b372021-10-18 21:34:31 +08004464 io_ring_submit_lock(ctx, needs_lock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004465
4466 lockdep_assert_held(&ctx->uring_lock);
4467
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004468 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004469
4470 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004471 if (ret >= 0 && !list) {
4472 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4473 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004474 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004475 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004476 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004477 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004478 /* complete before unlock, IOPOLL may need the lock */
4479 __io_req_complete(req, issue_flags, ret, 0);
Hao Xu3b44b372021-10-18 21:34:31 +08004480 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004481 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004482}
4483
Jens Axboe3e4827b2020-01-08 15:18:09 -07004484static int io_epoll_ctl_prep(struct io_kiocb *req,
4485 const struct io_uring_sqe *sqe)
4486{
4487#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004488 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004489 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004490 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004491 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004492
4493 req->epoll.epfd = READ_ONCE(sqe->fd);
4494 req->epoll.op = READ_ONCE(sqe->len);
4495 req->epoll.fd = READ_ONCE(sqe->off);
4496
4497 if (ep_op_has_event(req->epoll.op)) {
4498 struct epoll_event __user *ev;
4499
4500 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4501 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4502 return -EFAULT;
4503 }
4504
4505 return 0;
4506#else
4507 return -EOPNOTSUPP;
4508#endif
4509}
4510
Pavel Begunkov889fca72021-02-10 00:03:09 +00004511static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004512{
4513#if defined(CONFIG_EPOLL)
4514 struct io_epoll *ie = &req->epoll;
4515 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004516 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004517
4518 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4519 if (force_nonblock && ret == -EAGAIN)
4520 return -EAGAIN;
4521
4522 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004523 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004524 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004525 return 0;
4526#else
4527 return -EOPNOTSUPP;
4528#endif
4529}
4530
Jens Axboec1ca7572019-12-25 22:18:28 -07004531static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4532{
4533#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004534 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004535 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004536 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4537 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004538
4539 req->madvise.addr = READ_ONCE(sqe->addr);
4540 req->madvise.len = READ_ONCE(sqe->len);
4541 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4542 return 0;
4543#else
4544 return -EOPNOTSUPP;
4545#endif
4546}
4547
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004548static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004549{
4550#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4551 struct io_madvise *ma = &req->madvise;
4552 int ret;
4553
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004554 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004555 return -EAGAIN;
4556
Minchan Kim0726b012020-10-17 16:14:50 -07004557 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004558 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004559 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004560 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004561 return 0;
4562#else
4563 return -EOPNOTSUPP;
4564#endif
4565}
4566
Jens Axboe4840e412019-12-25 22:03:45 -07004567static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4568{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004569 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004570 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004571 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4572 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004573
4574 req->fadvise.offset = READ_ONCE(sqe->off);
4575 req->fadvise.len = READ_ONCE(sqe->len);
4576 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4577 return 0;
4578}
4579
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004580static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004581{
4582 struct io_fadvise *fa = &req->fadvise;
4583 int ret;
4584
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004585 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004586 switch (fa->advice) {
4587 case POSIX_FADV_NORMAL:
4588 case POSIX_FADV_RANDOM:
4589 case POSIX_FADV_SEQUENTIAL:
4590 break;
4591 default:
4592 return -EAGAIN;
4593 }
4594 }
Jens Axboe4840e412019-12-25 22:03:45 -07004595
4596 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4597 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004598 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004599 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004600 return 0;
4601}
4602
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004603static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4604{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004605 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004606 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004607 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004608 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004609 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004610 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004611
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004612 req->statx.dfd = READ_ONCE(sqe->fd);
4613 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004614 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004615 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4616 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004617
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004618 return 0;
4619}
4620
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004621static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004622{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004623 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004624 int ret;
4625
Pavel Begunkov59d70012021-03-22 01:58:30 +00004626 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004627 return -EAGAIN;
4628
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004629 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4630 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004631
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004632 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004633 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004634 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004635 return 0;
4636}
4637
Jens Axboeb5dba592019-12-11 14:02:38 -07004638static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4639{
Jens Axboe14587a462020-09-05 11:36:08 -06004640 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004641 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004642 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004643 sqe->rw_flags || sqe->buf_index)
Jens Axboeb5dba592019-12-11 14:02:38 -07004644 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004645 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004646 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004647
4648 req->close.fd = READ_ONCE(sqe->fd);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004649 req->close.file_slot = READ_ONCE(sqe->file_index);
4650 if (req->close.file_slot && req->close.fd)
4651 return -EINVAL;
4652
Jens Axboeb5dba592019-12-11 14:02:38 -07004653 return 0;
4654}
4655
Pavel Begunkov889fca72021-02-10 00:03:09 +00004656static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004657{
Jens Axboe9eac1902021-01-19 15:50:37 -07004658 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004659 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004660 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004661 struct file *file = NULL;
4662 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004663
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004664 if (req->close.file_slot) {
4665 ret = io_close_fixed(req, issue_flags);
4666 goto err;
4667 }
4668
Jens Axboe9eac1902021-01-19 15:50:37 -07004669 spin_lock(&files->file_lock);
4670 fdt = files_fdtable(files);
4671 if (close->fd >= fdt->max_fds) {
4672 spin_unlock(&files->file_lock);
4673 goto err;
4674 }
4675 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004676 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004677 spin_unlock(&files->file_lock);
4678 file = NULL;
4679 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004680 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004681
4682 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004683 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004684 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004685 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004686 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004687
Jens Axboe9eac1902021-01-19 15:50:37 -07004688 ret = __close_fd_get_file(close->fd, &file);
4689 spin_unlock(&files->file_lock);
4690 if (ret < 0) {
4691 if (ret == -ENOENT)
4692 ret = -EBADF;
4693 goto err;
4694 }
4695
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004696 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004697 ret = filp_close(file, current->files);
4698err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004699 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004700 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004701 if (file)
4702 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004703 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004704 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004705}
4706
Pavel Begunkov1155c762021-02-18 18:29:38 +00004707static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004708{
4709 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004710
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004711 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4712 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004713 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4714 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004715 return -EINVAL;
4716
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004717 req->sync.off = READ_ONCE(sqe->off);
4718 req->sync.len = READ_ONCE(sqe->len);
4719 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004720 return 0;
4721}
4722
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004723static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004724{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004725 int ret;
4726
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004727 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004728 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004729 return -EAGAIN;
4730
Jens Axboe9adbd452019-12-20 08:45:55 -07004731 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004732 req->sync.flags);
4733 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004734 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004735 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004736 return 0;
4737}
4738
YueHaibing469956e2020-03-04 15:53:52 +08004739#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004740static int io_setup_async_msg(struct io_kiocb *req,
4741 struct io_async_msghdr *kmsg)
4742{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004743 struct io_async_msghdr *async_msg = req->async_data;
4744
4745 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004746 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004747 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004748 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004749 return -ENOMEM;
4750 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004751 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004752 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004753 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004754 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004755 /* if were using fast_iov, set it to the new one */
4756 if (!async_msg->free_iov)
4757 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4758
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004759 return -EAGAIN;
4760}
4761
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004762static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4763 struct io_async_msghdr *iomsg)
4764{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004765 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004766 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004767 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004768 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004769}
4770
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004771static int io_sendmsg_prep_async(struct io_kiocb *req)
4772{
4773 int ret;
4774
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004775 ret = io_sendmsg_copy_hdr(req, req->async_data);
4776 if (!ret)
4777 req->flags |= REQ_F_NEED_CLEANUP;
4778 return ret;
4779}
4780
Jens Axboe3529d8c2019-12-19 18:24:38 -07004781static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004782{
Jens Axboee47293f2019-12-20 08:58:21 -07004783 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004784
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004785 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4786 return -EINVAL;
4787
Pavel Begunkov270a5942020-07-12 20:41:04 +03004788 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004789 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004790 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4791 if (sr->msg_flags & MSG_DONTWAIT)
4792 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004793
Jens Axboed8768362020-02-27 14:17:49 -07004794#ifdef CONFIG_COMPAT
4795 if (req->ctx->compat)
4796 sr->msg_flags |= MSG_CMSG_COMPAT;
4797#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004798 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004799}
4800
Pavel Begunkov889fca72021-02-10 00:03:09 +00004801static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004802{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004803 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004804 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004805 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004806 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004807 int ret;
4808
Florent Revestdba4a922020-12-04 12:36:04 +01004809 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004810 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004811 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004812
Pavel Begunkovd886e182021-10-04 20:02:56 +01004813 if (req_has_async_data(req)) {
4814 kmsg = req->async_data;
4815 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004816 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004817 if (ret)
4818 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004819 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004820 }
4821
Pavel Begunkov04411802021-04-01 15:44:00 +01004822 flags = req->sr_msg.msg_flags;
4823 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004824 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004825 if (flags & MSG_WAITALL)
4826 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4827
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004828 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004829
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004830 if (ret < min_ret) {
4831 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
4832 return io_setup_async_msg(req, kmsg);
4833 if (ret == -ERESTARTSYS)
4834 ret = -EINTR;
4835 req_set_fail(req);
4836 }
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004837 /* fast path, check for non-NULL to avoid function call */
4838 if (kmsg->free_iov)
4839 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004840 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov889fca72021-02-10 00:03:09 +00004841 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004842 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004843}
4844
Pavel Begunkov889fca72021-02-10 00:03:09 +00004845static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004846{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004847 struct io_sr_msg *sr = &req->sr_msg;
4848 struct msghdr msg;
4849 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004850 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004851 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004852 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004853 int ret;
4854
Florent Revestdba4a922020-12-04 12:36:04 +01004855 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004856 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004857 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004858
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004859 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4860 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004861 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004862
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004863 msg.msg_name = NULL;
4864 msg.msg_control = NULL;
4865 msg.msg_controllen = 0;
4866 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004867
Pavel Begunkov04411802021-04-01 15:44:00 +01004868 flags = req->sr_msg.msg_flags;
4869 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004870 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004871 if (flags & MSG_WAITALL)
4872 min_ret = iov_iter_count(&msg.msg_iter);
4873
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004874 msg.msg_flags = flags;
4875 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004876 if (ret < min_ret) {
4877 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
4878 return -EAGAIN;
4879 if (ret == -ERESTARTSYS)
4880 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004881 req_set_fail(req);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004882 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004883 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004884 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004885}
4886
Pavel Begunkov1400e692020-07-12 20:41:05 +03004887static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4888 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004889{
4890 struct io_sr_msg *sr = &req->sr_msg;
4891 struct iovec __user *uiov;
4892 size_t iov_len;
4893 int ret;
4894
Pavel Begunkov1400e692020-07-12 20:41:05 +03004895 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4896 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004897 if (ret)
4898 return ret;
4899
4900 if (req->flags & REQ_F_BUFFER_SELECT) {
4901 if (iov_len > 1)
4902 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004903 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004904 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004905 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004906 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004907 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004908 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004909 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004910 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004911 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004912 if (ret > 0)
4913 ret = 0;
4914 }
4915
4916 return ret;
4917}
4918
4919#ifdef CONFIG_COMPAT
4920static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004921 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004922{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004923 struct io_sr_msg *sr = &req->sr_msg;
4924 struct compat_iovec __user *uiov;
4925 compat_uptr_t ptr;
4926 compat_size_t len;
4927 int ret;
4928
Pavel Begunkov4af34172021-04-11 01:46:30 +01004929 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4930 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004931 if (ret)
4932 return ret;
4933
4934 uiov = compat_ptr(ptr);
4935 if (req->flags & REQ_F_BUFFER_SELECT) {
4936 compat_ssize_t clen;
4937
4938 if (len > 1)
4939 return -EINVAL;
4940 if (!access_ok(uiov, sizeof(*uiov)))
4941 return -EFAULT;
4942 if (__get_user(clen, &uiov->iov_len))
4943 return -EFAULT;
4944 if (clen < 0)
4945 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004946 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004947 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004948 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004949 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004950 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004951 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004952 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004953 if (ret < 0)
4954 return ret;
4955 }
4956
4957 return 0;
4958}
Jens Axboe03b12302019-12-02 18:50:25 -07004959#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004960
Pavel Begunkov1400e692020-07-12 20:41:05 +03004961static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4962 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004963{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004964 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004965
4966#ifdef CONFIG_COMPAT
4967 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004968 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004969#endif
4970
Pavel Begunkov1400e692020-07-12 20:41:05 +03004971 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004972}
4973
Jens Axboebcda7ba2020-02-23 16:42:51 -07004974static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov51aac422021-10-14 16:10:17 +01004975 unsigned int issue_flags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004976{
4977 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004978
Pavel Begunkov51aac422021-10-14 16:10:17 +01004979 return io_buffer_select(req, &sr->len, sr->bgid, issue_flags);
Jens Axboe03b12302019-12-02 18:50:25 -07004980}
4981
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004982static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4983{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01004984 return io_put_kbuf(req, req->kbuf);
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004985}
4986
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004987static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004988{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004989 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004990
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004991 ret = io_recvmsg_copy_hdr(req, req->async_data);
4992 if (!ret)
4993 req->flags |= REQ_F_NEED_CLEANUP;
4994 return ret;
4995}
4996
4997static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4998{
4999 struct io_sr_msg *sr = &req->sr_msg;
5000
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03005001 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5002 return -EINVAL;
5003
Pavel Begunkov270a5942020-07-12 20:41:04 +03005004 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07005005 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005006 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01005007 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
5008 if (sr->msg_flags & MSG_DONTWAIT)
5009 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005010
Jens Axboed8768362020-02-27 14:17:49 -07005011#ifdef CONFIG_COMPAT
5012 if (req->ctx->compat)
5013 sr->msg_flags |= MSG_CMSG_COMPAT;
5014#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005015 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07005016}
5017
Pavel Begunkov889fca72021-02-10 00:03:09 +00005018static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07005019{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005020 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005021 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005022 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005023 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005024 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07005025 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005026 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005027
Florent Revestdba4a922020-12-04 12:36:04 +01005028 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005029 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005030 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005031
Pavel Begunkovd886e182021-10-04 20:02:56 +01005032 if (req_has_async_data(req)) {
5033 kmsg = req->async_data;
5034 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005035 ret = io_recvmsg_copy_hdr(req, &iomsg);
5036 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03005037 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005038 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005039 }
5040
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005041 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01005042 kbuf = io_recv_buffer_select(req, issue_flags);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005043 if (IS_ERR(kbuf))
5044 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005045 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00005046 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
5047 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005048 1, req->sr_msg.len);
5049 }
5050
Pavel Begunkov04411802021-04-01 15:44:00 +01005051 flags = req->sr_msg.msg_flags;
5052 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005053 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005054 if (flags & MSG_WAITALL)
5055 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
5056
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005057 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
5058 kmsg->uaddr, flags);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005059 if (ret < min_ret) {
5060 if (ret == -EAGAIN && force_nonblock)
5061 return io_setup_async_msg(req, kmsg);
5062 if (ret == -ERESTARTSYS)
5063 ret = -EINTR;
5064 req_set_fail(req);
5065 } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
5066 req_set_fail(req);
5067 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005068
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005069 if (req->flags & REQ_F_BUFFER_SELECTED)
5070 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005071 /* fast path, check for non-NULL to avoid function call */
5072 if (kmsg->free_iov)
5073 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005074 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov889fca72021-02-10 00:03:09 +00005075 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005076 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005077}
5078
Pavel Begunkov889fca72021-02-10 00:03:09 +00005079static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07005080{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005081 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005082 struct io_sr_msg *sr = &req->sr_msg;
5083 struct msghdr msg;
5084 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005085 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005086 struct iovec iov;
5087 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005088 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005089 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005090 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005091
Florent Revestdba4a922020-12-04 12:36:04 +01005092 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005093 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005094 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005095
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005096 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01005097 kbuf = io_recv_buffer_select(req, issue_flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005098 if (IS_ERR(kbuf))
5099 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005100 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005101 }
5102
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005103 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005104 if (unlikely(ret))
5105 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005106
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005107 msg.msg_name = NULL;
5108 msg.msg_control = NULL;
5109 msg.msg_controllen = 0;
5110 msg.msg_namelen = 0;
5111 msg.msg_iocb = NULL;
5112 msg.msg_flags = 0;
5113
Pavel Begunkov04411802021-04-01 15:44:00 +01005114 flags = req->sr_msg.msg_flags;
5115 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005116 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005117 if (flags & MSG_WAITALL)
5118 min_ret = iov_iter_count(&msg.msg_iter);
5119
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005120 ret = sock_recvmsg(sock, &msg, flags);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005121out_free:
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005122 if (ret < min_ret) {
5123 if (ret == -EAGAIN && force_nonblock)
5124 return -EAGAIN;
5125 if (ret == -ERESTARTSYS)
5126 ret = -EINTR;
5127 req_set_fail(req);
5128 } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
5129 req_set_fail(req);
5130 }
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005131 if (req->flags & REQ_F_BUFFER_SELECTED)
5132 cflags = io_put_recv_kbuf(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005133 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005134 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005135}
5136
Jens Axboe3529d8c2019-12-19 18:24:38 -07005137static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005138{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005139 struct io_accept *accept = &req->accept;
5140
Jens Axboe14587a462020-09-05 11:36:08 -06005141 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005142 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005143 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005144 return -EINVAL;
5145
Jens Axboed55e5f52019-12-11 16:12:15 -07005146 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5147 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005148 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005149 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005150
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005151 accept->file_slot = READ_ONCE(sqe->file_index);
5152 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5153 (accept->flags & SOCK_CLOEXEC)))
5154 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005155 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5156 return -EINVAL;
5157 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5158 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005159 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005160}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005161
Pavel Begunkov889fca72021-02-10 00:03:09 +00005162static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005163{
5164 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005165 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005166 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005167 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005168 struct file *file;
5169 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005170
Jiufei Xuee697dee2020-06-10 13:41:59 +08005171 if (req->file->f_flags & O_NONBLOCK)
5172 req->flags |= REQ_F_NOWAIT;
5173
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005174 if (!fixed) {
5175 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5176 if (unlikely(fd < 0))
5177 return fd;
5178 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005179 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5180 accept->flags);
5181 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005182 if (!fixed)
5183 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005184 ret = PTR_ERR(file);
5185 if (ret == -EAGAIN && force_nonblock)
5186 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005187 if (ret == -ERESTARTSYS)
5188 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005189 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005190 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005191 fd_install(fd, file);
5192 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005193 } else {
5194 ret = io_install_fixed_file(req, file, issue_flags,
5195 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005196 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005197 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005198 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005199}
5200
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005201static int io_connect_prep_async(struct io_kiocb *req)
5202{
5203 struct io_async_connect *io = req->async_data;
5204 struct io_connect *conn = &req->connect;
5205
5206 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5207}
5208
Jens Axboe3529d8c2019-12-19 18:24:38 -07005209static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005210{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005211 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005212
Jens Axboe14587a462020-09-05 11:36:08 -06005213 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005214 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005215 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5216 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005217 return -EINVAL;
5218
Jens Axboe3529d8c2019-12-19 18:24:38 -07005219 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5220 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005221 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005222}
5223
Pavel Begunkov889fca72021-02-10 00:03:09 +00005224static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005225{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005226 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005227 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005228 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005229 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005230
Pavel Begunkovd886e182021-10-04 20:02:56 +01005231 if (req_has_async_data(req)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005232 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005233 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005234 ret = move_addr_to_kernel(req->connect.addr,
5235 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005236 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005237 if (ret)
5238 goto out;
5239 io = &__io;
5240 }
5241
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005242 file_flags = force_nonblock ? O_NONBLOCK : 0;
5243
Jens Axboee8c2bc12020-08-15 18:44:09 -07005244 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005245 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005246 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Pavel Begunkovd886e182021-10-04 20:02:56 +01005247 if (req_has_async_data(req))
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005248 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005249 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005250 ret = -ENOMEM;
5251 goto out;
5252 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005253 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005254 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005255 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005256 if (ret == -ERESTARTSYS)
5257 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005258out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005259 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005260 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005261 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005262 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005263}
YueHaibing469956e2020-03-04 15:53:52 +08005264#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005265#define IO_NETOP_FN(op) \
5266static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5267{ \
5268 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005269}
5270
Jens Axboe99a10082021-02-19 09:35:19 -07005271#define IO_NETOP_PREP(op) \
5272IO_NETOP_FN(op) \
5273static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5274{ \
5275 return -EOPNOTSUPP; \
5276} \
5277
5278#define IO_NETOP_PREP_ASYNC(op) \
5279IO_NETOP_PREP(op) \
5280static int io_##op##_prep_async(struct io_kiocb *req) \
5281{ \
5282 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005283}
5284
Jens Axboe99a10082021-02-19 09:35:19 -07005285IO_NETOP_PREP_ASYNC(sendmsg);
5286IO_NETOP_PREP_ASYNC(recvmsg);
5287IO_NETOP_PREP_ASYNC(connect);
5288IO_NETOP_PREP(accept);
5289IO_NETOP_FN(send);
5290IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005291#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005292
Jens Axboed7718a92020-02-14 22:23:12 -07005293struct io_poll_table {
5294 struct poll_table_struct pt;
5295 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005296 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005297 int error;
5298};
5299
Jens Axboed7718a92020-02-14 22:23:12 -07005300static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005301 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07005302{
Jens Axboed7718a92020-02-14 22:23:12 -07005303 /* for instances that support it check for an event match first: */
5304 if (mask && !(mask & poll->events))
5305 return 0;
5306
5307 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5308
5309 list_del_init(&poll->wait.entry);
5310
Jens Axboed7718a92020-02-14 22:23:12 -07005311 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005312 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005313
Jens Axboed7718a92020-02-14 22:23:12 -07005314 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005315 * If this fails, then the task is exiting. When a task exits, the
5316 * work gets canceled, so just cancel this request as well instead
5317 * of executing it. We can't safely execute it anyway, as we may not
5318 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005319 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005320 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005321 return 1;
5322}
5323
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005324static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5325 __acquires(&req->ctx->completion_lock)
5326{
5327 struct io_ring_ctx *ctx = req->ctx;
5328
Jens Axboe316319e2021-08-19 09:41:42 -06005329 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005330 if (unlikely(req->task->flags & PF_EXITING))
5331 WRITE_ONCE(poll->canceled, true);
5332
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005333 if (!req->result && !READ_ONCE(poll->canceled)) {
5334 struct poll_table_struct pt = { ._key = poll->events };
5335
5336 req->result = vfs_poll(req->file, &pt) & poll->events;
5337 }
5338
Jens Axboe79ebeae2021-08-10 15:18:27 -06005339 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005340 if (!req->result && !READ_ONCE(poll->canceled)) {
5341 add_wait_queue(poll->head, &poll->wait);
5342 return true;
5343 }
5344
5345 return false;
5346}
5347
Jens Axboed4e7cd32020-08-15 11:44:50 -07005348static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005349{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005350 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005351 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005352 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005353 return req->apoll->double_poll;
5354}
5355
5356static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5357{
5358 if (req->opcode == IORING_OP_POLL_ADD)
5359 return &req->poll;
5360 return &req->apoll->poll;
5361}
5362
5363static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005364 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005365{
5366 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005367
5368 lockdep_assert_held(&req->ctx->completion_lock);
5369
5370 if (poll && poll->head) {
5371 struct wait_queue_head *head = poll->head;
5372
Jens Axboe79ebeae2021-08-10 15:18:27 -06005373 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005374 list_del_init(&poll->wait.entry);
5375 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005376 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005377 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005378 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005379 }
5380}
5381
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005382static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005383 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005384{
5385 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005386 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005387 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005388
Pavel Begunkove27414b2021-04-09 09:13:20 +01005389 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005390 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005391 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005392 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005393 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005394 }
Jens Axboeb69de282021-03-17 08:37:41 -06005395 if (req->poll.events & EPOLLONESHOT)
5396 flags = 0;
Pavel Begunkov913a5712021-11-10 15:49:31 +00005397
5398 if (!(flags & IORING_CQE_F_MORE)) {
5399 io_fill_cqe_req(req, error, flags);
5400 } else if (!io_fill_cqe_aux(ctx, req->user_data, error, flags)) {
Hao Xua62682f2021-09-22 18:12:37 +08005401 req->poll.events |= EPOLLONESHOT;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005402 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005403 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005404 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005405}
5406
Pavel Begunkovf237c302021-08-18 12:42:46 +01005407static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005408{
Jens Axboe6d816e02020-08-11 08:04:14 -06005409 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005410 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005411
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005412 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005413 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005414 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005415 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005416
Hao Xu5b7aa382021-09-22 18:12:38 +08005417 if (req->poll.done) {
5418 spin_unlock(&ctx->completion_lock);
5419 return;
5420 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005421 done = __io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005422 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005423 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005424 hash_del(&req->hash_node);
Hao Xubd99c712021-09-22 18:12:36 +08005425 req->poll.done = true;
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005426 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005427 req->result = 0;
5428 add_wait_queue(req->poll.head, &req->poll.wait);
5429 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005430 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005431 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005432 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005433
Jens Axboe88e41cf2021-02-22 22:08:01 -07005434 if (done) {
5435 nxt = io_put_req_find_next(req);
5436 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005437 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005438 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005439 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005440}
5441
5442static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5443 int sync, void *key)
5444{
5445 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005446 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005447 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005448 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005449
5450 /* for instances that support it check for an event match first: */
5451 if (mask && !(mask & poll->events))
5452 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005453 if (!(poll->events & EPOLLONESHOT))
5454 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005455
Jens Axboe8706e042020-09-28 08:38:54 -06005456 list_del_init(&wait->entry);
5457
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005458 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005459 bool done;
5460
Jens Axboe79ebeae2021-08-10 15:18:27 -06005461 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005462 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005463 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005464 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005465 /* make sure double remove sees this as being gone */
5466 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005467 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005468 if (!done) {
5469 /* use wait func handler, so it matches the rq type */
5470 poll->wait.func(&poll->wait, mode, sync, key);
5471 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005472 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005473 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005474 return 1;
5475}
5476
5477static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5478 wait_queue_func_t wake_func)
5479{
5480 poll->head = NULL;
5481 poll->done = false;
5482 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005483#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5484 /* mask in events that we always want/need */
5485 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005486 INIT_LIST_HEAD(&poll->wait.entry);
5487 init_waitqueue_func_entry(&poll->wait, wake_func);
5488}
5489
5490static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005491 struct wait_queue_head *head,
5492 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005493{
5494 struct io_kiocb *req = pt->req;
5495
5496 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005497 * The file being polled uses multiple waitqueues for poll handling
5498 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5499 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005500 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005501 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005502 struct io_poll_iocb *poll_one = poll;
5503
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005504 /* double add on the same waitqueue head, ignore */
5505 if (poll_one->head == head)
5506 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005507 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005508 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005509 if ((*poll_ptr)->head == head)
5510 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005511 pt->error = -EINVAL;
5512 return;
5513 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005514 /*
5515 * Can't handle multishot for double wait for now, turn it
5516 * into one-shot mode.
5517 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005518 if (!(poll_one->events & EPOLLONESHOT))
5519 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005520 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5521 if (!poll) {
5522 pt->error = -ENOMEM;
5523 return;
5524 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005525 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005526 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005527 poll->wait.private = req;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005528
Jens Axboe807abcb2020-07-17 17:09:27 -06005529 *poll_ptr = poll;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005530 if (req->opcode == IORING_OP_POLL_ADD)
5531 req->flags |= REQ_F_ASYNC_DATA;
Jens Axboe18bceab2020-05-15 11:56:54 -06005532 }
5533
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005534 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005535 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005536
5537 if (poll->events & EPOLLEXCLUSIVE)
5538 add_wait_queue_exclusive(head, &poll->wait);
5539 else
5540 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005541}
5542
5543static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5544 struct poll_table_struct *p)
5545{
5546 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005547 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005548
Jens Axboe807abcb2020-07-17 17:09:27 -06005549 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005550}
5551
Pavel Begunkovf237c302021-08-18 12:42:46 +01005552static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005553{
Jens Axboed7718a92020-02-14 22:23:12 -07005554 struct async_poll *apoll = req->apoll;
5555 struct io_ring_ctx *ctx = req->ctx;
5556
Olivier Langlois236daeae2021-05-31 02:36:37 -04005557 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005558
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005559 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005560 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005561 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005562 }
5563
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005564 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005565 io_poll_remove_double(req);
Hao Xubd99c712021-09-22 18:12:36 +08005566 apoll->poll.done = true;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005567 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005568
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005569 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005570 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005571 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005572 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005573}
5574
5575static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5576 void *key)
5577{
5578 struct io_kiocb *req = wait->private;
5579 struct io_poll_iocb *poll = &req->apoll->poll;
5580
5581 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5582 key_to_poll(key));
5583
5584 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5585}
5586
5587static void io_poll_req_insert(struct io_kiocb *req)
5588{
5589 struct io_ring_ctx *ctx = req->ctx;
5590 struct hlist_head *list;
5591
5592 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5593 hlist_add_head(&req->hash_node, list);
5594}
5595
5596static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5597 struct io_poll_iocb *poll,
5598 struct io_poll_table *ipt, __poll_t mask,
5599 wait_queue_func_t wake_func)
5600 __acquires(&ctx->completion_lock)
5601{
5602 struct io_ring_ctx *ctx = req->ctx;
5603 bool cancel = false;
5604
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005605 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005606 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005607 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005608 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005609
5610 ipt->pt._key = mask;
5611 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005612 ipt->error = 0;
5613 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005614
Jens Axboed7718a92020-02-14 22:23:12 -07005615 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005616 if (unlikely(!ipt->nr_entries) && !ipt->error)
5617 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005618
Jens Axboe79ebeae2021-08-10 15:18:27 -06005619 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005620 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005621 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005622 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005623 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005624 if (unlikely(list_empty(&poll->wait.entry))) {
5625 if (ipt->error)
5626 cancel = true;
5627 ipt->error = 0;
5628 mask = 0;
5629 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005630 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005631 list_del_init(&poll->wait.entry);
5632 else if (cancel)
5633 WRITE_ONCE(poll->canceled, true);
5634 else if (!poll->done) /* actually waiting for an event */
5635 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005636 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005637 }
5638
5639 return mask;
5640}
5641
Olivier Langlois59b735a2021-06-22 05:17:39 -07005642enum {
5643 IO_APOLL_OK,
5644 IO_APOLL_ABORTED,
5645 IO_APOLL_READY
5646};
5647
5648static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005649{
5650 const struct io_op_def *def = &io_op_defs[req->opcode];
5651 struct io_ring_ctx *ctx = req->ctx;
5652 struct async_poll *apoll;
5653 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005654 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboed7718a92020-02-14 22:23:12 -07005655
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005656 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005657 return IO_APOLL_ABORTED;
Pavel Begunkov658d0a42021-10-23 12:13:58 +01005658 if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED))
5659 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005660
5661 if (def->pollin) {
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005662 mask |= POLLIN | POLLRDNORM;
5663
5664 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5665 if ((req->opcode == IORING_OP_RECVMSG) &&
5666 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5667 mask &= ~POLLIN;
5668 } else {
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005669 mask |= POLLOUT | POLLWRNORM;
5670 }
5671
Jens Axboed7718a92020-02-14 22:23:12 -07005672 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5673 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005674 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005675 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005676 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005677 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005678 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005679 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005680
5681 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5682 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005683 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005684 if (ret || ipt.error)
5685 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5686
Olivier Langlois236daeae2021-05-31 02:36:37 -04005687 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5688 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005689 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005690}
5691
5692static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005693 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005694 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005695{
Jens Axboeb41e9852020-02-17 09:52:41 -07005696 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005697
Jens Axboe50826202021-02-23 09:02:26 -07005698 if (!poll->head)
5699 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005700 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005701 if (do_cancel)
5702 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005703 if (!list_empty(&poll->wait.entry)) {
5704 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005705 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005706 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005707 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005708 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005709 return do_complete;
5710}
5711
Pavel Begunkov5d709042021-08-09 20:18:13 +01005712static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005713 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005714{
5715 bool do_complete;
5716
Jens Axboed4e7cd32020-08-15 11:44:50 -07005717 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005718 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005719
Jens Axboeb41e9852020-02-17 09:52:41 -07005720 if (do_complete) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005721 req_set_fail(req);
Pavel Begunkov913a5712021-11-10 15:49:31 +00005722 io_fill_cqe_req(req, -ECANCELED, 0);
5723 io_commit_cqring(req->ctx);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005724 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005725 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005726 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005727}
5728
Jens Axboe76e1b642020-09-26 15:05:03 -06005729/*
5730 * Returns true if we found and killed one or more poll requests
5731 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01005732static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx,
5733 struct task_struct *tsk, bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005734{
Jens Axboe78076bb2019-12-04 19:56:40 -07005735 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005736 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005737 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005738
Jens Axboe79ebeae2021-08-10 15:18:27 -06005739 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005740 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5741 struct hlist_head *list;
5742
5743 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005744 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005745 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005746 posted += io_poll_remove_one(req);
5747 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005748 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005749 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005750
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005751 if (posted)
5752 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005753
5754 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005755}
5756
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005757static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5758 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005759 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005760{
Jens Axboe78076bb2019-12-04 19:56:40 -07005761 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005762 struct io_kiocb *req;
5763
Jens Axboe78076bb2019-12-04 19:56:40 -07005764 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5765 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005766 if (sqe_addr != req->user_data)
5767 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005768 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5769 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005770 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005771 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005772 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005773}
5774
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005775static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5776 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005777 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005778{
5779 struct io_kiocb *req;
5780
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005781 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005782 if (!req)
5783 return -ENOENT;
5784 if (io_poll_remove_one(req))
5785 return 0;
5786
5787 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005788}
5789
Pavel Begunkov9096af32021-04-14 13:38:36 +01005790static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5791 unsigned int flags)
5792{
5793 u32 events;
5794
5795 events = READ_ONCE(sqe->poll32_events);
5796#ifdef __BIG_ENDIAN
5797 events = swahw32(events);
5798#endif
5799 if (!(flags & IORING_POLL_ADD_MULTI))
5800 events |= EPOLLONESHOT;
5801 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5802}
5803
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005804static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005805 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005806{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005807 struct io_poll_update *upd = &req->poll_update;
5808 u32 flags;
5809
Jens Axboe221c5eb2019-01-17 09:41:58 -07005810 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5811 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005812 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005813 return -EINVAL;
5814 flags = READ_ONCE(sqe->len);
5815 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5816 IORING_POLL_ADD_MULTI))
5817 return -EINVAL;
5818 /* meaningless without update */
5819 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005820 return -EINVAL;
5821
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005822 upd->old_user_data = READ_ONCE(sqe->addr);
5823 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5824 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005825
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005826 upd->new_user_data = READ_ONCE(sqe->off);
5827 if (!upd->update_user_data && upd->new_user_data)
5828 return -EINVAL;
5829 if (upd->update_events)
5830 upd->events = io_poll_parse_events(sqe, flags);
5831 else if (sqe->poll32_events)
5832 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005833
Jens Axboe221c5eb2019-01-17 09:41:58 -07005834 return 0;
5835}
5836
Jens Axboe221c5eb2019-01-17 09:41:58 -07005837static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5838 void *key)
5839{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005840 struct io_kiocb *req = wait->private;
5841 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005842
Jens Axboed7718a92020-02-14 22:23:12 -07005843 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005844}
5845
Jens Axboe221c5eb2019-01-17 09:41:58 -07005846static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5847 struct poll_table_struct *p)
5848{
5849 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5850
Jens Axboee8c2bc12020-08-15 18:44:09 -07005851 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005852}
5853
Jens Axboe3529d8c2019-12-19 18:24:38 -07005854static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005855{
5856 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005857 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005858
5859 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5860 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005861 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005862 return -EINVAL;
5863 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005864 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005865 return -EINVAL;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00005866 if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
5867 return -EINVAL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005868
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005869 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005870 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005871 return 0;
5872}
5873
Pavel Begunkov61e98202021-02-10 00:03:08 +00005874static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005875{
5876 struct io_poll_iocb *poll = &req->poll;
5877 struct io_ring_ctx *ctx = req->ctx;
5878 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005879 __poll_t mask;
Hao Xu5b7aa382021-09-22 18:12:38 +08005880 bool done;
Jens Axboe0969e782019-12-17 18:40:57 -07005881
Jens Axboed7718a92020-02-14 22:23:12 -07005882 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005883
Jens Axboed7718a92020-02-14 22:23:12 -07005884 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5885 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005886
Jens Axboe8c838782019-03-12 15:48:16 -06005887 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005888 ipt.error = 0;
Pavel Begunkoveb6e6f02021-10-04 20:02:59 +01005889 done = __io_poll_complete(req, mask);
5890 io_commit_cqring(req->ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06005891 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005892 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005893
Jens Axboe8c838782019-03-12 15:48:16 -06005894 if (mask) {
5895 io_cqring_ev_posted(ctx);
Hao Xu5b7aa382021-09-22 18:12:38 +08005896 if (done)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005897 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005898 }
Jens Axboe8c838782019-03-12 15:48:16 -06005899 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005900}
5901
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005902static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005903{
5904 struct io_ring_ctx *ctx = req->ctx;
5905 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005906 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005907 int ret;
5908
Jens Axboe79ebeae2021-08-10 15:18:27 -06005909 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005910 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005911 if (!preq) {
5912 ret = -ENOENT;
5913 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005914 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005915
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005916 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5917 completing = true;
5918 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5919 goto err;
5920 }
5921
Jens Axboecb3b200e2021-04-06 09:49:31 -06005922 /*
5923 * Don't allow racy completion with singleshot, as we cannot safely
5924 * update those. For multishot, if we're racing with completion, just
5925 * let completion re-add it.
5926 */
5927 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5928 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5929 ret = -EALREADY;
5930 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005931 }
5932 /* we now have a detached poll request. reissue. */
5933 ret = 0;
5934err:
Jens Axboeb69de282021-03-17 08:37:41 -06005935 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005936 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005937 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005938 io_req_complete(req, ret);
5939 return 0;
5940 }
5941 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005942 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005943 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005944 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005945 preq->poll.events |= IO_POLL_UNMASK;
5946 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005947 if (req->poll_update.update_user_data)
5948 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005949 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005950
Jens Axboeb69de282021-03-17 08:37:41 -06005951 /* complete update request, we're done with it */
5952 io_req_complete(req, ret);
5953
Jens Axboecb3b200e2021-04-06 09:49:31 -06005954 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005955 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005956 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005957 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005958 io_req_complete(preq, ret);
5959 }
Jens Axboeb69de282021-03-17 08:37:41 -06005960 }
5961 return 0;
5962}
5963
Pavel Begunkovf237c302021-08-18 12:42:46 +01005964static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005965{
Pavel Begunkov62245902021-10-02 19:36:14 +01005966 struct io_timeout_data *data = req->async_data;
5967
5968 if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
5969 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005970 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005971}
5972
Jens Axboe5262f562019-09-17 12:26:57 -06005973static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5974{
Jens Axboead8a48a2019-11-15 08:49:11 -07005975 struct io_timeout_data *data = container_of(timer,
5976 struct io_timeout_data, timer);
5977 struct io_kiocb *req = data->req;
5978 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005979 unsigned long flags;
5980
Jens Axboe89850fc2021-08-10 15:11:51 -06005981 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005982 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005983 atomic_set(&req->ctx->cq_timeouts,
5984 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005985 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005986
Jens Axboe89850fc2021-08-10 15:11:51 -06005987 req->io_task_work.func = io_req_task_timeout;
5988 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005989 return HRTIMER_NORESTART;
5990}
5991
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005992static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5993 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005994 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005995{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005996 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005997 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005998 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005999
6000 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006001 found = user_data == req->user_data;
6002 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06006003 break;
Jens Axboef254ac02020-08-12 17:33:30 -06006004 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006005 if (!found)
6006 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06006007
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006008 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006009 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006010 return ERR_PTR(-EALREADY);
6011 list_del_init(&req->timeout.list);
6012 return req;
6013}
6014
6015static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006016 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06006017 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006018{
6019 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6020
6021 if (IS_ERR(req))
6022 return PTR_ERR(req);
6023
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006024 req_set_fail(req);
Pavel Begunkov913a5712021-11-10 15:49:31 +00006025 io_fill_cqe_req(req, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01006026 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006027 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06006028}
6029
Jens Axboe50c1df22021-08-27 17:11:06 -06006030static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
6031{
6032 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
6033 case IORING_TIMEOUT_BOOTTIME:
6034 return CLOCK_BOOTTIME;
6035 case IORING_TIMEOUT_REALTIME:
6036 return CLOCK_REALTIME;
6037 default:
6038 /* can't happen, vetted at prep time */
6039 WARN_ON_ONCE(1);
6040 fallthrough;
6041 case 0:
6042 return CLOCK_MONOTONIC;
6043 }
6044}
6045
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006046static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6047 struct timespec64 *ts, enum hrtimer_mode mode)
6048 __must_hold(&ctx->timeout_lock)
6049{
6050 struct io_timeout_data *io;
6051 struct io_kiocb *req;
6052 bool found = false;
6053
6054 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
6055 found = user_data == req->user_data;
6056 if (found)
6057 break;
6058 }
6059 if (!found)
6060 return -ENOENT;
6061
6062 io = req->async_data;
6063 if (hrtimer_try_to_cancel(&io->timer) == -1)
6064 return -EALREADY;
6065 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
6066 io->timer.function = io_link_timeout_fn;
6067 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
6068 return 0;
6069}
6070
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006071static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6072 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06006073 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006074{
6075 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6076 struct io_timeout_data *data;
6077
6078 if (IS_ERR(req))
6079 return PTR_ERR(req);
6080
6081 req->timeout.off = 0; /* noseq */
6082 data = req->async_data;
6083 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06006084 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006085 data->timer.function = io_timeout_fn;
6086 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
6087 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07006088}
6089
Jens Axboe3529d8c2019-12-19 18:24:38 -07006090static int io_timeout_remove_prep(struct io_kiocb *req,
6091 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07006092{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006093 struct io_timeout_rem *tr = &req->timeout_rem;
6094
Jens Axboeb29472e2019-12-17 18:50:29 -07006095 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6096 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006097 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6098 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006099 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07006100 return -EINVAL;
6101
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006102 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006103 tr->addr = READ_ONCE(sqe->addr);
6104 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006105 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6106 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6107 return -EINVAL;
6108 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6109 tr->ltimeout = true;
6110 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006111 return -EINVAL;
6112 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6113 return -EFAULT;
Ye Bin20870092021-11-29 12:15:37 +08006114 if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0)
6115 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006116 } else if (tr->flags) {
6117 /* timeout removal doesn't support flags */
6118 return -EINVAL;
6119 }
6120
Jens Axboeb29472e2019-12-17 18:50:29 -07006121 return 0;
6122}
6123
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006124static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6125{
6126 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6127 : HRTIMER_MODE_REL;
6128}
6129
Jens Axboe11365042019-10-16 09:08:32 -06006130/*
6131 * Remove or update an existing timeout command
6132 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006133static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006134{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006135 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006136 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006137 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006138
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006139 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6140 spin_lock(&ctx->completion_lock);
6141 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006142 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006143 spin_unlock_irq(&ctx->timeout_lock);
6144 spin_unlock(&ctx->completion_lock);
6145 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006146 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6147
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006148 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006149 if (tr->ltimeout)
6150 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6151 else
6152 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006153 spin_unlock_irq(&ctx->timeout_lock);
6154 }
Jens Axboe11365042019-10-16 09:08:32 -06006155
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006156 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006157 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006158 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006159 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006160}
6161
Jens Axboe3529d8c2019-12-19 18:24:38 -07006162static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006163 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006164{
Jens Axboead8a48a2019-11-15 08:49:11 -07006165 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006166 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006167 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006168
Jens Axboead8a48a2019-11-15 08:49:11 -07006169 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006170 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006171 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6172 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006173 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006174 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006175 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006176 flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkov62245902021-10-02 19:36:14 +01006177 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
6178 IORING_TIMEOUT_ETIME_SUCCESS))
Jens Axboe50c1df22021-08-27 17:11:06 -06006179 return -EINVAL;
6180 /* more than one clock specified is invalid, obviously */
6181 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006182 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006183
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006184 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006185 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006186 if (unlikely(off && !req->ctx->off_timeout_used))
6187 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006188
Pavel Begunkovd6a644a2021-10-23 12:14:00 +01006189 if (WARN_ON_ONCE(req_has_async_data(req)))
6190 return -EFAULT;
6191 if (io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006192 return -ENOMEM;
6193
Jens Axboee8c2bc12020-08-15 18:44:09 -07006194 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006195 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006196 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006197
6198 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006199 return -EFAULT;
6200
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006201 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006202 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006203
6204 if (is_timeout_link) {
6205 struct io_submit_link *link = &req->ctx->submit_state.link;
6206
6207 if (!link->head)
6208 return -EINVAL;
6209 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6210 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006211 req->timeout.head = link->last;
6212 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006213 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006214 return 0;
6215}
6216
Pavel Begunkov61e98202021-02-10 00:03:08 +00006217static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006218{
Jens Axboead8a48a2019-11-15 08:49:11 -07006219 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006220 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006221 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006222 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006223
Jens Axboe89850fc2021-08-10 15:11:51 -06006224 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006225
Jens Axboe5262f562019-09-17 12:26:57 -06006226 /*
6227 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006228 * timeout event to be satisfied. If it isn't set, then this is
6229 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006230 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006231 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006232 entry = ctx->timeout_list.prev;
6233 goto add;
6234 }
Jens Axboe5262f562019-09-17 12:26:57 -06006235
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006236 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6237 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006238
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006239 /* Update the last seq here in case io_flush_timeouts() hasn't.
6240 * This is safe because ->completion_lock is held, and submissions
6241 * and completions are never mixed in the same ->completion_lock section.
6242 */
6243 ctx->cq_last_tm_flush = tail;
6244
Jens Axboe5262f562019-09-17 12:26:57 -06006245 /*
6246 * Insertion sort, ensuring the first entry in the list is always
6247 * the one we need first.
6248 */
Jens Axboe5262f562019-09-17 12:26:57 -06006249 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006250 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6251 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006252
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006253 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006254 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006255 /* nxt.seq is behind @tail, otherwise would've been completed */
6256 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006257 break;
6258 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006259add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006260 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006261 data->timer.function = io_timeout_fn;
6262 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006263 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006264 return 0;
6265}
6266
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006267struct io_cancel_data {
6268 struct io_ring_ctx *ctx;
6269 u64 user_data;
6270};
6271
Jens Axboe62755e32019-10-28 21:49:21 -06006272static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006273{
Jens Axboe62755e32019-10-28 21:49:21 -06006274 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006275 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006276
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006277 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006278}
6279
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006280static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6281 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006282{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006283 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006284 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006285 int ret = 0;
6286
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006287 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006288 return -ENOENT;
6289
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006290 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006291 switch (cancel_ret) {
6292 case IO_WQ_CANCEL_OK:
6293 ret = 0;
6294 break;
6295 case IO_WQ_CANCEL_RUNNING:
6296 ret = -EALREADY;
6297 break;
6298 case IO_WQ_CANCEL_NOTFOUND:
6299 ret = -ENOENT;
6300 break;
6301 }
6302
Jens Axboee977d6d2019-11-05 12:39:45 -07006303 return ret;
6304}
6305
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006306static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006307{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006308 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006309 int ret;
6310
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006311 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006312
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006313 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01006314 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006315 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006316
6317 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006318 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006319 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006320 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006321 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01006322 goto out;
6323 ret = io_poll_cancel(ctx, sqe_addr, false);
6324out:
6325 spin_unlock(&ctx->completion_lock);
6326 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006327}
6328
Jens Axboe3529d8c2019-12-19 18:24:38 -07006329static int io_async_cancel_prep(struct io_kiocb *req,
6330 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006331{
Jens Axboefbf23842019-12-17 18:45:56 -07006332 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006333 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006334 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6335 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006336 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6337 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006338 return -EINVAL;
6339
Jens Axboefbf23842019-12-17 18:45:56 -07006340 req->cancel.addr = READ_ONCE(sqe->addr);
6341 return 0;
6342}
6343
Pavel Begunkov61e98202021-02-10 00:03:08 +00006344static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006345{
6346 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006347 u64 sqe_addr = req->cancel.addr;
Hao Xu3b44b372021-10-18 21:34:31 +08006348 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006349 struct io_tctx_node *node;
6350 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006351
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006352 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006353 if (ret != -ENOENT)
6354 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006355
6356 /* slow path, try all io-wq's */
Hao Xu3b44b372021-10-18 21:34:31 +08006357 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006358 ret = -ENOENT;
6359 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6360 struct io_uring_task *tctx = node->task->io_uring;
6361
Pavel Begunkov58f99372021-03-12 16:25:55 +00006362 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6363 if (ret != -ENOENT)
6364 break;
6365 }
Hao Xu3b44b372021-10-18 21:34:31 +08006366 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006367done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006368 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006369 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006370 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006371 return 0;
6372}
6373
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006374static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006375 const struct io_uring_sqe *sqe)
6376{
Daniele Albano61710e42020-07-18 14:15:16 -06006377 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6378 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006379 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006380 return -EINVAL;
6381
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006382 req->rsrc_update.offset = READ_ONCE(sqe->off);
6383 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6384 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006385 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006386 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006387 return 0;
6388}
6389
Pavel Begunkov889fca72021-02-10 00:03:09 +00006390static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006391{
6392 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08006393 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006394 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006395 int ret;
6396
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006397 up.offset = req->rsrc_update.offset;
6398 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006399 up.nr = 0;
6400 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006401 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006402
Hao Xu3b44b372021-10-18 21:34:31 +08006403 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006404 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006405 &up, req->rsrc_update.nr_args);
Hao Xu3b44b372021-10-18 21:34:31 +08006406 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006407
6408 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006409 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006410 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006411 return 0;
6412}
6413
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006414static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006415{
Jens Axboed625c6e2019-12-17 19:53:05 -07006416 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006417 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006418 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006419 case IORING_OP_READV:
6420 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006421 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006422 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006423 case IORING_OP_WRITEV:
6424 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006425 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006426 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006427 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006428 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006429 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006430 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006431 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006432 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006433 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006434 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006435 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006436 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006437 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006438 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006439 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006440 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006441 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006442 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006443 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006444 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006445 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006446 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006447 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006448 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006449 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006450 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006451 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006452 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006453 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006454 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006455 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006456 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006457 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006458 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006459 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006460 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006461 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006462 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006463 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006464 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006465 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006466 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006467 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006468 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006469 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006470 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006471 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006472 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006473 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006474 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006475 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006476 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006477 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006478 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006479 case IORING_OP_SHUTDOWN:
6480 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006481 case IORING_OP_RENAMEAT:
6482 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006483 case IORING_OP_UNLINKAT:
6484 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006485 case IORING_OP_MKDIRAT:
6486 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006487 case IORING_OP_SYMLINKAT:
6488 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006489 case IORING_OP_LINKAT:
6490 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006491 }
6492
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006493 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6494 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006495 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006496}
6497
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006498static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006499{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006500 if (!io_op_defs[req->opcode].needs_async_setup)
6501 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01006502 if (WARN_ON_ONCE(req_has_async_data(req)))
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006503 return -EFAULT;
6504 if (io_alloc_async_data(req))
6505 return -EAGAIN;
6506
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006507 switch (req->opcode) {
6508 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006509 return io_rw_prep_async(req, READ);
6510 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006511 return io_rw_prep_async(req, WRITE);
6512 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006513 return io_sendmsg_prep_async(req);
6514 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006515 return io_recvmsg_prep_async(req);
6516 case IORING_OP_CONNECT:
6517 return io_connect_prep_async(req);
6518 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006519 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6520 req->opcode);
6521 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006522}
6523
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006524static u32 io_get_sequence(struct io_kiocb *req)
6525{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006526 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006527
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006528 /* need original cached_sq_head, but it was increased for each req */
6529 io_for_each_link(req, req)
6530 seq--;
6531 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006532}
6533
Pavel Begunkovc0724812021-10-04 20:02:54 +01006534static __cold void io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006535{
6536 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006537 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006538 int ret;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006539 u32 seq = io_get_sequence(req);
Pavel Begunkov3c199662021-06-15 16:47:57 +01006540
Jens Axboedef596e2019-01-09 08:59:42 -07006541 /* Still need defer if there is pending req in defer list. */
Hao Xue302f102021-11-25 17:21:02 +08006542 spin_lock(&ctx->completion_lock);
Pavel Begunkov5e371262021-09-24 22:00:04 +01006543 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) {
Hao Xue302f102021-11-25 17:21:02 +08006544 spin_unlock(&ctx->completion_lock);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006545queue:
Pavel Begunkov10c66902021-06-15 16:47:56 +01006546 ctx->drain_active = false;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006547 io_req_task_queue(req);
6548 return;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006549 }
Hao Xue302f102021-11-25 17:21:02 +08006550 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006551
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006552 ret = io_req_prep_async(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006553 if (ret) {
6554fail:
6555 io_req_complete_failed(req, ret);
6556 return;
6557 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006558 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006559 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006560 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006561 ret = -ENOMEM;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006562 goto fail;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006563 }
Jens Axboe31b51512019-01-18 22:56:34 -07006564
Jens Axboe79ebeae2021-08-10 15:18:27 -06006565 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006566 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006567 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006568 kfree(de);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006569 goto queue;
Jens Axboe31b51512019-01-18 22:56:34 -07006570 }
6571
6572 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006573 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006574 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006575 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006576 spin_unlock(&ctx->completion_lock);
Jens Axboe31b51512019-01-18 22:56:34 -07006577}
6578
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006579static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006580{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006581 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01006582 kfree(req->kbuf);
6583 req->kbuf = NULL;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006584 }
6585
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006586 if (req->flags & REQ_F_NEED_CLEANUP) {
6587 switch (req->opcode) {
6588 case IORING_OP_READV:
6589 case IORING_OP_READ_FIXED:
6590 case IORING_OP_READ:
6591 case IORING_OP_WRITEV:
6592 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006593 case IORING_OP_WRITE: {
6594 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006595
6596 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006597 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006598 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006599 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006600 case IORING_OP_SENDMSG: {
6601 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006602
6603 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006604 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006605 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006606 case IORING_OP_SPLICE:
6607 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006608 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6609 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006610 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006611 case IORING_OP_OPENAT:
6612 case IORING_OP_OPENAT2:
6613 if (req->open.filename)
6614 putname(req->open.filename);
6615 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006616 case IORING_OP_RENAMEAT:
6617 putname(req->rename.oldpath);
6618 putname(req->rename.newpath);
6619 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006620 case IORING_OP_UNLINKAT:
6621 putname(req->unlink.filename);
6622 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006623 case IORING_OP_MKDIRAT:
6624 putname(req->mkdir.filename);
6625 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006626 case IORING_OP_SYMLINKAT:
6627 putname(req->symlink.oldpath);
6628 putname(req->symlink.newpath);
6629 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006630 case IORING_OP_LINKAT:
6631 putname(req->hardlink.oldpath);
6632 putname(req->hardlink.newpath);
6633 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006634 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006635 }
Jens Axboe75652a302021-04-15 09:52:40 -06006636 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6637 kfree(req->apoll->double_poll);
6638 kfree(req->apoll);
6639 req->apoll = NULL;
6640 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006641 if (req->flags & REQ_F_INFLIGHT) {
6642 struct io_uring_task *tctx = req->task->io_uring;
6643
6644 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006645 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006646 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006647 put_cred(req->creds);
Pavel Begunkovd886e182021-10-04 20:02:56 +01006648 if (req->flags & REQ_F_ASYNC_DATA) {
6649 kfree(req->async_data);
6650 req->async_data = NULL;
6651 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006652 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006653}
6654
Pavel Begunkov889fca72021-02-10 00:03:09 +00006655static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006656{
Jens Axboe5730b272021-02-27 15:57:30 -07006657 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006658 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006659
Pavel Begunkov6878b402021-09-24 21:59:41 +01006660 if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006661 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006662
Paul Moore5bd21822021-02-16 19:46:48 -05006663 if (!io_op_defs[req->opcode].audit_skip)
6664 audit_uring_entry(req->opcode);
6665
Jens Axboed625c6e2019-12-17 19:53:05 -07006666 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006667 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006668 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006669 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006670 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006671 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006672 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006673 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006674 break;
6675 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006676 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006677 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006678 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006679 break;
6680 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006681 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006682 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006683 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006684 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006685 break;
6686 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006687 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006688 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006689 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006690 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006691 break;
6692 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006693 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006694 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006695 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006696 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006697 break;
6698 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006699 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006700 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006701 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006702 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006703 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006704 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006705 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006706 break;
6707 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006708 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006709 break;
6710 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006711 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006712 break;
6713 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006714 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006715 break;
6716 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006717 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006718 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006719 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006720 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006721 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006722 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006723 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006724 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006725 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006726 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006727 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006728 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006729 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006730 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006731 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006732 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006733 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006734 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006735 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006736 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006737 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006738 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006739 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006740 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006741 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006742 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006743 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006744 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006745 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006746 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006747 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006748 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006749 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006750 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006751 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006752 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006753 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006754 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006755 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006756 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006757 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006758 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006759 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006760 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006761 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006762 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006763 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006764 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006765 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006766 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006767 case IORING_OP_MKDIRAT:
6768 ret = io_mkdirat(req, issue_flags);
6769 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006770 case IORING_OP_SYMLINKAT:
6771 ret = io_symlinkat(req, issue_flags);
6772 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006773 case IORING_OP_LINKAT:
6774 ret = io_linkat(req, issue_flags);
6775 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006776 default:
6777 ret = -EINVAL;
6778 break;
6779 }
Jens Axboe31b51512019-01-18 22:56:34 -07006780
Paul Moore5bd21822021-02-16 19:46:48 -05006781 if (!io_op_defs[req->opcode].audit_skip)
6782 audit_uring_exit(!ret, ret);
6783
Jens Axboe5730b272021-02-27 15:57:30 -07006784 if (creds)
6785 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006786 if (ret)
6787 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006788 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkov99830282021-10-15 17:09:11 +01006789 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file)
Pavel Begunkov98821312021-10-15 17:09:12 +01006790 io_iopoll_req_issued(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006791
6792 return 0;
6793}
6794
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006795static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6796{
6797 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6798
6799 req = io_put_req_find_next(req);
6800 return req ? &req->work : NULL;
6801}
6802
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006803static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006804{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006805 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006806 unsigned int issue_flags = IO_URING_F_UNLOCKED;
6807 bool needs_poll = false;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006808 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006809 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006810
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006811 /* one will be dropped by ->io_free_work() after returning to io-wq */
6812 if (!(req->flags & REQ_F_REFCOUNT))
6813 __io_req_set_refcount(req, 2);
6814 else
6815 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006816
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006817 timeout = io_prep_linked_timeout(req);
6818 if (timeout)
6819 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006820
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006821 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006822 if (work->flags & IO_WQ_WORK_CANCEL) {
6823 io_req_task_queue_fail(req, -ECANCELED);
6824 return;
Jens Axboe561fb042019-10-24 07:25:42 -06006825 }
Jens Axboe31b51512019-01-18 22:56:34 -07006826
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006827 if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovafb7f56f2021-10-23 12:13:59 +01006828 const struct io_op_def *def = &io_op_defs[req->opcode];
6829 bool opcode_poll = def->pollin || def->pollout;
6830
6831 if (opcode_poll && file_can_poll(req->file)) {
6832 needs_poll = true;
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006833 issue_flags |= IO_URING_F_NONBLOCK;
Pavel Begunkovafb7f56f2021-10-23 12:13:59 +01006834 }
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006835 }
Hao Xu90fa0282021-10-18 21:34:45 +08006836
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006837 do {
6838 ret = io_issue_sqe(req, issue_flags);
6839 if (ret != -EAGAIN)
6840 break;
6841 /*
6842 * We can get EAGAIN for iopolled IO even though we're
6843 * forcing a sync submission from here, since we can't
6844 * wait for request slots on the block side.
6845 */
6846 if (!needs_poll) {
6847 cond_resched();
6848 continue;
Hao Xu90fa0282021-10-18 21:34:45 +08006849 }
6850
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006851 if (io_arm_poll_handler(req) == IO_APOLL_OK)
6852 return;
6853 /* aborted or ready, in either case retry blocking */
6854 needs_poll = false;
6855 issue_flags &= ~IO_URING_F_NONBLOCK;
6856 } while (1);
Jens Axboe31b51512019-01-18 22:56:34 -07006857
Pavel Begunkova3df76982021-02-18 22:32:52 +00006858 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006859 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006860 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006861}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006862
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006863static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006864 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006865{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006866 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006867}
6868
Jens Axboe09bb8392019-03-13 12:39:28 -06006869static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6870 int index)
6871{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006872 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006873
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006874 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006875}
6876
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006877static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006878{
6879 unsigned long file_ptr = (unsigned long) file;
6880
Pavel Begunkov88459b52021-10-17 00:07:10 +01006881 file_ptr |= io_file_get_flags(file);
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006882 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006883}
6884
Pavel Begunkovac177052021-08-09 13:04:02 +01006885static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6886 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006887{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006888 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006889 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006890
Pavel Begunkovac177052021-08-09 13:04:02 +01006891 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6892 return NULL;
6893 fd = array_index_nospec(fd, ctx->nr_user_files);
6894 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6895 file = (struct file *) (file_ptr & FFS_MASK);
6896 file_ptr &= ~FFS_MASK;
6897 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01006898 req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
Pavel Begunkova46be972021-10-09 23:14:40 +01006899 io_req_set_rsrc_node(req, ctx);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006900 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006901}
6902
Pavel Begunkovac177052021-08-09 13:04:02 +01006903static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006904 struct io_kiocb *req, int fd)
6905{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006906 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006907
6908 trace_io_uring_file_get(ctx, fd);
6909
6910 /* we don't allow fixed io_uring files */
6911 if (file && unlikely(file->f_op == &io_uring_fops))
6912 io_req_track_inflight(req);
6913 return file;
6914}
6915
6916static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006917 struct io_kiocb *req, int fd, bool fixed)
6918{
6919 if (fixed)
6920 return io_file_get_fixed(ctx, req, fd);
6921 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006922 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006923}
6924
Pavel Begunkovf237c302021-08-18 12:42:46 +01006925static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006926{
6927 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006928 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006929
6930 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006931 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006932 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006933 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006934 } else {
6935 io_req_complete_post(req, -ETIME, 0);
6936 }
6937}
6938
Jens Axboe2665abf2019-11-05 12:40:47 -07006939static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6940{
Jens Axboead8a48a2019-11-15 08:49:11 -07006941 struct io_timeout_data *data = container_of(timer,
6942 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006943 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006944 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006945 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006946
Jens Axboe89b263f2021-08-10 15:14:18 -06006947 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006948 prev = req->timeout.head;
6949 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006950
6951 /*
6952 * We don't expect the list to be empty, that will only happen if we
6953 * race with the completion of the linked work.
6954 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006955 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006956 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006957 if (!req_ref_inc_not_zero(prev))
6958 prev = NULL;
6959 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006960 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06006961 req->timeout.prev = prev;
6962 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006963
Jens Axboe89b263f2021-08-10 15:14:18 -06006964 req->io_task_work.func = io_req_task_link_timeout;
6965 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006966 return HRTIMER_NORESTART;
6967}
6968
Pavel Begunkovde968c12021-03-19 17:22:33 +00006969static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006970{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006971 struct io_ring_ctx *ctx = req->ctx;
6972
Jens Axboe89b263f2021-08-10 15:14:18 -06006973 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006974 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006975 * If the back reference is NULL, then our linked request finished
6976 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006977 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006978 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006979 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006980
Jens Axboead8a48a2019-11-15 08:49:11 -07006981 data->timer.function = io_link_timeout_fn;
6982 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6983 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006984 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07006985 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006986 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006987 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006988 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006989}
6990
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006991static void io_queue_sqe_arm_apoll(struct io_kiocb *req)
6992 __must_hold(&req->ctx->uring_lock)
6993{
6994 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
6995
6996 switch (io_arm_poll_handler(req)) {
6997 case IO_APOLL_READY:
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006998 io_req_task_queue(req);
6999 break;
7000 case IO_APOLL_ABORTED:
7001 /*
7002 * Queued up for async execution, worker will release
7003 * submit reference when the iocb is actually submitted.
7004 */
7005 io_queue_async_work(req, NULL);
7006 break;
7007 }
7008
7009 if (linked_timeout)
7010 io_queue_linked_timeout(linked_timeout);
7011}
7012
7013static inline void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007014 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007015{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01007016 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06007017 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007018
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00007019 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06007020
Pavel Begunkovfff4e402021-10-04 20:02:48 +01007021 if (req->flags & REQ_F_COMPLETE_INLINE) {
7022 io_req_add_compl_list(req);
Pavel Begunkovd9f9d282021-09-24 22:00:00 +01007023 return;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01007024 }
Jens Axboe491381ce2019-10-17 09:20:46 -06007025 /*
7026 * We async punt it if the file wasn't marked NOWAIT, or if the file
7027 * doesn't support non-blocking read/write attempts
7028 */
Pavel Begunkov18400382021-03-19 17:22:34 +00007029 if (likely(!ret)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01007030 linked_timeout = io_prep_linked_timeout(req);
7031 if (linked_timeout)
7032 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00007033 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01007034 io_queue_sqe_arm_apoll(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01007035 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00007036 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06007037 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007038}
7039
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007040static void io_queue_sqe_fallback(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007041 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08007042{
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007043 if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01007044 io_req_complete_fail_submit(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01007045 } else if (unlikely(req->ctx->drain_active)) {
7046 io_drain_req(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01007047 } else {
7048 int ret = io_req_prep_async(req);
7049
7050 if (unlikely(ret))
7051 io_req_complete_failed(req, ret);
7052 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01007053 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07007054 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007055}
7056
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007057static inline void io_queue_sqe(struct io_kiocb *req)
7058 __must_hold(&req->ctx->uring_lock)
7059{
7060 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))))
7061 __io_queue_sqe(req);
7062 else
7063 io_queue_sqe_fallback(req);
7064}
7065
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007066/*
7067 * Check SQE restrictions (opcode and flags).
7068 *
7069 * Returns 'true' if SQE is allowed, 'false' otherwise.
7070 */
7071static inline bool io_check_restriction(struct io_ring_ctx *ctx,
7072 struct io_kiocb *req,
7073 unsigned int sqe_flags)
7074{
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007075 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
7076 return false;
7077
7078 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
7079 ctx->restrictions.sqe_flags_required)
7080 return false;
7081
7082 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
7083 ctx->restrictions.sqe_flags_required))
7084 return false;
7085
7086 return true;
7087}
7088
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007089static void io_init_req_drain(struct io_kiocb *req)
7090{
7091 struct io_ring_ctx *ctx = req->ctx;
7092 struct io_kiocb *head = ctx->submit_state.link.head;
7093
7094 ctx->drain_active = true;
7095 if (head) {
7096 /*
7097 * If we need to drain a request in the middle of a link, drain
7098 * the head request and the next request/link after the current
7099 * link. Considering sequential execution of links,
Hao Xub6c7db32021-11-25 17:21:03 +08007100 * REQ_F_IO_DRAIN will be maintained for every request of our
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007101 * link.
7102 */
Hao Xub6c7db32021-11-25 17:21:03 +08007103 head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007104 ctx->drain_next = true;
7105 }
7106}
7107
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007108static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007109 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007110 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007111{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007112 unsigned int sqe_flags;
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007113 int personality;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007114 u8 opcode;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007115
Pavel Begunkov864ea922021-08-09 13:04:08 +01007116 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007117 req->opcode = opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007118 /* same numerical values with corresponding REQ_F_*, safe to copy */
7119 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007120 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007121 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007122 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03007123 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007124
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007125 if (unlikely(opcode >= IORING_OP_LAST)) {
7126 req->opcode = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007127 return -EINVAL;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007128 }
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007129 if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
7130 /* enforce forwards compatibility on users */
7131 if (sqe_flags & ~SQE_VALID_FLAGS)
7132 return -EINVAL;
7133 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007134 !io_op_defs[opcode].buffer_select)
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007135 return -EOPNOTSUPP;
Pavel Begunkov5562a8d2021-11-10 15:49:34 +00007136 if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
7137 ctx->drain_disabled = true;
7138 if (sqe_flags & IOSQE_IO_DRAIN) {
7139 if (ctx->drain_disabled)
7140 return -EOPNOTSUPP;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007141 io_init_req_drain(req);
Pavel Begunkov5562a8d2021-11-10 15:49:34 +00007142 }
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007143 }
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007144 if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) {
7145 if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags))
7146 return -EACCES;
7147 /* knock it to the slow queue path, will be drained there */
7148 if (ctx->drain_active)
7149 req->flags |= REQ_F_FORCE_ASYNC;
7150 /* if there is no link, we're at "next" request and need to drain */
7151 if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
7152 ctx->drain_next = false;
7153 ctx->drain_active = true;
Hao Xub6c7db32021-11-25 17:21:03 +08007154 req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007155 }
7156 }
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007157
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007158 if (io_op_defs[opcode].needs_file) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007159 struct io_submit_state *state = &ctx->submit_state;
7160
7161 /*
7162 * Plug now if we have more than 2 IO left after this, and the
7163 * target is potentially a read/write to block based storage.
7164 */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007165 if (state->need_plug && io_op_defs[opcode].plug) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007166 state->plug_started = true;
7167 state->need_plug = false;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -06007168 blk_start_plug_nr_ios(&state->plug, state->submit_nr);
Pavel Begunkov6d634162021-10-06 16:06:46 +01007169 }
7170
Pavel Begunkov62906e82021-08-10 14:52:47 +01007171 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007172 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007173 if (unlikely(!req->file))
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007174 return -EBADF;
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007175 }
Pavel Begunkovc11368a52020-05-17 14:13:42 +03007176
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007177 personality = READ_ONCE(sqe->personality);
Jens Axboe63ff8222020-05-07 14:56:15 -06007178 if (personality) {
Linus Torvaldscdab10b2021-11-01 21:06:18 -07007179 int ret;
7180
Jens Axboe63ff8222020-05-07 14:56:15 -06007181 req->creds = xa_load(&ctx->personalities, personality);
7182 if (!req->creds)
Jens Axboe27926b62020-10-28 09:33:23 -06007183 return -EINVAL;
Jens Axboe63ff8222020-05-07 14:56:15 -06007184 get_cred(req->creds);
Paul Moorecdc14042021-02-01 19:56:49 -05007185 ret = security_uring_override_creds(req->creds);
7186 if (ret) {
7187 put_cred(req->creds);
7188 return ret;
7189 }
Jens Axboe63ff8222020-05-07 14:56:15 -06007190 req->flags |= REQ_F_CREDS;
7191 }
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007192
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007193 return io_req_prep(req, sqe);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007194}
7195
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007196static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007197 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007198 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007199{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007200 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007201 int ret;
7202
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007203 ret = io_init_req(ctx, req, sqe);
7204 if (unlikely(ret)) {
Jens Axboea87acfd2021-09-11 16:04:50 -06007205 trace_io_uring_req_failed(sqe, ret);
7206
Hao Xua8295b92021-08-27 17:46:09 +08007207 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007208 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007209 /*
7210 * we can judge a link req is failed or cancelled by if
7211 * REQ_F_FAIL is set, but the head is an exception since
7212 * it may be set REQ_F_FAIL because of other req's failure
7213 * so let's leverage req->result to distinguish if a head
7214 * is set REQ_F_FAIL because of its failure or other req's
7215 * failure so that we can set the correct ret code for it.
7216 * init result here to avoid affecting the normal path.
7217 */
7218 if (!(link->head->flags & REQ_F_FAIL))
7219 req_fail_link_node(link->head, -ECANCELED);
7220 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7221 /*
7222 * the current req is a normal req, we should return
7223 * error and thus break the submittion loop.
7224 */
7225 io_req_complete_failed(req, ret);
7226 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007227 }
Hao Xua8295b92021-08-27 17:46:09 +08007228 req_fail_link_node(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007229 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007230
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007231 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007232 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7233 req->flags, true,
7234 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007235
Jens Axboe6c271ce2019-01-10 11:22:30 -07007236 /*
7237 * If we already have a head request, queue this one for async
7238 * submittal once the head completes. If we don't have a head but
7239 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7240 * submitted sync once the chain is complete. If none of those
7241 * conditions are true (normal request), then just queue it.
7242 */
7243 if (link->head) {
7244 struct io_kiocb *head = link->head;
7245
Hao Xua8295b92021-08-27 17:46:09 +08007246 if (!(req->flags & REQ_F_FAIL)) {
7247 ret = io_req_prep_async(req);
7248 if (unlikely(ret)) {
7249 req_fail_link_node(req, ret);
7250 if (!(head->flags & REQ_F_FAIL))
7251 req_fail_link_node(head, -ECANCELED);
7252 }
7253 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007254 trace_io_uring_link(ctx, req, head);
7255 link->last->link = req;
7256 link->last = req;
7257
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007258 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK))
7259 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007260 /* last request of a link, enqueue the link */
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007261 link->head = NULL;
7262 req = head;
7263 } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
7264 link->head = req;
7265 link->last = req;
7266 return 0;
Jackie Liu4fe2c962019-09-09 20:50:40 +08007267 }
7268
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007269 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007270 return 0;
7271}
7272
7273/*
7274 * Batched submission is done, ensure local IO is flushed out.
7275 */
Pavel Begunkov553deff2021-09-24 21:59:55 +01007276static void io_submit_state_end(struct io_ring_ctx *ctx)
Jens Axboe3529d8c2019-12-19 18:24:38 -07007277{
Pavel Begunkov553deff2021-09-24 21:59:55 +01007278 struct io_submit_state *state = &ctx->submit_state;
7279
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007280 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007281 io_queue_sqe(state->link.head);
Pavel Begunkov553deff2021-09-24 21:59:55 +01007282 /* flush only after queuing links as they can generate completions */
Pavel Begunkovc4501782021-09-08 16:40:52 +01007283 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007284 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007285 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007286}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007287
Jens Axboe9e645e112019-05-10 16:07:28 -06007288/*
7289 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007290 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007291static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007292 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007293{
7294 state->plug_started = false;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007295 state->need_plug = max_ios > 2;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -06007296 state->submit_nr = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007297 /* set only head, no need to init link_last in advance */
7298 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007299}
7300
Jens Axboe193155c2020-02-22 23:22:19 -07007301static void io_commit_sqring(struct io_ring_ctx *ctx)
7302{
Jens Axboe75c6a032020-01-28 10:15:23 -07007303 struct io_rings *rings = ctx->rings;
7304
7305 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007306 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007307 * since once we write the new head, the application could
7308 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007309 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007310 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007311}
7312
Jens Axboe9e645e112019-05-10 16:07:28 -06007313/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007314 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007315 * that is mapped by userspace. This means that care needs to be taken to
7316 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007317 * being a good citizen. If members of the sqe are validated and then later
7318 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007319 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007320 */
7321static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007322{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007323 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007324 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007325
7326 /*
7327 * The cached sq head (or cq tail) serves two purposes:
7328 *
7329 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007330 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007331 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007332 * though the application is the one updating it.
7333 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007334 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007335 if (likely(head < ctx->sq_entries))
7336 return &ctx->sq_sqes[head];
7337
7338 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007339 ctx->cq_extra--;
7340 WRITE_ONCE(ctx->rings->sq_dropped,
7341 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007342 return NULL;
7343}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007344
Jens Axboe0f212202020-09-13 13:09:39 -06007345static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007346 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007347{
Pavel Begunkov69629802021-09-24 22:00:01 +01007348 unsigned int entries = io_sqring_entries(ctx);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007349 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007350
Pavel Begunkov51d48da2021-10-04 20:02:47 +01007351 if (unlikely(!entries))
Pavel Begunkov69629802021-09-24 22:00:01 +01007352 return 0;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03007353 /* make sure SQ entry isn't read before tail */
Pavel Begunkov69629802021-09-24 22:00:01 +01007354 nr = min3(nr, ctx->sq_entries, entries);
Pavel Begunkov9a108672021-08-27 11:55:01 +01007355 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007356
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007357 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov69629802021-09-24 22:00:01 +01007358 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007359 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007360 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007361
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007362 if (unlikely(!io_alloc_req_refill(ctx))) {
Pavel Begunkov196be952019-11-07 01:41:06 +03007363 if (!submitted)
7364 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007365 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007366 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007367 req = io_alloc_req(ctx);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007368 sqe = io_get_sqe(ctx);
7369 if (unlikely(!sqe)) {
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01007370 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007371 break;
7372 }
Jens Axboed3656342019-12-18 09:50:26 -07007373 /* will complete beyond this point, count as submitted */
7374 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007375 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007376 break;
Pavel Begunkov69629802021-09-24 22:00:01 +01007377 } while (submitted < nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007378
Pavel Begunkov9466f432020-01-25 22:34:01 +03007379 if (unlikely(submitted != nr)) {
7380 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007381 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007382
Pavel Begunkov09899b12021-06-14 02:36:22 +01007383 current->io_uring->cached_refs += unused;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007384 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007385
Pavel Begunkov553deff2021-09-24 21:59:55 +01007386 io_submit_state_end(ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007387 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7388 io_commit_sqring(ctx);
7389
Jens Axboe6c271ce2019-01-10 11:22:30 -07007390 return submitted;
7391}
7392
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007393static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7394{
7395 return READ_ONCE(sqd->state);
7396}
7397
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007398static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7399{
7400 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007401 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007402 WRITE_ONCE(ctx->rings->sq_flags,
7403 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007404 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007405}
7406
7407static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7408{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007409 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007410 WRITE_ONCE(ctx->rings->sq_flags,
7411 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007412 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007413}
7414
Xiaoguang Wang08369242020-11-03 14:15:59 +08007415static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007416{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007417 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007418 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007419
Jens Axboec8d1ba52020-09-14 11:07:26 -06007420 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007421 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007422 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7423 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007424
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007425 if (!wq_list_empty(&ctx->iopoll_list) || to_submit) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007426 const struct cred *creds = NULL;
7427
7428 if (ctx->sq_creds != current_cred())
7429 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007430
Xiaoguang Wang08369242020-11-03 14:15:59 +08007431 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007432 if (!wq_list_empty(&ctx->iopoll_list))
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01007433 io_do_iopoll(ctx, true);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007434
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007435 /*
7436 * Don't submit if refs are dying, good for io_uring_register(),
7437 * but also it is relied upon by io_ring_exit_work()
7438 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007439 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7440 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007441 ret = io_submit_sqes(ctx, to_submit);
7442 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007443
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007444 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7445 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007446 if (creds)
7447 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007448 }
Jens Axboe90554202020-09-03 12:12:41 -06007449
Xiaoguang Wang08369242020-11-03 14:15:59 +08007450 return ret;
7451}
7452
Pavel Begunkovc0724812021-10-04 20:02:54 +01007453static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd)
Xiaoguang Wang08369242020-11-03 14:15:59 +08007454{
7455 struct io_ring_ctx *ctx;
7456 unsigned sq_thread_idle = 0;
7457
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007458 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7459 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007460 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007461}
7462
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007463static bool io_sqd_handle_event(struct io_sq_data *sqd)
7464{
7465 bool did_sig = false;
7466 struct ksignal ksig;
7467
7468 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7469 signal_pending(current)) {
7470 mutex_unlock(&sqd->lock);
7471 if (signal_pending(current))
7472 did_sig = get_signal(&ksig);
7473 cond_resched();
7474 mutex_lock(&sqd->lock);
7475 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007476 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7477}
7478
Jens Axboe6c271ce2019-01-10 11:22:30 -07007479static int io_sq_thread(void *data)
7480{
Jens Axboe69fb2132020-09-14 11:16:23 -06007481 struct io_sq_data *sqd = data;
7482 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007483 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007484 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007485 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007486
Pavel Begunkov696ee882021-04-01 09:55:04 +01007487 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007488 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007489
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007490 if (sqd->sq_cpu != -1)
7491 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7492 else
7493 set_cpus_allowed_ptr(current, cpu_online_mask);
7494 current->flags |= PF_NO_SETAFFINITY;
7495
Paul Moore5bd21822021-02-16 19:46:48 -05007496 audit_alloc_kernel(current);
7497
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007498 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007499 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007500 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007501
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007502 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7503 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007504 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007505 timeout = jiffies + sqd->sq_thread_idle;
7506 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007507
Jens Axboee95eee22020-09-08 09:11:32 -06007508 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007509 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007510 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007511
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007512 if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007513 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007514 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007515 if (io_run_task_work())
7516 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007517
Xiaoguang Wang08369242020-11-03 14:15:59 +08007518 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007519 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007520 if (sqt_spin)
7521 timeout = jiffies + sqd->sq_thread_idle;
7522 continue;
7523 }
7524
Xiaoguang Wang08369242020-11-03 14:15:59 +08007525 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007526 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007527 bool needs_sched = true;
7528
Hao Xu724cb4f2021-04-21 23:19:11 +08007529 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007530 io_ring_set_wakeup_flag(ctx);
7531
Hao Xu724cb4f2021-04-21 23:19:11 +08007532 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007533 !wq_list_empty(&ctx->iopoll_list)) {
Hao Xu724cb4f2021-04-21 23:19:11 +08007534 needs_sched = false;
7535 break;
7536 }
7537 if (io_sqring_entries(ctx)) {
7538 needs_sched = false;
7539 break;
7540 }
7541 }
7542
7543 if (needs_sched) {
7544 mutex_unlock(&sqd->lock);
7545 schedule();
7546 mutex_lock(&sqd->lock);
7547 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007548 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7549 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007550 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007551
7552 finish_wait(&sqd->wait, &wait);
7553 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007554 }
7555
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007556 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007557 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007558 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007559 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007560 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007561 mutex_unlock(&sqd->lock);
7562
Paul Moore5bd21822021-02-16 19:46:48 -05007563 audit_free(current);
7564
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007565 complete(&sqd->exited);
7566 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007567}
7568
Jens Axboebda52162019-09-24 13:47:15 -06007569struct io_wait_queue {
7570 struct wait_queue_entry wq;
7571 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007572 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007573 unsigned nr_timeouts;
7574};
7575
Pavel Begunkov6c503152021-01-04 20:36:36 +00007576static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007577{
7578 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007579 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007580
7581 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007582 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007583 * started waiting. For timeouts, we always want to return to userspace,
7584 * regardless of event count.
7585 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007586 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007587}
7588
7589static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7590 int wake_flags, void *key)
7591{
7592 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7593 wq);
7594
Pavel Begunkov6c503152021-01-04 20:36:36 +00007595 /*
7596 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7597 * the task, and the next invocation will do it.
7598 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007599 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007600 return autoremove_wake_function(curr, mode, wake_flags, key);
7601 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007602}
7603
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007604static int io_run_task_work_sig(void)
7605{
7606 if (io_run_task_work())
7607 return 1;
7608 if (!signal_pending(current))
7609 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007610 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007611 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007612 return -EINTR;
7613}
7614
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007615/* when returns >0, the caller should retry */
7616static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7617 struct io_wait_queue *iowq,
7618 signed long *timeout)
7619{
7620 int ret;
7621
7622 /* make sure we run task_work before checking for signals */
7623 ret = io_run_task_work_sig();
7624 if (ret || io_should_wake(iowq))
7625 return ret;
7626 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007627 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007628 return 1;
7629
7630 *timeout = schedule_timeout(*timeout);
7631 return !*timeout ? -ETIME : 1;
7632}
7633
Jens Axboe2b188cc2019-01-07 10:46:33 -07007634/*
7635 * Wait until events become available, if we don't already have some. The
7636 * application must reap them itself, as they reside on the shared cq ring.
7637 */
7638static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007639 const sigset_t __user *sig, size_t sigsz,
7640 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007641{
Pavel Begunkov902910992021-08-09 09:07:32 -06007642 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007643 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007644 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7645 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007646
Jens Axboeb41e9852020-02-17 09:52:41 -07007647 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007648 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007649 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007650 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007651 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007652 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007653 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007654
Xiaoguang Wang44df58d2021-09-14 22:38:52 +08007655 if (uts) {
7656 struct timespec64 ts;
7657
7658 if (get_timespec64(&ts, uts))
7659 return -EFAULT;
7660 timeout = timespec64_to_jiffies(&ts);
7661 }
7662
Jens Axboe2b188cc2019-01-07 10:46:33 -07007663 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007664#ifdef CONFIG_COMPAT
7665 if (in_compat_syscall())
7666 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007667 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007668 else
7669#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007670 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007671
Jens Axboe2b188cc2019-01-07 10:46:33 -07007672 if (ret)
7673 return ret;
7674 }
7675
Pavel Begunkov902910992021-08-09 09:07:32 -06007676 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7677 iowq.wq.private = current;
7678 INIT_LIST_HEAD(&iowq.wq.entry);
7679 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007680 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007681 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007682
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007683 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007684 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007685 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007686 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007687 ret = -EBUSY;
7688 break;
7689 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007690 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007691 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007692 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007693 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007694 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007695 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007696
Jens Axboeb7db41c2020-07-04 08:55:50 -06007697 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007698
Hristo Venev75b28af2019-08-26 17:23:46 +00007699 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007700}
7701
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007702static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007703{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007704 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007705
7706 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007707 kfree(table[i]);
7708 kfree(table);
7709}
7710
Pavel Begunkovc0724812021-10-04 20:02:54 +01007711static __cold void **io_alloc_page_table(size_t size)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007712{
7713 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7714 size_t init_size = size;
7715 void **table;
7716
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007717 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007718 if (!table)
7719 return NULL;
7720
7721 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007722 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007723
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007724 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007725 if (!table[i]) {
7726 io_free_page_table(table, init_size);
7727 return NULL;
7728 }
7729 size -= this_size;
7730 }
7731 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007732}
7733
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007734static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7735{
7736 percpu_ref_exit(&ref_node->refs);
7737 kfree(ref_node);
7738}
7739
Pavel Begunkovc0724812021-10-04 20:02:54 +01007740static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007741{
7742 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7743 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7744 unsigned long flags;
7745 bool first_add = false;
7746
7747 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7748 node->done = true;
7749
7750 while (!list_empty(&ctx->rsrc_ref_list)) {
7751 node = list_first_entry(&ctx->rsrc_ref_list,
7752 struct io_rsrc_node, node);
7753 /* recycle ref nodes in order */
7754 if (!node->done)
7755 break;
7756 list_del(&node->node);
7757 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7758 }
7759 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7760
7761 if (first_add)
7762 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7763}
7764
7765static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7766{
7767 struct io_rsrc_node *ref_node;
7768
7769 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7770 if (!ref_node)
7771 return NULL;
7772
7773 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7774 0, GFP_KERNEL)) {
7775 kfree(ref_node);
7776 return NULL;
7777 }
7778 INIT_LIST_HEAD(&ref_node->node);
7779 INIT_LIST_HEAD(&ref_node->rsrc_list);
7780 ref_node->done = false;
7781 return ref_node;
7782}
7783
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007784static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7785 struct io_rsrc_data *data_to_kill)
Pavel Begunkovab409402021-10-09 23:14:41 +01007786 __must_hold(&ctx->uring_lock)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007787{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007788 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7789 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007790
Pavel Begunkovab409402021-10-09 23:14:41 +01007791 io_rsrc_refs_drop(ctx);
7792
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007793 if (data_to_kill) {
7794 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007795
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007796 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007797 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007798 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007799 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007800
Pavel Begunkov3e942492021-04-11 01:46:34 +01007801 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007802 percpu_ref_kill(&rsrc_node->refs);
7803 ctx->rsrc_node = NULL;
7804 }
7805
7806 if (!ctx->rsrc_node) {
7807 ctx->rsrc_node = ctx->rsrc_backup_node;
7808 ctx->rsrc_backup_node = NULL;
7809 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007810}
7811
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007812static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007813{
7814 if (ctx->rsrc_backup_node)
7815 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007816 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007817 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7818}
7819
Pavel Begunkovc0724812021-10-04 20:02:54 +01007820static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
7821 struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007822{
7823 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007824
Pavel Begunkov215c3902021-04-01 15:43:48 +01007825 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007826 if (data->quiesce)
7827 return -ENXIO;
7828
7829 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007830 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007831 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007832 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007833 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007834 io_rsrc_node_switch(ctx, data);
7835
Pavel Begunkov3e942492021-04-11 01:46:34 +01007836 /* kill initial ref, already quiesced if zero */
7837 if (atomic_dec_and_test(&data->refs))
7838 break;
Jens Axboec018db42021-08-09 08:15:50 -06007839 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007840 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007841 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007842 if (!ret) {
7843 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007844 break;
Jens Axboec018db42021-08-09 08:15:50 -06007845 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007846
Pavel Begunkov3e942492021-04-11 01:46:34 +01007847 atomic_inc(&data->refs);
7848 /* wait for all works potentially completing data->done */
7849 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007850 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007851
Hao Xu8bad28d2021-02-19 17:19:36 +08007852 ret = io_run_task_work_sig();
7853 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007854 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007855 data->quiesce = false;
7856
Hao Xu8bad28d2021-02-19 17:19:36 +08007857 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007858}
7859
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007860static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7861{
7862 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7863 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7864
7865 return &data->tags[table_idx][off];
7866}
7867
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007868static void io_rsrc_data_free(struct io_rsrc_data *data)
7869{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007870 size_t size = data->nr * sizeof(data->tags[0][0]);
7871
7872 if (data->tags)
7873 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007874 kfree(data);
7875}
7876
Pavel Begunkovc0724812021-10-04 20:02:54 +01007877static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7878 u64 __user *utags, unsigned nr,
7879 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007880{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007881 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007882 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007883 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007884
7885 data = kzalloc(sizeof(*data), GFP_KERNEL);
7886 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007887 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007888 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007889 if (!data->tags) {
7890 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007891 return -ENOMEM;
7892 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007893
7894 data->nr = nr;
7895 data->ctx = ctx;
7896 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007897 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007898 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007899 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007900 u64 *tag_slot = io_get_tag_slot(data, i);
7901
7902 if (copy_from_user(tag_slot, &utags[i],
7903 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007904 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007905 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007906 }
7907
Pavel Begunkov3e942492021-04-11 01:46:34 +01007908 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007909 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007910 *pdata = data;
7911 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007912fail:
7913 io_rsrc_data_free(data);
7914 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007915}
7916
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007917static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7918{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007919 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7920 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007921 return !!table->files;
7922}
7923
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007924static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007925{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007926 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007927 table->files = NULL;
7928}
7929
Jens Axboe2b188cc2019-01-07 10:46:33 -07007930static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7931{
7932#if defined(CONFIG_UNIX)
7933 if (ctx->ring_sock) {
7934 struct sock *sock = ctx->ring_sock->sk;
7935 struct sk_buff *skb;
7936
7937 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7938 kfree_skb(skb);
7939 }
7940#else
7941 int i;
7942
7943 for (i = 0; i < ctx->nr_user_files; i++) {
7944 struct file *file;
7945
7946 file = io_file_from_index(ctx, i);
7947 if (file)
7948 fput(file);
7949 }
7950#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007951 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007952 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007953 ctx->file_data = NULL;
7954 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007955}
7956
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007957static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7958{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007959 int ret;
7960
Pavel Begunkov08480402021-04-13 02:58:38 +01007961 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007962 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007963 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7964 if (!ret)
7965 __io_sqe_files_unregister(ctx);
7966 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007967}
7968
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007969static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007970 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007971{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007972 WARN_ON_ONCE(sqd->thread == current);
7973
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007974 /*
7975 * Do the dance but not conditional clear_bit() because it'd race with
7976 * other threads incrementing park_pending and setting the bit.
7977 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007978 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007979 if (atomic_dec_return(&sqd->park_pending))
7980 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007981 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007982}
7983
Jens Axboe86e0d672021-03-05 08:44:39 -07007984static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007985 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007986{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007987 WARN_ON_ONCE(sqd->thread == current);
7988
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007989 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007990 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007991 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007992 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007993 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007994}
7995
7996static void io_sq_thread_stop(struct io_sq_data *sqd)
7997{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007998 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007999 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008000
Jens Axboe05962f92021-03-06 13:58:48 -07008001 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01008002 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07008003 if (sqd->thread)
8004 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008005 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07008006 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008007}
8008
Jens Axboe534ca6d2020-09-02 13:52:19 -06008009static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07008010{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008011 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008012 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
8013
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008014 io_sq_thread_stop(sqd);
8015 kfree(sqd);
8016 }
8017}
8018
8019static void io_sq_thread_finish(struct io_ring_ctx *ctx)
8020{
8021 struct io_sq_data *sqd = ctx->sq_data;
8022
8023 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07008024 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008025 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008026 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07008027 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008028
8029 io_put_sq_data(sqd);
8030 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008031 }
8032}
8033
Jens Axboeaa061652020-09-02 14:50:27 -06008034static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
8035{
8036 struct io_ring_ctx *ctx_attach;
8037 struct io_sq_data *sqd;
8038 struct fd f;
8039
8040 f = fdget(p->wq_fd);
8041 if (!f.file)
8042 return ERR_PTR(-ENXIO);
8043 if (f.file->f_op != &io_uring_fops) {
8044 fdput(f);
8045 return ERR_PTR(-EINVAL);
8046 }
8047
8048 ctx_attach = f.file->private_data;
8049 sqd = ctx_attach->sq_data;
8050 if (!sqd) {
8051 fdput(f);
8052 return ERR_PTR(-EINVAL);
8053 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07008054 if (sqd->task_tgid != current->tgid) {
8055 fdput(f);
8056 return ERR_PTR(-EPERM);
8057 }
Jens Axboeaa061652020-09-02 14:50:27 -06008058
8059 refcount_inc(&sqd->refs);
8060 fdput(f);
8061 return sqd;
8062}
8063
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008064static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
8065 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008066{
8067 struct io_sq_data *sqd;
8068
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008069 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008070 if (p->flags & IORING_SETUP_ATTACH_WQ) {
8071 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008072 if (!IS_ERR(sqd)) {
8073 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008074 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008075 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07008076 /* fall through for EPERM case, setup new sqd/task */
8077 if (PTR_ERR(sqd) != -EPERM)
8078 return sqd;
8079 }
Jens Axboeaa061652020-09-02 14:50:27 -06008080
Jens Axboe534ca6d2020-09-02 13:52:19 -06008081 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
8082 if (!sqd)
8083 return ERR_PTR(-ENOMEM);
8084
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008085 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008086 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06008087 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008088 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008089 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008090 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008091 return sqd;
8092}
8093
Jens Axboe6b063142019-01-10 22:13:58 -07008094#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07008095/*
8096 * Ensure the UNIX gc is aware of our file set, so we are certain that
8097 * the io_uring can be safely unregistered on process exit, even if we have
8098 * loops in the file referencing.
8099 */
8100static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
8101{
8102 struct sock *sk = ctx->ring_sock->sk;
8103 struct scm_fp_list *fpl;
8104 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06008105 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07008106
Jens Axboe6b063142019-01-10 22:13:58 -07008107 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
8108 if (!fpl)
8109 return -ENOMEM;
8110
8111 skb = alloc_skb(0, GFP_KERNEL);
8112 if (!skb) {
8113 kfree(fpl);
8114 return -ENOMEM;
8115 }
8116
8117 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07008118
Jens Axboe08a45172019-10-03 08:11:03 -06008119 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07008120 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07008121 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008122 struct file *file = io_file_from_index(ctx, i + offset);
8123
8124 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06008125 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06008126 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06008127 unix_inflight(fpl->user, fpl->fp[nr_files]);
8128 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07008129 }
8130
Jens Axboe08a45172019-10-03 08:11:03 -06008131 if (nr_files) {
8132 fpl->max = SCM_MAX_FD;
8133 fpl->count = nr_files;
8134 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008135 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06008136 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
8137 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07008138
Jens Axboe08a45172019-10-03 08:11:03 -06008139 for (i = 0; i < nr_files; i++)
8140 fput(fpl->fp[i]);
8141 } else {
8142 kfree_skb(skb);
8143 kfree(fpl);
8144 }
Jens Axboe6b063142019-01-10 22:13:58 -07008145
8146 return 0;
8147}
8148
8149/*
8150 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8151 * causes regular reference counting to break down. We rely on the UNIX
8152 * garbage collection to take care of this problem for us.
8153 */
8154static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8155{
8156 unsigned left, total;
8157 int ret = 0;
8158
8159 total = 0;
8160 left = ctx->nr_user_files;
8161 while (left) {
8162 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008163
8164 ret = __io_sqe_files_scm(ctx, this_files, total);
8165 if (ret)
8166 break;
8167 left -= this_files;
8168 total += this_files;
8169 }
8170
8171 if (!ret)
8172 return 0;
8173
8174 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008175 struct file *file = io_file_from_index(ctx, total);
8176
8177 if (file)
8178 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008179 total++;
8180 }
8181
8182 return ret;
8183}
8184#else
8185static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8186{
8187 return 0;
8188}
8189#endif
8190
Pavel Begunkov47e90392021-04-01 15:43:56 +01008191static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008192{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008193 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008194#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008195 struct sock *sock = ctx->ring_sock->sk;
8196 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8197 struct sk_buff *skb;
8198 int i;
8199
8200 __skb_queue_head_init(&list);
8201
8202 /*
8203 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8204 * remove this entry and rearrange the file array.
8205 */
8206 skb = skb_dequeue(head);
8207 while (skb) {
8208 struct scm_fp_list *fp;
8209
8210 fp = UNIXCB(skb).fp;
8211 for (i = 0; i < fp->count; i++) {
8212 int left;
8213
8214 if (fp->fp[i] != file)
8215 continue;
8216
8217 unix_notinflight(fp->user, fp->fp[i]);
8218 left = fp->count - 1 - i;
8219 if (left) {
8220 memmove(&fp->fp[i], &fp->fp[i + 1],
8221 left * sizeof(struct file *));
8222 }
8223 fp->count--;
8224 if (!fp->count) {
8225 kfree_skb(skb);
8226 skb = NULL;
8227 } else {
8228 __skb_queue_tail(&list, skb);
8229 }
8230 fput(file);
8231 file = NULL;
8232 break;
8233 }
8234
8235 if (!file)
8236 break;
8237
8238 __skb_queue_tail(&list, skb);
8239
8240 skb = skb_dequeue(head);
8241 }
8242
8243 if (skb_peek(&list)) {
8244 spin_lock_irq(&head->lock);
8245 while ((skb = __skb_dequeue(&list)) != NULL)
8246 __skb_queue_tail(head, skb);
8247 spin_unlock_irq(&head->lock);
8248 }
8249#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008250 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008251#endif
8252}
8253
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008254static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008255{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008256 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008257 struct io_ring_ctx *ctx = rsrc_data->ctx;
8258 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008259
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008260 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8261 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008262
8263 if (prsrc->tag) {
8264 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008265
8266 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008267 spin_lock(&ctx->completion_lock);
Pavel Begunkov913a5712021-11-10 15:49:31 +00008268 io_fill_cqe_aux(ctx, prsrc->tag, 0, 0);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008269 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008270 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008271 io_cqring_ev_posted(ctx);
8272 io_ring_submit_unlock(ctx, lock_ring);
8273 }
8274
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008275 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008276 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008277 }
8278
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008279 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008280 if (atomic_dec_and_test(&rsrc_data->refs))
8281 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008282}
8283
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008284static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008285{
8286 struct io_ring_ctx *ctx;
8287 struct llist_node *node;
8288
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008289 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8290 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008291
8292 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008293 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008294 struct llist_node *next = node->next;
8295
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008296 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008297 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008298 node = next;
8299 }
8300}
8301
Jens Axboe05f3fb32019-12-09 11:22:50 -07008302static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008303 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008304{
8305 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008306 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008307 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008308 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008309
8310 if (ctx->file_data)
8311 return -EBUSY;
8312 if (!nr_args)
8313 return -EINVAL;
8314 if (nr_args > IORING_MAX_FIXED_FILES)
8315 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008316 if (nr_args > rlimit(RLIMIT_NOFILE))
8317 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008318 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008319 if (ret)
8320 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008321 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8322 &ctx->file_data);
8323 if (ret)
8324 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008325
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008326 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008327 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008328 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008329
Jens Axboe05f3fb32019-12-09 11:22:50 -07008330 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008331 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008332 ret = -EFAULT;
8333 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008334 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008335 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008336 if (fd == -1) {
8337 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008338 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008339 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008340 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008341 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008342
Jens Axboe05f3fb32019-12-09 11:22:50 -07008343 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008344 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008345 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008346 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008347
8348 /*
8349 * Don't allow io_uring instances to be registered. If UNIX
8350 * isn't enabled, then this causes a reference cycle and this
8351 * instance can never get freed. If UNIX is enabled we'll
8352 * handle it just fine, but there's still no point in allowing
8353 * a ring fd as it doesn't support regular read/write anyway.
8354 */
8355 if (file->f_op == &io_uring_fops) {
8356 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008357 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008358 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008359 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008360 }
8361
Jens Axboe05f3fb32019-12-09 11:22:50 -07008362 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008363 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008364 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008365 return ret;
8366 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008367
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008368 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008369 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008370out_fput:
8371 for (i = 0; i < ctx->nr_user_files; i++) {
8372 file = io_file_from_index(ctx, i);
8373 if (file)
8374 fput(file);
8375 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008376 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008377 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008378out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008379 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008380 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008381 return ret;
8382}
8383
Jens Axboec3a31e62019-10-03 13:59:56 -06008384static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8385 int index)
8386{
8387#if defined(CONFIG_UNIX)
8388 struct sock *sock = ctx->ring_sock->sk;
8389 struct sk_buff_head *head = &sock->sk_receive_queue;
8390 struct sk_buff *skb;
8391
8392 /*
8393 * See if we can merge this file into an existing skb SCM_RIGHTS
8394 * file set. If there's no room, fall back to allocating a new skb
8395 * and filling it in.
8396 */
8397 spin_lock_irq(&head->lock);
8398 skb = skb_peek(head);
8399 if (skb) {
8400 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8401
8402 if (fpl->count < SCM_MAX_FD) {
8403 __skb_unlink(skb, head);
8404 spin_unlock_irq(&head->lock);
8405 fpl->fp[fpl->count] = get_file(file);
8406 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8407 fpl->count++;
8408 spin_lock_irq(&head->lock);
8409 __skb_queue_head(head, skb);
8410 } else {
8411 skb = NULL;
8412 }
8413 }
8414 spin_unlock_irq(&head->lock);
8415
8416 if (skb) {
8417 fput(file);
8418 return 0;
8419 }
8420
8421 return __io_sqe_files_scm(ctx, 1, index);
8422#else
8423 return 0;
8424#endif
8425}
8426
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008427static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
8428 struct io_rsrc_node *node, void *rsrc)
8429{
8430 struct io_rsrc_put *prsrc;
8431
8432 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8433 if (!prsrc)
8434 return -ENOMEM;
8435
8436 prsrc->tag = *io_get_tag_slot(data, idx);
8437 prsrc->rsrc = rsrc;
8438 list_add(&prsrc->list, &node->rsrc_list);
8439 return 0;
8440}
8441
Pavel Begunkovb9445592021-08-25 12:25:45 +01008442static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8443 unsigned int issue_flags, u32 slot_index)
8444{
8445 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08008446 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008447 bool needs_switch = false;
Pavel Begunkovb9445592021-08-25 12:25:45 +01008448 struct io_fixed_file *file_slot;
8449 int ret = -EBADF;
8450
Hao Xu3b44b372021-10-18 21:34:31 +08008451 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008452 if (file->f_op == &io_uring_fops)
8453 goto err;
8454 ret = -ENXIO;
8455 if (!ctx->file_data)
8456 goto err;
8457 ret = -EINVAL;
8458 if (slot_index >= ctx->nr_user_files)
8459 goto err;
8460
8461 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8462 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008463
8464 if (file_slot->file_ptr) {
8465 struct file *old_file;
8466
8467 ret = io_rsrc_node_switch_start(ctx);
8468 if (ret)
8469 goto err;
8470
8471 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8472 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
8473 ctx->rsrc_node, old_file);
8474 if (ret)
8475 goto err;
8476 file_slot->file_ptr = 0;
8477 needs_switch = true;
8478 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01008479
8480 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8481 io_fixed_file_set(file_slot, file);
8482 ret = io_sqe_file_register(ctx, file, slot_index);
8483 if (ret) {
8484 file_slot->file_ptr = 0;
8485 goto err;
8486 }
8487
8488 ret = 0;
8489err:
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008490 if (needs_switch)
8491 io_rsrc_node_switch(ctx, ctx->file_data);
Hao Xu3b44b372021-10-18 21:34:31 +08008492 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008493 if (ret)
8494 fput(file);
8495 return ret;
8496}
8497
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008498static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
8499{
8500 unsigned int offset = req->close.file_slot - 1;
8501 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08008502 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008503 struct io_fixed_file *file_slot;
8504 struct file *file;
8505 int ret, i;
8506
Hao Xu3b44b372021-10-18 21:34:31 +08008507 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008508 ret = -ENXIO;
8509 if (unlikely(!ctx->file_data))
8510 goto out;
8511 ret = -EINVAL;
8512 if (offset >= ctx->nr_user_files)
8513 goto out;
8514 ret = io_rsrc_node_switch_start(ctx);
8515 if (ret)
8516 goto out;
8517
8518 i = array_index_nospec(offset, ctx->nr_user_files);
8519 file_slot = io_fixed_file_slot(&ctx->file_table, i);
8520 ret = -EBADF;
8521 if (!file_slot->file_ptr)
8522 goto out;
8523
8524 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8525 ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
8526 if (ret)
8527 goto out;
8528
8529 file_slot->file_ptr = 0;
8530 io_rsrc_node_switch(ctx, ctx->file_data);
8531 ret = 0;
8532out:
Hao Xu3b44b372021-10-18 21:34:31 +08008533 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008534 return ret;
8535}
8536
Jens Axboe05f3fb32019-12-09 11:22:50 -07008537static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008538 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008539 unsigned nr_args)
8540{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008541 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008542 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008543 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008544 struct io_fixed_file *file_slot;
8545 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008546 int fd, i, err = 0;
8547 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008548 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008549
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008550 if (!ctx->file_data)
8551 return -ENXIO;
8552 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008553 return -EINVAL;
8554
Pavel Begunkov67973b92021-01-26 13:51:09 +00008555 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008556 u64 tag = 0;
8557
8558 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8559 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008560 err = -EFAULT;
8561 break;
8562 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008563 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8564 err = -EINVAL;
8565 break;
8566 }
noah4e0377a2021-01-26 15:23:28 -05008567 if (fd == IORING_REGISTER_FILES_SKIP)
8568 continue;
8569
Pavel Begunkov67973b92021-01-26 13:51:09 +00008570 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008571 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008572
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008573 if (file_slot->file_ptr) {
8574 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008575 err = io_queue_rsrc_removal(data, up->offset + done,
8576 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008577 if (err)
8578 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008579 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008580 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008581 }
8582 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008583 file = fget(fd);
8584 if (!file) {
8585 err = -EBADF;
8586 break;
8587 }
8588 /*
8589 * Don't allow io_uring instances to be registered. If
8590 * UNIX isn't enabled, then this causes a reference
8591 * cycle and this instance can never get freed. If UNIX
8592 * is enabled we'll handle it just fine, but there's
8593 * still no point in allowing a ring fd as it doesn't
8594 * support regular read/write anyway.
8595 */
8596 if (file->f_op == &io_uring_fops) {
8597 fput(file);
8598 err = -EBADF;
8599 break;
8600 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008601 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008602 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008603 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008604 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008605 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008606 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008607 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008608 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008609 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008610 }
8611
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008612 if (needs_switch)
8613 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008614 return done ? done : err;
8615}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008616
Jens Axboe685fe7f2021-03-08 09:37:51 -07008617static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8618 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008619{
Jens Axboee9418942021-02-19 12:33:30 -07008620 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008621 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008622 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008623
Yang Yingliang362a9e62021-07-20 16:38:05 +08008624 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008625 hash = ctx->hash_map;
8626 if (!hash) {
8627 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008628 if (!hash) {
8629 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008630 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008631 }
Jens Axboee9418942021-02-19 12:33:30 -07008632 refcount_set(&hash->refs, 1);
8633 init_waitqueue_head(&hash->wait);
8634 ctx->hash_map = hash;
8635 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008636 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008637
8638 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008639 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008640 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008641 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008642
Jens Axboed25e3a32021-02-16 11:41:41 -07008643 /* Do QD, or 4 * CPUS, whatever is smallest */
8644 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008645
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008646 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008647}
8648
Pavel Begunkovc0724812021-10-04 20:02:54 +01008649static __cold int io_uring_alloc_task_context(struct task_struct *task,
8650 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008651{
8652 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008653 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008654
Pavel Begunkov09899b12021-06-14 02:36:22 +01008655 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008656 if (unlikely(!tctx))
8657 return -ENOMEM;
8658
Jens Axboed8a6df12020-10-15 16:24:45 -06008659 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8660 if (unlikely(ret)) {
8661 kfree(tctx);
8662 return ret;
8663 }
8664
Jens Axboe685fe7f2021-03-08 09:37:51 -07008665 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008666 if (IS_ERR(tctx->io_wq)) {
8667 ret = PTR_ERR(tctx->io_wq);
8668 percpu_counter_destroy(&tctx->inflight);
8669 kfree(tctx);
8670 return ret;
8671 }
8672
Jens Axboe0f212202020-09-13 13:09:39 -06008673 xa_init(&tctx->xa);
8674 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008675 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008676 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008677 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008678 spin_lock_init(&tctx->task_lock);
8679 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008680 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008681 return 0;
8682}
8683
8684void __io_uring_free(struct task_struct *tsk)
8685{
8686 struct io_uring_task *tctx = tsk->io_uring;
8687
8688 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008689 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008690 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008691
Jens Axboed8a6df12020-10-15 16:24:45 -06008692 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008693 kfree(tctx);
8694 tsk->io_uring = NULL;
8695}
8696
Pavel Begunkovc0724812021-10-04 20:02:54 +01008697static __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
8698 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008699{
8700 int ret;
8701
Jens Axboed25e3a32021-02-16 11:41:41 -07008702 /* Retain compatibility with failing for an invalid attach attempt */
8703 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8704 IORING_SETUP_ATTACH_WQ) {
8705 struct fd f;
8706
8707 f = fdget(p->wq_fd);
8708 if (!f.file)
8709 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008710 if (f.file->f_op != &io_uring_fops) {
8711 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008712 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008713 }
8714 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008715 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008716 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008717 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008718 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008719 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008720
Paul Moorecdc14042021-02-01 19:56:49 -05008721 ret = security_uring_sqpoll();
8722 if (ret)
8723 return ret;
8724
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008725 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008726 if (IS_ERR(sqd)) {
8727 ret = PTR_ERR(sqd);
8728 goto err;
8729 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008730
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008731 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008732 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008733 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8734 if (!ctx->sq_thread_idle)
8735 ctx->sq_thread_idle = HZ;
8736
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008737 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008738 list_add(&ctx->sqd_list, &sqd->ctx_list);
8739 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008740 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008741 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008742 io_sq_thread_unpark(sqd);
8743
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008744 if (ret < 0)
8745 goto err;
8746 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008747 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008748
Jens Axboe6c271ce2019-01-10 11:22:30 -07008749 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008750 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008751
Jens Axboe917257d2019-04-13 09:28:55 -06008752 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008753 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008754 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008755 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008756 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008757 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008758 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008759
8760 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008761 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008762 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8763 if (IS_ERR(tsk)) {
8764 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008765 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008766 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008767
Jens Axboe46fe18b2021-03-04 12:39:36 -07008768 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008769 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008770 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008771 if (ret)
8772 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008773 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8774 /* Can't have SQ_AFF without SQPOLL */
8775 ret = -EINVAL;
8776 goto err;
8777 }
8778
Jens Axboe2b188cc2019-01-07 10:46:33 -07008779 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008780err_sqpoll:
8781 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008782err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008783 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008784 return ret;
8785}
8786
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008787static inline void __io_unaccount_mem(struct user_struct *user,
8788 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008789{
8790 atomic_long_sub(nr_pages, &user->locked_vm);
8791}
8792
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008793static inline int __io_account_mem(struct user_struct *user,
8794 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008795{
8796 unsigned long page_limit, cur_pages, new_pages;
8797
8798 /* Don't allow more pages than we can safely lock */
8799 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8800
8801 do {
8802 cur_pages = atomic_long_read(&user->locked_vm);
8803 new_pages = cur_pages + nr_pages;
8804 if (new_pages > page_limit)
8805 return -ENOMEM;
8806 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8807 new_pages) != cur_pages);
8808
8809 return 0;
8810}
8811
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008812static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008813{
Jens Axboe62e398b2021-02-21 16:19:37 -07008814 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008815 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008816
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008817 if (ctx->mm_account)
8818 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008819}
8820
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008821static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008822{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008823 int ret;
8824
Jens Axboe62e398b2021-02-21 16:19:37 -07008825 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008826 ret = __io_account_mem(ctx->user, nr_pages);
8827 if (ret)
8828 return ret;
8829 }
8830
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008831 if (ctx->mm_account)
8832 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008833
8834 return 0;
8835}
8836
Jens Axboe2b188cc2019-01-07 10:46:33 -07008837static void io_mem_free(void *ptr)
8838{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008839 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008840
Mark Rutland52e04ef2019-04-30 17:30:21 +01008841 if (!ptr)
8842 return;
8843
8844 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008845 if (put_page_testzero(page))
8846 free_compound_page(page);
8847}
8848
8849static void *io_mem_alloc(size_t size)
8850{
8851 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008852 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008853
8854 return (void *) __get_free_pages(gfp_flags, get_order(size));
8855}
8856
Hristo Venev75b28af2019-08-26 17:23:46 +00008857static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8858 size_t *sq_offset)
8859{
8860 struct io_rings *rings;
8861 size_t off, sq_array_size;
8862
8863 off = struct_size(rings, cqes, cq_entries);
8864 if (off == SIZE_MAX)
8865 return SIZE_MAX;
8866
8867#ifdef CONFIG_SMP
8868 off = ALIGN(off, SMP_CACHE_BYTES);
8869 if (off == 0)
8870 return SIZE_MAX;
8871#endif
8872
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008873 if (sq_offset)
8874 *sq_offset = off;
8875
Hristo Venev75b28af2019-08-26 17:23:46 +00008876 sq_array_size = array_size(sizeof(u32), sq_entries);
8877 if (sq_array_size == SIZE_MAX)
8878 return SIZE_MAX;
8879
8880 if (check_add_overflow(off, sq_array_size, &off))
8881 return SIZE_MAX;
8882
Hristo Venev75b28af2019-08-26 17:23:46 +00008883 return off;
8884}
8885
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008886static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008887{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008888 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008889 unsigned int i;
8890
Pavel Begunkov62248432021-04-28 13:11:29 +01008891 if (imu != ctx->dummy_ubuf) {
8892 for (i = 0; i < imu->nr_bvecs; i++)
8893 unpin_user_page(imu->bvec[i].bv_page);
8894 if (imu->acct_pages)
8895 io_unaccount_mem(ctx, imu->acct_pages);
8896 kvfree(imu);
8897 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008898 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008899}
8900
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008901static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8902{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008903 io_buffer_unmap(ctx, &prsrc->buf);
8904 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008905}
8906
8907static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008908{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008909 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008910
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008911 for (i = 0; i < ctx->nr_user_bufs; i++)
8912 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008913 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008914 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008915 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008916 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008917 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008918}
8919
Jens Axboeedafcce2019-01-09 09:16:05 -07008920static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8921{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008922 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008923
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008924 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008925 return -ENXIO;
8926
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008927 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8928 if (!ret)
8929 __io_sqe_buffers_unregister(ctx);
8930 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008931}
8932
8933static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8934 void __user *arg, unsigned index)
8935{
8936 struct iovec __user *src;
8937
8938#ifdef CONFIG_COMPAT
8939 if (ctx->compat) {
8940 struct compat_iovec __user *ciovs;
8941 struct compat_iovec ciov;
8942
8943 ciovs = (struct compat_iovec __user *) arg;
8944 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8945 return -EFAULT;
8946
Jens Axboed55e5f52019-12-11 16:12:15 -07008947 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008948 dst->iov_len = ciov.iov_len;
8949 return 0;
8950 }
8951#endif
8952 src = (struct iovec __user *) arg;
8953 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8954 return -EFAULT;
8955 return 0;
8956}
8957
Jens Axboede293932020-09-17 16:19:16 -06008958/*
8959 * Not super efficient, but this is just a registration time. And we do cache
8960 * the last compound head, so generally we'll only do a full search if we don't
8961 * match that one.
8962 *
8963 * We check if the given compound head page has already been accounted, to
8964 * avoid double accounting it. This allows us to account the full size of the
8965 * page, not just the constituent pages of a huge page.
8966 */
8967static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8968 int nr_pages, struct page *hpage)
8969{
8970 int i, j;
8971
8972 /* check current page array */
8973 for (i = 0; i < nr_pages; i++) {
8974 if (!PageCompound(pages[i]))
8975 continue;
8976 if (compound_head(pages[i]) == hpage)
8977 return true;
8978 }
8979
8980 /* check previously registered pages */
8981 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008982 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008983
8984 for (j = 0; j < imu->nr_bvecs; j++) {
8985 if (!PageCompound(imu->bvec[j].bv_page))
8986 continue;
8987 if (compound_head(imu->bvec[j].bv_page) == hpage)
8988 return true;
8989 }
8990 }
8991
8992 return false;
8993}
8994
8995static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8996 int nr_pages, struct io_mapped_ubuf *imu,
8997 struct page **last_hpage)
8998{
8999 int i, ret;
9000
Pavel Begunkov216e5832021-05-29 12:01:02 +01009001 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06009002 for (i = 0; i < nr_pages; i++) {
9003 if (!PageCompound(pages[i])) {
9004 imu->acct_pages++;
9005 } else {
9006 struct page *hpage;
9007
9008 hpage = compound_head(pages[i]);
9009 if (hpage == *last_hpage)
9010 continue;
9011 *last_hpage = hpage;
9012 if (headpage_already_acct(ctx, pages, i, hpage))
9013 continue;
9014 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
9015 }
9016 }
9017
9018 if (!imu->acct_pages)
9019 return 0;
9020
Jens Axboe26bfa89e2021-02-09 20:14:12 -07009021 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06009022 if (ret)
9023 imu->acct_pages = 0;
9024 return ret;
9025}
9026
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009027static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009028 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009029 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07009030{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009031 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07009032 struct vm_area_struct **vmas = NULL;
9033 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009034 unsigned long off, start, end, ubuf;
9035 size_t size;
9036 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07009037
Pavel Begunkov62248432021-04-28 13:11:29 +01009038 if (!iov->iov_base) {
9039 *pimu = ctx->dummy_ubuf;
9040 return 0;
9041 }
9042
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009043 ubuf = (unsigned long) iov->iov_base;
9044 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
9045 start = ubuf >> PAGE_SHIFT;
9046 nr_pages = end - start;
9047
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009048 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009049 ret = -ENOMEM;
9050
9051 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
9052 if (!pages)
9053 goto done;
9054
9055 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
9056 GFP_KERNEL);
9057 if (!vmas)
9058 goto done;
9059
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009060 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01009061 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009062 goto done;
9063
9064 ret = 0;
9065 mmap_read_lock(current->mm);
9066 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
9067 pages, vmas);
9068 if (pret == nr_pages) {
9069 /* don't support file backed memory */
9070 for (i = 0; i < nr_pages; i++) {
9071 struct vm_area_struct *vma = vmas[i];
9072
Pavel Begunkov40dad762021-06-09 15:26:54 +01009073 if (vma_is_shmem(vma))
9074 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009075 if (vma->vm_file &&
9076 !is_file_hugepages(vma->vm_file)) {
9077 ret = -EOPNOTSUPP;
9078 break;
9079 }
9080 }
9081 } else {
9082 ret = pret < 0 ? pret : -EFAULT;
9083 }
9084 mmap_read_unlock(current->mm);
9085 if (ret) {
9086 /*
9087 * if we did partial map, or found file backed vmas,
9088 * release any pages we did get
9089 */
9090 if (pret > 0)
9091 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009092 goto done;
9093 }
9094
9095 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
9096 if (ret) {
9097 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009098 goto done;
9099 }
9100
9101 off = ubuf & ~PAGE_MASK;
9102 size = iov->iov_len;
9103 for (i = 0; i < nr_pages; i++) {
9104 size_t vec_len;
9105
9106 vec_len = min_t(size_t, size, PAGE_SIZE - off);
9107 imu->bvec[i].bv_page = pages[i];
9108 imu->bvec[i].bv_len = vec_len;
9109 imu->bvec[i].bv_offset = off;
9110 off = 0;
9111 size -= vec_len;
9112 }
9113 /* store original address for later verification */
9114 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01009115 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009116 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009117 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009118 ret = 0;
9119done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009120 if (ret)
9121 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009122 kvfree(pages);
9123 kvfree(vmas);
9124 return ret;
9125}
9126
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009127static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009128{
Pavel Begunkov87094462021-04-11 01:46:36 +01009129 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
9130 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009131}
9132
9133static int io_buffer_validate(struct iovec *iov)
9134{
Pavel Begunkov50e96982021-03-24 22:59:01 +00009135 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
9136
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009137 /*
9138 * Don't impose further limits on the size and buffer
9139 * constraints here, we'll -EINVAL later when IO is
9140 * submitted if they are wrong.
9141 */
Pavel Begunkov62248432021-04-28 13:11:29 +01009142 if (!iov->iov_base)
9143 return iov->iov_len ? -EFAULT : 0;
9144 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009145 return -EFAULT;
9146
9147 /* arbitrary limit, but we need something */
9148 if (iov->iov_len > SZ_1G)
9149 return -EFAULT;
9150
Pavel Begunkov50e96982021-03-24 22:59:01 +00009151 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
9152 return -EOVERFLOW;
9153
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009154 return 0;
9155}
9156
9157static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009158 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009159{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009160 struct page *last_hpage = NULL;
9161 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009162 int i, ret;
9163 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009164
Pavel Begunkov87094462021-04-11 01:46:36 +01009165 if (ctx->user_bufs)
9166 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01009167 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01009168 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009169 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009170 if (ret)
9171 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01009172 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9173 if (ret)
9174 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009175 ret = io_buffers_map_alloc(ctx, nr_args);
9176 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08009177 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009178 return ret;
9179 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009180
Pavel Begunkov87094462021-04-11 01:46:36 +01009181 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07009182 ret = io_copy_iov(ctx, &iov, arg, i);
9183 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009184 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009185 ret = io_buffer_validate(&iov);
9186 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009187 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009188 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009189 ret = -EINVAL;
9190 break;
9191 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009192
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009193 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9194 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009195 if (ret)
9196 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009197 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009198
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009199 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009200
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009201 ctx->buf_data = data;
9202 if (ret)
9203 __io_sqe_buffers_unregister(ctx);
9204 else
9205 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009206 return ret;
9207}
9208
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009209static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9210 struct io_uring_rsrc_update2 *up,
9211 unsigned int nr_args)
9212{
9213 u64 __user *tags = u64_to_user_ptr(up->tags);
9214 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009215 struct page *last_hpage = NULL;
9216 bool needs_switch = false;
9217 __u32 done;
9218 int i, err;
9219
9220 if (!ctx->buf_data)
9221 return -ENXIO;
9222 if (up->offset + nr_args > ctx->nr_user_bufs)
9223 return -EINVAL;
9224
9225 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009226 struct io_mapped_ubuf *imu;
9227 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009228 u64 tag = 0;
9229
9230 err = io_copy_iov(ctx, &iov, iovs, done);
9231 if (err)
9232 break;
9233 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9234 err = -EFAULT;
9235 break;
9236 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009237 err = io_buffer_validate(&iov);
9238 if (err)
9239 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009240 if (!iov.iov_base && tag) {
9241 err = -EINVAL;
9242 break;
9243 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009244 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9245 if (err)
9246 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009247
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009248 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009249 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009250 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9251 ctx->rsrc_node, ctx->user_bufs[i]);
9252 if (unlikely(err)) {
9253 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009254 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009255 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009256 ctx->user_bufs[i] = NULL;
9257 needs_switch = true;
9258 }
9259
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009260 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009261 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009262 }
9263
9264 if (needs_switch)
9265 io_rsrc_node_switch(ctx, ctx->buf_data);
9266 return done ? done : err;
9267}
9268
Jens Axboe9b402842019-04-11 11:45:41 -06009269static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9270{
9271 __s32 __user *fds = arg;
9272 int fd;
9273
9274 if (ctx->cq_ev_fd)
9275 return -EBUSY;
9276
9277 if (copy_from_user(&fd, fds, sizeof(*fds)))
9278 return -EFAULT;
9279
9280 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9281 if (IS_ERR(ctx->cq_ev_fd)) {
9282 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009283
Jens Axboe9b402842019-04-11 11:45:41 -06009284 ctx->cq_ev_fd = NULL;
9285 return ret;
9286 }
9287
9288 return 0;
9289}
9290
9291static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9292{
9293 if (ctx->cq_ev_fd) {
9294 eventfd_ctx_put(ctx->cq_ev_fd);
9295 ctx->cq_ev_fd = NULL;
9296 return 0;
9297 }
9298
9299 return -ENXIO;
9300}
9301
Jens Axboe5a2e7452020-02-23 16:23:11 -07009302static void io_destroy_buffers(struct io_ring_ctx *ctx)
9303{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009304 struct io_buffer *buf;
9305 unsigned long index;
9306
Jens Axboe8bab4c02021-09-24 07:12:27 -06009307 xa_for_each(&ctx->io_buffers, index, buf) {
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009308 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009309 cond_resched();
9310 }
Jens Axboe5a2e7452020-02-23 16:23:11 -07009311}
9312
Jens Axboe4010fec2021-02-27 15:04:18 -07009313static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009314{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009315 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009316 int nr = 0;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009317
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009318 mutex_lock(&ctx->uring_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009319 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009320
9321 while (state->free_list.next) {
9322 struct io_wq_work_node *node;
9323 struct io_kiocb *req;
9324
9325 node = wq_stack_extract(&state->free_list);
9326 req = container_of(node, struct io_kiocb, comp_list);
9327 kmem_cache_free(req_cachep, req);
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009328 nr++;
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009329 }
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009330 if (nr)
9331 percpu_ref_put_many(&ctx->refs, nr);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009332 mutex_unlock(&ctx->uring_lock);
9333}
9334
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009335static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009336{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009337 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009338 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009339}
9340
Pavel Begunkovc0724812021-10-04 20:02:54 +01009341static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009342{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009343 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009344
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009345 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009346 mmdrop(ctx->mm_account);
9347 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009348 }
Jens Axboedef596e2019-01-09 08:59:42 -07009349
Pavel Begunkovab409402021-10-09 23:14:41 +01009350 io_rsrc_refs_drop(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009351 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9352 io_wait_rsrc_data(ctx->buf_data);
9353 io_wait_rsrc_data(ctx->file_data);
9354
Hao Xu8bad28d2021-02-19 17:19:36 +08009355 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009356 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009357 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009358 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009359 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009360 if (ctx->rings)
9361 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009362 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009363 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009364 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009365 if (ctx->sq_creds)
9366 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009367
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009368 /* there are no registered resources left, nobody uses it */
9369 if (ctx->rsrc_node)
9370 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009371 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009372 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009373 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov756ab7c2021-10-06 16:06:47 +01009374 flush_delayed_work(&ctx->fallback_work);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009375
9376 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9377 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009378
9379#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07009380 if (ctx->ring_sock) {
9381 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009382 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07009383 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009384#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009385 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009386
Hristo Venev75b28af2019-08-26 17:23:46 +00009387 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009388 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009389
9390 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009391 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009392 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009393 if (ctx->hash_map)
9394 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009395 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009396 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009397 kfree(ctx);
9398}
9399
9400static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9401{
9402 struct io_ring_ctx *ctx = file->private_data;
9403 __poll_t mask = 0;
9404
Pavel Begunkovd60aa652021-10-04 20:02:52 +01009405 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009406 /*
9407 * synchronizes with barrier from wq_has_sleeper call in
9408 * io_commit_cqring
9409 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009410 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009411 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009412 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009413
9414 /*
9415 * Don't flush cqring overflow list here, just do a simple check.
9416 * Otherwise there could possible be ABBA deadlock:
9417 * CPU0 CPU1
9418 * ---- ----
9419 * lock(&ctx->uring_lock);
9420 * lock(&ep->mtx);
9421 * lock(&ctx->uring_lock);
9422 * lock(&ep->mtx);
9423 *
9424 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9425 * pushs them to do the flush.
9426 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009427 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009428 mask |= EPOLLIN | EPOLLRDNORM;
9429
9430 return mask;
9431}
9432
Yejune Deng0bead8c2020-12-24 11:02:20 +08009433static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009434{
Jens Axboe4379bf82021-02-15 13:40:22 -07009435 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009436
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009437 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009438 if (creds) {
9439 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009440 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009441 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009442
9443 return -EINVAL;
9444}
9445
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009446struct io_tctx_exit {
9447 struct callback_head task_work;
9448 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009449 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009450};
9451
Pavel Begunkovc0724812021-10-04 20:02:54 +01009452static __cold void io_tctx_exit_cb(struct callback_head *cb)
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009453{
9454 struct io_uring_task *tctx = current->io_uring;
9455 struct io_tctx_exit *work;
9456
9457 work = container_of(cb, struct io_tctx_exit, task_work);
9458 /*
9459 * When @in_idle, we're in cancellation and it's racy to remove the
9460 * node. It'll be removed by the end of cancellation, just ignore it.
9461 */
9462 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009463 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009464 complete(&work->completion);
9465}
9466
Pavel Begunkovc0724812021-10-04 20:02:54 +01009467static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
Pavel Begunkov28090c12021-04-25 23:34:45 +01009468{
9469 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9470
9471 return req->ctx == data;
9472}
9473
Pavel Begunkovc0724812021-10-04 20:02:54 +01009474static __cold void io_ring_exit_work(struct work_struct *work)
Jens Axboe85faa7b2020-04-09 18:14:00 -06009475{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009476 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009477 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009478 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009479 struct io_tctx_exit exit;
9480 struct io_tctx_node *node;
9481 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009482
Jens Axboe56952e92020-06-17 15:00:04 -06009483 /*
9484 * If we're doing polled IO and end up having requests being
9485 * submitted async (out-of-line), then completions can come in while
9486 * we're waiting for refs to drop. We need to reap these manually,
9487 * as nobody else will be looking for them.
9488 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009489 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009490 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009491 if (ctx->sq_data) {
9492 struct io_sq_data *sqd = ctx->sq_data;
9493 struct task_struct *tsk;
9494
9495 io_sq_thread_park(sqd);
9496 tsk = sqd->thread;
9497 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9498 io_wq_cancel_cb(tsk->io_uring->io_wq,
9499 io_cancel_ctx_cb, ctx, true);
9500 io_sq_thread_unpark(sqd);
9501 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009502
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009503 io_req_caches_free(ctx);
9504
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009505 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9506 /* there is little hope left, don't run it too often */
9507 interval = HZ * 60;
9508 }
9509 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009510
Pavel Begunkov7f006512021-04-14 13:38:34 +01009511 init_completion(&exit.completion);
9512 init_task_work(&exit.task_work, io_tctx_exit_cb);
9513 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009514 /*
9515 * Some may use context even when all refs and requests have been put,
9516 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009517 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009518 * this lock/unlock section also waits them to finish.
9519 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009520 mutex_lock(&ctx->uring_lock);
9521 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009522 WARN_ON_ONCE(time_after(jiffies, timeout));
9523
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009524 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9525 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009526 /* don't spin on a single task if cancellation failed */
9527 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009528 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9529 if (WARN_ON_ONCE(ret))
9530 continue;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009531
9532 mutex_unlock(&ctx->uring_lock);
9533 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009534 mutex_lock(&ctx->uring_lock);
9535 }
9536 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009537 spin_lock(&ctx->completion_lock);
9538 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009539
Jens Axboe85faa7b2020-04-09 18:14:00 -06009540 io_ring_ctx_free(ctx);
9541}
9542
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009543/* Returns true if we found and killed one or more timeouts */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009544static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx,
9545 struct task_struct *tsk, bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009546{
9547 struct io_kiocb *req, *tmp;
9548 int canceled = 0;
9549
Jens Axboe79ebeae2021-08-10 15:18:27 -06009550 spin_lock(&ctx->completion_lock);
9551 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009552 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009553 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009554 io_kill_timeout(req, -ECANCELED);
9555 canceled++;
9556 }
9557 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009558 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009559 if (canceled != 0)
9560 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009561 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009562 if (canceled != 0)
9563 io_cqring_ev_posted(ctx);
9564 return canceled != 0;
9565}
9566
Pavel Begunkovc0724812021-10-04 20:02:54 +01009567static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009568{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009569 unsigned long index;
9570 struct creds *creds;
9571
Jens Axboe2b188cc2019-01-07 10:46:33 -07009572 mutex_lock(&ctx->uring_lock);
9573 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009574 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009575 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009576 xa_for_each(&ctx->personalities, index, creds)
9577 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009578 mutex_unlock(&ctx->uring_lock);
9579
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009580 io_kill_timeouts(ctx, NULL, true);
9581 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009582
Jens Axboe15dff282019-11-13 09:09:23 -07009583 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009584 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009585
Jens Axboe85faa7b2020-04-09 18:14:00 -06009586 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009587 /*
9588 * Use system_unbound_wq to avoid spawning tons of event kworkers
9589 * if we're exiting a ton of rings at the same time. It just adds
9590 * noise and overhead, there's no discernable change in runtime
9591 * over using system_wq.
9592 */
9593 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009594}
9595
9596static int io_uring_release(struct inode *inode, struct file *file)
9597{
9598 struct io_ring_ctx *ctx = file->private_data;
9599
9600 file->private_data = NULL;
9601 io_ring_ctx_wait_and_kill(ctx);
9602 return 0;
9603}
9604
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009605struct io_task_cancel {
9606 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009607 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009608};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009609
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009610static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009611{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009612 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009613 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009614 bool ret;
9615
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009616 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009617 struct io_ring_ctx *ctx = req->ctx;
9618
9619 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009620 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009621 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009622 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009623 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009624 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009625 }
9626 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009627}
9628
Pavel Begunkovc0724812021-10-04 20:02:54 +01009629static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
9630 struct task_struct *task,
9631 bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009632{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009633 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009634 LIST_HEAD(list);
9635
Jens Axboe79ebeae2021-08-10 15:18:27 -06009636 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009637 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009638 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009639 list_cut_position(&list, &ctx->defer_list, &de->list);
9640 break;
9641 }
9642 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009643 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009644 if (list_empty(&list))
9645 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009646
9647 while (!list_empty(&list)) {
9648 de = list_first_entry(&list, struct io_defer_entry, list);
9649 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009650 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009651 kfree(de);
9652 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009653 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009654}
9655
Pavel Begunkovc0724812021-10-04 20:02:54 +01009656static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
Pavel Begunkov1b007642021-03-06 11:02:17 +00009657{
9658 struct io_tctx_node *node;
9659 enum io_wq_cancel cret;
9660 bool ret = false;
9661
9662 mutex_lock(&ctx->uring_lock);
9663 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9664 struct io_uring_task *tctx = node->task->io_uring;
9665
9666 /*
9667 * io_wq will stay alive while we hold uring_lock, because it's
9668 * killed after ctx nodes, which requires to take the lock.
9669 */
9670 if (!tctx || !tctx->io_wq)
9671 continue;
9672 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9673 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9674 }
9675 mutex_unlock(&ctx->uring_lock);
9676
9677 return ret;
9678}
9679
Pavel Begunkovc0724812021-10-04 20:02:54 +01009680static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9681 struct task_struct *task,
9682 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009683{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009684 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009685 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009686
9687 while (1) {
9688 enum io_wq_cancel cret;
9689 bool ret = false;
9690
Pavel Begunkov1b007642021-03-06 11:02:17 +00009691 if (!task) {
9692 ret |= io_uring_try_cancel_iowq(ctx);
9693 } else if (tctx && tctx->io_wq) {
9694 /*
9695 * Cancels requests of all rings, not only @ctx, but
9696 * it's fine as the task is in exit/exec.
9697 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009698 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009699 &cancel, true);
9700 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9701 }
9702
9703 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009704 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009705 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01009706 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009707 io_iopoll_try_reap_events(ctx);
9708 ret = true;
9709 }
9710 }
9711
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009712 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9713 ret |= io_poll_remove_all(ctx, task, cancel_all);
9714 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009715 if (task)
9716 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009717 if (!ret)
9718 break;
9719 cond_resched();
9720 }
9721}
9722
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009723static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009724{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009725 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009726 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009727 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009728
9729 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009730 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009731 if (unlikely(ret))
9732 return ret;
Pavel Begunkove139a1e2021-10-19 23:43:46 +01009733
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009734 tctx = current->io_uring;
Pavel Begunkove139a1e2021-10-19 23:43:46 +01009735 if (ctx->iowq_limits_set) {
9736 unsigned int limits[2] = { ctx->iowq_limits[0],
9737 ctx->iowq_limits[1], };
9738
9739 ret = io_wq_max_workers(tctx->io_wq, limits);
9740 if (ret)
9741 return ret;
9742 }
Jens Axboe0f212202020-09-13 13:09:39 -06009743 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009744 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9745 node = kmalloc(sizeof(*node), GFP_KERNEL);
9746 if (!node)
9747 return -ENOMEM;
9748 node->ctx = ctx;
9749 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009750
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009751 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9752 node, GFP_KERNEL));
9753 if (ret) {
9754 kfree(node);
9755 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009756 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009757
9758 mutex_lock(&ctx->uring_lock);
9759 list_add(&node->ctx_node, &ctx->tctx_list);
9760 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009761 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009762 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009763 return 0;
9764}
9765
9766/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009767 * Note that this task has used io_uring. We use it for cancelation purposes.
9768 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009769static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009770{
9771 struct io_uring_task *tctx = current->io_uring;
9772
9773 if (likely(tctx && tctx->last == ctx))
9774 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009775 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009776}
9777
9778/*
Jens Axboe0f212202020-09-13 13:09:39 -06009779 * Remove this io_uring_file -> task mapping.
9780 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009781static __cold void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009782{
9783 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009784 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009785
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009786 if (!tctx)
9787 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009788 node = xa_erase(&tctx->xa, index);
9789 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009790 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009791
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009792 WARN_ON_ONCE(current != node->task);
9793 WARN_ON_ONCE(list_empty(&node->ctx_node));
9794
9795 mutex_lock(&node->ctx->uring_lock);
9796 list_del(&node->ctx_node);
9797 mutex_unlock(&node->ctx->uring_lock);
9798
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009799 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009800 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009801 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009802}
9803
Pavel Begunkovc0724812021-10-04 20:02:54 +01009804static __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009805{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009806 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009807 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009808 unsigned long index;
9809
Jens Axboe8bab4c02021-09-24 07:12:27 -06009810 xa_for_each(&tctx->xa, index, node) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009811 io_uring_del_tctx_node(index);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009812 cond_resched();
9813 }
Marco Elverb16ef422021-05-27 11:25:48 +02009814 if (wq) {
9815 /*
9816 * Must be after io_uring_del_task_file() (removes nodes under
9817 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9818 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009819 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009820 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009821 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009822}
9823
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009824static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009825{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009826 if (tracked)
9827 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009828 return percpu_counter_sum(&tctx->inflight);
9829}
9830
Pavel Begunkovc0724812021-10-04 20:02:54 +01009831static __cold void io_uring_drop_tctx_refs(struct task_struct *task)
Pavel Begunkov09899b12021-06-14 02:36:22 +01009832{
9833 struct io_uring_task *tctx = task->io_uring;
9834 unsigned int refs = tctx->cached_refs;
9835
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009836 if (refs) {
9837 tctx->cached_refs = 0;
9838 percpu_counter_sub(&tctx->inflight, refs);
9839 put_task_struct_many(task, refs);
9840 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009841}
9842
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009843/*
9844 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9845 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9846 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009847static __cold void io_uring_cancel_generic(bool cancel_all,
9848 struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009849{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009850 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009851 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009852 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009853 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009854
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009855 WARN_ON_ONCE(sqd && sqd->thread != current);
9856
Palash Oswal6d042ff2021-04-27 18:21:49 +05309857 if (!current->io_uring)
9858 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009859 if (tctx->io_wq)
9860 io_wq_exit_start(tctx->io_wq);
9861
Jens Axboefdaf0832020-10-30 09:37:30 -06009862 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009863 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009864 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009865 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009866 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009867 if (!inflight)
9868 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009869
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009870 if (!sqd) {
9871 struct io_tctx_node *node;
9872 unsigned long index;
9873
9874 xa_for_each(&tctx->xa, index, node) {
9875 /* sqpoll task will cancel all its requests */
9876 if (node->ctx->sq_data)
9877 continue;
9878 io_uring_try_cancel_requests(node->ctx, current,
9879 cancel_all);
9880 }
9881 } else {
9882 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9883 io_uring_try_cancel_requests(ctx, current,
9884 cancel_all);
9885 }
9886
9887 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009888 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009889 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009890 * If we've seen completions, retry without waiting. This
9891 * avoids a race where a completion comes in before we did
9892 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009893 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009894 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009895 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009896 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009897 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009898 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009899
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009900 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009901 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009902 /* for exec all current's requests should be gone, kill tctx */
9903 __io_uring_free(current);
9904 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009905}
9906
Hao Xuf552a272021-08-12 12:14:35 +08009907void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009908{
Hao Xuf552a272021-08-12 12:14:35 +08009909 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009910}
9911
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009912static void *io_uring_validate_mmap_request(struct file *file,
9913 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009914{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009915 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009916 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009917 struct page *page;
9918 void *ptr;
9919
9920 switch (offset) {
9921 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009922 case IORING_OFF_CQ_RING:
9923 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009924 break;
9925 case IORING_OFF_SQES:
9926 ptr = ctx->sq_sqes;
9927 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009928 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009929 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009930 }
9931
9932 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009933 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009934 return ERR_PTR(-EINVAL);
9935
9936 return ptr;
9937}
9938
9939#ifdef CONFIG_MMU
9940
Pavel Begunkovc0724812021-10-04 20:02:54 +01009941static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009942{
9943 size_t sz = vma->vm_end - vma->vm_start;
9944 unsigned long pfn;
9945 void *ptr;
9946
9947 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9948 if (IS_ERR(ptr))
9949 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009950
9951 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9952 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9953}
9954
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009955#else /* !CONFIG_MMU */
9956
9957static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9958{
9959 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9960}
9961
9962static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9963{
9964 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9965}
9966
9967static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9968 unsigned long addr, unsigned long len,
9969 unsigned long pgoff, unsigned long flags)
9970{
9971 void *ptr;
9972
9973 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9974 if (IS_ERR(ptr))
9975 return PTR_ERR(ptr);
9976
9977 return (unsigned long) ptr;
9978}
9979
9980#endif /* !CONFIG_MMU */
9981
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009982static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009983{
9984 DEFINE_WAIT(wait);
9985
9986 do {
9987 if (!io_sqring_full(ctx))
9988 break;
Jens Axboe90554202020-09-03 12:12:41 -06009989 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9990
9991 if (!io_sqring_full(ctx))
9992 break;
Jens Axboe90554202020-09-03 12:12:41 -06009993 schedule();
9994 } while (!signal_pending(current));
9995
9996 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009997 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009998}
9999
Hao Xuc73ebb62020-11-03 10:54:37 +080010000static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
10001 struct __kernel_timespec __user **ts,
10002 const sigset_t __user **sig)
10003{
10004 struct io_uring_getevents_arg arg;
10005
10006 /*
10007 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
10008 * is just a pointer to the sigset_t.
10009 */
10010 if (!(flags & IORING_ENTER_EXT_ARG)) {
10011 *sig = (const sigset_t __user *) argp;
10012 *ts = NULL;
10013 return 0;
10014 }
10015
10016 /*
10017 * EXT_ARG is set - ensure we agree on the size of it and copy in our
10018 * timespec and sigset_t pointers if good.
10019 */
10020 if (*argsz != sizeof(arg))
10021 return -EINVAL;
10022 if (copy_from_user(&arg, argp, sizeof(arg)))
10023 return -EFAULT;
10024 *sig = u64_to_user_ptr(arg.sigmask);
10025 *argsz = arg.sigmask_sz;
10026 *ts = u64_to_user_ptr(arg.ts);
10027 return 0;
10028}
10029
Jens Axboe2b188cc2019-01-07 10:46:33 -070010030SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +080010031 u32, min_complete, u32, flags, const void __user *, argp,
10032 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010033{
10034 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010035 int submitted = 0;
10036 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010037 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010038
Jens Axboe4c6e2772020-07-01 11:29:10 -060010039 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -070010040
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010041 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
10042 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010043 return -EINVAL;
10044
10045 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010046 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010047 return -EBADF;
10048
10049 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010050 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010051 goto out_fput;
10052
10053 ret = -ENXIO;
10054 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010055 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010056 goto out_fput;
10057
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010058 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010059 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010060 goto out;
10061
Jens Axboe6c271ce2019-01-10 11:22:30 -070010062 /*
10063 * For SQ polling, the thread will do all submissions and completions.
10064 * Just return the requested submit count, and wake the thread if
10065 * we were asked to.
10066 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -060010067 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -070010068 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +010010069 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +000010070
Jens Axboe21f96522021-08-14 09:04:40 -060010071 if (unlikely(ctx->sq_data->thread == NULL)) {
10072 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +010010073 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -060010074 }
Jens Axboe6c271ce2019-01-10 11:22:30 -070010075 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -060010076 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +000010077 if (flags & IORING_ENTER_SQ_WAIT) {
10078 ret = io_sqpoll_wait_sq(ctx);
10079 if (ret)
10080 goto out;
10081 }
Jens Axboe6c271ce2019-01-10 11:22:30 -070010082 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -060010083 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010084 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -060010085 if (unlikely(ret))
10086 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010087 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -060010088 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010089 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010090
10091 if (submitted != to_submit)
10092 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010093 }
10094 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +080010095 const sigset_t __user *sig;
10096 struct __kernel_timespec __user *ts;
10097
10098 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
10099 if (unlikely(ret))
10100 goto out;
10101
Jens Axboe2b188cc2019-01-07 10:46:33 -070010102 min_complete = min(min_complete, ctx->cq_entries);
10103
Xiaoguang Wang32b22442020-03-11 09:26:09 +080010104 /*
10105 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
10106 * space applications don't need to do io completion events
10107 * polling again, they can rely on io_sq_thread to do polling
10108 * work, which can reduce cpu usage and uring_lock contention.
10109 */
10110 if (ctx->flags & IORING_SETUP_IOPOLL &&
10111 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +030010112 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -070010113 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +080010114 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -070010115 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010116 }
10117
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010118out:
Pavel Begunkov6805b322019-10-08 02:18:42 +030010119 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010120out_fput:
10121 fdput(f);
10122 return submitted ? submitted : ret;
10123}
10124
Tobias Klauserbebdb652020-02-26 18:38:32 +010010125#ifdef CONFIG_PROC_FS
Pavel Begunkovc0724812021-10-04 20:02:54 +010010126static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010127 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -070010128{
Jens Axboe87ce9552020-01-30 08:25:34 -070010129 struct user_namespace *uns = seq_user_ns(m);
10130 struct group_info *gi;
10131 kernel_cap_t cap;
10132 unsigned __capi;
10133 int g;
10134
10135 seq_printf(m, "%5d\n", id);
10136 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
10137 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
10138 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
10139 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
10140 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
10141 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
10142 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
10143 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
10144 seq_puts(m, "\n\tGroups:\t");
10145 gi = cred->group_info;
10146 for (g = 0; g < gi->ngroups; g++) {
10147 seq_put_decimal_ull(m, g ? " " : "",
10148 from_kgid_munged(uns, gi->gid[g]));
10149 }
10150 seq_puts(m, "\n\tCapEff:\t");
10151 cap = cred->cap_effective;
10152 CAP_FOR_EACH_U32(__capi)
10153 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
10154 seq_putc(m, '\n');
10155 return 0;
10156}
10157
Pavel Begunkovc0724812021-10-04 20:02:54 +010010158static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx,
10159 struct seq_file *m)
Jens Axboe87ce9552020-01-30 08:25:34 -070010160{
Joseph Qidbbe9c62020-09-29 09:01:22 -060010161 struct io_sq_data *sq = NULL;
Hao Xu83f84352021-09-13 21:08:54 +080010162 struct io_overflow_cqe *ocqe;
10163 struct io_rings *r = ctx->rings;
10164 unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
Hao Xu83f84352021-09-13 21:08:54 +080010165 unsigned int sq_head = READ_ONCE(r->sq.head);
10166 unsigned int sq_tail = READ_ONCE(r->sq.tail);
10167 unsigned int cq_head = READ_ONCE(r->cq.head);
10168 unsigned int cq_tail = READ_ONCE(r->cq.tail);
Jens Axboef75d1182021-10-29 06:36:45 -060010169 unsigned int sq_entries, cq_entries;
Jens Axboefad8e0d2020-09-28 08:57:48 -060010170 bool has_lock;
Hao Xu83f84352021-09-13 21:08:54 +080010171 unsigned int i;
10172
10173 /*
10174 * we may get imprecise sqe and cqe info if uring is actively running
10175 * since we get cached_sq_head and cached_cq_tail without uring_lock
10176 * and sq_tail and cq_head are changed by userspace. But it's ok since
10177 * we usually use these info when it is stuck.
10178 */
Jens Axboef75d1182021-10-29 06:36:45 -060010179 seq_printf(m, "SqMask:\t\t0x%x\n", sq_mask);
10180 seq_printf(m, "SqHead:\t%u\n", sq_head);
10181 seq_printf(m, "SqTail:\t%u\n", sq_tail);
10182 seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head);
10183 seq_printf(m, "CqMask:\t0x%x\n", cq_mask);
10184 seq_printf(m, "CqHead:\t%u\n", cq_head);
10185 seq_printf(m, "CqTail:\t%u\n", cq_tail);
10186 seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail);
10187 seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head);
10188 sq_entries = min(sq_tail - sq_head, ctx->sq_entries);
10189 for (i = 0; i < sq_entries; i++) {
10190 unsigned int entry = i + sq_head;
10191 unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]);
Jens Axboea1957782021-11-05 09:31:05 -060010192 struct io_uring_sqe *sqe;
Hao Xu83f84352021-09-13 21:08:54 +080010193
Jens Axboef75d1182021-10-29 06:36:45 -060010194 if (sq_idx > sq_mask)
10195 continue;
10196 sqe = &ctx->sq_sqes[sq_idx];
10197 seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n",
10198 sq_idx, sqe->opcode, sqe->fd, sqe->flags,
10199 sqe->user_data);
Hao Xu83f84352021-09-13 21:08:54 +080010200 }
Jens Axboef75d1182021-10-29 06:36:45 -060010201 seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head);
10202 cq_entries = min(cq_tail - cq_head, ctx->cq_entries);
10203 for (i = 0; i < cq_entries; i++) {
10204 unsigned int entry = i + cq_head;
10205 struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask];
Hao Xu83f84352021-09-13 21:08:54 +080010206
10207 seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n",
Jens Axboef75d1182021-10-29 06:36:45 -060010208 entry & cq_mask, cqe->user_data, cqe->res,
10209 cqe->flags);
Hao Xu83f84352021-09-13 21:08:54 +080010210 }
Jens Axboe87ce9552020-01-30 08:25:34 -070010211
Jens Axboefad8e0d2020-09-28 08:57:48 -060010212 /*
10213 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10214 * since fdinfo case grabs it in the opposite direction of normal use
10215 * cases. If we fail to get the lock, we just don't iterate any
10216 * structures that could be going away outside the io_uring mutex.
10217 */
10218 has_lock = mutex_trylock(&ctx->uring_lock);
10219
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010220 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -060010221 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010222 if (!sq->thread)
10223 sq = NULL;
10224 }
Joseph Qidbbe9c62020-09-29 09:01:22 -060010225
10226 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10227 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -070010228 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010229 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -070010230 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -070010231
Jens Axboe87ce9552020-01-30 08:25:34 -070010232 if (f)
10233 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10234 else
10235 seq_printf(m, "%5u: <none>\n", i);
10236 }
10237 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010238 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +010010239 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +010010240 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -070010241
Pavel Begunkov4751f532021-04-01 15:43:55 +010010242 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -070010243 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010244 if (has_lock && !xa_empty(&ctx->personalities)) {
10245 unsigned long index;
10246 const struct cred *cred;
10247
Jens Axboe87ce9552020-01-30 08:25:34 -070010248 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010249 xa_for_each(&ctx->personalities, index, cred)
10250 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010251 }
Hao Xu83f84352021-09-13 21:08:54 +080010252 if (has_lock)
10253 mutex_unlock(&ctx->uring_lock);
10254
10255 seq_puts(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010256 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010257 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10258 struct hlist_head *list = &ctx->cancel_hash[i];
10259 struct io_kiocb *req;
10260
10261 hlist_for_each_entry(req, list, hash_node)
10262 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10263 req->task->task_works != NULL);
10264 }
Hao Xu83f84352021-09-13 21:08:54 +080010265
10266 seq_puts(m, "CqOverflowList:\n");
10267 list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
10268 struct io_uring_cqe *cqe = &ocqe->cqe;
10269
10270 seq_printf(m, " user_data=%llu, res=%d, flags=%x\n",
10271 cqe->user_data, cqe->res, cqe->flags);
10272
10273 }
10274
Jens Axboe79ebeae2021-08-10 15:18:27 -060010275 spin_unlock(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010276}
10277
Pavel Begunkovc0724812021-10-04 20:02:54 +010010278static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
Jens Axboe87ce9552020-01-30 08:25:34 -070010279{
10280 struct io_ring_ctx *ctx = f->private_data;
10281
10282 if (percpu_ref_tryget(&ctx->refs)) {
10283 __io_uring_show_fdinfo(ctx, m);
10284 percpu_ref_put(&ctx->refs);
10285 }
10286}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010287#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010288
Jens Axboe2b188cc2019-01-07 10:46:33 -070010289static const struct file_operations io_uring_fops = {
10290 .release = io_uring_release,
10291 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010292#ifndef CONFIG_MMU
10293 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10294 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10295#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010296 .poll = io_uring_poll,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010297#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010298 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010299#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010300};
10301
Pavel Begunkovc0724812021-10-04 20:02:54 +010010302static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10303 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010304{
Hristo Venev75b28af2019-08-26 17:23:46 +000010305 struct io_rings *rings;
10306 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010307
Jens Axboebd740482020-08-05 12:58:23 -060010308 /* make sure these are sane, as we already accounted them */
10309 ctx->sq_entries = p->sq_entries;
10310 ctx->cq_entries = p->cq_entries;
10311
Hristo Venev75b28af2019-08-26 17:23:46 +000010312 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10313 if (size == SIZE_MAX)
10314 return -EOVERFLOW;
10315
10316 rings = io_mem_alloc(size);
10317 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010318 return -ENOMEM;
10319
Hristo Venev75b28af2019-08-26 17:23:46 +000010320 ctx->rings = rings;
10321 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10322 rings->sq_ring_mask = p->sq_entries - 1;
10323 rings->cq_ring_mask = p->cq_entries - 1;
10324 rings->sq_ring_entries = p->sq_entries;
10325 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010326
10327 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010328 if (size == SIZE_MAX) {
10329 io_mem_free(ctx->rings);
10330 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010331 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010332 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010333
10334 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010335 if (!ctx->sq_sqes) {
10336 io_mem_free(ctx->rings);
10337 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010338 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010339 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010340
Jens Axboe2b188cc2019-01-07 10:46:33 -070010341 return 0;
10342}
10343
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010344static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10345{
10346 int ret, fd;
10347
10348 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10349 if (fd < 0)
10350 return fd;
10351
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010352 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010353 if (ret) {
10354 put_unused_fd(fd);
10355 return ret;
10356 }
10357 fd_install(fd, file);
10358 return fd;
10359}
10360
Jens Axboe2b188cc2019-01-07 10:46:33 -070010361/*
10362 * Allocate an anonymous fd, this is what constitutes the application
10363 * visible backing of an io_uring instance. The application mmaps this
10364 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10365 * we have to tie this fd to a socket for file garbage collection purposes.
10366 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010367static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010368{
10369 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010370#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010371 int ret;
10372
Jens Axboe2b188cc2019-01-07 10:46:33 -070010373 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10374 &ctx->ring_sock);
10375 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010376 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010377#endif
10378
Paul Moore91a9ab72021-02-01 19:33:52 -050010379 file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx,
10380 O_RDWR | O_CLOEXEC, NULL);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010381#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010382 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010383 sock_release(ctx->ring_sock);
10384 ctx->ring_sock = NULL;
10385 } else {
10386 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010387 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010388#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010389 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010390}
10391
Pavel Begunkovc0724812021-10-04 20:02:54 +010010392static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
10393 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010394{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010395 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010396 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010397 int ret;
10398
Jens Axboe8110c1a2019-12-28 15:39:54 -070010399 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010400 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010401 if (entries > IORING_MAX_ENTRIES) {
10402 if (!(p->flags & IORING_SETUP_CLAMP))
10403 return -EINVAL;
10404 entries = IORING_MAX_ENTRIES;
10405 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010406
10407 /*
10408 * Use twice as many entries for the CQ ring. It's possible for the
10409 * application to drive a higher depth than the size of the SQ ring,
10410 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010411 * some flexibility in overcommitting a bit. If the application has
10412 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10413 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010414 */
10415 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010416 if (p->flags & IORING_SETUP_CQSIZE) {
10417 /*
10418 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10419 * to a power-of-two, if it isn't already. We do NOT impose
10420 * any cq vs sq ring sizing.
10421 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010422 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010423 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010424 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10425 if (!(p->flags & IORING_SETUP_CLAMP))
10426 return -EINVAL;
10427 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10428 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010429 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10430 if (p->cq_entries < p->sq_entries)
10431 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010432 } else {
10433 p->cq_entries = 2 * p->sq_entries;
10434 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010435
Jens Axboe2b188cc2019-01-07 10:46:33 -070010436 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010437 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010438 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010439 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010440 if (!capable(CAP_IPC_LOCK))
10441 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010442
10443 /*
10444 * This is just grabbed for accounting purposes. When a process exits,
10445 * the mm is exited and dropped before the files, hence we need to hang
10446 * on to this mm purely for the purposes of being able to unaccount
10447 * memory (locked/pinned vm). It's not used for anything else.
10448 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010449 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010450 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010451
Jens Axboe2b188cc2019-01-07 10:46:33 -070010452 ret = io_allocate_scq_urings(ctx, p);
10453 if (ret)
10454 goto err;
10455
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010456 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010457 if (ret)
10458 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010459 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010460 ret = io_rsrc_node_switch_start(ctx);
10461 if (ret)
10462 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010463 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010464
Jens Axboe2b188cc2019-01-07 10:46:33 -070010465 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010466 p->sq_off.head = offsetof(struct io_rings, sq.head);
10467 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10468 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10469 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10470 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10471 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10472 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010473
10474 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010475 p->cq_off.head = offsetof(struct io_rings, cq.head);
10476 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10477 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10478 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10479 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10480 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010481 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010482
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010483 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10484 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010485 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010486 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010487 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
Pavel Begunkov04c76b42021-11-10 15:49:32 +000010488 IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010489
10490 if (copy_to_user(params, p, sizeof(*p))) {
10491 ret = -EFAULT;
10492 goto err;
10493 }
Jens Axboed1719f72020-07-30 13:43:53 -060010494
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010495 file = io_uring_get_file(ctx);
10496 if (IS_ERR(file)) {
10497 ret = PTR_ERR(file);
10498 goto err;
10499 }
10500
Jens Axboed1719f72020-07-30 13:43:53 -060010501 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010502 * Install ring fd as the very last thing, so we don't risk someone
10503 * having closed it before we finish setup
10504 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010505 ret = io_uring_install_fd(ctx, file);
10506 if (ret < 0) {
10507 /* fput will clean it up */
10508 fput(file);
10509 return ret;
10510 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010511
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010512 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010513 return ret;
10514err:
10515 io_ring_ctx_wait_and_kill(ctx);
10516 return ret;
10517}
10518
10519/*
10520 * Sets up an aio uring context, and returns the fd. Applications asks for a
10521 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10522 * params structure passed in.
10523 */
10524static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10525{
10526 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010527 int i;
10528
10529 if (copy_from_user(&p, params, sizeof(p)))
10530 return -EFAULT;
10531 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10532 if (p.resv[i])
10533 return -EINVAL;
10534 }
10535
Jens Axboe6c271ce2019-01-10 11:22:30 -070010536 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010537 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010538 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10539 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010540 return -EINVAL;
10541
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010542 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010543}
10544
10545SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10546 struct io_uring_params __user *, params)
10547{
10548 return io_uring_setup(entries, params);
10549}
10550
Pavel Begunkovc0724812021-10-04 20:02:54 +010010551static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg,
10552 unsigned nr_args)
Jens Axboe66f4af92020-01-16 15:36:52 -070010553{
10554 struct io_uring_probe *p;
10555 size_t size;
10556 int i, ret;
10557
10558 size = struct_size(p, ops, nr_args);
10559 if (size == SIZE_MAX)
10560 return -EOVERFLOW;
10561 p = kzalloc(size, GFP_KERNEL);
10562 if (!p)
10563 return -ENOMEM;
10564
10565 ret = -EFAULT;
10566 if (copy_from_user(p, arg, size))
10567 goto out;
10568 ret = -EINVAL;
10569 if (memchr_inv(p, 0, size))
10570 goto out;
10571
10572 p->last_op = IORING_OP_LAST - 1;
10573 if (nr_args > IORING_OP_LAST)
10574 nr_args = IORING_OP_LAST;
10575
10576 for (i = 0; i < nr_args; i++) {
10577 p->ops[i].op = i;
10578 if (!io_op_defs[i].not_supported)
10579 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10580 }
10581 p->ops_len = i;
10582
10583 ret = 0;
10584 if (copy_to_user(arg, p, size))
10585 ret = -EFAULT;
10586out:
10587 kfree(p);
10588 return ret;
10589}
10590
Jens Axboe071698e2020-01-28 10:04:42 -070010591static int io_register_personality(struct io_ring_ctx *ctx)
10592{
Jens Axboe4379bf82021-02-15 13:40:22 -070010593 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010594 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010595 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010596
Jens Axboe4379bf82021-02-15 13:40:22 -070010597 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010598
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010599 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10600 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010601 if (ret < 0) {
10602 put_cred(creds);
10603 return ret;
10604 }
10605 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010606}
10607
Pavel Begunkovc0724812021-10-04 20:02:54 +010010608static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
10609 void __user *arg, unsigned int nr_args)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010610{
10611 struct io_uring_restriction *res;
10612 size_t size;
10613 int i, ret;
10614
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010615 /* Restrictions allowed only if rings started disabled */
10616 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10617 return -EBADFD;
10618
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010619 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010620 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010621 return -EBUSY;
10622
10623 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10624 return -EINVAL;
10625
10626 size = array_size(nr_args, sizeof(*res));
10627 if (size == SIZE_MAX)
10628 return -EOVERFLOW;
10629
10630 res = memdup_user(arg, size);
10631 if (IS_ERR(res))
10632 return PTR_ERR(res);
10633
10634 ret = 0;
10635
10636 for (i = 0; i < nr_args; i++) {
10637 switch (res[i].opcode) {
10638 case IORING_RESTRICTION_REGISTER_OP:
10639 if (res[i].register_op >= IORING_REGISTER_LAST) {
10640 ret = -EINVAL;
10641 goto out;
10642 }
10643
10644 __set_bit(res[i].register_op,
10645 ctx->restrictions.register_op);
10646 break;
10647 case IORING_RESTRICTION_SQE_OP:
10648 if (res[i].sqe_op >= IORING_OP_LAST) {
10649 ret = -EINVAL;
10650 goto out;
10651 }
10652
10653 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10654 break;
10655 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10656 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10657 break;
10658 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10659 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10660 break;
10661 default:
10662 ret = -EINVAL;
10663 goto out;
10664 }
10665 }
10666
10667out:
10668 /* Reset all restrictions if an error happened */
10669 if (ret != 0)
10670 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10671 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010672 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010673
10674 kfree(res);
10675 return ret;
10676}
10677
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010678static int io_register_enable_rings(struct io_ring_ctx *ctx)
10679{
10680 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10681 return -EBADFD;
10682
10683 if (ctx->restrictions.registered)
10684 ctx->restricted = 1;
10685
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010686 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10687 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10688 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010689 return 0;
10690}
10691
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010692static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010693 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010694 unsigned nr_args)
10695{
10696 __u32 tmp;
10697 int err;
10698
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010699 if (up->resv)
10700 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010701 if (check_add_overflow(up->offset, nr_args, &tmp))
10702 return -EOVERFLOW;
10703 err = io_rsrc_node_switch_start(ctx);
10704 if (err)
10705 return err;
10706
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010707 switch (type) {
10708 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010709 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010710 case IORING_RSRC_BUFFER:
10711 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010712 }
10713 return -EINVAL;
10714}
10715
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010716static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10717 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010718{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010719 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010720
10721 if (!nr_args)
10722 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010723 memset(&up, 0, sizeof(up));
10724 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10725 return -EFAULT;
10726 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10727}
10728
10729static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010730 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010731{
10732 struct io_uring_rsrc_update2 up;
10733
10734 if (size != sizeof(up))
10735 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010736 if (copy_from_user(&up, arg, sizeof(up)))
10737 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010738 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010739 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010740 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010741}
10742
Pavel Begunkovc0724812021-10-04 20:02:54 +010010743static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010744 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010745{
10746 struct io_uring_rsrc_register rr;
10747
10748 /* keep it extendible */
10749 if (size != sizeof(rr))
10750 return -EINVAL;
10751
10752 memset(&rr, 0, sizeof(rr));
10753 if (copy_from_user(&rr, arg, size))
10754 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010755 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010756 return -EINVAL;
10757
Pavel Begunkov992da012021-06-10 16:37:37 +010010758 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010759 case IORING_RSRC_FILE:
10760 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10761 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010762 case IORING_RSRC_BUFFER:
10763 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10764 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010765 }
10766 return -EINVAL;
10767}
10768
Pavel Begunkovc0724812021-10-04 20:02:54 +010010769static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
10770 void __user *arg, unsigned len)
Jens Axboefe764212021-06-17 10:19:54 -060010771{
10772 struct io_uring_task *tctx = current->io_uring;
10773 cpumask_var_t new_mask;
10774 int ret;
10775
10776 if (!tctx || !tctx->io_wq)
10777 return -EINVAL;
10778
10779 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10780 return -ENOMEM;
10781
10782 cpumask_clear(new_mask);
10783 if (len > cpumask_size())
10784 len = cpumask_size();
10785
10786 if (copy_from_user(new_mask, arg, len)) {
10787 free_cpumask_var(new_mask);
10788 return -EFAULT;
10789 }
10790
10791 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10792 free_cpumask_var(new_mask);
10793 return ret;
10794}
10795
Pavel Begunkovc0724812021-10-04 20:02:54 +010010796static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
Jens Axboefe764212021-06-17 10:19:54 -060010797{
10798 struct io_uring_task *tctx = current->io_uring;
10799
10800 if (!tctx || !tctx->io_wq)
10801 return -EINVAL;
10802
10803 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10804}
10805
Pavel Begunkovc0724812021-10-04 20:02:54 +010010806static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10807 void __user *arg)
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010808 __must_hold(&ctx->uring_lock)
Jens Axboe2e480052021-08-27 11:33:19 -060010809{
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010810 struct io_tctx_node *node;
Jens Axboefa846932021-09-01 14:15:59 -060010811 struct io_uring_task *tctx = NULL;
10812 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010813 __u32 new_count[2];
10814 int i, ret;
10815
Jens Axboe2e480052021-08-27 11:33:19 -060010816 if (copy_from_user(new_count, arg, sizeof(new_count)))
10817 return -EFAULT;
10818 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10819 if (new_count[i] > INT_MAX)
10820 return -EINVAL;
10821
Jens Axboefa846932021-09-01 14:15:59 -060010822 if (ctx->flags & IORING_SETUP_SQPOLL) {
10823 sqd = ctx->sq_data;
10824 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010825 /*
10826 * Observe the correct sqd->lock -> ctx->uring_lock
10827 * ordering. Fine to drop uring_lock here, we hold
10828 * a ref to the ctx.
10829 */
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010830 refcount_inc(&sqd->refs);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010831 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010832 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010833 mutex_lock(&ctx->uring_lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010834 if (sqd->thread)
10835 tctx = sqd->thread->io_uring;
Jens Axboefa846932021-09-01 14:15:59 -060010836 }
10837 } else {
10838 tctx = current->io_uring;
10839 }
10840
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010841 BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits));
Jens Axboefa846932021-09-01 14:15:59 -060010842
Pavel Begunkovbad119b2021-11-08 15:10:03 +000010843 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10844 if (new_count[i])
10845 ctx->iowq_limits[i] = new_count[i];
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010846 ctx->iowq_limits_set = true;
10847
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010848 if (tctx && tctx->io_wq) {
10849 ret = io_wq_max_workers(tctx->io_wq, new_count);
10850 if (ret)
10851 goto err;
10852 } else {
10853 memset(new_count, 0, sizeof(new_count));
10854 }
Jens Axboefa846932021-09-01 14:15:59 -060010855
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010856 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010857 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010858 io_put_sq_data(sqd);
10859 }
Jens Axboe2e480052021-08-27 11:33:19 -060010860
10861 if (copy_to_user(arg, new_count, sizeof(new_count)))
10862 return -EFAULT;
10863
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010864 /* that's it for SQPOLL, only the SQPOLL task creates requests */
10865 if (sqd)
10866 return 0;
10867
10868 /* now propagate the restriction to all registered users */
10869 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
10870 struct io_uring_task *tctx = node->task->io_uring;
10871
10872 if (WARN_ON_ONCE(!tctx->io_wq))
10873 continue;
10874
10875 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10876 new_count[i] = ctx->iowq_limits[i];
10877 /* ignore errors, it always returns zero anyway */
10878 (void)io_wq_max_workers(tctx->io_wq, new_count);
10879 }
Jens Axboe2e480052021-08-27 11:33:19 -060010880 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010881err:
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010882 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010883 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010884 io_put_sq_data(sqd);
10885 }
Jens Axboefa846932021-09-01 14:15:59 -060010886 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010887}
10888
Jens Axboe071698e2020-01-28 10:04:42 -070010889static bool io_register_op_must_quiesce(int op)
10890{
10891 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010892 case IORING_REGISTER_BUFFERS:
10893 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010894 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010895 case IORING_UNREGISTER_FILES:
10896 case IORING_REGISTER_FILES_UPDATE:
10897 case IORING_REGISTER_PROBE:
10898 case IORING_REGISTER_PERSONALITY:
10899 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010900 case IORING_REGISTER_FILES2:
10901 case IORING_REGISTER_FILES_UPDATE2:
10902 case IORING_REGISTER_BUFFERS2:
10903 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010904 case IORING_REGISTER_IOWQ_AFF:
10905 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010906 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010907 return false;
10908 default:
10909 return true;
10910 }
10911}
10912
Pavel Begunkovc0724812021-10-04 20:02:54 +010010913static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx)
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010914{
10915 long ret;
10916
10917 percpu_ref_kill(&ctx->refs);
10918
10919 /*
10920 * Drop uring mutex before waiting for references to exit. If another
10921 * thread is currently inside io_uring_enter() it might need to grab the
10922 * uring_lock to make progress. If we hold it here across the drain
10923 * wait, then we can deadlock. It's safe to drop the mutex here, since
10924 * no new references will come in after we've killed the percpu ref.
10925 */
10926 mutex_unlock(&ctx->uring_lock);
10927 do {
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010928 ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ);
10929 if (ret) {
10930 ret = min(0L, ret);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010931 break;
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010932 }
10933
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010934 ret = io_run_task_work_sig();
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010935 io_req_caches_free(ctx);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010936 } while (ret >= 0);
10937 mutex_lock(&ctx->uring_lock);
10938
10939 if (ret)
10940 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10941 return ret;
10942}
10943
Jens Axboeedafcce2019-01-09 09:16:05 -070010944static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10945 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010946 __releases(ctx->uring_lock)
10947 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010948{
10949 int ret;
10950
Jens Axboe35fa71a2019-04-22 10:23:23 -060010951 /*
10952 * We're inside the ring mutex, if the ref is already dying, then
10953 * someone else killed the ctx or is already going through
10954 * io_uring_register().
10955 */
10956 if (percpu_ref_is_dying(&ctx->refs))
10957 return -ENXIO;
10958
Pavel Begunkov75c40212021-04-15 13:07:40 +010010959 if (ctx->restricted) {
10960 if (opcode >= IORING_REGISTER_LAST)
10961 return -EINVAL;
10962 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10963 if (!test_bit(opcode, ctx->restrictions.register_op))
10964 return -EACCES;
10965 }
10966
Jens Axboe071698e2020-01-28 10:04:42 -070010967 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010968 ret = io_ctx_quiesce(ctx);
10969 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010970 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010971 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010972
10973 switch (opcode) {
10974 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010975 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010976 break;
10977 case IORING_UNREGISTER_BUFFERS:
10978 ret = -EINVAL;
10979 if (arg || nr_args)
10980 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010981 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010982 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010983 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010984 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010985 break;
10986 case IORING_UNREGISTER_FILES:
10987 ret = -EINVAL;
10988 if (arg || nr_args)
10989 break;
10990 ret = io_sqe_files_unregister(ctx);
10991 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010992 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010993 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010994 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010995 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010996 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010997 ret = -EINVAL;
10998 if (nr_args != 1)
10999 break;
11000 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070011001 if (ret)
11002 break;
11003 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
11004 ctx->eventfd_async = 1;
11005 else
11006 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060011007 break;
11008 case IORING_UNREGISTER_EVENTFD:
11009 ret = -EINVAL;
11010 if (arg || nr_args)
11011 break;
11012 ret = io_eventfd_unregister(ctx);
11013 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070011014 case IORING_REGISTER_PROBE:
11015 ret = -EINVAL;
11016 if (!arg || nr_args > 256)
11017 break;
11018 ret = io_probe(ctx, arg, nr_args);
11019 break;
Jens Axboe071698e2020-01-28 10:04:42 -070011020 case IORING_REGISTER_PERSONALITY:
11021 ret = -EINVAL;
11022 if (arg || nr_args)
11023 break;
11024 ret = io_register_personality(ctx);
11025 break;
11026 case IORING_UNREGISTER_PERSONALITY:
11027 ret = -EINVAL;
11028 if (arg)
11029 break;
11030 ret = io_unregister_personality(ctx, nr_args);
11031 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020011032 case IORING_REGISTER_ENABLE_RINGS:
11033 ret = -EINVAL;
11034 if (arg || nr_args)
11035 break;
11036 ret = io_register_enable_rings(ctx);
11037 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020011038 case IORING_REGISTER_RESTRICTIONS:
11039 ret = io_register_restrictions(ctx, arg, nr_args);
11040 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010011041 case IORING_REGISTER_FILES2:
11042 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010011043 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010011044 case IORING_REGISTER_FILES_UPDATE2:
11045 ret = io_register_rsrc_update(ctx, arg, nr_args,
11046 IORING_RSRC_FILE);
11047 break;
11048 case IORING_REGISTER_BUFFERS2:
11049 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
11050 break;
11051 case IORING_REGISTER_BUFFERS_UPDATE:
11052 ret = io_register_rsrc_update(ctx, arg, nr_args,
11053 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010011054 break;
Jens Axboefe764212021-06-17 10:19:54 -060011055 case IORING_REGISTER_IOWQ_AFF:
11056 ret = -EINVAL;
11057 if (!arg || !nr_args)
11058 break;
11059 ret = io_register_iowq_aff(ctx, arg, nr_args);
11060 break;
11061 case IORING_UNREGISTER_IOWQ_AFF:
11062 ret = -EINVAL;
11063 if (arg || nr_args)
11064 break;
11065 ret = io_unregister_iowq_aff(ctx);
11066 break;
Jens Axboe2e480052021-08-27 11:33:19 -060011067 case IORING_REGISTER_IOWQ_MAX_WORKERS:
11068 ret = -EINVAL;
11069 if (!arg || nr_args != 2)
11070 break;
11071 ret = io_register_iowq_max_workers(ctx, arg);
11072 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070011073 default:
11074 ret = -EINVAL;
11075 break;
11076 }
11077
Jens Axboe071698e2020-01-28 10:04:42 -070011078 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070011079 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070011080 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060011081 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070011082 }
Jens Axboeedafcce2019-01-09 09:16:05 -070011083 return ret;
11084}
11085
11086SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
11087 void __user *, arg, unsigned int, nr_args)
11088{
11089 struct io_ring_ctx *ctx;
11090 long ret = -EBADF;
11091 struct fd f;
11092
11093 f = fdget(fd);
11094 if (!f.file)
11095 return -EBADF;
11096
11097 ret = -EOPNOTSUPP;
11098 if (f.file->f_op != &io_uring_fops)
11099 goto out_fput;
11100
11101 ctx = f.file->private_data;
11102
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000011103 io_run_task_work();
11104
Jens Axboeedafcce2019-01-09 09:16:05 -070011105 mutex_lock(&ctx->uring_lock);
11106 ret = __io_uring_register(ctx, opcode, arg, nr_args);
11107 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020011108 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
11109 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070011110out_fput:
11111 fdput(f);
11112 return ret;
11113}
11114
Jens Axboe2b188cc2019-01-07 10:46:33 -070011115static int __init io_uring_init(void)
11116{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011117#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
11118 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
11119 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
11120} while (0)
11121
11122#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
11123 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
11124 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
11125 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
11126 BUILD_BUG_SQE_ELEM(1, __u8, flags);
11127 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
11128 BUILD_BUG_SQE_ELEM(4, __s32, fd);
11129 BUILD_BUG_SQE_ELEM(8, __u64, off);
11130 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
11131 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011132 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011133 BUILD_BUG_SQE_ELEM(24, __u32, len);
11134 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
11135 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
11136 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
11137 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080011138 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
11139 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011140 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
11141 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
11142 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
11143 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
11144 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
11145 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
11146 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
11147 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011148 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011149 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
11150 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011151 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011152 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011153 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010011154 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011155
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011156 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
11157 sizeof(struct io_uring_rsrc_update));
11158 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
11159 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010011160
11161 /* ->buf_index is u16 */
11162 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
11163
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011164 /* should fit into one byte */
11165 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
Pavel Begunkov68fe2562021-09-15 12:03:38 +010011166 BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
11167 BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011168
Jens Axboed3656342019-12-18 09:50:26 -070011169 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080011170 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011171
Jens Axboe91f245d2021-02-09 13:48:50 -070011172 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
11173 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070011174 return 0;
11175};
11176__initcall(io_uring_init);