blob: d48be0ccc5906adf9ece546f6563b224bdab5a2d [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
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * 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>
Jens Axboeff002b32020-02-07 16:05:21 -070077#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030078#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070079#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060080#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060081#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070082#include <linux/blk-cgroup.h>
Jens Axboe4ea33a92020-10-15 13:46:44 -060083#include <linux/audit.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)
Jens Axboe65e19f52019-10-26 07:20:21 -060095
96/*
97 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
98 */
99#define IORING_FILE_TABLE_SHIFT 9
100#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
101#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
102#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200103#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
104 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700105
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000106#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
107 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
108 IOSQE_BUFFER_SELECT)
109
Jens Axboe2b188cc2019-01-07 10:46:33 -0700110struct io_uring {
111 u32 head ____cacheline_aligned_in_smp;
112 u32 tail ____cacheline_aligned_in_smp;
113};
114
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * This data is shared with the application through the mmap at offsets
117 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 *
119 * The offsets to the member fields are published through struct
120 * io_sqring_offsets when calling io_uring_setup.
121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
124 * Head and tail offsets into the ring; the offsets need to be
125 * masked to get valid indices.
126 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 * The kernel controls head of the sq ring and the tail of the cq ring,
128 * and the application controls tail of the sq ring and the head of the
129 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000131 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200132 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 * ring_entries - 1)
135 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000136 u32 sq_ring_mask, cq_ring_mask;
137 /* Ring sizes (constant, power of 2) */
138 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200139 /*
140 * Number of invalid entries dropped by the kernel due to
141 * invalid index stored in array
142 *
143 * Written by the kernel, shouldn't be modified by the
144 * application (i.e. get number of "new events" by comparing to
145 * cached value).
146 *
147 * After a new SQ head value was read by the application this
148 * counter includes all submissions that were dropped reaching
149 * the new SQ head (and possibly more).
150 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000151 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200152 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200153 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 *
155 * Written by the kernel, shouldn't be modified by the
156 * application.
157 *
158 * The application needs a full memory barrier before checking
159 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
160 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000161 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200163 * Runtime CQ flags
164 *
165 * Written by the application, shouldn't be modified by the
166 * kernel.
167 */
168 u32 cq_flags;
169 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200170 * Number of completion events lost because the queue was full;
171 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800172 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200173 * the completion queue.
174 *
175 * Written by the kernel, shouldn't be modified by the
176 * application (i.e. get number of "new events" by comparing to
177 * cached value).
178 *
179 * As completion events come in out of order this counter is not
180 * ordered with any other data.
181 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000182 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200183 /*
184 * Ring buffer of completion events.
185 *
186 * The kernel writes completion events fresh every time they are
187 * produced, so the application is allowed to modify pending
188 * entries.
189 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000190 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700191};
192
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000193enum io_uring_cmd_flags {
194 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000195 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000196};
197
Jens Axboeedafcce2019-01-09 09:16:05 -0700198struct io_mapped_ubuf {
199 u64 ubuf;
200 size_t len;
201 struct bio_vec *bvec;
202 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600203 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700204};
205
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000206struct io_ring_ctx;
207
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000208struct io_rsrc_put {
209 struct list_head list;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000210 union {
211 void *rsrc;
212 struct file *file;
213 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000214};
215
216struct fixed_rsrc_table {
Jens Axboe65e19f52019-10-26 07:20:21 -0600217 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700218};
219
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000220struct fixed_rsrc_ref_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800221 struct percpu_ref refs;
222 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000223 struct list_head rsrc_list;
224 struct fixed_rsrc_data *rsrc_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000225 void (*rsrc_put)(struct io_ring_ctx *ctx,
226 struct io_rsrc_put *prsrc);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600227 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000228 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800229};
230
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000231struct fixed_rsrc_data {
232 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700233 struct io_ring_ctx *ctx;
234
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000235 struct fixed_rsrc_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700236 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700237 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800238 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700239};
240
Jens Axboe5a2e7452020-02-23 16:23:11 -0700241struct io_buffer {
242 struct list_head list;
243 __u64 addr;
244 __s32 len;
245 __u16 bid;
246};
247
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200248struct io_restriction {
249 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
250 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
251 u8 sqe_flags_allowed;
252 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200253 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200254};
255
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700256enum {
257 IO_SQ_THREAD_SHOULD_STOP = 0,
258 IO_SQ_THREAD_SHOULD_PARK,
259};
260
Jens Axboe534ca6d2020-09-02 13:52:19 -0600261struct io_sq_data {
262 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600263 struct mutex lock;
264
265 /* ctx's that are using this sqd */
266 struct list_head ctx_list;
267 struct list_head ctx_new_list;
268 struct mutex ctx_lock;
269
Jens Axboe534ca6d2020-09-02 13:52:19 -0600270 struct task_struct *thread;
271 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800272
273 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700274 int sq_cpu;
275 pid_t task_pid;
276
277 unsigned long state;
278 struct completion startup;
279 struct completion completion;
280 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600281};
282
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000283#define IO_IOPOLL_BATCH 8
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000284#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000285#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000286#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000287
288struct io_comp_state {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000289 struct io_kiocb *reqs[IO_COMPL_BATCH];
Jens Axboe1b4c3512021-02-10 00:03:19 +0000290 unsigned int nr;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700291 unsigned int locked_free_nr;
292 /* inline/task_work completion list, under ->uring_lock */
Jens Axboe1b4c3512021-02-10 00:03:19 +0000293 struct list_head free_list;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700294 /* IRQ completion list, under ->completion_lock */
295 struct list_head locked_free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000296};
297
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000298struct io_submit_link {
299 struct io_kiocb *head;
300 struct io_kiocb *last;
301};
302
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000303struct io_submit_state {
304 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000305 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000306
307 /*
308 * io_kiocb alloc cache
309 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000310 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000311 unsigned int free_reqs;
312
313 bool plug_started;
314
315 /*
316 * Batch completion logic
317 */
318 struct io_comp_state comp;
319
320 /*
321 * File reference cache
322 */
323 struct file *file;
324 unsigned int fd;
325 unsigned int file_refs;
326 unsigned int ios_left;
327};
328
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329struct io_ring_ctx {
330 struct {
331 struct percpu_ref refs;
332 } ____cacheline_aligned_in_smp;
333
334 struct {
335 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 cq_overflow_flushed: 1;
338 unsigned int drain_next: 1;
339 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200340 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000341 unsigned int sqo_dead: 1;
Jens Axboe5f3f26f2021-02-25 10:17:46 -0700342 unsigned int sqo_exec: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700343
Hristo Venev75b28af2019-08-26 17:23:46 +0000344 /*
345 * Ring buffer of indices into array of io_uring_sqe, which is
346 * mmapped by the application using the IORING_OFF_SQES offset.
347 *
348 * This indirection could e.g. be used to assign fixed
349 * io_uring_sqe entries to operations and only submit them to
350 * the queue when needed.
351 *
352 * The kernel modifies neither the indices array nor the entries
353 * array.
354 */
355 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700356 unsigned cached_sq_head;
357 unsigned sq_entries;
358 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700359 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600360 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100361 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700362 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600363
Jens Axboee9418942021-02-19 12:33:30 -0700364 /* hashed buffered write serialization */
365 struct io_wq_hash *hash_map;
366
Jens Axboede0617e2019-04-06 21:51:27 -0600367 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600368 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700369 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700370
Jens Axboead3eb2c2019-12-18 17:12:20 -0700371 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700372 } ____cacheline_aligned_in_smp;
373
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700374 struct {
375 struct mutex uring_lock;
376 wait_queue_head_t wait;
377 } ____cacheline_aligned_in_smp;
378
379 struct io_submit_state submit_state;
380
Hristo Venev75b28af2019-08-26 17:23:46 +0000381 struct io_rings *rings;
382
Jens Axboe2aede0e2020-09-14 10:45:53 -0600383 /*
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700384 * For SQPOLL usage
Jens Axboe2aede0e2020-09-14 10:45:53 -0600385 */
386 struct task_struct *sqo_task;
387
388 /* Only used for accounting purposes */
389 struct mm_struct *mm_account;
390
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;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700395
Jens Axboe6b063142019-01-10 22:13:58 -0700396 /*
397 * If used, fixed file set. Writers must ensure that ->refs is dead,
398 * readers must ensure that ->refs is alive as long as the file* is
399 * used. Only updated through io_uring_register(2).
400 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000401 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700402 unsigned nr_user_files;
403
Jens Axboeedafcce2019-01-09 09:16:05 -0700404 /* if used, fixed mapped user buffers */
405 unsigned nr_user_bufs;
406 struct io_mapped_ubuf *user_bufs;
407
Jens Axboe2b188cc2019-01-07 10:46:33 -0700408 struct user_struct *user;
409
Jens Axboe0f158b42020-05-14 17:18:39 -0600410 struct completion ref_comp;
411 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700412
413#if defined(CONFIG_UNIX)
414 struct socket *ring_sock;
415#endif
416
Jens Axboe5a2e7452020-02-23 16:23:11 -0700417 struct idr io_buffer_idr;
418
Jens Axboe071698e2020-01-28 10:04:42 -0700419 struct idr personality_idr;
420
Jens Axboe206aefd2019-11-07 18:27:42 -0700421 struct {
422 unsigned cached_cq_tail;
423 unsigned cq_entries;
424 unsigned cq_mask;
425 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500426 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700427 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700428 struct wait_queue_head cq_wait;
429 struct fasync_struct *cq_fasync;
430 struct eventfd_ctx *cq_ev_fd;
431 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432
433 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700434 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700435
Jens Axboedef596e2019-01-09 08:59:42 -0700436 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300437 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700438 * io_uring instances that don't use IORING_SETUP_SQPOLL.
439 * For SQPOLL, only the single threaded io_sq_thread() will
440 * manipulate the list, hence no extra locking is needed there.
441 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300442 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700443 struct hlist_head *cancel_hash;
444 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700445 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600446
447 spinlock_t inflight_lock;
448 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700449 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600450
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000451 struct delayed_work rsrc_put_work;
452 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000453 struct list_head rsrc_ref_list;
454 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600455
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200456 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700457
Jens Axboe7c25c0d2021-02-16 07:17:00 -0700458 /* exit task_work */
459 struct callback_head *exit_task_work;
460
Jens Axboee9418942021-02-19 12:33:30 -0700461 struct wait_queue_head hash_wait;
462
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700463 /* Keep this last, we don't need it for the fast path */
464 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700465};
466
Jens Axboe09bb8392019-03-13 12:39:28 -0600467/*
468 * First field must be the file pointer in all the
469 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
470 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700471struct io_poll_iocb {
472 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000473 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700474 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600475 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700476 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700477 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700478};
479
Pavel Begunkov018043b2020-10-27 23:17:18 +0000480struct io_poll_remove {
481 struct file *file;
482 u64 addr;
483};
484
Jens Axboeb5dba592019-12-11 14:02:38 -0700485struct io_close {
486 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700487 int fd;
488};
489
Jens Axboead8a48a2019-11-15 08:49:11 -0700490struct io_timeout_data {
491 struct io_kiocb *req;
492 struct hrtimer timer;
493 struct timespec64 ts;
494 enum hrtimer_mode mode;
495};
496
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700497struct io_accept {
498 struct file *file;
499 struct sockaddr __user *addr;
500 int __user *addr_len;
501 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600502 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700503};
504
505struct io_sync {
506 struct file *file;
507 loff_t len;
508 loff_t off;
509 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700510 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700511};
512
Jens Axboefbf23842019-12-17 18:45:56 -0700513struct io_cancel {
514 struct file *file;
515 u64 addr;
516};
517
Jens Axboeb29472e2019-12-17 18:50:29 -0700518struct io_timeout {
519 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300520 u32 off;
521 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300522 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000523 /* head of the link, used by linked timeouts only */
524 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700525};
526
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100527struct io_timeout_rem {
528 struct file *file;
529 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000530
531 /* timeout update */
532 struct timespec64 ts;
533 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100534};
535
Jens Axboe9adbd452019-12-20 08:45:55 -0700536struct io_rw {
537 /* NOTE: kiocb has the file as the first member, so don't do it here */
538 struct kiocb kiocb;
539 u64 addr;
540 u64 len;
541};
542
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700543struct io_connect {
544 struct file *file;
545 struct sockaddr __user *addr;
546 int addr_len;
547};
548
Jens Axboee47293f2019-12-20 08:58:21 -0700549struct io_sr_msg {
550 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700551 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300552 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700553 void __user *buf;
554 };
Jens Axboee47293f2019-12-20 08:58:21 -0700555 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700556 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700557 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700558 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700559};
560
Jens Axboe15b71ab2019-12-11 11:20:36 -0700561struct io_open {
562 struct file *file;
563 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700564 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700565 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600566 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700567};
568
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000569struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700570 struct file *file;
571 u64 arg;
572 u32 nr_args;
573 u32 offset;
574};
575
Jens Axboe4840e412019-12-25 22:03:45 -0700576struct io_fadvise {
577 struct file *file;
578 u64 offset;
579 u32 len;
580 u32 advice;
581};
582
Jens Axboec1ca7572019-12-25 22:18:28 -0700583struct io_madvise {
584 struct file *file;
585 u64 addr;
586 u32 len;
587 u32 advice;
588};
589
Jens Axboe3e4827b2020-01-08 15:18:09 -0700590struct io_epoll {
591 struct file *file;
592 int epfd;
593 int op;
594 int fd;
595 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700596};
597
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300598struct io_splice {
599 struct file *file_out;
600 struct file *file_in;
601 loff_t off_out;
602 loff_t off_in;
603 u64 len;
604 unsigned int flags;
605};
606
Jens Axboeddf0322d2020-02-23 16:41:33 -0700607struct io_provide_buf {
608 struct file *file;
609 __u64 addr;
610 __s32 len;
611 __u32 bgid;
612 __u16 nbufs;
613 __u16 bid;
614};
615
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700616struct io_statx {
617 struct file *file;
618 int dfd;
619 unsigned int mask;
620 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700621 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700622 struct statx __user *buffer;
623};
624
Jens Axboe36f4fa62020-09-05 11:14:22 -0600625struct io_shutdown {
626 struct file *file;
627 int how;
628};
629
Jens Axboe80a261f2020-09-28 14:23:58 -0600630struct io_rename {
631 struct file *file;
632 int old_dfd;
633 int new_dfd;
634 struct filename *oldpath;
635 struct filename *newpath;
636 int flags;
637};
638
Jens Axboe14a11432020-09-28 14:27:37 -0600639struct io_unlink {
640 struct file *file;
641 int dfd;
642 int flags;
643 struct filename *filename;
644};
645
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300646struct io_completion {
647 struct file *file;
648 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300649 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300650};
651
Jens Axboef499a022019-12-02 16:28:46 -0700652struct io_async_connect {
653 struct sockaddr_storage address;
654};
655
Jens Axboe03b12302019-12-02 18:50:25 -0700656struct io_async_msghdr {
657 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000658 /* points to an allocated iov, if NULL we use fast_iov instead */
659 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700660 struct sockaddr __user *uaddr;
661 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700662 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700663};
664
Jens Axboef67676d2019-12-02 11:03:47 -0700665struct io_async_rw {
666 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600667 const struct iovec *free_iovec;
668 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600669 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600670 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700671};
672
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300673enum {
674 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
675 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
676 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
677 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
678 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700679 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300680
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300681 REQ_F_FAIL_LINK_BIT,
682 REQ_F_INFLIGHT_BIT,
683 REQ_F_CUR_POS_BIT,
684 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300685 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300686 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300687 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700688 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700689 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600690 REQ_F_NO_FILE_TABLE_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100691 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000692 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700693
694 /* not a real bit, just to check we're not overflowing the space */
695 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300696};
697
698enum {
699 /* ctx owns file */
700 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
701 /* drain existing IO first */
702 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
703 /* linked sqes */
704 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
705 /* doesn't sever on completion < 0 */
706 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
707 /* IOSQE_ASYNC */
708 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700709 /* IOSQE_BUFFER_SELECT */
710 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300711
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300712 /* fail rest of links */
713 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
714 /* on inflight list */
715 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
716 /* read/write uses file position */
717 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
718 /* must not punt to workers */
719 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100720 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300721 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300722 /* regular file */
723 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300724 /* needs cleanup */
725 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700726 /* already went through poll handler */
727 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700728 /* buffer already selected */
729 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600730 /* doesn't need file table for this request */
731 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100732 /* linked timeout is active, i.e. prepared by link's head */
733 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000734 /* completion is deferred through io_comp_state */
735 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700736};
737
738struct async_poll {
739 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600740 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300741};
742
Jens Axboe7cbf1722021-02-10 00:03:20 +0000743struct io_task_work {
744 struct io_wq_work_node node;
745 task_work_func_t func;
746};
747
Jens Axboe09bb8392019-03-13 12:39:28 -0600748/*
749 * NOTE! Each of the iocb union members has the file pointer
750 * as the first entry in their struct definition. So you can
751 * access the file pointer through any of the sub-structs,
752 * or directly as just 'ki_filp' in this struct.
753 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700754struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700755 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600756 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700757 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700758 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000759 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700760 struct io_accept accept;
761 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700762 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700763 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100764 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700765 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700766 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700767 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700768 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000769 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700770 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700771 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700772 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300773 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700774 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700775 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600776 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600777 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600778 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300779 /* use only after cleaning per-op data, see io_clean_op() */
780 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700781 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700782
Jens Axboee8c2bc12020-08-15 18:44:09 -0700783 /* opcode allocated if it needs to store data for async defer */
784 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700785 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800786 /* polled IO has completed */
787 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700788
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700789 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300790 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700791
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300792 struct io_ring_ctx *ctx;
793 unsigned int flags;
794 refcount_t refs;
795 struct task_struct *task;
796 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700797
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000798 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000799 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700800
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300801 /*
802 * 1. used with ctx->iopoll_list with reads/writes
803 * 2. to track reqs with ->files (see io_op_def::file_table)
804 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300805 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000806 union {
807 struct io_task_work io_task_work;
808 struct callback_head task_work;
809 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300810 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
811 struct hlist_node hash_node;
812 struct async_poll *apoll;
813 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700814};
815
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300816struct io_defer_entry {
817 struct list_head list;
818 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300819 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300820};
821
Jens Axboed3656342019-12-18 09:50:26 -0700822struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700823 /* needs req->file assigned */
824 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700825 /* hash wq insertion if file is a regular file */
826 unsigned hash_reg_file : 1;
827 /* unbound wq insertion if file is a non-regular file */
828 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700829 /* opcode is not supported by this kernel */
830 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700831 /* set if opcode supports polled "wait" */
832 unsigned pollin : 1;
833 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700834 /* op supports buffer selection */
835 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700836 /* must always have async data allocated */
837 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600838 /* should block plug */
839 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700840 /* size of async data needed, if any */
841 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700842};
843
Jens Axboe09186822020-10-13 15:01:40 -0600844static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_NOP] = {},
846 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700847 .needs_file = 1,
848 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700849 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700850 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700851 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600852 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700853 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700854 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300855 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700856 .needs_file = 1,
857 .hash_reg_file = 1,
858 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700859 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700860 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600861 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700862 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700863 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300864 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700865 .needs_file = 1,
866 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300867 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700868 .needs_file = 1,
869 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700870 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600871 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700872 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700873 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300874 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700875 .needs_file = 1,
876 .hash_reg_file = 1,
877 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700878 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600879 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700880 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700881 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300882 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700883 .needs_file = 1,
884 .unbound_nonreg_file = 1,
885 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300886 [IORING_OP_POLL_REMOVE] = {},
887 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700888 .needs_file = 1,
889 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300890 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700891 .needs_file = 1,
892 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700893 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700894 .needs_async_data = 1,
895 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700896 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300897 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700898 .needs_file = 1,
899 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700900 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700901 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700902 .needs_async_data = 1,
903 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700904 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300905 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700906 .needs_async_data = 1,
907 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700908 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000909 [IORING_OP_TIMEOUT_REMOVE] = {
910 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000911 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300912 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700913 .needs_file = 1,
914 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700915 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700916 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300917 [IORING_OP_ASYNC_CANCEL] = {},
918 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700919 .needs_async_data = 1,
920 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700921 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300922 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700923 .needs_file = 1,
924 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700925 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700926 .needs_async_data = 1,
927 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700928 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300929 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700930 .needs_file = 1,
931 },
Jens Axboe44526be2021-02-15 13:32:18 -0700932 [IORING_OP_OPENAT] = {},
933 [IORING_OP_CLOSE] = {},
934 [IORING_OP_FILES_UPDATE] = {},
935 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300936 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700937 .needs_file = 1,
938 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700939 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700940 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600941 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700942 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700943 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300944 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700945 .needs_file = 1,
946 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700947 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600948 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700949 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700950 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300951 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700952 .needs_file = 1,
953 },
Jens Axboe44526be2021-02-15 13:32:18 -0700954 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300955 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700956 .needs_file = 1,
957 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700958 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700959 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300960 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700961 .needs_file = 1,
962 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700963 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700964 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700965 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300966 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700967 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700968 [IORING_OP_EPOLL_CTL] = {
969 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700970 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300971 [IORING_OP_SPLICE] = {
972 .needs_file = 1,
973 .hash_reg_file = 1,
974 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700975 },
976 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700977 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300978 [IORING_OP_TEE] = {
979 .needs_file = 1,
980 .hash_reg_file = 1,
981 .unbound_nonreg_file = 1,
982 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600983 [IORING_OP_SHUTDOWN] = {
984 .needs_file = 1,
985 },
Jens Axboe44526be2021-02-15 13:32:18 -0700986 [IORING_OP_RENAMEAT] = {},
987 [IORING_OP_UNLINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700988};
989
Pavel Begunkov9936c7c2021-02-04 13:51:56 +0000990static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
991 struct task_struct *task,
992 struct files_struct *files);
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700993static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000994static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +0000995static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000996 struct io_ring_ctx *ctx);
Pavel Begunkovf2303b12021-02-20 18:03:49 +0000997static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000998
Pavel Begunkov23faba32021-02-11 18:28:22 +0000999static bool io_rw_reissue(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001000static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001001static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001002static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001003static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001004static void io_dismantle_req(struct io_kiocb *req);
1005static void io_put_task(struct task_struct *task, int nr);
1006static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001007static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001008static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001009static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001010static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001011 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001012 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001013static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001014static struct file *io_file_get(struct io_submit_state *state,
1015 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001016static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001017static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001018
Pavel Begunkov847595d2021-02-04 13:52:06 +00001019static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1020 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001021static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1022 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001023 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001024static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001025static void io_submit_flush_completions(struct io_comp_state *cs,
1026 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001027
Jens Axboe2b188cc2019-01-07 10:46:33 -07001028static struct kmem_cache *req_cachep;
1029
Jens Axboe09186822020-10-13 15:01:40 -06001030static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031
1032struct sock *io_uring_get_socket(struct file *file)
1033{
1034#if defined(CONFIG_UNIX)
1035 if (file->f_op == &io_uring_fops) {
1036 struct io_ring_ctx *ctx = file->private_data;
1037
1038 return ctx->ring_sock->sk;
1039 }
1040#endif
1041 return NULL;
1042}
1043EXPORT_SYMBOL(io_uring_get_socket);
1044
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001045#define io_for_each_link(pos, head) \
1046 for (pos = (head); pos; pos = pos->link)
1047
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001048static inline void io_clean_op(struct io_kiocb *req)
1049{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001050 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001051 __io_clean_op(req);
1052}
1053
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001054static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001055{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001056 struct io_ring_ctx *ctx = req->ctx;
1057
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001058 if (!req->fixed_rsrc_refs) {
1059 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1060 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001061 }
1062}
1063
Pavel Begunkov08d23632020-11-06 13:00:22 +00001064static bool io_match_task(struct io_kiocb *head,
1065 struct task_struct *task,
1066 struct files_struct *files)
1067{
1068 struct io_kiocb *req;
1069
Jens Axboe84965ff2021-01-23 15:51:11 -07001070 if (task && head->task != task) {
1071 /* in terms of cancelation, always match if req task is dead */
1072 if (head->task->flags & PF_EXITING)
1073 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001074 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001075 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001076 if (!files)
1077 return true;
1078
1079 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001080 if (req->file && req->file->f_op == &io_uring_fops)
1081 return true;
Jens Axboe4379bf82021-02-15 13:40:22 -07001082 if (req->task->files == files)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001083 return true;
1084 }
1085 return false;
1086}
1087
Jens Axboec40f6372020-06-25 15:39:59 -06001088static inline void req_set_fail_links(struct io_kiocb *req)
1089{
1090 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1091 req->flags |= REQ_F_FAIL_LINK;
1092}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001093
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1095{
1096 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1097
Jens Axboe0f158b42020-05-14 17:18:39 -06001098 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001099}
1100
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001101static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1102{
1103 return !req->timeout.off;
1104}
1105
Jens Axboe2b188cc2019-01-07 10:46:33 -07001106static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1107{
1108 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001109 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001110
1111 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1112 if (!ctx)
1113 return NULL;
1114
Jens Axboe78076bb2019-12-04 19:56:40 -07001115 /*
1116 * Use 5 bits less than the max cq entries, that should give us around
1117 * 32 entries per hash list if totally full and uniformly spread.
1118 */
1119 hash_bits = ilog2(p->cq_entries);
1120 hash_bits -= 5;
1121 if (hash_bits <= 0)
1122 hash_bits = 1;
1123 ctx->cancel_hash_bits = hash_bits;
1124 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1125 GFP_KERNEL);
1126 if (!ctx->cancel_hash)
1127 goto err;
1128 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1129
Roman Gushchin21482892019-05-07 10:01:48 -07001130 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001131 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1132 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001133
1134 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001135 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001136 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001137 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001138 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001139 init_completion(&ctx->ref_comp);
1140 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001141 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001142 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001143 mutex_init(&ctx->uring_lock);
1144 init_waitqueue_head(&ctx->wait);
1145 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001146 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001147 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001148 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001149 spin_lock_init(&ctx->inflight_lock);
1150 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001151 spin_lock_init(&ctx->rsrc_ref_lock);
1152 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001153 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1154 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001155 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001156 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001157 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001158err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001159 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001160 kfree(ctx);
1161 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001162}
1163
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001164static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001165{
Jens Axboe2bc99302020-07-09 09:43:27 -06001166 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1167 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001168
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001169 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001170 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001171 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001172
Bob Liu9d858b22019-11-13 18:06:25 +08001173 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001174}
1175
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001176static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001177{
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001178 if (req->flags & REQ_F_INFLIGHT) {
1179 struct io_ring_ctx *ctx = req->ctx;
1180 struct io_uring_task *tctx = req->task->io_uring;
1181 unsigned long flags;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001182
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001183 spin_lock_irqsave(&ctx->inflight_lock, flags);
1184 list_del(&req->inflight_entry);
1185 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1186 req->flags &= ~REQ_F_INFLIGHT;
1187 if (atomic_read(&tctx->in_idle))
1188 wake_up(&tctx->wait);
1189 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001190}
1191
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001192static void io_req_track_inflight(struct io_kiocb *req)
1193{
1194 struct io_ring_ctx *ctx = req->ctx;
1195
1196 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001197 req->flags |= REQ_F_INFLIGHT;
1198
1199 spin_lock_irq(&ctx->inflight_lock);
1200 list_add(&req->inflight_entry, &ctx->inflight_list);
1201 spin_unlock_irq(&ctx->inflight_lock);
1202 }
1203}
1204
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001205static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001206{
Jens Axboed3656342019-12-18 09:50:26 -07001207 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001208 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001209
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001210 if (req->flags & REQ_F_FORCE_ASYNC)
1211 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1212
Jens Axboed3656342019-12-18 09:50:26 -07001213 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001214 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001215 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001216 } else {
1217 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001218 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001219 }
Jens Axboe561fb042019-10-24 07:25:42 -06001220}
1221
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001222static void io_prep_async_link(struct io_kiocb *req)
1223{
1224 struct io_kiocb *cur;
1225
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001226 io_for_each_link(cur, req)
1227 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001228}
1229
Jens Axboe7271ef32020-08-10 09:55:22 -06001230static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001231{
Jackie Liua197f662019-11-08 08:09:12 -07001232 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001233 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001234 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001235
Jens Axboe3bfe6102021-02-16 14:15:30 -07001236 BUG_ON(!tctx);
1237 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001238
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001239 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1240 &req->work, req->flags);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001241 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001242 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001243}
1244
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001245static void io_queue_async_work(struct io_kiocb *req)
1246{
Jens Axboe7271ef32020-08-10 09:55:22 -06001247 struct io_kiocb *link;
1248
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001249 /* init ->work of the whole link before punting */
1250 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001251 link = __io_queue_async_work(req);
1252
1253 if (link)
1254 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001255}
1256
Jens Axboe5262f562019-09-17 12:26:57 -06001257static void io_kill_timeout(struct io_kiocb *req)
1258{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001259 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001260 int ret;
1261
Jens Axboee8c2bc12020-08-15 18:44:09 -07001262 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001263 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001264 atomic_set(&req->ctx->cq_timeouts,
1265 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001266 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001267 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001268 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001269 }
1270}
1271
Jens Axboe76e1b642020-09-26 15:05:03 -06001272/*
1273 * Returns true if we found and killed one or more timeouts
1274 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001275static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1276 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001277{
1278 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001279 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001280
1281 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001282 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001283 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001284 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001285 canceled++;
1286 }
Jens Axboef3606e32020-09-22 08:18:24 -06001287 }
Jens Axboe5262f562019-09-17 12:26:57 -06001288 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001289 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001290}
1291
Pavel Begunkov04518942020-05-26 20:34:05 +03001292static void __io_queue_deferred(struct io_ring_ctx *ctx)
1293{
1294 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001295 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1296 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001297
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001298 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001299 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001300 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001301 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001302 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001303 } while (!list_empty(&ctx->defer_list));
1304}
1305
Pavel Begunkov360428f2020-05-30 14:54:17 +03001306static void io_flush_timeouts(struct io_ring_ctx *ctx)
1307{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001308 u32 seq;
1309
1310 if (list_empty(&ctx->timeout_list))
1311 return;
1312
1313 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1314
1315 do {
1316 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001317 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001318 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001319
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001320 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001321 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001322
1323 /*
1324 * Since seq can easily wrap around over time, subtract
1325 * the last seq at which timeouts were flushed before comparing.
1326 * Assuming not more than 2^31-1 events have happened since,
1327 * these subtractions won't have wrapped, so we can check if
1328 * target is in [last_seq, current_seq] by comparing the two.
1329 */
1330 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1331 events_got = seq - ctx->cq_last_tm_flush;
1332 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001333 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001334
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001335 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001336 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001337 } while (!list_empty(&ctx->timeout_list));
1338
1339 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001340}
1341
Jens Axboede0617e2019-04-06 21:51:27 -06001342static void io_commit_cqring(struct io_ring_ctx *ctx)
1343{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001344 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001345
1346 /* order cqe stores with ring update */
1347 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001348
Pavel Begunkov04518942020-05-26 20:34:05 +03001349 if (unlikely(!list_empty(&ctx->defer_list)))
1350 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001351}
1352
Jens Axboe90554202020-09-03 12:12:41 -06001353static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1354{
1355 struct io_rings *r = ctx->rings;
1356
1357 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1358}
1359
Pavel Begunkov888aae22021-01-19 13:32:39 +00001360static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1361{
1362 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1363}
1364
Jens Axboe2b188cc2019-01-07 10:46:33 -07001365static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1366{
Hristo Venev75b28af2019-08-26 17:23:46 +00001367 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001368 unsigned tail;
1369
Stefan Bühler115e12e2019-04-24 23:54:18 +02001370 /*
1371 * writes to the cq entry need to come after reading head; the
1372 * control dependency is enough as we're using WRITE_ONCE to
1373 * fill the cq entry
1374 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001375 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001376 return NULL;
1377
Pavel Begunkov888aae22021-01-19 13:32:39 +00001378 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001379 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001380}
1381
Jens Axboef2842ab2020-01-08 11:04:00 -07001382static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1383{
Jens Axboef0b493e2020-02-01 21:30:11 -07001384 if (!ctx->cq_ev_fd)
1385 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001386 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1387 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001388 if (!ctx->eventfd_async)
1389 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001390 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001391}
1392
Jens Axboeb41e9852020-02-17 09:52:41 -07001393static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001394{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001395 /* see waitqueue_active() comment */
1396 smp_mb();
1397
Jens Axboe8c838782019-03-12 15:48:16 -06001398 if (waitqueue_active(&ctx->wait))
1399 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001400 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1401 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001402 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001403 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001404 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001405 wake_up_interruptible(&ctx->cq_wait);
1406 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1407 }
Jens Axboe8c838782019-03-12 15:48:16 -06001408}
1409
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001410static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1411{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001412 /* see waitqueue_active() comment */
1413 smp_mb();
1414
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001415 if (ctx->flags & IORING_SETUP_SQPOLL) {
1416 if (waitqueue_active(&ctx->wait))
1417 wake_up(&ctx->wait);
1418 }
1419 if (io_should_trigger_evfd(ctx))
1420 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001421 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001422 wake_up_interruptible(&ctx->cq_wait);
1423 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1424 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001425}
1426
Jens Axboec4a2ed72019-11-21 21:01:26 -07001427/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001428static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1429 struct task_struct *tsk,
1430 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001431{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001432 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001433 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001434 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001435 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001436 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001437 LIST_HEAD(list);
1438
Pavel Begunkove23de152020-12-17 00:24:37 +00001439 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1440 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001441
Jens Axboeb18032b2021-01-24 16:58:56 -07001442 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001443 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001444 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001445 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001446 continue;
1447
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001448 cqe = io_get_cqring(ctx);
1449 if (!cqe && !force)
1450 break;
1451
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001452 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001453 if (cqe) {
1454 WRITE_ONCE(cqe->user_data, req->user_data);
1455 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001456 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001457 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001458 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001459 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001460 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001461 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001462 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001463 }
1464
Pavel Begunkov09e88402020-12-17 00:24:38 +00001465 all_flushed = list_empty(&ctx->cq_overflow_list);
1466 if (all_flushed) {
1467 clear_bit(0, &ctx->sq_check_overflow);
1468 clear_bit(0, &ctx->cq_check_overflow);
1469 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1470 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001471
Jens Axboeb18032b2021-01-24 16:58:56 -07001472 if (posted)
1473 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001474 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001475 if (posted)
1476 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001477
1478 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001479 req = list_first_entry(&list, struct io_kiocb, compl.list);
1480 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001481 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001482 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001483
Pavel Begunkov09e88402020-12-17 00:24:38 +00001484 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001485}
1486
Pavel Begunkov6c503152021-01-04 20:36:36 +00001487static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1488 struct task_struct *tsk,
1489 struct files_struct *files)
1490{
1491 if (test_bit(0, &ctx->cq_check_overflow)) {
1492 /* iopoll syncs against uring_lock, not completion_lock */
1493 if (ctx->flags & IORING_SETUP_IOPOLL)
1494 mutex_lock(&ctx->uring_lock);
1495 __io_cqring_overflow_flush(ctx, force, tsk, files);
1496 if (ctx->flags & IORING_SETUP_IOPOLL)
1497 mutex_unlock(&ctx->uring_lock);
1498 }
1499}
1500
Jens Axboebcda7ba2020-02-23 16:42:51 -07001501static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001502{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001503 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001504 struct io_uring_cqe *cqe;
1505
Jens Axboe78e19bb2019-11-06 15:21:34 -07001506 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001507
Jens Axboe2b188cc2019-01-07 10:46:33 -07001508 /*
1509 * If we can't get a cq entry, userspace overflowed the
1510 * submission (by quite a lot). Increment the overflow count in
1511 * the ring.
1512 */
1513 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001514 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001515 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001516 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001517 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001518 } else if (ctx->cq_overflow_flushed ||
1519 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001520 /*
1521 * If we're in ring overflow flush mode, or in task cancel mode,
1522 * then we cannot store the request for later flushing, we need
1523 * to drop it on the floor.
1524 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001525 ctx->cached_cq_overflow++;
1526 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001527 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001528 if (list_empty(&ctx->cq_overflow_list)) {
1529 set_bit(0, &ctx->sq_check_overflow);
1530 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001531 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001532 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001533 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001534 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001535 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001536 refcount_inc(&req->refs);
1537 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001538 }
1539}
1540
Jens Axboebcda7ba2020-02-23 16:42:51 -07001541static void io_cqring_fill_event(struct io_kiocb *req, long res)
1542{
1543 __io_cqring_fill_event(req, res, 0);
1544}
1545
Jens Axboec7dae4b2021-02-09 19:53:37 -07001546static inline void io_req_complete_post(struct io_kiocb *req, long res,
1547 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001548{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001549 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001550 unsigned long flags;
1551
1552 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001553 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001554 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001555 /*
1556 * If we're the last reference to this request, add to our locked
1557 * free_list cache.
1558 */
1559 if (refcount_dec_and_test(&req->refs)) {
1560 struct io_comp_state *cs = &ctx->submit_state.comp;
1561
1562 io_dismantle_req(req);
1563 io_put_task(req->task, 1);
1564 list_add(&req->compl.list, &cs->locked_free_list);
1565 cs->locked_free_nr++;
1566 } else
1567 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001568 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1569
Jens Axboe8c838782019-03-12 15:48:16 -06001570 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001571 if (req) {
1572 io_queue_next(req);
1573 percpu_ref_put(&ctx->refs);
1574 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001575}
1576
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001577static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001578 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001579{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001580 io_clean_op(req);
1581 req->result = res;
1582 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001583 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001584}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001585
Pavel Begunkov889fca72021-02-10 00:03:09 +00001586static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1587 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001588{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001589 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1590 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001591 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001592 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001593}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001594
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001595static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001596{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001597 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001598}
1599
Jens Axboec7dae4b2021-02-09 19:53:37 -07001600static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001601{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001602 struct io_submit_state *state = &ctx->submit_state;
1603 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001604 struct io_kiocb *req = NULL;
1605
Jens Axboec7dae4b2021-02-09 19:53:37 -07001606 /*
1607 * If we have more than a batch's worth of requests in our IRQ side
1608 * locked cache, grab the lock and move them over to our submission
1609 * side cache.
1610 */
1611 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
1612 spin_lock_irq(&ctx->completion_lock);
1613 list_splice_init(&cs->locked_free_list, &cs->free_list);
1614 cs->locked_free_nr = 0;
1615 spin_unlock_irq(&ctx->completion_lock);
1616 }
1617
1618 while (!list_empty(&cs->free_list)) {
1619 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001620 compl.list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001621 list_del(&req->compl.list);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001622 state->reqs[state->free_reqs++] = req;
1623 if (state->free_reqs == ARRAY_SIZE(state->reqs))
1624 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001625 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001626
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001627 return req != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001628}
1629
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001630static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001631{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001632 struct io_submit_state *state = &ctx->submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001633
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001634 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
Jens Axboe2b188cc2019-01-07 10:46:33 -07001635
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001636 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001637 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001638 int ret;
1639
Jens Axboec7dae4b2021-02-09 19:53:37 -07001640 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001641 goto got_req;
1642
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001643 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1644 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06001645
1646 /*
1647 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1648 * retry single alloc to be on the safe side.
1649 */
1650 if (unlikely(ret <= 0)) {
1651 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1652 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00001653 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06001654 ret = 1;
1655 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001656 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001657 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001658got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001659 state->free_reqs--;
1660 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001661}
1662
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001663static inline void io_put_file(struct io_kiocb *req, struct file *file,
1664 bool fixed)
1665{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001666 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001667 fput(file);
1668}
1669
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001670static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001671{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001672 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001673
Jens Axboee8c2bc12020-08-15 18:44:09 -07001674 if (req->async_data)
1675 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001676 if (req->file)
1677 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001678 if (req->fixed_rsrc_refs)
1679 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001680 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001681}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001682
Pavel Begunkov7c660732021-01-25 11:42:21 +00001683static inline void io_put_task(struct task_struct *task, int nr)
1684{
1685 struct io_uring_task *tctx = task->io_uring;
1686
1687 percpu_counter_sub(&tctx->inflight, nr);
1688 if (unlikely(atomic_read(&tctx->in_idle)))
1689 wake_up(&tctx->wait);
1690 put_task_struct_many(task, nr);
1691}
1692
Pavel Begunkov216578e2020-10-13 09:44:00 +01001693static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001694{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001695 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001696
Pavel Begunkov216578e2020-10-13 09:44:00 +01001697 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001698 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001699
Pavel Begunkov3893f392021-02-10 00:03:15 +00001700 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001701 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001702}
1703
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001704static inline void io_remove_next_linked(struct io_kiocb *req)
1705{
1706 struct io_kiocb *nxt = req->link;
1707
1708 req->link = nxt->link;
1709 nxt->link = NULL;
1710}
1711
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001712static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001713{
Jackie Liua197f662019-11-08 08:09:12 -07001714 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001715 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001716 bool cancelled = false;
1717 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001718
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001719 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001720 link = req->link;
1721
Pavel Begunkov900fad42020-10-19 16:39:16 +01001722 /*
1723 * Can happen if a linked timeout fired and link had been like
1724 * req -> link t-out -> link t-out [-> ...]
1725 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001726 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1727 struct io_timeout_data *io = link->async_data;
1728 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001729
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001730 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001731 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001732 ret = hrtimer_try_to_cancel(&io->timer);
1733 if (ret != -1) {
1734 io_cqring_fill_event(link, -ECANCELED);
1735 io_commit_cqring(ctx);
1736 cancelled = true;
1737 }
1738 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001739 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001740 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001741
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001742 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001743 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001744 io_put_req(link);
1745 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001746}
1747
Jens Axboe4d7dd462019-11-20 13:03:52 -07001748
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001749static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001750{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001751 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07001752 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001753 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06001754
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001755 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001756 link = req->link;
1757 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06001758
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001759 while (link) {
1760 nxt = link->link;
1761 link->link = NULL;
1762
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001763 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001764 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001765
Jens Axboe1575f212021-02-27 15:20:49 -07001766 io_put_req_deferred(link, 2);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001767 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001768 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001769 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001770 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001771
Jens Axboe2665abf2019-11-05 12:40:47 -07001772 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001773}
1774
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001775static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001776{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001777 if (req->flags & REQ_F_LINK_TIMEOUT)
1778 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001779
Jens Axboe9e645e112019-05-10 16:07:28 -06001780 /*
1781 * If LINK is set, we have dependent requests in this chain. If we
1782 * didn't fail this request, queue the first one up, moving any other
1783 * dependencies to the next request. In case of failure, fail the rest
1784 * of the chain.
1785 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001786 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
1787 struct io_kiocb *nxt = req->link;
1788
1789 req->link = NULL;
1790 return nxt;
1791 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001792 io_fail_links(req);
1793 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001794}
Jens Axboe2665abf2019-11-05 12:40:47 -07001795
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001796static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001797{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00001798 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001799 return NULL;
1800 return __io_req_find_next(req);
1801}
1802
Jens Axboe7cbf1722021-02-10 00:03:20 +00001803static bool __tctx_task_work(struct io_uring_task *tctx)
1804{
Jens Axboe65453d12021-02-10 00:03:21 +00001805 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001806 struct io_wq_work_list list;
1807 struct io_wq_work_node *node;
1808
1809 if (wq_list_empty(&tctx->task_list))
1810 return false;
1811
Jens Axboe0b81e802021-02-16 10:33:53 -07001812 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001813 list = tctx->task_list;
1814 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001815 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001816
1817 node = list.first;
1818 while (node) {
1819 struct io_wq_work_node *next = node->next;
Jens Axboe65453d12021-02-10 00:03:21 +00001820 struct io_ring_ctx *this_ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001821 struct io_kiocb *req;
1822
1823 req = container_of(node, struct io_kiocb, io_task_work.node);
Jens Axboe65453d12021-02-10 00:03:21 +00001824 this_ctx = req->ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001825 req->task_work.func(&req->task_work);
1826 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00001827
1828 if (!ctx) {
1829 ctx = this_ctx;
1830 } else if (ctx != this_ctx) {
1831 mutex_lock(&ctx->uring_lock);
1832 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
1833 mutex_unlock(&ctx->uring_lock);
1834 ctx = this_ctx;
1835 }
1836 }
1837
1838 if (ctx && ctx->submit_state.comp.nr) {
1839 mutex_lock(&ctx->uring_lock);
1840 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
1841 mutex_unlock(&ctx->uring_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001842 }
1843
1844 return list.first != NULL;
1845}
1846
1847static void tctx_task_work(struct callback_head *cb)
1848{
1849 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
1850
Jens Axboe1d5f3602021-02-26 14:54:16 -07001851 clear_bit(0, &tctx->task_state);
1852
Jens Axboe7cbf1722021-02-10 00:03:20 +00001853 while (__tctx_task_work(tctx))
1854 cond_resched();
Jens Axboe7cbf1722021-02-10 00:03:20 +00001855}
1856
1857static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
1858 enum task_work_notify_mode notify)
1859{
1860 struct io_uring_task *tctx = tsk->io_uring;
1861 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07001862 unsigned long flags;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001863 int ret;
1864
1865 WARN_ON_ONCE(!tctx);
1866
Jens Axboe0b81e802021-02-16 10:33:53 -07001867 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001868 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001869 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001870
1871 /* task_work already pending, we're done */
1872 if (test_bit(0, &tctx->task_state) ||
1873 test_and_set_bit(0, &tctx->task_state))
1874 return 0;
1875
1876 if (!task_work_add(tsk, &tctx->task_work, notify))
1877 return 0;
1878
1879 /*
1880 * Slow path - we failed, find and delete work. if the work is not
1881 * in the list, it got run and we're fine.
1882 */
1883 ret = 0;
Jens Axboe0b81e802021-02-16 10:33:53 -07001884 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001885 wq_list_for_each(node, prev, &tctx->task_list) {
1886 if (&req->io_task_work.node == node) {
1887 wq_list_del(&tctx->task_list, node, prev);
1888 ret = 1;
1889 break;
1890 }
1891 }
Jens Axboe0b81e802021-02-16 10:33:53 -07001892 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001893 clear_bit(0, &tctx->task_state);
1894 return ret;
1895}
1896
Jens Axboe355fb9e2020-10-22 20:19:35 -06001897static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06001898{
1899 struct task_struct *tsk = req->task;
1900 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06001901 enum task_work_notify_mode notify;
1902 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06001903
Jens Axboe6200b0a2020-09-13 14:38:30 -06001904 if (tsk->flags & PF_EXITING)
1905 return -ESRCH;
1906
Jens Axboec2c4c832020-07-01 15:37:11 -06001907 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001908 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1909 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1910 * processing task_work. There's no reliable way to tell if TWA_RESUME
1911 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001912 */
Jens Axboe91989c72020-10-16 09:02:26 -06001913 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06001914 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06001915 notify = TWA_SIGNAL;
1916
Jens Axboe7cbf1722021-02-10 00:03:20 +00001917 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06001918 if (!ret)
1919 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001920
Jens Axboec2c4c832020-07-01 15:37:11 -06001921 return ret;
1922}
1923
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001924static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00001925 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001926{
Jens Axboe7c25c0d2021-02-16 07:17:00 -07001927 struct io_ring_ctx *ctx = req->ctx;
1928 struct callback_head *head;
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001929
1930 init_task_work(&req->task_work, cb);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07001931 do {
1932 head = READ_ONCE(ctx->exit_task_work);
1933 req->task_work.next = head;
1934 } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001935}
1936
Jens Axboec40f6372020-06-25 15:39:59 -06001937static void __io_req_task_cancel(struct io_kiocb *req, int error)
1938{
1939 struct io_ring_ctx *ctx = req->ctx;
1940
1941 spin_lock_irq(&ctx->completion_lock);
1942 io_cqring_fill_event(req, error);
1943 io_commit_cqring(ctx);
1944 spin_unlock_irq(&ctx->completion_lock);
1945
1946 io_cqring_ev_posted(ctx);
1947 req_set_fail_links(req);
1948 io_double_put_req(req);
1949}
1950
1951static void io_req_task_cancel(struct callback_head *cb)
1952{
1953 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001954 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001955
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00001956 mutex_lock(&ctx->uring_lock);
Pavel Begunkova3df76982021-02-18 22:32:52 +00001957 __io_req_task_cancel(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00001958 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001959 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001960}
1961
1962static void __io_req_task_submit(struct io_kiocb *req)
1963{
1964 struct io_ring_ctx *ctx = req->ctx;
1965
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00001966 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00001967 mutex_lock(&ctx->uring_lock);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001968 if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && !current->in_execve)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001969 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00001970 else
Jens Axboec40f6372020-06-25 15:39:59 -06001971 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00001972 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06001973}
1974
Jens Axboec40f6372020-06-25 15:39:59 -06001975static void io_req_task_submit(struct callback_head *cb)
1976{
1977 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1978
1979 __io_req_task_submit(req);
1980}
1981
1982static void io_req_task_queue(struct io_kiocb *req)
1983{
Jens Axboec40f6372020-06-25 15:39:59 -06001984 int ret;
1985
Jens Axboe7cbf1722021-02-10 00:03:20 +00001986 req->task_work.func = io_req_task_submit;
Jens Axboe355fb9e2020-10-22 20:19:35 -06001987 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06001988 if (unlikely(ret)) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00001989 req->result = -ECANCELED;
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00001990 percpu_ref_get(&req->ctx->refs);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001991 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06001992 }
Jens Axboec40f6372020-06-25 15:39:59 -06001993}
1994
Pavel Begunkova3df76982021-02-18 22:32:52 +00001995static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
1996{
1997 percpu_ref_get(&req->ctx->refs);
1998 req->result = ret;
1999 req->task_work.func = io_req_task_cancel;
2000
2001 if (unlikely(io_req_task_work_add(req)))
2002 io_req_task_work_add_fallback(req, io_req_task_cancel);
2003}
2004
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002005static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002006{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002007 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002008
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002009 if (nxt)
2010 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002011}
2012
Jens Axboe9e645e112019-05-10 16:07:28 -06002013static void io_free_req(struct io_kiocb *req)
2014{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002015 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002016 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002017}
2018
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002019struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002020 struct task_struct *task;
2021 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002022 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002023};
2024
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002025static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002026{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002027 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002028 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002029 rb->task = NULL;
2030}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002031
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002032static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2033 struct req_batch *rb)
2034{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002035 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002036 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002037 if (rb->ctx_refs)
2038 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002039}
2040
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002041static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2042 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002043{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002044 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002045
Jens Axboee3bc8e92020-09-24 08:45:57 -06002046 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002047 if (rb->task)
2048 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002049 rb->task = req->task;
2050 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002051 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002052 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002053 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002054
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002055 io_dismantle_req(req);
Pavel Begunkovbd759042021-02-12 03:23:50 +00002056 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002057 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002058 else
2059 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002060}
2061
Pavel Begunkov905c1722021-02-10 00:03:14 +00002062static void io_submit_flush_completions(struct io_comp_state *cs,
2063 struct io_ring_ctx *ctx)
2064{
2065 int i, nr = cs->nr;
2066 struct io_kiocb *req;
2067 struct req_batch rb;
2068
2069 io_init_req_batch(&rb);
2070 spin_lock_irq(&ctx->completion_lock);
2071 for (i = 0; i < nr; i++) {
2072 req = cs->reqs[i];
2073 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2074 }
2075 io_commit_cqring(ctx);
2076 spin_unlock_irq(&ctx->completion_lock);
2077
2078 io_cqring_ev_posted(ctx);
2079 for (i = 0; i < nr; i++) {
2080 req = cs->reqs[i];
2081
2082 /* submission and completion refs */
2083 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002084 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002085 }
2086
2087 io_req_free_batch_finish(ctx, &rb);
2088 cs->nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002089}
2090
Jens Axboeba816ad2019-09-28 11:36:45 -06002091/*
2092 * Drop reference to request, return next in chain (if there is one) if this
2093 * was the last reference to this request.
2094 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002095static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002096{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002097 struct io_kiocb *nxt = NULL;
2098
Jens Axboe2a44f462020-02-25 13:25:41 -07002099 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002100 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002101 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002102 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002103 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002104}
2105
Jens Axboe2b188cc2019-01-07 10:46:33 -07002106static void io_put_req(struct io_kiocb *req)
2107{
Jens Axboedef596e2019-01-09 08:59:42 -07002108 if (refcount_dec_and_test(&req->refs))
2109 io_free_req(req);
2110}
2111
Pavel Begunkov216578e2020-10-13 09:44:00 +01002112static void io_put_req_deferred_cb(struct callback_head *cb)
2113{
2114 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2115
2116 io_free_req(req);
2117}
2118
2119static void io_free_req_deferred(struct io_kiocb *req)
2120{
2121 int ret;
2122
Jens Axboe7cbf1722021-02-10 00:03:20 +00002123 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002124 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002125 if (unlikely(ret))
2126 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002127}
2128
2129static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2130{
2131 if (refcount_sub_and_test(refs, &req->refs))
2132 io_free_req_deferred(req);
2133}
2134
Jens Axboe978db572019-11-14 22:39:04 -07002135static void io_double_put_req(struct io_kiocb *req)
2136{
2137 /* drop both submit and complete references */
2138 if (refcount_sub_and_test(2, &req->refs))
2139 io_free_req(req);
2140}
2141
Pavel Begunkov6c503152021-01-04 20:36:36 +00002142static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002143{
2144 /* See comment at the top of this file */
2145 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002146 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002147}
2148
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002149static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2150{
2151 struct io_rings *rings = ctx->rings;
2152
2153 /* make sure SQ entry isn't read before tail */
2154 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2155}
2156
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002157static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002158{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002159 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002160
Jens Axboebcda7ba2020-02-23 16:42:51 -07002161 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2162 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002163 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002164 kfree(kbuf);
2165 return cflags;
2166}
2167
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002168static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2169{
2170 struct io_buffer *kbuf;
2171
2172 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2173 return io_put_kbuf(req, kbuf);
2174}
2175
Jens Axboe4c6e2772020-07-01 11:29:10 -06002176static inline bool io_run_task_work(void)
2177{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002178 /*
2179 * Not safe to run on exiting task, and the task_work handling will
2180 * not add work to such a task.
2181 */
2182 if (unlikely(current->flags & PF_EXITING))
2183 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002184 if (current->task_works) {
2185 __set_current_state(TASK_RUNNING);
2186 task_work_run();
2187 return true;
2188 }
2189
2190 return false;
2191}
2192
Jens Axboedef596e2019-01-09 08:59:42 -07002193/*
2194 * Find and free completed poll iocbs
2195 */
2196static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2197 struct list_head *done)
2198{
Jens Axboe8237e042019-12-28 10:48:22 -07002199 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002200 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002201
2202 /* order with ->result store in io_complete_rw_iopoll() */
2203 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002204
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002205 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002206 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002207 int cflags = 0;
2208
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002209 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002210 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002211
Pavel Begunkovf1613402021-02-11 18:28:21 +00002212 if (READ_ONCE(req->result) == -EAGAIN) {
2213 req->iopoll_completed = 0;
Pavel Begunkov23faba32021-02-11 18:28:22 +00002214 if (io_rw_reissue(req))
Pavel Begunkovf1613402021-02-11 18:28:21 +00002215 continue;
2216 }
2217
Jens Axboebcda7ba2020-02-23 16:42:51 -07002218 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002219 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002220
2221 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002222 (*nr_events)++;
2223
Pavel Begunkovc3524382020-06-28 12:52:32 +03002224 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002225 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002226 }
Jens Axboedef596e2019-01-09 08:59:42 -07002227
Jens Axboe09bb8392019-03-13 12:39:28 -06002228 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002229 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002230 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002231}
2232
Jens Axboedef596e2019-01-09 08:59:42 -07002233static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2234 long min)
2235{
2236 struct io_kiocb *req, *tmp;
2237 LIST_HEAD(done);
2238 bool spin;
2239 int ret;
2240
2241 /*
2242 * Only spin for completions if we don't have multiple devices hanging
2243 * off our complete list, and we're under the requested amount.
2244 */
2245 spin = !ctx->poll_multi_file && *nr_events < min;
2246
2247 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002248 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002249 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002250
2251 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002252 * Move completed and retryable entries to our local lists.
2253 * If we find a request that requires polling, break out
2254 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002255 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002256 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002257 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002258 continue;
2259 }
2260 if (!list_empty(&done))
2261 break;
2262
2263 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2264 if (ret < 0)
2265 break;
2266
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002267 /* iopoll may have completed current req */
2268 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002269 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002270
Jens Axboedef596e2019-01-09 08:59:42 -07002271 if (ret && spin)
2272 spin = false;
2273 ret = 0;
2274 }
2275
2276 if (!list_empty(&done))
2277 io_iopoll_complete(ctx, nr_events, &done);
2278
2279 return ret;
2280}
2281
2282/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002283 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002284 * non-spinning poll check - we'll still enter the driver poll loop, but only
2285 * as a non-spinning completion check.
2286 */
2287static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2288 long min)
2289{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002290 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002291 int ret;
2292
2293 ret = io_do_iopoll(ctx, nr_events, min);
2294 if (ret < 0)
2295 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002296 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002297 return 0;
2298 }
2299
2300 return 1;
2301}
2302
2303/*
2304 * We can't just wait for polled events to come to us, we have to actively
2305 * find and complete them.
2306 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002307static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002308{
2309 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2310 return;
2311
2312 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002313 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002314 unsigned int nr_events = 0;
2315
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002316 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002317
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002318 /* let it sleep and repeat later if can't complete a request */
2319 if (nr_events == 0)
2320 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002321 /*
2322 * Ensure we allow local-to-the-cpu processing to take place,
2323 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002324 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002325 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002326 if (need_resched()) {
2327 mutex_unlock(&ctx->uring_lock);
2328 cond_resched();
2329 mutex_lock(&ctx->uring_lock);
2330 }
Jens Axboedef596e2019-01-09 08:59:42 -07002331 }
2332 mutex_unlock(&ctx->uring_lock);
2333}
2334
Pavel Begunkov7668b922020-07-07 16:36:21 +03002335static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002336{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002337 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002338 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002339
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002340 /*
2341 * We disallow the app entering submit/complete with polling, but we
2342 * still need to lock the ring to prevent racing with polled issue
2343 * that got punted to a workqueue.
2344 */
2345 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002346 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002347 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002348 * Don't enter poll loop if we already have events pending.
2349 * If we do, we can potentially be spinning for commands that
2350 * already triggered a CQE (eg in error).
2351 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002352 if (test_bit(0, &ctx->cq_check_overflow))
2353 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2354 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002355 break;
2356
2357 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002358 * If a submit got punted to a workqueue, we can have the
2359 * application entering polling for a command before it gets
2360 * issued. That app will hold the uring_lock for the duration
2361 * of the poll right here, so we need to take a breather every
2362 * now and then to ensure that the issue has a chance to add
2363 * the poll to the issued list. Otherwise we can spin here
2364 * forever, while the workqueue is stuck trying to acquire the
2365 * very same mutex.
2366 */
2367 if (!(++iters & 7)) {
2368 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002369 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002370 mutex_lock(&ctx->uring_lock);
2371 }
2372
Pavel Begunkov7668b922020-07-07 16:36:21 +03002373 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002374 if (ret <= 0)
2375 break;
2376 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002377 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002378
Jens Axboe500f9fb2019-08-19 12:15:59 -06002379 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002380 return ret;
2381}
2382
Jens Axboe491381ce2019-10-17 09:20:46 -06002383static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002384{
Jens Axboe491381ce2019-10-17 09:20:46 -06002385 /*
2386 * Tell lockdep we inherited freeze protection from submission
2387 * thread.
2388 */
2389 if (req->flags & REQ_F_ISREG) {
2390 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002391
Jens Axboe491381ce2019-10-17 09:20:46 -06002392 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002393 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002394 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002395}
2396
Jens Axboeb63534c2020-06-04 11:28:00 -06002397#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002398static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002399{
2400 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Colin Ian King4a245472021-02-10 20:00:07 +00002401 int rw, ret;
Jens Axboeb63534c2020-06-04 11:28:00 -06002402 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002403
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002404 /* already prepared */
2405 if (req->async_data)
2406 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002407
2408 switch (req->opcode) {
2409 case IORING_OP_READV:
2410 case IORING_OP_READ_FIXED:
2411 case IORING_OP_READ:
2412 rw = READ;
2413 break;
2414 case IORING_OP_WRITEV:
2415 case IORING_OP_WRITE_FIXED:
2416 case IORING_OP_WRITE:
2417 rw = WRITE;
2418 break;
2419 default:
2420 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2421 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002422 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002423 }
2424
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002425 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2426 if (ret < 0)
2427 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002428 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002429}
Jens Axboeb63534c2020-06-04 11:28:00 -06002430#endif
2431
Pavel Begunkov23faba32021-02-11 18:28:22 +00002432static bool io_rw_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002433{
2434#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002435 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002436
Jens Axboe355afae2020-09-02 09:30:31 -06002437 if (!S_ISBLK(mode) && !S_ISREG(mode))
2438 return false;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002439 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002440 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002441 /*
2442 * If ref is dying, we might be running poll reap from the exit work.
2443 * Don't attempt to reissue from that path, just let it fail with
2444 * -EAGAIN.
2445 */
2446 if (percpu_ref_is_dying(&req->ctx->refs))
2447 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002448
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002449 lockdep_assert_held(&req->ctx->uring_lock);
2450
Jens Axboe37d1e2e2021-02-17 21:03:43 -07002451 if (io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002452 refcount_inc(&req->refs);
2453 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002454 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002455 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002456 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002457#endif
2458 return false;
2459}
2460
Jens Axboea1d7c392020-06-22 11:09:46 -06002461static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002462 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002463{
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002464 int cflags = 0;
2465
Pavel Begunkov23faba32021-02-11 18:28:22 +00002466 if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req))
2467 return;
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002468 if (res != req->result)
2469 req_set_fail_links(req);
Pavel Begunkov23faba32021-02-11 18:28:22 +00002470
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002471 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2472 kiocb_end_write(req);
2473 if (req->flags & REQ_F_BUFFER_SELECTED)
2474 cflags = io_put_rw_kbuf(req);
2475 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002476}
2477
2478static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2479{
Jens Axboe9adbd452019-12-20 08:45:55 -07002480 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002481
Pavel Begunkov889fca72021-02-10 00:03:09 +00002482 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002483}
2484
Jens Axboedef596e2019-01-09 08:59:42 -07002485static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2486{
Jens Axboe9adbd452019-12-20 08:45:55 -07002487 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002488
Jens Axboe491381ce2019-10-17 09:20:46 -06002489 if (kiocb->ki_flags & IOCB_WRITE)
2490 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002491
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002492 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002493 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002494
2495 WRITE_ONCE(req->result, res);
2496 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002497 smp_wmb();
2498 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002499}
2500
2501/*
2502 * After the iocb has been issued, it's safe to be found on the poll list.
2503 * Adding the kiocb to the list AFTER submission ensures that we don't
2504 * find it from a io_iopoll_getevents() thread before the issuer is done
2505 * accessing the kiocb cookie.
2506 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002507static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002508{
2509 struct io_ring_ctx *ctx = req->ctx;
2510
2511 /*
2512 * Track whether we have multiple files in our lists. This will impact
2513 * how we do polling eventually, not spinning if we're on potentially
2514 * different devices.
2515 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002516 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002517 ctx->poll_multi_file = false;
2518 } else if (!ctx->poll_multi_file) {
2519 struct io_kiocb *list_req;
2520
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002521 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002522 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002523 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002524 ctx->poll_multi_file = true;
2525 }
2526
2527 /*
2528 * For fast devices, IO may have already completed. If it has, add
2529 * it to the front so we find it first.
2530 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002531 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002532 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002533 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002534 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002535
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002536 /*
2537 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2538 * task context or in io worker task context. If current task context is
2539 * sq thread, we don't need to check whether should wake up sq thread.
2540 */
2541 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002542 wq_has_sleeper(&ctx->sq_data->wait))
2543 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002544}
2545
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002546static inline void io_state_file_put(struct io_submit_state *state)
2547{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002548 if (state->file_refs) {
2549 fput_many(state->file, state->file_refs);
2550 state->file_refs = 0;
2551 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002552}
2553
2554/*
2555 * Get as many references to a file as we have IOs left in this submission,
2556 * assuming most submissions are for one file, or at least that each file
2557 * has more than one submission.
2558 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002559static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002560{
2561 if (!state)
2562 return fget(fd);
2563
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002564 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002565 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002566 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002567 return state->file;
2568 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002569 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002570 }
2571 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002572 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002573 return NULL;
2574
2575 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002576 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002577 return state->file;
2578}
2579
Jens Axboe4503b762020-06-01 10:00:27 -06002580static bool io_bdev_nowait(struct block_device *bdev)
2581{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002582 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002583}
2584
Jens Axboe2b188cc2019-01-07 10:46:33 -07002585/*
2586 * If we tracked the file through the SCM inflight mechanism, we could support
2587 * any file. For now, just ensure that anything potentially problematic is done
2588 * inline.
2589 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002590static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002591{
2592 umode_t mode = file_inode(file)->i_mode;
2593
Jens Axboe4503b762020-06-01 10:00:27 -06002594 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002595 if (IS_ENABLED(CONFIG_BLOCK) &&
2596 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002597 return true;
2598 return false;
2599 }
2600 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002601 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002602 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002603 if (IS_ENABLED(CONFIG_BLOCK) &&
2604 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002605 file->f_op != &io_uring_fops)
2606 return true;
2607 return false;
2608 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002609
Jens Axboec5b85622020-06-09 19:23:05 -06002610 /* any ->read/write should understand O_NONBLOCK */
2611 if (file->f_flags & O_NONBLOCK)
2612 return true;
2613
Jens Axboeaf197f52020-04-28 13:15:06 -06002614 if (!(file->f_mode & FMODE_NOWAIT))
2615 return false;
2616
2617 if (rw == READ)
2618 return file->f_op->read_iter != NULL;
2619
2620 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002621}
2622
Pavel Begunkova88fc402020-09-30 22:57:53 +03002623static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002624{
Jens Axboedef596e2019-01-09 08:59:42 -07002625 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002626 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002627 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002628 unsigned ioprio;
2629 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002630
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002631 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002632 req->flags |= REQ_F_ISREG;
2633
Jens Axboe2b188cc2019-01-07 10:46:33 -07002634 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002635 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002636 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002637 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002638 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002639 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002640 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2641 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2642 if (unlikely(ret))
2643 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002644
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002645 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2646 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2647 req->flags |= REQ_F_NOWAIT;
2648
Jens Axboe2b188cc2019-01-07 10:46:33 -07002649 ioprio = READ_ONCE(sqe->ioprio);
2650 if (ioprio) {
2651 ret = ioprio_check_cap(ioprio);
2652 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002653 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654
2655 kiocb->ki_ioprio = ioprio;
2656 } else
2657 kiocb->ki_ioprio = get_current_ioprio();
2658
Jens Axboedef596e2019-01-09 08:59:42 -07002659 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002660 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2661 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002662 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002663
Jens Axboedef596e2019-01-09 08:59:42 -07002664 kiocb->ki_flags |= IOCB_HIPRI;
2665 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002666 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002667 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002668 if (kiocb->ki_flags & IOCB_HIPRI)
2669 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002670 kiocb->ki_complete = io_complete_rw;
2671 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002672
Jens Axboe3529d8c2019-12-19 18:24:38 -07002673 req->rw.addr = READ_ONCE(sqe->addr);
2674 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002675 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002676 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002677}
2678
2679static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2680{
2681 switch (ret) {
2682 case -EIOCBQUEUED:
2683 break;
2684 case -ERESTARTSYS:
2685 case -ERESTARTNOINTR:
2686 case -ERESTARTNOHAND:
2687 case -ERESTART_RESTARTBLOCK:
2688 /*
2689 * We can't just restart the syscall, since previously
2690 * submitted sqes may already be in progress. Just fail this
2691 * IO with EINTR.
2692 */
2693 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002694 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002695 default:
2696 kiocb->ki_complete(kiocb, ret, 0);
2697 }
2698}
2699
Jens Axboea1d7c392020-06-22 11:09:46 -06002700static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002701 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002702{
Jens Axboeba042912019-12-25 16:33:42 -07002703 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002704 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002705
Jens Axboe227c0c92020-08-13 11:51:40 -06002706 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002707 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002708 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002709 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002710 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002711 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002712 }
2713
Jens Axboeba042912019-12-25 16:33:42 -07002714 if (req->flags & REQ_F_CUR_POS)
2715 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002716 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002717 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002718 else
2719 io_rw_done(kiocb, ret);
2720}
2721
Pavel Begunkov847595d2021-02-04 13:52:06 +00002722static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002723{
Jens Axboe9adbd452019-12-20 08:45:55 -07002724 struct io_ring_ctx *ctx = req->ctx;
2725 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002726 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002727 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002728 size_t offset;
2729 u64 buf_addr;
2730
Jens Axboeedafcce2019-01-09 09:16:05 -07002731 if (unlikely(buf_index >= ctx->nr_user_bufs))
2732 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002733 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2734 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002735 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002736
2737 /* overflow */
2738 if (buf_addr + len < buf_addr)
2739 return -EFAULT;
2740 /* not inside the mapped region */
2741 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2742 return -EFAULT;
2743
2744 /*
2745 * May not be a start of buffer, set size appropriately
2746 * and advance us to the beginning.
2747 */
2748 offset = buf_addr - imu->ubuf;
2749 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002750
2751 if (offset) {
2752 /*
2753 * Don't use iov_iter_advance() here, as it's really slow for
2754 * using the latter parts of a big fixed buffer - it iterates
2755 * over each segment manually. We can cheat a bit here, because
2756 * we know that:
2757 *
2758 * 1) it's a BVEC iter, we set it up
2759 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2760 * first and last bvec
2761 *
2762 * So just find our index, and adjust the iterator afterwards.
2763 * If the offset is within the first bvec (or the whole first
2764 * bvec, just use iov_iter_advance(). This makes it easier
2765 * since we can just skip the first segment, which may not
2766 * be PAGE_SIZE aligned.
2767 */
2768 const struct bio_vec *bvec = imu->bvec;
2769
2770 if (offset <= bvec->bv_len) {
2771 iov_iter_advance(iter, offset);
2772 } else {
2773 unsigned long seg_skip;
2774
2775 /* skip first vec */
2776 offset -= bvec->bv_len;
2777 seg_skip = 1 + (offset >> PAGE_SHIFT);
2778
2779 iter->bvec = bvec + seg_skip;
2780 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002781 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002782 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002783 }
2784 }
2785
Pavel Begunkov847595d2021-02-04 13:52:06 +00002786 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002787}
2788
Jens Axboebcda7ba2020-02-23 16:42:51 -07002789static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2790{
2791 if (needs_lock)
2792 mutex_unlock(&ctx->uring_lock);
2793}
2794
2795static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2796{
2797 /*
2798 * "Normal" inline submissions always hold the uring_lock, since we
2799 * grab it from the system call. Same is true for the SQPOLL offload.
2800 * The only exception is when we've detached the request and issue it
2801 * from an async worker thread, grab the lock for that case.
2802 */
2803 if (needs_lock)
2804 mutex_lock(&ctx->uring_lock);
2805}
2806
2807static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2808 int bgid, struct io_buffer *kbuf,
2809 bool needs_lock)
2810{
2811 struct io_buffer *head;
2812
2813 if (req->flags & REQ_F_BUFFER_SELECTED)
2814 return kbuf;
2815
2816 io_ring_submit_lock(req->ctx, needs_lock);
2817
2818 lockdep_assert_held(&req->ctx->uring_lock);
2819
2820 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2821 if (head) {
2822 if (!list_empty(&head->list)) {
2823 kbuf = list_last_entry(&head->list, struct io_buffer,
2824 list);
2825 list_del(&kbuf->list);
2826 } else {
2827 kbuf = head;
2828 idr_remove(&req->ctx->io_buffer_idr, bgid);
2829 }
2830 if (*len > kbuf->len)
2831 *len = kbuf->len;
2832 } else {
2833 kbuf = ERR_PTR(-ENOBUFS);
2834 }
2835
2836 io_ring_submit_unlock(req->ctx, needs_lock);
2837
2838 return kbuf;
2839}
2840
Jens Axboe4d954c22020-02-27 07:31:19 -07002841static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2842 bool needs_lock)
2843{
2844 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002845 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002846
2847 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002848 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002849 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2850 if (IS_ERR(kbuf))
2851 return kbuf;
2852 req->rw.addr = (u64) (unsigned long) kbuf;
2853 req->flags |= REQ_F_BUFFER_SELECTED;
2854 return u64_to_user_ptr(kbuf->addr);
2855}
2856
2857#ifdef CONFIG_COMPAT
2858static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2859 bool needs_lock)
2860{
2861 struct compat_iovec __user *uiov;
2862 compat_ssize_t clen;
2863 void __user *buf;
2864 ssize_t len;
2865
2866 uiov = u64_to_user_ptr(req->rw.addr);
2867 if (!access_ok(uiov, sizeof(*uiov)))
2868 return -EFAULT;
2869 if (__get_user(clen, &uiov->iov_len))
2870 return -EFAULT;
2871 if (clen < 0)
2872 return -EINVAL;
2873
2874 len = clen;
2875 buf = io_rw_buffer_select(req, &len, needs_lock);
2876 if (IS_ERR(buf))
2877 return PTR_ERR(buf);
2878 iov[0].iov_base = buf;
2879 iov[0].iov_len = (compat_size_t) len;
2880 return 0;
2881}
2882#endif
2883
2884static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2885 bool needs_lock)
2886{
2887 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2888 void __user *buf;
2889 ssize_t len;
2890
2891 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2892 return -EFAULT;
2893
2894 len = iov[0].iov_len;
2895 if (len < 0)
2896 return -EINVAL;
2897 buf = io_rw_buffer_select(req, &len, needs_lock);
2898 if (IS_ERR(buf))
2899 return PTR_ERR(buf);
2900 iov[0].iov_base = buf;
2901 iov[0].iov_len = len;
2902 return 0;
2903}
2904
2905static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2906 bool needs_lock)
2907{
Jens Axboedddb3e22020-06-04 11:27:01 -06002908 if (req->flags & REQ_F_BUFFER_SELECTED) {
2909 struct io_buffer *kbuf;
2910
2911 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2912 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2913 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002914 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002915 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00002916 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07002917 return -EINVAL;
2918
2919#ifdef CONFIG_COMPAT
2920 if (req->ctx->compat)
2921 return io_compat_import(req, iov, needs_lock);
2922#endif
2923
2924 return __io_iov_buffer_select(req, iov, needs_lock);
2925}
2926
Pavel Begunkov847595d2021-02-04 13:52:06 +00002927static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
2928 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002929{
Jens Axboe9adbd452019-12-20 08:45:55 -07002930 void __user *buf = u64_to_user_ptr(req->rw.addr);
2931 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002932 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07002933 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002934
Pavel Begunkov7d009162019-11-25 23:14:40 +03002935 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002936 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002937 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002938 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939
Jens Axboebcda7ba2020-02-23 16:42:51 -07002940 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002941 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002942 return -EINVAL;
2943
Jens Axboe3a6820f2019-12-22 15:19:35 -07002944 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002945 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002946 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002947 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002948 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002949 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002950 }
2951
Jens Axboe3a6820f2019-12-22 15:19:35 -07002952 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2953 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00002954 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002955 }
2956
Jens Axboe4d954c22020-02-27 07:31:19 -07002957 if (req->flags & REQ_F_BUFFER_SELECT) {
2958 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00002959 if (!ret)
2960 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07002961 *iovec = NULL;
2962 return ret;
2963 }
2964
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02002965 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
2966 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002967}
2968
Jens Axboe0fef9482020-08-26 10:36:20 -06002969static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2970{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03002971 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06002972}
2973
Jens Axboe32960612019-09-23 11:05:34 -06002974/*
2975 * For files that don't have ->read_iter() and ->write_iter(), handle them
2976 * by looping over ->read() or ->write() manually.
2977 */
Jens Axboe4017eb92020-10-22 14:14:12 -06002978static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06002979{
Jens Axboe4017eb92020-10-22 14:14:12 -06002980 struct kiocb *kiocb = &req->rw.kiocb;
2981 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06002982 ssize_t ret = 0;
2983
2984 /*
2985 * Don't support polled IO through this interface, and we can't
2986 * support non-blocking either. For the latter, this just causes
2987 * the kiocb to be handled from an async context.
2988 */
2989 if (kiocb->ki_flags & IOCB_HIPRI)
2990 return -EOPNOTSUPP;
2991 if (kiocb->ki_flags & IOCB_NOWAIT)
2992 return -EAGAIN;
2993
2994 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002995 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002996 ssize_t nr;
2997
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002998 if (!iov_iter_is_bvec(iter)) {
2999 iovec = iov_iter_iovec(iter);
3000 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003001 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3002 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003003 }
3004
Jens Axboe32960612019-09-23 11:05:34 -06003005 if (rw == READ) {
3006 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003007 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003008 } else {
3009 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003010 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003011 }
3012
3013 if (nr < 0) {
3014 if (!ret)
3015 ret = nr;
3016 break;
3017 }
3018 ret += nr;
3019 if (nr != iovec.iov_len)
3020 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003021 req->rw.len -= nr;
3022 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003023 iov_iter_advance(iter, nr);
3024 }
3025
3026 return ret;
3027}
3028
Jens Axboeff6165b2020-08-13 09:47:43 -06003029static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3030 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003031{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003032 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003033
Jens Axboeff6165b2020-08-13 09:47:43 -06003034 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003035 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003036 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003037 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003038 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003039 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003040 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003041 unsigned iov_off = 0;
3042
3043 rw->iter.iov = rw->fast_iov;
3044 if (iter->iov != fast_iov) {
3045 iov_off = iter->iov - fast_iov;
3046 rw->iter.iov += iov_off;
3047 }
3048 if (rw->fast_iov != fast_iov)
3049 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003050 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003051 } else {
3052 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003053 }
3054}
3055
Jens Axboee8c2bc12020-08-15 18:44:09 -07003056static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003057{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003058 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3059 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3060 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003061}
3062
Jens Axboee8c2bc12020-08-15 18:44:09 -07003063static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003064{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003065 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003066 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003067
Jens Axboee8c2bc12020-08-15 18:44:09 -07003068 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003069}
3070
Jens Axboeff6165b2020-08-13 09:47:43 -06003071static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3072 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003073 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003074{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003075 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003076 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003077 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003078 if (__io_alloc_async_data(req)) {
3079 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003080 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003081 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003082
Jens Axboeff6165b2020-08-13 09:47:43 -06003083 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003084 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003085 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003086}
3087
Pavel Begunkov73debe62020-09-30 22:57:54 +03003088static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003089{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003090 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003091 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003092 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003093
Pavel Begunkov2846c482020-11-07 13:16:27 +00003094 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003095 if (unlikely(ret < 0))
3096 return ret;
3097
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003098 iorw->bytes_done = 0;
3099 iorw->free_iovec = iov;
3100 if (iov)
3101 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003102 return 0;
3103}
3104
Pavel Begunkov73debe62020-09-30 22:57:54 +03003105static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003106{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003107 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3108 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003109 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003110}
3111
Jens Axboec1dd91d2020-08-03 16:43:59 -06003112/*
3113 * This is our waitqueue callback handler, registered through lock_page_async()
3114 * when we initially tried to do the IO with the iocb armed our waitqueue.
3115 * This gets called when the page is unlocked, and we generally expect that to
3116 * happen when the page IO is completed and the page is now uptodate. This will
3117 * queue a task_work based retry of the operation, attempting to copy the data
3118 * again. If the latter fails because the page was NOT uptodate, then we will
3119 * do a thread based blocking retry of the operation. That's the unexpected
3120 * slow path.
3121 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003122static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3123 int sync, void *arg)
3124{
3125 struct wait_page_queue *wpq;
3126 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003127 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003128
3129 wpq = container_of(wait, struct wait_page_queue, wait);
3130
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003131 if (!wake_page_match(wpq, key))
3132 return 0;
3133
Hao Xuc8d317a2020-09-29 20:00:45 +08003134 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003135 list_del_init(&wait->entry);
3136
Jens Axboebcf5a062020-05-22 09:24:42 -06003137 /* submit ref gets dropped, acquire a new one */
3138 refcount_inc(&req->refs);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003139 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003140 return 1;
3141}
3142
Jens Axboec1dd91d2020-08-03 16:43:59 -06003143/*
3144 * This controls whether a given IO request should be armed for async page
3145 * based retry. If we return false here, the request is handed to the async
3146 * worker threads for retry. If we're doing buffered reads on a regular file,
3147 * we prepare a private wait_page_queue entry and retry the operation. This
3148 * will either succeed because the page is now uptodate and unlocked, or it
3149 * will register a callback when the page is unlocked at IO completion. Through
3150 * that callback, io_uring uses task_work to setup a retry of the operation.
3151 * That retry will attempt the buffered read again. The retry will generally
3152 * succeed, or in rare cases where it fails, we then fall back to using the
3153 * async worker threads for a blocking retry.
3154 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003155static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003156{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003157 struct io_async_rw *rw = req->async_data;
3158 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003159 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003160
3161 /* never retry for NOWAIT, we just complete with -EAGAIN */
3162 if (req->flags & REQ_F_NOWAIT)
3163 return false;
3164
Jens Axboe227c0c92020-08-13 11:51:40 -06003165 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003166 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003167 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003168
Jens Axboebcf5a062020-05-22 09:24:42 -06003169 /*
3170 * just use poll if we can, and don't attempt if the fs doesn't
3171 * support callback based unlocks
3172 */
3173 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3174 return false;
3175
Jens Axboe3b2a4432020-08-16 10:58:43 -07003176 wait->wait.func = io_async_buf_func;
3177 wait->wait.private = req;
3178 wait->wait.flags = 0;
3179 INIT_LIST_HEAD(&wait->wait.entry);
3180 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003181 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003182 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003183 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003184}
3185
3186static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3187{
3188 if (req->file->f_op->read_iter)
3189 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003190 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003191 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003192 else
3193 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003194}
3195
Pavel Begunkov889fca72021-02-10 00:03:09 +00003196static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003197{
3198 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003199 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003200 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003201 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003202 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003203 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003204
Pavel Begunkov2846c482020-11-07 13:16:27 +00003205 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003206 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003207 iovec = NULL;
3208 } else {
3209 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3210 if (ret < 0)
3211 return ret;
3212 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003213 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003214 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003215
Jens Axboefd6c2e42019-12-18 12:19:41 -07003216 /* Ensure we clear previously set non-block flag */
3217 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003218 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003219 else
3220 kiocb->ki_flags |= IOCB_NOWAIT;
3221
Pavel Begunkov24c74672020-06-21 13:09:51 +03003222 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003223 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3224 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003225 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003226 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003227
Pavel Begunkov632546c2020-11-07 13:16:26 +00003228 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003229 if (unlikely(ret)) {
3230 kfree(iovec);
3231 return ret;
3232 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003233
Jens Axboe227c0c92020-08-13 11:51:40 -06003234 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003235
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003236 if (ret == -EIOCBQUEUED) {
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003237 goto out_free;
Jens Axboe227c0c92020-08-13 11:51:40 -06003238 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003239 /* IOPOLL retry should happen for io-wq threads */
3240 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003241 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003242 /* no retry on NONBLOCK nor RWF_NOWAIT */
3243 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003244 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003245 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003246 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003247 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003248 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003249 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003250 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003251 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003252 }
3253
Jens Axboe227c0c92020-08-13 11:51:40 -06003254 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003255 if (ret2)
3256 return ret2;
3257
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003258 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003259 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003260 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003261 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003262
Pavel Begunkovb23df912021-02-04 13:52:04 +00003263 do {
3264 io_size -= ret;
3265 rw->bytes_done += ret;
3266 /* if we can retry, do so with the callbacks armed */
3267 if (!io_rw_should_retry(req)) {
3268 kiocb->ki_flags &= ~IOCB_WAITQ;
3269 return -EAGAIN;
3270 }
3271
3272 /*
3273 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3274 * we get -EIOCBQUEUED, then we'll get a notification when the
3275 * desired page gets unlocked. We can also get a partial read
3276 * here, and if we do, then just retry at the new offset.
3277 */
3278 ret = io_iter_do_read(req, iter);
3279 if (ret == -EIOCBQUEUED)
3280 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003281 /* we got some bytes, but not all. retry. */
Pavel Begunkovb23df912021-02-04 13:52:04 +00003282 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003283done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003284 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003285out_free:
3286 /* it's faster to check here then delegate to kfree */
3287 if (iovec)
3288 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003289 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003290}
3291
Pavel Begunkov73debe62020-09-30 22:57:54 +03003292static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003293{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003294 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3295 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003296 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003297}
3298
Pavel Begunkov889fca72021-02-10 00:03:09 +00003299static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003300{
3301 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003302 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003303 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003304 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003305 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003306 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003307
Pavel Begunkov2846c482020-11-07 13:16:27 +00003308 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003309 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003310 iovec = NULL;
3311 } else {
3312 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3313 if (ret < 0)
3314 return ret;
3315 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003316 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003317 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003318
Jens Axboefd6c2e42019-12-18 12:19:41 -07003319 /* Ensure we clear previously set non-block flag */
3320 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003321 kiocb->ki_flags &= ~IOCB_NOWAIT;
3322 else
3323 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003324
Pavel Begunkov24c74672020-06-21 13:09:51 +03003325 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003326 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003327 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003328
Jens Axboe10d59342019-12-09 20:16:22 -07003329 /* file path doesn't support NOWAIT for non-direct_IO */
3330 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3331 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003332 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003333
Pavel Begunkov632546c2020-11-07 13:16:26 +00003334 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003335 if (unlikely(ret))
3336 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003337
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003338 /*
3339 * Open-code file_start_write here to grab freeze protection,
3340 * which will be released by another thread in
3341 * io_complete_rw(). Fool lockdep by telling it the lock got
3342 * released so that it doesn't complain about the held lock when
3343 * we return to userspace.
3344 */
3345 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003346 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003347 __sb_writers_release(file_inode(req->file)->i_sb,
3348 SB_FREEZE_WRITE);
3349 }
3350 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003351
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003352 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003353 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003354 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003355 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003356 else
3357 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003358
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003359 /*
3360 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3361 * retry them without IOCB_NOWAIT.
3362 */
3363 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3364 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003365 /* no retry on NONBLOCK nor RWF_NOWAIT */
3366 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003367 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003368 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003369 /* IOPOLL retry should happen for io-wq threads */
3370 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3371 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003372done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003373 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003374 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003375copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003376 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003377 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003378 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003379 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003380 }
Jens Axboe31b51512019-01-18 22:56:34 -07003381out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003382 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003383 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003384 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003385 return ret;
3386}
3387
Jens Axboe80a261f2020-09-28 14:23:58 -06003388static int io_renameat_prep(struct io_kiocb *req,
3389 const struct io_uring_sqe *sqe)
3390{
3391 struct io_rename *ren = &req->rename;
3392 const char __user *oldf, *newf;
3393
3394 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3395 return -EBADF;
3396
3397 ren->old_dfd = READ_ONCE(sqe->fd);
3398 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3399 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3400 ren->new_dfd = READ_ONCE(sqe->len);
3401 ren->flags = READ_ONCE(sqe->rename_flags);
3402
3403 ren->oldpath = getname(oldf);
3404 if (IS_ERR(ren->oldpath))
3405 return PTR_ERR(ren->oldpath);
3406
3407 ren->newpath = getname(newf);
3408 if (IS_ERR(ren->newpath)) {
3409 putname(ren->oldpath);
3410 return PTR_ERR(ren->newpath);
3411 }
3412
3413 req->flags |= REQ_F_NEED_CLEANUP;
3414 return 0;
3415}
3416
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003417static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003418{
3419 struct io_rename *ren = &req->rename;
3420 int ret;
3421
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003422 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003423 return -EAGAIN;
3424
3425 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3426 ren->newpath, ren->flags);
3427
3428 req->flags &= ~REQ_F_NEED_CLEANUP;
3429 if (ret < 0)
3430 req_set_fail_links(req);
3431 io_req_complete(req, ret);
3432 return 0;
3433}
3434
Jens Axboe14a11432020-09-28 14:27:37 -06003435static int io_unlinkat_prep(struct io_kiocb *req,
3436 const struct io_uring_sqe *sqe)
3437{
3438 struct io_unlink *un = &req->unlink;
3439 const char __user *fname;
3440
3441 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3442 return -EBADF;
3443
3444 un->dfd = READ_ONCE(sqe->fd);
3445
3446 un->flags = READ_ONCE(sqe->unlink_flags);
3447 if (un->flags & ~AT_REMOVEDIR)
3448 return -EINVAL;
3449
3450 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3451 un->filename = getname(fname);
3452 if (IS_ERR(un->filename))
3453 return PTR_ERR(un->filename);
3454
3455 req->flags |= REQ_F_NEED_CLEANUP;
3456 return 0;
3457}
3458
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003459static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003460{
3461 struct io_unlink *un = &req->unlink;
3462 int ret;
3463
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003464 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003465 return -EAGAIN;
3466
3467 if (un->flags & AT_REMOVEDIR)
3468 ret = do_rmdir(un->dfd, un->filename);
3469 else
3470 ret = do_unlinkat(un->dfd, un->filename);
3471
3472 req->flags &= ~REQ_F_NEED_CLEANUP;
3473 if (ret < 0)
3474 req_set_fail_links(req);
3475 io_req_complete(req, ret);
3476 return 0;
3477}
3478
Jens Axboe36f4fa62020-09-05 11:14:22 -06003479static int io_shutdown_prep(struct io_kiocb *req,
3480 const struct io_uring_sqe *sqe)
3481{
3482#if defined(CONFIG_NET)
3483 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3484 return -EINVAL;
3485 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3486 sqe->buf_index)
3487 return -EINVAL;
3488
3489 req->shutdown.how = READ_ONCE(sqe->len);
3490 return 0;
3491#else
3492 return -EOPNOTSUPP;
3493#endif
3494}
3495
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003496static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003497{
3498#if defined(CONFIG_NET)
3499 struct socket *sock;
3500 int ret;
3501
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003502 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003503 return -EAGAIN;
3504
Linus Torvalds48aba792020-12-16 12:44:05 -08003505 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003506 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003507 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003508
3509 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003510 if (ret < 0)
3511 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003512 io_req_complete(req, ret);
3513 return 0;
3514#else
3515 return -EOPNOTSUPP;
3516#endif
3517}
3518
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003519static int __io_splice_prep(struct io_kiocb *req,
3520 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003521{
3522 struct io_splice* sp = &req->splice;
3523 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003524
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003525 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3526 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003527
3528 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003529 sp->len = READ_ONCE(sqe->len);
3530 sp->flags = READ_ONCE(sqe->splice_flags);
3531
3532 if (unlikely(sp->flags & ~valid_flags))
3533 return -EINVAL;
3534
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003535 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3536 (sp->flags & SPLICE_F_FD_IN_FIXED));
3537 if (!sp->file_in)
3538 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003539 req->flags |= REQ_F_NEED_CLEANUP;
3540
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003541 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3542 /*
3543 * Splice operation will be punted aync, and here need to
3544 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3545 */
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003546 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003547 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003548
3549 return 0;
3550}
3551
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003552static int io_tee_prep(struct io_kiocb *req,
3553 const struct io_uring_sqe *sqe)
3554{
3555 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3556 return -EINVAL;
3557 return __io_splice_prep(req, sqe);
3558}
3559
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003560static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003561{
3562 struct io_splice *sp = &req->splice;
3563 struct file *in = sp->file_in;
3564 struct file *out = sp->file_out;
3565 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3566 long ret = 0;
3567
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003568 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003569 return -EAGAIN;
3570 if (sp->len)
3571 ret = do_tee(in, out, sp->len, flags);
3572
3573 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3574 req->flags &= ~REQ_F_NEED_CLEANUP;
3575
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003576 if (ret != sp->len)
3577 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003578 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003579 return 0;
3580}
3581
3582static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3583{
3584 struct io_splice* sp = &req->splice;
3585
3586 sp->off_in = READ_ONCE(sqe->splice_off_in);
3587 sp->off_out = READ_ONCE(sqe->off);
3588 return __io_splice_prep(req, sqe);
3589}
3590
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003591static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003592{
3593 struct io_splice *sp = &req->splice;
3594 struct file *in = sp->file_in;
3595 struct file *out = sp->file_out;
3596 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3597 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003598 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003599
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003600 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003601 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003602
3603 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3604 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003605
Jens Axboe948a7742020-05-17 14:21:38 -06003606 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003607 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003608
3609 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3610 req->flags &= ~REQ_F_NEED_CLEANUP;
3611
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003612 if (ret != sp->len)
3613 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003614 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003615 return 0;
3616}
3617
Jens Axboe2b188cc2019-01-07 10:46:33 -07003618/*
3619 * IORING_OP_NOP just posts a completion event, nothing else.
3620 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003621static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003622{
3623 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003624
Jens Axboedef596e2019-01-09 08:59:42 -07003625 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3626 return -EINVAL;
3627
Pavel Begunkov889fca72021-02-10 00:03:09 +00003628 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003629 return 0;
3630}
3631
Pavel Begunkov1155c762021-02-18 18:29:38 +00003632static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003633{
Jens Axboe6b063142019-01-10 22:13:58 -07003634 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003635
Jens Axboe09bb8392019-03-13 12:39:28 -06003636 if (!req->file)
3637 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003638
Jens Axboe6b063142019-01-10 22:13:58 -07003639 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003640 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003641 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003642 return -EINVAL;
3643
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003644 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3645 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3646 return -EINVAL;
3647
3648 req->sync.off = READ_ONCE(sqe->off);
3649 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003650 return 0;
3651}
3652
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003653static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003654{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003655 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003656 int ret;
3657
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003658 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003659 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003660 return -EAGAIN;
3661
Jens Axboe9adbd452019-12-20 08:45:55 -07003662 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003663 end > 0 ? end : LLONG_MAX,
3664 req->sync.flags & IORING_FSYNC_DATASYNC);
3665 if (ret < 0)
3666 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003667 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003668 return 0;
3669}
3670
Jens Axboed63d1b52019-12-10 10:38:56 -07003671static int io_fallocate_prep(struct io_kiocb *req,
3672 const struct io_uring_sqe *sqe)
3673{
3674 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3675 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003676 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3677 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003678
3679 req->sync.off = READ_ONCE(sqe->off);
3680 req->sync.len = READ_ONCE(sqe->addr);
3681 req->sync.mode = READ_ONCE(sqe->len);
3682 return 0;
3683}
3684
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003685static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003686{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003687 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003688
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003689 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003690 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003691 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003692 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3693 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003694 if (ret < 0)
3695 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003696 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003697 return 0;
3698}
3699
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003700static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003701{
Jens Axboef8748882020-01-08 17:47:02 -07003702 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003703 int ret;
3704
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003705 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003706 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003707 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003708 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003709
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003710 /* open.how should be already initialised */
3711 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003712 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003713
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003714 req->open.dfd = READ_ONCE(sqe->fd);
3715 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003716 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003717 if (IS_ERR(req->open.filename)) {
3718 ret = PTR_ERR(req->open.filename);
3719 req->open.filename = NULL;
3720 return ret;
3721 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003722 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003723 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003724 return 0;
3725}
3726
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003727static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3728{
3729 u64 flags, mode;
3730
Jens Axboe14587a462020-09-05 11:36:08 -06003731 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003732 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003733 mode = READ_ONCE(sqe->len);
3734 flags = READ_ONCE(sqe->open_flags);
3735 req->open.how = build_open_how(flags, mode);
3736 return __io_openat_prep(req, sqe);
3737}
3738
Jens Axboecebdb982020-01-08 17:59:24 -07003739static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3740{
3741 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003742 size_t len;
3743 int ret;
3744
Jens Axboe14587a462020-09-05 11:36:08 -06003745 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003746 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003747 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3748 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003749 if (len < OPEN_HOW_SIZE_VER0)
3750 return -EINVAL;
3751
3752 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3753 len);
3754 if (ret)
3755 return ret;
3756
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003757 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003758}
3759
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003760static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003761{
3762 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003763 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003764 bool nonblock_set;
3765 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003766 int ret;
3767
Jens Axboecebdb982020-01-08 17:59:24 -07003768 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003769 if (ret)
3770 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003771 nonblock_set = op.open_flag & O_NONBLOCK;
3772 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003773 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003774 /*
3775 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3776 * it'll always -EAGAIN
3777 */
3778 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3779 return -EAGAIN;
3780 op.lookup_flags |= LOOKUP_CACHED;
3781 op.open_flag |= O_NONBLOCK;
3782 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003783
Jens Axboe4022e7a2020-03-19 19:23:18 -06003784 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003785 if (ret < 0)
3786 goto err;
3787
3788 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07003789 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003790 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
3791 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003792 /*
3793 * We could hang on to this 'fd', but seems like marginal
3794 * gain for something that is now known to be a slower path.
3795 * So just put it, and we'll get a new one when we retry.
3796 */
3797 put_unused_fd(ret);
3798 return -EAGAIN;
3799 }
3800
Jens Axboe15b71ab2019-12-11 11:20:36 -07003801 if (IS_ERR(file)) {
3802 put_unused_fd(ret);
3803 ret = PTR_ERR(file);
3804 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003805 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07003806 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003807 fsnotify_open(file);
3808 fd_install(ret, file);
3809 }
3810err:
3811 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003812 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003813 if (ret < 0)
3814 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003815 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003816 return 0;
3817}
3818
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003819static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07003820{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003821 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07003822}
3823
Jens Axboe067524e2020-03-02 16:32:28 -07003824static int io_remove_buffers_prep(struct io_kiocb *req,
3825 const struct io_uring_sqe *sqe)
3826{
3827 struct io_provide_buf *p = &req->pbuf;
3828 u64 tmp;
3829
3830 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3831 return -EINVAL;
3832
3833 tmp = READ_ONCE(sqe->fd);
3834 if (!tmp || tmp > USHRT_MAX)
3835 return -EINVAL;
3836
3837 memset(p, 0, sizeof(*p));
3838 p->nbufs = tmp;
3839 p->bgid = READ_ONCE(sqe->buf_group);
3840 return 0;
3841}
3842
3843static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3844 int bgid, unsigned nbufs)
3845{
3846 unsigned i = 0;
3847
3848 /* shouldn't happen */
3849 if (!nbufs)
3850 return 0;
3851
3852 /* the head kbuf is the list itself */
3853 while (!list_empty(&buf->list)) {
3854 struct io_buffer *nxt;
3855
3856 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3857 list_del(&nxt->list);
3858 kfree(nxt);
3859 if (++i == nbufs)
3860 return i;
3861 }
3862 i++;
3863 kfree(buf);
3864 idr_remove(&ctx->io_buffer_idr, bgid);
3865
3866 return i;
3867}
3868
Pavel Begunkov889fca72021-02-10 00:03:09 +00003869static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07003870{
3871 struct io_provide_buf *p = &req->pbuf;
3872 struct io_ring_ctx *ctx = req->ctx;
3873 struct io_buffer *head;
3874 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003875 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07003876
3877 io_ring_submit_lock(ctx, !force_nonblock);
3878
3879 lockdep_assert_held(&ctx->uring_lock);
3880
3881 ret = -ENOENT;
3882 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3883 if (head)
3884 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07003885 if (ret < 0)
3886 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003887
3888 /* need to hold the lock to complete IOPOLL requests */
3889 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00003890 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003891 io_ring_submit_unlock(ctx, !force_nonblock);
3892 } else {
3893 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00003894 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003895 }
Jens Axboe067524e2020-03-02 16:32:28 -07003896 return 0;
3897}
3898
Jens Axboeddf0322d2020-02-23 16:41:33 -07003899static int io_provide_buffers_prep(struct io_kiocb *req,
3900 const struct io_uring_sqe *sqe)
3901{
3902 struct io_provide_buf *p = &req->pbuf;
3903 u64 tmp;
3904
3905 if (sqe->ioprio || sqe->rw_flags)
3906 return -EINVAL;
3907
3908 tmp = READ_ONCE(sqe->fd);
3909 if (!tmp || tmp > USHRT_MAX)
3910 return -E2BIG;
3911 p->nbufs = tmp;
3912 p->addr = READ_ONCE(sqe->addr);
3913 p->len = READ_ONCE(sqe->len);
3914
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003915 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003916 return -EFAULT;
3917
3918 p->bgid = READ_ONCE(sqe->buf_group);
3919 tmp = READ_ONCE(sqe->off);
3920 if (tmp > USHRT_MAX)
3921 return -E2BIG;
3922 p->bid = tmp;
3923 return 0;
3924}
3925
3926static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3927{
3928 struct io_buffer *buf;
3929 u64 addr = pbuf->addr;
3930 int i, bid = pbuf->bid;
3931
3932 for (i = 0; i < pbuf->nbufs; i++) {
3933 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3934 if (!buf)
3935 break;
3936
3937 buf->addr = addr;
3938 buf->len = pbuf->len;
3939 buf->bid = bid;
3940 addr += pbuf->len;
3941 bid++;
3942 if (!*head) {
3943 INIT_LIST_HEAD(&buf->list);
3944 *head = buf;
3945 } else {
3946 list_add_tail(&buf->list, &(*head)->list);
3947 }
3948 }
3949
3950 return i ? i : -ENOMEM;
3951}
3952
Pavel Begunkov889fca72021-02-10 00:03:09 +00003953static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003954{
3955 struct io_provide_buf *p = &req->pbuf;
3956 struct io_ring_ctx *ctx = req->ctx;
3957 struct io_buffer *head, *list;
3958 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003959 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07003960
3961 io_ring_submit_lock(ctx, !force_nonblock);
3962
3963 lockdep_assert_held(&ctx->uring_lock);
3964
3965 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3966
3967 ret = io_add_buffers(p, &head);
3968 if (ret < 0)
3969 goto out;
3970
3971 if (!list) {
3972 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3973 GFP_KERNEL);
3974 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003975 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003976 goto out;
3977 }
3978 }
3979out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07003980 if (ret < 0)
3981 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003982
3983 /* need to hold the lock to complete IOPOLL requests */
3984 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00003985 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003986 io_ring_submit_unlock(ctx, !force_nonblock);
3987 } else {
3988 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00003989 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003990 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07003991 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003992}
3993
Jens Axboe3e4827b2020-01-08 15:18:09 -07003994static int io_epoll_ctl_prep(struct io_kiocb *req,
3995 const struct io_uring_sqe *sqe)
3996{
3997#if defined(CONFIG_EPOLL)
3998 if (sqe->ioprio || sqe->buf_index)
3999 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004000 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004001 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004002
4003 req->epoll.epfd = READ_ONCE(sqe->fd);
4004 req->epoll.op = READ_ONCE(sqe->len);
4005 req->epoll.fd = READ_ONCE(sqe->off);
4006
4007 if (ep_op_has_event(req->epoll.op)) {
4008 struct epoll_event __user *ev;
4009
4010 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4011 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4012 return -EFAULT;
4013 }
4014
4015 return 0;
4016#else
4017 return -EOPNOTSUPP;
4018#endif
4019}
4020
Pavel Begunkov889fca72021-02-10 00:03:09 +00004021static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004022{
4023#if defined(CONFIG_EPOLL)
4024 struct io_epoll *ie = &req->epoll;
4025 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004026 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004027
4028 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4029 if (force_nonblock && ret == -EAGAIN)
4030 return -EAGAIN;
4031
4032 if (ret < 0)
4033 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004034 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004035 return 0;
4036#else
4037 return -EOPNOTSUPP;
4038#endif
4039}
4040
Jens Axboec1ca7572019-12-25 22:18:28 -07004041static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4042{
4043#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4044 if (sqe->ioprio || sqe->buf_index || sqe->off)
4045 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004046 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4047 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004048
4049 req->madvise.addr = READ_ONCE(sqe->addr);
4050 req->madvise.len = READ_ONCE(sqe->len);
4051 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4052 return 0;
4053#else
4054 return -EOPNOTSUPP;
4055#endif
4056}
4057
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004058static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004059{
4060#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4061 struct io_madvise *ma = &req->madvise;
4062 int ret;
4063
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004064 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004065 return -EAGAIN;
4066
Minchan Kim0726b012020-10-17 16:14:50 -07004067 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004068 if (ret < 0)
4069 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004070 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004071 return 0;
4072#else
4073 return -EOPNOTSUPP;
4074#endif
4075}
4076
Jens Axboe4840e412019-12-25 22:03:45 -07004077static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4078{
4079 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4080 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004081 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4082 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004083
4084 req->fadvise.offset = READ_ONCE(sqe->off);
4085 req->fadvise.len = READ_ONCE(sqe->len);
4086 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4087 return 0;
4088}
4089
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004090static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004091{
4092 struct io_fadvise *fa = &req->fadvise;
4093 int ret;
4094
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004095 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004096 switch (fa->advice) {
4097 case POSIX_FADV_NORMAL:
4098 case POSIX_FADV_RANDOM:
4099 case POSIX_FADV_SEQUENTIAL:
4100 break;
4101 default:
4102 return -EAGAIN;
4103 }
4104 }
Jens Axboe4840e412019-12-25 22:03:45 -07004105
4106 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4107 if (ret < 0)
4108 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004109 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004110 return 0;
4111}
4112
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004113static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4114{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004115 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004116 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004117 if (sqe->ioprio || sqe->buf_index)
4118 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004119 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004120 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004121
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004122 req->statx.dfd = READ_ONCE(sqe->fd);
4123 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004124 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004125 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4126 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004127
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004128 return 0;
4129}
4130
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004131static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004132{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004133 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004134 int ret;
4135
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004136 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004137 /* only need file table for an actual valid fd */
4138 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4139 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004140 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004141 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004142
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004143 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4144 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004145
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004146 if (ret < 0)
4147 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004148 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004149 return 0;
4150}
4151
Jens Axboeb5dba592019-12-11 14:02:38 -07004152static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4153{
Jens Axboe14587a462020-09-05 11:36:08 -06004154 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004155 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004156 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4157 sqe->rw_flags || sqe->buf_index)
4158 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004159 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004160 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004161
4162 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004163 return 0;
4164}
4165
Pavel Begunkov889fca72021-02-10 00:03:09 +00004166static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004167{
Jens Axboe9eac1902021-01-19 15:50:37 -07004168 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004169 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004170 struct fdtable *fdt;
4171 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004172 int ret;
4173
Jens Axboe9eac1902021-01-19 15:50:37 -07004174 file = NULL;
4175 ret = -EBADF;
4176 spin_lock(&files->file_lock);
4177 fdt = files_fdtable(files);
4178 if (close->fd >= fdt->max_fds) {
4179 spin_unlock(&files->file_lock);
4180 goto err;
4181 }
4182 file = fdt->fd[close->fd];
4183 if (!file) {
4184 spin_unlock(&files->file_lock);
4185 goto err;
4186 }
4187
4188 if (file->f_op == &io_uring_fops) {
4189 spin_unlock(&files->file_lock);
4190 file = NULL;
4191 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004192 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004193
4194 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004195 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004196 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004197 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004198 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004199
Jens Axboe9eac1902021-01-19 15:50:37 -07004200 ret = __close_fd_get_file(close->fd, &file);
4201 spin_unlock(&files->file_lock);
4202 if (ret < 0) {
4203 if (ret == -ENOENT)
4204 ret = -EBADF;
4205 goto err;
4206 }
4207
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004208 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004209 ret = filp_close(file, current->files);
4210err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004211 if (ret < 0)
4212 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004213 if (file)
4214 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004215 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004216 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004217}
4218
Pavel Begunkov1155c762021-02-18 18:29:38 +00004219static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004220{
4221 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004222
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004223 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4224 return -EINVAL;
4225 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4226 return -EINVAL;
4227
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004228 req->sync.off = READ_ONCE(sqe->off);
4229 req->sync.len = READ_ONCE(sqe->len);
4230 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004231 return 0;
4232}
4233
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004234static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004235{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004236 int ret;
4237
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004238 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004239 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004240 return -EAGAIN;
4241
Jens Axboe9adbd452019-12-20 08:45:55 -07004242 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004243 req->sync.flags);
4244 if (ret < 0)
4245 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004246 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004247 return 0;
4248}
4249
YueHaibing469956e2020-03-04 15:53:52 +08004250#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004251static int io_setup_async_msg(struct io_kiocb *req,
4252 struct io_async_msghdr *kmsg)
4253{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004254 struct io_async_msghdr *async_msg = req->async_data;
4255
4256 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004257 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004258 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004259 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004260 return -ENOMEM;
4261 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004262 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004263 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004264 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004265 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004266 /* if were using fast_iov, set it to the new one */
4267 if (!async_msg->free_iov)
4268 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4269
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004270 return -EAGAIN;
4271}
4272
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004273static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4274 struct io_async_msghdr *iomsg)
4275{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004276 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004277 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004278 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004279 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004280}
4281
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004282static int io_sendmsg_prep_async(struct io_kiocb *req)
4283{
4284 int ret;
4285
4286 if (!io_op_defs[req->opcode].needs_async_data)
4287 return 0;
4288 ret = io_sendmsg_copy_hdr(req, req->async_data);
4289 if (!ret)
4290 req->flags |= REQ_F_NEED_CLEANUP;
4291 return ret;
4292}
4293
Jens Axboe3529d8c2019-12-19 18:24:38 -07004294static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004295{
Jens Axboee47293f2019-12-20 08:58:21 -07004296 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004297
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004298 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4299 return -EINVAL;
4300
Jens Axboee47293f2019-12-20 08:58:21 -07004301 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004302 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004303 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004304
Jens Axboed8768362020-02-27 14:17:49 -07004305#ifdef CONFIG_COMPAT
4306 if (req->ctx->compat)
4307 sr->msg_flags |= MSG_CMSG_COMPAT;
4308#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004309 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004310}
4311
Pavel Begunkov889fca72021-02-10 00:03:09 +00004312static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004313{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004314 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004315 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004316 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004317 int ret;
4318
Florent Revestdba4a922020-12-04 12:36:04 +01004319 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004320 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004321 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004322
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004323 kmsg = req->async_data;
4324 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004325 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004326 if (ret)
4327 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004328 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004329 }
4330
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004331 flags = req->sr_msg.msg_flags;
4332 if (flags & MSG_DONTWAIT)
4333 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004334 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004335 flags |= MSG_DONTWAIT;
4336
4337 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004338 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004339 return io_setup_async_msg(req, kmsg);
4340 if (ret == -ERESTARTSYS)
4341 ret = -EINTR;
4342
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004343 /* fast path, check for non-NULL to avoid function call */
4344 if (kmsg->free_iov)
4345 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004346 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004347 if (ret < 0)
4348 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004349 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004350 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004351}
4352
Pavel Begunkov889fca72021-02-10 00:03:09 +00004353static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004354{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004355 struct io_sr_msg *sr = &req->sr_msg;
4356 struct msghdr msg;
4357 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004358 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004359 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004360 int ret;
4361
Florent Revestdba4a922020-12-04 12:36:04 +01004362 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004363 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004364 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004365
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004366 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4367 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004368 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004369
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004370 msg.msg_name = NULL;
4371 msg.msg_control = NULL;
4372 msg.msg_controllen = 0;
4373 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004374
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004375 flags = req->sr_msg.msg_flags;
4376 if (flags & MSG_DONTWAIT)
4377 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004378 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004379 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004380
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004381 msg.msg_flags = flags;
4382 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004383 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004384 return -EAGAIN;
4385 if (ret == -ERESTARTSYS)
4386 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004387
Jens Axboe03b12302019-12-02 18:50:25 -07004388 if (ret < 0)
4389 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004390 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004391 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004392}
4393
Pavel Begunkov1400e692020-07-12 20:41:05 +03004394static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4395 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004396{
4397 struct io_sr_msg *sr = &req->sr_msg;
4398 struct iovec __user *uiov;
4399 size_t iov_len;
4400 int ret;
4401
Pavel Begunkov1400e692020-07-12 20:41:05 +03004402 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4403 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004404 if (ret)
4405 return ret;
4406
4407 if (req->flags & REQ_F_BUFFER_SELECT) {
4408 if (iov_len > 1)
4409 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004410 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004411 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004412 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004413 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004414 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004415 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004416 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004417 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004418 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004419 if (ret > 0)
4420 ret = 0;
4421 }
4422
4423 return ret;
4424}
4425
4426#ifdef CONFIG_COMPAT
4427static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004428 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004429{
4430 struct compat_msghdr __user *msg_compat;
4431 struct io_sr_msg *sr = &req->sr_msg;
4432 struct compat_iovec __user *uiov;
4433 compat_uptr_t ptr;
4434 compat_size_t len;
4435 int ret;
4436
Pavel Begunkov270a5942020-07-12 20:41:04 +03004437 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004438 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004439 &ptr, &len);
4440 if (ret)
4441 return ret;
4442
4443 uiov = compat_ptr(ptr);
4444 if (req->flags & REQ_F_BUFFER_SELECT) {
4445 compat_ssize_t clen;
4446
4447 if (len > 1)
4448 return -EINVAL;
4449 if (!access_ok(uiov, sizeof(*uiov)))
4450 return -EFAULT;
4451 if (__get_user(clen, &uiov->iov_len))
4452 return -EFAULT;
4453 if (clen < 0)
4454 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004455 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004456 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004457 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004458 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004459 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004460 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004461 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004462 if (ret < 0)
4463 return ret;
4464 }
4465
4466 return 0;
4467}
Jens Axboe03b12302019-12-02 18:50:25 -07004468#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004469
Pavel Begunkov1400e692020-07-12 20:41:05 +03004470static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4471 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004472{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004473 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004474
4475#ifdef CONFIG_COMPAT
4476 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004477 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004478#endif
4479
Pavel Begunkov1400e692020-07-12 20:41:05 +03004480 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004481}
4482
Jens Axboebcda7ba2020-02-23 16:42:51 -07004483static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004484 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004485{
4486 struct io_sr_msg *sr = &req->sr_msg;
4487 struct io_buffer *kbuf;
4488
Jens Axboebcda7ba2020-02-23 16:42:51 -07004489 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4490 if (IS_ERR(kbuf))
4491 return kbuf;
4492
4493 sr->kbuf = kbuf;
4494 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004495 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004496}
4497
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004498static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4499{
4500 return io_put_kbuf(req, req->sr_msg.kbuf);
4501}
4502
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004503static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004504{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004505 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004506
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004507 if (!io_op_defs[req->opcode].needs_async_data)
4508 return 0;
4509 ret = io_recvmsg_copy_hdr(req, req->async_data);
4510 if (!ret)
4511 req->flags |= REQ_F_NEED_CLEANUP;
4512 return ret;
4513}
4514
4515static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4516{
4517 struct io_sr_msg *sr = &req->sr_msg;
4518
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004519 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4520 return -EINVAL;
4521
Jens Axboe3529d8c2019-12-19 18:24:38 -07004522 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004523 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004524 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004525 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004526
Jens Axboed8768362020-02-27 14:17:49 -07004527#ifdef CONFIG_COMPAT
4528 if (req->ctx->compat)
4529 sr->msg_flags |= MSG_CMSG_COMPAT;
4530#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004531 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004532}
4533
Pavel Begunkov889fca72021-02-10 00:03:09 +00004534static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004535{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004536 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004537 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004538 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004539 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004540 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004541 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004542
Florent Revestdba4a922020-12-04 12:36:04 +01004543 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004544 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004545 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004546
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004547 kmsg = req->async_data;
4548 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004549 ret = io_recvmsg_copy_hdr(req, &iomsg);
4550 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004551 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004552 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004553 }
4554
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004555 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004556 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004557 if (IS_ERR(kbuf))
4558 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004559 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004560 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4561 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004562 1, req->sr_msg.len);
4563 }
4564
4565 flags = req->sr_msg.msg_flags;
4566 if (flags & MSG_DONTWAIT)
4567 req->flags |= REQ_F_NOWAIT;
4568 else if (force_nonblock)
4569 flags |= MSG_DONTWAIT;
4570
4571 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4572 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004573 if (force_nonblock && ret == -EAGAIN)
4574 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004575 if (ret == -ERESTARTSYS)
4576 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004577
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004578 if (req->flags & REQ_F_BUFFER_SELECTED)
4579 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004580 /* fast path, check for non-NULL to avoid function call */
4581 if (kmsg->free_iov)
4582 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004583 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004584 if (ret < 0)
4585 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004586 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004587 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004588}
4589
Pavel Begunkov889fca72021-02-10 00:03:09 +00004590static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004591{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004592 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004593 struct io_sr_msg *sr = &req->sr_msg;
4594 struct msghdr msg;
4595 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004596 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004597 struct iovec iov;
4598 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004599 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004600 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004601
Florent Revestdba4a922020-12-04 12:36:04 +01004602 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004603 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004604 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004605
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004606 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004607 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004608 if (IS_ERR(kbuf))
4609 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004610 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004611 }
4612
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004613 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004614 if (unlikely(ret))
4615 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004616
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004617 msg.msg_name = NULL;
4618 msg.msg_control = NULL;
4619 msg.msg_controllen = 0;
4620 msg.msg_namelen = 0;
4621 msg.msg_iocb = NULL;
4622 msg.msg_flags = 0;
4623
4624 flags = req->sr_msg.msg_flags;
4625 if (flags & MSG_DONTWAIT)
4626 req->flags |= REQ_F_NOWAIT;
4627 else if (force_nonblock)
4628 flags |= MSG_DONTWAIT;
4629
4630 ret = sock_recvmsg(sock, &msg, flags);
4631 if (force_nonblock && ret == -EAGAIN)
4632 return -EAGAIN;
4633 if (ret == -ERESTARTSYS)
4634 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004635out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004636 if (req->flags & REQ_F_BUFFER_SELECTED)
4637 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004638 if (ret < 0)
4639 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004640 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004641 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004642}
4643
Jens Axboe3529d8c2019-12-19 18:24:38 -07004644static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004645{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004646 struct io_accept *accept = &req->accept;
4647
Jens Axboe14587a462020-09-05 11:36:08 -06004648 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004649 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004650 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004651 return -EINVAL;
4652
Jens Axboed55e5f52019-12-11 16:12:15 -07004653 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4654 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004655 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004656 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004657 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004658}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004659
Pavel Begunkov889fca72021-02-10 00:03:09 +00004660static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004661{
4662 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004663 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004664 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004665 int ret;
4666
Jiufei Xuee697dee2020-06-10 13:41:59 +08004667 if (req->file->f_flags & O_NONBLOCK)
4668 req->flags |= REQ_F_NOWAIT;
4669
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004670 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004671 accept->addr_len, accept->flags,
4672 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004673 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004674 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004675 if (ret < 0) {
4676 if (ret == -ERESTARTSYS)
4677 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004678 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004679 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004680 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004681 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004682}
4683
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004684static int io_connect_prep_async(struct io_kiocb *req)
4685{
4686 struct io_async_connect *io = req->async_data;
4687 struct io_connect *conn = &req->connect;
4688
4689 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4690}
4691
Jens Axboe3529d8c2019-12-19 18:24:38 -07004692static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004693{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004694 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004695
Jens Axboe14587a462020-09-05 11:36:08 -06004696 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004697 return -EINVAL;
4698 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4699 return -EINVAL;
4700
Jens Axboe3529d8c2019-12-19 18:24:38 -07004701 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4702 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004703 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004704}
4705
Pavel Begunkov889fca72021-02-10 00:03:09 +00004706static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004707{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004708 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004709 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004710 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004711 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004712
Jens Axboee8c2bc12020-08-15 18:44:09 -07004713 if (req->async_data) {
4714 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004715 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004716 ret = move_addr_to_kernel(req->connect.addr,
4717 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004718 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004719 if (ret)
4720 goto out;
4721 io = &__io;
4722 }
4723
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004724 file_flags = force_nonblock ? O_NONBLOCK : 0;
4725
Jens Axboee8c2bc12020-08-15 18:44:09 -07004726 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004727 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004728 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004729 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004730 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004731 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004732 ret = -ENOMEM;
4733 goto out;
4734 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004735 io = req->async_data;
4736 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004737 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004738 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004739 if (ret == -ERESTARTSYS)
4740 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004741out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004742 if (ret < 0)
4743 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004744 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004745 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004746}
YueHaibing469956e2020-03-04 15:53:52 +08004747#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004748#define IO_NETOP_FN(op) \
4749static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4750{ \
4751 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004752}
4753
Jens Axboe99a10082021-02-19 09:35:19 -07004754#define IO_NETOP_PREP(op) \
4755IO_NETOP_FN(op) \
4756static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4757{ \
4758 return -EOPNOTSUPP; \
4759} \
4760
4761#define IO_NETOP_PREP_ASYNC(op) \
4762IO_NETOP_PREP(op) \
4763static int io_##op##_prep_async(struct io_kiocb *req) \
4764{ \
4765 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004766}
4767
Jens Axboe99a10082021-02-19 09:35:19 -07004768IO_NETOP_PREP_ASYNC(sendmsg);
4769IO_NETOP_PREP_ASYNC(recvmsg);
4770IO_NETOP_PREP_ASYNC(connect);
4771IO_NETOP_PREP(accept);
4772IO_NETOP_FN(send);
4773IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004774#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004775
Jens Axboed7718a92020-02-14 22:23:12 -07004776struct io_poll_table {
4777 struct poll_table_struct pt;
4778 struct io_kiocb *req;
4779 int error;
4780};
4781
Jens Axboed7718a92020-02-14 22:23:12 -07004782static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4783 __poll_t mask, task_work_func_t func)
4784{
Jens Axboeaa96bf82020-04-03 11:26:26 -06004785 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004786
4787 /* for instances that support it check for an event match first: */
4788 if (mask && !(mask & poll->events))
4789 return 0;
4790
4791 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4792
4793 list_del_init(&poll->wait.entry);
4794
Jens Axboed7718a92020-02-14 22:23:12 -07004795 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00004796 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06004797 percpu_ref_get(&req->ctx->refs);
4798
Jens Axboed7718a92020-02-14 22:23:12 -07004799 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004800 * If this fails, then the task is exiting. When a task exits, the
4801 * work gets canceled, so just cancel this request as well instead
4802 * of executing it. We can't safely execute it anyway, as we may not
4803 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004804 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06004805 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004806 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004807 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00004808 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004809 }
Jens Axboed7718a92020-02-14 22:23:12 -07004810 return 1;
4811}
4812
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004813static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4814 __acquires(&req->ctx->completion_lock)
4815{
4816 struct io_ring_ctx *ctx = req->ctx;
4817
4818 if (!req->result && !READ_ONCE(poll->canceled)) {
4819 struct poll_table_struct pt = { ._key = poll->events };
4820
4821 req->result = vfs_poll(req->file, &pt) & poll->events;
4822 }
4823
4824 spin_lock_irq(&ctx->completion_lock);
4825 if (!req->result && !READ_ONCE(poll->canceled)) {
4826 add_wait_queue(poll->head, &poll->wait);
4827 return true;
4828 }
4829
4830 return false;
4831}
4832
Jens Axboed4e7cd32020-08-15 11:44:50 -07004833static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004834{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004835 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004836 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004837 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004838 return req->apoll->double_poll;
4839}
4840
4841static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4842{
4843 if (req->opcode == IORING_OP_POLL_ADD)
4844 return &req->poll;
4845 return &req->apoll->poll;
4846}
4847
4848static void io_poll_remove_double(struct io_kiocb *req)
4849{
4850 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004851
4852 lockdep_assert_held(&req->ctx->completion_lock);
4853
4854 if (poll && poll->head) {
4855 struct wait_queue_head *head = poll->head;
4856
4857 spin_lock(&head->lock);
4858 list_del_init(&poll->wait.entry);
4859 if (poll->wait.private)
4860 refcount_dec(&req->refs);
4861 poll->head = NULL;
4862 spin_unlock(&head->lock);
4863 }
4864}
4865
4866static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4867{
4868 struct io_ring_ctx *ctx = req->ctx;
4869
Jens Axboed4e7cd32020-08-15 11:44:50 -07004870 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004871 req->poll.done = true;
4872 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4873 io_commit_cqring(ctx);
4874}
4875
Jens Axboe18bceab2020-05-15 11:56:54 -06004876static void io_poll_task_func(struct callback_head *cb)
4877{
4878 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004879 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004880 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06004881
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004882 if (io_poll_rewait(req, &req->poll)) {
4883 spin_unlock_irq(&ctx->completion_lock);
4884 } else {
4885 hash_del(&req->hash_node);
4886 io_poll_complete(req, req->result, 0);
4887 spin_unlock_irq(&ctx->completion_lock);
4888
4889 nxt = io_put_req_find_next(req);
4890 io_cqring_ev_posted(ctx);
4891 if (nxt)
4892 __io_req_task_submit(nxt);
4893 }
4894
Jens Axboe6d816e02020-08-11 08:04:14 -06004895 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004896}
4897
4898static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4899 int sync, void *key)
4900{
4901 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004902 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004903 __poll_t mask = key_to_poll(key);
4904
4905 /* for instances that support it check for an event match first: */
4906 if (mask && !(mask & poll->events))
4907 return 0;
4908
Jens Axboe8706e042020-09-28 08:38:54 -06004909 list_del_init(&wait->entry);
4910
Jens Axboe807abcb2020-07-17 17:09:27 -06004911 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004912 bool done;
4913
Jens Axboe807abcb2020-07-17 17:09:27 -06004914 spin_lock(&poll->head->lock);
4915 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004916 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004917 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004918 /* make sure double remove sees this as being gone */
4919 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004920 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06004921 if (!done) {
4922 /* use wait func handler, so it matches the rq type */
4923 poll->wait.func(&poll->wait, mode, sync, key);
4924 }
Jens Axboe18bceab2020-05-15 11:56:54 -06004925 }
4926 refcount_dec(&req->refs);
4927 return 1;
4928}
4929
4930static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4931 wait_queue_func_t wake_func)
4932{
4933 poll->head = NULL;
4934 poll->done = false;
4935 poll->canceled = false;
4936 poll->events = events;
4937 INIT_LIST_HEAD(&poll->wait.entry);
4938 init_waitqueue_func_entry(&poll->wait, wake_func);
4939}
4940
4941static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004942 struct wait_queue_head *head,
4943 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004944{
4945 struct io_kiocb *req = pt->req;
4946
4947 /*
4948 * If poll->head is already set, it's because the file being polled
4949 * uses multiple waitqueues for poll handling (eg one for read, one
4950 * for write). Setup a separate io_poll_iocb if this happens.
4951 */
4952 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01004953 struct io_poll_iocb *poll_one = poll;
4954
Jens Axboe18bceab2020-05-15 11:56:54 -06004955 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004956 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004957 pt->error = -EINVAL;
4958 return;
4959 }
4960 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4961 if (!poll) {
4962 pt->error = -ENOMEM;
4963 return;
4964 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01004965 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06004966 refcount_inc(&req->refs);
4967 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004968 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004969 }
4970
4971 pt->error = 0;
4972 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004973
4974 if (poll->events & EPOLLEXCLUSIVE)
4975 add_wait_queue_exclusive(head, &poll->wait);
4976 else
4977 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004978}
4979
4980static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4981 struct poll_table_struct *p)
4982{
4983 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004984 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004985
Jens Axboe807abcb2020-07-17 17:09:27 -06004986 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004987}
4988
Jens Axboed7718a92020-02-14 22:23:12 -07004989static void io_async_task_func(struct callback_head *cb)
4990{
4991 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4992 struct async_poll *apoll = req->apoll;
4993 struct io_ring_ctx *ctx = req->ctx;
4994
4995 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4996
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004997 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004998 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004999 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005000 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005001 }
5002
Jens Axboe31067252020-05-17 17:43:31 -06005003 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005004 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005005 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005006
Jens Axboed4e7cd32020-08-15 11:44:50 -07005007 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005008 spin_unlock_irq(&ctx->completion_lock);
5009
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005010 if (!READ_ONCE(apoll->poll.canceled))
5011 __io_req_task_submit(req);
5012 else
5013 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005014
Jens Axboe6d816e02020-08-11 08:04:14 -06005015 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005016 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005017 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005018}
5019
5020static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5021 void *key)
5022{
5023 struct io_kiocb *req = wait->private;
5024 struct io_poll_iocb *poll = &req->apoll->poll;
5025
5026 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5027 key_to_poll(key));
5028
5029 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5030}
5031
5032static void io_poll_req_insert(struct io_kiocb *req)
5033{
5034 struct io_ring_ctx *ctx = req->ctx;
5035 struct hlist_head *list;
5036
5037 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5038 hlist_add_head(&req->hash_node, list);
5039}
5040
5041static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5042 struct io_poll_iocb *poll,
5043 struct io_poll_table *ipt, __poll_t mask,
5044 wait_queue_func_t wake_func)
5045 __acquires(&ctx->completion_lock)
5046{
5047 struct io_ring_ctx *ctx = req->ctx;
5048 bool cancel = false;
5049
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005050 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005051 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005052 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005053 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005054
5055 ipt->pt._key = mask;
5056 ipt->req = req;
5057 ipt->error = -EINVAL;
5058
Jens Axboed7718a92020-02-14 22:23:12 -07005059 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5060
5061 spin_lock_irq(&ctx->completion_lock);
5062 if (likely(poll->head)) {
5063 spin_lock(&poll->head->lock);
5064 if (unlikely(list_empty(&poll->wait.entry))) {
5065 if (ipt->error)
5066 cancel = true;
5067 ipt->error = 0;
5068 mask = 0;
5069 }
5070 if (mask || ipt->error)
5071 list_del_init(&poll->wait.entry);
5072 else if (cancel)
5073 WRITE_ONCE(poll->canceled, true);
5074 else if (!poll->done) /* actually waiting for an event */
5075 io_poll_req_insert(req);
5076 spin_unlock(&poll->head->lock);
5077 }
5078
5079 return mask;
5080}
5081
5082static bool io_arm_poll_handler(struct io_kiocb *req)
5083{
5084 const struct io_op_def *def = &io_op_defs[req->opcode];
5085 struct io_ring_ctx *ctx = req->ctx;
5086 struct async_poll *apoll;
5087 struct io_poll_table ipt;
5088 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005089 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005090
5091 if (!req->file || !file_can_poll(req->file))
5092 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005093 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005094 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005095 if (def->pollin)
5096 rw = READ;
5097 else if (def->pollout)
5098 rw = WRITE;
5099 else
5100 return false;
5101 /* if we can't nonblock try, then no point in arming a poll handler */
5102 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005103 return false;
5104
5105 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5106 if (unlikely(!apoll))
5107 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005108 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005109
5110 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005111 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005112
Nathan Chancellor8755d972020-03-02 16:01:19 -07005113 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005114 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005115 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005116 if (def->pollout)
5117 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005118
5119 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5120 if ((req->opcode == IORING_OP_RECVMSG) &&
5121 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5122 mask &= ~POLLIN;
5123
Jens Axboed7718a92020-02-14 22:23:12 -07005124 mask |= POLLERR | POLLPRI;
5125
5126 ipt.pt._qproc = io_async_queue_proc;
5127
5128 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5129 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005130 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005131 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005132 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005133 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005134 kfree(apoll);
5135 return false;
5136 }
5137 spin_unlock_irq(&ctx->completion_lock);
5138 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5139 apoll->poll.events);
5140 return true;
5141}
5142
5143static bool __io_poll_remove_one(struct io_kiocb *req,
5144 struct io_poll_iocb *poll)
5145{
Jens Axboeb41e9852020-02-17 09:52:41 -07005146 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005147
5148 spin_lock(&poll->head->lock);
5149 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005150 if (!list_empty(&poll->wait.entry)) {
5151 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005152 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005153 }
5154 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005155 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005156 return do_complete;
5157}
5158
5159static bool io_poll_remove_one(struct io_kiocb *req)
5160{
5161 bool do_complete;
5162
Jens Axboed4e7cd32020-08-15 11:44:50 -07005163 io_poll_remove_double(req);
5164
Jens Axboed7718a92020-02-14 22:23:12 -07005165 if (req->opcode == IORING_OP_POLL_ADD) {
5166 do_complete = __io_poll_remove_one(req, &req->poll);
5167 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005168 struct async_poll *apoll = req->apoll;
5169
Jens Axboed7718a92020-02-14 22:23:12 -07005170 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005171 do_complete = __io_poll_remove_one(req, &apoll->poll);
5172 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005173 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005174 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005175 kfree(apoll);
5176 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005177 }
5178
Jens Axboeb41e9852020-02-17 09:52:41 -07005179 if (do_complete) {
5180 io_cqring_fill_event(req, -ECANCELED);
5181 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005182 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005183 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005184 }
5185
5186 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005187}
5188
Jens Axboe76e1b642020-09-26 15:05:03 -06005189/*
5190 * Returns true if we found and killed one or more poll requests
5191 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005192static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5193 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005194{
Jens Axboe78076bb2019-12-04 19:56:40 -07005195 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005196 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005197 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005198
5199 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005200 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5201 struct hlist_head *list;
5202
5203 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005204 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005205 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005206 posted += io_poll_remove_one(req);
5207 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005208 }
5209 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005210
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005211 if (posted)
5212 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005213
5214 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005215}
5216
Jens Axboe47f46762019-11-09 17:43:02 -07005217static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5218{
Jens Axboe78076bb2019-12-04 19:56:40 -07005219 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005220 struct io_kiocb *req;
5221
Jens Axboe78076bb2019-12-04 19:56:40 -07005222 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5223 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005224 if (sqe_addr != req->user_data)
5225 continue;
5226 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005227 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005228 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005229 }
5230
5231 return -ENOENT;
5232}
5233
Jens Axboe3529d8c2019-12-19 18:24:38 -07005234static int io_poll_remove_prep(struct io_kiocb *req,
5235 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005236{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005237 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5238 return -EINVAL;
5239 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5240 sqe->poll_events)
5241 return -EINVAL;
5242
Pavel Begunkov018043b2020-10-27 23:17:18 +00005243 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005244 return 0;
5245}
5246
5247/*
5248 * Find a running poll command that matches one specified in sqe->addr,
5249 * and remove it if found.
5250 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005251static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005252{
5253 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005254 int ret;
5255
Jens Axboe221c5eb2019-01-17 09:41:58 -07005256 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005257 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005258 spin_unlock_irq(&ctx->completion_lock);
5259
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005260 if (ret < 0)
5261 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005262 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005263 return 0;
5264}
5265
Jens Axboe221c5eb2019-01-17 09:41:58 -07005266static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5267 void *key)
5268{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005269 struct io_kiocb *req = wait->private;
5270 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005271
Jens Axboed7718a92020-02-14 22:23:12 -07005272 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005273}
5274
Jens Axboe221c5eb2019-01-17 09:41:58 -07005275static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5276 struct poll_table_struct *p)
5277{
5278 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5279
Jens Axboee8c2bc12020-08-15 18:44:09 -07005280 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005281}
5282
Jens Axboe3529d8c2019-12-19 18:24:38 -07005283static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005284{
5285 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005286 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005287
5288 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5289 return -EINVAL;
5290 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5291 return -EINVAL;
5292
Jiufei Xue5769a352020-06-17 17:53:55 +08005293 events = READ_ONCE(sqe->poll32_events);
5294#ifdef __BIG_ENDIAN
5295 events = swahw32(events);
5296#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005297 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5298 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005299 return 0;
5300}
5301
Pavel Begunkov61e98202021-02-10 00:03:08 +00005302static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005303{
5304 struct io_poll_iocb *poll = &req->poll;
5305 struct io_ring_ctx *ctx = req->ctx;
5306 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005307 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005308
Jens Axboed7718a92020-02-14 22:23:12 -07005309 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005310
Jens Axboed7718a92020-02-14 22:23:12 -07005311 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5312 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005313
Jens Axboe8c838782019-03-12 15:48:16 -06005314 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005315 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005316 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005317 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005318 spin_unlock_irq(&ctx->completion_lock);
5319
Jens Axboe8c838782019-03-12 15:48:16 -06005320 if (mask) {
5321 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005322 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005323 }
Jens Axboe8c838782019-03-12 15:48:16 -06005324 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005325}
5326
Jens Axboe5262f562019-09-17 12:26:57 -06005327static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5328{
Jens Axboead8a48a2019-11-15 08:49:11 -07005329 struct io_timeout_data *data = container_of(timer,
5330 struct io_timeout_data, timer);
5331 struct io_kiocb *req = data->req;
5332 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005333 unsigned long flags;
5334
Jens Axboe5262f562019-09-17 12:26:57 -06005335 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005336 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005337 atomic_set(&req->ctx->cq_timeouts,
5338 atomic_read(&req->ctx->cq_timeouts) + 1);
5339
Jens Axboe78e19bb2019-11-06 15:21:34 -07005340 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005341 io_commit_cqring(ctx);
5342 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5343
5344 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005345 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005346 io_put_req(req);
5347 return HRTIMER_NORESTART;
5348}
5349
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005350static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5351 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005352{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005353 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005354 struct io_kiocb *req;
5355 int ret = -ENOENT;
5356
5357 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5358 if (user_data == req->user_data) {
5359 ret = 0;
5360 break;
5361 }
5362 }
5363
5364 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005365 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005366
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005367 io = req->async_data;
5368 ret = hrtimer_try_to_cancel(&io->timer);
5369 if (ret == -1)
5370 return ERR_PTR(-EALREADY);
5371 list_del_init(&req->timeout.list);
5372 return req;
5373}
5374
5375static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5376{
5377 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5378
5379 if (IS_ERR(req))
5380 return PTR_ERR(req);
5381
5382 req_set_fail_links(req);
5383 io_cqring_fill_event(req, -ECANCELED);
5384 io_put_req_deferred(req, 1);
5385 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005386}
5387
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005388static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5389 struct timespec64 *ts, enum hrtimer_mode mode)
5390{
5391 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5392 struct io_timeout_data *data;
5393
5394 if (IS_ERR(req))
5395 return PTR_ERR(req);
5396
5397 req->timeout.off = 0; /* noseq */
5398 data = req->async_data;
5399 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5400 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5401 data->timer.function = io_timeout_fn;
5402 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5403 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005404}
5405
Jens Axboe3529d8c2019-12-19 18:24:38 -07005406static int io_timeout_remove_prep(struct io_kiocb *req,
5407 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005408{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005409 struct io_timeout_rem *tr = &req->timeout_rem;
5410
Jens Axboeb29472e2019-12-17 18:50:29 -07005411 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5412 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005413 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5414 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005415 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005416 return -EINVAL;
5417
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005418 tr->addr = READ_ONCE(sqe->addr);
5419 tr->flags = READ_ONCE(sqe->timeout_flags);
5420 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5421 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5422 return -EINVAL;
5423 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5424 return -EFAULT;
5425 } else if (tr->flags) {
5426 /* timeout removal doesn't support flags */
5427 return -EINVAL;
5428 }
5429
Jens Axboeb29472e2019-12-17 18:50:29 -07005430 return 0;
5431}
5432
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005433static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5434{
5435 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5436 : HRTIMER_MODE_REL;
5437}
5438
Jens Axboe11365042019-10-16 09:08:32 -06005439/*
5440 * Remove or update an existing timeout command
5441 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005442static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005443{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005444 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005445 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005446 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005447
Jens Axboe11365042019-10-16 09:08:32 -06005448 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005449 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005450 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005451 else
5452 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5453 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005454
Jens Axboe47f46762019-11-09 17:43:02 -07005455 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005456 io_commit_cqring(ctx);
5457 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005458 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005459 if (ret < 0)
5460 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005461 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005462 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005463}
5464
Jens Axboe3529d8c2019-12-19 18:24:38 -07005465static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005466 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005467{
Jens Axboead8a48a2019-11-15 08:49:11 -07005468 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005469 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005470 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005471
Jens Axboead8a48a2019-11-15 08:49:11 -07005472 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005473 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005474 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005475 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005476 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005477 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005478 flags = READ_ONCE(sqe->timeout_flags);
5479 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005480 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005481
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005482 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005483
Jens Axboee8c2bc12020-08-15 18:44:09 -07005484 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005485 return -ENOMEM;
5486
Jens Axboee8c2bc12020-08-15 18:44:09 -07005487 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005488 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005489
5490 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005491 return -EFAULT;
5492
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005493 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005494 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5495 return 0;
5496}
5497
Pavel Begunkov61e98202021-02-10 00:03:08 +00005498static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005499{
Jens Axboead8a48a2019-11-15 08:49:11 -07005500 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005501 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005502 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005503 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005504
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005505 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005506
Jens Axboe5262f562019-09-17 12:26:57 -06005507 /*
5508 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005509 * timeout event to be satisfied. If it isn't set, then this is
5510 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005511 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005512 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005513 entry = ctx->timeout_list.prev;
5514 goto add;
5515 }
Jens Axboe5262f562019-09-17 12:26:57 -06005516
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005517 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5518 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005519
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005520 /* Update the last seq here in case io_flush_timeouts() hasn't.
5521 * This is safe because ->completion_lock is held, and submissions
5522 * and completions are never mixed in the same ->completion_lock section.
5523 */
5524 ctx->cq_last_tm_flush = tail;
5525
Jens Axboe5262f562019-09-17 12:26:57 -06005526 /*
5527 * Insertion sort, ensuring the first entry in the list is always
5528 * the one we need first.
5529 */
Jens Axboe5262f562019-09-17 12:26:57 -06005530 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005531 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5532 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005533
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005534 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005535 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005536 /* nxt.seq is behind @tail, otherwise would've been completed */
5537 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005538 break;
5539 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005540add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005541 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005542 data->timer.function = io_timeout_fn;
5543 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005544 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005545 return 0;
5546}
5547
Jens Axboe62755e32019-10-28 21:49:21 -06005548static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005549{
Jens Axboe62755e32019-10-28 21:49:21 -06005550 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005551
Jens Axboe62755e32019-10-28 21:49:21 -06005552 return req->user_data == (unsigned long) data;
5553}
5554
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005555static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005556{
Jens Axboe62755e32019-10-28 21:49:21 -06005557 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005558 int ret = 0;
5559
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005560 if (!tctx->io_wq)
5561 return -ENOENT;
5562
5563 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005564 switch (cancel_ret) {
5565 case IO_WQ_CANCEL_OK:
5566 ret = 0;
5567 break;
5568 case IO_WQ_CANCEL_RUNNING:
5569 ret = -EALREADY;
5570 break;
5571 case IO_WQ_CANCEL_NOTFOUND:
5572 ret = -ENOENT;
5573 break;
5574 }
5575
Jens Axboee977d6d2019-11-05 12:39:45 -07005576 return ret;
5577}
5578
Jens Axboe47f46762019-11-09 17:43:02 -07005579static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5580 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005581 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005582{
5583 unsigned long flags;
5584 int ret;
5585
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005586 ret = io_async_cancel_one(req->task->io_uring,
5587 (void *) (unsigned long) sqe_addr);
Jens Axboe47f46762019-11-09 17:43:02 -07005588 if (ret != -ENOENT) {
5589 spin_lock_irqsave(&ctx->completion_lock, flags);
5590 goto done;
5591 }
5592
5593 spin_lock_irqsave(&ctx->completion_lock, flags);
5594 ret = io_timeout_cancel(ctx, sqe_addr);
5595 if (ret != -ENOENT)
5596 goto done;
5597 ret = io_poll_cancel(ctx, sqe_addr);
5598done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005599 if (!ret)
5600 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005601 io_cqring_fill_event(req, ret);
5602 io_commit_cqring(ctx);
5603 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5604 io_cqring_ev_posted(ctx);
5605
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005606 if (ret < 0)
5607 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005608 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005609}
5610
Jens Axboe3529d8c2019-12-19 18:24:38 -07005611static int io_async_cancel_prep(struct io_kiocb *req,
5612 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005613{
Jens Axboefbf23842019-12-17 18:45:56 -07005614 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005615 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005616 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5617 return -EINVAL;
5618 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005619 return -EINVAL;
5620
Jens Axboefbf23842019-12-17 18:45:56 -07005621 req->cancel.addr = READ_ONCE(sqe->addr);
5622 return 0;
5623}
5624
Pavel Begunkov61e98202021-02-10 00:03:08 +00005625static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005626{
5627 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005628
Pavel Begunkov014db002020-03-03 21:33:12 +03005629 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005630 return 0;
5631}
5632
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005633static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005634 const struct io_uring_sqe *sqe)
5635{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005636 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5637 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005638 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5639 return -EINVAL;
5640 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005641 return -EINVAL;
5642
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005643 req->rsrc_update.offset = READ_ONCE(sqe->off);
5644 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5645 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005646 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005647 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005648 return 0;
5649}
5650
Pavel Begunkov889fca72021-02-10 00:03:09 +00005651static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005652{
5653 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005654 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005655 int ret;
5656
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005657 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005658 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005659
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005660 up.offset = req->rsrc_update.offset;
5661 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005662
5663 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005664 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005665 mutex_unlock(&ctx->uring_lock);
5666
5667 if (ret < 0)
5668 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005669 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005670 return 0;
5671}
5672
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005673static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005674{
Jens Axboed625c6e2019-12-17 19:53:05 -07005675 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005676 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005677 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005678 case IORING_OP_READV:
5679 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005680 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005681 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005682 case IORING_OP_WRITEV:
5683 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005684 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005685 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005686 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005687 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005688 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005689 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005690 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005691 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005692 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005693 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005694 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005695 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005696 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005697 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005698 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005699 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005700 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005701 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005702 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005703 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005704 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005705 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005706 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005707 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005708 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005709 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005710 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005711 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005712 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005713 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005714 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005715 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005716 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005717 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005718 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005719 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005720 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005721 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005722 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005723 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005724 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005725 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005726 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005727 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005728 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005729 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005730 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005731 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005732 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005733 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005734 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005735 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005736 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005737 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06005738 case IORING_OP_SHUTDOWN:
5739 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06005740 case IORING_OP_RENAMEAT:
5741 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06005742 case IORING_OP_UNLINKAT:
5743 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005744 }
5745
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005746 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5747 req->opcode);
5748 return-EINVAL;
5749}
5750
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005751static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005752{
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005753 switch (req->opcode) {
5754 case IORING_OP_READV:
5755 case IORING_OP_READ_FIXED:
5756 case IORING_OP_READ:
5757 return io_rw_prep_async(req, READ);
5758 case IORING_OP_WRITEV:
5759 case IORING_OP_WRITE_FIXED:
5760 case IORING_OP_WRITE:
5761 return io_rw_prep_async(req, WRITE);
5762 case IORING_OP_SENDMSG:
5763 case IORING_OP_SEND:
5764 return io_sendmsg_prep_async(req);
5765 case IORING_OP_RECVMSG:
5766 case IORING_OP_RECV:
5767 return io_recvmsg_prep_async(req);
5768 case IORING_OP_CONNECT:
5769 return io_connect_prep_async(req);
5770 }
5771 return 0;
5772}
5773
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005774static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005775{
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005776 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005777 return 0;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005778 /* some opcodes init it during the inital prep */
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005779 if (req->async_data)
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005780 return 0;
5781 if (__io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005782 return -EAGAIN;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005783 return io_req_prep_async(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005784}
5785
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005786static u32 io_get_sequence(struct io_kiocb *req)
5787{
5788 struct io_kiocb *pos;
5789 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005790 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005791
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005792 io_for_each_link(pos, req)
5793 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005794
5795 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5796 return total_submitted - nr_reqs;
5797}
5798
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005799static int io_req_defer(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005800{
5801 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005802 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005803 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005804 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005805
5806 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005807 if (likely(list_empty_careful(&ctx->defer_list) &&
5808 !(req->flags & REQ_F_IO_DRAIN)))
5809 return 0;
5810
5811 seq = io_get_sequence(req);
5812 /* Still a chance to pass the sequence check */
5813 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005814 return 0;
5815
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005816 ret = io_req_defer_prep(req);
5817 if (ret)
5818 return ret;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005819 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005820 de = kmalloc(sizeof(*de), GFP_KERNEL);
5821 if (!de)
5822 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005823
5824 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005825 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005826 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005827 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005828 io_queue_async_work(req);
5829 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005830 }
5831
5832 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005833 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005834 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005835 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005836 spin_unlock_irq(&ctx->completion_lock);
5837 return -EIOCBQUEUED;
5838}
Jens Axboeedafcce2019-01-09 09:16:05 -07005839
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005840static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005841{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005842 if (req->flags & REQ_F_BUFFER_SELECTED) {
5843 switch (req->opcode) {
5844 case IORING_OP_READV:
5845 case IORING_OP_READ_FIXED:
5846 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005847 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005848 break;
5849 case IORING_OP_RECVMSG:
5850 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005851 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005852 break;
5853 }
5854 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005855 }
5856
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005857 if (req->flags & REQ_F_NEED_CLEANUP) {
5858 switch (req->opcode) {
5859 case IORING_OP_READV:
5860 case IORING_OP_READ_FIXED:
5861 case IORING_OP_READ:
5862 case IORING_OP_WRITEV:
5863 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005864 case IORING_OP_WRITE: {
5865 struct io_async_rw *io = req->async_data;
5866 if (io->free_iovec)
5867 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005868 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005869 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005870 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005871 case IORING_OP_SENDMSG: {
5872 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005873
5874 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005875 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005876 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005877 case IORING_OP_SPLICE:
5878 case IORING_OP_TEE:
5879 io_put_file(req, req->splice.file_in,
5880 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5881 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005882 case IORING_OP_OPENAT:
5883 case IORING_OP_OPENAT2:
5884 if (req->open.filename)
5885 putname(req->open.filename);
5886 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06005887 case IORING_OP_RENAMEAT:
5888 putname(req->rename.oldpath);
5889 putname(req->rename.newpath);
5890 break;
Jens Axboe14a11432020-09-28 14:27:37 -06005891 case IORING_OP_UNLINKAT:
5892 putname(req->unlink.filename);
5893 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005894 }
5895 req->flags &= ~REQ_F_NEED_CLEANUP;
5896 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005897}
5898
Pavel Begunkov889fca72021-02-10 00:03:09 +00005899static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07005900{
Jens Axboeedafcce2019-01-09 09:16:05 -07005901 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07005902 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07005903 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07005904
Jens Axboe5730b272021-02-27 15:57:30 -07005905 if (req->work.personality) {
5906 const struct cred *new_creds;
5907
5908 if (!(issue_flags & IO_URING_F_NONBLOCK))
5909 mutex_lock(&ctx->uring_lock);
5910 new_creds = idr_find(&ctx->personality_idr, req->work.personality);
5911 if (!(issue_flags & IO_URING_F_NONBLOCK))
5912 mutex_unlock(&ctx->uring_lock);
5913 if (!new_creds)
5914 return -EINVAL;
5915 creds = override_creds(new_creds);
5916 }
5917
Jens Axboed625c6e2019-12-17 19:53:05 -07005918 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005919 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005920 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07005921 break;
5922 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005923 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005924 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005925 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005926 break;
5927 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07005928 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005929 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005930 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005931 break;
5932 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005933 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005934 break;
5935 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005936 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005937 break;
5938 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005939 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005940 break;
5941 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005942 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005943 break;
5944 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005945 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005946 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005947 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005948 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005949 break;
5950 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005951 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005952 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005953 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005954 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005955 break;
5956 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005957 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005958 break;
5959 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005960 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005961 break;
5962 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005963 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005964 break;
5965 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005966 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005967 break;
5968 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005969 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005970 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005971 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005972 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07005973 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005974 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005975 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005976 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005977 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005978 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07005979 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005980 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005981 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005982 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005983 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005984 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005985 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005986 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005987 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07005988 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005989 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005990 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07005991 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005992 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005993 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07005994 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005995 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005996 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005997 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005998 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005999 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006000 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006001 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006002 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006003 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006004 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006005 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006006 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006007 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006008 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006009 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006010 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006011 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006012 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006013 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006014 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006015 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006016 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006017 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006018 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006019 default:
6020 ret = -EINVAL;
6021 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006022 }
6023
Jens Axboe5730b272021-02-27 15:57:30 -07006024 if (creds)
6025 revert_creds(creds);
6026
Jens Axboe2b188cc2019-01-07 10:46:33 -07006027 if (ret)
6028 return ret;
6029
Jens Axboeb5325762020-05-19 21:20:27 -06006030 /* If the op doesn't have a file, we're not polling for it */
6031 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006032 const bool in_async = io_wq_current_is_worker();
6033
Jens Axboe11ba8202020-01-15 21:51:17 -07006034 /* workqueue context doesn't hold uring_lock, grab it now */
6035 if (in_async)
6036 mutex_lock(&ctx->uring_lock);
6037
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006038 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006039
6040 if (in_async)
6041 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006042 }
6043
6044 return 0;
6045}
6046
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006047static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006048{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006049 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006050 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006051 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006052
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006053 timeout = io_prep_linked_timeout(req);
6054 if (timeout)
6055 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006056
Jens Axboe4014d942021-01-19 15:53:54 -07006057 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006058 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006059
Jens Axboe561fb042019-10-24 07:25:42 -06006060 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006061 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006062 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006063 /*
6064 * We can get EAGAIN for polled IO even though we're
6065 * forcing a sync submission from here, since we can't
6066 * wait for request slots on the block side.
6067 */
6068 if (ret != -EAGAIN)
6069 break;
6070 cond_resched();
6071 } while (1);
6072 }
Jens Axboe31b51512019-01-18 22:56:34 -07006073
Pavel Begunkova3df76982021-02-18 22:32:52 +00006074 /* avoid locking problems by failing it from a clean context */
Jens Axboe561fb042019-10-24 07:25:42 -06006075 if (ret) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00006076 /* io-wq is going to take one down */
6077 refcount_inc(&req->refs);
6078 io_req_task_queue_fail(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006079 }
Jens Axboe31b51512019-01-18 22:56:34 -07006080}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006081
Jens Axboe65e19f52019-10-26 07:20:21 -06006082static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6083 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006084{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006085 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006086
Jens Axboe05f3fb32019-12-09 11:22:50 -07006087 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006088 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006089}
6090
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006091static struct file *io_file_get(struct io_submit_state *state,
6092 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006093{
6094 struct io_ring_ctx *ctx = req->ctx;
6095 struct file *file;
6096
6097 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006098 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006099 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006100 fd = array_index_nospec(fd, ctx->nr_user_files);
6101 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006102 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006103 } else {
6104 trace_io_uring_file_get(ctx, fd);
6105 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006106 }
6107
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006108 if (file && unlikely(file->f_op == &io_uring_fops))
6109 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006110 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006111}
6112
Jens Axboe2665abf2019-11-05 12:40:47 -07006113static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6114{
Jens Axboead8a48a2019-11-15 08:49:11 -07006115 struct io_timeout_data *data = container_of(timer,
6116 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006117 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006118 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006119 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006120
6121 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006122 prev = req->timeout.head;
6123 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006124
6125 /*
6126 * We don't expect the list to be empty, that will only happen if we
6127 * race with the completion of the linked work.
6128 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006129 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006130 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006131 else
6132 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006133 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6134
6135 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006136 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006137 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006138 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006139 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006140 io_req_complete_post(req, -ETIME, 0);
6141 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006142 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006143 return HRTIMER_NORESTART;
6144}
6145
Jens Axboe7271ef32020-08-10 09:55:22 -06006146static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006147{
Jens Axboe76a46e02019-11-10 23:34:16 -07006148 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006149 * If the back reference is NULL, then our linked request finished
6150 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006151 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006152 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006153 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006154
Jens Axboead8a48a2019-11-15 08:49:11 -07006155 data->timer.function = io_link_timeout_fn;
6156 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6157 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006158 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006159}
6160
6161static void io_queue_linked_timeout(struct io_kiocb *req)
6162{
6163 struct io_ring_ctx *ctx = req->ctx;
6164
6165 spin_lock_irq(&ctx->completion_lock);
6166 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006167 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006168
Jens Axboe2665abf2019-11-05 12:40:47 -07006169 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006170 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006171}
6172
Jens Axboead8a48a2019-11-15 08:49:11 -07006173static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006174{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006175 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006176
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006177 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6178 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006179 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006180
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006181 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006182 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006183 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006184 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006185}
6186
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006187static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006188{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006189 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006190 int ret;
6191
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006192 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006193
6194 /*
6195 * We async punt it if the file wasn't marked NOWAIT, or if the file
6196 * doesn't support non-blocking read/write attempts
6197 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006198 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006199 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006200 /*
6201 * Queued up for async execution, worker will release
6202 * submit reference when the iocb is actually submitted.
6203 */
6204 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006205 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006206 } else if (likely(!ret)) {
6207 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006208 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006209 struct io_ring_ctx *ctx = req->ctx;
6210 struct io_comp_state *cs = &ctx->submit_state.comp;
Jens Axboee65ef562019-03-12 10:16:44 -06006211
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006212 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006213 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006214 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006215 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006216 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006217 }
6218 } else {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006219 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006220 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006221 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006222 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006223 if (linked_timeout)
6224 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006225}
6226
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006227static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006228{
6229 int ret;
6230
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006231 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006232 if (ret) {
6233 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006234fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006235 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006236 io_put_req(req);
6237 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006238 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006239 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006240 ret = io_req_defer_prep(req);
6241 if (unlikely(ret))
6242 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07006243 io_queue_async_work(req);
6244 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006245 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006246 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006247}
6248
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006249/*
6250 * Check SQE restrictions (opcode and flags).
6251 *
6252 * Returns 'true' if SQE is allowed, 'false' otherwise.
6253 */
6254static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6255 struct io_kiocb *req,
6256 unsigned int sqe_flags)
6257{
6258 if (!ctx->restricted)
6259 return true;
6260
6261 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6262 return false;
6263
6264 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6265 ctx->restrictions.sqe_flags_required)
6266 return false;
6267
6268 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6269 ctx->restrictions.sqe_flags_required))
6270 return false;
6271
6272 return true;
6273}
6274
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006275static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006276 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006277{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006278 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006279 unsigned int sqe_flags;
Jens Axboe5730b272021-02-27 15:57:30 -07006280 int ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006281
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006282 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006283 /* same numerical values with corresponding REQ_F_*, safe to copy */
6284 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006285 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006286 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006287 req->file = NULL;
6288 req->ctx = ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006289 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006290 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006291 /* one is dropped after submission, the other at completion */
6292 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006293 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006294 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006295
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006296 /* enforce forwards compatibility on users */
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006297 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
6298 req->flags = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006299 return -EINVAL;
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006300 }
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006301
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006302 if (unlikely(req->opcode >= IORING_OP_LAST))
6303 return -EINVAL;
6304
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006305 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6306 return -EACCES;
6307
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006308 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6309 !io_op_defs[req->opcode].buffer_select)
6310 return -EOPNOTSUPP;
6311
Jens Axboe5730b272021-02-27 15:57:30 -07006312 req->work.list.next = NULL;
6313 req->work.flags = 0;
6314 req->work.personality = READ_ONCE(sqe->personality);
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006315 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006316
Jens Axboe27926b62020-10-28 09:33:23 -06006317 /*
6318 * Plug now if we have more than 1 IO left after this, and the target
6319 * is potentially a read/write to block based storage.
6320 */
6321 if (!state->plug_started && state->ios_left > 1 &&
6322 io_op_defs[req->opcode].plug) {
6323 blk_start_plug(&state->plug);
6324 state->plug_started = true;
6325 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006326
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006327 if (io_op_defs[req->opcode].needs_file) {
6328 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006329
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006330 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006331 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006332 ret = -EBADF;
6333 }
6334
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006335 state->ios_left--;
6336 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006337}
6338
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006339static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006340 const struct io_uring_sqe *sqe)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006341{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006342 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006343 int ret;
6344
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006345 ret = io_init_req(ctx, req, sqe);
6346 if (unlikely(ret)) {
6347fail_req:
6348 io_put_req(req);
6349 io_req_complete(req, ret);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006350 if (link->head) {
6351 /* fail even hard links since we don't submit */
Pavel Begunkovcf109602021-02-18 18:29:43 +00006352 link->head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006353 io_put_req(link->head);
6354 io_req_complete(link->head, -ECANCELED);
6355 link->head = NULL;
6356 }
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006357 return ret;
6358 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006359 ret = io_req_prep(req, sqe);
6360 if (unlikely(ret))
6361 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006362
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006363 /* don't need @sqe from now on */
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006364 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
6365 true, ctx->flags & IORING_SETUP_SQPOLL);
6366
Jens Axboe6c271ce2019-01-10 11:22:30 -07006367 /*
6368 * If we already have a head request, queue this one for async
6369 * submittal once the head completes. If we don't have a head but
6370 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6371 * submitted sync once the chain is complete. If none of those
6372 * conditions are true (normal request), then just queue it.
6373 */
6374 if (link->head) {
6375 struct io_kiocb *head = link->head;
6376
6377 /*
6378 * Taking sequential execution of a link, draining both sides
6379 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6380 * requests in the link. So, it drains the head and the
6381 * next after the link request. The last one is done via
6382 * drain_next flag to persist the effect across calls.
6383 */
6384 if (req->flags & REQ_F_IO_DRAIN) {
6385 head->flags |= REQ_F_IO_DRAIN;
6386 ctx->drain_next = 1;
6387 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006388 ret = io_req_defer_prep(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006389 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006390 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006391 trace_io_uring_link(ctx, req, head);
6392 link->last->link = req;
6393 link->last = req;
6394
6395 /* last request of a link, enqueue the link */
6396 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006397 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006398 link->head = NULL;
6399 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006400 } else {
6401 if (unlikely(ctx->drain_next)) {
6402 req->flags |= REQ_F_IO_DRAIN;
6403 ctx->drain_next = 0;
6404 }
6405 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006406 link->head = req;
6407 link->last = req;
6408 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006409 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006410 }
6411 }
6412
6413 return 0;
6414}
6415
6416/*
6417 * Batched submission is done, ensure local IO is flushed out.
6418 */
6419static void io_submit_state_end(struct io_submit_state *state,
6420 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006421{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006422 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006423 io_queue_sqe(state->link.head);
Jens Axboe3529d8c2019-12-19 18:24:38 -07006424 if (state->comp.nr)
Jens Axboe9e645e112019-05-10 16:07:28 -06006425 io_submit_flush_completions(&state->comp, ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006426 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006427 blk_finish_plug(&state->plug);
Jens Axboe75c6a032020-01-28 10:15:23 -07006428 io_state_file_put(state);
Jens Axboe9e645e112019-05-10 16:07:28 -06006429}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006430
Jens Axboe9e645e112019-05-10 16:07:28 -06006431/*
6432 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006433 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006434static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006435 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006436{
6437 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006438 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006439 /* set only head, no need to init link_last in advance */
6440 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006441}
6442
Jens Axboe193155c2020-02-22 23:22:19 -07006443static void io_commit_sqring(struct io_ring_ctx *ctx)
6444{
Jens Axboe75c6a032020-01-28 10:15:23 -07006445 struct io_rings *rings = ctx->rings;
6446
6447 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006448 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006449 * since once we write the new head, the application could
6450 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006451 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006452 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006453}
6454
Jens Axboe9e645e112019-05-10 16:07:28 -06006455/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006456 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006457 * that is mapped by userspace. This means that care needs to be taken to
6458 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006459 * being a good citizen. If members of the sqe are validated and then later
6460 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006461 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006462 */
6463static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006464{
6465 u32 *sq_array = ctx->sq_array;
6466 unsigned head;
6467
6468 /*
6469 * The cached sq head (or cq tail) serves two purposes:
6470 *
6471 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006472 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006473 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006474 * though the application is the one updating it.
6475 */
6476 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
6477 if (likely(head < ctx->sq_entries))
6478 return &ctx->sq_sqes[head];
6479
6480 /* drop invalid entries */
Pavel Begunkov711be032020-01-17 03:57:59 +03006481 ctx->cached_sq_dropped++;
6482 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
6483 return NULL;
6484}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006485
Jens Axboe0f212202020-09-13 13:09:39 -06006486static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006487{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006488 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006489
Jens Axboec4a2ed72019-11-21 21:01:26 -07006490 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006491 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006492 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006493 return -EBUSY;
6494 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006495
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006496 /* make sure SQ entry isn't read before tail */
6497 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006498
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006499 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6500 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006501
Jens Axboed8a6df12020-10-15 16:24:45 -06006502 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006503 refcount_add(nr, &current->usage);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006504 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006505
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006506 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006507 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006508 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006509
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006510 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006511 if (unlikely(!req)) {
6512 if (!submitted)
6513 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006514 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006515 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006516 sqe = io_get_sqe(ctx);
6517 if (unlikely(!sqe)) {
6518 kmem_cache_free(req_cachep, req);
6519 break;
6520 }
Jens Axboed3656342019-12-18 09:50:26 -07006521 /* will complete beyond this point, count as submitted */
6522 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006523 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006524 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006525 }
6526
Pavel Begunkov9466f432020-01-25 22:34:01 +03006527 if (unlikely(submitted != nr)) {
6528 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006529 struct io_uring_task *tctx = current->io_uring;
6530 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006531
Jens Axboed8a6df12020-10-15 16:24:45 -06006532 percpu_ref_put_many(&ctx->refs, unused);
6533 percpu_counter_sub(&tctx->inflight, unused);
6534 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006535 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006536
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006537 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006538 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6539 io_commit_sqring(ctx);
6540
Jens Axboe6c271ce2019-01-10 11:22:30 -07006541 return submitted;
6542}
6543
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006544static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6545{
6546 /* Tell userspace we may need a wakeup call */
6547 spin_lock_irq(&ctx->completion_lock);
6548 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6549 spin_unlock_irq(&ctx->completion_lock);
6550}
6551
6552static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6553{
6554 spin_lock_irq(&ctx->completion_lock);
6555 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6556 spin_unlock_irq(&ctx->completion_lock);
6557}
6558
Xiaoguang Wang08369242020-11-03 14:15:59 +08006559static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006560{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006561 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006562 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006563
Jens Axboec8d1ba52020-09-14 11:07:26 -06006564 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006565 /* if we're handling multiple rings, cap submit size for fairness */
6566 if (cap_entries && to_submit > 8)
6567 to_submit = 8;
6568
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006569 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6570 unsigned nr_events = 0;
6571
Xiaoguang Wang08369242020-11-03 14:15:59 +08006572 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006573 if (!list_empty(&ctx->iopoll_list))
6574 io_do_iopoll(ctx, &nr_events, 0);
6575
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006576 if (to_submit && !ctx->sqo_dead &&
6577 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006578 ret = io_submit_sqes(ctx, to_submit);
6579 mutex_unlock(&ctx->uring_lock);
6580 }
Jens Axboe90554202020-09-03 12:12:41 -06006581
6582 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6583 wake_up(&ctx->sqo_sq_wait);
6584
Xiaoguang Wang08369242020-11-03 14:15:59 +08006585 return ret;
6586}
6587
6588static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6589{
6590 struct io_ring_ctx *ctx;
6591 unsigned sq_thread_idle = 0;
6592
6593 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6594 if (sq_thread_idle < ctx->sq_thread_idle)
6595 sq_thread_idle = ctx->sq_thread_idle;
6596 }
6597
6598 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006599}
6600
Jens Axboe69fb2132020-09-14 11:16:23 -06006601static void io_sqd_init_new(struct io_sq_data *sqd)
6602{
6603 struct io_ring_ctx *ctx;
6604
6605 while (!list_empty(&sqd->ctx_new_list)) {
6606 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006607 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6608 complete(&ctx->sq_thread_comp);
6609 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006610
6611 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006612}
6613
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006614static bool io_sq_thread_should_stop(struct io_sq_data *sqd)
6615{
6616 return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
6617}
6618
6619static bool io_sq_thread_should_park(struct io_sq_data *sqd)
6620{
6621 return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
6622}
6623
6624static void io_sq_thread_parkme(struct io_sq_data *sqd)
6625{
6626 for (;;) {
6627 /*
6628 * TASK_PARKED is a special state; we must serialize against
6629 * possible pending wakeups to avoid store-store collisions on
6630 * task->state.
6631 *
6632 * Such a collision might possibly result in the task state
6633 * changin from TASK_PARKED and us failing the
6634 * wait_task_inactive() in kthread_park().
6635 */
6636 set_special_state(TASK_PARKED);
6637 if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state))
6638 break;
6639
6640 /*
6641 * Thread is going to call schedule(), do not preempt it,
6642 * or the caller of kthread_park() may spend more time in
6643 * wait_task_inactive().
6644 */
6645 preempt_disable();
6646 complete(&sqd->completion);
6647 schedule_preempt_disabled();
6648 preempt_enable();
6649 }
6650 __set_current_state(TASK_RUNNING);
6651}
6652
Jens Axboe6c271ce2019-01-10 11:22:30 -07006653static int io_sq_thread(void *data)
6654{
Jens Axboe69fb2132020-09-14 11:16:23 -06006655 struct io_sq_data *sqd = data;
6656 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006657 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006658 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08006659 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006660
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006661 sprintf(buf, "iou-sqp-%d", sqd->task_pid);
6662 set_task_comm(current, buf);
6663 sqd->thread = current;
6664 current->pf_io_worker = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006665
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006666 if (sqd->sq_cpu != -1)
6667 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
6668 else
6669 set_cpus_allowed_ptr(current, cpu_online_mask);
6670 current->flags |= PF_NO_SETAFFINITY;
6671
6672 complete(&sqd->completion);
6673
6674 wait_for_completion(&sqd->startup);
6675
6676 while (!io_sq_thread_should_stop(sqd)) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006677 int ret;
6678 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006679
6680 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006681 * Any changes to the sqd lists are synchronized through the
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006682 * thread parking. This synchronizes the thread vs users,
Jens Axboe69fb2132020-09-14 11:16:23 -06006683 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006684 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006685 if (io_sq_thread_should_park(sqd)) {
6686 io_sq_thread_parkme(sqd);
6687 continue;
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006688 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006689 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006690 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006691 timeout = jiffies + sqd->sq_thread_idle;
6692 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006693 if (fatal_signal_pending(current))
6694 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006695 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006696 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006697 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006698 ret = __io_sq_thread(ctx, cap_entries);
6699 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
6700 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006701 }
6702
Xiaoguang Wang08369242020-11-03 14:15:59 +08006703 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006704 io_run_task_work();
6705 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08006706 if (sqt_spin)
6707 timeout = jiffies + sqd->sq_thread_idle;
6708 continue;
6709 }
6710
Xiaoguang Wang08369242020-11-03 14:15:59 +08006711 needs_sched = true;
6712 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
6713 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6714 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6715 !list_empty_careful(&ctx->iopoll_list)) {
6716 needs_sched = false;
6717 break;
6718 }
6719 if (io_sqring_entries(ctx)) {
6720 needs_sched = false;
6721 break;
6722 }
6723 }
6724
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006725 if (needs_sched && !io_sq_thread_should_park(sqd)) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006726 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6727 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006728
Jens Axboe69fb2132020-09-14 11:16:23 -06006729 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06006730 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6731 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006732 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006733
6734 finish_wait(&sqd->wait, &wait);
6735 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006736 }
6737
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006738 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6739 io_uring_cancel_sqpoll(ctx);
6740
Jens Axboe4c6e2772020-07-01 11:29:10 -06006741 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006742
Jens Axboe86293972021-02-26 13:46:49 -07006743 if (io_sq_thread_should_park(sqd))
6744 io_sq_thread_parkme(sqd);
6745
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006746 /*
6747 * Clear thread under lock so that concurrent parks work correctly
6748 */
Jens Axboe86293972021-02-26 13:46:49 -07006749 complete(&sqd->completion);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006750 mutex_lock(&sqd->lock);
6751 sqd->thread = NULL;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07006752 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6753 ctx->sqo_exec = 1;
6754 io_ring_set_wakeup_flag(ctx);
6755 }
Jens Axboe06058632019-04-13 09:26:03 -06006756
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006757 complete(&sqd->exited);
Jens Axboee54945a2021-02-26 11:27:15 -07006758 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006759 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006760}
6761
Jens Axboebda52162019-09-24 13:47:15 -06006762struct io_wait_queue {
6763 struct wait_queue_entry wq;
6764 struct io_ring_ctx *ctx;
6765 unsigned to_wait;
6766 unsigned nr_timeouts;
6767};
6768
Pavel Begunkov6c503152021-01-04 20:36:36 +00006769static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006770{
6771 struct io_ring_ctx *ctx = iowq->ctx;
6772
6773 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006774 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006775 * started waiting. For timeouts, we always want to return to userspace,
6776 * regardless of event count.
6777 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00006778 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006779 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6780}
6781
6782static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6783 int wake_flags, void *key)
6784{
6785 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6786 wq);
6787
Pavel Begunkov6c503152021-01-04 20:36:36 +00006788 /*
6789 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6790 * the task, and the next invocation will do it.
6791 */
6792 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6793 return autoremove_wake_function(curr, mode, wake_flags, key);
6794 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006795}
6796
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006797static int io_run_task_work_sig(void)
6798{
6799 if (io_run_task_work())
6800 return 1;
6801 if (!signal_pending(current))
6802 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06006803 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
6804 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006805 return -EINTR;
6806}
6807
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006808/* when returns >0, the caller should retry */
6809static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
6810 struct io_wait_queue *iowq,
6811 signed long *timeout)
6812{
6813 int ret;
6814
6815 /* make sure we run task_work before checking for signals */
6816 ret = io_run_task_work_sig();
6817 if (ret || io_should_wake(iowq))
6818 return ret;
6819 /* let the caller flush overflows, retry */
6820 if (test_bit(0, &ctx->cq_check_overflow))
6821 return 1;
6822
6823 *timeout = schedule_timeout(*timeout);
6824 return !*timeout ? -ETIME : 1;
6825}
6826
Jens Axboe2b188cc2019-01-07 10:46:33 -07006827/*
6828 * Wait until events become available, if we don't already have some. The
6829 * application must reap them itself, as they reside on the shared cq ring.
6830 */
6831static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08006832 const sigset_t __user *sig, size_t sigsz,
6833 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006834{
Jens Axboebda52162019-09-24 13:47:15 -06006835 struct io_wait_queue iowq = {
6836 .wq = {
6837 .private = current,
6838 .func = io_wake_function,
6839 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6840 },
6841 .ctx = ctx,
6842 .to_wait = min_events,
6843 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006844 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006845 signed long timeout = MAX_SCHEDULE_TIMEOUT;
6846 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006847
Jens Axboeb41e9852020-02-17 09:52:41 -07006848 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006849 io_cqring_overflow_flush(ctx, false, NULL, NULL);
6850 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07006851 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006852 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006853 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006854 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006855
6856 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006857#ifdef CONFIG_COMPAT
6858 if (in_compat_syscall())
6859 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006860 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006861 else
6862#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006863 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006864
Jens Axboe2b188cc2019-01-07 10:46:33 -07006865 if (ret)
6866 return ret;
6867 }
6868
Hao Xuc73ebb62020-11-03 10:54:37 +08006869 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006870 struct timespec64 ts;
6871
Hao Xuc73ebb62020-11-03 10:54:37 +08006872 if (get_timespec64(&ts, uts))
6873 return -EFAULT;
6874 timeout = timespec64_to_jiffies(&ts);
6875 }
6876
Jens Axboebda52162019-09-24 13:47:15 -06006877 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006878 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006879 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006880 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06006881 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6882 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006883 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
6884 finish_wait(&ctx->wait, &iowq.wq);
6885 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06006886
Jens Axboeb7db41c2020-07-04 08:55:50 -06006887 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006888
Hristo Venev75b28af2019-08-26 17:23:46 +00006889 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006890}
6891
Jens Axboe6b063142019-01-10 22:13:58 -07006892static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6893{
6894#if defined(CONFIG_UNIX)
6895 if (ctx->ring_sock) {
6896 struct sock *sock = ctx->ring_sock->sk;
6897 struct sk_buff *skb;
6898
6899 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6900 kfree_skb(skb);
6901 }
6902#else
6903 int i;
6904
Jens Axboe65e19f52019-10-26 07:20:21 -06006905 for (i = 0; i < ctx->nr_user_files; i++) {
6906 struct file *file;
6907
6908 file = io_file_from_index(ctx, i);
6909 if (file)
6910 fput(file);
6911 }
Jens Axboe6b063142019-01-10 22:13:58 -07006912#endif
6913}
6914
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00006915static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006916{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006917 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006918
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006919 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006920 complete(&data->done);
6921}
6922
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006923static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
Pavel Begunkov1642b442020-12-30 21:34:14 +00006924{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006925 spin_lock_bh(&ctx->rsrc_ref_lock);
Pavel Begunkov1642b442020-12-30 21:34:14 +00006926}
6927
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006928static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
Jens Axboe6b063142019-01-10 22:13:58 -07006929{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006930 spin_unlock_bh(&ctx->rsrc_ref_lock);
6931}
6932
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00006933static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
6934 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006935 struct fixed_rsrc_ref_node *ref_node)
Jens Axboe6b063142019-01-10 22:13:58 -07006936{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006937 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006938 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00006939 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006940 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006941 percpu_ref_get(&rsrc_data->refs);
Jens Axboe6b063142019-01-10 22:13:58 -07006942}
6943
Hao Xu8bad28d2021-02-19 17:19:36 +08006944static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data)
Jens Axboe6b063142019-01-10 22:13:58 -07006945{
Hao Xu8bad28d2021-02-19 17:19:36 +08006946 struct fixed_rsrc_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006947
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006948 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00006949 ref_node = data->node;
Pavel Begunkove6cb0072021-02-20 18:03:47 +00006950 data->node = NULL;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006951 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006952 if (ref_node)
6953 percpu_ref_kill(&ref_node->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08006954}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006955
Hao Xu8bad28d2021-02-19 17:19:36 +08006956static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
6957 struct io_ring_ctx *ctx,
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006958 void (*rsrc_put)(struct io_ring_ctx *ctx,
6959 struct io_rsrc_put *prsrc))
Hao Xu8bad28d2021-02-19 17:19:36 +08006960{
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006961 struct fixed_rsrc_ref_node *backup_node;
Hao Xu8bad28d2021-02-19 17:19:36 +08006962 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006963
Hao Xu8bad28d2021-02-19 17:19:36 +08006964 if (data->quiesce)
6965 return -ENXIO;
6966
6967 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00006968 do {
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006969 ret = -ENOMEM;
6970 backup_node = alloc_fixed_rsrc_ref_node(ctx);
6971 if (!backup_node)
6972 break;
6973 backup_node->rsrc_data = data;
6974 backup_node->rsrc_put = rsrc_put;
6975
Hao Xu8bad28d2021-02-19 17:19:36 +08006976 io_sqe_rsrc_kill_node(ctx, data);
6977 percpu_ref_kill(&data->refs);
6978 flush_delayed_work(&ctx->rsrc_put_work);
6979
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00006980 ret = wait_for_completion_interruptible(&data->done);
6981 if (!ret)
6982 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006983
Jens Axboecb5e1b82021-02-25 07:37:35 -07006984 percpu_ref_resurrect(&data->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08006985 io_sqe_rsrc_set_node(ctx, data, backup_node);
6986 backup_node = NULL;
Jens Axboecb5e1b82021-02-25 07:37:35 -07006987 reinit_completion(&data->done);
Hao Xu8bad28d2021-02-19 17:19:36 +08006988 mutex_unlock(&ctx->uring_lock);
6989 ret = io_run_task_work_sig();
6990 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00006991 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08006992 data->quiesce = false;
6993
6994 if (backup_node)
6995 destroy_fixed_rsrc_ref_node(backup_node);
6996 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00006997}
6998
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00006999static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7000{
7001 struct fixed_rsrc_data *data;
7002
7003 data = kzalloc(sizeof(*data), GFP_KERNEL);
7004 if (!data)
7005 return NULL;
7006
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007007 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007008 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7009 kfree(data);
7010 return NULL;
7011 }
7012 data->ctx = ctx;
7013 init_completion(&data->done);
7014 return data;
7015}
7016
7017static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7018{
7019 percpu_ref_exit(&data->refs);
7020 kfree(data->table);
7021 kfree(data);
7022}
7023
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007024static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7025{
7026 struct fixed_rsrc_data *data = ctx->file_data;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007027 unsigned nr_tables, i;
7028 int ret;
7029
Hao Xu8bad28d2021-02-19 17:19:36 +08007030 /*
7031 * percpu_ref_is_dying() is to stop parallel files unregister
7032 * Since we possibly drop uring lock later in this function to
7033 * run task work.
7034 */
7035 if (!data || percpu_ref_is_dying(&data->refs))
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007036 return -ENXIO;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007037 ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put);
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007038 if (ret)
7039 return ret;
7040
Jens Axboe6b063142019-01-10 22:13:58 -07007041 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007042 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7043 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007044 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007045 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007046 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007047 ctx->nr_user_files = 0;
7048 return 0;
7049}
7050
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007051static void io_sq_thread_unpark(struct io_sq_data *sqd)
7052 __releases(&sqd->lock)
7053{
7054 if (!sqd->thread)
7055 return;
7056 if (sqd->thread == current)
7057 return;
7058 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7059 wake_up_state(sqd->thread, TASK_PARKED);
7060 mutex_unlock(&sqd->lock);
7061}
7062
7063static bool io_sq_thread_park(struct io_sq_data *sqd)
7064 __acquires(&sqd->lock)
7065{
7066 if (sqd->thread == current)
7067 return true;
7068 mutex_lock(&sqd->lock);
7069 if (!sqd->thread) {
7070 mutex_unlock(&sqd->lock);
7071 return false;
7072 }
7073 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7074 wake_up_process(sqd->thread);
7075 wait_for_completion(&sqd->completion);
7076 return true;
7077}
7078
7079static void io_sq_thread_stop(struct io_sq_data *sqd)
7080{
Jens Axboee54945a2021-02-26 11:27:15 -07007081 if (test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state))
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007082 return;
Jens Axboee54945a2021-02-26 11:27:15 -07007083 mutex_lock(&sqd->lock);
7084 if (sqd->thread) {
7085 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7086 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state));
7087 wake_up_process(sqd->thread);
7088 mutex_unlock(&sqd->lock);
7089 wait_for_completion(&sqd->exited);
7090 WARN_ON_ONCE(sqd->thread);
7091 } else {
7092 mutex_unlock(&sqd->lock);
7093 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007094}
7095
Jens Axboe534ca6d2020-09-02 13:52:19 -06007096static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007097{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007098 if (refcount_dec_and_test(&sqd->refs)) {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007099 io_sq_thread_stop(sqd);
7100 kfree(sqd);
7101 }
7102}
7103
7104static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7105{
7106 struct io_sq_data *sqd = ctx->sq_data;
7107
7108 if (sqd) {
Jens Axboeeb858902021-02-25 10:13:29 -07007109 complete(&sqd->startup);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007110 if (sqd->thread) {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007111 wait_for_completion(&ctx->sq_thread_comp);
7112 io_sq_thread_park(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007113 }
7114
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007115 mutex_lock(&sqd->ctx_lock);
7116 list_del(&ctx->sqd_list);
7117 io_sqd_update_thread_idle(sqd);
7118 mutex_unlock(&sqd->ctx_lock);
7119
7120 if (sqd->thread)
7121 io_sq_thread_unpark(sqd);
7122
7123 io_put_sq_data(sqd);
7124 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007125 }
7126}
7127
Jens Axboeaa061652020-09-02 14:50:27 -06007128static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7129{
7130 struct io_ring_ctx *ctx_attach;
7131 struct io_sq_data *sqd;
7132 struct fd f;
7133
7134 f = fdget(p->wq_fd);
7135 if (!f.file)
7136 return ERR_PTR(-ENXIO);
7137 if (f.file->f_op != &io_uring_fops) {
7138 fdput(f);
7139 return ERR_PTR(-EINVAL);
7140 }
7141
7142 ctx_attach = f.file->private_data;
7143 sqd = ctx_attach->sq_data;
7144 if (!sqd) {
7145 fdput(f);
7146 return ERR_PTR(-EINVAL);
7147 }
7148
7149 refcount_inc(&sqd->refs);
7150 fdput(f);
7151 return sqd;
7152}
7153
Jens Axboe534ca6d2020-09-02 13:52:19 -06007154static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7155{
7156 struct io_sq_data *sqd;
7157
Jens Axboeaa061652020-09-02 14:50:27 -06007158 if (p->flags & IORING_SETUP_ATTACH_WQ)
7159 return io_attach_sq_data(p);
7160
Jens Axboe534ca6d2020-09-02 13:52:19 -06007161 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7162 if (!sqd)
7163 return ERR_PTR(-ENOMEM);
7164
7165 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007166 INIT_LIST_HEAD(&sqd->ctx_list);
7167 INIT_LIST_HEAD(&sqd->ctx_new_list);
7168 mutex_init(&sqd->ctx_lock);
7169 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007170 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007171 init_completion(&sqd->startup);
7172 init_completion(&sqd->completion);
7173 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007174 return sqd;
7175}
7176
Jens Axboe6b063142019-01-10 22:13:58 -07007177#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007178/*
7179 * Ensure the UNIX gc is aware of our file set, so we are certain that
7180 * the io_uring can be safely unregistered on process exit, even if we have
7181 * loops in the file referencing.
7182 */
7183static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7184{
7185 struct sock *sk = ctx->ring_sock->sk;
7186 struct scm_fp_list *fpl;
7187 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007188 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007189
Jens Axboe6b063142019-01-10 22:13:58 -07007190 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7191 if (!fpl)
7192 return -ENOMEM;
7193
7194 skb = alloc_skb(0, GFP_KERNEL);
7195 if (!skb) {
7196 kfree(fpl);
7197 return -ENOMEM;
7198 }
7199
7200 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007201
Jens Axboe08a45172019-10-03 08:11:03 -06007202 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007203 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007204 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007205 struct file *file = io_file_from_index(ctx, i + offset);
7206
7207 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007208 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007209 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007210 unix_inflight(fpl->user, fpl->fp[nr_files]);
7211 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007212 }
7213
Jens Axboe08a45172019-10-03 08:11:03 -06007214 if (nr_files) {
7215 fpl->max = SCM_MAX_FD;
7216 fpl->count = nr_files;
7217 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007218 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007219 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7220 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007221
Jens Axboe08a45172019-10-03 08:11:03 -06007222 for (i = 0; i < nr_files; i++)
7223 fput(fpl->fp[i]);
7224 } else {
7225 kfree_skb(skb);
7226 kfree(fpl);
7227 }
Jens Axboe6b063142019-01-10 22:13:58 -07007228
7229 return 0;
7230}
7231
7232/*
7233 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7234 * causes regular reference counting to break down. We rely on the UNIX
7235 * garbage collection to take care of this problem for us.
7236 */
7237static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7238{
7239 unsigned left, total;
7240 int ret = 0;
7241
7242 total = 0;
7243 left = ctx->nr_user_files;
7244 while (left) {
7245 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007246
7247 ret = __io_sqe_files_scm(ctx, this_files, total);
7248 if (ret)
7249 break;
7250 left -= this_files;
7251 total += this_files;
7252 }
7253
7254 if (!ret)
7255 return 0;
7256
7257 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007258 struct file *file = io_file_from_index(ctx, total);
7259
7260 if (file)
7261 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007262 total++;
7263 }
7264
7265 return ret;
7266}
7267#else
7268static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7269{
7270 return 0;
7271}
7272#endif
7273
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007274static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007275 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007276{
7277 int i;
7278
7279 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007280 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007281 unsigned this_files;
7282
7283 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7284 table->files = kcalloc(this_files, sizeof(struct file *),
7285 GFP_KERNEL);
7286 if (!table->files)
7287 break;
7288 nr_files -= this_files;
7289 }
7290
7291 if (i == nr_tables)
7292 return 0;
7293
7294 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007295 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007296 kfree(table->files);
7297 }
7298 return 1;
7299}
7300
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007301static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007302{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007303 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007304#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007305 struct sock *sock = ctx->ring_sock->sk;
7306 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7307 struct sk_buff *skb;
7308 int i;
7309
7310 __skb_queue_head_init(&list);
7311
7312 /*
7313 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7314 * remove this entry and rearrange the file array.
7315 */
7316 skb = skb_dequeue(head);
7317 while (skb) {
7318 struct scm_fp_list *fp;
7319
7320 fp = UNIXCB(skb).fp;
7321 for (i = 0; i < fp->count; i++) {
7322 int left;
7323
7324 if (fp->fp[i] != file)
7325 continue;
7326
7327 unix_notinflight(fp->user, fp->fp[i]);
7328 left = fp->count - 1 - i;
7329 if (left) {
7330 memmove(&fp->fp[i], &fp->fp[i + 1],
7331 left * sizeof(struct file *));
7332 }
7333 fp->count--;
7334 if (!fp->count) {
7335 kfree_skb(skb);
7336 skb = NULL;
7337 } else {
7338 __skb_queue_tail(&list, skb);
7339 }
7340 fput(file);
7341 file = NULL;
7342 break;
7343 }
7344
7345 if (!file)
7346 break;
7347
7348 __skb_queue_tail(&list, skb);
7349
7350 skb = skb_dequeue(head);
7351 }
7352
7353 if (skb_peek(&list)) {
7354 spin_lock_irq(&head->lock);
7355 while ((skb = __skb_dequeue(&list)) != NULL)
7356 __skb_queue_tail(head, skb);
7357 spin_unlock_irq(&head->lock);
7358 }
7359#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007360 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007361#endif
7362}
7363
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007364static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007365{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007366 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7367 struct io_ring_ctx *ctx = rsrc_data->ctx;
7368 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007369
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007370 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7371 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007372 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007373 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007374 }
7375
Xiaoguang Wang05589552020-03-31 14:05:18 +08007376 percpu_ref_exit(&ref_node->refs);
7377 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007378 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007379}
7380
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007381static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007382{
7383 struct io_ring_ctx *ctx;
7384 struct llist_node *node;
7385
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007386 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7387 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007388
7389 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007390 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007391 struct llist_node *next = node->next;
7392
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007393 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7394 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007395 node = next;
7396 }
7397}
7398
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007399static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7400 unsigned i)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007401{
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007402 struct fixed_rsrc_table *table;
7403
7404 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7405 return &table->files[i & IORING_FILE_TABLE_MASK];
7406}
7407
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007408static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007409{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007410 struct fixed_rsrc_ref_node *ref_node;
7411 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007412 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007413 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007414 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007415
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007416 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7417 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007418 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007419
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007420 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007421 ref_node->done = true;
7422
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007423 while (!list_empty(&ctx->rsrc_ref_list)) {
7424 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007425 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007426 /* recycle ref nodes in order */
7427 if (!ref_node->done)
7428 break;
7429 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007430 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007431 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007432 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007433
7434 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007435 delay = 0;
7436
Jens Axboe4a38aed22020-05-14 17:21:15 -06007437 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007438 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007439 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007440 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007441}
7442
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007443static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007444 struct io_ring_ctx *ctx)
7445{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007446 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007447
7448 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7449 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007450 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007451
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007452 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007453 0, GFP_KERNEL)) {
7454 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007455 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007456 }
7457 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007458 INIT_LIST_HEAD(&ref_node->rsrc_list);
Pavel Begunkove2978222020-11-18 14:56:26 +00007459 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007460 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007461}
7462
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007463static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7464 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007465{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007466 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007467 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007468}
7469
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007470static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007471{
7472 percpu_ref_exit(&ref_node->refs);
7473 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007474}
7475
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007476
Jens Axboe05f3fb32019-12-09 11:22:50 -07007477static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7478 unsigned nr_args)
7479{
7480 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007481 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007482 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007483 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007484 struct fixed_rsrc_ref_node *ref_node;
7485 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007486
7487 if (ctx->file_data)
7488 return -EBUSY;
7489 if (!nr_args)
7490 return -EINVAL;
7491 if (nr_args > IORING_MAX_FIXED_FILES)
7492 return -EMFILE;
7493
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007494 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007495 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007496 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007497 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007498
7499 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007500 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007501 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007502 if (!file_data->table)
7503 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007504
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007505 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Jens Axboe05f3fb32019-12-09 11:22:50 -07007506 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007507
Jens Axboe05f3fb32019-12-09 11:22:50 -07007508 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007509 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7510 ret = -EFAULT;
7511 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007512 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007513 /* allow sparse sets */
7514 if (fd == -1)
7515 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007516
Jens Axboe05f3fb32019-12-09 11:22:50 -07007517 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007518 ret = -EBADF;
7519 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007520 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007521
7522 /*
7523 * Don't allow io_uring instances to be registered. If UNIX
7524 * isn't enabled, then this causes a reference cycle and this
7525 * instance can never get freed. If UNIX is enabled we'll
7526 * handle it just fine, but there's still no point in allowing
7527 * a ring fd as it doesn't support regular read/write anyway.
7528 */
7529 if (file->f_op == &io_uring_fops) {
7530 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007531 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007532 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007533 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007534 }
7535
Jens Axboe05f3fb32019-12-09 11:22:50 -07007536 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007537 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007538 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007539 return ret;
7540 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007541
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007542 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007543 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007544 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007545 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007546 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007547 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007548
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007549 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007550 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007551out_fput:
7552 for (i = 0; i < ctx->nr_user_files; i++) {
7553 file = io_file_from_index(ctx, i);
7554 if (file)
7555 fput(file);
7556 }
7557 for (i = 0; i < nr_tables; i++)
7558 kfree(file_data->table[i].files);
7559 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007560out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007561 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007562 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007563 return ret;
7564}
7565
Jens Axboec3a31e62019-10-03 13:59:56 -06007566static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7567 int index)
7568{
7569#if defined(CONFIG_UNIX)
7570 struct sock *sock = ctx->ring_sock->sk;
7571 struct sk_buff_head *head = &sock->sk_receive_queue;
7572 struct sk_buff *skb;
7573
7574 /*
7575 * See if we can merge this file into an existing skb SCM_RIGHTS
7576 * file set. If there's no room, fall back to allocating a new skb
7577 * and filling it in.
7578 */
7579 spin_lock_irq(&head->lock);
7580 skb = skb_peek(head);
7581 if (skb) {
7582 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7583
7584 if (fpl->count < SCM_MAX_FD) {
7585 __skb_unlink(skb, head);
7586 spin_unlock_irq(&head->lock);
7587 fpl->fp[fpl->count] = get_file(file);
7588 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7589 fpl->count++;
7590 spin_lock_irq(&head->lock);
7591 __skb_queue_head(head, skb);
7592 } else {
7593 skb = NULL;
7594 }
7595 }
7596 spin_unlock_irq(&head->lock);
7597
7598 if (skb) {
7599 fput(file);
7600 return 0;
7601 }
7602
7603 return __io_sqe_files_scm(ctx, 1, index);
7604#else
7605 return 0;
7606#endif
7607}
7608
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007609static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007610{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007611 struct io_rsrc_put *prsrc;
7612 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007613
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007614 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7615 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007616 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007617
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007618 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007619 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007620
Hillf Dantona5318d32020-03-23 17:47:15 +08007621 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007622}
7623
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007624static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7625 struct file *file)
7626{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007627 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007628}
7629
Jens Axboe05f3fb32019-12-09 11:22:50 -07007630static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007631 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007632 unsigned nr_args)
7633{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007634 struct fixed_rsrc_data *data = ctx->file_data;
7635 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007636 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007637 __s32 __user *fds;
7638 int fd, i, err;
7639 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007640 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007641
Jens Axboe05f3fb32019-12-09 11:22:50 -07007642 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007643 return -EOVERFLOW;
7644 if (done > ctx->nr_user_files)
7645 return -EINVAL;
7646
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007647 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007648 if (!ref_node)
7649 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007650 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007651
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007652 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007653 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007654 err = 0;
7655 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7656 err = -EFAULT;
7657 break;
7658 }
noah4e0377a2021-01-26 15:23:28 -05007659 if (fd == IORING_REGISTER_FILES_SKIP)
7660 continue;
7661
Pavel Begunkov67973b92021-01-26 13:51:09 +00007662 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007663 file_slot = io_fixed_file_slot(ctx->file_data, i);
7664
7665 if (*file_slot) {
7666 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007667 if (err)
7668 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007669 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007670 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007671 }
7672 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007673 file = fget(fd);
7674 if (!file) {
7675 err = -EBADF;
7676 break;
7677 }
7678 /*
7679 * Don't allow io_uring instances to be registered. If
7680 * UNIX isn't enabled, then this causes a reference
7681 * cycle and this instance can never get freed. If UNIX
7682 * is enabled we'll handle it just fine, but there's
7683 * still no point in allowing a ring fd as it doesn't
7684 * support regular read/write anyway.
7685 */
7686 if (file->f_op == &io_uring_fops) {
7687 fput(file);
7688 err = -EBADF;
7689 break;
7690 }
Jens Axboee68a3ff2021-02-11 07:45:08 -07007691 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007692 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007693 if (err) {
Jens Axboee68a3ff2021-02-11 07:45:08 -07007694 *file_slot = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007695 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007696 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007697 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007698 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007699 }
7700
Xiaoguang Wang05589552020-03-31 14:05:18 +08007701 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007702 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007703 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007704 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007705 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007706
7707 return done ? done : err;
7708}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007709
Jens Axboe05f3fb32019-12-09 11:22:50 -07007710static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7711 unsigned nr_args)
7712{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007713 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007714
7715 if (!ctx->file_data)
7716 return -ENXIO;
7717 if (!nr_args)
7718 return -EINVAL;
7719 if (copy_from_user(&up, arg, sizeof(up)))
7720 return -EFAULT;
7721 if (up.resv)
7722 return -EINVAL;
7723
7724 return __io_sqe_files_update(ctx, &up, nr_args);
7725}
Jens Axboec3a31e62019-10-03 13:59:56 -06007726
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007727static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007728{
7729 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7730
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007731 req = io_put_req_find_next(req);
7732 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07007733}
7734
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007735static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx)
Pavel Begunkov24369c22020-01-28 03:15:48 +03007736{
Jens Axboee9418942021-02-19 12:33:30 -07007737 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007738 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007739 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007740
Jens Axboee9418942021-02-19 12:33:30 -07007741 hash = ctx->hash_map;
7742 if (!hash) {
7743 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
7744 if (!hash)
7745 return ERR_PTR(-ENOMEM);
7746 refcount_set(&hash->refs, 1);
7747 init_waitqueue_head(&hash->wait);
7748 ctx->hash_map = hash;
7749 }
7750
7751 data.hash = hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007752 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007753 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007754
Jens Axboed25e3a32021-02-16 11:41:41 -07007755 /* Do QD, or 4 * CPUS, whatever is smallest */
7756 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03007757
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007758 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03007759}
7760
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007761static int io_uring_alloc_task_context(struct task_struct *task,
7762 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06007763{
7764 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007765 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007766
7767 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7768 if (unlikely(!tctx))
7769 return -ENOMEM;
7770
Jens Axboed8a6df12020-10-15 16:24:45 -06007771 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7772 if (unlikely(ret)) {
7773 kfree(tctx);
7774 return ret;
7775 }
7776
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007777 tctx->io_wq = io_init_wq_offload(ctx);
7778 if (IS_ERR(tctx->io_wq)) {
7779 ret = PTR_ERR(tctx->io_wq);
7780 percpu_counter_destroy(&tctx->inflight);
7781 kfree(tctx);
7782 return ret;
7783 }
7784
Jens Axboe0f212202020-09-13 13:09:39 -06007785 xa_init(&tctx->xa);
7786 init_waitqueue_head(&tctx->wait);
7787 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007788 atomic_set(&tctx->in_idle, 0);
7789 tctx->sqpoll = false;
Jens Axboe0f212202020-09-13 13:09:39 -06007790 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00007791 spin_lock_init(&tctx->task_lock);
7792 INIT_WQ_LIST(&tctx->task_list);
7793 tctx->task_state = 0;
7794 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06007795 return 0;
7796}
7797
7798void __io_uring_free(struct task_struct *tsk)
7799{
7800 struct io_uring_task *tctx = tsk->io_uring;
7801
7802 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00007803 WARN_ON_ONCE(tctx->io_wq);
7804
Jens Axboed8a6df12020-10-15 16:24:45 -06007805 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007806 kfree(tctx);
7807 tsk->io_uring = NULL;
7808}
7809
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007810static int io_sq_thread_fork(struct io_sq_data *sqd, struct io_ring_ctx *ctx)
7811{
7812 int ret;
7813
7814 clear_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7815 reinit_completion(&sqd->completion);
7816 ctx->sqo_dead = ctx->sqo_exec = 0;
7817 sqd->task_pid = current->pid;
7818 current->flags |= PF_IO_WORKER;
7819 ret = io_wq_fork_thread(io_sq_thread, sqd);
7820 current->flags &= ~PF_IO_WORKER;
7821 if (ret < 0) {
7822 sqd->thread = NULL;
7823 return ret;
7824 }
7825 wait_for_completion(&sqd->completion);
7826 return io_uring_alloc_task_context(sqd->thread, ctx);
7827}
7828
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007829static int io_sq_offload_create(struct io_ring_ctx *ctx,
7830 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007831{
7832 int ret;
7833
Jens Axboed25e3a32021-02-16 11:41:41 -07007834 /* Retain compatibility with failing for an invalid attach attempt */
7835 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
7836 IORING_SETUP_ATTACH_WQ) {
7837 struct fd f;
7838
7839 f = fdget(p->wq_fd);
7840 if (!f.file)
7841 return -ENXIO;
7842 if (f.file->f_op != &io_uring_fops) {
7843 fdput(f);
7844 return -EINVAL;
7845 }
7846 fdput(f);
7847 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007848 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007849 struct io_sq_data *sqd;
7850
Jens Axboe3ec482d2019-04-08 10:51:01 -06007851 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06007852 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06007853 goto err;
7854
Jens Axboe534ca6d2020-09-02 13:52:19 -06007855 sqd = io_get_sq_data(p);
7856 if (IS_ERR(sqd)) {
7857 ret = PTR_ERR(sqd);
7858 goto err;
7859 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007860
Jens Axboe534ca6d2020-09-02 13:52:19 -06007861 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007862 io_sq_thread_park(sqd);
7863 mutex_lock(&sqd->ctx_lock);
7864 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7865 mutex_unlock(&sqd->ctx_lock);
7866 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007867
Jens Axboe917257d2019-04-13 09:28:55 -06007868 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7869 if (!ctx->sq_thread_idle)
7870 ctx->sq_thread_idle = HZ;
7871
Jens Axboeaa061652020-09-02 14:50:27 -06007872 if (sqd->thread)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007873 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06007874
Jens Axboe6c271ce2019-01-10 11:22:30 -07007875 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007876 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007877
Jens Axboe917257d2019-04-13 09:28:55 -06007878 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007879 if (cpu >= nr_cpu_ids)
7880 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007881 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007882 goto err;
7883
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007884 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007885 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007886 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007887 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007888
7889 sqd->task_pid = current->pid;
7890 current->flags |= PF_IO_WORKER;
7891 ret = io_wq_fork_thread(io_sq_thread, sqd);
7892 current->flags &= ~PF_IO_WORKER;
7893 if (ret < 0) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007894 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007895 goto err;
7896 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007897 wait_for_completion(&sqd->completion);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007898 ret = io_uring_alloc_task_context(sqd->thread, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06007899 if (ret)
7900 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007901 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7902 /* Can't have SQ_AFF without SQPOLL */
7903 ret = -EINVAL;
7904 goto err;
7905 }
7906
Jens Axboe2b188cc2019-01-07 10:46:33 -07007907 return 0;
7908err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007909 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007910 return ret;
7911}
7912
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007913static void io_sq_offload_start(struct io_ring_ctx *ctx)
7914{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007915 struct io_sq_data *sqd = ctx->sq_data;
7916
Jens Axboeeb858902021-02-25 10:13:29 -07007917 if (ctx->flags & IORING_SETUP_SQPOLL)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007918 complete(&sqd->startup);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007919}
7920
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007921static inline void __io_unaccount_mem(struct user_struct *user,
7922 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007923{
7924 atomic_long_sub(nr_pages, &user->locked_vm);
7925}
7926
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007927static inline int __io_account_mem(struct user_struct *user,
7928 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007929{
7930 unsigned long page_limit, cur_pages, new_pages;
7931
7932 /* Don't allow more pages than we can safely lock */
7933 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7934
7935 do {
7936 cur_pages = atomic_long_read(&user->locked_vm);
7937 new_pages = cur_pages + nr_pages;
7938 if (new_pages > page_limit)
7939 return -ENOMEM;
7940 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7941 new_pages) != cur_pages);
7942
7943 return 0;
7944}
7945
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007946static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007947{
Jens Axboe62e398b2021-02-21 16:19:37 -07007948 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007949 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007950
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007951 if (ctx->mm_account)
7952 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007953}
7954
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007955static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007956{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007957 int ret;
7958
Jens Axboe62e398b2021-02-21 16:19:37 -07007959 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007960 ret = __io_account_mem(ctx->user, nr_pages);
7961 if (ret)
7962 return ret;
7963 }
7964
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007965 if (ctx->mm_account)
7966 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007967
7968 return 0;
7969}
7970
Jens Axboe2b188cc2019-01-07 10:46:33 -07007971static void io_mem_free(void *ptr)
7972{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007973 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007974
Mark Rutland52e04ef2019-04-30 17:30:21 +01007975 if (!ptr)
7976 return;
7977
7978 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007979 if (put_page_testzero(page))
7980 free_compound_page(page);
7981}
7982
7983static void *io_mem_alloc(size_t size)
7984{
7985 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007986 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007987
7988 return (void *) __get_free_pages(gfp_flags, get_order(size));
7989}
7990
Hristo Venev75b28af2019-08-26 17:23:46 +00007991static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7992 size_t *sq_offset)
7993{
7994 struct io_rings *rings;
7995 size_t off, sq_array_size;
7996
7997 off = struct_size(rings, cqes, cq_entries);
7998 if (off == SIZE_MAX)
7999 return SIZE_MAX;
8000
8001#ifdef CONFIG_SMP
8002 off = ALIGN(off, SMP_CACHE_BYTES);
8003 if (off == 0)
8004 return SIZE_MAX;
8005#endif
8006
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008007 if (sq_offset)
8008 *sq_offset = off;
8009
Hristo Venev75b28af2019-08-26 17:23:46 +00008010 sq_array_size = array_size(sizeof(u32), sq_entries);
8011 if (sq_array_size == SIZE_MAX)
8012 return SIZE_MAX;
8013
8014 if (check_add_overflow(off, sq_array_size, &off))
8015 return SIZE_MAX;
8016
Hristo Venev75b28af2019-08-26 17:23:46 +00008017 return off;
8018}
8019
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008020static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008021{
8022 int i, j;
8023
8024 if (!ctx->user_bufs)
8025 return -ENXIO;
8026
8027 for (i = 0; i < ctx->nr_user_bufs; i++) {
8028 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8029
8030 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008031 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008032
Jens Axboede293932020-09-17 16:19:16 -06008033 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008034 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008035 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008036 imu->nr_bvecs = 0;
8037 }
8038
8039 kfree(ctx->user_bufs);
8040 ctx->user_bufs = NULL;
8041 ctx->nr_user_bufs = 0;
8042 return 0;
8043}
8044
8045static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8046 void __user *arg, unsigned index)
8047{
8048 struct iovec __user *src;
8049
8050#ifdef CONFIG_COMPAT
8051 if (ctx->compat) {
8052 struct compat_iovec __user *ciovs;
8053 struct compat_iovec ciov;
8054
8055 ciovs = (struct compat_iovec __user *) arg;
8056 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8057 return -EFAULT;
8058
Jens Axboed55e5f52019-12-11 16:12:15 -07008059 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008060 dst->iov_len = ciov.iov_len;
8061 return 0;
8062 }
8063#endif
8064 src = (struct iovec __user *) arg;
8065 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8066 return -EFAULT;
8067 return 0;
8068}
8069
Jens Axboede293932020-09-17 16:19:16 -06008070/*
8071 * Not super efficient, but this is just a registration time. And we do cache
8072 * the last compound head, so generally we'll only do a full search if we don't
8073 * match that one.
8074 *
8075 * We check if the given compound head page has already been accounted, to
8076 * avoid double accounting it. This allows us to account the full size of the
8077 * page, not just the constituent pages of a huge page.
8078 */
8079static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8080 int nr_pages, struct page *hpage)
8081{
8082 int i, j;
8083
8084 /* check current page array */
8085 for (i = 0; i < nr_pages; i++) {
8086 if (!PageCompound(pages[i]))
8087 continue;
8088 if (compound_head(pages[i]) == hpage)
8089 return true;
8090 }
8091
8092 /* check previously registered pages */
8093 for (i = 0; i < ctx->nr_user_bufs; i++) {
8094 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8095
8096 for (j = 0; j < imu->nr_bvecs; j++) {
8097 if (!PageCompound(imu->bvec[j].bv_page))
8098 continue;
8099 if (compound_head(imu->bvec[j].bv_page) == hpage)
8100 return true;
8101 }
8102 }
8103
8104 return false;
8105}
8106
8107static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8108 int nr_pages, struct io_mapped_ubuf *imu,
8109 struct page **last_hpage)
8110{
8111 int i, ret;
8112
8113 for (i = 0; i < nr_pages; i++) {
8114 if (!PageCompound(pages[i])) {
8115 imu->acct_pages++;
8116 } else {
8117 struct page *hpage;
8118
8119 hpage = compound_head(pages[i]);
8120 if (hpage == *last_hpage)
8121 continue;
8122 *last_hpage = hpage;
8123 if (headpage_already_acct(ctx, pages, i, hpage))
8124 continue;
8125 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8126 }
8127 }
8128
8129 if (!imu->acct_pages)
8130 return 0;
8131
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008132 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008133 if (ret)
8134 imu->acct_pages = 0;
8135 return ret;
8136}
8137
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008138static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8139 struct io_mapped_ubuf *imu,
8140 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008141{
8142 struct vm_area_struct **vmas = NULL;
8143 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008144 unsigned long off, start, end, ubuf;
8145 size_t size;
8146 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008147
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008148 ubuf = (unsigned long) iov->iov_base;
8149 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8150 start = ubuf >> PAGE_SHIFT;
8151 nr_pages = end - start;
8152
8153 ret = -ENOMEM;
8154
8155 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8156 if (!pages)
8157 goto done;
8158
8159 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8160 GFP_KERNEL);
8161 if (!vmas)
8162 goto done;
8163
8164 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8165 GFP_KERNEL);
8166 if (!imu->bvec)
8167 goto done;
8168
8169 ret = 0;
8170 mmap_read_lock(current->mm);
8171 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8172 pages, vmas);
8173 if (pret == nr_pages) {
8174 /* don't support file backed memory */
8175 for (i = 0; i < nr_pages; i++) {
8176 struct vm_area_struct *vma = vmas[i];
8177
8178 if (vma->vm_file &&
8179 !is_file_hugepages(vma->vm_file)) {
8180 ret = -EOPNOTSUPP;
8181 break;
8182 }
8183 }
8184 } else {
8185 ret = pret < 0 ? pret : -EFAULT;
8186 }
8187 mmap_read_unlock(current->mm);
8188 if (ret) {
8189 /*
8190 * if we did partial map, or found file backed vmas,
8191 * release any pages we did get
8192 */
8193 if (pret > 0)
8194 unpin_user_pages(pages, pret);
8195 kvfree(imu->bvec);
8196 goto done;
8197 }
8198
8199 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8200 if (ret) {
8201 unpin_user_pages(pages, pret);
8202 kvfree(imu->bvec);
8203 goto done;
8204 }
8205
8206 off = ubuf & ~PAGE_MASK;
8207 size = iov->iov_len;
8208 for (i = 0; i < nr_pages; i++) {
8209 size_t vec_len;
8210
8211 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8212 imu->bvec[i].bv_page = pages[i];
8213 imu->bvec[i].bv_len = vec_len;
8214 imu->bvec[i].bv_offset = off;
8215 off = 0;
8216 size -= vec_len;
8217 }
8218 /* store original address for later verification */
8219 imu->ubuf = ubuf;
8220 imu->len = iov->iov_len;
8221 imu->nr_bvecs = nr_pages;
8222 ret = 0;
8223done:
8224 kvfree(pages);
8225 kvfree(vmas);
8226 return ret;
8227}
8228
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008229static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008230{
Jens Axboeedafcce2019-01-09 09:16:05 -07008231 if (ctx->user_bufs)
8232 return -EBUSY;
8233 if (!nr_args || nr_args > UIO_MAXIOV)
8234 return -EINVAL;
8235
8236 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8237 GFP_KERNEL);
8238 if (!ctx->user_bufs)
8239 return -ENOMEM;
8240
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008241 return 0;
8242}
8243
8244static int io_buffer_validate(struct iovec *iov)
8245{
8246 /*
8247 * Don't impose further limits on the size and buffer
8248 * constraints here, we'll -EINVAL later when IO is
8249 * submitted if they are wrong.
8250 */
8251 if (!iov->iov_base || !iov->iov_len)
8252 return -EFAULT;
8253
8254 /* arbitrary limit, but we need something */
8255 if (iov->iov_len > SZ_1G)
8256 return -EFAULT;
8257
8258 return 0;
8259}
8260
8261static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8262 unsigned int nr_args)
8263{
8264 int i, ret;
8265 struct iovec iov;
8266 struct page *last_hpage = NULL;
8267
8268 ret = io_buffers_map_alloc(ctx, nr_args);
8269 if (ret)
8270 return ret;
8271
Jens Axboeedafcce2019-01-09 09:16:05 -07008272 for (i = 0; i < nr_args; i++) {
8273 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008274
8275 ret = io_copy_iov(ctx, &iov, arg, i);
8276 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008277 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008278
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008279 ret = io_buffer_validate(&iov);
8280 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008281 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008282
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008283 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8284 if (ret)
8285 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008286
8287 ctx->nr_user_bufs++;
8288 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008289
8290 if (ret)
8291 io_sqe_buffers_unregister(ctx);
8292
Jens Axboeedafcce2019-01-09 09:16:05 -07008293 return ret;
8294}
8295
Jens Axboe9b402842019-04-11 11:45:41 -06008296static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8297{
8298 __s32 __user *fds = arg;
8299 int fd;
8300
8301 if (ctx->cq_ev_fd)
8302 return -EBUSY;
8303
8304 if (copy_from_user(&fd, fds, sizeof(*fds)))
8305 return -EFAULT;
8306
8307 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8308 if (IS_ERR(ctx->cq_ev_fd)) {
8309 int ret = PTR_ERR(ctx->cq_ev_fd);
8310 ctx->cq_ev_fd = NULL;
8311 return ret;
8312 }
8313
8314 return 0;
8315}
8316
8317static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8318{
8319 if (ctx->cq_ev_fd) {
8320 eventfd_ctx_put(ctx->cq_ev_fd);
8321 ctx->cq_ev_fd = NULL;
8322 return 0;
8323 }
8324
8325 return -ENXIO;
8326}
8327
Jens Axboe5a2e7452020-02-23 16:23:11 -07008328static int __io_destroy_buffers(int id, void *p, void *data)
8329{
8330 struct io_ring_ctx *ctx = data;
8331 struct io_buffer *buf = p;
8332
Jens Axboe067524e2020-03-02 16:32:28 -07008333 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008334 return 0;
8335}
8336
8337static void io_destroy_buffers(struct io_ring_ctx *ctx)
8338{
8339 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8340 idr_destroy(&ctx->io_buffer_idr);
8341}
8342
Jens Axboe68e68ee2021-02-13 09:00:02 -07008343static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008344{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008345 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008346
Jens Axboe68e68ee2021-02-13 09:00:02 -07008347 list_for_each_entry_safe(req, nxt, list, compl.list) {
8348 if (tsk && req->task != tsk)
8349 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008350 list_del(&req->compl.list);
8351 kmem_cache_free(req_cachep, req);
8352 }
8353}
8354
Jens Axboe4010fec2021-02-27 15:04:18 -07008355static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008356{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008357 struct io_submit_state *submit_state = &ctx->submit_state;
Pavel Begunkove5547d22021-02-23 22:17:20 +00008358 struct io_comp_state *cs = &ctx->submit_state.comp;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008359
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008360 mutex_lock(&ctx->uring_lock);
8361
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008362 if (submit_state->free_reqs) {
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008363 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8364 submit_state->reqs);
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008365 submit_state->free_reqs = 0;
8366 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008367
8368 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkove5547d22021-02-23 22:17:20 +00008369 list_splice_init(&cs->locked_free_list, &cs->free_list);
8370 cs->locked_free_nr = 0;
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008371 spin_unlock_irq(&ctx->completion_lock);
8372
Pavel Begunkove5547d22021-02-23 22:17:20 +00008373 io_req_cache_free(&cs->free_list, NULL);
8374
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008375 mutex_unlock(&ctx->uring_lock);
8376}
8377
Jens Axboe2b188cc2019-01-07 10:46:33 -07008378static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8379{
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00008380 /*
8381 * Some may use context even when all refs and requests have been put,
8382 * and they are free to do so while still holding uring_lock, see
8383 * __io_req_task_submit(). Wait for them to finish.
8384 */
8385 mutex_lock(&ctx->uring_lock);
8386 mutex_unlock(&ctx->uring_lock);
8387
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008388 io_sq_thread_finish(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008389 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008390
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008391 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008392 mmdrop(ctx->mm_account);
8393 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008394 }
Jens Axboedef596e2019-01-09 08:59:42 -07008395
Hao Xu8bad28d2021-02-19 17:19:36 +08008396 mutex_lock(&ctx->uring_lock);
Jens Axboe6b063142019-01-10 22:13:58 -07008397 io_sqe_files_unregister(ctx);
Hao Xu8bad28d2021-02-19 17:19:36 +08008398 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008399 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008400 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008401 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008402
Jens Axboe2b188cc2019-01-07 10:46:33 -07008403#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008404 if (ctx->ring_sock) {
8405 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008406 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008407 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008408#endif
8409
Hristo Venev75b28af2019-08-26 17:23:46 +00008410 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008411 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008412
8413 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008414 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07008415 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07008416 if (ctx->hash_map)
8417 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07008418 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008419 kfree(ctx);
8420}
8421
8422static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8423{
8424 struct io_ring_ctx *ctx = file->private_data;
8425 __poll_t mask = 0;
8426
8427 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008428 /*
8429 * synchronizes with barrier from wq_has_sleeper call in
8430 * io_commit_cqring
8431 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008432 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008433 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008434 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008435
8436 /*
8437 * Don't flush cqring overflow list here, just do a simple check.
8438 * Otherwise there could possible be ABBA deadlock:
8439 * CPU0 CPU1
8440 * ---- ----
8441 * lock(&ctx->uring_lock);
8442 * lock(&ep->mtx);
8443 * lock(&ctx->uring_lock);
8444 * lock(&ep->mtx);
8445 *
8446 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8447 * pushs them to do the flush.
8448 */
8449 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008450 mask |= EPOLLIN | EPOLLRDNORM;
8451
8452 return mask;
8453}
8454
8455static int io_uring_fasync(int fd, struct file *file, int on)
8456{
8457 struct io_ring_ctx *ctx = file->private_data;
8458
8459 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8460}
8461
Yejune Deng0bead8c2020-12-24 11:02:20 +08008462static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008463{
Jens Axboe4379bf82021-02-15 13:40:22 -07008464 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008465
Jens Axboe4379bf82021-02-15 13:40:22 -07008466 creds = idr_remove(&ctx->personality_idr, id);
8467 if (creds) {
8468 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008469 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008470 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008471
8472 return -EINVAL;
8473}
8474
8475static int io_remove_personalities(int id, void *p, void *data)
8476{
8477 struct io_ring_ctx *ctx = data;
8478
8479 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008480 return 0;
8481}
8482
Pavel Begunkovba50a032021-02-26 15:47:56 +00008483static bool io_run_ctx_fallback(struct io_ring_ctx *ctx)
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008484{
8485 struct callback_head *work, *head, *next;
Pavel Begunkovba50a032021-02-26 15:47:56 +00008486 bool executed = false;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008487
8488 do {
8489 do {
8490 head = NULL;
8491 work = READ_ONCE(ctx->exit_task_work);
8492 } while (cmpxchg(&ctx->exit_task_work, work, head) != work);
8493
8494 if (!work)
8495 break;
8496
8497 do {
8498 next = work->next;
8499 work->func(work);
8500 work = next;
8501 cond_resched();
8502 } while (work);
Pavel Begunkovba50a032021-02-26 15:47:56 +00008503 executed = true;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008504 } while (1);
Pavel Begunkovba50a032021-02-26 15:47:56 +00008505
8506 return executed;
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008507}
8508
Jens Axboe85faa7b2020-04-09 18:14:00 -06008509static void io_ring_exit_work(struct work_struct *work)
8510{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008511 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8512 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008513
Jens Axboe56952e92020-06-17 15:00:04 -06008514 /*
8515 * If we're doing polled IO and end up having requests being
8516 * submitted async (out-of-line), then completions can come in while
8517 * we're waiting for refs to drop. We need to reap these manually,
8518 * as nobody else will be looking for them.
8519 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008520 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008521 io_uring_try_cancel_requests(ctx, NULL, NULL);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008522 io_run_ctx_fallback(ctx);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008523 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008524 io_ring_ctx_free(ctx);
8525}
8526
Jens Axboe2b188cc2019-01-07 10:46:33 -07008527static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8528{
8529 mutex_lock(&ctx->uring_lock);
8530 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008531
8532 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8533 ctx->sqo_dead = 1;
8534
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008535 /* if force is set, the ring is going away. always drop after that */
8536 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008537 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008538 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008539 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008540 mutex_unlock(&ctx->uring_lock);
8541
Pavel Begunkov6b819282020-11-06 13:00:25 +00008542 io_kill_timeouts(ctx, NULL, NULL);
8543 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008544
Jens Axboe15dff282019-11-13 09:09:23 -07008545 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008546 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008547
Jens Axboe85faa7b2020-04-09 18:14:00 -06008548 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008549 /*
8550 * Use system_unbound_wq to avoid spawning tons of event kworkers
8551 * if we're exiting a ton of rings at the same time. It just adds
8552 * noise and overhead, there's no discernable change in runtime
8553 * over using system_wq.
8554 */
8555 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008556}
8557
8558static int io_uring_release(struct inode *inode, struct file *file)
8559{
8560 struct io_ring_ctx *ctx = file->private_data;
8561
8562 file->private_data = NULL;
8563 io_ring_ctx_wait_and_kill(ctx);
8564 return 0;
8565}
8566
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008567struct io_task_cancel {
8568 struct task_struct *task;
8569 struct files_struct *files;
8570};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008571
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008572static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008573{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008574 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008575 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008576 bool ret;
8577
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008578 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008579 unsigned long flags;
8580 struct io_ring_ctx *ctx = req->ctx;
8581
8582 /* protect against races with linked timeouts */
8583 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008584 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008585 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8586 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008587 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008588 }
8589 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008590}
8591
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008592static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008593 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008594 struct files_struct *files)
8595{
8596 struct io_defer_entry *de = NULL;
8597 LIST_HEAD(list);
8598
8599 spin_lock_irq(&ctx->completion_lock);
8600 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008601 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008602 list_cut_position(&list, &ctx->defer_list, &de->list);
8603 break;
8604 }
8605 }
8606 spin_unlock_irq(&ctx->completion_lock);
8607
8608 while (!list_empty(&list)) {
8609 de = list_first_entry(&list, struct io_defer_entry, list);
8610 list_del_init(&de->list);
8611 req_set_fail_links(de->req);
8612 io_put_req(de->req);
8613 io_req_complete(de->req, -ECANCELED);
8614 kfree(de);
8615 }
8616}
8617
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008618static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8619 struct task_struct *task,
8620 struct files_struct *files)
8621{
8622 struct io_task_cancel cancel = { .task = task, .files = files, };
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008623 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008624
8625 while (1) {
8626 enum io_wq_cancel cret;
8627 bool ret = false;
8628
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008629 if (tctx && tctx->io_wq) {
8630 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008631 &cancel, true);
8632 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8633 }
8634
8635 /* SQPOLL thread does its own polling */
8636 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8637 while (!list_empty_careful(&ctx->iopoll_list)) {
8638 io_iopoll_try_reap_events(ctx);
8639 ret = true;
8640 }
8641 }
8642
8643 ret |= io_poll_remove_all(ctx, task, files);
8644 ret |= io_kill_timeouts(ctx, task, files);
8645 ret |= io_run_task_work();
Pavel Begunkovba50a032021-02-26 15:47:56 +00008646 ret |= io_run_ctx_fallback(ctx);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008647 io_cqring_overflow_flush(ctx, true, task, files);
8648 if (!ret)
8649 break;
8650 cond_resched();
8651 }
8652}
8653
Pavel Begunkovca70f002021-01-26 15:28:27 +00008654static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8655 struct task_struct *task,
8656 struct files_struct *files)
8657{
8658 struct io_kiocb *req;
8659 int cnt = 0;
8660
8661 spin_lock_irq(&ctx->inflight_lock);
8662 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8663 cnt += io_match_task(req, task, files);
8664 spin_unlock_irq(&ctx->inflight_lock);
8665 return cnt;
8666}
8667
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008668static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008669 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008670 struct files_struct *files)
8671{
Jens Axboefcb323c2019-10-24 12:39:47 -06008672 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008673 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008674 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008675
Pavel Begunkovca70f002021-01-26 15:28:27 +00008676 inflight = io_uring_count_inflight(ctx, task, files);
8677 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008678 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008679
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008680 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008681
Pavel Begunkov34343782021-02-10 11:45:42 +00008682 if (ctx->sq_data)
8683 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008684 prepare_to_wait(&task->io_uring->wait, &wait,
8685 TASK_UNINTERRUPTIBLE);
8686 if (inflight == io_uring_count_inflight(ctx, task, files))
8687 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008688 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00008689 if (ctx->sq_data)
8690 io_sq_thread_park(ctx->sq_data);
Jens Axboe0f212202020-09-13 13:09:39 -06008691 }
Jens Axboe0f212202020-09-13 13:09:39 -06008692}
8693
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008694static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8695{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008696 mutex_lock(&ctx->uring_lock);
8697 ctx->sqo_dead = 1;
8698 mutex_unlock(&ctx->uring_lock);
8699
8700 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008701 if (ctx->rings)
8702 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008703}
8704
Jens Axboe0f212202020-09-13 13:09:39 -06008705/*
8706 * We need to iteratively cancel requests, in case a request has dependent
8707 * hard links. These persist even for failure of cancelations, hence keep
8708 * looping until none are found.
8709 */
8710static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8711 struct files_struct *files)
8712{
8713 struct task_struct *task = current;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008714 bool did_park = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008715
Jens Axboefdaf0832020-10-30 09:37:30 -06008716 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008717 io_disable_sqo_submit(ctx);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008718 did_park = io_sq_thread_park(ctx->sq_data);
8719 if (did_park) {
8720 task = ctx->sq_data->thread;
8721 atomic_inc(&task->io_uring->in_idle);
8722 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008723 }
Jens Axboe0f212202020-09-13 13:09:39 -06008724
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008725 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008726
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008727 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008728 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008729 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008730
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008731 if (did_park) {
Jens Axboefdaf0832020-10-30 09:37:30 -06008732 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06008733 io_sq_thread_unpark(ctx->sq_data);
8734 }
Jens Axboe0f212202020-09-13 13:09:39 -06008735}
8736
8737/*
8738 * Note that this task has used io_uring. We use it for cancelation purposes.
8739 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008740static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008741{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008742 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008743 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008744
8745 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008746 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06008747 if (unlikely(ret))
8748 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008749 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008750 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008751 if (tctx->last != file) {
8752 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008753
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008754 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008755 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008756 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8757 file, GFP_KERNEL));
8758 if (ret) {
8759 fput(file);
8760 return ret;
8761 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00008762
8763 /* one and only SQPOLL file note, held by sqo_task */
8764 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
8765 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06008766 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008767 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008768 }
8769
Jens Axboefdaf0832020-10-30 09:37:30 -06008770 /*
8771 * This is race safe in that the task itself is doing this, hence it
8772 * cannot be going through the exit/cancel paths at the same time.
8773 * This cannot be modified while exit/cancel is running.
8774 */
8775 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8776 tctx->sqpoll = true;
8777
Jens Axboe0f212202020-09-13 13:09:39 -06008778 return 0;
8779}
8780
8781/*
8782 * Remove this io_uring_file -> task mapping.
8783 */
8784static void io_uring_del_task_file(struct file *file)
8785{
8786 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008787
8788 if (tctx->last == file)
8789 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008790 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008791 if (file)
8792 fput(file);
8793}
8794
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008795static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008796{
8797 struct file *file;
8798 unsigned long index;
8799
8800 xa_for_each(&tctx->xa, index, file)
8801 io_uring_del_task_file(file);
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008802 if (tctx->io_wq) {
8803 io_wq_put_and_exit(tctx->io_wq);
8804 tctx->io_wq = NULL;
8805 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008806}
8807
Jens Axboe0f212202020-09-13 13:09:39 -06008808void __io_uring_files_cancel(struct files_struct *files)
8809{
8810 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008811 struct file *file;
8812 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008813
8814 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008815 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008816 xa_for_each(&tctx->xa, index, file)
8817 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008818 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008819
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008820 if (files)
8821 io_uring_clean_tctx(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06008822}
8823
8824static s64 tctx_inflight(struct io_uring_task *tctx)
8825{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008826 return percpu_counter_sum(&tctx->inflight);
8827}
8828
8829static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
8830{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008831 struct io_sq_data *sqd = ctx->sq_data;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008832 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06008833 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008834 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008835
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008836 if (!sqd)
8837 return;
8838 io_disable_sqo_submit(ctx);
8839 if (!io_sq_thread_park(sqd))
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008840 return;
8841 tctx = ctx->sq_data->thread->io_uring;
Jens Axboee54945a2021-02-26 11:27:15 -07008842 /* can happen on fork/alloc failure, just ignore that state */
8843 if (!tctx) {
8844 io_sq_thread_unpark(sqd);
8845 return;
8846 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008847
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008848 atomic_inc(&tctx->in_idle);
8849 do {
8850 /* read completions before cancelations */
8851 inflight = tctx_inflight(tctx);
8852 if (!inflight)
8853 break;
8854 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008855
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008856 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8857 /*
8858 * If we've seen completions, retry without waiting. This
8859 * avoids a race where a completion comes in before we did
8860 * prepare_to_wait().
8861 */
8862 if (inflight == tctx_inflight(tctx))
8863 schedule();
8864 finish_wait(&tctx->wait, &wait);
8865 } while (1);
8866 atomic_dec(&tctx->in_idle);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008867 io_sq_thread_unpark(sqd);
Jens Axboe0f212202020-09-13 13:09:39 -06008868}
8869
Jens Axboe0f212202020-09-13 13:09:39 -06008870/*
8871 * Find any io_uring fd that this task has registered or done IO on, and cancel
8872 * requests.
8873 */
8874void __io_uring_task_cancel(void)
8875{
8876 struct io_uring_task *tctx = current->io_uring;
8877 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008878 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008879
8880 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008881 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008882
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00008883 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008884 if (tctx->sqpoll) {
8885 struct file *file;
8886 unsigned long index;
8887
8888 xa_for_each(&tctx->xa, index, file)
8889 io_uring_cancel_sqpoll(file->private_data);
8890 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00008891
Jens Axboed8a6df12020-10-15 16:24:45 -06008892 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008893 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008894 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008895 if (!inflight)
8896 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008897 __io_uring_files_cancel(NULL);
8898
8899 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8900
8901 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008902 * If we've seen completions, retry without waiting. This
8903 * avoids a race where a completion comes in before we did
8904 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06008905 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008906 if (inflight == tctx_inflight(tctx))
8907 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00008908 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008909 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008910
Jens Axboefdaf0832020-10-30 09:37:30 -06008911 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008912
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00008913 io_uring_clean_tctx(tctx);
8914 /* all current's requests should be gone, we can kill tctx */
8915 __io_uring_free(current);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008916}
8917
Jens Axboefcb323c2019-10-24 12:39:47 -06008918static int io_uring_flush(struct file *file, void *data)
8919{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008920 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008921 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008922
Jens Axboe3bfe6102021-02-16 14:15:30 -07008923 /* Ignore helper thread files exit */
8924 if (current->flags & PF_IO_WORKER)
8925 return 0;
8926
Jens Axboe41be53e2021-02-13 09:11:04 -07008927 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
Jens Axboe84965ff2021-01-23 15:51:11 -07008928 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboe4010fec2021-02-27 15:04:18 -07008929 io_req_caches_free(ctx);
Jens Axboe41be53e2021-02-13 09:11:04 -07008930 }
Jens Axboe84965ff2021-01-23 15:51:11 -07008931
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008932 io_run_ctx_fallback(ctx);
8933
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008934 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00008935 return 0;
8936
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008937 /* we should have cancelled and erased it before PF_EXITING */
8938 WARN_ON_ONCE((current->flags & PF_EXITING) &&
8939 xa_load(&tctx->xa, (unsigned long)file));
8940
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00008941 /*
8942 * fput() is pending, will be 2 if the only other ref is our potential
8943 * task file note. If the task is exiting, drop regardless of count.
8944 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008945 if (atomic_long_read(&file->f_count) != 2)
8946 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00008947
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008948 if (ctx->flags & IORING_SETUP_SQPOLL) {
8949 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00008950 WARN_ON_ONCE(ctx->sqo_task != current &&
8951 xa_load(&tctx->xa, (unsigned long)file));
8952 /* sqo_dead check is for when this happens after cancellation */
8953 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008954 !xa_load(&tctx->xa, (unsigned long)file));
8955
8956 io_disable_sqo_submit(ctx);
8957 }
8958
8959 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
8960 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06008961 return 0;
8962}
8963
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008964static void *io_uring_validate_mmap_request(struct file *file,
8965 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008966{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008967 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008968 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008969 struct page *page;
8970 void *ptr;
8971
8972 switch (offset) {
8973 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008974 case IORING_OFF_CQ_RING:
8975 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008976 break;
8977 case IORING_OFF_SQES:
8978 ptr = ctx->sq_sqes;
8979 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008980 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008981 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008982 }
8983
8984 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008985 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008986 return ERR_PTR(-EINVAL);
8987
8988 return ptr;
8989}
8990
8991#ifdef CONFIG_MMU
8992
8993static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8994{
8995 size_t sz = vma->vm_end - vma->vm_start;
8996 unsigned long pfn;
8997 void *ptr;
8998
8999 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9000 if (IS_ERR(ptr))
9001 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009002
9003 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9004 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9005}
9006
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009007#else /* !CONFIG_MMU */
9008
9009static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9010{
9011 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9012}
9013
9014static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9015{
9016 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9017}
9018
9019static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9020 unsigned long addr, unsigned long len,
9021 unsigned long pgoff, unsigned long flags)
9022{
9023 void *ptr;
9024
9025 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9026 if (IS_ERR(ptr))
9027 return PTR_ERR(ptr);
9028
9029 return (unsigned long) ptr;
9030}
9031
9032#endif /* !CONFIG_MMU */
9033
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009034static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009035{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009036 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009037 DEFINE_WAIT(wait);
9038
9039 do {
9040 if (!io_sqring_full(ctx))
9041 break;
9042
9043 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9044
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009045 if (unlikely(ctx->sqo_dead)) {
9046 ret = -EOWNERDEAD;
9047 goto out;
9048 }
9049
Jens Axboe90554202020-09-03 12:12:41 -06009050 if (!io_sqring_full(ctx))
9051 break;
9052
9053 schedule();
9054 } while (!signal_pending(current));
9055
9056 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009057out:
9058 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009059}
9060
Hao Xuc73ebb62020-11-03 10:54:37 +08009061static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9062 struct __kernel_timespec __user **ts,
9063 const sigset_t __user **sig)
9064{
9065 struct io_uring_getevents_arg arg;
9066
9067 /*
9068 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9069 * is just a pointer to the sigset_t.
9070 */
9071 if (!(flags & IORING_ENTER_EXT_ARG)) {
9072 *sig = (const sigset_t __user *) argp;
9073 *ts = NULL;
9074 return 0;
9075 }
9076
9077 /*
9078 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9079 * timespec and sigset_t pointers if good.
9080 */
9081 if (*argsz != sizeof(arg))
9082 return -EINVAL;
9083 if (copy_from_user(&arg, argp, sizeof(arg)))
9084 return -EFAULT;
9085 *sig = u64_to_user_ptr(arg.sigmask);
9086 *argsz = arg.sigmask_sz;
9087 *ts = u64_to_user_ptr(arg.ts);
9088 return 0;
9089}
9090
Jens Axboe2b188cc2019-01-07 10:46:33 -07009091SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009092 u32, min_complete, u32, flags, const void __user *, argp,
9093 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009094{
9095 struct io_ring_ctx *ctx;
9096 long ret = -EBADF;
9097 int submitted = 0;
9098 struct fd f;
9099
Jens Axboe4c6e2772020-07-01 11:29:10 -06009100 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009101
Jens Axboe90554202020-09-03 12:12:41 -06009102 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009103 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009104 return -EINVAL;
9105
9106 f = fdget(fd);
9107 if (!f.file)
9108 return -EBADF;
9109
9110 ret = -EOPNOTSUPP;
9111 if (f.file->f_op != &io_uring_fops)
9112 goto out_fput;
9113
9114 ret = -ENXIO;
9115 ctx = f.file->private_data;
9116 if (!percpu_ref_tryget(&ctx->refs))
9117 goto out_fput;
9118
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009119 ret = -EBADFD;
9120 if (ctx->flags & IORING_SETUP_R_DISABLED)
9121 goto out;
9122
Jens Axboe6c271ce2019-01-10 11:22:30 -07009123 /*
9124 * For SQ polling, the thread will do all submissions and completions.
9125 * Just return the requested submit count, and wake the thread if
9126 * we were asked to.
9127 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009128 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009129 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009130 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009131
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009132 if (unlikely(ctx->sqo_exec)) {
9133 ret = io_sq_thread_fork(ctx->sq_data, ctx);
9134 if (ret)
9135 goto out;
9136 ctx->sqo_exec = 0;
9137 }
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009138 ret = -EOWNERDEAD;
9139 if (unlikely(ctx->sqo_dead))
9140 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009141 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009142 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009143 if (flags & IORING_ENTER_SQ_WAIT) {
9144 ret = io_sqpoll_wait_sq(ctx);
9145 if (ret)
9146 goto out;
9147 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009148 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009149 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009150 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009151 if (unlikely(ret))
9152 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009153 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009154 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009155 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009156
9157 if (submitted != to_submit)
9158 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009159 }
9160 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009161 const sigset_t __user *sig;
9162 struct __kernel_timespec __user *ts;
9163
9164 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9165 if (unlikely(ret))
9166 goto out;
9167
Jens Axboe2b188cc2019-01-07 10:46:33 -07009168 min_complete = min(min_complete, ctx->cq_entries);
9169
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009170 /*
9171 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9172 * space applications don't need to do io completion events
9173 * polling again, they can rely on io_sq_thread to do polling
9174 * work, which can reduce cpu usage and uring_lock contention.
9175 */
9176 if (ctx->flags & IORING_SETUP_IOPOLL &&
9177 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009178 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009179 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009180 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009181 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009182 }
9183
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009184out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009185 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009186out_fput:
9187 fdput(f);
9188 return submitted ? submitted : ret;
9189}
9190
Tobias Klauserbebdb652020-02-26 18:38:32 +01009191#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009192static int io_uring_show_cred(int id, void *p, void *data)
9193{
Jens Axboe4379bf82021-02-15 13:40:22 -07009194 const struct cred *cred = p;
Jens Axboe87ce9552020-01-30 08:25:34 -07009195 struct seq_file *m = data;
9196 struct user_namespace *uns = seq_user_ns(m);
9197 struct group_info *gi;
9198 kernel_cap_t cap;
9199 unsigned __capi;
9200 int g;
9201
9202 seq_printf(m, "%5d\n", id);
9203 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9204 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9205 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9206 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9207 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9208 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9209 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9210 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9211 seq_puts(m, "\n\tGroups:\t");
9212 gi = cred->group_info;
9213 for (g = 0; g < gi->ngroups; g++) {
9214 seq_put_decimal_ull(m, g ? " " : "",
9215 from_kgid_munged(uns, gi->gid[g]));
9216 }
9217 seq_puts(m, "\n\tCapEff:\t");
9218 cap = cred->cap_effective;
9219 CAP_FOR_EACH_U32(__capi)
9220 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9221 seq_putc(m, '\n');
9222 return 0;
9223}
9224
9225static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9226{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009227 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009228 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009229 int i;
9230
Jens Axboefad8e0d2020-09-28 08:57:48 -06009231 /*
9232 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9233 * since fdinfo case grabs it in the opposite direction of normal use
9234 * cases. If we fail to get the lock, we just don't iterate any
9235 * structures that could be going away outside the io_uring mutex.
9236 */
9237 has_lock = mutex_trylock(&ctx->uring_lock);
9238
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009239 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -06009240 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009241 if (!sq->thread)
9242 sq = NULL;
9243 }
Joseph Qidbbe9c62020-09-29 09:01:22 -06009244
9245 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9246 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009247 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009248 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009249 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009250
Jens Axboe87ce9552020-01-30 08:25:34 -07009251 if (f)
9252 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9253 else
9254 seq_printf(m, "%5u: <none>\n", i);
9255 }
9256 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009257 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009258 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9259
9260 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9261 (unsigned int) buf->len);
9262 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009263 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009264 seq_printf(m, "Personalities:\n");
9265 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9266 }
Jens Axboed7718a92020-02-14 22:23:12 -07009267 seq_printf(m, "PollList:\n");
9268 spin_lock_irq(&ctx->completion_lock);
9269 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9270 struct hlist_head *list = &ctx->cancel_hash[i];
9271 struct io_kiocb *req;
9272
9273 hlist_for_each_entry(req, list, hash_node)
9274 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9275 req->task->task_works != NULL);
9276 }
9277 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009278 if (has_lock)
9279 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009280}
9281
9282static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9283{
9284 struct io_ring_ctx *ctx = f->private_data;
9285
9286 if (percpu_ref_tryget(&ctx->refs)) {
9287 __io_uring_show_fdinfo(ctx, m);
9288 percpu_ref_put(&ctx->refs);
9289 }
9290}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009291#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009292
Jens Axboe2b188cc2019-01-07 10:46:33 -07009293static const struct file_operations io_uring_fops = {
9294 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009295 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009296 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009297#ifndef CONFIG_MMU
9298 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9299 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9300#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009301 .poll = io_uring_poll,
9302 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009303#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009304 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009305#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009306};
9307
9308static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9309 struct io_uring_params *p)
9310{
Hristo Venev75b28af2019-08-26 17:23:46 +00009311 struct io_rings *rings;
9312 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009313
Jens Axboebd740482020-08-05 12:58:23 -06009314 /* make sure these are sane, as we already accounted them */
9315 ctx->sq_entries = p->sq_entries;
9316 ctx->cq_entries = p->cq_entries;
9317
Hristo Venev75b28af2019-08-26 17:23:46 +00009318 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9319 if (size == SIZE_MAX)
9320 return -EOVERFLOW;
9321
9322 rings = io_mem_alloc(size);
9323 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009324 return -ENOMEM;
9325
Hristo Venev75b28af2019-08-26 17:23:46 +00009326 ctx->rings = rings;
9327 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9328 rings->sq_ring_mask = p->sq_entries - 1;
9329 rings->cq_ring_mask = p->cq_entries - 1;
9330 rings->sq_ring_entries = p->sq_entries;
9331 rings->cq_ring_entries = p->cq_entries;
9332 ctx->sq_mask = rings->sq_ring_mask;
9333 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009334
9335 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009336 if (size == SIZE_MAX) {
9337 io_mem_free(ctx->rings);
9338 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009339 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009340 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009341
9342 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009343 if (!ctx->sq_sqes) {
9344 io_mem_free(ctx->rings);
9345 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009346 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009347 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009348
Jens Axboe2b188cc2019-01-07 10:46:33 -07009349 return 0;
9350}
9351
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009352static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9353{
9354 int ret, fd;
9355
9356 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9357 if (fd < 0)
9358 return fd;
9359
9360 ret = io_uring_add_task_file(ctx, file);
9361 if (ret) {
9362 put_unused_fd(fd);
9363 return ret;
9364 }
9365 fd_install(fd, file);
9366 return fd;
9367}
9368
Jens Axboe2b188cc2019-01-07 10:46:33 -07009369/*
9370 * Allocate an anonymous fd, this is what constitutes the application
9371 * visible backing of an io_uring instance. The application mmaps this
9372 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9373 * we have to tie this fd to a socket for file garbage collection purposes.
9374 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009375static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009376{
9377 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009378#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009379 int ret;
9380
Jens Axboe2b188cc2019-01-07 10:46:33 -07009381 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9382 &ctx->ring_sock);
9383 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009384 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009385#endif
9386
Jens Axboe2b188cc2019-01-07 10:46:33 -07009387 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9388 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009389#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009390 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009391 sock_release(ctx->ring_sock);
9392 ctx->ring_sock = NULL;
9393 } else {
9394 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009395 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009396#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009397 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009398}
9399
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009400static int io_uring_create(unsigned entries, struct io_uring_params *p,
9401 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009402{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009403 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009404 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009405 int ret;
9406
Jens Axboe8110c1a2019-12-28 15:39:54 -07009407 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009408 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009409 if (entries > IORING_MAX_ENTRIES) {
9410 if (!(p->flags & IORING_SETUP_CLAMP))
9411 return -EINVAL;
9412 entries = IORING_MAX_ENTRIES;
9413 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009414
9415 /*
9416 * Use twice as many entries for the CQ ring. It's possible for the
9417 * application to drive a higher depth than the size of the SQ ring,
9418 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009419 * some flexibility in overcommitting a bit. If the application has
9420 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9421 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009422 */
9423 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009424 if (p->flags & IORING_SETUP_CQSIZE) {
9425 /*
9426 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9427 * to a power-of-two, if it isn't already. We do NOT impose
9428 * any cq vs sq ring sizing.
9429 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009430 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009431 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009432 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9433 if (!(p->flags & IORING_SETUP_CLAMP))
9434 return -EINVAL;
9435 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9436 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009437 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9438 if (p->cq_entries < p->sq_entries)
9439 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009440 } else {
9441 p->cq_entries = 2 * p->sq_entries;
9442 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009443
Jens Axboe2b188cc2019-01-07 10:46:33 -07009444 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -07009445 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009446 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009447 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -07009448 if (!capable(CAP_IPC_LOCK))
9449 ctx->user = get_uid(current_user());
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009450 ctx->sqo_task = current;
Jens Axboe2aede0e2020-09-14 10:45:53 -06009451
9452 /*
9453 * This is just grabbed for accounting purposes. When a process exits,
9454 * the mm is exited and dropped before the files, hence we need to hang
9455 * on to this mm purely for the purposes of being able to unaccount
9456 * memory (locked/pinned vm). It's not used for anything else.
9457 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009458 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009459 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009460
Jens Axboe2b188cc2019-01-07 10:46:33 -07009461 ret = io_allocate_scq_urings(ctx, p);
9462 if (ret)
9463 goto err;
9464
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009465 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009466 if (ret)
9467 goto err;
9468
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009469 if (!(p->flags & IORING_SETUP_R_DISABLED))
9470 io_sq_offload_start(ctx);
9471
Jens Axboe2b188cc2019-01-07 10:46:33 -07009472 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009473 p->sq_off.head = offsetof(struct io_rings, sq.head);
9474 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9475 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9476 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9477 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9478 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9479 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009480
9481 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009482 p->cq_off.head = offsetof(struct io_rings, cq.head);
9483 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9484 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9485 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9486 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9487 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009488 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009489
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009490 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9491 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009492 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009493 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Jens Axboe1c0aa1f2021-02-20 11:55:28 -07009494 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009495
9496 if (copy_to_user(params, p, sizeof(*p))) {
9497 ret = -EFAULT;
9498 goto err;
9499 }
Jens Axboed1719f72020-07-30 13:43:53 -06009500
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009501 file = io_uring_get_file(ctx);
9502 if (IS_ERR(file)) {
9503 ret = PTR_ERR(file);
9504 goto err;
9505 }
9506
Jens Axboed1719f72020-07-30 13:43:53 -06009507 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009508 * Install ring fd as the very last thing, so we don't risk someone
9509 * having closed it before we finish setup
9510 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009511 ret = io_uring_install_fd(ctx, file);
9512 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009513 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009514 /* fput will clean it up */
9515 fput(file);
9516 return ret;
9517 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009518
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009519 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009520 return ret;
9521err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009522 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009523 io_ring_ctx_wait_and_kill(ctx);
9524 return ret;
9525}
9526
9527/*
9528 * Sets up an aio uring context, and returns the fd. Applications asks for a
9529 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9530 * params structure passed in.
9531 */
9532static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9533{
9534 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009535 int i;
9536
9537 if (copy_from_user(&p, params, sizeof(p)))
9538 return -EFAULT;
9539 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9540 if (p.resv[i])
9541 return -EINVAL;
9542 }
9543
Jens Axboe6c271ce2019-01-10 11:22:30 -07009544 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009545 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009546 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9547 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009548 return -EINVAL;
9549
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009550 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009551}
9552
9553SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9554 struct io_uring_params __user *, params)
9555{
9556 return io_uring_setup(entries, params);
9557}
9558
Jens Axboe66f4af92020-01-16 15:36:52 -07009559static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9560{
9561 struct io_uring_probe *p;
9562 size_t size;
9563 int i, ret;
9564
9565 size = struct_size(p, ops, nr_args);
9566 if (size == SIZE_MAX)
9567 return -EOVERFLOW;
9568 p = kzalloc(size, GFP_KERNEL);
9569 if (!p)
9570 return -ENOMEM;
9571
9572 ret = -EFAULT;
9573 if (copy_from_user(p, arg, size))
9574 goto out;
9575 ret = -EINVAL;
9576 if (memchr_inv(p, 0, size))
9577 goto out;
9578
9579 p->last_op = IORING_OP_LAST - 1;
9580 if (nr_args > IORING_OP_LAST)
9581 nr_args = IORING_OP_LAST;
9582
9583 for (i = 0; i < nr_args; i++) {
9584 p->ops[i].op = i;
9585 if (!io_op_defs[i].not_supported)
9586 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9587 }
9588 p->ops_len = i;
9589
9590 ret = 0;
9591 if (copy_to_user(arg, p, size))
9592 ret = -EFAULT;
9593out:
9594 kfree(p);
9595 return ret;
9596}
9597
Jens Axboe071698e2020-01-28 10:04:42 -07009598static int io_register_personality(struct io_ring_ctx *ctx)
9599{
Jens Axboe4379bf82021-02-15 13:40:22 -07009600 const struct cred *creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009601 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009602
Jens Axboe4379bf82021-02-15 13:40:22 -07009603 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009604
Jens Axboe4379bf82021-02-15 13:40:22 -07009605 ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9606 USHRT_MAX, GFP_KERNEL);
9607 if (ret < 0)
9608 put_cred(creds);
Jens Axboe1e6fa522020-10-15 08:46:24 -06009609 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009610}
9611
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009612static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9613 unsigned int nr_args)
9614{
9615 struct io_uring_restriction *res;
9616 size_t size;
9617 int i, ret;
9618
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009619 /* Restrictions allowed only if rings started disabled */
9620 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9621 return -EBADFD;
9622
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009623 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009624 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009625 return -EBUSY;
9626
9627 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9628 return -EINVAL;
9629
9630 size = array_size(nr_args, sizeof(*res));
9631 if (size == SIZE_MAX)
9632 return -EOVERFLOW;
9633
9634 res = memdup_user(arg, size);
9635 if (IS_ERR(res))
9636 return PTR_ERR(res);
9637
9638 ret = 0;
9639
9640 for (i = 0; i < nr_args; i++) {
9641 switch (res[i].opcode) {
9642 case IORING_RESTRICTION_REGISTER_OP:
9643 if (res[i].register_op >= IORING_REGISTER_LAST) {
9644 ret = -EINVAL;
9645 goto out;
9646 }
9647
9648 __set_bit(res[i].register_op,
9649 ctx->restrictions.register_op);
9650 break;
9651 case IORING_RESTRICTION_SQE_OP:
9652 if (res[i].sqe_op >= IORING_OP_LAST) {
9653 ret = -EINVAL;
9654 goto out;
9655 }
9656
9657 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9658 break;
9659 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9660 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9661 break;
9662 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9663 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9664 break;
9665 default:
9666 ret = -EINVAL;
9667 goto out;
9668 }
9669 }
9670
9671out:
9672 /* Reset all restrictions if an error happened */
9673 if (ret != 0)
9674 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9675 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009676 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009677
9678 kfree(res);
9679 return ret;
9680}
9681
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009682static int io_register_enable_rings(struct io_ring_ctx *ctx)
9683{
9684 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9685 return -EBADFD;
9686
9687 if (ctx->restrictions.registered)
9688 ctx->restricted = 1;
9689
9690 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9691
9692 io_sq_offload_start(ctx);
9693
9694 return 0;
9695}
9696
Jens Axboe071698e2020-01-28 10:04:42 -07009697static bool io_register_op_must_quiesce(int op)
9698{
9699 switch (op) {
9700 case IORING_UNREGISTER_FILES:
9701 case IORING_REGISTER_FILES_UPDATE:
9702 case IORING_REGISTER_PROBE:
9703 case IORING_REGISTER_PERSONALITY:
9704 case IORING_UNREGISTER_PERSONALITY:
9705 return false;
9706 default:
9707 return true;
9708 }
9709}
9710
Jens Axboeedafcce2019-01-09 09:16:05 -07009711static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9712 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009713 __releases(ctx->uring_lock)
9714 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009715{
9716 int ret;
9717
Jens Axboe35fa71a2019-04-22 10:23:23 -06009718 /*
9719 * We're inside the ring mutex, if the ref is already dying, then
9720 * someone else killed the ctx or is already going through
9721 * io_uring_register().
9722 */
9723 if (percpu_ref_is_dying(&ctx->refs))
9724 return -ENXIO;
9725
Jens Axboe071698e2020-01-28 10:04:42 -07009726 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009727 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009728
Jens Axboe05f3fb32019-12-09 11:22:50 -07009729 /*
9730 * Drop uring mutex before waiting for references to exit. If
9731 * another thread is currently inside io_uring_enter() it might
9732 * need to grab the uring_lock to make progress. If we hold it
9733 * here across the drain wait, then we can deadlock. It's safe
9734 * to drop the mutex here, since no new references will come in
9735 * after we've killed the percpu ref.
9736 */
9737 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009738 do {
9739 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9740 if (!ret)
9741 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009742 ret = io_run_task_work_sig();
9743 if (ret < 0)
9744 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009745 } while (1);
9746
Jens Axboe05f3fb32019-12-09 11:22:50 -07009747 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009748
Jens Axboec1503682020-01-08 08:26:07 -07009749 if (ret) {
9750 percpu_ref_resurrect(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009751 goto out_quiesce;
9752 }
9753 }
9754
9755 if (ctx->restricted) {
9756 if (opcode >= IORING_REGISTER_LAST) {
9757 ret = -EINVAL;
9758 goto out;
9759 }
9760
9761 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9762 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009763 goto out;
9764 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009765 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009766
9767 switch (opcode) {
9768 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009769 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -07009770 break;
9771 case IORING_UNREGISTER_BUFFERS:
9772 ret = -EINVAL;
9773 if (arg || nr_args)
9774 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009775 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07009776 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009777 case IORING_REGISTER_FILES:
9778 ret = io_sqe_files_register(ctx, arg, nr_args);
9779 break;
9780 case IORING_UNREGISTER_FILES:
9781 ret = -EINVAL;
9782 if (arg || nr_args)
9783 break;
9784 ret = io_sqe_files_unregister(ctx);
9785 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009786 case IORING_REGISTER_FILES_UPDATE:
9787 ret = io_sqe_files_update(ctx, arg, nr_args);
9788 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009789 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009790 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009791 ret = -EINVAL;
9792 if (nr_args != 1)
9793 break;
9794 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009795 if (ret)
9796 break;
9797 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9798 ctx->eventfd_async = 1;
9799 else
9800 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009801 break;
9802 case IORING_UNREGISTER_EVENTFD:
9803 ret = -EINVAL;
9804 if (arg || nr_args)
9805 break;
9806 ret = io_eventfd_unregister(ctx);
9807 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009808 case IORING_REGISTER_PROBE:
9809 ret = -EINVAL;
9810 if (!arg || nr_args > 256)
9811 break;
9812 ret = io_probe(ctx, arg, nr_args);
9813 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009814 case IORING_REGISTER_PERSONALITY:
9815 ret = -EINVAL;
9816 if (arg || nr_args)
9817 break;
9818 ret = io_register_personality(ctx);
9819 break;
9820 case IORING_UNREGISTER_PERSONALITY:
9821 ret = -EINVAL;
9822 if (arg)
9823 break;
9824 ret = io_unregister_personality(ctx, nr_args);
9825 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009826 case IORING_REGISTER_ENABLE_RINGS:
9827 ret = -EINVAL;
9828 if (arg || nr_args)
9829 break;
9830 ret = io_register_enable_rings(ctx);
9831 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009832 case IORING_REGISTER_RESTRICTIONS:
9833 ret = io_register_restrictions(ctx, arg, nr_args);
9834 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009835 default:
9836 ret = -EINVAL;
9837 break;
9838 }
9839
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009840out:
Jens Axboe071698e2020-01-28 10:04:42 -07009841 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009842 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009843 percpu_ref_reinit(&ctx->refs);
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009844out_quiesce:
Jens Axboe0f158b42020-05-14 17:18:39 -06009845 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009846 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009847 return ret;
9848}
9849
9850SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9851 void __user *, arg, unsigned int, nr_args)
9852{
9853 struct io_ring_ctx *ctx;
9854 long ret = -EBADF;
9855 struct fd f;
9856
9857 f = fdget(fd);
9858 if (!f.file)
9859 return -EBADF;
9860
9861 ret = -EOPNOTSUPP;
9862 if (f.file->f_op != &io_uring_fops)
9863 goto out_fput;
9864
9865 ctx = f.file->private_data;
9866
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +00009867 io_run_task_work();
9868
Jens Axboeedafcce2019-01-09 09:16:05 -07009869 mutex_lock(&ctx->uring_lock);
9870 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9871 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009872 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9873 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009874out_fput:
9875 fdput(f);
9876 return ret;
9877}
9878
Jens Axboe2b188cc2019-01-07 10:46:33 -07009879static int __init io_uring_init(void)
9880{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009881#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9882 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9883 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9884} while (0)
9885
9886#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9887 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9888 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9889 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9890 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9891 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9892 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9893 BUILD_BUG_SQE_ELEM(8, __u64, off);
9894 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9895 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009896 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009897 BUILD_BUG_SQE_ELEM(24, __u32, len);
9898 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9899 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9900 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9901 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009902 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9903 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009904 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9905 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9906 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9907 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9908 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9909 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9910 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9911 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009912 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009913 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9914 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9915 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009916 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009917
Jens Axboed3656342019-12-18 09:50:26 -07009918 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009919 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -07009920 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
9921 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009922 return 0;
9923};
9924__initcall(io_uring_init);