blob: f66a8137e1251533ec23622251adf14f9aac38e5 [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 Axboe2b188cc2019-01-07 10:46:33 -0700342
Hristo Venev75b28af2019-08-26 17:23:46 +0000343 /*
344 * Ring buffer of indices into array of io_uring_sqe, which is
345 * mmapped by the application using the IORING_OFF_SQES offset.
346 *
347 * This indirection could e.g. be used to assign fixed
348 * io_uring_sqe entries to operations and only submit them to
349 * the queue when needed.
350 *
351 * The kernel modifies neither the indices array nor the entries
352 * array.
353 */
354 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700355 unsigned cached_sq_head;
356 unsigned sq_entries;
357 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700358 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600359 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100360 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700361 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600362
Jens Axboee9418942021-02-19 12:33:30 -0700363 /* hashed buffered write serialization */
364 struct io_wq_hash *hash_map;
365
Jens Axboede0617e2019-04-06 21:51:27 -0600366 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600367 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700368 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700369
Jens Axboead3eb2c2019-12-18 17:12:20 -0700370 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700371 } ____cacheline_aligned_in_smp;
372
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700373 struct {
374 struct mutex uring_lock;
375 wait_queue_head_t wait;
376 } ____cacheline_aligned_in_smp;
377
378 struct io_submit_state submit_state;
379
Hristo Venev75b28af2019-08-26 17:23:46 +0000380 struct io_rings *rings;
381
Jens Axboe2aede0e2020-09-14 10:45:53 -0600382 /*
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700383 * For SQPOLL usage
Jens Axboe2aede0e2020-09-14 10:45:53 -0600384 */
385 struct task_struct *sqo_task;
386
387 /* Only used for accounting purposes */
388 struct mm_struct *mm_account;
389
Jens Axboe534ca6d2020-09-02 13:52:19 -0600390 struct io_sq_data *sq_data; /* if using sq thread polling */
391
Jens Axboe90554202020-09-03 12:12:41 -0600392 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600393 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700394
Jens Axboe6b063142019-01-10 22:13:58 -0700395 /*
396 * If used, fixed file set. Writers must ensure that ->refs is dead,
397 * readers must ensure that ->refs is alive as long as the file* is
398 * used. Only updated through io_uring_register(2).
399 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000400 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700401 unsigned nr_user_files;
402
Jens Axboeedafcce2019-01-09 09:16:05 -0700403 /* if used, fixed mapped user buffers */
404 unsigned nr_user_bufs;
405 struct io_mapped_ubuf *user_bufs;
406
Jens Axboe2b188cc2019-01-07 10:46:33 -0700407 struct user_struct *user;
408
Jens Axboe0f158b42020-05-14 17:18:39 -0600409 struct completion ref_comp;
410 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700411
412#if defined(CONFIG_UNIX)
413 struct socket *ring_sock;
414#endif
415
Jens Axboe5a2e7452020-02-23 16:23:11 -0700416 struct idr io_buffer_idr;
417
Jens Axboe071698e2020-01-28 10:04:42 -0700418 struct idr personality_idr;
419
Jens Axboe206aefd2019-11-07 18:27:42 -0700420 struct {
421 unsigned cached_cq_tail;
422 unsigned cq_entries;
423 unsigned cq_mask;
424 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500425 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700426 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700427 struct wait_queue_head cq_wait;
428 struct fasync_struct *cq_fasync;
429 struct eventfd_ctx *cq_ev_fd;
430 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700431
432 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700433 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700434
Jens Axboedef596e2019-01-09 08:59:42 -0700435 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300436 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700437 * io_uring instances that don't use IORING_SETUP_SQPOLL.
438 * For SQPOLL, only the single threaded io_sq_thread() will
439 * manipulate the list, hence no extra locking is needed there.
440 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300441 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700442 struct hlist_head *cancel_hash;
443 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700444 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600445
446 spinlock_t inflight_lock;
447 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700448 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600449
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000450 struct delayed_work rsrc_put_work;
451 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000452 struct list_head rsrc_ref_list;
453 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600454
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200455 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700456
Jens Axboe7c25c0d2021-02-16 07:17:00 -0700457 /* exit task_work */
458 struct callback_head *exit_task_work;
459
Jens Axboee9418942021-02-19 12:33:30 -0700460 struct wait_queue_head hash_wait;
461
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700462 /* Keep this last, we don't need it for the fast path */
463 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700464};
465
Jens Axboe09bb8392019-03-13 12:39:28 -0600466/*
467 * First field must be the file pointer in all the
468 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
469 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700470struct io_poll_iocb {
471 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000472 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700473 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600474 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700475 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700476 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700477};
478
Pavel Begunkov018043b2020-10-27 23:17:18 +0000479struct io_poll_remove {
480 struct file *file;
481 u64 addr;
482};
483
Jens Axboeb5dba592019-12-11 14:02:38 -0700484struct io_close {
485 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700486 int fd;
487};
488
Jens Axboead8a48a2019-11-15 08:49:11 -0700489struct io_timeout_data {
490 struct io_kiocb *req;
491 struct hrtimer timer;
492 struct timespec64 ts;
493 enum hrtimer_mode mode;
494};
495
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700496struct io_accept {
497 struct file *file;
498 struct sockaddr __user *addr;
499 int __user *addr_len;
500 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600501 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700502};
503
504struct io_sync {
505 struct file *file;
506 loff_t len;
507 loff_t off;
508 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700509 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700510};
511
Jens Axboefbf23842019-12-17 18:45:56 -0700512struct io_cancel {
513 struct file *file;
514 u64 addr;
515};
516
Jens Axboeb29472e2019-12-17 18:50:29 -0700517struct io_timeout {
518 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300519 u32 off;
520 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300521 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000522 /* head of the link, used by linked timeouts only */
523 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700524};
525
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100526struct io_timeout_rem {
527 struct file *file;
528 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000529
530 /* timeout update */
531 struct timespec64 ts;
532 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100533};
534
Jens Axboe9adbd452019-12-20 08:45:55 -0700535struct io_rw {
536 /* NOTE: kiocb has the file as the first member, so don't do it here */
537 struct kiocb kiocb;
538 u64 addr;
539 u64 len;
540};
541
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700542struct io_connect {
543 struct file *file;
544 struct sockaddr __user *addr;
545 int addr_len;
546};
547
Jens Axboee47293f2019-12-20 08:58:21 -0700548struct io_sr_msg {
549 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700550 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300551 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700552 void __user *buf;
553 };
Jens Axboee47293f2019-12-20 08:58:21 -0700554 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700555 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700556 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700557 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700558};
559
Jens Axboe15b71ab2019-12-11 11:20:36 -0700560struct io_open {
561 struct file *file;
562 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700563 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700564 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600565 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700566};
567
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000568struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700569 struct file *file;
570 u64 arg;
571 u32 nr_args;
572 u32 offset;
573};
574
Jens Axboe4840e412019-12-25 22:03:45 -0700575struct io_fadvise {
576 struct file *file;
577 u64 offset;
578 u32 len;
579 u32 advice;
580};
581
Jens Axboec1ca7572019-12-25 22:18:28 -0700582struct io_madvise {
583 struct file *file;
584 u64 addr;
585 u32 len;
586 u32 advice;
587};
588
Jens Axboe3e4827b2020-01-08 15:18:09 -0700589struct io_epoll {
590 struct file *file;
591 int epfd;
592 int op;
593 int fd;
594 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700595};
596
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300597struct io_splice {
598 struct file *file_out;
599 struct file *file_in;
600 loff_t off_out;
601 loff_t off_in;
602 u64 len;
603 unsigned int flags;
604};
605
Jens Axboeddf0322d2020-02-23 16:41:33 -0700606struct io_provide_buf {
607 struct file *file;
608 __u64 addr;
609 __s32 len;
610 __u32 bgid;
611 __u16 nbufs;
612 __u16 bid;
613};
614
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700615struct io_statx {
616 struct file *file;
617 int dfd;
618 unsigned int mask;
619 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700620 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700621 struct statx __user *buffer;
622};
623
Jens Axboe36f4fa62020-09-05 11:14:22 -0600624struct io_shutdown {
625 struct file *file;
626 int how;
627};
628
Jens Axboe80a261f2020-09-28 14:23:58 -0600629struct io_rename {
630 struct file *file;
631 int old_dfd;
632 int new_dfd;
633 struct filename *oldpath;
634 struct filename *newpath;
635 int flags;
636};
637
Jens Axboe14a11432020-09-28 14:27:37 -0600638struct io_unlink {
639 struct file *file;
640 int dfd;
641 int flags;
642 struct filename *filename;
643};
644
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300645struct io_completion {
646 struct file *file;
647 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300648 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300649};
650
Jens Axboef499a022019-12-02 16:28:46 -0700651struct io_async_connect {
652 struct sockaddr_storage address;
653};
654
Jens Axboe03b12302019-12-02 18:50:25 -0700655struct io_async_msghdr {
656 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000657 /* points to an allocated iov, if NULL we use fast_iov instead */
658 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700659 struct sockaddr __user *uaddr;
660 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700661 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700662};
663
Jens Axboef67676d2019-12-02 11:03:47 -0700664struct io_async_rw {
665 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600666 const struct iovec *free_iovec;
667 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600668 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600669 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700670};
671
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300672enum {
673 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
674 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
675 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
676 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
677 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700678 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300679
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300680 REQ_F_FAIL_LINK_BIT,
681 REQ_F_INFLIGHT_BIT,
682 REQ_F_CUR_POS_BIT,
683 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300684 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300685 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300686 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700687 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700688 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600689 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800690 REQ_F_WORK_INITIALIZED_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),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800732 /* io_wq_work is initialized */
733 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100734 /* linked timeout is active, i.e. prepared by link's head */
735 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000736 /* completion is deferred through io_comp_state */
737 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700738};
739
740struct async_poll {
741 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600742 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300743};
744
Jens Axboe7cbf1722021-02-10 00:03:20 +0000745struct io_task_work {
746 struct io_wq_work_node node;
747 task_work_func_t func;
748};
749
Jens Axboe09bb8392019-03-13 12:39:28 -0600750/*
751 * NOTE! Each of the iocb union members has the file pointer
752 * as the first entry in their struct definition. So you can
753 * access the file pointer through any of the sub-structs,
754 * or directly as just 'ki_filp' in this struct.
755 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700756struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700757 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600758 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700759 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700760 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000761 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700762 struct io_accept accept;
763 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700764 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700765 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100766 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700767 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700768 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700769 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700770 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000771 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700772 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700773 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700774 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300775 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700776 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700777 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600778 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600779 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600780 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300781 /* use only after cleaning per-op data, see io_clean_op() */
782 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700783 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700784
Jens Axboee8c2bc12020-08-15 18:44:09 -0700785 /* opcode allocated if it needs to store data for async defer */
786 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700787 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800788 /* polled IO has completed */
789 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700790
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700791 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300792 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700793
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300794 struct io_ring_ctx *ctx;
795 unsigned int flags;
796 refcount_t refs;
797 struct task_struct *task;
798 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700799
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000800 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000801 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700802
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300803 /*
804 * 1. used with ctx->iopoll_list with reads/writes
805 * 2. to track reqs with ->files (see io_op_def::file_table)
806 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300807 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000808 union {
809 struct io_task_work io_task_work;
810 struct callback_head task_work;
811 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300812 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
813 struct hlist_node hash_node;
814 struct async_poll *apoll;
815 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700816};
817
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300818struct io_defer_entry {
819 struct list_head list;
820 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300821 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300822};
823
Jens Axboed3656342019-12-18 09:50:26 -0700824struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700825 /* needs req->file assigned */
826 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700827 /* hash wq insertion if file is a regular file */
828 unsigned hash_reg_file : 1;
829 /* unbound wq insertion if file is a non-regular file */
830 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700831 /* opcode is not supported by this kernel */
832 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700833 /* set if opcode supports polled "wait" */
834 unsigned pollin : 1;
835 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700836 /* op supports buffer selection */
837 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700838 /* must always have async data allocated */
839 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600840 /* should block plug */
841 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700842 /* size of async data needed, if any */
843 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700844};
845
Jens Axboe09186822020-10-13 15:01:40 -0600846static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300847 [IORING_OP_NOP] = {},
848 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700849 .needs_file = 1,
850 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700851 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700852 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700853 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600854 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700855 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700856 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300857 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700858 .needs_file = 1,
859 .hash_reg_file = 1,
860 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700861 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700862 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600863 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700864 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700865 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300866 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700867 .needs_file = 1,
868 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300869 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700870 .needs_file = 1,
871 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700872 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600873 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700874 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700875 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300876 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700877 .needs_file = 1,
878 .hash_reg_file = 1,
879 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700880 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600881 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700882 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700883 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300884 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700885 .needs_file = 1,
886 .unbound_nonreg_file = 1,
887 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300888 [IORING_OP_POLL_REMOVE] = {},
889 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700890 .needs_file = 1,
891 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300892 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700893 .needs_file = 1,
894 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700895 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700896 .needs_async_data = 1,
897 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700898 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300899 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700900 .needs_file = 1,
901 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700902 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700903 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700904 .needs_async_data = 1,
905 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700906 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300907 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700908 .needs_async_data = 1,
909 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700910 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000911 [IORING_OP_TIMEOUT_REMOVE] = {
912 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000913 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300914 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700915 .needs_file = 1,
916 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700917 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700918 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300919 [IORING_OP_ASYNC_CANCEL] = {},
920 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700921 .needs_async_data = 1,
922 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700923 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300924 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700925 .needs_file = 1,
926 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700927 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700928 .needs_async_data = 1,
929 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700930 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300931 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700932 .needs_file = 1,
933 },
Jens Axboe44526be2021-02-15 13:32:18 -0700934 [IORING_OP_OPENAT] = {},
935 [IORING_OP_CLOSE] = {},
936 [IORING_OP_FILES_UPDATE] = {},
937 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300938 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700939 .needs_file = 1,
940 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700941 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700942 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600943 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700944 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700945 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300946 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700947 .needs_file = 1,
948 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700949 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600950 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700951 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700952 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300953 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700954 .needs_file = 1,
955 },
Jens Axboe44526be2021-02-15 13:32:18 -0700956 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700958 .needs_file = 1,
959 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700960 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700961 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300962 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700963 .needs_file = 1,
964 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700965 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700966 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700967 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300968 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700969 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700970 [IORING_OP_EPOLL_CTL] = {
971 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700972 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300973 [IORING_OP_SPLICE] = {
974 .needs_file = 1,
975 .hash_reg_file = 1,
976 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700977 },
978 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700979 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300980 [IORING_OP_TEE] = {
981 .needs_file = 1,
982 .hash_reg_file = 1,
983 .unbound_nonreg_file = 1,
984 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600985 [IORING_OP_SHUTDOWN] = {
986 .needs_file = 1,
987 },
Jens Axboe44526be2021-02-15 13:32:18 -0700988 [IORING_OP_RENAMEAT] = {},
989 [IORING_OP_UNLINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700990};
991
Pavel Begunkov9936c7c2021-02-04 13:51:56 +0000992static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
993 struct task_struct *task,
994 struct files_struct *files);
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700995static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000996static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +0000997static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000998 struct io_ring_ctx *ctx);
Pavel Begunkovf2303b12021-02-20 18:03:49 +0000999static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001000
Pavel Begunkov23faba32021-02-11 18:28:22 +00001001static bool io_rw_reissue(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001002static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001003static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001004static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001005static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001006static void io_dismantle_req(struct io_kiocb *req);
1007static void io_put_task(struct task_struct *task, int nr);
1008static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001009static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001010static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001011static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001012static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001013 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001014 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001015static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001016static struct file *io_file_get(struct io_submit_state *state,
1017 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001018static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001019static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001020
Pavel Begunkov847595d2021-02-04 13:52:06 +00001021static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1022 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001023static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1024 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001025 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001026static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001027static void io_submit_flush_completions(struct io_comp_state *cs,
1028 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001029
Jens Axboe2b188cc2019-01-07 10:46:33 -07001030static struct kmem_cache *req_cachep;
1031
Jens Axboe09186822020-10-13 15:01:40 -06001032static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001033
1034struct sock *io_uring_get_socket(struct file *file)
1035{
1036#if defined(CONFIG_UNIX)
1037 if (file->f_op == &io_uring_fops) {
1038 struct io_ring_ctx *ctx = file->private_data;
1039
1040 return ctx->ring_sock->sk;
1041 }
1042#endif
1043 return NULL;
1044}
1045EXPORT_SYMBOL(io_uring_get_socket);
1046
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001047#define io_for_each_link(pos, head) \
1048 for (pos = (head); pos; pos = pos->link)
1049
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001050static inline void io_clean_op(struct io_kiocb *req)
1051{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001052 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001053 __io_clean_op(req);
1054}
1055
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001056static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001057{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001058 struct io_ring_ctx *ctx = req->ctx;
1059
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001060 if (!req->fixed_rsrc_refs) {
1061 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1062 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001063 }
1064}
1065
Pavel Begunkov88f171a2021-02-20 18:03:50 +00001066static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1067{
1068 if (!percpu_ref_tryget(ref)) {
1069 /* already at zero, wait for ->release() */
1070 if (!try_wait_for_completion(compl))
1071 synchronize_rcu();
1072 return false;
1073 }
1074
1075 percpu_ref_resurrect(ref);
1076 reinit_completion(compl);
1077 percpu_ref_put(ref);
1078 return true;
1079}
1080
Pavel Begunkov08d23632020-11-06 13:00:22 +00001081static bool io_match_task(struct io_kiocb *head,
1082 struct task_struct *task,
1083 struct files_struct *files)
1084{
1085 struct io_kiocb *req;
1086
Jens Axboe84965ff2021-01-23 15:51:11 -07001087 if (task && head->task != task) {
1088 /* in terms of cancelation, always match if req task is dead */
1089 if (head->task->flags & PF_EXITING)
1090 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001091 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001092 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001093 if (!files)
1094 return true;
1095
1096 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001097 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1098 continue;
1099 if (req->file && req->file->f_op == &io_uring_fops)
1100 return true;
Jens Axboe4379bf82021-02-15 13:40:22 -07001101 if (req->task->files == files)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001102 return true;
1103 }
1104 return false;
1105}
1106
Jens Axboec40f6372020-06-25 15:39:59 -06001107static inline void req_set_fail_links(struct io_kiocb *req)
1108{
1109 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1110 req->flags |= REQ_F_FAIL_LINK;
1111}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001112
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001113static inline void __io_req_init_async(struct io_kiocb *req)
1114{
1115 memset(&req->work, 0, sizeof(req->work));
1116 req->flags |= REQ_F_WORK_INITIALIZED;
1117}
1118
Jens Axboe1e6fa522020-10-15 08:46:24 -06001119/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001120 * Note: must call io_req_init_async() for the first time you
1121 * touch any members of io_wq_work.
1122 */
1123static inline void io_req_init_async(struct io_kiocb *req)
1124{
1125 if (req->flags & REQ_F_WORK_INITIALIZED)
1126 return;
1127
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001128 __io_req_init_async(req);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001129}
1130
Jens Axboe2b188cc2019-01-07 10:46:33 -07001131static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1132{
1133 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1134
Jens Axboe0f158b42020-05-14 17:18:39 -06001135 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136}
1137
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001138static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1139{
1140 return !req->timeout.off;
1141}
1142
Jens Axboe2b188cc2019-01-07 10:46:33 -07001143static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1144{
1145 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001146 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001147
1148 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1149 if (!ctx)
1150 return NULL;
1151
Jens Axboe78076bb2019-12-04 19:56:40 -07001152 /*
1153 * Use 5 bits less than the max cq entries, that should give us around
1154 * 32 entries per hash list if totally full and uniformly spread.
1155 */
1156 hash_bits = ilog2(p->cq_entries);
1157 hash_bits -= 5;
1158 if (hash_bits <= 0)
1159 hash_bits = 1;
1160 ctx->cancel_hash_bits = hash_bits;
1161 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1162 GFP_KERNEL);
1163 if (!ctx->cancel_hash)
1164 goto err;
1165 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1166
Roman Gushchin21482892019-05-07 10:01:48 -07001167 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001168 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1169 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001170
1171 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001172 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001173 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001174 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001175 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001176 init_completion(&ctx->ref_comp);
1177 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001178 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001179 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001180 mutex_init(&ctx->uring_lock);
1181 init_waitqueue_head(&ctx->wait);
1182 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001183 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001184 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001185 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001186 spin_lock_init(&ctx->inflight_lock);
1187 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001188 spin_lock_init(&ctx->rsrc_ref_lock);
1189 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001190 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1191 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001192 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001193 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001194 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001195err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001196 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001197 kfree(ctx);
1198 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001199}
1200
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001201static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001202{
Jens Axboe2bc99302020-07-09 09:43:27 -06001203 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1204 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001205
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001206 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001207 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001208 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001209
Bob Liu9d858b22019-11-13 18:06:25 +08001210 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001211}
1212
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001213static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001214{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001215 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001216 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001217
Jens Axboe4379bf82021-02-15 13:40:22 -07001218 if (req->work.creds) {
1219 put_cred(req->work.creds);
1220 req->work.creds = NULL;
1221 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001222 if (req->flags & REQ_F_INFLIGHT) {
1223 struct io_ring_ctx *ctx = req->ctx;
1224 struct io_uring_task *tctx = req->task->io_uring;
1225 unsigned long flags;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001226
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001227 spin_lock_irqsave(&ctx->inflight_lock, flags);
1228 list_del(&req->inflight_entry);
1229 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1230 req->flags &= ~REQ_F_INFLIGHT;
1231 if (atomic_read(&tctx->in_idle))
1232 wake_up(&tctx->wait);
1233 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001234
Pavel Begunkove86d0042021-02-01 18:59:54 +00001235 req->flags &= ~REQ_F_WORK_INITIALIZED;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001236}
1237
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001238static void io_req_track_inflight(struct io_kiocb *req)
1239{
1240 struct io_ring_ctx *ctx = req->ctx;
1241
1242 if (!(req->flags & REQ_F_INFLIGHT)) {
1243 io_req_init_async(req);
1244 req->flags |= REQ_F_INFLIGHT;
1245
1246 spin_lock_irq(&ctx->inflight_lock);
1247 list_add(&req->inflight_entry, &ctx->inflight_list);
1248 spin_unlock_irq(&ctx->inflight_lock);
1249 }
1250}
1251
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001252static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001253{
Jens Axboed3656342019-12-18 09:50:26 -07001254 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001255 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001256
Pavel Begunkov16d59802020-07-12 16:16:47 +03001257 io_req_init_async(req);
1258
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001259 if (req->flags & REQ_F_FORCE_ASYNC)
1260 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1261
Jens Axboed3656342019-12-18 09:50:26 -07001262 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001263 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001264 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001265 } else {
1266 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001267 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001268 }
Jens Axboe4379bf82021-02-15 13:40:22 -07001269 if (!req->work.creds)
1270 req->work.creds = get_current_cred();
Jens Axboe561fb042019-10-24 07:25:42 -06001271}
1272
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001273static void io_prep_async_link(struct io_kiocb *req)
1274{
1275 struct io_kiocb *cur;
1276
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001277 io_for_each_link(cur, req)
1278 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001279}
1280
Jens Axboe7271ef32020-08-10 09:55:22 -06001281static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001282{
Jackie Liua197f662019-11-08 08:09:12 -07001283 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001284 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001285 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001286
Jens Axboe3bfe6102021-02-16 14:15:30 -07001287 BUG_ON(!tctx);
1288 BUG_ON(!tctx->io_wq);
1289
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001290 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1291 &req->work, req->flags);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001292 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001293 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001294}
1295
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001296static void io_queue_async_work(struct io_kiocb *req)
1297{
Jens Axboe7271ef32020-08-10 09:55:22 -06001298 struct io_kiocb *link;
1299
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001300 /* init ->work of the whole link before punting */
1301 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001302 link = __io_queue_async_work(req);
1303
1304 if (link)
1305 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001306}
1307
Jens Axboe5262f562019-09-17 12:26:57 -06001308static void io_kill_timeout(struct io_kiocb *req)
1309{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001310 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001311 int ret;
1312
Jens Axboee8c2bc12020-08-15 18:44:09 -07001313 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001314 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001315 atomic_set(&req->ctx->cq_timeouts,
1316 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001317 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001318 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001319 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001320 }
1321}
1322
Jens Axboe76e1b642020-09-26 15:05:03 -06001323/*
1324 * Returns true if we found and killed one or more timeouts
1325 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001326static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1327 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001328{
1329 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001330 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001331
1332 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001333 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001334 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001335 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001336 canceled++;
1337 }
Jens Axboef3606e32020-09-22 08:18:24 -06001338 }
Jens Axboe5262f562019-09-17 12:26:57 -06001339 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001340 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001341}
1342
Pavel Begunkov04518942020-05-26 20:34:05 +03001343static void __io_queue_deferred(struct io_ring_ctx *ctx)
1344{
1345 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001346 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1347 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001348
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001349 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001350 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001351 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001352 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001353 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001354 } while (!list_empty(&ctx->defer_list));
1355}
1356
Pavel Begunkov360428f2020-05-30 14:54:17 +03001357static void io_flush_timeouts(struct io_ring_ctx *ctx)
1358{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001359 u32 seq;
1360
1361 if (list_empty(&ctx->timeout_list))
1362 return;
1363
1364 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1365
1366 do {
1367 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001368 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001369 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001370
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001371 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001372 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001373
1374 /*
1375 * Since seq can easily wrap around over time, subtract
1376 * the last seq at which timeouts were flushed before comparing.
1377 * Assuming not more than 2^31-1 events have happened since,
1378 * these subtractions won't have wrapped, so we can check if
1379 * target is in [last_seq, current_seq] by comparing the two.
1380 */
1381 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1382 events_got = seq - ctx->cq_last_tm_flush;
1383 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001384 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001385
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001386 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001387 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001388 } while (!list_empty(&ctx->timeout_list));
1389
1390 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001391}
1392
Jens Axboede0617e2019-04-06 21:51:27 -06001393static void io_commit_cqring(struct io_ring_ctx *ctx)
1394{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001395 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001396
1397 /* order cqe stores with ring update */
1398 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001399
Pavel Begunkov04518942020-05-26 20:34:05 +03001400 if (unlikely(!list_empty(&ctx->defer_list)))
1401 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001402}
1403
Jens Axboe90554202020-09-03 12:12:41 -06001404static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1405{
1406 struct io_rings *r = ctx->rings;
1407
1408 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1409}
1410
Pavel Begunkov888aae22021-01-19 13:32:39 +00001411static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1412{
1413 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1414}
1415
Jens Axboe2b188cc2019-01-07 10:46:33 -07001416static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1417{
Hristo Venev75b28af2019-08-26 17:23:46 +00001418 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001419 unsigned tail;
1420
Stefan Bühler115e12e2019-04-24 23:54:18 +02001421 /*
1422 * writes to the cq entry need to come after reading head; the
1423 * control dependency is enough as we're using WRITE_ONCE to
1424 * fill the cq entry
1425 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001426 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001427 return NULL;
1428
Pavel Begunkov888aae22021-01-19 13:32:39 +00001429 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001430 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001431}
1432
Jens Axboef2842ab2020-01-08 11:04:00 -07001433static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1434{
Jens Axboef0b493e2020-02-01 21:30:11 -07001435 if (!ctx->cq_ev_fd)
1436 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001437 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1438 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001439 if (!ctx->eventfd_async)
1440 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001441 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001442}
1443
Jens Axboeb41e9852020-02-17 09:52:41 -07001444static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001445{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001446 /* see waitqueue_active() comment */
1447 smp_mb();
1448
Jens Axboe8c838782019-03-12 15:48:16 -06001449 if (waitqueue_active(&ctx->wait))
1450 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001451 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1452 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001453 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001454 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001455 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001456 wake_up_interruptible(&ctx->cq_wait);
1457 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1458 }
Jens Axboe8c838782019-03-12 15:48:16 -06001459}
1460
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001461static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1462{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001463 /* see waitqueue_active() comment */
1464 smp_mb();
1465
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001466 if (ctx->flags & IORING_SETUP_SQPOLL) {
1467 if (waitqueue_active(&ctx->wait))
1468 wake_up(&ctx->wait);
1469 }
1470 if (io_should_trigger_evfd(ctx))
1471 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001472 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001473 wake_up_interruptible(&ctx->cq_wait);
1474 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1475 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001476}
1477
Jens Axboec4a2ed72019-11-21 21:01:26 -07001478/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001479static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1480 struct task_struct *tsk,
1481 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001482{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001483 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001484 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001485 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001486 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001487 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001488 LIST_HEAD(list);
1489
Pavel Begunkove23de152020-12-17 00:24:37 +00001490 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1491 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001492
Jens Axboeb18032b2021-01-24 16:58:56 -07001493 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001494 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001495 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001496 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001497 continue;
1498
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001499 cqe = io_get_cqring(ctx);
1500 if (!cqe && !force)
1501 break;
1502
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001503 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001504 if (cqe) {
1505 WRITE_ONCE(cqe->user_data, req->user_data);
1506 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001507 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001508 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001509 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001510 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001511 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001512 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001513 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001514 }
1515
Pavel Begunkov09e88402020-12-17 00:24:38 +00001516 all_flushed = list_empty(&ctx->cq_overflow_list);
1517 if (all_flushed) {
1518 clear_bit(0, &ctx->sq_check_overflow);
1519 clear_bit(0, &ctx->cq_check_overflow);
1520 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1521 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001522
Jens Axboeb18032b2021-01-24 16:58:56 -07001523 if (posted)
1524 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001525 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001526 if (posted)
1527 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001528
1529 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001530 req = list_first_entry(&list, struct io_kiocb, compl.list);
1531 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001532 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001533 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001534
Pavel Begunkov09e88402020-12-17 00:24:38 +00001535 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001536}
1537
Pavel Begunkov6c503152021-01-04 20:36:36 +00001538static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1539 struct task_struct *tsk,
1540 struct files_struct *files)
1541{
1542 if (test_bit(0, &ctx->cq_check_overflow)) {
1543 /* iopoll syncs against uring_lock, not completion_lock */
1544 if (ctx->flags & IORING_SETUP_IOPOLL)
1545 mutex_lock(&ctx->uring_lock);
1546 __io_cqring_overflow_flush(ctx, force, tsk, files);
1547 if (ctx->flags & IORING_SETUP_IOPOLL)
1548 mutex_unlock(&ctx->uring_lock);
1549 }
1550}
1551
Jens Axboebcda7ba2020-02-23 16:42:51 -07001552static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001553{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001554 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001555 struct io_uring_cqe *cqe;
1556
Jens Axboe78e19bb2019-11-06 15:21:34 -07001557 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001558
Jens Axboe2b188cc2019-01-07 10:46:33 -07001559 /*
1560 * If we can't get a cq entry, userspace overflowed the
1561 * submission (by quite a lot). Increment the overflow count in
1562 * the ring.
1563 */
1564 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001565 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001566 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001567 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001568 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001569 } else if (ctx->cq_overflow_flushed ||
1570 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001571 /*
1572 * If we're in ring overflow flush mode, or in task cancel mode,
1573 * then we cannot store the request for later flushing, we need
1574 * to drop it on the floor.
1575 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001576 ctx->cached_cq_overflow++;
1577 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001578 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001579 if (list_empty(&ctx->cq_overflow_list)) {
1580 set_bit(0, &ctx->sq_check_overflow);
1581 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001582 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001583 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001584 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001585 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001586 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001587 refcount_inc(&req->refs);
1588 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001589 }
1590}
1591
Jens Axboebcda7ba2020-02-23 16:42:51 -07001592static void io_cqring_fill_event(struct io_kiocb *req, long res)
1593{
1594 __io_cqring_fill_event(req, res, 0);
1595}
1596
Jens Axboec7dae4b2021-02-09 19:53:37 -07001597static inline void io_req_complete_post(struct io_kiocb *req, long res,
1598 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001599{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001600 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001601 unsigned long flags;
1602
1603 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001604 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001605 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001606 /*
1607 * If we're the last reference to this request, add to our locked
1608 * free_list cache.
1609 */
1610 if (refcount_dec_and_test(&req->refs)) {
1611 struct io_comp_state *cs = &ctx->submit_state.comp;
1612
1613 io_dismantle_req(req);
1614 io_put_task(req->task, 1);
1615 list_add(&req->compl.list, &cs->locked_free_list);
1616 cs->locked_free_nr++;
1617 } else
1618 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001619 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1620
Jens Axboe8c838782019-03-12 15:48:16 -06001621 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001622 if (req) {
1623 io_queue_next(req);
1624 percpu_ref_put(&ctx->refs);
1625 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001626}
1627
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001628static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001629 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001630{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001631 io_clean_op(req);
1632 req->result = res;
1633 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001634 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001635}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001636
Pavel Begunkov889fca72021-02-10 00:03:09 +00001637static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1638 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001639{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001640 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1641 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001642 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001643 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001644}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001645
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001646static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001647{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001648 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001649}
1650
Jens Axboec7dae4b2021-02-09 19:53:37 -07001651static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001652{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001653 struct io_submit_state *state = &ctx->submit_state;
1654 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001655 struct io_kiocb *req = NULL;
1656
Jens Axboec7dae4b2021-02-09 19:53:37 -07001657 /*
1658 * If we have more than a batch's worth of requests in our IRQ side
1659 * locked cache, grab the lock and move them over to our submission
1660 * side cache.
1661 */
1662 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
1663 spin_lock_irq(&ctx->completion_lock);
1664 list_splice_init(&cs->locked_free_list, &cs->free_list);
1665 cs->locked_free_nr = 0;
1666 spin_unlock_irq(&ctx->completion_lock);
1667 }
1668
1669 while (!list_empty(&cs->free_list)) {
1670 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001671 compl.list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001672 list_del(&req->compl.list);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001673 state->reqs[state->free_reqs++] = req;
1674 if (state->free_reqs == ARRAY_SIZE(state->reqs))
1675 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001676 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001678 return req != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001679}
1680
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001681static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001683 struct io_submit_state *state = &ctx->submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001684
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001685 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001687 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001688 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001689 int ret;
1690
Jens Axboec7dae4b2021-02-09 19:53:37 -07001691 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001692 goto got_req;
1693
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001694 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1695 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06001696
1697 /*
1698 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1699 * retry single alloc to be on the safe side.
1700 */
1701 if (unlikely(ret <= 0)) {
1702 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1703 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00001704 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06001705 ret = 1;
1706 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001707 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001709got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001710 state->free_reqs--;
1711 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001712}
1713
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001714static inline void io_put_file(struct io_kiocb *req, struct file *file,
1715 bool fixed)
1716{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001717 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001718 fput(file);
1719}
1720
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001721static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001723 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001724
Jens Axboee8c2bc12020-08-15 18:44:09 -07001725 if (req->async_data)
1726 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001727 if (req->file)
1728 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001729 if (req->fixed_rsrc_refs)
1730 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001731 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001732}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001733
Pavel Begunkov7c660732021-01-25 11:42:21 +00001734static inline void io_put_task(struct task_struct *task, int nr)
1735{
1736 struct io_uring_task *tctx = task->io_uring;
1737
1738 percpu_counter_sub(&tctx->inflight, nr);
1739 if (unlikely(atomic_read(&tctx->in_idle)))
1740 wake_up(&tctx->wait);
1741 put_task_struct_many(task, nr);
1742}
1743
Pavel Begunkov216578e2020-10-13 09:44:00 +01001744static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001745{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001746 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001747
Pavel Begunkov216578e2020-10-13 09:44:00 +01001748 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001749 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001750
Pavel Begunkov3893f392021-02-10 00:03:15 +00001751 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001752 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001753}
1754
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001755static inline void io_remove_next_linked(struct io_kiocb *req)
1756{
1757 struct io_kiocb *nxt = req->link;
1758
1759 req->link = nxt->link;
1760 nxt->link = NULL;
1761}
1762
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001763static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001764{
Jackie Liua197f662019-11-08 08:09:12 -07001765 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001766 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001767 bool cancelled = false;
1768 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001769
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001770 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001771 link = req->link;
1772
Pavel Begunkov900fad42020-10-19 16:39:16 +01001773 /*
1774 * Can happen if a linked timeout fired and link had been like
1775 * req -> link t-out -> link t-out [-> ...]
1776 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001777 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1778 struct io_timeout_data *io = link->async_data;
1779 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001780
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001781 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001782 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001783 ret = hrtimer_try_to_cancel(&io->timer);
1784 if (ret != -1) {
1785 io_cqring_fill_event(link, -ECANCELED);
1786 io_commit_cqring(ctx);
1787 cancelled = true;
1788 }
1789 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001790 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001791 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001792
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001793 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001794 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001795 io_put_req(link);
1796 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001797}
1798
Jens Axboe4d7dd462019-11-20 13:03:52 -07001799
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001800static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001801{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001802 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07001803 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001804 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06001805
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001806 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001807 link = req->link;
1808 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06001809
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001810 while (link) {
1811 nxt = link->link;
1812 link->link = NULL;
1813
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001814 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001815 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001816
1817 /*
1818 * It's ok to free under spinlock as they're not linked anymore,
1819 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
1820 * work.fs->lock.
1821 */
1822 if (link->flags & REQ_F_WORK_INITIALIZED)
1823 io_put_req_deferred(link, 2);
1824 else
1825 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001826 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001827 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001828 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001829 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001830
Jens Axboe2665abf2019-11-05 12:40:47 -07001831 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001832}
1833
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001834static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001835{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001836 if (req->flags & REQ_F_LINK_TIMEOUT)
1837 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001838
Jens Axboe9e645e112019-05-10 16:07:28 -06001839 /*
1840 * If LINK is set, we have dependent requests in this chain. If we
1841 * didn't fail this request, queue the first one up, moving any other
1842 * dependencies to the next request. In case of failure, fail the rest
1843 * of the chain.
1844 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001845 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
1846 struct io_kiocb *nxt = req->link;
1847
1848 req->link = NULL;
1849 return nxt;
1850 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001851 io_fail_links(req);
1852 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001853}
Jens Axboe2665abf2019-11-05 12:40:47 -07001854
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001855static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001856{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00001857 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001858 return NULL;
1859 return __io_req_find_next(req);
1860}
1861
Jens Axboe7cbf1722021-02-10 00:03:20 +00001862static bool __tctx_task_work(struct io_uring_task *tctx)
1863{
Jens Axboe65453d12021-02-10 00:03:21 +00001864 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001865 struct io_wq_work_list list;
1866 struct io_wq_work_node *node;
1867
1868 if (wq_list_empty(&tctx->task_list))
1869 return false;
1870
Jens Axboe0b81e802021-02-16 10:33:53 -07001871 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001872 list = tctx->task_list;
1873 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001874 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001875
1876 node = list.first;
1877 while (node) {
1878 struct io_wq_work_node *next = node->next;
Jens Axboe65453d12021-02-10 00:03:21 +00001879 struct io_ring_ctx *this_ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001880 struct io_kiocb *req;
1881
1882 req = container_of(node, struct io_kiocb, io_task_work.node);
Jens Axboe65453d12021-02-10 00:03:21 +00001883 this_ctx = req->ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001884 req->task_work.func(&req->task_work);
1885 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00001886
1887 if (!ctx) {
1888 ctx = this_ctx;
1889 } else if (ctx != this_ctx) {
1890 mutex_lock(&ctx->uring_lock);
1891 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
1892 mutex_unlock(&ctx->uring_lock);
1893 ctx = this_ctx;
1894 }
1895 }
1896
1897 if (ctx && ctx->submit_state.comp.nr) {
1898 mutex_lock(&ctx->uring_lock);
1899 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
1900 mutex_unlock(&ctx->uring_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001901 }
1902
1903 return list.first != NULL;
1904}
1905
1906static void tctx_task_work(struct callback_head *cb)
1907{
1908 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
1909
1910 while (__tctx_task_work(tctx))
1911 cond_resched();
1912
1913 clear_bit(0, &tctx->task_state);
1914}
1915
1916static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
1917 enum task_work_notify_mode notify)
1918{
1919 struct io_uring_task *tctx = tsk->io_uring;
1920 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07001921 unsigned long flags;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001922 int ret;
1923
1924 WARN_ON_ONCE(!tctx);
1925
Jens Axboe0b81e802021-02-16 10:33:53 -07001926 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001927 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001928 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001929
1930 /* task_work already pending, we're done */
1931 if (test_bit(0, &tctx->task_state) ||
1932 test_and_set_bit(0, &tctx->task_state))
1933 return 0;
1934
1935 if (!task_work_add(tsk, &tctx->task_work, notify))
1936 return 0;
1937
1938 /*
1939 * Slow path - we failed, find and delete work. if the work is not
1940 * in the list, it got run and we're fine.
1941 */
1942 ret = 0;
Jens Axboe0b81e802021-02-16 10:33:53 -07001943 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001944 wq_list_for_each(node, prev, &tctx->task_list) {
1945 if (&req->io_task_work.node == node) {
1946 wq_list_del(&tctx->task_list, node, prev);
1947 ret = 1;
1948 break;
1949 }
1950 }
Jens Axboe0b81e802021-02-16 10:33:53 -07001951 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001952 clear_bit(0, &tctx->task_state);
1953 return ret;
1954}
1955
Jens Axboe355fb9e2020-10-22 20:19:35 -06001956static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06001957{
1958 struct task_struct *tsk = req->task;
1959 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06001960 enum task_work_notify_mode notify;
1961 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06001962
Jens Axboe6200b0a2020-09-13 14:38:30 -06001963 if (tsk->flags & PF_EXITING)
1964 return -ESRCH;
1965
Jens Axboec2c4c832020-07-01 15:37:11 -06001966 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001967 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1968 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1969 * processing task_work. There's no reliable way to tell if TWA_RESUME
1970 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001971 */
Jens Axboe91989c72020-10-16 09:02:26 -06001972 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06001973 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06001974 notify = TWA_SIGNAL;
1975
Jens Axboe7cbf1722021-02-10 00:03:20 +00001976 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06001977 if (!ret)
1978 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001979
Jens Axboec2c4c832020-07-01 15:37:11 -06001980 return ret;
1981}
1982
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001983static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00001984 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001985{
Jens Axboe7c25c0d2021-02-16 07:17:00 -07001986 struct io_ring_ctx *ctx = req->ctx;
1987 struct callback_head *head;
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001988
1989 init_task_work(&req->task_work, cb);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07001990 do {
1991 head = READ_ONCE(ctx->exit_task_work);
1992 req->task_work.next = head;
1993 } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00001994}
1995
Jens Axboec40f6372020-06-25 15:39:59 -06001996static void __io_req_task_cancel(struct io_kiocb *req, int error)
1997{
1998 struct io_ring_ctx *ctx = req->ctx;
1999
2000 spin_lock_irq(&ctx->completion_lock);
2001 io_cqring_fill_event(req, error);
2002 io_commit_cqring(ctx);
2003 spin_unlock_irq(&ctx->completion_lock);
2004
2005 io_cqring_ev_posted(ctx);
2006 req_set_fail_links(req);
2007 io_double_put_req(req);
2008}
2009
2010static void io_req_task_cancel(struct callback_head *cb)
2011{
2012 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002013 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002014
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002015 mutex_lock(&ctx->uring_lock);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002016 __io_req_task_cancel(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002017 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002018 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002019}
2020
2021static void __io_req_task_submit(struct io_kiocb *req)
2022{
2023 struct io_ring_ctx *ctx = req->ctx;
2024
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002025 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002026 mutex_lock(&ctx->uring_lock);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07002027 if (!ctx->sqo_dead && !(current->flags & PF_EXITING) && !current->in_execve)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002028 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002029 else
Jens Axboec40f6372020-06-25 15:39:59 -06002030 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002031 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002032}
2033
Jens Axboec40f6372020-06-25 15:39:59 -06002034static void io_req_task_submit(struct callback_head *cb)
2035{
2036 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2037
2038 __io_req_task_submit(req);
2039}
2040
2041static void io_req_task_queue(struct io_kiocb *req)
2042{
Jens Axboec40f6372020-06-25 15:39:59 -06002043 int ret;
2044
Jens Axboe7cbf1722021-02-10 00:03:20 +00002045 req->task_work.func = io_req_task_submit;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002046 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06002047 if (unlikely(ret)) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00002048 req->result = -ECANCELED;
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002049 percpu_ref_get(&req->ctx->refs);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002050 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002051 }
Jens Axboec40f6372020-06-25 15:39:59 -06002052}
2053
Pavel Begunkova3df76982021-02-18 22:32:52 +00002054static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2055{
2056 percpu_ref_get(&req->ctx->refs);
2057 req->result = ret;
2058 req->task_work.func = io_req_task_cancel;
2059
2060 if (unlikely(io_req_task_work_add(req)))
2061 io_req_task_work_add_fallback(req, io_req_task_cancel);
2062}
2063
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002064static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002065{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002066 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002067
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002068 if (nxt)
2069 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002070}
2071
Jens Axboe9e645e112019-05-10 16:07:28 -06002072static void io_free_req(struct io_kiocb *req)
2073{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002074 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002075 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002076}
2077
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002078struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002079 struct task_struct *task;
2080 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002081 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002082};
2083
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002084static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002085{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002086 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002087 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002088 rb->task = NULL;
2089}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002090
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002091static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2092 struct req_batch *rb)
2093{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002094 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002095 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002096 if (rb->ctx_refs)
2097 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002098}
2099
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002100static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2101 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002102{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002103 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002104
Jens Axboee3bc8e92020-09-24 08:45:57 -06002105 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002106 if (rb->task)
2107 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002108 rb->task = req->task;
2109 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002110 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002111 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002112 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002113
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002114 io_dismantle_req(req);
Pavel Begunkovbd759042021-02-12 03:23:50 +00002115 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002116 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002117 else
2118 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002119}
2120
Pavel Begunkov905c1722021-02-10 00:03:14 +00002121static void io_submit_flush_completions(struct io_comp_state *cs,
2122 struct io_ring_ctx *ctx)
2123{
2124 int i, nr = cs->nr;
2125 struct io_kiocb *req;
2126 struct req_batch rb;
2127
2128 io_init_req_batch(&rb);
2129 spin_lock_irq(&ctx->completion_lock);
2130 for (i = 0; i < nr; i++) {
2131 req = cs->reqs[i];
2132 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2133 }
2134 io_commit_cqring(ctx);
2135 spin_unlock_irq(&ctx->completion_lock);
2136
2137 io_cqring_ev_posted(ctx);
2138 for (i = 0; i < nr; i++) {
2139 req = cs->reqs[i];
2140
2141 /* submission and completion refs */
2142 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002143 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002144 }
2145
2146 io_req_free_batch_finish(ctx, &rb);
2147 cs->nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002148}
2149
Jens Axboeba816ad2019-09-28 11:36:45 -06002150/*
2151 * Drop reference to request, return next in chain (if there is one) if this
2152 * was the last reference to this request.
2153 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002154static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002155{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002156 struct io_kiocb *nxt = NULL;
2157
Jens Axboe2a44f462020-02-25 13:25:41 -07002158 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002159 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002160 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002161 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002162 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002163}
2164
Jens Axboe2b188cc2019-01-07 10:46:33 -07002165static void io_put_req(struct io_kiocb *req)
2166{
Jens Axboedef596e2019-01-09 08:59:42 -07002167 if (refcount_dec_and_test(&req->refs))
2168 io_free_req(req);
2169}
2170
Pavel Begunkov216578e2020-10-13 09:44:00 +01002171static void io_put_req_deferred_cb(struct callback_head *cb)
2172{
2173 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2174
2175 io_free_req(req);
2176}
2177
2178static void io_free_req_deferred(struct io_kiocb *req)
2179{
2180 int ret;
2181
Jens Axboe7cbf1722021-02-10 00:03:20 +00002182 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002183 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002184 if (unlikely(ret))
2185 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002186}
2187
2188static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2189{
2190 if (refcount_sub_and_test(refs, &req->refs))
2191 io_free_req_deferred(req);
2192}
2193
Jens Axboe978db572019-11-14 22:39:04 -07002194static void io_double_put_req(struct io_kiocb *req)
2195{
2196 /* drop both submit and complete references */
2197 if (refcount_sub_and_test(2, &req->refs))
2198 io_free_req(req);
2199}
2200
Pavel Begunkov6c503152021-01-04 20:36:36 +00002201static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002202{
2203 /* See comment at the top of this file */
2204 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002205 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002206}
2207
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002208static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2209{
2210 struct io_rings *rings = ctx->rings;
2211
2212 /* make sure SQ entry isn't read before tail */
2213 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2214}
2215
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002216static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002217{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002218 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002219
Jens Axboebcda7ba2020-02-23 16:42:51 -07002220 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2221 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002222 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002223 kfree(kbuf);
2224 return cflags;
2225}
2226
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002227static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2228{
2229 struct io_buffer *kbuf;
2230
2231 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2232 return io_put_kbuf(req, kbuf);
2233}
2234
Jens Axboe4c6e2772020-07-01 11:29:10 -06002235static inline bool io_run_task_work(void)
2236{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002237 /*
2238 * Not safe to run on exiting task, and the task_work handling will
2239 * not add work to such a task.
2240 */
2241 if (unlikely(current->flags & PF_EXITING))
2242 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002243 if (current->task_works) {
2244 __set_current_state(TASK_RUNNING);
2245 task_work_run();
2246 return true;
2247 }
2248
2249 return false;
2250}
2251
Jens Axboedef596e2019-01-09 08:59:42 -07002252/*
2253 * Find and free completed poll iocbs
2254 */
2255static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2256 struct list_head *done)
2257{
Jens Axboe8237e042019-12-28 10:48:22 -07002258 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002259 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002260
2261 /* order with ->result store in io_complete_rw_iopoll() */
2262 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002263
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002264 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002265 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002266 int cflags = 0;
2267
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002268 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002269 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002270
Pavel Begunkovf1613402021-02-11 18:28:21 +00002271 if (READ_ONCE(req->result) == -EAGAIN) {
2272 req->iopoll_completed = 0;
Pavel Begunkov23faba32021-02-11 18:28:22 +00002273 if (io_rw_reissue(req))
Pavel Begunkovf1613402021-02-11 18:28:21 +00002274 continue;
2275 }
2276
Jens Axboebcda7ba2020-02-23 16:42:51 -07002277 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002278 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002279
2280 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002281 (*nr_events)++;
2282
Pavel Begunkovc3524382020-06-28 12:52:32 +03002283 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002284 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002285 }
Jens Axboedef596e2019-01-09 08:59:42 -07002286
Jens Axboe09bb8392019-03-13 12:39:28 -06002287 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002288 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002289 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002290}
2291
Jens Axboedef596e2019-01-09 08:59:42 -07002292static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2293 long min)
2294{
2295 struct io_kiocb *req, *tmp;
2296 LIST_HEAD(done);
2297 bool spin;
2298 int ret;
2299
2300 /*
2301 * Only spin for completions if we don't have multiple devices hanging
2302 * off our complete list, and we're under the requested amount.
2303 */
2304 spin = !ctx->poll_multi_file && *nr_events < min;
2305
2306 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002307 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002308 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002309
2310 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002311 * Move completed and retryable entries to our local lists.
2312 * If we find a request that requires polling, break out
2313 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002314 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002315 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002316 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002317 continue;
2318 }
2319 if (!list_empty(&done))
2320 break;
2321
2322 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2323 if (ret < 0)
2324 break;
2325
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002326 /* iopoll may have completed current req */
2327 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002328 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002329
Jens Axboedef596e2019-01-09 08:59:42 -07002330 if (ret && spin)
2331 spin = false;
2332 ret = 0;
2333 }
2334
2335 if (!list_empty(&done))
2336 io_iopoll_complete(ctx, nr_events, &done);
2337
2338 return ret;
2339}
2340
2341/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002342 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002343 * non-spinning poll check - we'll still enter the driver poll loop, but only
2344 * as a non-spinning completion check.
2345 */
2346static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2347 long min)
2348{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002349 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002350 int ret;
2351
2352 ret = io_do_iopoll(ctx, nr_events, min);
2353 if (ret < 0)
2354 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002355 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002356 return 0;
2357 }
2358
2359 return 1;
2360}
2361
2362/*
2363 * We can't just wait for polled events to come to us, we have to actively
2364 * find and complete them.
2365 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002366static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002367{
2368 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2369 return;
2370
2371 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002372 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002373 unsigned int nr_events = 0;
2374
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002375 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002376
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002377 /* let it sleep and repeat later if can't complete a request */
2378 if (nr_events == 0)
2379 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002380 /*
2381 * Ensure we allow local-to-the-cpu processing to take place,
2382 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002383 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002384 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002385 if (need_resched()) {
2386 mutex_unlock(&ctx->uring_lock);
2387 cond_resched();
2388 mutex_lock(&ctx->uring_lock);
2389 }
Jens Axboedef596e2019-01-09 08:59:42 -07002390 }
2391 mutex_unlock(&ctx->uring_lock);
2392}
2393
Pavel Begunkov7668b922020-07-07 16:36:21 +03002394static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002395{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002396 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002397 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002398
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002399 /*
2400 * We disallow the app entering submit/complete with polling, but we
2401 * still need to lock the ring to prevent racing with polled issue
2402 * that got punted to a workqueue.
2403 */
2404 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002405 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002406 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002407 * Don't enter poll loop if we already have events pending.
2408 * If we do, we can potentially be spinning for commands that
2409 * already triggered a CQE (eg in error).
2410 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002411 if (test_bit(0, &ctx->cq_check_overflow))
2412 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2413 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002414 break;
2415
2416 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002417 * If a submit got punted to a workqueue, we can have the
2418 * application entering polling for a command before it gets
2419 * issued. That app will hold the uring_lock for the duration
2420 * of the poll right here, so we need to take a breather every
2421 * now and then to ensure that the issue has a chance to add
2422 * the poll to the issued list. Otherwise we can spin here
2423 * forever, while the workqueue is stuck trying to acquire the
2424 * very same mutex.
2425 */
2426 if (!(++iters & 7)) {
2427 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002428 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002429 mutex_lock(&ctx->uring_lock);
2430 }
2431
Pavel Begunkov7668b922020-07-07 16:36:21 +03002432 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002433 if (ret <= 0)
2434 break;
2435 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002436 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002437
Jens Axboe500f9fb2019-08-19 12:15:59 -06002438 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002439 return ret;
2440}
2441
Jens Axboe491381ce2019-10-17 09:20:46 -06002442static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002443{
Jens Axboe491381ce2019-10-17 09:20:46 -06002444 /*
2445 * Tell lockdep we inherited freeze protection from submission
2446 * thread.
2447 */
2448 if (req->flags & REQ_F_ISREG) {
2449 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002450
Jens Axboe491381ce2019-10-17 09:20:46 -06002451 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002452 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002453 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002454}
2455
Jens Axboeb63534c2020-06-04 11:28:00 -06002456#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002457static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002458{
2459 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Colin Ian King4a245472021-02-10 20:00:07 +00002460 int rw, ret;
Jens Axboeb63534c2020-06-04 11:28:00 -06002461 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002462
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002463 /* already prepared */
2464 if (req->async_data)
2465 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002466
2467 switch (req->opcode) {
2468 case IORING_OP_READV:
2469 case IORING_OP_READ_FIXED:
2470 case IORING_OP_READ:
2471 rw = READ;
2472 break;
2473 case IORING_OP_WRITEV:
2474 case IORING_OP_WRITE_FIXED:
2475 case IORING_OP_WRITE:
2476 rw = WRITE;
2477 break;
2478 default:
2479 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2480 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002481 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002482 }
2483
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002484 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2485 if (ret < 0)
2486 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002487 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002488}
Jens Axboeb63534c2020-06-04 11:28:00 -06002489#endif
2490
Pavel Begunkov23faba32021-02-11 18:28:22 +00002491static bool io_rw_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002492{
2493#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002494 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002495
Jens Axboe355afae2020-09-02 09:30:31 -06002496 if (!S_ISBLK(mode) && !S_ISREG(mode))
2497 return false;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002498 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002499 return false;
2500
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002501 lockdep_assert_held(&req->ctx->uring_lock);
2502
Jens Axboe37d1e2e2021-02-17 21:03:43 -07002503 if (io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002504 refcount_inc(&req->refs);
2505 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002506 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002507 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002508 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002509#endif
2510 return false;
2511}
2512
Jens Axboea1d7c392020-06-22 11:09:46 -06002513static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002514 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002515{
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002516 int cflags = 0;
2517
Pavel Begunkov23faba32021-02-11 18:28:22 +00002518 if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req))
2519 return;
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002520 if (res != req->result)
2521 req_set_fail_links(req);
Pavel Begunkov23faba32021-02-11 18:28:22 +00002522
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002523 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2524 kiocb_end_write(req);
2525 if (req->flags & REQ_F_BUFFER_SELECTED)
2526 cflags = io_put_rw_kbuf(req);
2527 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002528}
2529
2530static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2531{
Jens Axboe9adbd452019-12-20 08:45:55 -07002532 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002533
Pavel Begunkov889fca72021-02-10 00:03:09 +00002534 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002535}
2536
Jens Axboedef596e2019-01-09 08:59:42 -07002537static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2538{
Jens Axboe9adbd452019-12-20 08:45:55 -07002539 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002540
Jens Axboe491381ce2019-10-17 09:20:46 -06002541 if (kiocb->ki_flags & IOCB_WRITE)
2542 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002543
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002544 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002545 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002546
2547 WRITE_ONCE(req->result, res);
2548 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002549 smp_wmb();
2550 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002551}
2552
2553/*
2554 * After the iocb has been issued, it's safe to be found on the poll list.
2555 * Adding the kiocb to the list AFTER submission ensures that we don't
2556 * find it from a io_iopoll_getevents() thread before the issuer is done
2557 * accessing the kiocb cookie.
2558 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002559static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002560{
2561 struct io_ring_ctx *ctx = req->ctx;
2562
2563 /*
2564 * Track whether we have multiple files in our lists. This will impact
2565 * how we do polling eventually, not spinning if we're on potentially
2566 * different devices.
2567 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002568 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002569 ctx->poll_multi_file = false;
2570 } else if (!ctx->poll_multi_file) {
2571 struct io_kiocb *list_req;
2572
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002573 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002574 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002575 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002576 ctx->poll_multi_file = true;
2577 }
2578
2579 /*
2580 * For fast devices, IO may have already completed. If it has, add
2581 * it to the front so we find it first.
2582 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002583 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002584 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002585 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002586 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002587
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002588 /*
2589 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2590 * task context or in io worker task context. If current task context is
2591 * sq thread, we don't need to check whether should wake up sq thread.
2592 */
2593 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002594 wq_has_sleeper(&ctx->sq_data->wait))
2595 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002596}
2597
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002598static inline void io_state_file_put(struct io_submit_state *state)
2599{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002600 if (state->file_refs) {
2601 fput_many(state->file, state->file_refs);
2602 state->file_refs = 0;
2603 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002604}
2605
2606/*
2607 * Get as many references to a file as we have IOs left in this submission,
2608 * assuming most submissions are for one file, or at least that each file
2609 * has more than one submission.
2610 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002611static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002612{
2613 if (!state)
2614 return fget(fd);
2615
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002616 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002617 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002618 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002619 return state->file;
2620 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002621 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002622 }
2623 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002624 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002625 return NULL;
2626
2627 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002628 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002629 return state->file;
2630}
2631
Jens Axboe4503b762020-06-01 10:00:27 -06002632static bool io_bdev_nowait(struct block_device *bdev)
2633{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002634 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002635}
2636
Jens Axboe2b188cc2019-01-07 10:46:33 -07002637/*
2638 * If we tracked the file through the SCM inflight mechanism, we could support
2639 * any file. For now, just ensure that anything potentially problematic is done
2640 * inline.
2641 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002642static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643{
2644 umode_t mode = file_inode(file)->i_mode;
2645
Jens Axboe4503b762020-06-01 10:00:27 -06002646 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002647 if (IS_ENABLED(CONFIG_BLOCK) &&
2648 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002649 return true;
2650 return false;
2651 }
2652 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002653 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002654 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002655 if (IS_ENABLED(CONFIG_BLOCK) &&
2656 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002657 file->f_op != &io_uring_fops)
2658 return true;
2659 return false;
2660 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002661
Jens Axboec5b85622020-06-09 19:23:05 -06002662 /* any ->read/write should understand O_NONBLOCK */
2663 if (file->f_flags & O_NONBLOCK)
2664 return true;
2665
Jens Axboeaf197f52020-04-28 13:15:06 -06002666 if (!(file->f_mode & FMODE_NOWAIT))
2667 return false;
2668
2669 if (rw == READ)
2670 return file->f_op->read_iter != NULL;
2671
2672 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002673}
2674
Pavel Begunkova88fc402020-09-30 22:57:53 +03002675static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002676{
Jens Axboedef596e2019-01-09 08:59:42 -07002677 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002678 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002679 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002680 unsigned ioprio;
2681 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002682
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002683 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002684 req->flags |= REQ_F_ISREG;
2685
Jens Axboe2b188cc2019-01-07 10:46:33 -07002686 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002687 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002688 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002689 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002690 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002691 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002692 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2693 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2694 if (unlikely(ret))
2695 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002696
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002697 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2698 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2699 req->flags |= REQ_F_NOWAIT;
2700
Jens Axboe2b188cc2019-01-07 10:46:33 -07002701 ioprio = READ_ONCE(sqe->ioprio);
2702 if (ioprio) {
2703 ret = ioprio_check_cap(ioprio);
2704 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002705 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002706
2707 kiocb->ki_ioprio = ioprio;
2708 } else
2709 kiocb->ki_ioprio = get_current_ioprio();
2710
Jens Axboedef596e2019-01-09 08:59:42 -07002711 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002712 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2713 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002714 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002715
Jens Axboedef596e2019-01-09 08:59:42 -07002716 kiocb->ki_flags |= IOCB_HIPRI;
2717 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002718 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002719 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002720 if (kiocb->ki_flags & IOCB_HIPRI)
2721 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002722 kiocb->ki_complete = io_complete_rw;
2723 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002724
Jens Axboe3529d8c2019-12-19 18:24:38 -07002725 req->rw.addr = READ_ONCE(sqe->addr);
2726 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002727 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002728 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002729}
2730
2731static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2732{
2733 switch (ret) {
2734 case -EIOCBQUEUED:
2735 break;
2736 case -ERESTARTSYS:
2737 case -ERESTARTNOINTR:
2738 case -ERESTARTNOHAND:
2739 case -ERESTART_RESTARTBLOCK:
2740 /*
2741 * We can't just restart the syscall, since previously
2742 * submitted sqes may already be in progress. Just fail this
2743 * IO with EINTR.
2744 */
2745 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002746 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002747 default:
2748 kiocb->ki_complete(kiocb, ret, 0);
2749 }
2750}
2751
Jens Axboea1d7c392020-06-22 11:09:46 -06002752static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002753 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002754{
Jens Axboeba042912019-12-25 16:33:42 -07002755 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002756 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002757
Jens Axboe227c0c92020-08-13 11:51:40 -06002758 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002759 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002760 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002761 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002762 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002763 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002764 }
2765
Jens Axboeba042912019-12-25 16:33:42 -07002766 if (req->flags & REQ_F_CUR_POS)
2767 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002768 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002769 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002770 else
2771 io_rw_done(kiocb, ret);
2772}
2773
Pavel Begunkov847595d2021-02-04 13:52:06 +00002774static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002775{
Jens Axboe9adbd452019-12-20 08:45:55 -07002776 struct io_ring_ctx *ctx = req->ctx;
2777 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002778 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002779 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002780 size_t offset;
2781 u64 buf_addr;
2782
Jens Axboeedafcce2019-01-09 09:16:05 -07002783 if (unlikely(buf_index >= ctx->nr_user_bufs))
2784 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002785 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2786 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002787 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002788
2789 /* overflow */
2790 if (buf_addr + len < buf_addr)
2791 return -EFAULT;
2792 /* not inside the mapped region */
2793 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2794 return -EFAULT;
2795
2796 /*
2797 * May not be a start of buffer, set size appropriately
2798 * and advance us to the beginning.
2799 */
2800 offset = buf_addr - imu->ubuf;
2801 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002802
2803 if (offset) {
2804 /*
2805 * Don't use iov_iter_advance() here, as it's really slow for
2806 * using the latter parts of a big fixed buffer - it iterates
2807 * over each segment manually. We can cheat a bit here, because
2808 * we know that:
2809 *
2810 * 1) it's a BVEC iter, we set it up
2811 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2812 * first and last bvec
2813 *
2814 * So just find our index, and adjust the iterator afterwards.
2815 * If the offset is within the first bvec (or the whole first
2816 * bvec, just use iov_iter_advance(). This makes it easier
2817 * since we can just skip the first segment, which may not
2818 * be PAGE_SIZE aligned.
2819 */
2820 const struct bio_vec *bvec = imu->bvec;
2821
2822 if (offset <= bvec->bv_len) {
2823 iov_iter_advance(iter, offset);
2824 } else {
2825 unsigned long seg_skip;
2826
2827 /* skip first vec */
2828 offset -= bvec->bv_len;
2829 seg_skip = 1 + (offset >> PAGE_SHIFT);
2830
2831 iter->bvec = bvec + seg_skip;
2832 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002833 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002834 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002835 }
2836 }
2837
Pavel Begunkov847595d2021-02-04 13:52:06 +00002838 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002839}
2840
Jens Axboebcda7ba2020-02-23 16:42:51 -07002841static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2842{
2843 if (needs_lock)
2844 mutex_unlock(&ctx->uring_lock);
2845}
2846
2847static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2848{
2849 /*
2850 * "Normal" inline submissions always hold the uring_lock, since we
2851 * grab it from the system call. Same is true for the SQPOLL offload.
2852 * The only exception is when we've detached the request and issue it
2853 * from an async worker thread, grab the lock for that case.
2854 */
2855 if (needs_lock)
2856 mutex_lock(&ctx->uring_lock);
2857}
2858
2859static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2860 int bgid, struct io_buffer *kbuf,
2861 bool needs_lock)
2862{
2863 struct io_buffer *head;
2864
2865 if (req->flags & REQ_F_BUFFER_SELECTED)
2866 return kbuf;
2867
2868 io_ring_submit_lock(req->ctx, needs_lock);
2869
2870 lockdep_assert_held(&req->ctx->uring_lock);
2871
2872 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2873 if (head) {
2874 if (!list_empty(&head->list)) {
2875 kbuf = list_last_entry(&head->list, struct io_buffer,
2876 list);
2877 list_del(&kbuf->list);
2878 } else {
2879 kbuf = head;
2880 idr_remove(&req->ctx->io_buffer_idr, bgid);
2881 }
2882 if (*len > kbuf->len)
2883 *len = kbuf->len;
2884 } else {
2885 kbuf = ERR_PTR(-ENOBUFS);
2886 }
2887
2888 io_ring_submit_unlock(req->ctx, needs_lock);
2889
2890 return kbuf;
2891}
2892
Jens Axboe4d954c22020-02-27 07:31:19 -07002893static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2894 bool needs_lock)
2895{
2896 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002897 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002898
2899 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002900 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002901 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2902 if (IS_ERR(kbuf))
2903 return kbuf;
2904 req->rw.addr = (u64) (unsigned long) kbuf;
2905 req->flags |= REQ_F_BUFFER_SELECTED;
2906 return u64_to_user_ptr(kbuf->addr);
2907}
2908
2909#ifdef CONFIG_COMPAT
2910static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2911 bool needs_lock)
2912{
2913 struct compat_iovec __user *uiov;
2914 compat_ssize_t clen;
2915 void __user *buf;
2916 ssize_t len;
2917
2918 uiov = u64_to_user_ptr(req->rw.addr);
2919 if (!access_ok(uiov, sizeof(*uiov)))
2920 return -EFAULT;
2921 if (__get_user(clen, &uiov->iov_len))
2922 return -EFAULT;
2923 if (clen < 0)
2924 return -EINVAL;
2925
2926 len = clen;
2927 buf = io_rw_buffer_select(req, &len, needs_lock);
2928 if (IS_ERR(buf))
2929 return PTR_ERR(buf);
2930 iov[0].iov_base = buf;
2931 iov[0].iov_len = (compat_size_t) len;
2932 return 0;
2933}
2934#endif
2935
2936static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2937 bool needs_lock)
2938{
2939 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2940 void __user *buf;
2941 ssize_t len;
2942
2943 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2944 return -EFAULT;
2945
2946 len = iov[0].iov_len;
2947 if (len < 0)
2948 return -EINVAL;
2949 buf = io_rw_buffer_select(req, &len, needs_lock);
2950 if (IS_ERR(buf))
2951 return PTR_ERR(buf);
2952 iov[0].iov_base = buf;
2953 iov[0].iov_len = len;
2954 return 0;
2955}
2956
2957static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2958 bool needs_lock)
2959{
Jens Axboedddb3e22020-06-04 11:27:01 -06002960 if (req->flags & REQ_F_BUFFER_SELECTED) {
2961 struct io_buffer *kbuf;
2962
2963 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2964 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2965 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002966 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002967 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00002968 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07002969 return -EINVAL;
2970
2971#ifdef CONFIG_COMPAT
2972 if (req->ctx->compat)
2973 return io_compat_import(req, iov, needs_lock);
2974#endif
2975
2976 return __io_iov_buffer_select(req, iov, needs_lock);
2977}
2978
Pavel Begunkov847595d2021-02-04 13:52:06 +00002979static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
2980 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002981{
Jens Axboe9adbd452019-12-20 08:45:55 -07002982 void __user *buf = u64_to_user_ptr(req->rw.addr);
2983 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002984 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07002985 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002986
Pavel Begunkov7d009162019-11-25 23:14:40 +03002987 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002988 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002989 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002990 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002991
Jens Axboebcda7ba2020-02-23 16:42:51 -07002992 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002993 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002994 return -EINVAL;
2995
Jens Axboe3a6820f2019-12-22 15:19:35 -07002996 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002997 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002998 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002999 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003000 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003001 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003002 }
3003
Jens Axboe3a6820f2019-12-22 15:19:35 -07003004 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3005 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003006 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003007 }
3008
Jens Axboe4d954c22020-02-27 07:31:19 -07003009 if (req->flags & REQ_F_BUFFER_SELECT) {
3010 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003011 if (!ret)
3012 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003013 *iovec = NULL;
3014 return ret;
3015 }
3016
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003017 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3018 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003019}
3020
Jens Axboe0fef9482020-08-26 10:36:20 -06003021static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3022{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003023 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003024}
3025
Jens Axboe32960612019-09-23 11:05:34 -06003026/*
3027 * For files that don't have ->read_iter() and ->write_iter(), handle them
3028 * by looping over ->read() or ->write() manually.
3029 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003030static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003031{
Jens Axboe4017eb92020-10-22 14:14:12 -06003032 struct kiocb *kiocb = &req->rw.kiocb;
3033 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003034 ssize_t ret = 0;
3035
3036 /*
3037 * Don't support polled IO through this interface, and we can't
3038 * support non-blocking either. For the latter, this just causes
3039 * the kiocb to be handled from an async context.
3040 */
3041 if (kiocb->ki_flags & IOCB_HIPRI)
3042 return -EOPNOTSUPP;
3043 if (kiocb->ki_flags & IOCB_NOWAIT)
3044 return -EAGAIN;
3045
3046 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003047 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003048 ssize_t nr;
3049
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003050 if (!iov_iter_is_bvec(iter)) {
3051 iovec = iov_iter_iovec(iter);
3052 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003053 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3054 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003055 }
3056
Jens Axboe32960612019-09-23 11:05:34 -06003057 if (rw == READ) {
3058 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003059 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003060 } else {
3061 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003062 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003063 }
3064
3065 if (nr < 0) {
3066 if (!ret)
3067 ret = nr;
3068 break;
3069 }
3070 ret += nr;
3071 if (nr != iovec.iov_len)
3072 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003073 req->rw.len -= nr;
3074 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003075 iov_iter_advance(iter, nr);
3076 }
3077
3078 return ret;
3079}
3080
Jens Axboeff6165b2020-08-13 09:47:43 -06003081static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3082 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003083{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003084 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003085
Jens Axboeff6165b2020-08-13 09:47:43 -06003086 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003087 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003088 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003089 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003090 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003091 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003092 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003093 unsigned iov_off = 0;
3094
3095 rw->iter.iov = rw->fast_iov;
3096 if (iter->iov != fast_iov) {
3097 iov_off = iter->iov - fast_iov;
3098 rw->iter.iov += iov_off;
3099 }
3100 if (rw->fast_iov != fast_iov)
3101 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003102 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003103 } else {
3104 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003105 }
3106}
3107
Jens Axboee8c2bc12020-08-15 18:44:09 -07003108static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003109{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003110 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3111 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3112 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003113}
3114
Jens Axboee8c2bc12020-08-15 18:44:09 -07003115static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003116{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003117 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003118 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003119
Jens Axboee8c2bc12020-08-15 18:44:09 -07003120 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003121}
3122
Jens Axboeff6165b2020-08-13 09:47:43 -06003123static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3124 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003125 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003126{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003127 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003128 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003129 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003130 if (__io_alloc_async_data(req)) {
3131 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003132 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003133 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003134
Jens Axboeff6165b2020-08-13 09:47:43 -06003135 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003136 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003137 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003138}
3139
Pavel Begunkov73debe62020-09-30 22:57:54 +03003140static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003141{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003142 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003143 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003144 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003145
Pavel Begunkov2846c482020-11-07 13:16:27 +00003146 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003147 if (unlikely(ret < 0))
3148 return ret;
3149
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003150 iorw->bytes_done = 0;
3151 iorw->free_iovec = iov;
3152 if (iov)
3153 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003154 return 0;
3155}
3156
Pavel Begunkov73debe62020-09-30 22:57:54 +03003157static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003158{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003159 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3160 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003161 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003162}
3163
Jens Axboec1dd91d2020-08-03 16:43:59 -06003164/*
3165 * This is our waitqueue callback handler, registered through lock_page_async()
3166 * when we initially tried to do the IO with the iocb armed our waitqueue.
3167 * This gets called when the page is unlocked, and we generally expect that to
3168 * happen when the page IO is completed and the page is now uptodate. This will
3169 * queue a task_work based retry of the operation, attempting to copy the data
3170 * again. If the latter fails because the page was NOT uptodate, then we will
3171 * do a thread based blocking retry of the operation. That's the unexpected
3172 * slow path.
3173 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003174static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3175 int sync, void *arg)
3176{
3177 struct wait_page_queue *wpq;
3178 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003179 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003180
3181 wpq = container_of(wait, struct wait_page_queue, wait);
3182
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003183 if (!wake_page_match(wpq, key))
3184 return 0;
3185
Hao Xuc8d317a2020-09-29 20:00:45 +08003186 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003187 list_del_init(&wait->entry);
3188
Jens Axboebcf5a062020-05-22 09:24:42 -06003189 /* submit ref gets dropped, acquire a new one */
3190 refcount_inc(&req->refs);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003191 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003192 return 1;
3193}
3194
Jens Axboec1dd91d2020-08-03 16:43:59 -06003195/*
3196 * This controls whether a given IO request should be armed for async page
3197 * based retry. If we return false here, the request is handed to the async
3198 * worker threads for retry. If we're doing buffered reads on a regular file,
3199 * we prepare a private wait_page_queue entry and retry the operation. This
3200 * will either succeed because the page is now uptodate and unlocked, or it
3201 * will register a callback when the page is unlocked at IO completion. Through
3202 * that callback, io_uring uses task_work to setup a retry of the operation.
3203 * That retry will attempt the buffered read again. The retry will generally
3204 * succeed, or in rare cases where it fails, we then fall back to using the
3205 * async worker threads for a blocking retry.
3206 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003207static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003208{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003209 struct io_async_rw *rw = req->async_data;
3210 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003211 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003212
3213 /* never retry for NOWAIT, we just complete with -EAGAIN */
3214 if (req->flags & REQ_F_NOWAIT)
3215 return false;
3216
Jens Axboe227c0c92020-08-13 11:51:40 -06003217 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003218 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003219 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003220
Jens Axboebcf5a062020-05-22 09:24:42 -06003221 /*
3222 * just use poll if we can, and don't attempt if the fs doesn't
3223 * support callback based unlocks
3224 */
3225 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3226 return false;
3227
Jens Axboe3b2a4432020-08-16 10:58:43 -07003228 wait->wait.func = io_async_buf_func;
3229 wait->wait.private = req;
3230 wait->wait.flags = 0;
3231 INIT_LIST_HEAD(&wait->wait.entry);
3232 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003233 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003234 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003235 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003236}
3237
3238static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3239{
3240 if (req->file->f_op->read_iter)
3241 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003242 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003243 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003244 else
3245 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003246}
3247
Pavel Begunkov889fca72021-02-10 00:03:09 +00003248static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003249{
3250 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003251 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003252 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003253 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003254 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003255 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003256
Pavel Begunkov2846c482020-11-07 13:16:27 +00003257 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003258 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003259 iovec = NULL;
3260 } else {
3261 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3262 if (ret < 0)
3263 return ret;
3264 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003265 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003266 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003267
Jens Axboefd6c2e42019-12-18 12:19:41 -07003268 /* Ensure we clear previously set non-block flag */
3269 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003270 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003271 else
3272 kiocb->ki_flags |= IOCB_NOWAIT;
3273
Pavel Begunkov24c74672020-06-21 13:09:51 +03003274 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003275 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3276 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003277 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003278 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003279
Pavel Begunkov632546c2020-11-07 13:16:26 +00003280 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003281 if (unlikely(ret)) {
3282 kfree(iovec);
3283 return ret;
3284 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003285
Jens Axboe227c0c92020-08-13 11:51:40 -06003286 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003287
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003288 if (ret == -EIOCBQUEUED) {
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003289 goto out_free;
Jens Axboe227c0c92020-08-13 11:51:40 -06003290 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003291 /* IOPOLL retry should happen for io-wq threads */
3292 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003293 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003294 /* no retry on NONBLOCK nor RWF_NOWAIT */
3295 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003296 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003297 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003298 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003299 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003300 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003301 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003302 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003303 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003304 }
3305
Jens Axboe227c0c92020-08-13 11:51:40 -06003306 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003307 if (ret2)
3308 return ret2;
3309
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003310 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003311 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003312 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003313 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003314
Pavel Begunkovb23df912021-02-04 13:52:04 +00003315 do {
3316 io_size -= ret;
3317 rw->bytes_done += ret;
3318 /* if we can retry, do so with the callbacks armed */
3319 if (!io_rw_should_retry(req)) {
3320 kiocb->ki_flags &= ~IOCB_WAITQ;
3321 return -EAGAIN;
3322 }
3323
3324 /*
3325 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3326 * we get -EIOCBQUEUED, then we'll get a notification when the
3327 * desired page gets unlocked. We can also get a partial read
3328 * here, and if we do, then just retry at the new offset.
3329 */
3330 ret = io_iter_do_read(req, iter);
3331 if (ret == -EIOCBQUEUED)
3332 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003333 /* we got some bytes, but not all. retry. */
Pavel Begunkovb23df912021-02-04 13:52:04 +00003334 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003335done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003336 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003337out_free:
3338 /* it's faster to check here then delegate to kfree */
3339 if (iovec)
3340 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003341 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003342}
3343
Pavel Begunkov73debe62020-09-30 22:57:54 +03003344static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003345{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003346 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3347 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003348 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003349}
3350
Pavel Begunkov889fca72021-02-10 00:03:09 +00003351static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003352{
3353 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003354 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003355 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003356 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003357 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003358 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003359
Pavel Begunkov2846c482020-11-07 13:16:27 +00003360 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003361 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003362 iovec = NULL;
3363 } else {
3364 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3365 if (ret < 0)
3366 return ret;
3367 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003368 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003369 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003370
Jens Axboefd6c2e42019-12-18 12:19:41 -07003371 /* Ensure we clear previously set non-block flag */
3372 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003373 kiocb->ki_flags &= ~IOCB_NOWAIT;
3374 else
3375 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003376
Pavel Begunkov24c74672020-06-21 13:09:51 +03003377 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003378 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003379 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003380
Jens Axboe10d59342019-12-09 20:16:22 -07003381 /* file path doesn't support NOWAIT for non-direct_IO */
3382 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3383 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003384 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003385
Pavel Begunkov632546c2020-11-07 13:16:26 +00003386 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003387 if (unlikely(ret))
3388 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003389
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003390 /*
3391 * Open-code file_start_write here to grab freeze protection,
3392 * which will be released by another thread in
3393 * io_complete_rw(). Fool lockdep by telling it the lock got
3394 * released so that it doesn't complain about the held lock when
3395 * we return to userspace.
3396 */
3397 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003398 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003399 __sb_writers_release(file_inode(req->file)->i_sb,
3400 SB_FREEZE_WRITE);
3401 }
3402 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003403
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003404 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003405 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003406 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003407 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003408 else
3409 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003410
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003411 /*
3412 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3413 * retry them without IOCB_NOWAIT.
3414 */
3415 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3416 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003417 /* no retry on NONBLOCK nor RWF_NOWAIT */
3418 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003419 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003420 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003421 /* IOPOLL retry should happen for io-wq threads */
3422 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3423 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003424done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003425 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003426 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003427copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003428 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003429 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003430 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003431 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003432 }
Jens Axboe31b51512019-01-18 22:56:34 -07003433out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003434 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003435 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003436 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003437 return ret;
3438}
3439
Jens Axboe80a261f2020-09-28 14:23:58 -06003440static int io_renameat_prep(struct io_kiocb *req,
3441 const struct io_uring_sqe *sqe)
3442{
3443 struct io_rename *ren = &req->rename;
3444 const char __user *oldf, *newf;
3445
3446 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3447 return -EBADF;
3448
3449 ren->old_dfd = READ_ONCE(sqe->fd);
3450 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3451 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3452 ren->new_dfd = READ_ONCE(sqe->len);
3453 ren->flags = READ_ONCE(sqe->rename_flags);
3454
3455 ren->oldpath = getname(oldf);
3456 if (IS_ERR(ren->oldpath))
3457 return PTR_ERR(ren->oldpath);
3458
3459 ren->newpath = getname(newf);
3460 if (IS_ERR(ren->newpath)) {
3461 putname(ren->oldpath);
3462 return PTR_ERR(ren->newpath);
3463 }
3464
3465 req->flags |= REQ_F_NEED_CLEANUP;
3466 return 0;
3467}
3468
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003469static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003470{
3471 struct io_rename *ren = &req->rename;
3472 int ret;
3473
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003474 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003475 return -EAGAIN;
3476
3477 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3478 ren->newpath, ren->flags);
3479
3480 req->flags &= ~REQ_F_NEED_CLEANUP;
3481 if (ret < 0)
3482 req_set_fail_links(req);
3483 io_req_complete(req, ret);
3484 return 0;
3485}
3486
Jens Axboe14a11432020-09-28 14:27:37 -06003487static int io_unlinkat_prep(struct io_kiocb *req,
3488 const struct io_uring_sqe *sqe)
3489{
3490 struct io_unlink *un = &req->unlink;
3491 const char __user *fname;
3492
3493 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3494 return -EBADF;
3495
3496 un->dfd = READ_ONCE(sqe->fd);
3497
3498 un->flags = READ_ONCE(sqe->unlink_flags);
3499 if (un->flags & ~AT_REMOVEDIR)
3500 return -EINVAL;
3501
3502 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3503 un->filename = getname(fname);
3504 if (IS_ERR(un->filename))
3505 return PTR_ERR(un->filename);
3506
3507 req->flags |= REQ_F_NEED_CLEANUP;
3508 return 0;
3509}
3510
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003511static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003512{
3513 struct io_unlink *un = &req->unlink;
3514 int ret;
3515
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003516 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003517 return -EAGAIN;
3518
3519 if (un->flags & AT_REMOVEDIR)
3520 ret = do_rmdir(un->dfd, un->filename);
3521 else
3522 ret = do_unlinkat(un->dfd, un->filename);
3523
3524 req->flags &= ~REQ_F_NEED_CLEANUP;
3525 if (ret < 0)
3526 req_set_fail_links(req);
3527 io_req_complete(req, ret);
3528 return 0;
3529}
3530
Jens Axboe36f4fa62020-09-05 11:14:22 -06003531static int io_shutdown_prep(struct io_kiocb *req,
3532 const struct io_uring_sqe *sqe)
3533{
3534#if defined(CONFIG_NET)
3535 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3536 return -EINVAL;
3537 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3538 sqe->buf_index)
3539 return -EINVAL;
3540
3541 req->shutdown.how = READ_ONCE(sqe->len);
3542 return 0;
3543#else
3544 return -EOPNOTSUPP;
3545#endif
3546}
3547
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003548static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003549{
3550#if defined(CONFIG_NET)
3551 struct socket *sock;
3552 int ret;
3553
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003554 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003555 return -EAGAIN;
3556
Linus Torvalds48aba792020-12-16 12:44:05 -08003557 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003558 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003559 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003560
3561 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003562 if (ret < 0)
3563 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003564 io_req_complete(req, ret);
3565 return 0;
3566#else
3567 return -EOPNOTSUPP;
3568#endif
3569}
3570
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003571static int __io_splice_prep(struct io_kiocb *req,
3572 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003573{
3574 struct io_splice* sp = &req->splice;
3575 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003576
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003577 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3578 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003579
3580 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003581 sp->len = READ_ONCE(sqe->len);
3582 sp->flags = READ_ONCE(sqe->splice_flags);
3583
3584 if (unlikely(sp->flags & ~valid_flags))
3585 return -EINVAL;
3586
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003587 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3588 (sp->flags & SPLICE_F_FD_IN_FIXED));
3589 if (!sp->file_in)
3590 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003591 req->flags |= REQ_F_NEED_CLEANUP;
3592
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003593 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3594 /*
3595 * Splice operation will be punted aync, and here need to
3596 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3597 */
3598 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003599 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003600 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003601
3602 return 0;
3603}
3604
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003605static int io_tee_prep(struct io_kiocb *req,
3606 const struct io_uring_sqe *sqe)
3607{
3608 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3609 return -EINVAL;
3610 return __io_splice_prep(req, sqe);
3611}
3612
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003613static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003614{
3615 struct io_splice *sp = &req->splice;
3616 struct file *in = sp->file_in;
3617 struct file *out = sp->file_out;
3618 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3619 long ret = 0;
3620
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003621 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003622 return -EAGAIN;
3623 if (sp->len)
3624 ret = do_tee(in, out, sp->len, flags);
3625
3626 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3627 req->flags &= ~REQ_F_NEED_CLEANUP;
3628
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003629 if (ret != sp->len)
3630 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003631 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003632 return 0;
3633}
3634
3635static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3636{
3637 struct io_splice* sp = &req->splice;
3638
3639 sp->off_in = READ_ONCE(sqe->splice_off_in);
3640 sp->off_out = READ_ONCE(sqe->off);
3641 return __io_splice_prep(req, sqe);
3642}
3643
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003644static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003645{
3646 struct io_splice *sp = &req->splice;
3647 struct file *in = sp->file_in;
3648 struct file *out = sp->file_out;
3649 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3650 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003651 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003652
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003653 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003654 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003655
3656 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3657 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003658
Jens Axboe948a7742020-05-17 14:21:38 -06003659 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003660 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003661
3662 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3663 req->flags &= ~REQ_F_NEED_CLEANUP;
3664
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003665 if (ret != sp->len)
3666 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003667 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003668 return 0;
3669}
3670
Jens Axboe2b188cc2019-01-07 10:46:33 -07003671/*
3672 * IORING_OP_NOP just posts a completion event, nothing else.
3673 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003674static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003675{
3676 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003677
Jens Axboedef596e2019-01-09 08:59:42 -07003678 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3679 return -EINVAL;
3680
Pavel Begunkov889fca72021-02-10 00:03:09 +00003681 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003682 return 0;
3683}
3684
Pavel Begunkov1155c762021-02-18 18:29:38 +00003685static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003686{
Jens Axboe6b063142019-01-10 22:13:58 -07003687 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003688
Jens Axboe09bb8392019-03-13 12:39:28 -06003689 if (!req->file)
3690 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003691
Jens Axboe6b063142019-01-10 22:13:58 -07003692 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003693 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003694 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003695 return -EINVAL;
3696
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003697 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3698 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3699 return -EINVAL;
3700
3701 req->sync.off = READ_ONCE(sqe->off);
3702 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003703 return 0;
3704}
3705
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003706static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003707{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003708 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003709 int ret;
3710
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003711 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003712 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003713 return -EAGAIN;
3714
Jens Axboe9adbd452019-12-20 08:45:55 -07003715 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003716 end > 0 ? end : LLONG_MAX,
3717 req->sync.flags & IORING_FSYNC_DATASYNC);
3718 if (ret < 0)
3719 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003720 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003721 return 0;
3722}
3723
Jens Axboed63d1b52019-12-10 10:38:56 -07003724static int io_fallocate_prep(struct io_kiocb *req,
3725 const struct io_uring_sqe *sqe)
3726{
3727 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3728 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003729 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3730 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003731
3732 req->sync.off = READ_ONCE(sqe->off);
3733 req->sync.len = READ_ONCE(sqe->addr);
3734 req->sync.mode = READ_ONCE(sqe->len);
3735 return 0;
3736}
3737
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003738static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003739{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003740 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003741
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003742 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003743 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003744 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003745 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3746 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003747 if (ret < 0)
3748 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003749 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003750 return 0;
3751}
3752
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003753static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003754{
Jens Axboef8748882020-01-08 17:47:02 -07003755 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003756 int ret;
3757
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003758 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003759 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003760 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003761 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003762
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003763 /* open.how should be already initialised */
3764 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003765 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003766
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003767 req->open.dfd = READ_ONCE(sqe->fd);
3768 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003769 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003770 if (IS_ERR(req->open.filename)) {
3771 ret = PTR_ERR(req->open.filename);
3772 req->open.filename = NULL;
3773 return ret;
3774 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003775 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003776 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003777 return 0;
3778}
3779
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003780static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3781{
3782 u64 flags, mode;
3783
Jens Axboe14587a462020-09-05 11:36:08 -06003784 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003785 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003786 mode = READ_ONCE(sqe->len);
3787 flags = READ_ONCE(sqe->open_flags);
3788 req->open.how = build_open_how(flags, mode);
3789 return __io_openat_prep(req, sqe);
3790}
3791
Jens Axboecebdb982020-01-08 17:59:24 -07003792static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3793{
3794 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003795 size_t len;
3796 int ret;
3797
Jens Axboe14587a462020-09-05 11:36:08 -06003798 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003799 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003800 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3801 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003802 if (len < OPEN_HOW_SIZE_VER0)
3803 return -EINVAL;
3804
3805 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3806 len);
3807 if (ret)
3808 return ret;
3809
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003810 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003811}
3812
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003813static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003814{
3815 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003816 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003817 bool nonblock_set;
3818 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003819 int ret;
3820
Jens Axboecebdb982020-01-08 17:59:24 -07003821 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003822 if (ret)
3823 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003824 nonblock_set = op.open_flag & O_NONBLOCK;
3825 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003826 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003827 /*
3828 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3829 * it'll always -EAGAIN
3830 */
3831 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3832 return -EAGAIN;
3833 op.lookup_flags |= LOOKUP_CACHED;
3834 op.open_flag |= O_NONBLOCK;
3835 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003836
Jens Axboe4022e7a2020-03-19 19:23:18 -06003837 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003838 if (ret < 0)
3839 goto err;
3840
3841 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07003842 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003843 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
3844 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003845 /*
3846 * We could hang on to this 'fd', but seems like marginal
3847 * gain for something that is now known to be a slower path.
3848 * So just put it, and we'll get a new one when we retry.
3849 */
3850 put_unused_fd(ret);
3851 return -EAGAIN;
3852 }
3853
Jens Axboe15b71ab2019-12-11 11:20:36 -07003854 if (IS_ERR(file)) {
3855 put_unused_fd(ret);
3856 ret = PTR_ERR(file);
3857 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003858 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07003859 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003860 fsnotify_open(file);
3861 fd_install(ret, file);
3862 }
3863err:
3864 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003865 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003866 if (ret < 0)
3867 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003868 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003869 return 0;
3870}
3871
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003872static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07003873{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003874 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07003875}
3876
Jens Axboe067524e2020-03-02 16:32:28 -07003877static int io_remove_buffers_prep(struct io_kiocb *req,
3878 const struct io_uring_sqe *sqe)
3879{
3880 struct io_provide_buf *p = &req->pbuf;
3881 u64 tmp;
3882
3883 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3884 return -EINVAL;
3885
3886 tmp = READ_ONCE(sqe->fd);
3887 if (!tmp || tmp > USHRT_MAX)
3888 return -EINVAL;
3889
3890 memset(p, 0, sizeof(*p));
3891 p->nbufs = tmp;
3892 p->bgid = READ_ONCE(sqe->buf_group);
3893 return 0;
3894}
3895
3896static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3897 int bgid, unsigned nbufs)
3898{
3899 unsigned i = 0;
3900
3901 /* shouldn't happen */
3902 if (!nbufs)
3903 return 0;
3904
3905 /* the head kbuf is the list itself */
3906 while (!list_empty(&buf->list)) {
3907 struct io_buffer *nxt;
3908
3909 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3910 list_del(&nxt->list);
3911 kfree(nxt);
3912 if (++i == nbufs)
3913 return i;
3914 }
3915 i++;
3916 kfree(buf);
3917 idr_remove(&ctx->io_buffer_idr, bgid);
3918
3919 return i;
3920}
3921
Pavel Begunkov889fca72021-02-10 00:03:09 +00003922static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07003923{
3924 struct io_provide_buf *p = &req->pbuf;
3925 struct io_ring_ctx *ctx = req->ctx;
3926 struct io_buffer *head;
3927 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003928 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07003929
3930 io_ring_submit_lock(ctx, !force_nonblock);
3931
3932 lockdep_assert_held(&ctx->uring_lock);
3933
3934 ret = -ENOENT;
3935 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3936 if (head)
3937 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07003938 if (ret < 0)
3939 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003940
3941 /* need to hold the lock to complete IOPOLL requests */
3942 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00003943 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003944 io_ring_submit_unlock(ctx, !force_nonblock);
3945 } else {
3946 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00003947 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003948 }
Jens Axboe067524e2020-03-02 16:32:28 -07003949 return 0;
3950}
3951
Jens Axboeddf0322d2020-02-23 16:41:33 -07003952static int io_provide_buffers_prep(struct io_kiocb *req,
3953 const struct io_uring_sqe *sqe)
3954{
3955 struct io_provide_buf *p = &req->pbuf;
3956 u64 tmp;
3957
3958 if (sqe->ioprio || sqe->rw_flags)
3959 return -EINVAL;
3960
3961 tmp = READ_ONCE(sqe->fd);
3962 if (!tmp || tmp > USHRT_MAX)
3963 return -E2BIG;
3964 p->nbufs = tmp;
3965 p->addr = READ_ONCE(sqe->addr);
3966 p->len = READ_ONCE(sqe->len);
3967
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003968 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003969 return -EFAULT;
3970
3971 p->bgid = READ_ONCE(sqe->buf_group);
3972 tmp = READ_ONCE(sqe->off);
3973 if (tmp > USHRT_MAX)
3974 return -E2BIG;
3975 p->bid = tmp;
3976 return 0;
3977}
3978
3979static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3980{
3981 struct io_buffer *buf;
3982 u64 addr = pbuf->addr;
3983 int i, bid = pbuf->bid;
3984
3985 for (i = 0; i < pbuf->nbufs; i++) {
3986 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3987 if (!buf)
3988 break;
3989
3990 buf->addr = addr;
3991 buf->len = pbuf->len;
3992 buf->bid = bid;
3993 addr += pbuf->len;
3994 bid++;
3995 if (!*head) {
3996 INIT_LIST_HEAD(&buf->list);
3997 *head = buf;
3998 } else {
3999 list_add_tail(&buf->list, &(*head)->list);
4000 }
4001 }
4002
4003 return i ? i : -ENOMEM;
4004}
4005
Pavel Begunkov889fca72021-02-10 00:03:09 +00004006static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004007{
4008 struct io_provide_buf *p = &req->pbuf;
4009 struct io_ring_ctx *ctx = req->ctx;
4010 struct io_buffer *head, *list;
4011 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004012 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004013
4014 io_ring_submit_lock(ctx, !force_nonblock);
4015
4016 lockdep_assert_held(&ctx->uring_lock);
4017
4018 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4019
4020 ret = io_add_buffers(p, &head);
4021 if (ret < 0)
4022 goto out;
4023
4024 if (!list) {
4025 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4026 GFP_KERNEL);
4027 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004028 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004029 goto out;
4030 }
4031 }
4032out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004033 if (ret < 0)
4034 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004035
4036 /* need to hold the lock to complete IOPOLL requests */
4037 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004038 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004039 io_ring_submit_unlock(ctx, !force_nonblock);
4040 } else {
4041 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004042 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004043 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004044 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004045}
4046
Jens Axboe3e4827b2020-01-08 15:18:09 -07004047static int io_epoll_ctl_prep(struct io_kiocb *req,
4048 const struct io_uring_sqe *sqe)
4049{
4050#if defined(CONFIG_EPOLL)
4051 if (sqe->ioprio || sqe->buf_index)
4052 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004053 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004054 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004055
4056 req->epoll.epfd = READ_ONCE(sqe->fd);
4057 req->epoll.op = READ_ONCE(sqe->len);
4058 req->epoll.fd = READ_ONCE(sqe->off);
4059
4060 if (ep_op_has_event(req->epoll.op)) {
4061 struct epoll_event __user *ev;
4062
4063 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4064 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4065 return -EFAULT;
4066 }
4067
4068 return 0;
4069#else
4070 return -EOPNOTSUPP;
4071#endif
4072}
4073
Pavel Begunkov889fca72021-02-10 00:03:09 +00004074static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004075{
4076#if defined(CONFIG_EPOLL)
4077 struct io_epoll *ie = &req->epoll;
4078 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004079 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004080
4081 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4082 if (force_nonblock && ret == -EAGAIN)
4083 return -EAGAIN;
4084
4085 if (ret < 0)
4086 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004087 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004088 return 0;
4089#else
4090 return -EOPNOTSUPP;
4091#endif
4092}
4093
Jens Axboec1ca7572019-12-25 22:18:28 -07004094static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4095{
4096#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4097 if (sqe->ioprio || sqe->buf_index || sqe->off)
4098 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004099 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4100 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004101
4102 req->madvise.addr = READ_ONCE(sqe->addr);
4103 req->madvise.len = READ_ONCE(sqe->len);
4104 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4105 return 0;
4106#else
4107 return -EOPNOTSUPP;
4108#endif
4109}
4110
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004111static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004112{
4113#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4114 struct io_madvise *ma = &req->madvise;
4115 int ret;
4116
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004117 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004118 return -EAGAIN;
4119
Minchan Kim0726b012020-10-17 16:14:50 -07004120 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004121 if (ret < 0)
4122 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004123 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004124 return 0;
4125#else
4126 return -EOPNOTSUPP;
4127#endif
4128}
4129
Jens Axboe4840e412019-12-25 22:03:45 -07004130static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4131{
4132 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4133 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004134 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4135 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004136
4137 req->fadvise.offset = READ_ONCE(sqe->off);
4138 req->fadvise.len = READ_ONCE(sqe->len);
4139 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4140 return 0;
4141}
4142
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004143static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004144{
4145 struct io_fadvise *fa = &req->fadvise;
4146 int ret;
4147
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004148 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004149 switch (fa->advice) {
4150 case POSIX_FADV_NORMAL:
4151 case POSIX_FADV_RANDOM:
4152 case POSIX_FADV_SEQUENTIAL:
4153 break;
4154 default:
4155 return -EAGAIN;
4156 }
4157 }
Jens Axboe4840e412019-12-25 22:03:45 -07004158
4159 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4160 if (ret < 0)
4161 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004162 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004163 return 0;
4164}
4165
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004166static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4167{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004168 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004169 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004170 if (sqe->ioprio || sqe->buf_index)
4171 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004172 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004173 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004174
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004175 req->statx.dfd = READ_ONCE(sqe->fd);
4176 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004177 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004178 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4179 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004180
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004181 return 0;
4182}
4183
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004184static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004185{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004186 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004187 int ret;
4188
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004189 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004190 /* only need file table for an actual valid fd */
4191 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4192 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004193 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004194 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004195
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004196 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4197 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004198
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004199 if (ret < 0)
4200 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004201 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004202 return 0;
4203}
4204
Jens Axboeb5dba592019-12-11 14:02:38 -07004205static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4206{
Jens Axboe14587a462020-09-05 11:36:08 -06004207 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004208 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004209 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4210 sqe->rw_flags || sqe->buf_index)
4211 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004212 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004213 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004214
4215 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004216 return 0;
4217}
4218
Pavel Begunkov889fca72021-02-10 00:03:09 +00004219static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004220{
Jens Axboe9eac1902021-01-19 15:50:37 -07004221 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004222 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004223 struct fdtable *fdt;
4224 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004225 int ret;
4226
Jens Axboe9eac1902021-01-19 15:50:37 -07004227 file = NULL;
4228 ret = -EBADF;
4229 spin_lock(&files->file_lock);
4230 fdt = files_fdtable(files);
4231 if (close->fd >= fdt->max_fds) {
4232 spin_unlock(&files->file_lock);
4233 goto err;
4234 }
4235 file = fdt->fd[close->fd];
4236 if (!file) {
4237 spin_unlock(&files->file_lock);
4238 goto err;
4239 }
4240
4241 if (file->f_op == &io_uring_fops) {
4242 spin_unlock(&files->file_lock);
4243 file = NULL;
4244 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004245 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004246
4247 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004248 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004249 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004250 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004251 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004252
Jens Axboe9eac1902021-01-19 15:50:37 -07004253 ret = __close_fd_get_file(close->fd, &file);
4254 spin_unlock(&files->file_lock);
4255 if (ret < 0) {
4256 if (ret == -ENOENT)
4257 ret = -EBADF;
4258 goto err;
4259 }
4260
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004261 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004262 ret = filp_close(file, current->files);
4263err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004264 if (ret < 0)
4265 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004266 if (file)
4267 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004268 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004269 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004270}
4271
Pavel Begunkov1155c762021-02-18 18:29:38 +00004272static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004273{
4274 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004275
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004276 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4277 return -EINVAL;
4278 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4279 return -EINVAL;
4280
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004281 req->sync.off = READ_ONCE(sqe->off);
4282 req->sync.len = READ_ONCE(sqe->len);
4283 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004284 return 0;
4285}
4286
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004287static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004288{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004289 int ret;
4290
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004291 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004292 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004293 return -EAGAIN;
4294
Jens Axboe9adbd452019-12-20 08:45:55 -07004295 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004296 req->sync.flags);
4297 if (ret < 0)
4298 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004299 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004300 return 0;
4301}
4302
YueHaibing469956e2020-03-04 15:53:52 +08004303#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004304static int io_setup_async_msg(struct io_kiocb *req,
4305 struct io_async_msghdr *kmsg)
4306{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004307 struct io_async_msghdr *async_msg = req->async_data;
4308
4309 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004310 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004311 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004312 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004313 return -ENOMEM;
4314 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004315 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004316 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004317 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004318 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004319 /* if were using fast_iov, set it to the new one */
4320 if (!async_msg->free_iov)
4321 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4322
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004323 return -EAGAIN;
4324}
4325
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004326static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4327 struct io_async_msghdr *iomsg)
4328{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004329 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004330 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004331 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004332 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004333}
4334
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004335static int io_sendmsg_prep_async(struct io_kiocb *req)
4336{
4337 int ret;
4338
4339 if (!io_op_defs[req->opcode].needs_async_data)
4340 return 0;
4341 ret = io_sendmsg_copy_hdr(req, req->async_data);
4342 if (!ret)
4343 req->flags |= REQ_F_NEED_CLEANUP;
4344 return ret;
4345}
4346
Jens Axboe3529d8c2019-12-19 18:24:38 -07004347static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004348{
Jens Axboee47293f2019-12-20 08:58:21 -07004349 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004350
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004351 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4352 return -EINVAL;
4353
Jens Axboee47293f2019-12-20 08:58:21 -07004354 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004355 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004356 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004357
Jens Axboed8768362020-02-27 14:17:49 -07004358#ifdef CONFIG_COMPAT
4359 if (req->ctx->compat)
4360 sr->msg_flags |= MSG_CMSG_COMPAT;
4361#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004362 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004363}
4364
Pavel Begunkov889fca72021-02-10 00:03:09 +00004365static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004366{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004367 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004368 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004369 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004370 int ret;
4371
Florent Revestdba4a922020-12-04 12:36:04 +01004372 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004373 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004374 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004375
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004376 kmsg = req->async_data;
4377 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004378 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004379 if (ret)
4380 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004381 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004382 }
4383
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004384 flags = req->sr_msg.msg_flags;
4385 if (flags & MSG_DONTWAIT)
4386 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004387 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004388 flags |= MSG_DONTWAIT;
4389
4390 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004391 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004392 return io_setup_async_msg(req, kmsg);
4393 if (ret == -ERESTARTSYS)
4394 ret = -EINTR;
4395
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004396 /* fast path, check for non-NULL to avoid function call */
4397 if (kmsg->free_iov)
4398 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004399 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004400 if (ret < 0)
4401 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004402 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004403 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004404}
4405
Pavel Begunkov889fca72021-02-10 00:03:09 +00004406static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004407{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004408 struct io_sr_msg *sr = &req->sr_msg;
4409 struct msghdr msg;
4410 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004411 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004412 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004413 int ret;
4414
Florent Revestdba4a922020-12-04 12:36:04 +01004415 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004416 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004417 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004418
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004419 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4420 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004421 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004422
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004423 msg.msg_name = NULL;
4424 msg.msg_control = NULL;
4425 msg.msg_controllen = 0;
4426 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004427
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004428 flags = req->sr_msg.msg_flags;
4429 if (flags & MSG_DONTWAIT)
4430 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004431 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004432 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004433
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004434 msg.msg_flags = flags;
4435 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004436 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004437 return -EAGAIN;
4438 if (ret == -ERESTARTSYS)
4439 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004440
Jens Axboe03b12302019-12-02 18:50:25 -07004441 if (ret < 0)
4442 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004443 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004444 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004445}
4446
Pavel Begunkov1400e692020-07-12 20:41:05 +03004447static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4448 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004449{
4450 struct io_sr_msg *sr = &req->sr_msg;
4451 struct iovec __user *uiov;
4452 size_t iov_len;
4453 int ret;
4454
Pavel Begunkov1400e692020-07-12 20:41:05 +03004455 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4456 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004457 if (ret)
4458 return ret;
4459
4460 if (req->flags & REQ_F_BUFFER_SELECT) {
4461 if (iov_len > 1)
4462 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004463 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004464 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004465 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004466 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004467 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004468 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004469 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004470 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004471 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004472 if (ret > 0)
4473 ret = 0;
4474 }
4475
4476 return ret;
4477}
4478
4479#ifdef CONFIG_COMPAT
4480static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004481 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004482{
4483 struct compat_msghdr __user *msg_compat;
4484 struct io_sr_msg *sr = &req->sr_msg;
4485 struct compat_iovec __user *uiov;
4486 compat_uptr_t ptr;
4487 compat_size_t len;
4488 int ret;
4489
Pavel Begunkov270a5942020-07-12 20:41:04 +03004490 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004491 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004492 &ptr, &len);
4493 if (ret)
4494 return ret;
4495
4496 uiov = compat_ptr(ptr);
4497 if (req->flags & REQ_F_BUFFER_SELECT) {
4498 compat_ssize_t clen;
4499
4500 if (len > 1)
4501 return -EINVAL;
4502 if (!access_ok(uiov, sizeof(*uiov)))
4503 return -EFAULT;
4504 if (__get_user(clen, &uiov->iov_len))
4505 return -EFAULT;
4506 if (clen < 0)
4507 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004508 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004509 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004510 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004511 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004512 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004513 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004514 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004515 if (ret < 0)
4516 return ret;
4517 }
4518
4519 return 0;
4520}
Jens Axboe03b12302019-12-02 18:50:25 -07004521#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004522
Pavel Begunkov1400e692020-07-12 20:41:05 +03004523static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4524 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004525{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004526 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004527
4528#ifdef CONFIG_COMPAT
4529 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004530 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004531#endif
4532
Pavel Begunkov1400e692020-07-12 20:41:05 +03004533 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004534}
4535
Jens Axboebcda7ba2020-02-23 16:42:51 -07004536static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004537 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004538{
4539 struct io_sr_msg *sr = &req->sr_msg;
4540 struct io_buffer *kbuf;
4541
Jens Axboebcda7ba2020-02-23 16:42:51 -07004542 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4543 if (IS_ERR(kbuf))
4544 return kbuf;
4545
4546 sr->kbuf = kbuf;
4547 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004548 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004549}
4550
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004551static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4552{
4553 return io_put_kbuf(req, req->sr_msg.kbuf);
4554}
4555
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004556static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004557{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004558 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004559
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004560 if (!io_op_defs[req->opcode].needs_async_data)
4561 return 0;
4562 ret = io_recvmsg_copy_hdr(req, req->async_data);
4563 if (!ret)
4564 req->flags |= REQ_F_NEED_CLEANUP;
4565 return ret;
4566}
4567
4568static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4569{
4570 struct io_sr_msg *sr = &req->sr_msg;
4571
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004572 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4573 return -EINVAL;
4574
Jens Axboe3529d8c2019-12-19 18:24:38 -07004575 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004576 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004577 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004578 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004579
Jens Axboed8768362020-02-27 14:17:49 -07004580#ifdef CONFIG_COMPAT
4581 if (req->ctx->compat)
4582 sr->msg_flags |= MSG_CMSG_COMPAT;
4583#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004584 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004585}
4586
Pavel Begunkov889fca72021-02-10 00:03:09 +00004587static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004588{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004589 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004590 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004591 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004592 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004593 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004594 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004595
Florent Revestdba4a922020-12-04 12:36:04 +01004596 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004597 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004598 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004599
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004600 kmsg = req->async_data;
4601 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004602 ret = io_recvmsg_copy_hdr(req, &iomsg);
4603 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004604 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004605 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004606 }
4607
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004608 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004609 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004610 if (IS_ERR(kbuf))
4611 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004612 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004613 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4614 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004615 1, req->sr_msg.len);
4616 }
4617
4618 flags = req->sr_msg.msg_flags;
4619 if (flags & MSG_DONTWAIT)
4620 req->flags |= REQ_F_NOWAIT;
4621 else if (force_nonblock)
4622 flags |= MSG_DONTWAIT;
4623
4624 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4625 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004626 if (force_nonblock && ret == -EAGAIN)
4627 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004628 if (ret == -ERESTARTSYS)
4629 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004630
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004631 if (req->flags & REQ_F_BUFFER_SELECTED)
4632 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004633 /* fast path, check for non-NULL to avoid function call */
4634 if (kmsg->free_iov)
4635 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004636 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004637 if (ret < 0)
4638 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004639 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004640 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004641}
4642
Pavel Begunkov889fca72021-02-10 00:03:09 +00004643static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004644{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004645 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004646 struct io_sr_msg *sr = &req->sr_msg;
4647 struct msghdr msg;
4648 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004649 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004650 struct iovec iov;
4651 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004652 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004653 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004654
Florent Revestdba4a922020-12-04 12:36:04 +01004655 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004656 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004657 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004658
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004659 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004660 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004661 if (IS_ERR(kbuf))
4662 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004663 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004664 }
4665
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004666 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004667 if (unlikely(ret))
4668 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004669
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004670 msg.msg_name = NULL;
4671 msg.msg_control = NULL;
4672 msg.msg_controllen = 0;
4673 msg.msg_namelen = 0;
4674 msg.msg_iocb = NULL;
4675 msg.msg_flags = 0;
4676
4677 flags = req->sr_msg.msg_flags;
4678 if (flags & MSG_DONTWAIT)
4679 req->flags |= REQ_F_NOWAIT;
4680 else if (force_nonblock)
4681 flags |= MSG_DONTWAIT;
4682
4683 ret = sock_recvmsg(sock, &msg, flags);
4684 if (force_nonblock && ret == -EAGAIN)
4685 return -EAGAIN;
4686 if (ret == -ERESTARTSYS)
4687 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004688out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004689 if (req->flags & REQ_F_BUFFER_SELECTED)
4690 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004691 if (ret < 0)
4692 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004693 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004694 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004695}
4696
Jens Axboe3529d8c2019-12-19 18:24:38 -07004697static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004698{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004699 struct io_accept *accept = &req->accept;
4700
Jens Axboe14587a462020-09-05 11:36:08 -06004701 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004702 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004703 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004704 return -EINVAL;
4705
Jens Axboed55e5f52019-12-11 16:12:15 -07004706 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4707 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004708 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004709 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004710 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004711}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004712
Pavel Begunkov889fca72021-02-10 00:03:09 +00004713static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004714{
4715 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004716 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004717 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004718 int ret;
4719
Jiufei Xuee697dee2020-06-10 13:41:59 +08004720 if (req->file->f_flags & O_NONBLOCK)
4721 req->flags |= REQ_F_NOWAIT;
4722
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004723 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004724 accept->addr_len, accept->flags,
4725 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004726 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004727 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004728 if (ret < 0) {
4729 if (ret == -ERESTARTSYS)
4730 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004731 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004732 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004733 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004734 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004735}
4736
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004737static int io_connect_prep_async(struct io_kiocb *req)
4738{
4739 struct io_async_connect *io = req->async_data;
4740 struct io_connect *conn = &req->connect;
4741
4742 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4743}
4744
Jens Axboe3529d8c2019-12-19 18:24:38 -07004745static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004746{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004747 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004748
Jens Axboe14587a462020-09-05 11:36:08 -06004749 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004750 return -EINVAL;
4751 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4752 return -EINVAL;
4753
Jens Axboe3529d8c2019-12-19 18:24:38 -07004754 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4755 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004756 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004757}
4758
Pavel Begunkov889fca72021-02-10 00:03:09 +00004759static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004760{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004761 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004762 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004763 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004764 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004765
Jens Axboee8c2bc12020-08-15 18:44:09 -07004766 if (req->async_data) {
4767 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004768 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004769 ret = move_addr_to_kernel(req->connect.addr,
4770 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004771 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004772 if (ret)
4773 goto out;
4774 io = &__io;
4775 }
4776
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004777 file_flags = force_nonblock ? O_NONBLOCK : 0;
4778
Jens Axboee8c2bc12020-08-15 18:44:09 -07004779 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004780 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004781 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004782 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004783 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004784 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004785 ret = -ENOMEM;
4786 goto out;
4787 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004788 io = req->async_data;
4789 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004790 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004791 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004792 if (ret == -ERESTARTSYS)
4793 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004794out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004795 if (ret < 0)
4796 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004797 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004798 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004799}
YueHaibing469956e2020-03-04 15:53:52 +08004800#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004801#define IO_NETOP_FN(op) \
4802static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4803{ \
4804 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004805}
4806
Jens Axboe99a10082021-02-19 09:35:19 -07004807#define IO_NETOP_PREP(op) \
4808IO_NETOP_FN(op) \
4809static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4810{ \
4811 return -EOPNOTSUPP; \
4812} \
4813
4814#define IO_NETOP_PREP_ASYNC(op) \
4815IO_NETOP_PREP(op) \
4816static int io_##op##_prep_async(struct io_kiocb *req) \
4817{ \
4818 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004819}
4820
Jens Axboe99a10082021-02-19 09:35:19 -07004821IO_NETOP_PREP_ASYNC(sendmsg);
4822IO_NETOP_PREP_ASYNC(recvmsg);
4823IO_NETOP_PREP_ASYNC(connect);
4824IO_NETOP_PREP(accept);
4825IO_NETOP_FN(send);
4826IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004827#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004828
Jens Axboed7718a92020-02-14 22:23:12 -07004829struct io_poll_table {
4830 struct poll_table_struct pt;
4831 struct io_kiocb *req;
4832 int error;
4833};
4834
Jens Axboed7718a92020-02-14 22:23:12 -07004835static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4836 __poll_t mask, task_work_func_t func)
4837{
Jens Axboeaa96bf82020-04-03 11:26:26 -06004838 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004839
4840 /* for instances that support it check for an event match first: */
4841 if (mask && !(mask & poll->events))
4842 return 0;
4843
4844 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4845
4846 list_del_init(&poll->wait.entry);
4847
Jens Axboed7718a92020-02-14 22:23:12 -07004848 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00004849 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06004850 percpu_ref_get(&req->ctx->refs);
4851
Jens Axboed7718a92020-02-14 22:23:12 -07004852 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004853 * If this fails, then the task is exiting. When a task exits, the
4854 * work gets canceled, so just cancel this request as well instead
4855 * of executing it. We can't safely execute it anyway, as we may not
4856 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004857 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06004858 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004859 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004860 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00004861 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004862 }
Jens Axboed7718a92020-02-14 22:23:12 -07004863 return 1;
4864}
4865
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004866static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4867 __acquires(&req->ctx->completion_lock)
4868{
4869 struct io_ring_ctx *ctx = req->ctx;
4870
4871 if (!req->result && !READ_ONCE(poll->canceled)) {
4872 struct poll_table_struct pt = { ._key = poll->events };
4873
4874 req->result = vfs_poll(req->file, &pt) & poll->events;
4875 }
4876
4877 spin_lock_irq(&ctx->completion_lock);
4878 if (!req->result && !READ_ONCE(poll->canceled)) {
4879 add_wait_queue(poll->head, &poll->wait);
4880 return true;
4881 }
4882
4883 return false;
4884}
4885
Jens Axboed4e7cd32020-08-15 11:44:50 -07004886static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004887{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004888 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004889 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004890 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004891 return req->apoll->double_poll;
4892}
4893
4894static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4895{
4896 if (req->opcode == IORING_OP_POLL_ADD)
4897 return &req->poll;
4898 return &req->apoll->poll;
4899}
4900
4901static void io_poll_remove_double(struct io_kiocb *req)
4902{
4903 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004904
4905 lockdep_assert_held(&req->ctx->completion_lock);
4906
4907 if (poll && poll->head) {
4908 struct wait_queue_head *head = poll->head;
4909
4910 spin_lock(&head->lock);
4911 list_del_init(&poll->wait.entry);
4912 if (poll->wait.private)
4913 refcount_dec(&req->refs);
4914 poll->head = NULL;
4915 spin_unlock(&head->lock);
4916 }
4917}
4918
4919static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4920{
4921 struct io_ring_ctx *ctx = req->ctx;
4922
Jens Axboed4e7cd32020-08-15 11:44:50 -07004923 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004924 req->poll.done = true;
4925 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4926 io_commit_cqring(ctx);
4927}
4928
Jens Axboe18bceab2020-05-15 11:56:54 -06004929static void io_poll_task_func(struct callback_head *cb)
4930{
4931 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004932 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004933 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06004934
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004935 if (io_poll_rewait(req, &req->poll)) {
4936 spin_unlock_irq(&ctx->completion_lock);
4937 } else {
4938 hash_del(&req->hash_node);
4939 io_poll_complete(req, req->result, 0);
4940 spin_unlock_irq(&ctx->completion_lock);
4941
4942 nxt = io_put_req_find_next(req);
4943 io_cqring_ev_posted(ctx);
4944 if (nxt)
4945 __io_req_task_submit(nxt);
4946 }
4947
Jens Axboe6d816e02020-08-11 08:04:14 -06004948 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004949}
4950
4951static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4952 int sync, void *key)
4953{
4954 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004955 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004956 __poll_t mask = key_to_poll(key);
4957
4958 /* for instances that support it check for an event match first: */
4959 if (mask && !(mask & poll->events))
4960 return 0;
4961
Jens Axboe8706e042020-09-28 08:38:54 -06004962 list_del_init(&wait->entry);
4963
Jens Axboe807abcb2020-07-17 17:09:27 -06004964 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004965 bool done;
4966
Jens Axboe807abcb2020-07-17 17:09:27 -06004967 spin_lock(&poll->head->lock);
4968 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004969 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004970 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004971 /* make sure double remove sees this as being gone */
4972 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004973 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06004974 if (!done) {
4975 /* use wait func handler, so it matches the rq type */
4976 poll->wait.func(&poll->wait, mode, sync, key);
4977 }
Jens Axboe18bceab2020-05-15 11:56:54 -06004978 }
4979 refcount_dec(&req->refs);
4980 return 1;
4981}
4982
4983static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4984 wait_queue_func_t wake_func)
4985{
4986 poll->head = NULL;
4987 poll->done = false;
4988 poll->canceled = false;
4989 poll->events = events;
4990 INIT_LIST_HEAD(&poll->wait.entry);
4991 init_waitqueue_func_entry(&poll->wait, wake_func);
4992}
4993
4994static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004995 struct wait_queue_head *head,
4996 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004997{
4998 struct io_kiocb *req = pt->req;
4999
5000 /*
5001 * If poll->head is already set, it's because the file being polled
5002 * uses multiple waitqueues for poll handling (eg one for read, one
5003 * for write). Setup a separate io_poll_iocb if this happens.
5004 */
5005 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005006 struct io_poll_iocb *poll_one = poll;
5007
Jens Axboe18bceab2020-05-15 11:56:54 -06005008 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005009 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005010 pt->error = -EINVAL;
5011 return;
5012 }
5013 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5014 if (!poll) {
5015 pt->error = -ENOMEM;
5016 return;
5017 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005018 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005019 refcount_inc(&req->refs);
5020 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005021 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005022 }
5023
5024 pt->error = 0;
5025 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005026
5027 if (poll->events & EPOLLEXCLUSIVE)
5028 add_wait_queue_exclusive(head, &poll->wait);
5029 else
5030 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005031}
5032
5033static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5034 struct poll_table_struct *p)
5035{
5036 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005037 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005038
Jens Axboe807abcb2020-07-17 17:09:27 -06005039 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005040}
5041
Jens Axboed7718a92020-02-14 22:23:12 -07005042static void io_async_task_func(struct callback_head *cb)
5043{
5044 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5045 struct async_poll *apoll = req->apoll;
5046 struct io_ring_ctx *ctx = req->ctx;
5047
5048 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5049
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005050 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005051 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005052 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005053 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005054 }
5055
Jens Axboe31067252020-05-17 17:43:31 -06005056 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005057 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005058 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005059
Jens Axboed4e7cd32020-08-15 11:44:50 -07005060 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005061 spin_unlock_irq(&ctx->completion_lock);
5062
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005063 if (!READ_ONCE(apoll->poll.canceled))
5064 __io_req_task_submit(req);
5065 else
5066 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005067
Jens Axboe6d816e02020-08-11 08:04:14 -06005068 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005069 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005070 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005071}
5072
5073static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5074 void *key)
5075{
5076 struct io_kiocb *req = wait->private;
5077 struct io_poll_iocb *poll = &req->apoll->poll;
5078
5079 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5080 key_to_poll(key));
5081
5082 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5083}
5084
5085static void io_poll_req_insert(struct io_kiocb *req)
5086{
5087 struct io_ring_ctx *ctx = req->ctx;
5088 struct hlist_head *list;
5089
5090 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5091 hlist_add_head(&req->hash_node, list);
5092}
5093
5094static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5095 struct io_poll_iocb *poll,
5096 struct io_poll_table *ipt, __poll_t mask,
5097 wait_queue_func_t wake_func)
5098 __acquires(&ctx->completion_lock)
5099{
5100 struct io_ring_ctx *ctx = req->ctx;
5101 bool cancel = false;
5102
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005103 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005104 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005105 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005106 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005107
5108 ipt->pt._key = mask;
5109 ipt->req = req;
5110 ipt->error = -EINVAL;
5111
Jens Axboed7718a92020-02-14 22:23:12 -07005112 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5113
5114 spin_lock_irq(&ctx->completion_lock);
5115 if (likely(poll->head)) {
5116 spin_lock(&poll->head->lock);
5117 if (unlikely(list_empty(&poll->wait.entry))) {
5118 if (ipt->error)
5119 cancel = true;
5120 ipt->error = 0;
5121 mask = 0;
5122 }
5123 if (mask || ipt->error)
5124 list_del_init(&poll->wait.entry);
5125 else if (cancel)
5126 WRITE_ONCE(poll->canceled, true);
5127 else if (!poll->done) /* actually waiting for an event */
5128 io_poll_req_insert(req);
5129 spin_unlock(&poll->head->lock);
5130 }
5131
5132 return mask;
5133}
5134
5135static bool io_arm_poll_handler(struct io_kiocb *req)
5136{
5137 const struct io_op_def *def = &io_op_defs[req->opcode];
5138 struct io_ring_ctx *ctx = req->ctx;
5139 struct async_poll *apoll;
5140 struct io_poll_table ipt;
5141 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005142 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005143
5144 if (!req->file || !file_can_poll(req->file))
5145 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005146 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005147 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005148 if (def->pollin)
5149 rw = READ;
5150 else if (def->pollout)
5151 rw = WRITE;
5152 else
5153 return false;
5154 /* if we can't nonblock try, then no point in arming a poll handler */
5155 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005156 return false;
5157
5158 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5159 if (unlikely(!apoll))
5160 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005161 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005162
5163 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005164 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005165
Nathan Chancellor8755d972020-03-02 16:01:19 -07005166 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005167 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005168 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005169 if (def->pollout)
5170 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005171
5172 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5173 if ((req->opcode == IORING_OP_RECVMSG) &&
5174 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5175 mask &= ~POLLIN;
5176
Jens Axboed7718a92020-02-14 22:23:12 -07005177 mask |= POLLERR | POLLPRI;
5178
5179 ipt.pt._qproc = io_async_queue_proc;
5180
5181 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5182 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005183 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005184 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005185 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005186 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005187 kfree(apoll);
5188 return false;
5189 }
5190 spin_unlock_irq(&ctx->completion_lock);
5191 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5192 apoll->poll.events);
5193 return true;
5194}
5195
5196static bool __io_poll_remove_one(struct io_kiocb *req,
5197 struct io_poll_iocb *poll)
5198{
Jens Axboeb41e9852020-02-17 09:52:41 -07005199 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005200
5201 spin_lock(&poll->head->lock);
5202 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005203 if (!list_empty(&poll->wait.entry)) {
5204 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005205 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005206 }
5207 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005208 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005209 return do_complete;
5210}
5211
5212static bool io_poll_remove_one(struct io_kiocb *req)
5213{
5214 bool do_complete;
5215
Jens Axboed4e7cd32020-08-15 11:44:50 -07005216 io_poll_remove_double(req);
5217
Jens Axboed7718a92020-02-14 22:23:12 -07005218 if (req->opcode == IORING_OP_POLL_ADD) {
5219 do_complete = __io_poll_remove_one(req, &req->poll);
5220 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005221 struct async_poll *apoll = req->apoll;
5222
Jens Axboed7718a92020-02-14 22:23:12 -07005223 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005224 do_complete = __io_poll_remove_one(req, &apoll->poll);
5225 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005226 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005227 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005228 kfree(apoll);
5229 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005230 }
5231
Jens Axboeb41e9852020-02-17 09:52:41 -07005232 if (do_complete) {
5233 io_cqring_fill_event(req, -ECANCELED);
5234 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005235 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005236 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005237 }
5238
5239 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005240}
5241
Jens Axboe76e1b642020-09-26 15:05:03 -06005242/*
5243 * Returns true if we found and killed one or more poll requests
5244 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005245static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5246 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005247{
Jens Axboe78076bb2019-12-04 19:56:40 -07005248 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005249 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005250 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005251
5252 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005253 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5254 struct hlist_head *list;
5255
5256 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005257 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005258 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005259 posted += io_poll_remove_one(req);
5260 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005261 }
5262 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005263
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005264 if (posted)
5265 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005266
5267 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005268}
5269
Jens Axboe47f46762019-11-09 17:43:02 -07005270static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5271{
Jens Axboe78076bb2019-12-04 19:56:40 -07005272 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005273 struct io_kiocb *req;
5274
Jens Axboe78076bb2019-12-04 19:56:40 -07005275 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5276 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005277 if (sqe_addr != req->user_data)
5278 continue;
5279 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005280 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005281 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005282 }
5283
5284 return -ENOENT;
5285}
5286
Jens Axboe3529d8c2019-12-19 18:24:38 -07005287static int io_poll_remove_prep(struct io_kiocb *req,
5288 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005289{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005290 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5291 return -EINVAL;
5292 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5293 sqe->poll_events)
5294 return -EINVAL;
5295
Pavel Begunkov018043b2020-10-27 23:17:18 +00005296 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005297 return 0;
5298}
5299
5300/*
5301 * Find a running poll command that matches one specified in sqe->addr,
5302 * and remove it if found.
5303 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005304static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005305{
5306 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005307 int ret;
5308
Jens Axboe221c5eb2019-01-17 09:41:58 -07005309 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005310 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005311 spin_unlock_irq(&ctx->completion_lock);
5312
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005313 if (ret < 0)
5314 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005315 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005316 return 0;
5317}
5318
Jens Axboe221c5eb2019-01-17 09:41:58 -07005319static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5320 void *key)
5321{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005322 struct io_kiocb *req = wait->private;
5323 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005324
Jens Axboed7718a92020-02-14 22:23:12 -07005325 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005326}
5327
Jens Axboe221c5eb2019-01-17 09:41:58 -07005328static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5329 struct poll_table_struct *p)
5330{
5331 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5332
Jens Axboee8c2bc12020-08-15 18:44:09 -07005333 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005334}
5335
Jens Axboe3529d8c2019-12-19 18:24:38 -07005336static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005337{
5338 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005339 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005340
5341 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5342 return -EINVAL;
5343 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5344 return -EINVAL;
5345
Jiufei Xue5769a352020-06-17 17:53:55 +08005346 events = READ_ONCE(sqe->poll32_events);
5347#ifdef __BIG_ENDIAN
5348 events = swahw32(events);
5349#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005350 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5351 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005352 return 0;
5353}
5354
Pavel Begunkov61e98202021-02-10 00:03:08 +00005355static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005356{
5357 struct io_poll_iocb *poll = &req->poll;
5358 struct io_ring_ctx *ctx = req->ctx;
5359 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005360 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005361
Jens Axboed7718a92020-02-14 22:23:12 -07005362 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005363
Jens Axboed7718a92020-02-14 22:23:12 -07005364 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5365 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005366
Jens Axboe8c838782019-03-12 15:48:16 -06005367 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005368 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005369 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005370 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005371 spin_unlock_irq(&ctx->completion_lock);
5372
Jens Axboe8c838782019-03-12 15:48:16 -06005373 if (mask) {
5374 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005375 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005376 }
Jens Axboe8c838782019-03-12 15:48:16 -06005377 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005378}
5379
Jens Axboe5262f562019-09-17 12:26:57 -06005380static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5381{
Jens Axboead8a48a2019-11-15 08:49:11 -07005382 struct io_timeout_data *data = container_of(timer,
5383 struct io_timeout_data, timer);
5384 struct io_kiocb *req = data->req;
5385 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005386 unsigned long flags;
5387
Jens Axboe5262f562019-09-17 12:26:57 -06005388 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005389 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005390 atomic_set(&req->ctx->cq_timeouts,
5391 atomic_read(&req->ctx->cq_timeouts) + 1);
5392
Jens Axboe78e19bb2019-11-06 15:21:34 -07005393 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005394 io_commit_cqring(ctx);
5395 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5396
5397 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005398 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005399 io_put_req(req);
5400 return HRTIMER_NORESTART;
5401}
5402
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005403static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5404 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005405{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005406 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005407 struct io_kiocb *req;
5408 int ret = -ENOENT;
5409
5410 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5411 if (user_data == req->user_data) {
5412 ret = 0;
5413 break;
5414 }
5415 }
5416
5417 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005418 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005419
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005420 io = req->async_data;
5421 ret = hrtimer_try_to_cancel(&io->timer);
5422 if (ret == -1)
5423 return ERR_PTR(-EALREADY);
5424 list_del_init(&req->timeout.list);
5425 return req;
5426}
5427
5428static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5429{
5430 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5431
5432 if (IS_ERR(req))
5433 return PTR_ERR(req);
5434
5435 req_set_fail_links(req);
5436 io_cqring_fill_event(req, -ECANCELED);
5437 io_put_req_deferred(req, 1);
5438 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005439}
5440
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005441static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5442 struct timespec64 *ts, enum hrtimer_mode mode)
5443{
5444 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5445 struct io_timeout_data *data;
5446
5447 if (IS_ERR(req))
5448 return PTR_ERR(req);
5449
5450 req->timeout.off = 0; /* noseq */
5451 data = req->async_data;
5452 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5453 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5454 data->timer.function = io_timeout_fn;
5455 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5456 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005457}
5458
Jens Axboe3529d8c2019-12-19 18:24:38 -07005459static int io_timeout_remove_prep(struct io_kiocb *req,
5460 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005461{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005462 struct io_timeout_rem *tr = &req->timeout_rem;
5463
Jens Axboeb29472e2019-12-17 18:50:29 -07005464 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5465 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005466 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5467 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005468 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005469 return -EINVAL;
5470
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005471 tr->addr = READ_ONCE(sqe->addr);
5472 tr->flags = READ_ONCE(sqe->timeout_flags);
5473 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5474 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5475 return -EINVAL;
5476 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5477 return -EFAULT;
5478 } else if (tr->flags) {
5479 /* timeout removal doesn't support flags */
5480 return -EINVAL;
5481 }
5482
Jens Axboeb29472e2019-12-17 18:50:29 -07005483 return 0;
5484}
5485
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005486static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5487{
5488 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5489 : HRTIMER_MODE_REL;
5490}
5491
Jens Axboe11365042019-10-16 09:08:32 -06005492/*
5493 * Remove or update an existing timeout command
5494 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005495static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005496{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005497 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005498 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005499 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005500
Jens Axboe11365042019-10-16 09:08:32 -06005501 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005502 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005503 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005504 else
5505 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5506 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005507
Jens Axboe47f46762019-11-09 17:43:02 -07005508 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005509 io_commit_cqring(ctx);
5510 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005511 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005512 if (ret < 0)
5513 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005514 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005515 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005516}
5517
Jens Axboe3529d8c2019-12-19 18:24:38 -07005518static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005519 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005520{
Jens Axboead8a48a2019-11-15 08:49:11 -07005521 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005522 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005523 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005524
Jens Axboead8a48a2019-11-15 08:49:11 -07005525 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005526 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005527 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005528 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005529 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005530 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005531 flags = READ_ONCE(sqe->timeout_flags);
5532 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005533 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005534
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005535 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005536
Jens Axboee8c2bc12020-08-15 18:44:09 -07005537 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005538 return -ENOMEM;
5539
Jens Axboee8c2bc12020-08-15 18:44:09 -07005540 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005541 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005542
5543 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005544 return -EFAULT;
5545
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005546 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005547 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5548 return 0;
5549}
5550
Pavel Begunkov61e98202021-02-10 00:03:08 +00005551static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005552{
Jens Axboead8a48a2019-11-15 08:49:11 -07005553 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005554 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005555 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005556 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005557
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005558 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005559
Jens Axboe5262f562019-09-17 12:26:57 -06005560 /*
5561 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005562 * timeout event to be satisfied. If it isn't set, then this is
5563 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005564 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005565 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005566 entry = ctx->timeout_list.prev;
5567 goto add;
5568 }
Jens Axboe5262f562019-09-17 12:26:57 -06005569
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005570 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5571 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005572
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005573 /* Update the last seq here in case io_flush_timeouts() hasn't.
5574 * This is safe because ->completion_lock is held, and submissions
5575 * and completions are never mixed in the same ->completion_lock section.
5576 */
5577 ctx->cq_last_tm_flush = tail;
5578
Jens Axboe5262f562019-09-17 12:26:57 -06005579 /*
5580 * Insertion sort, ensuring the first entry in the list is always
5581 * the one we need first.
5582 */
Jens Axboe5262f562019-09-17 12:26:57 -06005583 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005584 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5585 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005586
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005587 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005588 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005589 /* nxt.seq is behind @tail, otherwise would've been completed */
5590 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005591 break;
5592 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005593add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005594 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005595 data->timer.function = io_timeout_fn;
5596 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005597 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005598 return 0;
5599}
5600
Jens Axboe62755e32019-10-28 21:49:21 -06005601static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005602{
Jens Axboe62755e32019-10-28 21:49:21 -06005603 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005604
Jens Axboe62755e32019-10-28 21:49:21 -06005605 return req->user_data == (unsigned long) data;
5606}
5607
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005608static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005609{
Jens Axboe62755e32019-10-28 21:49:21 -06005610 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005611 int ret = 0;
5612
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005613 if (!tctx->io_wq)
5614 return -ENOENT;
5615
5616 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005617 switch (cancel_ret) {
5618 case IO_WQ_CANCEL_OK:
5619 ret = 0;
5620 break;
5621 case IO_WQ_CANCEL_RUNNING:
5622 ret = -EALREADY;
5623 break;
5624 case IO_WQ_CANCEL_NOTFOUND:
5625 ret = -ENOENT;
5626 break;
5627 }
5628
Jens Axboee977d6d2019-11-05 12:39:45 -07005629 return ret;
5630}
5631
Jens Axboe47f46762019-11-09 17:43:02 -07005632static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5633 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005634 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005635{
5636 unsigned long flags;
5637 int ret;
5638
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005639 ret = io_async_cancel_one(req->task->io_uring,
5640 (void *) (unsigned long) sqe_addr);
Jens Axboe47f46762019-11-09 17:43:02 -07005641 if (ret != -ENOENT) {
5642 spin_lock_irqsave(&ctx->completion_lock, flags);
5643 goto done;
5644 }
5645
5646 spin_lock_irqsave(&ctx->completion_lock, flags);
5647 ret = io_timeout_cancel(ctx, sqe_addr);
5648 if (ret != -ENOENT)
5649 goto done;
5650 ret = io_poll_cancel(ctx, sqe_addr);
5651done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005652 if (!ret)
5653 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005654 io_cqring_fill_event(req, ret);
5655 io_commit_cqring(ctx);
5656 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5657 io_cqring_ev_posted(ctx);
5658
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005659 if (ret < 0)
5660 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005661 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005662}
5663
Jens Axboe3529d8c2019-12-19 18:24:38 -07005664static int io_async_cancel_prep(struct io_kiocb *req,
5665 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005666{
Jens Axboefbf23842019-12-17 18:45:56 -07005667 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005668 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005669 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5670 return -EINVAL;
5671 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005672 return -EINVAL;
5673
Jens Axboefbf23842019-12-17 18:45:56 -07005674 req->cancel.addr = READ_ONCE(sqe->addr);
5675 return 0;
5676}
5677
Pavel Begunkov61e98202021-02-10 00:03:08 +00005678static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005679{
5680 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005681
Pavel Begunkov014db002020-03-03 21:33:12 +03005682 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005683 return 0;
5684}
5685
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005686static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005687 const struct io_uring_sqe *sqe)
5688{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005689 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5690 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005691 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5692 return -EINVAL;
5693 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005694 return -EINVAL;
5695
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005696 req->rsrc_update.offset = READ_ONCE(sqe->off);
5697 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5698 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005699 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005700 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005701 return 0;
5702}
5703
Pavel Begunkov889fca72021-02-10 00:03:09 +00005704static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005705{
5706 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005707 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005708 int ret;
5709
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005710 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005711 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005712
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005713 up.offset = req->rsrc_update.offset;
5714 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005715
5716 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005717 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005718 mutex_unlock(&ctx->uring_lock);
5719
5720 if (ret < 0)
5721 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005722 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005723 return 0;
5724}
5725
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005726static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005727{
Jens Axboed625c6e2019-12-17 19:53:05 -07005728 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005729 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005730 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005731 case IORING_OP_READV:
5732 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005733 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005734 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005735 case IORING_OP_WRITEV:
5736 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005737 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005738 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005739 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005740 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005741 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005742 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005743 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005744 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005745 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005746 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005747 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005748 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005749 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005750 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005751 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005752 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005753 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005754 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005755 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005756 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005757 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005758 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005759 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005760 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005761 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005762 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005763 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005764 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005765 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005766 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005767 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005768 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005769 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005770 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005771 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005772 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005773 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005774 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005775 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005776 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005777 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005778 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005779 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005780 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005781 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005782 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005783 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005784 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005785 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005786 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005787 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005788 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005789 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005790 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06005791 case IORING_OP_SHUTDOWN:
5792 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06005793 case IORING_OP_RENAMEAT:
5794 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06005795 case IORING_OP_UNLINKAT:
5796 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005797 }
5798
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005799 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5800 req->opcode);
5801 return-EINVAL;
5802}
5803
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005804static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005805{
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005806 switch (req->opcode) {
5807 case IORING_OP_READV:
5808 case IORING_OP_READ_FIXED:
5809 case IORING_OP_READ:
5810 return io_rw_prep_async(req, READ);
5811 case IORING_OP_WRITEV:
5812 case IORING_OP_WRITE_FIXED:
5813 case IORING_OP_WRITE:
5814 return io_rw_prep_async(req, WRITE);
5815 case IORING_OP_SENDMSG:
5816 case IORING_OP_SEND:
5817 return io_sendmsg_prep_async(req);
5818 case IORING_OP_RECVMSG:
5819 case IORING_OP_RECV:
5820 return io_recvmsg_prep_async(req);
5821 case IORING_OP_CONNECT:
5822 return io_connect_prep_async(req);
5823 }
5824 return 0;
5825}
5826
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005827static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005828{
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005829 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005830 return 0;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005831 /* some opcodes init it during the inital prep */
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005832 if (req->async_data)
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005833 return 0;
5834 if (__io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005835 return -EAGAIN;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005836 return io_req_prep_async(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005837}
5838
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005839static u32 io_get_sequence(struct io_kiocb *req)
5840{
5841 struct io_kiocb *pos;
5842 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005843 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005844
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005845 io_for_each_link(pos, req)
5846 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005847
5848 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5849 return total_submitted - nr_reqs;
5850}
5851
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005852static int io_req_defer(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005853{
5854 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005855 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005856 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005857 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005858
5859 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005860 if (likely(list_empty_careful(&ctx->defer_list) &&
5861 !(req->flags & REQ_F_IO_DRAIN)))
5862 return 0;
5863
5864 seq = io_get_sequence(req);
5865 /* Still a chance to pass the sequence check */
5866 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005867 return 0;
5868
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005869 ret = io_req_defer_prep(req);
5870 if (ret)
5871 return ret;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005872 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005873 de = kmalloc(sizeof(*de), GFP_KERNEL);
5874 if (!de)
5875 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005876
5877 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005878 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005879 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005880 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005881 io_queue_async_work(req);
5882 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005883 }
5884
5885 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005886 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005887 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005888 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07005889 spin_unlock_irq(&ctx->completion_lock);
5890 return -EIOCBQUEUED;
5891}
Jens Axboeedafcce2019-01-09 09:16:05 -07005892
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005893static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005894{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005895 if (req->flags & REQ_F_BUFFER_SELECTED) {
5896 switch (req->opcode) {
5897 case IORING_OP_READV:
5898 case IORING_OP_READ_FIXED:
5899 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005900 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005901 break;
5902 case IORING_OP_RECVMSG:
5903 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005904 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005905 break;
5906 }
5907 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005908 }
5909
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005910 if (req->flags & REQ_F_NEED_CLEANUP) {
5911 switch (req->opcode) {
5912 case IORING_OP_READV:
5913 case IORING_OP_READ_FIXED:
5914 case IORING_OP_READ:
5915 case IORING_OP_WRITEV:
5916 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005917 case IORING_OP_WRITE: {
5918 struct io_async_rw *io = req->async_data;
5919 if (io->free_iovec)
5920 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005921 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005922 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005923 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07005924 case IORING_OP_SENDMSG: {
5925 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005926
5927 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005928 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005929 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005930 case IORING_OP_SPLICE:
5931 case IORING_OP_TEE:
5932 io_put_file(req, req->splice.file_in,
5933 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5934 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005935 case IORING_OP_OPENAT:
5936 case IORING_OP_OPENAT2:
5937 if (req->open.filename)
5938 putname(req->open.filename);
5939 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06005940 case IORING_OP_RENAMEAT:
5941 putname(req->rename.oldpath);
5942 putname(req->rename.newpath);
5943 break;
Jens Axboe14a11432020-09-28 14:27:37 -06005944 case IORING_OP_UNLINKAT:
5945 putname(req->unlink.filename);
5946 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005947 }
5948 req->flags &= ~REQ_F_NEED_CLEANUP;
5949 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005950}
5951
Pavel Begunkov889fca72021-02-10 00:03:09 +00005952static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07005953{
Jens Axboeedafcce2019-01-09 09:16:05 -07005954 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005955 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07005956
Jens Axboed625c6e2019-12-17 19:53:05 -07005957 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005958 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005959 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07005960 break;
5961 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005962 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005963 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005964 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005965 break;
5966 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07005967 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005968 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005969 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005970 break;
5971 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005972 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005973 break;
5974 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005975 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005976 break;
5977 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005978 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005979 break;
5980 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005981 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005982 break;
5983 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005984 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005985 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005986 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005987 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07005988 break;
5989 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005990 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01005991 break;
Jens Axboefddafac2020-01-04 20:19:44 -07005992 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00005993 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005994 break;
5995 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005996 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005997 break;
5998 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00005999 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006000 break;
6001 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006002 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006003 break;
6004 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006005 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006006 break;
6007 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006008 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006009 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006010 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006011 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006012 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006013 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006014 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006015 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006016 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006017 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006018 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006019 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006020 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006021 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006022 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006023 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006024 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006025 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006026 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006027 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006028 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006029 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006030 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006031 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006032 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006033 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006034 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006035 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006036 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006037 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006038 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006039 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006040 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006041 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006042 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006043 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006044 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006045 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006046 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006047 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006048 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006049 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006050 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006051 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006052 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006053 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006054 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006055 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006056 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006057 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006058 default:
6059 ret = -EINVAL;
6060 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006061 }
6062
6063 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006064 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006065
Jens Axboeb5325762020-05-19 21:20:27 -06006066 /* If the op doesn't have a file, we're not polling for it */
6067 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006068 const bool in_async = io_wq_current_is_worker();
6069
Jens Axboe11ba8202020-01-15 21:51:17 -07006070 /* workqueue context doesn't hold uring_lock, grab it now */
6071 if (in_async)
6072 mutex_lock(&ctx->uring_lock);
6073
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006074 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006075
6076 if (in_async)
6077 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006078 }
6079
6080 return 0;
6081}
6082
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006083static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006084{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006085 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006086 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006087 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006088
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006089 timeout = io_prep_linked_timeout(req);
6090 if (timeout)
6091 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006092
Jens Axboe4014d942021-01-19 15:53:54 -07006093 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006094 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006095
Jens Axboe561fb042019-10-24 07:25:42 -06006096 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006097 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006098 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006099 /*
6100 * We can get EAGAIN for polled IO even though we're
6101 * forcing a sync submission from here, since we can't
6102 * wait for request slots on the block side.
6103 */
6104 if (ret != -EAGAIN)
6105 break;
6106 cond_resched();
6107 } while (1);
6108 }
Jens Axboe31b51512019-01-18 22:56:34 -07006109
Pavel Begunkova3df76982021-02-18 22:32:52 +00006110 /* avoid locking problems by failing it from a clean context */
Jens Axboe561fb042019-10-24 07:25:42 -06006111 if (ret) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00006112 /* io-wq is going to take one down */
6113 refcount_inc(&req->refs);
6114 io_req_task_queue_fail(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006115 }
Jens Axboe31b51512019-01-18 22:56:34 -07006116}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006117
Jens Axboe65e19f52019-10-26 07:20:21 -06006118static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6119 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006120{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006121 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006122
Jens Axboe05f3fb32019-12-09 11:22:50 -07006123 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006124 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006125}
6126
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006127static struct file *io_file_get(struct io_submit_state *state,
6128 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006129{
6130 struct io_ring_ctx *ctx = req->ctx;
6131 struct file *file;
6132
6133 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006134 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006135 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006136 fd = array_index_nospec(fd, ctx->nr_user_files);
6137 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006138 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006139 } else {
6140 trace_io_uring_file_get(ctx, fd);
6141 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006142 }
6143
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006144 if (file && unlikely(file->f_op == &io_uring_fops))
6145 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006146 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006147}
6148
Jens Axboe2665abf2019-11-05 12:40:47 -07006149static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6150{
Jens Axboead8a48a2019-11-15 08:49:11 -07006151 struct io_timeout_data *data = container_of(timer,
6152 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006153 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006154 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006155 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006156
6157 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006158 prev = req->timeout.head;
6159 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006160
6161 /*
6162 * We don't expect the list to be empty, that will only happen if we
6163 * race with the completion of the linked work.
6164 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006165 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006166 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006167 else
6168 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006169 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6170
6171 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006172 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006173 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006174 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006175 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006176 io_req_complete_post(req, -ETIME, 0);
6177 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006178 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006179 return HRTIMER_NORESTART;
6180}
6181
Jens Axboe7271ef32020-08-10 09:55:22 -06006182static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006183{
Jens Axboe76a46e02019-11-10 23:34:16 -07006184 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006185 * If the back reference is NULL, then our linked request finished
6186 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006187 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006188 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006189 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006190
Jens Axboead8a48a2019-11-15 08:49:11 -07006191 data->timer.function = io_link_timeout_fn;
6192 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6193 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006194 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006195}
6196
6197static void io_queue_linked_timeout(struct io_kiocb *req)
6198{
6199 struct io_ring_ctx *ctx = req->ctx;
6200
6201 spin_lock_irq(&ctx->completion_lock);
6202 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006203 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006204
Jens Axboe2665abf2019-11-05 12:40:47 -07006205 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006206 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006207}
6208
Jens Axboead8a48a2019-11-15 08:49:11 -07006209static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006210{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006211 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006212
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006213 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6214 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006215 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006216
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006217 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006218 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006219 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006220 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006221}
6222
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006223static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006224{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006225 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe193155c2020-02-22 23:22:19 -07006226 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006227 int ret;
6228
Jens Axboe4379bf82021-02-15 13:40:22 -07006229 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6230 req->work.creds != current_cred())
6231 old_creds = override_creds(req->work.creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006232
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006233 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006234
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006235 if (old_creds)
6236 revert_creds(old_creds);
Jens Axboe491381ce2019-10-17 09:20:46 -06006237
6238 /*
6239 * We async punt it if the file wasn't marked NOWAIT, or if the file
6240 * doesn't support non-blocking read/write attempts
6241 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006242 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006243 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006244 /*
6245 * Queued up for async execution, worker will release
6246 * submit reference when the iocb is actually submitted.
6247 */
6248 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006249 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006250 } else if (likely(!ret)) {
6251 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006252 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006253 struct io_ring_ctx *ctx = req->ctx;
6254 struct io_comp_state *cs = &ctx->submit_state.comp;
Jens Axboee65ef562019-03-12 10:16:44 -06006255
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006256 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006257 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006258 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006259 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006260 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006261 }
6262 } else {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006263 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006264 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006265 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006266 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006267 if (linked_timeout)
6268 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006269}
6270
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006271static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006272{
6273 int ret;
6274
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006275 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006276 if (ret) {
6277 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006278fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006279 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006280 io_put_req(req);
6281 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006282 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006283 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006284 ret = io_req_defer_prep(req);
6285 if (unlikely(ret))
6286 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07006287 io_queue_async_work(req);
6288 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006289 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006290 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006291}
6292
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006293/*
6294 * Check SQE restrictions (opcode and flags).
6295 *
6296 * Returns 'true' if SQE is allowed, 'false' otherwise.
6297 */
6298static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6299 struct io_kiocb *req,
6300 unsigned int sqe_flags)
6301{
6302 if (!ctx->restricted)
6303 return true;
6304
6305 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6306 return false;
6307
6308 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6309 ctx->restrictions.sqe_flags_required)
6310 return false;
6311
6312 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6313 ctx->restrictions.sqe_flags_required))
6314 return false;
6315
6316 return true;
6317}
6318
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006319static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006320 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006321{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006322 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006323 unsigned int sqe_flags;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006324 int id, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006325
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006326 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006327 /* same numerical values with corresponding REQ_F_*, safe to copy */
6328 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006329 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006330 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006331 req->file = NULL;
6332 req->ctx = ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006333 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006334 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006335 /* one is dropped after submission, the other at completion */
6336 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006337 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006338 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006339
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006340 /* enforce forwards compatibility on users */
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006341 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
6342 req->flags = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006343 return -EINVAL;
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006344 }
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006345
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006346 if (unlikely(req->opcode >= IORING_OP_LAST))
6347 return -EINVAL;
6348
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006349 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6350 return -EACCES;
6351
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006352 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6353 !io_op_defs[req->opcode].buffer_select)
6354 return -EOPNOTSUPP;
6355
6356 id = READ_ONCE(sqe->personality);
6357 if (id) {
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006358 __io_req_init_async(req);
Jens Axboe4379bf82021-02-15 13:40:22 -07006359 req->work.creds = idr_find(&ctx->personality_idr, id);
6360 if (unlikely(!req->work.creds))
6361 return -EINVAL;
6362 get_cred(req->work.creds);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006363 }
6364
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006365 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006366
Jens Axboe27926b62020-10-28 09:33:23 -06006367 /*
6368 * Plug now if we have more than 1 IO left after this, and the target
6369 * is potentially a read/write to block based storage.
6370 */
6371 if (!state->plug_started && state->ios_left > 1 &&
6372 io_op_defs[req->opcode].plug) {
6373 blk_start_plug(&state->plug);
6374 state->plug_started = true;
6375 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006376
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006377 if (io_op_defs[req->opcode].needs_file) {
6378 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006379
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006380 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006381 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006382 ret = -EBADF;
6383 }
6384
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006385 state->ios_left--;
6386 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006387}
6388
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006389static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006390 const struct io_uring_sqe *sqe)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006391{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006392 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006393 int ret;
6394
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006395 ret = io_init_req(ctx, req, sqe);
6396 if (unlikely(ret)) {
6397fail_req:
6398 io_put_req(req);
6399 io_req_complete(req, ret);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006400 if (link->head) {
6401 /* fail even hard links since we don't submit */
Pavel Begunkovcf109602021-02-18 18:29:43 +00006402 link->head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006403 io_put_req(link->head);
6404 io_req_complete(link->head, -ECANCELED);
6405 link->head = NULL;
6406 }
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006407 return ret;
6408 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006409 ret = io_req_prep(req, sqe);
6410 if (unlikely(ret))
6411 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006412
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006413 /* don't need @sqe from now on */
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006414 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
6415 true, ctx->flags & IORING_SETUP_SQPOLL);
6416
Jens Axboe6c271ce2019-01-10 11:22:30 -07006417 /*
6418 * If we already have a head request, queue this one for async
6419 * submittal once the head completes. If we don't have a head but
6420 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6421 * submitted sync once the chain is complete. If none of those
6422 * conditions are true (normal request), then just queue it.
6423 */
6424 if (link->head) {
6425 struct io_kiocb *head = link->head;
6426
6427 /*
6428 * Taking sequential execution of a link, draining both sides
6429 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6430 * requests in the link. So, it drains the head and the
6431 * next after the link request. The last one is done via
6432 * drain_next flag to persist the effect across calls.
6433 */
6434 if (req->flags & REQ_F_IO_DRAIN) {
6435 head->flags |= REQ_F_IO_DRAIN;
6436 ctx->drain_next = 1;
6437 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006438 ret = io_req_defer_prep(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006439 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006440 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006441 trace_io_uring_link(ctx, req, head);
6442 link->last->link = req;
6443 link->last = req;
6444
6445 /* last request of a link, enqueue the link */
6446 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006447 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006448 link->head = NULL;
6449 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006450 } else {
6451 if (unlikely(ctx->drain_next)) {
6452 req->flags |= REQ_F_IO_DRAIN;
6453 ctx->drain_next = 0;
6454 }
6455 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006456 link->head = req;
6457 link->last = req;
6458 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006459 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006460 }
6461 }
6462
6463 return 0;
6464}
6465
6466/*
6467 * Batched submission is done, ensure local IO is flushed out.
6468 */
6469static void io_submit_state_end(struct io_submit_state *state,
6470 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006471{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006472 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006473 io_queue_sqe(state->link.head);
Jens Axboe3529d8c2019-12-19 18:24:38 -07006474 if (state->comp.nr)
Jens Axboe9e645e112019-05-10 16:07:28 -06006475 io_submit_flush_completions(&state->comp, ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006476 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006477 blk_finish_plug(&state->plug);
Jens Axboe75c6a032020-01-28 10:15:23 -07006478 io_state_file_put(state);
Jens Axboe9e645e112019-05-10 16:07:28 -06006479}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006480
Jens Axboe9e645e112019-05-10 16:07:28 -06006481/*
6482 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006483 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006484static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006485 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006486{
6487 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006488 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006489 /* set only head, no need to init link_last in advance */
6490 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006491}
6492
Jens Axboe193155c2020-02-22 23:22:19 -07006493static void io_commit_sqring(struct io_ring_ctx *ctx)
6494{
Jens Axboe75c6a032020-01-28 10:15:23 -07006495 struct io_rings *rings = ctx->rings;
6496
6497 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006498 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006499 * since once we write the new head, the application could
6500 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006501 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006502 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006503}
6504
Jens Axboe9e645e112019-05-10 16:07:28 -06006505/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006506 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006507 * that is mapped by userspace. This means that care needs to be taken to
6508 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006509 * being a good citizen. If members of the sqe are validated and then later
6510 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006511 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006512 */
6513static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006514{
6515 u32 *sq_array = ctx->sq_array;
6516 unsigned head;
6517
6518 /*
6519 * The cached sq head (or cq tail) serves two purposes:
6520 *
6521 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006522 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006523 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006524 * though the application is the one updating it.
6525 */
6526 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
6527 if (likely(head < ctx->sq_entries))
6528 return &ctx->sq_sqes[head];
6529
6530 /* drop invalid entries */
Pavel Begunkov711be032020-01-17 03:57:59 +03006531 ctx->cached_sq_dropped++;
6532 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
6533 return NULL;
6534}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006535
Jens Axboe0f212202020-09-13 13:09:39 -06006536static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006537{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006538 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006539
Jens Axboec4a2ed72019-11-21 21:01:26 -07006540 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006541 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006542 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006543 return -EBUSY;
6544 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006545
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006546 /* make sure SQ entry isn't read before tail */
6547 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006548
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006549 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6550 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006551
Jens Axboed8a6df12020-10-15 16:24:45 -06006552 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006553 refcount_add(nr, &current->usage);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006554 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006555
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006556 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006557 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006558 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006559
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006560 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006561 if (unlikely(!req)) {
6562 if (!submitted)
6563 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006564 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006565 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006566 sqe = io_get_sqe(ctx);
6567 if (unlikely(!sqe)) {
6568 kmem_cache_free(req_cachep, req);
6569 break;
6570 }
Jens Axboed3656342019-12-18 09:50:26 -07006571 /* will complete beyond this point, count as submitted */
6572 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006573 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006574 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006575 }
6576
Pavel Begunkov9466f432020-01-25 22:34:01 +03006577 if (unlikely(submitted != nr)) {
6578 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006579 struct io_uring_task *tctx = current->io_uring;
6580 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006581
Jens Axboed8a6df12020-10-15 16:24:45 -06006582 percpu_ref_put_many(&ctx->refs, unused);
6583 percpu_counter_sub(&tctx->inflight, unused);
6584 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006585 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006586
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006587 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006588 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6589 io_commit_sqring(ctx);
6590
Jens Axboe6c271ce2019-01-10 11:22:30 -07006591 return submitted;
6592}
6593
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006594static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6595{
6596 /* Tell userspace we may need a wakeup call */
6597 spin_lock_irq(&ctx->completion_lock);
6598 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6599 spin_unlock_irq(&ctx->completion_lock);
6600}
6601
6602static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6603{
6604 spin_lock_irq(&ctx->completion_lock);
6605 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6606 spin_unlock_irq(&ctx->completion_lock);
6607}
6608
Xiaoguang Wang08369242020-11-03 14:15:59 +08006609static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006610{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006611 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006612 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006613
Jens Axboec8d1ba52020-09-14 11:07:26 -06006614 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006615 /* if we're handling multiple rings, cap submit size for fairness */
6616 if (cap_entries && to_submit > 8)
6617 to_submit = 8;
6618
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006619 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6620 unsigned nr_events = 0;
6621
Xiaoguang Wang08369242020-11-03 14:15:59 +08006622 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006623 if (!list_empty(&ctx->iopoll_list))
6624 io_do_iopoll(ctx, &nr_events, 0);
6625
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006626 if (to_submit && !ctx->sqo_dead &&
6627 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006628 ret = io_submit_sqes(ctx, to_submit);
6629 mutex_unlock(&ctx->uring_lock);
6630 }
Jens Axboe90554202020-09-03 12:12:41 -06006631
6632 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6633 wake_up(&ctx->sqo_sq_wait);
6634
Xiaoguang Wang08369242020-11-03 14:15:59 +08006635 return ret;
6636}
6637
6638static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6639{
6640 struct io_ring_ctx *ctx;
6641 unsigned sq_thread_idle = 0;
6642
6643 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6644 if (sq_thread_idle < ctx->sq_thread_idle)
6645 sq_thread_idle = ctx->sq_thread_idle;
6646 }
6647
6648 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006649}
6650
Jens Axboe69fb2132020-09-14 11:16:23 -06006651static void io_sqd_init_new(struct io_sq_data *sqd)
6652{
6653 struct io_ring_ctx *ctx;
6654
6655 while (!list_empty(&sqd->ctx_new_list)) {
6656 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006657 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6658 complete(&ctx->sq_thread_comp);
6659 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006660
6661 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006662}
6663
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006664static bool io_sq_thread_should_stop(struct io_sq_data *sqd)
6665{
6666 return test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
6667}
6668
6669static bool io_sq_thread_should_park(struct io_sq_data *sqd)
6670{
6671 return test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
6672}
6673
6674static void io_sq_thread_parkme(struct io_sq_data *sqd)
6675{
6676 for (;;) {
6677 /*
6678 * TASK_PARKED is a special state; we must serialize against
6679 * possible pending wakeups to avoid store-store collisions on
6680 * task->state.
6681 *
6682 * Such a collision might possibly result in the task state
6683 * changin from TASK_PARKED and us failing the
6684 * wait_task_inactive() in kthread_park().
6685 */
6686 set_special_state(TASK_PARKED);
6687 if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state))
6688 break;
6689
6690 /*
6691 * Thread is going to call schedule(), do not preempt it,
6692 * or the caller of kthread_park() may spend more time in
6693 * wait_task_inactive().
6694 */
6695 preempt_disable();
6696 complete(&sqd->completion);
6697 schedule_preempt_disabled();
6698 preempt_enable();
6699 }
6700 __set_current_state(TASK_RUNNING);
6701}
6702
Jens Axboe6c271ce2019-01-10 11:22:30 -07006703static int io_sq_thread(void *data)
6704{
Jens Axboe69fb2132020-09-14 11:16:23 -06006705 struct io_sq_data *sqd = data;
6706 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006707 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006708 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08006709 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006710
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006711 sprintf(buf, "iou-sqp-%d", sqd->task_pid);
6712 set_task_comm(current, buf);
6713 sqd->thread = current;
6714 current->pf_io_worker = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006715
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006716 if (sqd->sq_cpu != -1)
6717 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
6718 else
6719 set_cpus_allowed_ptr(current, cpu_online_mask);
6720 current->flags |= PF_NO_SETAFFINITY;
6721
6722 complete(&sqd->completion);
6723
6724 wait_for_completion(&sqd->startup);
6725
6726 while (!io_sq_thread_should_stop(sqd)) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006727 int ret;
6728 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006729
6730 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006731 * Any changes to the sqd lists are synchronized through the
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006732 * thread parking. This synchronizes the thread vs users,
Jens Axboe69fb2132020-09-14 11:16:23 -06006733 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006734 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006735 if (io_sq_thread_should_park(sqd)) {
6736 io_sq_thread_parkme(sqd);
6737 continue;
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006738 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006739 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006740 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006741 timeout = jiffies + sqd->sq_thread_idle;
6742 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006743 if (fatal_signal_pending(current))
6744 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006745 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006746 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006747 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006748 ret = __io_sq_thread(ctx, cap_entries);
6749 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
6750 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006751 }
6752
Xiaoguang Wang08369242020-11-03 14:15:59 +08006753 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006754 io_run_task_work();
6755 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08006756 if (sqt_spin)
6757 timeout = jiffies + sqd->sq_thread_idle;
6758 continue;
6759 }
6760
Xiaoguang Wang08369242020-11-03 14:15:59 +08006761 needs_sched = true;
6762 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
6763 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6764 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6765 !list_empty_careful(&ctx->iopoll_list)) {
6766 needs_sched = false;
6767 break;
6768 }
6769 if (io_sqring_entries(ctx)) {
6770 needs_sched = false;
6771 break;
6772 }
6773 }
6774
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006775 if (needs_sched && !io_sq_thread_should_park(sqd)) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006776 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6777 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006778
Jens Axboe69fb2132020-09-14 11:16:23 -06006779 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06006780 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6781 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006782 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006783
6784 finish_wait(&sqd->wait, &wait);
6785 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006786 }
6787
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006788 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6789 io_uring_cancel_sqpoll(ctx);
6790
Jens Axboe4c6e2772020-07-01 11:29:10 -06006791 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006792
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006793 /*
6794 * Clear thread under lock so that concurrent parks work correctly
6795 */
6796 complete_all(&sqd->completion);
6797 mutex_lock(&sqd->lock);
6798 sqd->thread = NULL;
6799 mutex_unlock(&sqd->lock);
Jens Axboe06058632019-04-13 09:26:03 -06006800
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006801 complete(&sqd->exited);
6802 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006803}
6804
Jens Axboebda52162019-09-24 13:47:15 -06006805struct io_wait_queue {
6806 struct wait_queue_entry wq;
6807 struct io_ring_ctx *ctx;
6808 unsigned to_wait;
6809 unsigned nr_timeouts;
6810};
6811
Pavel Begunkov6c503152021-01-04 20:36:36 +00006812static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006813{
6814 struct io_ring_ctx *ctx = iowq->ctx;
6815
6816 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006817 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006818 * started waiting. For timeouts, we always want to return to userspace,
6819 * regardless of event count.
6820 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00006821 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006822 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6823}
6824
6825static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6826 int wake_flags, void *key)
6827{
6828 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6829 wq);
6830
Pavel Begunkov6c503152021-01-04 20:36:36 +00006831 /*
6832 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6833 * the task, and the next invocation will do it.
6834 */
6835 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6836 return autoremove_wake_function(curr, mode, wake_flags, key);
6837 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006838}
6839
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006840static int io_run_task_work_sig(void)
6841{
6842 if (io_run_task_work())
6843 return 1;
6844 if (!signal_pending(current))
6845 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06006846 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
6847 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006848 return -EINTR;
6849}
6850
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006851/* when returns >0, the caller should retry */
6852static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
6853 struct io_wait_queue *iowq,
6854 signed long *timeout)
6855{
6856 int ret;
6857
6858 /* make sure we run task_work before checking for signals */
6859 ret = io_run_task_work_sig();
6860 if (ret || io_should_wake(iowq))
6861 return ret;
6862 /* let the caller flush overflows, retry */
6863 if (test_bit(0, &ctx->cq_check_overflow))
6864 return 1;
6865
6866 *timeout = schedule_timeout(*timeout);
6867 return !*timeout ? -ETIME : 1;
6868}
6869
Jens Axboe2b188cc2019-01-07 10:46:33 -07006870/*
6871 * Wait until events become available, if we don't already have some. The
6872 * application must reap them itself, as they reside on the shared cq ring.
6873 */
6874static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08006875 const sigset_t __user *sig, size_t sigsz,
6876 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006877{
Jens Axboebda52162019-09-24 13:47:15 -06006878 struct io_wait_queue iowq = {
6879 .wq = {
6880 .private = current,
6881 .func = io_wake_function,
6882 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6883 },
6884 .ctx = ctx,
6885 .to_wait = min_events,
6886 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006887 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006888 signed long timeout = MAX_SCHEDULE_TIMEOUT;
6889 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006890
Jens Axboeb41e9852020-02-17 09:52:41 -07006891 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006892 io_cqring_overflow_flush(ctx, false, NULL, NULL);
6893 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07006894 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006895 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006896 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006897 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006898
6899 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006900#ifdef CONFIG_COMPAT
6901 if (in_compat_syscall())
6902 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006903 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006904 else
6905#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006906 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006907
Jens Axboe2b188cc2019-01-07 10:46:33 -07006908 if (ret)
6909 return ret;
6910 }
6911
Hao Xuc73ebb62020-11-03 10:54:37 +08006912 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006913 struct timespec64 ts;
6914
Hao Xuc73ebb62020-11-03 10:54:37 +08006915 if (get_timespec64(&ts, uts))
6916 return -EFAULT;
6917 timeout = timespec64_to_jiffies(&ts);
6918 }
6919
Jens Axboebda52162019-09-24 13:47:15 -06006920 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006921 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006922 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006923 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06006924 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6925 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006926 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
6927 finish_wait(&ctx->wait, &iowq.wq);
6928 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06006929
Jens Axboeb7db41c2020-07-04 08:55:50 -06006930 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006931
Hristo Venev75b28af2019-08-26 17:23:46 +00006932 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006933}
6934
Jens Axboe6b063142019-01-10 22:13:58 -07006935static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6936{
6937#if defined(CONFIG_UNIX)
6938 if (ctx->ring_sock) {
6939 struct sock *sock = ctx->ring_sock->sk;
6940 struct sk_buff *skb;
6941
6942 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6943 kfree_skb(skb);
6944 }
6945#else
6946 int i;
6947
Jens Axboe65e19f52019-10-26 07:20:21 -06006948 for (i = 0; i < ctx->nr_user_files; i++) {
6949 struct file *file;
6950
6951 file = io_file_from_index(ctx, i);
6952 if (file)
6953 fput(file);
6954 }
Jens Axboe6b063142019-01-10 22:13:58 -07006955#endif
6956}
6957
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00006958static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006959{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006960 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006961
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006962 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006963 complete(&data->done);
6964}
6965
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006966static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
Pavel Begunkov1642b442020-12-30 21:34:14 +00006967{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006968 spin_lock_bh(&ctx->rsrc_ref_lock);
Pavel Begunkov1642b442020-12-30 21:34:14 +00006969}
6970
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006971static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
Jens Axboe6b063142019-01-10 22:13:58 -07006972{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006973 spin_unlock_bh(&ctx->rsrc_ref_lock);
6974}
6975
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00006976static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
6977 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006978 struct fixed_rsrc_ref_node *ref_node)
Jens Axboe6b063142019-01-10 22:13:58 -07006979{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006980 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006981 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00006982 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006983 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006984 percpu_ref_get(&rsrc_data->refs);
Jens Axboe6b063142019-01-10 22:13:58 -07006985}
6986
Hao Xu8bad28d2021-02-19 17:19:36 +08006987static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data)
Jens Axboe6b063142019-01-10 22:13:58 -07006988{
Hao Xu8bad28d2021-02-19 17:19:36 +08006989 struct fixed_rsrc_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006990
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006991 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00006992 ref_node = data->node;
Pavel Begunkove6cb0072021-02-20 18:03:47 +00006993 data->node = NULL;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00006994 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006995 if (ref_node)
6996 percpu_ref_kill(&ref_node->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08006997}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006998
Hao Xu8bad28d2021-02-19 17:19:36 +08006999static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7000 struct io_ring_ctx *ctx,
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007001 void (*rsrc_put)(struct io_ring_ctx *ctx,
7002 struct io_rsrc_put *prsrc))
Hao Xu8bad28d2021-02-19 17:19:36 +08007003{
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007004 struct fixed_rsrc_ref_node *backup_node;
Hao Xu8bad28d2021-02-19 17:19:36 +08007005 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007006
Hao Xu8bad28d2021-02-19 17:19:36 +08007007 if (data->quiesce)
7008 return -ENXIO;
7009
7010 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007011 do {
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007012 ret = -ENOMEM;
7013 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7014 if (!backup_node)
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007015 break;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007016 backup_node->rsrc_data = data;
7017 backup_node->rsrc_put = rsrc_put;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007018
Hao Xu8bad28d2021-02-19 17:19:36 +08007019 io_sqe_rsrc_kill_node(ctx, data);
7020 percpu_ref_kill(&data->refs);
7021 flush_delayed_work(&ctx->rsrc_put_work);
7022
Jens Axboe6b063142019-01-10 22:13:58 -07007023 ret = wait_for_completion_interruptible(&data->done);
Pavel Begunkov88f171a2021-02-20 18:03:50 +00007024 if (!ret || !io_refs_resurrect(&data->refs, &data->done))
Jens Axboe6b063142019-01-10 22:13:58 -07007025 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007026
Hao Xu8bad28d2021-02-19 17:19:36 +08007027 io_sqe_rsrc_set_node(ctx, data, backup_node);
7028 backup_node = NULL;
Hao Xu8bad28d2021-02-19 17:19:36 +08007029 mutex_unlock(&ctx->uring_lock);
7030 ret = io_run_task_work_sig();
7031 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007032 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007033 data->quiesce = false;
7034
7035 if (backup_node)
7036 destroy_fixed_rsrc_ref_node(backup_node);
7037 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007038}
7039
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007040static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7041{
7042 struct fixed_rsrc_data *data;
7043
7044 data = kzalloc(sizeof(*data), GFP_KERNEL);
7045 if (!data)
7046 return NULL;
7047
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007048 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007049 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7050 kfree(data);
7051 return NULL;
7052 }
7053 data->ctx = ctx;
7054 init_completion(&data->done);
7055 return data;
7056}
7057
7058static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7059{
7060 percpu_ref_exit(&data->refs);
7061 kfree(data->table);
7062 kfree(data);
7063}
7064
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007065static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7066{
7067 struct fixed_rsrc_data *data = ctx->file_data;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007068 unsigned nr_tables, i;
7069 int ret;
7070
Hao Xu8bad28d2021-02-19 17:19:36 +08007071 /*
7072 * percpu_ref_is_dying() is to stop parallel files unregister
7073 * Since we possibly drop uring lock later in this function to
7074 * run task work.
7075 */
7076 if (!data || percpu_ref_is_dying(&data->refs))
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007077 return -ENXIO;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007078 ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put);
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007079 if (ret)
7080 return ret;
7081
Jens Axboe6b063142019-01-10 22:13:58 -07007082 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007083 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7084 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007085 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007086 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007087 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007088 ctx->nr_user_files = 0;
7089 return 0;
7090}
7091
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007092static void io_sq_thread_unpark(struct io_sq_data *sqd)
7093 __releases(&sqd->lock)
7094{
7095 if (!sqd->thread)
7096 return;
7097 if (sqd->thread == current)
7098 return;
7099 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7100 wake_up_state(sqd->thread, TASK_PARKED);
7101 mutex_unlock(&sqd->lock);
7102}
7103
7104static bool io_sq_thread_park(struct io_sq_data *sqd)
7105 __acquires(&sqd->lock)
7106{
7107 if (sqd->thread == current)
7108 return true;
7109 mutex_lock(&sqd->lock);
7110 if (!sqd->thread) {
7111 mutex_unlock(&sqd->lock);
7112 return false;
7113 }
7114 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7115 wake_up_process(sqd->thread);
7116 wait_for_completion(&sqd->completion);
7117 return true;
7118}
7119
7120static void io_sq_thread_stop(struct io_sq_data *sqd)
7121{
7122 if (!sqd->thread)
7123 return;
7124
7125 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7126 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state));
7127 wake_up_process(sqd->thread);
7128 wait_for_completion(&sqd->exited);
7129}
7130
Jens Axboe534ca6d2020-09-02 13:52:19 -06007131static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007132{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007133 if (refcount_dec_and_test(&sqd->refs)) {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007134 io_sq_thread_stop(sqd);
7135 kfree(sqd);
7136 }
7137}
7138
7139static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7140{
7141 struct io_sq_data *sqd = ctx->sq_data;
7142
7143 if (sqd) {
Jens Axboeeb858902021-02-25 10:13:29 -07007144 complete(&sqd->startup);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007145 if (sqd->thread) {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007146 wait_for_completion(&ctx->sq_thread_comp);
7147 io_sq_thread_park(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007148 }
7149
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007150 mutex_lock(&sqd->ctx_lock);
7151 list_del(&ctx->sqd_list);
7152 io_sqd_update_thread_idle(sqd);
7153 mutex_unlock(&sqd->ctx_lock);
7154
7155 if (sqd->thread)
7156 io_sq_thread_unpark(sqd);
7157
7158 io_put_sq_data(sqd);
7159 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007160 }
7161}
7162
Jens Axboeaa061652020-09-02 14:50:27 -06007163static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7164{
7165 struct io_ring_ctx *ctx_attach;
7166 struct io_sq_data *sqd;
7167 struct fd f;
7168
7169 f = fdget(p->wq_fd);
7170 if (!f.file)
7171 return ERR_PTR(-ENXIO);
7172 if (f.file->f_op != &io_uring_fops) {
7173 fdput(f);
7174 return ERR_PTR(-EINVAL);
7175 }
7176
7177 ctx_attach = f.file->private_data;
7178 sqd = ctx_attach->sq_data;
7179 if (!sqd) {
7180 fdput(f);
7181 return ERR_PTR(-EINVAL);
7182 }
7183
7184 refcount_inc(&sqd->refs);
7185 fdput(f);
7186 return sqd;
7187}
7188
Jens Axboe534ca6d2020-09-02 13:52:19 -06007189static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7190{
7191 struct io_sq_data *sqd;
7192
Jens Axboeaa061652020-09-02 14:50:27 -06007193 if (p->flags & IORING_SETUP_ATTACH_WQ)
7194 return io_attach_sq_data(p);
7195
Jens Axboe534ca6d2020-09-02 13:52:19 -06007196 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7197 if (!sqd)
7198 return ERR_PTR(-ENOMEM);
7199
7200 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007201 INIT_LIST_HEAD(&sqd->ctx_list);
7202 INIT_LIST_HEAD(&sqd->ctx_new_list);
7203 mutex_init(&sqd->ctx_lock);
7204 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007205 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007206 init_completion(&sqd->startup);
7207 init_completion(&sqd->completion);
7208 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007209 return sqd;
7210}
7211
Jens Axboe6b063142019-01-10 22:13:58 -07007212#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007213/*
7214 * Ensure the UNIX gc is aware of our file set, so we are certain that
7215 * the io_uring can be safely unregistered on process exit, even if we have
7216 * loops in the file referencing.
7217 */
7218static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7219{
7220 struct sock *sk = ctx->ring_sock->sk;
7221 struct scm_fp_list *fpl;
7222 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007223 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007224
Jens Axboe6b063142019-01-10 22:13:58 -07007225 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7226 if (!fpl)
7227 return -ENOMEM;
7228
7229 skb = alloc_skb(0, GFP_KERNEL);
7230 if (!skb) {
7231 kfree(fpl);
7232 return -ENOMEM;
7233 }
7234
7235 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007236
Jens Axboe08a45172019-10-03 08:11:03 -06007237 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007238 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007239 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007240 struct file *file = io_file_from_index(ctx, i + offset);
7241
7242 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007243 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007244 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007245 unix_inflight(fpl->user, fpl->fp[nr_files]);
7246 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007247 }
7248
Jens Axboe08a45172019-10-03 08:11:03 -06007249 if (nr_files) {
7250 fpl->max = SCM_MAX_FD;
7251 fpl->count = nr_files;
7252 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007253 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007254 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7255 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007256
Jens Axboe08a45172019-10-03 08:11:03 -06007257 for (i = 0; i < nr_files; i++)
7258 fput(fpl->fp[i]);
7259 } else {
7260 kfree_skb(skb);
7261 kfree(fpl);
7262 }
Jens Axboe6b063142019-01-10 22:13:58 -07007263
7264 return 0;
7265}
7266
7267/*
7268 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7269 * causes regular reference counting to break down. We rely on the UNIX
7270 * garbage collection to take care of this problem for us.
7271 */
7272static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7273{
7274 unsigned left, total;
7275 int ret = 0;
7276
7277 total = 0;
7278 left = ctx->nr_user_files;
7279 while (left) {
7280 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007281
7282 ret = __io_sqe_files_scm(ctx, this_files, total);
7283 if (ret)
7284 break;
7285 left -= this_files;
7286 total += this_files;
7287 }
7288
7289 if (!ret)
7290 return 0;
7291
7292 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007293 struct file *file = io_file_from_index(ctx, total);
7294
7295 if (file)
7296 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007297 total++;
7298 }
7299
7300 return ret;
7301}
7302#else
7303static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7304{
7305 return 0;
7306}
7307#endif
7308
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007309static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007310 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007311{
7312 int i;
7313
7314 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007315 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007316 unsigned this_files;
7317
7318 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7319 table->files = kcalloc(this_files, sizeof(struct file *),
7320 GFP_KERNEL);
7321 if (!table->files)
7322 break;
7323 nr_files -= this_files;
7324 }
7325
7326 if (i == nr_tables)
7327 return 0;
7328
7329 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007330 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007331 kfree(table->files);
7332 }
7333 return 1;
7334}
7335
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007336static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007337{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007338 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007339#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007340 struct sock *sock = ctx->ring_sock->sk;
7341 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7342 struct sk_buff *skb;
7343 int i;
7344
7345 __skb_queue_head_init(&list);
7346
7347 /*
7348 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7349 * remove this entry and rearrange the file array.
7350 */
7351 skb = skb_dequeue(head);
7352 while (skb) {
7353 struct scm_fp_list *fp;
7354
7355 fp = UNIXCB(skb).fp;
7356 for (i = 0; i < fp->count; i++) {
7357 int left;
7358
7359 if (fp->fp[i] != file)
7360 continue;
7361
7362 unix_notinflight(fp->user, fp->fp[i]);
7363 left = fp->count - 1 - i;
7364 if (left) {
7365 memmove(&fp->fp[i], &fp->fp[i + 1],
7366 left * sizeof(struct file *));
7367 }
7368 fp->count--;
7369 if (!fp->count) {
7370 kfree_skb(skb);
7371 skb = NULL;
7372 } else {
7373 __skb_queue_tail(&list, skb);
7374 }
7375 fput(file);
7376 file = NULL;
7377 break;
7378 }
7379
7380 if (!file)
7381 break;
7382
7383 __skb_queue_tail(&list, skb);
7384
7385 skb = skb_dequeue(head);
7386 }
7387
7388 if (skb_peek(&list)) {
7389 spin_lock_irq(&head->lock);
7390 while ((skb = __skb_dequeue(&list)) != NULL)
7391 __skb_queue_tail(head, skb);
7392 spin_unlock_irq(&head->lock);
7393 }
7394#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007395 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007396#endif
7397}
7398
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007399static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007400{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007401 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7402 struct io_ring_ctx *ctx = rsrc_data->ctx;
7403 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007404
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007405 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7406 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007407 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007408 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007409 }
7410
Xiaoguang Wang05589552020-03-31 14:05:18 +08007411 percpu_ref_exit(&ref_node->refs);
7412 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007413 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007414}
7415
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007416static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007417{
7418 struct io_ring_ctx *ctx;
7419 struct llist_node *node;
7420
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007421 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7422 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007423
7424 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007425 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007426 struct llist_node *next = node->next;
7427
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007428 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7429 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007430 node = next;
7431 }
7432}
7433
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007434static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7435 unsigned i)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007436{
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007437 struct fixed_rsrc_table *table;
7438
7439 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7440 return &table->files[i & IORING_FILE_TABLE_MASK];
7441}
7442
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007443static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007444{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007445 struct fixed_rsrc_ref_node *ref_node;
7446 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007447 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007448 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007449 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007450
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007451 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7452 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007453 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007454
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007455 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007456 ref_node->done = true;
7457
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007458 while (!list_empty(&ctx->rsrc_ref_list)) {
7459 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007460 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007461 /* recycle ref nodes in order */
7462 if (!ref_node->done)
7463 break;
7464 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007465 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007466 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007467 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007468
7469 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007470 delay = 0;
7471
Jens Axboe4a38aed22020-05-14 17:21:15 -06007472 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007473 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007474 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007475 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007476}
7477
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007478static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007479 struct io_ring_ctx *ctx)
7480{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007481 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007482
7483 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7484 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007485 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007486
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007487 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007488 0, GFP_KERNEL)) {
7489 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007490 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007491 }
7492 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007493 INIT_LIST_HEAD(&ref_node->rsrc_list);
Pavel Begunkove2978222020-11-18 14:56:26 +00007494 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007495 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007496}
7497
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007498static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7499 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007500{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007501 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007502 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007503}
7504
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007505static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007506{
7507 percpu_ref_exit(&ref_node->refs);
7508 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007509}
7510
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007511
Jens Axboe05f3fb32019-12-09 11:22:50 -07007512static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7513 unsigned nr_args)
7514{
7515 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007516 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007517 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007518 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007519 struct fixed_rsrc_ref_node *ref_node;
7520 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007521
7522 if (ctx->file_data)
7523 return -EBUSY;
7524 if (!nr_args)
7525 return -EINVAL;
7526 if (nr_args > IORING_MAX_FIXED_FILES)
7527 return -EMFILE;
7528
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007529 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007530 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007531 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007532 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007533
7534 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007535 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007536 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007537 if (!file_data->table)
7538 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007539
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007540 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Jens Axboe05f3fb32019-12-09 11:22:50 -07007541 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007542
Jens Axboe05f3fb32019-12-09 11:22:50 -07007543 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007544 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7545 ret = -EFAULT;
7546 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007547 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007548 /* allow sparse sets */
7549 if (fd == -1)
7550 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007551
Jens Axboe05f3fb32019-12-09 11:22:50 -07007552 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007553 ret = -EBADF;
7554 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007555 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007556
7557 /*
7558 * Don't allow io_uring instances to be registered. If UNIX
7559 * isn't enabled, then this causes a reference cycle and this
7560 * instance can never get freed. If UNIX is enabled we'll
7561 * handle it just fine, but there's still no point in allowing
7562 * a ring fd as it doesn't support regular read/write anyway.
7563 */
7564 if (file->f_op == &io_uring_fops) {
7565 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007566 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007567 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007568 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007569 }
7570
Jens Axboe05f3fb32019-12-09 11:22:50 -07007571 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007572 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007573 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007574 return ret;
7575 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007576
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007577 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007578 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007579 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007580 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007581 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007582 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007583
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007584 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007585 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007586out_fput:
7587 for (i = 0; i < ctx->nr_user_files; i++) {
7588 file = io_file_from_index(ctx, i);
7589 if (file)
7590 fput(file);
7591 }
7592 for (i = 0; i < nr_tables; i++)
7593 kfree(file_data->table[i].files);
7594 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007595out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007596 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007597 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007598 return ret;
7599}
7600
Jens Axboec3a31e62019-10-03 13:59:56 -06007601static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7602 int index)
7603{
7604#if defined(CONFIG_UNIX)
7605 struct sock *sock = ctx->ring_sock->sk;
7606 struct sk_buff_head *head = &sock->sk_receive_queue;
7607 struct sk_buff *skb;
7608
7609 /*
7610 * See if we can merge this file into an existing skb SCM_RIGHTS
7611 * file set. If there's no room, fall back to allocating a new skb
7612 * and filling it in.
7613 */
7614 spin_lock_irq(&head->lock);
7615 skb = skb_peek(head);
7616 if (skb) {
7617 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7618
7619 if (fpl->count < SCM_MAX_FD) {
7620 __skb_unlink(skb, head);
7621 spin_unlock_irq(&head->lock);
7622 fpl->fp[fpl->count] = get_file(file);
7623 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7624 fpl->count++;
7625 spin_lock_irq(&head->lock);
7626 __skb_queue_head(head, skb);
7627 } else {
7628 skb = NULL;
7629 }
7630 }
7631 spin_unlock_irq(&head->lock);
7632
7633 if (skb) {
7634 fput(file);
7635 return 0;
7636 }
7637
7638 return __io_sqe_files_scm(ctx, 1, index);
7639#else
7640 return 0;
7641#endif
7642}
7643
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007644static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007645{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007646 struct io_rsrc_put *prsrc;
7647 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007648
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007649 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7650 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007651 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007652
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007653 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007654 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007655
Hillf Dantona5318d32020-03-23 17:47:15 +08007656 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007657}
7658
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007659static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7660 struct file *file)
7661{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007662 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007663}
7664
Jens Axboe05f3fb32019-12-09 11:22:50 -07007665static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007666 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007667 unsigned nr_args)
7668{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007669 struct fixed_rsrc_data *data = ctx->file_data;
7670 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007671 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007672 __s32 __user *fds;
7673 int fd, i, err;
7674 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007675 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007676
Jens Axboe05f3fb32019-12-09 11:22:50 -07007677 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007678 return -EOVERFLOW;
7679 if (done > ctx->nr_user_files)
7680 return -EINVAL;
7681
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007682 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007683 if (!ref_node)
7684 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007685 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007686
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007687 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007688 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007689 err = 0;
7690 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7691 err = -EFAULT;
7692 break;
7693 }
noah4e0377a2021-01-26 15:23:28 -05007694 if (fd == IORING_REGISTER_FILES_SKIP)
7695 continue;
7696
Pavel Begunkov67973b92021-01-26 13:51:09 +00007697 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007698 file_slot = io_fixed_file_slot(ctx->file_data, i);
7699
7700 if (*file_slot) {
7701 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007702 if (err)
7703 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007704 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007705 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007706 }
7707 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007708 file = fget(fd);
7709 if (!file) {
7710 err = -EBADF;
7711 break;
7712 }
7713 /*
7714 * Don't allow io_uring instances to be registered. If
7715 * UNIX isn't enabled, then this causes a reference
7716 * cycle and this instance can never get freed. If UNIX
7717 * is enabled we'll handle it just fine, but there's
7718 * still no point in allowing a ring fd as it doesn't
7719 * support regular read/write anyway.
7720 */
7721 if (file->f_op == &io_uring_fops) {
7722 fput(file);
7723 err = -EBADF;
7724 break;
7725 }
Jens Axboee68a3ff2021-02-11 07:45:08 -07007726 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007727 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007728 if (err) {
Jens Axboee68a3ff2021-02-11 07:45:08 -07007729 *file_slot = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007730 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007731 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007732 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007733 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007734 }
7735
Xiaoguang Wang05589552020-03-31 14:05:18 +08007736 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007737 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007738 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007739 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007740 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007741
7742 return done ? done : err;
7743}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007744
Jens Axboe05f3fb32019-12-09 11:22:50 -07007745static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7746 unsigned nr_args)
7747{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007748 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007749
7750 if (!ctx->file_data)
7751 return -ENXIO;
7752 if (!nr_args)
7753 return -EINVAL;
7754 if (copy_from_user(&up, arg, sizeof(up)))
7755 return -EFAULT;
7756 if (up.resv)
7757 return -EINVAL;
7758
7759 return __io_sqe_files_update(ctx, &up, nr_args);
7760}
Jens Axboec3a31e62019-10-03 13:59:56 -06007761
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007762static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007763{
7764 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7765
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007766 req = io_put_req_find_next(req);
7767 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07007768}
7769
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007770static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx)
Pavel Begunkov24369c22020-01-28 03:15:48 +03007771{
Jens Axboee9418942021-02-19 12:33:30 -07007772 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007773 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007774 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007775
Jens Axboee9418942021-02-19 12:33:30 -07007776 hash = ctx->hash_map;
7777 if (!hash) {
7778 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
7779 if (!hash)
7780 return ERR_PTR(-ENOMEM);
7781 refcount_set(&hash->refs, 1);
7782 init_waitqueue_head(&hash->wait);
7783 ctx->hash_map = hash;
7784 }
7785
7786 data.hash = hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007787 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007788 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007789
Jens Axboed25e3a32021-02-16 11:41:41 -07007790 /* Do QD, or 4 * CPUS, whatever is smallest */
7791 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03007792
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007793 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03007794}
7795
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007796static int io_uring_alloc_task_context(struct task_struct *task,
7797 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06007798{
7799 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007800 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007801
7802 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7803 if (unlikely(!tctx))
7804 return -ENOMEM;
7805
Jens Axboed8a6df12020-10-15 16:24:45 -06007806 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7807 if (unlikely(ret)) {
7808 kfree(tctx);
7809 return ret;
7810 }
7811
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007812 tctx->io_wq = io_init_wq_offload(ctx);
7813 if (IS_ERR(tctx->io_wq)) {
7814 ret = PTR_ERR(tctx->io_wq);
7815 percpu_counter_destroy(&tctx->inflight);
7816 kfree(tctx);
7817 return ret;
7818 }
7819
Jens Axboe0f212202020-09-13 13:09:39 -06007820 xa_init(&tctx->xa);
7821 init_waitqueue_head(&tctx->wait);
7822 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007823 atomic_set(&tctx->in_idle, 0);
7824 tctx->sqpoll = false;
Jens Axboe0f212202020-09-13 13:09:39 -06007825 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00007826 spin_lock_init(&tctx->task_lock);
7827 INIT_WQ_LIST(&tctx->task_list);
7828 tctx->task_state = 0;
7829 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06007830 return 0;
7831}
7832
7833void __io_uring_free(struct task_struct *tsk)
7834{
7835 struct io_uring_task *tctx = tsk->io_uring;
7836
7837 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboed8a6df12020-10-15 16:24:45 -06007838 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007839 kfree(tctx);
7840 tsk->io_uring = NULL;
7841}
7842
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007843static int io_sq_offload_create(struct io_ring_ctx *ctx,
7844 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007845{
7846 int ret;
7847
Jens Axboed25e3a32021-02-16 11:41:41 -07007848 /* Retain compatibility with failing for an invalid attach attempt */
7849 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
7850 IORING_SETUP_ATTACH_WQ) {
7851 struct fd f;
7852
7853 f = fdget(p->wq_fd);
7854 if (!f.file)
7855 return -ENXIO;
7856 if (f.file->f_op != &io_uring_fops) {
7857 fdput(f);
7858 return -EINVAL;
7859 }
7860 fdput(f);
7861 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007862 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007863 struct io_sq_data *sqd;
7864
Jens Axboe3ec482d2019-04-08 10:51:01 -06007865 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06007866 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06007867 goto err;
7868
Jens Axboe534ca6d2020-09-02 13:52:19 -06007869 sqd = io_get_sq_data(p);
7870 if (IS_ERR(sqd)) {
7871 ret = PTR_ERR(sqd);
7872 goto err;
7873 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007874
Jens Axboe534ca6d2020-09-02 13:52:19 -06007875 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007876 io_sq_thread_park(sqd);
7877 mutex_lock(&sqd->ctx_lock);
7878 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7879 mutex_unlock(&sqd->ctx_lock);
7880 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007881
Jens Axboe917257d2019-04-13 09:28:55 -06007882 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7883 if (!ctx->sq_thread_idle)
7884 ctx->sq_thread_idle = HZ;
7885
Jens Axboeaa061652020-09-02 14:50:27 -06007886 if (sqd->thread)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007887 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06007888
Jens Axboe6c271ce2019-01-10 11:22:30 -07007889 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007890 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007891
Jens Axboe917257d2019-04-13 09:28:55 -06007892 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007893 if (cpu >= nr_cpu_ids)
7894 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007895 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007896 goto err;
7897
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007898 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007899 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007900 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007901 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007902
7903 sqd->task_pid = current->pid;
7904 current->flags |= PF_IO_WORKER;
7905 ret = io_wq_fork_thread(io_sq_thread, sqd);
7906 current->flags &= ~PF_IO_WORKER;
7907 if (ret < 0) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007908 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007909 goto err;
7910 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007911 wait_for_completion(&sqd->completion);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007912 ret = io_uring_alloc_task_context(sqd->thread, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06007913 if (ret)
7914 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007915 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7916 /* Can't have SQ_AFF without SQPOLL */
7917 ret = -EINVAL;
7918 goto err;
7919 }
7920
Jens Axboe2b188cc2019-01-07 10:46:33 -07007921 return 0;
7922err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007923 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007924 return ret;
7925}
7926
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007927static void io_sq_offload_start(struct io_ring_ctx *ctx)
7928{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007929 struct io_sq_data *sqd = ctx->sq_data;
7930
Jens Axboeeb858902021-02-25 10:13:29 -07007931 if (ctx->flags & IORING_SETUP_SQPOLL)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007932 complete(&sqd->startup);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007933}
7934
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007935static inline void __io_unaccount_mem(struct user_struct *user,
7936 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007937{
7938 atomic_long_sub(nr_pages, &user->locked_vm);
7939}
7940
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007941static inline int __io_account_mem(struct user_struct *user,
7942 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007943{
7944 unsigned long page_limit, cur_pages, new_pages;
7945
7946 /* Don't allow more pages than we can safely lock */
7947 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7948
7949 do {
7950 cur_pages = atomic_long_read(&user->locked_vm);
7951 new_pages = cur_pages + nr_pages;
7952 if (new_pages > page_limit)
7953 return -ENOMEM;
7954 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7955 new_pages) != cur_pages);
7956
7957 return 0;
7958}
7959
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007960static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007961{
Jens Axboe62e398b2021-02-21 16:19:37 -07007962 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007963 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007964
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007965 if (ctx->mm_account)
7966 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007967}
7968
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007969static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007970{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007971 int ret;
7972
Jens Axboe62e398b2021-02-21 16:19:37 -07007973 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007974 ret = __io_account_mem(ctx->user, nr_pages);
7975 if (ret)
7976 return ret;
7977 }
7978
Jens Axboe26bfa89e2021-02-09 20:14:12 -07007979 if (ctx->mm_account)
7980 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007981
7982 return 0;
7983}
7984
Jens Axboe2b188cc2019-01-07 10:46:33 -07007985static void io_mem_free(void *ptr)
7986{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007987 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007988
Mark Rutland52e04ef2019-04-30 17:30:21 +01007989 if (!ptr)
7990 return;
7991
7992 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007993 if (put_page_testzero(page))
7994 free_compound_page(page);
7995}
7996
7997static void *io_mem_alloc(size_t size)
7998{
7999 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008000 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008001
8002 return (void *) __get_free_pages(gfp_flags, get_order(size));
8003}
8004
Hristo Venev75b28af2019-08-26 17:23:46 +00008005static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8006 size_t *sq_offset)
8007{
8008 struct io_rings *rings;
8009 size_t off, sq_array_size;
8010
8011 off = struct_size(rings, cqes, cq_entries);
8012 if (off == SIZE_MAX)
8013 return SIZE_MAX;
8014
8015#ifdef CONFIG_SMP
8016 off = ALIGN(off, SMP_CACHE_BYTES);
8017 if (off == 0)
8018 return SIZE_MAX;
8019#endif
8020
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008021 if (sq_offset)
8022 *sq_offset = off;
8023
Hristo Venev75b28af2019-08-26 17:23:46 +00008024 sq_array_size = array_size(sizeof(u32), sq_entries);
8025 if (sq_array_size == SIZE_MAX)
8026 return SIZE_MAX;
8027
8028 if (check_add_overflow(off, sq_array_size, &off))
8029 return SIZE_MAX;
8030
Hristo Venev75b28af2019-08-26 17:23:46 +00008031 return off;
8032}
8033
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008034static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008035{
8036 int i, j;
8037
8038 if (!ctx->user_bufs)
8039 return -ENXIO;
8040
8041 for (i = 0; i < ctx->nr_user_bufs; i++) {
8042 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8043
8044 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008045 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008046
Jens Axboede293932020-09-17 16:19:16 -06008047 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008048 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008049 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008050 imu->nr_bvecs = 0;
8051 }
8052
8053 kfree(ctx->user_bufs);
8054 ctx->user_bufs = NULL;
8055 ctx->nr_user_bufs = 0;
8056 return 0;
8057}
8058
8059static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8060 void __user *arg, unsigned index)
8061{
8062 struct iovec __user *src;
8063
8064#ifdef CONFIG_COMPAT
8065 if (ctx->compat) {
8066 struct compat_iovec __user *ciovs;
8067 struct compat_iovec ciov;
8068
8069 ciovs = (struct compat_iovec __user *) arg;
8070 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8071 return -EFAULT;
8072
Jens Axboed55e5f52019-12-11 16:12:15 -07008073 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008074 dst->iov_len = ciov.iov_len;
8075 return 0;
8076 }
8077#endif
8078 src = (struct iovec __user *) arg;
8079 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8080 return -EFAULT;
8081 return 0;
8082}
8083
Jens Axboede293932020-09-17 16:19:16 -06008084/*
8085 * Not super efficient, but this is just a registration time. And we do cache
8086 * the last compound head, so generally we'll only do a full search if we don't
8087 * match that one.
8088 *
8089 * We check if the given compound head page has already been accounted, to
8090 * avoid double accounting it. This allows us to account the full size of the
8091 * page, not just the constituent pages of a huge page.
8092 */
8093static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8094 int nr_pages, struct page *hpage)
8095{
8096 int i, j;
8097
8098 /* check current page array */
8099 for (i = 0; i < nr_pages; i++) {
8100 if (!PageCompound(pages[i]))
8101 continue;
8102 if (compound_head(pages[i]) == hpage)
8103 return true;
8104 }
8105
8106 /* check previously registered pages */
8107 for (i = 0; i < ctx->nr_user_bufs; i++) {
8108 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8109
8110 for (j = 0; j < imu->nr_bvecs; j++) {
8111 if (!PageCompound(imu->bvec[j].bv_page))
8112 continue;
8113 if (compound_head(imu->bvec[j].bv_page) == hpage)
8114 return true;
8115 }
8116 }
8117
8118 return false;
8119}
8120
8121static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8122 int nr_pages, struct io_mapped_ubuf *imu,
8123 struct page **last_hpage)
8124{
8125 int i, ret;
8126
8127 for (i = 0; i < nr_pages; i++) {
8128 if (!PageCompound(pages[i])) {
8129 imu->acct_pages++;
8130 } else {
8131 struct page *hpage;
8132
8133 hpage = compound_head(pages[i]);
8134 if (hpage == *last_hpage)
8135 continue;
8136 *last_hpage = hpage;
8137 if (headpage_already_acct(ctx, pages, i, hpage))
8138 continue;
8139 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8140 }
8141 }
8142
8143 if (!imu->acct_pages)
8144 return 0;
8145
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008146 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008147 if (ret)
8148 imu->acct_pages = 0;
8149 return ret;
8150}
8151
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008152static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8153 struct io_mapped_ubuf *imu,
8154 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008155{
8156 struct vm_area_struct **vmas = NULL;
8157 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008158 unsigned long off, start, end, ubuf;
8159 size_t size;
8160 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008161
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008162 ubuf = (unsigned long) iov->iov_base;
8163 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8164 start = ubuf >> PAGE_SHIFT;
8165 nr_pages = end - start;
8166
8167 ret = -ENOMEM;
8168
8169 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8170 if (!pages)
8171 goto done;
8172
8173 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8174 GFP_KERNEL);
8175 if (!vmas)
8176 goto done;
8177
8178 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8179 GFP_KERNEL);
8180 if (!imu->bvec)
8181 goto done;
8182
8183 ret = 0;
8184 mmap_read_lock(current->mm);
8185 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8186 pages, vmas);
8187 if (pret == nr_pages) {
8188 /* don't support file backed memory */
8189 for (i = 0; i < nr_pages; i++) {
8190 struct vm_area_struct *vma = vmas[i];
8191
8192 if (vma->vm_file &&
8193 !is_file_hugepages(vma->vm_file)) {
8194 ret = -EOPNOTSUPP;
8195 break;
8196 }
8197 }
8198 } else {
8199 ret = pret < 0 ? pret : -EFAULT;
8200 }
8201 mmap_read_unlock(current->mm);
8202 if (ret) {
8203 /*
8204 * if we did partial map, or found file backed vmas,
8205 * release any pages we did get
8206 */
8207 if (pret > 0)
8208 unpin_user_pages(pages, pret);
8209 kvfree(imu->bvec);
8210 goto done;
8211 }
8212
8213 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8214 if (ret) {
8215 unpin_user_pages(pages, pret);
8216 kvfree(imu->bvec);
8217 goto done;
8218 }
8219
8220 off = ubuf & ~PAGE_MASK;
8221 size = iov->iov_len;
8222 for (i = 0; i < nr_pages; i++) {
8223 size_t vec_len;
8224
8225 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8226 imu->bvec[i].bv_page = pages[i];
8227 imu->bvec[i].bv_len = vec_len;
8228 imu->bvec[i].bv_offset = off;
8229 off = 0;
8230 size -= vec_len;
8231 }
8232 /* store original address for later verification */
8233 imu->ubuf = ubuf;
8234 imu->len = iov->iov_len;
8235 imu->nr_bvecs = nr_pages;
8236 ret = 0;
8237done:
8238 kvfree(pages);
8239 kvfree(vmas);
8240 return ret;
8241}
8242
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008243static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008244{
Jens Axboeedafcce2019-01-09 09:16:05 -07008245 if (ctx->user_bufs)
8246 return -EBUSY;
8247 if (!nr_args || nr_args > UIO_MAXIOV)
8248 return -EINVAL;
8249
8250 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8251 GFP_KERNEL);
8252 if (!ctx->user_bufs)
8253 return -ENOMEM;
8254
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008255 return 0;
8256}
8257
8258static int io_buffer_validate(struct iovec *iov)
8259{
8260 /*
8261 * Don't impose further limits on the size and buffer
8262 * constraints here, we'll -EINVAL later when IO is
8263 * submitted if they are wrong.
8264 */
8265 if (!iov->iov_base || !iov->iov_len)
8266 return -EFAULT;
8267
8268 /* arbitrary limit, but we need something */
8269 if (iov->iov_len > SZ_1G)
8270 return -EFAULT;
8271
8272 return 0;
8273}
8274
8275static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8276 unsigned int nr_args)
8277{
8278 int i, ret;
8279 struct iovec iov;
8280 struct page *last_hpage = NULL;
8281
8282 ret = io_buffers_map_alloc(ctx, nr_args);
8283 if (ret)
8284 return ret;
8285
Jens Axboeedafcce2019-01-09 09:16:05 -07008286 for (i = 0; i < nr_args; i++) {
8287 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008288
8289 ret = io_copy_iov(ctx, &iov, arg, i);
8290 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008291 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008292
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008293 ret = io_buffer_validate(&iov);
8294 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008295 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008296
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008297 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8298 if (ret)
8299 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008300
8301 ctx->nr_user_bufs++;
8302 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008303
8304 if (ret)
8305 io_sqe_buffers_unregister(ctx);
8306
Jens Axboeedafcce2019-01-09 09:16:05 -07008307 return ret;
8308}
8309
Jens Axboe9b402842019-04-11 11:45:41 -06008310static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8311{
8312 __s32 __user *fds = arg;
8313 int fd;
8314
8315 if (ctx->cq_ev_fd)
8316 return -EBUSY;
8317
8318 if (copy_from_user(&fd, fds, sizeof(*fds)))
8319 return -EFAULT;
8320
8321 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8322 if (IS_ERR(ctx->cq_ev_fd)) {
8323 int ret = PTR_ERR(ctx->cq_ev_fd);
8324 ctx->cq_ev_fd = NULL;
8325 return ret;
8326 }
8327
8328 return 0;
8329}
8330
8331static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8332{
8333 if (ctx->cq_ev_fd) {
8334 eventfd_ctx_put(ctx->cq_ev_fd);
8335 ctx->cq_ev_fd = NULL;
8336 return 0;
8337 }
8338
8339 return -ENXIO;
8340}
8341
Jens Axboe5a2e7452020-02-23 16:23:11 -07008342static int __io_destroy_buffers(int id, void *p, void *data)
8343{
8344 struct io_ring_ctx *ctx = data;
8345 struct io_buffer *buf = p;
8346
Jens Axboe067524e2020-03-02 16:32:28 -07008347 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008348 return 0;
8349}
8350
8351static void io_destroy_buffers(struct io_ring_ctx *ctx)
8352{
8353 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8354 idr_destroy(&ctx->io_buffer_idr);
8355}
8356
Jens Axboe68e68ee2021-02-13 09:00:02 -07008357static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008358{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008359 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008360
Jens Axboe68e68ee2021-02-13 09:00:02 -07008361 list_for_each_entry_safe(req, nxt, list, compl.list) {
8362 if (tsk && req->task != tsk)
8363 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008364 list_del(&req->compl.list);
8365 kmem_cache_free(req_cachep, req);
8366 }
8367}
8368
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008369static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008370{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008371 struct io_submit_state *submit_state = &ctx->submit_state;
8372
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008373 mutex_lock(&ctx->uring_lock);
8374
8375 if (submit_state->free_reqs)
8376 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8377 submit_state->reqs);
8378
8379 io_req_cache_free(&submit_state->comp.free_list, NULL);
8380
8381 spin_lock_irq(&ctx->completion_lock);
8382 io_req_cache_free(&submit_state->comp.locked_free_list, NULL);
8383 spin_unlock_irq(&ctx->completion_lock);
8384
8385 mutex_unlock(&ctx->uring_lock);
8386}
8387
Jens Axboe2b188cc2019-01-07 10:46:33 -07008388static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8389{
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00008390 /*
8391 * Some may use context even when all refs and requests have been put,
8392 * and they are free to do so while still holding uring_lock, see
8393 * __io_req_task_submit(). Wait for them to finish.
8394 */
8395 mutex_lock(&ctx->uring_lock);
8396 mutex_unlock(&ctx->uring_lock);
8397
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008398 io_sq_thread_finish(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008399 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008400
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008401 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008402 mmdrop(ctx->mm_account);
8403 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008404 }
Jens Axboedef596e2019-01-09 08:59:42 -07008405
Hao Xu8bad28d2021-02-19 17:19:36 +08008406 mutex_lock(&ctx->uring_lock);
Jens Axboe6b063142019-01-10 22:13:58 -07008407 io_sqe_files_unregister(ctx);
Hao Xu8bad28d2021-02-19 17:19:36 +08008408 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008409 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008410 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008411 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008412
Jens Axboe2b188cc2019-01-07 10:46:33 -07008413#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008414 if (ctx->ring_sock) {
8415 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008416 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008417 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008418#endif
8419
Hristo Venev75b28af2019-08-26 17:23:46 +00008420 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008421 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008422
8423 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008424 free_uid(ctx->user);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008425 io_req_caches_free(ctx, NULL);
Jens Axboee9418942021-02-19 12:33:30 -07008426 if (ctx->hash_map)
8427 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07008428 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008429 kfree(ctx);
8430}
8431
8432static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8433{
8434 struct io_ring_ctx *ctx = file->private_data;
8435 __poll_t mask = 0;
8436
8437 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008438 /*
8439 * synchronizes with barrier from wq_has_sleeper call in
8440 * io_commit_cqring
8441 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008442 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008443 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008444 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008445
8446 /*
8447 * Don't flush cqring overflow list here, just do a simple check.
8448 * Otherwise there could possible be ABBA deadlock:
8449 * CPU0 CPU1
8450 * ---- ----
8451 * lock(&ctx->uring_lock);
8452 * lock(&ep->mtx);
8453 * lock(&ctx->uring_lock);
8454 * lock(&ep->mtx);
8455 *
8456 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8457 * pushs them to do the flush.
8458 */
8459 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008460 mask |= EPOLLIN | EPOLLRDNORM;
8461
8462 return mask;
8463}
8464
8465static int io_uring_fasync(int fd, struct file *file, int on)
8466{
8467 struct io_ring_ctx *ctx = file->private_data;
8468
8469 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8470}
8471
Yejune Deng0bead8c2020-12-24 11:02:20 +08008472static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008473{
Jens Axboe4379bf82021-02-15 13:40:22 -07008474 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008475
Jens Axboe4379bf82021-02-15 13:40:22 -07008476 creds = idr_remove(&ctx->personality_idr, id);
8477 if (creds) {
8478 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008479 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008480 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008481
8482 return -EINVAL;
8483}
8484
8485static int io_remove_personalities(int id, void *p, void *data)
8486{
8487 struct io_ring_ctx *ctx = data;
8488
8489 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008490 return 0;
8491}
8492
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008493static void io_run_ctx_fallback(struct io_ring_ctx *ctx)
8494{
8495 struct callback_head *work, *head, *next;
8496
8497 do {
8498 do {
8499 head = NULL;
8500 work = READ_ONCE(ctx->exit_task_work);
8501 } while (cmpxchg(&ctx->exit_task_work, work, head) != work);
8502
8503 if (!work)
8504 break;
8505
8506 do {
8507 next = work->next;
8508 work->func(work);
8509 work = next;
8510 cond_resched();
8511 } while (work);
8512 } while (1);
8513}
8514
Jens Axboe85faa7b2020-04-09 18:14:00 -06008515static void io_ring_exit_work(struct work_struct *work)
8516{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008517 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8518 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008519
Jens Axboe56952e92020-06-17 15:00:04 -06008520 /*
8521 * If we're doing polled IO and end up having requests being
8522 * submitted async (out-of-line), then completions can come in while
8523 * we're waiting for refs to drop. We need to reap these manually,
8524 * as nobody else will be looking for them.
8525 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008526 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008527 io_uring_try_cancel_requests(ctx, NULL, NULL);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008528 io_run_ctx_fallback(ctx);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008529 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008530 io_ring_ctx_free(ctx);
8531}
8532
Jens Axboe2b188cc2019-01-07 10:46:33 -07008533static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8534{
8535 mutex_lock(&ctx->uring_lock);
8536 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008537
8538 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8539 ctx->sqo_dead = 1;
8540
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008541 /* if force is set, the ring is going away. always drop after that */
8542 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008543 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008544 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008545 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008546 mutex_unlock(&ctx->uring_lock);
8547
Pavel Begunkov6b819282020-11-06 13:00:25 +00008548 io_kill_timeouts(ctx, NULL, NULL);
8549 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008550
Jens Axboe15dff282019-11-13 09:09:23 -07008551 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008552 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008553
Jens Axboe85faa7b2020-04-09 18:14:00 -06008554 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008555 /*
8556 * Use system_unbound_wq to avoid spawning tons of event kworkers
8557 * if we're exiting a ton of rings at the same time. It just adds
8558 * noise and overhead, there's no discernable change in runtime
8559 * over using system_wq.
8560 */
8561 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008562}
8563
8564static int io_uring_release(struct inode *inode, struct file *file)
8565{
8566 struct io_ring_ctx *ctx = file->private_data;
8567
8568 file->private_data = NULL;
8569 io_ring_ctx_wait_and_kill(ctx);
8570 return 0;
8571}
8572
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008573struct io_task_cancel {
8574 struct task_struct *task;
8575 struct files_struct *files;
8576};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008577
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008578static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008579{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008580 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008581 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008582 bool ret;
8583
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008584 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008585 unsigned long flags;
8586 struct io_ring_ctx *ctx = req->ctx;
8587
8588 /* protect against races with linked timeouts */
8589 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008590 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008591 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8592 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008593 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008594 }
8595 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008596}
8597
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008598static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008599 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008600 struct files_struct *files)
8601{
8602 struct io_defer_entry *de = NULL;
8603 LIST_HEAD(list);
8604
8605 spin_lock_irq(&ctx->completion_lock);
8606 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008607 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008608 list_cut_position(&list, &ctx->defer_list, &de->list);
8609 break;
8610 }
8611 }
8612 spin_unlock_irq(&ctx->completion_lock);
8613
8614 while (!list_empty(&list)) {
8615 de = list_first_entry(&list, struct io_defer_entry, list);
8616 list_del_init(&de->list);
8617 req_set_fail_links(de->req);
8618 io_put_req(de->req);
8619 io_req_complete(de->req, -ECANCELED);
8620 kfree(de);
8621 }
8622}
8623
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008624static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8625 struct task_struct *task,
8626 struct files_struct *files)
8627{
8628 struct io_task_cancel cancel = { .task = task, .files = files, };
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008629 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008630
8631 while (1) {
8632 enum io_wq_cancel cret;
8633 bool ret = false;
8634
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008635 if (tctx && tctx->io_wq) {
8636 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008637 &cancel, true);
8638 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8639 }
8640
8641 /* SQPOLL thread does its own polling */
8642 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8643 while (!list_empty_careful(&ctx->iopoll_list)) {
8644 io_iopoll_try_reap_events(ctx);
8645 ret = true;
8646 }
8647 }
8648
8649 ret |= io_poll_remove_all(ctx, task, files);
8650 ret |= io_kill_timeouts(ctx, task, files);
8651 ret |= io_run_task_work();
8652 io_cqring_overflow_flush(ctx, true, task, files);
8653 if (!ret)
8654 break;
8655 cond_resched();
8656 }
8657}
8658
Pavel Begunkovca70f002021-01-26 15:28:27 +00008659static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8660 struct task_struct *task,
8661 struct files_struct *files)
8662{
8663 struct io_kiocb *req;
8664 int cnt = 0;
8665
8666 spin_lock_irq(&ctx->inflight_lock);
8667 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8668 cnt += io_match_task(req, task, files);
8669 spin_unlock_irq(&ctx->inflight_lock);
8670 return cnt;
8671}
8672
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008673static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008674 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008675 struct files_struct *files)
8676{
Jens Axboefcb323c2019-10-24 12:39:47 -06008677 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008678 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008679 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008680
Pavel Begunkovca70f002021-01-26 15:28:27 +00008681 inflight = io_uring_count_inflight(ctx, task, files);
8682 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008683 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008684
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008685 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008686
Pavel Begunkov34343782021-02-10 11:45:42 +00008687 if (ctx->sq_data)
8688 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008689 prepare_to_wait(&task->io_uring->wait, &wait,
8690 TASK_UNINTERRUPTIBLE);
8691 if (inflight == io_uring_count_inflight(ctx, task, files))
8692 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008693 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00008694 if (ctx->sq_data)
8695 io_sq_thread_park(ctx->sq_data);
Jens Axboe0f212202020-09-13 13:09:39 -06008696 }
Jens Axboe0f212202020-09-13 13:09:39 -06008697}
8698
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008699static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8700{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008701 mutex_lock(&ctx->uring_lock);
8702 ctx->sqo_dead = 1;
8703 mutex_unlock(&ctx->uring_lock);
8704
8705 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008706 if (ctx->rings)
8707 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008708}
8709
Jens Axboe0f212202020-09-13 13:09:39 -06008710/*
8711 * We need to iteratively cancel requests, in case a request has dependent
8712 * hard links. These persist even for failure of cancelations, hence keep
8713 * looping until none are found.
8714 */
8715static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8716 struct files_struct *files)
8717{
8718 struct task_struct *task = current;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008719 bool did_park = false;
Jens Axboe0f212202020-09-13 13:09:39 -06008720
Jens Axboefdaf0832020-10-30 09:37:30 -06008721 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008722 io_disable_sqo_submit(ctx);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008723 did_park = io_sq_thread_park(ctx->sq_data);
8724 if (did_park) {
8725 task = ctx->sq_data->thread;
8726 atomic_inc(&task->io_uring->in_idle);
8727 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008728 }
Jens Axboe0f212202020-09-13 13:09:39 -06008729
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008730 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008731
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008732 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008733 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008734 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008735
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008736 if (did_park) {
Jens Axboefdaf0832020-10-30 09:37:30 -06008737 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06008738 io_sq_thread_unpark(ctx->sq_data);
8739 }
Jens Axboe0f212202020-09-13 13:09:39 -06008740}
8741
8742/*
8743 * Note that this task has used io_uring. We use it for cancelation purposes.
8744 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008745static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008746{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008747 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008748 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008749
8750 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008751 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06008752 if (unlikely(ret))
8753 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008754 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008755 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008756 if (tctx->last != file) {
8757 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008758
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008759 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008760 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008761 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8762 file, GFP_KERNEL));
8763 if (ret) {
8764 fput(file);
8765 return ret;
8766 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00008767
8768 /* one and only SQPOLL file note, held by sqo_task */
8769 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
8770 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06008771 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008772 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008773 }
8774
Jens Axboefdaf0832020-10-30 09:37:30 -06008775 /*
8776 * This is race safe in that the task itself is doing this, hence it
8777 * cannot be going through the exit/cancel paths at the same time.
8778 * This cannot be modified while exit/cancel is running.
8779 */
8780 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8781 tctx->sqpoll = true;
8782
Jens Axboe0f212202020-09-13 13:09:39 -06008783 return 0;
8784}
8785
8786/*
8787 * Remove this io_uring_file -> task mapping.
8788 */
8789static void io_uring_del_task_file(struct file *file)
8790{
8791 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008792
8793 if (tctx->last == file)
8794 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008795 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008796 if (file)
8797 fput(file);
8798}
8799
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008800static void io_uring_remove_task_files(struct io_uring_task *tctx)
8801{
8802 struct file *file;
8803 unsigned long index;
8804
8805 xa_for_each(&tctx->xa, index, file)
8806 io_uring_del_task_file(file);
8807}
8808
Jens Axboe0f212202020-09-13 13:09:39 -06008809void __io_uring_files_cancel(struct files_struct *files)
8810{
8811 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008812 struct file *file;
8813 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008814
8815 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008816 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008817 xa_for_each(&tctx->xa, index, file)
8818 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008819 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008820
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008821 if (files) {
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008822 io_uring_remove_task_files(tctx);
Jens Axboe8a378fb2021-02-23 12:27:49 -07008823 if (tctx->io_wq) {
Jens Axboe4fb6ac32021-02-25 10:17:09 -07008824 io_wq_put(tctx->io_wq);
Jens Axboe8a378fb2021-02-23 12:27:49 -07008825 tctx->io_wq = NULL;
8826 }
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008827 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008828}
8829
8830static s64 tctx_inflight(struct io_uring_task *tctx)
8831{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008832 return percpu_counter_sum(&tctx->inflight);
8833}
8834
8835static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
8836{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008837 struct io_sq_data *sqd = ctx->sq_data;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008838 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06008839 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008840 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008841
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008842 if (!sqd)
8843 return;
8844 io_disable_sqo_submit(ctx);
8845 if (!io_sq_thread_park(sqd))
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008846 return;
8847 tctx = ctx->sq_data->thread->io_uring;
Jens Axboefdaf0832020-10-30 09:37:30 -06008848
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008849 atomic_inc(&tctx->in_idle);
8850 do {
8851 /* read completions before cancelations */
8852 inflight = tctx_inflight(tctx);
8853 if (!inflight)
8854 break;
8855 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008856
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008857 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8858 /*
8859 * If we've seen completions, retry without waiting. This
8860 * avoids a race where a completion comes in before we did
8861 * prepare_to_wait().
8862 */
8863 if (inflight == tctx_inflight(tctx))
8864 schedule();
8865 finish_wait(&tctx->wait, &wait);
8866 } while (1);
8867 atomic_dec(&tctx->in_idle);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008868 io_sq_thread_unpark(sqd);
Jens Axboe0f212202020-09-13 13:09:39 -06008869}
8870
Jens Axboe0f212202020-09-13 13:09:39 -06008871/*
8872 * Find any io_uring fd that this task has registered or done IO on, and cancel
8873 * requests.
8874 */
8875void __io_uring_task_cancel(void)
8876{
8877 struct io_uring_task *tctx = current->io_uring;
8878 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008879 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008880
8881 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008882 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008883
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00008884 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008885 if (tctx->sqpoll) {
8886 struct file *file;
8887 unsigned long index;
8888
8889 xa_for_each(&tctx->xa, index, file)
8890 io_uring_cancel_sqpoll(file->private_data);
8891 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00008892
Jens Axboed8a6df12020-10-15 16:24:45 -06008893 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008894 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008895 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008896 if (!inflight)
8897 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008898 __io_uring_files_cancel(NULL);
8899
8900 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8901
8902 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008903 * If we've seen completions, retry without waiting. This
8904 * avoids a race where a completion comes in before we did
8905 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06008906 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008907 if (inflight == tctx_inflight(tctx))
8908 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00008909 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008910 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008911
Jens Axboefdaf0832020-10-30 09:37:30 -06008912 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008913
8914 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008915}
8916
Jens Axboefcb323c2019-10-24 12:39:47 -06008917static int io_uring_flush(struct file *file, void *data)
8918{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008919 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008920 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008921
Jens Axboe3bfe6102021-02-16 14:15:30 -07008922 /* Ignore helper thread files exit */
8923 if (current->flags & PF_IO_WORKER)
8924 return 0;
8925
Jens Axboe41be53e2021-02-13 09:11:04 -07008926 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
Jens Axboe84965ff2021-01-23 15:51:11 -07008927 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboe41be53e2021-02-13 09:11:04 -07008928 io_req_caches_free(ctx, current);
8929 }
Jens Axboe84965ff2021-01-23 15:51:11 -07008930
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008931 io_run_ctx_fallback(ctx);
8932
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008933 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00008934 return 0;
8935
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008936 /* we should have cancelled and erased it before PF_EXITING */
8937 WARN_ON_ONCE((current->flags & PF_EXITING) &&
8938 xa_load(&tctx->xa, (unsigned long)file));
8939
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00008940 /*
8941 * fput() is pending, will be 2 if the only other ref is our potential
8942 * task file note. If the task is exiting, drop regardless of count.
8943 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008944 if (atomic_long_read(&file->f_count) != 2)
8945 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00008946
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008947 if (ctx->flags & IORING_SETUP_SQPOLL) {
8948 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00008949 WARN_ON_ONCE(ctx->sqo_task != current &&
8950 xa_load(&tctx->xa, (unsigned long)file));
8951 /* sqo_dead check is for when this happens after cancellation */
8952 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008953 !xa_load(&tctx->xa, (unsigned long)file));
8954
8955 io_disable_sqo_submit(ctx);
8956 }
8957
8958 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
8959 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06008960 return 0;
8961}
8962
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008963static void *io_uring_validate_mmap_request(struct file *file,
8964 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008965{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008966 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008967 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008968 struct page *page;
8969 void *ptr;
8970
8971 switch (offset) {
8972 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008973 case IORING_OFF_CQ_RING:
8974 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008975 break;
8976 case IORING_OFF_SQES:
8977 ptr = ctx->sq_sqes;
8978 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008979 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008980 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008981 }
8982
8983 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008984 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008985 return ERR_PTR(-EINVAL);
8986
8987 return ptr;
8988}
8989
8990#ifdef CONFIG_MMU
8991
8992static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8993{
8994 size_t sz = vma->vm_end - vma->vm_start;
8995 unsigned long pfn;
8996 void *ptr;
8997
8998 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8999 if (IS_ERR(ptr))
9000 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009001
9002 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9003 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9004}
9005
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009006#else /* !CONFIG_MMU */
9007
9008static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9009{
9010 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9011}
9012
9013static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9014{
9015 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9016}
9017
9018static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9019 unsigned long addr, unsigned long len,
9020 unsigned long pgoff, unsigned long flags)
9021{
9022 void *ptr;
9023
9024 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9025 if (IS_ERR(ptr))
9026 return PTR_ERR(ptr);
9027
9028 return (unsigned long) ptr;
9029}
9030
9031#endif /* !CONFIG_MMU */
9032
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009033static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009034{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009035 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009036 DEFINE_WAIT(wait);
9037
9038 do {
9039 if (!io_sqring_full(ctx))
9040 break;
9041
9042 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9043
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009044 if (unlikely(ctx->sqo_dead)) {
9045 ret = -EOWNERDEAD;
9046 goto out;
9047 }
9048
Jens Axboe90554202020-09-03 12:12:41 -06009049 if (!io_sqring_full(ctx))
9050 break;
9051
9052 schedule();
9053 } while (!signal_pending(current));
9054
9055 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009056out:
9057 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009058}
9059
Hao Xuc73ebb62020-11-03 10:54:37 +08009060static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9061 struct __kernel_timespec __user **ts,
9062 const sigset_t __user **sig)
9063{
9064 struct io_uring_getevents_arg arg;
9065
9066 /*
9067 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9068 * is just a pointer to the sigset_t.
9069 */
9070 if (!(flags & IORING_ENTER_EXT_ARG)) {
9071 *sig = (const sigset_t __user *) argp;
9072 *ts = NULL;
9073 return 0;
9074 }
9075
9076 /*
9077 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9078 * timespec and sigset_t pointers if good.
9079 */
9080 if (*argsz != sizeof(arg))
9081 return -EINVAL;
9082 if (copy_from_user(&arg, argp, sizeof(arg)))
9083 return -EFAULT;
9084 *sig = u64_to_user_ptr(arg.sigmask);
9085 *argsz = arg.sigmask_sz;
9086 *ts = u64_to_user_ptr(arg.ts);
9087 return 0;
9088}
9089
Jens Axboe2b188cc2019-01-07 10:46:33 -07009090SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009091 u32, min_complete, u32, flags, const void __user *, argp,
9092 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009093{
9094 struct io_ring_ctx *ctx;
9095 long ret = -EBADF;
9096 int submitted = 0;
9097 struct fd f;
9098
Jens Axboe4c6e2772020-07-01 11:29:10 -06009099 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009100
Jens Axboe90554202020-09-03 12:12:41 -06009101 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009102 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009103 return -EINVAL;
9104
9105 f = fdget(fd);
9106 if (!f.file)
9107 return -EBADF;
9108
9109 ret = -EOPNOTSUPP;
9110 if (f.file->f_op != &io_uring_fops)
9111 goto out_fput;
9112
9113 ret = -ENXIO;
9114 ctx = f.file->private_data;
9115 if (!percpu_ref_tryget(&ctx->refs))
9116 goto out_fput;
9117
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009118 ret = -EBADFD;
9119 if (ctx->flags & IORING_SETUP_R_DISABLED)
9120 goto out;
9121
Jens Axboe6c271ce2019-01-10 11:22:30 -07009122 /*
9123 * For SQ polling, the thread will do all submissions and completions.
9124 * Just return the requested submit count, and wake the thread if
9125 * we were asked to.
9126 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009127 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009128 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009129 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009130
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009131 ret = -EOWNERDEAD;
9132 if (unlikely(ctx->sqo_dead))
9133 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009134 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009135 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009136 if (flags & IORING_ENTER_SQ_WAIT) {
9137 ret = io_sqpoll_wait_sq(ctx);
9138 if (ret)
9139 goto out;
9140 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009141 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009142 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009143 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009144 if (unlikely(ret))
9145 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009146 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009147 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009148 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009149
9150 if (submitted != to_submit)
9151 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009152 }
9153 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009154 const sigset_t __user *sig;
9155 struct __kernel_timespec __user *ts;
9156
9157 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9158 if (unlikely(ret))
9159 goto out;
9160
Jens Axboe2b188cc2019-01-07 10:46:33 -07009161 min_complete = min(min_complete, ctx->cq_entries);
9162
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009163 /*
9164 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9165 * space applications don't need to do io completion events
9166 * polling again, they can rely on io_sq_thread to do polling
9167 * work, which can reduce cpu usage and uring_lock contention.
9168 */
9169 if (ctx->flags & IORING_SETUP_IOPOLL &&
9170 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009171 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009172 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009173 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009174 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009175 }
9176
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009177out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009178 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009179out_fput:
9180 fdput(f);
9181 return submitted ? submitted : ret;
9182}
9183
Tobias Klauserbebdb652020-02-26 18:38:32 +01009184#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009185static int io_uring_show_cred(int id, void *p, void *data)
9186{
Jens Axboe4379bf82021-02-15 13:40:22 -07009187 const struct cred *cred = p;
Jens Axboe87ce9552020-01-30 08:25:34 -07009188 struct seq_file *m = data;
9189 struct user_namespace *uns = seq_user_ns(m);
9190 struct group_info *gi;
9191 kernel_cap_t cap;
9192 unsigned __capi;
9193 int g;
9194
9195 seq_printf(m, "%5d\n", id);
9196 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9197 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9198 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9199 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9200 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9201 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9202 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9203 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9204 seq_puts(m, "\n\tGroups:\t");
9205 gi = cred->group_info;
9206 for (g = 0; g < gi->ngroups; g++) {
9207 seq_put_decimal_ull(m, g ? " " : "",
9208 from_kgid_munged(uns, gi->gid[g]));
9209 }
9210 seq_puts(m, "\n\tCapEff:\t");
9211 cap = cred->cap_effective;
9212 CAP_FOR_EACH_U32(__capi)
9213 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9214 seq_putc(m, '\n');
9215 return 0;
9216}
9217
9218static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9219{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009220 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009221 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009222 int i;
9223
Jens Axboefad8e0d2020-09-28 08:57:48 -06009224 /*
9225 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9226 * since fdinfo case grabs it in the opposite direction of normal use
9227 * cases. If we fail to get the lock, we just don't iterate any
9228 * structures that could be going away outside the io_uring mutex.
9229 */
9230 has_lock = mutex_trylock(&ctx->uring_lock);
9231
Joseph Qidbbe9c62020-09-29 09:01:22 -06009232 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9233 sq = ctx->sq_data;
9234
9235 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9236 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009237 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009238 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009239 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009240
Jens Axboe87ce9552020-01-30 08:25:34 -07009241 if (f)
9242 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9243 else
9244 seq_printf(m, "%5u: <none>\n", i);
9245 }
9246 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009247 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009248 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9249
9250 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9251 (unsigned int) buf->len);
9252 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009253 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009254 seq_printf(m, "Personalities:\n");
9255 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9256 }
Jens Axboed7718a92020-02-14 22:23:12 -07009257 seq_printf(m, "PollList:\n");
9258 spin_lock_irq(&ctx->completion_lock);
9259 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9260 struct hlist_head *list = &ctx->cancel_hash[i];
9261 struct io_kiocb *req;
9262
9263 hlist_for_each_entry(req, list, hash_node)
9264 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9265 req->task->task_works != NULL);
9266 }
9267 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009268 if (has_lock)
9269 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009270}
9271
9272static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9273{
9274 struct io_ring_ctx *ctx = f->private_data;
9275
9276 if (percpu_ref_tryget(&ctx->refs)) {
9277 __io_uring_show_fdinfo(ctx, m);
9278 percpu_ref_put(&ctx->refs);
9279 }
9280}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009281#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009282
Jens Axboe2b188cc2019-01-07 10:46:33 -07009283static const struct file_operations io_uring_fops = {
9284 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009285 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009286 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009287#ifndef CONFIG_MMU
9288 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9289 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9290#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009291 .poll = io_uring_poll,
9292 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009293#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009294 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009295#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009296};
9297
9298static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9299 struct io_uring_params *p)
9300{
Hristo Venev75b28af2019-08-26 17:23:46 +00009301 struct io_rings *rings;
9302 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009303
Jens Axboebd740482020-08-05 12:58:23 -06009304 /* make sure these are sane, as we already accounted them */
9305 ctx->sq_entries = p->sq_entries;
9306 ctx->cq_entries = p->cq_entries;
9307
Hristo Venev75b28af2019-08-26 17:23:46 +00009308 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9309 if (size == SIZE_MAX)
9310 return -EOVERFLOW;
9311
9312 rings = io_mem_alloc(size);
9313 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009314 return -ENOMEM;
9315
Hristo Venev75b28af2019-08-26 17:23:46 +00009316 ctx->rings = rings;
9317 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9318 rings->sq_ring_mask = p->sq_entries - 1;
9319 rings->cq_ring_mask = p->cq_entries - 1;
9320 rings->sq_ring_entries = p->sq_entries;
9321 rings->cq_ring_entries = p->cq_entries;
9322 ctx->sq_mask = rings->sq_ring_mask;
9323 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009324
9325 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009326 if (size == SIZE_MAX) {
9327 io_mem_free(ctx->rings);
9328 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009329 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009330 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009331
9332 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009333 if (!ctx->sq_sqes) {
9334 io_mem_free(ctx->rings);
9335 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009336 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009337 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009338
Jens Axboe2b188cc2019-01-07 10:46:33 -07009339 return 0;
9340}
9341
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009342static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9343{
9344 int ret, fd;
9345
9346 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9347 if (fd < 0)
9348 return fd;
9349
9350 ret = io_uring_add_task_file(ctx, file);
9351 if (ret) {
9352 put_unused_fd(fd);
9353 return ret;
9354 }
9355 fd_install(fd, file);
9356 return fd;
9357}
9358
Jens Axboe2b188cc2019-01-07 10:46:33 -07009359/*
9360 * Allocate an anonymous fd, this is what constitutes the application
9361 * visible backing of an io_uring instance. The application mmaps this
9362 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9363 * we have to tie this fd to a socket for file garbage collection purposes.
9364 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009365static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009366{
9367 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009368#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009369 int ret;
9370
Jens Axboe2b188cc2019-01-07 10:46:33 -07009371 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9372 &ctx->ring_sock);
9373 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009374 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009375#endif
9376
Jens Axboe2b188cc2019-01-07 10:46:33 -07009377 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9378 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009379#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009380 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009381 sock_release(ctx->ring_sock);
9382 ctx->ring_sock = NULL;
9383 } else {
9384 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009385 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009386#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009387 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009388}
9389
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009390static int io_uring_create(unsigned entries, struct io_uring_params *p,
9391 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009392{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009393 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009394 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009395 int ret;
9396
Jens Axboe8110c1a2019-12-28 15:39:54 -07009397 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009398 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009399 if (entries > IORING_MAX_ENTRIES) {
9400 if (!(p->flags & IORING_SETUP_CLAMP))
9401 return -EINVAL;
9402 entries = IORING_MAX_ENTRIES;
9403 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009404
9405 /*
9406 * Use twice as many entries for the CQ ring. It's possible for the
9407 * application to drive a higher depth than the size of the SQ ring,
9408 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009409 * some flexibility in overcommitting a bit. If the application has
9410 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9411 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009412 */
9413 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009414 if (p->flags & IORING_SETUP_CQSIZE) {
9415 /*
9416 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9417 * to a power-of-two, if it isn't already. We do NOT impose
9418 * any cq vs sq ring sizing.
9419 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009420 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009421 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009422 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9423 if (!(p->flags & IORING_SETUP_CLAMP))
9424 return -EINVAL;
9425 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9426 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009427 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9428 if (p->cq_entries < p->sq_entries)
9429 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009430 } else {
9431 p->cq_entries = 2 * p->sq_entries;
9432 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009433
Jens Axboe2b188cc2019-01-07 10:46:33 -07009434 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -07009435 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009436 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009437 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -07009438 if (!capable(CAP_IPC_LOCK))
9439 ctx->user = get_uid(current_user());
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009440 ctx->sqo_task = current;
Jens Axboe2aede0e2020-09-14 10:45:53 -06009441
9442 /*
9443 * This is just grabbed for accounting purposes. When a process exits,
9444 * the mm is exited and dropped before the files, hence we need to hang
9445 * on to this mm purely for the purposes of being able to unaccount
9446 * memory (locked/pinned vm). It's not used for anything else.
9447 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009448 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009449 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009450
Jens Axboe2b188cc2019-01-07 10:46:33 -07009451 ret = io_allocate_scq_urings(ctx, p);
9452 if (ret)
9453 goto err;
9454
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009455 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009456 if (ret)
9457 goto err;
9458
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009459 if (!(p->flags & IORING_SETUP_R_DISABLED))
9460 io_sq_offload_start(ctx);
9461
Jens Axboe2b188cc2019-01-07 10:46:33 -07009462 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009463 p->sq_off.head = offsetof(struct io_rings, sq.head);
9464 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9465 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9466 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9467 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9468 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9469 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009470
9471 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009472 p->cq_off.head = offsetof(struct io_rings, cq.head);
9473 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9474 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9475 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9476 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9477 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009478 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009479
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009480 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9481 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009482 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009483 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Jens Axboe1c0aa1f2021-02-20 11:55:28 -07009484 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009485
9486 if (copy_to_user(params, p, sizeof(*p))) {
9487 ret = -EFAULT;
9488 goto err;
9489 }
Jens Axboed1719f72020-07-30 13:43:53 -06009490
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009491 file = io_uring_get_file(ctx);
9492 if (IS_ERR(file)) {
9493 ret = PTR_ERR(file);
9494 goto err;
9495 }
9496
Jens Axboed1719f72020-07-30 13:43:53 -06009497 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009498 * Install ring fd as the very last thing, so we don't risk someone
9499 * having closed it before we finish setup
9500 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009501 ret = io_uring_install_fd(ctx, file);
9502 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009503 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009504 /* fput will clean it up */
9505 fput(file);
9506 return ret;
9507 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009508
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009509 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009510 return ret;
9511err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009512 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009513 io_ring_ctx_wait_and_kill(ctx);
9514 return ret;
9515}
9516
9517/*
9518 * Sets up an aio uring context, and returns the fd. Applications asks for a
9519 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9520 * params structure passed in.
9521 */
9522static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9523{
9524 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009525 int i;
9526
9527 if (copy_from_user(&p, params, sizeof(p)))
9528 return -EFAULT;
9529 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9530 if (p.resv[i])
9531 return -EINVAL;
9532 }
9533
Jens Axboe6c271ce2019-01-10 11:22:30 -07009534 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009535 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009536 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9537 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009538 return -EINVAL;
9539
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009540 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009541}
9542
9543SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9544 struct io_uring_params __user *, params)
9545{
9546 return io_uring_setup(entries, params);
9547}
9548
Jens Axboe66f4af92020-01-16 15:36:52 -07009549static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9550{
9551 struct io_uring_probe *p;
9552 size_t size;
9553 int i, ret;
9554
9555 size = struct_size(p, ops, nr_args);
9556 if (size == SIZE_MAX)
9557 return -EOVERFLOW;
9558 p = kzalloc(size, GFP_KERNEL);
9559 if (!p)
9560 return -ENOMEM;
9561
9562 ret = -EFAULT;
9563 if (copy_from_user(p, arg, size))
9564 goto out;
9565 ret = -EINVAL;
9566 if (memchr_inv(p, 0, size))
9567 goto out;
9568
9569 p->last_op = IORING_OP_LAST - 1;
9570 if (nr_args > IORING_OP_LAST)
9571 nr_args = IORING_OP_LAST;
9572
9573 for (i = 0; i < nr_args; i++) {
9574 p->ops[i].op = i;
9575 if (!io_op_defs[i].not_supported)
9576 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9577 }
9578 p->ops_len = i;
9579
9580 ret = 0;
9581 if (copy_to_user(arg, p, size))
9582 ret = -EFAULT;
9583out:
9584 kfree(p);
9585 return ret;
9586}
9587
Jens Axboe071698e2020-01-28 10:04:42 -07009588static int io_register_personality(struct io_ring_ctx *ctx)
9589{
Jens Axboe4379bf82021-02-15 13:40:22 -07009590 const struct cred *creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009591 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009592
Jens Axboe4379bf82021-02-15 13:40:22 -07009593 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009594
Jens Axboe4379bf82021-02-15 13:40:22 -07009595 ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9596 USHRT_MAX, GFP_KERNEL);
9597 if (ret < 0)
9598 put_cred(creds);
Jens Axboe1e6fa522020-10-15 08:46:24 -06009599 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009600}
9601
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009602static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9603 unsigned int nr_args)
9604{
9605 struct io_uring_restriction *res;
9606 size_t size;
9607 int i, ret;
9608
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009609 /* Restrictions allowed only if rings started disabled */
9610 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9611 return -EBADFD;
9612
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009613 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009614 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009615 return -EBUSY;
9616
9617 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9618 return -EINVAL;
9619
9620 size = array_size(nr_args, sizeof(*res));
9621 if (size == SIZE_MAX)
9622 return -EOVERFLOW;
9623
9624 res = memdup_user(arg, size);
9625 if (IS_ERR(res))
9626 return PTR_ERR(res);
9627
9628 ret = 0;
9629
9630 for (i = 0; i < nr_args; i++) {
9631 switch (res[i].opcode) {
9632 case IORING_RESTRICTION_REGISTER_OP:
9633 if (res[i].register_op >= IORING_REGISTER_LAST) {
9634 ret = -EINVAL;
9635 goto out;
9636 }
9637
9638 __set_bit(res[i].register_op,
9639 ctx->restrictions.register_op);
9640 break;
9641 case IORING_RESTRICTION_SQE_OP:
9642 if (res[i].sqe_op >= IORING_OP_LAST) {
9643 ret = -EINVAL;
9644 goto out;
9645 }
9646
9647 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9648 break;
9649 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9650 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9651 break;
9652 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9653 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9654 break;
9655 default:
9656 ret = -EINVAL;
9657 goto out;
9658 }
9659 }
9660
9661out:
9662 /* Reset all restrictions if an error happened */
9663 if (ret != 0)
9664 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9665 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009666 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009667
9668 kfree(res);
9669 return ret;
9670}
9671
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009672static int io_register_enable_rings(struct io_ring_ctx *ctx)
9673{
9674 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9675 return -EBADFD;
9676
9677 if (ctx->restrictions.registered)
9678 ctx->restricted = 1;
9679
9680 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9681
9682 io_sq_offload_start(ctx);
9683
9684 return 0;
9685}
9686
Jens Axboe071698e2020-01-28 10:04:42 -07009687static bool io_register_op_must_quiesce(int op)
9688{
9689 switch (op) {
9690 case IORING_UNREGISTER_FILES:
9691 case IORING_REGISTER_FILES_UPDATE:
9692 case IORING_REGISTER_PROBE:
9693 case IORING_REGISTER_PERSONALITY:
9694 case IORING_UNREGISTER_PERSONALITY:
9695 return false;
9696 default:
9697 return true;
9698 }
9699}
9700
Jens Axboeedafcce2019-01-09 09:16:05 -07009701static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9702 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009703 __releases(ctx->uring_lock)
9704 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009705{
9706 int ret;
9707
Jens Axboe35fa71a2019-04-22 10:23:23 -06009708 /*
9709 * We're inside the ring mutex, if the ref is already dying, then
9710 * someone else killed the ctx or is already going through
9711 * io_uring_register().
9712 */
9713 if (percpu_ref_is_dying(&ctx->refs))
9714 return -ENXIO;
9715
Jens Axboe071698e2020-01-28 10:04:42 -07009716 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009717 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009718
Jens Axboe05f3fb32019-12-09 11:22:50 -07009719 /*
9720 * Drop uring mutex before waiting for references to exit. If
9721 * another thread is currently inside io_uring_enter() it might
9722 * need to grab the uring_lock to make progress. If we hold it
9723 * here across the drain wait, then we can deadlock. It's safe
9724 * to drop the mutex here, since no new references will come in
9725 * after we've killed the percpu ref.
9726 */
9727 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009728 do {
9729 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9730 if (!ret)
9731 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009732 ret = io_run_task_work_sig();
9733 if (ret < 0)
9734 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009735 } while (1);
9736
Jens Axboe05f3fb32019-12-09 11:22:50 -07009737 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009738
Pavel Begunkov88f171a2021-02-20 18:03:50 +00009739 if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp))
9740 return ret;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009741 }
9742
9743 if (ctx->restricted) {
9744 if (opcode >= IORING_REGISTER_LAST) {
9745 ret = -EINVAL;
9746 goto out;
9747 }
9748
9749 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9750 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009751 goto out;
9752 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009753 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009754
9755 switch (opcode) {
9756 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009757 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -07009758 break;
9759 case IORING_UNREGISTER_BUFFERS:
9760 ret = -EINVAL;
9761 if (arg || nr_args)
9762 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009763 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07009764 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009765 case IORING_REGISTER_FILES:
9766 ret = io_sqe_files_register(ctx, arg, nr_args);
9767 break;
9768 case IORING_UNREGISTER_FILES:
9769 ret = -EINVAL;
9770 if (arg || nr_args)
9771 break;
9772 ret = io_sqe_files_unregister(ctx);
9773 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009774 case IORING_REGISTER_FILES_UPDATE:
9775 ret = io_sqe_files_update(ctx, arg, nr_args);
9776 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009777 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009778 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009779 ret = -EINVAL;
9780 if (nr_args != 1)
9781 break;
9782 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009783 if (ret)
9784 break;
9785 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9786 ctx->eventfd_async = 1;
9787 else
9788 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009789 break;
9790 case IORING_UNREGISTER_EVENTFD:
9791 ret = -EINVAL;
9792 if (arg || nr_args)
9793 break;
9794 ret = io_eventfd_unregister(ctx);
9795 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009796 case IORING_REGISTER_PROBE:
9797 ret = -EINVAL;
9798 if (!arg || nr_args > 256)
9799 break;
9800 ret = io_probe(ctx, arg, nr_args);
9801 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009802 case IORING_REGISTER_PERSONALITY:
9803 ret = -EINVAL;
9804 if (arg || nr_args)
9805 break;
9806 ret = io_register_personality(ctx);
9807 break;
9808 case IORING_UNREGISTER_PERSONALITY:
9809 ret = -EINVAL;
9810 if (arg)
9811 break;
9812 ret = io_unregister_personality(ctx, nr_args);
9813 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009814 case IORING_REGISTER_ENABLE_RINGS:
9815 ret = -EINVAL;
9816 if (arg || nr_args)
9817 break;
9818 ret = io_register_enable_rings(ctx);
9819 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009820 case IORING_REGISTER_RESTRICTIONS:
9821 ret = io_register_restrictions(ctx, arg, nr_args);
9822 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009823 default:
9824 ret = -EINVAL;
9825 break;
9826 }
9827
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009828out:
Jens Axboe071698e2020-01-28 10:04:42 -07009829 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009830 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009831 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -06009832 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009833 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009834 return ret;
9835}
9836
9837SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9838 void __user *, arg, unsigned int, nr_args)
9839{
9840 struct io_ring_ctx *ctx;
9841 long ret = -EBADF;
9842 struct fd f;
9843
9844 f = fdget(fd);
9845 if (!f.file)
9846 return -EBADF;
9847
9848 ret = -EOPNOTSUPP;
9849 if (f.file->f_op != &io_uring_fops)
9850 goto out_fput;
9851
9852 ctx = f.file->private_data;
9853
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +00009854 io_run_task_work();
9855
Jens Axboeedafcce2019-01-09 09:16:05 -07009856 mutex_lock(&ctx->uring_lock);
9857 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9858 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009859 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9860 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009861out_fput:
9862 fdput(f);
9863 return ret;
9864}
9865
Jens Axboe2b188cc2019-01-07 10:46:33 -07009866static int __init io_uring_init(void)
9867{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009868#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9869 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9870 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9871} while (0)
9872
9873#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9874 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9875 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9876 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9877 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9878 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9879 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9880 BUILD_BUG_SQE_ELEM(8, __u64, off);
9881 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9882 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009883 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009884 BUILD_BUG_SQE_ELEM(24, __u32, len);
9885 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9886 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9887 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9888 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009889 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9890 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009891 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9892 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9893 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9894 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9895 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9896 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9897 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9898 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009899 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009900 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9901 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9902 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009903 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009904
Jens Axboed3656342019-12-18 09:50:26 -07009905 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009906 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -07009907 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
9908 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009909 return 0;
9910};
9911__initcall(io_uring_init);