blob: 3e406bc1f8550103101c7ab94809c767df1bec84 [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 */
268 struct mm_struct *sqo_mm;
269 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700270
Jens Axboe6b063142019-01-10 22:13:58 -0700271 /*
272 * If used, fixed file set. Writers must ensure that ->refs is dead,
273 * readers must ensure that ->refs is alive as long as the file* is
274 * used. Only updated through io_uring_register(2).
275 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700276 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700277 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300278 int ring_fd;
279 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700280
Jens Axboeedafcce2019-01-09 09:16:05 -0700281 /* if used, fixed mapped user buffers */
282 unsigned nr_user_bufs;
283 struct io_mapped_ubuf *user_bufs;
284
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285 struct user_struct *user;
286
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700287 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700288
Jens Axboe0f158b42020-05-14 17:18:39 -0600289 struct completion ref_comp;
290 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700291
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700292 /* if all else fails... */
293 struct io_kiocb *fallback_req;
294
Jens Axboe206aefd2019-11-07 18:27:42 -0700295#if defined(CONFIG_UNIX)
296 struct socket *ring_sock;
297#endif
298
Jens Axboe5a2e7452020-02-23 16:23:11 -0700299 struct idr io_buffer_idr;
300
Jens Axboe071698e2020-01-28 10:04:42 -0700301 struct idr personality_idr;
302
Jens Axboe206aefd2019-11-07 18:27:42 -0700303 struct {
304 unsigned cached_cq_tail;
305 unsigned cq_entries;
306 unsigned cq_mask;
307 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700308 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700309 struct wait_queue_head cq_wait;
310 struct fasync_struct *cq_fasync;
311 struct eventfd_ctx *cq_ev_fd;
312 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313
314 struct {
315 struct mutex uring_lock;
316 wait_queue_head_t wait;
317 } ____cacheline_aligned_in_smp;
318
319 struct {
320 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700321
Jens Axboedef596e2019-01-09 08:59:42 -0700322 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300323 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700324 * io_uring instances that don't use IORING_SETUP_SQPOLL.
325 * For SQPOLL, only the single threaded io_sq_thread() will
326 * manipulate the list, hence no extra locking is needed there.
327 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300328 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700329 struct hlist_head *cancel_hash;
330 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700331 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600332
333 spinlock_t inflight_lock;
334 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600336
Jens Axboe4a38aed22020-05-14 17:21:15 -0600337 struct delayed_work file_put_work;
338 struct llist_head file_put_llist;
339
Jens Axboe85faa7b2020-04-09 18:14:00 -0600340 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341};
342
Jens Axboe09bb8392019-03-13 12:39:28 -0600343/*
344 * First field must be the file pointer in all the
345 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
346 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700347struct io_poll_iocb {
348 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700349 union {
350 struct wait_queue_head *head;
351 u64 addr;
352 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700353 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600354 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700355 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700356 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700357};
358
Jens Axboeb5dba592019-12-11 14:02:38 -0700359struct io_close {
360 struct file *file;
361 struct file *put_file;
362 int fd;
363};
364
Jens Axboead8a48a2019-11-15 08:49:11 -0700365struct io_timeout_data {
366 struct io_kiocb *req;
367 struct hrtimer timer;
368 struct timespec64 ts;
369 enum hrtimer_mode mode;
370};
371
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700372struct io_accept {
373 struct file *file;
374 struct sockaddr __user *addr;
375 int __user *addr_len;
376 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600377 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700378};
379
380struct io_sync {
381 struct file *file;
382 loff_t len;
383 loff_t off;
384 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700385 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700386};
387
Jens Axboefbf23842019-12-17 18:45:56 -0700388struct io_cancel {
389 struct file *file;
390 u64 addr;
391};
392
Jens Axboeb29472e2019-12-17 18:50:29 -0700393struct io_timeout {
394 struct file *file;
395 u64 addr;
396 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300397 u32 off;
398 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300399 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700400};
401
Jens Axboe9adbd452019-12-20 08:45:55 -0700402struct io_rw {
403 /* NOTE: kiocb has the file as the first member, so don't do it here */
404 struct kiocb kiocb;
405 u64 addr;
406 u64 len;
407};
408
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700409struct io_connect {
410 struct file *file;
411 struct sockaddr __user *addr;
412 int addr_len;
413};
414
Jens Axboee47293f2019-12-20 08:58:21 -0700415struct io_sr_msg {
416 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700417 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300418 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700419 void __user *buf;
420 };
Jens Axboee47293f2019-12-20 08:58:21 -0700421 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700422 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700423 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700424 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700425};
426
Jens Axboe15b71ab2019-12-11 11:20:36 -0700427struct io_open {
428 struct file *file;
429 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700430 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700431 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600432 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700433};
434
Jens Axboe05f3fb32019-12-09 11:22:50 -0700435struct io_files_update {
436 struct file *file;
437 u64 arg;
438 u32 nr_args;
439 u32 offset;
440};
441
Jens Axboe4840e412019-12-25 22:03:45 -0700442struct io_fadvise {
443 struct file *file;
444 u64 offset;
445 u32 len;
446 u32 advice;
447};
448
Jens Axboec1ca7572019-12-25 22:18:28 -0700449struct io_madvise {
450 struct file *file;
451 u64 addr;
452 u32 len;
453 u32 advice;
454};
455
Jens Axboe3e4827b2020-01-08 15:18:09 -0700456struct io_epoll {
457 struct file *file;
458 int epfd;
459 int op;
460 int fd;
461 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700462};
463
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300464struct io_splice {
465 struct file *file_out;
466 struct file *file_in;
467 loff_t off_out;
468 loff_t off_in;
469 u64 len;
470 unsigned int flags;
471};
472
Jens Axboeddf0322d2020-02-23 16:41:33 -0700473struct io_provide_buf {
474 struct file *file;
475 __u64 addr;
476 __s32 len;
477 __u32 bgid;
478 __u16 nbufs;
479 __u16 bid;
480};
481
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700482struct io_statx {
483 struct file *file;
484 int dfd;
485 unsigned int mask;
486 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700487 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700488 struct statx __user *buffer;
489};
490
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300491struct io_completion {
492 struct file *file;
493 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300494 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300495};
496
Jens Axboef499a022019-12-02 16:28:46 -0700497struct io_async_connect {
498 struct sockaddr_storage address;
499};
500
Jens Axboe03b12302019-12-02 18:50:25 -0700501struct io_async_msghdr {
502 struct iovec fast_iov[UIO_FASTIOV];
503 struct iovec *iov;
504 struct sockaddr __user *uaddr;
505 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700506 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700507};
508
Jens Axboef67676d2019-12-02 11:03:47 -0700509struct io_async_rw {
510 struct iovec fast_iov[UIO_FASTIOV];
511 struct iovec *iov;
512 ssize_t nr_segs;
513 ssize_t size;
Jens Axboebcf5a062020-05-22 09:24:42 -0600514 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700515};
516
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700517struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700518 union {
519 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700520 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700521 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700522 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700523 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700524};
525
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300526enum {
527 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
528 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
529 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
530 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
531 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700532 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300533
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300534 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300535 REQ_F_FAIL_LINK_BIT,
536 REQ_F_INFLIGHT_BIT,
537 REQ_F_CUR_POS_BIT,
538 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300539 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300540 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300541 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300542 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700543 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700544 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700545 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600546 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800547 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300548 REQ_F_TASK_PINNED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700549
550 /* not a real bit, just to check we're not overflowing the space */
551 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300552};
553
554enum {
555 /* ctx owns file */
556 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
557 /* drain existing IO first */
558 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
559 /* linked sqes */
560 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
561 /* doesn't sever on completion < 0 */
562 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
563 /* IOSQE_ASYNC */
564 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700565 /* IOSQE_BUFFER_SELECT */
566 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300567
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300568 /* head of a link */
569 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300570 /* fail rest of links */
571 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
572 /* on inflight list */
573 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
574 /* read/write uses file position */
575 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
576 /* must not punt to workers */
577 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300578 /* has linked timeout */
579 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300580 /* regular file */
581 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300582 /* completion under lock */
583 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300584 /* needs cleanup */
585 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700586 /* in overflow list */
587 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700588 /* already went through poll handler */
589 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700590 /* buffer already selected */
591 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600592 /* doesn't need file table for this request */
593 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800594 /* io_wq_work is initialized */
595 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300596 /* req->task is refcounted */
597 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700598};
599
600struct async_poll {
601 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600602 struct io_poll_iocb *double_poll;
Jens Axboed7718a92020-02-14 22:23:12 -0700603 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300604};
605
Jens Axboe09bb8392019-03-13 12:39:28 -0600606/*
607 * NOTE! Each of the iocb union members has the file pointer
608 * as the first entry in their struct definition. So you can
609 * access the file pointer through any of the sub-structs,
610 * or directly as just 'ki_filp' in this struct.
611 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700612struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700613 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600614 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700615 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700616 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700617 struct io_accept accept;
618 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700619 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700620 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700621 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700622 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700623 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700624 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700625 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700626 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700627 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700628 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300629 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700630 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700631 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300632 /* use only after cleaning per-op data, see io_clean_op() */
633 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700634 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700635
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700636 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700637 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800638 /* polled IO has completed */
639 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700640
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700641 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300642 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700643
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700646 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600647 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700648 u64 user_data;
649
Jens Axboed7718a92020-02-14 22:23:12 -0700650 struct list_head link_list;
651
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300652 /*
653 * 1. used with ctx->iopoll_list with reads/writes
654 * 2. to track reqs with ->files (see io_op_def::file_table)
655 */
Jens Axboefcb323c2019-10-24 12:39:47 -0600656 struct list_head inflight_entry;
657
Xiaoguang Wang05589552020-03-31 14:05:18 +0800658 struct percpu_ref *fixed_file_refs;
659
Jens Axboeb41e9852020-02-17 09:52:41 -0700660 union {
661 /*
662 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700663 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
664 * async armed poll handlers for regular commands. The latter
665 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700666 */
667 struct {
Jens Axboed7718a92020-02-14 22:23:12 -0700668 struct hlist_node hash_node;
669 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700670 };
671 struct io_wq_work work;
672 };
Pavel Begunkov8ef77762020-06-27 14:04:59 +0300673 struct callback_head task_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700674};
675
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300676struct io_defer_entry {
677 struct list_head list;
678 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300679 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300680};
681
Jens Axboedef596e2019-01-09 08:59:42 -0700682#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700683
Jens Axboe013538b2020-06-22 09:29:15 -0600684struct io_comp_state {
685 unsigned int nr;
686 struct list_head list;
687 struct io_ring_ctx *ctx;
688};
689
Jens Axboe9a56a232019-01-09 09:06:50 -0700690struct io_submit_state {
691 struct blk_plug plug;
692
693 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700694 * io_kiocb alloc cache
695 */
696 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300697 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700698
699 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600700 * Batch completion logic
701 */
702 struct io_comp_state comp;
703
704 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700705 * File reference cache
706 */
707 struct file *file;
708 unsigned int fd;
709 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700710 unsigned int ios_left;
711};
712
Jens Axboed3656342019-12-18 09:50:26 -0700713struct io_op_def {
714 /* needs req->io allocated for deferral/async */
715 unsigned async_ctx : 1;
716 /* needs current->mm setup, does mm access */
717 unsigned needs_mm : 1;
718 /* needs req->file assigned */
719 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600720 /* don't fail if file grab fails */
721 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700722 /* hash wq insertion if file is a regular file */
723 unsigned hash_reg_file : 1;
724 /* unbound wq insertion if file is a non-regular file */
725 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700726 /* opcode is not supported by this kernel */
727 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700728 /* needs file table */
729 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700730 /* needs ->fs */
731 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700732 /* set if opcode supports polled "wait" */
733 unsigned pollin : 1;
734 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700735 /* op supports buffer selection */
736 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300737 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700738};
739
740static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300741 [IORING_OP_NOP] = {},
742 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700743 .async_ctx = 1,
744 .needs_mm = 1,
745 .needs_file = 1,
746 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700747 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700748 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700749 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300750 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700751 .async_ctx = 1,
752 .needs_mm = 1,
753 .needs_file = 1,
754 .hash_reg_file = 1,
755 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700756 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300757 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700758 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300759 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700760 .needs_file = 1,
761 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300762 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700763 .needs_file = 1,
764 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700765 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700766 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700768 .needs_file = 1,
769 .hash_reg_file = 1,
770 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700771 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300772 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_file = 1,
776 .unbound_nonreg_file = 1,
777 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300778 [IORING_OP_POLL_REMOVE] = {},
779 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700780 .needs_file = 1,
781 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300782 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700783 .async_ctx = 1,
784 .needs_mm = 1,
785 .needs_file = 1,
786 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700787 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700788 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700789 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300790 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700791 .async_ctx = 1,
792 .needs_mm = 1,
793 .needs_file = 1,
794 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700795 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700796 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700797 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700798 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300799 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700800 .async_ctx = 1,
801 .needs_mm = 1,
802 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300803 [IORING_OP_TIMEOUT_REMOVE] = {},
804 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700805 .needs_mm = 1,
806 .needs_file = 1,
807 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700808 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700809 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700810 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300811 [IORING_OP_ASYNC_CANCEL] = {},
812 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700813 .async_ctx = 1,
814 .needs_mm = 1,
815 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300816 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700817 .async_ctx = 1,
818 .needs_mm = 1,
819 .needs_file = 1,
820 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700821 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700822 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300823 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700824 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300825 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700826 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300827 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700828 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700829 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700830 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300831 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600832 .needs_file = 1,
833 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700834 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700835 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300836 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700837 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700838 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700839 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300840 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700841 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700842 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600843 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700844 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700846 .needs_mm = 1,
847 .needs_file = 1,
848 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700849 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700850 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700851 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300852 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700853 .needs_mm = 1,
854 .needs_file = 1,
855 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700856 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300857 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700858 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300859 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700860 .needs_file = 1,
861 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300862 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700863 .needs_mm = 1,
864 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300865 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700866 .needs_mm = 1,
867 .needs_file = 1,
868 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700869 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700872 .needs_mm = 1,
873 .needs_file = 1,
874 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700875 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700876 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700877 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300878 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700879 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700880 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700881 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700882 [IORING_OP_EPOLL_CTL] = {
883 .unbound_nonreg_file = 1,
884 .file_table = 1,
885 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300886 [IORING_OP_SPLICE] = {
887 .needs_file = 1,
888 .hash_reg_file = 1,
889 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700890 },
891 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700892 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300893 [IORING_OP_TEE] = {
894 .needs_file = 1,
895 .hash_reg_file = 1,
896 .unbound_nonreg_file = 1,
897 },
Jens Axboed3656342019-12-18 09:50:26 -0700898};
899
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700900enum io_mem_account {
901 ACCT_LOCKED,
902 ACCT_PINNED,
903};
904
Pavel Begunkovf3a6fa22020-06-28 12:52:38 +0300905static bool io_rw_reissue(struct io_kiocb *req, long res);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700906static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800907static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600908static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700909static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700910static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
911static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700912static int __io_sqe_files_update(struct io_ring_ctx *ctx,
913 struct io_uring_files_update *ip,
914 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300915static int io_prep_work_files(struct io_kiocb *req);
Jens Axboe2237d762020-06-26 13:44:16 -0600916static void io_complete_rw_common(struct kiocb *kiocb, long res,
917 struct io_comp_state *cs);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300918static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700919static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
920 int fd, struct file **out_file, bool fixed);
921static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600922 const struct io_uring_sqe *sqe,
923 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600924static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600925
Jens Axboeb63534c2020-06-04 11:28:00 -0600926static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
927 struct iovec **iovec, struct iov_iter *iter,
928 bool needs_lock);
929static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
930 struct iovec *iovec, struct iovec *fast_iov,
931 struct iov_iter *iter);
932
Jens Axboe2b188cc2019-01-07 10:46:33 -0700933static struct kmem_cache *req_cachep;
934
935static const struct file_operations io_uring_fops;
936
937struct sock *io_uring_get_socket(struct file *file)
938{
939#if defined(CONFIG_UNIX)
940 if (file->f_op == &io_uring_fops) {
941 struct io_ring_ctx *ctx = file->private_data;
942
943 return ctx->ring_sock->sk;
944 }
945#endif
946 return NULL;
947}
948EXPORT_SYMBOL(io_uring_get_socket);
949
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300950static void io_get_req_task(struct io_kiocb *req)
951{
952 if (req->flags & REQ_F_TASK_PINNED)
953 return;
954 get_task_struct(req->task);
955 req->flags |= REQ_F_TASK_PINNED;
956}
957
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300958static inline void io_clean_op(struct io_kiocb *req)
959{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +0300960 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300961 __io_clean_op(req);
962}
963
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300964/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
965static void __io_put_req_task(struct io_kiocb *req)
966{
967 if (req->flags & REQ_F_TASK_PINNED)
968 put_task_struct(req->task);
969}
970
Jens Axboe4349f302020-07-09 15:07:01 -0600971static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600972{
973 struct mm_struct *mm = current->mm;
974
975 if (mm) {
976 kthread_unuse_mm(mm);
977 mmput(mm);
978 }
979}
980
981static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
982{
983 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300984 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
985 !mmget_not_zero(ctx->sqo_mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600986 return -EFAULT;
987 kthread_use_mm(ctx->sqo_mm);
988 }
989
990 return 0;
991}
992
993static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
994 struct io_kiocb *req)
995{
996 if (!io_op_defs[req->opcode].needs_mm)
997 return 0;
998 return __io_sq_thread_acquire_mm(ctx);
999}
1000
1001static inline void req_set_fail_links(struct io_kiocb *req)
1002{
1003 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1004 req->flags |= REQ_F_FAIL_LINK;
1005}
1006
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001007/*
1008 * Note: must call io_req_init_async() for the first time you
1009 * touch any members of io_wq_work.
1010 */
1011static inline void io_req_init_async(struct io_kiocb *req)
1012{
1013 if (req->flags & REQ_F_WORK_INITIALIZED)
1014 return;
1015
1016 memset(&req->work, 0, sizeof(req->work));
1017 req->flags |= REQ_F_WORK_INITIALIZED;
1018}
1019
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03001020static inline bool io_async_submit(struct io_ring_ctx *ctx)
1021{
1022 return ctx->flags & IORING_SETUP_SQPOLL;
1023}
1024
Jens Axboe2b188cc2019-01-07 10:46:33 -07001025static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1026{
1027 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1028
Jens Axboe0f158b42020-05-14 17:18:39 -06001029 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001030}
1031
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001032static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1033{
1034 return !req->timeout.off;
1035}
1036
Jens Axboe2b188cc2019-01-07 10:46:33 -07001037static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1038{
1039 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001040 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001041
1042 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1043 if (!ctx)
1044 return NULL;
1045
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001046 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1047 if (!ctx->fallback_req)
1048 goto err;
1049
Jens Axboe78076bb2019-12-04 19:56:40 -07001050 /*
1051 * Use 5 bits less than the max cq entries, that should give us around
1052 * 32 entries per hash list if totally full and uniformly spread.
1053 */
1054 hash_bits = ilog2(p->cq_entries);
1055 hash_bits -= 5;
1056 if (hash_bits <= 0)
1057 hash_bits = 1;
1058 ctx->cancel_hash_bits = hash_bits;
1059 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1060 GFP_KERNEL);
1061 if (!ctx->cancel_hash)
1062 goto err;
1063 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1064
Roman Gushchin21482892019-05-07 10:01:48 -07001065 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001066 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1067 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001068
1069 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001070 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001071 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001072 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001073 init_completion(&ctx->ref_comp);
1074 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001075 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001076 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001077 mutex_init(&ctx->uring_lock);
1078 init_waitqueue_head(&ctx->wait);
1079 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001080 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001081 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001082 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001083 init_waitqueue_head(&ctx->inflight_wait);
1084 spin_lock_init(&ctx->inflight_lock);
1085 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001086 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1087 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001088 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001089err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001090 if (ctx->fallback_req)
1091 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001092 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001093 kfree(ctx);
1094 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001095}
1096
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001097static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001098{
Jens Axboe2bc99302020-07-09 09:43:27 -06001099 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1100 struct io_ring_ctx *ctx = req->ctx;
1101
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001102 return seq != ctx->cached_cq_tail
1103 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001104 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001105
Bob Liu9d858b22019-11-13 18:06:25 +08001106 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001107}
1108
Jens Axboede0617e2019-04-06 21:51:27 -06001109static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001110{
Hristo Venev75b28af2019-08-26 17:23:46 +00001111 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001112
Pavel Begunkov07910152020-01-17 03:52:46 +03001113 /* order cqe stores with ring update */
1114 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001115
Pavel Begunkov07910152020-01-17 03:52:46 +03001116 if (wq_has_sleeper(&ctx->cq_wait)) {
1117 wake_up_interruptible(&ctx->cq_wait);
1118 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001119 }
1120}
1121
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001122static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001123{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001124 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1125 return;
1126
Jens Axboecccf0ee2020-01-27 16:34:48 -07001127 if (req->work.mm) {
1128 mmdrop(req->work.mm);
1129 req->work.mm = NULL;
1130 }
1131 if (req->work.creds) {
1132 put_cred(req->work.creds);
1133 req->work.creds = NULL;
1134 }
Jens Axboeff002b32020-02-07 16:05:21 -07001135 if (req->work.fs) {
1136 struct fs_struct *fs = req->work.fs;
1137
1138 spin_lock(&req->work.fs->lock);
1139 if (--fs->users)
1140 fs = NULL;
1141 spin_unlock(&req->work.fs->lock);
1142 if (fs)
1143 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001144 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001145 }
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001146 req->flags &= ~REQ_F_WORK_INITIALIZED;
Jens Axboe561fb042019-10-24 07:25:42 -06001147}
1148
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001149static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001150{
Jens Axboed3656342019-12-18 09:50:26 -07001151 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001152
Pavel Begunkov16d59802020-07-12 16:16:47 +03001153 io_req_init_async(req);
1154
Jens Axboed3656342019-12-18 09:50:26 -07001155 if (req->flags & REQ_F_ISREG) {
1156 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001157 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001158 } else {
1159 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001160 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001161 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001162 if (!req->work.mm && def->needs_mm) {
1163 mmgrab(current->mm);
1164 req->work.mm = current->mm;
1165 }
1166 if (!req->work.creds)
1167 req->work.creds = get_current_cred();
1168 if (!req->work.fs && def->needs_fs) {
1169 spin_lock(&current->fs->lock);
1170 if (!current->fs->in_exec) {
1171 req->work.fs = current->fs;
1172 req->work.fs->users++;
1173 } else {
1174 req->work.flags |= IO_WQ_WORK_CANCEL;
1175 }
1176 spin_unlock(&current->fs->lock);
1177 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001178 if (def->needs_fsize)
1179 req->work.fsize = rlimit(RLIMIT_FSIZE);
1180 else
1181 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001182}
1183
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001184static void io_prep_async_link(struct io_kiocb *req)
1185{
1186 struct io_kiocb *cur;
1187
1188 io_prep_async_work(req);
1189 if (req->flags & REQ_F_LINK_HEAD)
1190 list_for_each_entry(cur, &req->link_list, link_list)
1191 io_prep_async_work(cur);
1192}
1193
1194static void __io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001195{
Jackie Liua197f662019-11-08 08:09:12 -07001196 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001197 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001198
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001199 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1200 &req->work, req->flags);
1201 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001202
1203 if (link)
1204 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001205}
1206
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001207static void io_queue_async_work(struct io_kiocb *req)
1208{
1209 /* init ->work of the whole link before punting */
1210 io_prep_async_link(req);
1211 __io_queue_async_work(req);
1212}
1213
Jens Axboe5262f562019-09-17 12:26:57 -06001214static void io_kill_timeout(struct io_kiocb *req)
1215{
1216 int ret;
1217
Jens Axboe2d283902019-12-04 11:08:05 -07001218 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001219 if (ret != -1) {
1220 atomic_inc(&req->ctx->cq_timeouts);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001221 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001222 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001223 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001224 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001225 }
1226}
1227
1228static void io_kill_timeouts(struct io_ring_ctx *ctx)
1229{
1230 struct io_kiocb *req, *tmp;
1231
1232 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001233 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list)
Jens Axboe5262f562019-09-17 12:26:57 -06001234 io_kill_timeout(req);
1235 spin_unlock_irq(&ctx->completion_lock);
1236}
1237
Pavel Begunkov04518942020-05-26 20:34:05 +03001238static void __io_queue_deferred(struct io_ring_ctx *ctx)
1239{
1240 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001241 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1242 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001243
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001244 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001245 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001246 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001247 /* punt-init is done before queueing for defer */
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001248 __io_queue_async_work(de->req);
1249 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001250 } while (!list_empty(&ctx->defer_list));
1251}
1252
Pavel Begunkov360428f2020-05-30 14:54:17 +03001253static void io_flush_timeouts(struct io_ring_ctx *ctx)
1254{
1255 while (!list_empty(&ctx->timeout_list)) {
1256 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001257 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001258
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001259 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001260 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001261 if (req->timeout.target_seq != ctx->cached_cq_tail
1262 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001263 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001264
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001265 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001266 io_kill_timeout(req);
1267 }
1268}
1269
Jens Axboede0617e2019-04-06 21:51:27 -06001270static void io_commit_cqring(struct io_ring_ctx *ctx)
1271{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001272 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001273 __io_commit_cqring(ctx);
1274
Pavel Begunkov04518942020-05-26 20:34:05 +03001275 if (unlikely(!list_empty(&ctx->defer_list)))
1276 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001277}
1278
Jens Axboe2b188cc2019-01-07 10:46:33 -07001279static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1280{
Hristo Venev75b28af2019-08-26 17:23:46 +00001281 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001282 unsigned tail;
1283
1284 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001285 /*
1286 * writes to the cq entry need to come after reading head; the
1287 * control dependency is enough as we're using WRITE_ONCE to
1288 * fill the cq entry
1289 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001290 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001291 return NULL;
1292
1293 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001294 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001295}
1296
Jens Axboef2842ab2020-01-08 11:04:00 -07001297static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1298{
Jens Axboef0b493e2020-02-01 21:30:11 -07001299 if (!ctx->cq_ev_fd)
1300 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001301 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1302 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001303 if (!ctx->eventfd_async)
1304 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001305 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001306}
1307
Jens Axboeb41e9852020-02-17 09:52:41 -07001308static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001309{
1310 if (waitqueue_active(&ctx->wait))
1311 wake_up(&ctx->wait);
1312 if (waitqueue_active(&ctx->sqo_wait))
1313 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001314 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001315 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001316}
1317
Jens Axboec4a2ed72019-11-21 21:01:26 -07001318/* Returns true if there are no backlogged entries after the flush */
1319static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001320{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001321 struct io_rings *rings = ctx->rings;
1322 struct io_uring_cqe *cqe;
1323 struct io_kiocb *req;
1324 unsigned long flags;
1325 LIST_HEAD(list);
1326
1327 if (!force) {
1328 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001329 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001330 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1331 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001332 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001333 }
1334
1335 spin_lock_irqsave(&ctx->completion_lock, flags);
1336
1337 /* if force is set, the ring is going away. always drop after that */
1338 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001339 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001340
Jens Axboec4a2ed72019-11-21 21:01:26 -07001341 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001342 while (!list_empty(&ctx->cq_overflow_list)) {
1343 cqe = io_get_cqring(ctx);
1344 if (!cqe && !force)
1345 break;
1346
1347 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001348 compl.list);
1349 list_move(&req->compl.list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001350 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001351 if (cqe) {
1352 WRITE_ONCE(cqe->user_data, req->user_data);
1353 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001354 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001355 } else {
1356 WRITE_ONCE(ctx->rings->cq_overflow,
1357 atomic_inc_return(&ctx->cached_cq_overflow));
1358 }
1359 }
1360
1361 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001362 if (cqe) {
1363 clear_bit(0, &ctx->sq_check_overflow);
1364 clear_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001365 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001366 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001367 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1368 io_cqring_ev_posted(ctx);
1369
1370 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001371 req = list_first_entry(&list, struct io_kiocb, compl.list);
1372 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001373 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001374 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001375
1376 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001377}
1378
Jens Axboebcda7ba2020-02-23 16:42:51 -07001379static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001380{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001381 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001382 struct io_uring_cqe *cqe;
1383
Jens Axboe78e19bb2019-11-06 15:21:34 -07001384 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001385
Jens Axboe2b188cc2019-01-07 10:46:33 -07001386 /*
1387 * If we can't get a cq entry, userspace overflowed the
1388 * submission (by quite a lot). Increment the overflow count in
1389 * the ring.
1390 */
1391 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001392 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001393 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001394 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001395 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001396 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001397 WRITE_ONCE(ctx->rings->cq_overflow,
1398 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001399 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001400 if (list_empty(&ctx->cq_overflow_list)) {
1401 set_bit(0, &ctx->sq_check_overflow);
1402 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001403 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001404 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001405 io_clean_op(req);
Jens Axboe2ca10252020-02-13 17:17:35 -07001406 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001407 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001408 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001409 refcount_inc(&req->refs);
1410 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001411 }
1412}
1413
Jens Axboebcda7ba2020-02-23 16:42:51 -07001414static void io_cqring_fill_event(struct io_kiocb *req, long res)
1415{
1416 __io_cqring_fill_event(req, res, 0);
1417}
1418
Jens Axboee1e16092020-06-22 09:17:17 -06001419static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001421 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422 unsigned long flags;
1423
1424 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001425 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001426 io_commit_cqring(ctx);
1427 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1428
Jens Axboe8c838782019-03-12 15:48:16 -06001429 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001430}
1431
Jens Axboe229a7b62020-06-22 10:13:11 -06001432static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001433{
Jens Axboe229a7b62020-06-22 10:13:11 -06001434 struct io_ring_ctx *ctx = cs->ctx;
1435
1436 spin_lock_irq(&ctx->completion_lock);
1437 while (!list_empty(&cs->list)) {
1438 struct io_kiocb *req;
1439
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001440 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1441 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001442 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001443 if (!(req->flags & REQ_F_LINK_HEAD)) {
1444 req->flags |= REQ_F_COMP_LOCKED;
1445 io_put_req(req);
1446 } else {
1447 spin_unlock_irq(&ctx->completion_lock);
1448 io_put_req(req);
1449 spin_lock_irq(&ctx->completion_lock);
1450 }
1451 }
1452 io_commit_cqring(ctx);
1453 spin_unlock_irq(&ctx->completion_lock);
1454
1455 io_cqring_ev_posted(ctx);
1456 cs->nr = 0;
1457}
1458
1459static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1460 struct io_comp_state *cs)
1461{
1462 if (!cs) {
1463 io_cqring_add_event(req, res, cflags);
1464 io_put_req(req);
1465 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001466 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001467 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001468 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001469 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001470 if (++cs->nr >= 32)
1471 io_submit_flush_completions(cs);
1472 }
Jens Axboee1e16092020-06-22 09:17:17 -06001473}
1474
1475static void io_req_complete(struct io_kiocb *req, long res)
1476{
Jens Axboe229a7b62020-06-22 10:13:11 -06001477 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001478}
1479
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001480static inline bool io_is_fallback_req(struct io_kiocb *req)
1481{
1482 return req == (struct io_kiocb *)
1483 ((unsigned long) req->ctx->fallback_req & ~1UL);
1484}
1485
1486static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1487{
1488 struct io_kiocb *req;
1489
1490 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001491 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001492 return req;
1493
1494 return NULL;
1495}
1496
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001497static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1498 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001499{
Jens Axboefd6fab22019-03-14 16:30:06 -06001500 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501 struct io_kiocb *req;
1502
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001503 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001504 size_t sz;
1505 int ret;
1506
1507 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001508 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1509
1510 /*
1511 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1512 * retry single alloc to be on the safe side.
1513 */
1514 if (unlikely(ret <= 0)) {
1515 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1516 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001517 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001518 ret = 1;
1519 }
Jens Axboe2579f912019-01-09 09:10:43 -07001520 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001521 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001522 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001523 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001524 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001525 }
1526
Jens Axboe2579f912019-01-09 09:10:43 -07001527 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001528fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001529 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001530}
1531
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001532static inline void io_put_file(struct io_kiocb *req, struct file *file,
1533 bool fixed)
1534{
1535 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001536 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001537 else
1538 fput(file);
1539}
1540
Pavel Begunkove6543a82020-06-28 12:52:30 +03001541static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001542{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001543 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001544
Jens Axboe5acbbc82020-07-08 15:15:26 -06001545 if (req->io)
1546 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001547 if (req->file)
1548 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001549 io_req_clean_work(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001550
Jens Axboefcb323c2019-10-24 12:39:47 -06001551 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001552 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001553 unsigned long flags;
1554
1555 spin_lock_irqsave(&ctx->inflight_lock, flags);
1556 list_del(&req->inflight_entry);
1557 if (waitqueue_active(&ctx->inflight_wait))
1558 wake_up(&ctx->inflight_wait);
1559 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1560 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001561}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001562
Pavel Begunkove6543a82020-06-28 12:52:30 +03001563static void __io_free_req(struct io_kiocb *req)
1564{
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001565 struct io_ring_ctx *ctx;
1566
Pavel Begunkove6543a82020-06-28 12:52:30 +03001567 io_dismantle_req(req);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001568 __io_put_req_task(req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001569 ctx = req->ctx;
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001570 if (likely(!io_is_fallback_req(req)))
1571 kmem_cache_free(req_cachep, req);
1572 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001573 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1574 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001575}
1576
Jackie Liua197f662019-11-08 08:09:12 -07001577static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001578{
Jackie Liua197f662019-11-08 08:09:12 -07001579 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001580 int ret;
1581
Jens Axboe2d283902019-12-04 11:08:05 -07001582 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001583 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001584 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001585 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001586 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001587 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001588 return true;
1589 }
1590
1591 return false;
1592}
1593
Jens Axboeab0b6452020-06-30 08:43:15 -06001594static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001595{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001596 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001597 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001598
1599 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001600 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001601 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1602 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001603 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001604
1605 list_del_init(&link->link_list);
1606 wake_ev = io_link_cancel_timeout(link);
1607 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001608 return wake_ev;
1609}
1610
1611static void io_kill_linked_timeout(struct io_kiocb *req)
1612{
1613 struct io_ring_ctx *ctx = req->ctx;
1614 bool wake_ev;
1615
1616 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1617 unsigned long flags;
1618
1619 spin_lock_irqsave(&ctx->completion_lock, flags);
1620 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001621 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001622 } else {
1623 wake_ev = __io_kill_linked_timeout(req);
1624 }
1625
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001626 if (wake_ev)
1627 io_cqring_ev_posted(ctx);
1628}
1629
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001630static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001631{
1632 struct io_kiocb *nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001633
1634 /*
1635 * The list should never be empty when we are called here. But could
1636 * potentially happen if the chain is messed up, check to be on the
1637 * safe side.
1638 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001639 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001640 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001641
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001642 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1643 list_del_init(&req->link_list);
1644 if (!list_empty(&nxt->link_list))
1645 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001646 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001647}
1648
1649/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001650 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001651 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001652static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001653{
Jens Axboe2665abf2019-11-05 12:40:47 -07001654 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001655
1656 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001657 struct io_kiocb *link = list_first_entry(&req->link_list,
1658 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001659
Pavel Begunkov44932332019-12-05 16:16:35 +03001660 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001661 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001662
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001663 io_cqring_fill_event(link, -ECANCELED);
1664 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001665 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001666 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001667
1668 io_commit_cqring(ctx);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001669 io_cqring_ev_posted(ctx);
1670}
1671
1672static void io_fail_links(struct io_kiocb *req)
1673{
1674 struct io_ring_ctx *ctx = req->ctx;
1675
1676 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1677 unsigned long flags;
1678
1679 spin_lock_irqsave(&ctx->completion_lock, flags);
1680 __io_fail_links(req);
1681 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1682 } else {
1683 __io_fail_links(req);
1684 }
1685
Jens Axboe2665abf2019-11-05 12:40:47 -07001686 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001687}
1688
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001689static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001690{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001691 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001692 if (req->flags & REQ_F_LINK_TIMEOUT)
1693 io_kill_linked_timeout(req);
1694
Jens Axboe9e645e112019-05-10 16:07:28 -06001695 /*
1696 * If LINK is set, we have dependent requests in this chain. If we
1697 * didn't fail this request, queue the first one up, moving any other
1698 * dependencies to the next request. In case of failure, fail the rest
1699 * of the chain.
1700 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001701 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1702 return io_req_link_next(req);
1703 io_fail_links(req);
1704 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001705}
Jens Axboe9e645e112019-05-10 16:07:28 -06001706
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001707static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1708{
1709 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1710 return NULL;
1711 return __io_req_find_next(req);
1712}
1713
Jens Axboec2c4c832020-07-01 15:37:11 -06001714static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb)
1715{
1716 struct task_struct *tsk = req->task;
1717 struct io_ring_ctx *ctx = req->ctx;
1718 int ret, notify = TWA_RESUME;
1719
1720 /*
1721 * SQPOLL kernel thread doesn't need notification, just a wakeup.
1722 * If we're not using an eventfd, then TWA_RESUME is always fine,
1723 * as we won't have dependencies between request completions for
1724 * other kernel wait conditions.
1725 */
1726 if (ctx->flags & IORING_SETUP_SQPOLL)
1727 notify = 0;
1728 else if (ctx->cq_ev_fd)
1729 notify = TWA_SIGNAL;
1730
1731 ret = task_work_add(tsk, cb, notify);
1732 if (!ret)
1733 wake_up_process(tsk);
1734 return ret;
1735}
1736
Jens Axboec40f6372020-06-25 15:39:59 -06001737static void __io_req_task_cancel(struct io_kiocb *req, int error)
1738{
1739 struct io_ring_ctx *ctx = req->ctx;
1740
1741 spin_lock_irq(&ctx->completion_lock);
1742 io_cqring_fill_event(req, error);
1743 io_commit_cqring(ctx);
1744 spin_unlock_irq(&ctx->completion_lock);
1745
1746 io_cqring_ev_posted(ctx);
1747 req_set_fail_links(req);
1748 io_double_put_req(req);
1749}
1750
1751static void io_req_task_cancel(struct callback_head *cb)
1752{
1753 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1754
1755 __io_req_task_cancel(req, -ECANCELED);
1756}
1757
1758static void __io_req_task_submit(struct io_kiocb *req)
1759{
1760 struct io_ring_ctx *ctx = req->ctx;
1761
Jens Axboec40f6372020-06-25 15:39:59 -06001762 if (!__io_sq_thread_acquire_mm(ctx)) {
1763 mutex_lock(&ctx->uring_lock);
1764 __io_queue_sqe(req, NULL, NULL);
1765 mutex_unlock(&ctx->uring_lock);
1766 } else {
1767 __io_req_task_cancel(req, -EFAULT);
1768 }
1769}
1770
1771static void io_req_task_submit(struct callback_head *cb)
1772{
1773 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1774
1775 __io_req_task_submit(req);
1776}
1777
1778static void io_req_task_queue(struct io_kiocb *req)
1779{
Jens Axboec40f6372020-06-25 15:39:59 -06001780 int ret;
1781
1782 init_task_work(&req->task_work, io_req_task_submit);
1783
Jens Axboec2c4c832020-07-01 15:37:11 -06001784 ret = io_req_task_work_add(req, &req->task_work);
Jens Axboec40f6372020-06-25 15:39:59 -06001785 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001786 struct task_struct *tsk;
1787
Jens Axboec40f6372020-06-25 15:39:59 -06001788 init_task_work(&req->task_work, io_req_task_cancel);
1789 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001790 task_work_add(tsk, &req->task_work, 0);
1791 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001792 }
Jens Axboec40f6372020-06-25 15:39:59 -06001793}
1794
Pavel Begunkovc3524382020-06-28 12:52:32 +03001795static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001796{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001797 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001798
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001799 if (nxt)
1800 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001801}
1802
Pavel Begunkovc3524382020-06-28 12:52:32 +03001803static void io_free_req(struct io_kiocb *req)
1804{
1805 io_queue_next(req);
1806 __io_free_req(req);
1807}
1808
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001809struct req_batch {
1810 void *reqs[IO_IOPOLL_BATCH];
1811 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001812
1813 struct task_struct *task;
1814 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001815};
1816
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001817static inline void io_init_req_batch(struct req_batch *rb)
1818{
1819 rb->to_free = 0;
1820 rb->task_refs = 0;
1821 rb->task = NULL;
1822}
1823
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001824static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1825 struct req_batch *rb)
1826{
1827 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1828 percpu_ref_put_many(&ctx->refs, rb->to_free);
1829 rb->to_free = 0;
1830}
1831
1832static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1833 struct req_batch *rb)
1834{
1835 if (rb->to_free)
1836 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001837 if (rb->task) {
1838 put_task_struct_many(rb->task, rb->task_refs);
1839 rb->task = NULL;
1840 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001841}
1842
1843static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1844{
1845 if (unlikely(io_is_fallback_req(req))) {
1846 io_free_req(req);
1847 return;
1848 }
1849 if (req->flags & REQ_F_LINK_HEAD)
1850 io_queue_next(req);
1851
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001852 if (req->flags & REQ_F_TASK_PINNED) {
1853 if (req->task != rb->task) {
1854 if (rb->task)
1855 put_task_struct_many(rb->task, rb->task_refs);
1856 rb->task = req->task;
1857 rb->task_refs = 0;
1858 }
1859 rb->task_refs++;
1860 req->flags &= ~REQ_F_TASK_PINNED;
1861 }
1862
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001863 io_dismantle_req(req);
1864 rb->reqs[rb->to_free++] = req;
1865 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1866 __io_req_free_batch_flush(req->ctx, rb);
1867}
1868
Jens Axboeba816ad2019-09-28 11:36:45 -06001869/*
1870 * Drop reference to request, return next in chain (if there is one) if this
1871 * was the last reference to this request.
1872 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001873static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001874{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001875 struct io_kiocb *nxt = NULL;
1876
Jens Axboe2a44f462020-02-25 13:25:41 -07001877 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001878 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001879 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001880 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001881 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001882}
1883
Jens Axboe2b188cc2019-01-07 10:46:33 -07001884static void io_put_req(struct io_kiocb *req)
1885{
Jens Axboedef596e2019-01-09 08:59:42 -07001886 if (refcount_dec_and_test(&req->refs))
1887 io_free_req(req);
1888}
1889
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001890static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001891{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001892 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001893
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001894 /*
1895 * A ref is owned by io-wq in which context we're. So, if that's the
1896 * last one, it's safe to steal next work. False negatives are Ok,
1897 * it just will be re-punted async in io_put_work()
1898 */
1899 if (refcount_read(&req->refs) != 1)
1900 return NULL;
1901
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001902 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001903 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001904}
1905
Jens Axboe978db572019-11-14 22:39:04 -07001906/*
1907 * Must only be used if we don't need to care about links, usually from
1908 * within the completion handling itself.
1909 */
1910static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001911{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001912 /* drop both submit and complete references */
1913 if (refcount_sub_and_test(2, &req->refs))
1914 __io_free_req(req);
1915}
1916
Jens Axboe978db572019-11-14 22:39:04 -07001917static void io_double_put_req(struct io_kiocb *req)
1918{
1919 /* drop both submit and complete references */
1920 if (refcount_sub_and_test(2, &req->refs))
1921 io_free_req(req);
1922}
1923
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001924static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001925{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001926 struct io_rings *rings = ctx->rings;
1927
Jens Axboead3eb2c2019-12-18 17:12:20 -07001928 if (test_bit(0, &ctx->cq_check_overflow)) {
1929 /*
1930 * noflush == true is from the waitqueue handler, just ensure
1931 * we wake up the task, and the next invocation will flush the
1932 * entries. We cannot safely to it from here.
1933 */
1934 if (noflush && !list_empty(&ctx->cq_overflow_list))
1935 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001936
Jens Axboead3eb2c2019-12-18 17:12:20 -07001937 io_cqring_overflow_flush(ctx, false);
1938 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001939
Jens Axboea3a0e432019-08-20 11:03:11 -06001940 /* See comment at the top of this file */
1941 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001942 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001943}
1944
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001945static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1946{
1947 struct io_rings *rings = ctx->rings;
1948
1949 /* make sure SQ entry isn't read before tail */
1950 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1951}
1952
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03001953static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001954{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03001955 unsigned int cflags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001956
1957 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1958 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03001959 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001960 kfree(kbuf);
1961 return cflags;
1962}
1963
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03001964static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
1965{
1966 struct io_buffer *kbuf;
1967
1968 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
1969 return io_put_kbuf(req, kbuf);
1970}
1971
Jens Axboe4c6e2772020-07-01 11:29:10 -06001972static inline bool io_run_task_work(void)
1973{
1974 if (current->task_works) {
1975 __set_current_state(TASK_RUNNING);
1976 task_work_run();
1977 return true;
1978 }
1979
1980 return false;
1981}
1982
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001983static void io_iopoll_queue(struct list_head *again)
1984{
1985 struct io_kiocb *req;
1986
1987 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03001988 req = list_first_entry(again, struct io_kiocb, inflight_entry);
1989 list_del(&req->inflight_entry);
Pavel Begunkovcf2f5422020-06-30 15:20:40 +03001990 if (!io_rw_reissue(req, -EAGAIN))
Jens Axboe2237d762020-06-26 13:44:16 -06001991 io_complete_rw_common(&req->rw.kiocb, -EAGAIN, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001992 } while (!list_empty(again));
1993}
1994
Jens Axboedef596e2019-01-09 08:59:42 -07001995/*
1996 * Find and free completed poll iocbs
1997 */
1998static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1999 struct list_head *done)
2000{
Jens Axboe8237e042019-12-28 10:48:22 -07002001 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002002 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002003 LIST_HEAD(again);
2004
2005 /* order with ->result store in io_complete_rw_iopoll() */
2006 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002007
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002008 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002009 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002010 int cflags = 0;
2011
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002012 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002013 if (READ_ONCE(req->result) == -EAGAIN) {
2014 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002015 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002016 continue;
2017 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002018 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002019
Jens Axboebcda7ba2020-02-23 16:42:51 -07002020 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002021 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002022
2023 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002024 (*nr_events)++;
2025
Pavel Begunkovc3524382020-06-28 12:52:32 +03002026 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002027 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002028 }
Jens Axboedef596e2019-01-09 08:59:42 -07002029
Jens Axboe09bb8392019-03-13 12:39:28 -06002030 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002031 if (ctx->flags & IORING_SETUP_SQPOLL)
2032 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002033 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002034
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002035 if (!list_empty(&again))
2036 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002037}
2038
Jens Axboedef596e2019-01-09 08:59:42 -07002039static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2040 long min)
2041{
2042 struct io_kiocb *req, *tmp;
2043 LIST_HEAD(done);
2044 bool spin;
2045 int ret;
2046
2047 /*
2048 * Only spin for completions if we don't have multiple devices hanging
2049 * off our complete list, and we're under the requested amount.
2050 */
2051 spin = !ctx->poll_multi_file && *nr_events < min;
2052
2053 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002054 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002055 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002056
2057 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002058 * Move completed and retryable entries to our local lists.
2059 * If we find a request that requires polling, break out
2060 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002061 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002062 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002063 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002064 continue;
2065 }
2066 if (!list_empty(&done))
2067 break;
2068
2069 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2070 if (ret < 0)
2071 break;
2072
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002073 /* iopoll may have completed current req */
2074 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002075 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002076
Jens Axboedef596e2019-01-09 08:59:42 -07002077 if (ret && spin)
2078 spin = false;
2079 ret = 0;
2080 }
2081
2082 if (!list_empty(&done))
2083 io_iopoll_complete(ctx, nr_events, &done);
2084
2085 return ret;
2086}
2087
2088/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002089 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002090 * non-spinning poll check - we'll still enter the driver poll loop, but only
2091 * as a non-spinning completion check.
2092 */
2093static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2094 long min)
2095{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002096 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002097 int ret;
2098
2099 ret = io_do_iopoll(ctx, nr_events, min);
2100 if (ret < 0)
2101 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002102 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002103 return 0;
2104 }
2105
2106 return 1;
2107}
2108
2109/*
2110 * We can't just wait for polled events to come to us, we have to actively
2111 * find and complete them.
2112 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002113static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002114{
2115 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2116 return;
2117
2118 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002119 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002120 unsigned int nr_events = 0;
2121
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002122 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002123
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002124 /* let it sleep and repeat later if can't complete a request */
2125 if (nr_events == 0)
2126 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002127 /*
2128 * Ensure we allow local-to-the-cpu processing to take place,
2129 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002130 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002131 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002132 if (need_resched()) {
2133 mutex_unlock(&ctx->uring_lock);
2134 cond_resched();
2135 mutex_lock(&ctx->uring_lock);
2136 }
Jens Axboedef596e2019-01-09 08:59:42 -07002137 }
2138 mutex_unlock(&ctx->uring_lock);
2139}
2140
Pavel Begunkov7668b922020-07-07 16:36:21 +03002141static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002142{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002143 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002144 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002145
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002146 /*
2147 * We disallow the app entering submit/complete with polling, but we
2148 * still need to lock the ring to prevent racing with polled issue
2149 * that got punted to a workqueue.
2150 */
2151 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002152 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002153 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002154 * Don't enter poll loop if we already have events pending.
2155 * If we do, we can potentially be spinning for commands that
2156 * already triggered a CQE (eg in error).
2157 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002158 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002159 break;
2160
2161 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002162 * If a submit got punted to a workqueue, we can have the
2163 * application entering polling for a command before it gets
2164 * issued. That app will hold the uring_lock for the duration
2165 * of the poll right here, so we need to take a breather every
2166 * now and then to ensure that the issue has a chance to add
2167 * the poll to the issued list. Otherwise we can spin here
2168 * forever, while the workqueue is stuck trying to acquire the
2169 * very same mutex.
2170 */
2171 if (!(++iters & 7)) {
2172 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002173 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002174 mutex_lock(&ctx->uring_lock);
2175 }
2176
Pavel Begunkov7668b922020-07-07 16:36:21 +03002177 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002178 if (ret <= 0)
2179 break;
2180 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002181 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002182
Jens Axboe500f9fb2019-08-19 12:15:59 -06002183 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002184 return ret;
2185}
2186
Jens Axboe491381ce2019-10-17 09:20:46 -06002187static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002188{
Jens Axboe491381ce2019-10-17 09:20:46 -06002189 /*
2190 * Tell lockdep we inherited freeze protection from submission
2191 * thread.
2192 */
2193 if (req->flags & REQ_F_ISREG) {
2194 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002195
Jens Axboe491381ce2019-10-17 09:20:46 -06002196 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002197 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002198 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002199}
2200
Jens Axboea1d7c392020-06-22 11:09:46 -06002201static void io_complete_rw_common(struct kiocb *kiocb, long res,
2202 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002203{
Jens Axboe9adbd452019-12-20 08:45:55 -07002204 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002205 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002206
Jens Axboe491381ce2019-10-17 09:20:46 -06002207 if (kiocb->ki_flags & IOCB_WRITE)
2208 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002209
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002210 if (res != req->result)
2211 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002212 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002213 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002214 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002215}
2216
Jens Axboeb63534c2020-06-04 11:28:00 -06002217#ifdef CONFIG_BLOCK
2218static bool io_resubmit_prep(struct io_kiocb *req, int error)
2219{
2220 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2221 ssize_t ret = -ECANCELED;
2222 struct iov_iter iter;
2223 int rw;
2224
2225 if (error) {
2226 ret = error;
2227 goto end_req;
2228 }
2229
2230 switch (req->opcode) {
2231 case IORING_OP_READV:
2232 case IORING_OP_READ_FIXED:
2233 case IORING_OP_READ:
2234 rw = READ;
2235 break;
2236 case IORING_OP_WRITEV:
2237 case IORING_OP_WRITE_FIXED:
2238 case IORING_OP_WRITE:
2239 rw = WRITE;
2240 break;
2241 default:
2242 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2243 req->opcode);
2244 goto end_req;
2245 }
2246
2247 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2248 if (ret < 0)
2249 goto end_req;
2250 ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2251 if (!ret)
2252 return true;
2253 kfree(iovec);
2254end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002255 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002256 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002257 return false;
2258}
2259
2260static void io_rw_resubmit(struct callback_head *cb)
2261{
2262 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2263 struct io_ring_ctx *ctx = req->ctx;
2264 int err;
2265
Jens Axboeb63534c2020-06-04 11:28:00 -06002266 err = io_sq_thread_acquire_mm(ctx, req);
2267
2268 if (io_resubmit_prep(req, err)) {
2269 refcount_inc(&req->refs);
2270 io_queue_async_work(req);
2271 }
2272}
2273#endif
2274
2275static bool io_rw_reissue(struct io_kiocb *req, long res)
2276{
2277#ifdef CONFIG_BLOCK
Jens Axboeb63534c2020-06-04 11:28:00 -06002278 int ret;
2279
2280 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2281 return false;
2282
Jens Axboeb63534c2020-06-04 11:28:00 -06002283 init_task_work(&req->task_work, io_rw_resubmit);
Jens Axboec2c4c832020-07-01 15:37:11 -06002284 ret = io_req_task_work_add(req, &req->task_work);
2285 if (!ret)
Jens Axboeb63534c2020-06-04 11:28:00 -06002286 return true;
2287#endif
2288 return false;
2289}
2290
Jens Axboea1d7c392020-06-22 11:09:46 -06002291static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2292 struct io_comp_state *cs)
2293{
2294 if (!io_rw_reissue(req, res))
2295 io_complete_rw_common(&req->rw.kiocb, res, cs);
2296}
2297
Jens Axboeba816ad2019-09-28 11:36:45 -06002298static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2299{
Jens Axboe9adbd452019-12-20 08:45:55 -07002300 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002301
Jens Axboea1d7c392020-06-22 11:09:46 -06002302 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002303}
2304
Jens Axboedef596e2019-01-09 08:59:42 -07002305static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2306{
Jens Axboe9adbd452019-12-20 08:45:55 -07002307 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002308
Jens Axboe491381ce2019-10-17 09:20:46 -06002309 if (kiocb->ki_flags & IOCB_WRITE)
2310 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002311
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002312 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002313 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002314
2315 WRITE_ONCE(req->result, res);
2316 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002317 smp_wmb();
2318 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002319}
2320
2321/*
2322 * After the iocb has been issued, it's safe to be found on the poll list.
2323 * Adding the kiocb to the list AFTER submission ensures that we don't
2324 * find it from a io_iopoll_getevents() thread before the issuer is done
2325 * accessing the kiocb cookie.
2326 */
2327static void io_iopoll_req_issued(struct io_kiocb *req)
2328{
2329 struct io_ring_ctx *ctx = req->ctx;
2330
2331 /*
2332 * Track whether we have multiple files in our lists. This will impact
2333 * how we do polling eventually, not spinning if we're on potentially
2334 * different devices.
2335 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002336 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002337 ctx->poll_multi_file = false;
2338 } else if (!ctx->poll_multi_file) {
2339 struct io_kiocb *list_req;
2340
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002341 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002342 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002343 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002344 ctx->poll_multi_file = true;
2345 }
2346
2347 /*
2348 * For fast devices, IO may have already completed. If it has, add
2349 * it to the front so we find it first.
2350 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002351 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002352 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002353 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002354 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002355
2356 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2357 wq_has_sleeper(&ctx->sqo_wait))
2358 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002359}
2360
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002361static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002362{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002363 if (state->has_refs)
2364 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002365 state->file = NULL;
2366}
2367
2368static inline void io_state_file_put(struct io_submit_state *state)
2369{
2370 if (state->file)
2371 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002372}
2373
2374/*
2375 * Get as many references to a file as we have IOs left in this submission,
2376 * assuming most submissions are for one file, or at least that each file
2377 * has more than one submission.
2378 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002379static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002380{
2381 if (!state)
2382 return fget(fd);
2383
2384 if (state->file) {
2385 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002386 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002387 state->ios_left--;
2388 return state->file;
2389 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002390 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002391 }
2392 state->file = fget_many(fd, state->ios_left);
2393 if (!state->file)
2394 return NULL;
2395
2396 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002397 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002398 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002399 return state->file;
2400}
2401
Jens Axboe4503b762020-06-01 10:00:27 -06002402static bool io_bdev_nowait(struct block_device *bdev)
2403{
2404#ifdef CONFIG_BLOCK
2405 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2406#else
2407 return true;
2408#endif
2409}
2410
Jens Axboe2b188cc2019-01-07 10:46:33 -07002411/*
2412 * If we tracked the file through the SCM inflight mechanism, we could support
2413 * any file. For now, just ensure that anything potentially problematic is done
2414 * inline.
2415 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002416static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002417{
2418 umode_t mode = file_inode(file)->i_mode;
2419
Jens Axboe4503b762020-06-01 10:00:27 -06002420 if (S_ISBLK(mode)) {
2421 if (io_bdev_nowait(file->f_inode->i_bdev))
2422 return true;
2423 return false;
2424 }
2425 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002426 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002427 if (S_ISREG(mode)) {
2428 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2429 file->f_op != &io_uring_fops)
2430 return true;
2431 return false;
2432 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002433
Jens Axboec5b85622020-06-09 19:23:05 -06002434 /* any ->read/write should understand O_NONBLOCK */
2435 if (file->f_flags & O_NONBLOCK)
2436 return true;
2437
Jens Axboeaf197f52020-04-28 13:15:06 -06002438 if (!(file->f_mode & FMODE_NOWAIT))
2439 return false;
2440
2441 if (rw == READ)
2442 return file->f_op->read_iter != NULL;
2443
2444 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002445}
2446
Jens Axboe3529d8c2019-12-19 18:24:38 -07002447static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2448 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002449{
Jens Axboedef596e2019-01-09 08:59:42 -07002450 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002451 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002452 unsigned ioprio;
2453 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002454
Jens Axboe491381ce2019-10-17 09:20:46 -06002455 if (S_ISREG(file_inode(req->file)->i_mode))
2456 req->flags |= REQ_F_ISREG;
2457
Jens Axboe2b188cc2019-01-07 10:46:33 -07002458 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002459 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2460 req->flags |= REQ_F_CUR_POS;
2461 kiocb->ki_pos = req->file->f_pos;
2462 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002463 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002464 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2465 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2466 if (unlikely(ret))
2467 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002468
2469 ioprio = READ_ONCE(sqe->ioprio);
2470 if (ioprio) {
2471 ret = ioprio_check_cap(ioprio);
2472 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002473 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002474
2475 kiocb->ki_ioprio = ioprio;
2476 } else
2477 kiocb->ki_ioprio = get_current_ioprio();
2478
Stefan Bühler8449eed2019-04-27 20:34:19 +02002479 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002480 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002481 req->flags |= REQ_F_NOWAIT;
2482
Jens Axboeb63534c2020-06-04 11:28:00 -06002483 if (kiocb->ki_flags & IOCB_DIRECT)
2484 io_get_req_task(req);
2485
Stefan Bühler8449eed2019-04-27 20:34:19 +02002486 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002487 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002488
Jens Axboedef596e2019-01-09 08:59:42 -07002489 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002490 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2491 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002492 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002493
Jens Axboedef596e2019-01-09 08:59:42 -07002494 kiocb->ki_flags |= IOCB_HIPRI;
2495 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002496 req->iopoll_completed = 0;
Pavel Begunkovf3a6fa22020-06-28 12:52:38 +03002497 io_get_req_task(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002498 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002499 if (kiocb->ki_flags & IOCB_HIPRI)
2500 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002501 kiocb->ki_complete = io_complete_rw;
2502 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002503
Jens Axboe3529d8c2019-12-19 18:24:38 -07002504 req->rw.addr = READ_ONCE(sqe->addr);
2505 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002506 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002507 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002508}
2509
2510static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2511{
2512 switch (ret) {
2513 case -EIOCBQUEUED:
2514 break;
2515 case -ERESTARTSYS:
2516 case -ERESTARTNOINTR:
2517 case -ERESTARTNOHAND:
2518 case -ERESTART_RESTARTBLOCK:
2519 /*
2520 * We can't just restart the syscall, since previously
2521 * submitted sqes may already be in progress. Just fail this
2522 * IO with EINTR.
2523 */
2524 ret = -EINTR;
2525 /* fall through */
2526 default:
2527 kiocb->ki_complete(kiocb, ret, 0);
2528 }
2529}
2530
Jens Axboea1d7c392020-06-22 11:09:46 -06002531static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2532 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002533{
Jens Axboeba042912019-12-25 16:33:42 -07002534 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2535
2536 if (req->flags & REQ_F_CUR_POS)
2537 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002538 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002539 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002540 else
2541 io_rw_done(kiocb, ret);
2542}
2543
Jens Axboe9adbd452019-12-20 08:45:55 -07002544static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002545 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002546{
Jens Axboe9adbd452019-12-20 08:45:55 -07002547 struct io_ring_ctx *ctx = req->ctx;
2548 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002549 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002550 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002551 size_t offset;
2552 u64 buf_addr;
2553
2554 /* attempt to use fixed buffers without having provided iovecs */
2555 if (unlikely(!ctx->user_bufs))
2556 return -EFAULT;
2557
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002558 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002559 if (unlikely(buf_index >= ctx->nr_user_bufs))
2560 return -EFAULT;
2561
2562 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2563 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002564 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002565
2566 /* overflow */
2567 if (buf_addr + len < buf_addr)
2568 return -EFAULT;
2569 /* not inside the mapped region */
2570 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2571 return -EFAULT;
2572
2573 /*
2574 * May not be a start of buffer, set size appropriately
2575 * and advance us to the beginning.
2576 */
2577 offset = buf_addr - imu->ubuf;
2578 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002579
2580 if (offset) {
2581 /*
2582 * Don't use iov_iter_advance() here, as it's really slow for
2583 * using the latter parts of a big fixed buffer - it iterates
2584 * over each segment manually. We can cheat a bit here, because
2585 * we know that:
2586 *
2587 * 1) it's a BVEC iter, we set it up
2588 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2589 * first and last bvec
2590 *
2591 * So just find our index, and adjust the iterator afterwards.
2592 * If the offset is within the first bvec (or the whole first
2593 * bvec, just use iov_iter_advance(). This makes it easier
2594 * since we can just skip the first segment, which may not
2595 * be PAGE_SIZE aligned.
2596 */
2597 const struct bio_vec *bvec = imu->bvec;
2598
2599 if (offset <= bvec->bv_len) {
2600 iov_iter_advance(iter, offset);
2601 } else {
2602 unsigned long seg_skip;
2603
2604 /* skip first vec */
2605 offset -= bvec->bv_len;
2606 seg_skip = 1 + (offset >> PAGE_SHIFT);
2607
2608 iter->bvec = bvec + seg_skip;
2609 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002610 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002611 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002612 }
2613 }
2614
Jens Axboe5e559562019-11-13 16:12:46 -07002615 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002616}
2617
Jens Axboebcda7ba2020-02-23 16:42:51 -07002618static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2619{
2620 if (needs_lock)
2621 mutex_unlock(&ctx->uring_lock);
2622}
2623
2624static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2625{
2626 /*
2627 * "Normal" inline submissions always hold the uring_lock, since we
2628 * grab it from the system call. Same is true for the SQPOLL offload.
2629 * The only exception is when we've detached the request and issue it
2630 * from an async worker thread, grab the lock for that case.
2631 */
2632 if (needs_lock)
2633 mutex_lock(&ctx->uring_lock);
2634}
2635
2636static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2637 int bgid, struct io_buffer *kbuf,
2638 bool needs_lock)
2639{
2640 struct io_buffer *head;
2641
2642 if (req->flags & REQ_F_BUFFER_SELECTED)
2643 return kbuf;
2644
2645 io_ring_submit_lock(req->ctx, needs_lock);
2646
2647 lockdep_assert_held(&req->ctx->uring_lock);
2648
2649 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2650 if (head) {
2651 if (!list_empty(&head->list)) {
2652 kbuf = list_last_entry(&head->list, struct io_buffer,
2653 list);
2654 list_del(&kbuf->list);
2655 } else {
2656 kbuf = head;
2657 idr_remove(&req->ctx->io_buffer_idr, bgid);
2658 }
2659 if (*len > kbuf->len)
2660 *len = kbuf->len;
2661 } else {
2662 kbuf = ERR_PTR(-ENOBUFS);
2663 }
2664
2665 io_ring_submit_unlock(req->ctx, needs_lock);
2666
2667 return kbuf;
2668}
2669
Jens Axboe4d954c22020-02-27 07:31:19 -07002670static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2671 bool needs_lock)
2672{
2673 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002674 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002675
2676 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002677 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002678 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2679 if (IS_ERR(kbuf))
2680 return kbuf;
2681 req->rw.addr = (u64) (unsigned long) kbuf;
2682 req->flags |= REQ_F_BUFFER_SELECTED;
2683 return u64_to_user_ptr(kbuf->addr);
2684}
2685
2686#ifdef CONFIG_COMPAT
2687static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2688 bool needs_lock)
2689{
2690 struct compat_iovec __user *uiov;
2691 compat_ssize_t clen;
2692 void __user *buf;
2693 ssize_t len;
2694
2695 uiov = u64_to_user_ptr(req->rw.addr);
2696 if (!access_ok(uiov, sizeof(*uiov)))
2697 return -EFAULT;
2698 if (__get_user(clen, &uiov->iov_len))
2699 return -EFAULT;
2700 if (clen < 0)
2701 return -EINVAL;
2702
2703 len = clen;
2704 buf = io_rw_buffer_select(req, &len, needs_lock);
2705 if (IS_ERR(buf))
2706 return PTR_ERR(buf);
2707 iov[0].iov_base = buf;
2708 iov[0].iov_len = (compat_size_t) len;
2709 return 0;
2710}
2711#endif
2712
2713static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2714 bool needs_lock)
2715{
2716 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2717 void __user *buf;
2718 ssize_t len;
2719
2720 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2721 return -EFAULT;
2722
2723 len = iov[0].iov_len;
2724 if (len < 0)
2725 return -EINVAL;
2726 buf = io_rw_buffer_select(req, &len, needs_lock);
2727 if (IS_ERR(buf))
2728 return PTR_ERR(buf);
2729 iov[0].iov_base = buf;
2730 iov[0].iov_len = len;
2731 return 0;
2732}
2733
2734static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2735 bool needs_lock)
2736{
Jens Axboedddb3e22020-06-04 11:27:01 -06002737 if (req->flags & REQ_F_BUFFER_SELECTED) {
2738 struct io_buffer *kbuf;
2739
2740 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2741 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2742 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002743 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002744 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002745 if (!req->rw.len)
2746 return 0;
2747 else if (req->rw.len > 1)
2748 return -EINVAL;
2749
2750#ifdef CONFIG_COMPAT
2751 if (req->ctx->compat)
2752 return io_compat_import(req, iov, needs_lock);
2753#endif
2754
2755 return __io_iov_buffer_select(req, iov, needs_lock);
2756}
2757
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002758static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002759 struct iovec **iovec, struct iov_iter *iter,
2760 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002761{
Jens Axboe9adbd452019-12-20 08:45:55 -07002762 void __user *buf = u64_to_user_ptr(req->rw.addr);
2763 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002764 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002765 u8 opcode;
2766
Jens Axboed625c6e2019-12-17 19:53:05 -07002767 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002768 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002769 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002770 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002771 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002772
Jens Axboebcda7ba2020-02-23 16:42:51 -07002773 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002774 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002775 return -EINVAL;
2776
Jens Axboe3a6820f2019-12-22 15:19:35 -07002777 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002778 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002779 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2780 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002781 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002782 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002783 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002784 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002785 }
2786
Jens Axboe3a6820f2019-12-22 15:19:35 -07002787 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2788 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002789 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002790 }
2791
Jens Axboef67676d2019-12-02 11:03:47 -07002792 if (req->io) {
2793 struct io_async_rw *iorw = &req->io->rw;
2794
Pavel Begunkov252917c2020-07-13 22:59:20 +03002795 iov_iter_init(iter, rw, iorw->iov, iorw->nr_segs, iorw->size);
2796 *iovec = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07002797 return iorw->size;
2798 }
2799
Jens Axboe4d954c22020-02-27 07:31:19 -07002800 if (req->flags & REQ_F_BUFFER_SELECT) {
2801 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002802 if (!ret) {
2803 ret = (*iovec)->iov_len;
2804 iov_iter_init(iter, rw, *iovec, 1, ret);
2805 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002806 *iovec = NULL;
2807 return ret;
2808 }
2809
Jens Axboe2b188cc2019-01-07 10:46:33 -07002810#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002811 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002812 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2813 iovec, iter);
2814#endif
2815
2816 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2817}
2818
Jens Axboe32960612019-09-23 11:05:34 -06002819/*
2820 * For files that don't have ->read_iter() and ->write_iter(), handle them
2821 * by looping over ->read() or ->write() manually.
2822 */
2823static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2824 struct iov_iter *iter)
2825{
2826 ssize_t ret = 0;
2827
2828 /*
2829 * Don't support polled IO through this interface, and we can't
2830 * support non-blocking either. For the latter, this just causes
2831 * the kiocb to be handled from an async context.
2832 */
2833 if (kiocb->ki_flags & IOCB_HIPRI)
2834 return -EOPNOTSUPP;
2835 if (kiocb->ki_flags & IOCB_NOWAIT)
2836 return -EAGAIN;
2837
2838 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002839 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002840 ssize_t nr;
2841
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002842 if (!iov_iter_is_bvec(iter)) {
2843 iovec = iov_iter_iovec(iter);
2844 } else {
2845 /* fixed buffers import bvec */
2846 iovec.iov_base = kmap(iter->bvec->bv_page)
2847 + iter->iov_offset;
2848 iovec.iov_len = min(iter->count,
2849 iter->bvec->bv_len - iter->iov_offset);
2850 }
2851
Jens Axboe32960612019-09-23 11:05:34 -06002852 if (rw == READ) {
2853 nr = file->f_op->read(file, iovec.iov_base,
2854 iovec.iov_len, &kiocb->ki_pos);
2855 } else {
2856 nr = file->f_op->write(file, iovec.iov_base,
2857 iovec.iov_len, &kiocb->ki_pos);
2858 }
2859
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002860 if (iov_iter_is_bvec(iter))
2861 kunmap(iter->bvec->bv_page);
2862
Jens Axboe32960612019-09-23 11:05:34 -06002863 if (nr < 0) {
2864 if (!ret)
2865 ret = nr;
2866 break;
2867 }
2868 ret += nr;
2869 if (nr != iovec.iov_len)
2870 break;
2871 iov_iter_advance(iter, nr);
2872 }
2873
2874 return ret;
2875}
2876
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002877static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002878 struct iovec *iovec, struct iovec *fast_iov,
2879 struct iov_iter *iter)
2880{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002881 struct io_async_rw *rw = &req->io->rw;
2882
2883 rw->nr_segs = iter->nr_segs;
2884 rw->size = io_size;
2885 if (!iovec) {
2886 rw->iov = rw->fast_iov;
2887 if (rw->iov != fast_iov)
2888 memcpy(rw->iov, fast_iov,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002889 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002890 } else {
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002891 rw->iov = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002892 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002893 }
2894}
2895
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002896static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2897{
2898 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2899 return req->io == NULL;
2900}
2901
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002902static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002903{
Jens Axboed3656342019-12-18 09:50:26 -07002904 if (!io_op_defs[req->opcode].async_ctx)
2905 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002906
2907 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002908}
2909
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002910static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2911 struct iovec *iovec, struct iovec *fast_iov,
2912 struct iov_iter *iter)
2913{
Jens Axboe980ad262020-01-24 23:08:54 -07002914 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002915 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002916 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002917 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002918 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002919
Jens Axboe5d204bc2020-01-31 12:06:52 -07002920 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2921 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002922 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002923}
2924
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03002925static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
2926 bool force_nonblock)
2927{
2928 struct io_async_ctx *io = req->io;
2929 struct iov_iter iter;
2930 ssize_t ret;
2931
2932 io->rw.iov = io->rw.fast_iov;
2933 req->io = NULL;
2934 ret = io_import_iovec(rw, req, &io->rw.iov, &iter, !force_nonblock);
2935 req->io = io;
2936 if (unlikely(ret < 0))
2937 return ret;
2938
2939 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2940 return 0;
2941}
2942
Jens Axboe3529d8c2019-12-19 18:24:38 -07002943static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2944 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002945{
2946 ssize_t ret;
2947
Jens Axboe3529d8c2019-12-19 18:24:38 -07002948 ret = io_prep_rw(req, sqe, force_nonblock);
2949 if (ret)
2950 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002951
Jens Axboe3529d8c2019-12-19 18:24:38 -07002952 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2953 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002954
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002955 /* either don't need iovec imported or already have it */
2956 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002957 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03002958 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07002959}
2960
Jens Axboebcf5a062020-05-22 09:24:42 -06002961static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
2962 int sync, void *arg)
2963{
2964 struct wait_page_queue *wpq;
2965 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06002966 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06002967 int ret;
2968
2969 wpq = container_of(wait, struct wait_page_queue, wait);
2970
2971 ret = wake_page_match(wpq, key);
2972 if (ret != 1)
2973 return ret;
2974
2975 list_del_init(&wait->entry);
2976
Pavel Begunkove7375122020-07-12 20:42:04 +03002977 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboebcf5a062020-05-22 09:24:42 -06002978 /* submit ref gets dropped, acquire a new one */
2979 refcount_inc(&req->refs);
Pavel Begunkove7375122020-07-12 20:42:04 +03002980 ret = io_req_task_work_add(req, &req->task_work);
Jens Axboebcf5a062020-05-22 09:24:42 -06002981 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06002982 struct task_struct *tsk;
2983
Jens Axboebcf5a062020-05-22 09:24:42 -06002984 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03002985 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06002986 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03002987 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06002988 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06002989 }
Jens Axboebcf5a062020-05-22 09:24:42 -06002990 return 1;
2991}
2992
2993static bool io_rw_should_retry(struct io_kiocb *req)
2994{
2995 struct kiocb *kiocb = &req->rw.kiocb;
2996 int ret;
2997
2998 /* never retry for NOWAIT, we just complete with -EAGAIN */
2999 if (req->flags & REQ_F_NOWAIT)
3000 return false;
3001
3002 /* already tried, or we're doing O_DIRECT */
3003 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ))
3004 return false;
3005 /*
3006 * just use poll if we can, and don't attempt if the fs doesn't
3007 * support callback based unlocks
3008 */
3009 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3010 return false;
3011
3012 /*
3013 * If request type doesn't require req->io to defer in general,
3014 * we need to allocate it here
3015 */
3016 if (!req->io && __io_alloc_async_ctx(req))
3017 return false;
3018
3019 ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq,
3020 io_async_buf_func, req);
3021 if (!ret) {
3022 io_get_req_task(req);
3023 return true;
3024 }
3025
3026 return false;
3027}
3028
3029static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3030{
3031 if (req->file->f_op->read_iter)
3032 return call_read_iter(req->file, &req->rw.kiocb, iter);
3033 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3034}
3035
Jens Axboea1d7c392020-06-22 11:09:46 -06003036static int io_read(struct io_kiocb *req, bool force_nonblock,
3037 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003038{
3039 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003040 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003041 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003042 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003043 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003044
Jens Axboebcda7ba2020-02-23 16:42:51 -07003045 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003046 if (ret < 0)
3047 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003048
Jens Axboefd6c2e42019-12-18 12:19:41 -07003049 /* Ensure we clear previously set non-block flag */
3050 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003051 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003052
Jens Axboef67676d2019-12-02 11:03:47 -07003053 io_size = ret;
Pavel Begunkov6795c5a2020-06-28 12:52:35 +03003054 req->result = io_size;
Jens Axboef67676d2019-12-02 11:03:47 -07003055
Pavel Begunkov24c74672020-06-21 13:09:51 +03003056 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003057 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07003058 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003059
Jens Axboe31b51512019-01-18 22:56:34 -07003060 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003061 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003062 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003063 unsigned long nr_segs = iter.nr_segs;
Jens Axboe4503b762020-06-01 10:00:27 -06003064 ssize_t ret2 = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003065
Jens Axboebcf5a062020-05-22 09:24:42 -06003066 ret2 = io_iter_do_read(req, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003067
Jens Axboe9d93a3f2019-05-15 13:53:07 -06003068 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe4503b762020-06-01 10:00:27 -06003069 if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003070 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003071 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003072 iter.count = iov_count;
3073 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003074copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003075 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003076 inline_vecs, &iter);
3077 if (ret)
3078 goto out_free;
Pavel Begunkov252917c2020-07-13 22:59:20 +03003079 /* it's copied and will be cleaned with ->io */
3080 iovec = NULL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003081 /* if we can retry, do so with the callbacks armed */
3082 if (io_rw_should_retry(req)) {
3083 ret2 = io_iter_do_read(req, &iter);
3084 if (ret2 == -EIOCBQUEUED) {
3085 goto out_free;
3086 } else if (ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003087 kiocb_done(kiocb, ret2, cs);
Jens Axboebcf5a062020-05-22 09:24:42 -06003088 goto out_free;
3089 }
3090 }
3091 kiocb->ki_flags &= ~IOCB_WAITQ;
Jens Axboef67676d2019-12-02 11:03:47 -07003092 return -EAGAIN;
3093 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003094 }
Jens Axboef67676d2019-12-02 11:03:47 -07003095out_free:
Pavel Begunkov252917c2020-07-13 22:59:20 +03003096 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003097 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003098 return ret;
3099}
3100
Jens Axboe3529d8c2019-12-19 18:24:38 -07003101static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3102 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003103{
3104 ssize_t ret;
3105
Jens Axboe3529d8c2019-12-19 18:24:38 -07003106 ret = io_prep_rw(req, sqe, force_nonblock);
3107 if (ret)
3108 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003109
Jens Axboe3529d8c2019-12-19 18:24:38 -07003110 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3111 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003112
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003113 /* either don't need iovec imported or already have it */
3114 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003115 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003116 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003117}
3118
Jens Axboea1d7c392020-06-22 11:09:46 -06003119static int io_write(struct io_kiocb *req, bool force_nonblock,
3120 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003121{
3122 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003123 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003124 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003125 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07003126 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003127
Jens Axboebcda7ba2020-02-23 16:42:51 -07003128 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003129 if (ret < 0)
3130 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003131
Jens Axboefd6c2e42019-12-18 12:19:41 -07003132 /* Ensure we clear previously set non-block flag */
3133 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003134 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003135
Jens Axboef67676d2019-12-02 11:03:47 -07003136 io_size = ret;
Pavel Begunkov6795c5a2020-06-28 12:52:35 +03003137 req->result = io_size;
Jens Axboef67676d2019-12-02 11:03:47 -07003138
Pavel Begunkov24c74672020-06-21 13:09:51 +03003139 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003140 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003141 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003142
Jens Axboe10d59342019-12-09 20:16:22 -07003143 /* file path doesn't support NOWAIT for non-direct_IO */
3144 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3145 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003146 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003147
Jens Axboe31b51512019-01-18 22:56:34 -07003148 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07003149 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003150 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06003151 unsigned long nr_segs = iter.nr_segs;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003152 ssize_t ret2;
3153
Jens Axboe2b188cc2019-01-07 10:46:33 -07003154 /*
3155 * Open-code file_start_write here to grab freeze protection,
3156 * which will be released by another thread in
3157 * io_complete_rw(). Fool lockdep by telling it the lock got
3158 * released so that it doesn't complain about the held lock when
3159 * we return to userspace.
3160 */
Jens Axboe491381ce2019-10-17 09:20:46 -06003161 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07003162 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003163 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07003164 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003165 SB_FREEZE_WRITE);
3166 }
3167 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003168
Jens Axboe9adbd452019-12-20 08:45:55 -07003169 if (req->file->f_op->write_iter)
3170 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06003171 else
Jens Axboe9adbd452019-12-20 08:45:55 -07003172 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003173
Jens Axboefaac9962020-02-07 15:45:22 -07003174 /*
Chucheng Luobff60352020-03-25 11:31:38 +08003175 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07003176 * retry them without IOCB_NOWAIT.
3177 */
3178 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3179 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07003180 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboea1d7c392020-06-22 11:09:46 -06003181 kiocb_done(kiocb, ret2, cs);
Jens Axboef67676d2019-12-02 11:03:47 -07003182 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06003183 iter.count = iov_count;
3184 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07003185copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003186 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07003187 inline_vecs, &iter);
3188 if (ret)
3189 goto out_free;
Pavel Begunkov252917c2020-07-13 22:59:20 +03003190 /* it's copied and will be cleaned with ->io */
3191 iovec = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003192 return -EAGAIN;
3193 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003194 }
Jens Axboe31b51512019-01-18 22:56:34 -07003195out_free:
Pavel Begunkov252917c2020-07-13 22:59:20 +03003196 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003197 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003198 return ret;
3199}
3200
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003201static int __io_splice_prep(struct io_kiocb *req,
3202 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003203{
3204 struct io_splice* sp = &req->splice;
3205 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3206 int ret;
3207
3208 if (req->flags & REQ_F_NEED_CLEANUP)
3209 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003210 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3211 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003212
3213 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003214 sp->len = READ_ONCE(sqe->len);
3215 sp->flags = READ_ONCE(sqe->splice_flags);
3216
3217 if (unlikely(sp->flags & ~valid_flags))
3218 return -EINVAL;
3219
3220 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3221 (sp->flags & SPLICE_F_FD_IN_FIXED));
3222 if (ret)
3223 return ret;
3224 req->flags |= REQ_F_NEED_CLEANUP;
3225
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003226 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3227 /*
3228 * Splice operation will be punted aync, and here need to
3229 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3230 */
3231 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003232 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003233 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003234
3235 return 0;
3236}
3237
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003238static int io_tee_prep(struct io_kiocb *req,
3239 const struct io_uring_sqe *sqe)
3240{
3241 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3242 return -EINVAL;
3243 return __io_splice_prep(req, sqe);
3244}
3245
3246static int io_tee(struct io_kiocb *req, bool force_nonblock)
3247{
3248 struct io_splice *sp = &req->splice;
3249 struct file *in = sp->file_in;
3250 struct file *out = sp->file_out;
3251 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3252 long ret = 0;
3253
3254 if (force_nonblock)
3255 return -EAGAIN;
3256 if (sp->len)
3257 ret = do_tee(in, out, sp->len, flags);
3258
3259 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3260 req->flags &= ~REQ_F_NEED_CLEANUP;
3261
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003262 if (ret != sp->len)
3263 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003264 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003265 return 0;
3266}
3267
3268static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3269{
3270 struct io_splice* sp = &req->splice;
3271
3272 sp->off_in = READ_ONCE(sqe->splice_off_in);
3273 sp->off_out = READ_ONCE(sqe->off);
3274 return __io_splice_prep(req, sqe);
3275}
3276
Pavel Begunkov014db002020-03-03 21:33:12 +03003277static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003278{
3279 struct io_splice *sp = &req->splice;
3280 struct file *in = sp->file_in;
3281 struct file *out = sp->file_out;
3282 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3283 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003284 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003285
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003286 if (force_nonblock)
3287 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003288
3289 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3290 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003291
Jens Axboe948a7742020-05-17 14:21:38 -06003292 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003293 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003294
3295 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3296 req->flags &= ~REQ_F_NEED_CLEANUP;
3297
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003298 if (ret != sp->len)
3299 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003300 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003301 return 0;
3302}
3303
Jens Axboe2b188cc2019-01-07 10:46:33 -07003304/*
3305 * IORING_OP_NOP just posts a completion event, nothing else.
3306 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003307static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003308{
3309 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003310
Jens Axboedef596e2019-01-09 08:59:42 -07003311 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3312 return -EINVAL;
3313
Jens Axboe229a7b62020-06-22 10:13:11 -06003314 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003315 return 0;
3316}
3317
Jens Axboe3529d8c2019-12-19 18:24:38 -07003318static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003319{
Jens Axboe6b063142019-01-10 22:13:58 -07003320 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003321
Jens Axboe09bb8392019-03-13 12:39:28 -06003322 if (!req->file)
3323 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003324
Jens Axboe6b063142019-01-10 22:13:58 -07003325 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003326 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003327 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003328 return -EINVAL;
3329
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003330 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3331 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3332 return -EINVAL;
3333
3334 req->sync.off = READ_ONCE(sqe->off);
3335 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003336 return 0;
3337}
3338
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003339static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003340{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003341 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003342 int ret;
3343
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003344 /* fsync always requires a blocking context */
3345 if (force_nonblock)
3346 return -EAGAIN;
3347
Jens Axboe9adbd452019-12-20 08:45:55 -07003348 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003349 end > 0 ? end : LLONG_MAX,
3350 req->sync.flags & IORING_FSYNC_DATASYNC);
3351 if (ret < 0)
3352 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003353 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003354 return 0;
3355}
3356
Jens Axboed63d1b52019-12-10 10:38:56 -07003357static int io_fallocate_prep(struct io_kiocb *req,
3358 const struct io_uring_sqe *sqe)
3359{
3360 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3361 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003362 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3363 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003364
3365 req->sync.off = READ_ONCE(sqe->off);
3366 req->sync.len = READ_ONCE(sqe->addr);
3367 req->sync.mode = READ_ONCE(sqe->len);
3368 return 0;
3369}
3370
Pavel Begunkov014db002020-03-03 21:33:12 +03003371static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003372{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003373 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003374
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003375 /* fallocate always requiring blocking context */
3376 if (force_nonblock)
3377 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003378 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3379 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003380 if (ret < 0)
3381 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003382 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003383 return 0;
3384}
3385
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003386static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003387{
Jens Axboef8748882020-01-08 17:47:02 -07003388 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003389 int ret;
3390
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003391 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003392 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003393 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003394 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003395 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003396 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003397
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003398 /* open.how should be already initialised */
3399 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003400 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003401
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003402 req->open.dfd = READ_ONCE(sqe->fd);
3403 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003404 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003405 if (IS_ERR(req->open.filename)) {
3406 ret = PTR_ERR(req->open.filename);
3407 req->open.filename = NULL;
3408 return ret;
3409 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003410 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003411 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003412 return 0;
3413}
3414
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003415static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3416{
3417 u64 flags, mode;
3418
3419 if (req->flags & REQ_F_NEED_CLEANUP)
3420 return 0;
3421 mode = READ_ONCE(sqe->len);
3422 flags = READ_ONCE(sqe->open_flags);
3423 req->open.how = build_open_how(flags, mode);
3424 return __io_openat_prep(req, sqe);
3425}
3426
Jens Axboecebdb982020-01-08 17:59:24 -07003427static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3428{
3429 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003430 size_t len;
3431 int ret;
3432
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003433 if (req->flags & REQ_F_NEED_CLEANUP)
3434 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003435 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3436 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003437 if (len < OPEN_HOW_SIZE_VER0)
3438 return -EINVAL;
3439
3440 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3441 len);
3442 if (ret)
3443 return ret;
3444
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003445 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003446}
3447
Pavel Begunkov014db002020-03-03 21:33:12 +03003448static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003449{
3450 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003451 struct file *file;
3452 int ret;
3453
Jens Axboef86cd202020-01-29 13:46:44 -07003454 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003455 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003456
Jens Axboecebdb982020-01-08 17:59:24 -07003457 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003458 if (ret)
3459 goto err;
3460
Jens Axboe4022e7a2020-03-19 19:23:18 -06003461 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003462 if (ret < 0)
3463 goto err;
3464
3465 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3466 if (IS_ERR(file)) {
3467 put_unused_fd(ret);
3468 ret = PTR_ERR(file);
3469 } else {
3470 fsnotify_open(file);
3471 fd_install(ret, file);
3472 }
3473err:
3474 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003475 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003476 if (ret < 0)
3477 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003478 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003479 return 0;
3480}
3481
Pavel Begunkov014db002020-03-03 21:33:12 +03003482static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003483{
Pavel Begunkov014db002020-03-03 21:33:12 +03003484 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003485}
3486
Jens Axboe067524e2020-03-02 16:32:28 -07003487static int io_remove_buffers_prep(struct io_kiocb *req,
3488 const struct io_uring_sqe *sqe)
3489{
3490 struct io_provide_buf *p = &req->pbuf;
3491 u64 tmp;
3492
3493 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3494 return -EINVAL;
3495
3496 tmp = READ_ONCE(sqe->fd);
3497 if (!tmp || tmp > USHRT_MAX)
3498 return -EINVAL;
3499
3500 memset(p, 0, sizeof(*p));
3501 p->nbufs = tmp;
3502 p->bgid = READ_ONCE(sqe->buf_group);
3503 return 0;
3504}
3505
3506static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3507 int bgid, unsigned nbufs)
3508{
3509 unsigned i = 0;
3510
3511 /* shouldn't happen */
3512 if (!nbufs)
3513 return 0;
3514
3515 /* the head kbuf is the list itself */
3516 while (!list_empty(&buf->list)) {
3517 struct io_buffer *nxt;
3518
3519 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3520 list_del(&nxt->list);
3521 kfree(nxt);
3522 if (++i == nbufs)
3523 return i;
3524 }
3525 i++;
3526 kfree(buf);
3527 idr_remove(&ctx->io_buffer_idr, bgid);
3528
3529 return i;
3530}
3531
Jens Axboe229a7b62020-06-22 10:13:11 -06003532static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3533 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003534{
3535 struct io_provide_buf *p = &req->pbuf;
3536 struct io_ring_ctx *ctx = req->ctx;
3537 struct io_buffer *head;
3538 int ret = 0;
3539
3540 io_ring_submit_lock(ctx, !force_nonblock);
3541
3542 lockdep_assert_held(&ctx->uring_lock);
3543
3544 ret = -ENOENT;
3545 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3546 if (head)
3547 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3548
3549 io_ring_submit_lock(ctx, !force_nonblock);
3550 if (ret < 0)
3551 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003552 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003553 return 0;
3554}
3555
Jens Axboeddf0322d2020-02-23 16:41:33 -07003556static int io_provide_buffers_prep(struct io_kiocb *req,
3557 const struct io_uring_sqe *sqe)
3558{
3559 struct io_provide_buf *p = &req->pbuf;
3560 u64 tmp;
3561
3562 if (sqe->ioprio || sqe->rw_flags)
3563 return -EINVAL;
3564
3565 tmp = READ_ONCE(sqe->fd);
3566 if (!tmp || tmp > USHRT_MAX)
3567 return -E2BIG;
3568 p->nbufs = tmp;
3569 p->addr = READ_ONCE(sqe->addr);
3570 p->len = READ_ONCE(sqe->len);
3571
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003572 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003573 return -EFAULT;
3574
3575 p->bgid = READ_ONCE(sqe->buf_group);
3576 tmp = READ_ONCE(sqe->off);
3577 if (tmp > USHRT_MAX)
3578 return -E2BIG;
3579 p->bid = tmp;
3580 return 0;
3581}
3582
3583static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3584{
3585 struct io_buffer *buf;
3586 u64 addr = pbuf->addr;
3587 int i, bid = pbuf->bid;
3588
3589 for (i = 0; i < pbuf->nbufs; i++) {
3590 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3591 if (!buf)
3592 break;
3593
3594 buf->addr = addr;
3595 buf->len = pbuf->len;
3596 buf->bid = bid;
3597 addr += pbuf->len;
3598 bid++;
3599 if (!*head) {
3600 INIT_LIST_HEAD(&buf->list);
3601 *head = buf;
3602 } else {
3603 list_add_tail(&buf->list, &(*head)->list);
3604 }
3605 }
3606
3607 return i ? i : -ENOMEM;
3608}
3609
Jens Axboe229a7b62020-06-22 10:13:11 -06003610static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3611 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003612{
3613 struct io_provide_buf *p = &req->pbuf;
3614 struct io_ring_ctx *ctx = req->ctx;
3615 struct io_buffer *head, *list;
3616 int ret = 0;
3617
3618 io_ring_submit_lock(ctx, !force_nonblock);
3619
3620 lockdep_assert_held(&ctx->uring_lock);
3621
3622 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3623
3624 ret = io_add_buffers(p, &head);
3625 if (ret < 0)
3626 goto out;
3627
3628 if (!list) {
3629 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3630 GFP_KERNEL);
3631 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003632 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003633 goto out;
3634 }
3635 }
3636out:
3637 io_ring_submit_unlock(ctx, !force_nonblock);
3638 if (ret < 0)
3639 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003640 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003641 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003642}
3643
Jens Axboe3e4827b2020-01-08 15:18:09 -07003644static int io_epoll_ctl_prep(struct io_kiocb *req,
3645 const struct io_uring_sqe *sqe)
3646{
3647#if defined(CONFIG_EPOLL)
3648 if (sqe->ioprio || sqe->buf_index)
3649 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003650 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3651 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003652
3653 req->epoll.epfd = READ_ONCE(sqe->fd);
3654 req->epoll.op = READ_ONCE(sqe->len);
3655 req->epoll.fd = READ_ONCE(sqe->off);
3656
3657 if (ep_op_has_event(req->epoll.op)) {
3658 struct epoll_event __user *ev;
3659
3660 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3661 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3662 return -EFAULT;
3663 }
3664
3665 return 0;
3666#else
3667 return -EOPNOTSUPP;
3668#endif
3669}
3670
Jens Axboe229a7b62020-06-22 10:13:11 -06003671static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3672 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003673{
3674#if defined(CONFIG_EPOLL)
3675 struct io_epoll *ie = &req->epoll;
3676 int ret;
3677
3678 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3679 if (force_nonblock && ret == -EAGAIN)
3680 return -EAGAIN;
3681
3682 if (ret < 0)
3683 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003684 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003685 return 0;
3686#else
3687 return -EOPNOTSUPP;
3688#endif
3689}
3690
Jens Axboec1ca7572019-12-25 22:18:28 -07003691static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3692{
3693#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3694 if (sqe->ioprio || sqe->buf_index || sqe->off)
3695 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003696 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3697 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003698
3699 req->madvise.addr = READ_ONCE(sqe->addr);
3700 req->madvise.len = READ_ONCE(sqe->len);
3701 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3702 return 0;
3703#else
3704 return -EOPNOTSUPP;
3705#endif
3706}
3707
Pavel Begunkov014db002020-03-03 21:33:12 +03003708static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003709{
3710#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3711 struct io_madvise *ma = &req->madvise;
3712 int ret;
3713
3714 if (force_nonblock)
3715 return -EAGAIN;
3716
3717 ret = do_madvise(ma->addr, ma->len, ma->advice);
3718 if (ret < 0)
3719 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003720 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003721 return 0;
3722#else
3723 return -EOPNOTSUPP;
3724#endif
3725}
3726
Jens Axboe4840e412019-12-25 22:03:45 -07003727static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3728{
3729 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3730 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003731 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3732 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003733
3734 req->fadvise.offset = READ_ONCE(sqe->off);
3735 req->fadvise.len = READ_ONCE(sqe->len);
3736 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3737 return 0;
3738}
3739
Pavel Begunkov014db002020-03-03 21:33:12 +03003740static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003741{
3742 struct io_fadvise *fa = &req->fadvise;
3743 int ret;
3744
Jens Axboe3e694262020-02-01 09:22:49 -07003745 if (force_nonblock) {
3746 switch (fa->advice) {
3747 case POSIX_FADV_NORMAL:
3748 case POSIX_FADV_RANDOM:
3749 case POSIX_FADV_SEQUENTIAL:
3750 break;
3751 default:
3752 return -EAGAIN;
3753 }
3754 }
Jens Axboe4840e412019-12-25 22:03:45 -07003755
3756 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3757 if (ret < 0)
3758 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003759 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003760 return 0;
3761}
3762
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003763static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3764{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003765 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3766 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003767 if (sqe->ioprio || sqe->buf_index)
3768 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003769 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003770 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003771
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003772 req->statx.dfd = READ_ONCE(sqe->fd);
3773 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003774 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003775 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3776 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003777
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003778 return 0;
3779}
3780
Pavel Begunkov014db002020-03-03 21:33:12 +03003781static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003782{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003783 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003784 int ret;
3785
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003786 if (force_nonblock) {
3787 /* only need file table for an actual valid fd */
3788 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3789 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003790 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003791 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003792
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003793 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3794 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003795
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003796 if (ret < 0)
3797 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003798 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003799 return 0;
3800}
3801
Jens Axboeb5dba592019-12-11 14:02:38 -07003802static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3803{
3804 /*
3805 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003806 * leave the 'file' in an undeterminate state, and here need to modify
3807 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003808 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003809 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003810 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3811
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003812 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3813 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003814 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3815 sqe->rw_flags || sqe->buf_index)
3816 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003817 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003818 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003819
3820 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003821 if ((req->file && req->file->f_op == &io_uring_fops) ||
3822 req->close.fd == req->ctx->ring_fd)
3823 return -EBADF;
3824
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003825 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003826 return 0;
3827}
3828
Jens Axboe229a7b62020-06-22 10:13:11 -06003829static int io_close(struct io_kiocb *req, bool force_nonblock,
3830 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003831{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003832 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003833 int ret;
3834
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003835 /* might be already done during nonblock submission */
3836 if (!close->put_file) {
3837 ret = __close_fd_get_file(close->fd, &close->put_file);
3838 if (ret < 0)
3839 return (ret == -ENOENT) ? -EBADF : ret;
3840 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003841
3842 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003843 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003844 /* was never set, but play safe */
3845 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003846 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003847 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003848 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003849 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003850
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003851 /* No ->flush() or already async, safely close from here */
3852 ret = filp_close(close->put_file, req->work.files);
3853 if (ret < 0)
3854 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003855 fput(close->put_file);
3856 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06003857 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07003858 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003859}
3860
Jens Axboe3529d8c2019-12-19 18:24:38 -07003861static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003862{
3863 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003864
3865 if (!req->file)
3866 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003867
3868 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3869 return -EINVAL;
3870 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3871 return -EINVAL;
3872
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003873 req->sync.off = READ_ONCE(sqe->off);
3874 req->sync.len = READ_ONCE(sqe->len);
3875 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003876 return 0;
3877}
3878
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003879static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003880{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003881 int ret;
3882
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003883 /* sync_file_range always requires a blocking context */
3884 if (force_nonblock)
3885 return -EAGAIN;
3886
Jens Axboe9adbd452019-12-20 08:45:55 -07003887 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003888 req->sync.flags);
3889 if (ret < 0)
3890 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003891 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003892 return 0;
3893}
3894
YueHaibing469956e2020-03-04 15:53:52 +08003895#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003896static int io_setup_async_msg(struct io_kiocb *req,
3897 struct io_async_msghdr *kmsg)
3898{
3899 if (req->io)
3900 return -EAGAIN;
3901 if (io_alloc_async_ctx(req)) {
3902 if (kmsg->iov != kmsg->fast_iov)
3903 kfree(kmsg->iov);
3904 return -ENOMEM;
3905 }
3906 req->flags |= REQ_F_NEED_CLEANUP;
3907 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3908 return -EAGAIN;
3909}
3910
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03003911static int io_sendmsg_copy_hdr(struct io_kiocb *req,
3912 struct io_async_msghdr *iomsg)
3913{
3914 iomsg->iov = iomsg->fast_iov;
3915 iomsg->msg.msg_name = &iomsg->addr;
3916 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
3917 req->sr_msg.msg_flags, &iomsg->iov);
3918}
3919
Jens Axboe3529d8c2019-12-19 18:24:38 -07003920static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003921{
Jens Axboee47293f2019-12-20 08:58:21 -07003922 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003923 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003924 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003925
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003926 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3927 return -EINVAL;
3928
Jens Axboee47293f2019-12-20 08:58:21 -07003929 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03003930 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003931 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003932
Jens Axboed8768362020-02-27 14:17:49 -07003933#ifdef CONFIG_COMPAT
3934 if (req->ctx->compat)
3935 sr->msg_flags |= MSG_CMSG_COMPAT;
3936#endif
3937
Jens Axboefddafac2020-01-04 20:19:44 -07003938 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003939 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003940 /* iovec is already imported */
3941 if (req->flags & REQ_F_NEED_CLEANUP)
3942 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003943
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03003944 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003945 if (!ret)
3946 req->flags |= REQ_F_NEED_CLEANUP;
3947 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003948}
3949
Jens Axboe229a7b62020-06-22 10:13:11 -06003950static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
3951 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07003952{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03003953 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07003954 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03003955 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07003956 int ret;
3957
Jens Axboe03b12302019-12-02 18:50:25 -07003958 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03003959 if (unlikely(!sock))
3960 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003961
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03003962 if (req->io) {
3963 kmsg = &req->io->msg;
3964 kmsg->msg.msg_name = &req->io->msg.addr;
3965 /* if iov is set, it's allocated already */
3966 if (!kmsg->iov)
3967 kmsg->iov = kmsg->fast_iov;
3968 kmsg->msg.msg_iter.iov = kmsg->iov;
3969 } else {
3970 ret = io_sendmsg_copy_hdr(req, &iomsg);
3971 if (ret)
3972 return ret;
3973 kmsg = &iomsg;
Jens Axboe03b12302019-12-02 18:50:25 -07003974 }
3975
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03003976 flags = req->sr_msg.msg_flags;
3977 if (flags & MSG_DONTWAIT)
3978 req->flags |= REQ_F_NOWAIT;
3979 else if (force_nonblock)
3980 flags |= MSG_DONTWAIT;
3981
3982 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
3983 if (force_nonblock && ret == -EAGAIN)
3984 return io_setup_async_msg(req, kmsg);
3985 if (ret == -ERESTARTSYS)
3986 ret = -EINTR;
3987
Pavel Begunkov6b754c82020-07-16 23:28:00 +03003988 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003989 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003990 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003991 if (ret < 0)
3992 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003993 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07003994 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003995}
3996
Jens Axboe229a7b62020-06-22 10:13:11 -06003997static int io_send(struct io_kiocb *req, bool force_nonblock,
3998 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07003999{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004000 struct io_sr_msg *sr = &req->sr_msg;
4001 struct msghdr msg;
4002 struct iovec iov;
Jens Axboefddafac2020-01-04 20:19:44 -07004003 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004004 unsigned flags;
Jens Axboefddafac2020-01-04 20:19:44 -07004005 int ret;
4006
Jens Axboefddafac2020-01-04 20:19:44 -07004007 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004008 if (unlikely(!sock))
4009 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004010
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004011 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4012 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004013 return ret;;
Jens Axboefddafac2020-01-04 20:19:44 -07004014
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004015 msg.msg_name = NULL;
4016 msg.msg_control = NULL;
4017 msg.msg_controllen = 0;
4018 msg.msg_namelen = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004019
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004020 flags = req->sr_msg.msg_flags;
4021 if (flags & MSG_DONTWAIT)
4022 req->flags |= REQ_F_NOWAIT;
4023 else if (force_nonblock)
4024 flags |= MSG_DONTWAIT;
Jens Axboefddafac2020-01-04 20:19:44 -07004025
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004026 msg.msg_flags = flags;
4027 ret = sock_sendmsg(sock, &msg);
4028 if (force_nonblock && ret == -EAGAIN)
4029 return -EAGAIN;
4030 if (ret == -ERESTARTSYS)
4031 ret = -EINTR;
Jens Axboefddafac2020-01-04 20:19:44 -07004032
Jens Axboefddafac2020-01-04 20:19:44 -07004033 if (ret < 0)
4034 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004035 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004036 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004037}
4038
Pavel Begunkov1400e692020-07-12 20:41:05 +03004039static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4040 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004041{
4042 struct io_sr_msg *sr = &req->sr_msg;
4043 struct iovec __user *uiov;
4044 size_t iov_len;
4045 int ret;
4046
Pavel Begunkov1400e692020-07-12 20:41:05 +03004047 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4048 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004049 if (ret)
4050 return ret;
4051
4052 if (req->flags & REQ_F_BUFFER_SELECT) {
4053 if (iov_len > 1)
4054 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004055 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004056 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004057 sr->len = iomsg->iov[0].iov_len;
4058 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004059 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004060 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004061 } else {
4062 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004063 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004064 if (ret > 0)
4065 ret = 0;
4066 }
4067
4068 return ret;
4069}
4070
4071#ifdef CONFIG_COMPAT
4072static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004073 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004074{
4075 struct compat_msghdr __user *msg_compat;
4076 struct io_sr_msg *sr = &req->sr_msg;
4077 struct compat_iovec __user *uiov;
4078 compat_uptr_t ptr;
4079 compat_size_t len;
4080 int ret;
4081
Pavel Begunkov270a5942020-07-12 20:41:04 +03004082 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004083 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004084 &ptr, &len);
4085 if (ret)
4086 return ret;
4087
4088 uiov = compat_ptr(ptr);
4089 if (req->flags & REQ_F_BUFFER_SELECT) {
4090 compat_ssize_t clen;
4091
4092 if (len > 1)
4093 return -EINVAL;
4094 if (!access_ok(uiov, sizeof(*uiov)))
4095 return -EFAULT;
4096 if (__get_user(clen, &uiov->iov_len))
4097 return -EFAULT;
4098 if (clen < 0)
4099 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004100 sr->len = iomsg->iov[0].iov_len;
4101 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004102 } else {
4103 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004104 &iomsg->iov,
4105 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004106 if (ret < 0)
4107 return ret;
4108 }
4109
4110 return 0;
4111}
Jens Axboe03b12302019-12-02 18:50:25 -07004112#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004113
Pavel Begunkov1400e692020-07-12 20:41:05 +03004114static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4115 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004116{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004117 iomsg->msg.msg_name = &iomsg->addr;
4118 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004119
4120#ifdef CONFIG_COMPAT
4121 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004122 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004123#endif
4124
Pavel Begunkov1400e692020-07-12 20:41:05 +03004125 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004126}
4127
Jens Axboebcda7ba2020-02-23 16:42:51 -07004128static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004129 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004130{
4131 struct io_sr_msg *sr = &req->sr_msg;
4132 struct io_buffer *kbuf;
4133
Jens Axboebcda7ba2020-02-23 16:42:51 -07004134 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4135 if (IS_ERR(kbuf))
4136 return kbuf;
4137
4138 sr->kbuf = kbuf;
4139 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004140 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004141}
4142
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004143static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4144{
4145 return io_put_kbuf(req, req->sr_msg.kbuf);
4146}
4147
Jens Axboe3529d8c2019-12-19 18:24:38 -07004148static int io_recvmsg_prep(struct io_kiocb *req,
4149 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004150{
Jens Axboee47293f2019-12-20 08:58:21 -07004151 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004152 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004153 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004154
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004155 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4156 return -EINVAL;
4157
Jens Axboe3529d8c2019-12-19 18:24:38 -07004158 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004159 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004160 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004161 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004162
Jens Axboed8768362020-02-27 14:17:49 -07004163#ifdef CONFIG_COMPAT
4164 if (req->ctx->compat)
4165 sr->msg_flags |= MSG_CMSG_COMPAT;
4166#endif
4167
Jens Axboefddafac2020-01-04 20:19:44 -07004168 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004169 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004170 /* iovec is already imported */
4171 if (req->flags & REQ_F_NEED_CLEANUP)
4172 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004173
Pavel Begunkov1400e692020-07-12 20:41:05 +03004174 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004175 if (!ret)
4176 req->flags |= REQ_F_NEED_CLEANUP;
4177 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004178}
4179
Jens Axboe229a7b62020-06-22 10:13:11 -06004180static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4181 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004182{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004183 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004184 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004185 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004186 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004187 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004188
Jens Axboe0fa03c62019-04-19 13:34:07 -06004189 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004190 if (unlikely(!sock))
4191 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004192
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004193 if (req->io) {
4194 kmsg = &req->io->msg;
4195 kmsg->msg.msg_name = &req->io->msg.addr;
4196 /* if iov is set, it's allocated already */
4197 if (!kmsg->iov)
4198 kmsg->iov = kmsg->fast_iov;
4199 kmsg->msg.msg_iter.iov = kmsg->iov;
4200 } else {
4201 ret = io_recvmsg_copy_hdr(req, &iomsg);
4202 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004203 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004204 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004205 }
4206
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004207 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004208 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004209 if (IS_ERR(kbuf))
4210 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004211 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4212 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4213 1, req->sr_msg.len);
4214 }
4215
4216 flags = req->sr_msg.msg_flags;
4217 if (flags & MSG_DONTWAIT)
4218 req->flags |= REQ_F_NOWAIT;
4219 else if (force_nonblock)
4220 flags |= MSG_DONTWAIT;
4221
4222 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4223 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004224 if (force_nonblock && ret == -EAGAIN)
4225 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004226 if (ret == -ERESTARTSYS)
4227 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004228
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004229 if (req->flags & REQ_F_BUFFER_SELECTED)
4230 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004231 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004232 kfree(kmsg->iov);
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004233 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004234 if (ret < 0)
4235 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004236 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004237 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004238}
4239
Jens Axboe229a7b62020-06-22 10:13:11 -06004240static int io_recv(struct io_kiocb *req, bool force_nonblock,
4241 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004242{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004243 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004244 struct io_sr_msg *sr = &req->sr_msg;
4245 struct msghdr msg;
4246 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004247 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004248 struct iovec iov;
4249 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004250 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004251
Jens Axboefddafac2020-01-04 20:19:44 -07004252 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004253 if (unlikely(!sock))
4254 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004255
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004256 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004257 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004258 if (IS_ERR(kbuf))
4259 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004260 buf = u64_to_user_ptr(kbuf->addr);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004261 }
Jens Axboefddafac2020-01-04 20:19:44 -07004262
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004263 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004264 if (unlikely(ret))
4265 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004266
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004267 msg.msg_name = NULL;
4268 msg.msg_control = NULL;
4269 msg.msg_controllen = 0;
4270 msg.msg_namelen = 0;
4271 msg.msg_iocb = NULL;
4272 msg.msg_flags = 0;
4273
4274 flags = req->sr_msg.msg_flags;
4275 if (flags & MSG_DONTWAIT)
4276 req->flags |= REQ_F_NOWAIT;
4277 else if (force_nonblock)
4278 flags |= MSG_DONTWAIT;
4279
4280 ret = sock_recvmsg(sock, &msg, flags);
4281 if (force_nonblock && ret == -EAGAIN)
4282 return -EAGAIN;
4283 if (ret == -ERESTARTSYS)
4284 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004285out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004286 if (req->flags & REQ_F_BUFFER_SELECTED)
4287 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004288 if (ret < 0)
4289 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004290 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004291 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004292}
4293
Jens Axboe3529d8c2019-12-19 18:24:38 -07004294static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004295{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004296 struct io_accept *accept = &req->accept;
4297
Jens Axboe17f2fe32019-10-17 14:42:58 -06004298 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4299 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004300 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004301 return -EINVAL;
4302
Jens Axboed55e5f52019-12-11 16:12:15 -07004303 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4304 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004305 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004306 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004307 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004308}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004309
Jens Axboe229a7b62020-06-22 10:13:11 -06004310static int io_accept(struct io_kiocb *req, bool force_nonblock,
4311 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004312{
4313 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004314 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004315 int ret;
4316
Jiufei Xuee697dee2020-06-10 13:41:59 +08004317 if (req->file->f_flags & O_NONBLOCK)
4318 req->flags |= REQ_F_NOWAIT;
4319
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004320 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004321 accept->addr_len, accept->flags,
4322 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004323 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004324 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004325 if (ret < 0) {
4326 if (ret == -ERESTARTSYS)
4327 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004328 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004329 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004330 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004331 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004332}
4333
Jens Axboe3529d8c2019-12-19 18:24:38 -07004334static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004335{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004336 struct io_connect *conn = &req->connect;
4337 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004338
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004339 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4340 return -EINVAL;
4341 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4342 return -EINVAL;
4343
Jens Axboe3529d8c2019-12-19 18:24:38 -07004344 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4345 conn->addr_len = READ_ONCE(sqe->addr2);
4346
4347 if (!io)
4348 return 0;
4349
4350 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004351 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004352}
4353
Jens Axboe229a7b62020-06-22 10:13:11 -06004354static int io_connect(struct io_kiocb *req, bool force_nonblock,
4355 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004356{
Jens Axboef499a022019-12-02 16:28:46 -07004357 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004358 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004359 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004360
Jens Axboef499a022019-12-02 16:28:46 -07004361 if (req->io) {
4362 io = req->io;
4363 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004364 ret = move_addr_to_kernel(req->connect.addr,
4365 req->connect.addr_len,
4366 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004367 if (ret)
4368 goto out;
4369 io = &__io;
4370 }
4371
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004372 file_flags = force_nonblock ? O_NONBLOCK : 0;
4373
4374 ret = __sys_connect_file(req->file, &io->connect.address,
4375 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004376 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004377 if (req->io)
4378 return -EAGAIN;
4379 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004380 ret = -ENOMEM;
4381 goto out;
4382 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004383 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004384 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004385 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004386 if (ret == -ERESTARTSYS)
4387 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004388out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004389 if (ret < 0)
4390 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004391 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004392 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004393}
YueHaibing469956e2020-03-04 15:53:52 +08004394#else /* !CONFIG_NET */
4395static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4396{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004397 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004398}
4399
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004400static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4401 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004402{
YueHaibing469956e2020-03-04 15:53:52 +08004403 return -EOPNOTSUPP;
4404}
4405
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004406static int io_send(struct io_kiocb *req, bool force_nonblock,
4407 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004408{
4409 return -EOPNOTSUPP;
4410}
4411
4412static int io_recvmsg_prep(struct io_kiocb *req,
4413 const struct io_uring_sqe *sqe)
4414{
4415 return -EOPNOTSUPP;
4416}
4417
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004418static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4419 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004420{
4421 return -EOPNOTSUPP;
4422}
4423
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004424static int io_recv(struct io_kiocb *req, bool force_nonblock,
4425 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004426{
4427 return -EOPNOTSUPP;
4428}
4429
4430static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4431{
4432 return -EOPNOTSUPP;
4433}
4434
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004435static int io_accept(struct io_kiocb *req, bool force_nonblock,
4436 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004437{
4438 return -EOPNOTSUPP;
4439}
4440
4441static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4442{
4443 return -EOPNOTSUPP;
4444}
4445
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004446static int io_connect(struct io_kiocb *req, bool force_nonblock,
4447 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004448{
4449 return -EOPNOTSUPP;
4450}
4451#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004452
Jens Axboed7718a92020-02-14 22:23:12 -07004453struct io_poll_table {
4454 struct poll_table_struct pt;
4455 struct io_kiocb *req;
4456 int error;
4457};
4458
Jens Axboed7718a92020-02-14 22:23:12 -07004459static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4460 __poll_t mask, task_work_func_t func)
4461{
Jens Axboeaa96bf82020-04-03 11:26:26 -06004462 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004463
4464 /* for instances that support it check for an event match first: */
4465 if (mask && !(mask & poll->events))
4466 return 0;
4467
4468 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4469
4470 list_del_init(&poll->wait.entry);
4471
Jens Axboed7718a92020-02-14 22:23:12 -07004472 req->result = mask;
4473 init_task_work(&req->task_work, func);
4474 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004475 * If this fails, then the task is exiting. When a task exits, the
4476 * work gets canceled, so just cancel this request as well instead
4477 * of executing it. We can't safely execute it anyway, as we may not
4478 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004479 */
Jens Axboeb7db41c2020-07-04 08:55:50 -06004480 ret = io_req_task_work_add(req, &req->task_work);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004481 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004482 struct task_struct *tsk;
4483
Jens Axboee3aabf92020-05-18 11:04:17 -06004484 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004485 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004486 task_work_add(tsk, &req->task_work, 0);
4487 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004488 }
Jens Axboed7718a92020-02-14 22:23:12 -07004489 return 1;
4490}
4491
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004492static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4493 __acquires(&req->ctx->completion_lock)
4494{
4495 struct io_ring_ctx *ctx = req->ctx;
4496
4497 if (!req->result && !READ_ONCE(poll->canceled)) {
4498 struct poll_table_struct pt = { ._key = poll->events };
4499
4500 req->result = vfs_poll(req->file, &pt) & poll->events;
4501 }
4502
4503 spin_lock_irq(&ctx->completion_lock);
4504 if (!req->result && !READ_ONCE(poll->canceled)) {
4505 add_wait_queue(poll->head, &poll->wait);
4506 return true;
4507 }
4508
4509 return false;
4510}
4511
Jens Axboe807abcb2020-07-17 17:09:27 -06004512static void io_poll_remove_double(struct io_kiocb *req, void *data)
Jens Axboe18bceab2020-05-15 11:56:54 -06004513{
Jens Axboe807abcb2020-07-17 17:09:27 -06004514 struct io_poll_iocb *poll = data;
Jens Axboe18bceab2020-05-15 11:56:54 -06004515
4516 lockdep_assert_held(&req->ctx->completion_lock);
4517
4518 if (poll && poll->head) {
4519 struct wait_queue_head *head = poll->head;
4520
4521 spin_lock(&head->lock);
4522 list_del_init(&poll->wait.entry);
4523 if (poll->wait.private)
4524 refcount_dec(&req->refs);
4525 poll->head = NULL;
4526 spin_unlock(&head->lock);
4527 }
4528}
4529
4530static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4531{
4532 struct io_ring_ctx *ctx = req->ctx;
4533
Jens Axboe807abcb2020-07-17 17:09:27 -06004534 io_poll_remove_double(req, req->io);
Jens Axboe18bceab2020-05-15 11:56:54 -06004535 req->poll.done = true;
4536 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4537 io_commit_cqring(ctx);
4538}
4539
4540static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4541{
4542 struct io_ring_ctx *ctx = req->ctx;
4543
4544 if (io_poll_rewait(req, &req->poll)) {
4545 spin_unlock_irq(&ctx->completion_lock);
4546 return;
4547 }
4548
4549 hash_del(&req->hash_node);
4550 io_poll_complete(req, req->result, 0);
4551 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004552 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004553 spin_unlock_irq(&ctx->completion_lock);
4554
4555 io_cqring_ev_posted(ctx);
4556}
4557
4558static void io_poll_task_func(struct callback_head *cb)
4559{
4560 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4561 struct io_kiocb *nxt = NULL;
4562
4563 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004564 if (nxt)
4565 __io_req_task_submit(nxt);
Jens Axboe18bceab2020-05-15 11:56:54 -06004566}
4567
4568static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4569 int sync, void *key)
4570{
4571 struct io_kiocb *req = wait->private;
Jens Axboe807abcb2020-07-17 17:09:27 -06004572 struct io_poll_iocb *poll = req->apoll->double_poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004573 __poll_t mask = key_to_poll(key);
4574
4575 /* for instances that support it check for an event match first: */
4576 if (mask && !(mask & poll->events))
4577 return 0;
4578
Jens Axboe807abcb2020-07-17 17:09:27 -06004579 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004580 bool done;
4581
Jens Axboe807abcb2020-07-17 17:09:27 -06004582 spin_lock(&poll->head->lock);
4583 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004584 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004585 list_del_init(&poll->wait.entry);
4586 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004587 if (!done)
4588 __io_async_wake(req, poll, mask, io_poll_task_func);
4589 }
4590 refcount_dec(&req->refs);
4591 return 1;
4592}
4593
4594static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4595 wait_queue_func_t wake_func)
4596{
4597 poll->head = NULL;
4598 poll->done = false;
4599 poll->canceled = false;
4600 poll->events = events;
4601 INIT_LIST_HEAD(&poll->wait.entry);
4602 init_waitqueue_func_entry(&poll->wait, wake_func);
4603}
4604
4605static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004606 struct wait_queue_head *head,
4607 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004608{
4609 struct io_kiocb *req = pt->req;
4610
4611 /*
4612 * If poll->head is already set, it's because the file being polled
4613 * uses multiple waitqueues for poll handling (eg one for read, one
4614 * for write). Setup a separate io_poll_iocb if this happens.
4615 */
4616 if (unlikely(poll->head)) {
4617 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004618 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004619 pt->error = -EINVAL;
4620 return;
4621 }
4622 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4623 if (!poll) {
4624 pt->error = -ENOMEM;
4625 return;
4626 }
4627 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4628 refcount_inc(&req->refs);
4629 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004630 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004631 }
4632
4633 pt->error = 0;
4634 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004635
4636 if (poll->events & EPOLLEXCLUSIVE)
4637 add_wait_queue_exclusive(head, &poll->wait);
4638 else
4639 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004640}
4641
4642static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4643 struct poll_table_struct *p)
4644{
4645 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004646 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004647
Jens Axboe807abcb2020-07-17 17:09:27 -06004648 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004649}
4650
Jens Axboed7718a92020-02-14 22:23:12 -07004651static void io_async_task_func(struct callback_head *cb)
4652{
4653 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4654 struct async_poll *apoll = req->apoll;
4655 struct io_ring_ctx *ctx = req->ctx;
4656
4657 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4658
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004659 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004660 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004661 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004662 }
4663
Jens Axboe31067252020-05-17 17:43:31 -06004664 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004665 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004666 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004667
Jens Axboe807abcb2020-07-17 17:09:27 -06004668 io_poll_remove_double(req, apoll->double_poll);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004669 spin_unlock_irq(&ctx->completion_lock);
4670
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004671 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004672 if (req->flags & REQ_F_WORK_INITIALIZED)
4673 memcpy(&req->work, &apoll->work, sizeof(req->work));
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004674
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004675 if (!READ_ONCE(apoll->poll.canceled))
4676 __io_req_task_submit(req);
4677 else
4678 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004679
Jens Axboe807abcb2020-07-17 17:09:27 -06004680 kfree(apoll->double_poll);
Dan Carpenteraa340842020-07-08 21:47:11 +03004681 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004682}
4683
4684static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4685 void *key)
4686{
4687 struct io_kiocb *req = wait->private;
4688 struct io_poll_iocb *poll = &req->apoll->poll;
4689
4690 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4691 key_to_poll(key));
4692
4693 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4694}
4695
4696static void io_poll_req_insert(struct io_kiocb *req)
4697{
4698 struct io_ring_ctx *ctx = req->ctx;
4699 struct hlist_head *list;
4700
4701 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4702 hlist_add_head(&req->hash_node, list);
4703}
4704
4705static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4706 struct io_poll_iocb *poll,
4707 struct io_poll_table *ipt, __poll_t mask,
4708 wait_queue_func_t wake_func)
4709 __acquires(&ctx->completion_lock)
4710{
4711 struct io_ring_ctx *ctx = req->ctx;
4712 bool cancel = false;
4713
Jens Axboe18bceab2020-05-15 11:56:54 -06004714 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004715 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004716 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004717
4718 ipt->pt._key = mask;
4719 ipt->req = req;
4720 ipt->error = -EINVAL;
4721
Jens Axboed7718a92020-02-14 22:23:12 -07004722 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4723
4724 spin_lock_irq(&ctx->completion_lock);
4725 if (likely(poll->head)) {
4726 spin_lock(&poll->head->lock);
4727 if (unlikely(list_empty(&poll->wait.entry))) {
4728 if (ipt->error)
4729 cancel = true;
4730 ipt->error = 0;
4731 mask = 0;
4732 }
4733 if (mask || ipt->error)
4734 list_del_init(&poll->wait.entry);
4735 else if (cancel)
4736 WRITE_ONCE(poll->canceled, true);
4737 else if (!poll->done) /* actually waiting for an event */
4738 io_poll_req_insert(req);
4739 spin_unlock(&poll->head->lock);
4740 }
4741
4742 return mask;
4743}
4744
4745static bool io_arm_poll_handler(struct io_kiocb *req)
4746{
4747 const struct io_op_def *def = &io_op_defs[req->opcode];
4748 struct io_ring_ctx *ctx = req->ctx;
4749 struct async_poll *apoll;
4750 struct io_poll_table ipt;
4751 __poll_t mask, ret;
4752
4753 if (!req->file || !file_can_poll(req->file))
4754 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004755 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004756 return false;
4757 if (!def->pollin && !def->pollout)
4758 return false;
4759
4760 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4761 if (unlikely(!apoll))
4762 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004763 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004764
4765 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004766 if (req->flags & REQ_F_WORK_INITIALIZED)
4767 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004768
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004769 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004770 req->apoll = apoll;
4771 INIT_HLIST_NODE(&req->hash_node);
4772
Nathan Chancellor8755d972020-03-02 16:01:19 -07004773 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004774 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004775 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004776 if (def->pollout)
4777 mask |= POLLOUT | POLLWRNORM;
4778 mask |= POLLERR | POLLPRI;
4779
4780 ipt.pt._qproc = io_async_queue_proc;
4781
4782 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4783 io_async_wake);
4784 if (ret) {
Jens Axboe807abcb2020-07-17 17:09:27 -06004785 io_poll_remove_double(req, apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004786 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004787 if (req->flags & REQ_F_WORK_INITIALIZED)
4788 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe807abcb2020-07-17 17:09:27 -06004789 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004790 kfree(apoll);
4791 return false;
4792 }
4793 spin_unlock_irq(&ctx->completion_lock);
4794 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4795 apoll->poll.events);
4796 return true;
4797}
4798
4799static bool __io_poll_remove_one(struct io_kiocb *req,
4800 struct io_poll_iocb *poll)
4801{
Jens Axboeb41e9852020-02-17 09:52:41 -07004802 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004803
4804 spin_lock(&poll->head->lock);
4805 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004806 if (!list_empty(&poll->wait.entry)) {
4807 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004808 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004809 }
4810 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004811 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004812 return do_complete;
4813}
4814
4815static bool io_poll_remove_one(struct io_kiocb *req)
4816{
4817 bool do_complete;
4818
4819 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe807abcb2020-07-17 17:09:27 -06004820 io_poll_remove_double(req, req->io);
Jens Axboed7718a92020-02-14 22:23:12 -07004821 do_complete = __io_poll_remove_one(req, &req->poll);
4822 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004823 struct async_poll *apoll = req->apoll;
4824
Jens Axboe807abcb2020-07-17 17:09:27 -06004825 io_poll_remove_double(req, apoll->double_poll);
4826
Jens Axboed7718a92020-02-14 22:23:12 -07004827 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004828 do_complete = __io_poll_remove_one(req, &apoll->poll);
4829 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004830 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004831 /*
4832 * restore ->work because we will call
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03004833 * io_req_clean_work below when dropping the
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004834 * final reference.
4835 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004836 if (req->flags & REQ_F_WORK_INITIALIZED)
4837 memcpy(&req->work, &apoll->work,
4838 sizeof(req->work));
Jens Axboe807abcb2020-07-17 17:09:27 -06004839 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004840 kfree(apoll);
4841 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004842 }
4843
Jens Axboeb41e9852020-02-17 09:52:41 -07004844 if (do_complete) {
4845 io_cqring_fill_event(req, -ECANCELED);
4846 io_commit_cqring(req->ctx);
4847 req->flags |= REQ_F_COMP_LOCKED;
4848 io_put_req(req);
4849 }
4850
4851 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004852}
4853
4854static void io_poll_remove_all(struct io_ring_ctx *ctx)
4855{
Jens Axboe78076bb2019-12-04 19:56:40 -07004856 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004857 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004858 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004859
4860 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004861 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4862 struct hlist_head *list;
4863
4864 list = &ctx->cancel_hash[i];
4865 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004866 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004867 }
4868 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004869
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004870 if (posted)
4871 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004872}
4873
Jens Axboe47f46762019-11-09 17:43:02 -07004874static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4875{
Jens Axboe78076bb2019-12-04 19:56:40 -07004876 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004877 struct io_kiocb *req;
4878
Jens Axboe78076bb2019-12-04 19:56:40 -07004879 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4880 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004881 if (sqe_addr != req->user_data)
4882 continue;
4883 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004884 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004885 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004886 }
4887
4888 return -ENOENT;
4889}
4890
Jens Axboe3529d8c2019-12-19 18:24:38 -07004891static int io_poll_remove_prep(struct io_kiocb *req,
4892 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004893{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004894 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4895 return -EINVAL;
4896 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4897 sqe->poll_events)
4898 return -EINVAL;
4899
Jens Axboe0969e782019-12-17 18:40:57 -07004900 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004901 return 0;
4902}
4903
4904/*
4905 * Find a running poll command that matches one specified in sqe->addr,
4906 * and remove it if found.
4907 */
4908static int io_poll_remove(struct io_kiocb *req)
4909{
4910 struct io_ring_ctx *ctx = req->ctx;
4911 u64 addr;
4912 int ret;
4913
Jens Axboe0969e782019-12-17 18:40:57 -07004914 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004915 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004916 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004917 spin_unlock_irq(&ctx->completion_lock);
4918
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004919 if (ret < 0)
4920 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004921 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004922 return 0;
4923}
4924
Jens Axboe221c5eb2019-01-17 09:41:58 -07004925static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4926 void *key)
4927{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004928 struct io_kiocb *req = wait->private;
4929 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004930
Jens Axboed7718a92020-02-14 22:23:12 -07004931 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004932}
4933
Jens Axboe221c5eb2019-01-17 09:41:58 -07004934static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4935 struct poll_table_struct *p)
4936{
4937 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4938
Jens Axboe807abcb2020-07-17 17:09:27 -06004939 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07004940}
4941
Jens Axboe3529d8c2019-12-19 18:24:38 -07004942static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004943{
4944 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08004945 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004946
4947 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4948 return -EINVAL;
4949 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4950 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004951 if (!poll->file)
4952 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004953
Jiufei Xue5769a352020-06-17 17:53:55 +08004954 events = READ_ONCE(sqe->poll32_events);
4955#ifdef __BIG_ENDIAN
4956 events = swahw32(events);
4957#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004958 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4959 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07004960
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004961 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004962 return 0;
4963}
4964
Pavel Begunkov014db002020-03-03 21:33:12 +03004965static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004966{
4967 struct io_poll_iocb *poll = &req->poll;
4968 struct io_ring_ctx *ctx = req->ctx;
4969 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004970 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004971
Pavel Begunkovd5e16d82020-07-24 20:07:20 +03004972 /* ->work is in union with hash_node and others */
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03004973 io_req_clean_work(req);
Pavel Begunkovd5e16d82020-07-24 20:07:20 +03004974
Jens Axboe78076bb2019-12-04 19:56:40 -07004975 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004976 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004977
Jens Axboed7718a92020-02-14 22:23:12 -07004978 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4979 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004980
Jens Axboe8c838782019-03-12 15:48:16 -06004981 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004982 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004983 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004984 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004985 spin_unlock_irq(&ctx->completion_lock);
4986
Jens Axboe8c838782019-03-12 15:48:16 -06004987 if (mask) {
4988 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004989 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004990 }
Jens Axboe8c838782019-03-12 15:48:16 -06004991 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004992}
4993
Jens Axboe5262f562019-09-17 12:26:57 -06004994static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4995{
Jens Axboead8a48a2019-11-15 08:49:11 -07004996 struct io_timeout_data *data = container_of(timer,
4997 struct io_timeout_data, timer);
4998 struct io_kiocb *req = data->req;
4999 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005000 unsigned long flags;
5001
Jens Axboe5262f562019-09-17 12:26:57 -06005002 atomic_inc(&ctx->cq_timeouts);
5003
5004 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08005005 /*
Jens Axboe11365042019-10-16 09:08:32 -06005006 * We could be racing with timeout deletion. If the list is empty,
5007 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005008 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005009 if (!list_empty(&req->timeout.list))
5010 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005011
Jens Axboe78e19bb2019-11-06 15:21:34 -07005012 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005013 io_commit_cqring(ctx);
5014 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5015
5016 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005017 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005018 io_put_req(req);
5019 return HRTIMER_NORESTART;
5020}
5021
Jens Axboe47f46762019-11-09 17:43:02 -07005022static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5023{
5024 struct io_kiocb *req;
5025 int ret = -ENOENT;
5026
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005027 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Jens Axboe47f46762019-11-09 17:43:02 -07005028 if (user_data == req->user_data) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005029 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005030 ret = 0;
5031 break;
5032 }
5033 }
5034
5035 if (ret == -ENOENT)
5036 return ret;
5037
Jens Axboe2d283902019-12-04 11:08:05 -07005038 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005039 if (ret == -1)
5040 return -EALREADY;
5041
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005042 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005043 io_cqring_fill_event(req, -ECANCELED);
5044 io_put_req(req);
5045 return 0;
5046}
5047
Jens Axboe3529d8c2019-12-19 18:24:38 -07005048static int io_timeout_remove_prep(struct io_kiocb *req,
5049 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005050{
Jens Axboeb29472e2019-12-17 18:50:29 -07005051 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5052 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005053 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5054 return -EINVAL;
5055 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005056 return -EINVAL;
5057
5058 req->timeout.addr = READ_ONCE(sqe->addr);
5059 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5060 if (req->timeout.flags)
5061 return -EINVAL;
5062
Jens Axboeb29472e2019-12-17 18:50:29 -07005063 return 0;
5064}
5065
Jens Axboe11365042019-10-16 09:08:32 -06005066/*
5067 * Remove or update an existing timeout command
5068 */
Jens Axboefc4df992019-12-10 14:38:45 -07005069static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005070{
5071 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005072 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005073
Jens Axboe11365042019-10-16 09:08:32 -06005074 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005075 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005076
Jens Axboe47f46762019-11-09 17:43:02 -07005077 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005078 io_commit_cqring(ctx);
5079 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005080 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005081 if (ret < 0)
5082 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005083 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005084 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005085}
5086
Jens Axboe3529d8c2019-12-19 18:24:38 -07005087static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005088 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005089{
Jens Axboead8a48a2019-11-15 08:49:11 -07005090 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005091 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005092 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005093
Jens Axboead8a48a2019-11-15 08:49:11 -07005094 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005095 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005096 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005097 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005098 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005099 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005100 flags = READ_ONCE(sqe->timeout_flags);
5101 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005102 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005103
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005104 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005105
Jens Axboe3529d8c2019-12-19 18:24:38 -07005106 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005107 return -ENOMEM;
5108
5109 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005110 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005111
5112 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005113 return -EFAULT;
5114
Jens Axboe11365042019-10-16 09:08:32 -06005115 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005116 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005117 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005118 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005119
Jens Axboead8a48a2019-11-15 08:49:11 -07005120 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5121 return 0;
5122}
5123
Jens Axboefc4df992019-12-10 14:38:45 -07005124static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005125{
Jens Axboead8a48a2019-11-15 08:49:11 -07005126 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005127 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005128 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005129 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005130
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005131 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005132
Jens Axboe5262f562019-09-17 12:26:57 -06005133 /*
5134 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005135 * timeout event to be satisfied. If it isn't set, then this is
5136 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005137 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005138 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005139 entry = ctx->timeout_list.prev;
5140 goto add;
5141 }
Jens Axboe5262f562019-09-17 12:26:57 -06005142
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005143 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5144 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005145
5146 /*
5147 * Insertion sort, ensuring the first entry in the list is always
5148 * the one we need first.
5149 */
Jens Axboe5262f562019-09-17 12:26:57 -06005150 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005151 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5152 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005153
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005154 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005155 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005156 /* nxt.seq is behind @tail, otherwise would've been completed */
5157 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005158 break;
5159 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005160add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005161 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005162 data->timer.function = io_timeout_fn;
5163 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005164 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005165 return 0;
5166}
5167
Jens Axboe62755e32019-10-28 21:49:21 -06005168static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005169{
Jens Axboe62755e32019-10-28 21:49:21 -06005170 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005171
Jens Axboe62755e32019-10-28 21:49:21 -06005172 return req->user_data == (unsigned long) data;
5173}
5174
Jens Axboee977d6d2019-11-05 12:39:45 -07005175static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005176{
Jens Axboe62755e32019-10-28 21:49:21 -06005177 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005178 int ret = 0;
5179
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005180 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005181 switch (cancel_ret) {
5182 case IO_WQ_CANCEL_OK:
5183 ret = 0;
5184 break;
5185 case IO_WQ_CANCEL_RUNNING:
5186 ret = -EALREADY;
5187 break;
5188 case IO_WQ_CANCEL_NOTFOUND:
5189 ret = -ENOENT;
5190 break;
5191 }
5192
Jens Axboee977d6d2019-11-05 12:39:45 -07005193 return ret;
5194}
5195
Jens Axboe47f46762019-11-09 17:43:02 -07005196static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5197 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005198 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005199{
5200 unsigned long flags;
5201 int ret;
5202
5203 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5204 if (ret != -ENOENT) {
5205 spin_lock_irqsave(&ctx->completion_lock, flags);
5206 goto done;
5207 }
5208
5209 spin_lock_irqsave(&ctx->completion_lock, flags);
5210 ret = io_timeout_cancel(ctx, sqe_addr);
5211 if (ret != -ENOENT)
5212 goto done;
5213 ret = io_poll_cancel(ctx, sqe_addr);
5214done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005215 if (!ret)
5216 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005217 io_cqring_fill_event(req, ret);
5218 io_commit_cqring(ctx);
5219 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5220 io_cqring_ev_posted(ctx);
5221
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005222 if (ret < 0)
5223 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005224 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005225}
5226
Jens Axboe3529d8c2019-12-19 18:24:38 -07005227static int io_async_cancel_prep(struct io_kiocb *req,
5228 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005229{
Jens Axboefbf23842019-12-17 18:45:56 -07005230 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005231 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005232 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5233 return -EINVAL;
5234 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005235 return -EINVAL;
5236
Jens Axboefbf23842019-12-17 18:45:56 -07005237 req->cancel.addr = READ_ONCE(sqe->addr);
5238 return 0;
5239}
5240
Pavel Begunkov014db002020-03-03 21:33:12 +03005241static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005242{
5243 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005244
Pavel Begunkov014db002020-03-03 21:33:12 +03005245 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005246 return 0;
5247}
5248
Jens Axboe05f3fb32019-12-09 11:22:50 -07005249static int io_files_update_prep(struct io_kiocb *req,
5250 const struct io_uring_sqe *sqe)
5251{
Daniele Albano61710e42020-07-18 14:15:16 -06005252 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5253 return -EINVAL;
5254 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005255 return -EINVAL;
5256
5257 req->files_update.offset = READ_ONCE(sqe->off);
5258 req->files_update.nr_args = READ_ONCE(sqe->len);
5259 if (!req->files_update.nr_args)
5260 return -EINVAL;
5261 req->files_update.arg = READ_ONCE(sqe->addr);
5262 return 0;
5263}
5264
Jens Axboe229a7b62020-06-22 10:13:11 -06005265static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5266 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005267{
5268 struct io_ring_ctx *ctx = req->ctx;
5269 struct io_uring_files_update up;
5270 int ret;
5271
Jens Axboef86cd202020-01-29 13:46:44 -07005272 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005273 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005274
5275 up.offset = req->files_update.offset;
5276 up.fds = req->files_update.arg;
5277
5278 mutex_lock(&ctx->uring_lock);
5279 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5280 mutex_unlock(&ctx->uring_lock);
5281
5282 if (ret < 0)
5283 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005284 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005285 return 0;
5286}
5287
Jens Axboe3529d8c2019-12-19 18:24:38 -07005288static int io_req_defer_prep(struct io_kiocb *req,
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03005289 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005290{
Jens Axboee7815732019-12-17 19:45:06 -07005291 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005292
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005293 if (!sqe)
5294 return 0;
5295
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005296 if (io_alloc_async_ctx(req))
5297 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005298 ret = io_prep_work_files(req);
5299 if (unlikely(ret))
5300 return ret;
Pavel Begunkov710c2bf2020-06-27 14:04:58 +03005301
Jens Axboed625c6e2019-12-17 19:53:05 -07005302 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005303 case IORING_OP_NOP:
5304 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005305 case IORING_OP_READV:
5306 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005307 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005308 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005309 break;
5310 case IORING_OP_WRITEV:
5311 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005312 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005313 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005314 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005315 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005316 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005317 break;
5318 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005319 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005320 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005321 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005322 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005323 break;
5324 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005325 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005326 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005327 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005328 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005329 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005330 break;
5331 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005332 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005333 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005334 break;
Jens Axboef499a022019-12-02 16:28:46 -07005335 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005336 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005337 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005338 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005339 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005340 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005341 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005342 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005343 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005344 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005345 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005346 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005347 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005348 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005349 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005350 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005351 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005352 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005353 case IORING_OP_FALLOCATE:
5354 ret = io_fallocate_prep(req, sqe);
5355 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005356 case IORING_OP_OPENAT:
5357 ret = io_openat_prep(req, sqe);
5358 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005359 case IORING_OP_CLOSE:
5360 ret = io_close_prep(req, sqe);
5361 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005362 case IORING_OP_FILES_UPDATE:
5363 ret = io_files_update_prep(req, sqe);
5364 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005365 case IORING_OP_STATX:
5366 ret = io_statx_prep(req, sqe);
5367 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005368 case IORING_OP_FADVISE:
5369 ret = io_fadvise_prep(req, sqe);
5370 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005371 case IORING_OP_MADVISE:
5372 ret = io_madvise_prep(req, sqe);
5373 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005374 case IORING_OP_OPENAT2:
5375 ret = io_openat2_prep(req, sqe);
5376 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005377 case IORING_OP_EPOLL_CTL:
5378 ret = io_epoll_ctl_prep(req, sqe);
5379 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005380 case IORING_OP_SPLICE:
5381 ret = io_splice_prep(req, sqe);
5382 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005383 case IORING_OP_PROVIDE_BUFFERS:
5384 ret = io_provide_buffers_prep(req, sqe);
5385 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005386 case IORING_OP_REMOVE_BUFFERS:
5387 ret = io_remove_buffers_prep(req, sqe);
5388 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005389 case IORING_OP_TEE:
5390 ret = io_tee_prep(req, sqe);
5391 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005392 default:
Jens Axboee7815732019-12-17 19:45:06 -07005393 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5394 req->opcode);
5395 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005396 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005397 }
5398
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005399 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005400}
5401
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005402static u32 io_get_sequence(struct io_kiocb *req)
5403{
5404 struct io_kiocb *pos;
5405 struct io_ring_ctx *ctx = req->ctx;
5406 u32 total_submitted, nr_reqs = 1;
5407
5408 if (req->flags & REQ_F_LINK_HEAD)
5409 list_for_each_entry(pos, &req->link_list, link_list)
5410 nr_reqs++;
5411
5412 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5413 return total_submitted - nr_reqs;
5414}
5415
Jens Axboe3529d8c2019-12-19 18:24:38 -07005416static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005417{
Jackie Liua197f662019-11-08 08:09:12 -07005418 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005419 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005420 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005421 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005422
Bob Liu9d858b22019-11-13 18:06:25 +08005423 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005424 if (likely(list_empty_careful(&ctx->defer_list) &&
5425 !(req->flags & REQ_F_IO_DRAIN)))
5426 return 0;
5427
5428 seq = io_get_sequence(req);
5429 /* Still a chance to pass the sequence check */
5430 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005431 return 0;
5432
Pavel Begunkov650b5482020-05-17 14:02:11 +03005433 if (!req->io) {
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03005434 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005435 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005436 return ret;
5437 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005438 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005439 de = kmalloc(sizeof(*de), GFP_KERNEL);
5440 if (!de)
5441 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005442
Jens Axboede0617e2019-04-06 21:51:27 -06005443 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005444 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005445 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005446 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005447 io_queue_async_work(req);
5448 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005449 }
5450
Jens Axboe915967f2019-11-21 09:01:20 -07005451 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005452 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005453 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005454 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005455 spin_unlock_irq(&ctx->completion_lock);
5456 return -EIOCBQUEUED;
5457}
5458
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005459static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005460{
5461 struct io_async_ctx *io = req->io;
5462
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005463 if (req->flags & REQ_F_BUFFER_SELECTED) {
5464 switch (req->opcode) {
5465 case IORING_OP_READV:
5466 case IORING_OP_READ_FIXED:
5467 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005468 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005469 break;
5470 case IORING_OP_RECVMSG:
5471 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005472 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005473 break;
5474 }
5475 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005476 }
5477
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005478 if (req->flags & REQ_F_NEED_CLEANUP) {
5479 switch (req->opcode) {
5480 case IORING_OP_READV:
5481 case IORING_OP_READ_FIXED:
5482 case IORING_OP_READ:
5483 case IORING_OP_WRITEV:
5484 case IORING_OP_WRITE_FIXED:
5485 case IORING_OP_WRITE:
5486 if (io->rw.iov != io->rw.fast_iov)
5487 kfree(io->rw.iov);
5488 break;
5489 case IORING_OP_RECVMSG:
5490 case IORING_OP_SENDMSG:
5491 if (io->msg.iov != io->msg.fast_iov)
5492 kfree(io->msg.iov);
5493 break;
5494 case IORING_OP_SPLICE:
5495 case IORING_OP_TEE:
5496 io_put_file(req, req->splice.file_in,
5497 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5498 break;
5499 }
5500 req->flags &= ~REQ_F_NEED_CLEANUP;
5501 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005502}
5503
Jens Axboe3529d8c2019-12-19 18:24:38 -07005504static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005505 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005506{
Jackie Liua197f662019-11-08 08:09:12 -07005507 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005508 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005509
Jens Axboed625c6e2019-12-17 19:53:05 -07005510 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005511 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005512 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005513 break;
5514 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005515 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005516 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005517 if (sqe) {
5518 ret = io_read_prep(req, sqe, force_nonblock);
5519 if (ret < 0)
5520 break;
5521 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005522 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005523 break;
5524 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005525 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005526 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005527 if (sqe) {
5528 ret = io_write_prep(req, sqe, force_nonblock);
5529 if (ret < 0)
5530 break;
5531 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005532 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005533 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005534 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005535 if (sqe) {
5536 ret = io_prep_fsync(req, sqe);
5537 if (ret < 0)
5538 break;
5539 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005540 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005541 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005542 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005543 if (sqe) {
5544 ret = io_poll_add_prep(req, sqe);
5545 if (ret)
5546 break;
5547 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005548 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005549 break;
5550 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005551 if (sqe) {
5552 ret = io_poll_remove_prep(req, sqe);
5553 if (ret < 0)
5554 break;
5555 }
Jens Axboefc4df992019-12-10 14:38:45 -07005556 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005557 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005558 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005559 if (sqe) {
5560 ret = io_prep_sfr(req, sqe);
5561 if (ret < 0)
5562 break;
5563 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005564 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005565 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005566 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005567 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005568 if (sqe) {
5569 ret = io_sendmsg_prep(req, sqe);
5570 if (ret < 0)
5571 break;
5572 }
Jens Axboefddafac2020-01-04 20:19:44 -07005573 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005574 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005575 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005576 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005577 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005578 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005579 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005580 if (sqe) {
5581 ret = io_recvmsg_prep(req, sqe);
5582 if (ret)
5583 break;
5584 }
Jens Axboefddafac2020-01-04 20:19:44 -07005585 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005586 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005587 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005588 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005589 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005590 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005591 if (sqe) {
5592 ret = io_timeout_prep(req, sqe, false);
5593 if (ret)
5594 break;
5595 }
Jens Axboefc4df992019-12-10 14:38:45 -07005596 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005597 break;
Jens Axboe11365042019-10-16 09:08:32 -06005598 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005599 if (sqe) {
5600 ret = io_timeout_remove_prep(req, sqe);
5601 if (ret)
5602 break;
5603 }
Jens Axboefc4df992019-12-10 14:38:45 -07005604 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005605 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005606 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005607 if (sqe) {
5608 ret = io_accept_prep(req, sqe);
5609 if (ret)
5610 break;
5611 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005612 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005613 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005614 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005615 if (sqe) {
5616 ret = io_connect_prep(req, sqe);
5617 if (ret)
5618 break;
5619 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005620 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005621 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005622 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005623 if (sqe) {
5624 ret = io_async_cancel_prep(req, sqe);
5625 if (ret)
5626 break;
5627 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005628 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005629 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005630 case IORING_OP_FALLOCATE:
5631 if (sqe) {
5632 ret = io_fallocate_prep(req, sqe);
5633 if (ret)
5634 break;
5635 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005636 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005637 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005638 case IORING_OP_OPENAT:
5639 if (sqe) {
5640 ret = io_openat_prep(req, sqe);
5641 if (ret)
5642 break;
5643 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005644 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005645 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005646 case IORING_OP_CLOSE:
5647 if (sqe) {
5648 ret = io_close_prep(req, sqe);
5649 if (ret)
5650 break;
5651 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005652 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005653 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005654 case IORING_OP_FILES_UPDATE:
5655 if (sqe) {
5656 ret = io_files_update_prep(req, sqe);
5657 if (ret)
5658 break;
5659 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005660 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005661 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005662 case IORING_OP_STATX:
5663 if (sqe) {
5664 ret = io_statx_prep(req, sqe);
5665 if (ret)
5666 break;
5667 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005668 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005669 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005670 case IORING_OP_FADVISE:
5671 if (sqe) {
5672 ret = io_fadvise_prep(req, sqe);
5673 if (ret)
5674 break;
5675 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005676 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005677 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005678 case IORING_OP_MADVISE:
5679 if (sqe) {
5680 ret = io_madvise_prep(req, sqe);
5681 if (ret)
5682 break;
5683 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005684 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005685 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005686 case IORING_OP_OPENAT2:
5687 if (sqe) {
5688 ret = io_openat2_prep(req, sqe);
5689 if (ret)
5690 break;
5691 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005692 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005693 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005694 case IORING_OP_EPOLL_CTL:
5695 if (sqe) {
5696 ret = io_epoll_ctl_prep(req, sqe);
5697 if (ret)
5698 break;
5699 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005700 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005701 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005702 case IORING_OP_SPLICE:
5703 if (sqe) {
5704 ret = io_splice_prep(req, sqe);
5705 if (ret < 0)
5706 break;
5707 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005708 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005709 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005710 case IORING_OP_PROVIDE_BUFFERS:
5711 if (sqe) {
5712 ret = io_provide_buffers_prep(req, sqe);
5713 if (ret)
5714 break;
5715 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005716 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005717 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005718 case IORING_OP_REMOVE_BUFFERS:
5719 if (sqe) {
5720 ret = io_remove_buffers_prep(req, sqe);
5721 if (ret)
5722 break;
5723 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005724 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005725 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005726 case IORING_OP_TEE:
5727 if (sqe) {
5728 ret = io_tee_prep(req, sqe);
5729 if (ret < 0)
5730 break;
5731 }
5732 ret = io_tee(req, force_nonblock);
5733 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005734 default:
5735 ret = -EINVAL;
5736 break;
5737 }
5738
5739 if (ret)
5740 return ret;
5741
Jens Axboeb5325762020-05-19 21:20:27 -06005742 /* If the op doesn't have a file, we're not polling for it */
5743 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005744 const bool in_async = io_wq_current_is_worker();
5745
Jens Axboe11ba8202020-01-15 21:51:17 -07005746 /* workqueue context doesn't hold uring_lock, grab it now */
5747 if (in_async)
5748 mutex_lock(&ctx->uring_lock);
5749
Jens Axboe2b188cc2019-01-07 10:46:33 -07005750 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005751
5752 if (in_async)
5753 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005754 }
5755
5756 return 0;
5757}
5758
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005759static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Jens Axboedef596e2019-01-09 08:59:42 -07005760{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005761 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005762 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005763 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005764
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005765 timeout = io_prep_linked_timeout(req);
5766 if (timeout)
5767 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005768
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005769 /* if NO_CANCEL is set, we must still run the work */
5770 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5771 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005772 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005773 }
Jens Axboe31b51512019-01-18 22:56:34 -07005774
Jens Axboe561fb042019-10-24 07:25:42 -06005775 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005776 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005777 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005778 /*
5779 * We can get EAGAIN for polled IO even though we're
5780 * forcing a sync submission from here, since we can't
5781 * wait for request slots on the block side.
5782 */
5783 if (ret != -EAGAIN)
5784 break;
5785 cond_resched();
5786 } while (1);
5787 }
Jens Axboe31b51512019-01-18 22:56:34 -07005788
Jens Axboe561fb042019-10-24 07:25:42 -06005789 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005790 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005791 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005792 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005793
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005794 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005795}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005796
Jens Axboe65e19f52019-10-26 07:20:21 -06005797static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5798 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005799{
Jens Axboe65e19f52019-10-26 07:20:21 -06005800 struct fixed_file_table *table;
5801
Jens Axboe05f3fb32019-12-09 11:22:50 -07005802 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005803 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005804}
5805
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005806static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5807 int fd, struct file **out_file, bool fixed)
5808{
5809 struct io_ring_ctx *ctx = req->ctx;
5810 struct file *file;
5811
5812 if (fixed) {
5813 if (unlikely(!ctx->file_data ||
5814 (unsigned) fd >= ctx->nr_user_files))
5815 return -EBADF;
5816 fd = array_index_nospec(fd, ctx->nr_user_files);
5817 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005818 if (file) {
5819 req->fixed_file_refs = ctx->file_data->cur_refs;
5820 percpu_ref_get(req->fixed_file_refs);
5821 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005822 } else {
5823 trace_io_uring_file_get(ctx, fd);
5824 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005825 }
5826
Jens Axboefd2206e2020-06-02 16:40:47 -06005827 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5828 *out_file = file;
5829 return 0;
5830 }
5831 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005832}
5833
Jens Axboe3529d8c2019-12-19 18:24:38 -07005834static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005835 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005836{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005837 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005838
Jens Axboe63ff8222020-05-07 14:56:15 -06005839 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005840 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005841 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005842
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005843 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005844}
5845
Jackie Liua197f662019-11-08 08:09:12 -07005846static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005847{
Jens Axboefcb323c2019-10-24 12:39:47 -06005848 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005849 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005850
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005851 io_req_init_async(req);
5852
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005853 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005854 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005855 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005856 return -EBADF;
5857
Jens Axboefcb323c2019-10-24 12:39:47 -06005858 rcu_read_lock();
5859 spin_lock_irq(&ctx->inflight_lock);
5860 /*
5861 * We use the f_ops->flush() handler to ensure that we can flush
5862 * out work accessing these files if the fd is closed. Check if
5863 * the fd has changed since we started down this path, and disallow
5864 * this operation if it has.
5865 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005866 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005867 list_add(&req->inflight_entry, &ctx->inflight_list);
5868 req->flags |= REQ_F_INFLIGHT;
5869 req->work.files = current->files;
5870 ret = 0;
5871 }
5872 spin_unlock_irq(&ctx->inflight_lock);
5873 rcu_read_unlock();
5874
5875 return ret;
5876}
5877
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005878static inline int io_prep_work_files(struct io_kiocb *req)
5879{
5880 if (!io_op_defs[req->opcode].file_table)
5881 return 0;
5882 return io_grab_files(req);
5883}
5884
Jens Axboe2665abf2019-11-05 12:40:47 -07005885static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5886{
Jens Axboead8a48a2019-11-15 08:49:11 -07005887 struct io_timeout_data *data = container_of(timer,
5888 struct io_timeout_data, timer);
5889 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005890 struct io_ring_ctx *ctx = req->ctx;
5891 struct io_kiocb *prev = NULL;
5892 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005893
5894 spin_lock_irqsave(&ctx->completion_lock, flags);
5895
5896 /*
5897 * We don't expect the list to be empty, that will only happen if we
5898 * race with the completion of the linked work.
5899 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005900 if (!list_empty(&req->link_list)) {
5901 prev = list_entry(req->link_list.prev, struct io_kiocb,
5902 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005903 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005904 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005905 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5906 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005907 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005908 }
5909
5910 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5911
5912 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005913 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005914 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005915 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005916 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06005917 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07005918 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005919 return HRTIMER_NORESTART;
5920}
5921
Jens Axboead8a48a2019-11-15 08:49:11 -07005922static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005923{
Jens Axboe76a46e02019-11-10 23:34:16 -07005924 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005925
Jens Axboe76a46e02019-11-10 23:34:16 -07005926 /*
5927 * If the list is now empty, then our linked request finished before
5928 * we got a chance to setup the timer
5929 */
5930 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005931 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005932 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005933
Jens Axboead8a48a2019-11-15 08:49:11 -07005934 data->timer.function = io_link_timeout_fn;
5935 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5936 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005937 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005938 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005939
Jens Axboe2665abf2019-11-05 12:40:47 -07005940 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005941 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005942}
5943
Jens Axboead8a48a2019-11-15 08:49:11 -07005944static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005945{
5946 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005947
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005948 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005949 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005950 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07005951 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005952
Pavel Begunkov44932332019-12-05 16:16:35 +03005953 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5954 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005955 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005956 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005957
Jens Axboe76a46e02019-11-10 23:34:16 -07005958 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005959 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005960}
5961
Jens Axboef13fad72020-06-22 09:34:30 -06005962static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5963 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005964{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005965 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005966 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005967 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005968 int ret;
5969
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005970again:
5971 linked_timeout = io_prep_linked_timeout(req);
5972
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005973 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5974 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005975 if (old_creds)
5976 revert_creds(old_creds);
5977 if (old_creds == req->work.creds)
5978 old_creds = NULL; /* restored original creds */
5979 else
5980 old_creds = override_creds(req->work.creds);
5981 }
5982
Jens Axboef13fad72020-06-22 09:34:30 -06005983 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06005984
5985 /*
5986 * We async punt it if the file wasn't marked NOWAIT, or if the file
5987 * doesn't support non-blocking read/write attempts
5988 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03005989 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03005990 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005991punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03005992 ret = io_prep_work_files(req);
5993 if (unlikely(ret))
5994 goto err;
5995 /*
5996 * Queued up for async execution, worker will release
5997 * submit reference when the iocb is actually submitted.
5998 */
5999 io_queue_async_work(req);
6000 }
6001
6002 if (linked_timeout)
6003 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006004 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006005 }
Jens Axboee65ef562019-03-12 10:16:44 -06006006
Pavel Begunkov652532a2020-07-03 22:15:07 +03006007 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006008err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006009 /* un-prep timeout, so it'll be killed as any other linked */
6010 req->flags &= ~REQ_F_LINK_TIMEOUT;
6011 req_set_fail_links(req);
6012 io_put_req(req);
6013 io_req_complete(req, ret);
6014 goto exit;
6015 }
6016
Jens Axboee65ef562019-03-12 10:16:44 -06006017 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006018 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006019 if (linked_timeout)
6020 io_queue_linked_timeout(linked_timeout);
Jens Axboee65ef562019-03-12 10:16:44 -06006021
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006022 if (nxt) {
6023 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006024
6025 if (req->flags & REQ_F_FORCE_ASYNC)
6026 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006027 goto again;
6028 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006029exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006030 if (old_creds)
6031 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006032}
6033
Jens Axboef13fad72020-06-22 09:34:30 -06006034static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6035 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006036{
6037 int ret;
6038
Jens Axboe3529d8c2019-12-19 18:24:38 -07006039 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006040 if (ret) {
6041 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006042fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006043 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006044 io_put_req(req);
6045 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006046 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006047 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006048 if (!req->io) {
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006049 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006050 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006051 goto fail_req;
6052 }
6053
Jens Axboece35a472019-12-17 08:04:44 -07006054 /*
6055 * Never try inline submit of IOSQE_ASYNC is set, go straight
6056 * to async execution.
6057 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006058 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006059 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6060 io_queue_async_work(req);
6061 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006062 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006063 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006064}
6065
Jens Axboef13fad72020-06-22 09:34:30 -06006066static inline void io_queue_link_head(struct io_kiocb *req,
6067 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006068{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006069 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006070 io_put_req(req);
6071 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006072 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006073 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006074}
6075
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006076static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006077 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006078{
Jackie Liua197f662019-11-08 08:09:12 -07006079 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006080 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006081
Jens Axboe9e645e112019-05-10 16:07:28 -06006082 /*
6083 * If we already have a head request, queue this one for async
6084 * submittal once the head completes. If we don't have a head but
6085 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6086 * submitted sync once the chain is complete. If none of those
6087 * conditions are true (normal request), then just queue it.
6088 */
6089 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006090 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006091
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006092 /*
6093 * Taking sequential execution of a link, draining both sides
6094 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6095 * requests in the link. So, it drains the head and the
6096 * next after the link request. The last one is done via
6097 * drain_next flag to persist the effect across calls.
6098 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006099 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006100 head->flags |= REQ_F_IO_DRAIN;
6101 ctx->drain_next = 1;
6102 }
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006103 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006104 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006105 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006106 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006107 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006108 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006109 trace_io_uring_link(ctx, req, head);
Jens Axboec40f6372020-06-25 15:39:59 -06006110 io_get_req_task(req);
Pavel Begunkov9d763772019-12-17 02:22:07 +03006111 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006112
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006113 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006114 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006115 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006116 *link = NULL;
6117 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006118 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006119 if (unlikely(ctx->drain_next)) {
6120 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006121 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006122 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006123 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006124 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006125 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006126
Pavel Begunkovdebb85f2020-06-29 19:18:42 +03006127 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006128 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006129 req->flags |= REQ_F_FAIL_LINK;
6130 *link = req;
6131 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006132 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006133 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006134 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006135
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006136 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006137}
6138
Jens Axboe9a56a232019-01-09 09:06:50 -07006139/*
6140 * Batched submission is done, ensure local IO is flushed out.
6141 */
6142static void io_submit_state_end(struct io_submit_state *state)
6143{
Jens Axboef13fad72020-06-22 09:34:30 -06006144 if (!list_empty(&state->comp.list))
6145 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006146 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006147 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006148 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006149 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006150}
6151
6152/*
6153 * Start submission side cache.
6154 */
6155static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006156 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006157{
6158 blk_start_plug(&state->plug);
Jens Axboeb63534c2020-06-04 11:28:00 -06006159#ifdef CONFIG_BLOCK
6160 state->plug.nowait = true;
6161#endif
Jens Axboe013538b2020-06-22 09:29:15 -06006162 state->comp.nr = 0;
6163 INIT_LIST_HEAD(&state->comp.list);
6164 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006165 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006166 state->file = NULL;
6167 state->ios_left = max_ios;
6168}
6169
Jens Axboe2b188cc2019-01-07 10:46:33 -07006170static void io_commit_sqring(struct io_ring_ctx *ctx)
6171{
Hristo Venev75b28af2019-08-26 17:23:46 +00006172 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006173
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006174 /*
6175 * Ensure any loads from the SQEs are done at this point,
6176 * since once we write the new head, the application could
6177 * write new data to them.
6178 */
6179 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006180}
6181
6182/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006183 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006184 * that is mapped by userspace. This means that care needs to be taken to
6185 * ensure that reads are stable, as we cannot rely on userspace always
6186 * being a good citizen. If members of the sqe are validated and then later
6187 * used, it's important that those reads are done through READ_ONCE() to
6188 * prevent a re-load down the line.
6189 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006190static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006191{
Hristo Venev75b28af2019-08-26 17:23:46 +00006192 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006193 unsigned head;
6194
6195 /*
6196 * The cached sq head (or cq tail) serves two purposes:
6197 *
6198 * 1) allows us to batch the cost of updating the user visible
6199 * head updates.
6200 * 2) allows the kernel side to track the head on its own, even
6201 * though the application is the one updating it.
6202 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006203 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006204 if (likely(head < ctx->sq_entries))
6205 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006206
6207 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006208 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006209 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006210 return NULL;
6211}
6212
6213static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6214{
6215 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006216}
6217
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006218#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6219 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6220 IOSQE_BUFFER_SELECT)
6221
6222static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6223 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006224 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006225{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006226 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006227 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006228
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006229 req->opcode = READ_ONCE(sqe->opcode);
6230 req->user_data = READ_ONCE(sqe->user_data);
6231 req->io = NULL;
6232 req->file = NULL;
6233 req->ctx = ctx;
6234 req->flags = 0;
6235 /* one is dropped after submission, the other at completion */
6236 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006237 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006238 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006239
6240 if (unlikely(req->opcode >= IORING_OP_LAST))
6241 return -EINVAL;
6242
Jens Axboe9d8426a2020-06-16 18:42:49 -06006243 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6244 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006245
6246 sqe_flags = READ_ONCE(sqe->flags);
6247 /* enforce forwards compatibility on users */
6248 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6249 return -EINVAL;
6250
6251 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6252 !io_op_defs[req->opcode].buffer_select)
6253 return -EOPNOTSUPP;
6254
6255 id = READ_ONCE(sqe->personality);
6256 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006257 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006258 req->work.creds = idr_find(&ctx->personality_idr, id);
6259 if (unlikely(!req->work.creds))
6260 return -EINVAL;
6261 get_cred(req->work.creds);
6262 }
6263
6264 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006265 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006266
Jens Axboe63ff8222020-05-07 14:56:15 -06006267 if (!io_op_defs[req->opcode].needs_file)
6268 return 0;
6269
6270 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006271}
6272
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006273static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006274 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006275{
Jens Axboeac8691c2020-06-01 08:30:41 -06006276 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006277 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006278 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006279
Jens Axboec4a2ed72019-11-21 21:01:26 -07006280 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006281 if (test_bit(0, &ctx->sq_check_overflow)) {
6282 if (!list_empty(&ctx->cq_overflow_list) &&
6283 !io_cqring_overflow_flush(ctx, false))
6284 return -EBUSY;
6285 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006286
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006287 /* make sure SQ entry isn't read before tail */
6288 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006289
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006290 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6291 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006292
Jens Axboe013538b2020-06-22 09:29:15 -06006293 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006294
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006295 ctx->ring_fd = ring_fd;
6296 ctx->ring_file = ring_file;
6297
Jens Axboe6c271ce2019-01-10 11:22:30 -07006298 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006299 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006300 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006301 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006302
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006303 sqe = io_get_sqe(ctx);
6304 if (unlikely(!sqe)) {
6305 io_consume_sqe(ctx);
6306 break;
6307 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006308 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006309 if (unlikely(!req)) {
6310 if (!submitted)
6311 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006312 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006313 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006314
Jens Axboeac8691c2020-06-01 08:30:41 -06006315 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006316 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006317 /* will complete beyond this point, count as submitted */
6318 submitted++;
6319
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006320 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006321fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006322 io_put_req(req);
6323 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006324 break;
6325 }
6326
Jens Axboe354420f2020-01-08 18:55:15 -07006327 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006328 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006329 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006330 if (err)
6331 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006332 }
6333
Pavel Begunkov9466f432020-01-25 22:34:01 +03006334 if (unlikely(submitted != nr)) {
6335 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6336
6337 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6338 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006339 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006340 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006341 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006342
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006343 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6344 io_commit_sqring(ctx);
6345
Jens Axboe6c271ce2019-01-10 11:22:30 -07006346 return submitted;
6347}
6348
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006349static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6350{
6351 /* Tell userspace we may need a wakeup call */
6352 spin_lock_irq(&ctx->completion_lock);
6353 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6354 spin_unlock_irq(&ctx->completion_lock);
6355}
6356
6357static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6358{
6359 spin_lock_irq(&ctx->completion_lock);
6360 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6361 spin_unlock_irq(&ctx->completion_lock);
6362}
6363
Jens Axboe6c271ce2019-01-10 11:22:30 -07006364static int io_sq_thread(void *data)
6365{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006366 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006367 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006368 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006369 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006370 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006371
Jens Axboe0f158b42020-05-14 17:18:39 -06006372 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006373
Jens Axboe181e4482019-11-25 08:52:30 -07006374 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006375
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006376 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006377 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006378 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006379
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006380 if (!list_empty(&ctx->iopoll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006381 unsigned nr_events = 0;
6382
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006383 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006384 if (!list_empty(&ctx->iopoll_list) && !need_resched())
Pavel Begunkov9dedd562020-07-07 16:36:20 +03006385 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006386 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006387 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006388 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006389 }
6390
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006391 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006392
6393 /*
6394 * If submit got -EBUSY, flag us as needing the application
6395 * to enter the kernel to reap and flush events.
6396 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006397 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006398 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006399 * Drop cur_mm before scheduling, we can't hold it for
6400 * long periods (or over schedule()). Do this before
6401 * adding ourselves to the waitqueue, as the unuse/drop
6402 * may sleep.
6403 */
Jens Axboe4349f302020-07-09 15:07:01 -06006404 io_sq_thread_drop_mm();
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006405
6406 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006407 * We're polling. If we're within the defined idle
6408 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006409 * to sleep. The exception is if we got EBUSY doing
6410 * more IO, we should wait for the application to
6411 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006412 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006413 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006414 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6415 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06006416 io_run_task_work();
Jens Axboe9831a902019-09-19 09:48:55 -06006417 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006418 continue;
6419 }
6420
Jens Axboe6c271ce2019-01-10 11:22:30 -07006421 prepare_to_wait(&ctx->sqo_wait, &wait,
6422 TASK_INTERRUPTIBLE);
6423
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006424 /*
6425 * While doing polled IO, before going to sleep, we need
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006426 * to check if there are new reqs added to iopoll_list,
6427 * it is because reqs may have been punted to io worker
6428 * and will be added to iopoll_list later, hence check
6429 * the iopoll_list again.
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006430 */
6431 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006432 !list_empty_careful(&ctx->iopoll_list)) {
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006433 finish_wait(&ctx->sqo_wait, &wait);
6434 continue;
6435 }
6436
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006437 io_ring_set_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006438
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006439 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006440 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006441 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006442 finish_wait(&ctx->sqo_wait, &wait);
6443 break;
6444 }
Jens Axboe4c6e2772020-07-01 11:29:10 -06006445 if (io_run_task_work()) {
Hillf Danton10bea962020-04-01 17:19:33 +08006446 finish_wait(&ctx->sqo_wait, &wait);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006447 io_ring_clear_wakeup_flag(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07006448 continue;
6449 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006450 if (signal_pending(current))
6451 flush_signals(current);
6452 schedule();
6453 finish_wait(&ctx->sqo_wait, &wait);
6454
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006455 io_ring_clear_wakeup_flag(ctx);
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006456 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006457 continue;
6458 }
6459 finish_wait(&ctx->sqo_wait, &wait);
6460
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006461 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006462 }
6463
Jens Axboe8a4955f2019-12-09 14:52:35 -07006464 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006465 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6466 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006467 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006468 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006469 }
6470
Jens Axboe4c6e2772020-07-01 11:29:10 -06006471 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006472
Jens Axboe4349f302020-07-09 15:07:01 -06006473 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006474 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006475
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006476 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006477
Jens Axboe6c271ce2019-01-10 11:22:30 -07006478 return 0;
6479}
6480
Jens Axboebda52162019-09-24 13:47:15 -06006481struct io_wait_queue {
6482 struct wait_queue_entry wq;
6483 struct io_ring_ctx *ctx;
6484 unsigned to_wait;
6485 unsigned nr_timeouts;
6486};
6487
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006488static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006489{
6490 struct io_ring_ctx *ctx = iowq->ctx;
6491
6492 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006493 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006494 * started waiting. For timeouts, we always want to return to userspace,
6495 * regardless of event count.
6496 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006497 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006498 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6499}
6500
6501static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6502 int wake_flags, void *key)
6503{
6504 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6505 wq);
6506
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006507 /* use noflush == true, as we can't safely rely on locking context */
6508 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006509 return -1;
6510
6511 return autoremove_wake_function(curr, mode, wake_flags, key);
6512}
6513
Jens Axboe2b188cc2019-01-07 10:46:33 -07006514/*
6515 * Wait until events become available, if we don't already have some. The
6516 * application must reap them itself, as they reside on the shared cq ring.
6517 */
6518static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6519 const sigset_t __user *sig, size_t sigsz)
6520{
Jens Axboebda52162019-09-24 13:47:15 -06006521 struct io_wait_queue iowq = {
6522 .wq = {
6523 .private = current,
6524 .func = io_wake_function,
6525 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6526 },
6527 .ctx = ctx,
6528 .to_wait = min_events,
6529 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006530 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006531 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006532
Jens Axboeb41e9852020-02-17 09:52:41 -07006533 do {
6534 if (io_cqring_events(ctx, false) >= min_events)
6535 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006536 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006537 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006538 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006539
6540 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006541#ifdef CONFIG_COMPAT
6542 if (in_compat_syscall())
6543 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006544 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006545 else
6546#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006547 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006548
Jens Axboe2b188cc2019-01-07 10:46:33 -07006549 if (ret)
6550 return ret;
6551 }
6552
Jens Axboebda52162019-09-24 13:47:15 -06006553 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006554 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006555 do {
6556 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6557 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006558 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006559 if (io_run_task_work())
6560 continue;
Jens Axboebda52162019-09-24 13:47:15 -06006561 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006562 if (current->jobctl & JOBCTL_TASK_WORK) {
6563 spin_lock_irq(&current->sighand->siglock);
6564 current->jobctl &= ~JOBCTL_TASK_WORK;
6565 recalc_sigpending();
6566 spin_unlock_irq(&current->sighand->siglock);
6567 continue;
6568 }
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006569 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006570 break;
6571 }
Jens Axboebda52162019-09-24 13:47:15 -06006572 if (io_should_wake(&iowq, false))
6573 break;
6574 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006575 } while (1);
6576 finish_wait(&ctx->wait, &iowq.wq);
6577
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006578 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006579
Hristo Venev75b28af2019-08-26 17:23:46 +00006580 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006581}
6582
Jens Axboe6b063142019-01-10 22:13:58 -07006583static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6584{
6585#if defined(CONFIG_UNIX)
6586 if (ctx->ring_sock) {
6587 struct sock *sock = ctx->ring_sock->sk;
6588 struct sk_buff *skb;
6589
6590 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6591 kfree_skb(skb);
6592 }
6593#else
6594 int i;
6595
Jens Axboe65e19f52019-10-26 07:20:21 -06006596 for (i = 0; i < ctx->nr_user_files; i++) {
6597 struct file *file;
6598
6599 file = io_file_from_index(ctx, i);
6600 if (file)
6601 fput(file);
6602 }
Jens Axboe6b063142019-01-10 22:13:58 -07006603#endif
6604}
6605
Jens Axboe05f3fb32019-12-09 11:22:50 -07006606static void io_file_ref_kill(struct percpu_ref *ref)
6607{
6608 struct fixed_file_data *data;
6609
6610 data = container_of(ref, struct fixed_file_data, refs);
6611 complete(&data->done);
6612}
6613
Jens Axboe6b063142019-01-10 22:13:58 -07006614static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6615{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006616 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006617 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006618 unsigned nr_tables, i;
6619
Jens Axboe05f3fb32019-12-09 11:22:50 -07006620 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006621 return -ENXIO;
6622
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006623 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006624 if (!list_empty(&data->ref_list))
6625 ref_node = list_first_entry(&data->ref_list,
6626 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006627 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006628 if (ref_node)
6629 percpu_ref_kill(&ref_node->refs);
6630
6631 percpu_ref_kill(&data->refs);
6632
6633 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006634 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006635 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006636
Jens Axboe6b063142019-01-10 22:13:58 -07006637 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006638 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6639 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006640 kfree(data->table[i].files);
6641 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006642 percpu_ref_exit(&data->refs);
6643 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006644 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006645 ctx->nr_user_files = 0;
6646 return 0;
6647}
6648
Jens Axboe6c271ce2019-01-10 11:22:30 -07006649static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6650{
6651 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006652 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006653 /*
6654 * The park is a bit of a work-around, without it we get
6655 * warning spews on shutdown with SQPOLL set and affinity
6656 * set to a single CPU.
6657 */
Jens Axboe06058632019-04-13 09:26:03 -06006658 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006659 kthread_stop(ctx->sqo_thread);
6660 ctx->sqo_thread = NULL;
6661 }
6662}
6663
Jens Axboe6b063142019-01-10 22:13:58 -07006664static void io_finish_async(struct io_ring_ctx *ctx)
6665{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006666 io_sq_thread_stop(ctx);
6667
Jens Axboe561fb042019-10-24 07:25:42 -06006668 if (ctx->io_wq) {
6669 io_wq_destroy(ctx->io_wq);
6670 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006671 }
6672}
6673
6674#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006675/*
6676 * Ensure the UNIX gc is aware of our file set, so we are certain that
6677 * the io_uring can be safely unregistered on process exit, even if we have
6678 * loops in the file referencing.
6679 */
6680static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6681{
6682 struct sock *sk = ctx->ring_sock->sk;
6683 struct scm_fp_list *fpl;
6684 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006685 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006686
Jens Axboe6b063142019-01-10 22:13:58 -07006687 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6688 if (!fpl)
6689 return -ENOMEM;
6690
6691 skb = alloc_skb(0, GFP_KERNEL);
6692 if (!skb) {
6693 kfree(fpl);
6694 return -ENOMEM;
6695 }
6696
6697 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006698
Jens Axboe08a45172019-10-03 08:11:03 -06006699 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006700 fpl->user = get_uid(ctx->user);
6701 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006702 struct file *file = io_file_from_index(ctx, i + offset);
6703
6704 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006705 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006706 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006707 unix_inflight(fpl->user, fpl->fp[nr_files]);
6708 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006709 }
6710
Jens Axboe08a45172019-10-03 08:11:03 -06006711 if (nr_files) {
6712 fpl->max = SCM_MAX_FD;
6713 fpl->count = nr_files;
6714 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006715 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006716 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6717 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006718
Jens Axboe08a45172019-10-03 08:11:03 -06006719 for (i = 0; i < nr_files; i++)
6720 fput(fpl->fp[i]);
6721 } else {
6722 kfree_skb(skb);
6723 kfree(fpl);
6724 }
Jens Axboe6b063142019-01-10 22:13:58 -07006725
6726 return 0;
6727}
6728
6729/*
6730 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6731 * causes regular reference counting to break down. We rely on the UNIX
6732 * garbage collection to take care of this problem for us.
6733 */
6734static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6735{
6736 unsigned left, total;
6737 int ret = 0;
6738
6739 total = 0;
6740 left = ctx->nr_user_files;
6741 while (left) {
6742 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006743
6744 ret = __io_sqe_files_scm(ctx, this_files, total);
6745 if (ret)
6746 break;
6747 left -= this_files;
6748 total += this_files;
6749 }
6750
6751 if (!ret)
6752 return 0;
6753
6754 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006755 struct file *file = io_file_from_index(ctx, total);
6756
6757 if (file)
6758 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006759 total++;
6760 }
6761
6762 return ret;
6763}
6764#else
6765static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6766{
6767 return 0;
6768}
6769#endif
6770
Jens Axboe65e19f52019-10-26 07:20:21 -06006771static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6772 unsigned nr_files)
6773{
6774 int i;
6775
6776 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006777 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006778 unsigned this_files;
6779
6780 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6781 table->files = kcalloc(this_files, sizeof(struct file *),
6782 GFP_KERNEL);
6783 if (!table->files)
6784 break;
6785 nr_files -= this_files;
6786 }
6787
6788 if (i == nr_tables)
6789 return 0;
6790
6791 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006792 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006793 kfree(table->files);
6794 }
6795 return 1;
6796}
6797
Jens Axboe05f3fb32019-12-09 11:22:50 -07006798static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006799{
6800#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006801 struct sock *sock = ctx->ring_sock->sk;
6802 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6803 struct sk_buff *skb;
6804 int i;
6805
6806 __skb_queue_head_init(&list);
6807
6808 /*
6809 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6810 * remove this entry and rearrange the file array.
6811 */
6812 skb = skb_dequeue(head);
6813 while (skb) {
6814 struct scm_fp_list *fp;
6815
6816 fp = UNIXCB(skb).fp;
6817 for (i = 0; i < fp->count; i++) {
6818 int left;
6819
6820 if (fp->fp[i] != file)
6821 continue;
6822
6823 unix_notinflight(fp->user, fp->fp[i]);
6824 left = fp->count - 1 - i;
6825 if (left) {
6826 memmove(&fp->fp[i], &fp->fp[i + 1],
6827 left * sizeof(struct file *));
6828 }
6829 fp->count--;
6830 if (!fp->count) {
6831 kfree_skb(skb);
6832 skb = NULL;
6833 } else {
6834 __skb_queue_tail(&list, skb);
6835 }
6836 fput(file);
6837 file = NULL;
6838 break;
6839 }
6840
6841 if (!file)
6842 break;
6843
6844 __skb_queue_tail(&list, skb);
6845
6846 skb = skb_dequeue(head);
6847 }
6848
6849 if (skb_peek(&list)) {
6850 spin_lock_irq(&head->lock);
6851 while ((skb = __skb_dequeue(&list)) != NULL)
6852 __skb_queue_tail(head, skb);
6853 spin_unlock_irq(&head->lock);
6854 }
6855#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006856 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006857#endif
6858}
6859
Jens Axboe05f3fb32019-12-09 11:22:50 -07006860struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006861 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006862 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006863};
6864
Jens Axboe4a38aed22020-05-14 17:21:15 -06006865static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006866{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006867 struct fixed_file_data *file_data = ref_node->file_data;
6868 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006869 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006870
6871 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006872 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006873 io_ring_file_put(ctx, pfile->file);
6874 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006875 }
6876
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006877 spin_lock(&file_data->lock);
6878 list_del(&ref_node->node);
6879 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006880
Xiaoguang Wang05589552020-03-31 14:05:18 +08006881 percpu_ref_exit(&ref_node->refs);
6882 kfree(ref_node);
6883 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006884}
6885
Jens Axboe4a38aed22020-05-14 17:21:15 -06006886static void io_file_put_work(struct work_struct *work)
6887{
6888 struct io_ring_ctx *ctx;
6889 struct llist_node *node;
6890
6891 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6892 node = llist_del_all(&ctx->file_put_llist);
6893
6894 while (node) {
6895 struct fixed_file_ref_node *ref_node;
6896 struct llist_node *next = node->next;
6897
6898 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6899 __io_file_put_work(ref_node);
6900 node = next;
6901 }
6902}
6903
Jens Axboe05f3fb32019-12-09 11:22:50 -07006904static void io_file_data_ref_zero(struct percpu_ref *ref)
6905{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006906 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006907 struct io_ring_ctx *ctx;
6908 bool first_add;
6909 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006910
Xiaoguang Wang05589552020-03-31 14:05:18 +08006911 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006912 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006913
Jens Axboe4a38aed22020-05-14 17:21:15 -06006914 if (percpu_ref_is_dying(&ctx->file_data->refs))
6915 delay = 0;
6916
6917 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6918 if (!delay)
6919 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6920 else if (first_add)
6921 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006922}
6923
6924static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6925 struct io_ring_ctx *ctx)
6926{
6927 struct fixed_file_ref_node *ref_node;
6928
6929 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6930 if (!ref_node)
6931 return ERR_PTR(-ENOMEM);
6932
6933 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6934 0, GFP_KERNEL)) {
6935 kfree(ref_node);
6936 return ERR_PTR(-ENOMEM);
6937 }
6938 INIT_LIST_HEAD(&ref_node->node);
6939 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006940 ref_node->file_data = ctx->file_data;
6941 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006942}
6943
6944static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6945{
6946 percpu_ref_exit(&ref_node->refs);
6947 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006948}
6949
6950static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6951 unsigned nr_args)
6952{
6953 __s32 __user *fds = (__s32 __user *) arg;
6954 unsigned nr_tables;
6955 struct file *file;
6956 int fd, ret = 0;
6957 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006958 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006959
6960 if (ctx->file_data)
6961 return -EBUSY;
6962 if (!nr_args)
6963 return -EINVAL;
6964 if (nr_args > IORING_MAX_FIXED_FILES)
6965 return -EMFILE;
6966
6967 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6968 if (!ctx->file_data)
6969 return -ENOMEM;
6970 ctx->file_data->ctx = ctx;
6971 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006972 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006973 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006974
6975 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6976 ctx->file_data->table = kcalloc(nr_tables,
6977 sizeof(struct fixed_file_table),
6978 GFP_KERNEL);
6979 if (!ctx->file_data->table) {
6980 kfree(ctx->file_data);
6981 ctx->file_data = NULL;
6982 return -ENOMEM;
6983 }
6984
Xiaoguang Wang05589552020-03-31 14:05:18 +08006985 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006986 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6987 kfree(ctx->file_data->table);
6988 kfree(ctx->file_data);
6989 ctx->file_data = NULL;
6990 return -ENOMEM;
6991 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006992
6993 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6994 percpu_ref_exit(&ctx->file_data->refs);
6995 kfree(ctx->file_data->table);
6996 kfree(ctx->file_data);
6997 ctx->file_data = NULL;
6998 return -ENOMEM;
6999 }
7000
7001 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7002 struct fixed_file_table *table;
7003 unsigned index;
7004
7005 ret = -EFAULT;
7006 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7007 break;
7008 /* allow sparse sets */
7009 if (fd == -1) {
7010 ret = 0;
7011 continue;
7012 }
7013
7014 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7015 index = i & IORING_FILE_TABLE_MASK;
7016 file = fget(fd);
7017
7018 ret = -EBADF;
7019 if (!file)
7020 break;
7021
7022 /*
7023 * Don't allow io_uring instances to be registered. If UNIX
7024 * isn't enabled, then this causes a reference cycle and this
7025 * instance can never get freed. If UNIX is enabled we'll
7026 * handle it just fine, but there's still no point in allowing
7027 * a ring fd as it doesn't support regular read/write anyway.
7028 */
7029 if (file->f_op == &io_uring_fops) {
7030 fput(file);
7031 break;
7032 }
7033 ret = 0;
7034 table->files[index] = file;
7035 }
7036
7037 if (ret) {
7038 for (i = 0; i < ctx->nr_user_files; i++) {
7039 file = io_file_from_index(ctx, i);
7040 if (file)
7041 fput(file);
7042 }
7043 for (i = 0; i < nr_tables; i++)
7044 kfree(ctx->file_data->table[i].files);
7045
Yang Yingliang667e57d2020-07-10 14:14:20 +00007046 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007047 kfree(ctx->file_data->table);
7048 kfree(ctx->file_data);
7049 ctx->file_data = NULL;
7050 ctx->nr_user_files = 0;
7051 return ret;
7052 }
7053
7054 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007055 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007056 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007057 return ret;
7058 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007059
Xiaoguang Wang05589552020-03-31 14:05:18 +08007060 ref_node = alloc_fixed_file_ref_node(ctx);
7061 if (IS_ERR(ref_node)) {
7062 io_sqe_files_unregister(ctx);
7063 return PTR_ERR(ref_node);
7064 }
7065
7066 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007067 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007068 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007069 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007070 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007071 return ret;
7072}
7073
Jens Axboec3a31e62019-10-03 13:59:56 -06007074static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7075 int index)
7076{
7077#if defined(CONFIG_UNIX)
7078 struct sock *sock = ctx->ring_sock->sk;
7079 struct sk_buff_head *head = &sock->sk_receive_queue;
7080 struct sk_buff *skb;
7081
7082 /*
7083 * See if we can merge this file into an existing skb SCM_RIGHTS
7084 * file set. If there's no room, fall back to allocating a new skb
7085 * and filling it in.
7086 */
7087 spin_lock_irq(&head->lock);
7088 skb = skb_peek(head);
7089 if (skb) {
7090 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7091
7092 if (fpl->count < SCM_MAX_FD) {
7093 __skb_unlink(skb, head);
7094 spin_unlock_irq(&head->lock);
7095 fpl->fp[fpl->count] = get_file(file);
7096 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7097 fpl->count++;
7098 spin_lock_irq(&head->lock);
7099 __skb_queue_head(head, skb);
7100 } else {
7101 skb = NULL;
7102 }
7103 }
7104 spin_unlock_irq(&head->lock);
7105
7106 if (skb) {
7107 fput(file);
7108 return 0;
7109 }
7110
7111 return __io_sqe_files_scm(ctx, 1, index);
7112#else
7113 return 0;
7114#endif
7115}
7116
Hillf Dantona5318d32020-03-23 17:47:15 +08007117static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007118 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007119{
Hillf Dantona5318d32020-03-23 17:47:15 +08007120 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007121 struct percpu_ref *refs = data->cur_refs;
7122 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007123
Jens Axboe05f3fb32019-12-09 11:22:50 -07007124 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007125 if (!pfile)
7126 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007127
Xiaoguang Wang05589552020-03-31 14:05:18 +08007128 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007129 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007130 list_add(&pfile->list, &ref_node->file_list);
7131
Hillf Dantona5318d32020-03-23 17:47:15 +08007132 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007133}
7134
7135static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7136 struct io_uring_files_update *up,
7137 unsigned nr_args)
7138{
7139 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007140 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007141 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007142 __s32 __user *fds;
7143 int fd, i, err;
7144 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007145 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007146
Jens Axboe05f3fb32019-12-09 11:22:50 -07007147 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007148 return -EOVERFLOW;
7149 if (done > ctx->nr_user_files)
7150 return -EINVAL;
7151
Xiaoguang Wang05589552020-03-31 14:05:18 +08007152 ref_node = alloc_fixed_file_ref_node(ctx);
7153 if (IS_ERR(ref_node))
7154 return PTR_ERR(ref_node);
7155
Jens Axboec3a31e62019-10-03 13:59:56 -06007156 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007157 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007158 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007159 struct fixed_file_table *table;
7160 unsigned index;
7161
Jens Axboec3a31e62019-10-03 13:59:56 -06007162 err = 0;
7163 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7164 err = -EFAULT;
7165 break;
7166 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007167 i = array_index_nospec(up->offset, ctx->nr_user_files);
7168 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007169 index = i & IORING_FILE_TABLE_MASK;
7170 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007171 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08007172 err = io_queue_file_removal(data, file);
7173 if (err)
7174 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007175 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007176 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007177 }
7178 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007179 file = fget(fd);
7180 if (!file) {
7181 err = -EBADF;
7182 break;
7183 }
7184 /*
7185 * Don't allow io_uring instances to be registered. If
7186 * UNIX isn't enabled, then this causes a reference
7187 * cycle and this instance can never get freed. If UNIX
7188 * is enabled we'll handle it just fine, but there's
7189 * still no point in allowing a ring fd as it doesn't
7190 * support regular read/write anyway.
7191 */
7192 if (file->f_op == &io_uring_fops) {
7193 fput(file);
7194 err = -EBADF;
7195 break;
7196 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007197 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007198 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007199 if (err) {
7200 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007201 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007202 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007203 }
7204 nr_args--;
7205 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007206 up->offset++;
7207 }
7208
Xiaoguang Wang05589552020-03-31 14:05:18 +08007209 if (needs_switch) {
7210 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007211 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007212 list_add(&ref_node->node, &data->ref_list);
7213 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007214 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007215 percpu_ref_get(&ctx->file_data->refs);
7216 } else
7217 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007218
7219 return done ? done : err;
7220}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007221
Jens Axboe05f3fb32019-12-09 11:22:50 -07007222static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7223 unsigned nr_args)
7224{
7225 struct io_uring_files_update up;
7226
7227 if (!ctx->file_data)
7228 return -ENXIO;
7229 if (!nr_args)
7230 return -EINVAL;
7231 if (copy_from_user(&up, arg, sizeof(up)))
7232 return -EFAULT;
7233 if (up.resv)
7234 return -EINVAL;
7235
7236 return __io_sqe_files_update(ctx, &up, nr_args);
7237}
Jens Axboec3a31e62019-10-03 13:59:56 -06007238
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007239static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007240{
7241 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7242
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007243 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007244 io_put_req(req);
7245}
7246
Pavel Begunkov24369c22020-01-28 03:15:48 +03007247static int io_init_wq_offload(struct io_ring_ctx *ctx,
7248 struct io_uring_params *p)
7249{
7250 struct io_wq_data data;
7251 struct fd f;
7252 struct io_ring_ctx *ctx_attach;
7253 unsigned int concurrency;
7254 int ret = 0;
7255
7256 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007257 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007258 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007259
7260 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7261 /* Do QD, or 4 * CPUS, whatever is smallest */
7262 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7263
7264 ctx->io_wq = io_wq_create(concurrency, &data);
7265 if (IS_ERR(ctx->io_wq)) {
7266 ret = PTR_ERR(ctx->io_wq);
7267 ctx->io_wq = NULL;
7268 }
7269 return ret;
7270 }
7271
7272 f = fdget(p->wq_fd);
7273 if (!f.file)
7274 return -EBADF;
7275
7276 if (f.file->f_op != &io_uring_fops) {
7277 ret = -EINVAL;
7278 goto out_fput;
7279 }
7280
7281 ctx_attach = f.file->private_data;
7282 /* @io_wq is protected by holding the fd */
7283 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7284 ret = -EINVAL;
7285 goto out_fput;
7286 }
7287
7288 ctx->io_wq = ctx_attach->io_wq;
7289out_fput:
7290 fdput(f);
7291 return ret;
7292}
7293
Jens Axboe6c271ce2019-01-10 11:22:30 -07007294static int io_sq_offload_start(struct io_ring_ctx *ctx,
7295 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007296{
7297 int ret;
7298
Pavel Begunkovcbcf7212020-07-18 11:31:21 +03007299 mmgrab(current->mm);
7300 ctx->sqo_mm = current->mm;
Pavel Begunkov8eb06d72020-06-30 15:20:39 +03007301
Pavel Begunkovcbcf7212020-07-18 11:31:21 +03007302 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007303 ret = -EPERM;
7304 if (!capable(CAP_SYS_ADMIN))
7305 goto err;
7306
Jens Axboe917257d2019-04-13 09:28:55 -06007307 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7308 if (!ctx->sq_thread_idle)
7309 ctx->sq_thread_idle = HZ;
7310
Jens Axboe6c271ce2019-01-10 11:22:30 -07007311 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007312 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007313
Jens Axboe917257d2019-04-13 09:28:55 -06007314 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007315 if (cpu >= nr_cpu_ids)
7316 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007317 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007318 goto err;
7319
Jens Axboe6c271ce2019-01-10 11:22:30 -07007320 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7321 ctx, cpu,
7322 "io_uring-sq");
7323 } else {
7324 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7325 "io_uring-sq");
7326 }
7327 if (IS_ERR(ctx->sqo_thread)) {
7328 ret = PTR_ERR(ctx->sqo_thread);
7329 ctx->sqo_thread = NULL;
7330 goto err;
7331 }
7332 wake_up_process(ctx->sqo_thread);
7333 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7334 /* Can't have SQ_AFF without SQPOLL */
7335 ret = -EINVAL;
7336 goto err;
7337 }
7338
Pavel Begunkov24369c22020-01-28 03:15:48 +03007339 ret = io_init_wq_offload(ctx, p);
7340 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007341 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007342
7343 return 0;
7344err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007345 io_finish_async(ctx);
Pavel Begunkov8eb06d72020-06-30 15:20:39 +03007346 if (ctx->sqo_mm) {
7347 mmdrop(ctx->sqo_mm);
7348 ctx->sqo_mm = NULL;
7349 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007350 return ret;
7351}
7352
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007353static inline void __io_unaccount_mem(struct user_struct *user,
7354 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007355{
7356 atomic_long_sub(nr_pages, &user->locked_vm);
7357}
7358
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007359static inline int __io_account_mem(struct user_struct *user,
7360 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007361{
7362 unsigned long page_limit, cur_pages, new_pages;
7363
7364 /* Don't allow more pages than we can safely lock */
7365 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7366
7367 do {
7368 cur_pages = atomic_long_read(&user->locked_vm);
7369 new_pages = cur_pages + nr_pages;
7370 if (new_pages > page_limit)
7371 return -ENOMEM;
7372 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7373 new_pages) != cur_pages);
7374
7375 return 0;
7376}
7377
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007378static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7379 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007380{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007381 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007382 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007383
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007384 if (ctx->sqo_mm) {
7385 if (acct == ACCT_LOCKED)
7386 ctx->sqo_mm->locked_vm -= nr_pages;
7387 else if (acct == ACCT_PINNED)
7388 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7389 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007390}
7391
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007392static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7393 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007394{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007395 int ret;
7396
7397 if (ctx->limit_mem) {
7398 ret = __io_account_mem(ctx->user, nr_pages);
7399 if (ret)
7400 return ret;
7401 }
7402
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007403 if (ctx->sqo_mm) {
7404 if (acct == ACCT_LOCKED)
7405 ctx->sqo_mm->locked_vm += nr_pages;
7406 else if (acct == ACCT_PINNED)
7407 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7408 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007409
7410 return 0;
7411}
7412
Jens Axboe2b188cc2019-01-07 10:46:33 -07007413static void io_mem_free(void *ptr)
7414{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007415 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007416
Mark Rutland52e04ef2019-04-30 17:30:21 +01007417 if (!ptr)
7418 return;
7419
7420 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007421 if (put_page_testzero(page))
7422 free_compound_page(page);
7423}
7424
7425static void *io_mem_alloc(size_t size)
7426{
7427 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7428 __GFP_NORETRY;
7429
7430 return (void *) __get_free_pages(gfp_flags, get_order(size));
7431}
7432
Hristo Venev75b28af2019-08-26 17:23:46 +00007433static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7434 size_t *sq_offset)
7435{
7436 struct io_rings *rings;
7437 size_t off, sq_array_size;
7438
7439 off = struct_size(rings, cqes, cq_entries);
7440 if (off == SIZE_MAX)
7441 return SIZE_MAX;
7442
7443#ifdef CONFIG_SMP
7444 off = ALIGN(off, SMP_CACHE_BYTES);
7445 if (off == 0)
7446 return SIZE_MAX;
7447#endif
7448
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007449 if (sq_offset)
7450 *sq_offset = off;
7451
Hristo Venev75b28af2019-08-26 17:23:46 +00007452 sq_array_size = array_size(sizeof(u32), sq_entries);
7453 if (sq_array_size == SIZE_MAX)
7454 return SIZE_MAX;
7455
7456 if (check_add_overflow(off, sq_array_size, &off))
7457 return SIZE_MAX;
7458
Hristo Venev75b28af2019-08-26 17:23:46 +00007459 return off;
7460}
7461
Jens Axboe2b188cc2019-01-07 10:46:33 -07007462static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7463{
Hristo Venev75b28af2019-08-26 17:23:46 +00007464 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007465
Hristo Venev75b28af2019-08-26 17:23:46 +00007466 pages = (size_t)1 << get_order(
7467 rings_size(sq_entries, cq_entries, NULL));
7468 pages += (size_t)1 << get_order(
7469 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007470
Hristo Venev75b28af2019-08-26 17:23:46 +00007471 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007472}
7473
Jens Axboeedafcce2019-01-09 09:16:05 -07007474static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7475{
7476 int i, j;
7477
7478 if (!ctx->user_bufs)
7479 return -ENXIO;
7480
7481 for (i = 0; i < ctx->nr_user_bufs; i++) {
7482 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7483
7484 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007485 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007486
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007487 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007488 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007489 imu->nr_bvecs = 0;
7490 }
7491
7492 kfree(ctx->user_bufs);
7493 ctx->user_bufs = NULL;
7494 ctx->nr_user_bufs = 0;
7495 return 0;
7496}
7497
7498static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7499 void __user *arg, unsigned index)
7500{
7501 struct iovec __user *src;
7502
7503#ifdef CONFIG_COMPAT
7504 if (ctx->compat) {
7505 struct compat_iovec __user *ciovs;
7506 struct compat_iovec ciov;
7507
7508 ciovs = (struct compat_iovec __user *) arg;
7509 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7510 return -EFAULT;
7511
Jens Axboed55e5f52019-12-11 16:12:15 -07007512 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007513 dst->iov_len = ciov.iov_len;
7514 return 0;
7515 }
7516#endif
7517 src = (struct iovec __user *) arg;
7518 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7519 return -EFAULT;
7520 return 0;
7521}
7522
7523static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7524 unsigned nr_args)
7525{
7526 struct vm_area_struct **vmas = NULL;
7527 struct page **pages = NULL;
7528 int i, j, got_pages = 0;
7529 int ret = -EINVAL;
7530
7531 if (ctx->user_bufs)
7532 return -EBUSY;
7533 if (!nr_args || nr_args > UIO_MAXIOV)
7534 return -EINVAL;
7535
7536 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7537 GFP_KERNEL);
7538 if (!ctx->user_bufs)
7539 return -ENOMEM;
7540
7541 for (i = 0; i < nr_args; i++) {
7542 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7543 unsigned long off, start, end, ubuf;
7544 int pret, nr_pages;
7545 struct iovec iov;
7546 size_t size;
7547
7548 ret = io_copy_iov(ctx, &iov, arg, i);
7549 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007550 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007551
7552 /*
7553 * Don't impose further limits on the size and buffer
7554 * constraints here, we'll -EINVAL later when IO is
7555 * submitted if they are wrong.
7556 */
7557 ret = -EFAULT;
7558 if (!iov.iov_base || !iov.iov_len)
7559 goto err;
7560
7561 /* arbitrary limit, but we need something */
7562 if (iov.iov_len > SZ_1G)
7563 goto err;
7564
7565 ubuf = (unsigned long) iov.iov_base;
7566 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7567 start = ubuf >> PAGE_SHIFT;
7568 nr_pages = end - start;
7569
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007570 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007571 if (ret)
7572 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007573
7574 ret = 0;
7575 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007576 kvfree(vmas);
7577 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007578 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007579 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007580 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007581 sizeof(struct vm_area_struct *),
7582 GFP_KERNEL);
7583 if (!pages || !vmas) {
7584 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007585 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007586 goto err;
7587 }
7588 got_pages = nr_pages;
7589 }
7590
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007591 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007592 GFP_KERNEL);
7593 ret = -ENOMEM;
7594 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007595 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007596 goto err;
7597 }
7598
7599 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007600 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007601 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007602 FOLL_WRITE | FOLL_LONGTERM,
7603 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007604 if (pret == nr_pages) {
7605 /* don't support file backed memory */
7606 for (j = 0; j < nr_pages; j++) {
7607 struct vm_area_struct *vma = vmas[j];
7608
7609 if (vma->vm_file &&
7610 !is_file_hugepages(vma->vm_file)) {
7611 ret = -EOPNOTSUPP;
7612 break;
7613 }
7614 }
7615 } else {
7616 ret = pret < 0 ? pret : -EFAULT;
7617 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007618 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007619 if (ret) {
7620 /*
7621 * if we did partial map, or found file backed vmas,
7622 * release any pages we did get
7623 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007624 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007625 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007626 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007627 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007628 goto err;
7629 }
7630
7631 off = ubuf & ~PAGE_MASK;
7632 size = iov.iov_len;
7633 for (j = 0; j < nr_pages; j++) {
7634 size_t vec_len;
7635
7636 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7637 imu->bvec[j].bv_page = pages[j];
7638 imu->bvec[j].bv_len = vec_len;
7639 imu->bvec[j].bv_offset = off;
7640 off = 0;
7641 size -= vec_len;
7642 }
7643 /* store original address for later verification */
7644 imu->ubuf = ubuf;
7645 imu->len = iov.iov_len;
7646 imu->nr_bvecs = nr_pages;
7647
7648 ctx->nr_user_bufs++;
7649 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007650 kvfree(pages);
7651 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007652 return 0;
7653err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007654 kvfree(pages);
7655 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007656 io_sqe_buffer_unregister(ctx);
7657 return ret;
7658}
7659
Jens Axboe9b402842019-04-11 11:45:41 -06007660static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7661{
7662 __s32 __user *fds = arg;
7663 int fd;
7664
7665 if (ctx->cq_ev_fd)
7666 return -EBUSY;
7667
7668 if (copy_from_user(&fd, fds, sizeof(*fds)))
7669 return -EFAULT;
7670
7671 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7672 if (IS_ERR(ctx->cq_ev_fd)) {
7673 int ret = PTR_ERR(ctx->cq_ev_fd);
7674 ctx->cq_ev_fd = NULL;
7675 return ret;
7676 }
7677
7678 return 0;
7679}
7680
7681static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7682{
7683 if (ctx->cq_ev_fd) {
7684 eventfd_ctx_put(ctx->cq_ev_fd);
7685 ctx->cq_ev_fd = NULL;
7686 return 0;
7687 }
7688
7689 return -ENXIO;
7690}
7691
Jens Axboe5a2e7452020-02-23 16:23:11 -07007692static int __io_destroy_buffers(int id, void *p, void *data)
7693{
7694 struct io_ring_ctx *ctx = data;
7695 struct io_buffer *buf = p;
7696
Jens Axboe067524e2020-03-02 16:32:28 -07007697 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007698 return 0;
7699}
7700
7701static void io_destroy_buffers(struct io_ring_ctx *ctx)
7702{
7703 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7704 idr_destroy(&ctx->io_buffer_idr);
7705}
7706
Jens Axboe2b188cc2019-01-07 10:46:33 -07007707static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7708{
Jens Axboe6b063142019-01-10 22:13:58 -07007709 io_finish_async(ctx);
Pavel Begunkov5dbcad52020-07-18 11:31:20 +03007710 io_sqe_buffer_unregister(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007711 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007712 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007713 ctx->sqo_mm = NULL;
7714 }
Jens Axboedef596e2019-01-09 08:59:42 -07007715
Jens Axboe6b063142019-01-10 22:13:58 -07007716 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007717 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007718 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007719 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007720
Jens Axboe2b188cc2019-01-07 10:46:33 -07007721#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007722 if (ctx->ring_sock) {
7723 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007724 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007725 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007726#endif
7727
Hristo Venev75b28af2019-08-26 17:23:46 +00007728 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007729 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007730
7731 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007732 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007733 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007734 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007735 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007736 kfree(ctx);
7737}
7738
7739static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7740{
7741 struct io_ring_ctx *ctx = file->private_data;
7742 __poll_t mask = 0;
7743
7744 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007745 /*
7746 * synchronizes with barrier from wq_has_sleeper call in
7747 * io_commit_cqring
7748 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007749 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007750 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7751 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007752 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007753 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007754 mask |= EPOLLIN | EPOLLRDNORM;
7755
7756 return mask;
7757}
7758
7759static int io_uring_fasync(int fd, struct file *file, int on)
7760{
7761 struct io_ring_ctx *ctx = file->private_data;
7762
7763 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7764}
7765
Jens Axboe071698e2020-01-28 10:04:42 -07007766static int io_remove_personalities(int id, void *p, void *data)
7767{
7768 struct io_ring_ctx *ctx = data;
7769 const struct cred *cred;
7770
7771 cred = idr_remove(&ctx->personality_idr, id);
7772 if (cred)
7773 put_cred(cred);
7774 return 0;
7775}
7776
Jens Axboe85faa7b2020-04-09 18:14:00 -06007777static void io_ring_exit_work(struct work_struct *work)
7778{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007779 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
7780 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007781
Jens Axboe56952e92020-06-17 15:00:04 -06007782 /*
7783 * If we're doing polled IO and end up having requests being
7784 * submitted async (out-of-line), then completions can come in while
7785 * we're waiting for refs to drop. We need to reap these manually,
7786 * as nobody else will be looking for them.
7787 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007788 do {
Jens Axboe56952e92020-06-17 15:00:04 -06007789 if (ctx->rings)
7790 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007791 io_iopoll_try_reap_events(ctx);
7792 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06007793 io_ring_ctx_free(ctx);
7794}
7795
Jens Axboe2b188cc2019-01-07 10:46:33 -07007796static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7797{
7798 mutex_lock(&ctx->uring_lock);
7799 percpu_ref_kill(&ctx->refs);
7800 mutex_unlock(&ctx->uring_lock);
7801
Jens Axboe5262f562019-09-17 12:26:57 -06007802 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007803 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007804
7805 if (ctx->io_wq)
7806 io_wq_cancel_all(ctx->io_wq);
7807
Jens Axboe15dff282019-11-13 09:09:23 -07007808 /* if we failed setting up the ctx, we might not have any rings */
7809 if (ctx->rings)
7810 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007811 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07007812 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06007813
7814 /*
7815 * Do this upfront, so we won't have a grace period where the ring
7816 * is closed but resources aren't reaped yet. This can cause
7817 * spurious failure in setting up a new ring.
7818 */
Jens Axboe760618f2020-07-24 12:53:31 -06007819 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7820 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06007821
Jens Axboe85faa7b2020-04-09 18:14:00 -06007822 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7823 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007824}
7825
7826static int io_uring_release(struct inode *inode, struct file *file)
7827{
7828 struct io_ring_ctx *ctx = file->private_data;
7829
7830 file->private_data = NULL;
7831 io_ring_ctx_wait_and_kill(ctx);
7832 return 0;
7833}
7834
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007835static bool io_wq_files_match(struct io_wq_work *work, void *data)
7836{
7837 struct files_struct *files = data;
7838
7839 return work->files == files;
7840}
7841
Jens Axboefcb323c2019-10-24 12:39:47 -06007842static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7843 struct files_struct *files)
7844{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007845 if (list_empty_careful(&ctx->inflight_list))
7846 return;
7847
7848 /* cancel all at once, should be faster than doing it one by one*/
7849 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7850
Jens Axboefcb323c2019-10-24 12:39:47 -06007851 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007852 struct io_kiocb *cancel_req = NULL, *req;
7853 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007854
7855 spin_lock_irq(&ctx->inflight_lock);
7856 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007857 if (req->work.files != files)
7858 continue;
7859 /* req is being completed, ignore */
7860 if (!refcount_inc_not_zero(&req->refs))
7861 continue;
7862 cancel_req = req;
7863 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007864 }
Jens Axboe768134d2019-11-10 20:30:53 -07007865 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007866 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007867 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007868 spin_unlock_irq(&ctx->inflight_lock);
7869
Jens Axboe768134d2019-11-10 20:30:53 -07007870 /* We need to keep going until we don't find a matching req */
7871 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007872 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007873
Jens Axboe2ca10252020-02-13 17:17:35 -07007874 if (cancel_req->flags & REQ_F_OVERFLOW) {
7875 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03007876 list_del(&cancel_req->compl.list);
Jens Axboe2ca10252020-02-13 17:17:35 -07007877 cancel_req->flags &= ~REQ_F_OVERFLOW;
7878 if (list_empty(&ctx->cq_overflow_list)) {
7879 clear_bit(0, &ctx->sq_check_overflow);
7880 clear_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08007881 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
Jens Axboe2ca10252020-02-13 17:17:35 -07007882 }
7883 spin_unlock_irq(&ctx->completion_lock);
7884
7885 WRITE_ONCE(ctx->rings->cq_overflow,
7886 atomic_inc_return(&ctx->cached_cq_overflow));
7887
7888 /*
7889 * Put inflight ref and overflow ref. If that's
7890 * all we had, then we're done with this request.
7891 */
7892 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007893 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007894 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007895 continue;
7896 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007897 } else {
7898 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7899 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007900 }
7901
Jens Axboefcb323c2019-10-24 12:39:47 -06007902 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007903 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007904 }
7905}
7906
Pavel Begunkov801dd572020-06-15 10:33:14 +03007907static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007908{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007909 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7910 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007911
Pavel Begunkov801dd572020-06-15 10:33:14 +03007912 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007913}
7914
Jens Axboefcb323c2019-10-24 12:39:47 -06007915static int io_uring_flush(struct file *file, void *data)
7916{
7917 struct io_ring_ctx *ctx = file->private_data;
7918
7919 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007920
7921 /*
7922 * If the task is going away, cancel work it may have pending
7923 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007924 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7925 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007926
Jens Axboefcb323c2019-10-24 12:39:47 -06007927 return 0;
7928}
7929
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007930static void *io_uring_validate_mmap_request(struct file *file,
7931 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007932{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007933 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007934 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007935 struct page *page;
7936 void *ptr;
7937
7938 switch (offset) {
7939 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007940 case IORING_OFF_CQ_RING:
7941 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007942 break;
7943 case IORING_OFF_SQES:
7944 ptr = ctx->sq_sqes;
7945 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007946 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007947 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007948 }
7949
7950 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007951 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007952 return ERR_PTR(-EINVAL);
7953
7954 return ptr;
7955}
7956
7957#ifdef CONFIG_MMU
7958
7959static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7960{
7961 size_t sz = vma->vm_end - vma->vm_start;
7962 unsigned long pfn;
7963 void *ptr;
7964
7965 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7966 if (IS_ERR(ptr))
7967 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007968
7969 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7970 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7971}
7972
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007973#else /* !CONFIG_MMU */
7974
7975static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7976{
7977 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7978}
7979
7980static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7981{
7982 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7983}
7984
7985static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7986 unsigned long addr, unsigned long len,
7987 unsigned long pgoff, unsigned long flags)
7988{
7989 void *ptr;
7990
7991 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7992 if (IS_ERR(ptr))
7993 return PTR_ERR(ptr);
7994
7995 return (unsigned long) ptr;
7996}
7997
7998#endif /* !CONFIG_MMU */
7999
Jens Axboe2b188cc2019-01-07 10:46:33 -07008000SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8001 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8002 size_t, sigsz)
8003{
8004 struct io_ring_ctx *ctx;
8005 long ret = -EBADF;
8006 int submitted = 0;
8007 struct fd f;
8008
Jens Axboe4c6e2772020-07-01 11:29:10 -06008009 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008010
Jens Axboe6c271ce2019-01-10 11:22:30 -07008011 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008012 return -EINVAL;
8013
8014 f = fdget(fd);
8015 if (!f.file)
8016 return -EBADF;
8017
8018 ret = -EOPNOTSUPP;
8019 if (f.file->f_op != &io_uring_fops)
8020 goto out_fput;
8021
8022 ret = -ENXIO;
8023 ctx = f.file->private_data;
8024 if (!percpu_ref_tryget(&ctx->refs))
8025 goto out_fput;
8026
Jens Axboe6c271ce2019-01-10 11:22:30 -07008027 /*
8028 * For SQ polling, the thread will do all submissions and completions.
8029 * Just return the requested submit count, and wake the thread if
8030 * we were asked to.
8031 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008032 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008033 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008034 if (!list_empty_careful(&ctx->cq_overflow_list))
8035 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008036 if (flags & IORING_ENTER_SQ_WAKEUP)
8037 wake_up(&ctx->sqo_wait);
8038 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008039 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07008040 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03008041 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008042 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008043
8044 if (submitted != to_submit)
8045 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008046 }
8047 if (flags & IORING_ENTER_GETEVENTS) {
8048 min_complete = min(min_complete, ctx->cq_entries);
8049
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008050 /*
8051 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8052 * space applications don't need to do io completion events
8053 * polling again, they can rely on io_sq_thread to do polling
8054 * work, which can reduce cpu usage and uring_lock contention.
8055 */
8056 if (ctx->flags & IORING_SETUP_IOPOLL &&
8057 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008058 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008059 } else {
8060 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8061 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008062 }
8063
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008064out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008065 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008066out_fput:
8067 fdput(f);
8068 return submitted ? submitted : ret;
8069}
8070
Tobias Klauserbebdb652020-02-26 18:38:32 +01008071#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008072static int io_uring_show_cred(int id, void *p, void *data)
8073{
8074 const struct cred *cred = p;
8075 struct seq_file *m = data;
8076 struct user_namespace *uns = seq_user_ns(m);
8077 struct group_info *gi;
8078 kernel_cap_t cap;
8079 unsigned __capi;
8080 int g;
8081
8082 seq_printf(m, "%5d\n", id);
8083 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8084 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8085 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8086 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8087 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8088 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8089 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8090 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8091 seq_puts(m, "\n\tGroups:\t");
8092 gi = cred->group_info;
8093 for (g = 0; g < gi->ngroups; g++) {
8094 seq_put_decimal_ull(m, g ? " " : "",
8095 from_kgid_munged(uns, gi->gid[g]));
8096 }
8097 seq_puts(m, "\n\tCapEff:\t");
8098 cap = cred->cap_effective;
8099 CAP_FOR_EACH_U32(__capi)
8100 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8101 seq_putc(m, '\n');
8102 return 0;
8103}
8104
8105static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8106{
8107 int i;
8108
8109 mutex_lock(&ctx->uring_lock);
8110 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
8111 for (i = 0; i < ctx->nr_user_files; i++) {
8112 struct fixed_file_table *table;
8113 struct file *f;
8114
8115 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8116 f = table->files[i & IORING_FILE_TABLE_MASK];
8117 if (f)
8118 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8119 else
8120 seq_printf(m, "%5u: <none>\n", i);
8121 }
8122 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
8123 for (i = 0; i < ctx->nr_user_bufs; i++) {
8124 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8125
8126 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8127 (unsigned int) buf->len);
8128 }
8129 if (!idr_is_empty(&ctx->personality_idr)) {
8130 seq_printf(m, "Personalities:\n");
8131 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8132 }
Jens Axboed7718a92020-02-14 22:23:12 -07008133 seq_printf(m, "PollList:\n");
8134 spin_lock_irq(&ctx->completion_lock);
8135 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8136 struct hlist_head *list = &ctx->cancel_hash[i];
8137 struct io_kiocb *req;
8138
8139 hlist_for_each_entry(req, list, hash_node)
8140 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8141 req->task->task_works != NULL);
8142 }
8143 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008144 mutex_unlock(&ctx->uring_lock);
8145}
8146
8147static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8148{
8149 struct io_ring_ctx *ctx = f->private_data;
8150
8151 if (percpu_ref_tryget(&ctx->refs)) {
8152 __io_uring_show_fdinfo(ctx, m);
8153 percpu_ref_put(&ctx->refs);
8154 }
8155}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008156#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008157
Jens Axboe2b188cc2019-01-07 10:46:33 -07008158static const struct file_operations io_uring_fops = {
8159 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008160 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008161 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008162#ifndef CONFIG_MMU
8163 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8164 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8165#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008166 .poll = io_uring_poll,
8167 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008168#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008169 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008170#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008171};
8172
8173static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8174 struct io_uring_params *p)
8175{
Hristo Venev75b28af2019-08-26 17:23:46 +00008176 struct io_rings *rings;
8177 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008178
Hristo Venev75b28af2019-08-26 17:23:46 +00008179 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8180 if (size == SIZE_MAX)
8181 return -EOVERFLOW;
8182
8183 rings = io_mem_alloc(size);
8184 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008185 return -ENOMEM;
8186
Hristo Venev75b28af2019-08-26 17:23:46 +00008187 ctx->rings = rings;
8188 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8189 rings->sq_ring_mask = p->sq_entries - 1;
8190 rings->cq_ring_mask = p->cq_entries - 1;
8191 rings->sq_ring_entries = p->sq_entries;
8192 rings->cq_ring_entries = p->cq_entries;
8193 ctx->sq_mask = rings->sq_ring_mask;
8194 ctx->cq_mask = rings->cq_ring_mask;
8195 ctx->sq_entries = rings->sq_ring_entries;
8196 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008197
8198 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008199 if (size == SIZE_MAX) {
8200 io_mem_free(ctx->rings);
8201 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008202 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008203 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008204
8205 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008206 if (!ctx->sq_sqes) {
8207 io_mem_free(ctx->rings);
8208 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008209 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008210 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008211
Jens Axboe2b188cc2019-01-07 10:46:33 -07008212 return 0;
8213}
8214
8215/*
8216 * Allocate an anonymous fd, this is what constitutes the application
8217 * visible backing of an io_uring instance. The application mmaps this
8218 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8219 * we have to tie this fd to a socket for file garbage collection purposes.
8220 */
8221static int io_uring_get_fd(struct io_ring_ctx *ctx)
8222{
8223 struct file *file;
8224 int ret;
8225
8226#if defined(CONFIG_UNIX)
8227 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8228 &ctx->ring_sock);
8229 if (ret)
8230 return ret;
8231#endif
8232
8233 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8234 if (ret < 0)
8235 goto err;
8236
8237 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8238 O_RDWR | O_CLOEXEC);
8239 if (IS_ERR(file)) {
8240 put_unused_fd(ret);
8241 ret = PTR_ERR(file);
8242 goto err;
8243 }
8244
8245#if defined(CONFIG_UNIX)
8246 ctx->ring_sock->file = file;
8247#endif
8248 fd_install(ret, file);
8249 return ret;
8250err:
8251#if defined(CONFIG_UNIX)
8252 sock_release(ctx->ring_sock);
8253 ctx->ring_sock = NULL;
8254#endif
8255 return ret;
8256}
8257
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008258static int io_uring_create(unsigned entries, struct io_uring_params *p,
8259 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008260{
8261 struct user_struct *user = NULL;
8262 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008263 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008264 int ret;
8265
Jens Axboe8110c1a2019-12-28 15:39:54 -07008266 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008267 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008268 if (entries > IORING_MAX_ENTRIES) {
8269 if (!(p->flags & IORING_SETUP_CLAMP))
8270 return -EINVAL;
8271 entries = IORING_MAX_ENTRIES;
8272 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008273
8274 /*
8275 * Use twice as many entries for the CQ ring. It's possible for the
8276 * application to drive a higher depth than the size of the SQ ring,
8277 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008278 * some flexibility in overcommitting a bit. If the application has
8279 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8280 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008281 */
8282 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008283 if (p->flags & IORING_SETUP_CQSIZE) {
8284 /*
8285 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8286 * to a power-of-two, if it isn't already. We do NOT impose
8287 * any cq vs sq ring sizing.
8288 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008289 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008290 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008291 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8292 if (!(p->flags & IORING_SETUP_CLAMP))
8293 return -EINVAL;
8294 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8295 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008296 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8297 } else {
8298 p->cq_entries = 2 * p->sq_entries;
8299 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008300
8301 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008302 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008303
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008304 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008305 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008306 ring_pages(p->sq_entries, p->cq_entries));
8307 if (ret) {
8308 free_uid(user);
8309 return ret;
8310 }
8311 }
8312
8313 ctx = io_ring_ctx_alloc(p);
8314 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008315 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008316 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008317 p->cq_entries));
8318 free_uid(user);
8319 return -ENOMEM;
8320 }
8321 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008322 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008323 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008324
8325 ret = io_allocate_scq_urings(ctx, p);
8326 if (ret)
8327 goto err;
8328
Jens Axboe6c271ce2019-01-10 11:22:30 -07008329 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008330 if (ret)
8331 goto err;
8332
Jens Axboe2b188cc2019-01-07 10:46:33 -07008333 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008334 p->sq_off.head = offsetof(struct io_rings, sq.head);
8335 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8336 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8337 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8338 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8339 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8340 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008341
8342 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008343 p->cq_off.head = offsetof(struct io_rings, cq.head);
8344 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8345 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8346 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8347 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8348 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008349 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008350
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008351 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8352 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008353 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8354 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008355
8356 if (copy_to_user(params, p, sizeof(*p))) {
8357 ret = -EFAULT;
8358 goto err;
8359 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06008360 /*
8361 * Install ring fd as the very last thing, so we don't risk someone
8362 * having closed it before we finish setup
8363 */
8364 ret = io_uring_get_fd(ctx);
8365 if (ret < 0)
8366 goto err;
8367
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008368 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008369 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8370 ACCT_LOCKED);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008371 ctx->limit_mem = limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008372 return ret;
8373err:
8374 io_ring_ctx_wait_and_kill(ctx);
8375 return ret;
8376}
8377
8378/*
8379 * Sets up an aio uring context, and returns the fd. Applications asks for a
8380 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8381 * params structure passed in.
8382 */
8383static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8384{
8385 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008386 int i;
8387
8388 if (copy_from_user(&p, params, sizeof(p)))
8389 return -EFAULT;
8390 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8391 if (p.resv[i])
8392 return -EINVAL;
8393 }
8394
Jens Axboe6c271ce2019-01-10 11:22:30 -07008395 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008396 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008397 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008398 return -EINVAL;
8399
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008400 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008401}
8402
8403SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8404 struct io_uring_params __user *, params)
8405{
8406 return io_uring_setup(entries, params);
8407}
8408
Jens Axboe66f4af92020-01-16 15:36:52 -07008409static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8410{
8411 struct io_uring_probe *p;
8412 size_t size;
8413 int i, ret;
8414
8415 size = struct_size(p, ops, nr_args);
8416 if (size == SIZE_MAX)
8417 return -EOVERFLOW;
8418 p = kzalloc(size, GFP_KERNEL);
8419 if (!p)
8420 return -ENOMEM;
8421
8422 ret = -EFAULT;
8423 if (copy_from_user(p, arg, size))
8424 goto out;
8425 ret = -EINVAL;
8426 if (memchr_inv(p, 0, size))
8427 goto out;
8428
8429 p->last_op = IORING_OP_LAST - 1;
8430 if (nr_args > IORING_OP_LAST)
8431 nr_args = IORING_OP_LAST;
8432
8433 for (i = 0; i < nr_args; i++) {
8434 p->ops[i].op = i;
8435 if (!io_op_defs[i].not_supported)
8436 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8437 }
8438 p->ops_len = i;
8439
8440 ret = 0;
8441 if (copy_to_user(arg, p, size))
8442 ret = -EFAULT;
8443out:
8444 kfree(p);
8445 return ret;
8446}
8447
Jens Axboe071698e2020-01-28 10:04:42 -07008448static int io_register_personality(struct io_ring_ctx *ctx)
8449{
8450 const struct cred *creds = get_current_cred();
8451 int id;
8452
8453 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8454 USHRT_MAX, GFP_KERNEL);
8455 if (id < 0)
8456 put_cred(creds);
8457 return id;
8458}
8459
8460static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8461{
8462 const struct cred *old_creds;
8463
8464 old_creds = idr_remove(&ctx->personality_idr, id);
8465 if (old_creds) {
8466 put_cred(old_creds);
8467 return 0;
8468 }
8469
8470 return -EINVAL;
8471}
8472
8473static bool io_register_op_must_quiesce(int op)
8474{
8475 switch (op) {
8476 case IORING_UNREGISTER_FILES:
8477 case IORING_REGISTER_FILES_UPDATE:
8478 case IORING_REGISTER_PROBE:
8479 case IORING_REGISTER_PERSONALITY:
8480 case IORING_UNREGISTER_PERSONALITY:
8481 return false;
8482 default:
8483 return true;
8484 }
8485}
8486
Jens Axboeedafcce2019-01-09 09:16:05 -07008487static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8488 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008489 __releases(ctx->uring_lock)
8490 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008491{
8492 int ret;
8493
Jens Axboe35fa71a2019-04-22 10:23:23 -06008494 /*
8495 * We're inside the ring mutex, if the ref is already dying, then
8496 * someone else killed the ctx or is already going through
8497 * io_uring_register().
8498 */
8499 if (percpu_ref_is_dying(&ctx->refs))
8500 return -ENXIO;
8501
Jens Axboe071698e2020-01-28 10:04:42 -07008502 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008503 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008504
Jens Axboe05f3fb32019-12-09 11:22:50 -07008505 /*
8506 * Drop uring mutex before waiting for references to exit. If
8507 * another thread is currently inside io_uring_enter() it might
8508 * need to grab the uring_lock to make progress. If we hold it
8509 * here across the drain wait, then we can deadlock. It's safe
8510 * to drop the mutex here, since no new references will come in
8511 * after we've killed the percpu ref.
8512 */
8513 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008514 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008515 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008516 if (ret) {
8517 percpu_ref_resurrect(&ctx->refs);
8518 ret = -EINTR;
8519 goto out;
8520 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008521 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008522
8523 switch (opcode) {
8524 case IORING_REGISTER_BUFFERS:
8525 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8526 break;
8527 case IORING_UNREGISTER_BUFFERS:
8528 ret = -EINVAL;
8529 if (arg || nr_args)
8530 break;
8531 ret = io_sqe_buffer_unregister(ctx);
8532 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008533 case IORING_REGISTER_FILES:
8534 ret = io_sqe_files_register(ctx, arg, nr_args);
8535 break;
8536 case IORING_UNREGISTER_FILES:
8537 ret = -EINVAL;
8538 if (arg || nr_args)
8539 break;
8540 ret = io_sqe_files_unregister(ctx);
8541 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008542 case IORING_REGISTER_FILES_UPDATE:
8543 ret = io_sqe_files_update(ctx, arg, nr_args);
8544 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008545 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008546 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008547 ret = -EINVAL;
8548 if (nr_args != 1)
8549 break;
8550 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008551 if (ret)
8552 break;
8553 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8554 ctx->eventfd_async = 1;
8555 else
8556 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008557 break;
8558 case IORING_UNREGISTER_EVENTFD:
8559 ret = -EINVAL;
8560 if (arg || nr_args)
8561 break;
8562 ret = io_eventfd_unregister(ctx);
8563 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008564 case IORING_REGISTER_PROBE:
8565 ret = -EINVAL;
8566 if (!arg || nr_args > 256)
8567 break;
8568 ret = io_probe(ctx, arg, nr_args);
8569 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008570 case IORING_REGISTER_PERSONALITY:
8571 ret = -EINVAL;
8572 if (arg || nr_args)
8573 break;
8574 ret = io_register_personality(ctx);
8575 break;
8576 case IORING_UNREGISTER_PERSONALITY:
8577 ret = -EINVAL;
8578 if (arg)
8579 break;
8580 ret = io_unregister_personality(ctx, nr_args);
8581 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008582 default:
8583 ret = -EINVAL;
8584 break;
8585 }
8586
Jens Axboe071698e2020-01-28 10:04:42 -07008587 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008588 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008589 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008590out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008591 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008592 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008593 return ret;
8594}
8595
8596SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8597 void __user *, arg, unsigned int, nr_args)
8598{
8599 struct io_ring_ctx *ctx;
8600 long ret = -EBADF;
8601 struct fd f;
8602
8603 f = fdget(fd);
8604 if (!f.file)
8605 return -EBADF;
8606
8607 ret = -EOPNOTSUPP;
8608 if (f.file->f_op != &io_uring_fops)
8609 goto out_fput;
8610
8611 ctx = f.file->private_data;
8612
8613 mutex_lock(&ctx->uring_lock);
8614 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8615 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008616 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8617 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008618out_fput:
8619 fdput(f);
8620 return ret;
8621}
8622
Jens Axboe2b188cc2019-01-07 10:46:33 -07008623static int __init io_uring_init(void)
8624{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008625#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8626 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8627 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8628} while (0)
8629
8630#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8631 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8632 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8633 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8634 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8635 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8636 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8637 BUILD_BUG_SQE_ELEM(8, __u64, off);
8638 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8639 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008640 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008641 BUILD_BUG_SQE_ELEM(24, __u32, len);
8642 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8643 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8644 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8645 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008646 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8647 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008648 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8649 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8650 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8651 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8652 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8653 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8654 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8655 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008656 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008657 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8658 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8659 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008660 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008661
Jens Axboed3656342019-12-18 09:50:26 -07008662 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008663 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008664 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8665 return 0;
8666};
8667__initcall(io_uring_init);