blob: 53392f22b2129991949e517790da92ca2f6014c4 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060093
94/*
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
96 */
97#define IORING_FILE_TABLE_SHIFT 9
98#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
99#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
100#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
102struct io_uring {
103 u32 head ____cacheline_aligned_in_smp;
104 u32 tail ____cacheline_aligned_in_smp;
105};
106
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 *
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
118 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * The kernel controls head of the sq ring and the tail of the cq ring,
120 * and the application controls tail of the sq ring and the head of the
121 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 * ring_entries - 1)
127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 u32 sq_ring_mask, cq_ring_mask;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
134 *
135 * Written by the kernel, shouldn't be modified by the
136 * application (i.e. get number of "new events" by comparing to
137 * cached value).
138 *
139 * After a new SQ head value was read by the application this
140 * counter includes all submissions that were dropped reaching
141 * the new SQ head (and possibly more).
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200145 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application.
149 *
150 * The application needs a full memory barrier before checking
151 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
152 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000153 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200155 * Runtime CQ flags
156 *
157 * Written by the application, shouldn't be modified by the
158 * kernel.
159 */
160 u32 cq_flags;
161 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 * Number of completion events lost because the queue was full;
163 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800164 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 * the completion queue.
166 *
167 * Written by the kernel, shouldn't be modified by the
168 * application (i.e. get number of "new events" by comparing to
169 * cached value).
170 *
171 * As completion events come in out of order this counter is not
172 * ordered with any other data.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200175 /*
176 * Ring buffer of completion events.
177 *
178 * The kernel writes completion events fresh every time they are
179 * produced, so the application is allowed to modify pending
180 * entries.
181 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000182 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700183};
184
Jens Axboeedafcce2019-01-09 09:16:05 -0700185struct io_mapped_ubuf {
186 u64 ubuf;
187 size_t len;
188 struct bio_vec *bvec;
189 unsigned int nr_bvecs;
190};
191
Jens Axboe65e19f52019-10-26 07:20:21 -0600192struct fixed_file_table {
193 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700194};
195
Xiaoguang Wang05589552020-03-31 14:05:18 +0800196struct fixed_file_ref_node {
197 struct percpu_ref refs;
198 struct list_head node;
199 struct list_head file_list;
200 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600201 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800202};
203
Jens Axboe05f3fb32019-12-09 11:22:50 -0700204struct fixed_file_data {
205 struct fixed_file_table *table;
206 struct io_ring_ctx *ctx;
207
Xiaoguang Wang05589552020-03-31 14:05:18 +0800208 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700210 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800211 struct list_head ref_list;
212 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700213};
214
Jens Axboe5a2e7452020-02-23 16:23:11 -0700215struct io_buffer {
216 struct list_head list;
217 __u64 addr;
218 __s32 len;
219 __u16 bid;
220};
221
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222struct io_ring_ctx {
223 struct {
224 struct percpu_ref refs;
225 } ____cacheline_aligned_in_smp;
226
227 struct {
228 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800229 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700230 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800231 unsigned int cq_overflow_flushed: 1;
232 unsigned int drain_next: 1;
233 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Hristo Venev75b28af2019-08-26 17:23:46 +0000235 /*
236 * Ring buffer of indices into array of io_uring_sqe, which is
237 * mmapped by the application using the IORING_OFF_SQES offset.
238 *
239 * This indirection could e.g. be used to assign fixed
240 * io_uring_sqe entries to operations and only submit them to
241 * the queue when needed.
242 *
243 * The kernel modifies neither the indices array nor the entries
244 * array.
245 */
246 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700247 unsigned cached_sq_head;
248 unsigned sq_entries;
249 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700250 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600251 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700252 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700253 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600254
255 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600256 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700257 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258
Jens Axboefcb323c2019-10-24 12:39:47 -0600259 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700260 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261 } ____cacheline_aligned_in_smp;
262
Hristo Venev75b28af2019-08-26 17:23:46 +0000263 struct io_rings *rings;
264
Jens Axboe2b188cc2019-01-07 10:46:33 -0700265 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600266 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700267 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2aede0e2020-09-14 10:45:53 -0600268
269 /*
270 * For SQPOLL usage - we hold a reference to the parent task, so we
271 * have access to the ->files
272 */
273 struct task_struct *sqo_task;
274
275 /* Only used for accounting purposes */
276 struct mm_struct *mm_account;
277
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279
Jens Axboe6b063142019-01-10 22:13:58 -0700280 /*
281 * If used, fixed file set. Writers must ensure that ->refs is dead,
282 * readers must ensure that ->refs is alive as long as the file* is
283 * used. Only updated through io_uring_register(2).
284 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700285 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700286 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300287 int ring_fd;
288 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700289
Jens Axboeedafcce2019-01-09 09:16:05 -0700290 /* if used, fixed mapped user buffers */
291 unsigned nr_user_bufs;
292 struct io_mapped_ubuf *user_bufs;
293
Jens Axboe2b188cc2019-01-07 10:46:33 -0700294 struct user_struct *user;
295
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700296 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700297
Jens Axboe0f158b42020-05-14 17:18:39 -0600298 struct completion ref_comp;
299 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700300
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700301 /* if all else fails... */
302 struct io_kiocb *fallback_req;
303
Jens Axboe206aefd2019-11-07 18:27:42 -0700304#if defined(CONFIG_UNIX)
305 struct socket *ring_sock;
306#endif
307
Jens Axboe5a2e7452020-02-23 16:23:11 -0700308 struct idr io_buffer_idr;
309
Jens Axboe071698e2020-01-28 10:04:42 -0700310 struct idr personality_idr;
311
Jens Axboe206aefd2019-11-07 18:27:42 -0700312 struct {
313 unsigned cached_cq_tail;
314 unsigned cq_entries;
315 unsigned cq_mask;
316 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700317 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700318 struct wait_queue_head cq_wait;
319 struct fasync_struct *cq_fasync;
320 struct eventfd_ctx *cq_ev_fd;
321 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700322
323 struct {
324 struct mutex uring_lock;
325 wait_queue_head_t wait;
326 } ____cacheline_aligned_in_smp;
327
328 struct {
329 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700330
Jens Axboedef596e2019-01-09 08:59:42 -0700331 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300332 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700333 * io_uring instances that don't use IORING_SETUP_SQPOLL.
334 * For SQPOLL, only the single threaded io_sq_thread() will
335 * manipulate the list, hence no extra locking is needed there.
336 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300337 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700338 struct hlist_head *cancel_hash;
339 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700340 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600341
342 spinlock_t inflight_lock;
343 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600345
Jens Axboe4a38aed22020-05-14 17:21:15 -0600346 struct delayed_work file_put_work;
347 struct llist_head file_put_llist;
348
Jens Axboe85faa7b2020-04-09 18:14:00 -0600349 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700350};
351
Jens Axboe09bb8392019-03-13 12:39:28 -0600352/*
353 * First field must be the file pointer in all the
354 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
355 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700356struct io_poll_iocb {
357 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700358 union {
359 struct wait_queue_head *head;
360 u64 addr;
361 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700362 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600363 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700364 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700365 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700366};
367
Jens Axboeb5dba592019-12-11 14:02:38 -0700368struct io_close {
369 struct file *file;
370 struct file *put_file;
371 int fd;
372};
373
Jens Axboead8a48a2019-11-15 08:49:11 -0700374struct io_timeout_data {
375 struct io_kiocb *req;
376 struct hrtimer timer;
377 struct timespec64 ts;
378 enum hrtimer_mode mode;
379};
380
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700381struct io_accept {
382 struct file *file;
383 struct sockaddr __user *addr;
384 int __user *addr_len;
385 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600386 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700387};
388
389struct io_sync {
390 struct file *file;
391 loff_t len;
392 loff_t off;
393 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700394 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700395};
396
Jens Axboefbf23842019-12-17 18:45:56 -0700397struct io_cancel {
398 struct file *file;
399 u64 addr;
400};
401
Jens Axboeb29472e2019-12-17 18:50:29 -0700402struct io_timeout {
403 struct file *file;
404 u64 addr;
405 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300406 u32 off;
407 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300408 struct list_head list;
Jens Axboeb29472e2019-12-17 18:50:29 -0700409};
410
Jens Axboe9adbd452019-12-20 08:45:55 -0700411struct io_rw {
412 /* NOTE: kiocb has the file as the first member, so don't do it here */
413 struct kiocb kiocb;
414 u64 addr;
415 u64 len;
416};
417
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700418struct io_connect {
419 struct file *file;
420 struct sockaddr __user *addr;
421 int addr_len;
422};
423
Jens Axboee47293f2019-12-20 08:58:21 -0700424struct io_sr_msg {
425 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700426 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300427 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700428 void __user *buf;
429 };
Jens Axboee47293f2019-12-20 08:58:21 -0700430 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700431 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700432 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700433 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700434};
435
Jens Axboe15b71ab2019-12-11 11:20:36 -0700436struct io_open {
437 struct file *file;
438 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700439 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700440 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600441 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700442};
443
Jens Axboe05f3fb32019-12-09 11:22:50 -0700444struct io_files_update {
445 struct file *file;
446 u64 arg;
447 u32 nr_args;
448 u32 offset;
449};
450
Jens Axboe4840e412019-12-25 22:03:45 -0700451struct io_fadvise {
452 struct file *file;
453 u64 offset;
454 u32 len;
455 u32 advice;
456};
457
Jens Axboec1ca7572019-12-25 22:18:28 -0700458struct io_madvise {
459 struct file *file;
460 u64 addr;
461 u32 len;
462 u32 advice;
463};
464
Jens Axboe3e4827b2020-01-08 15:18:09 -0700465struct io_epoll {
466 struct file *file;
467 int epfd;
468 int op;
469 int fd;
470 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700471};
472
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300473struct io_splice {
474 struct file *file_out;
475 struct file *file_in;
476 loff_t off_out;
477 loff_t off_in;
478 u64 len;
479 unsigned int flags;
480};
481
Jens Axboeddf0322d2020-02-23 16:41:33 -0700482struct io_provide_buf {
483 struct file *file;
484 __u64 addr;
485 __s32 len;
486 __u32 bgid;
487 __u16 nbufs;
488 __u16 bid;
489};
490
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700491struct io_statx {
492 struct file *file;
493 int dfd;
494 unsigned int mask;
495 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700496 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700497 struct statx __user *buffer;
498};
499
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300500struct io_completion {
501 struct file *file;
502 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300503 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300504};
505
Jens Axboef499a022019-12-02 16:28:46 -0700506struct io_async_connect {
507 struct sockaddr_storage address;
508};
509
Jens Axboe03b12302019-12-02 18:50:25 -0700510struct io_async_msghdr {
511 struct iovec fast_iov[UIO_FASTIOV];
512 struct iovec *iov;
513 struct sockaddr __user *uaddr;
514 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700515 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700516};
517
Jens Axboef67676d2019-12-02 11:03:47 -0700518struct io_async_rw {
519 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600520 const struct iovec *free_iovec;
521 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600522 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600523 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700524};
525
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700526struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700527 union {
528 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700529 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700530 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700531 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700532 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700533};
534
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300535enum {
536 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
537 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
538 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
539 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
540 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700541 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300542
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300543 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300544 REQ_F_FAIL_LINK_BIT,
545 REQ_F_INFLIGHT_BIT,
546 REQ_F_CUR_POS_BIT,
547 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300548 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300549 REQ_F_ISREG_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300550 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300551 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700552 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700553 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600554 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800555 REQ_F_WORK_INITIALIZED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700556
557 /* not a real bit, just to check we're not overflowing the space */
558 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300559};
560
561enum {
562 /* ctx owns file */
563 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
564 /* drain existing IO first */
565 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
566 /* linked sqes */
567 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
568 /* doesn't sever on completion < 0 */
569 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
570 /* IOSQE_ASYNC */
571 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700572 /* IOSQE_BUFFER_SELECT */
573 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300574
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300575 /* head of a link */
576 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300577 /* fail rest of links */
578 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
579 /* on inflight list */
580 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
581 /* read/write uses file position */
582 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
583 /* must not punt to workers */
584 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300585 /* has linked timeout */
586 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300587 /* regular file */
588 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300589 /* completion under lock */
590 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300591 /* needs cleanup */
592 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700593 /* already went through poll handler */
594 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700595 /* buffer already selected */
596 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600597 /* doesn't need file table for this request */
598 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800599 /* io_wq_work is initialized */
600 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700601};
602
603struct async_poll {
604 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600605 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300606};
607
Jens Axboe09bb8392019-03-13 12:39:28 -0600608/*
609 * NOTE! Each of the iocb union members has the file pointer
610 * as the first entry in their struct definition. So you can
611 * access the file pointer through any of the sub-structs,
612 * or directly as just 'ki_filp' in this struct.
613 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700614struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700615 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600616 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700617 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700618 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700619 struct io_accept accept;
620 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700621 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700622 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700623 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700624 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700625 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700626 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700627 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700628 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700629 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700630 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300631 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700632 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700633 struct io_statx statx;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300634 /* use only after cleaning per-op data, see io_clean_op() */
635 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700636 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700637
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700638 struct io_async_ctx *io;
Jens Axboed625c6e2019-12-17 19:53:05 -0700639 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800640 /* polled IO has completed */
641 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700642
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700643 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300644 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700645
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300646 struct io_ring_ctx *ctx;
647 unsigned int flags;
648 refcount_t refs;
649 struct task_struct *task;
650 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700651
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300652 struct list_head link_list;
Jens Axboed7718a92020-02-14 22:23:12 -0700653
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300654 /*
655 * 1. used with ctx->iopoll_list with reads/writes
656 * 2. to track reqs with ->files (see io_op_def::file_table)
657 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300658 struct list_head inflight_entry;
Jens Axboefcb323c2019-10-24 12:39:47 -0600659
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300660 struct percpu_ref *fixed_file_refs;
661 struct callback_head task_work;
662 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
663 struct hlist_node hash_node;
664 struct async_poll *apoll;
665 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700666};
667
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300668struct io_defer_entry {
669 struct list_head list;
670 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300671 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300672};
673
Jens Axboedef596e2019-01-09 08:59:42 -0700674#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700675
Jens Axboe013538b2020-06-22 09:29:15 -0600676struct io_comp_state {
677 unsigned int nr;
678 struct list_head list;
679 struct io_ring_ctx *ctx;
680};
681
Jens Axboe9a56a232019-01-09 09:06:50 -0700682struct io_submit_state {
683 struct blk_plug plug;
684
685 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700686 * io_kiocb alloc cache
687 */
688 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300689 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700690
691 /*
Jens Axboe013538b2020-06-22 09:29:15 -0600692 * Batch completion logic
693 */
694 struct io_comp_state comp;
695
696 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700697 * File reference cache
698 */
699 struct file *file;
700 unsigned int fd;
701 unsigned int has_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -0700702 unsigned int ios_left;
703};
704
Jens Axboed3656342019-12-18 09:50:26 -0700705struct io_op_def {
706 /* needs req->io allocated for deferral/async */
707 unsigned async_ctx : 1;
708 /* needs current->mm setup, does mm access */
709 unsigned needs_mm : 1;
710 /* needs req->file assigned */
711 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600712 /* don't fail if file grab fails */
713 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700714 /* hash wq insertion if file is a regular file */
715 unsigned hash_reg_file : 1;
716 /* unbound wq insertion if file is a non-regular file */
717 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700718 /* opcode is not supported by this kernel */
719 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700720 /* needs file table */
721 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700722 /* needs ->fs */
723 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700724 /* set if opcode supports polled "wait" */
725 unsigned pollin : 1;
726 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700727 /* op supports buffer selection */
728 unsigned buffer_select : 1;
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300729 unsigned needs_fsize : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700730};
731
732static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300733 [IORING_OP_NOP] = {},
734 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700735 .async_ctx = 1,
736 .needs_mm = 1,
737 .needs_file = 1,
738 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700739 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700740 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700741 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300742 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700743 .async_ctx = 1,
744 .needs_mm = 1,
745 .needs_file = 1,
746 .hash_reg_file = 1,
747 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700748 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300749 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700750 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300751 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700752 .needs_file = 1,
753 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300754 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700755 .needs_file = 1,
756 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700757 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700758 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300759 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700760 .needs_file = 1,
761 .hash_reg_file = 1,
762 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700763 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300764 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700765 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300766 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700767 .needs_file = 1,
768 .unbound_nonreg_file = 1,
769 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300770 [IORING_OP_POLL_REMOVE] = {},
771 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700772 .needs_file = 1,
773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .async_ctx = 1,
776 .needs_mm = 1,
777 .needs_file = 1,
778 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700779 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700780 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700781 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300782 [IORING_OP_RECVMSG] = {
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 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700789 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700790 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300791 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700792 .async_ctx = 1,
793 .needs_mm = 1,
794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_TIMEOUT_REMOVE] = {},
796 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .needs_mm = 1,
798 .needs_file = 1,
799 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700800 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700801 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700802 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300803 [IORING_OP_ASYNC_CANCEL] = {},
804 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700805 .async_ctx = 1,
806 .needs_mm = 1,
807 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300808 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700809 .async_ctx = 1,
810 .needs_mm = 1,
811 .needs_file = 1,
812 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700813 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700814 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300815 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700816 .needs_file = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300817 .needs_fsize = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700818 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300819 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700820 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700821 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700822 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300823 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600824 .needs_file = 1,
825 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700826 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700827 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300828 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700829 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700830 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700831 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300832 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700833 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700834 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600835 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700836 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300837 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700838 .needs_mm = 1,
839 .needs_file = 1,
840 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700841 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700842 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700843 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700845 .needs_mm = 1,
846 .needs_file = 1,
847 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700848 .pollout = 1,
Pavel Begunkov57f1a642020-07-15 12:46:52 +0300849 .needs_fsize = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700850 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300851 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700852 .needs_file = 1,
853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700855 .needs_mm = 1,
856 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300857 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700858 .needs_mm = 1,
859 .needs_file = 1,
860 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700861 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700862 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300863 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700864 .needs_mm = 1,
865 .needs_file = 1,
866 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700867 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700868 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700869 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300870 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700871 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700872 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700873 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700874 [IORING_OP_EPOLL_CTL] = {
875 .unbound_nonreg_file = 1,
876 .file_table = 1,
877 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300878 [IORING_OP_SPLICE] = {
879 .needs_file = 1,
880 .hash_reg_file = 1,
881 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700882 },
883 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700884 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300885 [IORING_OP_TEE] = {
886 .needs_file = 1,
887 .hash_reg_file = 1,
888 .unbound_nonreg_file = 1,
889 },
Jens Axboed3656342019-12-18 09:50:26 -0700890};
891
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700892enum io_mem_account {
893 ACCT_LOCKED,
894 ACCT_PINNED,
895};
896
Pavel Begunkov81b68a52020-07-30 18:43:46 +0300897static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
898 struct io_comp_state *cs);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700899static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800900static void io_put_req(struct io_kiocb *req);
Jens Axboec40f6372020-06-25 15:39:59 -0600901static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700902static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700903static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -0600904static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700905static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700906static int __io_sqe_files_update(struct io_ring_ctx *ctx,
907 struct io_uring_files_update *ip,
908 unsigned nr_args);
Pavel Begunkovf56040b2020-07-23 20:25:21 +0300909static int io_prep_work_files(struct io_kiocb *req);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300910static void __io_clean_op(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700911static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
912 int fd, struct file **out_file, bool fixed);
913static void __io_queue_sqe(struct io_kiocb *req,
Jens Axboef13fad72020-06-22 09:34:30 -0600914 const struct io_uring_sqe *sqe,
915 struct io_comp_state *cs);
Jens Axboe4349f302020-07-09 15:07:01 -0600916static void io_file_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -0600917
Jens Axboeb63534c2020-06-04 11:28:00 -0600918static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
919 struct iovec **iovec, struct iov_iter *iter,
920 bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -0600921static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
922 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -0600923 struct iov_iter *iter, bool force);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700924
925static struct kmem_cache *req_cachep;
926
927static const struct file_operations io_uring_fops;
928
929struct sock *io_uring_get_socket(struct file *file)
930{
931#if defined(CONFIG_UNIX)
932 if (file->f_op == &io_uring_fops) {
933 struct io_ring_ctx *ctx = file->private_data;
934
935 return ctx->ring_sock->sk;
936 }
937#endif
938 return NULL;
939}
940EXPORT_SYMBOL(io_uring_get_socket);
941
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300942static inline void io_clean_op(struct io_kiocb *req)
943{
Pavel Begunkovbb175342020-08-20 11:33:35 +0300944 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
945 REQ_F_INFLIGHT))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300946 __io_clean_op(req);
947}
948
Jens Axboe4349f302020-07-09 15:07:01 -0600949static void io_sq_thread_drop_mm(void)
Jens Axboec40f6372020-06-25 15:39:59 -0600950{
951 struct mm_struct *mm = current->mm;
952
953 if (mm) {
954 kthread_unuse_mm(mm);
955 mmput(mm);
956 }
957}
958
959static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
960{
961 if (!current->mm) {
Pavel Begunkovcbcf7212020-07-18 11:31:21 +0300962 if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
Jens Axboe2aede0e2020-09-14 10:45:53 -0600963 !ctx->sqo_task->mm ||
964 !mmget_not_zero(ctx->sqo_task->mm)))
Jens Axboec40f6372020-06-25 15:39:59 -0600965 return -EFAULT;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600966 kthread_use_mm(ctx->sqo_task->mm);
Jens Axboec40f6372020-06-25 15:39:59 -0600967 }
968
969 return 0;
970}
971
972static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
973 struct io_kiocb *req)
974{
975 if (!io_op_defs[req->opcode].needs_mm)
976 return 0;
977 return __io_sq_thread_acquire_mm(ctx);
978}
979
980static inline void req_set_fail_links(struct io_kiocb *req)
981{
982 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
983 req->flags |= REQ_F_FAIL_LINK;
984}
Jens Axboe4a38aed22020-05-14 17:21:15 -0600985
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800986/*
987 * Note: must call io_req_init_async() for the first time you
988 * touch any members of io_wq_work.
989 */
990static inline void io_req_init_async(struct io_kiocb *req)
991{
992 if (req->flags & REQ_F_WORK_INITIALIZED)
993 return;
994
995 memset(&req->work, 0, sizeof(req->work));
996 req->flags |= REQ_F_WORK_INITIALIZED;
997}
998
Pavel Begunkov0cdaf762020-05-17 14:13:40 +0300999static inline bool io_async_submit(struct io_ring_ctx *ctx)
1000{
1001 return ctx->flags & IORING_SETUP_SQPOLL;
1002}
1003
Jens Axboe2b188cc2019-01-07 10:46:33 -07001004static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1005{
1006 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1007
Jens Axboe0f158b42020-05-14 17:18:39 -06001008 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009}
1010
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001011static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1012{
1013 return !req->timeout.off;
1014}
1015
Jens Axboe2b188cc2019-01-07 10:46:33 -07001016static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1017{
1018 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001019 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001020
1021 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1022 if (!ctx)
1023 return NULL;
1024
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001025 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
1026 if (!ctx->fallback_req)
1027 goto err;
1028
Jens Axboe78076bb2019-12-04 19:56:40 -07001029 /*
1030 * Use 5 bits less than the max cq entries, that should give us around
1031 * 32 entries per hash list if totally full and uniformly spread.
1032 */
1033 hash_bits = ilog2(p->cq_entries);
1034 hash_bits -= 5;
1035 if (hash_bits <= 0)
1036 hash_bits = 1;
1037 ctx->cancel_hash_bits = hash_bits;
1038 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1039 GFP_KERNEL);
1040 if (!ctx->cancel_hash)
1041 goto err;
1042 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1043
Roman Gushchin21482892019-05-07 10:01:48 -07001044 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001045 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1046 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047
1048 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001049 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001050 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001051 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001052 init_completion(&ctx->ref_comp);
1053 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001054 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001055 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001056 mutex_init(&ctx->uring_lock);
1057 init_waitqueue_head(&ctx->wait);
1058 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001059 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001060 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001061 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001062 init_waitqueue_head(&ctx->inflight_wait);
1063 spin_lock_init(&ctx->inflight_lock);
1064 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001065 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1066 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001067 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001068err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001069 if (ctx->fallback_req)
1070 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001071 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001072 kfree(ctx);
1073 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074}
1075
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001076static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001077{
Jens Axboe2bc99302020-07-09 09:43:27 -06001078 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1079 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001080
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001081 return seq != ctx->cached_cq_tail
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001082 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001083 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001084
Bob Liu9d858b22019-11-13 18:06:25 +08001085 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001086}
1087
Jens Axboede0617e2019-04-06 21:51:27 -06001088static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001089{
Hristo Venev75b28af2019-08-26 17:23:46 +00001090 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091
Pavel Begunkov07910152020-01-17 03:52:46 +03001092 /* order cqe stores with ring update */
1093 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094
Pavel Begunkov07910152020-01-17 03:52:46 +03001095 if (wq_has_sleeper(&ctx->cq_wait)) {
1096 wake_up_interruptible(&ctx->cq_wait);
1097 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098 }
1099}
1100
Jens Axboe51a4cc12020-08-10 10:55:56 -06001101/*
1102 * Returns true if we need to defer file table putting. This can only happen
1103 * from the error path with REQ_F_COMP_LOCKED set.
1104 */
1105static bool io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001106{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001107 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Jens Axboe51a4cc12020-08-10 10:55:56 -06001108 return false;
1109
1110 req->flags &= ~REQ_F_WORK_INITIALIZED;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001111
Jens Axboecccf0ee2020-01-27 16:34:48 -07001112 if (req->work.mm) {
1113 mmdrop(req->work.mm);
1114 req->work.mm = NULL;
1115 }
1116 if (req->work.creds) {
1117 put_cred(req->work.creds);
1118 req->work.creds = NULL;
1119 }
Jens Axboeff002b32020-02-07 16:05:21 -07001120 if (req->work.fs) {
1121 struct fs_struct *fs = req->work.fs;
1122
Jens Axboe51a4cc12020-08-10 10:55:56 -06001123 if (req->flags & REQ_F_COMP_LOCKED)
1124 return true;
1125
Jens Axboeff002b32020-02-07 16:05:21 -07001126 spin_lock(&req->work.fs->lock);
1127 if (--fs->users)
1128 fs = NULL;
1129 spin_unlock(&req->work.fs->lock);
1130 if (fs)
1131 free_fs_struct(fs);
Pavel Begunkovb65e0dd2020-07-25 14:41:58 +03001132 req->work.fs = NULL;
Jens Axboeff002b32020-02-07 16:05:21 -07001133 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001134
1135 return false;
Jens Axboe561fb042019-10-24 07:25:42 -06001136}
1137
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001138static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001139{
Jens Axboed3656342019-12-18 09:50:26 -07001140 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001141
Pavel Begunkov16d59802020-07-12 16:16:47 +03001142 io_req_init_async(req);
1143
Jens Axboed3656342019-12-18 09:50:26 -07001144 if (req->flags & REQ_F_ISREG) {
Jens Axboeeefdf302020-08-27 16:40:19 -06001145 if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001146 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001147 } else {
1148 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001149 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001150 }
Pavel Begunkovdca9cf82020-07-15 12:46:49 +03001151 if (!req->work.mm && def->needs_mm) {
1152 mmgrab(current->mm);
1153 req->work.mm = current->mm;
1154 }
1155 if (!req->work.creds)
1156 req->work.creds = get_current_cred();
1157 if (!req->work.fs && def->needs_fs) {
1158 spin_lock(&current->fs->lock);
1159 if (!current->fs->in_exec) {
1160 req->work.fs = current->fs;
1161 req->work.fs->users++;
1162 } else {
1163 req->work.flags |= IO_WQ_WORK_CANCEL;
1164 }
1165 spin_unlock(&current->fs->lock);
1166 }
Pavel Begunkov57f1a642020-07-15 12:46:52 +03001167 if (def->needs_fsize)
1168 req->work.fsize = rlimit(RLIMIT_FSIZE);
1169 else
1170 req->work.fsize = RLIM_INFINITY;
Jens Axboe561fb042019-10-24 07:25:42 -06001171}
1172
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001173static void io_prep_async_link(struct io_kiocb *req)
1174{
1175 struct io_kiocb *cur;
1176
1177 io_prep_async_work(req);
1178 if (req->flags & REQ_F_LINK_HEAD)
1179 list_for_each_entry(cur, &req->link_list, link_list)
1180 io_prep_async_work(cur);
1181}
1182
Jens Axboe7271ef32020-08-10 09:55:22 -06001183static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001184{
Jackie Liua197f662019-11-08 08:09:12 -07001185 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001186 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001187
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001188 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1189 &req->work, req->flags);
1190 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001191 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001192}
1193
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001194static void io_queue_async_work(struct io_kiocb *req)
1195{
Jens Axboe7271ef32020-08-10 09:55:22 -06001196 struct io_kiocb *link;
1197
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001198 /* init ->work of the whole link before punting */
1199 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001200 link = __io_queue_async_work(req);
1201
1202 if (link)
1203 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001204}
1205
Jens Axboe5262f562019-09-17 12:26:57 -06001206static void io_kill_timeout(struct io_kiocb *req)
1207{
1208 int ret;
1209
Jens Axboe2d283902019-12-04 11:08:05 -07001210 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001211 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001212 atomic_set(&req->ctx->cq_timeouts,
1213 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001214 list_del_init(&req->timeout.list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001215 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001216 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001217 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001218 }
1219}
1220
Jens Axboef3606e32020-09-22 08:18:24 -06001221static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
1222{
1223 struct io_ring_ctx *ctx = req->ctx;
1224
1225 if (!tsk || req->task == tsk)
1226 return true;
1227 if ((ctx->flags & IORING_SETUP_SQPOLL) && req->task == ctx->sqo_thread)
1228 return true;
1229 return false;
1230}
1231
1232static void io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe5262f562019-09-17 12:26:57 -06001233{
1234 struct io_kiocb *req, *tmp;
1235
1236 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001237 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
1238 if (io_task_match(req, tsk))
1239 io_kill_timeout(req);
1240 }
Jens Axboe5262f562019-09-17 12:26:57 -06001241 spin_unlock_irq(&ctx->completion_lock);
1242}
1243
Pavel Begunkov04518942020-05-26 20:34:05 +03001244static void __io_queue_deferred(struct io_ring_ctx *ctx)
1245{
1246 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001247 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1248 struct io_defer_entry, list);
Jens Axboe7271ef32020-08-10 09:55:22 -06001249 struct io_kiocb *link;
Pavel Begunkov04518942020-05-26 20:34:05 +03001250
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001251 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001252 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001253 list_del_init(&de->list);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001254 /* punt-init is done before queueing for defer */
Jens Axboe7271ef32020-08-10 09:55:22 -06001255 link = __io_queue_async_work(de->req);
1256 if (link) {
1257 __io_queue_linked_timeout(link);
1258 /* drop submission reference */
1259 link->flags |= REQ_F_COMP_LOCKED;
1260 io_put_req(link);
1261 }
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001262 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001263 } while (!list_empty(&ctx->defer_list));
1264}
1265
Pavel Begunkov360428f2020-05-30 14:54:17 +03001266static void io_flush_timeouts(struct io_ring_ctx *ctx)
1267{
1268 while (!list_empty(&ctx->timeout_list)) {
1269 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001270 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001271
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001272 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001273 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001274 if (req->timeout.target_seq != ctx->cached_cq_tail
1275 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001276 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001277
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001278 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001279 io_kill_timeout(req);
1280 }
1281}
1282
Jens Axboede0617e2019-04-06 21:51:27 -06001283static void io_commit_cqring(struct io_ring_ctx *ctx)
1284{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001285 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001286 __io_commit_cqring(ctx);
1287
Pavel Begunkov04518942020-05-26 20:34:05 +03001288 if (unlikely(!list_empty(&ctx->defer_list)))
1289 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001290}
1291
Jens Axboe2b188cc2019-01-07 10:46:33 -07001292static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1293{
Hristo Venev75b28af2019-08-26 17:23:46 +00001294 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001295 unsigned tail;
1296
1297 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001298 /*
1299 * writes to the cq entry need to come after reading head; the
1300 * control dependency is enough as we're using WRITE_ONCE to
1301 * fill the cq entry
1302 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001303 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001304 return NULL;
1305
1306 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001307 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308}
1309
Jens Axboef2842ab2020-01-08 11:04:00 -07001310static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1311{
Jens Axboef0b493e2020-02-01 21:30:11 -07001312 if (!ctx->cq_ev_fd)
1313 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001314 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1315 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001316 if (!ctx->eventfd_async)
1317 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001318 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001319}
1320
Jens Axboeb41e9852020-02-17 09:52:41 -07001321static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001322{
1323 if (waitqueue_active(&ctx->wait))
1324 wake_up(&ctx->wait);
1325 if (waitqueue_active(&ctx->sqo_wait))
1326 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001327 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001328 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001329}
1330
Pavel Begunkov46930142020-07-30 18:43:49 +03001331static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1332{
1333 if (list_empty(&ctx->cq_overflow_list)) {
1334 clear_bit(0, &ctx->sq_check_overflow);
1335 clear_bit(0, &ctx->cq_check_overflow);
1336 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1337 }
1338}
1339
Jens Axboec4a2ed72019-11-21 21:01:26 -07001340/* Returns true if there are no backlogged entries after the flush */
1341static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001342{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001343 struct io_rings *rings = ctx->rings;
1344 struct io_uring_cqe *cqe;
1345 struct io_kiocb *req;
1346 unsigned long flags;
1347 LIST_HEAD(list);
1348
1349 if (!force) {
1350 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001351 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001352 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1353 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001354 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001355 }
1356
1357 spin_lock_irqsave(&ctx->completion_lock, flags);
1358
1359 /* if force is set, the ring is going away. always drop after that */
1360 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001361 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001362
Jens Axboec4a2ed72019-11-21 21:01:26 -07001363 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001364 while (!list_empty(&ctx->cq_overflow_list)) {
1365 cqe = io_get_cqring(ctx);
1366 if (!cqe && !force)
1367 break;
1368
1369 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001370 compl.list);
1371 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001372 if (cqe) {
1373 WRITE_ONCE(cqe->user_data, req->user_data);
1374 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001375 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001376 } else {
1377 WRITE_ONCE(ctx->rings->cq_overflow,
1378 atomic_inc_return(&ctx->cached_cq_overflow));
1379 }
1380 }
1381
1382 io_commit_cqring(ctx);
Pavel Begunkov46930142020-07-30 18:43:49 +03001383 io_cqring_mark_overflow(ctx);
1384
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001385 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1386 io_cqring_ev_posted(ctx);
1387
1388 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001389 req = list_first_entry(&list, struct io_kiocb, compl.list);
1390 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001391 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001392 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001393
1394 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001395}
1396
Jens Axboebcda7ba2020-02-23 16:42:51 -07001397static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001399 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001400 struct io_uring_cqe *cqe;
1401
Jens Axboe78e19bb2019-11-06 15:21:34 -07001402 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001403
Jens Axboe2b188cc2019-01-07 10:46:33 -07001404 /*
1405 * If we can't get a cq entry, userspace overflowed the
1406 * submission (by quite a lot). Increment the overflow count in
1407 * the ring.
1408 */
1409 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001410 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001411 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001413 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001414 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001415 WRITE_ONCE(ctx->rings->cq_overflow,
1416 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001417 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001418 if (list_empty(&ctx->cq_overflow_list)) {
1419 set_bit(0, &ctx->sq_check_overflow);
1420 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001421 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001422 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001423 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001424 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001425 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001426 refcount_inc(&req->refs);
1427 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001428 }
1429}
1430
Jens Axboebcda7ba2020-02-23 16:42:51 -07001431static void io_cqring_fill_event(struct io_kiocb *req, long res)
1432{
1433 __io_cqring_fill_event(req, res, 0);
1434}
1435
Jens Axboee1e16092020-06-22 09:17:17 -06001436static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001437{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001438 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001439 unsigned long flags;
1440
1441 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001442 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001443 io_commit_cqring(ctx);
1444 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1445
Jens Axboe8c838782019-03-12 15:48:16 -06001446 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001447}
1448
Jens Axboe229a7b62020-06-22 10:13:11 -06001449static void io_submit_flush_completions(struct io_comp_state *cs)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001450{
Jens Axboe229a7b62020-06-22 10:13:11 -06001451 struct io_ring_ctx *ctx = cs->ctx;
1452
1453 spin_lock_irq(&ctx->completion_lock);
1454 while (!list_empty(&cs->list)) {
1455 struct io_kiocb *req;
1456
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001457 req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1458 list_del(&req->compl.list);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001459 __io_cqring_fill_event(req, req->result, req->compl.cflags);
Jens Axboe229a7b62020-06-22 10:13:11 -06001460 if (!(req->flags & REQ_F_LINK_HEAD)) {
1461 req->flags |= REQ_F_COMP_LOCKED;
1462 io_put_req(req);
1463 } else {
1464 spin_unlock_irq(&ctx->completion_lock);
1465 io_put_req(req);
1466 spin_lock_irq(&ctx->completion_lock);
1467 }
1468 }
1469 io_commit_cqring(ctx);
1470 spin_unlock_irq(&ctx->completion_lock);
1471
1472 io_cqring_ev_posted(ctx);
1473 cs->nr = 0;
1474}
1475
1476static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1477 struct io_comp_state *cs)
1478{
1479 if (!cs) {
1480 io_cqring_add_event(req, res, cflags);
1481 io_put_req(req);
1482 } else {
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001483 io_clean_op(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06001484 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001485 req->compl.cflags = cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001486 list_add_tail(&req->compl.list, &cs->list);
Jens Axboe229a7b62020-06-22 10:13:11 -06001487 if (++cs->nr >= 32)
1488 io_submit_flush_completions(cs);
1489 }
Jens Axboee1e16092020-06-22 09:17:17 -06001490}
1491
1492static void io_req_complete(struct io_kiocb *req, long res)
1493{
Jens Axboe229a7b62020-06-22 10:13:11 -06001494 __io_req_complete(req, res, 0, NULL);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001495}
1496
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001497static inline bool io_is_fallback_req(struct io_kiocb *req)
1498{
1499 return req == (struct io_kiocb *)
1500 ((unsigned long) req->ctx->fallback_req & ~1UL);
1501}
1502
1503static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1504{
1505 struct io_kiocb *req;
1506
1507 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001508 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001509 return req;
1510
1511 return NULL;
1512}
1513
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001514static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1515 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001516{
Jens Axboefd6fab22019-03-14 16:30:06 -06001517 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001518 struct io_kiocb *req;
1519
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001520 if (!state->free_reqs) {
Jens Axboe2579f912019-01-09 09:10:43 -07001521 size_t sz;
1522 int ret;
1523
1524 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001525 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1526
1527 /*
1528 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1529 * retry single alloc to be on the safe side.
1530 */
1531 if (unlikely(ret <= 0)) {
1532 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1533 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001534 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001535 ret = 1;
1536 }
Jens Axboe2579f912019-01-09 09:10:43 -07001537 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001538 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001539 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001540 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001541 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001542 }
1543
Jens Axboe2579f912019-01-09 09:10:43 -07001544 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001545fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001546 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001547}
1548
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001549static inline void io_put_file(struct io_kiocb *req, struct file *file,
1550 bool fixed)
1551{
1552 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001553 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001554 else
1555 fput(file);
1556}
1557
Jens Axboe51a4cc12020-08-10 10:55:56 -06001558static bool io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001559{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001560 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001561
Jens Axboe5acbbc82020-07-08 15:15:26 -06001562 if (req->io)
1563 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001564 if (req->file)
1565 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboefcb323c2019-10-24 12:39:47 -06001566
Jens Axboe51a4cc12020-08-10 10:55:56 -06001567 return io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001568}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001569
Jens Axboe51a4cc12020-08-10 10:55:56 -06001570static void __io_free_req_finish(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001571{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001572 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001573
Jens Axboee3bc8e92020-09-24 08:45:57 -06001574 put_task_struct(req->task);
1575
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001576 if (likely(!io_is_fallback_req(req)))
1577 kmem_cache_free(req_cachep, req);
1578 else
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001579 clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1580 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001581}
1582
Jens Axboe51a4cc12020-08-10 10:55:56 -06001583static void io_req_task_file_table_put(struct callback_head *cb)
1584{
1585 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1586 struct fs_struct *fs = req->work.fs;
1587
1588 spin_lock(&req->work.fs->lock);
1589 if (--fs->users)
1590 fs = NULL;
1591 spin_unlock(&req->work.fs->lock);
1592 if (fs)
1593 free_fs_struct(fs);
1594 req->work.fs = NULL;
1595 __io_free_req_finish(req);
1596}
1597
1598static void __io_free_req(struct io_kiocb *req)
1599{
1600 if (!io_dismantle_req(req)) {
1601 __io_free_req_finish(req);
1602 } else {
1603 int ret;
1604
1605 init_task_work(&req->task_work, io_req_task_file_table_put);
1606 ret = task_work_add(req->task, &req->task_work, TWA_RESUME);
1607 if (unlikely(ret)) {
1608 struct task_struct *tsk;
1609
1610 tsk = io_wq_get_task(req->ctx->io_wq);
1611 task_work_add(tsk, &req->task_work, 0);
1612 }
1613 }
1614}
1615
Jackie Liua197f662019-11-08 08:09:12 -07001616static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001617{
Jackie Liua197f662019-11-08 08:09:12 -07001618 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001619 int ret;
1620
Jens Axboe2d283902019-12-04 11:08:05 -07001621 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001622 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001623 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001624 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001625 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001626 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001627 return true;
1628 }
1629
1630 return false;
1631}
1632
Jens Axboeab0b6452020-06-30 08:43:15 -06001633static bool __io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001634{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001635 struct io_kiocb *link;
Jens Axboeab0b6452020-06-30 08:43:15 -06001636 bool wake_ev;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001637
1638 if (list_empty(&req->link_list))
Jens Axboeab0b6452020-06-30 08:43:15 -06001639 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001640 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1641 if (link->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboeab0b6452020-06-30 08:43:15 -06001642 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001643
1644 list_del_init(&link->link_list);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001645 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001646 wake_ev = io_link_cancel_timeout(link);
1647 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboeab0b6452020-06-30 08:43:15 -06001648 return wake_ev;
1649}
1650
1651static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001652{
Jens Axboe2665abf2019-11-05 12:40:47 -07001653 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeab0b6452020-06-30 08:43:15 -06001654 bool wake_ev;
Jens Axboe9e645e112019-05-10 16:07:28 -06001655
Jens Axboeab0b6452020-06-30 08:43:15 -06001656 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1657 unsigned long flags;
1658
1659 spin_lock_irqsave(&ctx->completion_lock, flags);
1660 wake_ev = __io_kill_linked_timeout(req);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001661 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001662 } else {
1663 wake_ev = __io_kill_linked_timeout(req);
1664 }
1665
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001666 if (wake_ev)
1667 io_cqring_ev_posted(ctx);
1668}
1669
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001670static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001671{
1672 struct io_kiocb *nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001673
Jens Axboe9e645e112019-05-10 16:07:28 -06001674 /*
1675 * The list should never be empty when we are called here. But could
1676 * potentially happen if the chain is messed up, check to be on the
1677 * safe side.
1678 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001679 if (unlikely(list_empty(&req->link_list)))
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001680 return NULL;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001681
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001682 nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1683 list_del_init(&req->link_list);
1684 if (!list_empty(&nxt->link_list))
1685 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001686 return nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001687}
1688
1689/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001690 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001691 */
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001692static void __io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001693{
Jens Axboe2665abf2019-11-05 12:40:47 -07001694 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06001695
1696 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001697 struct io_kiocb *link = list_first_entry(&req->link_list,
1698 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001699
Pavel Begunkov44932332019-12-05 16:16:35 +03001700 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001701 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001702
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001703 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe9b7adba2020-08-10 10:54:02 -06001704 link->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001705 __io_double_put_req(link);
Jens Axboe5d960722019-11-19 15:31:28 -07001706 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001707 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001708
1709 io_commit_cqring(ctx);
Jens Axboe2665abf2019-11-05 12:40:47 -07001710 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001711}
1712
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001713static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001714{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001715 struct io_ring_ctx *ctx = req->ctx;
1716
1717 if (!(req->flags & REQ_F_COMP_LOCKED)) {
1718 unsigned long flags;
1719
1720 spin_lock_irqsave(&ctx->completion_lock, flags);
1721 __io_fail_links(req);
1722 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1723 } else {
1724 __io_fail_links(req);
1725 }
1726
Jens Axboe9e645e112019-05-10 16:07:28 -06001727 io_cqring_ev_posted(ctx);
1728}
1729
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001730static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001731{
Pavel Begunkov9b0d9112020-06-28 12:52:34 +03001732 req->flags &= ~REQ_F_LINK_HEAD;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001733 if (req->flags & REQ_F_LINK_TIMEOUT)
1734 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001735
Jens Axboe9e645e112019-05-10 16:07:28 -06001736 /*
1737 * If LINK is set, we have dependent requests in this chain. If we
1738 * didn't fail this request, queue the first one up, moving any other
1739 * dependencies to the next request. In case of failure, fail the rest
1740 * of the chain.
1741 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001742 if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1743 return io_req_link_next(req);
1744 io_fail_links(req);
1745 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001746}
Jens Axboe2665abf2019-11-05 12:40:47 -07001747
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001748static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1749{
1750 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1751 return NULL;
1752 return __io_req_find_next(req);
1753}
1754
Jens Axboefd7d6de2020-08-23 11:00:37 -06001755static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
1756 bool twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001757{
1758 struct task_struct *tsk = req->task;
1759 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001760 int ret, notify;
Jens Axboec2c4c832020-07-01 15:37:11 -06001761
Jens Axboe6200b0a2020-09-13 14:38:30 -06001762 if (tsk->flags & PF_EXITING)
1763 return -ESRCH;
1764
Jens Axboec2c4c832020-07-01 15:37:11 -06001765 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001766 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1767 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1768 * processing task_work. There's no reliable way to tell if TWA_RESUME
1769 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06001770 */
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001771 notify = 0;
Jens Axboefd7d6de2020-08-23 11:00:37 -06001772 if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
Jens Axboec2c4c832020-07-01 15:37:11 -06001773 notify = TWA_SIGNAL;
1774
1775 ret = task_work_add(tsk, cb, notify);
1776 if (!ret)
1777 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06001778
Jens Axboec2c4c832020-07-01 15:37:11 -06001779 return ret;
1780}
1781
Jens Axboec40f6372020-06-25 15:39:59 -06001782static void __io_req_task_cancel(struct io_kiocb *req, int error)
1783{
1784 struct io_ring_ctx *ctx = req->ctx;
1785
1786 spin_lock_irq(&ctx->completion_lock);
1787 io_cqring_fill_event(req, error);
1788 io_commit_cqring(ctx);
1789 spin_unlock_irq(&ctx->completion_lock);
1790
1791 io_cqring_ev_posted(ctx);
1792 req_set_fail_links(req);
1793 io_double_put_req(req);
1794}
1795
1796static void io_req_task_cancel(struct callback_head *cb)
1797{
1798 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001799 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001800
1801 __io_req_task_cancel(req, -ECANCELED);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06001802 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001803}
1804
1805static void __io_req_task_submit(struct io_kiocb *req)
1806{
1807 struct io_ring_ctx *ctx = req->ctx;
1808
Jens Axboec40f6372020-06-25 15:39:59 -06001809 if (!__io_sq_thread_acquire_mm(ctx)) {
1810 mutex_lock(&ctx->uring_lock);
1811 __io_queue_sqe(req, NULL, NULL);
1812 mutex_unlock(&ctx->uring_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07001813 } else {
Jens Axboec40f6372020-06-25 15:39:59 -06001814 __io_req_task_cancel(req, -EFAULT);
Jens Axboe2665abf2019-11-05 12:40:47 -07001815 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001816}
1817
Jens Axboec40f6372020-06-25 15:39:59 -06001818static void io_req_task_submit(struct callback_head *cb)
1819{
1820 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06001821 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06001822
1823 __io_req_task_submit(req);
Jens Axboe6d816e02020-08-11 08:04:14 -06001824 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001825}
1826
1827static void io_req_task_queue(struct io_kiocb *req)
1828{
Jens Axboec40f6372020-06-25 15:39:59 -06001829 int ret;
1830
1831 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06001832 percpu_ref_get(&req->ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06001833
Jens Axboefd7d6de2020-08-23 11:00:37 -06001834 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboec40f6372020-06-25 15:39:59 -06001835 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06001836 struct task_struct *tsk;
1837
Jens Axboec40f6372020-06-25 15:39:59 -06001838 init_task_work(&req->task_work, io_req_task_cancel);
1839 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboec2c4c832020-07-01 15:37:11 -06001840 task_work_add(tsk, &req->task_work, 0);
1841 wake_up_process(tsk);
Jens Axboec40f6372020-06-25 15:39:59 -06001842 }
Jens Axboec40f6372020-06-25 15:39:59 -06001843}
1844
Pavel Begunkovc3524382020-06-28 12:52:32 +03001845static void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08001846{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001847 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001848
Pavel Begunkov906a8c32020-06-27 14:04:55 +03001849 if (nxt)
1850 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001851}
1852
Jens Axboe9e645e112019-05-10 16:07:28 -06001853static void io_free_req(struct io_kiocb *req)
1854{
Pavel Begunkovc3524382020-06-28 12:52:32 +03001855 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001856 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001857}
1858
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001859struct req_batch {
1860 void *reqs[IO_IOPOLL_BATCH];
1861 int to_free;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001862
1863 struct task_struct *task;
1864 int task_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001865};
1866
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001867static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001868{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001869 rb->to_free = 0;
1870 rb->task_refs = 0;
1871 rb->task = NULL;
1872}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001873
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001874static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1875 struct req_batch *rb)
1876{
1877 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1878 percpu_ref_put_many(&ctx->refs, rb->to_free);
1879 rb->to_free = 0;
1880}
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001881
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001882static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1883 struct req_batch *rb)
1884{
1885 if (rb->to_free)
1886 __io_req_free_batch_flush(ctx, rb);
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001887 if (rb->task) {
1888 put_task_struct_many(rb->task, rb->task_refs);
1889 rb->task = NULL;
1890 }
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001891}
1892
1893static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1894{
1895 if (unlikely(io_is_fallback_req(req))) {
1896 io_free_req(req);
1897 return;
1898 }
1899 if (req->flags & REQ_F_LINK_HEAD)
1900 io_queue_next(req);
1901
Jens Axboee3bc8e92020-09-24 08:45:57 -06001902 if (req->task != rb->task) {
1903 if (rb->task)
1904 put_task_struct_many(rb->task, rb->task_refs);
1905 rb->task = req->task;
1906 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001907 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06001908 rb->task_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03001909
Jens Axboe51a4cc12020-08-10 10:55:56 -06001910 WARN_ON_ONCE(io_dismantle_req(req));
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03001911 rb->reqs[rb->to_free++] = req;
1912 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1913 __io_req_free_batch_flush(req->ctx, rb);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001914}
1915
Jens Axboeba816ad2019-09-28 11:36:45 -06001916/*
1917 * Drop reference to request, return next in chain (if there is one) if this
1918 * was the last reference to this request.
1919 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001920static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06001921{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001922 struct io_kiocb *nxt = NULL;
1923
Jens Axboe2a44f462020-02-25 13:25:41 -07001924 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001925 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001926 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001927 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001928 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001929}
1930
Jens Axboe2b188cc2019-01-07 10:46:33 -07001931static void io_put_req(struct io_kiocb *req)
1932{
Jens Axboedef596e2019-01-09 08:59:42 -07001933 if (refcount_dec_and_test(&req->refs))
1934 io_free_req(req);
1935}
1936
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001937static struct io_wq_work *io_steal_work(struct io_kiocb *req)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001938{
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001939 struct io_kiocb *nxt;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001940
Pavel Begunkovf4db7182020-06-25 18:20:54 +03001941 /*
1942 * A ref is owned by io-wq in which context we're. So, if that's the
1943 * last one, it's safe to steal next work. False negatives are Ok,
1944 * it just will be re-punted async in io_put_work()
1945 */
1946 if (refcount_read(&req->refs) != 1)
1947 return NULL;
1948
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001949 nxt = io_req_find_next(req);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03001950 return nxt ? &nxt->work : NULL;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001951}
1952
Jens Axboe978db572019-11-14 22:39:04 -07001953/*
1954 * Must only be used if we don't need to care about links, usually from
1955 * within the completion handling itself.
1956 */
1957static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001958{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001959 /* drop both submit and complete references */
1960 if (refcount_sub_and_test(2, &req->refs))
1961 __io_free_req(req);
1962}
1963
Jens Axboe978db572019-11-14 22:39:04 -07001964static void io_double_put_req(struct io_kiocb *req)
1965{
1966 /* drop both submit and complete references */
1967 if (refcount_sub_and_test(2, &req->refs))
1968 io_free_req(req);
1969}
1970
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001971static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001972{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001973 struct io_rings *rings = ctx->rings;
1974
Jens Axboead3eb2c2019-12-18 17:12:20 -07001975 if (test_bit(0, &ctx->cq_check_overflow)) {
1976 /*
1977 * noflush == true is from the waitqueue handler, just ensure
1978 * we wake up the task, and the next invocation will flush the
1979 * entries. We cannot safely to it from here.
1980 */
1981 if (noflush && !list_empty(&ctx->cq_overflow_list))
1982 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001983
Jens Axboead3eb2c2019-12-18 17:12:20 -07001984 io_cqring_overflow_flush(ctx, false);
1985 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001986
Jens Axboea3a0e432019-08-20 11:03:11 -06001987 /* See comment at the top of this file */
1988 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001989 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001990}
1991
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001992static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1993{
1994 struct io_rings *rings = ctx->rings;
1995
1996 /* make sure SQ entry isn't read before tail */
1997 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1998}
1999
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002000static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002001{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002002 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002003
Jens Axboebcda7ba2020-02-23 16:42:51 -07002004 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2005 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002006 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002007 kfree(kbuf);
2008 return cflags;
2009}
2010
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002011static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2012{
2013 struct io_buffer *kbuf;
2014
2015 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2016 return io_put_kbuf(req, kbuf);
2017}
2018
Jens Axboe4c6e2772020-07-01 11:29:10 -06002019static inline bool io_run_task_work(void)
2020{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002021 /*
2022 * Not safe to run on exiting task, and the task_work handling will
2023 * not add work to such a task.
2024 */
2025 if (unlikely(current->flags & PF_EXITING))
2026 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002027 if (current->task_works) {
2028 __set_current_state(TASK_RUNNING);
2029 task_work_run();
2030 return true;
2031 }
2032
2033 return false;
2034}
2035
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002036static void io_iopoll_queue(struct list_head *again)
2037{
2038 struct io_kiocb *req;
2039
2040 do {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002041 req = list_first_entry(again, struct io_kiocb, inflight_entry);
2042 list_del(&req->inflight_entry);
Pavel Begunkov81b68a52020-07-30 18:43:46 +03002043 __io_complete_rw(req, -EAGAIN, 0, NULL);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002044 } while (!list_empty(again));
2045}
2046
Jens Axboedef596e2019-01-09 08:59:42 -07002047/*
2048 * Find and free completed poll iocbs
2049 */
2050static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2051 struct list_head *done)
2052{
Jens Axboe8237e042019-12-28 10:48:22 -07002053 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002054 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002055 LIST_HEAD(again);
2056
2057 /* order with ->result store in io_complete_rw_iopoll() */
2058 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002059
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002060 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002061 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002062 int cflags = 0;
2063
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002064 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002065 if (READ_ONCE(req->result) == -EAGAIN) {
Jens Axboe56450c22020-08-26 18:58:26 -06002066 req->result = 0;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002067 req->iopoll_completed = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002068 list_move_tail(&req->inflight_entry, &again);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002069 continue;
2070 }
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002071 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002072
Jens Axboebcda7ba2020-02-23 16:42:51 -07002073 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002074 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002075
2076 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002077 (*nr_events)++;
2078
Pavel Begunkovc3524382020-06-28 12:52:32 +03002079 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002080 io_req_free_batch(&rb, req);
Jens Axboedef596e2019-01-09 08:59:42 -07002081 }
Jens Axboedef596e2019-01-09 08:59:42 -07002082
Jens Axboe09bb8392019-03-13 12:39:28 -06002083 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08002084 if (ctx->flags & IORING_SETUP_SQPOLL)
2085 io_cqring_ev_posted(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002086 io_req_free_batch_finish(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002087
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002088 if (!list_empty(&again))
2089 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002090}
2091
Jens Axboedef596e2019-01-09 08:59:42 -07002092static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2093 long min)
2094{
2095 struct io_kiocb *req, *tmp;
2096 LIST_HEAD(done);
2097 bool spin;
2098 int ret;
2099
2100 /*
2101 * Only spin for completions if we don't have multiple devices hanging
2102 * off our complete list, and we're under the requested amount.
2103 */
2104 spin = !ctx->poll_multi_file && *nr_events < min;
2105
2106 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002107 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002108 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002109
2110 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002111 * Move completed and retryable entries to our local lists.
2112 * If we find a request that requires polling, break out
2113 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002114 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002115 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002116 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002117 continue;
2118 }
2119 if (!list_empty(&done))
2120 break;
2121
2122 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2123 if (ret < 0)
2124 break;
2125
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002126 /* iopoll may have completed current req */
2127 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002128 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002129
Jens Axboedef596e2019-01-09 08:59:42 -07002130 if (ret && spin)
2131 spin = false;
2132 ret = 0;
2133 }
2134
2135 if (!list_empty(&done))
2136 io_iopoll_complete(ctx, nr_events, &done);
2137
2138 return ret;
2139}
2140
2141/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002142 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002143 * non-spinning poll check - we'll still enter the driver poll loop, but only
2144 * as a non-spinning completion check.
2145 */
2146static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2147 long min)
2148{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002149 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002150 int ret;
2151
2152 ret = io_do_iopoll(ctx, nr_events, min);
2153 if (ret < 0)
2154 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002155 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002156 return 0;
2157 }
2158
2159 return 1;
2160}
2161
2162/*
2163 * We can't just wait for polled events to come to us, we have to actively
2164 * find and complete them.
2165 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002166static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002167{
2168 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2169 return;
2170
2171 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002172 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002173 unsigned int nr_events = 0;
2174
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002175 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002176
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002177 /* let it sleep and repeat later if can't complete a request */
2178 if (nr_events == 0)
2179 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002180 /*
2181 * Ensure we allow local-to-the-cpu processing to take place,
2182 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002183 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002184 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002185 if (need_resched()) {
2186 mutex_unlock(&ctx->uring_lock);
2187 cond_resched();
2188 mutex_lock(&ctx->uring_lock);
2189 }
Jens Axboedef596e2019-01-09 08:59:42 -07002190 }
2191 mutex_unlock(&ctx->uring_lock);
2192}
2193
Pavel Begunkov7668b922020-07-07 16:36:21 +03002194static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002195{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002196 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002197 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002198
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002199 /*
2200 * We disallow the app entering submit/complete with polling, but we
2201 * still need to lock the ring to prevent racing with polled issue
2202 * that got punted to a workqueue.
2203 */
2204 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002205 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002206 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002207 * Don't enter poll loop if we already have events pending.
2208 * If we do, we can potentially be spinning for commands that
2209 * already triggered a CQE (eg in error).
2210 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002211 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06002212 break;
2213
2214 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002215 * If a submit got punted to a workqueue, we can have the
2216 * application entering polling for a command before it gets
2217 * issued. That app will hold the uring_lock for the duration
2218 * of the poll right here, so we need to take a breather every
2219 * now and then to ensure that the issue has a chance to add
2220 * the poll to the issued list. Otherwise we can spin here
2221 * forever, while the workqueue is stuck trying to acquire the
2222 * very same mutex.
2223 */
2224 if (!(++iters & 7)) {
2225 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002226 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002227 mutex_lock(&ctx->uring_lock);
2228 }
2229
Pavel Begunkov7668b922020-07-07 16:36:21 +03002230 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002231 if (ret <= 0)
2232 break;
2233 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002234 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002235
Jens Axboe500f9fb2019-08-19 12:15:59 -06002236 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002237 return ret;
2238}
2239
Jens Axboe491381ce2019-10-17 09:20:46 -06002240static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002241{
Jens Axboe491381ce2019-10-17 09:20:46 -06002242 /*
2243 * Tell lockdep we inherited freeze protection from submission
2244 * thread.
2245 */
2246 if (req->flags & REQ_F_ISREG) {
2247 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002248
Jens Axboe491381ce2019-10-17 09:20:46 -06002249 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002250 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002251 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002252}
2253
Jens Axboea1d7c392020-06-22 11:09:46 -06002254static void io_complete_rw_common(struct kiocb *kiocb, long res,
2255 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002256{
Jens Axboe9adbd452019-12-20 08:45:55 -07002257 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002258 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002259
Jens Axboe491381ce2019-10-17 09:20:46 -06002260 if (kiocb->ki_flags & IOCB_WRITE)
2261 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002262
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002263 if (res != req->result)
2264 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002265 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002266 cflags = io_put_rw_kbuf(req);
Jens Axboea1d7c392020-06-22 11:09:46 -06002267 __io_req_complete(req, res, cflags, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002268}
2269
Jens Axboeb63534c2020-06-04 11:28:00 -06002270#ifdef CONFIG_BLOCK
2271static bool io_resubmit_prep(struct io_kiocb *req, int error)
2272{
2273 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2274 ssize_t ret = -ECANCELED;
2275 struct iov_iter iter;
2276 int rw;
2277
2278 if (error) {
2279 ret = error;
2280 goto end_req;
2281 }
2282
2283 switch (req->opcode) {
2284 case IORING_OP_READV:
2285 case IORING_OP_READ_FIXED:
2286 case IORING_OP_READ:
2287 rw = READ;
2288 break;
2289 case IORING_OP_WRITEV:
2290 case IORING_OP_WRITE_FIXED:
2291 case IORING_OP_WRITE:
2292 rw = WRITE;
2293 break;
2294 default:
2295 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2296 req->opcode);
2297 goto end_req;
2298 }
2299
Jens Axboe8f3d7492020-09-14 09:28:14 -06002300 if (!req->io) {
2301 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2302 if (ret < 0)
2303 goto end_req;
2304 ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
2305 if (!ret)
2306 return true;
2307 kfree(iovec);
2308 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002309 return true;
Jens Axboe8f3d7492020-09-14 09:28:14 -06002310 }
Jens Axboeb63534c2020-06-04 11:28:00 -06002311end_req:
Jens Axboeb63534c2020-06-04 11:28:00 -06002312 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06002313 io_req_complete(req, ret);
Jens Axboeb63534c2020-06-04 11:28:00 -06002314 return false;
2315}
Jens Axboeb63534c2020-06-04 11:28:00 -06002316#endif
2317
2318static bool io_rw_reissue(struct io_kiocb *req, long res)
2319{
2320#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002321 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002322 int ret;
2323
Jens Axboe355afae2020-09-02 09:30:31 -06002324 if (!S_ISBLK(mode) && !S_ISREG(mode))
2325 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002326 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2327 return false;
2328
Jens Axboefdee9462020-08-27 16:46:24 -06002329 ret = io_sq_thread_acquire_mm(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002330
Jens Axboefdee9462020-08-27 16:46:24 -06002331 if (io_resubmit_prep(req, ret)) {
2332 refcount_inc(&req->refs);
2333 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002334 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002335 }
2336
Jens Axboeb63534c2020-06-04 11:28:00 -06002337#endif
2338 return false;
2339}
2340
Jens Axboea1d7c392020-06-22 11:09:46 -06002341static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2342 struct io_comp_state *cs)
2343{
2344 if (!io_rw_reissue(req, res))
2345 io_complete_rw_common(&req->rw.kiocb, res, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002346}
2347
2348static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2349{
Jens Axboe9adbd452019-12-20 08:45:55 -07002350 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002351
Jens Axboea1d7c392020-06-22 11:09:46 -06002352 __io_complete_rw(req, res, res2, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002353}
2354
Jens Axboedef596e2019-01-09 08:59:42 -07002355static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2356{
Jens Axboe9adbd452019-12-20 08:45:55 -07002357 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002358
Jens Axboe491381ce2019-10-17 09:20:46 -06002359 if (kiocb->ki_flags & IOCB_WRITE)
2360 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002361
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002362 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002363 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002364
2365 WRITE_ONCE(req->result, res);
2366 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002367 smp_wmb();
2368 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002369}
2370
2371/*
2372 * After the iocb has been issued, it's safe to be found on the poll list.
2373 * Adding the kiocb to the list AFTER submission ensures that we don't
2374 * find it from a io_iopoll_getevents() thread before the issuer is done
2375 * accessing the kiocb cookie.
2376 */
2377static void io_iopoll_req_issued(struct io_kiocb *req)
2378{
2379 struct io_ring_ctx *ctx = req->ctx;
2380
2381 /*
2382 * Track whether we have multiple files in our lists. This will impact
2383 * how we do polling eventually, not spinning if we're on potentially
2384 * different devices.
2385 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002386 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002387 ctx->poll_multi_file = false;
2388 } else if (!ctx->poll_multi_file) {
2389 struct io_kiocb *list_req;
2390
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002391 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002392 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002393 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002394 ctx->poll_multi_file = true;
2395 }
2396
2397 /*
2398 * For fast devices, IO may have already completed. If it has, add
2399 * it to the front so we find it first.
2400 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002401 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002402 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002403 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002404 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002405
2406 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2407 wq_has_sleeper(&ctx->sqo_wait))
2408 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002409}
2410
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002411static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002412{
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002413 if (state->has_refs)
2414 fput_many(state->file, state->has_refs);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002415 state->file = NULL;
2416}
2417
2418static inline void io_state_file_put(struct io_submit_state *state)
2419{
2420 if (state->file)
2421 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002422}
2423
2424/*
2425 * Get as many references to a file as we have IOs left in this submission,
2426 * assuming most submissions are for one file, or at least that each file
2427 * has more than one submission.
2428 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002429static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002430{
2431 if (!state)
2432 return fget(fd);
2433
2434 if (state->file) {
2435 if (state->fd == fd) {
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002436 state->has_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002437 state->ios_left--;
2438 return state->file;
2439 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002440 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002441 }
2442 state->file = fget_many(fd, state->ios_left);
2443 if (!state->file)
2444 return NULL;
2445
2446 state->fd = fd;
Jens Axboe9a56a232019-01-09 09:06:50 -07002447 state->ios_left--;
Pavel Begunkov06ef3602020-07-16 23:28:33 +03002448 state->has_refs = state->ios_left;
Jens Axboe9a56a232019-01-09 09:06:50 -07002449 return state->file;
2450}
2451
Jens Axboe4503b762020-06-01 10:00:27 -06002452static bool io_bdev_nowait(struct block_device *bdev)
2453{
2454#ifdef CONFIG_BLOCK
2455 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2456#else
2457 return true;
2458#endif
2459}
2460
Jens Axboe2b188cc2019-01-07 10:46:33 -07002461/*
2462 * If we tracked the file through the SCM inflight mechanism, we could support
2463 * any file. For now, just ensure that anything potentially problematic is done
2464 * inline.
2465 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002466static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002467{
2468 umode_t mode = file_inode(file)->i_mode;
2469
Jens Axboe4503b762020-06-01 10:00:27 -06002470 if (S_ISBLK(mode)) {
2471 if (io_bdev_nowait(file->f_inode->i_bdev))
2472 return true;
2473 return false;
2474 }
2475 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002476 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002477 if (S_ISREG(mode)) {
2478 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2479 file->f_op != &io_uring_fops)
2480 return true;
2481 return false;
2482 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002483
Jens Axboec5b85622020-06-09 19:23:05 -06002484 /* any ->read/write should understand O_NONBLOCK */
2485 if (file->f_flags & O_NONBLOCK)
2486 return true;
2487
Jens Axboeaf197f52020-04-28 13:15:06 -06002488 if (!(file->f_mode & FMODE_NOWAIT))
2489 return false;
2490
2491 if (rw == READ)
2492 return file->f_op->read_iter != NULL;
2493
2494 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002495}
2496
Jens Axboe3529d8c2019-12-19 18:24:38 -07002497static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2498 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002499{
Jens Axboedef596e2019-01-09 08:59:42 -07002500 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002501 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002502 unsigned ioprio;
2503 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002504
Jens Axboe491381ce2019-10-17 09:20:46 -06002505 if (S_ISREG(file_inode(req->file)->i_mode))
2506 req->flags |= REQ_F_ISREG;
2507
Jens Axboe2b188cc2019-01-07 10:46:33 -07002508 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002509 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2510 req->flags |= REQ_F_CUR_POS;
2511 kiocb->ki_pos = req->file->f_pos;
2512 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002513 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002514 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2515 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2516 if (unlikely(ret))
2517 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002518
2519 ioprio = READ_ONCE(sqe->ioprio);
2520 if (ioprio) {
2521 ret = ioprio_check_cap(ioprio);
2522 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002523 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002524
2525 kiocb->ki_ioprio = ioprio;
2526 } else
2527 kiocb->ki_ioprio = get_current_ioprio();
2528
Stefan Bühler8449eed2019-04-27 20:34:19 +02002529 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002530 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002531 req->flags |= REQ_F_NOWAIT;
2532
2533 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002534 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002535
Jens Axboedef596e2019-01-09 08:59:42 -07002536 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002537 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2538 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002539 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002540
Jens Axboedef596e2019-01-09 08:59:42 -07002541 kiocb->ki_flags |= IOCB_HIPRI;
2542 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002543 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002544 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002545 if (kiocb->ki_flags & IOCB_HIPRI)
2546 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002547 kiocb->ki_complete = io_complete_rw;
2548 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002549
Jens Axboe3529d8c2019-12-19 18:24:38 -07002550 req->rw.addr = READ_ONCE(sqe->addr);
2551 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002552 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002553 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002554}
2555
2556static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2557{
2558 switch (ret) {
2559 case -EIOCBQUEUED:
2560 break;
2561 case -ERESTARTSYS:
2562 case -ERESTARTNOINTR:
2563 case -ERESTARTNOHAND:
2564 case -ERESTART_RESTARTBLOCK:
2565 /*
2566 * We can't just restart the syscall, since previously
2567 * submitted sqes may already be in progress. Just fail this
2568 * IO with EINTR.
2569 */
2570 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002571 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002572 default:
2573 kiocb->ki_complete(kiocb, ret, 0);
2574 }
2575}
2576
Jens Axboea1d7c392020-06-22 11:09:46 -06002577static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2578 struct io_comp_state *cs)
Jens Axboeba816ad2019-09-28 11:36:45 -06002579{
Jens Axboeba042912019-12-25 16:33:42 -07002580 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2581
Jens Axboe227c0c92020-08-13 11:51:40 -06002582 /* add previously done IO, if any */
2583 if (req->io && req->io->rw.bytes_done > 0) {
2584 if (ret < 0)
2585 ret = req->io->rw.bytes_done;
2586 else
2587 ret += req->io->rw.bytes_done;
2588 }
2589
Jens Axboeba042912019-12-25 16:33:42 -07002590 if (req->flags & REQ_F_CUR_POS)
2591 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002592 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboea1d7c392020-06-22 11:09:46 -06002593 __io_complete_rw(req, ret, 0, cs);
Jens Axboeba816ad2019-09-28 11:36:45 -06002594 else
2595 io_rw_done(kiocb, ret);
2596}
2597
Jens Axboe9adbd452019-12-20 08:45:55 -07002598static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002599 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002600{
Jens Axboe9adbd452019-12-20 08:45:55 -07002601 struct io_ring_ctx *ctx = req->ctx;
2602 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002603 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002604 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002605 size_t offset;
2606 u64 buf_addr;
2607
2608 /* attempt to use fixed buffers without having provided iovecs */
2609 if (unlikely(!ctx->user_bufs))
2610 return -EFAULT;
2611
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002612 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002613 if (unlikely(buf_index >= ctx->nr_user_bufs))
2614 return -EFAULT;
2615
2616 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2617 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002618 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002619
2620 /* overflow */
2621 if (buf_addr + len < buf_addr)
2622 return -EFAULT;
2623 /* not inside the mapped region */
2624 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2625 return -EFAULT;
2626
2627 /*
2628 * May not be a start of buffer, set size appropriately
2629 * and advance us to the beginning.
2630 */
2631 offset = buf_addr - imu->ubuf;
2632 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002633
2634 if (offset) {
2635 /*
2636 * Don't use iov_iter_advance() here, as it's really slow for
2637 * using the latter parts of a big fixed buffer - it iterates
2638 * over each segment manually. We can cheat a bit here, because
2639 * we know that:
2640 *
2641 * 1) it's a BVEC iter, we set it up
2642 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2643 * first and last bvec
2644 *
2645 * So just find our index, and adjust the iterator afterwards.
2646 * If the offset is within the first bvec (or the whole first
2647 * bvec, just use iov_iter_advance(). This makes it easier
2648 * since we can just skip the first segment, which may not
2649 * be PAGE_SIZE aligned.
2650 */
2651 const struct bio_vec *bvec = imu->bvec;
2652
2653 if (offset <= bvec->bv_len) {
2654 iov_iter_advance(iter, offset);
2655 } else {
2656 unsigned long seg_skip;
2657
2658 /* skip first vec */
2659 offset -= bvec->bv_len;
2660 seg_skip = 1 + (offset >> PAGE_SHIFT);
2661
2662 iter->bvec = bvec + seg_skip;
2663 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002664 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002665 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002666 }
2667 }
2668
Jens Axboe5e559562019-11-13 16:12:46 -07002669 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002670}
2671
Jens Axboebcda7ba2020-02-23 16:42:51 -07002672static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2673{
2674 if (needs_lock)
2675 mutex_unlock(&ctx->uring_lock);
2676}
2677
2678static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2679{
2680 /*
2681 * "Normal" inline submissions always hold the uring_lock, since we
2682 * grab it from the system call. Same is true for the SQPOLL offload.
2683 * The only exception is when we've detached the request and issue it
2684 * from an async worker thread, grab the lock for that case.
2685 */
2686 if (needs_lock)
2687 mutex_lock(&ctx->uring_lock);
2688}
2689
2690static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2691 int bgid, struct io_buffer *kbuf,
2692 bool needs_lock)
2693{
2694 struct io_buffer *head;
2695
2696 if (req->flags & REQ_F_BUFFER_SELECTED)
2697 return kbuf;
2698
2699 io_ring_submit_lock(req->ctx, needs_lock);
2700
2701 lockdep_assert_held(&req->ctx->uring_lock);
2702
2703 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2704 if (head) {
2705 if (!list_empty(&head->list)) {
2706 kbuf = list_last_entry(&head->list, struct io_buffer,
2707 list);
2708 list_del(&kbuf->list);
2709 } else {
2710 kbuf = head;
2711 idr_remove(&req->ctx->io_buffer_idr, bgid);
2712 }
2713 if (*len > kbuf->len)
2714 *len = kbuf->len;
2715 } else {
2716 kbuf = ERR_PTR(-ENOBUFS);
2717 }
2718
2719 io_ring_submit_unlock(req->ctx, needs_lock);
2720
2721 return kbuf;
2722}
2723
Jens Axboe4d954c22020-02-27 07:31:19 -07002724static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2725 bool needs_lock)
2726{
2727 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002728 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002729
2730 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002731 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002732 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2733 if (IS_ERR(kbuf))
2734 return kbuf;
2735 req->rw.addr = (u64) (unsigned long) kbuf;
2736 req->flags |= REQ_F_BUFFER_SELECTED;
2737 return u64_to_user_ptr(kbuf->addr);
2738}
2739
2740#ifdef CONFIG_COMPAT
2741static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2742 bool needs_lock)
2743{
2744 struct compat_iovec __user *uiov;
2745 compat_ssize_t clen;
2746 void __user *buf;
2747 ssize_t len;
2748
2749 uiov = u64_to_user_ptr(req->rw.addr);
2750 if (!access_ok(uiov, sizeof(*uiov)))
2751 return -EFAULT;
2752 if (__get_user(clen, &uiov->iov_len))
2753 return -EFAULT;
2754 if (clen < 0)
2755 return -EINVAL;
2756
2757 len = clen;
2758 buf = io_rw_buffer_select(req, &len, needs_lock);
2759 if (IS_ERR(buf))
2760 return PTR_ERR(buf);
2761 iov[0].iov_base = buf;
2762 iov[0].iov_len = (compat_size_t) len;
2763 return 0;
2764}
2765#endif
2766
2767static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2768 bool needs_lock)
2769{
2770 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2771 void __user *buf;
2772 ssize_t len;
2773
2774 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2775 return -EFAULT;
2776
2777 len = iov[0].iov_len;
2778 if (len < 0)
2779 return -EINVAL;
2780 buf = io_rw_buffer_select(req, &len, needs_lock);
2781 if (IS_ERR(buf))
2782 return PTR_ERR(buf);
2783 iov[0].iov_base = buf;
2784 iov[0].iov_len = len;
2785 return 0;
2786}
2787
2788static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2789 bool needs_lock)
2790{
Jens Axboedddb3e22020-06-04 11:27:01 -06002791 if (req->flags & REQ_F_BUFFER_SELECTED) {
2792 struct io_buffer *kbuf;
2793
2794 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2795 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2796 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002797 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002798 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002799 if (!req->rw.len)
2800 return 0;
2801 else if (req->rw.len > 1)
2802 return -EINVAL;
2803
2804#ifdef CONFIG_COMPAT
2805 if (req->ctx->compat)
2806 return io_compat_import(req, iov, needs_lock);
2807#endif
2808
2809 return __io_iov_buffer_select(req, iov, needs_lock);
2810}
2811
Jens Axboe8452fd02020-08-18 13:58:33 -07002812static ssize_t __io_import_iovec(int rw, struct io_kiocb *req,
2813 struct iovec **iovec, struct iov_iter *iter,
2814 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002815{
Jens Axboe9adbd452019-12-20 08:45:55 -07002816 void __user *buf = u64_to_user_ptr(req->rw.addr);
2817 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002818 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002819 u8 opcode;
2820
Jens Axboed625c6e2019-12-17 19:53:05 -07002821 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002822 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002823 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002824 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002825 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002826
Jens Axboebcda7ba2020-02-23 16:42:51 -07002827 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002828 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002829 return -EINVAL;
2830
Jens Axboe3a6820f2019-12-22 15:19:35 -07002831 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002832 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002833 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03002834 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07002835 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002836 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002837 }
2838
Jens Axboe3a6820f2019-12-22 15:19:35 -07002839 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2840 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002841 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002842 }
2843
Jens Axboe4d954c22020-02-27 07:31:19 -07002844 if (req->flags & REQ_F_BUFFER_SELECT) {
2845 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002846 if (!ret) {
2847 ret = (*iovec)->iov_len;
2848 iov_iter_init(iter, rw, *iovec, 1, ret);
2849 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002850 *iovec = NULL;
2851 return ret;
2852 }
2853
Jens Axboe2b188cc2019-01-07 10:46:33 -07002854#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002855 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002856 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2857 iovec, iter);
2858#endif
2859
2860 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2861}
2862
Jens Axboe8452fd02020-08-18 13:58:33 -07002863static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2864 struct iovec **iovec, struct iov_iter *iter,
2865 bool needs_lock)
2866{
2867 if (!req->io)
2868 return __io_import_iovec(rw, req, iovec, iter, needs_lock);
2869 *iovec = NULL;
2870 return iov_iter_count(&req->io->rw.iter);
2871}
2872
Jens Axboe0fef9482020-08-26 10:36:20 -06002873static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
2874{
2875 return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
2876}
2877
Jens Axboe32960612019-09-23 11:05:34 -06002878/*
2879 * For files that don't have ->read_iter() and ->write_iter(), handle them
2880 * by looping over ->read() or ->write() manually.
2881 */
2882static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2883 struct iov_iter *iter)
2884{
2885 ssize_t ret = 0;
2886
2887 /*
2888 * Don't support polled IO through this interface, and we can't
2889 * support non-blocking either. For the latter, this just causes
2890 * the kiocb to be handled from an async context.
2891 */
2892 if (kiocb->ki_flags & IOCB_HIPRI)
2893 return -EOPNOTSUPP;
2894 if (kiocb->ki_flags & IOCB_NOWAIT)
2895 return -EAGAIN;
2896
2897 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002898 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002899 ssize_t nr;
2900
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002901 if (!iov_iter_is_bvec(iter)) {
2902 iovec = iov_iter_iovec(iter);
2903 } else {
2904 /* fixed buffers import bvec */
2905 iovec.iov_base = kmap(iter->bvec->bv_page)
2906 + iter->iov_offset;
2907 iovec.iov_len = min(iter->count,
2908 iter->bvec->bv_len - iter->iov_offset);
2909 }
2910
Jens Axboe32960612019-09-23 11:05:34 -06002911 if (rw == READ) {
2912 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002913 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002914 } else {
2915 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06002916 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06002917 }
2918
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002919 if (iov_iter_is_bvec(iter))
2920 kunmap(iter->bvec->bv_page);
2921
Jens Axboe32960612019-09-23 11:05:34 -06002922 if (nr < 0) {
2923 if (!ret)
2924 ret = nr;
2925 break;
2926 }
2927 ret += nr;
2928 if (nr != iovec.iov_len)
2929 break;
2930 iov_iter_advance(iter, nr);
2931 }
2932
2933 return ret;
2934}
2935
Jens Axboeff6165b2020-08-13 09:47:43 -06002936static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
2937 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07002938{
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002939 struct io_async_rw *rw = &req->io->rw;
2940
Jens Axboeff6165b2020-08-13 09:47:43 -06002941 memcpy(&rw->iter, iter, sizeof(*iter));
2942 rw->free_iovec = NULL;
Jens Axboe227c0c92020-08-13 11:51:40 -06002943 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06002944 /* can only be fixed buffers, no need to do anything */
2945 if (iter->type == ITER_BVEC)
2946 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03002947 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06002948 unsigned iov_off = 0;
2949
2950 rw->iter.iov = rw->fast_iov;
2951 if (iter->iov != fast_iov) {
2952 iov_off = iter->iov - fast_iov;
2953 rw->iter.iov += iov_off;
2954 }
2955 if (rw->fast_iov != fast_iov)
2956 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002957 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002958 } else {
Jens Axboeff6165b2020-08-13 09:47:43 -06002959 rw->free_iovec = iovec;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002960 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002961 }
2962}
2963
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002964static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2965{
2966 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2967 return req->io == NULL;
2968}
2969
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002970static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002971{
Jens Axboed3656342019-12-18 09:50:26 -07002972 if (!io_op_defs[req->opcode].async_ctx)
2973 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002974
2975 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002976}
2977
Jens Axboeff6165b2020-08-13 09:47:43 -06002978static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
2979 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06002980 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002981{
Jens Axboe227c0c92020-08-13 11:51:40 -06002982 if (!force && !io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002983 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002984 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002985 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002986 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002987
Jens Axboeff6165b2020-08-13 09:47:43 -06002988 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07002989 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002990 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002991}
2992
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03002993static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
2994 bool force_nonblock)
2995{
Jens Axboeff6165b2020-08-13 09:47:43 -06002996 struct io_async_rw *iorw = &req->io->rw;
Jens Axboec183edf2020-09-04 22:36:52 -06002997 struct iovec *iov;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03002998 ssize_t ret;
2999
Jens Axboec183edf2020-09-04 22:36:52 -06003000 iorw->iter.iov = iov = iorw->fast_iov;
3001 ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003002 if (unlikely(ret < 0))
3003 return ret;
3004
Jens Axboec183edf2020-09-04 22:36:52 -06003005 iorw->iter.iov = iov;
Jens Axboeff6165b2020-08-13 09:47:43 -06003006 io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003007 return 0;
3008}
3009
Jens Axboe3529d8c2019-12-19 18:24:38 -07003010static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3011 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003012{
3013 ssize_t ret;
3014
Jens Axboe3529d8c2019-12-19 18:24:38 -07003015 ret = io_prep_rw(req, sqe, force_nonblock);
3016 if (ret)
3017 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003018
Jens Axboe3529d8c2019-12-19 18:24:38 -07003019 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3020 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003021
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003022 /* either don't need iovec imported or already have it */
3023 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003024 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003025 return io_rw_prep_async(req, READ, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003026}
3027
Jens Axboec1dd91d2020-08-03 16:43:59 -06003028/*
3029 * This is our waitqueue callback handler, registered through lock_page_async()
3030 * when we initially tried to do the IO with the iocb armed our waitqueue.
3031 * This gets called when the page is unlocked, and we generally expect that to
3032 * happen when the page IO is completed and the page is now uptodate. This will
3033 * queue a task_work based retry of the operation, attempting to copy the data
3034 * again. If the latter fails because the page was NOT uptodate, then we will
3035 * do a thread based blocking retry of the operation. That's the unexpected
3036 * slow path.
3037 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003038static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3039 int sync, void *arg)
3040{
3041 struct wait_page_queue *wpq;
3042 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003043 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003044 int ret;
3045
3046 wpq = container_of(wait, struct wait_page_queue, wait);
3047
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003048 if (!wake_page_match(wpq, key))
3049 return 0;
3050
Hao Xuc8d317a2020-09-29 20:00:45 +08003051 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003052 list_del_init(&wait->entry);
3053
Pavel Begunkove7375122020-07-12 20:42:04 +03003054 init_task_work(&req->task_work, io_req_task_submit);
Jens Axboe6d816e02020-08-11 08:04:14 -06003055 percpu_ref_get(&req->ctx->refs);
3056
Jens Axboebcf5a062020-05-22 09:24:42 -06003057 /* submit ref gets dropped, acquire a new one */
3058 refcount_inc(&req->refs);
Jens Axboefd7d6de2020-08-23 11:00:37 -06003059 ret = io_req_task_work_add(req, &req->task_work, true);
Jens Axboebcf5a062020-05-22 09:24:42 -06003060 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06003061 struct task_struct *tsk;
3062
Jens Axboebcf5a062020-05-22 09:24:42 -06003063 /* queue just for cancelation */
Pavel Begunkove7375122020-07-12 20:42:04 +03003064 init_task_work(&req->task_work, io_req_task_cancel);
Jens Axboebcf5a062020-05-22 09:24:42 -06003065 tsk = io_wq_get_task(req->ctx->io_wq);
Pavel Begunkove7375122020-07-12 20:42:04 +03003066 task_work_add(tsk, &req->task_work, 0);
Jens Axboec2c4c832020-07-01 15:37:11 -06003067 wake_up_process(tsk);
Jens Axboebcf5a062020-05-22 09:24:42 -06003068 }
Jens Axboebcf5a062020-05-22 09:24:42 -06003069 return 1;
3070}
3071
Jens Axboec1dd91d2020-08-03 16:43:59 -06003072/*
3073 * This controls whether a given IO request should be armed for async page
3074 * based retry. If we return false here, the request is handed to the async
3075 * worker threads for retry. If we're doing buffered reads on a regular file,
3076 * we prepare a private wait_page_queue entry and retry the operation. This
3077 * will either succeed because the page is now uptodate and unlocked, or it
3078 * will register a callback when the page is unlocked at IO completion. Through
3079 * that callback, io_uring uses task_work to setup a retry of the operation.
3080 * That retry will attempt the buffered read again. The retry will generally
3081 * succeed, or in rare cases where it fails, we then fall back to using the
3082 * async worker threads for a blocking retry.
3083 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003084static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003085{
Jens Axboe3b2a4432020-08-16 10:58:43 -07003086 struct wait_page_queue *wait = &req->io->rw.wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003087 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003088
3089 /* never retry for NOWAIT, we just complete with -EAGAIN */
3090 if (req->flags & REQ_F_NOWAIT)
3091 return false;
3092
Jens Axboe227c0c92020-08-13 11:51:40 -06003093 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003094 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003095 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003096
Jens Axboebcf5a062020-05-22 09:24:42 -06003097 /*
3098 * just use poll if we can, and don't attempt if the fs doesn't
3099 * support callback based unlocks
3100 */
3101 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3102 return false;
3103
Jens Axboe3b2a4432020-08-16 10:58:43 -07003104 wait->wait.func = io_async_buf_func;
3105 wait->wait.private = req;
3106 wait->wait.flags = 0;
3107 INIT_LIST_HEAD(&wait->wait.entry);
3108 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003109 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003110 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003111 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003112}
3113
3114static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3115{
3116 if (req->file->f_op->read_iter)
3117 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003118 else if (req->file->f_op->read)
3119 return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
3120 else
3121 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003122}
3123
Jens Axboea1d7c392020-06-22 11:09:46 -06003124static int io_read(struct io_kiocb *req, bool force_nonblock,
3125 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003126{
3127 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003128 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003129 struct iov_iter __iter, *iter = &__iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003130 ssize_t io_size, ret, ret2;
Jens Axboe31b51512019-01-18 22:56:34 -07003131 size_t iov_count;
Jens Axboef5cac8b2020-09-14 09:30:38 -06003132 bool no_async;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003133
Jens Axboeff6165b2020-08-13 09:47:43 -06003134 if (req->io)
3135 iter = &req->io->rw.iter;
3136
3137 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003138 if (ret < 0)
3139 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003140 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003141 io_size = ret;
3142 req->result = io_size;
Jens Axboe227c0c92020-08-13 11:51:40 -06003143 ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003144
Jens Axboefd6c2e42019-12-18 12:19:41 -07003145 /* Ensure we clear previously set non-block flag */
3146 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003147 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003148
Pavel Begunkov24c74672020-06-21 13:09:51 +03003149 /* If the file doesn't support async, just async punt */
Jens Axboef5cac8b2020-09-14 09:30:38 -06003150 no_async = force_nonblock && !io_file_supports_async(req->file, READ);
3151 if (no_async)
Jens Axboef67676d2019-12-02 11:03:47 -07003152 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003153
Jens Axboe0fef9482020-08-26 10:36:20 -06003154 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003155 if (unlikely(ret))
3156 goto out_free;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003157
Jens Axboe227c0c92020-08-13 11:51:40 -06003158 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003159
Jens Axboe227c0c92020-08-13 11:51:40 -06003160 if (!ret) {
3161 goto done;
3162 } else if (ret == -EIOCBQUEUED) {
3163 ret = 0;
3164 goto out_free;
3165 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003166 /* IOPOLL retry should happen for io-wq threads */
3167 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003168 goto done;
Jens Axboe355afae2020-09-02 09:30:31 -06003169 /* no retry on NONBLOCK marked file */
3170 if (req->file->f_flags & O_NONBLOCK)
3171 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003172 /* some cases will consume bytes even on error returns */
3173 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003174 ret = 0;
3175 goto copy_iov;
Jens Axboe227c0c92020-08-13 11:51:40 -06003176 } else if (ret < 0) {
Jens Axboe00d23d52020-08-25 12:59:22 -06003177 /* make sure -ERESTARTSYS -> -EINTR is done */
3178 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003179 }
3180
3181 /* read it all, or we did blocking attempt. no retry. */
Jens Axboef91daf52020-08-15 15:58:42 -07003182 if (!iov_iter_count(iter) || !force_nonblock ||
3183 (req->file->f_flags & O_NONBLOCK))
Jens Axboe227c0c92020-08-13 11:51:40 -06003184 goto done;
3185
3186 io_size -= ret;
3187copy_iov:
3188 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
3189 if (ret2) {
3190 ret = ret2;
3191 goto out_free;
3192 }
Jens Axboef5cac8b2020-09-14 09:30:38 -06003193 if (no_async)
3194 return -EAGAIN;
Jens Axboe227c0c92020-08-13 11:51:40 -06003195 /* it's copied and will be cleaned with ->io */
3196 iovec = NULL;
3197 /* now use our persistent iterator, if we aren't already */
3198 iter = &req->io->rw.iter;
3199retry:
3200 req->io->rw.bytes_done += ret;
3201 /* if we can retry, do so with the callbacks armed */
3202 if (!io_rw_should_retry(req)) {
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003203 kiocb->ki_flags &= ~IOCB_WAITQ;
3204 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003205 }
Jens Axboe227c0c92020-08-13 11:51:40 -06003206
3207 /*
3208 * Now retry read with the IOCB_WAITQ parts set in the iocb. If we
3209 * get -EIOCBQUEUED, then we'll get a notification when the desired
3210 * page gets unlocked. We can also get a partial read here, and if we
3211 * do, then just retry at the new offset.
3212 */
3213 ret = io_iter_do_read(req, iter);
3214 if (ret == -EIOCBQUEUED) {
3215 ret = 0;
3216 goto out_free;
3217 } else if (ret > 0 && ret < io_size) {
3218 /* we got some bytes, but not all. retry. */
3219 goto retry;
3220 }
3221done:
3222 kiocb_done(kiocb, ret, cs);
3223 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003224out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003225 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003226 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003227 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003228 return ret;
3229}
3230
Jens Axboe3529d8c2019-12-19 18:24:38 -07003231static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3232 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07003233{
3234 ssize_t ret;
3235
Jens Axboe3529d8c2019-12-19 18:24:38 -07003236 ret = io_prep_rw(req, sqe, force_nonblock);
3237 if (ret)
3238 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003239
Jens Axboe3529d8c2019-12-19 18:24:38 -07003240 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3241 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07003242
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003243 /* either don't need iovec imported or already have it */
3244 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003245 return 0;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003246 return io_rw_prep_async(req, WRITE, force_nonblock);
Jens Axboef67676d2019-12-02 11:03:47 -07003247}
3248
Jens Axboea1d7c392020-06-22 11:09:46 -06003249static int io_write(struct io_kiocb *req, bool force_nonblock,
3250 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003251{
3252 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003253 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003254 struct iov_iter __iter, *iter = &__iter;
Jens Axboe31b51512019-01-18 22:56:34 -07003255 size_t iov_count;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003256 ssize_t ret, ret2, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003257
Jens Axboeff6165b2020-08-13 09:47:43 -06003258 if (req->io)
3259 iter = &req->io->rw.iter;
3260
3261 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07003262 if (ret < 0)
3263 return ret;
Jens Axboeeefdf302020-08-27 16:40:19 -06003264 iov_count = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003265 io_size = ret;
3266 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003267
Jens Axboefd6c2e42019-12-18 12:19:41 -07003268 /* Ensure we clear previously set non-block flag */
3269 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07003270 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003271
Pavel Begunkov24c74672020-06-21 13:09:51 +03003272 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003273 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003274 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003275
Jens Axboe10d59342019-12-09 20:16:22 -07003276 /* file path doesn't support NOWAIT for non-direct_IO */
3277 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3278 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003279 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003280
Jens Axboe0fef9482020-08-26 10:36:20 -06003281 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), iov_count);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003282 if (unlikely(ret))
3283 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003284
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003285 /*
3286 * Open-code file_start_write here to grab freeze protection,
3287 * which will be released by another thread in
3288 * io_complete_rw(). Fool lockdep by telling it the lock got
3289 * released so that it doesn't complain about the held lock when
3290 * we return to userspace.
3291 */
3292 if (req->flags & REQ_F_ISREG) {
3293 __sb_start_write(file_inode(req->file)->i_sb,
3294 SB_FREEZE_WRITE, true);
3295 __sb_writers_release(file_inode(req->file)->i_sb,
3296 SB_FREEZE_WRITE);
3297 }
3298 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003299
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003300 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003301 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003302 else if (req->file->f_op->write)
Jens Axboeff6165b2020-08-13 09:47:43 -06003303 ret2 = loop_rw_iter(WRITE, req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003304 else
3305 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003306
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003307 /*
3308 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3309 * retry them without IOCB_NOWAIT.
3310 */
3311 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3312 ret2 = -EAGAIN;
Jens Axboe355afae2020-09-02 09:30:31 -06003313 /* no retry on NONBLOCK marked file */
3314 if (ret2 == -EAGAIN && (req->file->f_flags & O_NONBLOCK))
3315 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003316 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003317 /* IOPOLL retry should happen for io-wq threads */
3318 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3319 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003320done:
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003321 kiocb_done(kiocb, ret2, cs);
3322 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003323copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003324 /* some cases will consume bytes even on error returns */
3325 iov_iter_revert(iter, iov_count - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003326 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Jens Axboeff6165b2020-08-13 09:47:43 -06003327 if (!ret)
3328 return -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003329 }
Jens Axboe31b51512019-01-18 22:56:34 -07003330out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003331 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003332 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003333 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003334 return ret;
3335}
3336
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003337static int __io_splice_prep(struct io_kiocb *req,
3338 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003339{
3340 struct io_splice* sp = &req->splice;
3341 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
3342 int ret;
3343
3344 if (req->flags & REQ_F_NEED_CLEANUP)
3345 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003346 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3347 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003348
3349 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003350 sp->len = READ_ONCE(sqe->len);
3351 sp->flags = READ_ONCE(sqe->splice_flags);
3352
3353 if (unlikely(sp->flags & ~valid_flags))
3354 return -EINVAL;
3355
3356 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
3357 (sp->flags & SPLICE_F_FD_IN_FIXED));
3358 if (ret)
3359 return ret;
3360 req->flags |= REQ_F_NEED_CLEANUP;
3361
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003362 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3363 /*
3364 * Splice operation will be punted aync, and here need to
3365 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3366 */
3367 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003368 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003369 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003370
3371 return 0;
3372}
3373
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003374static int io_tee_prep(struct io_kiocb *req,
3375 const struct io_uring_sqe *sqe)
3376{
3377 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3378 return -EINVAL;
3379 return __io_splice_prep(req, sqe);
3380}
3381
3382static int io_tee(struct io_kiocb *req, bool force_nonblock)
3383{
3384 struct io_splice *sp = &req->splice;
3385 struct file *in = sp->file_in;
3386 struct file *out = sp->file_out;
3387 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3388 long ret = 0;
3389
3390 if (force_nonblock)
3391 return -EAGAIN;
3392 if (sp->len)
3393 ret = do_tee(in, out, sp->len, flags);
3394
3395 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3396 req->flags &= ~REQ_F_NEED_CLEANUP;
3397
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003398 if (ret != sp->len)
3399 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003400 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003401 return 0;
3402}
3403
3404static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3405{
3406 struct io_splice* sp = &req->splice;
3407
3408 sp->off_in = READ_ONCE(sqe->splice_off_in);
3409 sp->off_out = READ_ONCE(sqe->off);
3410 return __io_splice_prep(req, sqe);
3411}
3412
Pavel Begunkov014db002020-03-03 21:33:12 +03003413static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003414{
3415 struct io_splice *sp = &req->splice;
3416 struct file *in = sp->file_in;
3417 struct file *out = sp->file_out;
3418 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3419 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003420 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003421
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003422 if (force_nonblock)
3423 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003424
3425 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3426 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003427
Jens Axboe948a7742020-05-17 14:21:38 -06003428 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003429 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003430
3431 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3432 req->flags &= ~REQ_F_NEED_CLEANUP;
3433
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003434 if (ret != sp->len)
3435 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003436 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003437 return 0;
3438}
3439
Jens Axboe2b188cc2019-01-07 10:46:33 -07003440/*
3441 * IORING_OP_NOP just posts a completion event, nothing else.
3442 */
Jens Axboe229a7b62020-06-22 10:13:11 -06003443static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003444{
3445 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003446
Jens Axboedef596e2019-01-09 08:59:42 -07003447 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3448 return -EINVAL;
3449
Jens Axboe229a7b62020-06-22 10:13:11 -06003450 __io_req_complete(req, 0, 0, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003451 return 0;
3452}
3453
Jens Axboe3529d8c2019-12-19 18:24:38 -07003454static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003455{
Jens Axboe6b063142019-01-10 22:13:58 -07003456 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003457
Jens Axboe09bb8392019-03-13 12:39:28 -06003458 if (!req->file)
3459 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003460
Jens Axboe6b063142019-01-10 22:13:58 -07003461 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003462 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003463 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003464 return -EINVAL;
3465
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003466 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3467 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3468 return -EINVAL;
3469
3470 req->sync.off = READ_ONCE(sqe->off);
3471 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003472 return 0;
3473}
3474
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003475static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003476{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003477 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003478 int ret;
3479
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003480 /* fsync always requires a blocking context */
3481 if (force_nonblock)
3482 return -EAGAIN;
3483
Jens Axboe9adbd452019-12-20 08:45:55 -07003484 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003485 end > 0 ? end : LLONG_MAX,
3486 req->sync.flags & IORING_FSYNC_DATASYNC);
3487 if (ret < 0)
3488 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003489 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003490 return 0;
3491}
3492
Jens Axboed63d1b52019-12-10 10:38:56 -07003493static int io_fallocate_prep(struct io_kiocb *req,
3494 const struct io_uring_sqe *sqe)
3495{
3496 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3497 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003498 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3499 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003500
3501 req->sync.off = READ_ONCE(sqe->off);
3502 req->sync.len = READ_ONCE(sqe->addr);
3503 req->sync.mode = READ_ONCE(sqe->len);
3504 return 0;
3505}
3506
Pavel Begunkov014db002020-03-03 21:33:12 +03003507static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003508{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003509 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003510
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003511 /* fallocate always requiring blocking context */
3512 if (force_nonblock)
3513 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003514 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3515 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003516 if (ret < 0)
3517 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003518 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003519 return 0;
3520}
3521
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003522static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003523{
Jens Axboef8748882020-01-08 17:47:02 -07003524 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003525 int ret;
3526
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003527 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003528 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003529 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003530 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003531
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003532 /* open.how should be already initialised */
3533 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003534 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003535
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003536 req->open.dfd = READ_ONCE(sqe->fd);
3537 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003538 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003539 if (IS_ERR(req->open.filename)) {
3540 ret = PTR_ERR(req->open.filename);
3541 req->open.filename = NULL;
3542 return ret;
3543 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003544 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003545 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003546 return 0;
3547}
3548
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003549static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3550{
3551 u64 flags, mode;
3552
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003553 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3554 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003555 if (req->flags & REQ_F_NEED_CLEANUP)
3556 return 0;
3557 mode = READ_ONCE(sqe->len);
3558 flags = READ_ONCE(sqe->open_flags);
3559 req->open.how = build_open_how(flags, mode);
3560 return __io_openat_prep(req, sqe);
3561}
3562
Jens Axboecebdb982020-01-08 17:59:24 -07003563static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3564{
3565 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003566 size_t len;
3567 int ret;
3568
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003569 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3570 return -EINVAL;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003571 if (req->flags & REQ_F_NEED_CLEANUP)
3572 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003573 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3574 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003575 if (len < OPEN_HOW_SIZE_VER0)
3576 return -EINVAL;
3577
3578 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3579 len);
3580 if (ret)
3581 return ret;
3582
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003583 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003584}
3585
Pavel Begunkov014db002020-03-03 21:33:12 +03003586static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003587{
3588 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003589 struct file *file;
3590 int ret;
3591
Jens Axboef86cd202020-01-29 13:46:44 -07003592 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003593 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003594
Jens Axboecebdb982020-01-08 17:59:24 -07003595 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003596 if (ret)
3597 goto err;
3598
Jens Axboe4022e7a2020-03-19 19:23:18 -06003599 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003600 if (ret < 0)
3601 goto err;
3602
3603 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3604 if (IS_ERR(file)) {
3605 put_unused_fd(ret);
3606 ret = PTR_ERR(file);
3607 } else {
3608 fsnotify_open(file);
3609 fd_install(ret, file);
3610 }
3611err:
3612 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003613 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003614 if (ret < 0)
3615 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003616 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003617 return 0;
3618}
3619
Pavel Begunkov014db002020-03-03 21:33:12 +03003620static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003621{
Pavel Begunkov014db002020-03-03 21:33:12 +03003622 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003623}
3624
Jens Axboe067524e2020-03-02 16:32:28 -07003625static int io_remove_buffers_prep(struct io_kiocb *req,
3626 const struct io_uring_sqe *sqe)
3627{
3628 struct io_provide_buf *p = &req->pbuf;
3629 u64 tmp;
3630
3631 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3632 return -EINVAL;
3633
3634 tmp = READ_ONCE(sqe->fd);
3635 if (!tmp || tmp > USHRT_MAX)
3636 return -EINVAL;
3637
3638 memset(p, 0, sizeof(*p));
3639 p->nbufs = tmp;
3640 p->bgid = READ_ONCE(sqe->buf_group);
3641 return 0;
3642}
3643
3644static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3645 int bgid, unsigned nbufs)
3646{
3647 unsigned i = 0;
3648
3649 /* shouldn't happen */
3650 if (!nbufs)
3651 return 0;
3652
3653 /* the head kbuf is the list itself */
3654 while (!list_empty(&buf->list)) {
3655 struct io_buffer *nxt;
3656
3657 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3658 list_del(&nxt->list);
3659 kfree(nxt);
3660 if (++i == nbufs)
3661 return i;
3662 }
3663 i++;
3664 kfree(buf);
3665 idr_remove(&ctx->io_buffer_idr, bgid);
3666
3667 return i;
3668}
3669
Jens Axboe229a7b62020-06-22 10:13:11 -06003670static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3671 struct io_comp_state *cs)
Jens Axboe067524e2020-03-02 16:32:28 -07003672{
3673 struct io_provide_buf *p = &req->pbuf;
3674 struct io_ring_ctx *ctx = req->ctx;
3675 struct io_buffer *head;
3676 int ret = 0;
3677
3678 io_ring_submit_lock(ctx, !force_nonblock);
3679
3680 lockdep_assert_held(&ctx->uring_lock);
3681
3682 ret = -ENOENT;
3683 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3684 if (head)
3685 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3686
3687 io_ring_submit_lock(ctx, !force_nonblock);
3688 if (ret < 0)
3689 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003690 __io_req_complete(req, ret, 0, cs);
Jens Axboe067524e2020-03-02 16:32:28 -07003691 return 0;
3692}
3693
Jens Axboeddf0322d2020-02-23 16:41:33 -07003694static int io_provide_buffers_prep(struct io_kiocb *req,
3695 const struct io_uring_sqe *sqe)
3696{
3697 struct io_provide_buf *p = &req->pbuf;
3698 u64 tmp;
3699
3700 if (sqe->ioprio || sqe->rw_flags)
3701 return -EINVAL;
3702
3703 tmp = READ_ONCE(sqe->fd);
3704 if (!tmp || tmp > USHRT_MAX)
3705 return -E2BIG;
3706 p->nbufs = tmp;
3707 p->addr = READ_ONCE(sqe->addr);
3708 p->len = READ_ONCE(sqe->len);
3709
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003710 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003711 return -EFAULT;
3712
3713 p->bgid = READ_ONCE(sqe->buf_group);
3714 tmp = READ_ONCE(sqe->off);
3715 if (tmp > USHRT_MAX)
3716 return -E2BIG;
3717 p->bid = tmp;
3718 return 0;
3719}
3720
3721static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3722{
3723 struct io_buffer *buf;
3724 u64 addr = pbuf->addr;
3725 int i, bid = pbuf->bid;
3726
3727 for (i = 0; i < pbuf->nbufs; i++) {
3728 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3729 if (!buf)
3730 break;
3731
3732 buf->addr = addr;
3733 buf->len = pbuf->len;
3734 buf->bid = bid;
3735 addr += pbuf->len;
3736 bid++;
3737 if (!*head) {
3738 INIT_LIST_HEAD(&buf->list);
3739 *head = buf;
3740 } else {
3741 list_add_tail(&buf->list, &(*head)->list);
3742 }
3743 }
3744
3745 return i ? i : -ENOMEM;
3746}
3747
Jens Axboe229a7b62020-06-22 10:13:11 -06003748static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3749 struct io_comp_state *cs)
Jens Axboeddf0322d2020-02-23 16:41:33 -07003750{
3751 struct io_provide_buf *p = &req->pbuf;
3752 struct io_ring_ctx *ctx = req->ctx;
3753 struct io_buffer *head, *list;
3754 int ret = 0;
3755
3756 io_ring_submit_lock(ctx, !force_nonblock);
3757
3758 lockdep_assert_held(&ctx->uring_lock);
3759
3760 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3761
3762 ret = io_add_buffers(p, &head);
3763 if (ret < 0)
3764 goto out;
3765
3766 if (!list) {
3767 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3768 GFP_KERNEL);
3769 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003770 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003771 goto out;
3772 }
3773 }
3774out:
3775 io_ring_submit_unlock(ctx, !force_nonblock);
3776 if (ret < 0)
3777 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003778 __io_req_complete(req, ret, 0, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003779 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003780}
3781
Jens Axboe3e4827b2020-01-08 15:18:09 -07003782static int io_epoll_ctl_prep(struct io_kiocb *req,
3783 const struct io_uring_sqe *sqe)
3784{
3785#if defined(CONFIG_EPOLL)
3786 if (sqe->ioprio || sqe->buf_index)
3787 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06003788 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003789 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003790
3791 req->epoll.epfd = READ_ONCE(sqe->fd);
3792 req->epoll.op = READ_ONCE(sqe->len);
3793 req->epoll.fd = READ_ONCE(sqe->off);
3794
3795 if (ep_op_has_event(req->epoll.op)) {
3796 struct epoll_event __user *ev;
3797
3798 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3799 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3800 return -EFAULT;
3801 }
3802
3803 return 0;
3804#else
3805 return -EOPNOTSUPP;
3806#endif
3807}
3808
Jens Axboe229a7b62020-06-22 10:13:11 -06003809static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3810 struct io_comp_state *cs)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003811{
3812#if defined(CONFIG_EPOLL)
3813 struct io_epoll *ie = &req->epoll;
3814 int ret;
3815
3816 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3817 if (force_nonblock && ret == -EAGAIN)
3818 return -EAGAIN;
3819
3820 if (ret < 0)
3821 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06003822 __io_req_complete(req, ret, 0, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003823 return 0;
3824#else
3825 return -EOPNOTSUPP;
3826#endif
3827}
3828
Jens Axboec1ca7572019-12-25 22:18:28 -07003829static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3830{
3831#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3832 if (sqe->ioprio || sqe->buf_index || sqe->off)
3833 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003834 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3835 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003836
3837 req->madvise.addr = READ_ONCE(sqe->addr);
3838 req->madvise.len = READ_ONCE(sqe->len);
3839 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3840 return 0;
3841#else
3842 return -EOPNOTSUPP;
3843#endif
3844}
3845
Pavel Begunkov014db002020-03-03 21:33:12 +03003846static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003847{
3848#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3849 struct io_madvise *ma = &req->madvise;
3850 int ret;
3851
3852 if (force_nonblock)
3853 return -EAGAIN;
3854
3855 ret = do_madvise(ma->addr, ma->len, ma->advice);
3856 if (ret < 0)
3857 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003858 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07003859 return 0;
3860#else
3861 return -EOPNOTSUPP;
3862#endif
3863}
3864
Jens Axboe4840e412019-12-25 22:03:45 -07003865static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3866{
3867 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3868 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003869 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3870 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003871
3872 req->fadvise.offset = READ_ONCE(sqe->off);
3873 req->fadvise.len = READ_ONCE(sqe->len);
3874 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3875 return 0;
3876}
3877
Pavel Begunkov014db002020-03-03 21:33:12 +03003878static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003879{
3880 struct io_fadvise *fa = &req->fadvise;
3881 int ret;
3882
Jens Axboe3e694262020-02-01 09:22:49 -07003883 if (force_nonblock) {
3884 switch (fa->advice) {
3885 case POSIX_FADV_NORMAL:
3886 case POSIX_FADV_RANDOM:
3887 case POSIX_FADV_SEQUENTIAL:
3888 break;
3889 default:
3890 return -EAGAIN;
3891 }
3892 }
Jens Axboe4840e412019-12-25 22:03:45 -07003893
3894 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3895 if (ret < 0)
3896 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003897 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07003898 return 0;
3899}
3900
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003901static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3902{
Jens Axboe6ca56f82020-09-18 16:51:19 -06003903 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003904 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003905 if (sqe->ioprio || sqe->buf_index)
3906 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003907 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003908 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003909
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003910 req->statx.dfd = READ_ONCE(sqe->fd);
3911 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003912 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003913 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3914 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003915
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003916 return 0;
3917}
3918
Pavel Begunkov014db002020-03-03 21:33:12 +03003919static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003920{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003921 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003922 int ret;
3923
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003924 if (force_nonblock) {
3925 /* only need file table for an actual valid fd */
3926 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3927 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003928 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003929 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003930
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003931 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3932 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003933
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003934 if (ret < 0)
3935 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003936 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003937 return 0;
3938}
3939
Jens Axboeb5dba592019-12-11 14:02:38 -07003940static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3941{
3942 /*
3943 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003944 * leave the 'file' in an undeterminate state, and here need to modify
3945 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003946 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003947 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003948 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3949
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003950 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3951 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003952 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3953 sqe->rw_flags || sqe->buf_index)
3954 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003955 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003956 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003957
3958 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003959 if ((req->file && req->file->f_op == &io_uring_fops) ||
3960 req->close.fd == req->ctx->ring_fd)
3961 return -EBADF;
3962
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003963 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003964 return 0;
3965}
3966
Jens Axboe229a7b62020-06-22 10:13:11 -06003967static int io_close(struct io_kiocb *req, bool force_nonblock,
3968 struct io_comp_state *cs)
Jens Axboeb5dba592019-12-11 14:02:38 -07003969{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003970 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003971 int ret;
3972
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003973 /* might be already done during nonblock submission */
3974 if (!close->put_file) {
3975 ret = __close_fd_get_file(close->fd, &close->put_file);
3976 if (ret < 0)
3977 return (ret == -ENOENT) ? -EBADF : ret;
3978 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003979
3980 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003981 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov24c74672020-06-21 13:09:51 +03003982 /* was never set, but play safe */
3983 req->flags &= ~REQ_F_NOWAIT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003984 /* avoid grabbing files - we don't need the files */
Pavel Begunkov24c74672020-06-21 13:09:51 +03003985 req->flags |= REQ_F_NO_FILE_TABLE;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003986 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003987 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003988
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003989 /* No ->flush() or already async, safely close from here */
3990 ret = filp_close(close->put_file, req->work.files);
3991 if (ret < 0)
3992 req_set_fail_links(req);
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003993 fput(close->put_file);
3994 close->put_file = NULL;
Jens Axboe229a7b62020-06-22 10:13:11 -06003995 __io_req_complete(req, ret, 0, cs);
Jens Axboe1a417f42020-01-31 17:16:48 -07003996 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003997}
3998
Jens Axboe3529d8c2019-12-19 18:24:38 -07003999static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004000{
4001 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004002
4003 if (!req->file)
4004 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004005
4006 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4007 return -EINVAL;
4008 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4009 return -EINVAL;
4010
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004011 req->sync.off = READ_ONCE(sqe->off);
4012 req->sync.len = READ_ONCE(sqe->len);
4013 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004014 return 0;
4015}
4016
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004017static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004018{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004019 int ret;
4020
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004021 /* sync_file_range always requires a blocking context */
4022 if (force_nonblock)
4023 return -EAGAIN;
4024
Jens Axboe9adbd452019-12-20 08:45:55 -07004025 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004026 req->sync.flags);
4027 if (ret < 0)
4028 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004029 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004030 return 0;
4031}
4032
YueHaibing469956e2020-03-04 15:53:52 +08004033#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004034static int io_setup_async_msg(struct io_kiocb *req,
4035 struct io_async_msghdr *kmsg)
4036{
4037 if (req->io)
4038 return -EAGAIN;
4039 if (io_alloc_async_ctx(req)) {
4040 if (kmsg->iov != kmsg->fast_iov)
4041 kfree(kmsg->iov);
4042 return -ENOMEM;
4043 }
4044 req->flags |= REQ_F_NEED_CLEANUP;
4045 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
4046 return -EAGAIN;
4047}
4048
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004049static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4050 struct io_async_msghdr *iomsg)
4051{
4052 iomsg->iov = iomsg->fast_iov;
4053 iomsg->msg.msg_name = &iomsg->addr;
4054 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
4055 req->sr_msg.msg_flags, &iomsg->iov);
4056}
4057
Jens Axboe3529d8c2019-12-19 18:24:38 -07004058static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004059{
Jens Axboee47293f2019-12-20 08:58:21 -07004060 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004061 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004062 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004063
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004064 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4065 return -EINVAL;
4066
Jens Axboee47293f2019-12-20 08:58:21 -07004067 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004068 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004069 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004070
Jens Axboed8768362020-02-27 14:17:49 -07004071#ifdef CONFIG_COMPAT
4072 if (req->ctx->compat)
4073 sr->msg_flags |= MSG_CMSG_COMPAT;
4074#endif
4075
Jens Axboefddafac2020-01-04 20:19:44 -07004076 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07004077 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004078 /* iovec is already imported */
4079 if (req->flags & REQ_F_NEED_CLEANUP)
4080 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004081
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004082 ret = io_sendmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004083 if (!ret)
4084 req->flags |= REQ_F_NEED_CLEANUP;
4085 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004086}
4087
Jens Axboe229a7b62020-06-22 10:13:11 -06004088static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4089 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004090{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004091 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004092 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004093 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004094 int ret;
4095
Jens Axboe03b12302019-12-02 18:50:25 -07004096 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004097 if (unlikely(!sock))
4098 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004099
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004100 if (req->io) {
4101 kmsg = &req->io->msg;
4102 kmsg->msg.msg_name = &req->io->msg.addr;
4103 /* if iov is set, it's allocated already */
4104 if (!kmsg->iov)
4105 kmsg->iov = kmsg->fast_iov;
4106 kmsg->msg.msg_iter.iov = kmsg->iov;
4107 } else {
4108 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004109 if (ret)
4110 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004111 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004112 }
4113
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004114 flags = req->sr_msg.msg_flags;
4115 if (flags & MSG_DONTWAIT)
4116 req->flags |= REQ_F_NOWAIT;
4117 else if (force_nonblock)
4118 flags |= MSG_DONTWAIT;
4119
4120 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4121 if (force_nonblock && ret == -EAGAIN)
4122 return io_setup_async_msg(req, kmsg);
4123 if (ret == -ERESTARTSYS)
4124 ret = -EINTR;
4125
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004126 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe03b12302019-12-02 18:50:25 -07004127 kfree(kmsg->iov);
4128 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004129 if (ret < 0)
4130 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004131 __io_req_complete(req, ret, 0, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004132 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004133}
4134
Jens Axboe229a7b62020-06-22 10:13:11 -06004135static int io_send(struct io_kiocb *req, bool force_nonblock,
4136 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004137{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004138 struct io_sr_msg *sr = &req->sr_msg;
4139 struct msghdr msg;
4140 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004141 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004142 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004143 int ret;
4144
4145 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004146 if (unlikely(!sock))
4147 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004148
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004149 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4150 if (unlikely(ret))
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004151 return ret;;
Jens Axboe03b12302019-12-02 18:50:25 -07004152
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004153 msg.msg_name = NULL;
4154 msg.msg_control = NULL;
4155 msg.msg_controllen = 0;
4156 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004157
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004158 flags = req->sr_msg.msg_flags;
4159 if (flags & MSG_DONTWAIT)
4160 req->flags |= REQ_F_NOWAIT;
4161 else if (force_nonblock)
4162 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004163
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004164 msg.msg_flags = flags;
4165 ret = sock_sendmsg(sock, &msg);
4166 if (force_nonblock && ret == -EAGAIN)
4167 return -EAGAIN;
4168 if (ret == -ERESTARTSYS)
4169 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004170
Jens Axboe03b12302019-12-02 18:50:25 -07004171 if (ret < 0)
4172 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004173 __io_req_complete(req, ret, 0, cs);
Jens Axboe03b12302019-12-02 18:50:25 -07004174 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004175}
4176
Pavel Begunkov1400e692020-07-12 20:41:05 +03004177static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4178 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004179{
4180 struct io_sr_msg *sr = &req->sr_msg;
4181 struct iovec __user *uiov;
4182 size_t iov_len;
4183 int ret;
4184
Pavel Begunkov1400e692020-07-12 20:41:05 +03004185 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4186 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004187 if (ret)
4188 return ret;
4189
4190 if (req->flags & REQ_F_BUFFER_SELECT) {
4191 if (iov_len > 1)
4192 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004193 if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004194 return -EFAULT;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004195 sr->len = iomsg->iov[0].iov_len;
4196 iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004197 sr->len);
Pavel Begunkov1400e692020-07-12 20:41:05 +03004198 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004199 } else {
4200 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004201 &iomsg->iov, &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004202 if (ret > 0)
4203 ret = 0;
4204 }
4205
4206 return ret;
4207}
4208
4209#ifdef CONFIG_COMPAT
4210static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004211 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004212{
4213 struct compat_msghdr __user *msg_compat;
4214 struct io_sr_msg *sr = &req->sr_msg;
4215 struct compat_iovec __user *uiov;
4216 compat_uptr_t ptr;
4217 compat_size_t len;
4218 int ret;
4219
Pavel Begunkov270a5942020-07-12 20:41:04 +03004220 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004221 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004222 &ptr, &len);
4223 if (ret)
4224 return ret;
4225
4226 uiov = compat_ptr(ptr);
4227 if (req->flags & REQ_F_BUFFER_SELECT) {
4228 compat_ssize_t clen;
4229
4230 if (len > 1)
4231 return -EINVAL;
4232 if (!access_ok(uiov, sizeof(*uiov)))
4233 return -EFAULT;
4234 if (__get_user(clen, &uiov->iov_len))
4235 return -EFAULT;
4236 if (clen < 0)
4237 return -EINVAL;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004238 sr->len = iomsg->iov[0].iov_len;
4239 iomsg->iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004240 } else {
4241 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004242 &iomsg->iov,
4243 &iomsg->msg.msg_iter);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004244 if (ret < 0)
4245 return ret;
4246 }
4247
4248 return 0;
4249}
Jens Axboe03b12302019-12-02 18:50:25 -07004250#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004251
Pavel Begunkov1400e692020-07-12 20:41:05 +03004252static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4253 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004254{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004255 iomsg->msg.msg_name = &iomsg->addr;
4256 iomsg->iov = iomsg->fast_iov;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004257
4258#ifdef CONFIG_COMPAT
4259 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004260 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004261#endif
4262
Pavel Begunkov1400e692020-07-12 20:41:05 +03004263 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004264}
4265
Jens Axboebcda7ba2020-02-23 16:42:51 -07004266static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004267 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004268{
4269 struct io_sr_msg *sr = &req->sr_msg;
4270 struct io_buffer *kbuf;
4271
Jens Axboebcda7ba2020-02-23 16:42:51 -07004272 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4273 if (IS_ERR(kbuf))
4274 return kbuf;
4275
4276 sr->kbuf = kbuf;
4277 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004278 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004279}
4280
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004281static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4282{
4283 return io_put_kbuf(req, req->sr_msg.kbuf);
4284}
4285
Jens Axboe3529d8c2019-12-19 18:24:38 -07004286static int io_recvmsg_prep(struct io_kiocb *req,
4287 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07004288{
Jens Axboee47293f2019-12-20 08:58:21 -07004289 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004290 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004291 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004292
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004293 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4294 return -EINVAL;
4295
Jens Axboe3529d8c2019-12-19 18:24:38 -07004296 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004297 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004298 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004299 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004300
Jens Axboed8768362020-02-27 14:17:49 -07004301#ifdef CONFIG_COMPAT
4302 if (req->ctx->compat)
4303 sr->msg_flags |= MSG_CMSG_COMPAT;
4304#endif
4305
Jens Axboefddafac2020-01-04 20:19:44 -07004306 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07004307 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03004308 /* iovec is already imported */
4309 if (req->flags & REQ_F_NEED_CLEANUP)
4310 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004311
Pavel Begunkov1400e692020-07-12 20:41:05 +03004312 ret = io_recvmsg_copy_hdr(req, &io->msg);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004313 if (!ret)
4314 req->flags |= REQ_F_NEED_CLEANUP;
4315 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004316}
4317
Jens Axboe229a7b62020-06-22 10:13:11 -06004318static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4319 struct io_comp_state *cs)
Jens Axboe03b12302019-12-02 18:50:25 -07004320{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004321 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004322 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004323 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004324 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004325 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004326
Jens Axboe0fa03c62019-04-19 13:34:07 -06004327 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004328 if (unlikely(!sock))
4329 return ret;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004330
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004331 if (req->io) {
4332 kmsg = &req->io->msg;
4333 kmsg->msg.msg_name = &req->io->msg.addr;
4334 /* if iov is set, it's allocated already */
4335 if (!kmsg->iov)
4336 kmsg->iov = kmsg->fast_iov;
4337 kmsg->msg.msg_iter.iov = kmsg->iov;
4338 } else {
4339 ret = io_recvmsg_copy_hdr(req, &iomsg);
4340 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004341 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004342 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004343 }
4344
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004345 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004346 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004347 if (IS_ERR(kbuf))
4348 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004349 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4350 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4351 1, req->sr_msg.len);
4352 }
4353
4354 flags = req->sr_msg.msg_flags;
4355 if (flags & MSG_DONTWAIT)
4356 req->flags |= REQ_F_NOWAIT;
4357 else if (force_nonblock)
4358 flags |= MSG_DONTWAIT;
4359
4360 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4361 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004362 if (force_nonblock && ret == -EAGAIN)
4363 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004364 if (ret == -ERESTARTSYS)
4365 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004366
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004367 if (req->flags & REQ_F_BUFFER_SELECTED)
4368 cflags = io_put_recv_kbuf(req);
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004369 if (kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07004370 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004371 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004372 if (ret < 0)
4373 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004374 __io_req_complete(req, ret, cflags, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004375 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004376}
4377
Jens Axboe229a7b62020-06-22 10:13:11 -06004378static int io_recv(struct io_kiocb *req, bool force_nonblock,
4379 struct io_comp_state *cs)
Jens Axboefddafac2020-01-04 20:19:44 -07004380{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004381 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004382 struct io_sr_msg *sr = &req->sr_msg;
4383 struct msghdr msg;
4384 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004385 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004386 struct iovec iov;
4387 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004388 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004389
Jens Axboefddafac2020-01-04 20:19:44 -07004390 sock = sock_from_file(req->file, &ret);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004391 if (unlikely(!sock))
4392 return ret;
Jens Axboefddafac2020-01-04 20:19:44 -07004393
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004394 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004395 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004396 if (IS_ERR(kbuf))
4397 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004398 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004399 }
4400
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004401 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004402 if (unlikely(ret))
4403 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004404
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004405 msg.msg_name = NULL;
4406 msg.msg_control = NULL;
4407 msg.msg_controllen = 0;
4408 msg.msg_namelen = 0;
4409 msg.msg_iocb = NULL;
4410 msg.msg_flags = 0;
4411
4412 flags = req->sr_msg.msg_flags;
4413 if (flags & MSG_DONTWAIT)
4414 req->flags |= REQ_F_NOWAIT;
4415 else if (force_nonblock)
4416 flags |= MSG_DONTWAIT;
4417
4418 ret = sock_recvmsg(sock, &msg, flags);
4419 if (force_nonblock && ret == -EAGAIN)
4420 return -EAGAIN;
4421 if (ret == -ERESTARTSYS)
4422 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004423out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004424 if (req->flags & REQ_F_BUFFER_SELECTED)
4425 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004426 if (ret < 0)
4427 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004428 __io_req_complete(req, ret, cflags, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07004429 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004430}
4431
Jens Axboe3529d8c2019-12-19 18:24:38 -07004432static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004433{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004434 struct io_accept *accept = &req->accept;
4435
Jens Axboe17f2fe32019-10-17 14:42:58 -06004436 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4437 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004438 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004439 return -EINVAL;
4440
Jens Axboed55e5f52019-12-11 16:12:15 -07004441 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4442 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004443 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004444 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004445 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004446}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004447
Jens Axboe229a7b62020-06-22 10:13:11 -06004448static int io_accept(struct io_kiocb *req, bool force_nonblock,
4449 struct io_comp_state *cs)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004450{
4451 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004452 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004453 int ret;
4454
Jiufei Xuee697dee2020-06-10 13:41:59 +08004455 if (req->file->f_flags & O_NONBLOCK)
4456 req->flags |= REQ_F_NOWAIT;
4457
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004458 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004459 accept->addr_len, accept->flags,
4460 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004461 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004462 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004463 if (ret < 0) {
4464 if (ret == -ERESTARTSYS)
4465 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004466 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004467 }
Jens Axboe229a7b62020-06-22 10:13:11 -06004468 __io_req_complete(req, ret, 0, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004469 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004470}
4471
Jens Axboe3529d8c2019-12-19 18:24:38 -07004472static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004473{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004474 struct io_connect *conn = &req->connect;
4475 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004476
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004477 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4478 return -EINVAL;
4479 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4480 return -EINVAL;
4481
Jens Axboe3529d8c2019-12-19 18:24:38 -07004482 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4483 conn->addr_len = READ_ONCE(sqe->addr2);
4484
4485 if (!io)
4486 return 0;
4487
4488 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004489 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004490}
4491
Jens Axboe229a7b62020-06-22 10:13:11 -06004492static int io_connect(struct io_kiocb *req, bool force_nonblock,
4493 struct io_comp_state *cs)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004494{
Jens Axboef499a022019-12-02 16:28:46 -07004495 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004496 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004497 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004498
Jens Axboef499a022019-12-02 16:28:46 -07004499 if (req->io) {
4500 io = req->io;
4501 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004502 ret = move_addr_to_kernel(req->connect.addr,
4503 req->connect.addr_len,
4504 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004505 if (ret)
4506 goto out;
4507 io = &__io;
4508 }
4509
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004510 file_flags = force_nonblock ? O_NONBLOCK : 0;
4511
4512 ret = __sys_connect_file(req->file, &io->connect.address,
4513 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004514 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004515 if (req->io)
4516 return -EAGAIN;
4517 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004518 ret = -ENOMEM;
4519 goto out;
4520 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004521 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004522 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004523 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004524 if (ret == -ERESTARTSYS)
4525 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004526out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004527 if (ret < 0)
4528 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06004529 __io_req_complete(req, ret, 0, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004530 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004531}
YueHaibing469956e2020-03-04 15:53:52 +08004532#else /* !CONFIG_NET */
4533static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4534{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004535 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004536}
4537
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004538static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4539 struct io_comp_state *cs)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004540{
YueHaibing469956e2020-03-04 15:53:52 +08004541 return -EOPNOTSUPP;
4542}
4543
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004544static int io_send(struct io_kiocb *req, bool force_nonblock,
4545 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004546{
4547 return -EOPNOTSUPP;
4548}
4549
4550static int io_recvmsg_prep(struct io_kiocb *req,
4551 const struct io_uring_sqe *sqe)
4552{
4553 return -EOPNOTSUPP;
4554}
4555
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004556static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4557 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004558{
4559 return -EOPNOTSUPP;
4560}
4561
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004562static int io_recv(struct io_kiocb *req, bool force_nonblock,
4563 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004564{
4565 return -EOPNOTSUPP;
4566}
4567
4568static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4569{
4570 return -EOPNOTSUPP;
4571}
4572
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004573static int io_accept(struct io_kiocb *req, bool force_nonblock,
4574 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004575{
4576 return -EOPNOTSUPP;
4577}
4578
4579static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4580{
4581 return -EOPNOTSUPP;
4582}
4583
Randy Dunlap1e16c2f2020-06-26 16:32:50 -07004584static int io_connect(struct io_kiocb *req, bool force_nonblock,
4585 struct io_comp_state *cs)
YueHaibing469956e2020-03-04 15:53:52 +08004586{
4587 return -EOPNOTSUPP;
4588}
4589#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004590
Jens Axboed7718a92020-02-14 22:23:12 -07004591struct io_poll_table {
4592 struct poll_table_struct pt;
4593 struct io_kiocb *req;
4594 int error;
4595};
4596
Jens Axboed7718a92020-02-14 22:23:12 -07004597static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4598 __poll_t mask, task_work_func_t func)
4599{
Jens Axboefd7d6de2020-08-23 11:00:37 -06004600 bool twa_signal_ok;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004601 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004602
4603 /* for instances that support it check for an event match first: */
4604 if (mask && !(mask & poll->events))
4605 return 0;
4606
4607 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4608
4609 list_del_init(&poll->wait.entry);
4610
Jens Axboed7718a92020-02-14 22:23:12 -07004611 req->result = mask;
4612 init_task_work(&req->task_work, func);
Jens Axboe6d816e02020-08-11 08:04:14 -06004613 percpu_ref_get(&req->ctx->refs);
4614
Jens Axboed7718a92020-02-14 22:23:12 -07004615 /*
Jens Axboefd7d6de2020-08-23 11:00:37 -06004616 * If we using the signalfd wait_queue_head for this wakeup, then
4617 * it's not safe to use TWA_SIGNAL as we could be recursing on the
4618 * tsk->sighand->siglock on doing the wakeup. Should not be needed
4619 * either, as the normal wakeup will suffice.
4620 */
4621 twa_signal_ok = (poll->head != &req->task->sighand->signalfd_wqh);
4622
4623 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004624 * If this fails, then the task is exiting. When a task exits, the
4625 * work gets canceled, so just cancel this request as well instead
4626 * of executing it. We can't safely execute it anyway, as we may not
4627 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004628 */
Jens Axboefd7d6de2020-08-23 11:00:37 -06004629 ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004630 if (unlikely(ret)) {
Jens Axboec2c4c832020-07-01 15:37:11 -06004631 struct task_struct *tsk;
4632
Jens Axboee3aabf92020-05-18 11:04:17 -06004633 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004634 tsk = io_wq_get_task(req->ctx->io_wq);
Jens Axboece593a62020-06-30 12:39:05 -06004635 task_work_add(tsk, &req->task_work, 0);
4636 wake_up_process(tsk);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004637 }
Jens Axboed7718a92020-02-14 22:23:12 -07004638 return 1;
4639}
4640
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004641static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4642 __acquires(&req->ctx->completion_lock)
4643{
4644 struct io_ring_ctx *ctx = req->ctx;
4645
4646 if (!req->result && !READ_ONCE(poll->canceled)) {
4647 struct poll_table_struct pt = { ._key = poll->events };
4648
4649 req->result = vfs_poll(req->file, &pt) & poll->events;
4650 }
4651
4652 spin_lock_irq(&ctx->completion_lock);
4653 if (!req->result && !READ_ONCE(poll->canceled)) {
4654 add_wait_queue(poll->head, &poll->wait);
4655 return true;
4656 }
4657
4658 return false;
4659}
4660
Jens Axboed4e7cd32020-08-15 11:44:50 -07004661static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004662{
Jens Axboed4e7cd32020-08-15 11:44:50 -07004663 /* pure poll stashes this in ->io, poll driven retry elsewhere */
4664 if (req->opcode == IORING_OP_POLL_ADD)
4665 return (struct io_poll_iocb *) req->io;
4666 return req->apoll->double_poll;
4667}
4668
4669static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4670{
4671 if (req->opcode == IORING_OP_POLL_ADD)
4672 return &req->poll;
4673 return &req->apoll->poll;
4674}
4675
4676static void io_poll_remove_double(struct io_kiocb *req)
4677{
4678 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004679
4680 lockdep_assert_held(&req->ctx->completion_lock);
4681
4682 if (poll && poll->head) {
4683 struct wait_queue_head *head = poll->head;
4684
4685 spin_lock(&head->lock);
4686 list_del_init(&poll->wait.entry);
4687 if (poll->wait.private)
4688 refcount_dec(&req->refs);
4689 poll->head = NULL;
4690 spin_unlock(&head->lock);
4691 }
4692}
4693
4694static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4695{
4696 struct io_ring_ctx *ctx = req->ctx;
4697
Jens Axboed4e7cd32020-08-15 11:44:50 -07004698 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004699 req->poll.done = true;
4700 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4701 io_commit_cqring(ctx);
4702}
4703
4704static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4705{
4706 struct io_ring_ctx *ctx = req->ctx;
4707
4708 if (io_poll_rewait(req, &req->poll)) {
4709 spin_unlock_irq(&ctx->completion_lock);
4710 return;
4711 }
4712
4713 hash_del(&req->hash_node);
4714 io_poll_complete(req, req->result, 0);
4715 req->flags |= REQ_F_COMP_LOCKED;
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03004716 *nxt = io_put_req_find_next(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004717 spin_unlock_irq(&ctx->completion_lock);
4718
4719 io_cqring_ev_posted(ctx);
4720}
4721
4722static void io_poll_task_func(struct callback_head *cb)
4723{
4724 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004725 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe18bceab2020-05-15 11:56:54 -06004726 struct io_kiocb *nxt = NULL;
4727
4728 io_poll_task_handler(req, &nxt);
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004729 if (nxt)
4730 __io_req_task_submit(nxt);
Jens Axboe6d816e02020-08-11 08:04:14 -06004731 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06004732}
4733
4734static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4735 int sync, void *key)
4736{
4737 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004738 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004739 __poll_t mask = key_to_poll(key);
4740
4741 /* for instances that support it check for an event match first: */
4742 if (mask && !(mask & poll->events))
4743 return 0;
4744
Jens Axboe8706e042020-09-28 08:38:54 -06004745 list_del_init(&wait->entry);
4746
Jens Axboe807abcb2020-07-17 17:09:27 -06004747 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004748 bool done;
4749
Jens Axboe807abcb2020-07-17 17:09:27 -06004750 spin_lock(&poll->head->lock);
4751 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004752 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004753 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004754 /* make sure double remove sees this as being gone */
4755 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004756 spin_unlock(&poll->head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06004757 if (!done)
4758 __io_async_wake(req, poll, mask, io_poll_task_func);
4759 }
4760 refcount_dec(&req->refs);
4761 return 1;
4762}
4763
4764static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4765 wait_queue_func_t wake_func)
4766{
4767 poll->head = NULL;
4768 poll->done = false;
4769 poll->canceled = false;
4770 poll->events = events;
4771 INIT_LIST_HEAD(&poll->wait.entry);
4772 init_waitqueue_func_entry(&poll->wait, wake_func);
4773}
4774
4775static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06004776 struct wait_queue_head *head,
4777 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06004778{
4779 struct io_kiocb *req = pt->req;
4780
4781 /*
4782 * If poll->head is already set, it's because the file being polled
4783 * uses multiple waitqueues for poll handling (eg one for read, one
4784 * for write). Setup a separate io_poll_iocb if this happens.
4785 */
4786 if (unlikely(poll->head)) {
4787 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06004788 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004789 pt->error = -EINVAL;
4790 return;
4791 }
4792 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4793 if (!poll) {
4794 pt->error = -ENOMEM;
4795 return;
4796 }
4797 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4798 refcount_inc(&req->refs);
4799 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06004800 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004801 }
4802
4803 pt->error = 0;
4804 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004805
4806 if (poll->events & EPOLLEXCLUSIVE)
4807 add_wait_queue_exclusive(head, &poll->wait);
4808 else
4809 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004810}
4811
4812static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4813 struct poll_table_struct *p)
4814{
4815 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06004816 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06004817
Jens Axboe807abcb2020-07-17 17:09:27 -06004818 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06004819}
4820
Jens Axboed7718a92020-02-14 22:23:12 -07004821static void io_async_task_func(struct callback_head *cb)
4822{
4823 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4824 struct async_poll *apoll = req->apoll;
4825 struct io_ring_ctx *ctx = req->ctx;
4826
4827 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4828
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004829 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004830 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06004831 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004832 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004833 }
4834
Jens Axboe31067252020-05-17 17:43:31 -06004835 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004836 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004837 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06004838
Jens Axboed4e7cd32020-08-15 11:44:50 -07004839 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004840 spin_unlock_irq(&ctx->completion_lock);
4841
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03004842 if (!READ_ONCE(apoll->poll.canceled))
4843 __io_req_task_submit(req);
4844 else
4845 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03004846
Jens Axboe6d816e02020-08-11 08:04:14 -06004847 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06004848 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06004849 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07004850}
4851
4852static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4853 void *key)
4854{
4855 struct io_kiocb *req = wait->private;
4856 struct io_poll_iocb *poll = &req->apoll->poll;
4857
4858 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4859 key_to_poll(key));
4860
4861 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4862}
4863
4864static void io_poll_req_insert(struct io_kiocb *req)
4865{
4866 struct io_ring_ctx *ctx = req->ctx;
4867 struct hlist_head *list;
4868
4869 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4870 hlist_add_head(&req->hash_node, list);
4871}
4872
4873static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4874 struct io_poll_iocb *poll,
4875 struct io_poll_table *ipt, __poll_t mask,
4876 wait_queue_func_t wake_func)
4877 __acquires(&ctx->completion_lock)
4878{
4879 struct io_ring_ctx *ctx = req->ctx;
4880 bool cancel = false;
4881
Jens Axboe18bceab2020-05-15 11:56:54 -06004882 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03004883 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004884 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004885
4886 ipt->pt._key = mask;
4887 ipt->req = req;
4888 ipt->error = -EINVAL;
4889
Jens Axboed7718a92020-02-14 22:23:12 -07004890 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4891
4892 spin_lock_irq(&ctx->completion_lock);
4893 if (likely(poll->head)) {
4894 spin_lock(&poll->head->lock);
4895 if (unlikely(list_empty(&poll->wait.entry))) {
4896 if (ipt->error)
4897 cancel = true;
4898 ipt->error = 0;
4899 mask = 0;
4900 }
4901 if (mask || ipt->error)
4902 list_del_init(&poll->wait.entry);
4903 else if (cancel)
4904 WRITE_ONCE(poll->canceled, true);
4905 else if (!poll->done) /* actually waiting for an event */
4906 io_poll_req_insert(req);
4907 spin_unlock(&poll->head->lock);
4908 }
4909
4910 return mask;
4911}
4912
4913static bool io_arm_poll_handler(struct io_kiocb *req)
4914{
4915 const struct io_op_def *def = &io_op_defs[req->opcode];
4916 struct io_ring_ctx *ctx = req->ctx;
4917 struct async_poll *apoll;
4918 struct io_poll_table ipt;
4919 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004920 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07004921
4922 if (!req->file || !file_can_poll(req->file))
4923 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03004924 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07004925 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06004926 if (def->pollin)
4927 rw = READ;
4928 else if (def->pollout)
4929 rw = WRITE;
4930 else
4931 return false;
4932 /* if we can't nonblock try, then no point in arming a poll handler */
4933 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07004934 return false;
4935
4936 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4937 if (unlikely(!apoll))
4938 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06004939 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004940
4941 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07004942 req->apoll = apoll;
4943 INIT_HLIST_NODE(&req->hash_node);
4944
Nathan Chancellor8755d972020-03-02 16:01:19 -07004945 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004946 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004947 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004948 if (def->pollout)
4949 mask |= POLLOUT | POLLWRNORM;
4950 mask |= POLLERR | POLLPRI;
4951
4952 ipt.pt._qproc = io_async_queue_proc;
4953
4954 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4955 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06004956 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07004957 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004958 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06004959 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07004960 kfree(apoll);
4961 return false;
4962 }
4963 spin_unlock_irq(&ctx->completion_lock);
4964 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4965 apoll->poll.events);
4966 return true;
4967}
4968
4969static bool __io_poll_remove_one(struct io_kiocb *req,
4970 struct io_poll_iocb *poll)
4971{
Jens Axboeb41e9852020-02-17 09:52:41 -07004972 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004973
4974 spin_lock(&poll->head->lock);
4975 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004976 if (!list_empty(&poll->wait.entry)) {
4977 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004978 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004979 }
4980 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004981 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004982 return do_complete;
4983}
4984
4985static bool io_poll_remove_one(struct io_kiocb *req)
4986{
4987 bool do_complete;
4988
Jens Axboed4e7cd32020-08-15 11:44:50 -07004989 io_poll_remove_double(req);
4990
Jens Axboed7718a92020-02-14 22:23:12 -07004991 if (req->opcode == IORING_OP_POLL_ADD) {
4992 do_complete = __io_poll_remove_one(req, &req->poll);
4993 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004994 struct async_poll *apoll = req->apoll;
4995
Jens Axboed7718a92020-02-14 22:23:12 -07004996 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004997 do_complete = __io_poll_remove_one(req, &apoll->poll);
4998 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004999 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005000 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005001 kfree(apoll);
5002 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005003 }
5004
Jens Axboeb41e9852020-02-17 09:52:41 -07005005 if (do_complete) {
5006 io_cqring_fill_event(req, -ECANCELED);
5007 io_commit_cqring(req->ctx);
5008 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboef254ac02020-08-12 17:33:30 -06005009 req_set_fail_links(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005010 io_put_req(req);
5011 }
5012
5013 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005014}
5015
Jens Axboef3606e32020-09-22 08:18:24 -06005016static void io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005017{
Jens Axboe78076bb2019-12-04 19:56:40 -07005018 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005019 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005020 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005021
5022 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005023 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5024 struct hlist_head *list;
5025
5026 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005027 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
5028 if (io_task_match(req, tsk))
5029 posted += io_poll_remove_one(req);
5030 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005031 }
5032 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005033
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005034 if (posted)
5035 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005036}
5037
Jens Axboe47f46762019-11-09 17:43:02 -07005038static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5039{
Jens Axboe78076bb2019-12-04 19:56:40 -07005040 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005041 struct io_kiocb *req;
5042
Jens Axboe78076bb2019-12-04 19:56:40 -07005043 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5044 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005045 if (sqe_addr != req->user_data)
5046 continue;
5047 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005048 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005049 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005050 }
5051
5052 return -ENOENT;
5053}
5054
Jens Axboe3529d8c2019-12-19 18:24:38 -07005055static int io_poll_remove_prep(struct io_kiocb *req,
5056 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005057{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005058 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5059 return -EINVAL;
5060 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5061 sqe->poll_events)
5062 return -EINVAL;
5063
Jens Axboe0969e782019-12-17 18:40:57 -07005064 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005065 return 0;
5066}
5067
5068/*
5069 * Find a running poll command that matches one specified in sqe->addr,
5070 * and remove it if found.
5071 */
5072static int io_poll_remove(struct io_kiocb *req)
5073{
5074 struct io_ring_ctx *ctx = req->ctx;
5075 u64 addr;
5076 int ret;
5077
Jens Axboe0969e782019-12-17 18:40:57 -07005078 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005079 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07005080 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005081 spin_unlock_irq(&ctx->completion_lock);
5082
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005083 if (ret < 0)
5084 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005085 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005086 return 0;
5087}
5088
Jens Axboe221c5eb2019-01-17 09:41:58 -07005089static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5090 void *key)
5091{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005092 struct io_kiocb *req = wait->private;
5093 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005094
Jens Axboed7718a92020-02-14 22:23:12 -07005095 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005096}
5097
Jens Axboe221c5eb2019-01-17 09:41:58 -07005098static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5099 struct poll_table_struct *p)
5100{
5101 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5102
Jens Axboe807abcb2020-07-17 17:09:27 -06005103 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
Jens Axboeeac406c2019-11-14 12:09:58 -07005104}
5105
Jens Axboe3529d8c2019-12-19 18:24:38 -07005106static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005107{
5108 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005109 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005110
5111 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5112 return -EINVAL;
5113 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5114 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06005115 if (!poll->file)
5116 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005117
Jiufei Xue5769a352020-06-17 17:53:55 +08005118 events = READ_ONCE(sqe->poll32_events);
5119#ifdef __BIG_ENDIAN
5120 events = swahw32(events);
5121#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005122 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5123 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005124 return 0;
5125}
5126
Pavel Begunkov014db002020-03-03 21:33:12 +03005127static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07005128{
5129 struct io_poll_iocb *poll = &req->poll;
5130 struct io_ring_ctx *ctx = req->ctx;
5131 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005132 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005133
Jens Axboe78076bb2019-12-04 19:56:40 -07005134 INIT_HLIST_NODE(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005135 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005136
Jens Axboed7718a92020-02-14 22:23:12 -07005137 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5138 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005139
Jens Axboe8c838782019-03-12 15:48:16 -06005140 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005141 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005142 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005143 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005144 spin_unlock_irq(&ctx->completion_lock);
5145
Jens Axboe8c838782019-03-12 15:48:16 -06005146 if (mask) {
5147 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005148 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005149 }
Jens Axboe8c838782019-03-12 15:48:16 -06005150 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005151}
5152
Jens Axboe5262f562019-09-17 12:26:57 -06005153static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5154{
Jens Axboead8a48a2019-11-15 08:49:11 -07005155 struct io_timeout_data *data = container_of(timer,
5156 struct io_timeout_data, timer);
5157 struct io_kiocb *req = data->req;
5158 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005159 unsigned long flags;
5160
Jens Axboe5262f562019-09-17 12:26:57 -06005161 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005162 atomic_set(&req->ctx->cq_timeouts,
5163 atomic_read(&req->ctx->cq_timeouts) + 1);
5164
zhangyi (F)ef036812019-10-23 15:10:08 +08005165 /*
Jens Axboe11365042019-10-16 09:08:32 -06005166 * We could be racing with timeout deletion. If the list is empty,
5167 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08005168 */
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005169 if (!list_empty(&req->timeout.list))
5170 list_del_init(&req->timeout.list);
Jens Axboe842f9612019-10-29 12:34:10 -06005171
Jens Axboe78e19bb2019-11-06 15:21:34 -07005172 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005173 io_commit_cqring(ctx);
5174 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5175
5176 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005177 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005178 io_put_req(req);
5179 return HRTIMER_NORESTART;
5180}
5181
Jens Axboef254ac02020-08-12 17:33:30 -06005182static int __io_timeout_cancel(struct io_kiocb *req)
Jens Axboe47f46762019-11-09 17:43:02 -07005183{
Jens Axboef254ac02020-08-12 17:33:30 -06005184 int ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005185
Jens Axboef254ac02020-08-12 17:33:30 -06005186 list_del_init(&req->timeout.list);
Jens Axboe47f46762019-11-09 17:43:02 -07005187
Jens Axboe2d283902019-12-04 11:08:05 -07005188 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07005189 if (ret == -1)
5190 return -EALREADY;
5191
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005192 req_set_fail_links(req);
Jens Axboe9b7adba2020-08-10 10:54:02 -06005193 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe47f46762019-11-09 17:43:02 -07005194 io_cqring_fill_event(req, -ECANCELED);
5195 io_put_req(req);
5196 return 0;
5197}
5198
Jens Axboef254ac02020-08-12 17:33:30 -06005199static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5200{
5201 struct io_kiocb *req;
5202 int ret = -ENOENT;
5203
5204 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5205 if (user_data == req->user_data) {
5206 ret = 0;
5207 break;
5208 }
5209 }
5210
5211 if (ret == -ENOENT)
5212 return ret;
5213
5214 return __io_timeout_cancel(req);
5215}
5216
Jens Axboe3529d8c2019-12-19 18:24:38 -07005217static int io_timeout_remove_prep(struct io_kiocb *req,
5218 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005219{
Jens Axboeb29472e2019-12-17 18:50:29 -07005220 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5221 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005222 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5223 return -EINVAL;
5224 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005225 return -EINVAL;
5226
5227 req->timeout.addr = READ_ONCE(sqe->addr);
5228 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
5229 if (req->timeout.flags)
5230 return -EINVAL;
5231
Jens Axboeb29472e2019-12-17 18:50:29 -07005232 return 0;
5233}
5234
Jens Axboe11365042019-10-16 09:08:32 -06005235/*
5236 * Remove or update an existing timeout command
5237 */
Jens Axboefc4df992019-12-10 14:38:45 -07005238static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06005239{
5240 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005241 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005242
Jens Axboe11365042019-10-16 09:08:32 -06005243 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07005244 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06005245
Jens Axboe47f46762019-11-09 17:43:02 -07005246 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005247 io_commit_cqring(ctx);
5248 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005249 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005250 if (ret < 0)
5251 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005252 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005253 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005254}
5255
Jens Axboe3529d8c2019-12-19 18:24:38 -07005256static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005257 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005258{
Jens Axboead8a48a2019-11-15 08:49:11 -07005259 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005260 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005261 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005262
Jens Axboead8a48a2019-11-15 08:49:11 -07005263 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005264 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005265 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005266 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005267 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005268 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005269 flags = READ_ONCE(sqe->timeout_flags);
5270 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005271 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005272
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005273 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005274
Jens Axboe3529d8c2019-12-19 18:24:38 -07005275 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005276 return -ENOMEM;
5277
5278 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005279 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005280
5281 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005282 return -EFAULT;
5283
Jens Axboe11365042019-10-16 09:08:32 -06005284 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07005285 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06005286 else
Jens Axboead8a48a2019-11-15 08:49:11 -07005287 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06005288
Jens Axboead8a48a2019-11-15 08:49:11 -07005289 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5290 return 0;
5291}
5292
Jens Axboefc4df992019-12-10 14:38:45 -07005293static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07005294{
Jens Axboead8a48a2019-11-15 08:49:11 -07005295 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005296 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07005297 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005298 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005299
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005300 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005301
Jens Axboe5262f562019-09-17 12:26:57 -06005302 /*
5303 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005304 * timeout event to be satisfied. If it isn't set, then this is
5305 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005306 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005307 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005308 entry = ctx->timeout_list.prev;
5309 goto add;
5310 }
Jens Axboe5262f562019-09-17 12:26:57 -06005311
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005312 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5313 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005314
5315 /*
5316 * Insertion sort, ensuring the first entry in the list is always
5317 * the one we need first.
5318 */
Jens Axboe5262f562019-09-17 12:26:57 -06005319 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005320 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5321 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005322
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005323 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005324 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005325 /* nxt.seq is behind @tail, otherwise would've been completed */
5326 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005327 break;
5328 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005329add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005330 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005331 data->timer.function = io_timeout_fn;
5332 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005333 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005334 return 0;
5335}
5336
Jens Axboe62755e32019-10-28 21:49:21 -06005337static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005338{
Jens Axboe62755e32019-10-28 21:49:21 -06005339 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005340
Jens Axboe62755e32019-10-28 21:49:21 -06005341 return req->user_data == (unsigned long) data;
5342}
5343
Jens Axboee977d6d2019-11-05 12:39:45 -07005344static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005345{
Jens Axboe62755e32019-10-28 21:49:21 -06005346 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005347 int ret = 0;
5348
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005349 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005350 switch (cancel_ret) {
5351 case IO_WQ_CANCEL_OK:
5352 ret = 0;
5353 break;
5354 case IO_WQ_CANCEL_RUNNING:
5355 ret = -EALREADY;
5356 break;
5357 case IO_WQ_CANCEL_NOTFOUND:
5358 ret = -ENOENT;
5359 break;
5360 }
5361
Jens Axboee977d6d2019-11-05 12:39:45 -07005362 return ret;
5363}
5364
Jens Axboe47f46762019-11-09 17:43:02 -07005365static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5366 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005367 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005368{
5369 unsigned long flags;
5370 int ret;
5371
5372 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5373 if (ret != -ENOENT) {
5374 spin_lock_irqsave(&ctx->completion_lock, flags);
5375 goto done;
5376 }
5377
5378 spin_lock_irqsave(&ctx->completion_lock, flags);
5379 ret = io_timeout_cancel(ctx, sqe_addr);
5380 if (ret != -ENOENT)
5381 goto done;
5382 ret = io_poll_cancel(ctx, sqe_addr);
5383done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005384 if (!ret)
5385 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005386 io_cqring_fill_event(req, ret);
5387 io_commit_cqring(ctx);
5388 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5389 io_cqring_ev_posted(ctx);
5390
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005391 if (ret < 0)
5392 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005393 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005394}
5395
Jens Axboe3529d8c2019-12-19 18:24:38 -07005396static int io_async_cancel_prep(struct io_kiocb *req,
5397 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005398{
Jens Axboefbf23842019-12-17 18:45:56 -07005399 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005400 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005401 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5402 return -EINVAL;
5403 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005404 return -EINVAL;
5405
Jens Axboefbf23842019-12-17 18:45:56 -07005406 req->cancel.addr = READ_ONCE(sqe->addr);
5407 return 0;
5408}
5409
Pavel Begunkov014db002020-03-03 21:33:12 +03005410static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005411{
5412 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005413
Pavel Begunkov014db002020-03-03 21:33:12 +03005414 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005415 return 0;
5416}
5417
Jens Axboe05f3fb32019-12-09 11:22:50 -07005418static int io_files_update_prep(struct io_kiocb *req,
5419 const struct io_uring_sqe *sqe)
5420{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005421 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5422 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005423 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5424 return -EINVAL;
5425 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005426 return -EINVAL;
5427
5428 req->files_update.offset = READ_ONCE(sqe->off);
5429 req->files_update.nr_args = READ_ONCE(sqe->len);
5430 if (!req->files_update.nr_args)
5431 return -EINVAL;
5432 req->files_update.arg = READ_ONCE(sqe->addr);
5433 return 0;
5434}
5435
Jens Axboe229a7b62020-06-22 10:13:11 -06005436static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5437 struct io_comp_state *cs)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005438{
5439 struct io_ring_ctx *ctx = req->ctx;
5440 struct io_uring_files_update up;
5441 int ret;
5442
Jens Axboef86cd202020-01-29 13:46:44 -07005443 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005444 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005445
5446 up.offset = req->files_update.offset;
5447 up.fds = req->files_update.arg;
5448
5449 mutex_lock(&ctx->uring_lock);
5450 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5451 mutex_unlock(&ctx->uring_lock);
5452
5453 if (ret < 0)
5454 req_set_fail_links(req);
Jens Axboe229a7b62020-06-22 10:13:11 -06005455 __io_req_complete(req, ret, 0, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005456 return 0;
5457}
5458
Jens Axboe3529d8c2019-12-19 18:24:38 -07005459static int io_req_defer_prep(struct io_kiocb *req,
5460 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005461{
Jens Axboee7815732019-12-17 19:45:06 -07005462 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005463
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005464 if (!sqe)
5465 return 0;
5466
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005467 if (io_alloc_async_ctx(req))
5468 return -EAGAIN;
Pavel Begunkovf56040b2020-07-23 20:25:21 +03005469 ret = io_prep_work_files(req);
5470 if (unlikely(ret))
5471 return ret;
Jens Axboecccf0ee2020-01-27 16:34:48 -07005472
Jens Axboe202700e12020-09-12 13:18:10 -06005473 io_prep_async_work(req);
5474
Jens Axboed625c6e2019-12-17 19:53:05 -07005475 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005476 case IORING_OP_NOP:
5477 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005478 case IORING_OP_READV:
5479 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005480 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005481 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005482 break;
5483 case IORING_OP_WRITEV:
5484 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005485 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005486 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005487 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005488 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005489 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005490 break;
5491 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005492 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005493 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005494 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005495 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005496 break;
5497 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005498 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005499 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005500 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005501 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005502 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005503 break;
5504 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005505 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005506 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005507 break;
Jens Axboef499a022019-12-02 16:28:46 -07005508 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005509 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005510 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005511 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005512 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005513 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005514 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005515 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005516 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005517 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005518 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005519 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005520 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005521 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005522 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005523 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005524 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005525 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005526 case IORING_OP_FALLOCATE:
5527 ret = io_fallocate_prep(req, sqe);
5528 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005529 case IORING_OP_OPENAT:
5530 ret = io_openat_prep(req, sqe);
5531 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005532 case IORING_OP_CLOSE:
5533 ret = io_close_prep(req, sqe);
5534 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005535 case IORING_OP_FILES_UPDATE:
5536 ret = io_files_update_prep(req, sqe);
5537 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005538 case IORING_OP_STATX:
5539 ret = io_statx_prep(req, sqe);
5540 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005541 case IORING_OP_FADVISE:
5542 ret = io_fadvise_prep(req, sqe);
5543 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005544 case IORING_OP_MADVISE:
5545 ret = io_madvise_prep(req, sqe);
5546 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005547 case IORING_OP_OPENAT2:
5548 ret = io_openat2_prep(req, sqe);
5549 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005550 case IORING_OP_EPOLL_CTL:
5551 ret = io_epoll_ctl_prep(req, sqe);
5552 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005553 case IORING_OP_SPLICE:
5554 ret = io_splice_prep(req, sqe);
5555 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005556 case IORING_OP_PROVIDE_BUFFERS:
5557 ret = io_provide_buffers_prep(req, sqe);
5558 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005559 case IORING_OP_REMOVE_BUFFERS:
5560 ret = io_remove_buffers_prep(req, sqe);
5561 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005562 case IORING_OP_TEE:
5563 ret = io_tee_prep(req, sqe);
5564 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005565 default:
Jens Axboee7815732019-12-17 19:45:06 -07005566 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5567 req->opcode);
5568 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005569 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005570 }
5571
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005572 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005573}
5574
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005575static u32 io_get_sequence(struct io_kiocb *req)
5576{
5577 struct io_kiocb *pos;
5578 struct io_ring_ctx *ctx = req->ctx;
5579 u32 total_submitted, nr_reqs = 1;
5580
5581 if (req->flags & REQ_F_LINK_HEAD)
5582 list_for_each_entry(pos, &req->link_list, link_list)
5583 nr_reqs++;
5584
5585 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5586 return total_submitted - nr_reqs;
5587}
5588
Jens Axboe3529d8c2019-12-19 18:24:38 -07005589static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005590{
Jackie Liua197f662019-11-08 08:09:12 -07005591 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005592 struct io_defer_entry *de;
Jens Axboef67676d2019-12-02 11:03:47 -07005593 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005594 u32 seq;
Jens Axboede0617e2019-04-06 21:51:27 -06005595
Bob Liu9d858b22019-11-13 18:06:25 +08005596 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005597 if (likely(list_empty_careful(&ctx->defer_list) &&
5598 !(req->flags & REQ_F_IO_DRAIN)))
5599 return 0;
5600
5601 seq = io_get_sequence(req);
5602 /* Still a chance to pass the sequence check */
5603 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005604 return 0;
5605
Pavel Begunkov650b5482020-05-17 14:02:11 +03005606 if (!req->io) {
Pavel Begunkov650b5482020-05-17 14:02:11 +03005607 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03005608 if (ret)
Pavel Begunkov650b5482020-05-17 14:02:11 +03005609 return ret;
5610 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005611 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005612 de = kmalloc(sizeof(*de), GFP_KERNEL);
5613 if (!de)
5614 return -ENOMEM;
Jens Axboe2d283902019-12-04 11:08:05 -07005615
Jens Axboede0617e2019-04-06 21:51:27 -06005616 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005617 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005618 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005619 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005620 io_queue_async_work(req);
5621 return -EIOCBQUEUED;
Jens Axboede0617e2019-04-06 21:51:27 -06005622 }
5623
Jens Axboe915967f2019-11-21 09:01:20 -07005624 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005625 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005626 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005627 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboede0617e2019-04-06 21:51:27 -06005628 spin_unlock_irq(&ctx->completion_lock);
5629 return -EIOCBQUEUED;
5630}
5631
Jens Axboef573d382020-09-22 10:19:24 -06005632static void io_req_drop_files(struct io_kiocb *req)
5633{
5634 struct io_ring_ctx *ctx = req->ctx;
5635 unsigned long flags;
5636
5637 spin_lock_irqsave(&ctx->inflight_lock, flags);
5638 list_del(&req->inflight_entry);
5639 if (waitqueue_active(&ctx->inflight_wait))
5640 wake_up(&ctx->inflight_wait);
5641 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
5642 req->flags &= ~REQ_F_INFLIGHT;
5643 req->work.files = NULL;
5644}
5645
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03005646static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005647{
5648 struct io_async_ctx *io = req->io;
5649
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005650 if (req->flags & REQ_F_BUFFER_SELECTED) {
5651 switch (req->opcode) {
5652 case IORING_OP_READV:
5653 case IORING_OP_READ_FIXED:
5654 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005655 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005656 break;
5657 case IORING_OP_RECVMSG:
5658 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005659 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005660 break;
5661 }
5662 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005663 }
5664
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005665 if (req->flags & REQ_F_NEED_CLEANUP) {
5666 switch (req->opcode) {
5667 case IORING_OP_READV:
5668 case IORING_OP_READ_FIXED:
5669 case IORING_OP_READ:
5670 case IORING_OP_WRITEV:
5671 case IORING_OP_WRITE_FIXED:
5672 case IORING_OP_WRITE:
Jens Axboeff6165b2020-08-13 09:47:43 -06005673 if (io->rw.free_iovec)
5674 kfree(io->rw.free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005675 break;
5676 case IORING_OP_RECVMSG:
5677 case IORING_OP_SENDMSG:
5678 if (io->msg.iov != io->msg.fast_iov)
5679 kfree(io->msg.iov);
5680 break;
5681 case IORING_OP_SPLICE:
5682 case IORING_OP_TEE:
5683 io_put_file(req, req->splice.file_in,
5684 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5685 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06005686 case IORING_OP_OPENAT:
5687 case IORING_OP_OPENAT2:
5688 if (req->open.filename)
5689 putname(req->open.filename);
5690 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005691 }
5692 req->flags &= ~REQ_F_NEED_CLEANUP;
5693 }
Pavel Begunkovbb175342020-08-20 11:33:35 +03005694
Jens Axboef573d382020-09-22 10:19:24 -06005695 if (req->flags & REQ_F_INFLIGHT)
5696 io_req_drop_files(req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005697}
5698
Jens Axboe3529d8c2019-12-19 18:24:38 -07005699static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06005700 bool force_nonblock, struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005701{
Jackie Liua197f662019-11-08 08:09:12 -07005702 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005703 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005704
Jens Axboed625c6e2019-12-17 19:53:05 -07005705 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005706 case IORING_OP_NOP:
Jens Axboe229a7b62020-06-22 10:13:11 -06005707 ret = io_nop(req, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005708 break;
5709 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005710 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005711 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005712 if (sqe) {
5713 ret = io_read_prep(req, sqe, force_nonblock);
5714 if (ret < 0)
5715 break;
5716 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005717 ret = io_read(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005718 break;
5719 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005720 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005721 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005722 if (sqe) {
5723 ret = io_write_prep(req, sqe, force_nonblock);
5724 if (ret < 0)
5725 break;
5726 }
Jens Axboea1d7c392020-06-22 11:09:46 -06005727 ret = io_write(req, force_nonblock, cs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005728 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005729 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005730 if (sqe) {
5731 ret = io_prep_fsync(req, sqe);
5732 if (ret < 0)
5733 break;
5734 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005735 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005736 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005737 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005738 if (sqe) {
5739 ret = io_poll_add_prep(req, sqe);
5740 if (ret)
5741 break;
5742 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005743 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005744 break;
5745 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005746 if (sqe) {
5747 ret = io_poll_remove_prep(req, sqe);
5748 if (ret < 0)
5749 break;
5750 }
Jens Axboefc4df992019-12-10 14:38:45 -07005751 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005752 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005753 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005754 if (sqe) {
5755 ret = io_prep_sfr(req, sqe);
5756 if (ret < 0)
5757 break;
5758 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005759 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005760 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005761 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005762 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005763 if (sqe) {
5764 ret = io_sendmsg_prep(req, sqe);
5765 if (ret < 0)
5766 break;
5767 }
Jens Axboefddafac2020-01-04 20:19:44 -07005768 if (req->opcode == IORING_OP_SENDMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005769 ret = io_sendmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005770 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005771 ret = io_send(req, force_nonblock, cs);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005772 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005773 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005774 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005775 if (sqe) {
5776 ret = io_recvmsg_prep(req, sqe);
5777 if (ret)
5778 break;
5779 }
Jens Axboefddafac2020-01-04 20:19:44 -07005780 if (req->opcode == IORING_OP_RECVMSG)
Jens Axboe229a7b62020-06-22 10:13:11 -06005781 ret = io_recvmsg(req, force_nonblock, cs);
Jens Axboefddafac2020-01-04 20:19:44 -07005782 else
Jens Axboe229a7b62020-06-22 10:13:11 -06005783 ret = io_recv(req, force_nonblock, cs);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005784 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005785 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005786 if (sqe) {
5787 ret = io_timeout_prep(req, sqe, false);
5788 if (ret)
5789 break;
5790 }
Jens Axboefc4df992019-12-10 14:38:45 -07005791 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005792 break;
Jens Axboe11365042019-10-16 09:08:32 -06005793 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005794 if (sqe) {
5795 ret = io_timeout_remove_prep(req, sqe);
5796 if (ret)
5797 break;
5798 }
Jens Axboefc4df992019-12-10 14:38:45 -07005799 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005800 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005801 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005802 if (sqe) {
5803 ret = io_accept_prep(req, sqe);
5804 if (ret)
5805 break;
5806 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005807 ret = io_accept(req, force_nonblock, cs);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005808 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005809 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005810 if (sqe) {
5811 ret = io_connect_prep(req, sqe);
5812 if (ret)
5813 break;
5814 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005815 ret = io_connect(req, force_nonblock, cs);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005816 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005817 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005818 if (sqe) {
5819 ret = io_async_cancel_prep(req, sqe);
5820 if (ret)
5821 break;
5822 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005823 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005824 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005825 case IORING_OP_FALLOCATE:
5826 if (sqe) {
5827 ret = io_fallocate_prep(req, sqe);
5828 if (ret)
5829 break;
5830 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005831 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005832 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005833 case IORING_OP_OPENAT:
5834 if (sqe) {
5835 ret = io_openat_prep(req, sqe);
5836 if (ret)
5837 break;
5838 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005839 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005840 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005841 case IORING_OP_CLOSE:
5842 if (sqe) {
5843 ret = io_close_prep(req, sqe);
5844 if (ret)
5845 break;
5846 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005847 ret = io_close(req, force_nonblock, cs);
Jens Axboeb5dba592019-12-11 14:02:38 -07005848 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005849 case IORING_OP_FILES_UPDATE:
5850 if (sqe) {
5851 ret = io_files_update_prep(req, sqe);
5852 if (ret)
5853 break;
5854 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005855 ret = io_files_update(req, force_nonblock, cs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005856 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005857 case IORING_OP_STATX:
5858 if (sqe) {
5859 ret = io_statx_prep(req, sqe);
5860 if (ret)
5861 break;
5862 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005863 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005864 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005865 case IORING_OP_FADVISE:
5866 if (sqe) {
5867 ret = io_fadvise_prep(req, sqe);
5868 if (ret)
5869 break;
5870 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005871 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005872 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005873 case IORING_OP_MADVISE:
5874 if (sqe) {
5875 ret = io_madvise_prep(req, sqe);
5876 if (ret)
5877 break;
5878 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005879 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005880 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005881 case IORING_OP_OPENAT2:
5882 if (sqe) {
5883 ret = io_openat2_prep(req, sqe);
5884 if (ret)
5885 break;
5886 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005887 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005888 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005889 case IORING_OP_EPOLL_CTL:
5890 if (sqe) {
5891 ret = io_epoll_ctl_prep(req, sqe);
5892 if (ret)
5893 break;
5894 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005895 ret = io_epoll_ctl(req, force_nonblock, cs);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005896 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005897 case IORING_OP_SPLICE:
5898 if (sqe) {
5899 ret = io_splice_prep(req, sqe);
5900 if (ret < 0)
5901 break;
5902 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005903 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005904 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005905 case IORING_OP_PROVIDE_BUFFERS:
5906 if (sqe) {
5907 ret = io_provide_buffers_prep(req, sqe);
5908 if (ret)
5909 break;
5910 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005911 ret = io_provide_buffers(req, force_nonblock, cs);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005912 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005913 case IORING_OP_REMOVE_BUFFERS:
5914 if (sqe) {
5915 ret = io_remove_buffers_prep(req, sqe);
5916 if (ret)
5917 break;
5918 }
Jens Axboe229a7b62020-06-22 10:13:11 -06005919 ret = io_remove_buffers(req, force_nonblock, cs);
Jens Axboe31b51512019-01-18 22:56:34 -07005920 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005921 case IORING_OP_TEE:
5922 if (sqe) {
5923 ret = io_tee_prep(req, sqe);
5924 if (ret < 0)
5925 break;
5926 }
5927 ret = io_tee(req, force_nonblock);
5928 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005929 default:
5930 ret = -EINVAL;
5931 break;
5932 }
5933
5934 if (ret)
5935 return ret;
5936
Jens Axboeb5325762020-05-19 21:20:27 -06005937 /* If the op doesn't have a file, we're not polling for it */
5938 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005939 const bool in_async = io_wq_current_is_worker();
5940
Jens Axboe11ba8202020-01-15 21:51:17 -07005941 /* workqueue context doesn't hold uring_lock, grab it now */
5942 if (in_async)
5943 mutex_lock(&ctx->uring_lock);
5944
Jens Axboe2b188cc2019-01-07 10:46:33 -07005945 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005946
5947 if (in_async)
5948 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005949 }
5950
5951 return 0;
5952}
5953
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005954static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005955{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005956 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005957 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06005958 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005959
Pavel Begunkov6df1db62020-07-03 22:15:06 +03005960 timeout = io_prep_linked_timeout(req);
5961 if (timeout)
5962 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005963
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005964 /* if NO_CANCEL is set, we must still run the work */
5965 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5966 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005967 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005968 }
Jens Axboe31b51512019-01-18 22:56:34 -07005969
Jens Axboe561fb042019-10-24 07:25:42 -06005970 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005971 do {
Jens Axboef13fad72020-06-22 09:34:30 -06005972 ret = io_issue_sqe(req, NULL, false, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06005973 /*
5974 * We can get EAGAIN for polled IO even though we're
5975 * forcing a sync submission from here, since we can't
5976 * wait for request slots on the block side.
5977 */
5978 if (ret != -EAGAIN)
5979 break;
5980 cond_resched();
5981 } while (1);
5982 }
Jens Axboe31b51512019-01-18 22:56:34 -07005983
Jens Axboe561fb042019-10-24 07:25:42 -06005984 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005985 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005986 io_req_complete(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005987 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005988
Pavel Begunkovf4db7182020-06-25 18:20:54 +03005989 return io_steal_work(req);
Jens Axboe31b51512019-01-18 22:56:34 -07005990}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005991
Jens Axboe65e19f52019-10-26 07:20:21 -06005992static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5993 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005994{
Jens Axboe65e19f52019-10-26 07:20:21 -06005995 struct fixed_file_table *table;
5996
Jens Axboe05f3fb32019-12-09 11:22:50 -07005997 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005998 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005999}
6000
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006001static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
6002 int fd, struct file **out_file, bool fixed)
6003{
6004 struct io_ring_ctx *ctx = req->ctx;
6005 struct file *file;
6006
6007 if (fixed) {
6008 if (unlikely(!ctx->file_data ||
6009 (unsigned) fd >= ctx->nr_user_files))
6010 return -EBADF;
6011 fd = array_index_nospec(fd, ctx->nr_user_files);
6012 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06006013 if (file) {
6014 req->fixed_file_refs = ctx->file_data->cur_refs;
6015 percpu_ref_get(req->fixed_file_refs);
6016 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006017 } else {
6018 trace_io_uring_file_get(ctx, fd);
6019 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006020 }
6021
Jens Axboefd2206e2020-06-02 16:40:47 -06006022 if (file || io_op_defs[req->opcode].needs_file_no_error) {
6023 *out_file = file;
6024 return 0;
6025 }
6026 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006027}
6028
Jens Axboe3529d8c2019-12-19 18:24:38 -07006029static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06006030 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06006031{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006032 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06006033
Jens Axboe63ff8222020-05-07 14:56:15 -06006034 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006035 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006036 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06006037
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006038 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06006039}
6040
Jackie Liua197f662019-11-08 08:09:12 -07006041static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006042{
Jens Axboefcb323c2019-10-24 12:39:47 -06006043 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07006044 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06006045
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006046 io_req_init_async(req);
6047
Jens Axboe5b0bbee2020-04-27 10:41:22 -06006048 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07006049 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006050 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07006051 return -EBADF;
6052
Jens Axboefcb323c2019-10-24 12:39:47 -06006053 rcu_read_lock();
6054 spin_lock_irq(&ctx->inflight_lock);
6055 /*
6056 * We use the f_ops->flush() handler to ensure that we can flush
6057 * out work accessing these files if the fd is closed. Check if
6058 * the fd has changed since we started down this path, and disallow
6059 * this operation if it has.
6060 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006061 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006062 list_add(&req->inflight_entry, &ctx->inflight_list);
6063 req->flags |= REQ_F_INFLIGHT;
6064 req->work.files = current->files;
6065 ret = 0;
6066 }
6067 spin_unlock_irq(&ctx->inflight_lock);
6068 rcu_read_unlock();
6069
6070 return ret;
6071}
6072
Pavel Begunkovf56040b2020-07-23 20:25:21 +03006073static inline int io_prep_work_files(struct io_kiocb *req)
6074{
6075 if (!io_op_defs[req->opcode].file_table)
6076 return 0;
6077 return io_grab_files(req);
6078}
6079
Jens Axboe2665abf2019-11-05 12:40:47 -07006080static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6081{
Jens Axboead8a48a2019-11-15 08:49:11 -07006082 struct io_timeout_data *data = container_of(timer,
6083 struct io_timeout_data, timer);
6084 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006085 struct io_ring_ctx *ctx = req->ctx;
6086 struct io_kiocb *prev = NULL;
6087 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006088
6089 spin_lock_irqsave(&ctx->completion_lock, flags);
6090
6091 /*
6092 * We don't expect the list to be empty, that will only happen if we
6093 * race with the completion of the linked work.
6094 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006095 if (!list_empty(&req->link_list)) {
6096 prev = list_entry(req->link_list.prev, struct io_kiocb,
6097 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006098 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03006099 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07006100 prev->flags &= ~REQ_F_LINK_TIMEOUT;
6101 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07006102 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006103 }
6104
6105 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6106
6107 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006108 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006109 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07006110 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07006111 } else {
Jens Axboee1e16092020-06-22 09:17:17 -06006112 io_req_complete(req, -ETIME);
Jens Axboe2665abf2019-11-05 12:40:47 -07006113 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006114 return HRTIMER_NORESTART;
6115}
6116
Jens Axboe7271ef32020-08-10 09:55:22 -06006117static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006118{
Jens Axboe76a46e02019-11-10 23:34:16 -07006119 /*
6120 * If the list is now empty, then our linked request finished before
6121 * we got a chance to setup the timer
6122 */
Pavel Begunkov44932332019-12-05 16:16:35 +03006123 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07006124 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006125
Jens Axboead8a48a2019-11-15 08:49:11 -07006126 data->timer.function = io_link_timeout_fn;
6127 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6128 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006129 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006130}
6131
6132static void io_queue_linked_timeout(struct io_kiocb *req)
6133{
6134 struct io_ring_ctx *ctx = req->ctx;
6135
6136 spin_lock_irq(&ctx->completion_lock);
6137 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006138 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006139
Jens Axboe2665abf2019-11-05 12:40:47 -07006140 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006141 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006142}
6143
Jens Axboead8a48a2019-11-15 08:49:11 -07006144static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006145{
6146 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006147
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006148 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07006149 return NULL;
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006150 if (req->flags & REQ_F_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006151 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006152
Pavel Begunkov44932332019-12-05 16:16:35 +03006153 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
6154 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07006155 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07006156 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006157
Jens Axboe76a46e02019-11-10 23:34:16 -07006158 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006159 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006160}
6161
Jens Axboef13fad72020-06-22 09:34:30 -06006162static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6163 struct io_comp_state *cs)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006164{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006165 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006166 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07006167 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006168 int ret;
6169
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006170again:
6171 linked_timeout = io_prep_linked_timeout(req);
6172
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006173 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6174 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07006175 if (old_creds)
6176 revert_creds(old_creds);
6177 if (old_creds == req->work.creds)
6178 old_creds = NULL; /* restored original creds */
6179 else
6180 old_creds = override_creds(req->work.creds);
6181 }
6182
Jens Axboef13fad72020-06-22 09:34:30 -06006183 ret = io_issue_sqe(req, sqe, true, cs);
Jens Axboe491381ce2019-10-17 09:20:46 -06006184
6185 /*
6186 * We async punt it if the file wasn't marked NOWAIT, or if the file
6187 * doesn't support non-blocking read/write attempts
6188 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006189 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006190 if (!io_arm_poll_handler(req)) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006191punt:
Pavel Begunkovf063c542020-07-25 14:41:59 +03006192 ret = io_prep_work_files(req);
6193 if (unlikely(ret))
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006194 goto err;
Pavel Begunkovf063c542020-07-25 14:41:59 +03006195 /*
6196 * Queued up for async execution, worker will release
6197 * submit reference when the iocb is actually submitted.
6198 */
6199 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006200 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03006201
Pavel Begunkovf063c542020-07-25 14:41:59 +03006202 if (linked_timeout)
6203 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006204 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006205 }
Jens Axboee65ef562019-03-12 10:16:44 -06006206
Pavel Begunkov652532a2020-07-03 22:15:07 +03006207 if (unlikely(ret)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06006208err:
Pavel Begunkov652532a2020-07-03 22:15:07 +03006209 /* un-prep timeout, so it'll be killed as any other linked */
6210 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006211 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006212 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006213 io_req_complete(req, ret);
6214 goto exit;
Jens Axboe9e645e112019-05-10 16:07:28 -06006215 }
Pavel Begunkov652532a2020-07-03 22:15:07 +03006216
Jens Axboe6c271ce2019-01-10 11:22:30 -07006217 /* drop submission reference */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03006218 nxt = io_put_req_find_next(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006219 if (linked_timeout)
6220 io_queue_linked_timeout(linked_timeout);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006221
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006222 if (nxt) {
6223 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03006224
6225 if (req->flags & REQ_F_FORCE_ASYNC)
6226 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07006227 goto again;
6228 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03006229exit:
Jens Axboe193155c2020-02-22 23:22:19 -07006230 if (old_creds)
6231 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006232}
6233
Jens Axboef13fad72020-06-22 09:34:30 -06006234static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6235 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006236{
6237 int ret;
6238
Jens Axboe3529d8c2019-12-19 18:24:38 -07006239 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006240 if (ret) {
6241 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006242fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006243 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006244 io_put_req(req);
6245 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006246 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006247 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006248 if (!req->io) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006249 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006250 if (unlikely(ret))
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03006251 goto fail_req;
6252 }
6253
Jens Axboece35a472019-12-17 08:04:44 -07006254 /*
6255 * Never try inline submit of IOSQE_ASYNC is set, go straight
6256 * to async execution.
6257 */
Pavel Begunkov3e863ea2020-07-23 20:17:20 +03006258 io_req_init_async(req);
Jens Axboece35a472019-12-17 08:04:44 -07006259 req->work.flags |= IO_WQ_WORK_CONCURRENT;
6260 io_queue_async_work(req);
6261 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006262 __io_queue_sqe(req, sqe, cs);
Jens Axboece35a472019-12-17 08:04:44 -07006263 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006264}
6265
Jens Axboef13fad72020-06-22 09:34:30 -06006266static inline void io_queue_link_head(struct io_kiocb *req,
6267 struct io_comp_state *cs)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006268{
Jens Axboe94ae5e72019-11-14 19:39:52 -07006269 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Jens Axboee1e16092020-06-22 09:17:17 -06006270 io_put_req(req);
6271 io_req_complete(req, -ECANCELED);
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006272 } else
Jens Axboef13fad72020-06-22 09:34:30 -06006273 io_queue_sqe(req, NULL, cs);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006274}
6275
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006276static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboef13fad72020-06-22 09:34:30 -06006277 struct io_kiocb **link, struct io_comp_state *cs)
Jens Axboe9e645e112019-05-10 16:07:28 -06006278{
Jackie Liua197f662019-11-08 08:09:12 -07006279 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006280 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006281
Jens Axboe9e645e112019-05-10 16:07:28 -06006282 /*
6283 * If we already have a head request, queue this one for async
6284 * submittal once the head completes. If we don't have a head but
6285 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6286 * submitted sync once the chain is complete. If none of those
6287 * conditions are true (normal request), then just queue it.
6288 */
6289 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03006290 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06006291
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006292 /*
6293 * Taking sequential execution of a link, draining both sides
6294 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6295 * requests in the link. So, it drains the head and the
6296 * next after the link request. The last one is done via
6297 * drain_next flag to persist the effect across calls.
6298 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006299 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006300 head->flags |= REQ_F_IO_DRAIN;
6301 ctx->drain_next = 1;
6302 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07006303 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006304 if (unlikely(ret)) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006305 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03006306 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006307 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07006308 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03006309 trace_io_uring_link(ctx, req, head);
6310 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06006311
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006312 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006313 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Jens Axboef13fad72020-06-22 09:34:30 -06006314 io_queue_link_head(head, cs);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006315 *link = NULL;
6316 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006317 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006318 if (unlikely(ctx->drain_next)) {
6319 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006320 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006321 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006322 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03006323 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03006324 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03006325
Pavel Begunkov711be032020-01-17 03:57:59 +03006326 ret = io_req_defer_prep(req, sqe);
Pavel Begunkov327d6d92020-07-15 12:46:51 +03006327 if (unlikely(ret))
Pavel Begunkov711be032020-01-17 03:57:59 +03006328 req->flags |= REQ_F_FAIL_LINK;
6329 *link = req;
6330 } else {
Jens Axboef13fad72020-06-22 09:34:30 -06006331 io_queue_sqe(req, sqe, cs);
Pavel Begunkov711be032020-01-17 03:57:59 +03006332 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006333 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006334
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006335 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006336}
6337
Jens Axboe9a56a232019-01-09 09:06:50 -07006338/*
6339 * Batched submission is done, ensure local IO is flushed out.
6340 */
6341static void io_submit_state_end(struct io_submit_state *state)
6342{
Jens Axboef13fad72020-06-22 09:34:30 -06006343 if (!list_empty(&state->comp.list))
6344 io_submit_flush_completions(&state->comp);
Jens Axboe9a56a232019-01-09 09:06:50 -07006345 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006346 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07006347 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03006348 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07006349}
6350
6351/*
6352 * Start submission side cache.
6353 */
6354static void io_submit_state_start(struct io_submit_state *state,
Jens Axboe013538b2020-06-22 09:29:15 -06006355 struct io_ring_ctx *ctx, unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006356{
6357 blk_start_plug(&state->plug);
Jens Axboe013538b2020-06-22 09:29:15 -06006358 state->comp.nr = 0;
6359 INIT_LIST_HEAD(&state->comp.list);
6360 state->comp.ctx = ctx;
Jens Axboe2579f912019-01-09 09:10:43 -07006361 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07006362 state->file = NULL;
6363 state->ios_left = max_ios;
6364}
6365
Jens Axboe2b188cc2019-01-07 10:46:33 -07006366static void io_commit_sqring(struct io_ring_ctx *ctx)
6367{
Hristo Venev75b28af2019-08-26 17:23:46 +00006368 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006369
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006370 /*
6371 * Ensure any loads from the SQEs are done at this point,
6372 * since once we write the new head, the application could
6373 * write new data to them.
6374 */
6375 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006376}
6377
6378/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006379 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006380 * that is mapped by userspace. This means that care needs to be taken to
6381 * ensure that reads are stable, as we cannot rely on userspace always
6382 * being a good citizen. If members of the sqe are validated and then later
6383 * used, it's important that those reads are done through READ_ONCE() to
6384 * prevent a re-load down the line.
6385 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006386static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006387{
Hristo Venev75b28af2019-08-26 17:23:46 +00006388 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006389 unsigned head;
6390
6391 /*
6392 * The cached sq head (or cq tail) serves two purposes:
6393 *
6394 * 1) allows us to batch the cost of updating the user visible
6395 * head updates.
6396 * 2) allows the kernel side to track the head on its own, even
6397 * though the application is the one updating it.
6398 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006399 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006400 if (likely(head < ctx->sq_entries))
6401 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006402
6403 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006404 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006405 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006406 return NULL;
6407}
6408
6409static inline void io_consume_sqe(struct io_ring_ctx *ctx)
6410{
6411 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006412}
6413
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006414#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
6415 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
6416 IOSQE_BUFFER_SELECT)
6417
6418static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6419 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006420 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006421{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006422 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06006423 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006424
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006425 req->opcode = READ_ONCE(sqe->opcode);
6426 req->user_data = READ_ONCE(sqe->user_data);
6427 req->io = NULL;
6428 req->file = NULL;
6429 req->ctx = ctx;
6430 req->flags = 0;
6431 /* one is dropped after submission, the other at completion */
6432 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006433 req->task = current;
Jens Axboee3bc8e92020-09-24 08:45:57 -06006434 get_task_struct(req->task);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006435 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006436
6437 if (unlikely(req->opcode >= IORING_OP_LAST))
6438 return -EINVAL;
6439
Jens Axboe9d8426a2020-06-16 18:42:49 -06006440 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6441 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006442
6443 sqe_flags = READ_ONCE(sqe->flags);
6444 /* enforce forwards compatibility on users */
6445 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6446 return -EINVAL;
6447
6448 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6449 !io_op_defs[req->opcode].buffer_select)
6450 return -EOPNOTSUPP;
6451
6452 id = READ_ONCE(sqe->personality);
6453 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006454 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006455 req->work.creds = idr_find(&ctx->personality_idr, id);
6456 if (unlikely(!req->work.creds))
6457 return -EINVAL;
6458 get_cred(req->work.creds);
6459 }
6460
6461 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006462 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006463
Jens Axboe63ff8222020-05-07 14:56:15 -06006464 if (!io_op_defs[req->opcode].needs_file)
6465 return 0;
6466
6467 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006468}
6469
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006470static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006471 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006472{
Jens Axboeac8691c2020-06-01 08:30:41 -06006473 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006474 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006475 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006476
Jens Axboec4a2ed72019-11-21 21:01:26 -07006477 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006478 if (test_bit(0, &ctx->sq_check_overflow)) {
6479 if (!list_empty(&ctx->cq_overflow_list) &&
6480 !io_cqring_overflow_flush(ctx, false))
6481 return -EBUSY;
6482 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006483
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006484 /* make sure SQ entry isn't read before tail */
6485 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006486
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006487 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6488 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006489
Jens Axboe013538b2020-06-22 09:29:15 -06006490 io_submit_state_start(&state, ctx, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006491
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006492 ctx->ring_fd = ring_fd;
6493 ctx->ring_file = ring_file;
6494
Jens Axboe6c271ce2019-01-10 11:22:30 -07006495 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006496 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006497 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006498 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006499
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006500 sqe = io_get_sqe(ctx);
6501 if (unlikely(!sqe)) {
6502 io_consume_sqe(ctx);
6503 break;
6504 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006505 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006506 if (unlikely(!req)) {
6507 if (!submitted)
6508 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006509 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006510 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006511
Jens Axboeac8691c2020-06-01 08:30:41 -06006512 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006513 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006514 /* will complete beyond this point, count as submitted */
6515 submitted++;
6516
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006517 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006518fail_req:
Jens Axboee1e16092020-06-22 09:17:17 -06006519 io_put_req(req);
6520 io_req_complete(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006521 break;
6522 }
6523
Jens Axboe354420f2020-01-08 18:55:15 -07006524 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006525 true, io_async_submit(ctx));
Jens Axboef13fad72020-06-22 09:34:30 -06006526 err = io_submit_sqe(req, sqe, &link, &state.comp);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006527 if (err)
6528 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006529 }
6530
Pavel Begunkov9466f432020-01-25 22:34:01 +03006531 if (unlikely(submitted != nr)) {
6532 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6533
6534 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6535 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006536 if (link)
Jens Axboef13fad72020-06-22 09:34:30 -06006537 io_queue_link_head(link, &state.comp);
Jens Axboeac8691c2020-06-01 08:30:41 -06006538 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006539
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006540 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6541 io_commit_sqring(ctx);
6542
Jens Axboe6c271ce2019-01-10 11:22:30 -07006543 return submitted;
6544}
6545
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006546static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6547{
6548 /* Tell userspace we may need a wakeup call */
6549 spin_lock_irq(&ctx->completion_lock);
6550 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6551 spin_unlock_irq(&ctx->completion_lock);
6552}
6553
6554static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6555{
6556 spin_lock_irq(&ctx->completion_lock);
6557 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6558 spin_unlock_irq(&ctx->completion_lock);
6559}
6560
Jens Axboe6c271ce2019-01-10 11:22:30 -07006561static int io_sq_thread(void *data)
6562{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006563 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006564 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006565 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006566 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006567 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006568
Jens Axboe0f158b42020-05-14 17:18:39 -06006569 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006570
Jens Axboe181e4482019-11-25 08:52:30 -07006571 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006572
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006573 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006574 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006575 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006576
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006577 if (!list_empty(&ctx->iopoll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006578 unsigned nr_events = 0;
6579
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006580 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006581 if (!list_empty(&ctx->iopoll_list) && !need_resched())
Pavel Begunkov9dedd562020-07-07 16:36:20 +03006582 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006583 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006584 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006585 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006586 }
6587
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006588 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006589
6590 /*
6591 * If submit got -EBUSY, flag us as needing the application
6592 * to enter the kernel to reap and flush events.
6593 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006594 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006595 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006596 * Drop cur_mm before scheduling, we can't hold it for
6597 * long periods (or over schedule()). Do this before
6598 * adding ourselves to the waitqueue, as the unuse/drop
6599 * may sleep.
6600 */
Jens Axboe4349f302020-07-09 15:07:01 -06006601 io_sq_thread_drop_mm();
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006602
6603 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006604 * We're polling. If we're within the defined idle
6605 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006606 * to sleep. The exception is if we got EBUSY doing
6607 * more IO, we should wait for the application to
6608 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006609 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006610 if (!list_empty(&ctx->iopoll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006611 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6612 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06006613 io_run_task_work();
Jens Axboe9831a902019-09-19 09:48:55 -06006614 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006615 continue;
6616 }
6617
Jens Axboe6c271ce2019-01-10 11:22:30 -07006618 prepare_to_wait(&ctx->sqo_wait, &wait,
6619 TASK_INTERRUPTIBLE);
6620
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006621 /*
6622 * While doing polled IO, before going to sleep, we need
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006623 * to check if there are new reqs added to iopoll_list,
6624 * it is because reqs may have been punted to io worker
6625 * and will be added to iopoll_list later, hence check
6626 * the iopoll_list again.
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006627 */
6628 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
Pavel Begunkov540e32a2020-07-13 23:37:09 +03006629 !list_empty_careful(&ctx->iopoll_list)) {
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006630 finish_wait(&ctx->sqo_wait, &wait);
6631 continue;
6632 }
6633
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006634 io_ring_set_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006635
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006636 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006637 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006638 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006639 finish_wait(&ctx->sqo_wait, &wait);
6640 break;
6641 }
Jens Axboe4c6e2772020-07-01 11:29:10 -06006642 if (io_run_task_work()) {
Hillf Danton10bea962020-04-01 17:19:33 +08006643 finish_wait(&ctx->sqo_wait, &wait);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006644 io_ring_clear_wakeup_flag(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07006645 continue;
6646 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006647 if (signal_pending(current))
6648 flush_signals(current);
6649 schedule();
6650 finish_wait(&ctx->sqo_wait, &wait);
6651
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006652 io_ring_clear_wakeup_flag(ctx);
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006653 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006654 continue;
6655 }
6656 finish_wait(&ctx->sqo_wait, &wait);
6657
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006658 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006659 }
6660
Jens Axboe8a4955f2019-12-09 14:52:35 -07006661 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006662 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6663 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006664 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006665 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006666 }
6667
Jens Axboe4c6e2772020-07-01 11:29:10 -06006668 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07006669
Jens Axboe4349f302020-07-09 15:07:01 -06006670 io_sq_thread_drop_mm();
Jens Axboe181e4482019-11-25 08:52:30 -07006671 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006672
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006673 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006674
Jens Axboe6c271ce2019-01-10 11:22:30 -07006675 return 0;
6676}
6677
Jens Axboebda52162019-09-24 13:47:15 -06006678struct io_wait_queue {
6679 struct wait_queue_entry wq;
6680 struct io_ring_ctx *ctx;
6681 unsigned to_wait;
6682 unsigned nr_timeouts;
6683};
6684
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006685static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006686{
6687 struct io_ring_ctx *ctx = iowq->ctx;
6688
6689 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006690 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006691 * started waiting. For timeouts, we always want to return to userspace,
6692 * regardless of event count.
6693 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006694 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006695 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6696}
6697
6698static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6699 int wake_flags, void *key)
6700{
6701 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6702 wq);
6703
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006704 /* use noflush == true, as we can't safely rely on locking context */
6705 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006706 return -1;
6707
6708 return autoremove_wake_function(curr, mode, wake_flags, key);
6709}
6710
Jens Axboe2b188cc2019-01-07 10:46:33 -07006711/*
6712 * Wait until events become available, if we don't already have some. The
6713 * application must reap them itself, as they reside on the shared cq ring.
6714 */
6715static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6716 const sigset_t __user *sig, size_t sigsz)
6717{
Jens Axboebda52162019-09-24 13:47:15 -06006718 struct io_wait_queue iowq = {
6719 .wq = {
6720 .private = current,
6721 .func = io_wake_function,
6722 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6723 },
6724 .ctx = ctx,
6725 .to_wait = min_events,
6726 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006727 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006728 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006729
Jens Axboeb41e9852020-02-17 09:52:41 -07006730 do {
6731 if (io_cqring_events(ctx, false) >= min_events)
6732 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006733 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006734 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006735 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006736
6737 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006738#ifdef CONFIG_COMPAT
6739 if (in_compat_syscall())
6740 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006741 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006742 else
6743#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006744 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006745
Jens Axboe2b188cc2019-01-07 10:46:33 -07006746 if (ret)
6747 return ret;
6748 }
6749
Jens Axboebda52162019-09-24 13:47:15 -06006750 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006751 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006752 do {
6753 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6754 TASK_INTERRUPTIBLE);
Jens Axboece593a62020-06-30 12:39:05 -06006755 /* make sure we run task_work before checking for signals */
Jens Axboe4c6e2772020-07-01 11:29:10 -06006756 if (io_run_task_work())
6757 continue;
Jens Axboece593a62020-06-30 12:39:05 -06006758 if (signal_pending(current)) {
Jens Axboeb7db41c2020-07-04 08:55:50 -06006759 if (current->jobctl & JOBCTL_TASK_WORK) {
6760 spin_lock_irq(&current->sighand->siglock);
6761 current->jobctl &= ~JOBCTL_TASK_WORK;
6762 recalc_sigpending();
6763 spin_unlock_irq(&current->sighand->siglock);
6764 continue;
6765 }
6766 ret = -EINTR;
Jens Axboece593a62020-06-30 12:39:05 -06006767 break;
6768 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006769 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006770 break;
6771 schedule();
Jens Axboebda52162019-09-24 13:47:15 -06006772 } while (1);
6773 finish_wait(&ctx->wait, &iowq.wq);
6774
Jens Axboeb7db41c2020-07-04 08:55:50 -06006775 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006776
Hristo Venev75b28af2019-08-26 17:23:46 +00006777 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006778}
6779
Jens Axboe6b063142019-01-10 22:13:58 -07006780static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6781{
6782#if defined(CONFIG_UNIX)
6783 if (ctx->ring_sock) {
6784 struct sock *sock = ctx->ring_sock->sk;
6785 struct sk_buff *skb;
6786
6787 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6788 kfree_skb(skb);
6789 }
6790#else
6791 int i;
6792
Jens Axboe65e19f52019-10-26 07:20:21 -06006793 for (i = 0; i < ctx->nr_user_files; i++) {
6794 struct file *file;
6795
6796 file = io_file_from_index(ctx, i);
6797 if (file)
6798 fput(file);
6799 }
Jens Axboe6b063142019-01-10 22:13:58 -07006800#endif
6801}
6802
Jens Axboe05f3fb32019-12-09 11:22:50 -07006803static void io_file_ref_kill(struct percpu_ref *ref)
6804{
6805 struct fixed_file_data *data;
6806
6807 data = container_of(ref, struct fixed_file_data, refs);
6808 complete(&data->done);
6809}
6810
Jens Axboe6b063142019-01-10 22:13:58 -07006811static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6812{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006813 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006814 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006815 unsigned nr_tables, i;
6816
Jens Axboe05f3fb32019-12-09 11:22:50 -07006817 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006818 return -ENXIO;
6819
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006820 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006821 if (!list_empty(&data->ref_list))
6822 ref_node = list_first_entry(&data->ref_list,
6823 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006824 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006825 if (ref_node)
6826 percpu_ref_kill(&ref_node->refs);
6827
6828 percpu_ref_kill(&data->refs);
6829
6830 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006831 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006832 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006833
Jens Axboe6b063142019-01-10 22:13:58 -07006834 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006835 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6836 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006837 kfree(data->table[i].files);
6838 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006839 percpu_ref_exit(&data->refs);
6840 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006841 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006842 ctx->nr_user_files = 0;
6843 return 0;
6844}
6845
Jens Axboe6c271ce2019-01-10 11:22:30 -07006846static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6847{
6848 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006849 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006850 /*
6851 * The park is a bit of a work-around, without it we get
6852 * warning spews on shutdown with SQPOLL set and affinity
6853 * set to a single CPU.
6854 */
Jens Axboe06058632019-04-13 09:26:03 -06006855 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006856 kthread_stop(ctx->sqo_thread);
6857 ctx->sqo_thread = NULL;
6858 }
6859}
6860
Jens Axboe6b063142019-01-10 22:13:58 -07006861static void io_finish_async(struct io_ring_ctx *ctx)
6862{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006863 io_sq_thread_stop(ctx);
6864
Jens Axboe561fb042019-10-24 07:25:42 -06006865 if (ctx->io_wq) {
6866 io_wq_destroy(ctx->io_wq);
6867 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006868 }
6869}
6870
6871#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006872/*
6873 * Ensure the UNIX gc is aware of our file set, so we are certain that
6874 * the io_uring can be safely unregistered on process exit, even if we have
6875 * loops in the file referencing.
6876 */
6877static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6878{
6879 struct sock *sk = ctx->ring_sock->sk;
6880 struct scm_fp_list *fpl;
6881 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006882 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006883
Jens Axboe6b063142019-01-10 22:13:58 -07006884 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6885 if (!fpl)
6886 return -ENOMEM;
6887
6888 skb = alloc_skb(0, GFP_KERNEL);
6889 if (!skb) {
6890 kfree(fpl);
6891 return -ENOMEM;
6892 }
6893
6894 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006895
Jens Axboe08a45172019-10-03 08:11:03 -06006896 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006897 fpl->user = get_uid(ctx->user);
6898 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006899 struct file *file = io_file_from_index(ctx, i + offset);
6900
6901 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006902 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006903 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006904 unix_inflight(fpl->user, fpl->fp[nr_files]);
6905 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006906 }
6907
Jens Axboe08a45172019-10-03 08:11:03 -06006908 if (nr_files) {
6909 fpl->max = SCM_MAX_FD;
6910 fpl->count = nr_files;
6911 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006912 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006913 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6914 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006915
Jens Axboe08a45172019-10-03 08:11:03 -06006916 for (i = 0; i < nr_files; i++)
6917 fput(fpl->fp[i]);
6918 } else {
6919 kfree_skb(skb);
6920 kfree(fpl);
6921 }
Jens Axboe6b063142019-01-10 22:13:58 -07006922
6923 return 0;
6924}
6925
6926/*
6927 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6928 * causes regular reference counting to break down. We rely on the UNIX
6929 * garbage collection to take care of this problem for us.
6930 */
6931static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6932{
6933 unsigned left, total;
6934 int ret = 0;
6935
6936 total = 0;
6937 left = ctx->nr_user_files;
6938 while (left) {
6939 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006940
6941 ret = __io_sqe_files_scm(ctx, this_files, total);
6942 if (ret)
6943 break;
6944 left -= this_files;
6945 total += this_files;
6946 }
6947
6948 if (!ret)
6949 return 0;
6950
6951 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006952 struct file *file = io_file_from_index(ctx, total);
6953
6954 if (file)
6955 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006956 total++;
6957 }
6958
6959 return ret;
6960}
6961#else
6962static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6963{
6964 return 0;
6965}
6966#endif
6967
Jens Axboe65e19f52019-10-26 07:20:21 -06006968static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6969 unsigned nr_files)
6970{
6971 int i;
6972
6973 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006974 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006975 unsigned this_files;
6976
6977 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6978 table->files = kcalloc(this_files, sizeof(struct file *),
6979 GFP_KERNEL);
6980 if (!table->files)
6981 break;
6982 nr_files -= this_files;
6983 }
6984
6985 if (i == nr_tables)
6986 return 0;
6987
6988 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006989 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006990 kfree(table->files);
6991 }
6992 return 1;
6993}
6994
Jens Axboe05f3fb32019-12-09 11:22:50 -07006995static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006996{
6997#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006998 struct sock *sock = ctx->ring_sock->sk;
6999 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7000 struct sk_buff *skb;
7001 int i;
7002
7003 __skb_queue_head_init(&list);
7004
7005 /*
7006 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7007 * remove this entry and rearrange the file array.
7008 */
7009 skb = skb_dequeue(head);
7010 while (skb) {
7011 struct scm_fp_list *fp;
7012
7013 fp = UNIXCB(skb).fp;
7014 for (i = 0; i < fp->count; i++) {
7015 int left;
7016
7017 if (fp->fp[i] != file)
7018 continue;
7019
7020 unix_notinflight(fp->user, fp->fp[i]);
7021 left = fp->count - 1 - i;
7022 if (left) {
7023 memmove(&fp->fp[i], &fp->fp[i + 1],
7024 left * sizeof(struct file *));
7025 }
7026 fp->count--;
7027 if (!fp->count) {
7028 kfree_skb(skb);
7029 skb = NULL;
7030 } else {
7031 __skb_queue_tail(&list, skb);
7032 }
7033 fput(file);
7034 file = NULL;
7035 break;
7036 }
7037
7038 if (!file)
7039 break;
7040
7041 __skb_queue_tail(&list, skb);
7042
7043 skb = skb_dequeue(head);
7044 }
7045
7046 if (skb_peek(&list)) {
7047 spin_lock_irq(&head->lock);
7048 while ((skb = __skb_dequeue(&list)) != NULL)
7049 __skb_queue_tail(head, skb);
7050 spin_unlock_irq(&head->lock);
7051 }
7052#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007053 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007054#endif
7055}
7056
Jens Axboe05f3fb32019-12-09 11:22:50 -07007057struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007058 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007059 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007060};
7061
Jens Axboe4a38aed22020-05-14 17:21:15 -06007062static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007063{
Jens Axboe4a38aed22020-05-14 17:21:15 -06007064 struct fixed_file_data *file_data = ref_node->file_data;
7065 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007066 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007067
7068 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007069 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007070 io_ring_file_put(ctx, pfile->file);
7071 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007072 }
7073
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007074 spin_lock(&file_data->lock);
7075 list_del(&ref_node->node);
7076 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07007077
Xiaoguang Wang05589552020-03-31 14:05:18 +08007078 percpu_ref_exit(&ref_node->refs);
7079 kfree(ref_node);
7080 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007081}
7082
Jens Axboe4a38aed22020-05-14 17:21:15 -06007083static void io_file_put_work(struct work_struct *work)
7084{
7085 struct io_ring_ctx *ctx;
7086 struct llist_node *node;
7087
7088 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
7089 node = llist_del_all(&ctx->file_put_llist);
7090
7091 while (node) {
7092 struct fixed_file_ref_node *ref_node;
7093 struct llist_node *next = node->next;
7094
7095 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
7096 __io_file_put_work(ref_node);
7097 node = next;
7098 }
7099}
7100
Jens Axboe05f3fb32019-12-09 11:22:50 -07007101static void io_file_data_ref_zero(struct percpu_ref *ref)
7102{
Xiaoguang Wang05589552020-03-31 14:05:18 +08007103 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007104 struct io_ring_ctx *ctx;
7105 bool first_add;
7106 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007107
Xiaoguang Wang05589552020-03-31 14:05:18 +08007108 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007109 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007110
Jens Axboe4a38aed22020-05-14 17:21:15 -06007111 if (percpu_ref_is_dying(&ctx->file_data->refs))
7112 delay = 0;
7113
7114 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
7115 if (!delay)
7116 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
7117 else if (first_add)
7118 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007119}
7120
7121static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
7122 struct io_ring_ctx *ctx)
7123{
7124 struct fixed_file_ref_node *ref_node;
7125
7126 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7127 if (!ref_node)
7128 return ERR_PTR(-ENOMEM);
7129
7130 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
7131 0, GFP_KERNEL)) {
7132 kfree(ref_node);
7133 return ERR_PTR(-ENOMEM);
7134 }
7135 INIT_LIST_HEAD(&ref_node->node);
7136 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007137 ref_node->file_data = ctx->file_data;
7138 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007139}
7140
7141static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
7142{
7143 percpu_ref_exit(&ref_node->refs);
7144 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007145}
7146
7147static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7148 unsigned nr_args)
7149{
7150 __s32 __user *fds = (__s32 __user *) arg;
7151 unsigned nr_tables;
7152 struct file *file;
7153 int fd, ret = 0;
7154 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007155 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007156
7157 if (ctx->file_data)
7158 return -EBUSY;
7159 if (!nr_args)
7160 return -EINVAL;
7161 if (nr_args > IORING_MAX_FIXED_FILES)
7162 return -EMFILE;
7163
7164 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
7165 if (!ctx->file_data)
7166 return -ENOMEM;
7167 ctx->file_data->ctx = ctx;
7168 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007169 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08007170 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007171
7172 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
7173 ctx->file_data->table = kcalloc(nr_tables,
7174 sizeof(struct fixed_file_table),
7175 GFP_KERNEL);
7176 if (!ctx->file_data->table) {
7177 kfree(ctx->file_data);
7178 ctx->file_data = NULL;
7179 return -ENOMEM;
7180 }
7181
Xiaoguang Wang05589552020-03-31 14:05:18 +08007182 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007183 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7184 kfree(ctx->file_data->table);
7185 kfree(ctx->file_data);
7186 ctx->file_data = NULL;
7187 return -ENOMEM;
7188 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007189
7190 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
7191 percpu_ref_exit(&ctx->file_data->refs);
7192 kfree(ctx->file_data->table);
7193 kfree(ctx->file_data);
7194 ctx->file_data = NULL;
7195 return -ENOMEM;
7196 }
7197
7198 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
7199 struct fixed_file_table *table;
7200 unsigned index;
7201
7202 ret = -EFAULT;
7203 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
7204 break;
7205 /* allow sparse sets */
7206 if (fd == -1) {
7207 ret = 0;
7208 continue;
7209 }
7210
7211 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7212 index = i & IORING_FILE_TABLE_MASK;
7213 file = fget(fd);
7214
7215 ret = -EBADF;
7216 if (!file)
7217 break;
7218
7219 /*
7220 * Don't allow io_uring instances to be registered. If UNIX
7221 * isn't enabled, then this causes a reference cycle and this
7222 * instance can never get freed. If UNIX is enabled we'll
7223 * handle it just fine, but there's still no point in allowing
7224 * a ring fd as it doesn't support regular read/write anyway.
7225 */
7226 if (file->f_op == &io_uring_fops) {
7227 fput(file);
7228 break;
7229 }
7230 ret = 0;
7231 table->files[index] = file;
7232 }
7233
7234 if (ret) {
7235 for (i = 0; i < ctx->nr_user_files; i++) {
7236 file = io_file_from_index(ctx, i);
7237 if (file)
7238 fput(file);
7239 }
7240 for (i = 0; i < nr_tables; i++)
7241 kfree(ctx->file_data->table[i].files);
7242
Yang Yingliang667e57d2020-07-10 14:14:20 +00007243 percpu_ref_exit(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007244 kfree(ctx->file_data->table);
7245 kfree(ctx->file_data);
7246 ctx->file_data = NULL;
7247 ctx->nr_user_files = 0;
7248 return ret;
7249 }
7250
7251 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007252 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007253 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007254 return ret;
7255 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007256
Xiaoguang Wang05589552020-03-31 14:05:18 +08007257 ref_node = alloc_fixed_file_ref_node(ctx);
7258 if (IS_ERR(ref_node)) {
7259 io_sqe_files_unregister(ctx);
7260 return PTR_ERR(ref_node);
7261 }
7262
7263 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007264 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007265 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007266 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007267 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007268 return ret;
7269}
7270
Jens Axboec3a31e62019-10-03 13:59:56 -06007271static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7272 int index)
7273{
7274#if defined(CONFIG_UNIX)
7275 struct sock *sock = ctx->ring_sock->sk;
7276 struct sk_buff_head *head = &sock->sk_receive_queue;
7277 struct sk_buff *skb;
7278
7279 /*
7280 * See if we can merge this file into an existing skb SCM_RIGHTS
7281 * file set. If there's no room, fall back to allocating a new skb
7282 * and filling it in.
7283 */
7284 spin_lock_irq(&head->lock);
7285 skb = skb_peek(head);
7286 if (skb) {
7287 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7288
7289 if (fpl->count < SCM_MAX_FD) {
7290 __skb_unlink(skb, head);
7291 spin_unlock_irq(&head->lock);
7292 fpl->fp[fpl->count] = get_file(file);
7293 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7294 fpl->count++;
7295 spin_lock_irq(&head->lock);
7296 __skb_queue_head(head, skb);
7297 } else {
7298 skb = NULL;
7299 }
7300 }
7301 spin_unlock_irq(&head->lock);
7302
7303 if (skb) {
7304 fput(file);
7305 return 0;
7306 }
7307
7308 return __io_sqe_files_scm(ctx, 1, index);
7309#else
7310 return 0;
7311#endif
7312}
7313
Hillf Dantona5318d32020-03-23 17:47:15 +08007314static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007315 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007316{
Hillf Dantona5318d32020-03-23 17:47:15 +08007317 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007318 struct percpu_ref *refs = data->cur_refs;
7319 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007320
Jens Axboe05f3fb32019-12-09 11:22:50 -07007321 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08007322 if (!pfile)
7323 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007324
Xiaoguang Wang05589552020-03-31 14:05:18 +08007325 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007326 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007327 list_add(&pfile->list, &ref_node->file_list);
7328
Hillf Dantona5318d32020-03-23 17:47:15 +08007329 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007330}
7331
7332static int __io_sqe_files_update(struct io_ring_ctx *ctx,
7333 struct io_uring_files_update *up,
7334 unsigned nr_args)
7335{
7336 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007337 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007338 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007339 __s32 __user *fds;
7340 int fd, i, err;
7341 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007342 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007343
Jens Axboe05f3fb32019-12-09 11:22:50 -07007344 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007345 return -EOVERFLOW;
7346 if (done > ctx->nr_user_files)
7347 return -EINVAL;
7348
Xiaoguang Wang05589552020-03-31 14:05:18 +08007349 ref_node = alloc_fixed_file_ref_node(ctx);
7350 if (IS_ERR(ref_node))
7351 return PTR_ERR(ref_node);
7352
Jens Axboec3a31e62019-10-03 13:59:56 -06007353 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007354 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06007355 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007356 struct fixed_file_table *table;
7357 unsigned index;
7358
Jens Axboec3a31e62019-10-03 13:59:56 -06007359 err = 0;
7360 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7361 err = -EFAULT;
7362 break;
7363 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007364 i = array_index_nospec(up->offset, ctx->nr_user_files);
7365 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06007366 index = i & IORING_FILE_TABLE_MASK;
7367 if (table->files[index]) {
Jiufei Xue98dfd502020-09-01 13:35:02 +08007368 file = table->files[index];
Hillf Dantona5318d32020-03-23 17:47:15 +08007369 err = io_queue_file_removal(data, file);
7370 if (err)
7371 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06007372 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007373 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007374 }
7375 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007376 file = fget(fd);
7377 if (!file) {
7378 err = -EBADF;
7379 break;
7380 }
7381 /*
7382 * Don't allow io_uring instances to be registered. If
7383 * UNIX isn't enabled, then this causes a reference
7384 * cycle and this instance can never get freed. If UNIX
7385 * is enabled we'll handle it just fine, but there's
7386 * still no point in allowing a ring fd as it doesn't
7387 * support regular read/write anyway.
7388 */
7389 if (file->f_op == &io_uring_fops) {
7390 fput(file);
7391 err = -EBADF;
7392 break;
7393 }
Jens Axboe65e19f52019-10-26 07:20:21 -06007394 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007395 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007396 if (err) {
Jiufei Xue95d1c8e2020-09-02 17:59:39 +08007397 table->files[index] = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007398 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007399 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007400 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007401 }
7402 nr_args--;
7403 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007404 up->offset++;
7405 }
7406
Xiaoguang Wang05589552020-03-31 14:05:18 +08007407 if (needs_switch) {
7408 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007409 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007410 list_add(&ref_node->node, &data->ref_list);
7411 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06007412 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007413 percpu_ref_get(&ctx->file_data->refs);
7414 } else
7415 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007416
7417 return done ? done : err;
7418}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007419
Jens Axboe05f3fb32019-12-09 11:22:50 -07007420static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7421 unsigned nr_args)
7422{
7423 struct io_uring_files_update up;
7424
7425 if (!ctx->file_data)
7426 return -ENXIO;
7427 if (!nr_args)
7428 return -EINVAL;
7429 if (copy_from_user(&up, arg, sizeof(up)))
7430 return -EFAULT;
7431 if (up.resv)
7432 return -EINVAL;
7433
7434 return __io_sqe_files_update(ctx, &up, nr_args);
7435}
Jens Axboec3a31e62019-10-03 13:59:56 -06007436
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007437static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007438{
7439 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7440
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007441 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07007442 io_put_req(req);
7443}
7444
Pavel Begunkov24369c22020-01-28 03:15:48 +03007445static int io_init_wq_offload(struct io_ring_ctx *ctx,
7446 struct io_uring_params *p)
7447{
7448 struct io_wq_data data;
7449 struct fd f;
7450 struct io_ring_ctx *ctx_attach;
7451 unsigned int concurrency;
7452 int ret = 0;
7453
7454 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007455 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007456 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007457
7458 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
7459 /* Do QD, or 4 * CPUS, whatever is smallest */
7460 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7461
7462 ctx->io_wq = io_wq_create(concurrency, &data);
7463 if (IS_ERR(ctx->io_wq)) {
7464 ret = PTR_ERR(ctx->io_wq);
7465 ctx->io_wq = NULL;
7466 }
7467 return ret;
7468 }
7469
7470 f = fdget(p->wq_fd);
7471 if (!f.file)
7472 return -EBADF;
7473
7474 if (f.file->f_op != &io_uring_fops) {
7475 ret = -EINVAL;
7476 goto out_fput;
7477 }
7478
7479 ctx_attach = f.file->private_data;
7480 /* @io_wq is protected by holding the fd */
7481 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7482 ret = -EINVAL;
7483 goto out_fput;
7484 }
7485
7486 ctx->io_wq = ctx_attach->io_wq;
7487out_fput:
7488 fdput(f);
7489 return ret;
7490}
7491
Jens Axboe6c271ce2019-01-10 11:22:30 -07007492static int io_sq_offload_start(struct io_ring_ctx *ctx,
7493 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007494{
7495 int ret;
7496
Jens Axboe6c271ce2019-01-10 11:22:30 -07007497 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007498 ret = -EPERM;
7499 if (!capable(CAP_SYS_ADMIN))
7500 goto err;
7501
Jens Axboe917257d2019-04-13 09:28:55 -06007502 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7503 if (!ctx->sq_thread_idle)
7504 ctx->sq_thread_idle = HZ;
7505
Jens Axboe6c271ce2019-01-10 11:22:30 -07007506 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007507 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007508
Jens Axboe917257d2019-04-13 09:28:55 -06007509 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007510 if (cpu >= nr_cpu_ids)
7511 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007512 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007513 goto err;
7514
Jens Axboe6c271ce2019-01-10 11:22:30 -07007515 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7516 ctx, cpu,
7517 "io_uring-sq");
7518 } else {
7519 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7520 "io_uring-sq");
7521 }
7522 if (IS_ERR(ctx->sqo_thread)) {
7523 ret = PTR_ERR(ctx->sqo_thread);
7524 ctx->sqo_thread = NULL;
7525 goto err;
7526 }
7527 wake_up_process(ctx->sqo_thread);
7528 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7529 /* Can't have SQ_AFF without SQPOLL */
7530 ret = -EINVAL;
7531 goto err;
7532 }
7533
Pavel Begunkov24369c22020-01-28 03:15:48 +03007534 ret = io_init_wq_offload(ctx, p);
7535 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007536 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007537
7538 return 0;
7539err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007540 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007541 return ret;
7542}
7543
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007544static inline void __io_unaccount_mem(struct user_struct *user,
7545 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007546{
7547 atomic_long_sub(nr_pages, &user->locked_vm);
7548}
7549
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007550static inline int __io_account_mem(struct user_struct *user,
7551 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007552{
7553 unsigned long page_limit, cur_pages, new_pages;
7554
7555 /* Don't allow more pages than we can safely lock */
7556 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7557
7558 do {
7559 cur_pages = atomic_long_read(&user->locked_vm);
7560 new_pages = cur_pages + nr_pages;
7561 if (new_pages > page_limit)
7562 return -ENOMEM;
7563 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7564 new_pages) != cur_pages);
7565
7566 return 0;
7567}
7568
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007569static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7570 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007571{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007572 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007573 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007574
Jens Axboe2aede0e2020-09-14 10:45:53 -06007575 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007576 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007577 ctx->mm_account->locked_vm -= nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007578 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007579 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007580 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007581}
7582
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007583static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7584 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007585{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007586 int ret;
7587
7588 if (ctx->limit_mem) {
7589 ret = __io_account_mem(ctx->user, nr_pages);
7590 if (ret)
7591 return ret;
7592 }
7593
Jens Axboe2aede0e2020-09-14 10:45:53 -06007594 if (ctx->mm_account) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007595 if (acct == ACCT_LOCKED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007596 ctx->mm_account->locked_vm += nr_pages;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007597 else if (acct == ACCT_PINNED)
Jens Axboe2aede0e2020-09-14 10:45:53 -06007598 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007599 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007600
7601 return 0;
7602}
7603
Jens Axboe2b188cc2019-01-07 10:46:33 -07007604static void io_mem_free(void *ptr)
7605{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007606 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007607
Mark Rutland52e04ef2019-04-30 17:30:21 +01007608 if (!ptr)
7609 return;
7610
7611 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007612 if (put_page_testzero(page))
7613 free_compound_page(page);
7614}
7615
7616static void *io_mem_alloc(size_t size)
7617{
7618 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7619 __GFP_NORETRY;
7620
7621 return (void *) __get_free_pages(gfp_flags, get_order(size));
7622}
7623
Hristo Venev75b28af2019-08-26 17:23:46 +00007624static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7625 size_t *sq_offset)
7626{
7627 struct io_rings *rings;
7628 size_t off, sq_array_size;
7629
7630 off = struct_size(rings, cqes, cq_entries);
7631 if (off == SIZE_MAX)
7632 return SIZE_MAX;
7633
7634#ifdef CONFIG_SMP
7635 off = ALIGN(off, SMP_CACHE_BYTES);
7636 if (off == 0)
7637 return SIZE_MAX;
7638#endif
7639
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02007640 if (sq_offset)
7641 *sq_offset = off;
7642
Hristo Venev75b28af2019-08-26 17:23:46 +00007643 sq_array_size = array_size(sizeof(u32), sq_entries);
7644 if (sq_array_size == SIZE_MAX)
7645 return SIZE_MAX;
7646
7647 if (check_add_overflow(off, sq_array_size, &off))
7648 return SIZE_MAX;
7649
Hristo Venev75b28af2019-08-26 17:23:46 +00007650 return off;
7651}
7652
Jens Axboe2b188cc2019-01-07 10:46:33 -07007653static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7654{
Hristo Venev75b28af2019-08-26 17:23:46 +00007655 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007656
Hristo Venev75b28af2019-08-26 17:23:46 +00007657 pages = (size_t)1 << get_order(
7658 rings_size(sq_entries, cq_entries, NULL));
7659 pages += (size_t)1 << get_order(
7660 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007661
Hristo Venev75b28af2019-08-26 17:23:46 +00007662 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007663}
7664
Jens Axboeedafcce2019-01-09 09:16:05 -07007665static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7666{
7667 int i, j;
7668
7669 if (!ctx->user_bufs)
7670 return -ENXIO;
7671
7672 for (i = 0; i < ctx->nr_user_bufs; i++) {
7673 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7674
7675 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007676 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007677
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007678 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007679 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007680 imu->nr_bvecs = 0;
7681 }
7682
7683 kfree(ctx->user_bufs);
7684 ctx->user_bufs = NULL;
7685 ctx->nr_user_bufs = 0;
7686 return 0;
7687}
7688
7689static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7690 void __user *arg, unsigned index)
7691{
7692 struct iovec __user *src;
7693
7694#ifdef CONFIG_COMPAT
7695 if (ctx->compat) {
7696 struct compat_iovec __user *ciovs;
7697 struct compat_iovec ciov;
7698
7699 ciovs = (struct compat_iovec __user *) arg;
7700 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7701 return -EFAULT;
7702
Jens Axboed55e5f52019-12-11 16:12:15 -07007703 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007704 dst->iov_len = ciov.iov_len;
7705 return 0;
7706 }
7707#endif
7708 src = (struct iovec __user *) arg;
7709 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7710 return -EFAULT;
7711 return 0;
7712}
7713
7714static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7715 unsigned nr_args)
7716{
7717 struct vm_area_struct **vmas = NULL;
7718 struct page **pages = NULL;
7719 int i, j, got_pages = 0;
7720 int ret = -EINVAL;
7721
7722 if (ctx->user_bufs)
7723 return -EBUSY;
7724 if (!nr_args || nr_args > UIO_MAXIOV)
7725 return -EINVAL;
7726
7727 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7728 GFP_KERNEL);
7729 if (!ctx->user_bufs)
7730 return -ENOMEM;
7731
7732 for (i = 0; i < nr_args; i++) {
7733 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7734 unsigned long off, start, end, ubuf;
7735 int pret, nr_pages;
7736 struct iovec iov;
7737 size_t size;
7738
7739 ret = io_copy_iov(ctx, &iov, arg, i);
7740 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007741 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007742
7743 /*
7744 * Don't impose further limits on the size and buffer
7745 * constraints here, we'll -EINVAL later when IO is
7746 * submitted if they are wrong.
7747 */
7748 ret = -EFAULT;
7749 if (!iov.iov_base || !iov.iov_len)
7750 goto err;
7751
7752 /* arbitrary limit, but we need something */
7753 if (iov.iov_len > SZ_1G)
7754 goto err;
7755
7756 ubuf = (unsigned long) iov.iov_base;
7757 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7758 start = ubuf >> PAGE_SHIFT;
7759 nr_pages = end - start;
7760
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007761 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007762 if (ret)
7763 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007764
7765 ret = 0;
7766 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007767 kvfree(vmas);
7768 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007769 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007770 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007771 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007772 sizeof(struct vm_area_struct *),
7773 GFP_KERNEL);
7774 if (!pages || !vmas) {
7775 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007776 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007777 goto err;
7778 }
7779 got_pages = nr_pages;
7780 }
7781
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007782 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007783 GFP_KERNEL);
7784 ret = -ENOMEM;
7785 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007786 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007787 goto err;
7788 }
7789
7790 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007791 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007792 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007793 FOLL_WRITE | FOLL_LONGTERM,
7794 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007795 if (pret == nr_pages) {
7796 /* don't support file backed memory */
7797 for (j = 0; j < nr_pages; j++) {
7798 struct vm_area_struct *vma = vmas[j];
7799
7800 if (vma->vm_file &&
7801 !is_file_hugepages(vma->vm_file)) {
7802 ret = -EOPNOTSUPP;
7803 break;
7804 }
7805 }
7806 } else {
7807 ret = pret < 0 ? pret : -EFAULT;
7808 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007809 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007810 if (ret) {
7811 /*
7812 * if we did partial map, or found file backed vmas,
7813 * release any pages we did get
7814 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007815 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007816 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007817 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007818 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007819 goto err;
7820 }
7821
7822 off = ubuf & ~PAGE_MASK;
7823 size = iov.iov_len;
7824 for (j = 0; j < nr_pages; j++) {
7825 size_t vec_len;
7826
7827 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7828 imu->bvec[j].bv_page = pages[j];
7829 imu->bvec[j].bv_len = vec_len;
7830 imu->bvec[j].bv_offset = off;
7831 off = 0;
7832 size -= vec_len;
7833 }
7834 /* store original address for later verification */
7835 imu->ubuf = ubuf;
7836 imu->len = iov.iov_len;
7837 imu->nr_bvecs = nr_pages;
7838
7839 ctx->nr_user_bufs++;
7840 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007841 kvfree(pages);
7842 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007843 return 0;
7844err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007845 kvfree(pages);
7846 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007847 io_sqe_buffer_unregister(ctx);
7848 return ret;
7849}
7850
Jens Axboe9b402842019-04-11 11:45:41 -06007851static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7852{
7853 __s32 __user *fds = arg;
7854 int fd;
7855
7856 if (ctx->cq_ev_fd)
7857 return -EBUSY;
7858
7859 if (copy_from_user(&fd, fds, sizeof(*fds)))
7860 return -EFAULT;
7861
7862 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7863 if (IS_ERR(ctx->cq_ev_fd)) {
7864 int ret = PTR_ERR(ctx->cq_ev_fd);
7865 ctx->cq_ev_fd = NULL;
7866 return ret;
7867 }
7868
7869 return 0;
7870}
7871
7872static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7873{
7874 if (ctx->cq_ev_fd) {
7875 eventfd_ctx_put(ctx->cq_ev_fd);
7876 ctx->cq_ev_fd = NULL;
7877 return 0;
7878 }
7879
7880 return -ENXIO;
7881}
7882
Jens Axboe5a2e7452020-02-23 16:23:11 -07007883static int __io_destroy_buffers(int id, void *p, void *data)
7884{
7885 struct io_ring_ctx *ctx = data;
7886 struct io_buffer *buf = p;
7887
Jens Axboe067524e2020-03-02 16:32:28 -07007888 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007889 return 0;
7890}
7891
7892static void io_destroy_buffers(struct io_ring_ctx *ctx)
7893{
7894 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7895 idr_destroy(&ctx->io_buffer_idr);
7896}
7897
Jens Axboe2b188cc2019-01-07 10:46:33 -07007898static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7899{
Jens Axboe6b063142019-01-10 22:13:58 -07007900 io_finish_async(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007901 io_sqe_buffer_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06007902
7903 if (ctx->sqo_task) {
7904 put_task_struct(ctx->sqo_task);
7905 ctx->sqo_task = NULL;
7906 mmdrop(ctx->mm_account);
7907 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007908 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007909
Jens Axboe6b063142019-01-10 22:13:58 -07007910 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007911 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007912 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007913 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007914
Jens Axboe2b188cc2019-01-07 10:46:33 -07007915#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007916 if (ctx->ring_sock) {
7917 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007918 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007919 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007920#endif
7921
Hristo Venev75b28af2019-08-26 17:23:46 +00007922 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007923 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007924
7925 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007926 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007927 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007928 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007929 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007930 kfree(ctx);
7931}
7932
7933static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7934{
7935 struct io_ring_ctx *ctx = file->private_data;
7936 __poll_t mask = 0;
7937
7938 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007939 /*
7940 * synchronizes with barrier from wq_has_sleeper call in
7941 * io_commit_cqring
7942 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007943 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007944 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7945 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007946 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007947 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007948 mask |= EPOLLIN | EPOLLRDNORM;
7949
7950 return mask;
7951}
7952
7953static int io_uring_fasync(int fd, struct file *file, int on)
7954{
7955 struct io_ring_ctx *ctx = file->private_data;
7956
7957 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7958}
7959
Jens Axboe071698e2020-01-28 10:04:42 -07007960static int io_remove_personalities(int id, void *p, void *data)
7961{
7962 struct io_ring_ctx *ctx = data;
7963 const struct cred *cred;
7964
7965 cred = idr_remove(&ctx->personality_idr, id);
7966 if (cred)
7967 put_cred(cred);
7968 return 0;
7969}
7970
Jens Axboe85faa7b2020-04-09 18:14:00 -06007971static void io_ring_exit_work(struct work_struct *work)
7972{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007973 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
7974 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007975
Jens Axboe56952e92020-06-17 15:00:04 -06007976 /*
7977 * If we're doing polled IO and end up having requests being
7978 * submitted async (out-of-line), then completions can come in while
7979 * we're waiting for refs to drop. We need to reap these manually,
7980 * as nobody else will be looking for them.
7981 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007982 do {
Jens Axboe56952e92020-06-17 15:00:04 -06007983 if (ctx->rings)
7984 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03007985 io_iopoll_try_reap_events(ctx);
7986 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06007987 io_ring_ctx_free(ctx);
7988}
7989
Jens Axboe2b188cc2019-01-07 10:46:33 -07007990static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7991{
7992 mutex_lock(&ctx->uring_lock);
7993 percpu_ref_kill(&ctx->refs);
7994 mutex_unlock(&ctx->uring_lock);
7995
Jens Axboef3606e32020-09-22 08:18:24 -06007996 io_kill_timeouts(ctx, NULL);
7997 io_poll_remove_all(ctx, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06007998
7999 if (ctx->io_wq)
8000 io_wq_cancel_all(ctx->io_wq);
8001
Jens Axboe15dff282019-11-13 09:09:23 -07008002 /* if we failed setting up the ctx, we might not have any rings */
8003 if (ctx->rings)
8004 io_cqring_overflow_flush(ctx, true);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008005 io_iopoll_try_reap_events(ctx);
Jens Axboe071698e2020-01-28 10:04:42 -07008006 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008007
8008 /*
8009 * Do this upfront, so we won't have a grace period where the ring
8010 * is closed but resources aren't reaped yet. This can cause
8011 * spurious failure in setting up a new ring.
8012 */
Jens Axboe760618f2020-07-24 12:53:31 -06008013 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
8014 ACCT_LOCKED);
Jens Axboe309fc032020-07-10 09:13:34 -06008015
Jens Axboe85faa7b2020-04-09 18:14:00 -06008016 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008017 /*
8018 * Use system_unbound_wq to avoid spawning tons of event kworkers
8019 * if we're exiting a ton of rings at the same time. It just adds
8020 * noise and overhead, there's no discernable change in runtime
8021 * over using system_wq.
8022 */
8023 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008024}
8025
8026static int io_uring_release(struct inode *inode, struct file *file)
8027{
8028 struct io_ring_ctx *ctx = file->private_data;
8029
8030 file->private_data = NULL;
8031 io_ring_ctx_wait_and_kill(ctx);
8032 return 0;
8033}
8034
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008035static bool io_wq_files_match(struct io_wq_work *work, void *data)
8036{
8037 struct files_struct *files = data;
8038
8039 return work->files == files;
8040}
8041
Jens Axboef254ac02020-08-12 17:33:30 -06008042/*
8043 * Returns true if 'preq' is the link parent of 'req'
8044 */
8045static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
8046{
8047 struct io_kiocb *link;
8048
8049 if (!(preq->flags & REQ_F_LINK_HEAD))
8050 return false;
8051
8052 list_for_each_entry(link, &preq->link_list, link_list) {
8053 if (link == req)
8054 return true;
8055 }
8056
8057 return false;
8058}
8059
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008060static inline bool io_match_files(struct io_kiocb *req,
8061 struct files_struct *files)
8062{
8063 return (req->flags & REQ_F_WORK_INITIALIZED) && req->work.files == files;
8064}
8065
8066static bool io_match_link_files(struct io_kiocb *req,
8067 struct files_struct *files)
8068{
8069 struct io_kiocb *link;
8070
8071 if (io_match_files(req, files))
8072 return true;
8073 if (req->flags & REQ_F_LINK_HEAD) {
8074 list_for_each_entry(link, &req->link_list, link_list) {
8075 if (io_match_files(link, files))
8076 return true;
8077 }
8078 }
8079 return false;
8080}
8081
Jens Axboef254ac02020-08-12 17:33:30 -06008082/*
8083 * We're looking to cancel 'req' because it's holding on to our files, but
8084 * 'req' could be a link to another request. See if it is, and cancel that
8085 * parent request if so.
8086 */
8087static bool io_poll_remove_link(struct io_ring_ctx *ctx, struct io_kiocb *req)
8088{
8089 struct hlist_node *tmp;
8090 struct io_kiocb *preq;
8091 bool found = false;
8092 int i;
8093
8094 spin_lock_irq(&ctx->completion_lock);
8095 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8096 struct hlist_head *list;
8097
8098 list = &ctx->cancel_hash[i];
8099 hlist_for_each_entry_safe(preq, tmp, list, hash_node) {
8100 found = io_match_link(preq, req);
8101 if (found) {
8102 io_poll_remove_one(preq);
8103 break;
8104 }
8105 }
8106 }
8107 spin_unlock_irq(&ctx->completion_lock);
8108 return found;
8109}
8110
8111static bool io_timeout_remove_link(struct io_ring_ctx *ctx,
8112 struct io_kiocb *req)
8113{
8114 struct io_kiocb *preq;
8115 bool found = false;
8116
8117 spin_lock_irq(&ctx->completion_lock);
8118 list_for_each_entry(preq, &ctx->timeout_list, timeout.list) {
8119 found = io_match_link(preq, req);
8120 if (found) {
8121 __io_timeout_cancel(preq);
8122 break;
8123 }
8124 }
8125 spin_unlock_irq(&ctx->completion_lock);
8126 return found;
8127}
8128
Jens Axboeb711d4e2020-08-16 08:23:05 -07008129static bool io_cancel_link_cb(struct io_wq_work *work, void *data)
8130{
8131 return io_match_link(container_of(work, struct io_kiocb, work), data);
8132}
8133
8134static void io_attempt_cancel(struct io_ring_ctx *ctx, struct io_kiocb *req)
8135{
8136 enum io_wq_cancel cret;
8137
8138 /* cancel this particular work, if it's running */
8139 cret = io_wq_cancel_work(ctx->io_wq, &req->work);
8140 if (cret != IO_WQ_CANCEL_NOTFOUND)
8141 return;
8142
8143 /* find links that hold this pending, cancel those */
8144 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_link_cb, req, true);
8145 if (cret != IO_WQ_CANCEL_NOTFOUND)
8146 return;
8147
8148 /* if we have a poll link holding this pending, cancel that */
8149 if (io_poll_remove_link(ctx, req))
8150 return;
8151
8152 /* final option, timeout link is holding this req pending */
8153 io_timeout_remove_link(ctx, req);
8154}
8155
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008156static void io_cancel_defer_files(struct io_ring_ctx *ctx,
8157 struct files_struct *files)
8158{
8159 struct io_defer_entry *de = NULL;
8160 LIST_HEAD(list);
8161
8162 spin_lock_irq(&ctx->completion_lock);
8163 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkovc127a2a2020-09-06 00:45:15 +03008164 if (io_match_link_files(de->req, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008165 list_cut_position(&list, &ctx->defer_list, &de->list);
8166 break;
8167 }
8168 }
8169 spin_unlock_irq(&ctx->completion_lock);
8170
8171 while (!list_empty(&list)) {
8172 de = list_first_entry(&list, struct io_defer_entry, list);
8173 list_del_init(&de->list);
8174 req_set_fail_links(de->req);
8175 io_put_req(de->req);
8176 io_req_complete(de->req, -ECANCELED);
8177 kfree(de);
8178 }
8179}
8180
Jens Axboefcb323c2019-10-24 12:39:47 -06008181static void io_uring_cancel_files(struct io_ring_ctx *ctx,
8182 struct files_struct *files)
8183{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008184 if (list_empty_careful(&ctx->inflight_list))
8185 return;
8186
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008187 io_cancel_defer_files(ctx, files);
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008188 /* cancel all at once, should be faster than doing it one by one*/
8189 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
8190
Jens Axboefcb323c2019-10-24 12:39:47 -06008191 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008192 struct io_kiocb *cancel_req = NULL, *req;
8193 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008194
8195 spin_lock_irq(&ctx->inflight_lock);
8196 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07008197 if (req->work.files != files)
8198 continue;
8199 /* req is being completed, ignore */
8200 if (!refcount_inc_not_zero(&req->refs))
8201 continue;
8202 cancel_req = req;
8203 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06008204 }
Jens Axboe768134d2019-11-10 20:30:53 -07008205 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008206 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07008207 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06008208 spin_unlock_irq(&ctx->inflight_lock);
8209
Jens Axboe768134d2019-11-10 20:30:53 -07008210 /* We need to keep going until we don't find a matching req */
8211 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06008212 break;
Pavel Begunkovbb175342020-08-20 11:33:35 +03008213 /* cancel this request, or head link requests */
8214 io_attempt_cancel(ctx, cancel_req);
8215 io_put_req(cancel_req);
Jens Axboe6200b0a2020-09-13 14:38:30 -06008216 /* cancellations _may_ trigger task work */
8217 io_run_task_work();
Jens Axboefcb323c2019-10-24 12:39:47 -06008218 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008219 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06008220 }
8221}
8222
Pavel Begunkov801dd572020-06-15 10:33:14 +03008223static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008224{
Pavel Begunkov801dd572020-06-15 10:33:14 +03008225 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8226 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008227
Jens Axboef3606e32020-09-22 08:18:24 -06008228 return io_task_match(req, task);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008229}
8230
Jens Axboefcb323c2019-10-24 12:39:47 -06008231static int io_uring_flush(struct file *file, void *data)
8232{
8233 struct io_ring_ctx *ctx = file->private_data;
8234
8235 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07008236
8237 /*
8238 * If the task is going away, cancel work it may have pending
8239 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03008240 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
8241 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07008242
Jens Axboefcb323c2019-10-24 12:39:47 -06008243 return 0;
8244}
8245
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008246static void *io_uring_validate_mmap_request(struct file *file,
8247 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008248{
Jens Axboe2b188cc2019-01-07 10:46:33 -07008249 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008250 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008251 struct page *page;
8252 void *ptr;
8253
8254 switch (offset) {
8255 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00008256 case IORING_OFF_CQ_RING:
8257 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008258 break;
8259 case IORING_OFF_SQES:
8260 ptr = ctx->sq_sqes;
8261 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008262 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008263 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008264 }
8265
8266 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07008267 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008268 return ERR_PTR(-EINVAL);
8269
8270 return ptr;
8271}
8272
8273#ifdef CONFIG_MMU
8274
8275static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8276{
8277 size_t sz = vma->vm_end - vma->vm_start;
8278 unsigned long pfn;
8279 void *ptr;
8280
8281 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
8282 if (IS_ERR(ptr))
8283 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008284
8285 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
8286 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
8287}
8288
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008289#else /* !CONFIG_MMU */
8290
8291static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
8292{
8293 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
8294}
8295
8296static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
8297{
8298 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
8299}
8300
8301static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
8302 unsigned long addr, unsigned long len,
8303 unsigned long pgoff, unsigned long flags)
8304{
8305 void *ptr;
8306
8307 ptr = io_uring_validate_mmap_request(file, pgoff, len);
8308 if (IS_ERR(ptr))
8309 return PTR_ERR(ptr);
8310
8311 return (unsigned long) ptr;
8312}
8313
8314#endif /* !CONFIG_MMU */
8315
Jens Axboe2b188cc2019-01-07 10:46:33 -07008316SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
8317 u32, min_complete, u32, flags, const sigset_t __user *, sig,
8318 size_t, sigsz)
8319{
8320 struct io_ring_ctx *ctx;
8321 long ret = -EBADF;
8322 int submitted = 0;
8323 struct fd f;
8324
Jens Axboe4c6e2772020-07-01 11:29:10 -06008325 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07008326
Jens Axboe6c271ce2019-01-10 11:22:30 -07008327 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008328 return -EINVAL;
8329
8330 f = fdget(fd);
8331 if (!f.file)
8332 return -EBADF;
8333
8334 ret = -EOPNOTSUPP;
8335 if (f.file->f_op != &io_uring_fops)
8336 goto out_fput;
8337
8338 ret = -ENXIO;
8339 ctx = f.file->private_data;
8340 if (!percpu_ref_tryget(&ctx->refs))
8341 goto out_fput;
8342
Jens Axboe6c271ce2019-01-10 11:22:30 -07008343 /*
8344 * For SQ polling, the thread will do all submissions and completions.
8345 * Just return the requested submit count, and wake the thread if
8346 * we were asked to.
8347 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008348 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008349 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07008350 if (!list_empty_careful(&ctx->cq_overflow_list))
8351 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07008352 if (flags & IORING_ENTER_SQ_WAKEUP)
8353 wake_up(&ctx->sqo_wait);
8354 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06008355 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07008356 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03008357 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008358 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008359
8360 if (submitted != to_submit)
8361 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008362 }
8363 if (flags & IORING_ENTER_GETEVENTS) {
8364 min_complete = min(min_complete, ctx->cq_entries);
8365
Xiaoguang Wang32b22442020-03-11 09:26:09 +08008366 /*
8367 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
8368 * space applications don't need to do io completion events
8369 * polling again, they can rely on io_sq_thread to do polling
8370 * work, which can reduce cpu usage and uring_lock contention.
8371 */
8372 if (ctx->flags & IORING_SETUP_IOPOLL &&
8373 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03008374 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07008375 } else {
8376 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
8377 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008378 }
8379
Pavel Begunkov7c504e652019-12-18 19:53:45 +03008380out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03008381 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008382out_fput:
8383 fdput(f);
8384 return submitted ? submitted : ret;
8385}
8386
Tobias Klauserbebdb652020-02-26 18:38:32 +01008387#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008388static int io_uring_show_cred(int id, void *p, void *data)
8389{
8390 const struct cred *cred = p;
8391 struct seq_file *m = data;
8392 struct user_namespace *uns = seq_user_ns(m);
8393 struct group_info *gi;
8394 kernel_cap_t cap;
8395 unsigned __capi;
8396 int g;
8397
8398 seq_printf(m, "%5d\n", id);
8399 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
8400 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
8401 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
8402 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
8403 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
8404 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
8405 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
8406 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
8407 seq_puts(m, "\n\tGroups:\t");
8408 gi = cred->group_info;
8409 for (g = 0; g < gi->ngroups; g++) {
8410 seq_put_decimal_ull(m, g ? " " : "",
8411 from_kgid_munged(uns, gi->gid[g]));
8412 }
8413 seq_puts(m, "\n\tCapEff:\t");
8414 cap = cred->cap_effective;
8415 CAP_FOR_EACH_U32(__capi)
8416 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
8417 seq_putc(m, '\n');
8418 return 0;
8419}
8420
8421static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
8422{
Jens Axboefad8e0d2020-09-28 08:57:48 -06008423 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07008424 int i;
8425
Jens Axboefad8e0d2020-09-28 08:57:48 -06008426 /*
8427 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
8428 * since fdinfo case grabs it in the opposite direction of normal use
8429 * cases. If we fail to get the lock, we just don't iterate any
8430 * structures that could be going away outside the io_uring mutex.
8431 */
8432 has_lock = mutex_trylock(&ctx->uring_lock);
8433
Jens Axboe87ce9552020-01-30 08:25:34 -07008434 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008435 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008436 struct fixed_file_table *table;
8437 struct file *f;
8438
8439 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
8440 f = table->files[i & IORING_FILE_TABLE_MASK];
8441 if (f)
8442 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
8443 else
8444 seq_printf(m, "%5u: <none>\n", i);
8445 }
8446 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008447 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008448 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
8449
8450 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
8451 (unsigned int) buf->len);
8452 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06008453 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07008454 seq_printf(m, "Personalities:\n");
8455 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
8456 }
Jens Axboed7718a92020-02-14 22:23:12 -07008457 seq_printf(m, "PollList:\n");
8458 spin_lock_irq(&ctx->completion_lock);
8459 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
8460 struct hlist_head *list = &ctx->cancel_hash[i];
8461 struct io_kiocb *req;
8462
8463 hlist_for_each_entry(req, list, hash_node)
8464 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
8465 req->task->task_works != NULL);
8466 }
8467 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06008468 if (has_lock)
8469 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07008470}
8471
8472static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
8473{
8474 struct io_ring_ctx *ctx = f->private_data;
8475
8476 if (percpu_ref_tryget(&ctx->refs)) {
8477 __io_uring_show_fdinfo(ctx, m);
8478 percpu_ref_put(&ctx->refs);
8479 }
8480}
Tobias Klauserbebdb652020-02-26 18:38:32 +01008481#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07008482
Jens Axboe2b188cc2019-01-07 10:46:33 -07008483static const struct file_operations io_uring_fops = {
8484 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06008485 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008486 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01008487#ifndef CONFIG_MMU
8488 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
8489 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
8490#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008491 .poll = io_uring_poll,
8492 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008493#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07008494 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01008495#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07008496};
8497
8498static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
8499 struct io_uring_params *p)
8500{
Hristo Venev75b28af2019-08-26 17:23:46 +00008501 struct io_rings *rings;
8502 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008503
Jens Axboebd740482020-08-05 12:58:23 -06008504 /* make sure these are sane, as we already accounted them */
8505 ctx->sq_entries = p->sq_entries;
8506 ctx->cq_entries = p->cq_entries;
8507
Hristo Venev75b28af2019-08-26 17:23:46 +00008508 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
8509 if (size == SIZE_MAX)
8510 return -EOVERFLOW;
8511
8512 rings = io_mem_alloc(size);
8513 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008514 return -ENOMEM;
8515
Hristo Venev75b28af2019-08-26 17:23:46 +00008516 ctx->rings = rings;
8517 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
8518 rings->sq_ring_mask = p->sq_entries - 1;
8519 rings->cq_ring_mask = p->cq_entries - 1;
8520 rings->sq_ring_entries = p->sq_entries;
8521 rings->cq_ring_entries = p->cq_entries;
8522 ctx->sq_mask = rings->sq_ring_mask;
8523 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008524
8525 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07008526 if (size == SIZE_MAX) {
8527 io_mem_free(ctx->rings);
8528 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008529 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07008530 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008531
8532 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07008533 if (!ctx->sq_sqes) {
8534 io_mem_free(ctx->rings);
8535 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008536 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07008537 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008538
Jens Axboe2b188cc2019-01-07 10:46:33 -07008539 return 0;
8540}
8541
8542/*
8543 * Allocate an anonymous fd, this is what constitutes the application
8544 * visible backing of an io_uring instance. The application mmaps this
8545 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
8546 * we have to tie this fd to a socket for file garbage collection purposes.
8547 */
8548static int io_uring_get_fd(struct io_ring_ctx *ctx)
8549{
8550 struct file *file;
8551 int ret;
8552
8553#if defined(CONFIG_UNIX)
8554 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
8555 &ctx->ring_sock);
8556 if (ret)
8557 return ret;
8558#endif
8559
8560 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
8561 if (ret < 0)
8562 goto err;
8563
8564 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
8565 O_RDWR | O_CLOEXEC);
8566 if (IS_ERR(file)) {
8567 put_unused_fd(ret);
8568 ret = PTR_ERR(file);
8569 goto err;
8570 }
8571
8572#if defined(CONFIG_UNIX)
8573 ctx->ring_sock->file = file;
8574#endif
8575 fd_install(ret, file);
8576 return ret;
8577err:
8578#if defined(CONFIG_UNIX)
8579 sock_release(ctx->ring_sock);
8580 ctx->ring_sock = NULL;
8581#endif
8582 return ret;
8583}
8584
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008585static int io_uring_create(unsigned entries, struct io_uring_params *p,
8586 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008587{
8588 struct user_struct *user = NULL;
8589 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008590 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008591 int ret;
8592
Jens Axboe8110c1a2019-12-28 15:39:54 -07008593 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008594 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008595 if (entries > IORING_MAX_ENTRIES) {
8596 if (!(p->flags & IORING_SETUP_CLAMP))
8597 return -EINVAL;
8598 entries = IORING_MAX_ENTRIES;
8599 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008600
8601 /*
8602 * Use twice as many entries for the CQ ring. It's possible for the
8603 * application to drive a higher depth than the size of the SQ ring,
8604 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008605 * some flexibility in overcommitting a bit. If the application has
8606 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8607 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008608 */
8609 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008610 if (p->flags & IORING_SETUP_CQSIZE) {
8611 /*
8612 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8613 * to a power-of-two, if it isn't already. We do NOT impose
8614 * any cq vs sq ring sizing.
8615 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008616 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008617 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008618 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8619 if (!(p->flags & IORING_SETUP_CLAMP))
8620 return -EINVAL;
8621 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8622 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008623 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8624 } else {
8625 p->cq_entries = 2 * p->sq_entries;
8626 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008627
8628 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008629 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008630
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008631 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008632 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008633 ring_pages(p->sq_entries, p->cq_entries));
8634 if (ret) {
8635 free_uid(user);
8636 return ret;
8637 }
8638 }
8639
8640 ctx = io_ring_ctx_alloc(p);
8641 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008642 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008643 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008644 p->cq_entries));
8645 free_uid(user);
8646 return -ENOMEM;
8647 }
8648 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008649 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008650 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008651
Jens Axboe2aede0e2020-09-14 10:45:53 -06008652 ctx->sqo_task = get_task_struct(current);
8653
8654 /*
8655 * This is just grabbed for accounting purposes. When a process exits,
8656 * the mm is exited and dropped before the files, hence we need to hang
8657 * on to this mm purely for the purposes of being able to unaccount
8658 * memory (locked/pinned vm). It's not used for anything else.
8659 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06008660 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008661 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06008662
Jens Axboef74441e2020-08-05 13:00:44 -06008663 /*
8664 * Account memory _before_ installing the file descriptor. Once
8665 * the descriptor is installed, it can get closed at any time. Also
8666 * do this before hitting the general error path, as ring freeing
8667 * will un-account as well.
8668 */
8669 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8670 ACCT_LOCKED);
8671 ctx->limit_mem = limit_mem;
8672
Jens Axboe2b188cc2019-01-07 10:46:33 -07008673 ret = io_allocate_scq_urings(ctx, p);
8674 if (ret)
8675 goto err;
8676
Jens Axboe6c271ce2019-01-10 11:22:30 -07008677 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008678 if (ret)
8679 goto err;
8680
Jens Axboe2b188cc2019-01-07 10:46:33 -07008681 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008682 p->sq_off.head = offsetof(struct io_rings, sq.head);
8683 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8684 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8685 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8686 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8687 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8688 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008689
8690 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008691 p->cq_off.head = offsetof(struct io_rings, cq.head);
8692 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8693 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8694 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8695 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8696 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008697 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008698
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008699 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8700 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008701 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8702 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008703
8704 if (copy_to_user(params, p, sizeof(*p))) {
8705 ret = -EFAULT;
8706 goto err;
8707 }
Jens Axboed1719f72020-07-30 13:43:53 -06008708
8709 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06008710 * Install ring fd as the very last thing, so we don't risk someone
8711 * having closed it before we finish setup
8712 */
8713 ret = io_uring_get_fd(ctx);
8714 if (ret < 0)
8715 goto err;
8716
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008717 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008718 return ret;
8719err:
8720 io_ring_ctx_wait_and_kill(ctx);
8721 return ret;
8722}
8723
8724/*
8725 * Sets up an aio uring context, and returns the fd. Applications asks for a
8726 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8727 * params structure passed in.
8728 */
8729static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8730{
8731 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008732 int i;
8733
8734 if (copy_from_user(&p, params, sizeof(p)))
8735 return -EFAULT;
8736 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8737 if (p.resv[i])
8738 return -EINVAL;
8739 }
8740
Jens Axboe6c271ce2019-01-10 11:22:30 -07008741 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008742 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008743 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008744 return -EINVAL;
8745
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008746 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008747}
8748
8749SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8750 struct io_uring_params __user *, params)
8751{
8752 return io_uring_setup(entries, params);
8753}
8754
Jens Axboe66f4af92020-01-16 15:36:52 -07008755static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8756{
8757 struct io_uring_probe *p;
8758 size_t size;
8759 int i, ret;
8760
8761 size = struct_size(p, ops, nr_args);
8762 if (size == SIZE_MAX)
8763 return -EOVERFLOW;
8764 p = kzalloc(size, GFP_KERNEL);
8765 if (!p)
8766 return -ENOMEM;
8767
8768 ret = -EFAULT;
8769 if (copy_from_user(p, arg, size))
8770 goto out;
8771 ret = -EINVAL;
8772 if (memchr_inv(p, 0, size))
8773 goto out;
8774
8775 p->last_op = IORING_OP_LAST - 1;
8776 if (nr_args > IORING_OP_LAST)
8777 nr_args = IORING_OP_LAST;
8778
8779 for (i = 0; i < nr_args; i++) {
8780 p->ops[i].op = i;
8781 if (!io_op_defs[i].not_supported)
8782 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8783 }
8784 p->ops_len = i;
8785
8786 ret = 0;
8787 if (copy_to_user(arg, p, size))
8788 ret = -EFAULT;
8789out:
8790 kfree(p);
8791 return ret;
8792}
8793
Jens Axboe071698e2020-01-28 10:04:42 -07008794static int io_register_personality(struct io_ring_ctx *ctx)
8795{
8796 const struct cred *creds = get_current_cred();
8797 int id;
8798
8799 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8800 USHRT_MAX, GFP_KERNEL);
8801 if (id < 0)
8802 put_cred(creds);
8803 return id;
8804}
8805
8806static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8807{
8808 const struct cred *old_creds;
8809
8810 old_creds = idr_remove(&ctx->personality_idr, id);
8811 if (old_creds) {
8812 put_cred(old_creds);
8813 return 0;
8814 }
8815
8816 return -EINVAL;
8817}
8818
8819static bool io_register_op_must_quiesce(int op)
8820{
8821 switch (op) {
8822 case IORING_UNREGISTER_FILES:
8823 case IORING_REGISTER_FILES_UPDATE:
8824 case IORING_REGISTER_PROBE:
8825 case IORING_REGISTER_PERSONALITY:
8826 case IORING_UNREGISTER_PERSONALITY:
8827 return false;
8828 default:
8829 return true;
8830 }
8831}
8832
Jens Axboeedafcce2019-01-09 09:16:05 -07008833static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8834 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008835 __releases(ctx->uring_lock)
8836 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008837{
8838 int ret;
8839
Jens Axboe35fa71a2019-04-22 10:23:23 -06008840 /*
8841 * We're inside the ring mutex, if the ref is already dying, then
8842 * someone else killed the ctx or is already going through
8843 * io_uring_register().
8844 */
8845 if (percpu_ref_is_dying(&ctx->refs))
8846 return -ENXIO;
8847
Jens Axboe071698e2020-01-28 10:04:42 -07008848 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008849 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008850
Jens Axboe05f3fb32019-12-09 11:22:50 -07008851 /*
8852 * Drop uring mutex before waiting for references to exit. If
8853 * another thread is currently inside io_uring_enter() it might
8854 * need to grab the uring_lock to make progress. If we hold it
8855 * here across the drain wait, then we can deadlock. It's safe
8856 * to drop the mutex here, since no new references will come in
8857 * after we've killed the percpu ref.
8858 */
8859 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008860 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008861 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008862 if (ret) {
8863 percpu_ref_resurrect(&ctx->refs);
8864 ret = -EINTR;
8865 goto out;
8866 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008867 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008868
8869 switch (opcode) {
8870 case IORING_REGISTER_BUFFERS:
8871 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8872 break;
8873 case IORING_UNREGISTER_BUFFERS:
8874 ret = -EINVAL;
8875 if (arg || nr_args)
8876 break;
8877 ret = io_sqe_buffer_unregister(ctx);
8878 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008879 case IORING_REGISTER_FILES:
8880 ret = io_sqe_files_register(ctx, arg, nr_args);
8881 break;
8882 case IORING_UNREGISTER_FILES:
8883 ret = -EINVAL;
8884 if (arg || nr_args)
8885 break;
8886 ret = io_sqe_files_unregister(ctx);
8887 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008888 case IORING_REGISTER_FILES_UPDATE:
8889 ret = io_sqe_files_update(ctx, arg, nr_args);
8890 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008891 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008892 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008893 ret = -EINVAL;
8894 if (nr_args != 1)
8895 break;
8896 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008897 if (ret)
8898 break;
8899 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8900 ctx->eventfd_async = 1;
8901 else
8902 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008903 break;
8904 case IORING_UNREGISTER_EVENTFD:
8905 ret = -EINVAL;
8906 if (arg || nr_args)
8907 break;
8908 ret = io_eventfd_unregister(ctx);
8909 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008910 case IORING_REGISTER_PROBE:
8911 ret = -EINVAL;
8912 if (!arg || nr_args > 256)
8913 break;
8914 ret = io_probe(ctx, arg, nr_args);
8915 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008916 case IORING_REGISTER_PERSONALITY:
8917 ret = -EINVAL;
8918 if (arg || nr_args)
8919 break;
8920 ret = io_register_personality(ctx);
8921 break;
8922 case IORING_UNREGISTER_PERSONALITY:
8923 ret = -EINVAL;
8924 if (arg)
8925 break;
8926 ret = io_unregister_personality(ctx, nr_args);
8927 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008928 default:
8929 ret = -EINVAL;
8930 break;
8931 }
8932
Jens Axboe071698e2020-01-28 10:04:42 -07008933 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008934 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008935 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008936out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008937 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008938 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008939 return ret;
8940}
8941
8942SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8943 void __user *, arg, unsigned int, nr_args)
8944{
8945 struct io_ring_ctx *ctx;
8946 long ret = -EBADF;
8947 struct fd f;
8948
8949 f = fdget(fd);
8950 if (!f.file)
8951 return -EBADF;
8952
8953 ret = -EOPNOTSUPP;
8954 if (f.file->f_op != &io_uring_fops)
8955 goto out_fput;
8956
8957 ctx = f.file->private_data;
8958
8959 mutex_lock(&ctx->uring_lock);
8960 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8961 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008962 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8963 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008964out_fput:
8965 fdput(f);
8966 return ret;
8967}
8968
Jens Axboe2b188cc2019-01-07 10:46:33 -07008969static int __init io_uring_init(void)
8970{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008971#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8972 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8973 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8974} while (0)
8975
8976#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8977 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8978 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8979 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8980 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8981 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8982 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8983 BUILD_BUG_SQE_ELEM(8, __u64, off);
8984 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8985 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008986 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008987 BUILD_BUG_SQE_ELEM(24, __u32, len);
8988 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8989 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8990 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8991 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008992 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8993 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008994 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8995 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8996 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8997 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8998 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8999 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9000 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9001 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009002 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009003 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9004 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9005 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009006 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009007
Jens Axboed3656342019-12-18 09:50:26 -07009008 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07009009 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009010 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
9011 return 0;
9012};
9013__initcall(io_uring_init);