blob: d24e0322bd1d43f1fea1af63c959ca8fb2c7b6ce [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 Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060093
94/*
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
96 */
97#define IORING_FILE_TABLE_SHIFT 9
98#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
99#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
100#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
102struct io_uring {
103 u32 head ____cacheline_aligned_in_smp;
104 u32 tail ____cacheline_aligned_in_smp;
105};
106
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 *
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
118 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * The kernel controls head of the sq ring and the tail of the cq ring,
120 * and the application controls tail of the sq ring and the head of the
121 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 * ring_entries - 1)
127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 u32 sq_ring_mask, cq_ring_mask;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
134 *
135 * Written by the kernel, shouldn't be modified by the
136 * application (i.e. get number of "new events" by comparing to
137 * cached value).
138 *
139 * After a new SQ head value was read by the application this
140 * counter includes all submissions that were dropped reaching
141 * the new SQ head (and possibly more).
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200145 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application.
149 *
150 * The application needs a full memory barrier before checking
151 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
152 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000153 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200155 * Runtime CQ flags
156 *
157 * Written by the application, shouldn't be modified by the
158 * kernel.
159 */
160 u32 cq_flags;
161 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 * Number of completion events lost because the queue was full;
163 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800164 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * the completion queue.
166 *
167 * Written by the kernel, shouldn't be modified by the
168 * application (i.e. get number of "new events" by comparing to
169 * cached value).
170 *
171 * As completion events come in out of order this counter is not
172 * ordered with any other data.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200175 /*
176 * Ring buffer of completion events.
177 *
178 * The kernel writes completion events fresh every time they are
179 * produced, so the application is allowed to modify pending
180 * entries.
181 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000182 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700183};
184
Jens Axboeedafcce2019-01-09 09:16:05 -0700185struct io_mapped_ubuf {
186 u64 ubuf;
187 size_t len;
188 struct bio_vec *bvec;
189 unsigned int nr_bvecs;
190};
191
Jens Axboe65e19f52019-10-26 07:20:21 -0600192struct fixed_file_table {
193 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700194};
195
Xiaoguang Wang05589552020-03-31 14:05:18 +0800196struct fixed_file_ref_node {
197 struct percpu_ref refs;
198 struct list_head node;
199 struct list_head file_list;
200 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600201 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800202};
203
Jens Axboe05f3fb32019-12-09 11:22:50 -0700204struct fixed_file_data {
205 struct fixed_file_table *table;
206 struct io_ring_ctx *ctx;
207
Xiaoguang Wang05589552020-03-31 14:05:18 +0800208 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700210 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct list_head ref_list;
212 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213};
214
Jens Axboe5a2e7452020-02-23 16:23:11 -0700215struct io_buffer {
216 struct list_head list;
217 __u64 addr;
218 __s32 len;
219 __u16 bid;
220};
221
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222struct io_ring_ctx {
223 struct {
224 struct percpu_ref refs;
225 } ____cacheline_aligned_in_smp;
226
227 struct {
228 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800229 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700230 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800231 unsigned int cq_overflow_flushed: 1;
232 unsigned int drain_next: 1;
233 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Hristo Venev75b28af2019-08-26 17:23:46 +0000235 /*
236 * Ring buffer of indices into array of io_uring_sqe, which is
237 * mmapped by the application using the IORING_OFF_SQES offset.
238 *
239 * This indirection could e.g. be used to assign fixed
240 * io_uring_sqe entries to operations and only submit them to
241 * the queue when needed.
242 *
243 * The kernel modifies neither the indices array nor the entries
244 * array.
245 */
246 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700247 unsigned cached_sq_head;
248 unsigned sq_entries;
249 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700250 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600251 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700252 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700253 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600254
255 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600256 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700257 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258
Jens Axboefcb323c2019-10-24 12:39:47 -0600259 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700260 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261 } ____cacheline_aligned_in_smp;
262
Hristo Venev75b28af2019-08-26 17:23:46 +0000263 struct io_rings *rings;
264
Jens Axboe2b188cc2019-01-07 10:46:33 -0700265 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600266 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700267 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2aede0e2020-09-14 10:45:53 -0600268
269 /*
270 * For SQPOLL usage - we hold a reference to the parent task, so we
271 * have access to the ->files
272 */
273 struct task_struct *sqo_task;
274
275 /* Only used for accounting purposes */
276 struct mm_struct *mm_account;
277
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279
Jens Axboe6b063142019-01-10 22:13:58 -0700280 /*
281 * If used, fixed file set. Writers must ensure that ->refs is dead,
282 * readers must ensure that ->refs is alive as long as the file* is
283 * used. Only updated through io_uring_register(2).
284 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700285 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700286 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300287 int ring_fd;
288 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700289
Jens Axboeedafcce2019-01-09 09:16:05 -0700290 /* if used, fixed mapped user buffers */
291 unsigned nr_user_bufs;
292 struct io_mapped_ubuf *user_bufs;
293
Jens Axboe2b188cc2019-01-07 10:46:33 -0700294 struct user_struct *user;
295
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700296 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700297
Jens Axboe0f158b42020-05-14 17:18:39 -0600298 struct completion ref_comp;
299 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700300
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700301 /* if all else fails... */
302 struct io_kiocb *fallback_req;
303
Jens Axboe206aefd2019-11-07 18:27:42 -0700304#if defined(CONFIG_UNIX)
305 struct socket *ring_sock;
306#endif
307
Jens Axboe5a2e7452020-02-23 16:23:11 -0700308 struct idr io_buffer_idr;
309
Jens Axboe071698e2020-01-28 10:04:42 -0700310 struct idr personality_idr;
311
Jens Axboe206aefd2019-11-07 18:27:42 -0700312 struct {
313 unsigned cached_cq_tail;
314 unsigned cq_entries;
315 unsigned cq_mask;
316 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700317 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700318 struct wait_queue_head cq_wait;
319 struct fasync_struct *cq_fasync;
320 struct eventfd_ctx *cq_ev_fd;
321 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700322
323 struct {
324 struct mutex uring_lock;
325 wait_queue_head_t wait;
326 } ____cacheline_aligned_in_smp;
327
328 struct {
329 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700330
Jens Axboedef596e2019-01-09 08:59:42 -0700331 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300332 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700333 * io_uring instances that don't use IORING_SETUP_SQPOLL.
334 * For SQPOLL, only the single threaded io_sq_thread() will
335 * manipulate the list, hence no extra locking is needed there.
336 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300337 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700338 struct hlist_head *cancel_hash;
339 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700340 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600341
342 spinlock_t inflight_lock;
343 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600345
Jens Axboe4a38aed22020-05-14 17:21:15 -0600346 struct delayed_work file_put_work;
347 struct llist_head file_put_llist;
348
Jens Axboe85faa7b2020-04-09 18:14:00 -0600349 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700350};
351
Jens Axboe09bb8392019-03-13 12:39:28 -0600352/*
353 * First field must be the file pointer in all the
354 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
355 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700356struct io_poll_iocb {
357 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700358 union {
359 struct wait_queue_head *head;
360 u64 addr;
361 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700362 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600363 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700364 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700365 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700366};
367
Jens Axboeb5dba592019-12-11 14:02:38 -0700368struct io_close {
369 struct file *file;
370 struct file *put_file;
371 int fd;
372};
373
Jens Axboead8a48a2019-11-15 08:49:11 -0700374struct io_timeout_data {
375 struct io_kiocb *req;
376 struct hrtimer timer;
377 struct timespec64 ts;
378 enum hrtimer_mode mode;
379};
380
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700381struct io_accept {
382 struct file *file;
383 struct sockaddr __user *addr;
384 int __user *addr_len;
385 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600386 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700387};
388
389struct io_sync {
390 struct file *file;
391 loff_t len;
392 loff_t off;
393 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700394 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700395};
396
Jens Axboefbf23842019-12-17 18:45:56 -0700397struct io_cancel {
398 struct file *file;
399 u64 addr;
400};
401
Jens Axboeb29472e2019-12-17 18:50:29 -0700402struct io_timeout {
403 struct file *file;
404 u64 addr;
405 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300406 u32 off;
407 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300408 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700409};
410
Jens Axboe9adbd452019-12-20 08:45:55 -0700411struct io_rw {
412 /* NOTE: kiocb has the file as the first member, so don't do it here */
413 struct kiocb kiocb;
414 u64 addr;
415 u64 len;
416};
417
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700418struct io_connect {
419 struct file *file;
420 struct sockaddr __user *addr;
421 int addr_len;
422};
423
Jens Axboee47293f2019-12-20 08:58:21 -0700424struct io_sr_msg {
425 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700426 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300427 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700428 void __user *buf;
429 };
Jens Axboee47293f2019-12-20 08:58:21 -0700430 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700431 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700432 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700433 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700434};
435
Jens Axboe15b71ab2019-12-11 11:20:36 -0700436struct io_open {
437 struct file *file;
438 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700439 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700440 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600441 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700442};
443
Jens Axboe05f3fb32019-12-09 11:22:50 -0700444struct io_files_update {
445 struct file *file;
446 u64 arg;
447 u32 nr_args;
448 u32 offset;
449};
450
Jens Axboe4840e412019-12-25 22:03:45 -0700451struct io_fadvise {
452 struct file *file;
453 u64 offset;
454 u32 len;
455 u32 advice;
456};
457
Jens Axboec1ca7572019-12-25 22:18:28 -0700458struct io_madvise {
459 struct file *file;
460 u64 addr;
461 u32 len;
462 u32 advice;
463};
464
Jens Axboe3e4827b2020-01-08 15:18:09 -0700465struct io_epoll {
466 struct file *file;
467 int epfd;
468 int op;
469 int fd;
470 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700471};
472
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300473struct io_splice {
474 struct file *file_out;
475 struct file *file_in;
476 loff_t off_out;
477 loff_t off_in;
478 u64 len;
479 unsigned int flags;
480};
481
Jens Axboeddf0322d2020-02-23 16:41:33 -0700482struct io_provide_buf {
483 struct file *file;
484 __u64 addr;
485 __s32 len;
486 __u32 bgid;
487 __u16 nbufs;
488 __u16 bid;
489};
490
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700491struct io_statx {
492 struct file *file;
493 int dfd;
494 unsigned int mask;
495 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700496 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700497 struct statx __user *buffer;
498};
499
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300500struct io_completion {
501 struct file *file;
502 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300503 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300504};
505
Jens Axboef499a022019-12-02 16:28:46 -0700506struct io_async_connect {
507 struct sockaddr_storage address;
508};
509
Jens Axboe03b12302019-12-02 18:50:25 -0700510struct io_async_msghdr {
511 struct iovec fast_iov[UIO_FASTIOV];
512 struct iovec *iov;
513 struct sockaddr __user *uaddr;
514 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700515 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700516};
517
Jens Axboef67676d2019-12-02 11:03:47 -0700518struct io_async_rw {
519 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600520 const struct iovec *free_iovec;
521 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600522 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600523 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700524};
525
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700526struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700527 union {
528 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700529 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700530 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700531 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700532 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700533};
534
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300535enum {
536 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
537 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
538 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
539 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
540 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700541 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300542
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300543 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300544 REQ_F_FAIL_LINK_BIT,
545 REQ_F_INFLIGHT_BIT,
546 REQ_F_CUR_POS_BIT,
547 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300548 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300549 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300550 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300551 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700552 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700553 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600554 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800555 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300556 REQ_F_TASK_PINNED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700557
558 /* not a real bit, just to check we're not overflowing the space */
559 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300560};
561
562enum {
563 /* ctx owns file */
564 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
565 /* drain existing IO first */
566 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
567 /* linked sqes */
568 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
569 /* doesn't sever on completion < 0 */
570 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
571 /* IOSQE_ASYNC */
572 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700573 /* IOSQE_BUFFER_SELECT */
574 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300575
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300576 /* head of a link */
577 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300578 /* fail rest of links */
579 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
580 /* on inflight list */
581 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
582 /* read/write uses file position */
583 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
584 /* must not punt to workers */
585 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300586 /* has linked timeout */
587 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300588 /* regular file */
589 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300590 /* completion under lock */
591 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300592 /* needs cleanup */
593 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700594 /* already went through poll handler */
595 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700596 /* buffer already selected */
597 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600598 /* doesn't need file table for this request */
599 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800600 /* io_wq_work is initialized */
601 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300602 /* req->task is refcounted */
603 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700604};
605
606struct async_poll {
607 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600608 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300609};
610
Jens Axboe09bb8392019-03-13 12:39:28 -0600611/*
612 * NOTE! Each of the iocb union members has the file pointer
613 * as the first entry in their struct definition. So you can
614 * access the file pointer through any of the sub-structs,
615 * or directly as just 'ki_filp' in this struct.
616 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700617struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700618 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600619 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700620 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700621 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700622 struct io_accept accept;
623 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700624 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700625 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700626 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700627 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700628 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700629 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700630 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700631 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700632 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700633 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300634 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700635 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700636 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300637 /* use only after cleaning per-op data, see io_clean_op() */
638 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700639 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700640
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700641 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700642 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800643 /* polled IO has completed */
644 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700646 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300647 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700648
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300649 struct io_ring_ctx *ctx;
650 unsigned int flags;
651 refcount_t refs;
652 struct task_struct *task;
653 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700654
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300655 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700656
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300657 /*
658 * 1. used with ctx->iopoll_list with reads/writes
659 * 2. to track reqs with ->files (see io_op_def::file_table)
660 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300661 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600662
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300663 struct percpu_ref *fixed_file_refs;
664 struct callback_head task_work;
665 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
666 struct hlist_node hash_node;
667 struct async_poll *apoll;
668 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700669};
670
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300671struct io_defer_entry {
672 struct list_head list;
673 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300674 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300675};
676
Jens Axboedef596e2019-01-09 08:59:42 -0700677#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700678
Jens Axboe013538b2020-06-22 09:29:15 -0600679struct io_comp_state {
680 unsigned int nr;
681 struct list_head list;
682 struct io_ring_ctx *ctx;
683};
684
Jens Axboe9a56a232019-01-09 09:06:50 -0700685struct io_submit_state {
686 struct blk_plug plug;
687
688 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700689 * io_kiocb alloc cache
690 */
691 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300692 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700693
694 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600695 * Batch completion logic
696 */
697 struct io_comp_state comp;
698
699 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700700 * File reference cache
701 */
702 struct file *file;
703 unsigned int fd;
704 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700705 unsigned int ios_left;
706};
707
Jens Axboed3656342019-12-18 09:50:26 -0700708struct io_op_def {
709 /* needs req->io allocated for deferral/async */
710 unsigned async_ctx : 1;
711 /* needs current->mm setup, does mm access */
712 unsigned needs_mm : 1;
713 /* needs req->file assigned */
714 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600715 /* don't fail if file grab fails */
716 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700717 /* hash wq insertion if file is a regular file */
718 unsigned hash_reg_file : 1;
719 /* unbound wq insertion if file is a non-regular file */
720 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700721 /* opcode is not supported by this kernel */
722 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700723 /* needs file table */
724 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700725 /* needs ->fs */
726 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700727 /* set if opcode supports polled "wait" */
728 unsigned pollin : 1;
729 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700730 /* op supports buffer selection */
731 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300732 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700733};
734
735static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300736 [IORING_OP_NOP] = {},
737 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700738 .async_ctx = 1,
739 .needs_mm = 1,
740 .needs_file = 1,
741 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700742 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700743 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700744 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300745 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700746 .async_ctx = 1,
747 .needs_mm = 1,
748 .needs_file = 1,
749 .hash_reg_file = 1,
750 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700751 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300752 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700753 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300754 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700755 .needs_file = 1,
756 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300757 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700758 .needs_file = 1,
759 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700760 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700761 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300762 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700763 .needs_file = 1,
764 .hash_reg_file = 1,
765 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700766 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300767 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .needs_file = 1,
771 .unbound_nonreg_file = 1,
772 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300773 [IORING_OP_POLL_REMOVE] = {},
774 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_file = 1,
776 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300777 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700778 .async_ctx = 1,
779 .needs_mm = 1,
780 .needs_file = 1,
781 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700782 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700783 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700786 .async_ctx = 1,
787 .needs_mm = 1,
788 .needs_file = 1,
789 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700790 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700791 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700792 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700793 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300794 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700795 .async_ctx = 1,
796 .needs_mm = 1,
797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_TIMEOUT_REMOVE] = {},
799 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700800 .needs_mm = 1,
801 .needs_file = 1,
802 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700803 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700804 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700805 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300806 [IORING_OP_ASYNC_CANCEL] = {},
807 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700808 .async_ctx = 1,
809 .needs_mm = 1,
810 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300811 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700812 .async_ctx = 1,
813 .needs_mm = 1,
814 .needs_file = 1,
815 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700816 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700817 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300818 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700819 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300820 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700821 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300822 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700823 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700824 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700825 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300826 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600827 .needs_file = 1,
828 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700829 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700830 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300831 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700832 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700833 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700836 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700837 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600838 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700841 .needs_mm = 1,
842 .needs_file = 1,
843 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700844 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700845 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700846 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300847 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700848 .needs_mm = 1,
849 .needs_file = 1,
850 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700851 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300852 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700855 .needs_file = 1,
856 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300857 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700858 .needs_mm = 1,
859 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300860 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700861 .needs_mm = 1,
862 .needs_file = 1,
863 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700864 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700865 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300866 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700867 .needs_mm = 1,
868 .needs_file = 1,
869 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700870 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700871 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700872 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300873 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700874 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700875 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700876 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700877 [IORING_OP_EPOLL_CTL] = {
878 .unbound_nonreg_file = 1,
879 .file_table = 1,
880 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300881 [IORING_OP_SPLICE] = {
882 .needs_file = 1,
883 .hash_reg_file = 1,
884 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700885 },
886 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700887 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300888 [IORING_OP_TEE] = {
889 .needs_file = 1,
890 .hash_reg_file = 1,
891 .unbound_nonreg_file = 1,
892 },
Jens Axboed3656342019-12-18 09:50:26 -0700893};
894
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700895enum io_mem_account {
896 ACCT_LOCKED,
897 ACCT_PINNED,
898};
899
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300900static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
901 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700902static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800903static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600904static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700905static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700906static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600907static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700908static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700909static int __io_sqe_files_update(struct io_ring_ctx *ctx,
910 struct io_uring_files_update *ip,
911 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300912static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300913static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700914static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
915 int fd, struct file **out_file, bool fixed);
916static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600917 const struct io_uring_sqe *sqe,
918 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600919static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600920
Jens Axboeb63534c2020-06-04 11:28:00 -0600921static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
922 struct iovec **iovec, struct iov_iter *iter,
923 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600924static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
925 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600926 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700927
928static struct kmem_cache *req_cachep;
929
930static const struct file_operations io_uring_fops;
931
932struct sock *io_uring_get_socket(struct file *file)
933{
934#if defined(CONFIG_UNIX)
935 if (file->f_op == &io_uring_fops) {
936 struct io_ring_ctx *ctx = file->private_data;
937
938 return ctx->ring_sock->sk;
939 }
940#endif
941 return NULL;
942}
943EXPORT_SYMBOL(io_uring_get_socket);
944
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300945static void io_get_req_task(struct io_kiocb *req)
946{
947 if (req->flags & REQ_F_TASK_PINNED)
948 return;
949 get_task_struct(req->task);
950 req->flags |= REQ_F_TASK_PINNED;
951}
952
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300953static inline void io_clean_op(struct io_kiocb *req)
954{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300955 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
956 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300957 __io_clean_op(req);
958}
959
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300960/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
961static void __io_put_req_task(struct io_kiocb *req)
962{
963 if (req->flags & REQ_F_TASK_PINNED)
964 put_task_struct(req->task);
965}
966
Jens Axboe4349f302020-07-09 15:07:01 -0600967static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600968{
969 struct mm_struct *mm = current->mm;
970
971 if (mm) {
972 kthread_unuse_mm(mm);
973 mmput(mm);
974 }
975}
976
977static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
978{
979 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300980 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600981 !ctx->sqo_task->mm ||
982 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600983 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600984 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -0600985 }
986
987 return 0;
988}
989
990static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
991 struct io_kiocb *req)
992{
993 if (!io_op_defs[req->opcode].needs_mm)
994 return 0;
995 return __io_sq_thread_acquire_mm(ctx);
996}
997
998static inline void req_set_fail_links(struct io_kiocb *req)
999{
1000 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1001 req->flags |= REQ_F_FAIL_LINK;
1002}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001003
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001004/*
1005 * Note: must call io_req_init_async() for the first time you
1006 * touch any members of io_wq_work.
1007 */
1008static inline void io_req_init_async(struct io_kiocb *req)
1009{
1010 if (req->flags & REQ_F_WORK_INITIALIZED)
1011 return;
1012
1013 memset(&req->work, 0, sizeof(req->work));
1014 req->flags |= REQ_F_WORK_INITIALIZED;
1015}
1016
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001017static inline bool io_async_submit(struct io_ring_ctx *ctx)
1018{
1019 return ctx->flags & IORING_SETUP_SQPOLL;
1020}
1021
Jens Axboe2b188cc2019-01-07 10:46:33 -07001022static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1023{
1024 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1025
Jens Axboe0f158b42020-05-14 17:18:39 -06001026 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001027}
1028
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001029static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1030{
1031 return !req->timeout.off;
1032}
1033
Jens Axboe2b188cc2019-01-07 10:46:33 -07001034static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1035{
1036 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001037 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001038
1039 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1040 if (!ctx)
1041 return NULL;
1042
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001043 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1044 if (!ctx->fallback_req)
1045 goto err;
1046
Jens Axboe78076bb2019-12-04 19:56:40 -07001047 /*
1048 * Use 5 bits less than the max cq entries, that should give us around
1049 * 32 entries per hash list if totally full and uniformly spread.
1050 */
1051 hash_bits = ilog2(p->cq_entries);
1052 hash_bits -= 5;
1053 if (hash_bits <= 0)
1054 hash_bits = 1;
1055 ctx->cancel_hash_bits = hash_bits;
1056 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1057 GFP_KERNEL);
1058 if (!ctx->cancel_hash)
1059 goto err;
1060 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1061
Roman Gushchin21482892019-05-07 10:01:48 -07001062 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001063 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1064 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001065
1066 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001067 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001068 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001069 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001070 init_completion(&ctx->ref_comp);
1071 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001072 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001073 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074 mutex_init(&ctx->uring_lock);
1075 init_waitqueue_head(&ctx->wait);
1076 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001077 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001078 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001079 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001080 init_waitqueue_head(&ctx->inflight_wait);
1081 spin_lock_init(&ctx->inflight_lock);
1082 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001083 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1084 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001085 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001086err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001087 if (ctx->fallback_req)
1088 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001089 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001090 kfree(ctx);
1091 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001092}
1093
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001094static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001095{
Jens Axboe2bc99302020-07-09 09:43:27 -06001096 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1097 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001098
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001099 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001100 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001101 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001102
Bob Liu9d858b22019-11-13 18:06:25 +08001103 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001104}
1105
Jens Axboede0617e2019-04-06 21:51:27 -06001106static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001107{
Hristo Venev75b28af2019-08-26 17:23:46 +00001108 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109
Pavel Begunkov07910152020-01-17 03:52:46 +03001110 /* order cqe stores with ring update */
1111 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001112
Pavel Begunkov07910152020-01-17 03:52:46 +03001113 if (wq_has_sleeper(&ctx->cq_wait)) {
1114 wake_up_interruptible(&ctx->cq_wait);
1115 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001116 }
1117}
1118
Jens Axboe51a4cc12020-08-10 10:55:56 -06001119/*
1120 * Returns true if we need to defer file table putting. This can only happen
1121 * from the error path with REQ_F_COMP_LOCKED set.
1122 */
1123static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001124{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001125 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001126 return false;
1127
1128 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001129
Jens Axboecccf0ee2020-01-27 16:34:48 -07001130 if (req->work.mm) {
1131 mmdrop(req->work.mm);
1132 req->work.mm = NULL;
1133 }
1134 if (req->work.creds) {
1135 put_cred(req->work.creds);
1136 req->work.creds = NULL;
1137 }
Jens Axboeff002b32020-02-07 16:05:21 -07001138 if (req->work.fs) {
1139 struct fs_struct *fs = req->work.fs;
1140
Jens Axboe51a4cc12020-08-10 10:55:56 -06001141 if (req->flags & REQ_F_COMP_LOCKED)
1142 return true;
1143
Jens Axboeff002b32020-02-07 16:05:21 -07001144 spin_lock(&req->work.fs->lock);
1145 if (--fs->users)
1146 fs = NULL;
1147 spin_unlock(&req->work.fs->lock);
1148 if (fs)
1149 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001150 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001151 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001152
1153 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001154}
1155
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001156static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001157{
Jens Axboed3656342019-12-18 09:50:26 -07001158 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001159
Pavel Begunkov16d59802020-07-12 16:16:47 +03001160 io_req_init_async(req);
1161
Jens Axboed3656342019-12-18 09:50:26 -07001162 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001163 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001164 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001165 } else {
1166 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001167 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001168 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001169 if (!req->work.mm && def->needs_mm) {
1170 mmgrab(current->mm);
1171 req->work.mm = current->mm;
1172 }
1173 if (!req->work.creds)
1174 req->work.creds = get_current_cred();
1175 if (!req->work.fs && def->needs_fs) {
1176 spin_lock(&current->fs->lock);
1177 if (!current->fs->in_exec) {
1178 req->work.fs = current->fs;
1179 req->work.fs->users++;
1180 } else {
1181 req->work.flags |= IO_WQ_WORK_CANCEL;
1182 }
1183 spin_unlock(&current->fs->lock);
1184 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001185 if (def->needs_fsize)
1186 req->work.fsize = rlimit(RLIMIT_FSIZE);
1187 else
1188 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001189}
1190
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001191static void io_prep_async_link(struct io_kiocb *req)
1192{
1193 struct io_kiocb *cur;
1194
1195 io_prep_async_work(req);
1196 if (req->flags & REQ_F_LINK_HEAD)
1197 list_for_each_entry(cur, &req->link_list, link_list)
1198 io_prep_async_work(cur);
1199}
1200
Jens Axboe7271ef32020-08-10 09:55:22 -06001201static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001202{
Jackie Liua197f662019-11-08 08:09:12 -07001203 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001204 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001205
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001206 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1207 &req->work, req->flags);
1208 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001209 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001210}
1211
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001212static void io_queue_async_work(struct io_kiocb *req)
1213{
Jens Axboe7271ef32020-08-10 09:55:22 -06001214 struct io_kiocb *link;
1215
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001216 /* init ->work of the whole link before punting */
1217 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001218 link = __io_queue_async_work(req);
1219
1220 if (link)
1221 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001222}
1223
Jens Axboe5262f562019-09-17 12:26:57 -06001224static void io_kill_timeout(struct io_kiocb *req)
1225{
1226 int ret;
1227
Jens Axboe2d283902019-12-04 11:08:05 -07001228 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001229 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001230 atomic_set(&req->ctx->cq_timeouts,
1231 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001232 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001233 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001234 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001235 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001236 }
1237}
1238
Jens Axboef3606e32020-09-22 08:18:24 -06001239static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1240{
1241 struct io_ring_ctx *ctx = req->ctx;
1242
1243 if (!tsk || req->task == tsk)
1244 return true;
1245 if ((ctx->flags & IORING_SETUP_SQPOLL) && req->task == ctx->sqo_thread)
1246 return true;
1247 return false;
1248}
1249
1250static void io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001251{
1252 struct io_kiocb *req, *tmp;
1253
1254 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001255 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
1256 if (io_task_match(req, tsk))
1257 io_kill_timeout(req);
1258 }
Jens Axboe5262f562019-09-17 12:26:57 -06001259 spin_unlock_irq(&ctx->completion_lock);
1260}
1261
Pavel Begunkov04518942020-05-26 20:34:05 +03001262static void __io_queue_deferred(struct io_ring_ctx *ctx)
1263{
1264 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001265 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1266 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001267 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001268
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001269 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001270 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001271 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001272 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001273 link = __io_queue_async_work(de->req);
1274 if (link) {
1275 __io_queue_linked_timeout(link);
1276 /* drop submission reference */
1277 link->flags |= REQ_F_COMP_LOCKED;
1278 io_put_req(link);
1279 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001280 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001281 } while (!list_empty(&ctx->defer_list));
1282}
1283
Pavel Begunkov360428f2020-05-30 14:54:17 +03001284static void io_flush_timeouts(struct io_ring_ctx *ctx)
1285{
1286 while (!list_empty(&ctx->timeout_list)) {
1287 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001288 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001289
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001290 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001291 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001292 if (req->timeout.target_seq != ctx->cached_cq_tail
1293 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001294 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001295
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001296 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001297 io_kill_timeout(req);
1298 }
1299}
1300
Jens Axboede0617e2019-04-06 21:51:27 -06001301static void io_commit_cqring(struct io_ring_ctx *ctx)
1302{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001303 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001304 __io_commit_cqring(ctx);
1305
Pavel Begunkov04518942020-05-26 20:34:05 +03001306 if (unlikely(!list_empty(&ctx->defer_list)))
1307 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001308}
1309
Jens Axboe2b188cc2019-01-07 10:46:33 -07001310static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1311{
Hristo Venev75b28af2019-08-26 17:23:46 +00001312 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001313 unsigned tail;
1314
1315 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001316 /*
1317 * writes to the cq entry need to come after reading head; the
1318 * control dependency is enough as we're using WRITE_ONCE to
1319 * fill the cq entry
1320 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001321 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001322 return NULL;
1323
1324 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001325 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001326}
1327
Jens Axboef2842ab2020-01-08 11:04:00 -07001328static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1329{
Jens Axboef0b493e2020-02-01 21:30:11 -07001330 if (!ctx->cq_ev_fd)
1331 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001332 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1333 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001334 if (!ctx->eventfd_async)
1335 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001336 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001337}
1338
Jens Axboeb41e9852020-02-17 09:52:41 -07001339static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001340{
1341 if (waitqueue_active(&ctx->wait))
1342 wake_up(&ctx->wait);
1343 if (waitqueue_active(&ctx->sqo_wait))
1344 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001345 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001346 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001347}
1348
Pavel Begunkov46930142020-07-30 18:43:49 +03001349static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1350{
1351 if (list_empty(&ctx->cq_overflow_list)) {
1352 clear_bit(0, &ctx->sq_check_overflow);
1353 clear_bit(0, &ctx->cq_check_overflow);
1354 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1355 }
1356}
1357
Jens Axboec4a2ed72019-11-21 21:01:26 -07001358/* Returns true if there are no backlogged entries after the flush */
1359static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001360{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001361 struct io_rings *rings = ctx->rings;
1362 struct io_uring_cqe *cqe;
1363 struct io_kiocb *req;
1364 unsigned long flags;
1365 LIST_HEAD(list);
1366
1367 if (!force) {
1368 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001369 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001370 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1371 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001372 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001373 }
1374
1375 spin_lock_irqsave(&ctx->completion_lock, flags);
1376
1377 /* if force is set, the ring is going away. always drop after that */
1378 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001379 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001380
Jens Axboec4a2ed72019-11-21 21:01:26 -07001381 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001382 while (!list_empty(&ctx->cq_overflow_list)) {
1383 cqe = io_get_cqring(ctx);
1384 if (!cqe && !force)
1385 break;
1386
1387 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001388 compl.list);
1389 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001390 if (cqe) {
1391 WRITE_ONCE(cqe->user_data, req->user_data);
1392 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001393 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001394 } else {
1395 WRITE_ONCE(ctx->rings->cq_overflow,
1396 atomic_inc_return(&ctx->cached_cq_overflow));
1397 }
1398 }
1399
1400 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001401 io_cqring_mark_overflow(ctx);
1402
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001403 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1404 io_cqring_ev_posted(ctx);
1405
1406 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001407 req = list_first_entry(&list, struct io_kiocb, compl.list);
1408 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001409 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001410 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001411
1412 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001413}
1414
Jens Axboebcda7ba2020-02-23 16:42:51 -07001415static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001416{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001417 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001418 struct io_uring_cqe *cqe;
1419
Jens Axboe78e19bb2019-11-06 15:21:34 -07001420 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001421
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422 /*
1423 * If we can't get a cq entry, userspace overflowed the
1424 * submission (by quite a lot). Increment the overflow count in
1425 * the ring.
1426 */
1427 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001428 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001429 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001430 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001431 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001432 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001433 WRITE_ONCE(ctx->rings->cq_overflow,
1434 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001435 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001436 if (list_empty(&ctx->cq_overflow_list)) {
1437 set_bit(0, &ctx->sq_check_overflow);
1438 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001439 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001440 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001441 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001442 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001443 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001444 refcount_inc(&req->refs);
1445 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001446 }
1447}
1448
Jens Axboebcda7ba2020-02-23 16:42:51 -07001449static void io_cqring_fill_event(struct io_kiocb *req, long res)
1450{
1451 __io_cqring_fill_event(req, res, 0);
1452}
1453
Jens Axboee1e16092020-06-22 09:17:17 -06001454static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001455{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001456 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001457 unsigned long flags;
1458
1459 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001460 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001461 io_commit_cqring(ctx);
1462 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1463
Jens Axboe8c838782019-03-12 15:48:16 -06001464 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465}
1466
Jens Axboe229a7b62020-06-22 10:13:11 -06001467static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001468{
Jens Axboe229a7b62020-06-22 10:13:11 -06001469 struct io_ring_ctx *ctx = cs->ctx;
1470
1471 spin_lock_irq(&ctx->completion_lock);
1472 while (!list_empty(&cs->list)) {
1473 struct io_kiocb *req;
1474
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001475 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1476 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001477 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001478 if (!(req->flags & REQ_F_LINK_HEAD)) {
1479 req->flags |= REQ_F_COMP_LOCKED;
1480 io_put_req(req);
1481 } else {
1482 spin_unlock_irq(&ctx->completion_lock);
1483 io_put_req(req);
1484 spin_lock_irq(&ctx->completion_lock);
1485 }
1486 }
1487 io_commit_cqring(ctx);
1488 spin_unlock_irq(&ctx->completion_lock);
1489
1490 io_cqring_ev_posted(ctx);
1491 cs->nr = 0;
1492}
1493
1494static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1495 struct io_comp_state *cs)
1496{
1497 if (!cs) {
1498 io_cqring_add_event(req, res, cflags);
1499 io_put_req(req);
1500 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001501 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001502 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001503 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001504 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001505 if (++cs->nr >= 32)
1506 io_submit_flush_completions(cs);
1507 }
Jens Axboee1e16092020-06-22 09:17:17 -06001508}
1509
1510static void io_req_complete(struct io_kiocb *req, long res)
1511{
Jens Axboe229a7b62020-06-22 10:13:11 -06001512 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001513}
1514
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001515static inline bool io_is_fallback_req(struct io_kiocb *req)
1516{
1517 return req == (struct io_kiocb *)
1518 ((unsigned long) req->ctx->fallback_req & ~1UL);
1519}
1520
1521static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1522{
1523 struct io_kiocb *req;
1524
1525 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001526 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001527 return req;
1528
1529 return NULL;
1530}
1531
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001532static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1533 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001534{
Jens Axboefd6fab22019-03-14 16:30:06 -06001535 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001536 struct io_kiocb *req;
1537
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001538 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001539 size_t sz;
1540 int ret;
1541
1542 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001543 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1544
1545 /*
1546 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1547 * retry single alloc to be on the safe side.
1548 */
1549 if (unlikely(ret <= 0)) {
1550 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1551 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001552 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001553 ret = 1;
1554 }
Jens Axboe2579f912019-01-09 09:10:43 -07001555 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001556 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001557 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001558 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001559 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001560 }
1561
Jens Axboe2579f912019-01-09 09:10:43 -07001562 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001563fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001564 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001565}
1566
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001567static inline void io_put_file(struct io_kiocb *req, struct file *file,
1568 bool fixed)
1569{
1570 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001571 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001572 else
1573 fput(file);
1574}
1575
Jens Axboe51a4cc12020-08-10 10:55:56 -06001576static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001577{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001578 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001579
Jens Axboe5acbbc82020-07-08 15:15:26 -06001580 if (req->io)
1581 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001582 if (req->file)
1583 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001584
Jens Axboe51a4cc12020-08-10 10:55:56 -06001585 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001586}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001587
Jens Axboe51a4cc12020-08-10 10:55:56 -06001588static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001589{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001590 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001591
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001592 __io_put_req_task(req);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001593 if (likely(!io_is_fallback_req(req)))
1594 kmem_cache_free(req_cachep, req);
1595 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001596 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1597 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001598}
1599
Jens Axboe51a4cc12020-08-10 10:55:56 -06001600static void io_req_task_file_table_put(struct callback_head *cb)
1601{
1602 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1603 struct fs_struct *fs = req->work.fs;
1604
1605 spin_lock(&req->work.fs->lock);
1606 if (--fs->users)
1607 fs = NULL;
1608 spin_unlock(&req->work.fs->lock);
1609 if (fs)
1610 free_fs_struct(fs);
1611 req->work.fs = NULL;
1612 __io_free_req_finish(req);
1613}
1614
1615static void __io_free_req(struct io_kiocb *req)
1616{
1617 if (!io_dismantle_req(req)) {
1618 __io_free_req_finish(req);
1619 } else {
1620 int ret;
1621
1622 init_task_work(&req->task_work, io_req_task_file_table_put);
1623 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1624 if (unlikely(ret)) {
1625 struct task_struct *tsk;
1626
1627 tsk = io_wq_get_task(req->ctx->io_wq);
1628 task_work_add(tsk, &req->task_work, 0);
1629 }
1630 }
1631}
1632
Jackie Liua197f662019-11-08 08:09:12 -07001633static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001634{
Jackie Liua197f662019-11-08 08:09:12 -07001635 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001636 int ret;
1637
Jens Axboe2d283902019-12-04 11:08:05 -07001638 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001639 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001640 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001641 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001642 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001643 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001644 return true;
1645 }
1646
1647 return false;
1648}
1649
Jens Axboeab0b6452020-06-30 08:43:15 -06001650static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001651{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001652 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001653 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001654
1655 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001656 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001657 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1658 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001659 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001660
1661 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001662 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001663 wake_ev = io_link_cancel_timeout(link);
1664 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001665 return wake_ev;
1666}
1667
1668static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001669{
Jens Axboe2665abf2019-11-05 12:40:47 -07001670 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001671 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001672
Jens Axboeab0b6452020-06-30 08:43:15 -06001673 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1674 unsigned long flags;
1675
1676 spin_lock_irqsave(&ctx->completion_lock, flags);
1677 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001678 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001679 } else {
1680 wake_ev = __io_kill_linked_timeout(req);
1681 }
1682
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001683 if (wake_ev)
1684 io_cqring_ev_posted(ctx);
1685}
1686
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001687static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001688{
1689 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001690
Jens Axboe9e645e112019-05-10 16:07:28 -06001691 /*
1692 * The list should never be empty when we are called here. But could
1693 * potentially happen if the chain is messed up, check to be on the
1694 * safe side.
1695 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001696 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001697 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001698
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001699 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1700 list_del_init(&req->link_list);
1701 if (!list_empty(&nxt->link_list))
1702 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001703 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001704}
1705
1706/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001707 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001708 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001709static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001710{
Jens Axboe2665abf2019-11-05 12:40:47 -07001711 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001712
1713 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001714 struct io_kiocb *link = list_first_entry(&req->link_list,
1715 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001716
Pavel Begunkov44932332019-12-05 16:16:35 +03001717 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001718 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001719
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001720 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001721 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001722 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001723 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001724 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001725
1726 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001727 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001728}
1729
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001730static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001731{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001732 struct io_ring_ctx *ctx = req->ctx;
1733
1734 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1735 unsigned long flags;
1736
1737 spin_lock_irqsave(&ctx->completion_lock, flags);
1738 __io_fail_links(req);
1739 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1740 } else {
1741 __io_fail_links(req);
1742 }
1743
Jens Axboe9e645e112019-05-10 16:07:28 -06001744 io_cqring_ev_posted(ctx);
1745}
1746
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001747static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001748{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001749 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001750 if (req->flags & REQ_F_LINK_TIMEOUT)
1751 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001752
Jens Axboe9e645e112019-05-10 16:07:28 -06001753 /*
1754 * If LINK is set, we have dependent requests in this chain. If we
1755 * didn't fail this request, queue the first one up, moving any other
1756 * dependencies to the next request. In case of failure, fail the rest
1757 * of the chain.
1758 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001759 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1760 return io_req_link_next(req);
1761 io_fail_links(req);
1762 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001763}
Jens Axboe2665abf2019-11-05 12:40:47 -07001764
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001765static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1766{
1767 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1768 return NULL;
1769 return __io_req_find_next(req);
1770}
1771
Jens Axboefd7d6de2020-08-23 11:00:37 -06001772static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1773 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001774{
1775 struct task_struct *tsk = req->task;
1776 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001777 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001778
Jens Axboe6200b0a2020-09-13 14:38:30 -06001779 if (tsk->flags & PF_EXITING)
1780 return -ESRCH;
1781
Jens Axboec2c4c832020-07-01 15:37:11 -06001782 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001783 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1784 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1785 * processing task_work. There's no reliable way to tell if TWA_RESUME
1786 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001787 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001788 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001789 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001790 notify = TWA_SIGNAL;
1791
1792 ret = task_work_add(tsk, cb, notify);
1793 if (!ret)
1794 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001795
Jens Axboec2c4c832020-07-01 15:37:11 -06001796 return ret;
1797}
1798
Jens Axboec40f6372020-06-25 15:39:59 -06001799static void __io_req_task_cancel(struct io_kiocb *req, int error)
1800{
1801 struct io_ring_ctx *ctx = req->ctx;
1802
1803 spin_lock_irq(&ctx->completion_lock);
1804 io_cqring_fill_event(req, error);
1805 io_commit_cqring(ctx);
1806 spin_unlock_irq(&ctx->completion_lock);
1807
1808 io_cqring_ev_posted(ctx);
1809 req_set_fail_links(req);
1810 io_double_put_req(req);
1811}
1812
1813static void io_req_task_cancel(struct callback_head *cb)
1814{
1815 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001816 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001817
1818 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001819 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001820}
1821
1822static void __io_req_task_submit(struct io_kiocb *req)
1823{
1824 struct io_ring_ctx *ctx = req->ctx;
1825
Jens Axboec40f6372020-06-25 15:39:59 -06001826 if (!__io_sq_thread_acquire_mm(ctx)) {
1827 mutex_lock(&ctx->uring_lock);
1828 __io_queue_sqe(req, NULL, NULL);
1829 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001830 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001831 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001832 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001833}
1834
Jens Axboec40f6372020-06-25 15:39:59 -06001835static void io_req_task_submit(struct callback_head *cb)
1836{
1837 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001838 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001839
1840 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001841 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001842}
1843
1844static void io_req_task_queue(struct io_kiocb *req)
1845{
Jens Axboec40f6372020-06-25 15:39:59 -06001846 int ret;
1847
1848 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001849 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001850
Jens Axboefd7d6de2020-08-23 11:00:37 -06001851 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001852 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001853 struct task_struct *tsk;
1854
Jens Axboec40f6372020-06-25 15:39:59 -06001855 init_task_work(&req->task_work, io_req_task_cancel);
1856 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001857 task_work_add(tsk, &req->task_work, 0);
1858 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001859 }
Jens Axboec40f6372020-06-25 15:39:59 -06001860}
1861
Pavel Begunkovc3524382020-06-28 12:52:32 +03001862static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001863{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001864 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001865
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001866 if (nxt)
1867 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001868}
1869
Jens Axboe9e645e112019-05-10 16:07:28 -06001870static void io_free_req(struct io_kiocb *req)
1871{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001872 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001873 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001874}
1875
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001876struct req_batch {
1877 void *reqs[IO_IOPOLL_BATCH];
1878 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001879
1880 struct task_struct *task;
1881 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001882};
1883
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001884static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001885{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001886 rb->to_free = 0;
1887 rb->task_refs = 0;
1888 rb->task = NULL;
1889}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001890
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001891static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1892 struct req_batch *rb)
1893{
1894 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1895 percpu_ref_put_many(&ctx->refs, rb->to_free);
1896 rb->to_free = 0;
1897}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001898
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001899static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1900 struct req_batch *rb)
1901{
1902 if (rb->to_free)
1903 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001904 if (rb->task) {
1905 put_task_struct_many(rb->task, rb->task_refs);
1906 rb->task = NULL;
1907 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001908}
1909
1910static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1911{
1912 if (unlikely(io_is_fallback_req(req))) {
1913 io_free_req(req);
1914 return;
1915 }
1916 if (req->flags & REQ_F_LINK_HEAD)
1917 io_queue_next(req);
1918
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001919 if (req->flags & REQ_F_TASK_PINNED) {
1920 if (req->task != rb->task) {
1921 if (rb->task)
1922 put_task_struct_many(rb->task, rb->task_refs);
1923 rb->task = req->task;
1924 rb->task_refs = 0;
1925 }
1926 rb->task_refs++;
1927 req->flags &= ~REQ_F_TASK_PINNED;
1928 }
1929
Jens Axboe51a4cc12020-08-10 10:55:56 -06001930 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001931 rb->reqs[rb->to_free++] = req;
1932 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1933 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001934}
1935
Jens Axboeba816ad2019-09-28 11:36:45 -06001936/*
1937 * Drop reference to request, return next in chain (if there is one) if this
1938 * was the last reference to this request.
1939 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001940static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001941{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001942 struct io_kiocb *nxt = NULL;
1943
Jens Axboe2a44f462020-02-25 13:25:41 -07001944 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001945 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001946 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001947 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001948 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001949}
1950
Jens Axboe2b188cc2019-01-07 10:46:33 -07001951static void io_put_req(struct io_kiocb *req)
1952{
Jens Axboedef596e2019-01-09 08:59:42 -07001953 if (refcount_dec_and_test(&req->refs))
1954 io_free_req(req);
1955}
1956
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001957static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001958{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001959 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001960
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001961 /*
1962 * A ref is owned by io-wq in which context we're. So, if that's the
1963 * last one, it's safe to steal next work. False negatives are Ok,
1964 * it just will be re-punted async in io_put_work()
1965 */
1966 if (refcount_read(&req->refs) != 1)
1967 return NULL;
1968
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03001969 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001970 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001971}
1972
Jens Axboe978db572019-11-14 22:39:04 -07001973/*
1974 * Must only be used if we don't need to care about links, usually from
1975 * within the completion handling itself.
1976 */
1977static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001978{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001979 /* drop both submit and complete references */
1980 if (refcount_sub_and_test(2, &req->refs))
1981 __io_free_req(req);
1982}
1983
Jens Axboe978db572019-11-14 22:39:04 -07001984static void io_double_put_req(struct io_kiocb *req)
1985{
1986 /* drop both submit and complete references */
1987 if (refcount_sub_and_test(2, &req->refs))
1988 io_free_req(req);
1989}
1990
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001991static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001992{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001993 struct io_rings *rings = ctx->rings;
1994
Jens Axboead3eb2c2019-12-18 17:12:20 -07001995 if (test_bit(0, &ctx->cq_check_overflow)) {
1996 /*
1997 * noflush == true is from the waitqueue handler, just ensure
1998 * we wake up the task, and the next invocation will flush the
1999 * entries. We cannot safely to it from here.
2000 */
2001 if (noflush && !list_empty(&ctx->cq_overflow_list))
2002 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002003
Jens Axboead3eb2c2019-12-18 17:12:20 -07002004 io_cqring_overflow_flush(ctx, false);
2005 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002006
Jens Axboea3a0e432019-08-20 11:03:11 -06002007 /* See comment at the top of this file */
2008 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07002009 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06002010}
2011
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002012static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2013{
2014 struct io_rings *rings = ctx->rings;
2015
2016 /* make sure SQ entry isn't read before tail */
2017 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2018}
2019
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002020static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002021{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002022 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002023
Jens Axboebcda7ba2020-02-23 16:42:51 -07002024 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2025 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002026 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002027 kfree(kbuf);
2028 return cflags;
2029}
2030
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002031static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2032{
2033 struct io_buffer *kbuf;
2034
2035 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2036 return io_put_kbuf(req, kbuf);
2037}
2038
Jens Axboe4c6e2772020-07-01 11:29:10 -06002039static inline bool io_run_task_work(void)
2040{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002041 /*
2042 * Not safe to run on exiting task, and the task_work handling will
2043 * not add work to such a task.
2044 */
2045 if (unlikely(current->flags & PF_EXITING))
2046 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002047 if (current->task_works) {
2048 __set_current_state(TASK_RUNNING);
2049 task_work_run();
2050 return true;
2051 }
2052
2053 return false;
2054}
2055
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002056static void io_iopoll_queue(struct list_head *again)
2057{
2058 struct io_kiocb *req;
2059
2060 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002061 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2062 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002063 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002064 } while (!list_empty(again));
2065}
2066
Jens Axboedef596e2019-01-09 08:59:42 -07002067/*
2068 * Find and free completed poll iocbs
2069 */
2070static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2071 struct list_head *done)
2072{
Jens Axboe8237e042019-12-28 10:48:22 -07002073 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002074 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002075 LIST_HEAD(again);
2076
2077 /* order with ->result store in io_complete_rw_iopoll() */
2078 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002079
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002080 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002081 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002082 int cflags = 0;
2083
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002084 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002085 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002086 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002087 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002088 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002089 continue;
2090 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002091 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002092
Jens Axboebcda7ba2020-02-23 16:42:51 -07002093 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002094 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002095
2096 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002097 (*nr_events)++;
2098
Pavel Begunkovc3524382020-06-28 12:52:32 +03002099 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002100 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002101 }
Jens Axboedef596e2019-01-09 08:59:42 -07002102
Jens Axboe09bb8392019-03-13 12:39:28 -06002103 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002104 if (ctx->flags & IORING_SETUP_SQPOLL)
2105 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002106 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002107
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002108 if (!list_empty(&again))
2109 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002110}
2111
Jens Axboedef596e2019-01-09 08:59:42 -07002112static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2113 long min)
2114{
2115 struct io_kiocb *req, *tmp;
2116 LIST_HEAD(done);
2117 bool spin;
2118 int ret;
2119
2120 /*
2121 * Only spin for completions if we don't have multiple devices hanging
2122 * off our complete list, and we're under the requested amount.
2123 */
2124 spin = !ctx->poll_multi_file && *nr_events < min;
2125
2126 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002127 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002128 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002129
2130 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002131 * Move completed and retryable entries to our local lists.
2132 * If we find a request that requires polling, break out
2133 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002134 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002135 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002136 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002137 continue;
2138 }
2139 if (!list_empty(&done))
2140 break;
2141
2142 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2143 if (ret < 0)
2144 break;
2145
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002146 /* iopoll may have completed current req */
2147 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002148 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002149
Jens Axboedef596e2019-01-09 08:59:42 -07002150 if (ret && spin)
2151 spin = false;
2152 ret = 0;
2153 }
2154
2155 if (!list_empty(&done))
2156 io_iopoll_complete(ctx, nr_events, &done);
2157
2158 return ret;
2159}
2160
2161/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002162 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002163 * non-spinning poll check - we'll still enter the driver poll loop, but only
2164 * as a non-spinning completion check.
2165 */
2166static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2167 long min)
2168{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002169 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002170 int ret;
2171
2172 ret = io_do_iopoll(ctx, nr_events, min);
2173 if (ret < 0)
2174 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002175 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002176 return 0;
2177 }
2178
2179 return 1;
2180}
2181
2182/*
2183 * We can't just wait for polled events to come to us, we have to actively
2184 * find and complete them.
2185 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002186static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002187{
2188 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2189 return;
2190
2191 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002192 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002193 unsigned int nr_events = 0;
2194
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002195 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002196
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002197 /* let it sleep and repeat later if can't complete a request */
2198 if (nr_events == 0)
2199 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002200 /*
2201 * Ensure we allow local-to-the-cpu processing to take place,
2202 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002203 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002204 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002205 if (need_resched()) {
2206 mutex_unlock(&ctx->uring_lock);
2207 cond_resched();
2208 mutex_lock(&ctx->uring_lock);
2209 }
Jens Axboedef596e2019-01-09 08:59:42 -07002210 }
2211 mutex_unlock(&ctx->uring_lock);
2212}
2213
Pavel Begunkov7668b922020-07-07 16:36:21 +03002214static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002215{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002216 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002217 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002218
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002219 /*
2220 * We disallow the app entering submit/complete with polling, but we
2221 * still need to lock the ring to prevent racing with polled issue
2222 * that got punted to a workqueue.
2223 */
2224 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002225 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002226 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002227 * Don't enter poll loop if we already have events pending.
2228 * If we do, we can potentially be spinning for commands that
2229 * already triggered a CQE (eg in error).
2230 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002231 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002232 break;
2233
2234 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002235 * If a submit got punted to a workqueue, we can have the
2236 * application entering polling for a command before it gets
2237 * issued. That app will hold the uring_lock for the duration
2238 * of the poll right here, so we need to take a breather every
2239 * now and then to ensure that the issue has a chance to add
2240 * the poll to the issued list. Otherwise we can spin here
2241 * forever, while the workqueue is stuck trying to acquire the
2242 * very same mutex.
2243 */
2244 if (!(++iters & 7)) {
2245 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002246 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002247 mutex_lock(&ctx->uring_lock);
2248 }
2249
Pavel Begunkov7668b922020-07-07 16:36:21 +03002250 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002251 if (ret <= 0)
2252 break;
2253 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002254 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002255
Jens Axboe500f9fb2019-08-19 12:15:59 -06002256 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002257 return ret;
2258}
2259
Jens Axboe491381ce2019-10-17 09:20:46 -06002260static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002261{
Jens Axboe491381ce2019-10-17 09:20:46 -06002262 /*
2263 * Tell lockdep we inherited freeze protection from submission
2264 * thread.
2265 */
2266 if (req->flags & REQ_F_ISREG) {
2267 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002268
Jens Axboe491381ce2019-10-17 09:20:46 -06002269 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002270 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002271 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002272}
2273
Jens Axboea1d7c392020-06-22 11:09:46 -06002274static void io_complete_rw_common(struct kiocb *kiocb, long res,
2275 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002276{
Jens Axboe9adbd452019-12-20 08:45:55 -07002277 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002278 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002279
Jens Axboe491381ce2019-10-17 09:20:46 -06002280 if (kiocb->ki_flags & IOCB_WRITE)
2281 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002282
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002283 if (res != req->result)
2284 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002285 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002286 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002287 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002288}
2289
Jens Axboeb63534c2020-06-04 11:28:00 -06002290#ifdef CONFIG_BLOCK
2291static bool io_resubmit_prep(struct io_kiocb *req, int error)
2292{
2293 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2294 ssize_t ret = -ECANCELED;
2295 struct iov_iter iter;
2296 int rw;
2297
2298 if (error) {
2299 ret = error;
2300 goto end_req;
2301 }
2302
2303 switch (req->opcode) {
2304 case IORING_OP_READV:
2305 case IORING_OP_READ_FIXED:
2306 case IORING_OP_READ:
2307 rw = READ;
2308 break;
2309 case IORING_OP_WRITEV:
2310 case IORING_OP_WRITE_FIXED:
2311 case IORING_OP_WRITE:
2312 rw = WRITE;
2313 break;
2314 default:
2315 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2316 req->opcode);
2317 goto end_req;
2318 }
2319
Jens Axboe8f3d7492020-09-14 09:28:14 -06002320 if (!req->io) {
2321 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2322 if (ret < 0)
2323 goto end_req;
2324 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2325 if (!ret)
2326 return true;
2327 kfree(iovec);
2328 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002329 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002330 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002331end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002332 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002333 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002334 return false;
2335}
Jens Axboeb63534c2020-06-04 11:28:00 -06002336#endif
2337
2338static bool io_rw_reissue(struct io_kiocb *req, long res)
2339{
2340#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002341 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002342 int ret;
2343
Jens Axboe355afae2020-09-02 09:30:31 -06002344 if (!S_ISBLK(mode) && !S_ISREG(mode))
2345 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002346 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2347 return false;
2348
Jens Axboefdee9462020-08-27 16:46:24 -06002349 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002350
Jens Axboefdee9462020-08-27 16:46:24 -06002351 if (io_resubmit_prep(req, ret)) {
2352 refcount_inc(&req->refs);
2353 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002354 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002355 }
2356
Jens Axboeb63534c2020-06-04 11:28:00 -06002357#endif
2358 return false;
2359}
2360
Jens Axboea1d7c392020-06-22 11:09:46 -06002361static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2362 struct io_comp_state *cs)
2363{
2364 if (!io_rw_reissue(req, res))
2365 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002366}
2367
2368static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2369{
Jens Axboe9adbd452019-12-20 08:45:55 -07002370 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002371
Jens Axboea1d7c392020-06-22 11:09:46 -06002372 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002373}
2374
Jens Axboedef596e2019-01-09 08:59:42 -07002375static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2376{
Jens Axboe9adbd452019-12-20 08:45:55 -07002377 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002378
Jens Axboe491381ce2019-10-17 09:20:46 -06002379 if (kiocb->ki_flags & IOCB_WRITE)
2380 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002381
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002382 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002383 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002384
2385 WRITE_ONCE(req->result, res);
2386 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002387 smp_wmb();
2388 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002389}
2390
2391/*
2392 * After the iocb has been issued, it's safe to be found on the poll list.
2393 * Adding the kiocb to the list AFTER submission ensures that we don't
2394 * find it from a io_iopoll_getevents() thread before the issuer is done
2395 * accessing the kiocb cookie.
2396 */
2397static void io_iopoll_req_issued(struct io_kiocb *req)
2398{
2399 struct io_ring_ctx *ctx = req->ctx;
2400
2401 /*
2402 * Track whether we have multiple files in our lists. This will impact
2403 * how we do polling eventually, not spinning if we're on potentially
2404 * different devices.
2405 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002406 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002407 ctx->poll_multi_file = false;
2408 } else if (!ctx->poll_multi_file) {
2409 struct io_kiocb *list_req;
2410
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002411 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002412 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002413 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002414 ctx->poll_multi_file = true;
2415 }
2416
2417 /*
2418 * For fast devices, IO may have already completed. If it has, add
2419 * it to the front so we find it first.
2420 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002421 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002422 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002423 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002424 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002425
2426 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2427 wq_has_sleeper(&ctx->sqo_wait))
2428 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002429}
2430
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002431static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002432{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002433 if (state->has_refs)
2434 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002435 state->file = NULL;
2436}
2437
2438static inline void io_state_file_put(struct io_submit_state *state)
2439{
2440 if (state->file)
2441 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002442}
2443
2444/*
2445 * Get as many references to a file as we have IOs left in this submission,
2446 * assuming most submissions are for one file, or at least that each file
2447 * has more than one submission.
2448 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002449static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002450{
2451 if (!state)
2452 return fget(fd);
2453
2454 if (state->file) {
2455 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002456 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002457 state->ios_left--;
2458 return state->file;
2459 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002460 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002461 }
2462 state->file = fget_many(fd, state->ios_left);
2463 if (!state->file)
2464 return NULL;
2465
2466 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002467 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002468 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002469 return state->file;
2470}
2471
Jens Axboe4503b762020-06-01 10:00:27 -06002472static bool io_bdev_nowait(struct block_device *bdev)
2473{
2474#ifdef CONFIG_BLOCK
2475 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2476#else
2477 return true;
2478#endif
2479}
2480
Jens Axboe2b188cc2019-01-07 10:46:33 -07002481/*
2482 * If we tracked the file through the SCM inflight mechanism, we could support
2483 * any file. For now, just ensure that anything potentially problematic is done
2484 * inline.
2485 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002486static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002487{
2488 umode_t mode = file_inode(file)->i_mode;
2489
Jens Axboe4503b762020-06-01 10:00:27 -06002490 if (S_ISBLK(mode)) {
2491 if (io_bdev_nowait(file->f_inode->i_bdev))
2492 return true;
2493 return false;
2494 }
2495 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002496 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002497 if (S_ISREG(mode)) {
2498 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2499 file->f_op != &io_uring_fops)
2500 return true;
2501 return false;
2502 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002503
Jens Axboec5b85622020-06-09 19:23:05 -06002504 /* any ->read/write should understand O_NONBLOCK */
2505 if (file->f_flags & O_NONBLOCK)
2506 return true;
2507
Jens Axboeaf197f52020-04-28 13:15:06 -06002508 if (!(file->f_mode & FMODE_NOWAIT))
2509 return false;
2510
2511 if (rw == READ)
2512 return file->f_op->read_iter != NULL;
2513
2514 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002515}
2516
Jens Axboe3529d8c2019-12-19 18:24:38 -07002517static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2518 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002519{
Jens Axboedef596e2019-01-09 08:59:42 -07002520 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002521 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002522 unsigned ioprio;
2523 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002524
Jens Axboe491381ce2019-10-17 09:20:46 -06002525 if (S_ISREG(file_inode(req->file)->i_mode))
2526 req->flags |= REQ_F_ISREG;
2527
Jens Axboe2b188cc2019-01-07 10:46:33 -07002528 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002529 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2530 req->flags |= REQ_F_CUR_POS;
2531 kiocb->ki_pos = req->file->f_pos;
2532 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002533 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002534 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2535 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2536 if (unlikely(ret))
2537 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002538
2539 ioprio = READ_ONCE(sqe->ioprio);
2540 if (ioprio) {
2541 ret = ioprio_check_cap(ioprio);
2542 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002543 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002544
2545 kiocb->ki_ioprio = ioprio;
2546 } else
2547 kiocb->ki_ioprio = get_current_ioprio();
2548
Stefan Bühler8449eed2019-04-27 20:34:19 +02002549 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002550 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002551 req->flags |= REQ_F_NOWAIT;
2552
Jens Axboeb63534c2020-06-04 11:28:00 -06002553 if (kiocb->ki_flags & IOCB_DIRECT)
2554 io_get_req_task(req);
2555
Stefan Bühler8449eed2019-04-27 20:34:19 +02002556 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002557 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002558
Jens Axboedef596e2019-01-09 08:59:42 -07002559 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002560 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2561 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002562 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002563
Jens Axboedef596e2019-01-09 08:59:42 -07002564 kiocb->ki_flags |= IOCB_HIPRI;
2565 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002566 req->iopoll_completed = 0;
Pavel Begunkovf3a6fa22020-06-28 12:52:38 +03002567 io_get_req_task(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002568 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002569 if (kiocb->ki_flags & IOCB_HIPRI)
2570 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002571 kiocb->ki_complete = io_complete_rw;
2572 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002573
Jens Axboe3529d8c2019-12-19 18:24:38 -07002574 req->rw.addr = READ_ONCE(sqe->addr);
2575 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002576 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002577 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002578}
2579
2580static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2581{
2582 switch (ret) {
2583 case -EIOCBQUEUED:
2584 break;
2585 case -ERESTARTSYS:
2586 case -ERESTARTNOINTR:
2587 case -ERESTARTNOHAND:
2588 case -ERESTART_RESTARTBLOCK:
2589 /*
2590 * We can't just restart the syscall, since previously
2591 * submitted sqes may already be in progress. Just fail this
2592 * IO with EINTR.
2593 */
2594 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002595 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002596 default:
2597 kiocb->ki_complete(kiocb, ret, 0);
2598 }
2599}
2600
Jens Axboea1d7c392020-06-22 11:09:46 -06002601static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2602 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002603{
Jens Axboeba042912019-12-25 16:33:42 -07002604 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2605
Jens Axboe227c0c92020-08-13 11:51:40 -06002606 /* add previously done IO, if any */
2607 if (req->io && req->io->rw.bytes_done > 0) {
2608 if (ret < 0)
2609 ret = req->io->rw.bytes_done;
2610 else
2611 ret += req->io->rw.bytes_done;
2612 }
2613
Jens Axboeba042912019-12-25 16:33:42 -07002614 if (req->flags & REQ_F_CUR_POS)
2615 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002616 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002617 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002618 else
2619 io_rw_done(kiocb, ret);
2620}
2621
Jens Axboe9adbd452019-12-20 08:45:55 -07002622static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002623 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002624{
Jens Axboe9adbd452019-12-20 08:45:55 -07002625 struct io_ring_ctx *ctx = req->ctx;
2626 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002627 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002628 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002629 size_t offset;
2630 u64 buf_addr;
2631
2632 /* attempt to use fixed buffers without having provided iovecs */
2633 if (unlikely(!ctx->user_bufs))
2634 return -EFAULT;
2635
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002636 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002637 if (unlikely(buf_index >= ctx->nr_user_bufs))
2638 return -EFAULT;
2639
2640 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2641 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002642 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002643
2644 /* overflow */
2645 if (buf_addr + len < buf_addr)
2646 return -EFAULT;
2647 /* not inside the mapped region */
2648 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2649 return -EFAULT;
2650
2651 /*
2652 * May not be a start of buffer, set size appropriately
2653 * and advance us to the beginning.
2654 */
2655 offset = buf_addr - imu->ubuf;
2656 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002657
2658 if (offset) {
2659 /*
2660 * Don't use iov_iter_advance() here, as it's really slow for
2661 * using the latter parts of a big fixed buffer - it iterates
2662 * over each segment manually. We can cheat a bit here, because
2663 * we know that:
2664 *
2665 * 1) it's a BVEC iter, we set it up
2666 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2667 * first and last bvec
2668 *
2669 * So just find our index, and adjust the iterator afterwards.
2670 * If the offset is within the first bvec (or the whole first
2671 * bvec, just use iov_iter_advance(). This makes it easier
2672 * since we can just skip the first segment, which may not
2673 * be PAGE_SIZE aligned.
2674 */
2675 const struct bio_vec *bvec = imu->bvec;
2676
2677 if (offset <= bvec->bv_len) {
2678 iov_iter_advance(iter, offset);
2679 } else {
2680 unsigned long seg_skip;
2681
2682 /* skip first vec */
2683 offset -= bvec->bv_len;
2684 seg_skip = 1 + (offset >> PAGE_SHIFT);
2685
2686 iter->bvec = bvec + seg_skip;
2687 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002688 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002689 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002690 }
2691 }
2692
Jens Axboe5e559562019-11-13 16:12:46 -07002693 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002694}
2695
Jens Axboebcda7ba2020-02-23 16:42:51 -07002696static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2697{
2698 if (needs_lock)
2699 mutex_unlock(&ctx->uring_lock);
2700}
2701
2702static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2703{
2704 /*
2705 * "Normal" inline submissions always hold the uring_lock, since we
2706 * grab it from the system call. Same is true for the SQPOLL offload.
2707 * The only exception is when we've detached the request and issue it
2708 * from an async worker thread, grab the lock for that case.
2709 */
2710 if (needs_lock)
2711 mutex_lock(&ctx->uring_lock);
2712}
2713
2714static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2715 int bgid, struct io_buffer *kbuf,
2716 bool needs_lock)
2717{
2718 struct io_buffer *head;
2719
2720 if (req->flags & REQ_F_BUFFER_SELECTED)
2721 return kbuf;
2722
2723 io_ring_submit_lock(req->ctx, needs_lock);
2724
2725 lockdep_assert_held(&req->ctx->uring_lock);
2726
2727 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2728 if (head) {
2729 if (!list_empty(&head->list)) {
2730 kbuf = list_last_entry(&head->list, struct io_buffer,
2731 list);
2732 list_del(&kbuf->list);
2733 } else {
2734 kbuf = head;
2735 idr_remove(&req->ctx->io_buffer_idr, bgid);
2736 }
2737 if (*len > kbuf->len)
2738 *len = kbuf->len;
2739 } else {
2740 kbuf = ERR_PTR(-ENOBUFS);
2741 }
2742
2743 io_ring_submit_unlock(req->ctx, needs_lock);
2744
2745 return kbuf;
2746}
2747
Jens Axboe4d954c22020-02-27 07:31:19 -07002748static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2749 bool needs_lock)
2750{
2751 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002752 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002753
2754 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002755 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002756 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2757 if (IS_ERR(kbuf))
2758 return kbuf;
2759 req->rw.addr = (u64) (unsigned long) kbuf;
2760 req->flags |= REQ_F_BUFFER_SELECTED;
2761 return u64_to_user_ptr(kbuf->addr);
2762}
2763
2764#ifdef CONFIG_COMPAT
2765static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2766 bool needs_lock)
2767{
2768 struct compat_iovec __user *uiov;
2769 compat_ssize_t clen;
2770 void __user *buf;
2771 ssize_t len;
2772
2773 uiov = u64_to_user_ptr(req->rw.addr);
2774 if (!access_ok(uiov, sizeof(*uiov)))
2775 return -EFAULT;
2776 if (__get_user(clen, &uiov->iov_len))
2777 return -EFAULT;
2778 if (clen < 0)
2779 return -EINVAL;
2780
2781 len = clen;
2782 buf = io_rw_buffer_select(req, &len, needs_lock);
2783 if (IS_ERR(buf))
2784 return PTR_ERR(buf);
2785 iov[0].iov_base = buf;
2786 iov[0].iov_len = (compat_size_t) len;
2787 return 0;
2788}
2789#endif
2790
2791static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2792 bool needs_lock)
2793{
2794 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2795 void __user *buf;
2796 ssize_t len;
2797
2798 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2799 return -EFAULT;
2800
2801 len = iov[0].iov_len;
2802 if (len < 0)
2803 return -EINVAL;
2804 buf = io_rw_buffer_select(req, &len, needs_lock);
2805 if (IS_ERR(buf))
2806 return PTR_ERR(buf);
2807 iov[0].iov_base = buf;
2808 iov[0].iov_len = len;
2809 return 0;
2810}
2811
2812static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2813 bool needs_lock)
2814{
Jens Axboedddb3e22020-06-04 11:27:01 -06002815 if (req->flags & REQ_F_BUFFER_SELECTED) {
2816 struct io_buffer *kbuf;
2817
2818 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2819 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2820 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002821 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002822 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002823 if (!req->rw.len)
2824 return 0;
2825 else if (req->rw.len > 1)
2826 return -EINVAL;
2827
2828#ifdef CONFIG_COMPAT
2829 if (req->ctx->compat)
2830 return io_compat_import(req, iov, needs_lock);
2831#endif
2832
2833 return __io_iov_buffer_select(req, iov, needs_lock);
2834}
2835
Jens Axboe8452fd02020-08-18 13:58:33 -07002836static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2837 struct iovec **iovec, struct iov_iter *iter,
2838 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002839{
Jens Axboe9adbd452019-12-20 08:45:55 -07002840 void __user *buf = u64_to_user_ptr(req->rw.addr);
2841 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002842 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002843 u8 opcode;
2844
Jens Axboed625c6e2019-12-17 19:53:05 -07002845 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002846 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002847 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002848 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002849 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002850
Jens Axboebcda7ba2020-02-23 16:42:51 -07002851 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002852 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002853 return -EINVAL;
2854
Jens Axboe3a6820f2019-12-22 15:19:35 -07002855 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002856 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002857 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002858 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002859 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002860 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002861 }
2862
Jens Axboe3a6820f2019-12-22 15:19:35 -07002863 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2864 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002865 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002866 }
2867
Jens Axboe4d954c22020-02-27 07:31:19 -07002868 if (req->flags & REQ_F_BUFFER_SELECT) {
2869 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002870 if (!ret) {
2871 ret = (*iovec)->iov_len;
2872 iov_iter_init(iter, rw, *iovec, 1, ret);
2873 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002874 *iovec = NULL;
2875 return ret;
2876 }
2877
Jens Axboe2b188cc2019-01-07 10:46:33 -07002878#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002879 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002880 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2881 iovec, iter);
2882#endif
2883
2884 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2885}
2886
Jens Axboe8452fd02020-08-18 13:58:33 -07002887static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2888 struct iovec **iovec, struct iov_iter *iter,
2889 bool needs_lock)
2890{
2891 if (!req->io)
2892 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2893 *iovec = NULL;
2894 return iov_iter_count(&req->io->rw.iter);
2895}
2896
Jens Axboe0fef9482020-08-26 10:36:20 -06002897static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2898{
2899 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2900}
2901
Jens Axboe32960612019-09-23 11:05:34 -06002902/*
2903 * For files that don't have ->read_iter() and ->write_iter(), handle them
2904 * by looping over ->read() or ->write() manually.
2905 */
2906static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2907 struct iov_iter *iter)
2908{
2909 ssize_t ret = 0;
2910
2911 /*
2912 * Don't support polled IO through this interface, and we can't
2913 * support non-blocking either. For the latter, this just causes
2914 * the kiocb to be handled from an async context.
2915 */
2916 if (kiocb->ki_flags & IOCB_HIPRI)
2917 return -EOPNOTSUPP;
2918 if (kiocb->ki_flags & IOCB_NOWAIT)
2919 return -EAGAIN;
2920
2921 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002922 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002923 ssize_t nr;
2924
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002925 if (!iov_iter_is_bvec(iter)) {
2926 iovec = iov_iter_iovec(iter);
2927 } else {
2928 /* fixed buffers import bvec */
2929 iovec.iov_base = kmap(iter->bvec->bv_page)
2930 + iter->iov_offset;
2931 iovec.iov_len = min(iter->count,
2932 iter->bvec->bv_len - iter->iov_offset);
2933 }
2934
Jens Axboe32960612019-09-23 11:05:34 -06002935 if (rw == READ) {
2936 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002937 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002938 } else {
2939 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002940 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002941 }
2942
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002943 if (iov_iter_is_bvec(iter))
2944 kunmap(iter->bvec->bv_page);
2945
Jens Axboe32960612019-09-23 11:05:34 -06002946 if (nr < 0) {
2947 if (!ret)
2948 ret = nr;
2949 break;
2950 }
2951 ret += nr;
2952 if (nr != iovec.iov_len)
2953 break;
2954 iov_iter_advance(iter, nr);
2955 }
2956
2957 return ret;
2958}
2959
Jens Axboeff6165b2020-08-13 09:47:43 -06002960static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
2961 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07002962{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002963 struct io_async_rw *rw = &req->io->rw;
2964
Jens Axboeff6165b2020-08-13 09:47:43 -06002965 memcpy(&rw->iter, iter, sizeof(*iter));
2966 rw->free_iovec = NULL;
Jens Axboe227c0c92020-08-13 11:51:40 -06002967 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06002968 /* can only be fixed buffers, no need to do anything */
2969 if (iter->type == ITER_BVEC)
2970 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002971 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06002972 unsigned iov_off = 0;
2973
2974 rw->iter.iov = rw->fast_iov;
2975 if (iter->iov != fast_iov) {
2976 iov_off = iter->iov - fast_iov;
2977 rw->iter.iov += iov_off;
2978 }
2979 if (rw->fast_iov != fast_iov)
2980 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002981 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002982 } else {
Jens Axboeff6165b2020-08-13 09:47:43 -06002983 rw->free_iovec = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002984 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002985 }
2986}
2987
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002988static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2989{
2990 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2991 return req->io == NULL;
2992}
2993
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002994static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002995{
Jens Axboed3656342019-12-18 09:50:26 -07002996 if (!io_op_defs[req->opcode].async_ctx)
2997 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002998
2999 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003000}
3001
Jens Axboeff6165b2020-08-13 09:47:43 -06003002static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3003 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003004 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003005{
Jens Axboe227c0c92020-08-13 11:51:40 -06003006 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07003007 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07003008 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003009 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07003010 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003011
Jens Axboeff6165b2020-08-13 09:47:43 -06003012 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003013 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003014 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003015}
3016
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003017static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
3018 bool force_nonblock)
3019{
Jens Axboeff6165b2020-08-13 09:47:43 -06003020 struct io_async_rw *iorw = &req->io->rw;
Jens Axboec183edf2020-09-04 22:36:52 -06003021 struct iovec *iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003022 ssize_t ret;
3023
Jens Axboec183edf2020-09-04 22:36:52 -06003024 iorw->iter.iov = iov = iorw->fast_iov;
3025 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003026 if (unlikely(ret < 0))
3027 return ret;
3028
Jens Axboec183edf2020-09-04 22:36:52 -06003029 iorw->iter.iov = iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003030 io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003031 return 0;
3032}
3033
Jens Axboe3529d8c2019-12-19 18:24:38 -07003034static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3035 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003036{
3037 ssize_t ret;
3038
Jens Axboe3529d8c2019-12-19 18:24:38 -07003039 ret = io_prep_rw(req, sqe, force_nonblock);
3040 if (ret)
3041 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003042
Jens Axboe3529d8c2019-12-19 18:24:38 -07003043 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3044 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003045
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003046 /* either don't need iovec imported or already have it */
3047 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003048 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003049 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003050}
3051
Jens Axboec1dd91d2020-08-03 16:43:59 -06003052/*
3053 * This is our waitqueue callback handler, registered through lock_page_async()
3054 * when we initially tried to do the IO with the iocb armed our waitqueue.
3055 * This gets called when the page is unlocked, and we generally expect that to
3056 * happen when the page IO is completed and the page is now uptodate. This will
3057 * queue a task_work based retry of the operation, attempting to copy the data
3058 * again. If the latter fails because the page was NOT uptodate, then we will
3059 * do a thread based blocking retry of the operation. That's the unexpected
3060 * slow path.
3061 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003062static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3063 int sync, void *arg)
3064{
3065 struct wait_page_queue *wpq;
3066 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003067 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003068 int ret;
3069
3070 wpq = container_of(wait, struct wait_page_queue, wait);
3071
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003072 if (!wake_page_match(wpq, key))
3073 return 0;
3074
Hao Xuc8d317a2020-09-29 20:00:45 +08003075 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003076 list_del_init(&wait->entry);
3077
Pavel Begunkove7375122020-07-12 20:42:04 +03003078 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003079 percpu_ref_get(&req->ctx->refs);
3080
Jens Axboebcf5a062020-05-22 09:24:42 -06003081 /* submit ref gets dropped, acquire a new one */
3082 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003083 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003084 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003085 struct task_struct *tsk;
3086
Jens Axboebcf5a062020-05-22 09:24:42 -06003087 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003088 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003089 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003090 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003091 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003092 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003093 return 1;
3094}
3095
Jens Axboec1dd91d2020-08-03 16:43:59 -06003096/*
3097 * This controls whether a given IO request should be armed for async page
3098 * based retry. If we return false here, the request is handed to the async
3099 * worker threads for retry. If we're doing buffered reads on a regular file,
3100 * we prepare a private wait_page_queue entry and retry the operation. This
3101 * will either succeed because the page is now uptodate and unlocked, or it
3102 * will register a callback when the page is unlocked at IO completion. Through
3103 * that callback, io_uring uses task_work to setup a retry of the operation.
3104 * That retry will attempt the buffered read again. The retry will generally
3105 * succeed, or in rare cases where it fails, we then fall back to using the
3106 * async worker threads for a blocking retry.
3107 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003108static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003109{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003110 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003111 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003112
3113 /* never retry for NOWAIT, we just complete with -EAGAIN */
3114 if (req->flags & REQ_F_NOWAIT)
3115 return false;
3116
Jens Axboe227c0c92020-08-13 11:51:40 -06003117 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003118 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003119 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003120
Jens Axboebcf5a062020-05-22 09:24:42 -06003121 /*
3122 * just use poll if we can, and don't attempt if the fs doesn't
3123 * support callback based unlocks
3124 */
3125 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3126 return false;
3127
Jens Axboe3b2a4432020-08-16 10:58:43 -07003128 wait->wait.func = io_async_buf_func;
3129 wait->wait.private = req;
3130 wait->wait.flags = 0;
3131 INIT_LIST_HEAD(&wait->wait.entry);
3132 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003133 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003134 kiocb->ki_waitq = wait;
Jens Axboebcf5a062020-05-22 09:24:42 -06003135
Jens Axboe3b2a4432020-08-16 10:58:43 -07003136 io_get_req_task(req);
3137 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003138}
3139
3140static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3141{
3142 if (req->file->f_op->read_iter)
3143 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003144 else if (req->file->f_op->read)
3145 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3146 else
3147 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003148}
3149
Jens Axboea1d7c392020-06-22 11:09:46 -06003150static int io_read(struct io_kiocb *req, bool force_nonblock,
3151 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003152{
3153 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003154 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003155 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003156 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003157 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003158 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003159
Jens Axboeff6165b2020-08-13 09:47:43 -06003160 if (req->io)
3161 iter = &req->io->rw.iter;
3162
3163 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003164 if (ret < 0)
3165 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003166 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003167 io_size = ret;
3168 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003169 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003170
Jens Axboefd6c2e42019-12-18 12:19:41 -07003171 /* Ensure we clear previously set non-block flag */
3172 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003173 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003174
Pavel Begunkov24c74672020-06-21 13:09:51 +03003175 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003176 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3177 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003178 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003179
Jens Axboe0fef9482020-08-26 10:36:20 -06003180 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003181 if (unlikely(ret))
3182 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003183
Jens Axboe227c0c92020-08-13 11:51:40 -06003184 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003185
Jens Axboe227c0c92020-08-13 11:51:40 -06003186 if (!ret) {
3187 goto done;
3188 } else if (ret == -EIOCBQUEUED) {
3189 ret = 0;
3190 goto out_free;
3191 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003192 /* IOPOLL retry should happen for io-wq threads */
3193 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003194 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003195 /* no retry on NONBLOCK marked file */
3196 if (req->file->f_flags & O_NONBLOCK)
3197 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003198 /* some cases will consume bytes even on error returns */
3199 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003200 ret = 0;
3201 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003202 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003203 /* make sure -ERESTARTSYS -> -EINTR is done */
3204 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003205 }
3206
3207 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003208 if (!iov_iter_count(iter) || !force_nonblock ||
3209 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003210 goto done;
3211
3212 io_size -= ret;
3213copy_iov:
3214 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3215 if (ret2) {
3216 ret = ret2;
3217 goto out_free;
3218 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003219 if (no_async)
3220 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003221 /* it's copied and will be cleaned with ->io */
3222 iovec = NULL;
3223 /* now use our persistent iterator, if we aren't already */
3224 iter = &req->io->rw.iter;
3225retry:
3226 req->io->rw.bytes_done += ret;
3227 /* if we can retry, do so with the callbacks armed */
3228 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003229 kiocb->ki_flags &= ~IOCB_WAITQ;
3230 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003231 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003232
3233 /*
3234 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3235 * get -EIOCBQUEUED, then we'll get a notification when the desired
3236 * page gets unlocked. We can also get a partial read here, and if we
3237 * do, then just retry at the new offset.
3238 */
3239 ret = io_iter_do_read(req, iter);
3240 if (ret == -EIOCBQUEUED) {
3241 ret = 0;
3242 goto out_free;
3243 } else if (ret > 0 && ret < io_size) {
3244 /* we got some bytes, but not all. retry. */
3245 goto retry;
3246 }
3247done:
3248 kiocb_done(kiocb, ret, cs);
3249 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003250out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003251 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003252 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003253 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003254 return ret;
3255}
3256
Jens Axboe3529d8c2019-12-19 18:24:38 -07003257static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3258 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003259{
3260 ssize_t ret;
3261
Jens Axboe3529d8c2019-12-19 18:24:38 -07003262 ret = io_prep_rw(req, sqe, force_nonblock);
3263 if (ret)
3264 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003265
Jens Axboe3529d8c2019-12-19 18:24:38 -07003266 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3267 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003268
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003269 /* either don't need iovec imported or already have it */
3270 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003271 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003272 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003273}
3274
Jens Axboea1d7c392020-06-22 11:09:46 -06003275static int io_write(struct io_kiocb *req, bool force_nonblock,
3276 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003277{
3278 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003279 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003280 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003281 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003282 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003283
Jens Axboeff6165b2020-08-13 09:47:43 -06003284 if (req->io)
3285 iter = &req->io->rw.iter;
3286
3287 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003288 if (ret < 0)
3289 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003290 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003291 io_size = ret;
3292 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003293
Jens Axboefd6c2e42019-12-18 12:19:41 -07003294 /* Ensure we clear previously set non-block flag */
3295 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003296 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003297
Pavel Begunkov24c74672020-06-21 13:09:51 +03003298 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003299 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003300 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003301
Jens Axboe10d59342019-12-09 20:16:22 -07003302 /* file path doesn't support NOWAIT for non-direct_IO */
3303 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3304 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003305 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003306
Jens Axboe0fef9482020-08-26 10:36:20 -06003307 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003308 if (unlikely(ret))
3309 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003310
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003311 /*
3312 * Open-code file_start_write here to grab freeze protection,
3313 * which will be released by another thread in
3314 * io_complete_rw(). Fool lockdep by telling it the lock got
3315 * released so that it doesn't complain about the held lock when
3316 * we return to userspace.
3317 */
3318 if (req->flags & REQ_F_ISREG) {
3319 __sb_start_write(file_inode(req->file)->i_sb,
3320 SB_FREEZE_WRITE, true);
3321 __sb_writers_release(file_inode(req->file)->i_sb,
3322 SB_FREEZE_WRITE);
3323 }
3324 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003325
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003326 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003327 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003328 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003329 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003330 else
3331 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003332
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003333 /*
3334 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3335 * retry them without IOCB_NOWAIT.
3336 */
3337 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3338 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003339 /* no retry on NONBLOCK marked file */
3340 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3341 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003342 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003343 /* IOPOLL retry should happen for io-wq threads */
3344 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3345 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003346done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003347 kiocb_done(kiocb, ret2, cs);
3348 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003349copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003350 /* some cases will consume bytes even on error returns */
3351 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003352 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003353 if (!ret)
3354 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003355 }
Jens Axboe31b51512019-01-18 22:56:34 -07003356out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003357 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003358 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003359 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003360 return ret;
3361}
3362
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003363static int __io_splice_prep(struct io_kiocb *req,
3364 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003365{
3366 struct io_splice* sp = &req->splice;
3367 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3368 int ret;
3369
3370 if (req->flags & REQ_F_NEED_CLEANUP)
3371 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003372 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3373 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003374
3375 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003376 sp->len = READ_ONCE(sqe->len);
3377 sp->flags = READ_ONCE(sqe->splice_flags);
3378
3379 if (unlikely(sp->flags & ~valid_flags))
3380 return -EINVAL;
3381
3382 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3383 (sp->flags & SPLICE_F_FD_IN_FIXED));
3384 if (ret)
3385 return ret;
3386 req->flags |= REQ_F_NEED_CLEANUP;
3387
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003388 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3389 /*
3390 * Splice operation will be punted aync, and here need to
3391 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3392 */
3393 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003394 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003395 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003396
3397 return 0;
3398}
3399
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003400static int io_tee_prep(struct io_kiocb *req,
3401 const struct io_uring_sqe *sqe)
3402{
3403 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3404 return -EINVAL;
3405 return __io_splice_prep(req, sqe);
3406}
3407
3408static int io_tee(struct io_kiocb *req, bool force_nonblock)
3409{
3410 struct io_splice *sp = &req->splice;
3411 struct file *in = sp->file_in;
3412 struct file *out = sp->file_out;
3413 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3414 long ret = 0;
3415
3416 if (force_nonblock)
3417 return -EAGAIN;
3418 if (sp->len)
3419 ret = do_tee(in, out, sp->len, flags);
3420
3421 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3422 req->flags &= ~REQ_F_NEED_CLEANUP;
3423
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003424 if (ret != sp->len)
3425 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003426 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003427 return 0;
3428}
3429
3430static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3431{
3432 struct io_splice* sp = &req->splice;
3433
3434 sp->off_in = READ_ONCE(sqe->splice_off_in);
3435 sp->off_out = READ_ONCE(sqe->off);
3436 return __io_splice_prep(req, sqe);
3437}
3438
Pavel Begunkov014db002020-03-03 21:33:12 +03003439static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003440{
3441 struct io_splice *sp = &req->splice;
3442 struct file *in = sp->file_in;
3443 struct file *out = sp->file_out;
3444 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3445 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003446 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003447
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003448 if (force_nonblock)
3449 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003450
3451 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3452 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003453
Jens Axboe948a7742020-05-17 14:21:38 -06003454 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003455 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003456
3457 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3458 req->flags &= ~REQ_F_NEED_CLEANUP;
3459
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003460 if (ret != sp->len)
3461 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003462 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003463 return 0;
3464}
3465
Jens Axboe2b188cc2019-01-07 10:46:33 -07003466/*
3467 * IORING_OP_NOP just posts a completion event, nothing else.
3468 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003469static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003470{
3471 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003472
Jens Axboedef596e2019-01-09 08:59:42 -07003473 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3474 return -EINVAL;
3475
Jens Axboe229a7b62020-06-22 10:13:11 -06003476 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003477 return 0;
3478}
3479
Jens Axboe3529d8c2019-12-19 18:24:38 -07003480static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003481{
Jens Axboe6b063142019-01-10 22:13:58 -07003482 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003483
Jens Axboe09bb8392019-03-13 12:39:28 -06003484 if (!req->file)
3485 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003486
Jens Axboe6b063142019-01-10 22:13:58 -07003487 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003488 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003489 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003490 return -EINVAL;
3491
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003492 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3493 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3494 return -EINVAL;
3495
3496 req->sync.off = READ_ONCE(sqe->off);
3497 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003498 return 0;
3499}
3500
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003501static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003502{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003503 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003504 int ret;
3505
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003506 /* fsync always requires a blocking context */
3507 if (force_nonblock)
3508 return -EAGAIN;
3509
Jens Axboe9adbd452019-12-20 08:45:55 -07003510 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003511 end > 0 ? end : LLONG_MAX,
3512 req->sync.flags & IORING_FSYNC_DATASYNC);
3513 if (ret < 0)
3514 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003515 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003516 return 0;
3517}
3518
Jens Axboed63d1b52019-12-10 10:38:56 -07003519static int io_fallocate_prep(struct io_kiocb *req,
3520 const struct io_uring_sqe *sqe)
3521{
3522 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3523 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003524 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3525 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003526
3527 req->sync.off = READ_ONCE(sqe->off);
3528 req->sync.len = READ_ONCE(sqe->addr);
3529 req->sync.mode = READ_ONCE(sqe->len);
3530 return 0;
3531}
3532
Pavel Begunkov014db002020-03-03 21:33:12 +03003533static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003534{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003535 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003536
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003537 /* fallocate always requiring blocking context */
3538 if (force_nonblock)
3539 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003540 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3541 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003542 if (ret < 0)
3543 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003544 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003545 return 0;
3546}
3547
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003548static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003549{
Jens Axboef8748882020-01-08 17:47:02 -07003550 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003551 int ret;
3552
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003553 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003554 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003555 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003556 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003557
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003558 /* open.how should be already initialised */
3559 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003560 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003561
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003562 req->open.dfd = READ_ONCE(sqe->fd);
3563 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003564 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003565 if (IS_ERR(req->open.filename)) {
3566 ret = PTR_ERR(req->open.filename);
3567 req->open.filename = NULL;
3568 return ret;
3569 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003570 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003571 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003572 return 0;
3573}
3574
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003575static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3576{
3577 u64 flags, mode;
3578
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003579 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3580 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003581 if (req->flags & REQ_F_NEED_CLEANUP)
3582 return 0;
3583 mode = READ_ONCE(sqe->len);
3584 flags = READ_ONCE(sqe->open_flags);
3585 req->open.how = build_open_how(flags, mode);
3586 return __io_openat_prep(req, sqe);
3587}
3588
Jens Axboecebdb982020-01-08 17:59:24 -07003589static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3590{
3591 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003592 size_t len;
3593 int ret;
3594
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003595 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3596 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003597 if (req->flags & REQ_F_NEED_CLEANUP)
3598 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003599 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3600 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003601 if (len < OPEN_HOW_SIZE_VER0)
3602 return -EINVAL;
3603
3604 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3605 len);
3606 if (ret)
3607 return ret;
3608
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003609 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003610}
3611
Pavel Begunkov014db002020-03-03 21:33:12 +03003612static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003613{
3614 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003615 struct file *file;
3616 int ret;
3617
Jens Axboef86cd202020-01-29 13:46:44 -07003618 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003619 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003620
Jens Axboecebdb982020-01-08 17:59:24 -07003621 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003622 if (ret)
3623 goto err;
3624
Jens Axboe4022e7a2020-03-19 19:23:18 -06003625 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003626 if (ret < 0)
3627 goto err;
3628
3629 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3630 if (IS_ERR(file)) {
3631 put_unused_fd(ret);
3632 ret = PTR_ERR(file);
3633 } else {
3634 fsnotify_open(file);
3635 fd_install(ret, file);
3636 }
3637err:
3638 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003639 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003640 if (ret < 0)
3641 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003642 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003643 return 0;
3644}
3645
Pavel Begunkov014db002020-03-03 21:33:12 +03003646static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003647{
Pavel Begunkov014db002020-03-03 21:33:12 +03003648 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003649}
3650
Jens Axboe067524e2020-03-02 16:32:28 -07003651static int io_remove_buffers_prep(struct io_kiocb *req,
3652 const struct io_uring_sqe *sqe)
3653{
3654 struct io_provide_buf *p = &req->pbuf;
3655 u64 tmp;
3656
3657 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3658 return -EINVAL;
3659
3660 tmp = READ_ONCE(sqe->fd);
3661 if (!tmp || tmp > USHRT_MAX)
3662 return -EINVAL;
3663
3664 memset(p, 0, sizeof(*p));
3665 p->nbufs = tmp;
3666 p->bgid = READ_ONCE(sqe->buf_group);
3667 return 0;
3668}
3669
3670static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3671 int bgid, unsigned nbufs)
3672{
3673 unsigned i = 0;
3674
3675 /* shouldn't happen */
3676 if (!nbufs)
3677 return 0;
3678
3679 /* the head kbuf is the list itself */
3680 while (!list_empty(&buf->list)) {
3681 struct io_buffer *nxt;
3682
3683 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3684 list_del(&nxt->list);
3685 kfree(nxt);
3686 if (++i == nbufs)
3687 return i;
3688 }
3689 i++;
3690 kfree(buf);
3691 idr_remove(&ctx->io_buffer_idr, bgid);
3692
3693 return i;
3694}
3695
Jens Axboe229a7b62020-06-22 10:13:11 -06003696static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3697 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003698{
3699 struct io_provide_buf *p = &req->pbuf;
3700 struct io_ring_ctx *ctx = req->ctx;
3701 struct io_buffer *head;
3702 int ret = 0;
3703
3704 io_ring_submit_lock(ctx, !force_nonblock);
3705
3706 lockdep_assert_held(&ctx->uring_lock);
3707
3708 ret = -ENOENT;
3709 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3710 if (head)
3711 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3712
3713 io_ring_submit_lock(ctx, !force_nonblock);
3714 if (ret < 0)
3715 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003716 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003717 return 0;
3718}
3719
Jens Axboeddf0322d2020-02-23 16:41:33 -07003720static int io_provide_buffers_prep(struct io_kiocb *req,
3721 const struct io_uring_sqe *sqe)
3722{
3723 struct io_provide_buf *p = &req->pbuf;
3724 u64 tmp;
3725
3726 if (sqe->ioprio || sqe->rw_flags)
3727 return -EINVAL;
3728
3729 tmp = READ_ONCE(sqe->fd);
3730 if (!tmp || tmp > USHRT_MAX)
3731 return -E2BIG;
3732 p->nbufs = tmp;
3733 p->addr = READ_ONCE(sqe->addr);
3734 p->len = READ_ONCE(sqe->len);
3735
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003736 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003737 return -EFAULT;
3738
3739 p->bgid = READ_ONCE(sqe->buf_group);
3740 tmp = READ_ONCE(sqe->off);
3741 if (tmp > USHRT_MAX)
3742 return -E2BIG;
3743 p->bid = tmp;
3744 return 0;
3745}
3746
3747static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3748{
3749 struct io_buffer *buf;
3750 u64 addr = pbuf->addr;
3751 int i, bid = pbuf->bid;
3752
3753 for (i = 0; i < pbuf->nbufs; i++) {
3754 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3755 if (!buf)
3756 break;
3757
3758 buf->addr = addr;
3759 buf->len = pbuf->len;
3760 buf->bid = bid;
3761 addr += pbuf->len;
3762 bid++;
3763 if (!*head) {
3764 INIT_LIST_HEAD(&buf->list);
3765 *head = buf;
3766 } else {
3767 list_add_tail(&buf->list, &(*head)->list);
3768 }
3769 }
3770
3771 return i ? i : -ENOMEM;
3772}
3773
Jens Axboe229a7b62020-06-22 10:13:11 -06003774static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3775 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003776{
3777 struct io_provide_buf *p = &req->pbuf;
3778 struct io_ring_ctx *ctx = req->ctx;
3779 struct io_buffer *head, *list;
3780 int ret = 0;
3781
3782 io_ring_submit_lock(ctx, !force_nonblock);
3783
3784 lockdep_assert_held(&ctx->uring_lock);
3785
3786 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3787
3788 ret = io_add_buffers(p, &head);
3789 if (ret < 0)
3790 goto out;
3791
3792 if (!list) {
3793 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3794 GFP_KERNEL);
3795 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003796 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003797 goto out;
3798 }
3799 }
3800out:
3801 io_ring_submit_unlock(ctx, !force_nonblock);
3802 if (ret < 0)
3803 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003804 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003805 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003806}
3807
Jens Axboe3e4827b2020-01-08 15:18:09 -07003808static int io_epoll_ctl_prep(struct io_kiocb *req,
3809 const struct io_uring_sqe *sqe)
3810{
3811#if defined(CONFIG_EPOLL)
3812 if (sqe->ioprio || sqe->buf_index)
3813 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003814 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003815 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003816
3817 req->epoll.epfd = READ_ONCE(sqe->fd);
3818 req->epoll.op = READ_ONCE(sqe->len);
3819 req->epoll.fd = READ_ONCE(sqe->off);
3820
3821 if (ep_op_has_event(req->epoll.op)) {
3822 struct epoll_event __user *ev;
3823
3824 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3825 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3826 return -EFAULT;
3827 }
3828
3829 return 0;
3830#else
3831 return -EOPNOTSUPP;
3832#endif
3833}
3834
Jens Axboe229a7b62020-06-22 10:13:11 -06003835static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3836 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003837{
3838#if defined(CONFIG_EPOLL)
3839 struct io_epoll *ie = &req->epoll;
3840 int ret;
3841
3842 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3843 if (force_nonblock && ret == -EAGAIN)
3844 return -EAGAIN;
3845
3846 if (ret < 0)
3847 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003848 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003849 return 0;
3850#else
3851 return -EOPNOTSUPP;
3852#endif
3853}
3854
Jens Axboec1ca7572019-12-25 22:18:28 -07003855static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3856{
3857#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3858 if (sqe->ioprio || sqe->buf_index || sqe->off)
3859 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003860 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3861 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003862
3863 req->madvise.addr = READ_ONCE(sqe->addr);
3864 req->madvise.len = READ_ONCE(sqe->len);
3865 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3866 return 0;
3867#else
3868 return -EOPNOTSUPP;
3869#endif
3870}
3871
Pavel Begunkov014db002020-03-03 21:33:12 +03003872static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003873{
3874#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3875 struct io_madvise *ma = &req->madvise;
3876 int ret;
3877
3878 if (force_nonblock)
3879 return -EAGAIN;
3880
3881 ret = do_madvise(ma->addr, ma->len, ma->advice);
3882 if (ret < 0)
3883 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003884 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003885 return 0;
3886#else
3887 return -EOPNOTSUPP;
3888#endif
3889}
3890
Jens Axboe4840e412019-12-25 22:03:45 -07003891static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3892{
3893 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3894 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003895 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3896 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003897
3898 req->fadvise.offset = READ_ONCE(sqe->off);
3899 req->fadvise.len = READ_ONCE(sqe->len);
3900 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3901 return 0;
3902}
3903
Pavel Begunkov014db002020-03-03 21:33:12 +03003904static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003905{
3906 struct io_fadvise *fa = &req->fadvise;
3907 int ret;
3908
Jens Axboe3e694262020-02-01 09:22:49 -07003909 if (force_nonblock) {
3910 switch (fa->advice) {
3911 case POSIX_FADV_NORMAL:
3912 case POSIX_FADV_RANDOM:
3913 case POSIX_FADV_SEQUENTIAL:
3914 break;
3915 default:
3916 return -EAGAIN;
3917 }
3918 }
Jens Axboe4840e412019-12-25 22:03:45 -07003919
3920 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3921 if (ret < 0)
3922 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003923 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003924 return 0;
3925}
3926
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003927static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3928{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003929 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003930 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003931 if (sqe->ioprio || sqe->buf_index)
3932 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003933 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003934 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003935
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003936 req->statx.dfd = READ_ONCE(sqe->fd);
3937 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003938 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003939 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3940 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003941
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003942 return 0;
3943}
3944
Pavel Begunkov014db002020-03-03 21:33:12 +03003945static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003946{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003947 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003948 int ret;
3949
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003950 if (force_nonblock) {
3951 /* only need file table for an actual valid fd */
3952 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3953 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003954 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003955 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003956
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003957 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3958 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003959
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003960 if (ret < 0)
3961 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003962 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003963 return 0;
3964}
3965
Jens Axboeb5dba592019-12-11 14:02:38 -07003966static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3967{
3968 /*
3969 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003970 * leave the 'file' in an undeterminate state, and here need to modify
3971 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003972 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003973 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003974 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3975
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003976 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3977 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003978 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3979 sqe->rw_flags || sqe->buf_index)
3980 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003981 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003982 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003983
3984 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003985 if ((req->file && req->file->f_op == &io_uring_fops) ||
3986 req->close.fd == req->ctx->ring_fd)
3987 return -EBADF;
3988
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003989 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003990 return 0;
3991}
3992
Jens Axboe229a7b62020-06-22 10:13:11 -06003993static int io_close(struct io_kiocb *req, bool force_nonblock,
3994 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003995{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003996 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003997 int ret;
3998
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003999 /* might be already done during nonblock submission */
4000 if (!close->put_file) {
4001 ret = __close_fd_get_file(close->fd, &close->put_file);
4002 if (ret < 0)
4003 return (ret == -ENOENT) ? -EBADF : ret;
4004 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004005
4006 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004007 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03004008 /* was never set, but play safe */
4009 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004010 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03004011 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004012 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004013 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004014
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004015 /* No ->flush() or already async, safely close from here */
4016 ret = filp_close(close->put_file, req->work.files);
4017 if (ret < 0)
4018 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004019 fput(close->put_file);
4020 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06004021 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07004022 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004023}
4024
Jens Axboe3529d8c2019-12-19 18:24:38 -07004025static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004026{
4027 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004028
4029 if (!req->file)
4030 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004031
4032 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4033 return -EINVAL;
4034 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4035 return -EINVAL;
4036
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004037 req->sync.off = READ_ONCE(sqe->off);
4038 req->sync.len = READ_ONCE(sqe->len);
4039 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004040 return 0;
4041}
4042
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004043static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004044{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004045 int ret;
4046
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004047 /* sync_file_range always requires a blocking context */
4048 if (force_nonblock)
4049 return -EAGAIN;
4050
Jens Axboe9adbd452019-12-20 08:45:55 -07004051 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004052 req->sync.flags);
4053 if (ret < 0)
4054 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004055 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004056 return 0;
4057}
4058
YueHaibing469956e2020-03-04 15:53:52 +08004059#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004060static int io_setup_async_msg(struct io_kiocb *req,
4061 struct io_async_msghdr *kmsg)
4062{
4063 if (req->io)
4064 return -EAGAIN;
4065 if (io_alloc_async_ctx(req)) {
4066 if (kmsg->iov != kmsg->fast_iov)
4067 kfree(kmsg->iov);
4068 return -ENOMEM;
4069 }
4070 req->flags |= REQ_F_NEED_CLEANUP;
4071 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4072 return -EAGAIN;
4073}
4074
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004075static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4076 struct io_async_msghdr *iomsg)
4077{
4078 iomsg->iov = iomsg->fast_iov;
4079 iomsg->msg.msg_name = &iomsg->addr;
4080 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4081 req->sr_msg.msg_flags, &iomsg->iov);
4082}
4083
Jens Axboe3529d8c2019-12-19 18:24:38 -07004084static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004085{
Jens Axboee47293f2019-12-20 08:58:21 -07004086 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004087 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004088 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004089
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004090 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4091 return -EINVAL;
4092
Jens Axboee47293f2019-12-20 08:58:21 -07004093 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004094 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004095 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004096
Jens Axboed8768362020-02-27 14:17:49 -07004097#ifdef CONFIG_COMPAT
4098 if (req->ctx->compat)
4099 sr->msg_flags |= MSG_CMSG_COMPAT;
4100#endif
4101
Jens Axboefddafac2020-01-04 20:19:44 -07004102 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004103 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004104 /* iovec is already imported */
4105 if (req->flags & REQ_F_NEED_CLEANUP)
4106 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004107
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004108 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004109 if (!ret)
4110 req->flags |= REQ_F_NEED_CLEANUP;
4111 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004112}
4113
Jens Axboe229a7b62020-06-22 10:13:11 -06004114static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4115 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004116{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004117 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004118 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004119 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004120 int ret;
4121
Jens Axboe03b12302019-12-02 18:50:25 -07004122 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004123 if (unlikely(!sock))
4124 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004125
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004126 if (req->io) {
4127 kmsg = &req->io->msg;
4128 kmsg->msg.msg_name = &req->io->msg.addr;
4129 /* if iov is set, it's allocated already */
4130 if (!kmsg->iov)
4131 kmsg->iov = kmsg->fast_iov;
4132 kmsg->msg.msg_iter.iov = kmsg->iov;
4133 } else {
4134 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004135 if (ret)
4136 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004137 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004138 }
4139
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004140 flags = req->sr_msg.msg_flags;
4141 if (flags & MSG_DONTWAIT)
4142 req->flags |= REQ_F_NOWAIT;
4143 else if (force_nonblock)
4144 flags |= MSG_DONTWAIT;
4145
4146 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4147 if (force_nonblock && ret == -EAGAIN)
4148 return io_setup_async_msg(req, kmsg);
4149 if (ret == -ERESTARTSYS)
4150 ret = -EINTR;
4151
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004152 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004153 kfree(kmsg->iov);
4154 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004155 if (ret < 0)
4156 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004157 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004158 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004159}
4160
Jens Axboe229a7b62020-06-22 10:13:11 -06004161static int io_send(struct io_kiocb *req, bool force_nonblock,
4162 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004163{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004164 struct io_sr_msg *sr = &req->sr_msg;
4165 struct msghdr msg;
4166 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004167 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004168 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004169 int ret;
4170
4171 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004172 if (unlikely(!sock))
4173 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004174
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004175 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4176 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004177 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004178
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004179 msg.msg_name = NULL;
4180 msg.msg_control = NULL;
4181 msg.msg_controllen = 0;
4182 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004183
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004184 flags = req->sr_msg.msg_flags;
4185 if (flags & MSG_DONTWAIT)
4186 req->flags |= REQ_F_NOWAIT;
4187 else if (force_nonblock)
4188 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004189
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004190 msg.msg_flags = flags;
4191 ret = sock_sendmsg(sock, &msg);
4192 if (force_nonblock && ret == -EAGAIN)
4193 return -EAGAIN;
4194 if (ret == -ERESTARTSYS)
4195 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004196
Jens Axboe03b12302019-12-02 18:50:25 -07004197 if (ret < 0)
4198 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004199 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004200 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004201}
4202
Pavel Begunkov1400e692020-07-12 20:41:05 +03004203static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4204 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004205{
4206 struct io_sr_msg *sr = &req->sr_msg;
4207 struct iovec __user *uiov;
4208 size_t iov_len;
4209 int ret;
4210
Pavel Begunkov1400e692020-07-12 20:41:05 +03004211 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4212 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004213 if (ret)
4214 return ret;
4215
4216 if (req->flags & REQ_F_BUFFER_SELECT) {
4217 if (iov_len > 1)
4218 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004219 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004220 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004221 sr->len = iomsg->iov[0].iov_len;
4222 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004223 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004224 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004225 } else {
4226 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004227 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004228 if (ret > 0)
4229 ret = 0;
4230 }
4231
4232 return ret;
4233}
4234
4235#ifdef CONFIG_COMPAT
4236static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004237 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004238{
4239 struct compat_msghdr __user *msg_compat;
4240 struct io_sr_msg *sr = &req->sr_msg;
4241 struct compat_iovec __user *uiov;
4242 compat_uptr_t ptr;
4243 compat_size_t len;
4244 int ret;
4245
Pavel Begunkov270a5942020-07-12 20:41:04 +03004246 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004247 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004248 &ptr, &len);
4249 if (ret)
4250 return ret;
4251
4252 uiov = compat_ptr(ptr);
4253 if (req->flags & REQ_F_BUFFER_SELECT) {
4254 compat_ssize_t clen;
4255
4256 if (len > 1)
4257 return -EINVAL;
4258 if (!access_ok(uiov, sizeof(*uiov)))
4259 return -EFAULT;
4260 if (__get_user(clen, &uiov->iov_len))
4261 return -EFAULT;
4262 if (clen < 0)
4263 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004264 sr->len = iomsg->iov[0].iov_len;
4265 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004266 } else {
4267 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004268 &iomsg->iov,
4269 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004270 if (ret < 0)
4271 return ret;
4272 }
4273
4274 return 0;
4275}
Jens Axboe03b12302019-12-02 18:50:25 -07004276#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004277
Pavel Begunkov1400e692020-07-12 20:41:05 +03004278static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4279 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004280{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004281 iomsg->msg.msg_name = &iomsg->addr;
4282 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004283
4284#ifdef CONFIG_COMPAT
4285 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004286 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004287#endif
4288
Pavel Begunkov1400e692020-07-12 20:41:05 +03004289 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004290}
4291
Jens Axboebcda7ba2020-02-23 16:42:51 -07004292static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004293 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004294{
4295 struct io_sr_msg *sr = &req->sr_msg;
4296 struct io_buffer *kbuf;
4297
Jens Axboebcda7ba2020-02-23 16:42:51 -07004298 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4299 if (IS_ERR(kbuf))
4300 return kbuf;
4301
4302 sr->kbuf = kbuf;
4303 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004304 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004305}
4306
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004307static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4308{
4309 return io_put_kbuf(req, req->sr_msg.kbuf);
4310}
4311
Jens Axboe3529d8c2019-12-19 18:24:38 -07004312static int io_recvmsg_prep(struct io_kiocb *req,
4313 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004314{
Jens Axboee47293f2019-12-20 08:58:21 -07004315 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004316 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004317 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004318
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004319 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4320 return -EINVAL;
4321
Jens Axboe3529d8c2019-12-19 18:24:38 -07004322 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004323 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004324 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004325 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004326
Jens Axboed8768362020-02-27 14:17:49 -07004327#ifdef CONFIG_COMPAT
4328 if (req->ctx->compat)
4329 sr->msg_flags |= MSG_CMSG_COMPAT;
4330#endif
4331
Jens Axboefddafac2020-01-04 20:19:44 -07004332 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004333 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004334 /* iovec is already imported */
4335 if (req->flags & REQ_F_NEED_CLEANUP)
4336 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004337
Pavel Begunkov1400e692020-07-12 20:41:05 +03004338 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004339 if (!ret)
4340 req->flags |= REQ_F_NEED_CLEANUP;
4341 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004342}
4343
Jens Axboe229a7b62020-06-22 10:13:11 -06004344static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4345 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004346{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004347 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004348 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004349 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004350 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004351 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004352
Jens Axboe0fa03c62019-04-19 13:34:07 -06004353 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004354 if (unlikely(!sock))
4355 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004356
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004357 if (req->io) {
4358 kmsg = &req->io->msg;
4359 kmsg->msg.msg_name = &req->io->msg.addr;
4360 /* if iov is set, it's allocated already */
4361 if (!kmsg->iov)
4362 kmsg->iov = kmsg->fast_iov;
4363 kmsg->msg.msg_iter.iov = kmsg->iov;
4364 } else {
4365 ret = io_recvmsg_copy_hdr(req, &iomsg);
4366 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004367 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004368 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004369 }
4370
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004371 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004372 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004373 if (IS_ERR(kbuf))
4374 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004375 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4376 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4377 1, req->sr_msg.len);
4378 }
4379
4380 flags = req->sr_msg.msg_flags;
4381 if (flags & MSG_DONTWAIT)
4382 req->flags |= REQ_F_NOWAIT;
4383 else if (force_nonblock)
4384 flags |= MSG_DONTWAIT;
4385
4386 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4387 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004388 if (force_nonblock && ret == -EAGAIN)
4389 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004390 if (ret == -ERESTARTSYS)
4391 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004392
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004393 if (req->flags & REQ_F_BUFFER_SELECTED)
4394 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004395 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004396 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004397 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004398 if (ret < 0)
4399 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004400 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004401 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004402}
4403
Jens Axboe229a7b62020-06-22 10:13:11 -06004404static int io_recv(struct io_kiocb *req, bool force_nonblock,
4405 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004406{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004407 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004408 struct io_sr_msg *sr = &req->sr_msg;
4409 struct msghdr msg;
4410 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004411 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004412 struct iovec iov;
4413 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004414 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004415
Jens Axboefddafac2020-01-04 20:19:44 -07004416 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004417 if (unlikely(!sock))
4418 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004419
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004420 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004421 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004422 if (IS_ERR(kbuf))
4423 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004424 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004425 }
4426
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004427 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004428 if (unlikely(ret))
4429 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004430
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004431 msg.msg_name = NULL;
4432 msg.msg_control = NULL;
4433 msg.msg_controllen = 0;
4434 msg.msg_namelen = 0;
4435 msg.msg_iocb = NULL;
4436 msg.msg_flags = 0;
4437
4438 flags = req->sr_msg.msg_flags;
4439 if (flags & MSG_DONTWAIT)
4440 req->flags |= REQ_F_NOWAIT;
4441 else if (force_nonblock)
4442 flags |= MSG_DONTWAIT;
4443
4444 ret = sock_recvmsg(sock, &msg, flags);
4445 if (force_nonblock && ret == -EAGAIN)
4446 return -EAGAIN;
4447 if (ret == -ERESTARTSYS)
4448 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004449out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004450 if (req->flags & REQ_F_BUFFER_SELECTED)
4451 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004452 if (ret < 0)
4453 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004454 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004455 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004456}
4457
Jens Axboe3529d8c2019-12-19 18:24:38 -07004458static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004459{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004460 struct io_accept *accept = &req->accept;
4461
Jens Axboe17f2fe32019-10-17 14:42:58 -06004462 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4463 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004464 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004465 return -EINVAL;
4466
Jens Axboed55e5f52019-12-11 16:12:15 -07004467 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4468 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004469 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004470 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004471 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004472}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004473
Jens Axboe229a7b62020-06-22 10:13:11 -06004474static int io_accept(struct io_kiocb *req, bool force_nonblock,
4475 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004476{
4477 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004478 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004479 int ret;
4480
Jiufei Xuee697dee2020-06-10 13:41:59 +08004481 if (req->file->f_flags & O_NONBLOCK)
4482 req->flags |= REQ_F_NOWAIT;
4483
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004484 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004485 accept->addr_len, accept->flags,
4486 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004487 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004488 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004489 if (ret < 0) {
4490 if (ret == -ERESTARTSYS)
4491 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004492 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004493 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004494 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004495 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004496}
4497
Jens Axboe3529d8c2019-12-19 18:24:38 -07004498static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004499{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004500 struct io_connect *conn = &req->connect;
4501 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004502
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004503 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4504 return -EINVAL;
4505 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4506 return -EINVAL;
4507
Jens Axboe3529d8c2019-12-19 18:24:38 -07004508 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4509 conn->addr_len = READ_ONCE(sqe->addr2);
4510
4511 if (!io)
4512 return 0;
4513
4514 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004515 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004516}
4517
Jens Axboe229a7b62020-06-22 10:13:11 -06004518static int io_connect(struct io_kiocb *req, bool force_nonblock,
4519 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004520{
Jens Axboef499a022019-12-02 16:28:46 -07004521 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004522 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004523 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004524
Jens Axboef499a022019-12-02 16:28:46 -07004525 if (req->io) {
4526 io = req->io;
4527 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004528 ret = move_addr_to_kernel(req->connect.addr,
4529 req->connect.addr_len,
4530 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004531 if (ret)
4532 goto out;
4533 io = &__io;
4534 }
4535
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004536 file_flags = force_nonblock ? O_NONBLOCK : 0;
4537
4538 ret = __sys_connect_file(req->file, &io->connect.address,
4539 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004540 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004541 if (req->io)
4542 return -EAGAIN;
4543 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004544 ret = -ENOMEM;
4545 goto out;
4546 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004547 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004548 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004549 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004550 if (ret == -ERESTARTSYS)
4551 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004552out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004553 if (ret < 0)
4554 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004555 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004556 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004557}
YueHaibing469956e2020-03-04 15:53:52 +08004558#else /* !CONFIG_NET */
4559static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4560{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004561 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004562}
4563
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004564static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4565 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004566{
YueHaibing469956e2020-03-04 15:53:52 +08004567 return -EOPNOTSUPP;
4568}
4569
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004570static int io_send(struct io_kiocb *req, bool force_nonblock,
4571 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004572{
4573 return -EOPNOTSUPP;
4574}
4575
4576static int io_recvmsg_prep(struct io_kiocb *req,
4577 const struct io_uring_sqe *sqe)
4578{
4579 return -EOPNOTSUPP;
4580}
4581
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004582static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4583 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004584{
4585 return -EOPNOTSUPP;
4586}
4587
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004588static int io_recv(struct io_kiocb *req, bool force_nonblock,
4589 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004590{
4591 return -EOPNOTSUPP;
4592}
4593
4594static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4595{
4596 return -EOPNOTSUPP;
4597}
4598
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004599static int io_accept(struct io_kiocb *req, bool force_nonblock,
4600 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004601{
4602 return -EOPNOTSUPP;
4603}
4604
4605static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4606{
4607 return -EOPNOTSUPP;
4608}
4609
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004610static int io_connect(struct io_kiocb *req, bool force_nonblock,
4611 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004612{
4613 return -EOPNOTSUPP;
4614}
4615#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004616
Jens Axboed7718a92020-02-14 22:23:12 -07004617struct io_poll_table {
4618 struct poll_table_struct pt;
4619 struct io_kiocb *req;
4620 int error;
4621};
4622
Jens Axboed7718a92020-02-14 22:23:12 -07004623static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4624 __poll_t mask, task_work_func_t func)
4625{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004626 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004627 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004628
4629 /* for instances that support it check for an event match first: */
4630 if (mask && !(mask & poll->events))
4631 return 0;
4632
4633 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4634
4635 list_del_init(&poll->wait.entry);
4636
Jens Axboed7718a92020-02-14 22:23:12 -07004637 req->result = mask;
4638 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004639 percpu_ref_get(&req->ctx->refs);
4640
Jens Axboed7718a92020-02-14 22:23:12 -07004641 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004642 * If we using the signalfd wait_queue_head for this wakeup, then
4643 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4644 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4645 * either, as the normal wakeup will suffice.
4646 */
4647 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4648
4649 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004650 * If this fails, then the task is exiting. When a task exits, the
4651 * work gets canceled, so just cancel this request as well instead
4652 * of executing it. We can't safely execute it anyway, as we may not
4653 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004654 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004655 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004656 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004657 struct task_struct *tsk;
4658
Jens Axboee3aabf92020-05-18 11:04:17 -06004659 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004660 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004661 task_work_add(tsk, &req->task_work, 0);
4662 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004663 }
Jens Axboed7718a92020-02-14 22:23:12 -07004664 return 1;
4665}
4666
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004667static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4668 __acquires(&req->ctx->completion_lock)
4669{
4670 struct io_ring_ctx *ctx = req->ctx;
4671
4672 if (!req->result && !READ_ONCE(poll->canceled)) {
4673 struct poll_table_struct pt = { ._key = poll->events };
4674
4675 req->result = vfs_poll(req->file, &pt) & poll->events;
4676 }
4677
4678 spin_lock_irq(&ctx->completion_lock);
4679 if (!req->result && !READ_ONCE(poll->canceled)) {
4680 add_wait_queue(poll->head, &poll->wait);
4681 return true;
4682 }
4683
4684 return false;
4685}
4686
Jens Axboed4e7cd32020-08-15 11:44:50 -07004687static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004688{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004689 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4690 if (req->opcode == IORING_OP_POLL_ADD)
4691 return (struct io_poll_iocb *) req->io;
4692 return req->apoll->double_poll;
4693}
4694
4695static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4696{
4697 if (req->opcode == IORING_OP_POLL_ADD)
4698 return &req->poll;
4699 return &req->apoll->poll;
4700}
4701
4702static void io_poll_remove_double(struct io_kiocb *req)
4703{
4704 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004705
4706 lockdep_assert_held(&req->ctx->completion_lock);
4707
4708 if (poll && poll->head) {
4709 struct wait_queue_head *head = poll->head;
4710
4711 spin_lock(&head->lock);
4712 list_del_init(&poll->wait.entry);
4713 if (poll->wait.private)
4714 refcount_dec(&req->refs);
4715 poll->head = NULL;
4716 spin_unlock(&head->lock);
4717 }
4718}
4719
4720static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4721{
4722 struct io_ring_ctx *ctx = req->ctx;
4723
Jens Axboed4e7cd32020-08-15 11:44:50 -07004724 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004725 req->poll.done = true;
4726 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4727 io_commit_cqring(ctx);
4728}
4729
4730static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4731{
4732 struct io_ring_ctx *ctx = req->ctx;
4733
4734 if (io_poll_rewait(req, &req->poll)) {
4735 spin_unlock_irq(&ctx->completion_lock);
4736 return;
4737 }
4738
4739 hash_del(&req->hash_node);
4740 io_poll_complete(req, req->result, 0);
4741 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03004742 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004743 spin_unlock_irq(&ctx->completion_lock);
4744
4745 io_cqring_ev_posted(ctx);
4746}
4747
4748static void io_poll_task_func(struct callback_head *cb)
4749{
4750 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004751 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004752 struct io_kiocb *nxt = NULL;
4753
4754 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004755 if (nxt)
4756 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004757 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004758}
4759
4760static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4761 int sync, void *key)
4762{
4763 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004764 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004765 __poll_t mask = key_to_poll(key);
4766
4767 /* for instances that support it check for an event match first: */
4768 if (mask && !(mask & poll->events))
4769 return 0;
4770
Jens Axboe8706e042020-09-28 08:38:54 -06004771 list_del_init(&wait->entry);
4772
Jens Axboe807abcb2020-07-17 17:09:27 -06004773 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004774 bool done;
4775
Jens Axboe807abcb2020-07-17 17:09:27 -06004776 spin_lock(&poll->head->lock);
4777 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004778 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004779 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004780 /* make sure double remove sees this as being gone */
4781 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004782 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004783 if (!done)
4784 __io_async_wake(req, poll, mask, io_poll_task_func);
4785 }
4786 refcount_dec(&req->refs);
4787 return 1;
4788}
4789
4790static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4791 wait_queue_func_t wake_func)
4792{
4793 poll->head = NULL;
4794 poll->done = false;
4795 poll->canceled = false;
4796 poll->events = events;
4797 INIT_LIST_HEAD(&poll->wait.entry);
4798 init_waitqueue_func_entry(&poll->wait, wake_func);
4799}
4800
4801static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004802 struct wait_queue_head *head,
4803 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004804{
4805 struct io_kiocb *req = pt->req;
4806
4807 /*
4808 * If poll->head is already set, it's because the file being polled
4809 * uses multiple waitqueues for poll handling (eg one for read, one
4810 * for write). Setup a separate io_poll_iocb if this happens.
4811 */
4812 if (unlikely(poll->head)) {
4813 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004814 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004815 pt->error = -EINVAL;
4816 return;
4817 }
4818 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4819 if (!poll) {
4820 pt->error = -ENOMEM;
4821 return;
4822 }
4823 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4824 refcount_inc(&req->refs);
4825 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004826 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004827 }
4828
4829 pt->error = 0;
4830 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004831
4832 if (poll->events & EPOLLEXCLUSIVE)
4833 add_wait_queue_exclusive(head, &poll->wait);
4834 else
4835 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004836}
4837
4838static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4839 struct poll_table_struct *p)
4840{
4841 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004842 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004843
Jens Axboe807abcb2020-07-17 17:09:27 -06004844 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004845}
4846
Jens Axboed7718a92020-02-14 22:23:12 -07004847static void io_async_task_func(struct callback_head *cb)
4848{
4849 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4850 struct async_poll *apoll = req->apoll;
4851 struct io_ring_ctx *ctx = req->ctx;
4852
4853 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4854
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004855 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004856 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004857 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004858 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004859 }
4860
Jens Axboe31067252020-05-17 17:43:31 -06004861 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004862 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004863 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004864
Jens Axboed4e7cd32020-08-15 11:44:50 -07004865 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004866 spin_unlock_irq(&ctx->completion_lock);
4867
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004868 if (!READ_ONCE(apoll->poll.canceled))
4869 __io_req_task_submit(req);
4870 else
4871 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004872
Jens Axboe6d816e02020-08-11 08:04:14 -06004873 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004874 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004875 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004876}
4877
4878static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4879 void *key)
4880{
4881 struct io_kiocb *req = wait->private;
4882 struct io_poll_iocb *poll = &req->apoll->poll;
4883
4884 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4885 key_to_poll(key));
4886
4887 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4888}
4889
4890static void io_poll_req_insert(struct io_kiocb *req)
4891{
4892 struct io_ring_ctx *ctx = req->ctx;
4893 struct hlist_head *list;
4894
4895 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4896 hlist_add_head(&req->hash_node, list);
4897}
4898
4899static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4900 struct io_poll_iocb *poll,
4901 struct io_poll_table *ipt, __poll_t mask,
4902 wait_queue_func_t wake_func)
4903 __acquires(&ctx->completion_lock)
4904{
4905 struct io_ring_ctx *ctx = req->ctx;
4906 bool cancel = false;
4907
Jens Axboe18bceab2020-05-15 11:56:54 -06004908 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004909 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004910 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004911
4912 ipt->pt._key = mask;
4913 ipt->req = req;
4914 ipt->error = -EINVAL;
4915
Jens Axboed7718a92020-02-14 22:23:12 -07004916 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4917
4918 spin_lock_irq(&ctx->completion_lock);
4919 if (likely(poll->head)) {
4920 spin_lock(&poll->head->lock);
4921 if (unlikely(list_empty(&poll->wait.entry))) {
4922 if (ipt->error)
4923 cancel = true;
4924 ipt->error = 0;
4925 mask = 0;
4926 }
4927 if (mask || ipt->error)
4928 list_del_init(&poll->wait.entry);
4929 else if (cancel)
4930 WRITE_ONCE(poll->canceled, true);
4931 else if (!poll->done) /* actually waiting for an event */
4932 io_poll_req_insert(req);
4933 spin_unlock(&poll->head->lock);
4934 }
4935
4936 return mask;
4937}
4938
4939static bool io_arm_poll_handler(struct io_kiocb *req)
4940{
4941 const struct io_op_def *def = &io_op_defs[req->opcode];
4942 struct io_ring_ctx *ctx = req->ctx;
4943 struct async_poll *apoll;
4944 struct io_poll_table ipt;
4945 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004946 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004947
4948 if (!req->file || !file_can_poll(req->file))
4949 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004950 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004951 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004952 if (def->pollin)
4953 rw = READ;
4954 else if (def->pollout)
4955 rw = WRITE;
4956 else
4957 return false;
4958 /* if we can't nonblock try, then no point in arming a poll handler */
4959 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07004960 return false;
4961
4962 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4963 if (unlikely(!apoll))
4964 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004965 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004966
4967 req->flags |= REQ_F_POLLED;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004968 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004969 req->apoll = apoll;
4970 INIT_HLIST_NODE(&req->hash_node);
4971
Nathan Chancellor8755d972020-03-02 16:01:19 -07004972 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004973 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004974 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004975 if (def->pollout)
4976 mask |= POLLOUT | POLLWRNORM;
4977 mask |= POLLERR | POLLPRI;
4978
4979 ipt.pt._qproc = io_async_queue_proc;
4980
4981 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4982 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06004983 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07004984 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004985 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06004986 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004987 kfree(apoll);
4988 return false;
4989 }
4990 spin_unlock_irq(&ctx->completion_lock);
4991 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4992 apoll->poll.events);
4993 return true;
4994}
4995
4996static bool __io_poll_remove_one(struct io_kiocb *req,
4997 struct io_poll_iocb *poll)
4998{
Jens Axboeb41e9852020-02-17 09:52:41 -07004999 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005000
5001 spin_lock(&poll->head->lock);
5002 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005003 if (!list_empty(&poll->wait.entry)) {
5004 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005005 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005006 }
5007 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005008 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005009 return do_complete;
5010}
5011
5012static bool io_poll_remove_one(struct io_kiocb *req)
5013{
5014 bool do_complete;
5015
Jens Axboed4e7cd32020-08-15 11:44:50 -07005016 io_poll_remove_double(req);
5017
Jens Axboed7718a92020-02-14 22:23:12 -07005018 if (req->opcode == IORING_OP_POLL_ADD) {
5019 do_complete = __io_poll_remove_one(req, &req->poll);
5020 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005021 struct async_poll *apoll = req->apoll;
5022
Jens Axboed7718a92020-02-14 22:23:12 -07005023 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005024 do_complete = __io_poll_remove_one(req, &apoll->poll);
5025 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005026 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005027 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005028 kfree(apoll);
5029 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005030 }
5031
Jens Axboeb41e9852020-02-17 09:52:41 -07005032 if (do_complete) {
5033 io_cqring_fill_event(req, -ECANCELED);
5034 io_commit_cqring(req->ctx);
5035 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005036 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005037 io_put_req(req);
5038 }
5039
5040 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005041}
5042
Jens Axboef3606e32020-09-22 08:18:24 -06005043static void io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005044{
Jens Axboe78076bb2019-12-04 19:56:40 -07005045 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005046 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005047 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005048
5049 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005050 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5051 struct hlist_head *list;
5052
5053 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005054 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5055 if (io_task_match(req, tsk))
5056 posted += io_poll_remove_one(req);
5057 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005058 }
5059 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005060
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005061 if (posted)
5062 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005063}
5064
Jens Axboe47f46762019-11-09 17:43:02 -07005065static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5066{
Jens Axboe78076bb2019-12-04 19:56:40 -07005067 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005068 struct io_kiocb *req;
5069
Jens Axboe78076bb2019-12-04 19:56:40 -07005070 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5071 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005072 if (sqe_addr != req->user_data)
5073 continue;
5074 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005075 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005076 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005077 }
5078
5079 return -ENOENT;
5080}
5081
Jens Axboe3529d8c2019-12-19 18:24:38 -07005082static int io_poll_remove_prep(struct io_kiocb *req,
5083 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005084{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005085 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5086 return -EINVAL;
5087 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5088 sqe->poll_events)
5089 return -EINVAL;
5090
Jens Axboe0969e782019-12-17 18:40:57 -07005091 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005092 return 0;
5093}
5094
5095/*
5096 * Find a running poll command that matches one specified in sqe->addr,
5097 * and remove it if found.
5098 */
5099static int io_poll_remove(struct io_kiocb *req)
5100{
5101 struct io_ring_ctx *ctx = req->ctx;
5102 u64 addr;
5103 int ret;
5104
Jens Axboe0969e782019-12-17 18:40:57 -07005105 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005106 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005107 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005108 spin_unlock_irq(&ctx->completion_lock);
5109
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005110 if (ret < 0)
5111 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005112 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005113 return 0;
5114}
5115
Jens Axboe221c5eb2019-01-17 09:41:58 -07005116static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5117 void *key)
5118{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005119 struct io_kiocb *req = wait->private;
5120 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005121
Jens Axboed7718a92020-02-14 22:23:12 -07005122 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005123}
5124
Jens Axboe221c5eb2019-01-17 09:41:58 -07005125static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5126 struct poll_table_struct *p)
5127{
5128 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5129
Jens Axboe807abcb2020-07-17 17:09:27 -06005130 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005131}
5132
Jens Axboe3529d8c2019-12-19 18:24:38 -07005133static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005134{
5135 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005136 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005137
5138 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5139 return -EINVAL;
5140 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5141 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005142 if (!poll->file)
5143 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005144
Jiufei Xue5769a352020-06-17 17:53:55 +08005145 events = READ_ONCE(sqe->poll32_events);
5146#ifdef __BIG_ENDIAN
5147 events = swahw32(events);
5148#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005149 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5150 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005151
Pavel Begunkov4dd28242020-06-15 10:33:13 +03005152 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07005153 return 0;
5154}
5155
Pavel Begunkov014db002020-03-03 21:33:12 +03005156static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005157{
5158 struct io_poll_iocb *poll = &req->poll;
5159 struct io_ring_ctx *ctx = req->ctx;
5160 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005161 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005162
Jens Axboe78076bb2019-12-04 19:56:40 -07005163 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005164 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005165
Jens Axboed7718a92020-02-14 22:23:12 -07005166 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5167 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005168
Jens Axboe8c838782019-03-12 15:48:16 -06005169 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005170 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005171 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005172 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005173 spin_unlock_irq(&ctx->completion_lock);
5174
Jens Axboe8c838782019-03-12 15:48:16 -06005175 if (mask) {
5176 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005177 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005178 }
Jens Axboe8c838782019-03-12 15:48:16 -06005179 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005180}
5181
Jens Axboe5262f562019-09-17 12:26:57 -06005182static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5183{
Jens Axboead8a48a2019-11-15 08:49:11 -07005184 struct io_timeout_data *data = container_of(timer,
5185 struct io_timeout_data, timer);
5186 struct io_kiocb *req = data->req;
5187 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005188 unsigned long flags;
5189
Jens Axboe5262f562019-09-17 12:26:57 -06005190 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005191 atomic_set(&req->ctx->cq_timeouts,
5192 atomic_read(&req->ctx->cq_timeouts) + 1);
5193
zhangyi (F)ef036812019-10-23 15:10:08 +08005194 /*
Jens Axboe11365042019-10-16 09:08:32 -06005195 * We could be racing with timeout deletion. If the list is empty,
5196 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005197 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005198 if (!list_empty(&req->timeout.list))
5199 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005200
Jens Axboe78e19bb2019-11-06 15:21:34 -07005201 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005202 io_commit_cqring(ctx);
5203 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5204
5205 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005206 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005207 io_put_req(req);
5208 return HRTIMER_NORESTART;
5209}
5210
Jens Axboef254ac02020-08-12 17:33:30 -06005211static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005212{
Jens Axboef254ac02020-08-12 17:33:30 -06005213 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005214
Jens Axboef254ac02020-08-12 17:33:30 -06005215 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005216
Jens Axboe2d283902019-12-04 11:08:05 -07005217 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005218 if (ret == -1)
5219 return -EALREADY;
5220
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005221 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005222 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005223 io_cqring_fill_event(req, -ECANCELED);
5224 io_put_req(req);
5225 return 0;
5226}
5227
Jens Axboef254ac02020-08-12 17:33:30 -06005228static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5229{
5230 struct io_kiocb *req;
5231 int ret = -ENOENT;
5232
5233 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5234 if (user_data == req->user_data) {
5235 ret = 0;
5236 break;
5237 }
5238 }
5239
5240 if (ret == -ENOENT)
5241 return ret;
5242
5243 return __io_timeout_cancel(req);
5244}
5245
Jens Axboe3529d8c2019-12-19 18:24:38 -07005246static int io_timeout_remove_prep(struct io_kiocb *req,
5247 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005248{
Jens Axboeb29472e2019-12-17 18:50:29 -07005249 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5250 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005251 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5252 return -EINVAL;
5253 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005254 return -EINVAL;
5255
5256 req->timeout.addr = READ_ONCE(sqe->addr);
5257 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5258 if (req->timeout.flags)
5259 return -EINVAL;
5260
Jens Axboeb29472e2019-12-17 18:50:29 -07005261 return 0;
5262}
5263
Jens Axboe11365042019-10-16 09:08:32 -06005264/*
5265 * Remove or update an existing timeout command
5266 */
Jens Axboefc4df992019-12-10 14:38:45 -07005267static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005268{
5269 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005270 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005271
Jens Axboe11365042019-10-16 09:08:32 -06005272 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005273 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005274
Jens Axboe47f46762019-11-09 17:43:02 -07005275 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005276 io_commit_cqring(ctx);
5277 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005278 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005279 if (ret < 0)
5280 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005281 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005282 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005283}
5284
Jens Axboe3529d8c2019-12-19 18:24:38 -07005285static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005286 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005287{
Jens Axboead8a48a2019-11-15 08:49:11 -07005288 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005289 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005290 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005291
Jens Axboead8a48a2019-11-15 08:49:11 -07005292 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005293 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005294 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005295 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005296 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005297 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005298 flags = READ_ONCE(sqe->timeout_flags);
5299 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005300 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005301
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005302 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005303
Jens Axboe3529d8c2019-12-19 18:24:38 -07005304 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005305 return -ENOMEM;
5306
5307 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005308 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005309
5310 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005311 return -EFAULT;
5312
Jens Axboe11365042019-10-16 09:08:32 -06005313 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005314 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005315 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005316 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005317
Jens Axboead8a48a2019-11-15 08:49:11 -07005318 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5319 return 0;
5320}
5321
Jens Axboefc4df992019-12-10 14:38:45 -07005322static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005323{
Jens Axboead8a48a2019-11-15 08:49:11 -07005324 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005325 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005326 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005327 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005328
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005329 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005330
Jens Axboe5262f562019-09-17 12:26:57 -06005331 /*
5332 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005333 * timeout event to be satisfied. If it isn't set, then this is
5334 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005335 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005336 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005337 entry = ctx->timeout_list.prev;
5338 goto add;
5339 }
Jens Axboe5262f562019-09-17 12:26:57 -06005340
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005341 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5342 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005343
5344 /*
5345 * Insertion sort, ensuring the first entry in the list is always
5346 * the one we need first.
5347 */
Jens Axboe5262f562019-09-17 12:26:57 -06005348 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005349 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5350 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005351
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005352 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005353 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005354 /* nxt.seq is behind @tail, otherwise would've been completed */
5355 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005356 break;
5357 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005358add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005359 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005360 data->timer.function = io_timeout_fn;
5361 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005362 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005363 return 0;
5364}
5365
Jens Axboe62755e32019-10-28 21:49:21 -06005366static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005367{
Jens Axboe62755e32019-10-28 21:49:21 -06005368 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005369
Jens Axboe62755e32019-10-28 21:49:21 -06005370 return req->user_data == (unsigned long) data;
5371}
5372
Jens Axboee977d6d2019-11-05 12:39:45 -07005373static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005374{
Jens Axboe62755e32019-10-28 21:49:21 -06005375 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005376 int ret = 0;
5377
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005378 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005379 switch (cancel_ret) {
5380 case IO_WQ_CANCEL_OK:
5381 ret = 0;
5382 break;
5383 case IO_WQ_CANCEL_RUNNING:
5384 ret = -EALREADY;
5385 break;
5386 case IO_WQ_CANCEL_NOTFOUND:
5387 ret = -ENOENT;
5388 break;
5389 }
5390
Jens Axboee977d6d2019-11-05 12:39:45 -07005391 return ret;
5392}
5393
Jens Axboe47f46762019-11-09 17:43:02 -07005394static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5395 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005396 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005397{
5398 unsigned long flags;
5399 int ret;
5400
5401 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5402 if (ret != -ENOENT) {
5403 spin_lock_irqsave(&ctx->completion_lock, flags);
5404 goto done;
5405 }
5406
5407 spin_lock_irqsave(&ctx->completion_lock, flags);
5408 ret = io_timeout_cancel(ctx, sqe_addr);
5409 if (ret != -ENOENT)
5410 goto done;
5411 ret = io_poll_cancel(ctx, sqe_addr);
5412done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005413 if (!ret)
5414 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005415 io_cqring_fill_event(req, ret);
5416 io_commit_cqring(ctx);
5417 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5418 io_cqring_ev_posted(ctx);
5419
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005420 if (ret < 0)
5421 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005422 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005423}
5424
Jens Axboe3529d8c2019-12-19 18:24:38 -07005425static int io_async_cancel_prep(struct io_kiocb *req,
5426 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005427{
Jens Axboefbf23842019-12-17 18:45:56 -07005428 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005429 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005430 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5431 return -EINVAL;
5432 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005433 return -EINVAL;
5434
Jens Axboefbf23842019-12-17 18:45:56 -07005435 req->cancel.addr = READ_ONCE(sqe->addr);
5436 return 0;
5437}
5438
Pavel Begunkov014db002020-03-03 21:33:12 +03005439static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005440{
5441 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005442
Pavel Begunkov014db002020-03-03 21:33:12 +03005443 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005444 return 0;
5445}
5446
Jens Axboe05f3fb32019-12-09 11:22:50 -07005447static int io_files_update_prep(struct io_kiocb *req,
5448 const struct io_uring_sqe *sqe)
5449{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005450 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5451 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005452 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5453 return -EINVAL;
5454 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005455 return -EINVAL;
5456
5457 req->files_update.offset = READ_ONCE(sqe->off);
5458 req->files_update.nr_args = READ_ONCE(sqe->len);
5459 if (!req->files_update.nr_args)
5460 return -EINVAL;
5461 req->files_update.arg = READ_ONCE(sqe->addr);
5462 return 0;
5463}
5464
Jens Axboe229a7b62020-06-22 10:13:11 -06005465static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5466 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005467{
5468 struct io_ring_ctx *ctx = req->ctx;
5469 struct io_uring_files_update up;
5470 int ret;
5471
Jens Axboef86cd202020-01-29 13:46:44 -07005472 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005473 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005474
5475 up.offset = req->files_update.offset;
5476 up.fds = req->files_update.arg;
5477
5478 mutex_lock(&ctx->uring_lock);
5479 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5480 mutex_unlock(&ctx->uring_lock);
5481
5482 if (ret < 0)
5483 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005484 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005485 return 0;
5486}
5487
Jens Axboe3529d8c2019-12-19 18:24:38 -07005488static int io_req_defer_prep(struct io_kiocb *req,
5489 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005490{
Jens Axboee7815732019-12-17 19:45:06 -07005491 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005492
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005493 if (!sqe)
5494 return 0;
5495
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005496 if (io_alloc_async_ctx(req))
5497 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005498 ret = io_prep_work_files(req);
5499 if (unlikely(ret))
5500 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005501
Jens Axboe202700e12020-09-12 13:18:10 -06005502 io_prep_async_work(req);
5503
Jens Axboed625c6e2019-12-17 19:53:05 -07005504 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005505 case IORING_OP_NOP:
5506 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005507 case IORING_OP_READV:
5508 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005509 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005510 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005511 break;
5512 case IORING_OP_WRITEV:
5513 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005514 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005515 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005516 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005517 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005518 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005519 break;
5520 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005521 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005522 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005523 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005524 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005525 break;
5526 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005527 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005528 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005529 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005530 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005531 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005532 break;
5533 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005534 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005535 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005536 break;
Jens Axboef499a022019-12-02 16:28:46 -07005537 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005538 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005539 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005540 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005541 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005542 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005543 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005544 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005545 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005546 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005547 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005548 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005549 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005550 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005551 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005552 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005553 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005554 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005555 case IORING_OP_FALLOCATE:
5556 ret = io_fallocate_prep(req, sqe);
5557 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005558 case IORING_OP_OPENAT:
5559 ret = io_openat_prep(req, sqe);
5560 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005561 case IORING_OP_CLOSE:
5562 ret = io_close_prep(req, sqe);
5563 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005564 case IORING_OP_FILES_UPDATE:
5565 ret = io_files_update_prep(req, sqe);
5566 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005567 case IORING_OP_STATX:
5568 ret = io_statx_prep(req, sqe);
5569 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005570 case IORING_OP_FADVISE:
5571 ret = io_fadvise_prep(req, sqe);
5572 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005573 case IORING_OP_MADVISE:
5574 ret = io_madvise_prep(req, sqe);
5575 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005576 case IORING_OP_OPENAT2:
5577 ret = io_openat2_prep(req, sqe);
5578 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005579 case IORING_OP_EPOLL_CTL:
5580 ret = io_epoll_ctl_prep(req, sqe);
5581 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005582 case IORING_OP_SPLICE:
5583 ret = io_splice_prep(req, sqe);
5584 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005585 case IORING_OP_PROVIDE_BUFFERS:
5586 ret = io_provide_buffers_prep(req, sqe);
5587 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005588 case IORING_OP_REMOVE_BUFFERS:
5589 ret = io_remove_buffers_prep(req, sqe);
5590 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005591 case IORING_OP_TEE:
5592 ret = io_tee_prep(req, sqe);
5593 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005594 default:
Jens Axboee7815732019-12-17 19:45:06 -07005595 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5596 req->opcode);
5597 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005598 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005599 }
5600
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005601 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005602}
5603
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005604static u32 io_get_sequence(struct io_kiocb *req)
5605{
5606 struct io_kiocb *pos;
5607 struct io_ring_ctx *ctx = req->ctx;
5608 u32 total_submitted, nr_reqs = 1;
5609
5610 if (req->flags & REQ_F_LINK_HEAD)
5611 list_for_each_entry(pos, &req->link_list, link_list)
5612 nr_reqs++;
5613
5614 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5615 return total_submitted - nr_reqs;
5616}
5617
Jens Axboe3529d8c2019-12-19 18:24:38 -07005618static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005619{
Jackie Liua197f662019-11-08 08:09:12 -07005620 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005621 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005622 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005623 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005624
Bob Liu9d858b22019-11-13 18:06:25 +08005625 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005626 if (likely(list_empty_careful(&ctx->defer_list) &&
5627 !(req->flags & REQ_F_IO_DRAIN)))
5628 return 0;
5629
5630 seq = io_get_sequence(req);
5631 /* Still a chance to pass the sequence check */
5632 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005633 return 0;
5634
Pavel Begunkov650b5482020-05-17 14:02:11 +03005635 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005636 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005637 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005638 return ret;
5639 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005640 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005641 de = kmalloc(sizeof(*de), GFP_KERNEL);
5642 if (!de)
5643 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005644
Jens Axboede0617e2019-04-06 21:51:27 -06005645 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005646 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005647 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005648 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005649 io_queue_async_work(req);
5650 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005651 }
5652
Jens Axboe915967f2019-11-21 09:01:20 -07005653 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005654 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005655 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005656 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005657 spin_unlock_irq(&ctx->completion_lock);
5658 return -EIOCBQUEUED;
5659}
5660
Jens Axboef573d382020-09-22 10:19:24 -06005661static void io_req_drop_files(struct io_kiocb *req)
5662{
5663 struct io_ring_ctx *ctx = req->ctx;
5664 unsigned long flags;
5665
5666 spin_lock_irqsave(&ctx->inflight_lock, flags);
5667 list_del(&req->inflight_entry);
5668 if (waitqueue_active(&ctx->inflight_wait))
5669 wake_up(&ctx->inflight_wait);
5670 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5671 req->flags &= ~REQ_F_INFLIGHT;
5672 req->work.files = NULL;
5673}
5674
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005675static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005676{
5677 struct io_async_ctx *io = req->io;
5678
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005679 if (req->flags & REQ_F_BUFFER_SELECTED) {
5680 switch (req->opcode) {
5681 case IORING_OP_READV:
5682 case IORING_OP_READ_FIXED:
5683 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005684 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005685 break;
5686 case IORING_OP_RECVMSG:
5687 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005688 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005689 break;
5690 }
5691 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005692 }
5693
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005694 if (req->flags & REQ_F_NEED_CLEANUP) {
5695 switch (req->opcode) {
5696 case IORING_OP_READV:
5697 case IORING_OP_READ_FIXED:
5698 case IORING_OP_READ:
5699 case IORING_OP_WRITEV:
5700 case IORING_OP_WRITE_FIXED:
5701 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005702 if (io->rw.free_iovec)
5703 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005704 break;
5705 case IORING_OP_RECVMSG:
5706 case IORING_OP_SENDMSG:
5707 if (io->msg.iov != io->msg.fast_iov)
5708 kfree(io->msg.iov);
5709 break;
5710 case IORING_OP_SPLICE:
5711 case IORING_OP_TEE:
5712 io_put_file(req, req->splice.file_in,
5713 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5714 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005715 case IORING_OP_OPENAT:
5716 case IORING_OP_OPENAT2:
5717 if (req->open.filename)
5718 putname(req->open.filename);
5719 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005720 }
5721 req->flags &= ~REQ_F_NEED_CLEANUP;
5722 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005723
Jens Axboef573d382020-09-22 10:19:24 -06005724 if (req->flags & REQ_F_INFLIGHT)
5725 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005726}
5727
Jens Axboe3529d8c2019-12-19 18:24:38 -07005728static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005729 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005730{
Jackie Liua197f662019-11-08 08:09:12 -07005731 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005732 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005733
Jens Axboed625c6e2019-12-17 19:53:05 -07005734 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005735 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005736 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005737 break;
5738 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005739 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005740 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005741 if (sqe) {
5742 ret = io_read_prep(req, sqe, force_nonblock);
5743 if (ret < 0)
5744 break;
5745 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005746 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005747 break;
5748 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005749 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005750 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005751 if (sqe) {
5752 ret = io_write_prep(req, sqe, force_nonblock);
5753 if (ret < 0)
5754 break;
5755 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005756 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005757 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005758 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005759 if (sqe) {
5760 ret = io_prep_fsync(req, sqe);
5761 if (ret < 0)
5762 break;
5763 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005764 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005765 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005766 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005767 if (sqe) {
5768 ret = io_poll_add_prep(req, sqe);
5769 if (ret)
5770 break;
5771 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005772 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005773 break;
5774 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005775 if (sqe) {
5776 ret = io_poll_remove_prep(req, sqe);
5777 if (ret < 0)
5778 break;
5779 }
Jens Axboefc4df992019-12-10 14:38:45 -07005780 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005781 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005782 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005783 if (sqe) {
5784 ret = io_prep_sfr(req, sqe);
5785 if (ret < 0)
5786 break;
5787 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005788 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005789 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005790 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005791 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005792 if (sqe) {
5793 ret = io_sendmsg_prep(req, sqe);
5794 if (ret < 0)
5795 break;
5796 }
Jens Axboefddafac2020-01-04 20:19:44 -07005797 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005798 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005799 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005800 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005801 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005802 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005803 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005804 if (sqe) {
5805 ret = io_recvmsg_prep(req, sqe);
5806 if (ret)
5807 break;
5808 }
Jens Axboefddafac2020-01-04 20:19:44 -07005809 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005810 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005811 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005812 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005813 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005814 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005815 if (sqe) {
5816 ret = io_timeout_prep(req, sqe, false);
5817 if (ret)
5818 break;
5819 }
Jens Axboefc4df992019-12-10 14:38:45 -07005820 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005821 break;
Jens Axboe11365042019-10-16 09:08:32 -06005822 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005823 if (sqe) {
5824 ret = io_timeout_remove_prep(req, sqe);
5825 if (ret)
5826 break;
5827 }
Jens Axboefc4df992019-12-10 14:38:45 -07005828 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005829 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005830 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005831 if (sqe) {
5832 ret = io_accept_prep(req, sqe);
5833 if (ret)
5834 break;
5835 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005836 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005837 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005838 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005839 if (sqe) {
5840 ret = io_connect_prep(req, sqe);
5841 if (ret)
5842 break;
5843 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005844 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005845 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005846 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005847 if (sqe) {
5848 ret = io_async_cancel_prep(req, sqe);
5849 if (ret)
5850 break;
5851 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005852 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005853 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005854 case IORING_OP_FALLOCATE:
5855 if (sqe) {
5856 ret = io_fallocate_prep(req, sqe);
5857 if (ret)
5858 break;
5859 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005860 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005861 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005862 case IORING_OP_OPENAT:
5863 if (sqe) {
5864 ret = io_openat_prep(req, sqe);
5865 if (ret)
5866 break;
5867 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005868 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005869 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005870 case IORING_OP_CLOSE:
5871 if (sqe) {
5872 ret = io_close_prep(req, sqe);
5873 if (ret)
5874 break;
5875 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005876 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005877 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005878 case IORING_OP_FILES_UPDATE:
5879 if (sqe) {
5880 ret = io_files_update_prep(req, sqe);
5881 if (ret)
5882 break;
5883 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005884 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005885 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005886 case IORING_OP_STATX:
5887 if (sqe) {
5888 ret = io_statx_prep(req, sqe);
5889 if (ret)
5890 break;
5891 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005892 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005893 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005894 case IORING_OP_FADVISE:
5895 if (sqe) {
5896 ret = io_fadvise_prep(req, sqe);
5897 if (ret)
5898 break;
5899 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005900 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005901 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005902 case IORING_OP_MADVISE:
5903 if (sqe) {
5904 ret = io_madvise_prep(req, sqe);
5905 if (ret)
5906 break;
5907 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005908 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005909 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005910 case IORING_OP_OPENAT2:
5911 if (sqe) {
5912 ret = io_openat2_prep(req, sqe);
5913 if (ret)
5914 break;
5915 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005916 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005917 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005918 case IORING_OP_EPOLL_CTL:
5919 if (sqe) {
5920 ret = io_epoll_ctl_prep(req, sqe);
5921 if (ret)
5922 break;
5923 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005924 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005925 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005926 case IORING_OP_SPLICE:
5927 if (sqe) {
5928 ret = io_splice_prep(req, sqe);
5929 if (ret < 0)
5930 break;
5931 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005932 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005933 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005934 case IORING_OP_PROVIDE_BUFFERS:
5935 if (sqe) {
5936 ret = io_provide_buffers_prep(req, sqe);
5937 if (ret)
5938 break;
5939 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005940 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005941 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005942 case IORING_OP_REMOVE_BUFFERS:
5943 if (sqe) {
5944 ret = io_remove_buffers_prep(req, sqe);
5945 if (ret)
5946 break;
5947 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005948 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005949 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005950 case IORING_OP_TEE:
5951 if (sqe) {
5952 ret = io_tee_prep(req, sqe);
5953 if (ret < 0)
5954 break;
5955 }
5956 ret = io_tee(req, force_nonblock);
5957 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005958 default:
5959 ret = -EINVAL;
5960 break;
5961 }
5962
5963 if (ret)
5964 return ret;
5965
Jens Axboeb5325762020-05-19 21:20:27 -06005966 /* If the op doesn't have a file, we're not polling for it */
5967 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005968 const bool in_async = io_wq_current_is_worker();
5969
Jens Axboe11ba8202020-01-15 21:51:17 -07005970 /* workqueue context doesn't hold uring_lock, grab it now */
5971 if (in_async)
5972 mutex_lock(&ctx->uring_lock);
5973
Jens Axboe2b188cc2019-01-07 10:46:33 -07005974 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005975
5976 if (in_async)
5977 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005978 }
5979
5980 return 0;
5981}
5982
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005983static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005984{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005985 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005986 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005987 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005988
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005989 timeout = io_prep_linked_timeout(req);
5990 if (timeout)
5991 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005992
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005993 /* if NO_CANCEL is set, we must still run the work */
5994 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5995 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005996 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005997 }
Jens Axboe31b51512019-01-18 22:56:34 -07005998
Jens Axboe561fb042019-10-24 07:25:42 -06005999 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006000 do {
Jens Axboef13fad72020-06-22 09:34:30 -06006001 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06006002 /*
6003 * We can get EAGAIN for polled IO even though we're
6004 * forcing a sync submission from here, since we can't
6005 * wait for request slots on the block side.
6006 */
6007 if (ret != -EAGAIN)
6008 break;
6009 cond_resched();
6010 } while (1);
6011 }
Jens Axboe31b51512019-01-18 22:56:34 -07006012
Jens Axboe561fb042019-10-24 07:25:42 -06006013 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006014 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006015 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006016 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006017
Pavel Begunkovf4db7182020-06-25 18:20:54 +03006018 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07006019}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006020
Jens Axboe65e19f52019-10-26 07:20:21 -06006021static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6022 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006023{
Jens Axboe65e19f52019-10-26 07:20:21 -06006024 struct fixed_file_table *table;
6025
Jens Axboe05f3fb32019-12-09 11:22:50 -07006026 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006027 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006028}
6029
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006030static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6031 int fd, struct file **out_file, bool fixed)
6032{
6033 struct io_ring_ctx *ctx = req->ctx;
6034 struct file *file;
6035
6036 if (fixed) {
6037 if (unlikely(!ctx->file_data ||
6038 (unsigned) fd >= ctx->nr_user_files))
6039 return -EBADF;
6040 fd = array_index_nospec(fd, ctx->nr_user_files);
6041 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006042 if (file) {
6043 req->fixed_file_refs = ctx->file_data->cur_refs;
6044 percpu_ref_get(req->fixed_file_refs);
6045 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006046 } else {
6047 trace_io_uring_file_get(ctx, fd);
6048 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006049 }
6050
Jens Axboefd2206e2020-06-02 16:40:47 -06006051 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6052 *out_file = file;
6053 return 0;
6054 }
6055 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006056}
6057
Jens Axboe3529d8c2019-12-19 18:24:38 -07006058static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006059 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006060{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006061 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006062
Jens Axboe63ff8222020-05-07 14:56:15 -06006063 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006064 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006065 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006066
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006067 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006068}
6069
Jackie Liua197f662019-11-08 08:09:12 -07006070static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006071{
Jens Axboefcb323c2019-10-24 12:39:47 -06006072 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07006073 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006074
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006075 io_req_init_async(req);
6076
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006077 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006078 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006079 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07006080 return -EBADF;
6081
Jens Axboefcb323c2019-10-24 12:39:47 -06006082 rcu_read_lock();
6083 spin_lock_irq(&ctx->inflight_lock);
6084 /*
6085 * We use the f_ops->flush() handler to ensure that we can flush
6086 * out work accessing these files if the fd is closed. Check if
6087 * the fd has changed since we started down this path, and disallow
6088 * this operation if it has.
6089 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006090 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006091 list_add(&req->inflight_entry, &ctx->inflight_list);
6092 req->flags |= REQ_F_INFLIGHT;
6093 req->work.files = current->files;
6094 ret = 0;
6095 }
6096 spin_unlock_irq(&ctx->inflight_lock);
6097 rcu_read_unlock();
6098
6099 return ret;
6100}
6101
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006102static inline int io_prep_work_files(struct io_kiocb *req)
6103{
6104 if (!io_op_defs[req->opcode].file_table)
6105 return 0;
6106 return io_grab_files(req);
6107}
6108
Jens Axboe2665abf2019-11-05 12:40:47 -07006109static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6110{
Jens Axboead8a48a2019-11-15 08:49:11 -07006111 struct io_timeout_data *data = container_of(timer,
6112 struct io_timeout_data, timer);
6113 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006114 struct io_ring_ctx *ctx = req->ctx;
6115 struct io_kiocb *prev = NULL;
6116 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006117
6118 spin_lock_irqsave(&ctx->completion_lock, flags);
6119
6120 /*
6121 * We don't expect the list to be empty, that will only happen if we
6122 * race with the completion of the linked work.
6123 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006124 if (!list_empty(&req->link_list)) {
6125 prev = list_entry(req->link_list.prev, struct io_kiocb,
6126 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006127 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006128 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006129 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6130 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006131 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006132 }
6133
6134 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6135
6136 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006137 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006138 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006139 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006140 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006141 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006142 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006143 return HRTIMER_NORESTART;
6144}
6145
Jens Axboe7271ef32020-08-10 09:55:22 -06006146static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006147{
Jens Axboe76a46e02019-11-10 23:34:16 -07006148 /*
6149 * If the list is now empty, then our linked request finished before
6150 * we got a chance to setup the timer
6151 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006152 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006153 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006154
Jens Axboead8a48a2019-11-15 08:49:11 -07006155 data->timer.function = io_link_timeout_fn;
6156 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6157 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006158 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006159}
6160
6161static void io_queue_linked_timeout(struct io_kiocb *req)
6162{
6163 struct io_ring_ctx *ctx = req->ctx;
6164
6165 spin_lock_irq(&ctx->completion_lock);
6166 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006167 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006168
Jens Axboe2665abf2019-11-05 12:40:47 -07006169 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006170 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006171}
6172
Jens Axboead8a48a2019-11-15 08:49:11 -07006173static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006174{
6175 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006176
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006177 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006178 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006179 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006180 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006181
Pavel Begunkov44932332019-12-05 16:16:35 +03006182 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6183 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006184 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006185 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006186
Jens Axboe76a46e02019-11-10 23:34:16 -07006187 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006188 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006189}
6190
Jens Axboef13fad72020-06-22 09:34:30 -06006191static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6192 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006193{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006194 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006195 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006196 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006197 int ret;
6198
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006199again:
6200 linked_timeout = io_prep_linked_timeout(req);
6201
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006202 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6203 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006204 if (old_creds)
6205 revert_creds(old_creds);
6206 if (old_creds == req->work.creds)
6207 old_creds = NULL; /* restored original creds */
6208 else
6209 old_creds = override_creds(req->work.creds);
6210 }
6211
Jens Axboef13fad72020-06-22 09:34:30 -06006212 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006213
6214 /*
6215 * We async punt it if the file wasn't marked NOWAIT, or if the file
6216 * doesn't support non-blocking read/write attempts
6217 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006218 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006219 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006220punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006221 ret = io_prep_work_files(req);
6222 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006223 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006224 /*
6225 * Queued up for async execution, worker will release
6226 * submit reference when the iocb is actually submitted.
6227 */
6228 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006229 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006230
Pavel Begunkovf063c542020-07-25 14:41:59 +03006231 if (linked_timeout)
6232 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006233 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006234 }
Jens Axboee65ef562019-03-12 10:16:44 -06006235
Pavel Begunkov652532a2020-07-03 22:15:07 +03006236 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006237err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006238 /* un-prep timeout, so it'll be killed as any other linked */
6239 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006240 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006241 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006242 io_req_complete(req, ret);
6243 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006244 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006245
Jens Axboe6c271ce2019-01-10 11:22:30 -07006246 /* drop submission reference */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03006247 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006248 if (linked_timeout)
6249 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006250
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006251 if (nxt) {
6252 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006253
6254 if (req->flags & REQ_F_FORCE_ASYNC)
6255 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006256 goto again;
6257 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006258exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006259 if (old_creds)
6260 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006261}
6262
Jens Axboef13fad72020-06-22 09:34:30 -06006263static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6264 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006265{
6266 int ret;
6267
Jens Axboe3529d8c2019-12-19 18:24:38 -07006268 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006269 if (ret) {
6270 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006271fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006272 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006273 io_put_req(req);
6274 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006275 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006276 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006277 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006278 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006279 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006280 goto fail_req;
6281 }
6282
Jens Axboece35a472019-12-17 08:04:44 -07006283 /*
6284 * Never try inline submit of IOSQE_ASYNC is set, go straight
6285 * to async execution.
6286 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006287 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006288 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6289 io_queue_async_work(req);
6290 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006291 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006292 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006293}
6294
Jens Axboef13fad72020-06-22 09:34:30 -06006295static inline void io_queue_link_head(struct io_kiocb *req,
6296 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006297{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006298 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006299 io_put_req(req);
6300 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006301 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006302 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006303}
6304
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006305static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006306 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006307{
Jackie Liua197f662019-11-08 08:09:12 -07006308 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006309 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006310
Jens Axboe9e645e112019-05-10 16:07:28 -06006311 /*
6312 * If we already have a head request, queue this one for async
6313 * submittal once the head completes. If we don't have a head but
6314 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6315 * submitted sync once the chain is complete. If none of those
6316 * conditions are true (normal request), then just queue it.
6317 */
6318 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006319 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006320
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006321 /*
6322 * Taking sequential execution of a link, draining both sides
6323 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6324 * requests in the link. So, it drains the head and the
6325 * next after the link request. The last one is done via
6326 * drain_next flag to persist the effect across calls.
6327 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006328 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006329 head->flags |= REQ_F_IO_DRAIN;
6330 ctx->drain_next = 1;
6331 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006332 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006333 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006334 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006335 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006336 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006337 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006338 trace_io_uring_link(ctx, req, head);
Jens Axboec40f6372020-06-25 15:39:59 -06006339 io_get_req_task(req);
Pavel Begunkov9d763772019-12-17 02:22:07 +03006340 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006341
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006342 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006343 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006344 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006345 *link = NULL;
6346 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006347 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006348 if (unlikely(ctx->drain_next)) {
6349 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006350 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006351 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006352 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006353 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006354 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006355
Pavel Begunkov711be032020-01-17 03:57:59 +03006356 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006357 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006358 req->flags |= REQ_F_FAIL_LINK;
6359 *link = req;
6360 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006361 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006362 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006363 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006364
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006365 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006366}
6367
Jens Axboe9a56a232019-01-09 09:06:50 -07006368/*
6369 * Batched submission is done, ensure local IO is flushed out.
6370 */
6371static void io_submit_state_end(struct io_submit_state *state)
6372{
Jens Axboef13fad72020-06-22 09:34:30 -06006373 if (!list_empty(&state->comp.list))
6374 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006375 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006376 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006377 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006378 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006379}
6380
6381/*
6382 * Start submission side cache.
6383 */
6384static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006385 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006386{
6387 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006388 state->comp.nr = 0;
6389 INIT_LIST_HEAD(&state->comp.list);
6390 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006391 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006392 state->file = NULL;
6393 state->ios_left = max_ios;
6394}
6395
Jens Axboe2b188cc2019-01-07 10:46:33 -07006396static void io_commit_sqring(struct io_ring_ctx *ctx)
6397{
Hristo Venev75b28af2019-08-26 17:23:46 +00006398 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006399
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006400 /*
6401 * Ensure any loads from the SQEs are done at this point,
6402 * since once we write the new head, the application could
6403 * write new data to them.
6404 */
6405 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006406}
6407
6408/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006409 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006410 * that is mapped by userspace. This means that care needs to be taken to
6411 * ensure that reads are stable, as we cannot rely on userspace always
6412 * being a good citizen. If members of the sqe are validated and then later
6413 * used, it's important that those reads are done through READ_ONCE() to
6414 * prevent a re-load down the line.
6415 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006416static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006417{
Hristo Venev75b28af2019-08-26 17:23:46 +00006418 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006419 unsigned head;
6420
6421 /*
6422 * The cached sq head (or cq tail) serves two purposes:
6423 *
6424 * 1) allows us to batch the cost of updating the user visible
6425 * head updates.
6426 * 2) allows the kernel side to track the head on its own, even
6427 * though the application is the one updating it.
6428 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006429 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006430 if (likely(head < ctx->sq_entries))
6431 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006432
6433 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006434 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006435 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006436 return NULL;
6437}
6438
6439static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6440{
6441 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006442}
6443
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006444#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6445 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6446 IOSQE_BUFFER_SELECT)
6447
6448static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6449 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006450 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006451{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006452 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006453 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006454
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006455 req->opcode = READ_ONCE(sqe->opcode);
6456 req->user_data = READ_ONCE(sqe->user_data);
6457 req->io = NULL;
6458 req->file = NULL;
6459 req->ctx = ctx;
6460 req->flags = 0;
6461 /* one is dropped after submission, the other at completion */
6462 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006463 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006464 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006465
6466 if (unlikely(req->opcode >= IORING_OP_LAST))
6467 return -EINVAL;
6468
Jens Axboe9d8426a2020-06-16 18:42:49 -06006469 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6470 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006471
6472 sqe_flags = READ_ONCE(sqe->flags);
6473 /* enforce forwards compatibility on users */
6474 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6475 return -EINVAL;
6476
6477 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6478 !io_op_defs[req->opcode].buffer_select)
6479 return -EOPNOTSUPP;
6480
6481 id = READ_ONCE(sqe->personality);
6482 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006483 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006484 req->work.creds = idr_find(&ctx->personality_idr, id);
6485 if (unlikely(!req->work.creds))
6486 return -EINVAL;
6487 get_cred(req->work.creds);
6488 }
6489
6490 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006491 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006492
Jens Axboe63ff8222020-05-07 14:56:15 -06006493 if (!io_op_defs[req->opcode].needs_file)
6494 return 0;
6495
6496 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006497}
6498
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006499static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006500 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006501{
Jens Axboeac8691c2020-06-01 08:30:41 -06006502 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006503 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006504 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006505
Jens Axboec4a2ed72019-11-21 21:01:26 -07006506 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006507 if (test_bit(0, &ctx->sq_check_overflow)) {
6508 if (!list_empty(&ctx->cq_overflow_list) &&
6509 !io_cqring_overflow_flush(ctx, false))
6510 return -EBUSY;
6511 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006512
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006513 /* make sure SQ entry isn't read before tail */
6514 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006515
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006516 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6517 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006518
Jens Axboe013538b2020-06-22 09:29:15 -06006519 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006520
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006521 ctx->ring_fd = ring_fd;
6522 ctx->ring_file = ring_file;
6523
Jens Axboe6c271ce2019-01-10 11:22:30 -07006524 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006525 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006526 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006527 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006528
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006529 sqe = io_get_sqe(ctx);
6530 if (unlikely(!sqe)) {
6531 io_consume_sqe(ctx);
6532 break;
6533 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006534 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006535 if (unlikely(!req)) {
6536 if (!submitted)
6537 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006538 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006539 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006540
Jens Axboeac8691c2020-06-01 08:30:41 -06006541 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006542 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006543 /* will complete beyond this point, count as submitted */
6544 submitted++;
6545
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006546 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006547fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006548 io_put_req(req);
6549 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006550 break;
6551 }
6552
Jens Axboe354420f2020-01-08 18:55:15 -07006553 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006554 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006555 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006556 if (err)
6557 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006558 }
6559
Pavel Begunkov9466f432020-01-25 22:34:01 +03006560 if (unlikely(submitted != nr)) {
6561 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6562
6563 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6564 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006565 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006566 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006567 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006568
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006569 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6570 io_commit_sqring(ctx);
6571
Jens Axboe6c271ce2019-01-10 11:22:30 -07006572 return submitted;
6573}
6574
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006575static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6576{
6577 /* Tell userspace we may need a wakeup call */
6578 spin_lock_irq(&ctx->completion_lock);
6579 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6580 spin_unlock_irq(&ctx->completion_lock);
6581}
6582
6583static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6584{
6585 spin_lock_irq(&ctx->completion_lock);
6586 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6587 spin_unlock_irq(&ctx->completion_lock);
6588}
6589
Jens Axboe6c271ce2019-01-10 11:22:30 -07006590static int io_sq_thread(void *data)
6591{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006592 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006593 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006594 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006595 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006596 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006597
Jens Axboe0f158b42020-05-14 17:18:39 -06006598 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006599
Jens Axboe181e4482019-11-25 08:52:30 -07006600 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006601
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006602 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006603 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006604 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006605
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006606 if (!list_empty(&ctx->iopoll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006607 unsigned nr_events = 0;
6608
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006609 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006610 if (!list_empty(&ctx->iopoll_list) && !need_resched())
Pavel Begunkov9dedd562020-07-07 16:36:20 +03006611 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006612 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006613 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006614 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006615 }
6616
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006617 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006618
6619 /*
6620 * If submit got -EBUSY, flag us as needing the application
6621 * to enter the kernel to reap and flush events.
6622 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006623 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006624 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006625 * Drop cur_mm before scheduling, we can't hold it for
6626 * long periods (or over schedule()). Do this before
6627 * adding ourselves to the waitqueue, as the unuse/drop
6628 * may sleep.
6629 */
Jens Axboe4349f302020-07-09 15:07:01 -06006630 io_sq_thread_drop_mm();
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006631
6632 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006633 * We're polling. If we're within the defined idle
6634 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006635 * to sleep. The exception is if we got EBUSY doing
6636 * more IO, we should wait for the application to
6637 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006638 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006639 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006640 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6641 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06006642 io_run_task_work();
Jens Axboe9831a902019-09-19 09:48:55 -06006643 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006644 continue;
6645 }
6646
Jens Axboe6c271ce2019-01-10 11:22:30 -07006647 prepare_to_wait(&ctx->sqo_wait, &wait,
6648 TASK_INTERRUPTIBLE);
6649
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006650 /*
6651 * While doing polled IO, before going to sleep, we need
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006652 * to check if there are new reqs added to iopoll_list,
6653 * it is because reqs may have been punted to io worker
6654 * and will be added to iopoll_list later, hence check
6655 * the iopoll_list again.
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006656 */
6657 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006658 !list_empty_careful(&ctx->iopoll_list)) {
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006659 finish_wait(&ctx->sqo_wait, &wait);
6660 continue;
6661 }
6662
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006663 io_ring_set_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006664
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006665 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006666 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006667 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006668 finish_wait(&ctx->sqo_wait, &wait);
6669 break;
6670 }
Jens Axboe4c6e2772020-07-01 11:29:10 -06006671 if (io_run_task_work()) {
Hillf Danton10bea962020-04-01 17:19:33 +08006672 finish_wait(&ctx->sqo_wait, &wait);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006673 io_ring_clear_wakeup_flag(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07006674 continue;
6675 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006676 if (signal_pending(current))
6677 flush_signals(current);
6678 schedule();
6679 finish_wait(&ctx->sqo_wait, &wait);
6680
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006681 io_ring_clear_wakeup_flag(ctx);
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006682 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006683 continue;
6684 }
6685 finish_wait(&ctx->sqo_wait, &wait);
6686
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006687 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006688 }
6689
Jens Axboe8a4955f2019-12-09 14:52:35 -07006690 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006691 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6692 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006693 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006694 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006695 }
6696
Jens Axboe4c6e2772020-07-01 11:29:10 -06006697 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006698
Jens Axboe4349f302020-07-09 15:07:01 -06006699 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006700 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006701
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006702 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006703
Jens Axboe6c271ce2019-01-10 11:22:30 -07006704 return 0;
6705}
6706
Jens Axboebda52162019-09-24 13:47:15 -06006707struct io_wait_queue {
6708 struct wait_queue_entry wq;
6709 struct io_ring_ctx *ctx;
6710 unsigned to_wait;
6711 unsigned nr_timeouts;
6712};
6713
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006714static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006715{
6716 struct io_ring_ctx *ctx = iowq->ctx;
6717
6718 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006719 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006720 * started waiting. For timeouts, we always want to return to userspace,
6721 * regardless of event count.
6722 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006723 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006724 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6725}
6726
6727static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6728 int wake_flags, void *key)
6729{
6730 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6731 wq);
6732
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006733 /* use noflush == true, as we can't safely rely on locking context */
6734 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006735 return -1;
6736
6737 return autoremove_wake_function(curr, mode, wake_flags, key);
6738}
6739
Jens Axboe2b188cc2019-01-07 10:46:33 -07006740/*
6741 * Wait until events become available, if we don't already have some. The
6742 * application must reap them itself, as they reside on the shared cq ring.
6743 */
6744static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6745 const sigset_t __user *sig, size_t sigsz)
6746{
Jens Axboebda52162019-09-24 13:47:15 -06006747 struct io_wait_queue iowq = {
6748 .wq = {
6749 .private = current,
6750 .func = io_wake_function,
6751 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6752 },
6753 .ctx = ctx,
6754 .to_wait = min_events,
6755 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006756 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006757 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006758
Jens Axboeb41e9852020-02-17 09:52:41 -07006759 do {
6760 if (io_cqring_events(ctx, false) >= min_events)
6761 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006762 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006763 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006764 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006765
6766 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006767#ifdef CONFIG_COMPAT
6768 if (in_compat_syscall())
6769 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006770 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006771 else
6772#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006773 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006774
Jens Axboe2b188cc2019-01-07 10:46:33 -07006775 if (ret)
6776 return ret;
6777 }
6778
Jens Axboebda52162019-09-24 13:47:15 -06006779 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006780 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006781 do {
6782 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6783 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006784 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006785 if (io_run_task_work())
6786 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006787 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006788 if (current->jobctl & JOBCTL_TASK_WORK) {
6789 spin_lock_irq(&current->sighand->siglock);
6790 current->jobctl &= ~JOBCTL_TASK_WORK;
6791 recalc_sigpending();
6792 spin_unlock_irq(&current->sighand->siglock);
6793 continue;
6794 }
6795 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006796 break;
6797 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006798 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006799 break;
6800 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006801 } while (1);
6802 finish_wait(&ctx->wait, &iowq.wq);
6803
Jens Axboeb7db41c2020-07-04 08:55:50 -06006804 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006805
Hristo Venev75b28af2019-08-26 17:23:46 +00006806 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006807}
6808
Jens Axboe6b063142019-01-10 22:13:58 -07006809static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6810{
6811#if defined(CONFIG_UNIX)
6812 if (ctx->ring_sock) {
6813 struct sock *sock = ctx->ring_sock->sk;
6814 struct sk_buff *skb;
6815
6816 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6817 kfree_skb(skb);
6818 }
6819#else
6820 int i;
6821
Jens Axboe65e19f52019-10-26 07:20:21 -06006822 for (i = 0; i < ctx->nr_user_files; i++) {
6823 struct file *file;
6824
6825 file = io_file_from_index(ctx, i);
6826 if (file)
6827 fput(file);
6828 }
Jens Axboe6b063142019-01-10 22:13:58 -07006829#endif
6830}
6831
Jens Axboe05f3fb32019-12-09 11:22:50 -07006832static void io_file_ref_kill(struct percpu_ref *ref)
6833{
6834 struct fixed_file_data *data;
6835
6836 data = container_of(ref, struct fixed_file_data, refs);
6837 complete(&data->done);
6838}
6839
Jens Axboe6b063142019-01-10 22:13:58 -07006840static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6841{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006842 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006843 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006844 unsigned nr_tables, i;
6845
Jens Axboe05f3fb32019-12-09 11:22:50 -07006846 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006847 return -ENXIO;
6848
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006849 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006850 if (!list_empty(&data->ref_list))
6851 ref_node = list_first_entry(&data->ref_list,
6852 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006853 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006854 if (ref_node)
6855 percpu_ref_kill(&ref_node->refs);
6856
6857 percpu_ref_kill(&data->refs);
6858
6859 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006860 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006861 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006862
Jens Axboe6b063142019-01-10 22:13:58 -07006863 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006864 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6865 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006866 kfree(data->table[i].files);
6867 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006868 percpu_ref_exit(&data->refs);
6869 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006870 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006871 ctx->nr_user_files = 0;
6872 return 0;
6873}
6874
Jens Axboe6c271ce2019-01-10 11:22:30 -07006875static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6876{
6877 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006878 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006879 /*
6880 * The park is a bit of a work-around, without it we get
6881 * warning spews on shutdown with SQPOLL set and affinity
6882 * set to a single CPU.
6883 */
Jens Axboe06058632019-04-13 09:26:03 -06006884 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006885 kthread_stop(ctx->sqo_thread);
6886 ctx->sqo_thread = NULL;
6887 }
6888}
6889
Jens Axboe6b063142019-01-10 22:13:58 -07006890static void io_finish_async(struct io_ring_ctx *ctx)
6891{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006892 io_sq_thread_stop(ctx);
6893
Jens Axboe561fb042019-10-24 07:25:42 -06006894 if (ctx->io_wq) {
6895 io_wq_destroy(ctx->io_wq);
6896 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006897 }
6898}
6899
6900#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006901/*
6902 * Ensure the UNIX gc is aware of our file set, so we are certain that
6903 * the io_uring can be safely unregistered on process exit, even if we have
6904 * loops in the file referencing.
6905 */
6906static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6907{
6908 struct sock *sk = ctx->ring_sock->sk;
6909 struct scm_fp_list *fpl;
6910 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006911 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006912
Jens Axboe6b063142019-01-10 22:13:58 -07006913 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6914 if (!fpl)
6915 return -ENOMEM;
6916
6917 skb = alloc_skb(0, GFP_KERNEL);
6918 if (!skb) {
6919 kfree(fpl);
6920 return -ENOMEM;
6921 }
6922
6923 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006924
Jens Axboe08a45172019-10-03 08:11:03 -06006925 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006926 fpl->user = get_uid(ctx->user);
6927 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006928 struct file *file = io_file_from_index(ctx, i + offset);
6929
6930 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006931 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006932 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006933 unix_inflight(fpl->user, fpl->fp[nr_files]);
6934 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006935 }
6936
Jens Axboe08a45172019-10-03 08:11:03 -06006937 if (nr_files) {
6938 fpl->max = SCM_MAX_FD;
6939 fpl->count = nr_files;
6940 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006941 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006942 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6943 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006944
Jens Axboe08a45172019-10-03 08:11:03 -06006945 for (i = 0; i < nr_files; i++)
6946 fput(fpl->fp[i]);
6947 } else {
6948 kfree_skb(skb);
6949 kfree(fpl);
6950 }
Jens Axboe6b063142019-01-10 22:13:58 -07006951
6952 return 0;
6953}
6954
6955/*
6956 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6957 * causes regular reference counting to break down. We rely on the UNIX
6958 * garbage collection to take care of this problem for us.
6959 */
6960static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6961{
6962 unsigned left, total;
6963 int ret = 0;
6964
6965 total = 0;
6966 left = ctx->nr_user_files;
6967 while (left) {
6968 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006969
6970 ret = __io_sqe_files_scm(ctx, this_files, total);
6971 if (ret)
6972 break;
6973 left -= this_files;
6974 total += this_files;
6975 }
6976
6977 if (!ret)
6978 return 0;
6979
6980 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006981 struct file *file = io_file_from_index(ctx, total);
6982
6983 if (file)
6984 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006985 total++;
6986 }
6987
6988 return ret;
6989}
6990#else
6991static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6992{
6993 return 0;
6994}
6995#endif
6996
Jens Axboe65e19f52019-10-26 07:20:21 -06006997static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6998 unsigned nr_files)
6999{
7000 int i;
7001
7002 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007003 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007004 unsigned this_files;
7005
7006 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7007 table->files = kcalloc(this_files, sizeof(struct file *),
7008 GFP_KERNEL);
7009 if (!table->files)
7010 break;
7011 nr_files -= this_files;
7012 }
7013
7014 if (i == nr_tables)
7015 return 0;
7016
7017 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007018 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007019 kfree(table->files);
7020 }
7021 return 1;
7022}
7023
Jens Axboe05f3fb32019-12-09 11:22:50 -07007024static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06007025{
7026#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007027 struct sock *sock = ctx->ring_sock->sk;
7028 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7029 struct sk_buff *skb;
7030 int i;
7031
7032 __skb_queue_head_init(&list);
7033
7034 /*
7035 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7036 * remove this entry and rearrange the file array.
7037 */
7038 skb = skb_dequeue(head);
7039 while (skb) {
7040 struct scm_fp_list *fp;
7041
7042 fp = UNIXCB(skb).fp;
7043 for (i = 0; i < fp->count; i++) {
7044 int left;
7045
7046 if (fp->fp[i] != file)
7047 continue;
7048
7049 unix_notinflight(fp->user, fp->fp[i]);
7050 left = fp->count - 1 - i;
7051 if (left) {
7052 memmove(&fp->fp[i], &fp->fp[i + 1],
7053 left * sizeof(struct file *));
7054 }
7055 fp->count--;
7056 if (!fp->count) {
7057 kfree_skb(skb);
7058 skb = NULL;
7059 } else {
7060 __skb_queue_tail(&list, skb);
7061 }
7062 fput(file);
7063 file = NULL;
7064 break;
7065 }
7066
7067 if (!file)
7068 break;
7069
7070 __skb_queue_tail(&list, skb);
7071
7072 skb = skb_dequeue(head);
7073 }
7074
7075 if (skb_peek(&list)) {
7076 spin_lock_irq(&head->lock);
7077 while ((skb = __skb_dequeue(&list)) != NULL)
7078 __skb_queue_tail(head, skb);
7079 spin_unlock_irq(&head->lock);
7080 }
7081#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007082 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007083#endif
7084}
7085
Jens Axboe05f3fb32019-12-09 11:22:50 -07007086struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007087 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007088 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007089};
7090
Jens Axboe4a38aed22020-05-14 17:21:15 -06007091static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007092{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007093 struct fixed_file_data *file_data = ref_node->file_data;
7094 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007095 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007096
7097 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007098 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007099 io_ring_file_put(ctx, pfile->file);
7100 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007101 }
7102
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007103 spin_lock(&file_data->lock);
7104 list_del(&ref_node->node);
7105 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007106
Xiaoguang Wang05589552020-03-31 14:05:18 +08007107 percpu_ref_exit(&ref_node->refs);
7108 kfree(ref_node);
7109 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007110}
7111
Jens Axboe4a38aed22020-05-14 17:21:15 -06007112static void io_file_put_work(struct work_struct *work)
7113{
7114 struct io_ring_ctx *ctx;
7115 struct llist_node *node;
7116
7117 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7118 node = llist_del_all(&ctx->file_put_llist);
7119
7120 while (node) {
7121 struct fixed_file_ref_node *ref_node;
7122 struct llist_node *next = node->next;
7123
7124 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7125 __io_file_put_work(ref_node);
7126 node = next;
7127 }
7128}
7129
Jens Axboe05f3fb32019-12-09 11:22:50 -07007130static void io_file_data_ref_zero(struct percpu_ref *ref)
7131{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007132 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007133 struct io_ring_ctx *ctx;
7134 bool first_add;
7135 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007136
Xiaoguang Wang05589552020-03-31 14:05:18 +08007137 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007138 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007139
Jens Axboe4a38aed22020-05-14 17:21:15 -06007140 if (percpu_ref_is_dying(&ctx->file_data->refs))
7141 delay = 0;
7142
7143 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7144 if (!delay)
7145 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7146 else if (first_add)
7147 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007148}
7149
7150static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7151 struct io_ring_ctx *ctx)
7152{
7153 struct fixed_file_ref_node *ref_node;
7154
7155 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7156 if (!ref_node)
7157 return ERR_PTR(-ENOMEM);
7158
7159 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7160 0, GFP_KERNEL)) {
7161 kfree(ref_node);
7162 return ERR_PTR(-ENOMEM);
7163 }
7164 INIT_LIST_HEAD(&ref_node->node);
7165 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007166 ref_node->file_data = ctx->file_data;
7167 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007168}
7169
7170static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7171{
7172 percpu_ref_exit(&ref_node->refs);
7173 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007174}
7175
7176static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7177 unsigned nr_args)
7178{
7179 __s32 __user *fds = (__s32 __user *) arg;
7180 unsigned nr_tables;
7181 struct file *file;
7182 int fd, ret = 0;
7183 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007184 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007185
7186 if (ctx->file_data)
7187 return -EBUSY;
7188 if (!nr_args)
7189 return -EINVAL;
7190 if (nr_args > IORING_MAX_FIXED_FILES)
7191 return -EMFILE;
7192
7193 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7194 if (!ctx->file_data)
7195 return -ENOMEM;
7196 ctx->file_data->ctx = ctx;
7197 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007198 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007199 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007200
7201 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7202 ctx->file_data->table = kcalloc(nr_tables,
7203 sizeof(struct fixed_file_table),
7204 GFP_KERNEL);
7205 if (!ctx->file_data->table) {
7206 kfree(ctx->file_data);
7207 ctx->file_data = NULL;
7208 return -ENOMEM;
7209 }
7210
Xiaoguang Wang05589552020-03-31 14:05:18 +08007211 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007212 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7213 kfree(ctx->file_data->table);
7214 kfree(ctx->file_data);
7215 ctx->file_data = NULL;
7216 return -ENOMEM;
7217 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007218
7219 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7220 percpu_ref_exit(&ctx->file_data->refs);
7221 kfree(ctx->file_data->table);
7222 kfree(ctx->file_data);
7223 ctx->file_data = NULL;
7224 return -ENOMEM;
7225 }
7226
7227 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7228 struct fixed_file_table *table;
7229 unsigned index;
7230
7231 ret = -EFAULT;
7232 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7233 break;
7234 /* allow sparse sets */
7235 if (fd == -1) {
7236 ret = 0;
7237 continue;
7238 }
7239
7240 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7241 index = i & IORING_FILE_TABLE_MASK;
7242 file = fget(fd);
7243
7244 ret = -EBADF;
7245 if (!file)
7246 break;
7247
7248 /*
7249 * Don't allow io_uring instances to be registered. If UNIX
7250 * isn't enabled, then this causes a reference cycle and this
7251 * instance can never get freed. If UNIX is enabled we'll
7252 * handle it just fine, but there's still no point in allowing
7253 * a ring fd as it doesn't support regular read/write anyway.
7254 */
7255 if (file->f_op == &io_uring_fops) {
7256 fput(file);
7257 break;
7258 }
7259 ret = 0;
7260 table->files[index] = file;
7261 }
7262
7263 if (ret) {
7264 for (i = 0; i < ctx->nr_user_files; i++) {
7265 file = io_file_from_index(ctx, i);
7266 if (file)
7267 fput(file);
7268 }
7269 for (i = 0; i < nr_tables; i++)
7270 kfree(ctx->file_data->table[i].files);
7271
Yang Yingliang667e57d2020-07-10 14:14:20 +00007272 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007273 kfree(ctx->file_data->table);
7274 kfree(ctx->file_data);
7275 ctx->file_data = NULL;
7276 ctx->nr_user_files = 0;
7277 return ret;
7278 }
7279
7280 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007281 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007282 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007283 return ret;
7284 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007285
Xiaoguang Wang05589552020-03-31 14:05:18 +08007286 ref_node = alloc_fixed_file_ref_node(ctx);
7287 if (IS_ERR(ref_node)) {
7288 io_sqe_files_unregister(ctx);
7289 return PTR_ERR(ref_node);
7290 }
7291
7292 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007293 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007294 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007295 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007296 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007297 return ret;
7298}
7299
Jens Axboec3a31e62019-10-03 13:59:56 -06007300static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7301 int index)
7302{
7303#if defined(CONFIG_UNIX)
7304 struct sock *sock = ctx->ring_sock->sk;
7305 struct sk_buff_head *head = &sock->sk_receive_queue;
7306 struct sk_buff *skb;
7307
7308 /*
7309 * See if we can merge this file into an existing skb SCM_RIGHTS
7310 * file set. If there's no room, fall back to allocating a new skb
7311 * and filling it in.
7312 */
7313 spin_lock_irq(&head->lock);
7314 skb = skb_peek(head);
7315 if (skb) {
7316 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7317
7318 if (fpl->count < SCM_MAX_FD) {
7319 __skb_unlink(skb, head);
7320 spin_unlock_irq(&head->lock);
7321 fpl->fp[fpl->count] = get_file(file);
7322 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7323 fpl->count++;
7324 spin_lock_irq(&head->lock);
7325 __skb_queue_head(head, skb);
7326 } else {
7327 skb = NULL;
7328 }
7329 }
7330 spin_unlock_irq(&head->lock);
7331
7332 if (skb) {
7333 fput(file);
7334 return 0;
7335 }
7336
7337 return __io_sqe_files_scm(ctx, 1, index);
7338#else
7339 return 0;
7340#endif
7341}
7342
Hillf Dantona5318d32020-03-23 17:47:15 +08007343static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007344 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007345{
Hillf Dantona5318d32020-03-23 17:47:15 +08007346 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007347 struct percpu_ref *refs = data->cur_refs;
7348 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007349
Jens Axboe05f3fb32019-12-09 11:22:50 -07007350 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007351 if (!pfile)
7352 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007353
Xiaoguang Wang05589552020-03-31 14:05:18 +08007354 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007355 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007356 list_add(&pfile->list, &ref_node->file_list);
7357
Hillf Dantona5318d32020-03-23 17:47:15 +08007358 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007359}
7360
7361static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7362 struct io_uring_files_update *up,
7363 unsigned nr_args)
7364{
7365 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007366 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007367 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007368 __s32 __user *fds;
7369 int fd, i, err;
7370 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007371 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007372
Jens Axboe05f3fb32019-12-09 11:22:50 -07007373 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007374 return -EOVERFLOW;
7375 if (done > ctx->nr_user_files)
7376 return -EINVAL;
7377
Xiaoguang Wang05589552020-03-31 14:05:18 +08007378 ref_node = alloc_fixed_file_ref_node(ctx);
7379 if (IS_ERR(ref_node))
7380 return PTR_ERR(ref_node);
7381
Jens Axboec3a31e62019-10-03 13:59:56 -06007382 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007383 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007384 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007385 struct fixed_file_table *table;
7386 unsigned index;
7387
Jens Axboec3a31e62019-10-03 13:59:56 -06007388 err = 0;
7389 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7390 err = -EFAULT;
7391 break;
7392 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007393 i = array_index_nospec(up->offset, ctx->nr_user_files);
7394 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007395 index = i & IORING_FILE_TABLE_MASK;
7396 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007397 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007398 err = io_queue_file_removal(data, file);
7399 if (err)
7400 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007401 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007402 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007403 }
7404 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007405 file = fget(fd);
7406 if (!file) {
7407 err = -EBADF;
7408 break;
7409 }
7410 /*
7411 * Don't allow io_uring instances to be registered. If
7412 * UNIX isn't enabled, then this causes a reference
7413 * cycle and this instance can never get freed. If UNIX
7414 * is enabled we'll handle it just fine, but there's
7415 * still no point in allowing a ring fd as it doesn't
7416 * support regular read/write anyway.
7417 */
7418 if (file->f_op == &io_uring_fops) {
7419 fput(file);
7420 err = -EBADF;
7421 break;
7422 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007423 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007424 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007425 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007426 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007427 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007428 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007429 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007430 }
7431 nr_args--;
7432 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007433 up->offset++;
7434 }
7435
Xiaoguang Wang05589552020-03-31 14:05:18 +08007436 if (needs_switch) {
7437 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007438 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007439 list_add(&ref_node->node, &data->ref_list);
7440 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007441 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007442 percpu_ref_get(&ctx->file_data->refs);
7443 } else
7444 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007445
7446 return done ? done : err;
7447}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007448
Jens Axboe05f3fb32019-12-09 11:22:50 -07007449static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7450 unsigned nr_args)
7451{
7452 struct io_uring_files_update up;
7453
7454 if (!ctx->file_data)
7455 return -ENXIO;
7456 if (!nr_args)
7457 return -EINVAL;
7458 if (copy_from_user(&up, arg, sizeof(up)))
7459 return -EFAULT;
7460 if (up.resv)
7461 return -EINVAL;
7462
7463 return __io_sqe_files_update(ctx, &up, nr_args);
7464}
Jens Axboec3a31e62019-10-03 13:59:56 -06007465
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007466static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007467{
7468 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7469
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007470 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007471 io_put_req(req);
7472}
7473
Pavel Begunkov24369c22020-01-28 03:15:48 +03007474static int io_init_wq_offload(struct io_ring_ctx *ctx,
7475 struct io_uring_params *p)
7476{
7477 struct io_wq_data data;
7478 struct fd f;
7479 struct io_ring_ctx *ctx_attach;
7480 unsigned int concurrency;
7481 int ret = 0;
7482
7483 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007484 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007485 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007486
7487 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7488 /* Do QD, or 4 * CPUS, whatever is smallest */
7489 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7490
7491 ctx->io_wq = io_wq_create(concurrency, &data);
7492 if (IS_ERR(ctx->io_wq)) {
7493 ret = PTR_ERR(ctx->io_wq);
7494 ctx->io_wq = NULL;
7495 }
7496 return ret;
7497 }
7498
7499 f = fdget(p->wq_fd);
7500 if (!f.file)
7501 return -EBADF;
7502
7503 if (f.file->f_op != &io_uring_fops) {
7504 ret = -EINVAL;
7505 goto out_fput;
7506 }
7507
7508 ctx_attach = f.file->private_data;
7509 /* @io_wq is protected by holding the fd */
7510 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7511 ret = -EINVAL;
7512 goto out_fput;
7513 }
7514
7515 ctx->io_wq = ctx_attach->io_wq;
7516out_fput:
7517 fdput(f);
7518 return ret;
7519}
7520
Jens Axboe6c271ce2019-01-10 11:22:30 -07007521static int io_sq_offload_start(struct io_ring_ctx *ctx,
7522 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007523{
7524 int ret;
7525
Jens Axboe6c271ce2019-01-10 11:22:30 -07007526 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007527 ret = -EPERM;
7528 if (!capable(CAP_SYS_ADMIN))
7529 goto err;
7530
Jens Axboe917257d2019-04-13 09:28:55 -06007531 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7532 if (!ctx->sq_thread_idle)
7533 ctx->sq_thread_idle = HZ;
7534
Jens Axboe6c271ce2019-01-10 11:22:30 -07007535 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007536 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007537
Jens Axboe917257d2019-04-13 09:28:55 -06007538 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007539 if (cpu >= nr_cpu_ids)
7540 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007541 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007542 goto err;
7543
Jens Axboe6c271ce2019-01-10 11:22:30 -07007544 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7545 ctx, cpu,
7546 "io_uring-sq");
7547 } else {
7548 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7549 "io_uring-sq");
7550 }
7551 if (IS_ERR(ctx->sqo_thread)) {
7552 ret = PTR_ERR(ctx->sqo_thread);
7553 ctx->sqo_thread = NULL;
7554 goto err;
7555 }
7556 wake_up_process(ctx->sqo_thread);
7557 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7558 /* Can't have SQ_AFF without SQPOLL */
7559 ret = -EINVAL;
7560 goto err;
7561 }
7562
Pavel Begunkov24369c22020-01-28 03:15:48 +03007563 ret = io_init_wq_offload(ctx, p);
7564 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007565 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007566
7567 return 0;
7568err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007569 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007570 return ret;
7571}
7572
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007573static inline void __io_unaccount_mem(struct user_struct *user,
7574 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007575{
7576 atomic_long_sub(nr_pages, &user->locked_vm);
7577}
7578
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007579static inline int __io_account_mem(struct user_struct *user,
7580 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007581{
7582 unsigned long page_limit, cur_pages, new_pages;
7583
7584 /* Don't allow more pages than we can safely lock */
7585 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7586
7587 do {
7588 cur_pages = atomic_long_read(&user->locked_vm);
7589 new_pages = cur_pages + nr_pages;
7590 if (new_pages > page_limit)
7591 return -ENOMEM;
7592 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7593 new_pages) != cur_pages);
7594
7595 return 0;
7596}
7597
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007598static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7599 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007600{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007601 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007602 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007603
Jens Axboe2aede0e2020-09-14 10:45:53 -06007604 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007605 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007606 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007607 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007608 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007609 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007610}
7611
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007612static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7613 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007614{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007615 int ret;
7616
7617 if (ctx->limit_mem) {
7618 ret = __io_account_mem(ctx->user, nr_pages);
7619 if (ret)
7620 return ret;
7621 }
7622
Jens Axboe2aede0e2020-09-14 10:45:53 -06007623 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007624 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007625 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007626 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007627 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007628 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007629
7630 return 0;
7631}
7632
Jens Axboe2b188cc2019-01-07 10:46:33 -07007633static void io_mem_free(void *ptr)
7634{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007635 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007636
Mark Rutland52e04ef2019-04-30 17:30:21 +01007637 if (!ptr)
7638 return;
7639
7640 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007641 if (put_page_testzero(page))
7642 free_compound_page(page);
7643}
7644
7645static void *io_mem_alloc(size_t size)
7646{
7647 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7648 __GFP_NORETRY;
7649
7650 return (void *) __get_free_pages(gfp_flags, get_order(size));
7651}
7652
Hristo Venev75b28af2019-08-26 17:23:46 +00007653static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7654 size_t *sq_offset)
7655{
7656 struct io_rings *rings;
7657 size_t off, sq_array_size;
7658
7659 off = struct_size(rings, cqes, cq_entries);
7660 if (off == SIZE_MAX)
7661 return SIZE_MAX;
7662
7663#ifdef CONFIG_SMP
7664 off = ALIGN(off, SMP_CACHE_BYTES);
7665 if (off == 0)
7666 return SIZE_MAX;
7667#endif
7668
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007669 if (sq_offset)
7670 *sq_offset = off;
7671
Hristo Venev75b28af2019-08-26 17:23:46 +00007672 sq_array_size = array_size(sizeof(u32), sq_entries);
7673 if (sq_array_size == SIZE_MAX)
7674 return SIZE_MAX;
7675
7676 if (check_add_overflow(off, sq_array_size, &off))
7677 return SIZE_MAX;
7678
Hristo Venev75b28af2019-08-26 17:23:46 +00007679 return off;
7680}
7681
Jens Axboe2b188cc2019-01-07 10:46:33 -07007682static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7683{
Hristo Venev75b28af2019-08-26 17:23:46 +00007684 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007685
Hristo Venev75b28af2019-08-26 17:23:46 +00007686 pages = (size_t)1 << get_order(
7687 rings_size(sq_entries, cq_entries, NULL));
7688 pages += (size_t)1 << get_order(
7689 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007690
Hristo Venev75b28af2019-08-26 17:23:46 +00007691 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007692}
7693
Jens Axboeedafcce2019-01-09 09:16:05 -07007694static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7695{
7696 int i, j;
7697
7698 if (!ctx->user_bufs)
7699 return -ENXIO;
7700
7701 for (i = 0; i < ctx->nr_user_bufs; i++) {
7702 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7703
7704 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007705 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007706
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007707 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007708 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007709 imu->nr_bvecs = 0;
7710 }
7711
7712 kfree(ctx->user_bufs);
7713 ctx->user_bufs = NULL;
7714 ctx->nr_user_bufs = 0;
7715 return 0;
7716}
7717
7718static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7719 void __user *arg, unsigned index)
7720{
7721 struct iovec __user *src;
7722
7723#ifdef CONFIG_COMPAT
7724 if (ctx->compat) {
7725 struct compat_iovec __user *ciovs;
7726 struct compat_iovec ciov;
7727
7728 ciovs = (struct compat_iovec __user *) arg;
7729 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7730 return -EFAULT;
7731
Jens Axboed55e5f52019-12-11 16:12:15 -07007732 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007733 dst->iov_len = ciov.iov_len;
7734 return 0;
7735 }
7736#endif
7737 src = (struct iovec __user *) arg;
7738 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7739 return -EFAULT;
7740 return 0;
7741}
7742
7743static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7744 unsigned nr_args)
7745{
7746 struct vm_area_struct **vmas = NULL;
7747 struct page **pages = NULL;
7748 int i, j, got_pages = 0;
7749 int ret = -EINVAL;
7750
7751 if (ctx->user_bufs)
7752 return -EBUSY;
7753 if (!nr_args || nr_args > UIO_MAXIOV)
7754 return -EINVAL;
7755
7756 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7757 GFP_KERNEL);
7758 if (!ctx->user_bufs)
7759 return -ENOMEM;
7760
7761 for (i = 0; i < nr_args; i++) {
7762 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7763 unsigned long off, start, end, ubuf;
7764 int pret, nr_pages;
7765 struct iovec iov;
7766 size_t size;
7767
7768 ret = io_copy_iov(ctx, &iov, arg, i);
7769 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007770 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007771
7772 /*
7773 * Don't impose further limits on the size and buffer
7774 * constraints here, we'll -EINVAL later when IO is
7775 * submitted if they are wrong.
7776 */
7777 ret = -EFAULT;
7778 if (!iov.iov_base || !iov.iov_len)
7779 goto err;
7780
7781 /* arbitrary limit, but we need something */
7782 if (iov.iov_len > SZ_1G)
7783 goto err;
7784
7785 ubuf = (unsigned long) iov.iov_base;
7786 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7787 start = ubuf >> PAGE_SHIFT;
7788 nr_pages = end - start;
7789
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007790 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007791 if (ret)
7792 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007793
7794 ret = 0;
7795 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007796 kvfree(vmas);
7797 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007798 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007799 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007800 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007801 sizeof(struct vm_area_struct *),
7802 GFP_KERNEL);
7803 if (!pages || !vmas) {
7804 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007805 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007806 goto err;
7807 }
7808 got_pages = nr_pages;
7809 }
7810
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007811 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007812 GFP_KERNEL);
7813 ret = -ENOMEM;
7814 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007815 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007816 goto err;
7817 }
7818
7819 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007820 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007821 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007822 FOLL_WRITE | FOLL_LONGTERM,
7823 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007824 if (pret == nr_pages) {
7825 /* don't support file backed memory */
7826 for (j = 0; j < nr_pages; j++) {
7827 struct vm_area_struct *vma = vmas[j];
7828
7829 if (vma->vm_file &&
7830 !is_file_hugepages(vma->vm_file)) {
7831 ret = -EOPNOTSUPP;
7832 break;
7833 }
7834 }
7835 } else {
7836 ret = pret < 0 ? pret : -EFAULT;
7837 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007838 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007839 if (ret) {
7840 /*
7841 * if we did partial map, or found file backed vmas,
7842 * release any pages we did get
7843 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007844 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007845 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007846 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007847 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007848 goto err;
7849 }
7850
7851 off = ubuf & ~PAGE_MASK;
7852 size = iov.iov_len;
7853 for (j = 0; j < nr_pages; j++) {
7854 size_t vec_len;
7855
7856 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7857 imu->bvec[j].bv_page = pages[j];
7858 imu->bvec[j].bv_len = vec_len;
7859 imu->bvec[j].bv_offset = off;
7860 off = 0;
7861 size -= vec_len;
7862 }
7863 /* store original address for later verification */
7864 imu->ubuf = ubuf;
7865 imu->len = iov.iov_len;
7866 imu->nr_bvecs = nr_pages;
7867
7868 ctx->nr_user_bufs++;
7869 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007870 kvfree(pages);
7871 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007872 return 0;
7873err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007874 kvfree(pages);
7875 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007876 io_sqe_buffer_unregister(ctx);
7877 return ret;
7878}
7879
Jens Axboe9b402842019-04-11 11:45:41 -06007880static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7881{
7882 __s32 __user *fds = arg;
7883 int fd;
7884
7885 if (ctx->cq_ev_fd)
7886 return -EBUSY;
7887
7888 if (copy_from_user(&fd, fds, sizeof(*fds)))
7889 return -EFAULT;
7890
7891 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7892 if (IS_ERR(ctx->cq_ev_fd)) {
7893 int ret = PTR_ERR(ctx->cq_ev_fd);
7894 ctx->cq_ev_fd = NULL;
7895 return ret;
7896 }
7897
7898 return 0;
7899}
7900
7901static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7902{
7903 if (ctx->cq_ev_fd) {
7904 eventfd_ctx_put(ctx->cq_ev_fd);
7905 ctx->cq_ev_fd = NULL;
7906 return 0;
7907 }
7908
7909 return -ENXIO;
7910}
7911
Jens Axboe5a2e7452020-02-23 16:23:11 -07007912static int __io_destroy_buffers(int id, void *p, void *data)
7913{
7914 struct io_ring_ctx *ctx = data;
7915 struct io_buffer *buf = p;
7916
Jens Axboe067524e2020-03-02 16:32:28 -07007917 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007918 return 0;
7919}
7920
7921static void io_destroy_buffers(struct io_ring_ctx *ctx)
7922{
7923 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7924 idr_destroy(&ctx->io_buffer_idr);
7925}
7926
Jens Axboe2b188cc2019-01-07 10:46:33 -07007927static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7928{
Jens Axboe6b063142019-01-10 22:13:58 -07007929 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007930 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06007931
7932 if (ctx->sqo_task) {
7933 put_task_struct(ctx->sqo_task);
7934 ctx->sqo_task = NULL;
7935 mmdrop(ctx->mm_account);
7936 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007937 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007938
Jens Axboe6b063142019-01-10 22:13:58 -07007939 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007940 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007941 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007942 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007943
Jens Axboe2b188cc2019-01-07 10:46:33 -07007944#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007945 if (ctx->ring_sock) {
7946 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007947 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007948 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007949#endif
7950
Hristo Venev75b28af2019-08-26 17:23:46 +00007951 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007952 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007953
7954 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007955 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007956 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007957 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007958 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007959 kfree(ctx);
7960}
7961
7962static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7963{
7964 struct io_ring_ctx *ctx = file->private_data;
7965 __poll_t mask = 0;
7966
7967 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007968 /*
7969 * synchronizes with barrier from wq_has_sleeper call in
7970 * io_commit_cqring
7971 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007972 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007973 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7974 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007975 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007976 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007977 mask |= EPOLLIN | EPOLLRDNORM;
7978
7979 return mask;
7980}
7981
7982static int io_uring_fasync(int fd, struct file *file, int on)
7983{
7984 struct io_ring_ctx *ctx = file->private_data;
7985
7986 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7987}
7988
Jens Axboe071698e2020-01-28 10:04:42 -07007989static int io_remove_personalities(int id, void *p, void *data)
7990{
7991 struct io_ring_ctx *ctx = data;
7992 const struct cred *cred;
7993
7994 cred = idr_remove(&ctx->personality_idr, id);
7995 if (cred)
7996 put_cred(cred);
7997 return 0;
7998}
7999
Jens Axboe85faa7b2020-04-09 18:14:00 -06008000static void io_ring_exit_work(struct work_struct *work)
8001{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008002 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8003 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008004
Jens Axboe56952e92020-06-17 15:00:04 -06008005 /*
8006 * If we're doing polled IO and end up having requests being
8007 * submitted async (out-of-line), then completions can come in while
8008 * we're waiting for refs to drop. We need to reap these manually,
8009 * as nobody else will be looking for them.
8010 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008011 do {
Jens Axboe56952e92020-06-17 15:00:04 -06008012 if (ctx->rings)
8013 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008014 io_iopoll_try_reap_events(ctx);
8015 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008016 io_ring_ctx_free(ctx);
8017}
8018
Jens Axboe2b188cc2019-01-07 10:46:33 -07008019static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8020{
8021 mutex_lock(&ctx->uring_lock);
8022 percpu_ref_kill(&ctx->refs);
8023 mutex_unlock(&ctx->uring_lock);
8024
Jens Axboef3606e32020-09-22 08:18:24 -06008025 io_kill_timeouts(ctx, NULL);
8026 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008027
8028 if (ctx->io_wq)
8029 io_wq_cancel_all(ctx->io_wq);
8030
Jens Axboe15dff282019-11-13 09:09:23 -07008031 /* if we failed setting up the ctx, we might not have any rings */
8032 if (ctx->rings)
8033 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008034 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008035 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008036
8037 /*
8038 * Do this upfront, so we won't have a grace period where the ring
8039 * is closed but resources aren't reaped yet. This can cause
8040 * spurious failure in setting up a new ring.
8041 */
Jens Axboe760618f2020-07-24 12:53:31 -06008042 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8043 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008044
Jens Axboe85faa7b2020-04-09 18:14:00 -06008045 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008046 /*
8047 * Use system_unbound_wq to avoid spawning tons of event kworkers
8048 * if we're exiting a ton of rings at the same time. It just adds
8049 * noise and overhead, there's no discernable change in runtime
8050 * over using system_wq.
8051 */
8052 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008053}
8054
8055static int io_uring_release(struct inode *inode, struct file *file)
8056{
8057 struct io_ring_ctx *ctx = file->private_data;
8058
8059 file->private_data = NULL;
8060 io_ring_ctx_wait_and_kill(ctx);
8061 return 0;
8062}
8063
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008064static bool io_wq_files_match(struct io_wq_work *work, void *data)
8065{
8066 struct files_struct *files = data;
8067
8068 return work->files == files;
8069}
8070
Jens Axboef254ac02020-08-12 17:33:30 -06008071/*
8072 * Returns true if 'preq' is the link parent of 'req'
8073 */
8074static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8075{
8076 struct io_kiocb *link;
8077
8078 if (!(preq->flags & REQ_F_LINK_HEAD))
8079 return false;
8080
8081 list_for_each_entry(link, &preq->link_list, link_list) {
8082 if (link == req)
8083 return true;
8084 }
8085
8086 return false;
8087}
8088
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008089static inline bool io_match_files(struct io_kiocb *req,
8090 struct files_struct *files)
8091{
8092 return (req->flags & REQ_F_WORK_INITIALIZED) && req->work.files == files;
8093}
8094
8095static bool io_match_link_files(struct io_kiocb *req,
8096 struct files_struct *files)
8097{
8098 struct io_kiocb *link;
8099
8100 if (io_match_files(req, files))
8101 return true;
8102 if (req->flags & REQ_F_LINK_HEAD) {
8103 list_for_each_entry(link, &req->link_list, link_list) {
8104 if (io_match_files(link, files))
8105 return true;
8106 }
8107 }
8108 return false;
8109}
8110
Jens Axboef254ac02020-08-12 17:33:30 -06008111/*
8112 * We're looking to cancel 'req' because it's holding on to our files, but
8113 * 'req' could be a link to another request. See if it is, and cancel that
8114 * parent request if so.
8115 */
8116static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8117{
8118 struct hlist_node *tmp;
8119 struct io_kiocb *preq;
8120 bool found = false;
8121 int i;
8122
8123 spin_lock_irq(&ctx->completion_lock);
8124 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8125 struct hlist_head *list;
8126
8127 list = &ctx->cancel_hash[i];
8128 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8129 found = io_match_link(preq, req);
8130 if (found) {
8131 io_poll_remove_one(preq);
8132 break;
8133 }
8134 }
8135 }
8136 spin_unlock_irq(&ctx->completion_lock);
8137 return found;
8138}
8139
8140static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8141 struct io_kiocb *req)
8142{
8143 struct io_kiocb *preq;
8144 bool found = false;
8145
8146 spin_lock_irq(&ctx->completion_lock);
8147 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8148 found = io_match_link(preq, req);
8149 if (found) {
8150 __io_timeout_cancel(preq);
8151 break;
8152 }
8153 }
8154 spin_unlock_irq(&ctx->completion_lock);
8155 return found;
8156}
8157
Jens Axboeb711d4e2020-08-16 08:23:05 -07008158static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8159{
8160 return io_match_link(container_of(work, struct io_kiocb, work), data);
8161}
8162
8163static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8164{
8165 enum io_wq_cancel cret;
8166
8167 /* cancel this particular work, if it's running */
8168 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8169 if (cret != IO_WQ_CANCEL_NOTFOUND)
8170 return;
8171
8172 /* find links that hold this pending, cancel those */
8173 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8174 if (cret != IO_WQ_CANCEL_NOTFOUND)
8175 return;
8176
8177 /* if we have a poll link holding this pending, cancel that */
8178 if (io_poll_remove_link(ctx, req))
8179 return;
8180
8181 /* final option, timeout link is holding this req pending */
8182 io_timeout_remove_link(ctx, req);
8183}
8184
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008185static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8186 struct files_struct *files)
8187{
8188 struct io_defer_entry *de = NULL;
8189 LIST_HEAD(list);
8190
8191 spin_lock_irq(&ctx->completion_lock);
8192 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008193 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008194 list_cut_position(&list, &ctx->defer_list, &de->list);
8195 break;
8196 }
8197 }
8198 spin_unlock_irq(&ctx->completion_lock);
8199
8200 while (!list_empty(&list)) {
8201 de = list_first_entry(&list, struct io_defer_entry, list);
8202 list_del_init(&de->list);
8203 req_set_fail_links(de->req);
8204 io_put_req(de->req);
8205 io_req_complete(de->req, -ECANCELED);
8206 kfree(de);
8207 }
8208}
8209
Jens Axboefcb323c2019-10-24 12:39:47 -06008210static void io_uring_cancel_files(struct io_ring_ctx *ctx,
8211 struct files_struct *files)
8212{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008213 if (list_empty_careful(&ctx->inflight_list))
8214 return;
8215
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008216 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008217 /* cancel all at once, should be faster than doing it one by one*/
8218 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8219
Jens Axboefcb323c2019-10-24 12:39:47 -06008220 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008221 struct io_kiocb *cancel_req = NULL, *req;
8222 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008223
8224 spin_lock_irq(&ctx->inflight_lock);
8225 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07008226 if (req->work.files != files)
8227 continue;
8228 /* req is being completed, ignore */
8229 if (!refcount_inc_not_zero(&req->refs))
8230 continue;
8231 cancel_req = req;
8232 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008233 }
Jens Axboe768134d2019-11-10 20:30:53 -07008234 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008235 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008236 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008237 spin_unlock_irq(&ctx->inflight_lock);
8238
Jens Axboe768134d2019-11-10 20:30:53 -07008239 /* We need to keep going until we don't find a matching req */
8240 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008241 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008242 /* cancel this request, or head link requests */
8243 io_attempt_cancel(ctx, cancel_req);
8244 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008245 /* cancellations _may_ trigger task work */
8246 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008247 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008248 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008249 }
8250}
8251
Pavel Begunkov801dd572020-06-15 10:33:14 +03008252static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008253{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008254 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8255 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008256
Jens Axboef3606e32020-09-22 08:18:24 -06008257 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008258}
8259
Jens Axboefcb323c2019-10-24 12:39:47 -06008260static int io_uring_flush(struct file *file, void *data)
8261{
8262 struct io_ring_ctx *ctx = file->private_data;
8263
8264 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07008265
8266 /*
8267 * If the task is going away, cancel work it may have pending
8268 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008269 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
8270 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07008271
Jens Axboefcb323c2019-10-24 12:39:47 -06008272 return 0;
8273}
8274
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008275static void *io_uring_validate_mmap_request(struct file *file,
8276 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008277{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008278 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008279 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008280 struct page *page;
8281 void *ptr;
8282
8283 switch (offset) {
8284 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008285 case IORING_OFF_CQ_RING:
8286 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008287 break;
8288 case IORING_OFF_SQES:
8289 ptr = ctx->sq_sqes;
8290 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008291 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008292 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008293 }
8294
8295 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008296 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008297 return ERR_PTR(-EINVAL);
8298
8299 return ptr;
8300}
8301
8302#ifdef CONFIG_MMU
8303
8304static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8305{
8306 size_t sz = vma->vm_end - vma->vm_start;
8307 unsigned long pfn;
8308 void *ptr;
8309
8310 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8311 if (IS_ERR(ptr))
8312 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008313
8314 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8315 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8316}
8317
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008318#else /* !CONFIG_MMU */
8319
8320static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8321{
8322 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8323}
8324
8325static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8326{
8327 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8328}
8329
8330static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8331 unsigned long addr, unsigned long len,
8332 unsigned long pgoff, unsigned long flags)
8333{
8334 void *ptr;
8335
8336 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8337 if (IS_ERR(ptr))
8338 return PTR_ERR(ptr);
8339
8340 return (unsigned long) ptr;
8341}
8342
8343#endif /* !CONFIG_MMU */
8344
Jens Axboe2b188cc2019-01-07 10:46:33 -07008345SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8346 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8347 size_t, sigsz)
8348{
8349 struct io_ring_ctx *ctx;
8350 long ret = -EBADF;
8351 int submitted = 0;
8352 struct fd f;
8353
Jens Axboe4c6e2772020-07-01 11:29:10 -06008354 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008355
Jens Axboe6c271ce2019-01-10 11:22:30 -07008356 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008357 return -EINVAL;
8358
8359 f = fdget(fd);
8360 if (!f.file)
8361 return -EBADF;
8362
8363 ret = -EOPNOTSUPP;
8364 if (f.file->f_op != &io_uring_fops)
8365 goto out_fput;
8366
8367 ret = -ENXIO;
8368 ctx = f.file->private_data;
8369 if (!percpu_ref_tryget(&ctx->refs))
8370 goto out_fput;
8371
Jens Axboe6c271ce2019-01-10 11:22:30 -07008372 /*
8373 * For SQ polling, the thread will do all submissions and completions.
8374 * Just return the requested submit count, and wake the thread if
8375 * we were asked to.
8376 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008377 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008378 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008379 if (!list_empty_careful(&ctx->cq_overflow_list))
8380 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008381 if (flags & IORING_ENTER_SQ_WAKEUP)
8382 wake_up(&ctx->sqo_wait);
8383 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008384 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07008385 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03008386 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008387 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008388
8389 if (submitted != to_submit)
8390 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008391 }
8392 if (flags & IORING_ENTER_GETEVENTS) {
8393 min_complete = min(min_complete, ctx->cq_entries);
8394
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008395 /*
8396 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8397 * space applications don't need to do io completion events
8398 * polling again, they can rely on io_sq_thread to do polling
8399 * work, which can reduce cpu usage and uring_lock contention.
8400 */
8401 if (ctx->flags & IORING_SETUP_IOPOLL &&
8402 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008403 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008404 } else {
8405 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8406 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008407 }
8408
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008409out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008410 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008411out_fput:
8412 fdput(f);
8413 return submitted ? submitted : ret;
8414}
8415
Tobias Klauserbebdb652020-02-26 18:38:32 +01008416#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008417static int io_uring_show_cred(int id, void *p, void *data)
8418{
8419 const struct cred *cred = p;
8420 struct seq_file *m = data;
8421 struct user_namespace *uns = seq_user_ns(m);
8422 struct group_info *gi;
8423 kernel_cap_t cap;
8424 unsigned __capi;
8425 int g;
8426
8427 seq_printf(m, "%5d\n", id);
8428 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8429 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8430 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8431 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8432 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8433 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8434 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8435 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8436 seq_puts(m, "\n\tGroups:\t");
8437 gi = cred->group_info;
8438 for (g = 0; g < gi->ngroups; g++) {
8439 seq_put_decimal_ull(m, g ? " " : "",
8440 from_kgid_munged(uns, gi->gid[g]));
8441 }
8442 seq_puts(m, "\n\tCapEff:\t");
8443 cap = cred->cap_effective;
8444 CAP_FOR_EACH_U32(__capi)
8445 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8446 seq_putc(m, '\n');
8447 return 0;
8448}
8449
8450static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8451{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008452 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008453 int i;
8454
Jens Axboefad8e0d2020-09-28 08:57:48 -06008455 /*
8456 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8457 * since fdinfo case grabs it in the opposite direction of normal use
8458 * cases. If we fail to get the lock, we just don't iterate any
8459 * structures that could be going away outside the io_uring mutex.
8460 */
8461 has_lock = mutex_trylock(&ctx->uring_lock);
8462
Jens Axboe87ce9552020-01-30 08:25:34 -07008463 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008464 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008465 struct fixed_file_table *table;
8466 struct file *f;
8467
8468 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8469 f = table->files[i & IORING_FILE_TABLE_MASK];
8470 if (f)
8471 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8472 else
8473 seq_printf(m, "%5u: <none>\n", i);
8474 }
8475 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008476 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008477 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8478
8479 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8480 (unsigned int) buf->len);
8481 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008482 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008483 seq_printf(m, "Personalities:\n");
8484 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8485 }
Jens Axboed7718a92020-02-14 22:23:12 -07008486 seq_printf(m, "PollList:\n");
8487 spin_lock_irq(&ctx->completion_lock);
8488 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8489 struct hlist_head *list = &ctx->cancel_hash[i];
8490 struct io_kiocb *req;
8491
8492 hlist_for_each_entry(req, list, hash_node)
8493 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8494 req->task->task_works != NULL);
8495 }
8496 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008497 if (has_lock)
8498 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008499}
8500
8501static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8502{
8503 struct io_ring_ctx *ctx = f->private_data;
8504
8505 if (percpu_ref_tryget(&ctx->refs)) {
8506 __io_uring_show_fdinfo(ctx, m);
8507 percpu_ref_put(&ctx->refs);
8508 }
8509}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008510#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008511
Jens Axboe2b188cc2019-01-07 10:46:33 -07008512static const struct file_operations io_uring_fops = {
8513 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008514 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008515 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008516#ifndef CONFIG_MMU
8517 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8518 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8519#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008520 .poll = io_uring_poll,
8521 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008522#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008523 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008524#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008525};
8526
8527static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8528 struct io_uring_params *p)
8529{
Hristo Venev75b28af2019-08-26 17:23:46 +00008530 struct io_rings *rings;
8531 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008532
Jens Axboebd740482020-08-05 12:58:23 -06008533 /* make sure these are sane, as we already accounted them */
8534 ctx->sq_entries = p->sq_entries;
8535 ctx->cq_entries = p->cq_entries;
8536
Hristo Venev75b28af2019-08-26 17:23:46 +00008537 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8538 if (size == SIZE_MAX)
8539 return -EOVERFLOW;
8540
8541 rings = io_mem_alloc(size);
8542 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008543 return -ENOMEM;
8544
Hristo Venev75b28af2019-08-26 17:23:46 +00008545 ctx->rings = rings;
8546 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8547 rings->sq_ring_mask = p->sq_entries - 1;
8548 rings->cq_ring_mask = p->cq_entries - 1;
8549 rings->sq_ring_entries = p->sq_entries;
8550 rings->cq_ring_entries = p->cq_entries;
8551 ctx->sq_mask = rings->sq_ring_mask;
8552 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008553
8554 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008555 if (size == SIZE_MAX) {
8556 io_mem_free(ctx->rings);
8557 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008558 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008559 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008560
8561 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008562 if (!ctx->sq_sqes) {
8563 io_mem_free(ctx->rings);
8564 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008565 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008566 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008567
Jens Axboe2b188cc2019-01-07 10:46:33 -07008568 return 0;
8569}
8570
8571/*
8572 * Allocate an anonymous fd, this is what constitutes the application
8573 * visible backing of an io_uring instance. The application mmaps this
8574 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8575 * we have to tie this fd to a socket for file garbage collection purposes.
8576 */
8577static int io_uring_get_fd(struct io_ring_ctx *ctx)
8578{
8579 struct file *file;
8580 int ret;
8581
8582#if defined(CONFIG_UNIX)
8583 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8584 &ctx->ring_sock);
8585 if (ret)
8586 return ret;
8587#endif
8588
8589 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8590 if (ret < 0)
8591 goto err;
8592
8593 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8594 O_RDWR | O_CLOEXEC);
8595 if (IS_ERR(file)) {
8596 put_unused_fd(ret);
8597 ret = PTR_ERR(file);
8598 goto err;
8599 }
8600
8601#if defined(CONFIG_UNIX)
8602 ctx->ring_sock->file = file;
8603#endif
8604 fd_install(ret, file);
8605 return ret;
8606err:
8607#if defined(CONFIG_UNIX)
8608 sock_release(ctx->ring_sock);
8609 ctx->ring_sock = NULL;
8610#endif
8611 return ret;
8612}
8613
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008614static int io_uring_create(unsigned entries, struct io_uring_params *p,
8615 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008616{
8617 struct user_struct *user = NULL;
8618 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008619 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008620 int ret;
8621
Jens Axboe8110c1a2019-12-28 15:39:54 -07008622 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008623 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008624 if (entries > IORING_MAX_ENTRIES) {
8625 if (!(p->flags & IORING_SETUP_CLAMP))
8626 return -EINVAL;
8627 entries = IORING_MAX_ENTRIES;
8628 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008629
8630 /*
8631 * Use twice as many entries for the CQ ring. It's possible for the
8632 * application to drive a higher depth than the size of the SQ ring,
8633 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008634 * some flexibility in overcommitting a bit. If the application has
8635 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8636 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008637 */
8638 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008639 if (p->flags & IORING_SETUP_CQSIZE) {
8640 /*
8641 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8642 * to a power-of-two, if it isn't already. We do NOT impose
8643 * any cq vs sq ring sizing.
8644 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008645 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008646 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008647 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8648 if (!(p->flags & IORING_SETUP_CLAMP))
8649 return -EINVAL;
8650 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8651 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008652 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8653 } else {
8654 p->cq_entries = 2 * p->sq_entries;
8655 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008656
8657 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008658 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008659
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008660 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008661 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008662 ring_pages(p->sq_entries, p->cq_entries));
8663 if (ret) {
8664 free_uid(user);
8665 return ret;
8666 }
8667 }
8668
8669 ctx = io_ring_ctx_alloc(p);
8670 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008671 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008672 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008673 p->cq_entries));
8674 free_uid(user);
8675 return -ENOMEM;
8676 }
8677 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008678 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008679 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008680
Jens Axboe2aede0e2020-09-14 10:45:53 -06008681 ctx->sqo_task = get_task_struct(current);
8682
8683 /*
8684 * This is just grabbed for accounting purposes. When a process exits,
8685 * the mm is exited and dropped before the files, hence we need to hang
8686 * on to this mm purely for the purposes of being able to unaccount
8687 * memory (locked/pinned vm). It's not used for anything else.
8688 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06008689 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008690 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06008691
Jens Axboef74441e2020-08-05 13:00:44 -06008692 /*
8693 * Account memory _before_ installing the file descriptor. Once
8694 * the descriptor is installed, it can get closed at any time. Also
8695 * do this before hitting the general error path, as ring freeing
8696 * will un-account as well.
8697 */
8698 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8699 ACCT_LOCKED);
8700 ctx->limit_mem = limit_mem;
8701
Jens Axboe2b188cc2019-01-07 10:46:33 -07008702 ret = io_allocate_scq_urings(ctx, p);
8703 if (ret)
8704 goto err;
8705
Jens Axboe6c271ce2019-01-10 11:22:30 -07008706 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008707 if (ret)
8708 goto err;
8709
Jens Axboe2b188cc2019-01-07 10:46:33 -07008710 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008711 p->sq_off.head = offsetof(struct io_rings, sq.head);
8712 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8713 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8714 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8715 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8716 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8717 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008718
8719 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008720 p->cq_off.head = offsetof(struct io_rings, cq.head);
8721 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8722 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8723 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8724 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8725 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008726 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008727
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008728 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8729 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008730 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8731 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008732
8733 if (copy_to_user(params, p, sizeof(*p))) {
8734 ret = -EFAULT;
8735 goto err;
8736 }
Jens Axboed1719f72020-07-30 13:43:53 -06008737
8738 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06008739 * Install ring fd as the very last thing, so we don't risk someone
8740 * having closed it before we finish setup
8741 */
8742 ret = io_uring_get_fd(ctx);
8743 if (ret < 0)
8744 goto err;
8745
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008746 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008747 return ret;
8748err:
8749 io_ring_ctx_wait_and_kill(ctx);
8750 return ret;
8751}
8752
8753/*
8754 * Sets up an aio uring context, and returns the fd. Applications asks for a
8755 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8756 * params structure passed in.
8757 */
8758static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8759{
8760 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008761 int i;
8762
8763 if (copy_from_user(&p, params, sizeof(p)))
8764 return -EFAULT;
8765 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8766 if (p.resv[i])
8767 return -EINVAL;
8768 }
8769
Jens Axboe6c271ce2019-01-10 11:22:30 -07008770 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008771 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008772 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008773 return -EINVAL;
8774
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008775 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008776}
8777
8778SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8779 struct io_uring_params __user *, params)
8780{
8781 return io_uring_setup(entries, params);
8782}
8783
Jens Axboe66f4af92020-01-16 15:36:52 -07008784static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8785{
8786 struct io_uring_probe *p;
8787 size_t size;
8788 int i, ret;
8789
8790 size = struct_size(p, ops, nr_args);
8791 if (size == SIZE_MAX)
8792 return -EOVERFLOW;
8793 p = kzalloc(size, GFP_KERNEL);
8794 if (!p)
8795 return -ENOMEM;
8796
8797 ret = -EFAULT;
8798 if (copy_from_user(p, arg, size))
8799 goto out;
8800 ret = -EINVAL;
8801 if (memchr_inv(p, 0, size))
8802 goto out;
8803
8804 p->last_op = IORING_OP_LAST - 1;
8805 if (nr_args > IORING_OP_LAST)
8806 nr_args = IORING_OP_LAST;
8807
8808 for (i = 0; i < nr_args; i++) {
8809 p->ops[i].op = i;
8810 if (!io_op_defs[i].not_supported)
8811 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8812 }
8813 p->ops_len = i;
8814
8815 ret = 0;
8816 if (copy_to_user(arg, p, size))
8817 ret = -EFAULT;
8818out:
8819 kfree(p);
8820 return ret;
8821}
8822
Jens Axboe071698e2020-01-28 10:04:42 -07008823static int io_register_personality(struct io_ring_ctx *ctx)
8824{
8825 const struct cred *creds = get_current_cred();
8826 int id;
8827
8828 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8829 USHRT_MAX, GFP_KERNEL);
8830 if (id < 0)
8831 put_cred(creds);
8832 return id;
8833}
8834
8835static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8836{
8837 const struct cred *old_creds;
8838
8839 old_creds = idr_remove(&ctx->personality_idr, id);
8840 if (old_creds) {
8841 put_cred(old_creds);
8842 return 0;
8843 }
8844
8845 return -EINVAL;
8846}
8847
8848static bool io_register_op_must_quiesce(int op)
8849{
8850 switch (op) {
8851 case IORING_UNREGISTER_FILES:
8852 case IORING_REGISTER_FILES_UPDATE:
8853 case IORING_REGISTER_PROBE:
8854 case IORING_REGISTER_PERSONALITY:
8855 case IORING_UNREGISTER_PERSONALITY:
8856 return false;
8857 default:
8858 return true;
8859 }
8860}
8861
Jens Axboeedafcce2019-01-09 09:16:05 -07008862static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8863 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008864 __releases(ctx->uring_lock)
8865 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008866{
8867 int ret;
8868
Jens Axboe35fa71a2019-04-22 10:23:23 -06008869 /*
8870 * We're inside the ring mutex, if the ref is already dying, then
8871 * someone else killed the ctx or is already going through
8872 * io_uring_register().
8873 */
8874 if (percpu_ref_is_dying(&ctx->refs))
8875 return -ENXIO;
8876
Jens Axboe071698e2020-01-28 10:04:42 -07008877 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008878 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008879
Jens Axboe05f3fb32019-12-09 11:22:50 -07008880 /*
8881 * Drop uring mutex before waiting for references to exit. If
8882 * another thread is currently inside io_uring_enter() it might
8883 * need to grab the uring_lock to make progress. If we hold it
8884 * here across the drain wait, then we can deadlock. It's safe
8885 * to drop the mutex here, since no new references will come in
8886 * after we've killed the percpu ref.
8887 */
8888 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008889 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008890 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008891 if (ret) {
8892 percpu_ref_resurrect(&ctx->refs);
8893 ret = -EINTR;
8894 goto out;
8895 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008896 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008897
8898 switch (opcode) {
8899 case IORING_REGISTER_BUFFERS:
8900 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8901 break;
8902 case IORING_UNREGISTER_BUFFERS:
8903 ret = -EINVAL;
8904 if (arg || nr_args)
8905 break;
8906 ret = io_sqe_buffer_unregister(ctx);
8907 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008908 case IORING_REGISTER_FILES:
8909 ret = io_sqe_files_register(ctx, arg, nr_args);
8910 break;
8911 case IORING_UNREGISTER_FILES:
8912 ret = -EINVAL;
8913 if (arg || nr_args)
8914 break;
8915 ret = io_sqe_files_unregister(ctx);
8916 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008917 case IORING_REGISTER_FILES_UPDATE:
8918 ret = io_sqe_files_update(ctx, arg, nr_args);
8919 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008920 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008921 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008922 ret = -EINVAL;
8923 if (nr_args != 1)
8924 break;
8925 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008926 if (ret)
8927 break;
8928 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8929 ctx->eventfd_async = 1;
8930 else
8931 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008932 break;
8933 case IORING_UNREGISTER_EVENTFD:
8934 ret = -EINVAL;
8935 if (arg || nr_args)
8936 break;
8937 ret = io_eventfd_unregister(ctx);
8938 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008939 case IORING_REGISTER_PROBE:
8940 ret = -EINVAL;
8941 if (!arg || nr_args > 256)
8942 break;
8943 ret = io_probe(ctx, arg, nr_args);
8944 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008945 case IORING_REGISTER_PERSONALITY:
8946 ret = -EINVAL;
8947 if (arg || nr_args)
8948 break;
8949 ret = io_register_personality(ctx);
8950 break;
8951 case IORING_UNREGISTER_PERSONALITY:
8952 ret = -EINVAL;
8953 if (arg)
8954 break;
8955 ret = io_unregister_personality(ctx, nr_args);
8956 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008957 default:
8958 ret = -EINVAL;
8959 break;
8960 }
8961
Jens Axboe071698e2020-01-28 10:04:42 -07008962 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008963 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008964 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008965out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008966 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008967 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008968 return ret;
8969}
8970
8971SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8972 void __user *, arg, unsigned int, nr_args)
8973{
8974 struct io_ring_ctx *ctx;
8975 long ret = -EBADF;
8976 struct fd f;
8977
8978 f = fdget(fd);
8979 if (!f.file)
8980 return -EBADF;
8981
8982 ret = -EOPNOTSUPP;
8983 if (f.file->f_op != &io_uring_fops)
8984 goto out_fput;
8985
8986 ctx = f.file->private_data;
8987
8988 mutex_lock(&ctx->uring_lock);
8989 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8990 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008991 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8992 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008993out_fput:
8994 fdput(f);
8995 return ret;
8996}
8997
Jens Axboe2b188cc2019-01-07 10:46:33 -07008998static int __init io_uring_init(void)
8999{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009000#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9001 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9002 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9003} while (0)
9004
9005#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9006 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9007 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9008 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9009 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9010 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9011 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9012 BUILD_BUG_SQE_ELEM(8, __u64, off);
9013 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9014 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009015 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009016 BUILD_BUG_SQE_ELEM(24, __u32, len);
9017 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9018 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9019 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9020 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009021 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9022 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009023 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9024 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9025 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9026 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9027 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9028 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9029 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9030 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009031 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009032 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9033 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9034 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009035 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009036
Jens Axboed3656342019-12-18 09:50:26 -07009037 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009038 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009039 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9040 return 0;
9041};
9042__initcall(io_uring_init);