blob: f01263a31ea4eb56fd669585a4bda26517482a97 [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 Begunkov04c76b42021-11-10 15:49:32 +0000109 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
110 IOSQE_CQE_SKIP_SUCCESS)
Pavel Begunkov68fe2562021-09-15 12:03:38 +0100111
112#define SQE_VALID_FLAGS (SQE_COMMON_FLAGS|IOSQE_BUFFER_SELECT|IOSQE_IO_DRAIN)
113
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 Begunkovb52ecf82021-06-14 23:37:21 +0100342 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700343
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100344 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100345 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100346 struct mutex uring_lock;
347
Hristo Venev75b28af2019-08-26 17:23:46 +0000348 /*
349 * Ring buffer of indices into array of io_uring_sqe, which is
350 * mmapped by the application using the IORING_OFF_SQES offset.
351 *
352 * This indirection could e.g. be used to assign fixed
353 * io_uring_sqe entries to operations and only submit them to
354 * the queue when needed.
355 *
356 * The kernel modifies neither the indices array nor the entries
357 * array.
358 */
359 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100360 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700361 unsigned cached_sq_head;
362 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600363 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100364
365 /*
366 * Fixed resources fast path, should be accessed only under
367 * uring_lock, and updated through io_uring_register(2)
368 */
369 struct io_rsrc_node *rsrc_node;
Pavel Begunkovab409402021-10-09 23:14:41 +0100370 int rsrc_cached_refs;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100371 struct io_file_table file_table;
372 unsigned nr_user_files;
373 unsigned nr_user_bufs;
374 struct io_mapped_ubuf **user_bufs;
375
376 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600377 struct list_head timeout_list;
Pavel Begunkovef9dd632021-08-28 19:54:38 -0600378 struct list_head ltimeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700379 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100380 struct xarray io_buffers;
381 struct xarray personalities;
382 u32 pers_next;
383 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700384 } ____cacheline_aligned_in_smp;
385
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100386 /* IRQ completion list, under ->completion_lock */
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +0100387 struct io_wq_work_list locked_free_list;
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100388 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700389
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100390 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600391 struct io_sq_data *sq_data; /* if using sq thread polling */
392
Jens Axboe90554202020-09-03 12:12:41 -0600393 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600394 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000395
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100396 unsigned long check_cq_overflow;
397
Jens Axboe206aefd2019-11-07 18:27:42 -0700398 struct {
399 unsigned cached_cq_tail;
400 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700401 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100402 struct wait_queue_head cq_wait;
403 unsigned cq_extra;
404 atomic_t cq_timeouts;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100405 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700406 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700407
408 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700409 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700410
Jens Axboe89850fc2021-08-10 15:11:51 -0600411 spinlock_t timeout_lock;
412
Jens Axboedef596e2019-01-09 08:59:42 -0700413 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300414 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700415 * io_uring instances that don't use IORING_SETUP_SQPOLL.
416 * For SQPOLL, only the single threaded io_sq_thread() will
417 * manipulate the list, hence no extra locking is needed there.
418 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +0100419 struct io_wq_work_list iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700420 struct hlist_head *cancel_hash;
421 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800422 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700423 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600424
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200425 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700426
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100427 /* slow path rsrc auxilary data, used by update/register */
428 struct {
429 struct io_rsrc_node *rsrc_backup_node;
430 struct io_mapped_ubuf *dummy_ubuf;
431 struct io_rsrc_data *file_data;
432 struct io_rsrc_data *buf_data;
433
434 struct delayed_work rsrc_put_work;
435 struct llist_head rsrc_put_llist;
436 struct list_head rsrc_ref_list;
437 spinlock_t rsrc_ref_lock;
438 };
439
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700440 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100441 struct {
442 #if defined(CONFIG_UNIX)
443 struct socket *ring_sock;
444 #endif
445 /* hashed buffered write serialization */
446 struct io_wq_hash *hash_map;
447
448 /* Only used for accounting purposes */
449 struct user_struct *user;
450 struct mm_struct *mm_account;
451
452 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100453 struct llist_head fallback_llist;
454 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100455 struct work_struct exit_work;
456 struct list_head tctx_list;
457 struct completion ref_comp;
Pavel Begunkove139a1e2021-10-19 23:43:46 +0100458 u32 iowq_limits[2];
459 bool iowq_limits_set;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100460 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461};
462
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100463struct io_uring_task {
464 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100465 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100466 struct xarray xa;
467 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100468 const struct io_ring_ctx *last;
469 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100470 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100471 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100472 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100473
474 spinlock_t task_lock;
475 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100476 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100477 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100478};
479
Jens Axboe09bb8392019-03-13 12:39:28 -0600480/*
481 * First field must be the file pointer in all the
482 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
483 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700484struct io_poll_iocb {
485 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000486 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700487 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600488 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700489 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700490 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700491};
492
Pavel Begunkov9d805892021-04-13 02:58:40 +0100493struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000494 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100495 u64 old_user_data;
496 u64 new_user_data;
497 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600498 bool update_events;
499 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000500};
501
Jens Axboeb5dba592019-12-11 14:02:38 -0700502struct io_close {
503 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700504 int fd;
Pavel Begunkov7df778b2021-09-24 20:04:29 +0100505 u32 file_slot;
Jens Axboeb5dba592019-12-11 14:02:38 -0700506};
507
Jens Axboead8a48a2019-11-15 08:49:11 -0700508struct io_timeout_data {
509 struct io_kiocb *req;
510 struct hrtimer timer;
511 struct timespec64 ts;
512 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600513 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700514};
515
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700516struct io_accept {
517 struct file *file;
518 struct sockaddr __user *addr;
519 int __user *addr_len;
520 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100521 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600522 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700523};
524
525struct io_sync {
526 struct file *file;
527 loff_t len;
528 loff_t off;
529 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700530 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700531};
532
Jens Axboefbf23842019-12-17 18:45:56 -0700533struct io_cancel {
534 struct file *file;
535 u64 addr;
536};
537
Jens Axboeb29472e2019-12-17 18:50:29 -0700538struct io_timeout {
539 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300540 u32 off;
541 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300542 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000543 /* head of the link, used by linked timeouts only */
544 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600545 /* for linked completions */
546 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700547};
548
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100549struct io_timeout_rem {
550 struct file *file;
551 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000552
553 /* timeout update */
554 struct timespec64 ts;
555 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600556 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100557};
558
Jens Axboe9adbd452019-12-20 08:45:55 -0700559struct io_rw {
560 /* NOTE: kiocb has the file as the first member, so don't do it here */
561 struct kiocb kiocb;
562 u64 addr;
563 u64 len;
564};
565
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700566struct io_connect {
567 struct file *file;
568 struct sockaddr __user *addr;
569 int addr_len;
570};
571
Jens Axboee47293f2019-12-20 08:58:21 -0700572struct io_sr_msg {
573 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700574 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100575 struct compat_msghdr __user *umsg_compat;
576 struct user_msghdr __user *umsg;
577 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700578 };
Jens Axboee47293f2019-12-20 08:58:21 -0700579 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700580 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700581 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700582};
583
Jens Axboe15b71ab2019-12-11 11:20:36 -0700584struct io_open {
585 struct file *file;
586 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100587 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700588 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700589 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600590 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700591};
592
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000593struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700594 struct file *file;
595 u64 arg;
596 u32 nr_args;
597 u32 offset;
598};
599
Jens Axboe4840e412019-12-25 22:03:45 -0700600struct io_fadvise {
601 struct file *file;
602 u64 offset;
603 u32 len;
604 u32 advice;
605};
606
Jens Axboec1ca7572019-12-25 22:18:28 -0700607struct io_madvise {
608 struct file *file;
609 u64 addr;
610 u32 len;
611 u32 advice;
612};
613
Jens Axboe3e4827b2020-01-08 15:18:09 -0700614struct io_epoll {
615 struct file *file;
616 int epfd;
617 int op;
618 int fd;
619 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700620};
621
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300622struct io_splice {
623 struct file *file_out;
624 struct file *file_in;
625 loff_t off_out;
626 loff_t off_in;
627 u64 len;
628 unsigned int flags;
629};
630
Jens Axboeddf0322d2020-02-23 16:41:33 -0700631struct io_provide_buf {
632 struct file *file;
633 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100634 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700635 __u32 bgid;
636 __u16 nbufs;
637 __u16 bid;
638};
639
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700640struct io_statx {
641 struct file *file;
642 int dfd;
643 unsigned int mask;
644 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700645 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700646 struct statx __user *buffer;
647};
648
Jens Axboe36f4fa62020-09-05 11:14:22 -0600649struct io_shutdown {
650 struct file *file;
651 int how;
652};
653
Jens Axboe80a261f2020-09-28 14:23:58 -0600654struct io_rename {
655 struct file *file;
656 int old_dfd;
657 int new_dfd;
658 struct filename *oldpath;
659 struct filename *newpath;
660 int flags;
661};
662
Jens Axboe14a11432020-09-28 14:27:37 -0600663struct io_unlink {
664 struct file *file;
665 int dfd;
666 int flags;
667 struct filename *filename;
668};
669
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700670struct io_mkdir {
671 struct file *file;
672 int dfd;
673 umode_t mode;
674 struct filename *filename;
675};
676
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700677struct io_symlink {
678 struct file *file;
679 int new_dfd;
680 struct filename *oldpath;
681 struct filename *newpath;
682};
683
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700684struct io_hardlink {
685 struct file *file;
686 int old_dfd;
687 int new_dfd;
688 struct filename *oldpath;
689 struct filename *newpath;
690 int flags;
691};
692
Jens Axboef499a022019-12-02 16:28:46 -0700693struct io_async_connect {
694 struct sockaddr_storage address;
695};
696
Jens Axboe03b12302019-12-02 18:50:25 -0700697struct io_async_msghdr {
698 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000699 /* points to an allocated iov, if NULL we use fast_iov instead */
700 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700701 struct sockaddr __user *uaddr;
702 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700703 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700704};
705
Pavel Begunkov538941e2021-10-14 16:10:15 +0100706struct io_rw_state {
Jens Axboeff6165b2020-08-13 09:47:43 -0600707 struct iov_iter iter;
Jens Axboecd658692021-09-10 11:19:14 -0600708 struct iov_iter_state iter_state;
Pavel Begunkovc88598a2021-10-14 16:10:16 +0100709 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov538941e2021-10-14 16:10:15 +0100710};
711
712struct io_async_rw {
713 struct io_rw_state s;
714 const struct iovec *free_iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -0600715 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600716 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700717};
718
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300719enum {
720 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
721 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
722 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
723 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
724 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700725 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000726 REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300727
Pavel Begunkovdddca222021-04-27 16:13:52 +0100728 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100729 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300730 REQ_F_INFLIGHT_BIT,
731 REQ_F_CUR_POS_BIT,
732 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300733 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300734 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700735 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700736 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000737 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600738 REQ_F_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100739 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100740 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100741 REQ_F_ARM_LTIMEOUT_BIT,
Pavel Begunkovd886e182021-10-04 20:02:56 +0100742 REQ_F_ASYNC_DATA_BIT,
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000743 REQ_F_SKIP_LINK_CQES_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700744 /* keep async read/write and isreg together and in order */
Pavel Begunkov35645ac2021-10-17 00:07:09 +0100745 REQ_F_SUPPORT_NOWAIT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700746 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700747
748 /* not a real bit, just to check we're not overflowing the space */
749 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300750};
751
752enum {
753 /* ctx owns file */
754 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
755 /* drain existing IO first */
756 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
757 /* linked sqes */
758 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
759 /* doesn't sever on completion < 0 */
760 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
761 /* IOSQE_ASYNC */
762 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700763 /* IOSQE_BUFFER_SELECT */
764 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000765 /* IOSQE_CQE_SKIP_SUCCESS */
766 REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300767
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300768 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100769 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000770 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300771 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
772 /* read/write uses file position */
773 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
774 /* must not punt to workers */
775 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100776 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300777 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300778 /* needs cleanup */
779 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700780 /* already went through poll handler */
781 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700782 /* buffer already selected */
783 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000784 /* completion is deferred through io_comp_state */
785 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600786 /* caller should reissue async */
787 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov35645ac2021-10-17 00:07:09 +0100788 /* supports async reads/writes */
789 REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700790 /* regular file */
791 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100792 /* has creds assigned */
793 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100794 /* skip refcounting if not set */
795 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100796 /* there is a linked timeout that has to be armed */
797 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Pavel Begunkovd886e182021-10-04 20:02:56 +0100798 /* ->async_data allocated */
799 REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT),
Pavel Begunkov04c76b42021-11-10 15:49:32 +0000800 /* don't post CQEs while failing linked requests */
801 REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700802};
803
804struct async_poll {
805 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600806 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300807};
808
Pavel Begunkovf237c302021-08-18 12:42:46 +0100809typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100810
Jens Axboe7cbf1722021-02-10 00:03:20 +0000811struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100812 union {
813 struct io_wq_work_node node;
814 struct llist_node fallback_node;
815 };
816 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000817};
818
Pavel Begunkov992da012021-06-10 16:37:37 +0100819enum {
820 IORING_RSRC_FILE = 0,
821 IORING_RSRC_BUFFER = 1,
822};
823
Jens Axboe09bb8392019-03-13 12:39:28 -0600824/*
825 * NOTE! Each of the iocb union members has the file pointer
826 * as the first entry in their struct definition. So you can
827 * access the file pointer through any of the sub-structs,
828 * or directly as just 'ki_filp' in this struct.
829 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700830struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700831 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600832 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700833 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700834 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100835 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700836 struct io_accept accept;
837 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700838 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700839 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100840 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700841 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700842 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700843 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700844 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000845 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700846 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700847 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700848 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300849 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700850 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700851 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600852 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600853 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600854 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700855 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700856 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700857 struct io_hardlink hardlink;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700858 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859
Jens Axboed625c6e2019-12-17 19:53:05 -0700860 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800861 /* polled IO has completed */
862 u8 iopoll_completed;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700863 u16 buf_index;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100864 unsigned int flags;
865
866 u64 user_data;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300867 u32 result;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100868 u32 cflags;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700869
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300870 struct io_ring_ctx *ctx;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300871 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700872
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000873 struct percpu_ref *fixed_rsrc_refs;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100874 /* store used ubuf, so we can prevent reloading */
875 struct io_mapped_ubuf *imu;
Jens Axboed7718a92020-02-14 22:23:12 -0700876
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100877 /* used by request caches, completion batching and iopoll */
Pavel Begunkovef05d9e2021-09-24 22:00:02 +0100878 struct io_wq_work_node comp_list;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +0100879 atomic_t refs;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100880 struct io_kiocb *link;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100881 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300882 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
883 struct hlist_node hash_node;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100884 /* internal polling, see IORING_FEAT_FAST_POLL */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300885 struct async_poll *apoll;
Pavel Begunkovd886e182021-10-04 20:02:56 +0100886 /* opcode allocated if it needs to store data for async defer */
887 void *async_data;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300888 struct io_wq_work work;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100889 /* custom credentials, valid IFF REQ_F_CREDS is set */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100890 const struct cred *creds;
Pavel Begunkov7e3709d2021-10-04 20:02:46 +0100891 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
Pavel Begunkov30d51dd2021-10-01 18:07:03 +0100892 struct io_buffer *kbuf;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700893};
894
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000895struct io_tctx_node {
896 struct list_head ctx_node;
897 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000898 struct io_ring_ctx *ctx;
899};
900
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300901struct io_defer_entry {
902 struct list_head list;
903 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300904 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300905};
906
Jens Axboed3656342019-12-18 09:50:26 -0700907struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700908 /* needs req->file assigned */
909 unsigned needs_file : 1;
Pavel Begunkov6d634162021-10-06 16:06:46 +0100910 /* should block plug */
911 unsigned plug : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700912 /* hash wq insertion if file is a regular file */
913 unsigned hash_reg_file : 1;
914 /* unbound wq insertion if file is a non-regular file */
915 unsigned unbound_nonreg_file : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700916 /* set if opcode supports polled "wait" */
917 unsigned pollin : 1;
918 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700919 /* op supports buffer selection */
920 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000921 /* do prep async if is going to be punted */
922 unsigned needs_async_setup : 1;
Pavel Begunkov6d634162021-10-06 16:06:46 +0100923 /* opcode is not supported by this kernel */
924 unsigned not_supported : 1;
Paul Moore5bd21822021-02-16 19:46:48 -0500925 /* skip auditing */
926 unsigned audit_skip : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700927 /* size of async data needed, if any */
928 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700929};
930
Jens Axboe09186822020-10-13 15:01:40 -0600931static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300932 [IORING_OP_NOP] = {},
933 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700934 .needs_file = 1,
935 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700936 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700937 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000938 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600939 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500940 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700941 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700942 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300943 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700944 .needs_file = 1,
945 .hash_reg_file = 1,
946 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700947 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000948 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600949 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500950 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700951 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700952 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300953 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700954 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500955 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700958 .needs_file = 1,
959 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700960 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600961 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500962 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700963 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700964 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300965 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700966 .needs_file = 1,
967 .hash_reg_file = 1,
968 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700969 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600970 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500971 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700972 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700973 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300974 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700975 .needs_file = 1,
976 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500977 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700978 },
Paul Moore5bd21822021-02-16 19:46:48 -0500979 [IORING_OP_POLL_REMOVE] = {
980 .audit_skip = 1,
981 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300982 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700983 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500984 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700985 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300986 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700987 .needs_file = 1,
988 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700989 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000990 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700991 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700992 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300993 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700994 .needs_file = 1,
995 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700996 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700997 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000998 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700999 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -07001000 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001001 [IORING_OP_TIMEOUT] = {
Paul Moore5bd21822021-02-16 19:46:48 -05001002 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001003 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -07001004 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00001005 [IORING_OP_TIMEOUT_REMOVE] = {
1006 /* used by timeout updates' prep() */
Paul Moore5bd21822021-02-16 19:46:48 -05001007 .audit_skip = 1,
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00001008 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001009 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001010 .needs_file = 1,
1011 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001012 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -07001013 },
Paul Moore5bd21822021-02-16 19:46:48 -05001014 [IORING_OP_ASYNC_CANCEL] = {
1015 .audit_skip = 1,
1016 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001017 [IORING_OP_LINK_TIMEOUT] = {
Paul Moore5bd21822021-02-16 19:46:48 -05001018 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001019 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -07001020 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001021 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001022 .needs_file = 1,
1023 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001024 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +00001025 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001026 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -07001027 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001028 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -07001029 .needs_file = 1,
1030 },
Jens Axboe44526be2021-02-15 13:32:18 -07001031 [IORING_OP_OPENAT] = {},
1032 [IORING_OP_CLOSE] = {},
Paul Moore5bd21822021-02-16 19:46:48 -05001033 [IORING_OP_FILES_UPDATE] = {
1034 .audit_skip = 1,
1035 },
1036 [IORING_OP_STATX] = {
1037 .audit_skip = 1,
1038 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001039 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001040 .needs_file = 1,
1041 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001042 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001043 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001044 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001045 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001046 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001047 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001048 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001049 .needs_file = 1,
Jens Axboe7b3188e2021-08-30 19:37:41 -06001050 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -07001051 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001052 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001053 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001054 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001055 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001056 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001057 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001058 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001059 .audit_skip = 1,
Jens Axboe4840e412019-12-25 22:03:45 -07001060 },
Jens Axboe44526be2021-02-15 13:32:18 -07001061 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001062 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001063 .needs_file = 1,
1064 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001065 .pollout = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001066 .audit_skip = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001067 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001068 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001069 .needs_file = 1,
1070 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001071 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001072 .buffer_select = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001073 .audit_skip = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001074 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001075 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001076 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001077 [IORING_OP_EPOLL_CTL] = {
1078 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001079 .audit_skip = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001080 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001081 [IORING_OP_SPLICE] = {
1082 .needs_file = 1,
1083 .hash_reg_file = 1,
1084 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001085 .audit_skip = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001086 },
Paul Moore5bd21822021-02-16 19:46:48 -05001087 [IORING_OP_PROVIDE_BUFFERS] = {
1088 .audit_skip = 1,
1089 },
1090 [IORING_OP_REMOVE_BUFFERS] = {
1091 .audit_skip = 1,
1092 },
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001093 [IORING_OP_TEE] = {
1094 .needs_file = 1,
1095 .hash_reg_file = 1,
1096 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001097 .audit_skip = 1,
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001098 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001099 [IORING_OP_SHUTDOWN] = {
1100 .needs_file = 1,
1101 },
Jens Axboe44526be2021-02-15 13:32:18 -07001102 [IORING_OP_RENAMEAT] = {},
1103 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001104 [IORING_OP_MKDIRAT] = {},
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07001105 [IORING_OP_SYMLINKAT] = {},
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07001106 [IORING_OP_LINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001107};
1108
Pavel Begunkov0756a862021-08-15 10:40:25 +01001109/* requests with any of those set should undergo io_disarm_next() */
1110#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1111
Pavel Begunkov7a612352021-03-09 00:37:59 +00001112static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001113static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001114static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1115 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001116 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001117static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001118
Pavel Begunkov913a5712021-11-10 15:49:31 +00001119static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags);
1120
Jackie Liuec9c02a2019-11-08 23:50:36 +08001121static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001122static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001123static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001124static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001125static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001126 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001127 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001128static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001129static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001130 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001131static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001132static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001133
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001134static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkovc4501782021-09-08 16:40:52 +01001135static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001136static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001137
Pavel Begunkovb9445592021-08-25 12:25:45 +01001138static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1139 unsigned int issue_flags, u32 slot_index);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01001140static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags);
1141
Pavel Begunkovf1042b62021-08-28 19:54:39 -06001142static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
Pavel Begunkovb9445592021-08-25 12:25:45 +01001143
Jens Axboe2b188cc2019-01-07 10:46:33 -07001144static struct kmem_cache *req_cachep;
1145
Jens Axboe09186822020-10-13 15:01:40 -06001146static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001147
1148struct sock *io_uring_get_socket(struct file *file)
1149{
1150#if defined(CONFIG_UNIX)
1151 if (file->f_op == &io_uring_fops) {
1152 struct io_ring_ctx *ctx = file->private_data;
1153
1154 return ctx->ring_sock->sk;
1155 }
1156#endif
1157 return NULL;
1158}
1159EXPORT_SYMBOL(io_uring_get_socket);
1160
Pavel Begunkovf237c302021-08-18 12:42:46 +01001161static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1162{
1163 if (!*locked) {
1164 mutex_lock(&ctx->uring_lock);
1165 *locked = true;
1166 }
1167}
1168
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001169#define io_for_each_link(pos, head) \
1170 for (pos = (head); pos; pos = pos->link)
1171
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001172/*
1173 * Shamelessly stolen from the mm implementation of page reference checking,
1174 * see commit f958d7b528b1 for details.
1175 */
1176#define req_ref_zero_or_close_to_overflow(req) \
1177 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1178
1179static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1180{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001181 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001182 return atomic_inc_not_zero(&req->refs);
1183}
1184
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001185static inline bool req_ref_put_and_test(struct io_kiocb *req)
1186{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001187 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1188 return true;
1189
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001190 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1191 return atomic_dec_and_test(&req->refs);
1192}
1193
1194static inline void req_ref_put(struct io_kiocb *req)
1195{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001196 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001197 WARN_ON_ONCE(req_ref_put_and_test(req));
1198}
1199
1200static inline void req_ref_get(struct io_kiocb *req)
1201{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001202 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001203 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1204 atomic_inc(&req->refs);
1205}
1206
Pavel Begunkovc4501782021-09-08 16:40:52 +01001207static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
1208{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001209 if (!wq_list_empty(&ctx->submit_state.compl_reqs))
Pavel Begunkovc4501782021-09-08 16:40:52 +01001210 __io_submit_flush_completions(ctx);
1211}
1212
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001213static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001214{
1215 if (!(req->flags & REQ_F_REFCOUNT)) {
1216 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001217 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001218 }
1219}
1220
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001221static inline void io_req_set_refcount(struct io_kiocb *req)
1222{
1223 __io_req_set_refcount(req, 1);
1224}
1225
Pavel Begunkovab409402021-10-09 23:14:41 +01001226#define IO_RSRC_REF_BATCH 100
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001227
Pavel Begunkovab409402021-10-09 23:14:41 +01001228static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
1229 struct io_ring_ctx *ctx)
1230 __must_hold(&ctx->uring_lock)
1231{
1232 struct percpu_ref *ref = req->fixed_rsrc_refs;
1233
1234 if (ref) {
1235 if (ref == &ctx->rsrc_node->refs)
1236 ctx->rsrc_cached_refs++;
1237 else
1238 percpu_ref_put(ref);
1239 }
1240}
1241
1242static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx)
1243{
1244 if (req->fixed_rsrc_refs)
1245 percpu_ref_put(req->fixed_rsrc_refs);
1246}
1247
1248static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
1249 __must_hold(&ctx->uring_lock)
1250{
1251 if (ctx->rsrc_cached_refs) {
1252 percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs);
1253 ctx->rsrc_cached_refs = 0;
1254 }
1255}
1256
1257static void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
1258 __must_hold(&ctx->uring_lock)
1259{
1260 ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
1261 percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH);
1262}
1263
Pavel Begunkova46be972021-10-09 23:14:40 +01001264static inline void io_req_set_rsrc_node(struct io_kiocb *req,
1265 struct io_ring_ctx *ctx)
Jens Axboec40f6372020-06-25 15:39:59 -06001266{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001267 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001268 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Pavel Begunkovab409402021-10-09 23:14:41 +01001269 ctx->rsrc_cached_refs--;
1270 if (unlikely(ctx->rsrc_cached_refs < 0))
1271 io_rsrc_refs_refill(ctx);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001272 }
1273}
1274
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001275static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1276{
1277 bool got = percpu_ref_tryget(ref);
1278
1279 /* already at zero, wait for ->release() */
1280 if (!got)
1281 wait_for_completion(compl);
1282 percpu_ref_resurrect(ref);
1283 if (got)
1284 percpu_ref_put(ref);
1285}
1286
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001287static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1288 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001289{
1290 struct io_kiocb *req;
1291
Pavel Begunkov68207682021-03-22 01:58:25 +00001292 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001293 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001294 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001295 return true;
1296
1297 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001298 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001299 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001300 }
1301 return false;
1302}
1303
Pavel Begunkovd886e182021-10-04 20:02:56 +01001304static inline bool req_has_async_data(struct io_kiocb *req)
1305{
1306 return req->flags & REQ_F_ASYNC_DATA;
1307}
1308
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001309static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001310{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001311 req->flags |= REQ_F_FAIL;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001312 if (req->flags & REQ_F_CQE_SKIP) {
1313 req->flags &= ~REQ_F_CQE_SKIP;
1314 req->flags |= REQ_F_SKIP_LINK_CQES;
1315 }
Jens Axboec40f6372020-06-25 15:39:59 -06001316}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001317
Hao Xua8295b92021-08-27 17:46:09 +08001318static inline void req_fail_link_node(struct io_kiocb *req, int res)
1319{
1320 req_set_fail(req);
1321 req->result = res;
1322}
1323
Pavel Begunkovc0724812021-10-04 20:02:54 +01001324static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001325{
1326 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1327
Jens Axboe0f158b42020-05-14 17:18:39 -06001328 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001329}
1330
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001331static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1332{
1333 return !req->timeout.off;
1334}
1335
Pavel Begunkovc0724812021-10-04 20:02:54 +01001336static __cold void io_fallback_req_func(struct work_struct *work)
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001337{
1338 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1339 fallback_work.work);
1340 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1341 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001342 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001343
1344 percpu_ref_get(&ctx->refs);
1345 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001346 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001347
Pavel Begunkovf237c302021-08-18 12:42:46 +01001348 if (locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01001349 io_submit_flush_completions(ctx);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001350 mutex_unlock(&ctx->uring_lock);
1351 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001352 percpu_ref_put(&ctx->refs);
1353}
1354
Pavel Begunkovc0724812021-10-04 20:02:54 +01001355static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001356{
1357 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001358 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001359
1360 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1361 if (!ctx)
1362 return NULL;
1363
Jens Axboe78076bb2019-12-04 19:56:40 -07001364 /*
1365 * Use 5 bits less than the max cq entries, that should give us around
1366 * 32 entries per hash list if totally full and uniformly spread.
1367 */
1368 hash_bits = ilog2(p->cq_entries);
1369 hash_bits -= 5;
1370 if (hash_bits <= 0)
1371 hash_bits = 1;
1372 ctx->cancel_hash_bits = hash_bits;
1373 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1374 GFP_KERNEL);
1375 if (!ctx->cancel_hash)
1376 goto err;
1377 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1378
Pavel Begunkov62248432021-04-28 13:11:29 +01001379 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1380 if (!ctx->dummy_ubuf)
1381 goto err;
1382 /* set invalid range, so io_import_fixed() fails meeting it */
1383 ctx->dummy_ubuf->ubuf = -1UL;
1384
Roman Gushchin21482892019-05-07 10:01:48 -07001385 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001386 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1387 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001388
1389 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001390 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001391 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001392 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001393 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001394 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001395 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001396 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001397 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001399 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01001400 INIT_WQ_LIST(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001401 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001402 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001403 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001404 spin_lock_init(&ctx->rsrc_ref_lock);
1405 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001406 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1407 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001408 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001409 ctx->submit_state.free_list.next = NULL;
1410 INIT_WQ_LIST(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001411 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01001412 INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001413 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001414err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001415 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001416 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001417 kfree(ctx);
1418 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001419}
1420
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001421static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1422{
1423 struct io_rings *r = ctx->rings;
1424
1425 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1426 ctx->cq_extra--;
1427}
1428
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001429static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001430{
Jens Axboe2bc99302020-07-09 09:43:27 -06001431 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1432 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001433
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001434 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001435 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001436
Bob Liu9d858b22019-11-13 18:06:25 +08001437 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001438}
1439
Pavel Begunkov35645ac2021-10-17 00:07:09 +01001440#define FFS_NOWAIT 0x1UL
1441#define FFS_ISREG 0x2UL
1442#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG)
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001443
1444static inline bool io_req_ffs_set(struct io_kiocb *req)
1445{
Pavel Begunkov35645ac2021-10-17 00:07:09 +01001446 return req->flags & REQ_F_FIXED_FILE;
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001447}
1448
Pavel Begunkovc0724812021-10-04 20:02:54 +01001449static inline void io_req_track_inflight(struct io_kiocb *req)
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001450{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001451 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001452 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001453 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001454 }
1455}
1456
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001457static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1458{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001459 if (WARN_ON_ONCE(!req->link))
1460 return NULL;
1461
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001462 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1463 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001464
1465 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001466 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001467 __io_req_set_refcount(req->link, 2);
1468 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001469}
1470
1471static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1472{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001473 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001474 return NULL;
1475 return __io_prep_linked_timeout(req);
1476}
1477
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001478static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001479{
Jens Axboed3656342019-12-18 09:50:26 -07001480 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001481 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001482
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001483 if (!(req->flags & REQ_F_CREDS)) {
1484 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001485 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001486 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001487
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001488 req->work.list.next = NULL;
1489 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001490 if (req->flags & REQ_F_FORCE_ASYNC)
1491 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1492
Jens Axboed3656342019-12-18 09:50:26 -07001493 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001494 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001495 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001496 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001497 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001498 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001499 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001500
1501 switch (req->opcode) {
1502 case IORING_OP_SPLICE:
1503 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001504 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1505 req->work.flags |= IO_WQ_WORK_UNBOUND;
1506 break;
1507 }
Jens Axboe561fb042019-10-24 07:25:42 -06001508}
1509
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001510static void io_prep_async_link(struct io_kiocb *req)
1511{
1512 struct io_kiocb *cur;
1513
Pavel Begunkov44eff402021-07-26 14:14:31 +01001514 if (req->flags & REQ_F_LINK_TIMEOUT) {
1515 struct io_ring_ctx *ctx = req->ctx;
1516
Jens Axboe79ebeae2021-08-10 15:18:27 -06001517 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001518 io_for_each_link(cur, req)
1519 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001520 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001521 } else {
1522 io_for_each_link(cur, req)
1523 io_prep_async_work(cur);
1524 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001525}
1526
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001527static inline void io_req_add_compl_list(struct io_kiocb *req)
1528{
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00001529 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001530 struct io_submit_state *state = &req->ctx->submit_state;
1531
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00001532 if (!(req->flags & REQ_F_CQE_SKIP))
1533 ctx->submit_state.flush_cqes = true;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01001534 wq_list_add_tail(&req->comp_list, &state->compl_reqs);
1535}
1536
Arnd Bergmann00169242021-10-19 17:34:53 +02001537static void io_queue_async_work(struct io_kiocb *req, bool *dont_use)
Jens Axboe561fb042019-10-24 07:25:42 -06001538{
Jackie Liua197f662019-11-08 08:09:12 -07001539 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001540 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001541 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001542
Jens Axboe3bfe6102021-02-16 14:15:30 -07001543 BUG_ON(!tctx);
1544 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001545
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001546 /* init ->work of the whole link before punting */
1547 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001548
1549 /*
1550 * Not expected to happen, but if we do have a bug where this _can_
1551 * happen, catch it here and ensure the request is marked as
1552 * canceled. That will make io-wq go through the usual work cancel
1553 * procedure rather than attempt to run this request (or create a new
1554 * worker for it).
1555 */
1556 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1557 req->work.flags |= IO_WQ_WORK_CANCEL;
1558
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001559 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1560 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001561 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001562 if (link)
1563 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001564}
1565
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001566static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001567 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001568 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001569{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001570 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001571
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001572 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001573 if (status)
1574 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001575 atomic_set(&req->ctx->cq_timeouts,
1576 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001577 list_del_init(&req->timeout.list);
Pavel Begunkov913a5712021-11-10 15:49:31 +00001578 io_fill_cqe_req(req, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001579 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001580 }
1581}
1582
Pavel Begunkovc0724812021-10-04 20:02:54 +01001583static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001584{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001585 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001586 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1587 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001588
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001589 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001590 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001591 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001592 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001593 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001594 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001595}
1596
Pavel Begunkovc0724812021-10-04 20:02:54 +01001597static __cold void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001598 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001599{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001600 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001601
Jens Axboe79ebeae2021-08-10 15:18:27 -06001602 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001603 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001604 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001605 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001606 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001607
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001608 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001609 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001610
1611 /*
1612 * Since seq can easily wrap around over time, subtract
1613 * the last seq at which timeouts were flushed before comparing.
1614 * Assuming not more than 2^31-1 events have happened since,
1615 * these subtractions won't have wrapped, so we can check if
1616 * target is in [last_seq, current_seq] by comparing the two.
1617 */
1618 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1619 events_got = seq - ctx->cq_last_tm_flush;
1620 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001621 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001622
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001623 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001624 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001625 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001626 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001627 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001628}
1629
Pavel Begunkovc0724812021-10-04 20:02:54 +01001630static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001631{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001632 if (ctx->off_timeout_used)
1633 io_flush_timeouts(ctx);
1634 if (ctx->drain_active)
1635 io_queue_deferred(ctx);
1636}
1637
1638static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1639{
1640 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1641 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001642 /* order cqe stores with ring update */
1643 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001644}
1645
Jens Axboe90554202020-09-03 12:12:41 -06001646static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1647{
1648 struct io_rings *r = ctx->rings;
1649
Pavel Begunkova566c552021-05-16 22:58:08 +01001650 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001651}
1652
Pavel Begunkov888aae22021-01-19 13:32:39 +00001653static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1654{
1655 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1656}
1657
Pavel Begunkovd068b502021-05-16 22:58:11 +01001658static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001659{
Hristo Venev75b28af2019-08-26 17:23:46 +00001660 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001661 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662
Stefan Bühler115e12e2019-04-24 23:54:18 +02001663 /*
1664 * writes to the cq entry need to come after reading head; the
1665 * control dependency is enough as we're using WRITE_ONCE to
1666 * fill the cq entry
1667 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001668 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001669 return NULL;
1670
Pavel Begunkov888aae22021-01-19 13:32:39 +00001671 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001672 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001673}
1674
Jens Axboef2842ab2020-01-08 11:04:00 -07001675static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1676{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001677 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001678 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001679 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1680 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001681 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001682}
1683
Jens Axboe2c5d7632021-08-21 07:21:19 -06001684/*
1685 * This should only get called when at least one event has been posted.
1686 * Some applications rely on the eventfd notification count only changing
1687 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1688 * 1:1 relationship between how many times this function is called (and
1689 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1690 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001691static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001692{
Jens Axboe5fd46172021-08-06 14:04:31 -06001693 /*
1694 * wake_up_all() may seem excessive, but io_wake_function() and
1695 * io_should_wake() handle the termination of the loop and only
1696 * wake as many waiters as we need to.
1697 */
1698 if (wq_has_sleeper(&ctx->cq_wait))
1699 wake_up_all(&ctx->cq_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001700 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001701 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001702}
1703
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001704static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1705{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001706 /* see waitqueue_active() comment */
1707 smp_mb();
1708
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001709 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001710 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001711 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001712 }
1713 if (io_should_trigger_evfd(ctx))
1714 eventfd_signal(ctx->cq_ev_fd, 1);
1715}
1716
Jens Axboec4a2ed72019-11-21 21:01:26 -07001717/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001718static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001719{
Jens Axboeb18032b2021-01-24 16:58:56 -07001720 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001721
Pavel Begunkova566c552021-05-16 22:58:08 +01001722 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001723 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001724
Jens Axboeb18032b2021-01-24 16:58:56 -07001725 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001726 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001727 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001728 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001729 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001730
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001731 if (!cqe && !force)
1732 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001733 ocqe = list_first_entry(&ctx->cq_overflow_list,
1734 struct io_overflow_cqe, list);
1735 if (cqe)
1736 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1737 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001738 io_account_cq_overflow(ctx);
1739
Jens Axboeb18032b2021-01-24 16:58:56 -07001740 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001741 list_del(&ocqe->list);
1742 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001743 }
1744
Pavel Begunkov09e88402020-12-17 00:24:38 +00001745 all_flushed = list_empty(&ctx->cq_overflow_list);
1746 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001747 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001748 WRITE_ONCE(ctx->rings->sq_flags,
1749 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001750 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001751
Jens Axboeb18032b2021-01-24 16:58:56 -07001752 if (posted)
1753 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001754 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001755 if (posted)
1756 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001757 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001758}
1759
Pavel Begunkov90f67362021-08-09 20:18:12 +01001760static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001761{
Jens Axboeca0a2652021-03-04 17:15:48 -07001762 bool ret = true;
1763
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001764 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001765 /* iopoll syncs against uring_lock, not completion_lock */
1766 if (ctx->flags & IORING_SETUP_IOPOLL)
1767 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001768 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001769 if (ctx->flags & IORING_SETUP_IOPOLL)
1770 mutex_unlock(&ctx->uring_lock);
1771 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001772
1773 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001774}
1775
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001776/* must to be called somewhat shortly after putting a request */
1777static inline void io_put_task(struct task_struct *task, int nr)
1778{
1779 struct io_uring_task *tctx = task->io_uring;
1780
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001781 if (likely(task == current)) {
1782 tctx->cached_refs += nr;
1783 } else {
1784 percpu_counter_sub(&tctx->inflight, nr);
1785 if (unlikely(atomic_read(&tctx->in_idle)))
1786 wake_up(&tctx->wait);
1787 put_task_struct_many(task, nr);
1788 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001789}
1790
Pavel Begunkov9a108672021-08-27 11:55:01 +01001791static void io_task_refs_refill(struct io_uring_task *tctx)
1792{
1793 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1794
1795 percpu_counter_add(&tctx->inflight, refill);
1796 refcount_add(refill, &current->usage);
1797 tctx->cached_refs += refill;
1798}
1799
1800static inline void io_get_task_refs(int nr)
1801{
1802 struct io_uring_task *tctx = current->io_uring;
1803
1804 tctx->cached_refs -= nr;
1805 if (unlikely(tctx->cached_refs < 0))
1806 io_task_refs_refill(tctx);
1807}
1808
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001809static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001810 s32 res, u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001811{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001812 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001813
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001814 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1815 if (!ocqe) {
1816 /*
1817 * If we're in ring overflow flush mode, or in task cancel mode,
1818 * or cannot allocate an overflow entry, then we need to drop it
1819 * on the floor.
1820 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001821 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001822 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001823 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001824 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001825 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001826 WRITE_ONCE(ctx->rings->sq_flags,
1827 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1828
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001829 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001830 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001831 ocqe->cqe.res = res;
1832 ocqe->cqe.flags = cflags;
1833 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1834 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001835}
1836
Pavel Begunkov913a5712021-11-10 15:49:31 +00001837static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data,
1838 s32 res, u32 cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001839{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001840 struct io_uring_cqe *cqe;
1841
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001842 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001843
1844 /*
1845 * If we can't get a cq entry, userspace overflowed the
1846 * submission (by quite a lot). Increment the overflow count in
1847 * the ring.
1848 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001849 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001850 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001851 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001852 WRITE_ONCE(cqe->res, res);
1853 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001854 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001855 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001856 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001857}
1858
Pavel Begunkov913a5712021-11-10 15:49:31 +00001859static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001860{
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001861 if (!(req->flags & REQ_F_CQE_SKIP))
1862 __io_fill_cqe(req->ctx, req->user_data, res, cflags);
Pavel Begunkov913a5712021-11-10 15:49:31 +00001863}
1864
1865static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data,
1866 s32 res, u32 cflags)
1867{
1868 ctx->cq_extra++;
1869 return __io_fill_cqe(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001870}
1871
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001872static void io_req_complete_post(struct io_kiocb *req, s32 res,
1873 u32 cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001874{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001875 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876
Jens Axboe79ebeae2021-08-10 15:18:27 -06001877 spin_lock(&ctx->completion_lock);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00001878 if (!(req->flags & REQ_F_CQE_SKIP))
1879 __io_fill_cqe(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001880 /*
1881 * If we're the last reference to this request, add to our locked
1882 * free_list cache.
1883 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001884 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001885 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001886 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001887 io_disarm_next(req);
1888 if (req->link) {
1889 io_req_task_queue(req->link);
1890 req->link = NULL;
1891 }
1892 }
Pavel Begunkovab409402021-10-09 23:14:41 +01001893 io_req_put_rsrc(req, ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001894 io_dismantle_req(req);
1895 io_put_task(req->task, 1);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001896 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001897 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001898 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001899 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001900 spin_unlock(&ctx->completion_lock);
Pavel Begunkova3f349072021-09-15 12:04:20 +01001901 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902}
1903
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001904static inline void io_req_complete_state(struct io_kiocb *req, s32 res,
1905 u32 cflags)
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001906{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001907 req->result = res;
Pavel Begunkovd17e56e2021-10-04 20:02:57 +01001908 req->cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001909 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001910}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911
Pavel Begunkov889fca72021-02-10 00:03:09 +00001912static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001913 s32 res, u32 cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001914{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001915 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1916 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001917 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001918 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001919}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001920
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001921static inline void io_req_complete(struct io_kiocb *req, s32 res)
Jens Axboee1e16092020-06-22 09:17:17 -06001922{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001923 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001924}
1925
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01001926static void io_req_complete_failed(struct io_kiocb *req, s32 res)
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001927{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001928 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001929 io_req_complete_post(req, res, 0);
1930}
1931
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01001932static void io_req_complete_fail_submit(struct io_kiocb *req)
1933{
1934 /*
1935 * We don't submit, fail them all, for that replace hardlinks with
1936 * normal links. Extra REQ_F_LINK is tolerated.
1937 */
1938 req->flags &= ~REQ_F_HARDLINK;
1939 req->flags |= REQ_F_LINK;
1940 io_req_complete_failed(req, req->result);
1941}
1942
Pavel Begunkov864ea922021-08-09 13:04:08 +01001943/*
1944 * Don't initialise the fields below on every allocation, but do that in
1945 * advance and keep them valid across allocations.
1946 */
1947static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1948{
1949 req->ctx = ctx;
1950 req->link = NULL;
1951 req->async_data = NULL;
1952 /* not necessary, but safer to zero */
1953 req->result = 0;
1954}
1955
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001956static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001957 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001958{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001959 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001960 wq_list_splice(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001961 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001962 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001963}
1964
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001965/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001966static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001967{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001968 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001969
Jens Axboec7dae4b2021-02-09 19:53:37 -07001970 /*
1971 * If we have more than a batch's worth of requests in our IRQ side
1972 * locked cache, grab the lock and move them over to our submission
1973 * side cache.
1974 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001975 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001976 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001977 return !!state->free_list.next;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001978}
1979
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001980/*
1981 * A request might get retired back into the request caches even before opcode
1982 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1983 * Because of that, io_alloc_req() should be called only under ->uring_lock
1984 * and with extra caution to not get a request that is still worked on.
1985 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01001986static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001987 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001988{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001989 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001990 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001991 void *reqs[IO_REQ_ALLOC_BATCH];
1992 struct io_kiocb *req;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001993 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001994
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01001995 if (likely(state->free_list.next || io_flush_cached_reqs(ctx)))
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01001996 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001997
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01001998 ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001999
Pavel Begunkov864ea922021-08-09 13:04:08 +01002000 /*
2001 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2002 * retry single alloc to be on the safe side.
2003 */
2004 if (unlikely(ret <= 0)) {
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002005 reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2006 if (!reqs[0])
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01002007 return false;
Pavel Begunkov864ea922021-08-09 13:04:08 +01002008 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002009 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01002010
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002011 percpu_ref_get_many(&ctx->refs, ret);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002012 for (i = 0; i < ret; i++) {
2013 req = reqs[i];
2014
2015 io_preinit_req(req, ctx);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002016 wq_stack_add_head(&req->comp_list, &state->free_list);
Pavel Begunkov3ab665b2021-09-24 21:59:45 +01002017 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01002018 return true;
2019}
2020
2021static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
2022{
2023 if (unlikely(!ctx->submit_state.free_list.next))
2024 return __io_alloc_req_refill(ctx);
2025 return true;
2026}
2027
2028static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
2029{
2030 struct io_wq_work_node *node;
2031
2032 node = wq_stack_extract(&ctx->submit_state.free_list);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002033 return container_of(node, struct io_kiocb, comp_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002034}
2035
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002036static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002037{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002038 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002039 fput(file);
2040}
2041
Pavel Begunkov6b639522021-09-08 16:40:50 +01002042static inline void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002043{
Pavel Begunkov094bae42021-03-19 17:22:42 +00002044 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002045
Pavel Begunkov867f8fa2021-10-04 20:02:58 +01002046 if (unlikely(flags & IO_REQ_CLEAN_FLAGS))
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01002047 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002048 if (!(flags & REQ_F_FIXED_FILE))
2049 io_put_file(req->file);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002050}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002051
Pavel Begunkovc0724812021-10-04 20:02:54 +01002052static __cold void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002053{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002054 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002055
Pavel Begunkovab409402021-10-09 23:14:41 +01002056 io_req_put_rsrc(req, ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002057 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002058 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002059
Jens Axboe79ebeae2021-08-10 15:18:27 -06002060 spin_lock(&ctx->completion_lock);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01002061 wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01002062 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06002063 spin_unlock(&ctx->completion_lock);
Jens Axboee65ef562019-03-12 10:16:44 -06002064}
2065
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002066static inline void io_remove_next_linked(struct io_kiocb *req)
2067{
2068 struct io_kiocb *nxt = req->link;
2069
2070 req->link = nxt->link;
2071 nxt->link = NULL;
2072}
2073
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002074static bool io_kill_linked_timeout(struct io_kiocb *req)
2075 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06002076 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002077{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002078 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002079
Pavel Begunkovb97e7362021-08-15 10:40:23 +01002080 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002081 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002082
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002083 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002084 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01002085 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06002086 list_del(&link->timeout.list);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002087 /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */
Pavel Begunkov913a5712021-11-10 15:49:31 +00002088 io_fill_cqe_req(link, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002089 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002090 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002091 }
2092 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002093 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002094}
2095
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002096static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002097 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002098{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002099 struct io_kiocb *nxt, *link = req->link;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002100 bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES;
Jens Axboe9e645e112019-05-10 16:07:28 -06002101
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002102 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002103 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002104 long res = -ECANCELED;
2105
2106 if (link->flags & REQ_F_FAIL)
2107 res = link->result;
2108
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002109 nxt = link->link;
2110 link->link = NULL;
2111
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002112 trace_io_uring_fail_link(req, link);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002113 if (!ignore_cqes) {
2114 link->flags &= ~REQ_F_CQE_SKIP;
2115 io_fill_cqe_req(link, res, 0);
2116 }
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002117 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002118 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002119 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002120}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002121
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002122static bool io_disarm_next(struct io_kiocb *req)
2123 __must_hold(&req->ctx->completion_lock)
2124{
2125 bool posted = false;
2126
Pavel Begunkov0756a862021-08-15 10:40:25 +01002127 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2128 struct io_kiocb *link = req->link;
2129
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002130 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002131 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2132 io_remove_next_linked(req);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002133 /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */
Pavel Begunkov913a5712021-11-10 15:49:31 +00002134 io_fill_cqe_req(link, -ECANCELED, 0);
Pavel Begunkov0756a862021-08-15 10:40:25 +01002135 io_put_req_deferred(link);
2136 posted = true;
2137 }
2138 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002139 struct io_ring_ctx *ctx = req->ctx;
2140
2141 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002142 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002143 spin_unlock_irq(&ctx->timeout_lock);
2144 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002145 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002146 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002147 posted |= (req->link != NULL);
2148 io_fail_links(req);
2149 }
2150 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002151}
2152
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002153static void __io_req_find_next_prep(struct io_kiocb *req)
2154{
2155 struct io_ring_ctx *ctx = req->ctx;
2156 bool posted;
2157
2158 spin_lock(&ctx->completion_lock);
2159 posted = io_disarm_next(req);
2160 if (posted)
2161 io_commit_cqring(req->ctx);
2162 spin_unlock(&ctx->completion_lock);
2163 if (posted)
2164 io_cqring_ev_posted(ctx);
2165}
2166
2167static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002168{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002169 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002170
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002171 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
2172 return NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002173 /*
2174 * If LINK is set, we have dependent requests in this chain. If we
2175 * didn't fail this request, queue the first one up, moving any other
2176 * dependencies to the next request. In case of failure, fail the rest
2177 * of the chain.
2178 */
Pavel Begunkovd81499b2021-09-08 16:40:51 +01002179 if (unlikely(req->flags & IO_DISARM_MASK))
2180 __io_req_find_next_prep(req);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002181 nxt = req->link;
2182 req->link = NULL;
2183 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002184}
Jens Axboe2665abf2019-11-05 12:40:47 -07002185
Pavel Begunkovf237c302021-08-18 12:42:46 +01002186static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002187{
2188 if (!ctx)
2189 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002190 if (*locked) {
Pavel Begunkovc4501782021-09-08 16:40:52 +01002191 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002192 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002193 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002194 }
2195 percpu_ref_put(&ctx->refs);
2196}
2197
Jens Axboe7cbf1722021-02-10 00:03:20 +00002198static void tctx_task_work(struct callback_head *cb)
2199{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002200 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002201 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002202 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2203 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002204
Pavel Begunkov16f72072021-06-17 18:14:09 +01002205 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002206 struct io_wq_work_node *node;
2207
Pavel Begunkovc4501782021-09-08 16:40:52 +01002208 if (!tctx->task_list.first && locked)
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002209 io_submit_flush_completions(ctx);
2210
Pavel Begunkov3f184072021-06-17 18:14:06 +01002211 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002212 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002213 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002214 if (!node)
2215 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002216 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002217 if (!node)
2218 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002219
Pavel Begunkov6294f362021-08-10 17:53:55 +01002220 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002221 struct io_wq_work_node *next = node->next;
2222 struct io_kiocb *req = container_of(node, struct io_kiocb,
2223 io_task_work.node);
2224
2225 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002226 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002227 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002228 /* if not contended, grab and improve batching */
2229 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002230 percpu_ref_get(&ctx->refs);
2231 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002232 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002233 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002234 } while (node);
2235
Jens Axboe7cbf1722021-02-10 00:03:20 +00002236 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002237 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002238
Pavel Begunkovf237c302021-08-18 12:42:46 +01002239 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002240}
2241
Pavel Begunkove09ee512021-07-01 13:26:05 +01002242static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002243{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002244 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002245 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002246 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002247 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002248 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002249 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002250
2251 WARN_ON_ONCE(!tctx);
2252
Jens Axboe0b81e802021-02-16 10:33:53 -07002253 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002254 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002255 running = tctx->task_running;
2256 if (!running)
2257 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002258 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002259
2260 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002261 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002262 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002263
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002264 /*
2265 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2266 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2267 * processing task_work. There's no reliable way to tell if TWA_RESUME
2268 * will do the job.
2269 */
2270 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovd97ec622021-09-08 16:40:53 +01002271 if (likely(!task_work_add(tsk, &tctx->task_work, notify))) {
2272 if (notify == TWA_NONE)
2273 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002274 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002275 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002276
Pavel Begunkove09ee512021-07-01 13:26:05 +01002277 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002278 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002279 node = tctx->task_list.first;
2280 INIT_WQ_LIST(&tctx->task_list);
2281 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002282
Pavel Begunkove09ee512021-07-01 13:26:05 +01002283 while (node) {
2284 req = container_of(node, struct io_kiocb, io_task_work.node);
2285 node = node->next;
2286 if (llist_add(&req->io_task_work.fallback_node,
2287 &req->ctx->fallback_llist))
2288 schedule_delayed_work(&req->ctx->fallback_work, 1);
2289 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002290}
2291
Pavel Begunkovf237c302021-08-18 12:42:46 +01002292static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002293{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002294 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002295
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002296 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002297 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002298 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002299}
2300
Pavel Begunkovf237c302021-08-18 12:42:46 +01002301static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002302{
2303 struct io_ring_ctx *ctx = req->ctx;
2304
Pavel Begunkovf237c302021-08-18 12:42:46 +01002305 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002306 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002307 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002308 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002309 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002310 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002311}
2312
Pavel Begunkova3df76982021-02-18 22:32:52 +00002313static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2314{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002315 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002316 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002317 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002318}
2319
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002320static void io_req_task_queue(struct io_kiocb *req)
2321{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002322 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002323 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002324}
2325
Jens Axboe773af692021-07-27 10:25:55 -06002326static void io_req_task_queue_reissue(struct io_kiocb *req)
2327{
2328 req->io_task_work.func = io_queue_async_work;
2329 io_req_task_work_add(req);
2330}
2331
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002332static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002333{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002334 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002335
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002336 if (nxt)
2337 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002338}
2339
Jens Axboe9e645e112019-05-10 16:07:28 -06002340static void io_free_req(struct io_kiocb *req)
2341{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002342 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002343 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002344}
2345
Pavel Begunkovf237c302021-08-18 12:42:46 +01002346static void io_free_req_work(struct io_kiocb *req, bool *locked)
2347{
2348 io_free_req(req);
2349}
2350
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002351static void io_free_batch_list(struct io_ring_ctx *ctx,
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002352 struct io_wq_work_node *node)
Jens Axboea141dd82021-08-12 12:48:34 -06002353 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002354{
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002355 struct task_struct *task = NULL;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01002356 int task_refs = 0;
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002357
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002358 do {
2359 struct io_kiocb *req = container_of(node, struct io_kiocb,
2360 comp_list);
2361
Pavel Begunkovdef77ac2021-10-12 15:02:14 +01002362 if (unlikely(req->flags & REQ_F_REFCOUNT)) {
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002363 node = req->comp_list.next;
Pavel Begunkovdef77ac2021-10-12 15:02:14 +01002364 if (!req_ref_put_and_test(req))
2365 continue;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002366 }
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002367
Pavel Begunkovab409402021-10-09 23:14:41 +01002368 io_req_put_rsrc_locked(req, ctx);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002369 io_queue_next(req);
2370 io_dismantle_req(req);
2371
2372 if (req->task != task) {
2373 if (task)
2374 io_put_task(task, task_refs);
2375 task = req->task;
2376 task_refs = 0;
2377 }
2378 task_refs++;
Pavel Begunkovc1e53a62021-10-04 20:02:55 +01002379 node = req->comp_list.next;
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002380 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002381 } while (node);
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002382
Pavel Begunkovd4b7a5e2021-09-24 21:59:53 +01002383 if (task)
2384 io_put_task(task, task_refs);
Pavel Begunkov3aa83bf2021-09-24 21:59:50 +01002385}
2386
Pavel Begunkovc4501782021-09-08 16:40:52 +01002387static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002388 __must_hold(&ctx->uring_lock)
2389{
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002390 struct io_wq_work_node *node, *prev;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002391 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002392
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00002393 if (state->flush_cqes) {
2394 spin_lock(&ctx->completion_lock);
2395 wq_list_for_each(node, prev, &state->compl_reqs) {
2396 struct io_kiocb *req = container_of(node, struct io_kiocb,
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002397 comp_list);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002398
Pavel Begunkov3d4aeb92021-11-10 15:49:33 +00002399 if (!(req->flags & REQ_F_CQE_SKIP))
2400 __io_fill_cqe(ctx, req->user_data, req->result,
2401 req->cflags);
2402 }
2403
2404 io_commit_cqring(ctx);
2405 spin_unlock(&ctx->completion_lock);
2406 io_cqring_ev_posted(ctx);
2407 state->flush_cqes = false;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002408 }
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002409
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002410 io_free_batch_list(ctx, state->compl_reqs.first);
Pavel Begunkov6f33b0b2021-09-24 21:59:44 +01002411 INIT_WQ_LIST(&state->compl_reqs);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002412}
2413
Jens Axboeba816ad2019-09-28 11:36:45 -06002414/*
2415 * Drop reference to request, return next in chain (if there is one) if this
2416 * was the last reference to this request.
2417 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002418static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002419{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002420 struct io_kiocb *nxt = NULL;
2421
Jens Axboede9b4cc2021-02-24 13:28:27 -07002422 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002423 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002424 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002425 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002426 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002427}
2428
Pavel Begunkov0d850352021-03-19 17:22:37 +00002429static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002430{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002431 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002432 io_free_req(req);
2433}
2434
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002435static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002436{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002437 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002438 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002439 io_req_task_work_add(req);
2440 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002441}
2442
Pavel Begunkov6c503152021-01-04 20:36:36 +00002443static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002444{
2445 /* See comment at the top of this file */
2446 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002447 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002448}
2449
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002450static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2451{
2452 struct io_rings *rings = ctx->rings;
2453
2454 /* make sure SQ entry isn't read before tail */
2455 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2456}
2457
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002458static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002459{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002460 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002461
Jens Axboebcda7ba2020-02-23 16:42:51 -07002462 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2463 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002464 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002465 kfree(kbuf);
2466 return cflags;
2467}
2468
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002469static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2470{
Pavel Begunkovae421d92021-08-17 20:28:08 +01002471 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2472 return 0;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01002473 return io_put_kbuf(req, req->kbuf);
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002474}
2475
Jens Axboe4c6e2772020-07-01 11:29:10 -06002476static inline bool io_run_task_work(void)
2477{
Nadav Amitef98eb02021-08-07 17:13:41 -07002478 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002479 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002480 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002481 return true;
2482 }
2483
2484 return false;
2485}
2486
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002487static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
Jens Axboedef596e2019-01-09 08:59:42 -07002488{
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002489 struct io_wq_work_node *pos, *start, *prev;
Christoph Hellwigd729cf92021-10-12 13:12:20 +02002490 unsigned int poll_flags = BLK_POLL_NOSLEEP;
Jens Axboeb688f112021-10-12 09:28:46 -06002491 DEFINE_IO_COMP_BATCH(iob);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002492 int nr_events = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002493
2494 /*
2495 * Only spin for completions if we don't have multiple devices hanging
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002496 * off our complete list.
Jens Axboedef596e2019-01-09 08:59:42 -07002497 */
Pavel Begunkov87a115f2021-09-24 21:59:42 +01002498 if (ctx->poll_multi_queue || force_nonspin)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002499 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002500
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002501 wq_list_for_each(pos, start, &ctx->iopoll_list) {
2502 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002503 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002504 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002505
2506 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002507 * Move completed and retryable entries to our local lists.
2508 * If we find a request that requires polling, break out
2509 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002510 */
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002511 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002512 break;
2513
Jens Axboeb688f112021-10-12 09:28:46 -06002514 ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002515 if (unlikely(ret < 0))
2516 return ret;
2517 else if (ret)
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02002518 poll_flags |= BLK_POLL_ONESHOT;
Jens Axboedef596e2019-01-09 08:59:42 -07002519
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002520 /* iopoll may have completed current req */
Jens Axboeb688f112021-10-12 09:28:46 -06002521 if (!rq_list_empty(iob.req_list) ||
2522 READ_ONCE(req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002523 break;
Jens Axboedef596e2019-01-09 08:59:42 -07002524 }
2525
Jens Axboeb688f112021-10-12 09:28:46 -06002526 if (!rq_list_empty(iob.req_list))
2527 iob.complete(&iob);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002528 else if (!pos)
2529 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002530
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002531 prev = start;
2532 wq_list_for_each_resume(pos, prev) {
2533 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002534 u32 cflags;
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002535
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002536 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
2537 if (!smp_load_acquire(&req->iopoll_completed))
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002538 break;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00002539 cflags = io_put_rw_kbuf(req);
2540 if (!(req->flags & REQ_F_CQE_SKIP))
2541 __io_fill_cqe(ctx, req->user_data, req->result, cflags);
Pavel Begunkove3f721e2021-09-24 21:59:48 +01002542 nr_events++;
2543 }
Jens Axboedef596e2019-01-09 08:59:42 -07002544
Pavel Begunkovf5ed3bc2021-09-24 21:59:52 +01002545 if (unlikely(!nr_events))
2546 return 0;
2547
2548 io_commit_cqring(ctx);
2549 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002550 pos = start ? start->next : ctx->iopoll_list.first;
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002551 wq_list_cut(&ctx->iopoll_list, prev, start);
Pavel Begunkov1cce17a2021-09-24 21:59:54 +01002552 io_free_batch_list(ctx, pos);
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002553 return nr_events;
Jens Axboedef596e2019-01-09 08:59:42 -07002554}
2555
2556/*
Jens Axboedef596e2019-01-09 08:59:42 -07002557 * We can't just wait for polled events to come to us, we have to actively
2558 * find and complete them.
2559 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01002560static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002561{
2562 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2563 return;
2564
2565 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002566 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002567 /* let it sleep and repeat later if can't complete a request */
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002568 if (io_do_iopoll(ctx, true) == 0)
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002569 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002570 /*
2571 * Ensure we allow local-to-the-cpu processing to take place,
2572 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002573 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002574 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002575 if (need_resched()) {
2576 mutex_unlock(&ctx->uring_lock);
2577 cond_resched();
2578 mutex_lock(&ctx->uring_lock);
2579 }
Jens Axboedef596e2019-01-09 08:59:42 -07002580 }
2581 mutex_unlock(&ctx->uring_lock);
2582}
2583
Pavel Begunkov7668b922020-07-07 16:36:21 +03002584static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002585{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002586 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002587 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002588
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002589 /*
2590 * We disallow the app entering submit/complete with polling, but we
2591 * still need to lock the ring to prevent racing with polled issue
2592 * that got punted to a workqueue.
2593 */
2594 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002595 /*
2596 * Don't enter poll loop if we already have events pending.
2597 * If we do, we can potentially be spinning for commands that
2598 * already triggered a CQE (eg in error).
2599 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002600 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002601 __io_cqring_overflow_flush(ctx, false);
2602 if (io_cqring_events(ctx))
2603 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002604 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002605 /*
2606 * If a submit got punted to a workqueue, we can have the
2607 * application entering polling for a command before it gets
2608 * issued. That app will hold the uring_lock for the duration
2609 * of the poll right here, so we need to take a breather every
2610 * now and then to ensure that the issue has a chance to add
2611 * the poll to the issued list. Otherwise we can spin here
2612 * forever, while the workqueue is stuck trying to acquire the
2613 * very same mutex.
2614 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002615 if (wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002616 u32 tail = ctx->cached_cq_tail;
2617
Jens Axboe500f9fb2019-08-19 12:15:59 -06002618 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002619 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002620 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002621
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002622 /* some requests don't go through iopoll_list */
2623 if (tail != ctx->cached_cq_tail ||
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002624 wq_list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002625 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002626 }
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01002627 ret = io_do_iopoll(ctx, !min);
2628 if (ret < 0)
2629 break;
2630 nr_events += ret;
2631 ret = 0;
2632 } while (nr_events < min && !need_resched());
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002633out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002634 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002635 return ret;
2636}
2637
Jens Axboe491381ce2019-10-17 09:20:46 -06002638static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002639{
Jens Axboe491381ce2019-10-17 09:20:46 -06002640 /*
2641 * Tell lockdep we inherited freeze protection from submission
2642 * thread.
2643 */
2644 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002645 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002646
Pavel Begunkov1c986792021-03-22 01:58:31 +00002647 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2648 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002649 }
2650}
2651
Jens Axboeb63534c2020-06-04 11:28:00 -06002652#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002653static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002654{
Pavel Begunkovab454432021-03-22 01:58:33 +00002655 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002656
Pavel Begunkovd886e182021-10-04 20:02:56 +01002657 if (!req_has_async_data(req))
Pavel Begunkovab454432021-03-22 01:58:33 +00002658 return !io_req_prep_async(req);
Pavel Begunkov538941e2021-10-14 16:10:15 +01002659 iov_iter_restore(&rw->s.iter, &rw->s.iter_state);
Pavel Begunkovab454432021-03-22 01:58:33 +00002660 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002661}
Jens Axboeb63534c2020-06-04 11:28:00 -06002662
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002663static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002664{
Jens Axboe355afae2020-09-02 09:30:31 -06002665 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002666 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002667
Jens Axboe355afae2020-09-02 09:30:31 -06002668 if (!S_ISBLK(mode) && !S_ISREG(mode))
2669 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002670 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2671 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002672 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002673 /*
2674 * If ref is dying, we might be running poll reap from the exit work.
2675 * Don't attempt to reissue from that path, just let it fail with
2676 * -EAGAIN.
2677 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002678 if (percpu_ref_is_dying(&ctx->refs))
2679 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002680 /*
2681 * Play it safe and assume not safe to re-import and reissue if we're
2682 * not in the original thread group (or in task context).
2683 */
2684 if (!same_thread_group(req->task, current) || !in_task())
2685 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002686 return true;
2687}
Jens Axboee82ad482021-04-02 19:45:34 -06002688#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002689static bool io_resubmit_prep(struct io_kiocb *req)
2690{
2691 return false;
2692}
Jens Axboee82ad482021-04-02 19:45:34 -06002693static bool io_rw_should_reissue(struct io_kiocb *req)
2694{
2695 return false;
2696}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002697#endif
2698
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002699static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002700{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002701 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2702 kiocb_end_write(req);
Pavel Begunkov258f3a72021-10-14 16:10:14 +01002703 if (unlikely(res != req->result)) {
Pavel Begunkov9532b992021-03-22 01:58:34 +00002704 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2705 io_rw_should_reissue(req)) {
2706 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002707 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002708 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002709 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002710 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002711 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002712 return false;
2713}
2714
Pavel Begunkovf237c302021-08-18 12:42:46 +01002715static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002716{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002717 unsigned int cflags = io_put_rw_kbuf(req);
Pavel Begunkov54daa9b2021-10-04 20:03:00 +01002718 int res = req->result;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002719
2720 if (*locked) {
Pavel Begunkov126180b2021-08-18 12:42:47 +01002721 io_req_complete_state(req, res, cflags);
Pavel Begunkovfff4e402021-10-04 20:02:48 +01002722 io_req_add_compl_list(req);
Pavel Begunkov126180b2021-08-18 12:42:47 +01002723 } else {
2724 io_req_complete_post(req, res, cflags);
2725 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002726}
2727
2728static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2729 unsigned int issue_flags)
2730{
2731 if (__io_complete_rw_common(req, res))
2732 return;
Pavel Begunkov63637852021-09-02 00:38:22 +01002733 __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002734}
2735
Jens Axboe6b19b762021-10-21 09:22:35 -06002736static void io_complete_rw(struct kiocb *kiocb, long res)
Jens Axboeba816ad2019-09-28 11:36:45 -06002737{
Jens Axboe9adbd452019-12-20 08:45:55 -07002738 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002739
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002740 if (__io_complete_rw_common(req, res))
2741 return;
2742 req->result = res;
2743 req->io_task_work.func = io_req_task_complete;
2744 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002745}
2746
Jens Axboe6b19b762021-10-21 09:22:35 -06002747static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
Jens Axboedef596e2019-01-09 08:59:42 -07002748{
Jens Axboe9adbd452019-12-20 08:45:55 -07002749 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002750
Jens Axboe491381ce2019-10-17 09:20:46 -06002751 if (kiocb->ki_flags & IOCB_WRITE)
2752 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002753 if (unlikely(res != req->result)) {
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002754 if (res == -EAGAIN && io_rw_should_reissue(req)) {
2755 req->flags |= REQ_F_REISSUE;
2756 return;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002757 }
Pavel Begunkov258f3a72021-10-14 16:10:14 +01002758 req->result = res;
Pavel Begunkov8c130822021-03-22 01:58:32 +00002759 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002760
Pavel Begunkovb3fa03f2021-09-24 21:59:51 +01002761 /* order with io_iopoll_complete() checking ->iopoll_completed */
2762 smp_store_release(&req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002763}
2764
2765/*
2766 * After the iocb has been issued, it's safe to be found on the poll list.
2767 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002768 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002769 * accessing the kiocb cookie.
2770 */
Pavel Begunkov98821312021-10-15 17:09:12 +01002771static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboedef596e2019-01-09 08:59:42 -07002772{
2773 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08002774 const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002775
2776 /* workqueue context doesn't hold uring_lock, grab it now */
Hao Xu3b44b372021-10-18 21:34:31 +08002777 if (unlikely(needs_lock))
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002778 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002779
2780 /*
2781 * Track whether we have multiple files in our lists. This will impact
2782 * how we do polling eventually, not spinning if we're on potentially
2783 * different devices.
2784 */
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002785 if (wq_list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002786 ctx->poll_multi_queue = false;
2787 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002788 struct io_kiocb *list_req;
2789
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002790 list_req = container_of(ctx->iopoll_list.first, struct io_kiocb,
2791 comp_list);
Christoph Hellwig30da1b42021-10-12 13:12:14 +02002792 if (list_req->file != req->file)
Hao Xu915b3dd2021-06-28 05:37:30 +08002793 ctx->poll_multi_queue = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002794 }
2795
2796 /*
2797 * For fast devices, IO may have already completed. If it has, add
2798 * it to the front so we find it first.
2799 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002800 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002801 wq_list_add_head(&req->comp_list, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002802 else
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01002803 wq_list_add_tail(&req->comp_list, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002804
Hao Xu3b44b372021-10-18 21:34:31 +08002805 if (unlikely(needs_lock)) {
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002806 /*
2807 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2808 * in sq thread task context or in io worker task context. If
2809 * current task context is sq thread, we don't need to check
2810 * whether should wake up sq thread.
2811 */
2812 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2813 wq_has_sleeper(&ctx->sq_data->wait))
2814 wake_up(&ctx->sq_data->wait);
2815
2816 mutex_unlock(&ctx->uring_lock);
2817 }
Jens Axboedef596e2019-01-09 08:59:42 -07002818}
2819
Jens Axboe4503b762020-06-01 10:00:27 -06002820static bool io_bdev_nowait(struct block_device *bdev)
2821{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002822 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002823}
2824
Jens Axboe2b188cc2019-01-07 10:46:33 -07002825/*
2826 * If we tracked the file through the SCM inflight mechanism, we could support
2827 * any file. For now, just ensure that anything potentially problematic is done
2828 * inline.
2829 */
Pavel Begunkov88459b52021-10-17 00:07:10 +01002830static bool __io_file_supports_nowait(struct file *file, umode_t mode)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002831{
Jens Axboe4503b762020-06-01 10:00:27 -06002832 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002833 if (IS_ENABLED(CONFIG_BLOCK) &&
2834 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002835 return true;
2836 return false;
2837 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002838 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002839 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002840 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002841 if (IS_ENABLED(CONFIG_BLOCK) &&
2842 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002843 file->f_op != &io_uring_fops)
2844 return true;
2845 return false;
2846 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002847
Jens Axboec5b85622020-06-09 19:23:05 -06002848 /* any ->read/write should understand O_NONBLOCK */
2849 if (file->f_flags & O_NONBLOCK)
2850 return true;
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002851 return file->f_mode & FMODE_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002852}
2853
Pavel Begunkov88459b52021-10-17 00:07:10 +01002854/*
2855 * If we tracked the file through the SCM inflight mechanism, we could support
2856 * any file. For now, just ensure that anything potentially problematic is done
2857 * inline.
2858 */
2859static unsigned int io_file_get_flags(struct file *file)
Jens Axboe7b29f922021-03-12 08:30:14 -07002860{
Pavel Begunkov88459b52021-10-17 00:07:10 +01002861 umode_t mode = file_inode(file)->i_mode;
2862 unsigned int res = 0;
Jens Axboe7b29f922021-03-12 08:30:14 -07002863
Pavel Begunkov88459b52021-10-17 00:07:10 +01002864 if (S_ISREG(mode))
2865 res |= FFS_ISREG;
2866 if (__io_file_supports_nowait(file, mode))
2867 res |= FFS_NOWAIT;
2868 return res;
Jens Axboe7b29f922021-03-12 08:30:14 -07002869}
2870
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002871static inline bool io_file_supports_nowait(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002872{
Pavel Begunkov88459b52021-10-17 00:07:10 +01002873 return req->flags & REQ_F_SUPPORT_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002874}
2875
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01002876static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002877{
Jens Axboedef596e2019-01-09 08:59:42 -07002878 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002879 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002880 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002881 unsigned ioprio;
2882 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002883
Pavel Begunkov88459b52021-10-17 00:07:10 +01002884 if (!io_req_ffs_set(req))
2885 req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT;
Jens Axboe491381ce2019-10-17 09:20:46 -06002886
Jens Axboe2b188cc2019-01-07 10:46:33 -07002887 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002888 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002889 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002890 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002891 }
Pavel Begunkov5cb03d62021-10-15 17:09:16 +01002892 kiocb->ki_flags = iocb_flags(file);
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002893 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2894 if (unlikely(ret))
2895 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002896
Jens Axboe5d329e12021-09-14 11:08:37 -06002897 /*
2898 * If the file is marked O_NONBLOCK, still allow retry for it if it
2899 * supports async. Otherwise it's impossible to use O_NONBLOCK files
2900 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2901 */
2902 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
Pavel Begunkov35645ac2021-10-17 00:07:09 +01002903 ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req)))
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002904 req->flags |= REQ_F_NOWAIT;
2905
Jens Axboedef596e2019-01-09 08:59:42 -07002906 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov5cb03d62021-10-15 17:09:16 +01002907 if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002908 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002909
Jens Axboe394918e2021-03-08 11:40:23 -07002910 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
Jens Axboedef596e2019-01-09 08:59:42 -07002911 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002912 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002913 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002914 if (kiocb->ki_flags & IOCB_HIPRI)
2915 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002916 kiocb->ki_complete = io_complete_rw;
2917 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002918
Pavel Begunkovfb272742021-10-23 12:14:02 +01002919 ioprio = READ_ONCE(sqe->ioprio);
2920 if (ioprio) {
2921 ret = ioprio_check_cap(ioprio);
2922 if (ret)
2923 return ret;
2924
2925 kiocb->ki_ioprio = ioprio;
2926 } else {
2927 kiocb->ki_ioprio = get_current_ioprio();
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002928 }
2929
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01002930 req->imu = NULL;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002931 req->rw.addr = READ_ONCE(sqe->addr);
2932 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002933 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002934 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002935}
2936
2937static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2938{
2939 switch (ret) {
2940 case -EIOCBQUEUED:
2941 break;
2942 case -ERESTARTSYS:
2943 case -ERESTARTNOINTR:
2944 case -ERESTARTNOHAND:
2945 case -ERESTART_RESTARTBLOCK:
2946 /*
2947 * We can't just restart the syscall, since previously
2948 * submitted sqes may already be in progress. Just fail this
2949 * IO with EINTR.
2950 */
2951 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002952 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002953 default:
Jens Axboe6b19b762021-10-21 09:22:35 -06002954 kiocb->ki_complete(kiocb, ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002955 }
2956}
2957
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00002958static void kiocb_done(struct io_kiocb *req, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002959 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002960{
Jens Axboee8c2bc12020-08-15 18:44:09 -07002961 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002962
Jens Axboe227c0c92020-08-13 11:51:40 -06002963 /* add previously done IO, if any */
Pavel Begunkovd886e182021-10-04 20:02:56 +01002964 if (req_has_async_data(req) && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002965 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002966 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002967 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002968 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002969 }
2970
Jens Axboeba042912019-12-25 16:33:42 -07002971 if (req->flags & REQ_F_CUR_POS)
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00002972 req->file->f_pos = req->rw.kiocb.ki_pos;
2973 if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw))
Pavel Begunkov889fca72021-02-10 00:03:09 +00002974 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002975 else
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00002976 io_rw_done(&req->rw.kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002977
Pavel Begunkovb66ceaf2021-09-15 11:00:05 +01002978 if (req->flags & REQ_F_REISSUE) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002979 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002980 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002981 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002982 } else {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002983 req_set_fail(req);
Pavel Begunkov06bdea22021-11-23 00:07:46 +00002984 req->result = ret;
2985 req->io_task_work.func = io_req_task_complete;
2986 io_req_task_work_add(req);
Pavel Begunkov97284632021-04-08 19:28:03 +01002987 }
2988 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002989}
2990
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002991static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2992 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002993{
Jens Axboe9adbd452019-12-20 08:45:55 -07002994 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002995 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002996 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002997
Pavel Begunkov75769e32021-04-01 15:43:54 +01002998 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002999 return -EFAULT;
3000 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01003001 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07003002 return -EFAULT;
3003
3004 /*
3005 * May not be a start of buffer, set size appropriately
3006 * and advance us to the beginning.
3007 */
3008 offset = buf_addr - imu->ubuf;
3009 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003010
3011 if (offset) {
3012 /*
3013 * Don't use iov_iter_advance() here, as it's really slow for
3014 * using the latter parts of a big fixed buffer - it iterates
3015 * over each segment manually. We can cheat a bit here, because
3016 * we know that:
3017 *
3018 * 1) it's a BVEC iter, we set it up
3019 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3020 * first and last bvec
3021 *
3022 * So just find our index, and adjust the iterator afterwards.
3023 * If the offset is within the first bvec (or the whole first
3024 * bvec, just use iov_iter_advance(). This makes it easier
3025 * since we can just skip the first segment, which may not
3026 * be PAGE_SIZE aligned.
3027 */
3028 const struct bio_vec *bvec = imu->bvec;
3029
3030 if (offset <= bvec->bv_len) {
3031 iov_iter_advance(iter, offset);
3032 } else {
3033 unsigned long seg_skip;
3034
3035 /* skip first vec */
3036 offset -= bvec->bv_len;
3037 seg_skip = 1 + (offset >> PAGE_SHIFT);
3038
3039 iter->bvec = bvec + seg_skip;
3040 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003041 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003042 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003043 }
3044 }
3045
Pavel Begunkov847595d2021-02-04 13:52:06 +00003046 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003047}
3048
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003049static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3050{
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003051 struct io_mapped_ubuf *imu = req->imu;
3052 u16 index, buf_index = req->buf_index;
3053
3054 if (likely(!imu)) {
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003055 struct io_ring_ctx *ctx = req->ctx;
3056
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003057 if (unlikely(buf_index >= ctx->nr_user_bufs))
3058 return -EFAULT;
Pavel Begunkov578c0ee2021-10-15 17:09:15 +01003059 io_req_set_rsrc_node(req, ctx);
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003060 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3061 imu = READ_ONCE(ctx->user_bufs[index]);
3062 req->imu = imu;
3063 }
3064 return __io_import_fixed(req, rw, iter, imu);
3065}
3066
Jens Axboebcda7ba2020-02-23 16:42:51 -07003067static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3068{
3069 if (needs_lock)
3070 mutex_unlock(&ctx->uring_lock);
3071}
3072
3073static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3074{
3075 /*
3076 * "Normal" inline submissions always hold the uring_lock, since we
3077 * grab it from the system call. Same is true for the SQPOLL offload.
3078 * The only exception is when we've detached the request and issue it
3079 * from an async worker thread, grab the lock for that case.
3080 */
3081 if (needs_lock)
3082 mutex_lock(&ctx->uring_lock);
3083}
3084
3085static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003086 int bgid, unsigned int issue_flags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07003087{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003088 struct io_buffer *kbuf = req->kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003089 struct io_buffer *head;
Hao Xu3b44b372021-10-18 21:34:31 +08003090 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003091
3092 if (req->flags & REQ_F_BUFFER_SELECTED)
3093 return kbuf;
3094
3095 io_ring_submit_lock(req->ctx, needs_lock);
3096
3097 lockdep_assert_held(&req->ctx->uring_lock);
3098
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003099 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003100 if (head) {
3101 if (!list_empty(&head->list)) {
3102 kbuf = list_last_entry(&head->list, struct io_buffer,
3103 list);
3104 list_del(&kbuf->list);
3105 } else {
3106 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003107 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003108 }
3109 if (*len > kbuf->len)
3110 *len = kbuf->len;
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003111 req->flags |= REQ_F_BUFFER_SELECTED;
3112 req->kbuf = kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003113 } else {
3114 kbuf = ERR_PTR(-ENOBUFS);
3115 }
3116
3117 io_ring_submit_unlock(req->ctx, needs_lock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003118 return kbuf;
3119}
3120
Jens Axboe4d954c22020-02-27 07:31:19 -07003121static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003122 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003123{
3124 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003125 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003126
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003127 bgid = req->buf_index;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003128 kbuf = io_buffer_select(req, len, bgid, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003129 if (IS_ERR(kbuf))
3130 return kbuf;
Jens Axboe4d954c22020-02-27 07:31:19 -07003131 return u64_to_user_ptr(kbuf->addr);
3132}
3133
3134#ifdef CONFIG_COMPAT
3135static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003136 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003137{
3138 struct compat_iovec __user *uiov;
3139 compat_ssize_t clen;
3140 void __user *buf;
3141 ssize_t len;
3142
3143 uiov = u64_to_user_ptr(req->rw.addr);
3144 if (!access_ok(uiov, sizeof(*uiov)))
3145 return -EFAULT;
3146 if (__get_user(clen, &uiov->iov_len))
3147 return -EFAULT;
3148 if (clen < 0)
3149 return -EINVAL;
3150
3151 len = clen;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003152 buf = io_rw_buffer_select(req, &len, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003153 if (IS_ERR(buf))
3154 return PTR_ERR(buf);
3155 iov[0].iov_base = buf;
3156 iov[0].iov_len = (compat_size_t) len;
3157 return 0;
3158}
3159#endif
3160
3161static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003162 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003163{
3164 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3165 void __user *buf;
3166 ssize_t len;
3167
3168 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3169 return -EFAULT;
3170
3171 len = iov[0].iov_len;
3172 if (len < 0)
3173 return -EINVAL;
Pavel Begunkov51aac422021-10-14 16:10:17 +01003174 buf = io_rw_buffer_select(req, &len, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003175 if (IS_ERR(buf))
3176 return PTR_ERR(buf);
3177 iov[0].iov_base = buf;
3178 iov[0].iov_len = len;
3179 return 0;
3180}
3181
3182static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
Pavel Begunkov51aac422021-10-14 16:10:17 +01003183 unsigned int issue_flags)
Jens Axboe4d954c22020-02-27 07:31:19 -07003184{
Jens Axboedddb3e22020-06-04 11:27:01 -06003185 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01003186 struct io_buffer *kbuf = req->kbuf;
Jens Axboedddb3e22020-06-04 11:27:01 -06003187
Jens Axboedddb3e22020-06-04 11:27:01 -06003188 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3189 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003190 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003191 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003192 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003193 return -EINVAL;
3194
3195#ifdef CONFIG_COMPAT
3196 if (req->ctx->compat)
Pavel Begunkov51aac422021-10-14 16:10:17 +01003197 return io_compat_import(req, iov, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003198#endif
3199
Pavel Begunkov51aac422021-10-14 16:10:17 +01003200 return __io_iov_buffer_select(req, iov, issue_flags);
Jens Axboe4d954c22020-02-27 07:31:19 -07003201}
3202
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003203static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req,
3204 struct io_rw_state *s,
3205 unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003206{
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003207 struct iov_iter *iter = &s->iter;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003208 u8 opcode = req->opcode;
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003209 struct iovec *iovec;
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003210 void __user *buf;
3211 size_t sqe_len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003212 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003213
Pavel Begunkovf3251182021-11-23 00:07:48 +00003214 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
3215 ret = io_import_fixed(req, rw, iter);
3216 if (ret)
3217 return ERR_PTR(ret);
3218 return NULL;
3219 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003220
Jens Axboebcda7ba2020-02-23 16:42:51 -07003221 /* buffer index only valid with fixed read/write, or buffer select */
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003222 if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)))
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003223 return ERR_PTR(-EINVAL);
Jens Axboe9adbd452019-12-20 08:45:55 -07003224
Pavel Begunkovd1d681b2021-10-15 17:09:13 +01003225 buf = u64_to_user_ptr(req->rw.addr);
3226 sqe_len = req->rw.len;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003227
Jens Axboe3a6820f2019-12-22 15:19:35 -07003228 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003229 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01003230 buf = io_rw_buffer_select(req, &sqe_len, issue_flags);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003231 if (IS_ERR(buf))
Changcheng Deng898df242021-10-20 08:49:48 +00003232 return ERR_CAST(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003233 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003234 }
3235
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003236 ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter);
Pavel Begunkovf3251182021-11-23 00:07:48 +00003237 if (ret)
3238 return ERR_PTR(ret);
3239 return NULL;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003240 }
3241
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003242 iovec = s->fast_iov;
Jens Axboe4d954c22020-02-27 07:31:19 -07003243 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003244 ret = io_iov_buffer_select(req, iovec, issue_flags);
Pavel Begunkovf3251182021-11-23 00:07:48 +00003245 if (ret)
3246 return ERR_PTR(ret);
3247 iov_iter_init(iter, rw, iovec, 1, iovec->iov_len);
3248 return NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07003249 }
3250
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003251 ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003252 req->ctx->compat);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003253 if (unlikely(ret < 0))
3254 return ERR_PTR(ret);
3255 return iovec;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003256}
3257
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003258static inline int io_import_iovec(int rw, struct io_kiocb *req,
3259 struct iovec **iovec, struct io_rw_state *s,
3260 unsigned int issue_flags)
3261{
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003262 *iovec = __io_import_iovec(rw, req, s, issue_flags);
3263 if (unlikely(IS_ERR(*iovec)))
3264 return PTR_ERR(*iovec);
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003265
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003266 iov_iter_save_state(&s->iter, &s->iter_state);
Pavel Begunkovcaa8fe62021-10-15 17:09:14 +01003267 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003268}
3269
Jens Axboe0fef9482020-08-26 10:36:20 -06003270static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3271{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003272 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003273}
3274
Jens Axboe32960612019-09-23 11:05:34 -06003275/*
3276 * For files that don't have ->read_iter() and ->write_iter(), handle them
3277 * by looping over ->read() or ->write() manually.
3278 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003279static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003280{
Jens Axboe4017eb92020-10-22 14:14:12 -06003281 struct kiocb *kiocb = &req->rw.kiocb;
3282 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003283 ssize_t ret = 0;
3284
3285 /*
3286 * Don't support polled IO through this interface, and we can't
3287 * support non-blocking either. For the latter, this just causes
3288 * the kiocb to be handled from an async context.
3289 */
3290 if (kiocb->ki_flags & IOCB_HIPRI)
3291 return -EOPNOTSUPP;
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003292 if ((kiocb->ki_flags & IOCB_NOWAIT) &&
3293 !(kiocb->ki_filp->f_flags & O_NONBLOCK))
Jens Axboe32960612019-09-23 11:05:34 -06003294 return -EAGAIN;
3295
3296 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003297 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003298 ssize_t nr;
3299
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003300 if (!iov_iter_is_bvec(iter)) {
3301 iovec = iov_iter_iovec(iter);
3302 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003303 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3304 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003305 }
3306
Jens Axboe32960612019-09-23 11:05:34 -06003307 if (rw == READ) {
3308 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003309 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003310 } else {
3311 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003312 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003313 }
3314
3315 if (nr < 0) {
3316 if (!ret)
3317 ret = nr;
3318 break;
3319 }
Jens Axboe16c8d2d2021-09-12 06:45:07 -06003320 if (!iov_iter_is_bvec(iter)) {
3321 iov_iter_advance(iter, nr);
3322 } else {
3323 req->rw.len -= nr;
3324 req->rw.addr += nr;
3325 }
Jens Axboe32960612019-09-23 11:05:34 -06003326 ret += nr;
3327 if (nr != iovec.iov_len)
3328 break;
Jens Axboe32960612019-09-23 11:05:34 -06003329 }
3330
3331 return ret;
3332}
3333
Jens Axboeff6165b2020-08-13 09:47:43 -06003334static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3335 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003336{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003337 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003338
Pavel Begunkov538941e2021-10-14 16:10:15 +01003339 memcpy(&rw->s.iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003340 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003341 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003342 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003343 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003344 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003345 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003346 unsigned iov_off = 0;
3347
Pavel Begunkov538941e2021-10-14 16:10:15 +01003348 rw->s.iter.iov = rw->s.fast_iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003349 if (iter->iov != fast_iov) {
3350 iov_off = iter->iov - fast_iov;
Pavel Begunkov538941e2021-10-14 16:10:15 +01003351 rw->s.iter.iov += iov_off;
Jens Axboeff6165b2020-08-13 09:47:43 -06003352 }
Pavel Begunkov538941e2021-10-14 16:10:15 +01003353 if (rw->s.fast_iov != fast_iov)
3354 memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003355 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003356 } else {
3357 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003358 }
3359}
3360
Hao Xu8d4af682021-09-22 18:15:22 +08003361static inline bool io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003362{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003363 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3364 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
Pavel Begunkovd886e182021-10-04 20:02:56 +01003365 if (req->async_data) {
3366 req->flags |= REQ_F_ASYNC_DATA;
3367 return false;
3368 }
3369 return true;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003370}
3371
Jens Axboeff6165b2020-08-13 09:47:43 -06003372static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003373 struct io_rw_state *s, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003374{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003375 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003376 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003377 if (!req_has_async_data(req)) {
Jens Axboecd658692021-09-10 11:19:14 -06003378 struct io_async_rw *iorw;
3379
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003380 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003381 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003382 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003383 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003384
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003385 io_req_map_rw(req, iovec, s->fast_iov, &s->iter);
Jens Axboecd658692021-09-10 11:19:14 -06003386 iorw = req->async_data;
3387 /* we've copied and mapped the iter, ensure state is saved */
Pavel Begunkov538941e2021-10-14 16:10:15 +01003388 iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003389 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003390 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003391}
3392
Pavel Begunkov73debe62020-09-30 22:57:54 +03003393static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003394{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003395 struct io_async_rw *iorw = req->async_data;
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003396 struct iovec *iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003397 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003398
Pavel Begunkov51aac422021-10-14 16:10:17 +01003399 /* submission path, ->uring_lock should already be taken */
Hao Xu3b44b372021-10-18 21:34:31 +08003400 ret = io_import_iovec(rw, req, &iov, &iorw->s, 0);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003401 if (unlikely(ret < 0))
3402 return ret;
3403
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003404 iorw->bytes_done = 0;
3405 iorw->free_iovec = iov;
3406 if (iov)
3407 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003408 return 0;
3409}
3410
Pavel Begunkov73debe62020-09-30 22:57:54 +03003411static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003412{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003413 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3414 return -EBADF;
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01003415 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003416}
3417
Jens Axboec1dd91d2020-08-03 16:43:59 -06003418/*
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05003419 * This is our waitqueue callback handler, registered through __folio_lock_async()
Jens Axboec1dd91d2020-08-03 16:43:59 -06003420 * when we initially tried to do the IO with the iocb armed our waitqueue.
3421 * This gets called when the page is unlocked, and we generally expect that to
3422 * happen when the page IO is completed and the page is now uptodate. This will
3423 * queue a task_work based retry of the operation, attempting to copy the data
3424 * again. If the latter fails because the page was NOT uptodate, then we will
3425 * do a thread based blocking retry of the operation. That's the unexpected
3426 * slow path.
3427 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003428static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3429 int sync, void *arg)
3430{
3431 struct wait_page_queue *wpq;
3432 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003433 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003434
3435 wpq = container_of(wait, struct wait_page_queue, wait);
3436
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003437 if (!wake_page_match(wpq, key))
3438 return 0;
3439
Hao Xuc8d317a2020-09-29 20:00:45 +08003440 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003441 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003442 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003443 return 1;
3444}
3445
Jens Axboec1dd91d2020-08-03 16:43:59 -06003446/*
3447 * This controls whether a given IO request should be armed for async page
3448 * based retry. If we return false here, the request is handed to the async
3449 * worker threads for retry. If we're doing buffered reads on a regular file,
3450 * we prepare a private wait_page_queue entry and retry the operation. This
3451 * will either succeed because the page is now uptodate and unlocked, or it
3452 * will register a callback when the page is unlocked at IO completion. Through
3453 * that callback, io_uring uses task_work to setup a retry of the operation.
3454 * That retry will attempt the buffered read again. The retry will generally
3455 * succeed, or in rare cases where it fails, we then fall back to using the
3456 * async worker threads for a blocking retry.
3457 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003458static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003459{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003460 struct io_async_rw *rw = req->async_data;
3461 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003462 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003463
3464 /* never retry for NOWAIT, we just complete with -EAGAIN */
3465 if (req->flags & REQ_F_NOWAIT)
3466 return false;
3467
Jens Axboe227c0c92020-08-13 11:51:40 -06003468 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003469 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003470 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003471
Jens Axboebcf5a062020-05-22 09:24:42 -06003472 /*
3473 * just use poll if we can, and don't attempt if the fs doesn't
3474 * support callback based unlocks
3475 */
3476 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3477 return false;
3478
Jens Axboe3b2a4432020-08-16 10:58:43 -07003479 wait->wait.func = io_async_buf_func;
3480 wait->wait.private = req;
3481 wait->wait.flags = 0;
3482 INIT_LIST_HEAD(&wait->wait.entry);
3483 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003484 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003485 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003486 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003487}
3488
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003489static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003490{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003491 if (likely(req->file->f_op->read_iter))
Jens Axboebcf5a062020-05-22 09:24:42 -06003492 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003493 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003494 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003495 else
3496 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003497}
3498
Ming Lei7db30432021-08-21 23:07:51 +08003499static bool need_read_all(struct io_kiocb *req)
3500{
3501 return req->flags & REQ_F_ISREG ||
3502 S_ISBLK(file_inode(req->file)->i_mode);
3503}
3504
Pavel Begunkov889fca72021-02-10 00:03:09 +00003505static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003506{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003507 struct io_rw_state __s, *s = &__s;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003508 struct iovec *iovec;
Jens Axboe9adbd452019-12-20 08:45:55 -07003509 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003510 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovd886e182021-10-04 20:02:56 +01003511 struct io_async_rw *rw;
Jens Axboecd658692021-09-10 11:19:14 -06003512 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003513
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003514 if (!req_has_async_data(req)) {
3515 ret = io_import_iovec(READ, req, &iovec, s, issue_flags);
3516 if (unlikely(ret < 0))
3517 return ret;
3518 } else {
Pavel Begunkovd886e182021-10-04 20:02:56 +01003519 rw = req->async_data;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003520 s = &rw->s;
Jens Axboecd658692021-09-10 11:19:14 -06003521 /*
3522 * We come here from an earlier attempt, restore our state to
3523 * match in case it doesn't. It's cheap enough that we don't
3524 * need to make this conditional.
3525 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003526 iov_iter_restore(&s->iter, &s->iter_state);
Pavel Begunkov2846c482020-11-07 13:16:27 +00003527 iovec = NULL;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003528 }
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003529 req->result = iov_iter_count(&s->iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003530
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003531 if (force_nonblock) {
3532 /* If the file doesn't support async, just async punt */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003533 if (unlikely(!io_file_supports_nowait(req))) {
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003534 ret = io_setup_async_rw(req, iovec, s, true);
3535 return ret ?: -EAGAIN;
3536 }
Pavel Begunkova88fc402020-09-30 22:57:53 +03003537 kiocb->ki_flags |= IOCB_NOWAIT;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003538 } else {
3539 /* Ensure we clear previously set non-block flag */
3540 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003541 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003542
Jens Axboecd658692021-09-10 11:19:14 -06003543 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003544 if (unlikely(ret)) {
3545 kfree(iovec);
3546 return ret;
3547 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003548
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003549 ret = io_iter_do_read(req, &s->iter);
Jens Axboe32960612019-09-23 11:05:34 -06003550
Jens Axboe230d50d2021-04-01 20:41:15 -06003551 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003552 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003553 /* IOPOLL retry should happen for io-wq threads */
3554 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003555 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003556 /* no retry on NONBLOCK nor RWF_NOWAIT */
3557 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003558 goto done;
Jens Axboef38c7e32020-09-25 15:23:43 -06003559 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003560 } else if (ret == -EIOCBQUEUED) {
3561 goto out_free;
Pavel Begunkovf80a50a2021-10-14 16:10:13 +01003562 } else if (ret == req->result || ret <= 0 || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003563 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003564 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003565 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003566 }
3567
Jens Axboecd658692021-09-10 11:19:14 -06003568 /*
3569 * Don't depend on the iter state matching what was consumed, or being
3570 * untouched in case of error. Restore it and we'll advance it
3571 * manually if we need to.
3572 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003573 iov_iter_restore(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003574
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003575 ret2 = io_setup_async_rw(req, iovec, s, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003576 if (ret2)
3577 return ret2;
3578
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003579 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003580 rw = req->async_data;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003581 s = &rw->s;
Jens Axboecd658692021-09-10 11:19:14 -06003582 /*
3583 * Now use our persistent iterator and state, if we aren't already.
3584 * We've restored and mapped the iter to match.
3585 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003586
Pavel Begunkovb23df912021-02-04 13:52:04 +00003587 do {
Jens Axboecd658692021-09-10 11:19:14 -06003588 /*
3589 * We end up here because of a partial read, either from
3590 * above or inside this loop. Advance the iter by the bytes
3591 * that were consumed.
3592 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003593 iov_iter_advance(&s->iter, ret);
3594 if (!iov_iter_count(&s->iter))
Jens Axboecd658692021-09-10 11:19:14 -06003595 break;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003596 rw->bytes_done += ret;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003597 iov_iter_save_state(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003598
Pavel Begunkovb23df912021-02-04 13:52:04 +00003599 /* if we can retry, do so with the callbacks armed */
3600 if (!io_rw_should_retry(req)) {
3601 kiocb->ki_flags &= ~IOCB_WAITQ;
3602 return -EAGAIN;
3603 }
3604
3605 /*
3606 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3607 * we get -EIOCBQUEUED, then we'll get a notification when the
3608 * desired page gets unlocked. We can also get a partial read
3609 * here, and if we do, then just retry at the new offset.
3610 */
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003611 ret = io_iter_do_read(req, &s->iter);
Pavel Begunkovb23df912021-02-04 13:52:04 +00003612 if (ret == -EIOCBQUEUED)
3613 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003614 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003615 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003616 iov_iter_restore(&s->iter, &s->iter_state);
Jens Axboecd658692021-09-10 11:19:14 -06003617 } while (ret > 0);
Jens Axboe227c0c92020-08-13 11:51:40 -06003618done:
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003619 kiocb_done(req, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003620out_free:
3621 /* it's faster to check here then delegate to kfree */
3622 if (iovec)
3623 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003624 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003625}
3626
Pavel Begunkov73debe62020-09-30 22:57:54 +03003627static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003628{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003629 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3630 return -EBADF;
Jens Axboe3884b832021-10-25 13:45:12 -06003631 req->rw.kiocb.ki_hint = ki_hint_validate(file_write_hint(req->file));
Pavel Begunkovb9a6b8f2021-10-23 12:14:01 +01003632 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003633}
3634
Pavel Begunkov889fca72021-02-10 00:03:09 +00003635static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003636{
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003637 struct io_rw_state __s, *s = &__s;
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003638 struct iovec *iovec;
Jens Axboe9adbd452019-12-20 08:45:55 -07003639 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003640 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboecd658692021-09-10 11:19:14 -06003641 ssize_t ret, ret2;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003642
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003643 if (!req_has_async_data(req)) {
Pavel Begunkov5e49c972021-10-14 16:10:18 +01003644 ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags);
3645 if (unlikely(ret < 0))
Pavel Begunkov2846c482020-11-07 13:16:27 +00003646 return ret;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003647 } else {
3648 struct io_async_rw *rw = req->async_data;
3649
3650 s = &rw->s;
3651 iov_iter_restore(&s->iter, &s->iter_state);
3652 iovec = NULL;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003653 }
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003654 req->result = iov_iter_count(&s->iter);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003655
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003656 if (force_nonblock) {
3657 /* If the file doesn't support async, just async punt */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003658 if (unlikely(!io_file_supports_nowait(req)))
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003659 goto copy_iov;
3660
3661 /* file path doesn't support NOWAIT for non-direct_IO */
3662 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3663 (req->flags & REQ_F_ISREG))
3664 goto copy_iov;
3665
Pavel Begunkova88fc402020-09-30 22:57:53 +03003666 kiocb->ki_flags |= IOCB_NOWAIT;
Pavel Begunkov607b6fb2021-10-14 16:10:19 +01003667 } else {
3668 /* Ensure we clear previously set non-block flag */
3669 kiocb->ki_flags &= ~IOCB_NOWAIT;
3670 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003671
Jens Axboecd658692021-09-10 11:19:14 -06003672 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), req->result);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003673 if (unlikely(ret))
3674 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003675
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003676 /*
3677 * Open-code file_start_write here to grab freeze protection,
3678 * which will be released by another thread in
3679 * io_complete_rw(). Fool lockdep by telling it the lock got
3680 * released so that it doesn't complain about the held lock when
3681 * we return to userspace.
3682 */
3683 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003684 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003685 __sb_writers_release(file_inode(req->file)->i_sb,
3686 SB_FREEZE_WRITE);
3687 }
3688 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003689
Pavel Begunkov35645ac2021-10-17 00:07:09 +01003690 if (likely(req->file->f_op->write_iter))
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003691 ret2 = call_write_iter(req->file, kiocb, &s->iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003692 else if (req->file->f_op->write)
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003693 ret2 = loop_rw_iter(WRITE, req, &s->iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003694 else
3695 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003696
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003697 if (req->flags & REQ_F_REISSUE) {
3698 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003699 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003700 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003701
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003702 /*
3703 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3704 * retry them without IOCB_NOWAIT.
3705 */
3706 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3707 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003708 /* no retry on NONBLOCK nor RWF_NOWAIT */
3709 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003710 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003711 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003712 /* IOPOLL retry should happen for io-wq threads */
Noah Goldsteinb10841c2021-10-16 20:32:29 -05003713 if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboeeefdf302020-08-27 16:40:19 -06003714 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003715done:
Pavel Begunkov2ea537c2021-11-23 00:07:49 +00003716 kiocb_done(req, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003717 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003718copy_iov:
Pavel Begunkovc88598a2021-10-14 16:10:16 +01003719 iov_iter_restore(&s->iter, &s->iter_state);
3720 ret = io_setup_async_rw(req, iovec, s, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003721 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003722 }
Jens Axboe31b51512019-01-18 22:56:34 -07003723out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003724 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003725 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003726 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003727 return ret;
3728}
3729
Jens Axboe80a261f2020-09-28 14:23:58 -06003730static int io_renameat_prep(struct io_kiocb *req,
3731 const struct io_uring_sqe *sqe)
3732{
3733 struct io_rename *ren = &req->rename;
3734 const char __user *oldf, *newf;
3735
Jens Axboeed7eb252021-06-23 09:04:13 -06003736 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3737 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003738 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003739 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003740 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3741 return -EBADF;
3742
3743 ren->old_dfd = READ_ONCE(sqe->fd);
3744 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3745 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3746 ren->new_dfd = READ_ONCE(sqe->len);
3747 ren->flags = READ_ONCE(sqe->rename_flags);
3748
3749 ren->oldpath = getname(oldf);
3750 if (IS_ERR(ren->oldpath))
3751 return PTR_ERR(ren->oldpath);
3752
3753 ren->newpath = getname(newf);
3754 if (IS_ERR(ren->newpath)) {
3755 putname(ren->oldpath);
3756 return PTR_ERR(ren->newpath);
3757 }
3758
3759 req->flags |= REQ_F_NEED_CLEANUP;
3760 return 0;
3761}
3762
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003763static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003764{
3765 struct io_rename *ren = &req->rename;
3766 int ret;
3767
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003768 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003769 return -EAGAIN;
3770
3771 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3772 ren->newpath, ren->flags);
3773
3774 req->flags &= ~REQ_F_NEED_CLEANUP;
3775 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003776 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003777 io_req_complete(req, ret);
3778 return 0;
3779}
3780
Jens Axboe14a11432020-09-28 14:27:37 -06003781static int io_unlinkat_prep(struct io_kiocb *req,
3782 const struct io_uring_sqe *sqe)
3783{
3784 struct io_unlink *un = &req->unlink;
3785 const char __user *fname;
3786
Jens Axboe22634bc2021-06-23 09:07:45 -06003787 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3788 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003789 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3790 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003791 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003792 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3793 return -EBADF;
3794
3795 un->dfd = READ_ONCE(sqe->fd);
3796
3797 un->flags = READ_ONCE(sqe->unlink_flags);
3798 if (un->flags & ~AT_REMOVEDIR)
3799 return -EINVAL;
3800
3801 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3802 un->filename = getname(fname);
3803 if (IS_ERR(un->filename))
3804 return PTR_ERR(un->filename);
3805
3806 req->flags |= REQ_F_NEED_CLEANUP;
3807 return 0;
3808}
3809
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003810static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003811{
3812 struct io_unlink *un = &req->unlink;
3813 int ret;
3814
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003815 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003816 return -EAGAIN;
3817
3818 if (un->flags & AT_REMOVEDIR)
3819 ret = do_rmdir(un->dfd, un->filename);
3820 else
3821 ret = do_unlinkat(un->dfd, un->filename);
3822
3823 req->flags &= ~REQ_F_NEED_CLEANUP;
3824 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003825 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003826 io_req_complete(req, ret);
3827 return 0;
3828}
3829
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003830static int io_mkdirat_prep(struct io_kiocb *req,
3831 const struct io_uring_sqe *sqe)
3832{
3833 struct io_mkdir *mkd = &req->mkdir;
3834 const char __user *fname;
3835
3836 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3837 return -EINVAL;
3838 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3839 sqe->splice_fd_in)
3840 return -EINVAL;
3841 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3842 return -EBADF;
3843
3844 mkd->dfd = READ_ONCE(sqe->fd);
3845 mkd->mode = READ_ONCE(sqe->len);
3846
3847 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3848 mkd->filename = getname(fname);
3849 if (IS_ERR(mkd->filename))
3850 return PTR_ERR(mkd->filename);
3851
3852 req->flags |= REQ_F_NEED_CLEANUP;
3853 return 0;
3854}
3855
Pavel Begunkov04f34082021-10-14 16:10:12 +01003856static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003857{
3858 struct io_mkdir *mkd = &req->mkdir;
3859 int ret;
3860
3861 if (issue_flags & IO_URING_F_NONBLOCK)
3862 return -EAGAIN;
3863
3864 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3865
3866 req->flags &= ~REQ_F_NEED_CLEANUP;
3867 if (ret < 0)
3868 req_set_fail(req);
3869 io_req_complete(req, ret);
3870 return 0;
3871}
3872
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003873static int io_symlinkat_prep(struct io_kiocb *req,
3874 const struct io_uring_sqe *sqe)
3875{
3876 struct io_symlink *sl = &req->symlink;
3877 const char __user *oldpath, *newpath;
3878
3879 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3880 return -EINVAL;
3881 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3882 sqe->splice_fd_in)
3883 return -EINVAL;
3884 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3885 return -EBADF;
3886
3887 sl->new_dfd = READ_ONCE(sqe->fd);
3888 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3889 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3890
3891 sl->oldpath = getname(oldpath);
3892 if (IS_ERR(sl->oldpath))
3893 return PTR_ERR(sl->oldpath);
3894
3895 sl->newpath = getname(newpath);
3896 if (IS_ERR(sl->newpath)) {
3897 putname(sl->oldpath);
3898 return PTR_ERR(sl->newpath);
3899 }
3900
3901 req->flags |= REQ_F_NEED_CLEANUP;
3902 return 0;
3903}
3904
Pavel Begunkov04f34082021-10-14 16:10:12 +01003905static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003906{
3907 struct io_symlink *sl = &req->symlink;
3908 int ret;
3909
3910 if (issue_flags & IO_URING_F_NONBLOCK)
3911 return -EAGAIN;
3912
3913 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3914
3915 req->flags &= ~REQ_F_NEED_CLEANUP;
3916 if (ret < 0)
3917 req_set_fail(req);
3918 io_req_complete(req, ret);
3919 return 0;
3920}
3921
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003922static int io_linkat_prep(struct io_kiocb *req,
3923 const struct io_uring_sqe *sqe)
3924{
3925 struct io_hardlink *lnk = &req->hardlink;
3926 const char __user *oldf, *newf;
3927
3928 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3929 return -EINVAL;
3930 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3931 return -EINVAL;
3932 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3933 return -EBADF;
3934
3935 lnk->old_dfd = READ_ONCE(sqe->fd);
3936 lnk->new_dfd = READ_ONCE(sqe->len);
3937 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3938 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3939 lnk->flags = READ_ONCE(sqe->hardlink_flags);
3940
3941 lnk->oldpath = getname(oldf);
3942 if (IS_ERR(lnk->oldpath))
3943 return PTR_ERR(lnk->oldpath);
3944
3945 lnk->newpath = getname(newf);
3946 if (IS_ERR(lnk->newpath)) {
3947 putname(lnk->oldpath);
3948 return PTR_ERR(lnk->newpath);
3949 }
3950
3951 req->flags |= REQ_F_NEED_CLEANUP;
3952 return 0;
3953}
3954
Pavel Begunkov04f34082021-10-14 16:10:12 +01003955static int io_linkat(struct io_kiocb *req, unsigned int issue_flags)
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003956{
3957 struct io_hardlink *lnk = &req->hardlink;
3958 int ret;
3959
3960 if (issue_flags & IO_URING_F_NONBLOCK)
3961 return -EAGAIN;
3962
3963 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
3964 lnk->newpath, lnk->flags);
3965
3966 req->flags &= ~REQ_F_NEED_CLEANUP;
3967 if (ret < 0)
3968 req_set_fail(req);
3969 io_req_complete(req, ret);
3970 return 0;
3971}
3972
Jens Axboe36f4fa62020-09-05 11:14:22 -06003973static int io_shutdown_prep(struct io_kiocb *req,
3974 const struct io_uring_sqe *sqe)
3975{
3976#if defined(CONFIG_NET)
3977 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3978 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003979 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3980 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003981 return -EINVAL;
3982
3983 req->shutdown.how = READ_ONCE(sqe->len);
3984 return 0;
3985#else
3986 return -EOPNOTSUPP;
3987#endif
3988}
3989
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003990static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003991{
3992#if defined(CONFIG_NET)
3993 struct socket *sock;
3994 int ret;
3995
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003996 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003997 return -EAGAIN;
3998
Linus Torvalds48aba792020-12-16 12:44:05 -08003999 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06004000 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08004001 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06004002
4003 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07004004 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004005 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06004006 io_req_complete(req, ret);
4007 return 0;
4008#else
4009 return -EOPNOTSUPP;
4010#endif
4011}
4012
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004013static int __io_splice_prep(struct io_kiocb *req,
4014 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004015{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004016 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004017 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004018
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004019 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4020 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004021
4022 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004023 sp->len = READ_ONCE(sqe->len);
4024 sp->flags = READ_ONCE(sqe->splice_flags);
4025
4026 if (unlikely(sp->flags & ~valid_flags))
4027 return -EINVAL;
4028
Pavel Begunkov62906e82021-08-10 14:52:47 +01004029 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01004030 (sp->flags & SPLICE_F_FD_IN_FIXED));
4031 if (!sp->file_in)
4032 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004033 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004034 return 0;
4035}
4036
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004037static int io_tee_prep(struct io_kiocb *req,
4038 const struct io_uring_sqe *sqe)
4039{
4040 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
4041 return -EINVAL;
4042 return __io_splice_prep(req, sqe);
4043}
4044
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004045static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004046{
4047 struct io_splice *sp = &req->splice;
4048 struct file *in = sp->file_in;
4049 struct file *out = sp->file_out;
4050 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4051 long ret = 0;
4052
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004053 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004054 return -EAGAIN;
4055 if (sp->len)
4056 ret = do_tee(in, out, sp->len, flags);
4057
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004058 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4059 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004060 req->flags &= ~REQ_F_NEED_CLEANUP;
4061
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004062 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004063 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004064 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004065 return 0;
4066}
4067
4068static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4069{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004070 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004071
4072 sp->off_in = READ_ONCE(sqe->splice_off_in);
4073 sp->off_out = READ_ONCE(sqe->off);
4074 return __io_splice_prep(req, sqe);
4075}
4076
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004077static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004078{
4079 struct io_splice *sp = &req->splice;
4080 struct file *in = sp->file_in;
4081 struct file *out = sp->file_out;
4082 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4083 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004084 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004085
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004086 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004087 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004088
4089 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4090 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004091
Jens Axboe948a7742020-05-17 14:21:38 -06004092 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004093 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004094
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004095 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4096 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004097 req->flags &= ~REQ_F_NEED_CLEANUP;
4098
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004099 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004100 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004101 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004102 return 0;
4103}
4104
Jens Axboe2b188cc2019-01-07 10:46:33 -07004105/*
4106 * IORING_OP_NOP just posts a completion event, nothing else.
4107 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004108static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004109{
4110 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004111
Jens Axboedef596e2019-01-09 08:59:42 -07004112 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4113 return -EINVAL;
4114
Pavel Begunkov889fca72021-02-10 00:03:09 +00004115 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004116 return 0;
4117}
4118
Pavel Begunkov1155c762021-02-18 18:29:38 +00004119static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004120{
Jens Axboe6b063142019-01-10 22:13:58 -07004121 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004122
Jens Axboe09bb8392019-03-13 12:39:28 -06004123 if (!req->file)
4124 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004125
Jens Axboe6b063142019-01-10 22:13:58 -07004126 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004127 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004128 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4129 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004130 return -EINVAL;
4131
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004132 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4133 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4134 return -EINVAL;
4135
4136 req->sync.off = READ_ONCE(sqe->off);
4137 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004138 return 0;
4139}
4140
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004141static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004142{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004143 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004144 int ret;
4145
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004146 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004147 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004148 return -EAGAIN;
4149
Jens Axboe9adbd452019-12-20 08:45:55 -07004150 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004151 end > 0 ? end : LLONG_MAX,
4152 req->sync.flags & IORING_FSYNC_DATASYNC);
4153 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004154 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004155 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004156 return 0;
4157}
4158
Jens Axboed63d1b52019-12-10 10:38:56 -07004159static int io_fallocate_prep(struct io_kiocb *req,
4160 const struct io_uring_sqe *sqe)
4161{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004162 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4163 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004164 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004165 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4166 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004167
4168 req->sync.off = READ_ONCE(sqe->off);
4169 req->sync.len = READ_ONCE(sqe->addr);
4170 req->sync.mode = READ_ONCE(sqe->len);
4171 return 0;
4172}
4173
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004174static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004175{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004176 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004177
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004178 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004179 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004180 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004181 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4182 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004183 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004184 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004185 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004186 return 0;
4187}
4188
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004189static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004190{
Jens Axboef8748882020-01-08 17:47:02 -07004191 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004192 int ret;
4193
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004194 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4195 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004196 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004197 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004198 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004199 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004200
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004201 /* open.how should be already initialised */
4202 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004203 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004204
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004205 req->open.dfd = READ_ONCE(sqe->fd);
4206 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004207 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004208 if (IS_ERR(req->open.filename)) {
4209 ret = PTR_ERR(req->open.filename);
4210 req->open.filename = NULL;
4211 return ret;
4212 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004213
4214 req->open.file_slot = READ_ONCE(sqe->file_index);
4215 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4216 return -EINVAL;
4217
Jens Axboe4022e7a2020-03-19 19:23:18 -06004218 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004219 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004220 return 0;
4221}
4222
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004223static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4224{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004225 u64 mode = READ_ONCE(sqe->len);
4226 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004227
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004228 req->open.how = build_open_how(flags, mode);
4229 return __io_openat_prep(req, sqe);
4230}
4231
Jens Axboecebdb982020-01-08 17:59:24 -07004232static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4233{
4234 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004235 size_t len;
4236 int ret;
4237
Jens Axboecebdb982020-01-08 17:59:24 -07004238 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4239 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004240 if (len < OPEN_HOW_SIZE_VER0)
4241 return -EINVAL;
4242
4243 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4244 len);
4245 if (ret)
4246 return ret;
4247
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004248 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004249}
4250
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004251static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004252{
4253 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004254 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004255 bool resolve_nonblock, nonblock_set;
4256 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004257 int ret;
4258
Jens Axboecebdb982020-01-08 17:59:24 -07004259 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004260 if (ret)
4261 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004262 nonblock_set = op.open_flag & O_NONBLOCK;
4263 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004264 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004265 /*
4266 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4267 * it'll always -EAGAIN
4268 */
4269 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4270 return -EAGAIN;
4271 op.lookup_flags |= LOOKUP_CACHED;
4272 op.open_flag |= O_NONBLOCK;
4273 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004274
Pavel Begunkovb9445592021-08-25 12:25:45 +01004275 if (!fixed) {
4276 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4277 if (ret < 0)
4278 goto err;
4279 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004280
4281 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004282 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004283 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004284 * We could hang on to this 'fd' on retrying, but seems like
4285 * marginal gain for something that is now known to be a slower
4286 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004287 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004288 if (!fixed)
4289 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004290
4291 ret = PTR_ERR(file);
4292 /* only retry if RESOLVE_CACHED wasn't already set by application */
4293 if (ret == -EAGAIN &&
4294 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4295 return -EAGAIN;
4296 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004297 }
4298
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004299 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4300 file->f_flags &= ~O_NONBLOCK;
4301 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004302
4303 if (!fixed)
4304 fd_install(ret, file);
4305 else
4306 ret = io_install_fixed_file(req, file, issue_flags,
4307 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004308err:
4309 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004310 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004311 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004312 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004313 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004314 return 0;
4315}
4316
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004317static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004318{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004319 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004320}
4321
Jens Axboe067524e2020-03-02 16:32:28 -07004322static int io_remove_buffers_prep(struct io_kiocb *req,
4323 const struct io_uring_sqe *sqe)
4324{
4325 struct io_provide_buf *p = &req->pbuf;
4326 u64 tmp;
4327
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004328 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4329 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004330 return -EINVAL;
4331
4332 tmp = READ_ONCE(sqe->fd);
4333 if (!tmp || tmp > USHRT_MAX)
4334 return -EINVAL;
4335
4336 memset(p, 0, sizeof(*p));
4337 p->nbufs = tmp;
4338 p->bgid = READ_ONCE(sqe->buf_group);
4339 return 0;
4340}
4341
4342static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4343 int bgid, unsigned nbufs)
4344{
4345 unsigned i = 0;
4346
4347 /* shouldn't happen */
4348 if (!nbufs)
4349 return 0;
4350
4351 /* the head kbuf is the list itself */
4352 while (!list_empty(&buf->list)) {
4353 struct io_buffer *nxt;
4354
4355 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4356 list_del(&nxt->list);
4357 kfree(nxt);
4358 if (++i == nbufs)
4359 return i;
4360 }
4361 i++;
4362 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004363 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004364
4365 return i;
4366}
4367
Pavel Begunkov889fca72021-02-10 00:03:09 +00004368static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004369{
4370 struct io_provide_buf *p = &req->pbuf;
4371 struct io_ring_ctx *ctx = req->ctx;
4372 struct io_buffer *head;
4373 int ret = 0;
Hao Xu3b44b372021-10-18 21:34:31 +08004374 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboe067524e2020-03-02 16:32:28 -07004375
Hao Xu3b44b372021-10-18 21:34:31 +08004376 io_ring_submit_lock(ctx, needs_lock);
Jens Axboe067524e2020-03-02 16:32:28 -07004377
4378 lockdep_assert_held(&ctx->uring_lock);
4379
4380 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004381 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004382 if (head)
4383 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004384 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004385 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004386
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004387 /* complete before unlock, IOPOLL may need the lock */
4388 __io_req_complete(req, issue_flags, ret, 0);
Hao Xu3b44b372021-10-18 21:34:31 +08004389 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboe067524e2020-03-02 16:32:28 -07004390 return 0;
4391}
4392
Jens Axboeddf0322d2020-02-23 16:41:33 -07004393static int io_provide_buffers_prep(struct io_kiocb *req,
4394 const struct io_uring_sqe *sqe)
4395{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004396 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004397 struct io_provide_buf *p = &req->pbuf;
4398 u64 tmp;
4399
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004400 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004401 return -EINVAL;
4402
4403 tmp = READ_ONCE(sqe->fd);
4404 if (!tmp || tmp > USHRT_MAX)
4405 return -E2BIG;
4406 p->nbufs = tmp;
4407 p->addr = READ_ONCE(sqe->addr);
4408 p->len = READ_ONCE(sqe->len);
4409
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004410 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4411 &size))
4412 return -EOVERFLOW;
4413 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4414 return -EOVERFLOW;
4415
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004416 size = (unsigned long)p->len * p->nbufs;
4417 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004418 return -EFAULT;
4419
4420 p->bgid = READ_ONCE(sqe->buf_group);
4421 tmp = READ_ONCE(sqe->off);
4422 if (tmp > USHRT_MAX)
4423 return -E2BIG;
4424 p->bid = tmp;
4425 return 0;
4426}
4427
4428static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4429{
4430 struct io_buffer *buf;
4431 u64 addr = pbuf->addr;
4432 int i, bid = pbuf->bid;
4433
4434 for (i = 0; i < pbuf->nbufs; i++) {
Jens Axboe9990da92021-09-24 07:39:08 -06004435 buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004436 if (!buf)
4437 break;
4438
4439 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004440 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004441 buf->bid = bid;
4442 addr += pbuf->len;
4443 bid++;
4444 if (!*head) {
4445 INIT_LIST_HEAD(&buf->list);
4446 *head = buf;
4447 } else {
4448 list_add_tail(&buf->list, &(*head)->list);
4449 }
4450 }
4451
4452 return i ? i : -ENOMEM;
4453}
4454
Pavel Begunkov889fca72021-02-10 00:03:09 +00004455static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004456{
4457 struct io_provide_buf *p = &req->pbuf;
4458 struct io_ring_ctx *ctx = req->ctx;
4459 struct io_buffer *head, *list;
4460 int ret = 0;
Hao Xu3b44b372021-10-18 21:34:31 +08004461 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004462
Hao Xu3b44b372021-10-18 21:34:31 +08004463 io_ring_submit_lock(ctx, needs_lock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004464
4465 lockdep_assert_held(&ctx->uring_lock);
4466
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004467 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004468
4469 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004470 if (ret >= 0 && !list) {
4471 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4472 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004473 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004474 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004475 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004476 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004477 /* complete before unlock, IOPOLL may need the lock */
4478 __io_req_complete(req, issue_flags, ret, 0);
Hao Xu3b44b372021-10-18 21:34:31 +08004479 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004480 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004481}
4482
Jens Axboe3e4827b2020-01-08 15:18:09 -07004483static int io_epoll_ctl_prep(struct io_kiocb *req,
4484 const struct io_uring_sqe *sqe)
4485{
4486#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004487 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004488 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004489 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004490 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004491
4492 req->epoll.epfd = READ_ONCE(sqe->fd);
4493 req->epoll.op = READ_ONCE(sqe->len);
4494 req->epoll.fd = READ_ONCE(sqe->off);
4495
4496 if (ep_op_has_event(req->epoll.op)) {
4497 struct epoll_event __user *ev;
4498
4499 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4500 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4501 return -EFAULT;
4502 }
4503
4504 return 0;
4505#else
4506 return -EOPNOTSUPP;
4507#endif
4508}
4509
Pavel Begunkov889fca72021-02-10 00:03:09 +00004510static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004511{
4512#if defined(CONFIG_EPOLL)
4513 struct io_epoll *ie = &req->epoll;
4514 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004515 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004516
4517 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4518 if (force_nonblock && ret == -EAGAIN)
4519 return -EAGAIN;
4520
4521 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004522 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004523 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004524 return 0;
4525#else
4526 return -EOPNOTSUPP;
4527#endif
4528}
4529
Jens Axboec1ca7572019-12-25 22:18:28 -07004530static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4531{
4532#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004533 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004534 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004535 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4536 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004537
4538 req->madvise.addr = READ_ONCE(sqe->addr);
4539 req->madvise.len = READ_ONCE(sqe->len);
4540 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4541 return 0;
4542#else
4543 return -EOPNOTSUPP;
4544#endif
4545}
4546
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004547static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004548{
4549#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4550 struct io_madvise *ma = &req->madvise;
4551 int ret;
4552
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004553 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004554 return -EAGAIN;
4555
Minchan Kim0726b012020-10-17 16:14:50 -07004556 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004557 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004558 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004559 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004560 return 0;
4561#else
4562 return -EOPNOTSUPP;
4563#endif
4564}
4565
Jens Axboe4840e412019-12-25 22:03:45 -07004566static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4567{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004568 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004569 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004570 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4571 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004572
4573 req->fadvise.offset = READ_ONCE(sqe->off);
4574 req->fadvise.len = READ_ONCE(sqe->len);
4575 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4576 return 0;
4577}
4578
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004579static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004580{
4581 struct io_fadvise *fa = &req->fadvise;
4582 int ret;
4583
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004584 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004585 switch (fa->advice) {
4586 case POSIX_FADV_NORMAL:
4587 case POSIX_FADV_RANDOM:
4588 case POSIX_FADV_SEQUENTIAL:
4589 break;
4590 default:
4591 return -EAGAIN;
4592 }
4593 }
Jens Axboe4840e412019-12-25 22:03:45 -07004594
4595 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4596 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004597 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004598 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004599 return 0;
4600}
4601
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004602static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4603{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004604 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004605 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004606 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004607 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004608 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004609 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004610
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004611 req->statx.dfd = READ_ONCE(sqe->fd);
4612 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004613 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004614 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4615 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004616
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004617 return 0;
4618}
4619
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004620static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004621{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004622 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004623 int ret;
4624
Pavel Begunkov59d70012021-03-22 01:58:30 +00004625 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004626 return -EAGAIN;
4627
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004628 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4629 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004630
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004631 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004632 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004633 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004634 return 0;
4635}
4636
Jens Axboeb5dba592019-12-11 14:02:38 -07004637static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4638{
Jens Axboe14587a462020-09-05 11:36:08 -06004639 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004640 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004641 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004642 sqe->rw_flags || sqe->buf_index)
Jens Axboeb5dba592019-12-11 14:02:38 -07004643 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004644 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004645 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004646
4647 req->close.fd = READ_ONCE(sqe->fd);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004648 req->close.file_slot = READ_ONCE(sqe->file_index);
4649 if (req->close.file_slot && req->close.fd)
4650 return -EINVAL;
4651
Jens Axboeb5dba592019-12-11 14:02:38 -07004652 return 0;
4653}
4654
Pavel Begunkov889fca72021-02-10 00:03:09 +00004655static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004656{
Jens Axboe9eac1902021-01-19 15:50:37 -07004657 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004658 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004659 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004660 struct file *file = NULL;
4661 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004662
Pavel Begunkov7df778b2021-09-24 20:04:29 +01004663 if (req->close.file_slot) {
4664 ret = io_close_fixed(req, issue_flags);
4665 goto err;
4666 }
4667
Jens Axboe9eac1902021-01-19 15:50:37 -07004668 spin_lock(&files->file_lock);
4669 fdt = files_fdtable(files);
4670 if (close->fd >= fdt->max_fds) {
4671 spin_unlock(&files->file_lock);
4672 goto err;
4673 }
4674 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004675 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004676 spin_unlock(&files->file_lock);
4677 file = NULL;
4678 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004679 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004680
4681 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004682 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004683 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004684 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004685 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004686
Jens Axboe9eac1902021-01-19 15:50:37 -07004687 ret = __close_fd_get_file(close->fd, &file);
4688 spin_unlock(&files->file_lock);
4689 if (ret < 0) {
4690 if (ret == -ENOENT)
4691 ret = -EBADF;
4692 goto err;
4693 }
4694
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004695 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004696 ret = filp_close(file, current->files);
4697err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004698 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004699 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004700 if (file)
4701 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004702 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004703 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004704}
4705
Pavel Begunkov1155c762021-02-18 18:29:38 +00004706static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004707{
4708 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004709
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004710 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4711 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004712 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4713 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004714 return -EINVAL;
4715
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004716 req->sync.off = READ_ONCE(sqe->off);
4717 req->sync.len = READ_ONCE(sqe->len);
4718 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004719 return 0;
4720}
4721
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004722static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004723{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004724 int ret;
4725
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004726 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004727 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004728 return -EAGAIN;
4729
Jens Axboe9adbd452019-12-20 08:45:55 -07004730 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004731 req->sync.flags);
4732 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004733 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004734 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004735 return 0;
4736}
4737
YueHaibing469956e2020-03-04 15:53:52 +08004738#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004739static int io_setup_async_msg(struct io_kiocb *req,
4740 struct io_async_msghdr *kmsg)
4741{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004742 struct io_async_msghdr *async_msg = req->async_data;
4743
4744 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004745 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004746 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004747 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004748 return -ENOMEM;
4749 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004750 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004751 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004752 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004753 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004754 /* if were using fast_iov, set it to the new one */
4755 if (!async_msg->free_iov)
4756 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4757
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004758 return -EAGAIN;
4759}
4760
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004761static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4762 struct io_async_msghdr *iomsg)
4763{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004764 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004765 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004766 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004767 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004768}
4769
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004770static int io_sendmsg_prep_async(struct io_kiocb *req)
4771{
4772 int ret;
4773
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004774 ret = io_sendmsg_copy_hdr(req, req->async_data);
4775 if (!ret)
4776 req->flags |= REQ_F_NEED_CLEANUP;
4777 return ret;
4778}
4779
Jens Axboe3529d8c2019-12-19 18:24:38 -07004780static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004781{
Jens Axboee47293f2019-12-20 08:58:21 -07004782 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004783
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004784 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4785 return -EINVAL;
4786
Pavel Begunkov270a5942020-07-12 20:41:04 +03004787 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004788 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004789 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4790 if (sr->msg_flags & MSG_DONTWAIT)
4791 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004792
Jens Axboed8768362020-02-27 14:17:49 -07004793#ifdef CONFIG_COMPAT
4794 if (req->ctx->compat)
4795 sr->msg_flags |= MSG_CMSG_COMPAT;
4796#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004797 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004798}
4799
Pavel Begunkov889fca72021-02-10 00:03:09 +00004800static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004801{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004802 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004803 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004804 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004805 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004806 int ret;
4807
Florent Revestdba4a922020-12-04 12:36:04 +01004808 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004809 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004810 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004811
Pavel Begunkovd886e182021-10-04 20:02:56 +01004812 if (req_has_async_data(req)) {
4813 kmsg = req->async_data;
4814 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004815 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004816 if (ret)
4817 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004818 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004819 }
4820
Pavel Begunkov04411802021-04-01 15:44:00 +01004821 flags = req->sr_msg.msg_flags;
4822 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004823 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004824 if (flags & MSG_WAITALL)
4825 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4826
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004827 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004828
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004829 if (ret < min_ret) {
4830 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
4831 return io_setup_async_msg(req, kmsg);
4832 if (ret == -ERESTARTSYS)
4833 ret = -EINTR;
4834 req_set_fail(req);
4835 }
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004836 /* fast path, check for non-NULL to avoid function call */
4837 if (kmsg->free_iov)
4838 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004839 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov889fca72021-02-10 00:03:09 +00004840 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004841 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004842}
4843
Pavel Begunkov889fca72021-02-10 00:03:09 +00004844static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004845{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004846 struct io_sr_msg *sr = &req->sr_msg;
4847 struct msghdr msg;
4848 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004849 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004850 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004851 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004852 int ret;
4853
Florent Revestdba4a922020-12-04 12:36:04 +01004854 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004855 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004856 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004857
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004858 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4859 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004860 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004861
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004862 msg.msg_name = NULL;
4863 msg.msg_control = NULL;
4864 msg.msg_controllen = 0;
4865 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004866
Pavel Begunkov04411802021-04-01 15:44:00 +01004867 flags = req->sr_msg.msg_flags;
4868 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004869 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004870 if (flags & MSG_WAITALL)
4871 min_ret = iov_iter_count(&msg.msg_iter);
4872
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004873 msg.msg_flags = flags;
4874 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004875 if (ret < min_ret) {
4876 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
4877 return -EAGAIN;
4878 if (ret == -ERESTARTSYS)
4879 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004880 req_set_fail(req);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00004881 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004882 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004883 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004884}
4885
Pavel Begunkov1400e692020-07-12 20:41:05 +03004886static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4887 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004888{
4889 struct io_sr_msg *sr = &req->sr_msg;
4890 struct iovec __user *uiov;
4891 size_t iov_len;
4892 int ret;
4893
Pavel Begunkov1400e692020-07-12 20:41:05 +03004894 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4895 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004896 if (ret)
4897 return ret;
4898
4899 if (req->flags & REQ_F_BUFFER_SELECT) {
4900 if (iov_len > 1)
4901 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004902 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004903 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004904 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004905 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004906 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004907 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004908 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004909 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004910 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004911 if (ret > 0)
4912 ret = 0;
4913 }
4914
4915 return ret;
4916}
4917
4918#ifdef CONFIG_COMPAT
4919static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004920 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004921{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004922 struct io_sr_msg *sr = &req->sr_msg;
4923 struct compat_iovec __user *uiov;
4924 compat_uptr_t ptr;
4925 compat_size_t len;
4926 int ret;
4927
Pavel Begunkov4af34172021-04-11 01:46:30 +01004928 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4929 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004930 if (ret)
4931 return ret;
4932
4933 uiov = compat_ptr(ptr);
4934 if (req->flags & REQ_F_BUFFER_SELECT) {
4935 compat_ssize_t clen;
4936
4937 if (len > 1)
4938 return -EINVAL;
4939 if (!access_ok(uiov, sizeof(*uiov)))
4940 return -EFAULT;
4941 if (__get_user(clen, &uiov->iov_len))
4942 return -EFAULT;
4943 if (clen < 0)
4944 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004945 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004946 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004947 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004948 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004949 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004950 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004951 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004952 if (ret < 0)
4953 return ret;
4954 }
4955
4956 return 0;
4957}
Jens Axboe03b12302019-12-02 18:50:25 -07004958#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004959
Pavel Begunkov1400e692020-07-12 20:41:05 +03004960static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4961 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004962{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004963 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004964
4965#ifdef CONFIG_COMPAT
4966 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004967 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004968#endif
4969
Pavel Begunkov1400e692020-07-12 20:41:05 +03004970 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004971}
4972
Jens Axboebcda7ba2020-02-23 16:42:51 -07004973static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov51aac422021-10-14 16:10:17 +01004974 unsigned int issue_flags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004975{
4976 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004977
Pavel Begunkov51aac422021-10-14 16:10:17 +01004978 return io_buffer_select(req, &sr->len, sr->bgid, issue_flags);
Jens Axboe03b12302019-12-02 18:50:25 -07004979}
4980
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004981static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4982{
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01004983 return io_put_kbuf(req, req->kbuf);
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004984}
4985
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004986static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004987{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004988 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004989
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004990 ret = io_recvmsg_copy_hdr(req, req->async_data);
4991 if (!ret)
4992 req->flags |= REQ_F_NEED_CLEANUP;
4993 return ret;
4994}
4995
4996static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4997{
4998 struct io_sr_msg *sr = &req->sr_msg;
4999
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03005000 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5001 return -EINVAL;
5002
Pavel Begunkov270a5942020-07-12 20:41:04 +03005003 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07005004 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005005 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01005006 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
5007 if (sr->msg_flags & MSG_DONTWAIT)
5008 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005009
Jens Axboed8768362020-02-27 14:17:49 -07005010#ifdef CONFIG_COMPAT
5011 if (req->ctx->compat)
5012 sr->msg_flags |= MSG_CMSG_COMPAT;
5013#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005014 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07005015}
5016
Pavel Begunkov889fca72021-02-10 00:03:09 +00005017static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07005018{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005019 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005020 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005021 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005022 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005023 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07005024 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005025 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005026
Florent Revestdba4a922020-12-04 12:36:04 +01005027 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005028 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005029 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005030
Pavel Begunkovd886e182021-10-04 20:02:56 +01005031 if (req_has_async_data(req)) {
5032 kmsg = req->async_data;
5033 } else {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005034 ret = io_recvmsg_copy_hdr(req, &iomsg);
5035 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03005036 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005037 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005038 }
5039
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005040 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01005041 kbuf = io_recv_buffer_select(req, issue_flags);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005042 if (IS_ERR(kbuf))
5043 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005044 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00005045 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
5046 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005047 1, req->sr_msg.len);
5048 }
5049
Pavel Begunkov04411802021-04-01 15:44:00 +01005050 flags = req->sr_msg.msg_flags;
5051 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005052 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005053 if (flags & MSG_WAITALL)
5054 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
5055
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005056 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
5057 kmsg->uaddr, flags);
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005058 if (ret < min_ret) {
5059 if (ret == -EAGAIN && force_nonblock)
5060 return io_setup_async_msg(req, kmsg);
5061 if (ret == -ERESTARTSYS)
5062 ret = -EINTR;
5063 req_set_fail(req);
5064 } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
5065 req_set_fail(req);
5066 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005067
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005068 if (req->flags & REQ_F_BUFFER_SELECTED)
5069 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005070 /* fast path, check for non-NULL to avoid function call */
5071 if (kmsg->free_iov)
5072 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005073 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov889fca72021-02-10 00:03:09 +00005074 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005075 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005076}
5077
Pavel Begunkov889fca72021-02-10 00:03:09 +00005078static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07005079{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005080 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005081 struct io_sr_msg *sr = &req->sr_msg;
5082 struct msghdr msg;
5083 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005084 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005085 struct iovec iov;
5086 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005087 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005088 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005089 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005090
Florent Revestdba4a922020-12-04 12:36:04 +01005091 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005092 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005093 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005094
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005095 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov51aac422021-10-14 16:10:17 +01005096 kbuf = io_recv_buffer_select(req, issue_flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005097 if (IS_ERR(kbuf))
5098 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005099 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005100 }
5101
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005102 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005103 if (unlikely(ret))
5104 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005105
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005106 msg.msg_name = NULL;
5107 msg.msg_control = NULL;
5108 msg.msg_controllen = 0;
5109 msg.msg_namelen = 0;
5110 msg.msg_iocb = NULL;
5111 msg.msg_flags = 0;
5112
Pavel Begunkov04411802021-04-01 15:44:00 +01005113 flags = req->sr_msg.msg_flags;
5114 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005115 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005116 if (flags & MSG_WAITALL)
5117 min_ret = iov_iter_count(&msg.msg_iter);
5118
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005119 ret = sock_recvmsg(sock, &msg, flags);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005120out_free:
Pavel Begunkov7297ce32021-11-23 00:07:47 +00005121 if (ret < min_ret) {
5122 if (ret == -EAGAIN && force_nonblock)
5123 return -EAGAIN;
5124 if (ret == -ERESTARTSYS)
5125 ret = -EINTR;
5126 req_set_fail(req);
5127 } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
5128 req_set_fail(req);
5129 }
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005130 if (req->flags & REQ_F_BUFFER_SELECTED)
5131 cflags = io_put_recv_kbuf(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005132 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005133 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005134}
5135
Jens Axboe3529d8c2019-12-19 18:24:38 -07005136static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005137{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005138 struct io_accept *accept = &req->accept;
5139
Jens Axboe14587a462020-09-05 11:36:08 -06005140 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005141 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005142 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005143 return -EINVAL;
5144
Jens Axboed55e5f52019-12-11 16:12:15 -07005145 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5146 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005147 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005148 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005149
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005150 accept->file_slot = READ_ONCE(sqe->file_index);
5151 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5152 (accept->flags & SOCK_CLOEXEC)))
5153 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005154 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5155 return -EINVAL;
5156 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5157 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005158 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005159}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005160
Pavel Begunkov889fca72021-02-10 00:03:09 +00005161static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005162{
5163 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005164 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005165 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005166 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005167 struct file *file;
5168 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005169
Jiufei Xuee697dee2020-06-10 13:41:59 +08005170 if (req->file->f_flags & O_NONBLOCK)
5171 req->flags |= REQ_F_NOWAIT;
5172
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005173 if (!fixed) {
5174 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5175 if (unlikely(fd < 0))
5176 return fd;
5177 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005178 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5179 accept->flags);
5180 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005181 if (!fixed)
5182 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005183 ret = PTR_ERR(file);
5184 if (ret == -EAGAIN && force_nonblock)
5185 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005186 if (ret == -ERESTARTSYS)
5187 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005188 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005189 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005190 fd_install(fd, file);
5191 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005192 } else {
5193 ret = io_install_fixed_file(req, file, issue_flags,
5194 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005195 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005196 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005197 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005198}
5199
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005200static int io_connect_prep_async(struct io_kiocb *req)
5201{
5202 struct io_async_connect *io = req->async_data;
5203 struct io_connect *conn = &req->connect;
5204
5205 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5206}
5207
Jens Axboe3529d8c2019-12-19 18:24:38 -07005208static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005209{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005210 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005211
Jens Axboe14587a462020-09-05 11:36:08 -06005212 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005213 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005214 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5215 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005216 return -EINVAL;
5217
Jens Axboe3529d8c2019-12-19 18:24:38 -07005218 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5219 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005220 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005221}
5222
Pavel Begunkov889fca72021-02-10 00:03:09 +00005223static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005224{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005225 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005226 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005227 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005228 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005229
Pavel Begunkovd886e182021-10-04 20:02:56 +01005230 if (req_has_async_data(req)) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005231 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005232 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005233 ret = move_addr_to_kernel(req->connect.addr,
5234 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005235 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005236 if (ret)
5237 goto out;
5238 io = &__io;
5239 }
5240
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005241 file_flags = force_nonblock ? O_NONBLOCK : 0;
5242
Jens Axboee8c2bc12020-08-15 18:44:09 -07005243 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005244 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005245 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Pavel Begunkovd886e182021-10-04 20:02:56 +01005246 if (req_has_async_data(req))
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005247 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005248 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005249 ret = -ENOMEM;
5250 goto out;
5251 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005252 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005253 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005254 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005255 if (ret == -ERESTARTSYS)
5256 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005257out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005258 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005259 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005260 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005261 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005262}
YueHaibing469956e2020-03-04 15:53:52 +08005263#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005264#define IO_NETOP_FN(op) \
5265static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5266{ \
5267 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005268}
5269
Jens Axboe99a10082021-02-19 09:35:19 -07005270#define IO_NETOP_PREP(op) \
5271IO_NETOP_FN(op) \
5272static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5273{ \
5274 return -EOPNOTSUPP; \
5275} \
5276
5277#define IO_NETOP_PREP_ASYNC(op) \
5278IO_NETOP_PREP(op) \
5279static int io_##op##_prep_async(struct io_kiocb *req) \
5280{ \
5281 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005282}
5283
Jens Axboe99a10082021-02-19 09:35:19 -07005284IO_NETOP_PREP_ASYNC(sendmsg);
5285IO_NETOP_PREP_ASYNC(recvmsg);
5286IO_NETOP_PREP_ASYNC(connect);
5287IO_NETOP_PREP(accept);
5288IO_NETOP_FN(send);
5289IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005290#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005291
Jens Axboed7718a92020-02-14 22:23:12 -07005292struct io_poll_table {
5293 struct poll_table_struct pt;
5294 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005295 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005296 int error;
5297};
5298
Jens Axboed7718a92020-02-14 22:23:12 -07005299static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005300 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07005301{
Jens Axboed7718a92020-02-14 22:23:12 -07005302 /* for instances that support it check for an event match first: */
5303 if (mask && !(mask & poll->events))
5304 return 0;
5305
5306 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5307
5308 list_del_init(&poll->wait.entry);
5309
Jens Axboed7718a92020-02-14 22:23:12 -07005310 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005311 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005312
Jens Axboed7718a92020-02-14 22:23:12 -07005313 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005314 * If this fails, then the task is exiting. When a task exits, the
5315 * work gets canceled, so just cancel this request as well instead
5316 * of executing it. We can't safely execute it anyway, as we may not
5317 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005318 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005319 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005320 return 1;
5321}
5322
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005323static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5324 __acquires(&req->ctx->completion_lock)
5325{
5326 struct io_ring_ctx *ctx = req->ctx;
5327
Jens Axboe316319e2021-08-19 09:41:42 -06005328 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005329 if (unlikely(req->task->flags & PF_EXITING))
5330 WRITE_ONCE(poll->canceled, true);
5331
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005332 if (!req->result && !READ_ONCE(poll->canceled)) {
5333 struct poll_table_struct pt = { ._key = poll->events };
5334
5335 req->result = vfs_poll(req->file, &pt) & poll->events;
5336 }
5337
Jens Axboe79ebeae2021-08-10 15:18:27 -06005338 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005339 if (!req->result && !READ_ONCE(poll->canceled)) {
5340 add_wait_queue(poll->head, &poll->wait);
5341 return true;
5342 }
5343
5344 return false;
5345}
5346
Jens Axboed4e7cd32020-08-15 11:44:50 -07005347static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005348{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005349 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005350 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005351 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005352 return req->apoll->double_poll;
5353}
5354
5355static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5356{
5357 if (req->opcode == IORING_OP_POLL_ADD)
5358 return &req->poll;
5359 return &req->apoll->poll;
5360}
5361
5362static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005363 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005364{
5365 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005366
5367 lockdep_assert_held(&req->ctx->completion_lock);
5368
5369 if (poll && poll->head) {
5370 struct wait_queue_head *head = poll->head;
5371
Jens Axboe79ebeae2021-08-10 15:18:27 -06005372 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005373 list_del_init(&poll->wait.entry);
5374 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005375 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005376 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005377 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005378 }
5379}
5380
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005381static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005382 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005383{
5384 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005385 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005386 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005387
Pavel Begunkove27414b2021-04-09 09:13:20 +01005388 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005389 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005390 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005391 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005392 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005393 }
Jens Axboeb69de282021-03-17 08:37:41 -06005394 if (req->poll.events & EPOLLONESHOT)
5395 flags = 0;
Pavel Begunkov913a5712021-11-10 15:49:31 +00005396
5397 if (!(flags & IORING_CQE_F_MORE)) {
5398 io_fill_cqe_req(req, error, flags);
5399 } else if (!io_fill_cqe_aux(ctx, req->user_data, error, flags)) {
Hao Xua62682f2021-09-22 18:12:37 +08005400 req->poll.events |= EPOLLONESHOT;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005401 flags = 0;
Hao Xua62682f2021-09-22 18:12:37 +08005402 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005403 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005404}
5405
Pavel Begunkovf237c302021-08-18 12:42:46 +01005406static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005407{
Jens Axboe6d816e02020-08-11 08:04:14 -06005408 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005409 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005410
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005411 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005412 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005413 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005414 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005415
Hao Xu5b7aa382021-09-22 18:12:38 +08005416 if (req->poll.done) {
5417 spin_unlock(&ctx->completion_lock);
5418 return;
5419 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005420 done = __io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005421 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005422 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005423 hash_del(&req->hash_node);
Hao Xubd99c712021-09-22 18:12:36 +08005424 req->poll.done = true;
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005425 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005426 req->result = 0;
5427 add_wait_queue(req->poll.head, &req->poll.wait);
5428 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005429 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005430 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005431 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005432
Jens Axboe88e41cf2021-02-22 22:08:01 -07005433 if (done) {
5434 nxt = io_put_req_find_next(req);
5435 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005436 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005437 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005438 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005439}
5440
5441static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5442 int sync, void *key)
5443{
5444 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005445 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005446 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005447 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005448
5449 /* for instances that support it check for an event match first: */
5450 if (mask && !(mask & poll->events))
5451 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005452 if (!(poll->events & EPOLLONESHOT))
5453 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005454
Jens Axboe8706e042020-09-28 08:38:54 -06005455 list_del_init(&wait->entry);
5456
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005457 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005458 bool done;
5459
Jens Axboe79ebeae2021-08-10 15:18:27 -06005460 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005461 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005462 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005463 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005464 /* make sure double remove sees this as being gone */
5465 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005466 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005467 if (!done) {
5468 /* use wait func handler, so it matches the rq type */
5469 poll->wait.func(&poll->wait, mode, sync, key);
5470 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005471 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005472 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005473 return 1;
5474}
5475
5476static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5477 wait_queue_func_t wake_func)
5478{
5479 poll->head = NULL;
5480 poll->done = false;
5481 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005482#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5483 /* mask in events that we always want/need */
5484 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005485 INIT_LIST_HEAD(&poll->wait.entry);
5486 init_waitqueue_func_entry(&poll->wait, wake_func);
5487}
5488
5489static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005490 struct wait_queue_head *head,
5491 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005492{
5493 struct io_kiocb *req = pt->req;
5494
5495 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005496 * The file being polled uses multiple waitqueues for poll handling
5497 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5498 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005499 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005500 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005501 struct io_poll_iocb *poll_one = poll;
5502
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005503 /* double add on the same waitqueue head, ignore */
5504 if (poll_one->head == head)
5505 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005506 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005507 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005508 if ((*poll_ptr)->head == head)
5509 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005510 pt->error = -EINVAL;
5511 return;
5512 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005513 /*
5514 * Can't handle multishot for double wait for now, turn it
5515 * into one-shot mode.
5516 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005517 if (!(poll_one->events & EPOLLONESHOT))
5518 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005519 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5520 if (!poll) {
5521 pt->error = -ENOMEM;
5522 return;
5523 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005524 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005525 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005526 poll->wait.private = req;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005527
Jens Axboe807abcb2020-07-17 17:09:27 -06005528 *poll_ptr = poll;
Pavel Begunkovd886e182021-10-04 20:02:56 +01005529 if (req->opcode == IORING_OP_POLL_ADD)
5530 req->flags |= REQ_F_ASYNC_DATA;
Jens Axboe18bceab2020-05-15 11:56:54 -06005531 }
5532
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005533 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005534 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005535
5536 if (poll->events & EPOLLEXCLUSIVE)
5537 add_wait_queue_exclusive(head, &poll->wait);
5538 else
5539 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005540}
5541
5542static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5543 struct poll_table_struct *p)
5544{
5545 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005546 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005547
Jens Axboe807abcb2020-07-17 17:09:27 -06005548 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005549}
5550
Pavel Begunkovf237c302021-08-18 12:42:46 +01005551static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005552{
Jens Axboed7718a92020-02-14 22:23:12 -07005553 struct async_poll *apoll = req->apoll;
5554 struct io_ring_ctx *ctx = req->ctx;
5555
Olivier Langlois236daeae2021-05-31 02:36:37 -04005556 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005557
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005558 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005559 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005560 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005561 }
5562
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005563 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005564 io_poll_remove_double(req);
Hao Xubd99c712021-09-22 18:12:36 +08005565 apoll->poll.done = true;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005566 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005567
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005568 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005569 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005570 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005571 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005572}
5573
5574static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5575 void *key)
5576{
5577 struct io_kiocb *req = wait->private;
5578 struct io_poll_iocb *poll = &req->apoll->poll;
5579
5580 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5581 key_to_poll(key));
5582
5583 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5584}
5585
5586static void io_poll_req_insert(struct io_kiocb *req)
5587{
5588 struct io_ring_ctx *ctx = req->ctx;
5589 struct hlist_head *list;
5590
5591 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5592 hlist_add_head(&req->hash_node, list);
5593}
5594
5595static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5596 struct io_poll_iocb *poll,
5597 struct io_poll_table *ipt, __poll_t mask,
5598 wait_queue_func_t wake_func)
5599 __acquires(&ctx->completion_lock)
5600{
5601 struct io_ring_ctx *ctx = req->ctx;
5602 bool cancel = false;
5603
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005604 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005605 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005606 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005607 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005608
5609 ipt->pt._key = mask;
5610 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005611 ipt->error = 0;
5612 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005613
Jens Axboed7718a92020-02-14 22:23:12 -07005614 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005615 if (unlikely(!ipt->nr_entries) && !ipt->error)
5616 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005617
Jens Axboe79ebeae2021-08-10 15:18:27 -06005618 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005619 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005620 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005621 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005622 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005623 if (unlikely(list_empty(&poll->wait.entry))) {
5624 if (ipt->error)
5625 cancel = true;
5626 ipt->error = 0;
5627 mask = 0;
5628 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005629 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005630 list_del_init(&poll->wait.entry);
5631 else if (cancel)
5632 WRITE_ONCE(poll->canceled, true);
5633 else if (!poll->done) /* actually waiting for an event */
5634 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005635 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005636 }
5637
5638 return mask;
5639}
5640
Olivier Langlois59b735a2021-06-22 05:17:39 -07005641enum {
5642 IO_APOLL_OK,
5643 IO_APOLL_ABORTED,
5644 IO_APOLL_READY
5645};
5646
5647static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005648{
5649 const struct io_op_def *def = &io_op_defs[req->opcode];
5650 struct io_ring_ctx *ctx = req->ctx;
5651 struct async_poll *apoll;
5652 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005653 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboed7718a92020-02-14 22:23:12 -07005654
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005655 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005656 return IO_APOLL_ABORTED;
Pavel Begunkov658d0a42021-10-23 12:13:58 +01005657 if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED))
5658 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005659
5660 if (def->pollin) {
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005661 mask |= POLLIN | POLLRDNORM;
5662
5663 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5664 if ((req->opcode == IORING_OP_RECVMSG) &&
5665 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5666 mask &= ~POLLIN;
5667 } else {
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005668 mask |= POLLOUT | POLLWRNORM;
5669 }
5670
Jens Axboed7718a92020-02-14 22:23:12 -07005671 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5672 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005673 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005674 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005675 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005676 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005677 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005678 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005679
5680 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5681 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005682 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005683 if (ret || ipt.error)
5684 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5685
Olivier Langlois236daeae2021-05-31 02:36:37 -04005686 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5687 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005688 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005689}
5690
5691static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005692 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005693 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005694{
Jens Axboeb41e9852020-02-17 09:52:41 -07005695 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005696
Jens Axboe50826202021-02-23 09:02:26 -07005697 if (!poll->head)
5698 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005699 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005700 if (do_cancel)
5701 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005702 if (!list_empty(&poll->wait.entry)) {
5703 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005704 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005705 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005706 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005707 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005708 return do_complete;
5709}
5710
Pavel Begunkov5d709042021-08-09 20:18:13 +01005711static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005712 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005713{
5714 bool do_complete;
5715
Jens Axboed4e7cd32020-08-15 11:44:50 -07005716 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005717 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005718
Jens Axboeb41e9852020-02-17 09:52:41 -07005719 if (do_complete) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005720 req_set_fail(req);
Pavel Begunkov913a5712021-11-10 15:49:31 +00005721 io_fill_cqe_req(req, -ECANCELED, 0);
5722 io_commit_cqring(req->ctx);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005723 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005724 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005725 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005726}
5727
Jens Axboe76e1b642020-09-26 15:05:03 -06005728/*
5729 * Returns true if we found and killed one or more poll requests
5730 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01005731static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx,
5732 struct task_struct *tsk, bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005733{
Jens Axboe78076bb2019-12-04 19:56:40 -07005734 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005735 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005736 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005737
Jens Axboe79ebeae2021-08-10 15:18:27 -06005738 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005739 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5740 struct hlist_head *list;
5741
5742 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005743 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005744 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005745 posted += io_poll_remove_one(req);
5746 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005747 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005748 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005749
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005750 if (posted)
5751 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005752
5753 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005754}
5755
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005756static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5757 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005758 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005759{
Jens Axboe78076bb2019-12-04 19:56:40 -07005760 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005761 struct io_kiocb *req;
5762
Jens Axboe78076bb2019-12-04 19:56:40 -07005763 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5764 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005765 if (sqe_addr != req->user_data)
5766 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005767 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5768 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005769 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005770 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005771 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005772}
5773
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005774static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5775 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005776 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005777{
5778 struct io_kiocb *req;
5779
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005780 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005781 if (!req)
5782 return -ENOENT;
5783 if (io_poll_remove_one(req))
5784 return 0;
5785
5786 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005787}
5788
Pavel Begunkov9096af32021-04-14 13:38:36 +01005789static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5790 unsigned int flags)
5791{
5792 u32 events;
5793
5794 events = READ_ONCE(sqe->poll32_events);
5795#ifdef __BIG_ENDIAN
5796 events = swahw32(events);
5797#endif
5798 if (!(flags & IORING_POLL_ADD_MULTI))
5799 events |= EPOLLONESHOT;
5800 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5801}
5802
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005803static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005804 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005805{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005806 struct io_poll_update *upd = &req->poll_update;
5807 u32 flags;
5808
Jens Axboe221c5eb2019-01-17 09:41:58 -07005809 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5810 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005811 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005812 return -EINVAL;
5813 flags = READ_ONCE(sqe->len);
5814 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5815 IORING_POLL_ADD_MULTI))
5816 return -EINVAL;
5817 /* meaningless without update */
5818 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005819 return -EINVAL;
5820
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005821 upd->old_user_data = READ_ONCE(sqe->addr);
5822 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5823 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005824
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005825 upd->new_user_data = READ_ONCE(sqe->off);
5826 if (!upd->update_user_data && upd->new_user_data)
5827 return -EINVAL;
5828 if (upd->update_events)
5829 upd->events = io_poll_parse_events(sqe, flags);
5830 else if (sqe->poll32_events)
5831 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005832
Jens Axboe221c5eb2019-01-17 09:41:58 -07005833 return 0;
5834}
5835
Jens Axboe221c5eb2019-01-17 09:41:58 -07005836static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5837 void *key)
5838{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005839 struct io_kiocb *req = wait->private;
5840 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005841
Jens Axboed7718a92020-02-14 22:23:12 -07005842 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005843}
5844
Jens Axboe221c5eb2019-01-17 09:41:58 -07005845static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5846 struct poll_table_struct *p)
5847{
5848 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5849
Jens Axboee8c2bc12020-08-15 18:44:09 -07005850 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005851}
5852
Jens Axboe3529d8c2019-12-19 18:24:38 -07005853static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005854{
5855 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005856 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005857
5858 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5859 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005860 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005861 return -EINVAL;
5862 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005863 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005864 return -EINVAL;
Pavel Begunkov04c76b42021-11-10 15:49:32 +00005865 if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
5866 return -EINVAL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005867
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005868 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005869 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005870 return 0;
5871}
5872
Pavel Begunkov61e98202021-02-10 00:03:08 +00005873static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005874{
5875 struct io_poll_iocb *poll = &req->poll;
5876 struct io_ring_ctx *ctx = req->ctx;
5877 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005878 __poll_t mask;
Hao Xu5b7aa382021-09-22 18:12:38 +08005879 bool done;
Jens Axboe0969e782019-12-17 18:40:57 -07005880
Jens Axboed7718a92020-02-14 22:23:12 -07005881 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005882
Jens Axboed7718a92020-02-14 22:23:12 -07005883 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5884 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005885
Jens Axboe8c838782019-03-12 15:48:16 -06005886 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005887 ipt.error = 0;
Pavel Begunkoveb6e6f02021-10-04 20:02:59 +01005888 done = __io_poll_complete(req, mask);
5889 io_commit_cqring(req->ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06005890 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005891 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005892
Jens Axboe8c838782019-03-12 15:48:16 -06005893 if (mask) {
5894 io_cqring_ev_posted(ctx);
Hao Xu5b7aa382021-09-22 18:12:38 +08005895 if (done)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005896 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005897 }
Jens Axboe8c838782019-03-12 15:48:16 -06005898 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005899}
5900
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005901static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005902{
5903 struct io_ring_ctx *ctx = req->ctx;
5904 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005905 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005906 int ret;
5907
Jens Axboe79ebeae2021-08-10 15:18:27 -06005908 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005909 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005910 if (!preq) {
5911 ret = -ENOENT;
5912 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005913 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005914
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005915 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5916 completing = true;
5917 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5918 goto err;
5919 }
5920
Jens Axboecb3b200e2021-04-06 09:49:31 -06005921 /*
5922 * Don't allow racy completion with singleshot, as we cannot safely
5923 * update those. For multishot, if we're racing with completion, just
5924 * let completion re-add it.
5925 */
5926 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5927 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5928 ret = -EALREADY;
5929 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005930 }
5931 /* we now have a detached poll request. reissue. */
5932 ret = 0;
5933err:
Jens Axboeb69de282021-03-17 08:37:41 -06005934 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005935 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005936 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005937 io_req_complete(req, ret);
5938 return 0;
5939 }
5940 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005941 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005942 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005943 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005944 preq->poll.events |= IO_POLL_UNMASK;
5945 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005946 if (req->poll_update.update_user_data)
5947 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005948 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005949
Jens Axboeb69de282021-03-17 08:37:41 -06005950 /* complete update request, we're done with it */
5951 io_req_complete(req, ret);
5952
Jens Axboecb3b200e2021-04-06 09:49:31 -06005953 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005954 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005955 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005956 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005957 io_req_complete(preq, ret);
5958 }
Jens Axboeb69de282021-03-17 08:37:41 -06005959 }
5960 return 0;
5961}
5962
Pavel Begunkovf237c302021-08-18 12:42:46 +01005963static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005964{
Pavel Begunkov62245902021-10-02 19:36:14 +01005965 struct io_timeout_data *data = req->async_data;
5966
5967 if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS))
5968 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005969 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005970}
5971
Jens Axboe5262f562019-09-17 12:26:57 -06005972static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5973{
Jens Axboead8a48a2019-11-15 08:49:11 -07005974 struct io_timeout_data *data = container_of(timer,
5975 struct io_timeout_data, timer);
5976 struct io_kiocb *req = data->req;
5977 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005978 unsigned long flags;
5979
Jens Axboe89850fc2021-08-10 15:11:51 -06005980 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005981 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005982 atomic_set(&req->ctx->cq_timeouts,
5983 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005984 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005985
Jens Axboe89850fc2021-08-10 15:11:51 -06005986 req->io_task_work.func = io_req_task_timeout;
5987 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005988 return HRTIMER_NORESTART;
5989}
5990
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005991static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5992 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005993 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005994{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005995 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005996 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005997 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005998
5999 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006000 found = user_data == req->user_data;
6001 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06006002 break;
Jens Axboef254ac02020-08-12 17:33:30 -06006003 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006004 if (!found)
6005 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06006006
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006007 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01006008 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006009 return ERR_PTR(-EALREADY);
6010 list_del_init(&req->timeout.list);
6011 return req;
6012}
6013
6014static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006015 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06006016 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006017{
6018 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6019
6020 if (IS_ERR(req))
6021 return PTR_ERR(req);
6022
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006023 req_set_fail(req);
Pavel Begunkov913a5712021-11-10 15:49:31 +00006024 io_fill_cqe_req(req, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01006025 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00006026 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06006027}
6028
Jens Axboe50c1df22021-08-27 17:11:06 -06006029static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
6030{
6031 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
6032 case IORING_TIMEOUT_BOOTTIME:
6033 return CLOCK_BOOTTIME;
6034 case IORING_TIMEOUT_REALTIME:
6035 return CLOCK_REALTIME;
6036 default:
6037 /* can't happen, vetted at prep time */
6038 WARN_ON_ONCE(1);
6039 fallthrough;
6040 case 0:
6041 return CLOCK_MONOTONIC;
6042 }
6043}
6044
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006045static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6046 struct timespec64 *ts, enum hrtimer_mode mode)
6047 __must_hold(&ctx->timeout_lock)
6048{
6049 struct io_timeout_data *io;
6050 struct io_kiocb *req;
6051 bool found = false;
6052
6053 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
6054 found = user_data == req->user_data;
6055 if (found)
6056 break;
6057 }
6058 if (!found)
6059 return -ENOENT;
6060
6061 io = req->async_data;
6062 if (hrtimer_try_to_cancel(&io->timer) == -1)
6063 return -EALREADY;
6064 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
6065 io->timer.function = io_link_timeout_fn;
6066 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
6067 return 0;
6068}
6069
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006070static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6071 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06006072 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006073{
6074 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6075 struct io_timeout_data *data;
6076
6077 if (IS_ERR(req))
6078 return PTR_ERR(req);
6079
6080 req->timeout.off = 0; /* noseq */
6081 data = req->async_data;
6082 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06006083 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006084 data->timer.function = io_timeout_fn;
6085 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
6086 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07006087}
6088
Jens Axboe3529d8c2019-12-19 18:24:38 -07006089static int io_timeout_remove_prep(struct io_kiocb *req,
6090 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07006091{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006092 struct io_timeout_rem *tr = &req->timeout_rem;
6093
Jens Axboeb29472e2019-12-17 18:50:29 -07006094 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6095 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006096 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6097 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006098 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07006099 return -EINVAL;
6100
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006101 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006102 tr->addr = READ_ONCE(sqe->addr);
6103 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006104 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6105 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6106 return -EINVAL;
6107 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6108 tr->ltimeout = true;
6109 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006110 return -EINVAL;
6111 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6112 return -EFAULT;
6113 } else if (tr->flags) {
6114 /* timeout removal doesn't support flags */
6115 return -EINVAL;
6116 }
6117
Jens Axboeb29472e2019-12-17 18:50:29 -07006118 return 0;
6119}
6120
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006121static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6122{
6123 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6124 : HRTIMER_MODE_REL;
6125}
6126
Jens Axboe11365042019-10-16 09:08:32 -06006127/*
6128 * Remove or update an existing timeout command
6129 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006130static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006131{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006132 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006133 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006134 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006135
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006136 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6137 spin_lock(&ctx->completion_lock);
6138 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006139 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006140 spin_unlock_irq(&ctx->timeout_lock);
6141 spin_unlock(&ctx->completion_lock);
6142 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006143 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6144
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006145 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006146 if (tr->ltimeout)
6147 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6148 else
6149 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006150 spin_unlock_irq(&ctx->timeout_lock);
6151 }
Jens Axboe11365042019-10-16 09:08:32 -06006152
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006153 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006154 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006155 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006156 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006157}
6158
Jens Axboe3529d8c2019-12-19 18:24:38 -07006159static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006160 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006161{
Jens Axboead8a48a2019-11-15 08:49:11 -07006162 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006163 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006164 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006165
Jens Axboead8a48a2019-11-15 08:49:11 -07006166 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006167 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006168 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6169 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006170 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006171 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006172 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006173 flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkov62245902021-10-02 19:36:14 +01006174 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK |
6175 IORING_TIMEOUT_ETIME_SUCCESS))
Jens Axboe50c1df22021-08-27 17:11:06 -06006176 return -EINVAL;
6177 /* more than one clock specified is invalid, obviously */
6178 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006179 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006180
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006181 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006182 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006183 if (unlikely(off && !req->ctx->off_timeout_used))
6184 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006185
Pavel Begunkovd6a644a2021-10-23 12:14:00 +01006186 if (WARN_ON_ONCE(req_has_async_data(req)))
6187 return -EFAULT;
6188 if (io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006189 return -ENOMEM;
6190
Jens Axboee8c2bc12020-08-15 18:44:09 -07006191 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006192 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006193 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006194
6195 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006196 return -EFAULT;
6197
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006198 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006199 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006200
6201 if (is_timeout_link) {
6202 struct io_submit_link *link = &req->ctx->submit_state.link;
6203
6204 if (!link->head)
6205 return -EINVAL;
6206 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6207 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006208 req->timeout.head = link->last;
6209 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006210 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006211 return 0;
6212}
6213
Pavel Begunkov61e98202021-02-10 00:03:08 +00006214static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006215{
Jens Axboead8a48a2019-11-15 08:49:11 -07006216 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006217 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006218 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006219 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006220
Jens Axboe89850fc2021-08-10 15:11:51 -06006221 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006222
Jens Axboe5262f562019-09-17 12:26:57 -06006223 /*
6224 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006225 * timeout event to be satisfied. If it isn't set, then this is
6226 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006227 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006228 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006229 entry = ctx->timeout_list.prev;
6230 goto add;
6231 }
Jens Axboe5262f562019-09-17 12:26:57 -06006232
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006233 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6234 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006235
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006236 /* Update the last seq here in case io_flush_timeouts() hasn't.
6237 * This is safe because ->completion_lock is held, and submissions
6238 * and completions are never mixed in the same ->completion_lock section.
6239 */
6240 ctx->cq_last_tm_flush = tail;
6241
Jens Axboe5262f562019-09-17 12:26:57 -06006242 /*
6243 * Insertion sort, ensuring the first entry in the list is always
6244 * the one we need first.
6245 */
Jens Axboe5262f562019-09-17 12:26:57 -06006246 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006247 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6248 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006249
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006250 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006251 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006252 /* nxt.seq is behind @tail, otherwise would've been completed */
6253 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006254 break;
6255 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006256add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006257 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006258 data->timer.function = io_timeout_fn;
6259 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006260 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006261 return 0;
6262}
6263
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006264struct io_cancel_data {
6265 struct io_ring_ctx *ctx;
6266 u64 user_data;
6267};
6268
Jens Axboe62755e32019-10-28 21:49:21 -06006269static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006270{
Jens Axboe62755e32019-10-28 21:49:21 -06006271 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006272 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006273
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006274 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006275}
6276
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006277static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6278 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006279{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006280 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006281 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006282 int ret = 0;
6283
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006284 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006285 return -ENOENT;
6286
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006287 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006288 switch (cancel_ret) {
6289 case IO_WQ_CANCEL_OK:
6290 ret = 0;
6291 break;
6292 case IO_WQ_CANCEL_RUNNING:
6293 ret = -EALREADY;
6294 break;
6295 case IO_WQ_CANCEL_NOTFOUND:
6296 ret = -ENOENT;
6297 break;
6298 }
6299
Jens Axboee977d6d2019-11-05 12:39:45 -07006300 return ret;
6301}
6302
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006303static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006304{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006305 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006306 int ret;
6307
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006308 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006309
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006310 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01006311 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006312 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006313
6314 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006315 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006316 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006317 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006318 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01006319 goto out;
6320 ret = io_poll_cancel(ctx, sqe_addr, false);
6321out:
6322 spin_unlock(&ctx->completion_lock);
6323 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006324}
6325
Jens Axboe3529d8c2019-12-19 18:24:38 -07006326static int io_async_cancel_prep(struct io_kiocb *req,
6327 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006328{
Jens Axboefbf23842019-12-17 18:45:56 -07006329 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006330 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006331 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6332 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006333 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6334 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006335 return -EINVAL;
6336
Jens Axboefbf23842019-12-17 18:45:56 -07006337 req->cancel.addr = READ_ONCE(sqe->addr);
6338 return 0;
6339}
6340
Pavel Begunkov61e98202021-02-10 00:03:08 +00006341static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006342{
6343 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006344 u64 sqe_addr = req->cancel.addr;
Hao Xu3b44b372021-10-18 21:34:31 +08006345 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006346 struct io_tctx_node *node;
6347 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006348
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006349 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006350 if (ret != -ENOENT)
6351 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006352
6353 /* slow path, try all io-wq's */
Hao Xu3b44b372021-10-18 21:34:31 +08006354 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006355 ret = -ENOENT;
6356 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6357 struct io_uring_task *tctx = node->task->io_uring;
6358
Pavel Begunkov58f99372021-03-12 16:25:55 +00006359 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6360 if (ret != -ENOENT)
6361 break;
6362 }
Hao Xu3b44b372021-10-18 21:34:31 +08006363 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006364done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006365 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006366 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006367 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006368 return 0;
6369}
6370
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006371static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006372 const struct io_uring_sqe *sqe)
6373{
Daniele Albano61710e42020-07-18 14:15:16 -06006374 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6375 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006376 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006377 return -EINVAL;
6378
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006379 req->rsrc_update.offset = READ_ONCE(sqe->off);
6380 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6381 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006382 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006383 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006384 return 0;
6385}
6386
Pavel Begunkov889fca72021-02-10 00:03:09 +00006387static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006388{
6389 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08006390 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006391 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006392 int ret;
6393
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006394 up.offset = req->rsrc_update.offset;
6395 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006396 up.nr = 0;
6397 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006398 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006399
Hao Xu3b44b372021-10-18 21:34:31 +08006400 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006401 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006402 &up, req->rsrc_update.nr_args);
Hao Xu3b44b372021-10-18 21:34:31 +08006403 io_ring_submit_unlock(ctx, needs_lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006404
6405 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006406 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006407 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006408 return 0;
6409}
6410
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006411static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006412{
Jens Axboed625c6e2019-12-17 19:53:05 -07006413 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006414 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006415 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006416 case IORING_OP_READV:
6417 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006418 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006419 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006420 case IORING_OP_WRITEV:
6421 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006422 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006423 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006424 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006425 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006426 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006427 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006428 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006429 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006430 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006431 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006432 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006433 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006434 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006435 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006436 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006437 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006438 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006439 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006440 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006441 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006442 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006443 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006444 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006445 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006446 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006447 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006448 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006449 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006450 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006451 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006452 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006453 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006454 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006455 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006456 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006457 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006458 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006459 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006460 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006461 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006462 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006463 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006464 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006465 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006466 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006467 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006468 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006469 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006470 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006471 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006472 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006473 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006474 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006475 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006476 case IORING_OP_SHUTDOWN:
6477 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006478 case IORING_OP_RENAMEAT:
6479 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006480 case IORING_OP_UNLINKAT:
6481 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006482 case IORING_OP_MKDIRAT:
6483 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006484 case IORING_OP_SYMLINKAT:
6485 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006486 case IORING_OP_LINKAT:
6487 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006488 }
6489
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006490 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6491 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006492 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006493}
6494
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006495static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006496{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006497 if (!io_op_defs[req->opcode].needs_async_setup)
6498 return 0;
Pavel Begunkovd886e182021-10-04 20:02:56 +01006499 if (WARN_ON_ONCE(req_has_async_data(req)))
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006500 return -EFAULT;
6501 if (io_alloc_async_data(req))
6502 return -EAGAIN;
6503
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006504 switch (req->opcode) {
6505 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006506 return io_rw_prep_async(req, READ);
6507 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006508 return io_rw_prep_async(req, WRITE);
6509 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006510 return io_sendmsg_prep_async(req);
6511 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006512 return io_recvmsg_prep_async(req);
6513 case IORING_OP_CONNECT:
6514 return io_connect_prep_async(req);
6515 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006516 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6517 req->opcode);
6518 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006519}
6520
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006521static u32 io_get_sequence(struct io_kiocb *req)
6522{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006523 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006524
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006525 /* need original cached_sq_head, but it was increased for each req */
6526 io_for_each_link(req, req)
6527 seq--;
6528 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006529}
6530
Pavel Begunkovc0724812021-10-04 20:02:54 +01006531static __cold void io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006532{
6533 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006534 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006535 int ret;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006536 u32 seq = io_get_sequence(req);
Pavel Begunkov3c199662021-06-15 16:47:57 +01006537
Jens Axboedef596e2019-01-09 08:59:42 -07006538 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov5e371262021-09-24 22:00:04 +01006539 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) {
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006540queue:
Pavel Begunkov10c66902021-06-15 16:47:56 +01006541 ctx->drain_active = false;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006542 io_req_task_queue(req);
6543 return;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006544 }
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006545
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006546 ret = io_req_prep_async(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006547 if (ret) {
6548fail:
6549 io_req_complete_failed(req, ret);
6550 return;
6551 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006552 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006553 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006554 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006555 ret = -ENOMEM;
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006556 goto fail;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006557 }
Jens Axboe31b51512019-01-18 22:56:34 -07006558
Jens Axboe79ebeae2021-08-10 15:18:27 -06006559 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006560 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006561 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006562 kfree(de);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01006563 goto queue;
Jens Axboe31b51512019-01-18 22:56:34 -07006564 }
6565
6566 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006567 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006568 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006569 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006570 spin_unlock(&ctx->completion_lock);
Jens Axboe31b51512019-01-18 22:56:34 -07006571}
6572
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006573static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006574{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006575 if (req->flags & REQ_F_BUFFER_SELECTED) {
Pavel Begunkov30d51dd2021-10-01 18:07:03 +01006576 kfree(req->kbuf);
6577 req->kbuf = NULL;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006578 }
6579
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006580 if (req->flags & REQ_F_NEED_CLEANUP) {
6581 switch (req->opcode) {
6582 case IORING_OP_READV:
6583 case IORING_OP_READ_FIXED:
6584 case IORING_OP_READ:
6585 case IORING_OP_WRITEV:
6586 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006587 case IORING_OP_WRITE: {
6588 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006589
6590 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006591 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006592 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006593 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006594 case IORING_OP_SENDMSG: {
6595 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006596
6597 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006598 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006599 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006600 case IORING_OP_SPLICE:
6601 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006602 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6603 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006604 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006605 case IORING_OP_OPENAT:
6606 case IORING_OP_OPENAT2:
6607 if (req->open.filename)
6608 putname(req->open.filename);
6609 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006610 case IORING_OP_RENAMEAT:
6611 putname(req->rename.oldpath);
6612 putname(req->rename.newpath);
6613 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006614 case IORING_OP_UNLINKAT:
6615 putname(req->unlink.filename);
6616 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006617 case IORING_OP_MKDIRAT:
6618 putname(req->mkdir.filename);
6619 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006620 case IORING_OP_SYMLINKAT:
6621 putname(req->symlink.oldpath);
6622 putname(req->symlink.newpath);
6623 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006624 case IORING_OP_LINKAT:
6625 putname(req->hardlink.oldpath);
6626 putname(req->hardlink.newpath);
6627 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006628 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006629 }
Jens Axboe75652a302021-04-15 09:52:40 -06006630 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6631 kfree(req->apoll->double_poll);
6632 kfree(req->apoll);
6633 req->apoll = NULL;
6634 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006635 if (req->flags & REQ_F_INFLIGHT) {
6636 struct io_uring_task *tctx = req->task->io_uring;
6637
6638 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006639 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006640 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006641 put_cred(req->creds);
Pavel Begunkovd886e182021-10-04 20:02:56 +01006642 if (req->flags & REQ_F_ASYNC_DATA) {
6643 kfree(req->async_data);
6644 req->async_data = NULL;
6645 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006646 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006647}
6648
Pavel Begunkov889fca72021-02-10 00:03:09 +00006649static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006650{
Jens Axboe5730b272021-02-27 15:57:30 -07006651 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006652 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006653
Pavel Begunkov6878b402021-09-24 21:59:41 +01006654 if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006655 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006656
Paul Moore5bd21822021-02-16 19:46:48 -05006657 if (!io_op_defs[req->opcode].audit_skip)
6658 audit_uring_entry(req->opcode);
6659
Jens Axboed625c6e2019-12-17 19:53:05 -07006660 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006661 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006662 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006663 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006664 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006665 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006666 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006667 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006668 break;
6669 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006670 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006671 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006672 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006673 break;
6674 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006675 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006676 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006677 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006678 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006679 break;
6680 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006681 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006682 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006683 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006684 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006685 break;
6686 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006687 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006688 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006689 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006690 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006691 break;
6692 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006693 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006694 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006695 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006696 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006697 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006698 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006699 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006700 break;
6701 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006702 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006703 break;
6704 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006705 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006706 break;
6707 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006708 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006709 break;
6710 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006711 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006712 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006713 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006714 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006715 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006716 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006717 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006718 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006719 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006720 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006721 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006722 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006723 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006724 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006725 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006726 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006727 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006728 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006729 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006730 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006731 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006732 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006733 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006734 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006735 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006736 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006737 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006738 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006739 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006740 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006741 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006742 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006743 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006744 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006745 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006746 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006747 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006748 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006749 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006750 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006751 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006752 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006753 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006754 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006755 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006756 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006757 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006758 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006759 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006760 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006761 case IORING_OP_MKDIRAT:
6762 ret = io_mkdirat(req, issue_flags);
6763 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006764 case IORING_OP_SYMLINKAT:
6765 ret = io_symlinkat(req, issue_flags);
6766 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006767 case IORING_OP_LINKAT:
6768 ret = io_linkat(req, issue_flags);
6769 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006770 default:
6771 ret = -EINVAL;
6772 break;
6773 }
Jens Axboe31b51512019-01-18 22:56:34 -07006774
Paul Moore5bd21822021-02-16 19:46:48 -05006775 if (!io_op_defs[req->opcode].audit_skip)
6776 audit_uring_exit(!ret, ret);
6777
Jens Axboe5730b272021-02-27 15:57:30 -07006778 if (creds)
6779 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006780 if (ret)
6781 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006782 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkov99830282021-10-15 17:09:11 +01006783 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file)
Pavel Begunkov98821312021-10-15 17:09:12 +01006784 io_iopoll_req_issued(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006785
6786 return 0;
6787}
6788
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006789static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6790{
6791 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6792
6793 req = io_put_req_find_next(req);
6794 return req ? &req->work : NULL;
6795}
6796
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006797static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006798{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006799 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006800 unsigned int issue_flags = IO_URING_F_UNLOCKED;
6801 bool needs_poll = false;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006802 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006803 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006804
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006805 /* one will be dropped by ->io_free_work() after returning to io-wq */
6806 if (!(req->flags & REQ_F_REFCOUNT))
6807 __io_req_set_refcount(req, 2);
6808 else
6809 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006810
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006811 timeout = io_prep_linked_timeout(req);
6812 if (timeout)
6813 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006814
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006815 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006816 if (work->flags & IO_WQ_WORK_CANCEL) {
6817 io_req_task_queue_fail(req, -ECANCELED);
6818 return;
Jens Axboe561fb042019-10-24 07:25:42 -06006819 }
Jens Axboe31b51512019-01-18 22:56:34 -07006820
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006821 if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovafb7f56f2021-10-23 12:13:59 +01006822 const struct io_op_def *def = &io_op_defs[req->opcode];
6823 bool opcode_poll = def->pollin || def->pollout;
6824
6825 if (opcode_poll && file_can_poll(req->file)) {
6826 needs_poll = true;
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006827 issue_flags |= IO_URING_F_NONBLOCK;
Pavel Begunkovafb7f56f2021-10-23 12:13:59 +01006828 }
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006829 }
Hao Xu90fa0282021-10-18 21:34:45 +08006830
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006831 do {
6832 ret = io_issue_sqe(req, issue_flags);
6833 if (ret != -EAGAIN)
6834 break;
6835 /*
6836 * We can get EAGAIN for iopolled IO even though we're
6837 * forcing a sync submission from here, since we can't
6838 * wait for request slots on the block side.
6839 */
6840 if (!needs_poll) {
6841 cond_resched();
6842 continue;
Hao Xu90fa0282021-10-18 21:34:45 +08006843 }
6844
Pavel Begunkovd01905d2021-10-23 12:13:57 +01006845 if (io_arm_poll_handler(req) == IO_APOLL_OK)
6846 return;
6847 /* aborted or ready, in either case retry blocking */
6848 needs_poll = false;
6849 issue_flags &= ~IO_URING_F_NONBLOCK;
6850 } while (1);
Jens Axboe31b51512019-01-18 22:56:34 -07006851
Pavel Begunkova3df76982021-02-18 22:32:52 +00006852 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006853 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006854 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006855}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006856
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006857static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006858 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006859{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006860 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006861}
6862
Jens Axboe09bb8392019-03-13 12:39:28 -06006863static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6864 int index)
6865{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006866 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006867
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006868 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006869}
6870
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006871static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006872{
6873 unsigned long file_ptr = (unsigned long) file;
6874
Pavel Begunkov88459b52021-10-17 00:07:10 +01006875 file_ptr |= io_file_get_flags(file);
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006876 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006877}
6878
Pavel Begunkovac177052021-08-09 13:04:02 +01006879static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6880 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006881{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006882 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006883 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006884
Pavel Begunkovac177052021-08-09 13:04:02 +01006885 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6886 return NULL;
6887 fd = array_index_nospec(fd, ctx->nr_user_files);
6888 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6889 file = (struct file *) (file_ptr & FFS_MASK);
6890 file_ptr &= ~FFS_MASK;
6891 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkov35645ac2021-10-17 00:07:09 +01006892 req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
Pavel Begunkova46be972021-10-09 23:14:40 +01006893 io_req_set_rsrc_node(req, ctx);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006894 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006895}
6896
Pavel Begunkovac177052021-08-09 13:04:02 +01006897static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006898 struct io_kiocb *req, int fd)
6899{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006900 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006901
6902 trace_io_uring_file_get(ctx, fd);
6903
6904 /* we don't allow fixed io_uring files */
6905 if (file && unlikely(file->f_op == &io_uring_fops))
6906 io_req_track_inflight(req);
6907 return file;
6908}
6909
6910static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006911 struct io_kiocb *req, int fd, bool fixed)
6912{
6913 if (fixed)
6914 return io_file_get_fixed(ctx, req, fd);
6915 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006916 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006917}
6918
Pavel Begunkovf237c302021-08-18 12:42:46 +01006919static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006920{
6921 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006922 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006923
6924 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006925 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006926 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006927 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006928 } else {
6929 io_req_complete_post(req, -ETIME, 0);
6930 }
6931}
6932
Jens Axboe2665abf2019-11-05 12:40:47 -07006933static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6934{
Jens Axboead8a48a2019-11-15 08:49:11 -07006935 struct io_timeout_data *data = container_of(timer,
6936 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006937 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006938 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006939 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006940
Jens Axboe89b263f2021-08-10 15:14:18 -06006941 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006942 prev = req->timeout.head;
6943 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006944
6945 /*
6946 * We don't expect the list to be empty, that will only happen if we
6947 * race with the completion of the linked work.
6948 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006949 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006950 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006951 if (!req_ref_inc_not_zero(prev))
6952 prev = NULL;
6953 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006954 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06006955 req->timeout.prev = prev;
6956 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006957
Jens Axboe89b263f2021-08-10 15:14:18 -06006958 req->io_task_work.func = io_req_task_link_timeout;
6959 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006960 return HRTIMER_NORESTART;
6961}
6962
Pavel Begunkovde968c12021-03-19 17:22:33 +00006963static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006964{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006965 struct io_ring_ctx *ctx = req->ctx;
6966
Jens Axboe89b263f2021-08-10 15:14:18 -06006967 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006968 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006969 * If the back reference is NULL, then our linked request finished
6970 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006971 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006972 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006973 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006974
Jens Axboead8a48a2019-11-15 08:49:11 -07006975 data->timer.function = io_link_timeout_fn;
6976 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6977 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006978 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07006979 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006980 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006981 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006982 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006983}
6984
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006985static void io_queue_sqe_arm_apoll(struct io_kiocb *req)
6986 __must_hold(&req->ctx->uring_lock)
6987{
6988 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
6989
6990 switch (io_arm_poll_handler(req)) {
6991 case IO_APOLL_READY:
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01006992 io_req_task_queue(req);
6993 break;
6994 case IO_APOLL_ABORTED:
6995 /*
6996 * Queued up for async execution, worker will release
6997 * submit reference when the iocb is actually submitted.
6998 */
6999 io_queue_async_work(req, NULL);
7000 break;
7001 }
7002
7003 if (linked_timeout)
7004 io_queue_linked_timeout(linked_timeout);
7005}
7006
7007static inline void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007008 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007009{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01007010 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06007011 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007012
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00007013 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06007014
Pavel Begunkovfff4e402021-10-04 20:02:48 +01007015 if (req->flags & REQ_F_COMPLETE_INLINE) {
7016 io_req_add_compl_list(req);
Pavel Begunkovd9f9d282021-09-24 22:00:00 +01007017 return;
Pavel Begunkovfff4e402021-10-04 20:02:48 +01007018 }
Jens Axboe491381ce2019-10-17 09:20:46 -06007019 /*
7020 * We async punt it if the file wasn't marked NOWAIT, or if the file
7021 * doesn't support non-blocking read/write attempts
7022 */
Pavel Begunkov18400382021-03-19 17:22:34 +00007023 if (likely(!ret)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01007024 linked_timeout = io_prep_linked_timeout(req);
7025 if (linked_timeout)
7026 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00007027 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovd475a9a2021-09-24 21:59:59 +01007028 io_queue_sqe_arm_apoll(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01007029 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00007030 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06007031 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007032}
7033
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007034static void io_queue_sqe_fallback(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007035 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08007036{
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007037 if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01007038 io_req_complete_fail_submit(req);
Pavel Begunkove0eb71d2021-10-01 18:07:01 +01007039 } else if (unlikely(req->ctx->drain_active)) {
7040 io_drain_req(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01007041 } else {
7042 int ret = io_req_prep_async(req);
7043
7044 if (unlikely(ret))
7045 io_req_complete_failed(req, ret);
7046 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01007047 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07007048 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007049}
7050
Pavel Begunkov4652fe32021-09-24 21:59:58 +01007051static inline void io_queue_sqe(struct io_kiocb *req)
7052 __must_hold(&req->ctx->uring_lock)
7053{
7054 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))))
7055 __io_queue_sqe(req);
7056 else
7057 io_queue_sqe_fallback(req);
7058}
7059
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007060/*
7061 * Check SQE restrictions (opcode and flags).
7062 *
7063 * Returns 'true' if SQE is allowed, 'false' otherwise.
7064 */
7065static inline bool io_check_restriction(struct io_ring_ctx *ctx,
7066 struct io_kiocb *req,
7067 unsigned int sqe_flags)
7068{
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007069 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
7070 return false;
7071
7072 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
7073 ctx->restrictions.sqe_flags_required)
7074 return false;
7075
7076 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
7077 ctx->restrictions.sqe_flags_required))
7078 return false;
7079
7080 return true;
7081}
7082
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007083static void io_init_req_drain(struct io_kiocb *req)
7084{
7085 struct io_ring_ctx *ctx = req->ctx;
7086 struct io_kiocb *head = ctx->submit_state.link.head;
7087
7088 ctx->drain_active = true;
7089 if (head) {
7090 /*
7091 * If we need to drain a request in the middle of a link, drain
7092 * the head request and the next request/link after the current
7093 * link. Considering sequential execution of links,
7094 * IOSQE_IO_DRAIN will be maintained for every request of our
7095 * link.
7096 */
7097 head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC;
7098 ctx->drain_next = true;
7099 }
7100}
7101
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007102static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007103 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007104 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007105{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007106 unsigned int sqe_flags;
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007107 int personality;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007108 u8 opcode;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007109
Pavel Begunkov864ea922021-08-09 13:04:08 +01007110 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007111 req->opcode = opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007112 /* same numerical values with corresponding REQ_F_*, safe to copy */
7113 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007114 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007115 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007116 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03007117 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007118
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007119 if (unlikely(opcode >= IORING_OP_LAST)) {
7120 req->opcode = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007121 return -EINVAL;
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007122 }
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007123 if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
7124 /* enforce forwards compatibility on users */
7125 if (sqe_flags & ~SQE_VALID_FLAGS)
7126 return -EINVAL;
7127 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007128 !io_op_defs[opcode].buffer_select)
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007129 return -EOPNOTSUPP;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007130 if (sqe_flags & IOSQE_IO_DRAIN)
7131 io_init_req_drain(req);
Pavel Begunkov68fe2562021-09-15 12:03:38 +01007132 }
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007133 if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) {
7134 if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags))
7135 return -EACCES;
7136 /* knock it to the slow queue path, will be drained there */
7137 if (ctx->drain_active)
7138 req->flags |= REQ_F_FORCE_ASYNC;
7139 /* if there is no link, we're at "next" request and need to drain */
7140 if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
7141 ctx->drain_next = false;
7142 ctx->drain_active = true;
Pavel Begunkov22b2ca32021-10-01 18:07:00 +01007143 req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC;
Pavel Begunkov2a56a9b2021-09-24 21:59:57 +01007144 }
7145 }
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007146
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007147 if (io_op_defs[opcode].needs_file) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007148 struct io_submit_state *state = &ctx->submit_state;
7149
7150 /*
7151 * Plug now if we have more than 2 IO left after this, and the
7152 * target is potentially a read/write to block based storage.
7153 */
Pavel Begunkov4a04d1d2021-10-06 16:06:49 +01007154 if (state->need_plug && io_op_defs[opcode].plug) {
Pavel Begunkov6d634162021-10-06 16:06:46 +01007155 state->plug_started = true;
7156 state->need_plug = false;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -06007157 blk_start_plug_nr_ios(&state->plug, state->submit_nr);
Pavel Begunkov6d634162021-10-06 16:06:46 +01007158 }
7159
Pavel Begunkov62906e82021-08-10 14:52:47 +01007160 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007161 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007162 if (unlikely(!req->file))
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007163 return -EBADF;
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007164 }
Pavel Begunkovc11368a52020-05-17 14:13:42 +03007165
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007166 personality = READ_ONCE(sqe->personality);
Jens Axboe63ff8222020-05-07 14:56:15 -06007167 if (personality) {
Linus Torvaldscdab10b2021-11-01 21:06:18 -07007168 int ret;
7169
Jens Axboe63ff8222020-05-07 14:56:15 -06007170 req->creds = xa_load(&ctx->personalities, personality);
7171 if (!req->creds)
Jens Axboe27926b62020-10-28 09:33:23 -06007172 return -EINVAL;
Jens Axboe63ff8222020-05-07 14:56:15 -06007173 get_cred(req->creds);
Paul Moorecdc14042021-02-01 19:56:49 -05007174 ret = security_uring_override_creds(req->creds);
7175 if (ret) {
7176 put_cred(req->creds);
7177 return ret;
7178 }
Jens Axboe63ff8222020-05-07 14:56:15 -06007179 req->flags |= REQ_F_CREDS;
7180 }
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007181
Pavel Begunkovfc0ae022021-10-01 18:07:02 +01007182 return io_req_prep(req, sqe);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007183}
7184
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007185static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007186 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007187 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007188{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007189 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007190 int ret;
7191
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007192 ret = io_init_req(ctx, req, sqe);
7193 if (unlikely(ret)) {
Jens Axboea87acfd2021-09-11 16:04:50 -06007194 trace_io_uring_req_failed(sqe, ret);
7195
Hao Xua8295b92021-08-27 17:46:09 +08007196 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007197 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007198 /*
7199 * we can judge a link req is failed or cancelled by if
7200 * REQ_F_FAIL is set, but the head is an exception since
7201 * it may be set REQ_F_FAIL because of other req's failure
7202 * so let's leverage req->result to distinguish if a head
7203 * is set REQ_F_FAIL because of its failure or other req's
7204 * failure so that we can set the correct ret code for it.
7205 * init result here to avoid affecting the normal path.
7206 */
7207 if (!(link->head->flags & REQ_F_FAIL))
7208 req_fail_link_node(link->head, -ECANCELED);
7209 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7210 /*
7211 * the current req is a normal req, we should return
7212 * error and thus break the submittion loop.
7213 */
7214 io_req_complete_failed(req, ret);
7215 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007216 }
Hao Xua8295b92021-08-27 17:46:09 +08007217 req_fail_link_node(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007218 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007219
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007220 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007221 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7222 req->flags, true,
7223 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007224
Jens Axboe6c271ce2019-01-10 11:22:30 -07007225 /*
7226 * If we already have a head request, queue this one for async
7227 * submittal once the head completes. If we don't have a head but
7228 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7229 * submitted sync once the chain is complete. If none of those
7230 * conditions are true (normal request), then just queue it.
7231 */
7232 if (link->head) {
7233 struct io_kiocb *head = link->head;
7234
Hao Xua8295b92021-08-27 17:46:09 +08007235 if (!(req->flags & REQ_F_FAIL)) {
7236 ret = io_req_prep_async(req);
7237 if (unlikely(ret)) {
7238 req_fail_link_node(req, ret);
7239 if (!(head->flags & REQ_F_FAIL))
7240 req_fail_link_node(head, -ECANCELED);
7241 }
7242 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007243 trace_io_uring_link(ctx, req, head);
7244 link->last->link = req;
7245 link->last = req;
7246
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007247 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK))
7248 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007249 /* last request of a link, enqueue the link */
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007250 link->head = NULL;
7251 req = head;
7252 } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
7253 link->head = req;
7254 link->last = req;
7255 return 0;
Jackie Liu4fe2c962019-09-09 20:50:40 +08007256 }
7257
Pavel Begunkovf15a3432021-09-24 21:59:56 +01007258 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007259 return 0;
7260}
7261
7262/*
7263 * Batched submission is done, ensure local IO is flushed out.
7264 */
Pavel Begunkov553deff2021-09-24 21:59:55 +01007265static void io_submit_state_end(struct io_ring_ctx *ctx)
Jens Axboe3529d8c2019-12-19 18:24:38 -07007266{
Pavel Begunkov553deff2021-09-24 21:59:55 +01007267 struct io_submit_state *state = &ctx->submit_state;
7268
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007269 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007270 io_queue_sqe(state->link.head);
Pavel Begunkov553deff2021-09-24 21:59:55 +01007271 /* flush only after queuing links as they can generate completions */
Pavel Begunkovc4501782021-09-08 16:40:52 +01007272 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007273 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007274 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007275}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007276
Jens Axboe9e645e112019-05-10 16:07:28 -06007277/*
7278 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007279 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007280static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007281 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007282{
7283 state->plug_started = false;
Pavel Begunkov4b628ae2021-09-08 16:40:49 +01007284 state->need_plug = max_ios > 2;
Jens Axboe5ca7a8b2021-10-06 11:01:42 -06007285 state->submit_nr = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007286 /* set only head, no need to init link_last in advance */
7287 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007288}
7289
Jens Axboe193155c2020-02-22 23:22:19 -07007290static void io_commit_sqring(struct io_ring_ctx *ctx)
7291{
Jens Axboe75c6a032020-01-28 10:15:23 -07007292 struct io_rings *rings = ctx->rings;
7293
7294 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007295 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007296 * since once we write the new head, the application could
7297 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007298 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007299 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007300}
7301
Jens Axboe9e645e112019-05-10 16:07:28 -06007302/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007303 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007304 * that is mapped by userspace. This means that care needs to be taken to
7305 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007306 * being a good citizen. If members of the sqe are validated and then later
7307 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007308 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007309 */
7310static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007311{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007312 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007313 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007314
7315 /*
7316 * The cached sq head (or cq tail) serves two purposes:
7317 *
7318 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007319 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007320 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007321 * though the application is the one updating it.
7322 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007323 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007324 if (likely(head < ctx->sq_entries))
7325 return &ctx->sq_sqes[head];
7326
7327 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007328 ctx->cq_extra--;
7329 WRITE_ONCE(ctx->rings->sq_dropped,
7330 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007331 return NULL;
7332}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007333
Jens Axboe0f212202020-09-13 13:09:39 -06007334static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007335 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007336{
Pavel Begunkov69629802021-09-24 22:00:01 +01007337 unsigned int entries = io_sqring_entries(ctx);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007338 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007339
Pavel Begunkov51d48da2021-10-04 20:02:47 +01007340 if (unlikely(!entries))
Pavel Begunkov69629802021-09-24 22:00:01 +01007341 return 0;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03007342 /* make sure SQ entry isn't read before tail */
Pavel Begunkov69629802021-09-24 22:00:01 +01007343 nr = min3(nr, ctx->sq_entries, entries);
Pavel Begunkov9a108672021-08-27 11:55:01 +01007344 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007345
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007346 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov69629802021-09-24 22:00:01 +01007347 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007348 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007349 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007350
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007351 if (unlikely(!io_alloc_req_refill(ctx))) {
Pavel Begunkov196be952019-11-07 01:41:06 +03007352 if (!submitted)
7353 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007354 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007355 }
Pavel Begunkova33ae9c2021-10-04 20:02:49 +01007356 req = io_alloc_req(ctx);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007357 sqe = io_get_sqe(ctx);
7358 if (unlikely(!sqe)) {
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01007359 wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007360 break;
7361 }
Jens Axboed3656342019-12-18 09:50:26 -07007362 /* will complete beyond this point, count as submitted */
7363 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007364 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007365 break;
Pavel Begunkov69629802021-09-24 22:00:01 +01007366 } while (submitted < nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007367
Pavel Begunkov9466f432020-01-25 22:34:01 +03007368 if (unlikely(submitted != nr)) {
7369 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007370 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007371
Pavel Begunkov09899b12021-06-14 02:36:22 +01007372 current->io_uring->cached_refs += unused;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007373 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007374
Pavel Begunkov553deff2021-09-24 21:59:55 +01007375 io_submit_state_end(ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007376 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7377 io_commit_sqring(ctx);
7378
Jens Axboe6c271ce2019-01-10 11:22:30 -07007379 return submitted;
7380}
7381
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007382static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7383{
7384 return READ_ONCE(sqd->state);
7385}
7386
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007387static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7388{
7389 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007390 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007391 WRITE_ONCE(ctx->rings->sq_flags,
7392 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007393 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007394}
7395
7396static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7397{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007398 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007399 WRITE_ONCE(ctx->rings->sq_flags,
7400 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007401 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007402}
7403
Xiaoguang Wang08369242020-11-03 14:15:59 +08007404static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007405{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007406 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007407 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007408
Jens Axboec8d1ba52020-09-14 11:07:26 -06007409 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007410 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007411 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7412 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007413
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007414 if (!wq_list_empty(&ctx->iopoll_list) || to_submit) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007415 const struct cred *creds = NULL;
7416
7417 if (ctx->sq_creds != current_cred())
7418 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007419
Xiaoguang Wang08369242020-11-03 14:15:59 +08007420 mutex_lock(&ctx->uring_lock);
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007421 if (!wq_list_empty(&ctx->iopoll_list))
Pavel Begunkov5ba3c872021-09-24 21:59:43 +01007422 io_do_iopoll(ctx, true);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007423
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007424 /*
7425 * Don't submit if refs are dying, good for io_uring_register(),
7426 * but also it is relied upon by io_ring_exit_work()
7427 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007428 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7429 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007430 ret = io_submit_sqes(ctx, to_submit);
7431 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007432
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007433 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7434 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007435 if (creds)
7436 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007437 }
Jens Axboe90554202020-09-03 12:12:41 -06007438
Xiaoguang Wang08369242020-11-03 14:15:59 +08007439 return ret;
7440}
7441
Pavel Begunkovc0724812021-10-04 20:02:54 +01007442static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd)
Xiaoguang Wang08369242020-11-03 14:15:59 +08007443{
7444 struct io_ring_ctx *ctx;
7445 unsigned sq_thread_idle = 0;
7446
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007447 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7448 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007449 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007450}
7451
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007452static bool io_sqd_handle_event(struct io_sq_data *sqd)
7453{
7454 bool did_sig = false;
7455 struct ksignal ksig;
7456
7457 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7458 signal_pending(current)) {
7459 mutex_unlock(&sqd->lock);
7460 if (signal_pending(current))
7461 did_sig = get_signal(&ksig);
7462 cond_resched();
7463 mutex_lock(&sqd->lock);
7464 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007465 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7466}
7467
Jens Axboe6c271ce2019-01-10 11:22:30 -07007468static int io_sq_thread(void *data)
7469{
Jens Axboe69fb2132020-09-14 11:16:23 -06007470 struct io_sq_data *sqd = data;
7471 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007472 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007473 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007474 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007475
Pavel Begunkov696ee882021-04-01 09:55:04 +01007476 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007477 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007478
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007479 if (sqd->sq_cpu != -1)
7480 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7481 else
7482 set_cpus_allowed_ptr(current, cpu_online_mask);
7483 current->flags |= PF_NO_SETAFFINITY;
7484
Paul Moore5bd21822021-02-16 19:46:48 -05007485 audit_alloc_kernel(current);
7486
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007487 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007488 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007489 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007490
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007491 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7492 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007493 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007494 timeout = jiffies + sqd->sq_thread_idle;
7495 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007496
Jens Axboee95eee22020-09-08 09:11:32 -06007497 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007498 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007499 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007500
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007501 if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007502 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007503 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007504 if (io_run_task_work())
7505 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007506
Xiaoguang Wang08369242020-11-03 14:15:59 +08007507 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007508 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007509 if (sqt_spin)
7510 timeout = jiffies + sqd->sq_thread_idle;
7511 continue;
7512 }
7513
Xiaoguang Wang08369242020-11-03 14:15:59 +08007514 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007515 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007516 bool needs_sched = true;
7517
Hao Xu724cb4f2021-04-21 23:19:11 +08007518 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007519 io_ring_set_wakeup_flag(ctx);
7520
Hao Xu724cb4f2021-04-21 23:19:11 +08007521 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01007522 !wq_list_empty(&ctx->iopoll_list)) {
Hao Xu724cb4f2021-04-21 23:19:11 +08007523 needs_sched = false;
7524 break;
7525 }
7526 if (io_sqring_entries(ctx)) {
7527 needs_sched = false;
7528 break;
7529 }
7530 }
7531
7532 if (needs_sched) {
7533 mutex_unlock(&sqd->lock);
7534 schedule();
7535 mutex_lock(&sqd->lock);
7536 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007537 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7538 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007539 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007540
7541 finish_wait(&sqd->wait, &wait);
7542 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007543 }
7544
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007545 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007546 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007547 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007548 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007549 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007550 mutex_unlock(&sqd->lock);
7551
Paul Moore5bd21822021-02-16 19:46:48 -05007552 audit_free(current);
7553
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007554 complete(&sqd->exited);
7555 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007556}
7557
Jens Axboebda52162019-09-24 13:47:15 -06007558struct io_wait_queue {
7559 struct wait_queue_entry wq;
7560 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007561 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007562 unsigned nr_timeouts;
7563};
7564
Pavel Begunkov6c503152021-01-04 20:36:36 +00007565static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007566{
7567 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007568 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007569
7570 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007571 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007572 * started waiting. For timeouts, we always want to return to userspace,
7573 * regardless of event count.
7574 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007575 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007576}
7577
7578static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7579 int wake_flags, void *key)
7580{
7581 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7582 wq);
7583
Pavel Begunkov6c503152021-01-04 20:36:36 +00007584 /*
7585 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7586 * the task, and the next invocation will do it.
7587 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007588 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007589 return autoremove_wake_function(curr, mode, wake_flags, key);
7590 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007591}
7592
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007593static int io_run_task_work_sig(void)
7594{
7595 if (io_run_task_work())
7596 return 1;
7597 if (!signal_pending(current))
7598 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007599 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007600 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007601 return -EINTR;
7602}
7603
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007604/* when returns >0, the caller should retry */
7605static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7606 struct io_wait_queue *iowq,
7607 signed long *timeout)
7608{
7609 int ret;
7610
7611 /* make sure we run task_work before checking for signals */
7612 ret = io_run_task_work_sig();
7613 if (ret || io_should_wake(iowq))
7614 return ret;
7615 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007616 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007617 return 1;
7618
7619 *timeout = schedule_timeout(*timeout);
7620 return !*timeout ? -ETIME : 1;
7621}
7622
Jens Axboe2b188cc2019-01-07 10:46:33 -07007623/*
7624 * Wait until events become available, if we don't already have some. The
7625 * application must reap them itself, as they reside on the shared cq ring.
7626 */
7627static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007628 const sigset_t __user *sig, size_t sigsz,
7629 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007630{
Pavel Begunkov902910992021-08-09 09:07:32 -06007631 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007632 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007633 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7634 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007635
Jens Axboeb41e9852020-02-17 09:52:41 -07007636 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007637 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007638 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007639 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007640 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007641 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007642 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007643
Xiaoguang Wang44df58d2021-09-14 22:38:52 +08007644 if (uts) {
7645 struct timespec64 ts;
7646
7647 if (get_timespec64(&ts, uts))
7648 return -EFAULT;
7649 timeout = timespec64_to_jiffies(&ts);
7650 }
7651
Jens Axboe2b188cc2019-01-07 10:46:33 -07007652 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007653#ifdef CONFIG_COMPAT
7654 if (in_compat_syscall())
7655 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007656 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007657 else
7658#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007659 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007660
Jens Axboe2b188cc2019-01-07 10:46:33 -07007661 if (ret)
7662 return ret;
7663 }
7664
Pavel Begunkov902910992021-08-09 09:07:32 -06007665 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7666 iowq.wq.private = current;
7667 INIT_LIST_HEAD(&iowq.wq.entry);
7668 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007669 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007670 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007671
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007672 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007673 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007674 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007675 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007676 ret = -EBUSY;
7677 break;
7678 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007679 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007680 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007681 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007682 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007683 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007684 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007685
Jens Axboeb7db41c2020-07-04 08:55:50 -06007686 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007687
Hristo Venev75b28af2019-08-26 17:23:46 +00007688 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007689}
7690
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007691static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007692{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007693 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007694
7695 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007696 kfree(table[i]);
7697 kfree(table);
7698}
7699
Pavel Begunkovc0724812021-10-04 20:02:54 +01007700static __cold void **io_alloc_page_table(size_t size)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007701{
7702 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7703 size_t init_size = size;
7704 void **table;
7705
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007706 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007707 if (!table)
7708 return NULL;
7709
7710 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007711 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007712
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007713 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007714 if (!table[i]) {
7715 io_free_page_table(table, init_size);
7716 return NULL;
7717 }
7718 size -= this_size;
7719 }
7720 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007721}
7722
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007723static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7724{
7725 percpu_ref_exit(&ref_node->refs);
7726 kfree(ref_node);
7727}
7728
Pavel Begunkovc0724812021-10-04 20:02:54 +01007729static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007730{
7731 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7732 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7733 unsigned long flags;
7734 bool first_add = false;
7735
7736 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7737 node->done = true;
7738
7739 while (!list_empty(&ctx->rsrc_ref_list)) {
7740 node = list_first_entry(&ctx->rsrc_ref_list,
7741 struct io_rsrc_node, node);
7742 /* recycle ref nodes in order */
7743 if (!node->done)
7744 break;
7745 list_del(&node->node);
7746 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7747 }
7748 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7749
7750 if (first_add)
7751 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7752}
7753
7754static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7755{
7756 struct io_rsrc_node *ref_node;
7757
7758 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7759 if (!ref_node)
7760 return NULL;
7761
7762 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7763 0, GFP_KERNEL)) {
7764 kfree(ref_node);
7765 return NULL;
7766 }
7767 INIT_LIST_HEAD(&ref_node->node);
7768 INIT_LIST_HEAD(&ref_node->rsrc_list);
7769 ref_node->done = false;
7770 return ref_node;
7771}
7772
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007773static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7774 struct io_rsrc_data *data_to_kill)
Pavel Begunkovab409402021-10-09 23:14:41 +01007775 __must_hold(&ctx->uring_lock)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007776{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007777 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7778 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007779
Pavel Begunkovab409402021-10-09 23:14:41 +01007780 io_rsrc_refs_drop(ctx);
7781
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007782 if (data_to_kill) {
7783 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007784
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007785 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007786 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007787 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007788 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007789
Pavel Begunkov3e942492021-04-11 01:46:34 +01007790 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007791 percpu_ref_kill(&rsrc_node->refs);
7792 ctx->rsrc_node = NULL;
7793 }
7794
7795 if (!ctx->rsrc_node) {
7796 ctx->rsrc_node = ctx->rsrc_backup_node;
7797 ctx->rsrc_backup_node = NULL;
7798 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007799}
7800
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007801static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007802{
7803 if (ctx->rsrc_backup_node)
7804 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007805 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007806 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7807}
7808
Pavel Begunkovc0724812021-10-04 20:02:54 +01007809static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
7810 struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007811{
7812 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007813
Pavel Begunkov215c3902021-04-01 15:43:48 +01007814 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007815 if (data->quiesce)
7816 return -ENXIO;
7817
7818 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007819 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007820 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007821 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007822 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007823 io_rsrc_node_switch(ctx, data);
7824
Pavel Begunkov3e942492021-04-11 01:46:34 +01007825 /* kill initial ref, already quiesced if zero */
7826 if (atomic_dec_and_test(&data->refs))
7827 break;
Jens Axboec018db42021-08-09 08:15:50 -06007828 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007829 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007830 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007831 if (!ret) {
7832 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007833 break;
Jens Axboec018db42021-08-09 08:15:50 -06007834 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007835
Pavel Begunkov3e942492021-04-11 01:46:34 +01007836 atomic_inc(&data->refs);
7837 /* wait for all works potentially completing data->done */
7838 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007839 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007840
Hao Xu8bad28d2021-02-19 17:19:36 +08007841 ret = io_run_task_work_sig();
7842 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007843 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007844 data->quiesce = false;
7845
Hao Xu8bad28d2021-02-19 17:19:36 +08007846 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007847}
7848
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007849static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7850{
7851 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7852 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7853
7854 return &data->tags[table_idx][off];
7855}
7856
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007857static void io_rsrc_data_free(struct io_rsrc_data *data)
7858{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007859 size_t size = data->nr * sizeof(data->tags[0][0]);
7860
7861 if (data->tags)
7862 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007863 kfree(data);
7864}
7865
Pavel Begunkovc0724812021-10-04 20:02:54 +01007866static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7867 u64 __user *utags, unsigned nr,
7868 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007869{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007870 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007871 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007872 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007873
7874 data = kzalloc(sizeof(*data), GFP_KERNEL);
7875 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007876 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007877 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007878 if (!data->tags) {
7879 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007880 return -ENOMEM;
7881 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007882
7883 data->nr = nr;
7884 data->ctx = ctx;
7885 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007886 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007887 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007888 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007889 u64 *tag_slot = io_get_tag_slot(data, i);
7890
7891 if (copy_from_user(tag_slot, &utags[i],
7892 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007893 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007894 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007895 }
7896
Pavel Begunkov3e942492021-04-11 01:46:34 +01007897 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007898 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007899 *pdata = data;
7900 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007901fail:
7902 io_rsrc_data_free(data);
7903 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007904}
7905
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007906static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7907{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007908 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7909 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007910 return !!table->files;
7911}
7912
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007913static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007914{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007915 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007916 table->files = NULL;
7917}
7918
Jens Axboe2b188cc2019-01-07 10:46:33 -07007919static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7920{
7921#if defined(CONFIG_UNIX)
7922 if (ctx->ring_sock) {
7923 struct sock *sock = ctx->ring_sock->sk;
7924 struct sk_buff *skb;
7925
7926 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7927 kfree_skb(skb);
7928 }
7929#else
7930 int i;
7931
7932 for (i = 0; i < ctx->nr_user_files; i++) {
7933 struct file *file;
7934
7935 file = io_file_from_index(ctx, i);
7936 if (file)
7937 fput(file);
7938 }
7939#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007940 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007941 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007942 ctx->file_data = NULL;
7943 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007944}
7945
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007946static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7947{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007948 int ret;
7949
Pavel Begunkov08480402021-04-13 02:58:38 +01007950 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007951 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007952 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7953 if (!ret)
7954 __io_sqe_files_unregister(ctx);
7955 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007956}
7957
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007958static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007959 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007960{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007961 WARN_ON_ONCE(sqd->thread == current);
7962
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007963 /*
7964 * Do the dance but not conditional clear_bit() because it'd race with
7965 * other threads incrementing park_pending and setting the bit.
7966 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007967 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007968 if (atomic_dec_return(&sqd->park_pending))
7969 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007970 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007971}
7972
Jens Axboe86e0d672021-03-05 08:44:39 -07007973static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007974 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007975{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007976 WARN_ON_ONCE(sqd->thread == current);
7977
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007978 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007979 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007980 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007981 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007982 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007983}
7984
7985static void io_sq_thread_stop(struct io_sq_data *sqd)
7986{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007987 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007988 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007989
Jens Axboe05962f92021-03-06 13:58:48 -07007990 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007991 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007992 if (sqd->thread)
7993 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007994 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007995 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007996}
7997
Jens Axboe534ca6d2020-09-02 13:52:19 -06007998static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007999{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008000 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008001 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
8002
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008003 io_sq_thread_stop(sqd);
8004 kfree(sqd);
8005 }
8006}
8007
8008static void io_sq_thread_finish(struct io_ring_ctx *ctx)
8009{
8010 struct io_sq_data *sqd = ctx->sq_data;
8011
8012 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07008013 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00008014 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008015 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07008016 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008017
8018 io_put_sq_data(sqd);
8019 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008020 }
8021}
8022
Jens Axboeaa061652020-09-02 14:50:27 -06008023static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
8024{
8025 struct io_ring_ctx *ctx_attach;
8026 struct io_sq_data *sqd;
8027 struct fd f;
8028
8029 f = fdget(p->wq_fd);
8030 if (!f.file)
8031 return ERR_PTR(-ENXIO);
8032 if (f.file->f_op != &io_uring_fops) {
8033 fdput(f);
8034 return ERR_PTR(-EINVAL);
8035 }
8036
8037 ctx_attach = f.file->private_data;
8038 sqd = ctx_attach->sq_data;
8039 if (!sqd) {
8040 fdput(f);
8041 return ERR_PTR(-EINVAL);
8042 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07008043 if (sqd->task_tgid != current->tgid) {
8044 fdput(f);
8045 return ERR_PTR(-EPERM);
8046 }
Jens Axboeaa061652020-09-02 14:50:27 -06008047
8048 refcount_inc(&sqd->refs);
8049 fdput(f);
8050 return sqd;
8051}
8052
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008053static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
8054 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06008055{
8056 struct io_sq_data *sqd;
8057
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008058 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008059 if (p->flags & IORING_SETUP_ATTACH_WQ) {
8060 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008061 if (!IS_ERR(sqd)) {
8062 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008063 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008064 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07008065 /* fall through for EPERM case, setup new sqd/task */
8066 if (PTR_ERR(sqd) != -EPERM)
8067 return sqd;
8068 }
Jens Axboeaa061652020-09-02 14:50:27 -06008069
Jens Axboe534ca6d2020-09-02 13:52:19 -06008070 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
8071 if (!sqd)
8072 return ERR_PTR(-ENOMEM);
8073
Pavel Begunkov9e138a42021-03-14 20:57:12 +00008074 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008075 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06008076 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00008077 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008078 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008079 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008080 return sqd;
8081}
8082
Jens Axboe6b063142019-01-10 22:13:58 -07008083#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07008084/*
8085 * Ensure the UNIX gc is aware of our file set, so we are certain that
8086 * the io_uring can be safely unregistered on process exit, even if we have
8087 * loops in the file referencing.
8088 */
8089static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
8090{
8091 struct sock *sk = ctx->ring_sock->sk;
8092 struct scm_fp_list *fpl;
8093 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06008094 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07008095
Jens Axboe6b063142019-01-10 22:13:58 -07008096 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
8097 if (!fpl)
8098 return -ENOMEM;
8099
8100 skb = alloc_skb(0, GFP_KERNEL);
8101 if (!skb) {
8102 kfree(fpl);
8103 return -ENOMEM;
8104 }
8105
8106 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07008107
Jens Axboe08a45172019-10-03 08:11:03 -06008108 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07008109 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07008110 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008111 struct file *file = io_file_from_index(ctx, i + offset);
8112
8113 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06008114 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06008115 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06008116 unix_inflight(fpl->user, fpl->fp[nr_files]);
8117 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07008118 }
8119
Jens Axboe08a45172019-10-03 08:11:03 -06008120 if (nr_files) {
8121 fpl->max = SCM_MAX_FD;
8122 fpl->count = nr_files;
8123 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008124 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06008125 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
8126 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07008127
Jens Axboe08a45172019-10-03 08:11:03 -06008128 for (i = 0; i < nr_files; i++)
8129 fput(fpl->fp[i]);
8130 } else {
8131 kfree_skb(skb);
8132 kfree(fpl);
8133 }
Jens Axboe6b063142019-01-10 22:13:58 -07008134
8135 return 0;
8136}
8137
8138/*
8139 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8140 * causes regular reference counting to break down. We rely on the UNIX
8141 * garbage collection to take care of this problem for us.
8142 */
8143static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8144{
8145 unsigned left, total;
8146 int ret = 0;
8147
8148 total = 0;
8149 left = ctx->nr_user_files;
8150 while (left) {
8151 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008152
8153 ret = __io_sqe_files_scm(ctx, this_files, total);
8154 if (ret)
8155 break;
8156 left -= this_files;
8157 total += this_files;
8158 }
8159
8160 if (!ret)
8161 return 0;
8162
8163 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008164 struct file *file = io_file_from_index(ctx, total);
8165
8166 if (file)
8167 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008168 total++;
8169 }
8170
8171 return ret;
8172}
8173#else
8174static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8175{
8176 return 0;
8177}
8178#endif
8179
Pavel Begunkov47e90392021-04-01 15:43:56 +01008180static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008181{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008182 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008183#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008184 struct sock *sock = ctx->ring_sock->sk;
8185 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8186 struct sk_buff *skb;
8187 int i;
8188
8189 __skb_queue_head_init(&list);
8190
8191 /*
8192 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8193 * remove this entry and rearrange the file array.
8194 */
8195 skb = skb_dequeue(head);
8196 while (skb) {
8197 struct scm_fp_list *fp;
8198
8199 fp = UNIXCB(skb).fp;
8200 for (i = 0; i < fp->count; i++) {
8201 int left;
8202
8203 if (fp->fp[i] != file)
8204 continue;
8205
8206 unix_notinflight(fp->user, fp->fp[i]);
8207 left = fp->count - 1 - i;
8208 if (left) {
8209 memmove(&fp->fp[i], &fp->fp[i + 1],
8210 left * sizeof(struct file *));
8211 }
8212 fp->count--;
8213 if (!fp->count) {
8214 kfree_skb(skb);
8215 skb = NULL;
8216 } else {
8217 __skb_queue_tail(&list, skb);
8218 }
8219 fput(file);
8220 file = NULL;
8221 break;
8222 }
8223
8224 if (!file)
8225 break;
8226
8227 __skb_queue_tail(&list, skb);
8228
8229 skb = skb_dequeue(head);
8230 }
8231
8232 if (skb_peek(&list)) {
8233 spin_lock_irq(&head->lock);
8234 while ((skb = __skb_dequeue(&list)) != NULL)
8235 __skb_queue_tail(head, skb);
8236 spin_unlock_irq(&head->lock);
8237 }
8238#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008239 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008240#endif
8241}
8242
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008243static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008244{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008245 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008246 struct io_ring_ctx *ctx = rsrc_data->ctx;
8247 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008248
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008249 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8250 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008251
8252 if (prsrc->tag) {
8253 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008254
8255 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008256 spin_lock(&ctx->completion_lock);
Pavel Begunkov913a5712021-11-10 15:49:31 +00008257 io_fill_cqe_aux(ctx, prsrc->tag, 0, 0);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008258 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008259 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008260 io_cqring_ev_posted(ctx);
8261 io_ring_submit_unlock(ctx, lock_ring);
8262 }
8263
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008264 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008265 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008266 }
8267
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008268 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008269 if (atomic_dec_and_test(&rsrc_data->refs))
8270 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008271}
8272
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008273static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008274{
8275 struct io_ring_ctx *ctx;
8276 struct llist_node *node;
8277
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008278 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8279 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008280
8281 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008282 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008283 struct llist_node *next = node->next;
8284
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008285 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008286 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008287 node = next;
8288 }
8289}
8290
Jens Axboe05f3fb32019-12-09 11:22:50 -07008291static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008292 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008293{
8294 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008295 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008296 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008297 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008298
8299 if (ctx->file_data)
8300 return -EBUSY;
8301 if (!nr_args)
8302 return -EINVAL;
8303 if (nr_args > IORING_MAX_FIXED_FILES)
8304 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008305 if (nr_args > rlimit(RLIMIT_NOFILE))
8306 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008307 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008308 if (ret)
8309 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008310 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8311 &ctx->file_data);
8312 if (ret)
8313 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008314
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008315 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008316 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008317 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008318
Jens Axboe05f3fb32019-12-09 11:22:50 -07008319 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008320 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008321 ret = -EFAULT;
8322 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008323 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008324 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008325 if (fd == -1) {
8326 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008327 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008328 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008329 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008330 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008331
Jens Axboe05f3fb32019-12-09 11:22:50 -07008332 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008333 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008334 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008335 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008336
8337 /*
8338 * Don't allow io_uring instances to be registered. If UNIX
8339 * isn't enabled, then this causes a reference cycle and this
8340 * instance can never get freed. If UNIX is enabled we'll
8341 * handle it just fine, but there's still no point in allowing
8342 * a ring fd as it doesn't support regular read/write anyway.
8343 */
8344 if (file->f_op == &io_uring_fops) {
8345 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008346 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008347 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008348 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008349 }
8350
Jens Axboe05f3fb32019-12-09 11:22:50 -07008351 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008352 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008353 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008354 return ret;
8355 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008356
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008357 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008358 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008359out_fput:
8360 for (i = 0; i < ctx->nr_user_files; i++) {
8361 file = io_file_from_index(ctx, i);
8362 if (file)
8363 fput(file);
8364 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008365 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008366 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008367out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008368 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008369 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008370 return ret;
8371}
8372
Jens Axboec3a31e62019-10-03 13:59:56 -06008373static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8374 int index)
8375{
8376#if defined(CONFIG_UNIX)
8377 struct sock *sock = ctx->ring_sock->sk;
8378 struct sk_buff_head *head = &sock->sk_receive_queue;
8379 struct sk_buff *skb;
8380
8381 /*
8382 * See if we can merge this file into an existing skb SCM_RIGHTS
8383 * file set. If there's no room, fall back to allocating a new skb
8384 * and filling it in.
8385 */
8386 spin_lock_irq(&head->lock);
8387 skb = skb_peek(head);
8388 if (skb) {
8389 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8390
8391 if (fpl->count < SCM_MAX_FD) {
8392 __skb_unlink(skb, head);
8393 spin_unlock_irq(&head->lock);
8394 fpl->fp[fpl->count] = get_file(file);
8395 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8396 fpl->count++;
8397 spin_lock_irq(&head->lock);
8398 __skb_queue_head(head, skb);
8399 } else {
8400 skb = NULL;
8401 }
8402 }
8403 spin_unlock_irq(&head->lock);
8404
8405 if (skb) {
8406 fput(file);
8407 return 0;
8408 }
8409
8410 return __io_sqe_files_scm(ctx, 1, index);
8411#else
8412 return 0;
8413#endif
8414}
8415
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008416static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
8417 struct io_rsrc_node *node, void *rsrc)
8418{
8419 struct io_rsrc_put *prsrc;
8420
8421 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8422 if (!prsrc)
8423 return -ENOMEM;
8424
8425 prsrc->tag = *io_get_tag_slot(data, idx);
8426 prsrc->rsrc = rsrc;
8427 list_add(&prsrc->list, &node->rsrc_list);
8428 return 0;
8429}
8430
Pavel Begunkovb9445592021-08-25 12:25:45 +01008431static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8432 unsigned int issue_flags, u32 slot_index)
8433{
8434 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08008435 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008436 bool needs_switch = false;
Pavel Begunkovb9445592021-08-25 12:25:45 +01008437 struct io_fixed_file *file_slot;
8438 int ret = -EBADF;
8439
Hao Xu3b44b372021-10-18 21:34:31 +08008440 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008441 if (file->f_op == &io_uring_fops)
8442 goto err;
8443 ret = -ENXIO;
8444 if (!ctx->file_data)
8445 goto err;
8446 ret = -EINVAL;
8447 if (slot_index >= ctx->nr_user_files)
8448 goto err;
8449
8450 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8451 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008452
8453 if (file_slot->file_ptr) {
8454 struct file *old_file;
8455
8456 ret = io_rsrc_node_switch_start(ctx);
8457 if (ret)
8458 goto err;
8459
8460 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8461 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
8462 ctx->rsrc_node, old_file);
8463 if (ret)
8464 goto err;
8465 file_slot->file_ptr = 0;
8466 needs_switch = true;
8467 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01008468
8469 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8470 io_fixed_file_set(file_slot, file);
8471 ret = io_sqe_file_register(ctx, file, slot_index);
8472 if (ret) {
8473 file_slot->file_ptr = 0;
8474 goto err;
8475 }
8476
8477 ret = 0;
8478err:
Pavel Begunkov9c7b0ba2021-09-14 16:12:52 +01008479 if (needs_switch)
8480 io_rsrc_node_switch(ctx, ctx->file_data);
Hao Xu3b44b372021-10-18 21:34:31 +08008481 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkovb9445592021-08-25 12:25:45 +01008482 if (ret)
8483 fput(file);
8484 return ret;
8485}
8486
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008487static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
8488{
8489 unsigned int offset = req->close.file_slot - 1;
8490 struct io_ring_ctx *ctx = req->ctx;
Hao Xu3b44b372021-10-18 21:34:31 +08008491 bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008492 struct io_fixed_file *file_slot;
8493 struct file *file;
8494 int ret, i;
8495
Hao Xu3b44b372021-10-18 21:34:31 +08008496 io_ring_submit_lock(ctx, needs_lock);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008497 ret = -ENXIO;
8498 if (unlikely(!ctx->file_data))
8499 goto out;
8500 ret = -EINVAL;
8501 if (offset >= ctx->nr_user_files)
8502 goto out;
8503 ret = io_rsrc_node_switch_start(ctx);
8504 if (ret)
8505 goto out;
8506
8507 i = array_index_nospec(offset, ctx->nr_user_files);
8508 file_slot = io_fixed_file_slot(&ctx->file_table, i);
8509 ret = -EBADF;
8510 if (!file_slot->file_ptr)
8511 goto out;
8512
8513 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
8514 ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
8515 if (ret)
8516 goto out;
8517
8518 file_slot->file_ptr = 0;
8519 io_rsrc_node_switch(ctx, ctx->file_data);
8520 ret = 0;
8521out:
Hao Xu3b44b372021-10-18 21:34:31 +08008522 io_ring_submit_unlock(ctx, needs_lock);
Pavel Begunkov7df778b2021-09-24 20:04:29 +01008523 return ret;
8524}
8525
Jens Axboe05f3fb32019-12-09 11:22:50 -07008526static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008527 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008528 unsigned nr_args)
8529{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008530 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008531 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008532 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008533 struct io_fixed_file *file_slot;
8534 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008535 int fd, i, err = 0;
8536 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008537 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008538
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008539 if (!ctx->file_data)
8540 return -ENXIO;
8541 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008542 return -EINVAL;
8543
Pavel Begunkov67973b92021-01-26 13:51:09 +00008544 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008545 u64 tag = 0;
8546
8547 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8548 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008549 err = -EFAULT;
8550 break;
8551 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008552 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8553 err = -EINVAL;
8554 break;
8555 }
noah4e0377a2021-01-26 15:23:28 -05008556 if (fd == IORING_REGISTER_FILES_SKIP)
8557 continue;
8558
Pavel Begunkov67973b92021-01-26 13:51:09 +00008559 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008560 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008561
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008562 if (file_slot->file_ptr) {
8563 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008564 err = io_queue_rsrc_removal(data, up->offset + done,
8565 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008566 if (err)
8567 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008568 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008569 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008570 }
8571 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008572 file = fget(fd);
8573 if (!file) {
8574 err = -EBADF;
8575 break;
8576 }
8577 /*
8578 * Don't allow io_uring instances to be registered. If
8579 * UNIX isn't enabled, then this causes a reference
8580 * cycle and this instance can never get freed. If UNIX
8581 * is enabled we'll handle it just fine, but there's
8582 * still no point in allowing a ring fd as it doesn't
8583 * support regular read/write anyway.
8584 */
8585 if (file->f_op == &io_uring_fops) {
8586 fput(file);
8587 err = -EBADF;
8588 break;
8589 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008590 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008591 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008592 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008593 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008594 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008595 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008596 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008597 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008598 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008599 }
8600
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008601 if (needs_switch)
8602 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008603 return done ? done : err;
8604}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008605
Jens Axboe685fe7f2021-03-08 09:37:51 -07008606static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8607 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008608{
Jens Axboee9418942021-02-19 12:33:30 -07008609 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008610 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008611 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008612
Yang Yingliang362a9e62021-07-20 16:38:05 +08008613 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008614 hash = ctx->hash_map;
8615 if (!hash) {
8616 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008617 if (!hash) {
8618 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008619 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008620 }
Jens Axboee9418942021-02-19 12:33:30 -07008621 refcount_set(&hash->refs, 1);
8622 init_waitqueue_head(&hash->wait);
8623 ctx->hash_map = hash;
8624 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008625 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008626
8627 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008628 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008629 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008630 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008631
Jens Axboed25e3a32021-02-16 11:41:41 -07008632 /* Do QD, or 4 * CPUS, whatever is smallest */
8633 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008634
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008635 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008636}
8637
Pavel Begunkovc0724812021-10-04 20:02:54 +01008638static __cold int io_uring_alloc_task_context(struct task_struct *task,
8639 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008640{
8641 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008642 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008643
Pavel Begunkov09899b12021-06-14 02:36:22 +01008644 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008645 if (unlikely(!tctx))
8646 return -ENOMEM;
8647
Jens Axboed8a6df12020-10-15 16:24:45 -06008648 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8649 if (unlikely(ret)) {
8650 kfree(tctx);
8651 return ret;
8652 }
8653
Jens Axboe685fe7f2021-03-08 09:37:51 -07008654 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008655 if (IS_ERR(tctx->io_wq)) {
8656 ret = PTR_ERR(tctx->io_wq);
8657 percpu_counter_destroy(&tctx->inflight);
8658 kfree(tctx);
8659 return ret;
8660 }
8661
Jens Axboe0f212202020-09-13 13:09:39 -06008662 xa_init(&tctx->xa);
8663 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008664 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008665 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008666 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008667 spin_lock_init(&tctx->task_lock);
8668 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008669 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008670 return 0;
8671}
8672
8673void __io_uring_free(struct task_struct *tsk)
8674{
8675 struct io_uring_task *tctx = tsk->io_uring;
8676
8677 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008678 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008679 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008680
Jens Axboed8a6df12020-10-15 16:24:45 -06008681 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008682 kfree(tctx);
8683 tsk->io_uring = NULL;
8684}
8685
Pavel Begunkovc0724812021-10-04 20:02:54 +01008686static __cold int io_sq_offload_create(struct io_ring_ctx *ctx,
8687 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008688{
8689 int ret;
8690
Jens Axboed25e3a32021-02-16 11:41:41 -07008691 /* Retain compatibility with failing for an invalid attach attempt */
8692 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8693 IORING_SETUP_ATTACH_WQ) {
8694 struct fd f;
8695
8696 f = fdget(p->wq_fd);
8697 if (!f.file)
8698 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008699 if (f.file->f_op != &io_uring_fops) {
8700 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008701 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008702 }
8703 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008704 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008705 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008706 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008707 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008708 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008709
Paul Moorecdc14042021-02-01 19:56:49 -05008710 ret = security_uring_sqpoll();
8711 if (ret)
8712 return ret;
8713
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008714 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008715 if (IS_ERR(sqd)) {
8716 ret = PTR_ERR(sqd);
8717 goto err;
8718 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008719
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008720 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008721 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008722 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8723 if (!ctx->sq_thread_idle)
8724 ctx->sq_thread_idle = HZ;
8725
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008726 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008727 list_add(&ctx->sqd_list, &sqd->ctx_list);
8728 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008729 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008730 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008731 io_sq_thread_unpark(sqd);
8732
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008733 if (ret < 0)
8734 goto err;
8735 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008736 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008737
Jens Axboe6c271ce2019-01-10 11:22:30 -07008738 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008739 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008740
Jens Axboe917257d2019-04-13 09:28:55 -06008741 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008742 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008743 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008744 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008745 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008746 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008747 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008748
8749 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008750 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008751 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8752 if (IS_ERR(tsk)) {
8753 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008754 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008755 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008756
Jens Axboe46fe18b2021-03-04 12:39:36 -07008757 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008758 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008759 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008760 if (ret)
8761 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008762 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8763 /* Can't have SQ_AFF without SQPOLL */
8764 ret = -EINVAL;
8765 goto err;
8766 }
8767
Jens Axboe2b188cc2019-01-07 10:46:33 -07008768 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008769err_sqpoll:
8770 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008771err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008772 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008773 return ret;
8774}
8775
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008776static inline void __io_unaccount_mem(struct user_struct *user,
8777 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008778{
8779 atomic_long_sub(nr_pages, &user->locked_vm);
8780}
8781
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008782static inline int __io_account_mem(struct user_struct *user,
8783 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008784{
8785 unsigned long page_limit, cur_pages, new_pages;
8786
8787 /* Don't allow more pages than we can safely lock */
8788 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8789
8790 do {
8791 cur_pages = atomic_long_read(&user->locked_vm);
8792 new_pages = cur_pages + nr_pages;
8793 if (new_pages > page_limit)
8794 return -ENOMEM;
8795 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8796 new_pages) != cur_pages);
8797
8798 return 0;
8799}
8800
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008801static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008802{
Jens Axboe62e398b2021-02-21 16:19:37 -07008803 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008804 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008805
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008806 if (ctx->mm_account)
8807 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008808}
8809
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008810static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008811{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008812 int ret;
8813
Jens Axboe62e398b2021-02-21 16:19:37 -07008814 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008815 ret = __io_account_mem(ctx->user, nr_pages);
8816 if (ret)
8817 return ret;
8818 }
8819
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008820 if (ctx->mm_account)
8821 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008822
8823 return 0;
8824}
8825
Jens Axboe2b188cc2019-01-07 10:46:33 -07008826static void io_mem_free(void *ptr)
8827{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008828 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008829
Mark Rutland52e04ef2019-04-30 17:30:21 +01008830 if (!ptr)
8831 return;
8832
8833 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008834 if (put_page_testzero(page))
8835 free_compound_page(page);
8836}
8837
8838static void *io_mem_alloc(size_t size)
8839{
8840 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008841 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008842
8843 return (void *) __get_free_pages(gfp_flags, get_order(size));
8844}
8845
Hristo Venev75b28af2019-08-26 17:23:46 +00008846static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8847 size_t *sq_offset)
8848{
8849 struct io_rings *rings;
8850 size_t off, sq_array_size;
8851
8852 off = struct_size(rings, cqes, cq_entries);
8853 if (off == SIZE_MAX)
8854 return SIZE_MAX;
8855
8856#ifdef CONFIG_SMP
8857 off = ALIGN(off, SMP_CACHE_BYTES);
8858 if (off == 0)
8859 return SIZE_MAX;
8860#endif
8861
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008862 if (sq_offset)
8863 *sq_offset = off;
8864
Hristo Venev75b28af2019-08-26 17:23:46 +00008865 sq_array_size = array_size(sizeof(u32), sq_entries);
8866 if (sq_array_size == SIZE_MAX)
8867 return SIZE_MAX;
8868
8869 if (check_add_overflow(off, sq_array_size, &off))
8870 return SIZE_MAX;
8871
Hristo Venev75b28af2019-08-26 17:23:46 +00008872 return off;
8873}
8874
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008875static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008876{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008877 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008878 unsigned int i;
8879
Pavel Begunkov62248432021-04-28 13:11:29 +01008880 if (imu != ctx->dummy_ubuf) {
8881 for (i = 0; i < imu->nr_bvecs; i++)
8882 unpin_user_page(imu->bvec[i].bv_page);
8883 if (imu->acct_pages)
8884 io_unaccount_mem(ctx, imu->acct_pages);
8885 kvfree(imu);
8886 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008887 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008888}
8889
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008890static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8891{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008892 io_buffer_unmap(ctx, &prsrc->buf);
8893 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008894}
8895
8896static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008897{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008898 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008899
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008900 for (i = 0; i < ctx->nr_user_bufs; i++)
8901 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008902 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008903 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008904 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008905 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008906 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008907}
8908
Jens Axboeedafcce2019-01-09 09:16:05 -07008909static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8910{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008911 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008912
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008913 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008914 return -ENXIO;
8915
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008916 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8917 if (!ret)
8918 __io_sqe_buffers_unregister(ctx);
8919 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008920}
8921
8922static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8923 void __user *arg, unsigned index)
8924{
8925 struct iovec __user *src;
8926
8927#ifdef CONFIG_COMPAT
8928 if (ctx->compat) {
8929 struct compat_iovec __user *ciovs;
8930 struct compat_iovec ciov;
8931
8932 ciovs = (struct compat_iovec __user *) arg;
8933 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8934 return -EFAULT;
8935
Jens Axboed55e5f52019-12-11 16:12:15 -07008936 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008937 dst->iov_len = ciov.iov_len;
8938 return 0;
8939 }
8940#endif
8941 src = (struct iovec __user *) arg;
8942 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8943 return -EFAULT;
8944 return 0;
8945}
8946
Jens Axboede293932020-09-17 16:19:16 -06008947/*
8948 * Not super efficient, but this is just a registration time. And we do cache
8949 * the last compound head, so generally we'll only do a full search if we don't
8950 * match that one.
8951 *
8952 * We check if the given compound head page has already been accounted, to
8953 * avoid double accounting it. This allows us to account the full size of the
8954 * page, not just the constituent pages of a huge page.
8955 */
8956static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8957 int nr_pages, struct page *hpage)
8958{
8959 int i, j;
8960
8961 /* check current page array */
8962 for (i = 0; i < nr_pages; i++) {
8963 if (!PageCompound(pages[i]))
8964 continue;
8965 if (compound_head(pages[i]) == hpage)
8966 return true;
8967 }
8968
8969 /* check previously registered pages */
8970 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008971 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008972
8973 for (j = 0; j < imu->nr_bvecs; j++) {
8974 if (!PageCompound(imu->bvec[j].bv_page))
8975 continue;
8976 if (compound_head(imu->bvec[j].bv_page) == hpage)
8977 return true;
8978 }
8979 }
8980
8981 return false;
8982}
8983
8984static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8985 int nr_pages, struct io_mapped_ubuf *imu,
8986 struct page **last_hpage)
8987{
8988 int i, ret;
8989
Pavel Begunkov216e5832021-05-29 12:01:02 +01008990 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008991 for (i = 0; i < nr_pages; i++) {
8992 if (!PageCompound(pages[i])) {
8993 imu->acct_pages++;
8994 } else {
8995 struct page *hpage;
8996
8997 hpage = compound_head(pages[i]);
8998 if (hpage == *last_hpage)
8999 continue;
9000 *last_hpage = hpage;
9001 if (headpage_already_acct(ctx, pages, i, hpage))
9002 continue;
9003 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
9004 }
9005 }
9006
9007 if (!imu->acct_pages)
9008 return 0;
9009
Jens Axboe26bfa89e2021-02-09 20:14:12 -07009010 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06009011 if (ret)
9012 imu->acct_pages = 0;
9013 return ret;
9014}
9015
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009016static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009017 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009018 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07009019{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009020 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07009021 struct vm_area_struct **vmas = NULL;
9022 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009023 unsigned long off, start, end, ubuf;
9024 size_t size;
9025 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07009026
Pavel Begunkov62248432021-04-28 13:11:29 +01009027 if (!iov->iov_base) {
9028 *pimu = ctx->dummy_ubuf;
9029 return 0;
9030 }
9031
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009032 ubuf = (unsigned long) iov->iov_base;
9033 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
9034 start = ubuf >> PAGE_SHIFT;
9035 nr_pages = end - start;
9036
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009037 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009038 ret = -ENOMEM;
9039
9040 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
9041 if (!pages)
9042 goto done;
9043
9044 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
9045 GFP_KERNEL);
9046 if (!vmas)
9047 goto done;
9048
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009049 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01009050 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009051 goto done;
9052
9053 ret = 0;
9054 mmap_read_lock(current->mm);
9055 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
9056 pages, vmas);
9057 if (pret == nr_pages) {
9058 /* don't support file backed memory */
9059 for (i = 0; i < nr_pages; i++) {
9060 struct vm_area_struct *vma = vmas[i];
9061
Pavel Begunkov40dad762021-06-09 15:26:54 +01009062 if (vma_is_shmem(vma))
9063 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009064 if (vma->vm_file &&
9065 !is_file_hugepages(vma->vm_file)) {
9066 ret = -EOPNOTSUPP;
9067 break;
9068 }
9069 }
9070 } else {
9071 ret = pret < 0 ? pret : -EFAULT;
9072 }
9073 mmap_read_unlock(current->mm);
9074 if (ret) {
9075 /*
9076 * if we did partial map, or found file backed vmas,
9077 * release any pages we did get
9078 */
9079 if (pret > 0)
9080 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009081 goto done;
9082 }
9083
9084 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
9085 if (ret) {
9086 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009087 goto done;
9088 }
9089
9090 off = ubuf & ~PAGE_MASK;
9091 size = iov->iov_len;
9092 for (i = 0; i < nr_pages; i++) {
9093 size_t vec_len;
9094
9095 vec_len = min_t(size_t, size, PAGE_SIZE - off);
9096 imu->bvec[i].bv_page = pages[i];
9097 imu->bvec[i].bv_len = vec_len;
9098 imu->bvec[i].bv_offset = off;
9099 off = 0;
9100 size -= vec_len;
9101 }
9102 /* store original address for later verification */
9103 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01009104 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009105 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009106 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009107 ret = 0;
9108done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009109 if (ret)
9110 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009111 kvfree(pages);
9112 kvfree(vmas);
9113 return ret;
9114}
9115
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009116static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009117{
Pavel Begunkov87094462021-04-11 01:46:36 +01009118 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
9119 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009120}
9121
9122static int io_buffer_validate(struct iovec *iov)
9123{
Pavel Begunkov50e96982021-03-24 22:59:01 +00009124 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
9125
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009126 /*
9127 * Don't impose further limits on the size and buffer
9128 * constraints here, we'll -EINVAL later when IO is
9129 * submitted if they are wrong.
9130 */
Pavel Begunkov62248432021-04-28 13:11:29 +01009131 if (!iov->iov_base)
9132 return iov->iov_len ? -EFAULT : 0;
9133 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009134 return -EFAULT;
9135
9136 /* arbitrary limit, but we need something */
9137 if (iov->iov_len > SZ_1G)
9138 return -EFAULT;
9139
Pavel Begunkov50e96982021-03-24 22:59:01 +00009140 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
9141 return -EOVERFLOW;
9142
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009143 return 0;
9144}
9145
9146static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009147 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009148{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009149 struct page *last_hpage = NULL;
9150 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009151 int i, ret;
9152 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009153
Pavel Begunkov87094462021-04-11 01:46:36 +01009154 if (ctx->user_bufs)
9155 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01009156 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01009157 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009158 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009159 if (ret)
9160 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01009161 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9162 if (ret)
9163 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009164 ret = io_buffers_map_alloc(ctx, nr_args);
9165 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08009166 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009167 return ret;
9168 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009169
Pavel Begunkov87094462021-04-11 01:46:36 +01009170 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07009171 ret = io_copy_iov(ctx, &iov, arg, i);
9172 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009173 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009174 ret = io_buffer_validate(&iov);
9175 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009176 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009177 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009178 ret = -EINVAL;
9179 break;
9180 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009181
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009182 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9183 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009184 if (ret)
9185 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009186 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009187
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009188 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009189
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009190 ctx->buf_data = data;
9191 if (ret)
9192 __io_sqe_buffers_unregister(ctx);
9193 else
9194 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009195 return ret;
9196}
9197
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009198static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9199 struct io_uring_rsrc_update2 *up,
9200 unsigned int nr_args)
9201{
9202 u64 __user *tags = u64_to_user_ptr(up->tags);
9203 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009204 struct page *last_hpage = NULL;
9205 bool needs_switch = false;
9206 __u32 done;
9207 int i, err;
9208
9209 if (!ctx->buf_data)
9210 return -ENXIO;
9211 if (up->offset + nr_args > ctx->nr_user_bufs)
9212 return -EINVAL;
9213
9214 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009215 struct io_mapped_ubuf *imu;
9216 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009217 u64 tag = 0;
9218
9219 err = io_copy_iov(ctx, &iov, iovs, done);
9220 if (err)
9221 break;
9222 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9223 err = -EFAULT;
9224 break;
9225 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009226 err = io_buffer_validate(&iov);
9227 if (err)
9228 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009229 if (!iov.iov_base && tag) {
9230 err = -EINVAL;
9231 break;
9232 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009233 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9234 if (err)
9235 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009236
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009237 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009238 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009239 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9240 ctx->rsrc_node, ctx->user_bufs[i]);
9241 if (unlikely(err)) {
9242 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009243 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009244 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009245 ctx->user_bufs[i] = NULL;
9246 needs_switch = true;
9247 }
9248
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009249 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009250 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009251 }
9252
9253 if (needs_switch)
9254 io_rsrc_node_switch(ctx, ctx->buf_data);
9255 return done ? done : err;
9256}
9257
Jens Axboe9b402842019-04-11 11:45:41 -06009258static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9259{
9260 __s32 __user *fds = arg;
9261 int fd;
9262
9263 if (ctx->cq_ev_fd)
9264 return -EBUSY;
9265
9266 if (copy_from_user(&fd, fds, sizeof(*fds)))
9267 return -EFAULT;
9268
9269 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9270 if (IS_ERR(ctx->cq_ev_fd)) {
9271 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009272
Jens Axboe9b402842019-04-11 11:45:41 -06009273 ctx->cq_ev_fd = NULL;
9274 return ret;
9275 }
9276
9277 return 0;
9278}
9279
9280static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9281{
9282 if (ctx->cq_ev_fd) {
9283 eventfd_ctx_put(ctx->cq_ev_fd);
9284 ctx->cq_ev_fd = NULL;
9285 return 0;
9286 }
9287
9288 return -ENXIO;
9289}
9290
Jens Axboe5a2e7452020-02-23 16:23:11 -07009291static void io_destroy_buffers(struct io_ring_ctx *ctx)
9292{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009293 struct io_buffer *buf;
9294 unsigned long index;
9295
Jens Axboe8bab4c02021-09-24 07:12:27 -06009296 xa_for_each(&ctx->io_buffers, index, buf) {
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009297 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009298 cond_resched();
9299 }
Jens Axboe5a2e7452020-02-23 16:23:11 -07009300}
9301
Jens Axboe4010fec2021-02-27 15:04:18 -07009302static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009303{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009304 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009305 int nr = 0;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009306
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009307 mutex_lock(&ctx->uring_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009308 io_flush_cached_locked_reqs(ctx, state);
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009309
9310 while (state->free_list.next) {
9311 struct io_wq_work_node *node;
9312 struct io_kiocb *req;
9313
9314 node = wq_stack_extract(&state->free_list);
9315 req = container_of(node, struct io_kiocb, comp_list);
9316 kmem_cache_free(req_cachep, req);
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009317 nr++;
Pavel Begunkovc2b6c6b2021-09-24 21:59:47 +01009318 }
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009319 if (nr)
9320 percpu_ref_put_many(&ctx->refs, nr);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009321 mutex_unlock(&ctx->uring_lock);
9322}
9323
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009324static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009325{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009326 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009327 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009328}
9329
Pavel Begunkovc0724812021-10-04 20:02:54 +01009330static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009331{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009332 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009333
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009334 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009335 mmdrop(ctx->mm_account);
9336 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009337 }
Jens Axboedef596e2019-01-09 08:59:42 -07009338
Pavel Begunkovab409402021-10-09 23:14:41 +01009339 io_rsrc_refs_drop(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009340 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9341 io_wait_rsrc_data(ctx->buf_data);
9342 io_wait_rsrc_data(ctx->file_data);
9343
Hao Xu8bad28d2021-02-19 17:19:36 +08009344 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009345 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009346 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009347 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009348 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009349 if (ctx->rings)
9350 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009351 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009352 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009353 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009354 if (ctx->sq_creds)
9355 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009356
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009357 /* there are no registered resources left, nobody uses it */
9358 if (ctx->rsrc_node)
9359 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009360 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009361 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009362 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov756ab7c2021-10-06 16:06:47 +01009363 flush_delayed_work(&ctx->fallback_work);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009364
9365 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9366 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009367
9368#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07009369 if (ctx->ring_sock) {
9370 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009371 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07009372 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009373#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009374 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009375
Hristo Venev75b28af2019-08-26 17:23:46 +00009376 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009377 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009378
9379 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009380 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009381 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009382 if (ctx->hash_map)
9383 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009384 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009385 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009386 kfree(ctx);
9387}
9388
9389static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9390{
9391 struct io_ring_ctx *ctx = file->private_data;
9392 __poll_t mask = 0;
9393
Pavel Begunkovd60aa652021-10-04 20:02:52 +01009394 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009395 /*
9396 * synchronizes with barrier from wq_has_sleeper call in
9397 * io_commit_cqring
9398 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009399 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009400 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009401 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009402
9403 /*
9404 * Don't flush cqring overflow list here, just do a simple check.
9405 * Otherwise there could possible be ABBA deadlock:
9406 * CPU0 CPU1
9407 * ---- ----
9408 * lock(&ctx->uring_lock);
9409 * lock(&ep->mtx);
9410 * lock(&ctx->uring_lock);
9411 * lock(&ep->mtx);
9412 *
9413 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9414 * pushs them to do the flush.
9415 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009416 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009417 mask |= EPOLLIN | EPOLLRDNORM;
9418
9419 return mask;
9420}
9421
Yejune Deng0bead8c2020-12-24 11:02:20 +08009422static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009423{
Jens Axboe4379bf82021-02-15 13:40:22 -07009424 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009425
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009426 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009427 if (creds) {
9428 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009429 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009430 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009431
9432 return -EINVAL;
9433}
9434
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009435struct io_tctx_exit {
9436 struct callback_head task_work;
9437 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009438 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009439};
9440
Pavel Begunkovc0724812021-10-04 20:02:54 +01009441static __cold void io_tctx_exit_cb(struct callback_head *cb)
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009442{
9443 struct io_uring_task *tctx = current->io_uring;
9444 struct io_tctx_exit *work;
9445
9446 work = container_of(cb, struct io_tctx_exit, task_work);
9447 /*
9448 * When @in_idle, we're in cancellation and it's racy to remove the
9449 * node. It'll be removed by the end of cancellation, just ignore it.
9450 */
9451 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009452 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009453 complete(&work->completion);
9454}
9455
Pavel Begunkovc0724812021-10-04 20:02:54 +01009456static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
Pavel Begunkov28090c12021-04-25 23:34:45 +01009457{
9458 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9459
9460 return req->ctx == data;
9461}
9462
Pavel Begunkovc0724812021-10-04 20:02:54 +01009463static __cold void io_ring_exit_work(struct work_struct *work)
Jens Axboe85faa7b2020-04-09 18:14:00 -06009464{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009465 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009466 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009467 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009468 struct io_tctx_exit exit;
9469 struct io_tctx_node *node;
9470 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009471
Jens Axboe56952e92020-06-17 15:00:04 -06009472 /*
9473 * If we're doing polled IO and end up having requests being
9474 * submitted async (out-of-line), then completions can come in while
9475 * we're waiting for refs to drop. We need to reap these manually,
9476 * as nobody else will be looking for them.
9477 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009478 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009479 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009480 if (ctx->sq_data) {
9481 struct io_sq_data *sqd = ctx->sq_data;
9482 struct task_struct *tsk;
9483
9484 io_sq_thread_park(sqd);
9485 tsk = sqd->thread;
9486 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9487 io_wq_cancel_cb(tsk->io_uring->io_wq,
9488 io_cancel_ctx_cb, ctx, true);
9489 io_sq_thread_unpark(sqd);
9490 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009491
Pavel Begunkov37f0e762021-10-04 20:02:53 +01009492 io_req_caches_free(ctx);
9493
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009494 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9495 /* there is little hope left, don't run it too often */
9496 interval = HZ * 60;
9497 }
9498 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009499
Pavel Begunkov7f006512021-04-14 13:38:34 +01009500 init_completion(&exit.completion);
9501 init_task_work(&exit.task_work, io_tctx_exit_cb);
9502 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009503 /*
9504 * Some may use context even when all refs and requests have been put,
9505 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009506 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009507 * this lock/unlock section also waits them to finish.
9508 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009509 mutex_lock(&ctx->uring_lock);
9510 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009511 WARN_ON_ONCE(time_after(jiffies, timeout));
9512
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009513 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9514 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009515 /* don't spin on a single task if cancellation failed */
9516 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009517 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9518 if (WARN_ON_ONCE(ret))
9519 continue;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009520
9521 mutex_unlock(&ctx->uring_lock);
9522 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009523 mutex_lock(&ctx->uring_lock);
9524 }
9525 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009526 spin_lock(&ctx->completion_lock);
9527 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009528
Jens Axboe85faa7b2020-04-09 18:14:00 -06009529 io_ring_ctx_free(ctx);
9530}
9531
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009532/* Returns true if we found and killed one or more timeouts */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009533static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx,
9534 struct task_struct *tsk, bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009535{
9536 struct io_kiocb *req, *tmp;
9537 int canceled = 0;
9538
Jens Axboe79ebeae2021-08-10 15:18:27 -06009539 spin_lock(&ctx->completion_lock);
9540 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009541 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009542 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009543 io_kill_timeout(req, -ECANCELED);
9544 canceled++;
9545 }
9546 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009547 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009548 if (canceled != 0)
9549 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009550 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009551 if (canceled != 0)
9552 io_cqring_ev_posted(ctx);
9553 return canceled != 0;
9554}
9555
Pavel Begunkovc0724812021-10-04 20:02:54 +01009556static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009557{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009558 unsigned long index;
9559 struct creds *creds;
9560
Jens Axboe2b188cc2019-01-07 10:46:33 -07009561 mutex_lock(&ctx->uring_lock);
9562 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009563 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009564 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009565 xa_for_each(&ctx->personalities, index, creds)
9566 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009567 mutex_unlock(&ctx->uring_lock);
9568
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009569 io_kill_timeouts(ctx, NULL, true);
9570 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009571
Jens Axboe15dff282019-11-13 09:09:23 -07009572 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009573 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009574
Jens Axboe85faa7b2020-04-09 18:14:00 -06009575 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009576 /*
9577 * Use system_unbound_wq to avoid spawning tons of event kworkers
9578 * if we're exiting a ton of rings at the same time. It just adds
9579 * noise and overhead, there's no discernable change in runtime
9580 * over using system_wq.
9581 */
9582 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009583}
9584
9585static int io_uring_release(struct inode *inode, struct file *file)
9586{
9587 struct io_ring_ctx *ctx = file->private_data;
9588
9589 file->private_data = NULL;
9590 io_ring_ctx_wait_and_kill(ctx);
9591 return 0;
9592}
9593
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009594struct io_task_cancel {
9595 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009596 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009597};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009598
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009599static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009600{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009601 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009602 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009603 bool ret;
9604
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009605 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009606 struct io_ring_ctx *ctx = req->ctx;
9607
9608 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009609 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009610 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009611 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009612 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009613 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009614 }
9615 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009616}
9617
Pavel Begunkovc0724812021-10-04 20:02:54 +01009618static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
9619 struct task_struct *task,
9620 bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009621{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009622 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009623 LIST_HEAD(list);
9624
Jens Axboe79ebeae2021-08-10 15:18:27 -06009625 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009626 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009627 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009628 list_cut_position(&list, &ctx->defer_list, &de->list);
9629 break;
9630 }
9631 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009632 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009633 if (list_empty(&list))
9634 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009635
9636 while (!list_empty(&list)) {
9637 de = list_first_entry(&list, struct io_defer_entry, list);
9638 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009639 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009640 kfree(de);
9641 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009642 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009643}
9644
Pavel Begunkovc0724812021-10-04 20:02:54 +01009645static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
Pavel Begunkov1b007642021-03-06 11:02:17 +00009646{
9647 struct io_tctx_node *node;
9648 enum io_wq_cancel cret;
9649 bool ret = false;
9650
9651 mutex_lock(&ctx->uring_lock);
9652 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9653 struct io_uring_task *tctx = node->task->io_uring;
9654
9655 /*
9656 * io_wq will stay alive while we hold uring_lock, because it's
9657 * killed after ctx nodes, which requires to take the lock.
9658 */
9659 if (!tctx || !tctx->io_wq)
9660 continue;
9661 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9662 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9663 }
9664 mutex_unlock(&ctx->uring_lock);
9665
9666 return ret;
9667}
9668
Pavel Begunkovc0724812021-10-04 20:02:54 +01009669static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9670 struct task_struct *task,
9671 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009672{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009673 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009674 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009675
9676 while (1) {
9677 enum io_wq_cancel cret;
9678 bool ret = false;
9679
Pavel Begunkov1b007642021-03-06 11:02:17 +00009680 if (!task) {
9681 ret |= io_uring_try_cancel_iowq(ctx);
9682 } else if (tctx && tctx->io_wq) {
9683 /*
9684 * Cancels requests of all rings, not only @ctx, but
9685 * it's fine as the task is in exit/exec.
9686 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009687 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009688 &cancel, true);
9689 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9690 }
9691
9692 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009693 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009694 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov5eef4e82021-09-24 21:59:49 +01009695 while (!wq_list_empty(&ctx->iopoll_list)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009696 io_iopoll_try_reap_events(ctx);
9697 ret = true;
9698 }
9699 }
9700
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009701 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9702 ret |= io_poll_remove_all(ctx, task, cancel_all);
9703 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009704 if (task)
9705 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009706 if (!ret)
9707 break;
9708 cond_resched();
9709 }
9710}
9711
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009712static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009713{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009714 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009715 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009716 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009717
9718 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009719 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009720 if (unlikely(ret))
9721 return ret;
Pavel Begunkove139a1e2021-10-19 23:43:46 +01009722
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009723 tctx = current->io_uring;
Pavel Begunkove139a1e2021-10-19 23:43:46 +01009724 if (ctx->iowq_limits_set) {
9725 unsigned int limits[2] = { ctx->iowq_limits[0],
9726 ctx->iowq_limits[1], };
9727
9728 ret = io_wq_max_workers(tctx->io_wq, limits);
9729 if (ret)
9730 return ret;
9731 }
Jens Axboe0f212202020-09-13 13:09:39 -06009732 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009733 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9734 node = kmalloc(sizeof(*node), GFP_KERNEL);
9735 if (!node)
9736 return -ENOMEM;
9737 node->ctx = ctx;
9738 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009739
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009740 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9741 node, GFP_KERNEL));
9742 if (ret) {
9743 kfree(node);
9744 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009745 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009746
9747 mutex_lock(&ctx->uring_lock);
9748 list_add(&node->ctx_node, &ctx->tctx_list);
9749 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009750 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009751 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009752 return 0;
9753}
9754
9755/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009756 * Note that this task has used io_uring. We use it for cancelation purposes.
9757 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009758static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009759{
9760 struct io_uring_task *tctx = current->io_uring;
9761
9762 if (likely(tctx && tctx->last == ctx))
9763 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009764 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009765}
9766
9767/*
Jens Axboe0f212202020-09-13 13:09:39 -06009768 * Remove this io_uring_file -> task mapping.
9769 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009770static __cold void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009771{
9772 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009773 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009774
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009775 if (!tctx)
9776 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009777 node = xa_erase(&tctx->xa, index);
9778 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009779 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009780
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009781 WARN_ON_ONCE(current != node->task);
9782 WARN_ON_ONCE(list_empty(&node->ctx_node));
9783
9784 mutex_lock(&node->ctx->uring_lock);
9785 list_del(&node->ctx_node);
9786 mutex_unlock(&node->ctx->uring_lock);
9787
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009788 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009789 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009790 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009791}
9792
Pavel Begunkovc0724812021-10-04 20:02:54 +01009793static __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009794{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009795 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009796 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009797 unsigned long index;
9798
Jens Axboe8bab4c02021-09-24 07:12:27 -06009799 xa_for_each(&tctx->xa, index, node) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009800 io_uring_del_tctx_node(index);
Jens Axboe8bab4c02021-09-24 07:12:27 -06009801 cond_resched();
9802 }
Marco Elverb16ef422021-05-27 11:25:48 +02009803 if (wq) {
9804 /*
9805 * Must be after io_uring_del_task_file() (removes nodes under
9806 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9807 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009808 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009809 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009810 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009811}
9812
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009813static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009814{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009815 if (tracked)
9816 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009817 return percpu_counter_sum(&tctx->inflight);
9818}
9819
Pavel Begunkovc0724812021-10-04 20:02:54 +01009820static __cold void io_uring_drop_tctx_refs(struct task_struct *task)
Pavel Begunkov09899b12021-06-14 02:36:22 +01009821{
9822 struct io_uring_task *tctx = task->io_uring;
9823 unsigned int refs = tctx->cached_refs;
9824
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009825 if (refs) {
9826 tctx->cached_refs = 0;
9827 percpu_counter_sub(&tctx->inflight, refs);
9828 put_task_struct_many(task, refs);
9829 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009830}
9831
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009832/*
9833 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9834 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9835 */
Pavel Begunkovc0724812021-10-04 20:02:54 +01009836static __cold void io_uring_cancel_generic(bool cancel_all,
9837 struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009838{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009839 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009840 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009841 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009842 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009843
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009844 WARN_ON_ONCE(sqd && sqd->thread != current);
9845
Palash Oswal6d042ff2021-04-27 18:21:49 +05309846 if (!current->io_uring)
9847 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009848 if (tctx->io_wq)
9849 io_wq_exit_start(tctx->io_wq);
9850
Jens Axboefdaf0832020-10-30 09:37:30 -06009851 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009852 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009853 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009854 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009855 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009856 if (!inflight)
9857 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009858
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009859 if (!sqd) {
9860 struct io_tctx_node *node;
9861 unsigned long index;
9862
9863 xa_for_each(&tctx->xa, index, node) {
9864 /* sqpoll task will cancel all its requests */
9865 if (node->ctx->sq_data)
9866 continue;
9867 io_uring_try_cancel_requests(node->ctx, current,
9868 cancel_all);
9869 }
9870 } else {
9871 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9872 io_uring_try_cancel_requests(ctx, current,
9873 cancel_all);
9874 }
9875
9876 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009877 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009878 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009879 * If we've seen completions, retry without waiting. This
9880 * avoids a race where a completion comes in before we did
9881 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009882 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009883 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009884 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009885 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009886 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009887 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009888
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009889 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009890 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009891 /* for exec all current's requests should be gone, kill tctx */
9892 __io_uring_free(current);
9893 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009894}
9895
Hao Xuf552a272021-08-12 12:14:35 +08009896void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009897{
Hao Xuf552a272021-08-12 12:14:35 +08009898 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009899}
9900
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009901static void *io_uring_validate_mmap_request(struct file *file,
9902 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009903{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009904 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009905 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009906 struct page *page;
9907 void *ptr;
9908
9909 switch (offset) {
9910 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009911 case IORING_OFF_CQ_RING:
9912 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009913 break;
9914 case IORING_OFF_SQES:
9915 ptr = ctx->sq_sqes;
9916 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009917 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009918 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009919 }
9920
9921 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009922 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009923 return ERR_PTR(-EINVAL);
9924
9925 return ptr;
9926}
9927
9928#ifdef CONFIG_MMU
9929
Pavel Begunkovc0724812021-10-04 20:02:54 +01009930static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009931{
9932 size_t sz = vma->vm_end - vma->vm_start;
9933 unsigned long pfn;
9934 void *ptr;
9935
9936 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9937 if (IS_ERR(ptr))
9938 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009939
9940 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9941 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9942}
9943
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009944#else /* !CONFIG_MMU */
9945
9946static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9947{
9948 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9949}
9950
9951static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9952{
9953 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9954}
9955
9956static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9957 unsigned long addr, unsigned long len,
9958 unsigned long pgoff, unsigned long flags)
9959{
9960 void *ptr;
9961
9962 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9963 if (IS_ERR(ptr))
9964 return PTR_ERR(ptr);
9965
9966 return (unsigned long) ptr;
9967}
9968
9969#endif /* !CONFIG_MMU */
9970
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009971static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009972{
9973 DEFINE_WAIT(wait);
9974
9975 do {
9976 if (!io_sqring_full(ctx))
9977 break;
Jens Axboe90554202020-09-03 12:12:41 -06009978 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9979
9980 if (!io_sqring_full(ctx))
9981 break;
Jens Axboe90554202020-09-03 12:12:41 -06009982 schedule();
9983 } while (!signal_pending(current));
9984
9985 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009986 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009987}
9988
Hao Xuc73ebb62020-11-03 10:54:37 +08009989static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9990 struct __kernel_timespec __user **ts,
9991 const sigset_t __user **sig)
9992{
9993 struct io_uring_getevents_arg arg;
9994
9995 /*
9996 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9997 * is just a pointer to the sigset_t.
9998 */
9999 if (!(flags & IORING_ENTER_EXT_ARG)) {
10000 *sig = (const sigset_t __user *) argp;
10001 *ts = NULL;
10002 return 0;
10003 }
10004
10005 /*
10006 * EXT_ARG is set - ensure we agree on the size of it and copy in our
10007 * timespec and sigset_t pointers if good.
10008 */
10009 if (*argsz != sizeof(arg))
10010 return -EINVAL;
10011 if (copy_from_user(&arg, argp, sizeof(arg)))
10012 return -EFAULT;
10013 *sig = u64_to_user_ptr(arg.sigmask);
10014 *argsz = arg.sigmask_sz;
10015 *ts = u64_to_user_ptr(arg.ts);
10016 return 0;
10017}
10018
Jens Axboe2b188cc2019-01-07 10:46:33 -070010019SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +080010020 u32, min_complete, u32, flags, const void __user *, argp,
10021 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010022{
10023 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010024 int submitted = 0;
10025 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010026 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010027
Jens Axboe4c6e2772020-07-01 11:29:10 -060010028 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -070010029
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010030 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
10031 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010032 return -EINVAL;
10033
10034 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010035 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010036 return -EBADF;
10037
10038 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010039 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010040 goto out_fput;
10041
10042 ret = -ENXIO;
10043 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010044 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010045 goto out_fput;
10046
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010047 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +000010048 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010049 goto out;
10050
Jens Axboe6c271ce2019-01-10 11:22:30 -070010051 /*
10052 * For SQ polling, the thread will do all submissions and completions.
10053 * Just return the requested submit count, and wake the thread if
10054 * we were asked to.
10055 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -060010056 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -070010057 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +010010058 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +000010059
Jens Axboe21f96522021-08-14 09:04:40 -060010060 if (unlikely(ctx->sq_data->thread == NULL)) {
10061 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +010010062 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -060010063 }
Jens Axboe6c271ce2019-01-10 11:22:30 -070010064 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -060010065 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +000010066 if (flags & IORING_ENTER_SQ_WAIT) {
10067 ret = io_sqpoll_wait_sq(ctx);
10068 if (ret)
10069 goto out;
10070 }
Jens Axboe6c271ce2019-01-10 11:22:30 -070010071 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -060010072 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010073 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -060010074 if (unlikely(ret))
10075 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010076 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -060010077 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010078 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010079
10080 if (submitted != to_submit)
10081 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010082 }
10083 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +080010084 const sigset_t __user *sig;
10085 struct __kernel_timespec __user *ts;
10086
10087 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
10088 if (unlikely(ret))
10089 goto out;
10090
Jens Axboe2b188cc2019-01-07 10:46:33 -070010091 min_complete = min(min_complete, ctx->cq_entries);
10092
Xiaoguang Wang32b22442020-03-11 09:26:09 +080010093 /*
10094 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
10095 * space applications don't need to do io completion events
10096 * polling again, they can rely on io_sq_thread to do polling
10097 * work, which can reduce cpu usage and uring_lock contention.
10098 */
10099 if (ctx->flags & IORING_SETUP_IOPOLL &&
10100 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +030010101 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -070010102 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +080010103 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -070010104 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010105 }
10106
Pavel Begunkov7c504e652019-12-18 19:53:45 +030010107out:
Pavel Begunkov6805b322019-10-08 02:18:42 +030010108 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010109out_fput:
10110 fdput(f);
10111 return submitted ? submitted : ret;
10112}
10113
Tobias Klauserbebdb652020-02-26 18:38:32 +010010114#ifdef CONFIG_PROC_FS
Pavel Begunkovc0724812021-10-04 20:02:54 +010010115static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010116 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -070010117{
Jens Axboe87ce9552020-01-30 08:25:34 -070010118 struct user_namespace *uns = seq_user_ns(m);
10119 struct group_info *gi;
10120 kernel_cap_t cap;
10121 unsigned __capi;
10122 int g;
10123
10124 seq_printf(m, "%5d\n", id);
10125 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
10126 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
10127 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
10128 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
10129 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
10130 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
10131 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
10132 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
10133 seq_puts(m, "\n\tGroups:\t");
10134 gi = cred->group_info;
10135 for (g = 0; g < gi->ngroups; g++) {
10136 seq_put_decimal_ull(m, g ? " " : "",
10137 from_kgid_munged(uns, gi->gid[g]));
10138 }
10139 seq_puts(m, "\n\tCapEff:\t");
10140 cap = cred->cap_effective;
10141 CAP_FOR_EACH_U32(__capi)
10142 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
10143 seq_putc(m, '\n');
10144 return 0;
10145}
10146
Pavel Begunkovc0724812021-10-04 20:02:54 +010010147static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx,
10148 struct seq_file *m)
Jens Axboe87ce9552020-01-30 08:25:34 -070010149{
Joseph Qidbbe9c62020-09-29 09:01:22 -060010150 struct io_sq_data *sq = NULL;
Hao Xu83f84352021-09-13 21:08:54 +080010151 struct io_overflow_cqe *ocqe;
10152 struct io_rings *r = ctx->rings;
10153 unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
Hao Xu83f84352021-09-13 21:08:54 +080010154 unsigned int sq_head = READ_ONCE(r->sq.head);
10155 unsigned int sq_tail = READ_ONCE(r->sq.tail);
10156 unsigned int cq_head = READ_ONCE(r->cq.head);
10157 unsigned int cq_tail = READ_ONCE(r->cq.tail);
Jens Axboef75d1182021-10-29 06:36:45 -060010158 unsigned int sq_entries, cq_entries;
Jens Axboefad8e0d2020-09-28 08:57:48 -060010159 bool has_lock;
Hao Xu83f84352021-09-13 21:08:54 +080010160 unsigned int i;
10161
10162 /*
10163 * we may get imprecise sqe and cqe info if uring is actively running
10164 * since we get cached_sq_head and cached_cq_tail without uring_lock
10165 * and sq_tail and cq_head are changed by userspace. But it's ok since
10166 * we usually use these info when it is stuck.
10167 */
Jens Axboef75d1182021-10-29 06:36:45 -060010168 seq_printf(m, "SqMask:\t\t0x%x\n", sq_mask);
10169 seq_printf(m, "SqHead:\t%u\n", sq_head);
10170 seq_printf(m, "SqTail:\t%u\n", sq_tail);
10171 seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head);
10172 seq_printf(m, "CqMask:\t0x%x\n", cq_mask);
10173 seq_printf(m, "CqHead:\t%u\n", cq_head);
10174 seq_printf(m, "CqTail:\t%u\n", cq_tail);
10175 seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail);
10176 seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head);
10177 sq_entries = min(sq_tail - sq_head, ctx->sq_entries);
10178 for (i = 0; i < sq_entries; i++) {
10179 unsigned int entry = i + sq_head;
10180 unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]);
Jens Axboea1957782021-11-05 09:31:05 -060010181 struct io_uring_sqe *sqe;
Hao Xu83f84352021-09-13 21:08:54 +080010182
Jens Axboef75d1182021-10-29 06:36:45 -060010183 if (sq_idx > sq_mask)
10184 continue;
10185 sqe = &ctx->sq_sqes[sq_idx];
10186 seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n",
10187 sq_idx, sqe->opcode, sqe->fd, sqe->flags,
10188 sqe->user_data);
Hao Xu83f84352021-09-13 21:08:54 +080010189 }
Jens Axboef75d1182021-10-29 06:36:45 -060010190 seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head);
10191 cq_entries = min(cq_tail - cq_head, ctx->cq_entries);
10192 for (i = 0; i < cq_entries; i++) {
10193 unsigned int entry = i + cq_head;
10194 struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask];
Hao Xu83f84352021-09-13 21:08:54 +080010195
10196 seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n",
Jens Axboef75d1182021-10-29 06:36:45 -060010197 entry & cq_mask, cqe->user_data, cqe->res,
10198 cqe->flags);
Hao Xu83f84352021-09-13 21:08:54 +080010199 }
Jens Axboe87ce9552020-01-30 08:25:34 -070010200
Jens Axboefad8e0d2020-09-28 08:57:48 -060010201 /*
10202 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10203 * since fdinfo case grabs it in the opposite direction of normal use
10204 * cases. If we fail to get the lock, we just don't iterate any
10205 * structures that could be going away outside the io_uring mutex.
10206 */
10207 has_lock = mutex_trylock(&ctx->uring_lock);
10208
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010209 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -060010210 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010211 if (!sq->thread)
10212 sq = NULL;
10213 }
Joseph Qidbbe9c62020-09-29 09:01:22 -060010214
10215 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10216 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -070010217 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010218 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -070010219 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -070010220
Jens Axboe87ce9552020-01-30 08:25:34 -070010221 if (f)
10222 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10223 else
10224 seq_printf(m, "%5u: <none>\n", i);
10225 }
10226 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010227 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +010010228 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +010010229 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -070010230
Pavel Begunkov4751f532021-04-01 15:43:55 +010010231 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -070010232 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010233 if (has_lock && !xa_empty(&ctx->personalities)) {
10234 unsigned long index;
10235 const struct cred *cred;
10236
Jens Axboe87ce9552020-01-30 08:25:34 -070010237 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010238 xa_for_each(&ctx->personalities, index, cred)
10239 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010240 }
Hao Xu83f84352021-09-13 21:08:54 +080010241 if (has_lock)
10242 mutex_unlock(&ctx->uring_lock);
10243
10244 seq_puts(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010245 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010246 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10247 struct hlist_head *list = &ctx->cancel_hash[i];
10248 struct io_kiocb *req;
10249
10250 hlist_for_each_entry(req, list, hash_node)
10251 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10252 req->task->task_works != NULL);
10253 }
Hao Xu83f84352021-09-13 21:08:54 +080010254
10255 seq_puts(m, "CqOverflowList:\n");
10256 list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
10257 struct io_uring_cqe *cqe = &ocqe->cqe;
10258
10259 seq_printf(m, " user_data=%llu, res=%d, flags=%x\n",
10260 cqe->user_data, cqe->res, cqe->flags);
10261
10262 }
10263
Jens Axboe79ebeae2021-08-10 15:18:27 -060010264 spin_unlock(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010265}
10266
Pavel Begunkovc0724812021-10-04 20:02:54 +010010267static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
Jens Axboe87ce9552020-01-30 08:25:34 -070010268{
10269 struct io_ring_ctx *ctx = f->private_data;
10270
10271 if (percpu_ref_tryget(&ctx->refs)) {
10272 __io_uring_show_fdinfo(ctx, m);
10273 percpu_ref_put(&ctx->refs);
10274 }
10275}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010276#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010277
Jens Axboe2b188cc2019-01-07 10:46:33 -070010278static const struct file_operations io_uring_fops = {
10279 .release = io_uring_release,
10280 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010281#ifndef CONFIG_MMU
10282 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10283 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10284#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010285 .poll = io_uring_poll,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010286#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010287 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010288#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010289};
10290
Pavel Begunkovc0724812021-10-04 20:02:54 +010010291static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10292 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010293{
Hristo Venev75b28af2019-08-26 17:23:46 +000010294 struct io_rings *rings;
10295 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010296
Jens Axboebd740482020-08-05 12:58:23 -060010297 /* make sure these are sane, as we already accounted them */
10298 ctx->sq_entries = p->sq_entries;
10299 ctx->cq_entries = p->cq_entries;
10300
Hristo Venev75b28af2019-08-26 17:23:46 +000010301 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10302 if (size == SIZE_MAX)
10303 return -EOVERFLOW;
10304
10305 rings = io_mem_alloc(size);
10306 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010307 return -ENOMEM;
10308
Hristo Venev75b28af2019-08-26 17:23:46 +000010309 ctx->rings = rings;
10310 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10311 rings->sq_ring_mask = p->sq_entries - 1;
10312 rings->cq_ring_mask = p->cq_entries - 1;
10313 rings->sq_ring_entries = p->sq_entries;
10314 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010315
10316 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010317 if (size == SIZE_MAX) {
10318 io_mem_free(ctx->rings);
10319 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010320 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010321 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010322
10323 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010324 if (!ctx->sq_sqes) {
10325 io_mem_free(ctx->rings);
10326 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010327 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010328 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010329
Jens Axboe2b188cc2019-01-07 10:46:33 -070010330 return 0;
10331}
10332
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010333static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10334{
10335 int ret, fd;
10336
10337 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10338 if (fd < 0)
10339 return fd;
10340
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010341 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010342 if (ret) {
10343 put_unused_fd(fd);
10344 return ret;
10345 }
10346 fd_install(fd, file);
10347 return fd;
10348}
10349
Jens Axboe2b188cc2019-01-07 10:46:33 -070010350/*
10351 * Allocate an anonymous fd, this is what constitutes the application
10352 * visible backing of an io_uring instance. The application mmaps this
10353 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10354 * we have to tie this fd to a socket for file garbage collection purposes.
10355 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010356static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010357{
10358 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010359#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010360 int ret;
10361
Jens Axboe2b188cc2019-01-07 10:46:33 -070010362 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10363 &ctx->ring_sock);
10364 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010365 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010366#endif
10367
Paul Moore91a9ab72021-02-01 19:33:52 -050010368 file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx,
10369 O_RDWR | O_CLOEXEC, NULL);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010370#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010371 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010372 sock_release(ctx->ring_sock);
10373 ctx->ring_sock = NULL;
10374 } else {
10375 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010376 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010377#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010378 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010379}
10380
Pavel Begunkovc0724812021-10-04 20:02:54 +010010381static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
10382 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010383{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010384 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010385 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010386 int ret;
10387
Jens Axboe8110c1a2019-12-28 15:39:54 -070010388 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010389 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010390 if (entries > IORING_MAX_ENTRIES) {
10391 if (!(p->flags & IORING_SETUP_CLAMP))
10392 return -EINVAL;
10393 entries = IORING_MAX_ENTRIES;
10394 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010395
10396 /*
10397 * Use twice as many entries for the CQ ring. It's possible for the
10398 * application to drive a higher depth than the size of the SQ ring,
10399 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010400 * some flexibility in overcommitting a bit. If the application has
10401 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10402 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010403 */
10404 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010405 if (p->flags & IORING_SETUP_CQSIZE) {
10406 /*
10407 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10408 * to a power-of-two, if it isn't already. We do NOT impose
10409 * any cq vs sq ring sizing.
10410 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010411 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010412 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010413 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10414 if (!(p->flags & IORING_SETUP_CLAMP))
10415 return -EINVAL;
10416 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10417 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010418 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10419 if (p->cq_entries < p->sq_entries)
10420 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010421 } else {
10422 p->cq_entries = 2 * p->sq_entries;
10423 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010424
Jens Axboe2b188cc2019-01-07 10:46:33 -070010425 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010426 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010427 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010428 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010429 if (!capable(CAP_IPC_LOCK))
10430 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010431
10432 /*
10433 * This is just grabbed for accounting purposes. When a process exits,
10434 * the mm is exited and dropped before the files, hence we need to hang
10435 * on to this mm purely for the purposes of being able to unaccount
10436 * memory (locked/pinned vm). It's not used for anything else.
10437 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010438 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010439 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010440
Jens Axboe2b188cc2019-01-07 10:46:33 -070010441 ret = io_allocate_scq_urings(ctx, p);
10442 if (ret)
10443 goto err;
10444
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010445 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010446 if (ret)
10447 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010448 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010449 ret = io_rsrc_node_switch_start(ctx);
10450 if (ret)
10451 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010452 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010453
Jens Axboe2b188cc2019-01-07 10:46:33 -070010454 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010455 p->sq_off.head = offsetof(struct io_rings, sq.head);
10456 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10457 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10458 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10459 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10460 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10461 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010462
10463 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010464 p->cq_off.head = offsetof(struct io_rings, cq.head);
10465 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10466 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10467 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10468 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10469 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010470 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010471
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010472 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10473 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010474 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010475 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010476 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
Pavel Begunkov04c76b42021-11-10 15:49:32 +000010477 IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010478
10479 if (copy_to_user(params, p, sizeof(*p))) {
10480 ret = -EFAULT;
10481 goto err;
10482 }
Jens Axboed1719f72020-07-30 13:43:53 -060010483
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010484 file = io_uring_get_file(ctx);
10485 if (IS_ERR(file)) {
10486 ret = PTR_ERR(file);
10487 goto err;
10488 }
10489
Jens Axboed1719f72020-07-30 13:43:53 -060010490 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010491 * Install ring fd as the very last thing, so we don't risk someone
10492 * having closed it before we finish setup
10493 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010494 ret = io_uring_install_fd(ctx, file);
10495 if (ret < 0) {
10496 /* fput will clean it up */
10497 fput(file);
10498 return ret;
10499 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010500
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010501 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010502 return ret;
10503err:
10504 io_ring_ctx_wait_and_kill(ctx);
10505 return ret;
10506}
10507
10508/*
10509 * Sets up an aio uring context, and returns the fd. Applications asks for a
10510 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10511 * params structure passed in.
10512 */
10513static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10514{
10515 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010516 int i;
10517
10518 if (copy_from_user(&p, params, sizeof(p)))
10519 return -EFAULT;
10520 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10521 if (p.resv[i])
10522 return -EINVAL;
10523 }
10524
Jens Axboe6c271ce2019-01-10 11:22:30 -070010525 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010526 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010527 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10528 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010529 return -EINVAL;
10530
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010531 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010532}
10533
10534SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10535 struct io_uring_params __user *, params)
10536{
10537 return io_uring_setup(entries, params);
10538}
10539
Pavel Begunkovc0724812021-10-04 20:02:54 +010010540static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg,
10541 unsigned nr_args)
Jens Axboe66f4af92020-01-16 15:36:52 -070010542{
10543 struct io_uring_probe *p;
10544 size_t size;
10545 int i, ret;
10546
10547 size = struct_size(p, ops, nr_args);
10548 if (size == SIZE_MAX)
10549 return -EOVERFLOW;
10550 p = kzalloc(size, GFP_KERNEL);
10551 if (!p)
10552 return -ENOMEM;
10553
10554 ret = -EFAULT;
10555 if (copy_from_user(p, arg, size))
10556 goto out;
10557 ret = -EINVAL;
10558 if (memchr_inv(p, 0, size))
10559 goto out;
10560
10561 p->last_op = IORING_OP_LAST - 1;
10562 if (nr_args > IORING_OP_LAST)
10563 nr_args = IORING_OP_LAST;
10564
10565 for (i = 0; i < nr_args; i++) {
10566 p->ops[i].op = i;
10567 if (!io_op_defs[i].not_supported)
10568 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10569 }
10570 p->ops_len = i;
10571
10572 ret = 0;
10573 if (copy_to_user(arg, p, size))
10574 ret = -EFAULT;
10575out:
10576 kfree(p);
10577 return ret;
10578}
10579
Jens Axboe071698e2020-01-28 10:04:42 -070010580static int io_register_personality(struct io_ring_ctx *ctx)
10581{
Jens Axboe4379bf82021-02-15 13:40:22 -070010582 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010583 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010584 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010585
Jens Axboe4379bf82021-02-15 13:40:22 -070010586 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010587
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010588 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10589 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010590 if (ret < 0) {
10591 put_cred(creds);
10592 return ret;
10593 }
10594 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010595}
10596
Pavel Begunkovc0724812021-10-04 20:02:54 +010010597static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
10598 void __user *arg, unsigned int nr_args)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010599{
10600 struct io_uring_restriction *res;
10601 size_t size;
10602 int i, ret;
10603
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010604 /* Restrictions allowed only if rings started disabled */
10605 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10606 return -EBADFD;
10607
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010608 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010609 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010610 return -EBUSY;
10611
10612 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10613 return -EINVAL;
10614
10615 size = array_size(nr_args, sizeof(*res));
10616 if (size == SIZE_MAX)
10617 return -EOVERFLOW;
10618
10619 res = memdup_user(arg, size);
10620 if (IS_ERR(res))
10621 return PTR_ERR(res);
10622
10623 ret = 0;
10624
10625 for (i = 0; i < nr_args; i++) {
10626 switch (res[i].opcode) {
10627 case IORING_RESTRICTION_REGISTER_OP:
10628 if (res[i].register_op >= IORING_REGISTER_LAST) {
10629 ret = -EINVAL;
10630 goto out;
10631 }
10632
10633 __set_bit(res[i].register_op,
10634 ctx->restrictions.register_op);
10635 break;
10636 case IORING_RESTRICTION_SQE_OP:
10637 if (res[i].sqe_op >= IORING_OP_LAST) {
10638 ret = -EINVAL;
10639 goto out;
10640 }
10641
10642 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10643 break;
10644 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10645 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10646 break;
10647 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10648 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10649 break;
10650 default:
10651 ret = -EINVAL;
10652 goto out;
10653 }
10654 }
10655
10656out:
10657 /* Reset all restrictions if an error happened */
10658 if (ret != 0)
10659 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10660 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010661 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010662
10663 kfree(res);
10664 return ret;
10665}
10666
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010667static int io_register_enable_rings(struct io_ring_ctx *ctx)
10668{
10669 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10670 return -EBADFD;
10671
10672 if (ctx->restrictions.registered)
10673 ctx->restricted = 1;
10674
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010675 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10676 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10677 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010678 return 0;
10679}
10680
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010681static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010682 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010683 unsigned nr_args)
10684{
10685 __u32 tmp;
10686 int err;
10687
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010688 if (up->resv)
10689 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010690 if (check_add_overflow(up->offset, nr_args, &tmp))
10691 return -EOVERFLOW;
10692 err = io_rsrc_node_switch_start(ctx);
10693 if (err)
10694 return err;
10695
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010696 switch (type) {
10697 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010698 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010699 case IORING_RSRC_BUFFER:
10700 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010701 }
10702 return -EINVAL;
10703}
10704
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010705static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10706 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010707{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010708 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010709
10710 if (!nr_args)
10711 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010712 memset(&up, 0, sizeof(up));
10713 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10714 return -EFAULT;
10715 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10716}
10717
10718static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010719 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010720{
10721 struct io_uring_rsrc_update2 up;
10722
10723 if (size != sizeof(up))
10724 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010725 if (copy_from_user(&up, arg, sizeof(up)))
10726 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010727 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010728 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010729 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010730}
10731
Pavel Begunkovc0724812021-10-04 20:02:54 +010010732static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010733 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010734{
10735 struct io_uring_rsrc_register rr;
10736
10737 /* keep it extendible */
10738 if (size != sizeof(rr))
10739 return -EINVAL;
10740
10741 memset(&rr, 0, sizeof(rr));
10742 if (copy_from_user(&rr, arg, size))
10743 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010744 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010745 return -EINVAL;
10746
Pavel Begunkov992da012021-06-10 16:37:37 +010010747 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010748 case IORING_RSRC_FILE:
10749 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10750 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010751 case IORING_RSRC_BUFFER:
10752 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10753 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010754 }
10755 return -EINVAL;
10756}
10757
Pavel Begunkovc0724812021-10-04 20:02:54 +010010758static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
10759 void __user *arg, unsigned len)
Jens Axboefe764212021-06-17 10:19:54 -060010760{
10761 struct io_uring_task *tctx = current->io_uring;
10762 cpumask_var_t new_mask;
10763 int ret;
10764
10765 if (!tctx || !tctx->io_wq)
10766 return -EINVAL;
10767
10768 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10769 return -ENOMEM;
10770
10771 cpumask_clear(new_mask);
10772 if (len > cpumask_size())
10773 len = cpumask_size();
10774
10775 if (copy_from_user(new_mask, arg, len)) {
10776 free_cpumask_var(new_mask);
10777 return -EFAULT;
10778 }
10779
10780 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10781 free_cpumask_var(new_mask);
10782 return ret;
10783}
10784
Pavel Begunkovc0724812021-10-04 20:02:54 +010010785static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
Jens Axboefe764212021-06-17 10:19:54 -060010786{
10787 struct io_uring_task *tctx = current->io_uring;
10788
10789 if (!tctx || !tctx->io_wq)
10790 return -EINVAL;
10791
10792 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10793}
10794
Pavel Begunkovc0724812021-10-04 20:02:54 +010010795static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10796 void __user *arg)
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010797 __must_hold(&ctx->uring_lock)
Jens Axboe2e480052021-08-27 11:33:19 -060010798{
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010799 struct io_tctx_node *node;
Jens Axboefa846932021-09-01 14:15:59 -060010800 struct io_uring_task *tctx = NULL;
10801 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010802 __u32 new_count[2];
10803 int i, ret;
10804
Jens Axboe2e480052021-08-27 11:33:19 -060010805 if (copy_from_user(new_count, arg, sizeof(new_count)))
10806 return -EFAULT;
10807 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10808 if (new_count[i] > INT_MAX)
10809 return -EINVAL;
10810
Jens Axboefa846932021-09-01 14:15:59 -060010811 if (ctx->flags & IORING_SETUP_SQPOLL) {
10812 sqd = ctx->sq_data;
10813 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010814 /*
10815 * Observe the correct sqd->lock -> ctx->uring_lock
10816 * ordering. Fine to drop uring_lock here, we hold
10817 * a ref to the ctx.
10818 */
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010819 refcount_inc(&sqd->refs);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010820 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010821 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010822 mutex_lock(&ctx->uring_lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010823 if (sqd->thread)
10824 tctx = sqd->thread->io_uring;
Jens Axboefa846932021-09-01 14:15:59 -060010825 }
10826 } else {
10827 tctx = current->io_uring;
10828 }
10829
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010830 BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits));
Jens Axboefa846932021-09-01 14:15:59 -060010831
Pavel Begunkovbad119b2021-11-08 15:10:03 +000010832 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10833 if (new_count[i])
10834 ctx->iowq_limits[i] = new_count[i];
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010835 ctx->iowq_limits_set = true;
10836
Pavel Begunkove139a1e2021-10-19 23:43:46 +010010837 if (tctx && tctx->io_wq) {
10838 ret = io_wq_max_workers(tctx->io_wq, new_count);
10839 if (ret)
10840 goto err;
10841 } else {
10842 memset(new_count, 0, sizeof(new_count));
10843 }
Jens Axboefa846932021-09-01 14:15:59 -060010844
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010845 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010846 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010847 io_put_sq_data(sqd);
10848 }
Jens Axboe2e480052021-08-27 11:33:19 -060010849
10850 if (copy_to_user(arg, new_count, sizeof(new_count)))
10851 return -EFAULT;
10852
Pavel Begunkovb22fa622021-10-21 13:20:29 +010010853 /* that's it for SQPOLL, only the SQPOLL task creates requests */
10854 if (sqd)
10855 return 0;
10856
10857 /* now propagate the restriction to all registered users */
10858 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
10859 struct io_uring_task *tctx = node->task->io_uring;
10860
10861 if (WARN_ON_ONCE(!tctx->io_wq))
10862 continue;
10863
10864 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10865 new_count[i] = ctx->iowq_limits[i];
10866 /* ignore errors, it always returns zero anyway */
10867 (void)io_wq_max_workers(tctx->io_wq, new_count);
10868 }
Jens Axboe2e480052021-08-27 11:33:19 -060010869 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010870err:
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010871 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010872 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010873 io_put_sq_data(sqd);
10874 }
Jens Axboefa846932021-09-01 14:15:59 -060010875 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010876}
10877
Jens Axboe071698e2020-01-28 10:04:42 -070010878static bool io_register_op_must_quiesce(int op)
10879{
10880 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010881 case IORING_REGISTER_BUFFERS:
10882 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010883 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010884 case IORING_UNREGISTER_FILES:
10885 case IORING_REGISTER_FILES_UPDATE:
10886 case IORING_REGISTER_PROBE:
10887 case IORING_REGISTER_PERSONALITY:
10888 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010889 case IORING_REGISTER_FILES2:
10890 case IORING_REGISTER_FILES_UPDATE2:
10891 case IORING_REGISTER_BUFFERS2:
10892 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010893 case IORING_REGISTER_IOWQ_AFF:
10894 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010895 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010896 return false;
10897 default:
10898 return true;
10899 }
10900}
10901
Pavel Begunkovc0724812021-10-04 20:02:54 +010010902static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx)
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010903{
10904 long ret;
10905
10906 percpu_ref_kill(&ctx->refs);
10907
10908 /*
10909 * Drop uring mutex before waiting for references to exit. If another
10910 * thread is currently inside io_uring_enter() it might need to grab the
10911 * uring_lock to make progress. If we hold it here across the drain
10912 * wait, then we can deadlock. It's safe to drop the mutex here, since
10913 * no new references will come in after we've killed the percpu ref.
10914 */
10915 mutex_unlock(&ctx->uring_lock);
10916 do {
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010917 ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ);
10918 if (ret) {
10919 ret = min(0L, ret);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010920 break;
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010921 }
10922
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010923 ret = io_run_task_work_sig();
Pavel Begunkov37f0e762021-10-04 20:02:53 +010010924 io_req_caches_free(ctx);
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010925 } while (ret >= 0);
10926 mutex_lock(&ctx->uring_lock);
10927
10928 if (ret)
10929 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10930 return ret;
10931}
10932
Jens Axboeedafcce2019-01-09 09:16:05 -070010933static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10934 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010935 __releases(ctx->uring_lock)
10936 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010937{
10938 int ret;
10939
Jens Axboe35fa71a2019-04-22 10:23:23 -060010940 /*
10941 * We're inside the ring mutex, if the ref is already dying, then
10942 * someone else killed the ctx or is already going through
10943 * io_uring_register().
10944 */
10945 if (percpu_ref_is_dying(&ctx->refs))
10946 return -ENXIO;
10947
Pavel Begunkov75c40212021-04-15 13:07:40 +010010948 if (ctx->restricted) {
10949 if (opcode >= IORING_REGISTER_LAST)
10950 return -EINVAL;
10951 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10952 if (!test_bit(opcode, ctx->restrictions.register_op))
10953 return -EACCES;
10954 }
10955
Jens Axboe071698e2020-01-28 10:04:42 -070010956 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010957 ret = io_ctx_quiesce(ctx);
10958 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010959 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010960 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010961
10962 switch (opcode) {
10963 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010964 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010965 break;
10966 case IORING_UNREGISTER_BUFFERS:
10967 ret = -EINVAL;
10968 if (arg || nr_args)
10969 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010970 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010971 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010972 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010973 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010974 break;
10975 case IORING_UNREGISTER_FILES:
10976 ret = -EINVAL;
10977 if (arg || nr_args)
10978 break;
10979 ret = io_sqe_files_unregister(ctx);
10980 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010981 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010982 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010983 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010984 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010985 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010986 ret = -EINVAL;
10987 if (nr_args != 1)
10988 break;
10989 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010990 if (ret)
10991 break;
10992 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10993 ctx->eventfd_async = 1;
10994 else
10995 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010996 break;
10997 case IORING_UNREGISTER_EVENTFD:
10998 ret = -EINVAL;
10999 if (arg || nr_args)
11000 break;
11001 ret = io_eventfd_unregister(ctx);
11002 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070011003 case IORING_REGISTER_PROBE:
11004 ret = -EINVAL;
11005 if (!arg || nr_args > 256)
11006 break;
11007 ret = io_probe(ctx, arg, nr_args);
11008 break;
Jens Axboe071698e2020-01-28 10:04:42 -070011009 case IORING_REGISTER_PERSONALITY:
11010 ret = -EINVAL;
11011 if (arg || nr_args)
11012 break;
11013 ret = io_register_personality(ctx);
11014 break;
11015 case IORING_UNREGISTER_PERSONALITY:
11016 ret = -EINVAL;
11017 if (arg)
11018 break;
11019 ret = io_unregister_personality(ctx, nr_args);
11020 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020011021 case IORING_REGISTER_ENABLE_RINGS:
11022 ret = -EINVAL;
11023 if (arg || nr_args)
11024 break;
11025 ret = io_register_enable_rings(ctx);
11026 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020011027 case IORING_REGISTER_RESTRICTIONS:
11028 ret = io_register_restrictions(ctx, arg, nr_args);
11029 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010011030 case IORING_REGISTER_FILES2:
11031 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010011032 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010011033 case IORING_REGISTER_FILES_UPDATE2:
11034 ret = io_register_rsrc_update(ctx, arg, nr_args,
11035 IORING_RSRC_FILE);
11036 break;
11037 case IORING_REGISTER_BUFFERS2:
11038 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
11039 break;
11040 case IORING_REGISTER_BUFFERS_UPDATE:
11041 ret = io_register_rsrc_update(ctx, arg, nr_args,
11042 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010011043 break;
Jens Axboefe764212021-06-17 10:19:54 -060011044 case IORING_REGISTER_IOWQ_AFF:
11045 ret = -EINVAL;
11046 if (!arg || !nr_args)
11047 break;
11048 ret = io_register_iowq_aff(ctx, arg, nr_args);
11049 break;
11050 case IORING_UNREGISTER_IOWQ_AFF:
11051 ret = -EINVAL;
11052 if (arg || nr_args)
11053 break;
11054 ret = io_unregister_iowq_aff(ctx);
11055 break;
Jens Axboe2e480052021-08-27 11:33:19 -060011056 case IORING_REGISTER_IOWQ_MAX_WORKERS:
11057 ret = -EINVAL;
11058 if (!arg || nr_args != 2)
11059 break;
11060 ret = io_register_iowq_max_workers(ctx, arg);
11061 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070011062 default:
11063 ret = -EINVAL;
11064 break;
11065 }
11066
Jens Axboe071698e2020-01-28 10:04:42 -070011067 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070011068 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070011069 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060011070 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070011071 }
Jens Axboeedafcce2019-01-09 09:16:05 -070011072 return ret;
11073}
11074
11075SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
11076 void __user *, arg, unsigned int, nr_args)
11077{
11078 struct io_ring_ctx *ctx;
11079 long ret = -EBADF;
11080 struct fd f;
11081
11082 f = fdget(fd);
11083 if (!f.file)
11084 return -EBADF;
11085
11086 ret = -EOPNOTSUPP;
11087 if (f.file->f_op != &io_uring_fops)
11088 goto out_fput;
11089
11090 ctx = f.file->private_data;
11091
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000011092 io_run_task_work();
11093
Jens Axboeedafcce2019-01-09 09:16:05 -070011094 mutex_lock(&ctx->uring_lock);
11095 ret = __io_uring_register(ctx, opcode, arg, nr_args);
11096 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020011097 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
11098 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070011099out_fput:
11100 fdput(f);
11101 return ret;
11102}
11103
Jens Axboe2b188cc2019-01-07 10:46:33 -070011104static int __init io_uring_init(void)
11105{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011106#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
11107 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
11108 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
11109} while (0)
11110
11111#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
11112 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
11113 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
11114 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
11115 BUILD_BUG_SQE_ELEM(1, __u8, flags);
11116 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
11117 BUILD_BUG_SQE_ELEM(4, __s32, fd);
11118 BUILD_BUG_SQE_ELEM(8, __u64, off);
11119 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
11120 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011121 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011122 BUILD_BUG_SQE_ELEM(24, __u32, len);
11123 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
11124 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
11125 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
11126 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080011127 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
11128 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011129 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
11130 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
11131 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
11132 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
11133 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
11134 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
11135 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
11136 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011137 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011138 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
11139 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011140 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011141 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030011142 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010011143 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010011144
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011145 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
11146 sizeof(struct io_uring_rsrc_update));
11147 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
11148 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010011149
11150 /* ->buf_index is u16 */
11151 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
11152
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011153 /* should fit into one byte */
11154 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
Pavel Begunkov68fe2562021-09-15 12:03:38 +010011155 BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
11156 BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010011157
Jens Axboed3656342019-12-18 09:50:26 -070011158 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080011159 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010011160
Jens Axboe91f245d2021-02-09 13:48:50 -070011161 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
11162 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070011163 return 0;
11164};
11165__initcall(io_uring_init);