blob: a80b5c189c1436e7e1a1ff62be6c4b7c60c0204d [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>
47#include <linux/refcount.h>
48#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#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 Axboe2b188cc2019-01-07 10:46:33 -070081
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020082#define CREATE_TRACE_POINTS
83#include <trace/events/io_uring.h>
84
Jens Axboe2b188cc2019-01-07 10:46:33 -070085#include <uapi/linux/io_uring.h>
86
87#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060088#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070089
Daniel Xu5277dea2019-09-14 14:23:45 -070090#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060091#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060092
93/*
94 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
95 */
96#define IORING_FILE_TABLE_SHIFT 9
97#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
98#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
99#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700100
101struct io_uring {
102 u32 head ____cacheline_aligned_in_smp;
103 u32 tail ____cacheline_aligned_in_smp;
104};
105
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000107 * This data is shared with the application through the mmap at offsets
108 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 *
110 * The offsets to the member fields are published through struct
111 * io_sqring_offsets when calling io_uring_setup.
112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
115 * Head and tail offsets into the ring; the offsets need to be
116 * masked to get valid indices.
117 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 * The kernel controls head of the sq ring and the tail of the cq ring,
119 * and the application controls tail of the sq ring and the head of the
120 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 * ring_entries - 1)
126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 u32 sq_ring_mask, cq_ring_mask;
128 /* Ring sizes (constant, power of 2) */
129 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 /*
131 * Number of invalid entries dropped by the kernel due to
132 * invalid index stored in array
133 *
134 * Written by the kernel, shouldn't be modified by the
135 * application (i.e. get number of "new events" by comparing to
136 * cached value).
137 *
138 * After a new SQ head value was read by the application this
139 * counter includes all submissions that were dropped reaching
140 * the new SQ head (and possibly more).
141 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000142 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200143 /*
144 * Runtime flags
145 *
146 * Written by the kernel, shouldn't be modified by the
147 * application.
148 *
149 * The application needs a full memory barrier before checking
150 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
151 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000152 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 /*
154 * Number of completion events lost because the queue was full;
155 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800156 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 * the completion queue.
158 *
159 * Written by the kernel, shouldn't be modified by the
160 * application (i.e. get number of "new events" by comparing to
161 * cached value).
162 *
163 * As completion events come in out of order this counter is not
164 * ordered with any other data.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 /*
168 * Ring buffer of completion events.
169 *
170 * The kernel writes completion events fresh every time they are
171 * produced, so the application is allowed to modify pending
172 * entries.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700175};
176
Jens Axboeedafcce2019-01-09 09:16:05 -0700177struct io_mapped_ubuf {
178 u64 ubuf;
179 size_t len;
180 struct bio_vec *bvec;
181 unsigned int nr_bvecs;
182};
183
Jens Axboe65e19f52019-10-26 07:20:21 -0600184struct fixed_file_table {
185 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700186};
187
Jens Axboe05f3fb32019-12-09 11:22:50 -0700188struct fixed_file_data {
189 struct fixed_file_table *table;
190 struct io_ring_ctx *ctx;
191
192 struct percpu_ref refs;
193 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700194 struct work_struct ref_work;
195 struct completion done;
196};
197
Jens Axboe5a2e7452020-02-23 16:23:11 -0700198struct io_buffer {
199 struct list_head list;
200 __u64 addr;
201 __s32 len;
202 __u16 bid;
203};
204
Jens Axboe2b188cc2019-01-07 10:46:33 -0700205struct io_ring_ctx {
206 struct {
207 struct percpu_ref refs;
208 } ____cacheline_aligned_in_smp;
209
210 struct {
211 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800212 unsigned int compat: 1;
213 unsigned int account_mem: 1;
214 unsigned int cq_overflow_flushed: 1;
215 unsigned int drain_next: 1;
216 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217
Hristo Venev75b28af2019-08-26 17:23:46 +0000218 /*
219 * Ring buffer of indices into array of io_uring_sqe, which is
220 * mmapped by the application using the IORING_OFF_SQES offset.
221 *
222 * This indirection could e.g. be used to assign fixed
223 * io_uring_sqe entries to operations and only submit them to
224 * the queue when needed.
225 *
226 * The kernel modifies neither the indices array nor the entries
227 * array.
228 */
229 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700230 unsigned cached_sq_head;
231 unsigned sq_entries;
232 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700233 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600234 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700235 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700236 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600237
238 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600239 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700240 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241
Jens Axboefcb323c2019-10-24 12:39:47 -0600242 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700243 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244 } ____cacheline_aligned_in_smp;
245
Hristo Venev75b28af2019-08-26 17:23:46 +0000246 struct io_rings *rings;
247
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600249 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700250 struct task_struct *sqo_thread; /* if using sq thread polling */
251 struct mm_struct *sqo_mm;
252 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700253
Jens Axboe6b063142019-01-10 22:13:58 -0700254 /*
255 * If used, fixed file set. Writers must ensure that ->refs is dead,
256 * readers must ensure that ->refs is alive as long as the file* is
257 * used. Only updated through io_uring_register(2).
258 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700259 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700260 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300261 int ring_fd;
262 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700263
Jens Axboeedafcce2019-01-09 09:16:05 -0700264 /* if used, fixed mapped user buffers */
265 unsigned nr_user_bufs;
266 struct io_mapped_ubuf *user_bufs;
267
Jens Axboe2b188cc2019-01-07 10:46:33 -0700268 struct user_struct *user;
269
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700270 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700271
Jens Axboe206aefd2019-11-07 18:27:42 -0700272 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
273 struct completion *completions;
274
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700275 /* if all else fails... */
276 struct io_kiocb *fallback_req;
277
Jens Axboe206aefd2019-11-07 18:27:42 -0700278#if defined(CONFIG_UNIX)
279 struct socket *ring_sock;
280#endif
281
Jens Axboe5a2e7452020-02-23 16:23:11 -0700282 struct idr io_buffer_idr;
283
Jens Axboe071698e2020-01-28 10:04:42 -0700284 struct idr personality_idr;
285
Jens Axboe206aefd2019-11-07 18:27:42 -0700286 struct {
287 unsigned cached_cq_tail;
288 unsigned cq_entries;
289 unsigned cq_mask;
290 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700291 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700292 struct wait_queue_head cq_wait;
293 struct fasync_struct *cq_fasync;
294 struct eventfd_ctx *cq_ev_fd;
295 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700296
297 struct {
298 struct mutex uring_lock;
299 wait_queue_head_t wait;
300 } ____cacheline_aligned_in_smp;
301
302 struct {
303 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700304
Jens Axboedef596e2019-01-09 08:59:42 -0700305 /*
306 * ->poll_list is protected by the ctx->uring_lock for
307 * io_uring instances that don't use IORING_SETUP_SQPOLL.
308 * For SQPOLL, only the single threaded io_sq_thread() will
309 * manipulate the list, hence no extra locking is needed there.
310 */
311 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700312 struct hlist_head *cancel_hash;
313 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700314 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600315
316 spinlock_t inflight_lock;
317 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700318 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700319};
320
Jens Axboe09bb8392019-03-13 12:39:28 -0600321/*
322 * First field must be the file pointer in all the
323 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
324 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700325struct io_poll_iocb {
326 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700327 union {
328 struct wait_queue_head *head;
329 u64 addr;
330 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700331 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600332 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700333 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700334 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700335};
336
Jens Axboeb5dba592019-12-11 14:02:38 -0700337struct io_close {
338 struct file *file;
339 struct file *put_file;
340 int fd;
341};
342
Jens Axboead8a48a2019-11-15 08:49:11 -0700343struct io_timeout_data {
344 struct io_kiocb *req;
345 struct hrtimer timer;
346 struct timespec64 ts;
347 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300348 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700349};
350
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700351struct io_accept {
352 struct file *file;
353 struct sockaddr __user *addr;
354 int __user *addr_len;
355 int flags;
356};
357
358struct io_sync {
359 struct file *file;
360 loff_t len;
361 loff_t off;
362 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700363 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700364};
365
Jens Axboefbf23842019-12-17 18:45:56 -0700366struct io_cancel {
367 struct file *file;
368 u64 addr;
369};
370
Jens Axboeb29472e2019-12-17 18:50:29 -0700371struct io_timeout {
372 struct file *file;
373 u64 addr;
374 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700375 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700376};
377
Jens Axboe9adbd452019-12-20 08:45:55 -0700378struct io_rw {
379 /* NOTE: kiocb has the file as the first member, so don't do it here */
380 struct kiocb kiocb;
381 u64 addr;
382 u64 len;
383};
384
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700385struct io_connect {
386 struct file *file;
387 struct sockaddr __user *addr;
388 int addr_len;
389};
390
Jens Axboee47293f2019-12-20 08:58:21 -0700391struct io_sr_msg {
392 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700393 union {
394 struct user_msghdr __user *msg;
395 void __user *buf;
396 };
Jens Axboee47293f2019-12-20 08:58:21 -0700397 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700398 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700399 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700400 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700401};
402
Jens Axboe15b71ab2019-12-11 11:20:36 -0700403struct io_open {
404 struct file *file;
405 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700406 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700407 unsigned mask;
408 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700409 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700410 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700411 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700412};
413
Jens Axboe05f3fb32019-12-09 11:22:50 -0700414struct io_files_update {
415 struct file *file;
416 u64 arg;
417 u32 nr_args;
418 u32 offset;
419};
420
Jens Axboe4840e412019-12-25 22:03:45 -0700421struct io_fadvise {
422 struct file *file;
423 u64 offset;
424 u32 len;
425 u32 advice;
426};
427
Jens Axboec1ca7572019-12-25 22:18:28 -0700428struct io_madvise {
429 struct file *file;
430 u64 addr;
431 u32 len;
432 u32 advice;
433};
434
Jens Axboe3e4827b2020-01-08 15:18:09 -0700435struct io_epoll {
436 struct file *file;
437 int epfd;
438 int op;
439 int fd;
440 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700441};
442
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300443struct io_splice {
444 struct file *file_out;
445 struct file *file_in;
446 loff_t off_out;
447 loff_t off_in;
448 u64 len;
449 unsigned int flags;
450};
451
Jens Axboeddf0322d2020-02-23 16:41:33 -0700452struct io_provide_buf {
453 struct file *file;
454 __u64 addr;
455 __s32 len;
456 __u32 bgid;
457 __u16 nbufs;
458 __u16 bid;
459};
460
Jens Axboef499a022019-12-02 16:28:46 -0700461struct io_async_connect {
462 struct sockaddr_storage address;
463};
464
Jens Axboe03b12302019-12-02 18:50:25 -0700465struct io_async_msghdr {
466 struct iovec fast_iov[UIO_FASTIOV];
467 struct iovec *iov;
468 struct sockaddr __user *uaddr;
469 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700470 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700471};
472
Jens Axboef67676d2019-12-02 11:03:47 -0700473struct io_async_rw {
474 struct iovec fast_iov[UIO_FASTIOV];
475 struct iovec *iov;
476 ssize_t nr_segs;
477 ssize_t size;
478};
479
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700480struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700481 union {
482 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700483 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700484 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700485 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700486 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700487};
488
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300489enum {
490 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
491 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
492 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
493 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
494 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700495 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300496
497 REQ_F_LINK_NEXT_BIT,
498 REQ_F_FAIL_LINK_BIT,
499 REQ_F_INFLIGHT_BIT,
500 REQ_F_CUR_POS_BIT,
501 REQ_F_NOWAIT_BIT,
502 REQ_F_IOPOLL_COMPLETED_BIT,
503 REQ_F_LINK_TIMEOUT_BIT,
504 REQ_F_TIMEOUT_BIT,
505 REQ_F_ISREG_BIT,
506 REQ_F_MUST_PUNT_BIT,
507 REQ_F_TIMEOUT_NOSEQ_BIT,
508 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300509 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700510 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700511 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700512 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300513};
514
515enum {
516 /* ctx owns file */
517 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
518 /* drain existing IO first */
519 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
520 /* linked sqes */
521 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
522 /* doesn't sever on completion < 0 */
523 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
524 /* IOSQE_ASYNC */
525 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700526 /* IOSQE_BUFFER_SELECT */
527 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300528
529 /* already grabbed next link */
530 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
531 /* fail rest of links */
532 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
533 /* on inflight list */
534 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
535 /* read/write uses file position */
536 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
537 /* must not punt to workers */
538 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
539 /* polled IO has completed */
540 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
541 /* has linked timeout */
542 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
543 /* timeout request */
544 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
545 /* regular file */
546 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
547 /* must be punted even for NONBLOCK */
548 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
549 /* no timeout sequence */
550 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
551 /* completion under lock */
552 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300553 /* needs cleanup */
554 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700555 /* in overflow list */
556 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700557 /* already went through poll handler */
558 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700559 /* buffer already selected */
560 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700561};
562
563struct async_poll {
564 struct io_poll_iocb poll;
565 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300566};
567
Jens Axboe09bb8392019-03-13 12:39:28 -0600568/*
569 * NOTE! Each of the iocb union members has the file pointer
570 * as the first entry in their struct definition. So you can
571 * access the file pointer through any of the sub-structs,
572 * or directly as just 'ki_filp' in this struct.
573 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700575 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600576 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700577 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700578 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700579 struct io_accept accept;
580 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700581 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700582 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700583 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700584 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700585 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700586 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700587 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700588 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700589 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700590 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300591 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700592 struct io_provide_buf pbuf;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700593 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700594
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700595 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300596 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700597 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700598
599 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700600 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700601 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700602 refcount_t refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700603 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700604 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600605 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600606 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700607
Jens Axboed7718a92020-02-14 22:23:12 -0700608 struct list_head link_list;
609
Jens Axboefcb323c2019-10-24 12:39:47 -0600610 struct list_head inflight_entry;
611
Jens Axboeb41e9852020-02-17 09:52:41 -0700612 union {
613 /*
614 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700615 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
616 * async armed poll handlers for regular commands. The latter
617 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700618 */
619 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700620 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700621 struct hlist_node hash_node;
622 struct async_poll *apoll;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700623 int cflags;
Jens Axboeb41e9852020-02-17 09:52:41 -0700624 };
625 struct io_wq_work work;
626 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700627};
628
629#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700630#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631
Jens Axboe9a56a232019-01-09 09:06:50 -0700632struct io_submit_state {
633 struct blk_plug plug;
634
635 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700636 * io_kiocb alloc cache
637 */
638 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300639 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700640
641 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700642 * File reference cache
643 */
644 struct file *file;
645 unsigned int fd;
646 unsigned int has_refs;
647 unsigned int used_refs;
648 unsigned int ios_left;
649};
650
Jens Axboed3656342019-12-18 09:50:26 -0700651struct io_op_def {
652 /* needs req->io allocated for deferral/async */
653 unsigned async_ctx : 1;
654 /* needs current->mm setup, does mm access */
655 unsigned needs_mm : 1;
656 /* needs req->file assigned */
657 unsigned needs_file : 1;
658 /* needs req->file assigned IFF fd is >= 0 */
659 unsigned fd_non_neg : 1;
660 /* hash wq insertion if file is a regular file */
661 unsigned hash_reg_file : 1;
662 /* unbound wq insertion if file is a non-regular file */
663 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700664 /* opcode is not supported by this kernel */
665 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700666 /* needs file table */
667 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700668 /* needs ->fs */
669 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700670 /* set if opcode supports polled "wait" */
671 unsigned pollin : 1;
672 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700673 /* op supports buffer selection */
674 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700675};
676
677static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300678 [IORING_OP_NOP] = {},
679 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700680 .async_ctx = 1,
681 .needs_mm = 1,
682 .needs_file = 1,
683 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700684 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700685 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300686 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700687 .async_ctx = 1,
688 .needs_mm = 1,
689 .needs_file = 1,
690 .hash_reg_file = 1,
691 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700692 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700693 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300694 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700695 .needs_file = 1,
696 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300697 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700698 .needs_file = 1,
699 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700700 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700701 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300702 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700703 .needs_file = 1,
704 .hash_reg_file = 1,
705 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700706 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700707 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300708 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700709 .needs_file = 1,
710 .unbound_nonreg_file = 1,
711 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300712 [IORING_OP_POLL_REMOVE] = {},
713 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700714 .needs_file = 1,
715 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300716 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700717 .async_ctx = 1,
718 .needs_mm = 1,
719 .needs_file = 1,
720 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700721 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700722 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700723 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300724 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700725 .async_ctx = 1,
726 .needs_mm = 1,
727 .needs_file = 1,
728 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700729 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700730 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700731 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300732 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700733 .async_ctx = 1,
734 .needs_mm = 1,
735 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300736 [IORING_OP_TIMEOUT_REMOVE] = {},
737 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700738 .needs_mm = 1,
739 .needs_file = 1,
740 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700741 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700742 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700743 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300744 [IORING_OP_ASYNC_CANCEL] = {},
745 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700746 .async_ctx = 1,
747 .needs_mm = 1,
748 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300749 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700750 .async_ctx = 1,
751 .needs_mm = 1,
752 .needs_file = 1,
753 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700754 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700755 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300756 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700757 .needs_file = 1,
758 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300759 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700760 .needs_file = 1,
761 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700762 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700763 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700764 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300765 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700766 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700767 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700768 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300769 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700770 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700771 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700772 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300773 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700774 .needs_mm = 1,
775 .needs_file = 1,
776 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700777 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700778 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300779 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700780 .needs_mm = 1,
781 .needs_file = 1,
782 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700783 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700784 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700785 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300786 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700787 .needs_mm = 1,
788 .needs_file = 1,
789 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700790 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700791 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300792 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700793 .needs_file = 1,
794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700796 .needs_mm = 1,
797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700799 .needs_mm = 1,
800 .needs_file = 1,
801 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700802 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700803 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300804 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700805 .needs_mm = 1,
806 .needs_file = 1,
807 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700808 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700809 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700810 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300811 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700812 .needs_file = 1,
813 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700814 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700815 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700816 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700817 [IORING_OP_EPOLL_CTL] = {
818 .unbound_nonreg_file = 1,
819 .file_table = 1,
820 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300821 [IORING_OP_SPLICE] = {
822 .needs_file = 1,
823 .hash_reg_file = 1,
824 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700825 },
826 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700827};
828
Jens Axboe561fb042019-10-24 07:25:42 -0600829static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700830static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800831static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700832static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700833static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
834static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700835static int __io_sqe_files_update(struct io_ring_ctx *ctx,
836 struct io_uring_files_update *ip,
837 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700838static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700839static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300840static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700841static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
842 int fd, struct file **out_file, bool fixed);
843static void __io_queue_sqe(struct io_kiocb *req,
844 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600845
Jens Axboe2b188cc2019-01-07 10:46:33 -0700846static struct kmem_cache *req_cachep;
847
848static const struct file_operations io_uring_fops;
849
850struct sock *io_uring_get_socket(struct file *file)
851{
852#if defined(CONFIG_UNIX)
853 if (file->f_op == &io_uring_fops) {
854 struct io_ring_ctx *ctx = file->private_data;
855
856 return ctx->ring_sock->sk;
857 }
858#endif
859 return NULL;
860}
861EXPORT_SYMBOL(io_uring_get_socket);
862
863static void io_ring_ctx_ref_free(struct percpu_ref *ref)
864{
865 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
866
Jens Axboe206aefd2019-11-07 18:27:42 -0700867 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700868}
869
870static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
871{
872 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700873 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700874
875 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
876 if (!ctx)
877 return NULL;
878
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700879 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
880 if (!ctx->fallback_req)
881 goto err;
882
Jens Axboe206aefd2019-11-07 18:27:42 -0700883 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
884 if (!ctx->completions)
885 goto err;
886
Jens Axboe78076bb2019-12-04 19:56:40 -0700887 /*
888 * Use 5 bits less than the max cq entries, that should give us around
889 * 32 entries per hash list if totally full and uniformly spread.
890 */
891 hash_bits = ilog2(p->cq_entries);
892 hash_bits -= 5;
893 if (hash_bits <= 0)
894 hash_bits = 1;
895 ctx->cancel_hash_bits = hash_bits;
896 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
897 GFP_KERNEL);
898 if (!ctx->cancel_hash)
899 goto err;
900 __hash_init(ctx->cancel_hash, 1U << hash_bits);
901
Roman Gushchin21482892019-05-07 10:01:48 -0700902 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700903 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
904 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700905
906 ctx->flags = p->flags;
907 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700908 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700909 init_completion(&ctx->completions[0]);
910 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700911 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700912 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700913 mutex_init(&ctx->uring_lock);
914 init_waitqueue_head(&ctx->wait);
915 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700916 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600917 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600918 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600919 init_waitqueue_head(&ctx->inflight_wait);
920 spin_lock_init(&ctx->inflight_lock);
921 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700922 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700923err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700924 if (ctx->fallback_req)
925 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700926 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700927 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700928 kfree(ctx);
929 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700930}
931
Bob Liu9d858b22019-11-13 18:06:25 +0800932static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600933{
Jackie Liua197f662019-11-08 08:09:12 -0700934 struct io_ring_ctx *ctx = req->ctx;
935
Jens Axboe498ccd92019-10-25 10:04:25 -0600936 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
937 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600938}
939
Bob Liu9d858b22019-11-13 18:06:25 +0800940static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600941{
Pavel Begunkov87987892020-01-18 01:22:30 +0300942 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800943 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600944
Bob Liu9d858b22019-11-13 18:06:25 +0800945 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600946}
947
948static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600949{
950 struct io_kiocb *req;
951
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600952 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800953 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600954 list_del_init(&req->list);
955 return req;
956 }
957
958 return NULL;
959}
960
Jens Axboe5262f562019-09-17 12:26:57 -0600961static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
962{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600963 struct io_kiocb *req;
964
965 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700966 if (req) {
967 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
968 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800969 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700970 list_del_init(&req->list);
971 return req;
972 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600973 }
974
975 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600976}
977
Jens Axboede0617e2019-04-06 21:51:27 -0600978static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700979{
Hristo Venev75b28af2019-08-26 17:23:46 +0000980 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700981
Pavel Begunkov07910152020-01-17 03:52:46 +0300982 /* order cqe stores with ring update */
983 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700984
Pavel Begunkov07910152020-01-17 03:52:46 +0300985 if (wq_has_sleeper(&ctx->cq_wait)) {
986 wake_up_interruptible(&ctx->cq_wait);
987 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700988 }
989}
990
Jens Axboecccf0ee2020-01-27 16:34:48 -0700991static inline void io_req_work_grab_env(struct io_kiocb *req,
992 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600993{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700994 if (!req->work.mm && def->needs_mm) {
995 mmgrab(current->mm);
996 req->work.mm = current->mm;
997 }
998 if (!req->work.creds)
999 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001000 if (!req->work.fs && def->needs_fs) {
1001 spin_lock(&current->fs->lock);
1002 if (!current->fs->in_exec) {
1003 req->work.fs = current->fs;
1004 req->work.fs->users++;
1005 } else {
1006 req->work.flags |= IO_WQ_WORK_CANCEL;
1007 }
1008 spin_unlock(&current->fs->lock);
1009 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001010 if (!req->work.task_pid)
1011 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001012}
1013
1014static inline void io_req_work_drop_env(struct io_kiocb *req)
1015{
1016 if (req->work.mm) {
1017 mmdrop(req->work.mm);
1018 req->work.mm = NULL;
1019 }
1020 if (req->work.creds) {
1021 put_cred(req->work.creds);
1022 req->work.creds = NULL;
1023 }
Jens Axboeff002b32020-02-07 16:05:21 -07001024 if (req->work.fs) {
1025 struct fs_struct *fs = req->work.fs;
1026
1027 spin_lock(&req->work.fs->lock);
1028 if (--fs->users)
1029 fs = NULL;
1030 spin_unlock(&req->work.fs->lock);
1031 if (fs)
1032 free_fs_struct(fs);
1033 }
Jens Axboe561fb042019-10-24 07:25:42 -06001034}
1035
Jens Axboe94ae5e72019-11-14 19:39:52 -07001036static inline bool io_prep_async_work(struct io_kiocb *req,
1037 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001038{
Jens Axboed3656342019-12-18 09:50:26 -07001039 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -06001040 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -06001041
Jens Axboed3656342019-12-18 09:50:26 -07001042 if (req->flags & REQ_F_ISREG) {
1043 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001044 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -07001045 } else {
1046 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001047 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001048 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001049
1050 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001051
Jens Axboe94ae5e72019-11-14 19:39:52 -07001052 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001053 return do_hashed;
1054}
1055
Jackie Liua197f662019-11-08 08:09:12 -07001056static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001057{
Jackie Liua197f662019-11-08 08:09:12 -07001058 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001059 struct io_kiocb *link;
1060 bool do_hashed;
1061
1062 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001063
1064 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1065 req->flags);
1066 if (!do_hashed) {
1067 io_wq_enqueue(ctx->io_wq, &req->work);
1068 } else {
1069 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1070 file_inode(req->file));
1071 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001072
1073 if (link)
1074 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001075}
1076
Jens Axboe5262f562019-09-17 12:26:57 -06001077static void io_kill_timeout(struct io_kiocb *req)
1078{
1079 int ret;
1080
Jens Axboe2d283902019-12-04 11:08:05 -07001081 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001082 if (ret != -1) {
1083 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001084 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001085 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001086 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001087 }
1088}
1089
1090static void io_kill_timeouts(struct io_ring_ctx *ctx)
1091{
1092 struct io_kiocb *req, *tmp;
1093
1094 spin_lock_irq(&ctx->completion_lock);
1095 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1096 io_kill_timeout(req);
1097 spin_unlock_irq(&ctx->completion_lock);
1098}
1099
Jens Axboede0617e2019-04-06 21:51:27 -06001100static void io_commit_cqring(struct io_ring_ctx *ctx)
1101{
1102 struct io_kiocb *req;
1103
Jens Axboe5262f562019-09-17 12:26:57 -06001104 while ((req = io_get_timeout_req(ctx)) != NULL)
1105 io_kill_timeout(req);
1106
Jens Axboede0617e2019-04-06 21:51:27 -06001107 __io_commit_cqring(ctx);
1108
Pavel Begunkov87987892020-01-18 01:22:30 +03001109 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001110 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001111}
1112
Jens Axboe2b188cc2019-01-07 10:46:33 -07001113static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1114{
Hristo Venev75b28af2019-08-26 17:23:46 +00001115 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001116 unsigned tail;
1117
1118 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001119 /*
1120 * writes to the cq entry need to come after reading head; the
1121 * control dependency is enough as we're using WRITE_ONCE to
1122 * fill the cq entry
1123 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001124 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001125 return NULL;
1126
1127 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001128 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129}
1130
Jens Axboef2842ab2020-01-08 11:04:00 -07001131static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1132{
Jens Axboef0b493e2020-02-01 21:30:11 -07001133 if (!ctx->cq_ev_fd)
1134 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001135 if (!ctx->eventfd_async)
1136 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001137 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001138}
1139
Jens Axboeb41e9852020-02-17 09:52:41 -07001140static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001141{
1142 if (waitqueue_active(&ctx->wait))
1143 wake_up(&ctx->wait);
1144 if (waitqueue_active(&ctx->sqo_wait))
1145 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001146 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001147 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001148}
1149
Jens Axboec4a2ed72019-11-21 21:01:26 -07001150/* Returns true if there are no backlogged entries after the flush */
1151static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001152{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001153 struct io_rings *rings = ctx->rings;
1154 struct io_uring_cqe *cqe;
1155 struct io_kiocb *req;
1156 unsigned long flags;
1157 LIST_HEAD(list);
1158
1159 if (!force) {
1160 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001161 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001162 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1163 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001164 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001165 }
1166
1167 spin_lock_irqsave(&ctx->completion_lock, flags);
1168
1169 /* if force is set, the ring is going away. always drop after that */
1170 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001171 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001172
Jens Axboec4a2ed72019-11-21 21:01:26 -07001173 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001174 while (!list_empty(&ctx->cq_overflow_list)) {
1175 cqe = io_get_cqring(ctx);
1176 if (!cqe && !force)
1177 break;
1178
1179 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1180 list);
1181 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001182 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001183 if (cqe) {
1184 WRITE_ONCE(cqe->user_data, req->user_data);
1185 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001186 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001187 } else {
1188 WRITE_ONCE(ctx->rings->cq_overflow,
1189 atomic_inc_return(&ctx->cached_cq_overflow));
1190 }
1191 }
1192
1193 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001194 if (cqe) {
1195 clear_bit(0, &ctx->sq_check_overflow);
1196 clear_bit(0, &ctx->cq_check_overflow);
1197 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001198 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1199 io_cqring_ev_posted(ctx);
1200
1201 while (!list_empty(&list)) {
1202 req = list_first_entry(&list, struct io_kiocb, list);
1203 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001204 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001205 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001206
1207 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001208}
1209
Jens Axboebcda7ba2020-02-23 16:42:51 -07001210static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001211{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001212 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001213 struct io_uring_cqe *cqe;
1214
Jens Axboe78e19bb2019-11-06 15:21:34 -07001215 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001216
Jens Axboe2b188cc2019-01-07 10:46:33 -07001217 /*
1218 * If we can't get a cq entry, userspace overflowed the
1219 * submission (by quite a lot). Increment the overflow count in
1220 * the ring.
1221 */
1222 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001223 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001224 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001225 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001226 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001227 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228 WRITE_ONCE(ctx->rings->cq_overflow,
1229 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001230 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001231 if (list_empty(&ctx->cq_overflow_list)) {
1232 set_bit(0, &ctx->sq_check_overflow);
1233 set_bit(0, &ctx->cq_check_overflow);
1234 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001235 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001236 refcount_inc(&req->refs);
1237 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001238 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001239 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001240 }
1241}
1242
Jens Axboebcda7ba2020-02-23 16:42:51 -07001243static void io_cqring_fill_event(struct io_kiocb *req, long res)
1244{
1245 __io_cqring_fill_event(req, res, 0);
1246}
1247
1248static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001249{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001250 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001251 unsigned long flags;
1252
1253 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001254 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255 io_commit_cqring(ctx);
1256 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1257
Jens Axboe8c838782019-03-12 15:48:16 -06001258 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259}
1260
Jens Axboebcda7ba2020-02-23 16:42:51 -07001261static void io_cqring_add_event(struct io_kiocb *req, long res)
1262{
1263 __io_cqring_add_event(req, res, 0);
1264}
1265
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001266static inline bool io_is_fallback_req(struct io_kiocb *req)
1267{
1268 return req == (struct io_kiocb *)
1269 ((unsigned long) req->ctx->fallback_req & ~1UL);
1270}
1271
1272static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1273{
1274 struct io_kiocb *req;
1275
1276 req = ctx->fallback_req;
1277 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1278 return req;
1279
1280 return NULL;
1281}
1282
Jens Axboe2579f912019-01-09 09:10:43 -07001283static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1284 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001285{
Jens Axboefd6fab22019-03-14 16:30:06 -06001286 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287 struct io_kiocb *req;
1288
Jens Axboe2579f912019-01-09 09:10:43 -07001289 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001290 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001291 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001292 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001293 } else if (!state->free_reqs) {
1294 size_t sz;
1295 int ret;
1296
1297 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001298 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1299
1300 /*
1301 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1302 * retry single alloc to be on the safe side.
1303 */
1304 if (unlikely(ret <= 0)) {
1305 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1306 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001307 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001308 ret = 1;
1309 }
Jens Axboe2579f912019-01-09 09:10:43 -07001310 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001311 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001312 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001313 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001314 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001315 }
1316
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001317got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001318 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001319 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001320 req->ctx = ctx;
1321 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001322 /* one is dropped after submission, the other at completion */
1323 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001324 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001325 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001326 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001327fallback:
1328 req = io_get_fallback_req(ctx);
1329 if (req)
1330 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001331 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001332 return NULL;
1333}
1334
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001335static inline void io_put_file(struct io_kiocb *req, struct file *file,
1336 bool fixed)
1337{
1338 if (fixed)
1339 percpu_ref_put(&req->ctx->file_data->refs);
1340 else
1341 fput(file);
1342}
1343
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001344static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001345{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001346 if (likely(!io_is_fallback_req(req)))
1347 kmem_cache_free(req_cachep, req);
1348 else
1349 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1350}
1351
Jens Axboec6ca97b302019-12-28 12:11:08 -07001352static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001353{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001354 if (req->flags & REQ_F_NEED_CLEANUP)
1355 io_cleanup_req(req);
1356
YueHaibing96fd84d2020-01-07 22:22:44 +08001357 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001358 if (req->file)
1359 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001360
1361 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001362}
1363
1364static void __io_free_req(struct io_kiocb *req)
1365{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001366 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001367
Jens Axboefcb323c2019-10-24 12:39:47 -06001368 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001369 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001370 unsigned long flags;
1371
1372 spin_lock_irqsave(&ctx->inflight_lock, flags);
1373 list_del(&req->inflight_entry);
1374 if (waitqueue_active(&ctx->inflight_wait))
1375 wake_up(&ctx->inflight_wait);
1376 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1377 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001378
1379 percpu_ref_put(&req->ctx->refs);
1380 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001381}
1382
Jens Axboec6ca97b302019-12-28 12:11:08 -07001383struct req_batch {
1384 void *reqs[IO_IOPOLL_BATCH];
1385 int to_free;
1386 int need_iter;
1387};
1388
1389static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1390{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001391 int fixed_refs = rb->to_free;
1392
Jens Axboec6ca97b302019-12-28 12:11:08 -07001393 if (!rb->to_free)
1394 return;
1395 if (rb->need_iter) {
1396 int i, inflight = 0;
1397 unsigned long flags;
1398
Jens Axboe10fef4b2020-01-09 07:52:28 -07001399 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001400 for (i = 0; i < rb->to_free; i++) {
1401 struct io_kiocb *req = rb->reqs[i];
1402
Jens Axboe10fef4b2020-01-09 07:52:28 -07001403 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001404 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001405 fixed_refs++;
1406 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001407 if (req->flags & REQ_F_INFLIGHT)
1408 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001409 __io_req_aux_free(req);
1410 }
1411 if (!inflight)
1412 goto do_free;
1413
1414 spin_lock_irqsave(&ctx->inflight_lock, flags);
1415 for (i = 0; i < rb->to_free; i++) {
1416 struct io_kiocb *req = rb->reqs[i];
1417
Jens Axboe10fef4b2020-01-09 07:52:28 -07001418 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001419 list_del(&req->inflight_entry);
1420 if (!--inflight)
1421 break;
1422 }
1423 }
1424 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1425
1426 if (waitqueue_active(&ctx->inflight_wait))
1427 wake_up(&ctx->inflight_wait);
1428 }
1429do_free:
1430 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001431 if (fixed_refs)
1432 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001433 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001434 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001435}
1436
Jackie Liua197f662019-11-08 08:09:12 -07001437static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001438{
Jackie Liua197f662019-11-08 08:09:12 -07001439 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001440 int ret;
1441
Jens Axboe2d283902019-12-04 11:08:05 -07001442 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001443 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001444 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001445 io_commit_cqring(ctx);
1446 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001447 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001448 return true;
1449 }
1450
1451 return false;
1452}
1453
Jens Axboeba816ad2019-09-28 11:36:45 -06001454static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001455{
Jens Axboe2665abf2019-11-05 12:40:47 -07001456 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001457 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001458
Jens Axboe4d7dd462019-11-20 13:03:52 -07001459 /* Already got next link */
1460 if (req->flags & REQ_F_LINK_NEXT)
1461 return;
1462
Jens Axboe9e645e112019-05-10 16:07:28 -06001463 /*
1464 * The list should never be empty when we are called here. But could
1465 * potentially happen if the chain is messed up, check to be on the
1466 * safe side.
1467 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001468 while (!list_empty(&req->link_list)) {
1469 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1470 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001471
Pavel Begunkov44932332019-12-05 16:16:35 +03001472 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1473 (nxt->flags & REQ_F_TIMEOUT))) {
1474 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001475 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001476 req->flags &= ~REQ_F_LINK_TIMEOUT;
1477 continue;
1478 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001479
Pavel Begunkov44932332019-12-05 16:16:35 +03001480 list_del_init(&req->link_list);
1481 if (!list_empty(&nxt->link_list))
1482 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001483 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001484 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001485 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001486
Jens Axboe4d7dd462019-11-20 13:03:52 -07001487 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001488 if (wake_ev)
1489 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001490}
1491
1492/*
1493 * Called if REQ_F_LINK is set, and we fail the head request
1494 */
1495static void io_fail_links(struct io_kiocb *req)
1496{
Jens Axboe2665abf2019-11-05 12:40:47 -07001497 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001498 unsigned long flags;
1499
1500 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001501
1502 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001503 struct io_kiocb *link = list_first_entry(&req->link_list,
1504 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001505
Pavel Begunkov44932332019-12-05 16:16:35 +03001506 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001507 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001508
1509 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001510 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001511 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001512 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001513 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001514 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001515 }
Jens Axboe5d960722019-11-19 15:31:28 -07001516 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001517 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001518
1519 io_commit_cqring(ctx);
1520 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1521 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001522}
1523
Jens Axboe4d7dd462019-11-20 13:03:52 -07001524static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001525{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001526 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001527 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001528
Jens Axboe9e645e112019-05-10 16:07:28 -06001529 /*
1530 * If LINK is set, we have dependent requests in this chain. If we
1531 * didn't fail this request, queue the first one up, moving any other
1532 * dependencies to the next request. In case of failure, fail the rest
1533 * of the chain.
1534 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001535 if (req->flags & REQ_F_FAIL_LINK) {
1536 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001537 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1538 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001539 struct io_ring_ctx *ctx = req->ctx;
1540 unsigned long flags;
1541
1542 /*
1543 * If this is a timeout link, we could be racing with the
1544 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001545 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001546 */
1547 spin_lock_irqsave(&ctx->completion_lock, flags);
1548 io_req_link_next(req, nxt);
1549 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1550 } else {
1551 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001552 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001553}
Jens Axboe9e645e112019-05-10 16:07:28 -06001554
Jackie Liuc69f8db2019-11-09 11:00:08 +08001555static void io_free_req(struct io_kiocb *req)
1556{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001557 struct io_kiocb *nxt = NULL;
1558
1559 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001560 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001561
1562 if (nxt)
1563 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001564}
1565
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001566static void io_link_work_cb(struct io_wq_work **workptr)
1567{
1568 struct io_wq_work *work = *workptr;
1569 struct io_kiocb *link = work->data;
1570
1571 io_queue_linked_timeout(link);
1572 io_wq_submit_work(workptr);
1573}
1574
1575static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1576{
1577 struct io_kiocb *link;
1578
1579 *workptr = &nxt->work;
1580 link = io_prep_linked_timeout(nxt);
1581 if (link) {
1582 nxt->work.func = io_link_work_cb;
1583 nxt->work.data = link;
1584 }
1585}
1586
Jens Axboeba816ad2019-09-28 11:36:45 -06001587/*
1588 * Drop reference to request, return next in chain (if there is one) if this
1589 * was the last reference to this request.
1590 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001591__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001592static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001593{
Jens Axboe2a44f462020-02-25 13:25:41 -07001594 if (refcount_dec_and_test(&req->refs)) {
1595 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001596 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001597 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001598}
1599
Jens Axboe2b188cc2019-01-07 10:46:33 -07001600static void io_put_req(struct io_kiocb *req)
1601{
Jens Axboedef596e2019-01-09 08:59:42 -07001602 if (refcount_dec_and_test(&req->refs))
1603 io_free_req(req);
1604}
1605
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001606static void io_steal_work(struct io_kiocb *req,
1607 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001608{
1609 /*
1610 * It's in an io-wq worker, so there always should be at least
1611 * one reference, which will be dropped in io_put_work() just
1612 * after the current handler returns.
1613 *
1614 * It also means, that if the counter dropped to 1, then there is
1615 * no asynchronous users left, so it's safe to steal the next work.
1616 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001617 if (refcount_read(&req->refs) == 1) {
1618 struct io_kiocb *nxt = NULL;
1619
1620 io_req_find_next(req, &nxt);
1621 if (nxt)
1622 io_wq_assign_next(workptr, nxt);
1623 }
1624}
1625
Jens Axboe978db572019-11-14 22:39:04 -07001626/*
1627 * Must only be used if we don't need to care about links, usually from
1628 * within the completion handling itself.
1629 */
1630static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001631{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001632 /* drop both submit and complete references */
1633 if (refcount_sub_and_test(2, &req->refs))
1634 __io_free_req(req);
1635}
1636
Jens Axboe978db572019-11-14 22:39:04 -07001637static void io_double_put_req(struct io_kiocb *req)
1638{
1639 /* drop both submit and complete references */
1640 if (refcount_sub_and_test(2, &req->refs))
1641 io_free_req(req);
1642}
1643
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001644static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001645{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001646 struct io_rings *rings = ctx->rings;
1647
Jens Axboead3eb2c2019-12-18 17:12:20 -07001648 if (test_bit(0, &ctx->cq_check_overflow)) {
1649 /*
1650 * noflush == true is from the waitqueue handler, just ensure
1651 * we wake up the task, and the next invocation will flush the
1652 * entries. We cannot safely to it from here.
1653 */
1654 if (noflush && !list_empty(&ctx->cq_overflow_list))
1655 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001656
Jens Axboead3eb2c2019-12-18 17:12:20 -07001657 io_cqring_overflow_flush(ctx, false);
1658 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001659
Jens Axboea3a0e432019-08-20 11:03:11 -06001660 /* See comment at the top of this file */
1661 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001662 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001663}
1664
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001665static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1666{
1667 struct io_rings *rings = ctx->rings;
1668
1669 /* make sure SQ entry isn't read before tail */
1670 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1671}
1672
Jens Axboe8237e042019-12-28 10:48:22 -07001673static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001674{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001675 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1676 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001677
Jens Axboec6ca97b302019-12-28 12:11:08 -07001678 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1679 rb->need_iter++;
1680
1681 rb->reqs[rb->to_free++] = req;
1682 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1683 io_free_req_many(req->ctx, rb);
1684 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001685}
1686
Jens Axboebcda7ba2020-02-23 16:42:51 -07001687static int io_put_kbuf(struct io_kiocb *req)
1688{
1689 struct io_buffer *kbuf = (struct io_buffer *) req->rw.addr;
1690 int cflags;
1691
1692 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1693 cflags |= IORING_CQE_F_BUFFER;
1694 req->rw.addr = 0;
1695 kfree(kbuf);
1696 return cflags;
1697}
1698
Jens Axboedef596e2019-01-09 08:59:42 -07001699/*
1700 * Find and free completed poll iocbs
1701 */
1702static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1703 struct list_head *done)
1704{
Jens Axboe8237e042019-12-28 10:48:22 -07001705 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001706 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001707
Jens Axboec6ca97b302019-12-28 12:11:08 -07001708 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001709 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001710 int cflags = 0;
1711
Jens Axboedef596e2019-01-09 08:59:42 -07001712 req = list_first_entry(done, struct io_kiocb, list);
1713 list_del(&req->list);
1714
Jens Axboebcda7ba2020-02-23 16:42:51 -07001715 if (req->flags & REQ_F_BUFFER_SELECTED)
1716 cflags = io_put_kbuf(req);
1717
1718 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001719 (*nr_events)++;
1720
Jens Axboe8237e042019-12-28 10:48:22 -07001721 if (refcount_dec_and_test(&req->refs) &&
1722 !io_req_multi_free(&rb, req))
1723 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001724 }
Jens Axboedef596e2019-01-09 08:59:42 -07001725
Jens Axboe09bb8392019-03-13 12:39:28 -06001726 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001727 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001728}
1729
1730static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1731 long min)
1732{
1733 struct io_kiocb *req, *tmp;
1734 LIST_HEAD(done);
1735 bool spin;
1736 int ret;
1737
1738 /*
1739 * Only spin for completions if we don't have multiple devices hanging
1740 * off our complete list, and we're under the requested amount.
1741 */
1742 spin = !ctx->poll_multi_file && *nr_events < min;
1743
1744 ret = 0;
1745 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001746 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001747
1748 /*
1749 * Move completed entries to our local list. If we find a
1750 * request that requires polling, break out and complete
1751 * the done list first, if we have entries there.
1752 */
1753 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1754 list_move_tail(&req->list, &done);
1755 continue;
1756 }
1757 if (!list_empty(&done))
1758 break;
1759
1760 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1761 if (ret < 0)
1762 break;
1763
1764 if (ret && spin)
1765 spin = false;
1766 ret = 0;
1767 }
1768
1769 if (!list_empty(&done))
1770 io_iopoll_complete(ctx, nr_events, &done);
1771
1772 return ret;
1773}
1774
1775/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001776 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001777 * non-spinning poll check - we'll still enter the driver poll loop, but only
1778 * as a non-spinning completion check.
1779 */
1780static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1781 long min)
1782{
Jens Axboe08f54392019-08-21 22:19:11 -06001783 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001784 int ret;
1785
1786 ret = io_do_iopoll(ctx, nr_events, min);
1787 if (ret < 0)
1788 return ret;
1789 if (!min || *nr_events >= min)
1790 return 0;
1791 }
1792
1793 return 1;
1794}
1795
1796/*
1797 * We can't just wait for polled events to come to us, we have to actively
1798 * find and complete them.
1799 */
1800static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1801{
1802 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1803 return;
1804
1805 mutex_lock(&ctx->uring_lock);
1806 while (!list_empty(&ctx->poll_list)) {
1807 unsigned int nr_events = 0;
1808
1809 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001810
1811 /*
1812 * Ensure we allow local-to-the-cpu processing to take place,
1813 * in this case we need to ensure that we reap all events.
1814 */
1815 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001816 }
1817 mutex_unlock(&ctx->uring_lock);
1818}
1819
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001820static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1821 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001822{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001823 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001824
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001825 /*
1826 * We disallow the app entering submit/complete with polling, but we
1827 * still need to lock the ring to prevent racing with polled issue
1828 * that got punted to a workqueue.
1829 */
1830 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001831 do {
1832 int tmin = 0;
1833
Jens Axboe500f9fb2019-08-19 12:15:59 -06001834 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001835 * Don't enter poll loop if we already have events pending.
1836 * If we do, we can potentially be spinning for commands that
1837 * already triggered a CQE (eg in error).
1838 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001839 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001840 break;
1841
1842 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001843 * If a submit got punted to a workqueue, we can have the
1844 * application entering polling for a command before it gets
1845 * issued. That app will hold the uring_lock for the duration
1846 * of the poll right here, so we need to take a breather every
1847 * now and then to ensure that the issue has a chance to add
1848 * the poll to the issued list. Otherwise we can spin here
1849 * forever, while the workqueue is stuck trying to acquire the
1850 * very same mutex.
1851 */
1852 if (!(++iters & 7)) {
1853 mutex_unlock(&ctx->uring_lock);
1854 mutex_lock(&ctx->uring_lock);
1855 }
1856
Jens Axboedef596e2019-01-09 08:59:42 -07001857 if (*nr_events < min)
1858 tmin = min - *nr_events;
1859
1860 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1861 if (ret <= 0)
1862 break;
1863 ret = 0;
1864 } while (min && !*nr_events && !need_resched());
1865
Jens Axboe500f9fb2019-08-19 12:15:59 -06001866 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001867 return ret;
1868}
1869
Jens Axboe491381ce2019-10-17 09:20:46 -06001870static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001871{
Jens Axboe491381ce2019-10-17 09:20:46 -06001872 /*
1873 * Tell lockdep we inherited freeze protection from submission
1874 * thread.
1875 */
1876 if (req->flags & REQ_F_ISREG) {
1877 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001878
Jens Axboe491381ce2019-10-17 09:20:46 -06001879 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001880 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001881 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001882}
1883
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001884static inline void req_set_fail_links(struct io_kiocb *req)
1885{
1886 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1887 req->flags |= REQ_F_FAIL_LINK;
1888}
1889
Jens Axboeba816ad2019-09-28 11:36:45 -06001890static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891{
Jens Axboe9adbd452019-12-20 08:45:55 -07001892 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001893 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001894
Jens Axboe491381ce2019-10-17 09:20:46 -06001895 if (kiocb->ki_flags & IOCB_WRITE)
1896 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001897
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001898 if (res != req->result)
1899 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001900 if (req->flags & REQ_F_BUFFER_SELECTED)
1901 cflags = io_put_kbuf(req);
1902 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001903}
1904
1905static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1906{
Jens Axboe9adbd452019-12-20 08:45:55 -07001907 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001908
1909 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001910 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911}
1912
Jens Axboedef596e2019-01-09 08:59:42 -07001913static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1914{
Jens Axboe9adbd452019-12-20 08:45:55 -07001915 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001916
Jens Axboe491381ce2019-10-17 09:20:46 -06001917 if (kiocb->ki_flags & IOCB_WRITE)
1918 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001919
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001920 if (res != req->result)
1921 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001922 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001923 if (res != -EAGAIN)
1924 req->flags |= REQ_F_IOPOLL_COMPLETED;
1925}
1926
1927/*
1928 * After the iocb has been issued, it's safe to be found on the poll list.
1929 * Adding the kiocb to the list AFTER submission ensures that we don't
1930 * find it from a io_iopoll_getevents() thread before the issuer is done
1931 * accessing the kiocb cookie.
1932 */
1933static void io_iopoll_req_issued(struct io_kiocb *req)
1934{
1935 struct io_ring_ctx *ctx = req->ctx;
1936
1937 /*
1938 * Track whether we have multiple files in our lists. This will impact
1939 * how we do polling eventually, not spinning if we're on potentially
1940 * different devices.
1941 */
1942 if (list_empty(&ctx->poll_list)) {
1943 ctx->poll_multi_file = false;
1944 } else if (!ctx->poll_multi_file) {
1945 struct io_kiocb *list_req;
1946
1947 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1948 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001949 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001950 ctx->poll_multi_file = true;
1951 }
1952
1953 /*
1954 * For fast devices, IO may have already completed. If it has, add
1955 * it to the front so we find it first.
1956 */
1957 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1958 list_add(&req->list, &ctx->poll_list);
1959 else
1960 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001961
1962 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1963 wq_has_sleeper(&ctx->sqo_wait))
1964 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001965}
1966
Jens Axboe3d6770f2019-04-13 11:50:54 -06001967static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001968{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001969 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001970 int diff = state->has_refs - state->used_refs;
1971
1972 if (diff)
1973 fput_many(state->file, diff);
1974 state->file = NULL;
1975 }
1976}
1977
1978/*
1979 * Get as many references to a file as we have IOs left in this submission,
1980 * assuming most submissions are for one file, or at least that each file
1981 * has more than one submission.
1982 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001983static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001984{
1985 if (!state)
1986 return fget(fd);
1987
1988 if (state->file) {
1989 if (state->fd == fd) {
1990 state->used_refs++;
1991 state->ios_left--;
1992 return state->file;
1993 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001994 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001995 }
1996 state->file = fget_many(fd, state->ios_left);
1997 if (!state->file)
1998 return NULL;
1999
2000 state->fd = fd;
2001 state->has_refs = state->ios_left;
2002 state->used_refs = 1;
2003 state->ios_left--;
2004 return state->file;
2005}
2006
Jens Axboe2b188cc2019-01-07 10:46:33 -07002007/*
2008 * If we tracked the file through the SCM inflight mechanism, we could support
2009 * any file. For now, just ensure that anything potentially problematic is done
2010 * inline.
2011 */
2012static bool io_file_supports_async(struct file *file)
2013{
2014 umode_t mode = file_inode(file)->i_mode;
2015
Jens Axboe10d59342019-12-09 20:16:22 -07002016 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002017 return true;
2018 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2019 return true;
2020
2021 return false;
2022}
2023
Jens Axboe3529d8c2019-12-19 18:24:38 -07002024static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2025 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002026{
Jens Axboedef596e2019-01-09 08:59:42 -07002027 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002028 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002029 unsigned ioprio;
2030 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002031
Jens Axboe491381ce2019-10-17 09:20:46 -06002032 if (S_ISREG(file_inode(req->file)->i_mode))
2033 req->flags |= REQ_F_ISREG;
2034
Jens Axboe2b188cc2019-01-07 10:46:33 -07002035 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002036 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2037 req->flags |= REQ_F_CUR_POS;
2038 kiocb->ki_pos = req->file->f_pos;
2039 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002040 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002041 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2042 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2043 if (unlikely(ret))
2044 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002045
2046 ioprio = READ_ONCE(sqe->ioprio);
2047 if (ioprio) {
2048 ret = ioprio_check_cap(ioprio);
2049 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002050 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002051
2052 kiocb->ki_ioprio = ioprio;
2053 } else
2054 kiocb->ki_ioprio = get_current_ioprio();
2055
Stefan Bühler8449eed2019-04-27 20:34:19 +02002056 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002057 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2058 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002059 req->flags |= REQ_F_NOWAIT;
2060
2061 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002062 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002063
Jens Axboedef596e2019-01-09 08:59:42 -07002064 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002065 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2066 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002067 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002068
Jens Axboedef596e2019-01-09 08:59:42 -07002069 kiocb->ki_flags |= IOCB_HIPRI;
2070 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002071 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002072 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002073 if (kiocb->ki_flags & IOCB_HIPRI)
2074 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002075 kiocb->ki_complete = io_complete_rw;
2076 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002077
Jens Axboe3529d8c2019-12-19 18:24:38 -07002078 req->rw.addr = READ_ONCE(sqe->addr);
2079 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002080 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002081 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002082 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002083 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002084}
2085
2086static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2087{
2088 switch (ret) {
2089 case -EIOCBQUEUED:
2090 break;
2091 case -ERESTARTSYS:
2092 case -ERESTARTNOINTR:
2093 case -ERESTARTNOHAND:
2094 case -ERESTART_RESTARTBLOCK:
2095 /*
2096 * We can't just restart the syscall, since previously
2097 * submitted sqes may already be in progress. Just fail this
2098 * IO with EINTR.
2099 */
2100 ret = -EINTR;
2101 /* fall through */
2102 default:
2103 kiocb->ki_complete(kiocb, ret, 0);
2104 }
2105}
2106
Pavel Begunkov014db002020-03-03 21:33:12 +03002107static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002108{
Jens Axboeba042912019-12-25 16:33:42 -07002109 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2110
2111 if (req->flags & REQ_F_CUR_POS)
2112 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002113 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002114 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002115 else
2116 io_rw_done(kiocb, ret);
2117}
2118
Jens Axboe9adbd452019-12-20 08:45:55 -07002119static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002120 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002121{
Jens Axboe9adbd452019-12-20 08:45:55 -07002122 struct io_ring_ctx *ctx = req->ctx;
2123 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002124 struct io_mapped_ubuf *imu;
2125 unsigned index, buf_index;
2126 size_t offset;
2127 u64 buf_addr;
2128
2129 /* attempt to use fixed buffers without having provided iovecs */
2130 if (unlikely(!ctx->user_bufs))
2131 return -EFAULT;
2132
Jens Axboe9adbd452019-12-20 08:45:55 -07002133 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002134 if (unlikely(buf_index >= ctx->nr_user_bufs))
2135 return -EFAULT;
2136
2137 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2138 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002139 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002140
2141 /* overflow */
2142 if (buf_addr + len < buf_addr)
2143 return -EFAULT;
2144 /* not inside the mapped region */
2145 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2146 return -EFAULT;
2147
2148 /*
2149 * May not be a start of buffer, set size appropriately
2150 * and advance us to the beginning.
2151 */
2152 offset = buf_addr - imu->ubuf;
2153 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002154
2155 if (offset) {
2156 /*
2157 * Don't use iov_iter_advance() here, as it's really slow for
2158 * using the latter parts of a big fixed buffer - it iterates
2159 * over each segment manually. We can cheat a bit here, because
2160 * we know that:
2161 *
2162 * 1) it's a BVEC iter, we set it up
2163 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2164 * first and last bvec
2165 *
2166 * So just find our index, and adjust the iterator afterwards.
2167 * If the offset is within the first bvec (or the whole first
2168 * bvec, just use iov_iter_advance(). This makes it easier
2169 * since we can just skip the first segment, which may not
2170 * be PAGE_SIZE aligned.
2171 */
2172 const struct bio_vec *bvec = imu->bvec;
2173
2174 if (offset <= bvec->bv_len) {
2175 iov_iter_advance(iter, offset);
2176 } else {
2177 unsigned long seg_skip;
2178
2179 /* skip first vec */
2180 offset -= bvec->bv_len;
2181 seg_skip = 1 + (offset >> PAGE_SHIFT);
2182
2183 iter->bvec = bvec + seg_skip;
2184 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002185 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002186 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002187 }
2188 }
2189
Jens Axboe5e559562019-11-13 16:12:46 -07002190 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002191}
2192
Jens Axboebcda7ba2020-02-23 16:42:51 -07002193static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2194{
2195 if (needs_lock)
2196 mutex_unlock(&ctx->uring_lock);
2197}
2198
2199static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2200{
2201 /*
2202 * "Normal" inline submissions always hold the uring_lock, since we
2203 * grab it from the system call. Same is true for the SQPOLL offload.
2204 * The only exception is when we've detached the request and issue it
2205 * from an async worker thread, grab the lock for that case.
2206 */
2207 if (needs_lock)
2208 mutex_lock(&ctx->uring_lock);
2209}
2210
2211static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2212 int bgid, struct io_buffer *kbuf,
2213 bool needs_lock)
2214{
2215 struct io_buffer *head;
2216
2217 if (req->flags & REQ_F_BUFFER_SELECTED)
2218 return kbuf;
2219
2220 io_ring_submit_lock(req->ctx, needs_lock);
2221
2222 lockdep_assert_held(&req->ctx->uring_lock);
2223
2224 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2225 if (head) {
2226 if (!list_empty(&head->list)) {
2227 kbuf = list_last_entry(&head->list, struct io_buffer,
2228 list);
2229 list_del(&kbuf->list);
2230 } else {
2231 kbuf = head;
2232 idr_remove(&req->ctx->io_buffer_idr, bgid);
2233 }
2234 if (*len > kbuf->len)
2235 *len = kbuf->len;
2236 } else {
2237 kbuf = ERR_PTR(-ENOBUFS);
2238 }
2239
2240 io_ring_submit_unlock(req->ctx, needs_lock);
2241
2242 return kbuf;
2243}
2244
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002245static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002246 struct iovec **iovec, struct iov_iter *iter,
2247 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002248{
Jens Axboe9adbd452019-12-20 08:45:55 -07002249 void __user *buf = u64_to_user_ptr(req->rw.addr);
2250 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002251 u8 opcode;
2252
Jens Axboed625c6e2019-12-17 19:53:05 -07002253 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002254 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002255 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002256 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002257 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002258
Jens Axboebcda7ba2020-02-23 16:42:51 -07002259 /* buffer index only valid with fixed read/write, or buffer select */
2260 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002261 return -EINVAL;
2262
Jens Axboe3a6820f2019-12-22 15:19:35 -07002263 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2264 ssize_t ret;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002265
2266 if (req->flags & REQ_F_BUFFER_SELECT) {
2267 struct io_buffer *kbuf = (struct io_buffer *) req->rw.addr;
2268 int bgid;
2269
2270 bgid = (int) (unsigned long) req->rw.kiocb.private;
2271 kbuf = io_buffer_select(req, &sqe_len, bgid, kbuf,
2272 needs_lock);
2273 if (IS_ERR(kbuf)) {
2274 *iovec = NULL;
2275 return PTR_ERR(kbuf);
2276 }
2277 req->rw.addr = (u64) kbuf;
2278 req->flags |= REQ_F_BUFFER_SELECTED;
2279 buf = u64_to_user_ptr(kbuf->addr);
2280 }
2281
Jens Axboe3a6820f2019-12-22 15:19:35 -07002282 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2283 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002284 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002285 }
2286
Jens Axboef67676d2019-12-02 11:03:47 -07002287 if (req->io) {
2288 struct io_async_rw *iorw = &req->io->rw;
2289
2290 *iovec = iorw->iov;
2291 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2292 if (iorw->iov == iorw->fast_iov)
2293 *iovec = NULL;
2294 return iorw->size;
2295 }
2296
Jens Axboe2b188cc2019-01-07 10:46:33 -07002297#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002298 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002299 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2300 iovec, iter);
2301#endif
2302
2303 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2304}
2305
Jens Axboe32960612019-09-23 11:05:34 -06002306/*
2307 * For files that don't have ->read_iter() and ->write_iter(), handle them
2308 * by looping over ->read() or ->write() manually.
2309 */
2310static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2311 struct iov_iter *iter)
2312{
2313 ssize_t ret = 0;
2314
2315 /*
2316 * Don't support polled IO through this interface, and we can't
2317 * support non-blocking either. For the latter, this just causes
2318 * the kiocb to be handled from an async context.
2319 */
2320 if (kiocb->ki_flags & IOCB_HIPRI)
2321 return -EOPNOTSUPP;
2322 if (kiocb->ki_flags & IOCB_NOWAIT)
2323 return -EAGAIN;
2324
2325 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002326 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002327 ssize_t nr;
2328
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002329 if (!iov_iter_is_bvec(iter)) {
2330 iovec = iov_iter_iovec(iter);
2331 } else {
2332 /* fixed buffers import bvec */
2333 iovec.iov_base = kmap(iter->bvec->bv_page)
2334 + iter->iov_offset;
2335 iovec.iov_len = min(iter->count,
2336 iter->bvec->bv_len - iter->iov_offset);
2337 }
2338
Jens Axboe32960612019-09-23 11:05:34 -06002339 if (rw == READ) {
2340 nr = file->f_op->read(file, iovec.iov_base,
2341 iovec.iov_len, &kiocb->ki_pos);
2342 } else {
2343 nr = file->f_op->write(file, iovec.iov_base,
2344 iovec.iov_len, &kiocb->ki_pos);
2345 }
2346
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002347 if (iov_iter_is_bvec(iter))
2348 kunmap(iter->bvec->bv_page);
2349
Jens Axboe32960612019-09-23 11:05:34 -06002350 if (nr < 0) {
2351 if (!ret)
2352 ret = nr;
2353 break;
2354 }
2355 ret += nr;
2356 if (nr != iovec.iov_len)
2357 break;
2358 iov_iter_advance(iter, nr);
2359 }
2360
2361 return ret;
2362}
2363
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002364static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002365 struct iovec *iovec, struct iovec *fast_iov,
2366 struct iov_iter *iter)
2367{
2368 req->io->rw.nr_segs = iter->nr_segs;
2369 req->io->rw.size = io_size;
2370 req->io->rw.iov = iovec;
2371 if (!req->io->rw.iov) {
2372 req->io->rw.iov = req->io->rw.fast_iov;
2373 memcpy(req->io->rw.iov, fast_iov,
2374 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002375 } else {
2376 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002377 }
2378}
2379
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002380static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002381{
Jens Axboed3656342019-12-18 09:50:26 -07002382 if (!io_op_defs[req->opcode].async_ctx)
2383 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002384 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002385 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002386}
2387
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002388static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2389 struct iovec *iovec, struct iovec *fast_iov,
2390 struct iov_iter *iter)
2391{
Jens Axboe980ad262020-01-24 23:08:54 -07002392 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002393 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002394 if (!req->io) {
2395 if (io_alloc_async_ctx(req))
2396 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002397
Jens Axboe5d204bc2020-01-31 12:06:52 -07002398 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2399 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002400 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002401}
2402
Jens Axboe3529d8c2019-12-19 18:24:38 -07002403static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2404 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002405{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002406 struct io_async_ctx *io;
2407 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002408 ssize_t ret;
2409
Jens Axboe3529d8c2019-12-19 18:24:38 -07002410 ret = io_prep_rw(req, sqe, force_nonblock);
2411 if (ret)
2412 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002413
Jens Axboe3529d8c2019-12-19 18:24:38 -07002414 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2415 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002416
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002417 /* either don't need iovec imported or already have it */
2418 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002419 return 0;
2420
2421 io = req->io;
2422 io->rw.iov = io->rw.fast_iov;
2423 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002424 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002425 req->io = io;
2426 if (ret < 0)
2427 return ret;
2428
2429 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2430 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002431}
2432
Pavel Begunkov014db002020-03-03 21:33:12 +03002433static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002434{
2435 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002436 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002437 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002438 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002439 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002440
Jens Axboebcda7ba2020-02-23 16:42:51 -07002441 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002442 if (ret < 0)
2443 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002444
Jens Axboefd6c2e42019-12-18 12:19:41 -07002445 /* Ensure we clear previously set non-block flag */
2446 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002447 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002448
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002449 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002450 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002451 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002452 req->result = io_size;
2453
2454 /*
2455 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2456 * we know to async punt it even if it was opened O_NONBLOCK
2457 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002458 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002459 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002460
Jens Axboe31b51512019-01-18 22:56:34 -07002461 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002462 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002463 if (!ret) {
2464 ssize_t ret2;
2465
Jens Axboe9adbd452019-12-20 08:45:55 -07002466 if (req->file->f_op->read_iter)
2467 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002468 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002469 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002470
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002471 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002472 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002473 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002474 } else {
2475copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002476 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002477 inline_vecs, &iter);
2478 if (ret)
2479 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002480 /* any defer here is final, must blocking retry */
2481 if (!(req->flags & REQ_F_NOWAIT))
2482 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002483 return -EAGAIN;
2484 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002485 }
Jens Axboef67676d2019-12-02 11:03:47 -07002486out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002487 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002488 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002489 return ret;
2490}
2491
Jens Axboe3529d8c2019-12-19 18:24:38 -07002492static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2493 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002494{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002495 struct io_async_ctx *io;
2496 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002497 ssize_t ret;
2498
Jens Axboe3529d8c2019-12-19 18:24:38 -07002499 ret = io_prep_rw(req, sqe, force_nonblock);
2500 if (ret)
2501 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002502
Jens Axboe3529d8c2019-12-19 18:24:38 -07002503 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2504 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002505
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002506 /* either don't need iovec imported or already have it */
2507 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002508 return 0;
2509
2510 io = req->io;
2511 io->rw.iov = io->rw.fast_iov;
2512 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002513 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002514 req->io = io;
2515 if (ret < 0)
2516 return ret;
2517
2518 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2519 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002520}
2521
Pavel Begunkov014db002020-03-03 21:33:12 +03002522static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523{
2524 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002525 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002526 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002527 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002528 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002529
Jens Axboebcda7ba2020-02-23 16:42:51 -07002530 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002531 if (ret < 0)
2532 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002533
Jens Axboefd6c2e42019-12-18 12:19:41 -07002534 /* Ensure we clear previously set non-block flag */
2535 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002536 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002537
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002538 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002539 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002540 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002541 req->result = io_size;
2542
2543 /*
2544 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2545 * we know to async punt it even if it was opened O_NONBLOCK
2546 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002547 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002548 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002549
Jens Axboe10d59342019-12-09 20:16:22 -07002550 /* file path doesn't support NOWAIT for non-direct_IO */
2551 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2552 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002553 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002554
Jens Axboe31b51512019-01-18 22:56:34 -07002555 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002556 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002557 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002558 ssize_t ret2;
2559
Jens Axboe2b188cc2019-01-07 10:46:33 -07002560 /*
2561 * Open-code file_start_write here to grab freeze protection,
2562 * which will be released by another thread in
2563 * io_complete_rw(). Fool lockdep by telling it the lock got
2564 * released so that it doesn't complain about the held lock when
2565 * we return to userspace.
2566 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002567 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002568 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002569 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002570 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002571 SB_FREEZE_WRITE);
2572 }
2573 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002574
Jens Axboe9adbd452019-12-20 08:45:55 -07002575 if (req->file->f_op->write_iter)
2576 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002577 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002578 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002579 /*
2580 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2581 * retry them without IOCB_NOWAIT.
2582 */
2583 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2584 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002585 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002586 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002587 } else {
2588copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002589 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002590 inline_vecs, &iter);
2591 if (ret)
2592 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002593 /* any defer here is final, must blocking retry */
2594 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002595 return -EAGAIN;
2596 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002597 }
Jens Axboe31b51512019-01-18 22:56:34 -07002598out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002599 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002600 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002601 return ret;
2602}
2603
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002604static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2605{
2606 struct io_splice* sp = &req->splice;
2607 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2608 int ret;
2609
2610 if (req->flags & REQ_F_NEED_CLEANUP)
2611 return 0;
2612
2613 sp->file_in = NULL;
2614 sp->off_in = READ_ONCE(sqe->splice_off_in);
2615 sp->off_out = READ_ONCE(sqe->off);
2616 sp->len = READ_ONCE(sqe->len);
2617 sp->flags = READ_ONCE(sqe->splice_flags);
2618
2619 if (unlikely(sp->flags & ~valid_flags))
2620 return -EINVAL;
2621
2622 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2623 (sp->flags & SPLICE_F_FD_IN_FIXED));
2624 if (ret)
2625 return ret;
2626 req->flags |= REQ_F_NEED_CLEANUP;
2627
2628 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2629 req->work.flags |= IO_WQ_WORK_UNBOUND;
2630
2631 return 0;
2632}
2633
2634static bool io_splice_punt(struct file *file)
2635{
2636 if (get_pipe_info(file))
2637 return false;
2638 if (!io_file_supports_async(file))
2639 return true;
2640 return !(file->f_mode & O_NONBLOCK);
2641}
2642
Pavel Begunkov014db002020-03-03 21:33:12 +03002643static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002644{
2645 struct io_splice *sp = &req->splice;
2646 struct file *in = sp->file_in;
2647 struct file *out = sp->file_out;
2648 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2649 loff_t *poff_in, *poff_out;
2650 long ret;
2651
2652 if (force_nonblock) {
2653 if (io_splice_punt(in) || io_splice_punt(out))
2654 return -EAGAIN;
2655 flags |= SPLICE_F_NONBLOCK;
2656 }
2657
2658 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2659 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2660 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2661 if (force_nonblock && ret == -EAGAIN)
2662 return -EAGAIN;
2663
2664 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2665 req->flags &= ~REQ_F_NEED_CLEANUP;
2666
2667 io_cqring_add_event(req, ret);
2668 if (ret != sp->len)
2669 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002670 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002671 return 0;
2672}
2673
Jens Axboe2b188cc2019-01-07 10:46:33 -07002674/*
2675 * IORING_OP_NOP just posts a completion event, nothing else.
2676 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002677static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002678{
2679 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002680
Jens Axboedef596e2019-01-09 08:59:42 -07002681 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2682 return -EINVAL;
2683
Jens Axboe78e19bb2019-11-06 15:21:34 -07002684 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002685 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002686 return 0;
2687}
2688
Jens Axboe3529d8c2019-12-19 18:24:38 -07002689static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002690{
Jens Axboe6b063142019-01-10 22:13:58 -07002691 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002692
Jens Axboe09bb8392019-03-13 12:39:28 -06002693 if (!req->file)
2694 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002695
Jens Axboe6b063142019-01-10 22:13:58 -07002696 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002697 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002698 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002699 return -EINVAL;
2700
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002701 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2702 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2703 return -EINVAL;
2704
2705 req->sync.off = READ_ONCE(sqe->off);
2706 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002707 return 0;
2708}
2709
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002710static bool io_req_cancelled(struct io_kiocb *req)
2711{
2712 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2713 req_set_fail_links(req);
2714 io_cqring_add_event(req, -ECANCELED);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002715 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002716 return true;
2717 }
2718
2719 return false;
2720}
2721
Pavel Begunkov014db002020-03-03 21:33:12 +03002722static void __io_fsync(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002723{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002724 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002725 int ret;
2726
Jens Axboe9adbd452019-12-20 08:45:55 -07002727 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002728 end > 0 ? end : LLONG_MAX,
2729 req->sync.flags & IORING_FSYNC_DATASYNC);
2730 if (ret < 0)
2731 req_set_fail_links(req);
2732 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002733 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002734}
2735
2736static void io_fsync_finish(struct io_wq_work **workptr)
2737{
2738 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002739
2740 if (io_req_cancelled(req))
2741 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002742 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002743 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002744}
2745
Pavel Begunkov014db002020-03-03 21:33:12 +03002746static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002747{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002748 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002749 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002750 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002751 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002752 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002753 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002754 return 0;
2755}
2756
Pavel Begunkov014db002020-03-03 21:33:12 +03002757static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002758{
Jens Axboed63d1b52019-12-10 10:38:56 -07002759 int ret;
2760
2761 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2762 req->sync.len);
2763 if (ret < 0)
2764 req_set_fail_links(req);
2765 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002766 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002767}
2768
2769static void io_fallocate_finish(struct io_wq_work **workptr)
2770{
2771 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002772
Pavel Begunkov594506f2020-03-03 21:33:11 +03002773 if (io_req_cancelled(req))
2774 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002775 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002776 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002777}
2778
2779static int io_fallocate_prep(struct io_kiocb *req,
2780 const struct io_uring_sqe *sqe)
2781{
2782 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2783 return -EINVAL;
2784
2785 req->sync.off = READ_ONCE(sqe->off);
2786 req->sync.len = READ_ONCE(sqe->addr);
2787 req->sync.mode = READ_ONCE(sqe->len);
2788 return 0;
2789}
2790
Pavel Begunkov014db002020-03-03 21:33:12 +03002791static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002792{
Jens Axboed63d1b52019-12-10 10:38:56 -07002793 /* fallocate always requiring blocking context */
2794 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002795 req->work.func = io_fallocate_finish;
2796 return -EAGAIN;
2797 }
2798
Pavel Begunkov014db002020-03-03 21:33:12 +03002799 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002800 return 0;
2801}
2802
Jens Axboe15b71ab2019-12-11 11:20:36 -07002803static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2804{
Jens Axboef8748882020-01-08 17:47:02 -07002805 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002806 int ret;
2807
2808 if (sqe->ioprio || sqe->buf_index)
2809 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002810 if (sqe->flags & IOSQE_FIXED_FILE)
2811 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002812 if (req->flags & REQ_F_NEED_CLEANUP)
2813 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002814
2815 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002816 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002817 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002818 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002819
Jens Axboef8748882020-01-08 17:47:02 -07002820 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002821 if (IS_ERR(req->open.filename)) {
2822 ret = PTR_ERR(req->open.filename);
2823 req->open.filename = NULL;
2824 return ret;
2825 }
2826
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002827 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002828 return 0;
2829}
2830
Jens Axboecebdb982020-01-08 17:59:24 -07002831static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2832{
2833 struct open_how __user *how;
2834 const char __user *fname;
2835 size_t len;
2836 int ret;
2837
2838 if (sqe->ioprio || sqe->buf_index)
2839 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002840 if (sqe->flags & IOSQE_FIXED_FILE)
2841 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002842 if (req->flags & REQ_F_NEED_CLEANUP)
2843 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002844
2845 req->open.dfd = READ_ONCE(sqe->fd);
2846 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2847 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2848 len = READ_ONCE(sqe->len);
2849
2850 if (len < OPEN_HOW_SIZE_VER0)
2851 return -EINVAL;
2852
2853 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2854 len);
2855 if (ret)
2856 return ret;
2857
2858 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2859 req->open.how.flags |= O_LARGEFILE;
2860
2861 req->open.filename = getname(fname);
2862 if (IS_ERR(req->open.filename)) {
2863 ret = PTR_ERR(req->open.filename);
2864 req->open.filename = NULL;
2865 return ret;
2866 }
2867
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002868 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002869 return 0;
2870}
2871
Pavel Begunkov014db002020-03-03 21:33:12 +03002872static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002873{
2874 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002875 struct file *file;
2876 int ret;
2877
Jens Axboef86cd202020-01-29 13:46:44 -07002878 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002879 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002880
Jens Axboecebdb982020-01-08 17:59:24 -07002881 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002882 if (ret)
2883 goto err;
2884
Jens Axboecebdb982020-01-08 17:59:24 -07002885 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002886 if (ret < 0)
2887 goto err;
2888
2889 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2890 if (IS_ERR(file)) {
2891 put_unused_fd(ret);
2892 ret = PTR_ERR(file);
2893 } else {
2894 fsnotify_open(file);
2895 fd_install(ret, file);
2896 }
2897err:
2898 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002899 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002900 if (ret < 0)
2901 req_set_fail_links(req);
2902 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002903 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002904 return 0;
2905}
2906
Pavel Begunkov014db002020-03-03 21:33:12 +03002907static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07002908{
2909 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03002910 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07002911}
2912
Jens Axboeddf0322d2020-02-23 16:41:33 -07002913static int io_provide_buffers_prep(struct io_kiocb *req,
2914 const struct io_uring_sqe *sqe)
2915{
2916 struct io_provide_buf *p = &req->pbuf;
2917 u64 tmp;
2918
2919 if (sqe->ioprio || sqe->rw_flags)
2920 return -EINVAL;
2921
2922 tmp = READ_ONCE(sqe->fd);
2923 if (!tmp || tmp > USHRT_MAX)
2924 return -E2BIG;
2925 p->nbufs = tmp;
2926 p->addr = READ_ONCE(sqe->addr);
2927 p->len = READ_ONCE(sqe->len);
2928
2929 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
2930 return -EFAULT;
2931
2932 p->bgid = READ_ONCE(sqe->buf_group);
2933 tmp = READ_ONCE(sqe->off);
2934 if (tmp > USHRT_MAX)
2935 return -E2BIG;
2936 p->bid = tmp;
2937 return 0;
2938}
2939
2940static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
2941{
2942 struct io_buffer *buf;
2943 u64 addr = pbuf->addr;
2944 int i, bid = pbuf->bid;
2945
2946 for (i = 0; i < pbuf->nbufs; i++) {
2947 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
2948 if (!buf)
2949 break;
2950
2951 buf->addr = addr;
2952 buf->len = pbuf->len;
2953 buf->bid = bid;
2954 addr += pbuf->len;
2955 bid++;
2956 if (!*head) {
2957 INIT_LIST_HEAD(&buf->list);
2958 *head = buf;
2959 } else {
2960 list_add_tail(&buf->list, &(*head)->list);
2961 }
2962 }
2963
2964 return i ? i : -ENOMEM;
2965}
2966
Jens Axboeddf0322d2020-02-23 16:41:33 -07002967static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
2968{
2969 struct io_provide_buf *p = &req->pbuf;
2970 struct io_ring_ctx *ctx = req->ctx;
2971 struct io_buffer *head, *list;
2972 int ret = 0;
2973
2974 io_ring_submit_lock(ctx, !force_nonblock);
2975
2976 lockdep_assert_held(&ctx->uring_lock);
2977
2978 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
2979
2980 ret = io_add_buffers(p, &head);
2981 if (ret < 0)
2982 goto out;
2983
2984 if (!list) {
2985 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
2986 GFP_KERNEL);
2987 if (ret < 0) {
2988 while (!list_empty(&head->list)) {
2989 struct io_buffer *buf;
2990
2991 buf = list_first_entry(&head->list,
2992 struct io_buffer, list);
2993 list_del(&buf->list);
2994 kfree(buf);
2995 }
2996 kfree(head);
2997 goto out;
2998 }
2999 }
3000out:
3001 io_ring_submit_unlock(ctx, !force_nonblock);
3002 if (ret < 0)
3003 req_set_fail_links(req);
3004 io_cqring_add_event(req, ret);
3005 io_put_req(req);
3006 return 0;
3007}
3008
Jens Axboe3e4827b2020-01-08 15:18:09 -07003009static int io_epoll_ctl_prep(struct io_kiocb *req,
3010 const struct io_uring_sqe *sqe)
3011{
3012#if defined(CONFIG_EPOLL)
3013 if (sqe->ioprio || sqe->buf_index)
3014 return -EINVAL;
3015
3016 req->epoll.epfd = READ_ONCE(sqe->fd);
3017 req->epoll.op = READ_ONCE(sqe->len);
3018 req->epoll.fd = READ_ONCE(sqe->off);
3019
3020 if (ep_op_has_event(req->epoll.op)) {
3021 struct epoll_event __user *ev;
3022
3023 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3024 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3025 return -EFAULT;
3026 }
3027
3028 return 0;
3029#else
3030 return -EOPNOTSUPP;
3031#endif
3032}
3033
Pavel Begunkov014db002020-03-03 21:33:12 +03003034static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003035{
3036#if defined(CONFIG_EPOLL)
3037 struct io_epoll *ie = &req->epoll;
3038 int ret;
3039
3040 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3041 if (force_nonblock && ret == -EAGAIN)
3042 return -EAGAIN;
3043
3044 if (ret < 0)
3045 req_set_fail_links(req);
3046 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003047 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003048 return 0;
3049#else
3050 return -EOPNOTSUPP;
3051#endif
3052}
3053
Jens Axboec1ca7572019-12-25 22:18:28 -07003054static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3055{
3056#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3057 if (sqe->ioprio || sqe->buf_index || sqe->off)
3058 return -EINVAL;
3059
3060 req->madvise.addr = READ_ONCE(sqe->addr);
3061 req->madvise.len = READ_ONCE(sqe->len);
3062 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3063 return 0;
3064#else
3065 return -EOPNOTSUPP;
3066#endif
3067}
3068
Pavel Begunkov014db002020-03-03 21:33:12 +03003069static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003070{
3071#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3072 struct io_madvise *ma = &req->madvise;
3073 int ret;
3074
3075 if (force_nonblock)
3076 return -EAGAIN;
3077
3078 ret = do_madvise(ma->addr, ma->len, ma->advice);
3079 if (ret < 0)
3080 req_set_fail_links(req);
3081 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003082 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003083 return 0;
3084#else
3085 return -EOPNOTSUPP;
3086#endif
3087}
3088
Jens Axboe4840e412019-12-25 22:03:45 -07003089static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3090{
3091 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3092 return -EINVAL;
3093
3094 req->fadvise.offset = READ_ONCE(sqe->off);
3095 req->fadvise.len = READ_ONCE(sqe->len);
3096 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3097 return 0;
3098}
3099
Pavel Begunkov014db002020-03-03 21:33:12 +03003100static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003101{
3102 struct io_fadvise *fa = &req->fadvise;
3103 int ret;
3104
Jens Axboe3e694262020-02-01 09:22:49 -07003105 if (force_nonblock) {
3106 switch (fa->advice) {
3107 case POSIX_FADV_NORMAL:
3108 case POSIX_FADV_RANDOM:
3109 case POSIX_FADV_SEQUENTIAL:
3110 break;
3111 default:
3112 return -EAGAIN;
3113 }
3114 }
Jens Axboe4840e412019-12-25 22:03:45 -07003115
3116 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3117 if (ret < 0)
3118 req_set_fail_links(req);
3119 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003120 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003121 return 0;
3122}
3123
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003124static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3125{
Jens Axboef8748882020-01-08 17:47:02 -07003126 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003127 unsigned lookup_flags;
3128 int ret;
3129
3130 if (sqe->ioprio || sqe->buf_index)
3131 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07003132 if (sqe->flags & IOSQE_FIXED_FILE)
3133 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003134 if (req->flags & REQ_F_NEED_CLEANUP)
3135 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003136
3137 req->open.dfd = READ_ONCE(sqe->fd);
3138 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003139 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003140 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003141 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003142
Jens Axboec12cedf2020-01-08 17:41:21 -07003143 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003144 return -EINVAL;
3145
Jens Axboef8748882020-01-08 17:47:02 -07003146 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003147 if (IS_ERR(req->open.filename)) {
3148 ret = PTR_ERR(req->open.filename);
3149 req->open.filename = NULL;
3150 return ret;
3151 }
3152
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003153 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003154 return 0;
3155}
3156
Pavel Begunkov014db002020-03-03 21:33:12 +03003157static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003158{
3159 struct io_open *ctx = &req->open;
3160 unsigned lookup_flags;
3161 struct path path;
3162 struct kstat stat;
3163 int ret;
3164
3165 if (force_nonblock)
3166 return -EAGAIN;
3167
Jens Axboec12cedf2020-01-08 17:41:21 -07003168 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003169 return -EINVAL;
3170
3171retry:
3172 /* filename_lookup() drops it, keep a reference */
3173 ctx->filename->refcnt++;
3174
3175 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3176 NULL);
3177 if (ret)
3178 goto err;
3179
Jens Axboec12cedf2020-01-08 17:41:21 -07003180 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003181 path_put(&path);
3182 if (retry_estale(ret, lookup_flags)) {
3183 lookup_flags |= LOOKUP_REVAL;
3184 goto retry;
3185 }
3186 if (!ret)
3187 ret = cp_statx(&stat, ctx->buffer);
3188err:
3189 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003190 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003191 if (ret < 0)
3192 req_set_fail_links(req);
3193 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003194 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003195 return 0;
3196}
3197
Jens Axboeb5dba592019-12-11 14:02:38 -07003198static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3199{
3200 /*
3201 * If we queue this for async, it must not be cancellable. That would
3202 * leave the 'file' in an undeterminate state.
3203 */
3204 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3205
3206 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3207 sqe->rw_flags || sqe->buf_index)
3208 return -EINVAL;
3209 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003210 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003211
3212 req->close.fd = READ_ONCE(sqe->fd);
3213 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003214 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003215 return -EBADF;
3216
3217 return 0;
3218}
3219
Pavel Begunkova93b3332020-02-08 14:04:34 +03003220/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003221static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003222{
3223 int ret;
3224
3225 ret = filp_close(req->close.put_file, req->work.files);
3226 if (ret < 0)
3227 req_set_fail_links(req);
3228 io_cqring_add_event(req, ret);
3229 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003230 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003231}
3232
Jens Axboeb5dba592019-12-11 14:02:38 -07003233static void io_close_finish(struct io_wq_work **workptr)
3234{
3235 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003236
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003237 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003238 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003239 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003240}
3241
Pavel Begunkov014db002020-03-03 21:33:12 +03003242static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003243{
3244 int ret;
3245
3246 req->close.put_file = NULL;
3247 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3248 if (ret < 0)
3249 return ret;
3250
3251 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003252 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003253 /* submission ref will be dropped, take it for async */
3254 refcount_inc(&req->refs);
3255
Pavel Begunkova2100672020-03-02 23:45:16 +03003256 req->work.func = io_close_finish;
3257 /*
3258 * Do manual async queue here to avoid grabbing files - we don't
3259 * need the files, and it'll cause io_close_finish() to close
3260 * the file again and cause a double CQE entry for this request
3261 */
3262 io_queue_async_work(req);
3263 return 0;
3264 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003265
3266 /*
3267 * No ->flush(), safely close from here and just punt the
3268 * fput() to async context.
3269 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003270 __io_close_finish(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003271 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003272}
3273
Jens Axboe3529d8c2019-12-19 18:24:38 -07003274static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003275{
3276 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003277
3278 if (!req->file)
3279 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003280
3281 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3282 return -EINVAL;
3283 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3284 return -EINVAL;
3285
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003286 req->sync.off = READ_ONCE(sqe->off);
3287 req->sync.len = READ_ONCE(sqe->len);
3288 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003289 return 0;
3290}
3291
Pavel Begunkov014db002020-03-03 21:33:12 +03003292static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003293{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003294 int ret;
3295
Jens Axboe9adbd452019-12-20 08:45:55 -07003296 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003297 req->sync.flags);
3298 if (ret < 0)
3299 req_set_fail_links(req);
3300 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003301 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003302}
3303
3304
3305static void io_sync_file_range_finish(struct io_wq_work **workptr)
3306{
3307 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3308 struct io_kiocb *nxt = NULL;
3309
3310 if (io_req_cancelled(req))
3311 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003312 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003313 io_put_req(req); /* put submission ref */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003314 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003315 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003316}
3317
Pavel Begunkov014db002020-03-03 21:33:12 +03003318static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003319{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003320 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003321 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003322 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003323 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003324 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003325
Pavel Begunkov014db002020-03-03 21:33:12 +03003326 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003327 return 0;
3328}
3329
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003330static int io_setup_async_msg(struct io_kiocb *req,
3331 struct io_async_msghdr *kmsg)
3332{
3333 if (req->io)
3334 return -EAGAIN;
3335 if (io_alloc_async_ctx(req)) {
3336 if (kmsg->iov != kmsg->fast_iov)
3337 kfree(kmsg->iov);
3338 return -ENOMEM;
3339 }
3340 req->flags |= REQ_F_NEED_CLEANUP;
3341 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3342 return -EAGAIN;
3343}
3344
Jens Axboe3529d8c2019-12-19 18:24:38 -07003345static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003346{
Jens Axboe03b12302019-12-02 18:50:25 -07003347#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003348 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003349 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003350 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003351
Jens Axboee47293f2019-12-20 08:58:21 -07003352 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3353 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003354 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003355
Jens Axboed8768362020-02-27 14:17:49 -07003356#ifdef CONFIG_COMPAT
3357 if (req->ctx->compat)
3358 sr->msg_flags |= MSG_CMSG_COMPAT;
3359#endif
3360
Jens Axboefddafac2020-01-04 20:19:44 -07003361 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003362 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003363 /* iovec is already imported */
3364 if (req->flags & REQ_F_NEED_CLEANUP)
3365 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003366
Jens Axboed9688562019-12-09 19:35:20 -07003367 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003368 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003369 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003370 if (!ret)
3371 req->flags |= REQ_F_NEED_CLEANUP;
3372 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003373#else
Jens Axboee47293f2019-12-20 08:58:21 -07003374 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003375#endif
3376}
3377
Pavel Begunkov014db002020-03-03 21:33:12 +03003378static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003379{
3380#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003381 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003382 struct socket *sock;
3383 int ret;
3384
3385 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3386 return -EINVAL;
3387
3388 sock = sock_from_file(req->file, &ret);
3389 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003390 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003391 unsigned flags;
3392
Jens Axboe03b12302019-12-02 18:50:25 -07003393 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003394 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003395 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003396 /* if iov is set, it's allocated already */
3397 if (!kmsg->iov)
3398 kmsg->iov = kmsg->fast_iov;
3399 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003400 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003401 struct io_sr_msg *sr = &req->sr_msg;
3402
Jens Axboe0b416c32019-12-15 10:57:46 -07003403 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003404 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003405
3406 io.msg.iov = io.msg.fast_iov;
3407 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3408 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003409 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003410 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003411 }
3412
Jens Axboee47293f2019-12-20 08:58:21 -07003413 flags = req->sr_msg.msg_flags;
3414 if (flags & MSG_DONTWAIT)
3415 req->flags |= REQ_F_NOWAIT;
3416 else if (force_nonblock)
3417 flags |= MSG_DONTWAIT;
3418
Jens Axboe0b416c32019-12-15 10:57:46 -07003419 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003420 if (force_nonblock && ret == -EAGAIN)
3421 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003422 if (ret == -ERESTARTSYS)
3423 ret = -EINTR;
3424 }
3425
Pavel Begunkov1e950812020-02-06 19:51:16 +03003426 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003427 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003428 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003429 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003430 if (ret < 0)
3431 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003432 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003433 return 0;
3434#else
3435 return -EOPNOTSUPP;
3436#endif
3437}
3438
Pavel Begunkov014db002020-03-03 21:33:12 +03003439static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003440{
3441#if defined(CONFIG_NET)
3442 struct socket *sock;
3443 int ret;
3444
3445 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3446 return -EINVAL;
3447
3448 sock = sock_from_file(req->file, &ret);
3449 if (sock) {
3450 struct io_sr_msg *sr = &req->sr_msg;
3451 struct msghdr msg;
3452 struct iovec iov;
3453 unsigned flags;
3454
3455 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3456 &msg.msg_iter);
3457 if (ret)
3458 return ret;
3459
3460 msg.msg_name = NULL;
3461 msg.msg_control = NULL;
3462 msg.msg_controllen = 0;
3463 msg.msg_namelen = 0;
3464
3465 flags = req->sr_msg.msg_flags;
3466 if (flags & MSG_DONTWAIT)
3467 req->flags |= REQ_F_NOWAIT;
3468 else if (force_nonblock)
3469 flags |= MSG_DONTWAIT;
3470
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003471 msg.msg_flags = flags;
3472 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003473 if (force_nonblock && ret == -EAGAIN)
3474 return -EAGAIN;
3475 if (ret == -ERESTARTSYS)
3476 ret = -EINTR;
3477 }
3478
3479 io_cqring_add_event(req, ret);
3480 if (ret < 0)
3481 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003482 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003483 return 0;
3484#else
3485 return -EOPNOTSUPP;
3486#endif
3487}
3488
Jens Axboebcda7ba2020-02-23 16:42:51 -07003489static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3490 int *cflags, bool needs_lock)
3491{
3492 struct io_sr_msg *sr = &req->sr_msg;
3493 struct io_buffer *kbuf;
3494
3495 if (!(req->flags & REQ_F_BUFFER_SELECT))
3496 return NULL;
3497
3498 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3499 if (IS_ERR(kbuf))
3500 return kbuf;
3501
3502 sr->kbuf = kbuf;
3503 req->flags |= REQ_F_BUFFER_SELECTED;
3504
3505 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3506 *cflags |= IORING_CQE_F_BUFFER;
3507 return kbuf;
3508}
3509
Jens Axboe3529d8c2019-12-19 18:24:38 -07003510static int io_recvmsg_prep(struct io_kiocb *req,
3511 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003512{
3513#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003514 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003515 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003516 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003517
Jens Axboe3529d8c2019-12-19 18:24:38 -07003518 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3519 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003520 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003521 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003522
Jens Axboed8768362020-02-27 14:17:49 -07003523#ifdef CONFIG_COMPAT
3524 if (req->ctx->compat)
3525 sr->msg_flags |= MSG_CMSG_COMPAT;
3526#endif
3527
Jens Axboefddafac2020-01-04 20:19:44 -07003528 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003529 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003530 /* iovec is already imported */
3531 if (req->flags & REQ_F_NEED_CLEANUP)
3532 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003533
Jens Axboed9688562019-12-09 19:35:20 -07003534 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003535 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003536 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003537 if (!ret)
3538 req->flags |= REQ_F_NEED_CLEANUP;
3539 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003540#else
Jens Axboee47293f2019-12-20 08:58:21 -07003541 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003542#endif
3543}
3544
Pavel Begunkov014db002020-03-03 21:33:12 +03003545static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003546{
3547#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003548 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003549 struct socket *sock;
3550 int ret;
3551
3552 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3553 return -EINVAL;
3554
3555 sock = sock_from_file(req->file, &ret);
3556 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003557 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003558 unsigned flags;
3559
Jens Axboe03b12302019-12-02 18:50:25 -07003560 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003561 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003562 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003563 /* if iov is set, it's allocated already */
3564 if (!kmsg->iov)
3565 kmsg->iov = kmsg->fast_iov;
3566 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003567 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003568 struct io_sr_msg *sr = &req->sr_msg;
3569
Jens Axboe0b416c32019-12-15 10:57:46 -07003570 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003571 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003572
3573 io.msg.iov = io.msg.fast_iov;
3574 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3575 sr->msg_flags, &io.msg.uaddr,
3576 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003577 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003578 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003579 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003580
Jens Axboee47293f2019-12-20 08:58:21 -07003581 flags = req->sr_msg.msg_flags;
3582 if (flags & MSG_DONTWAIT)
3583 req->flags |= REQ_F_NOWAIT;
3584 else if (force_nonblock)
3585 flags |= MSG_DONTWAIT;
3586
3587 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3588 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003589 if (force_nonblock && ret == -EAGAIN)
3590 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003591 if (ret == -ERESTARTSYS)
3592 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003593 }
3594
Pavel Begunkov1e950812020-02-06 19:51:16 +03003595 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003596 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003597 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003598 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003599 if (ret < 0)
3600 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003601 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003602 return 0;
3603#else
3604 return -EOPNOTSUPP;
3605#endif
3606}
3607
Pavel Begunkov014db002020-03-03 21:33:12 +03003608static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003609{
3610#if defined(CONFIG_NET)
Jens Axboebcda7ba2020-02-23 16:42:51 -07003611 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003612 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003613 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003614
3615 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3616 return -EINVAL;
3617
3618 sock = sock_from_file(req->file, &ret);
3619 if (sock) {
3620 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003621 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003622 struct msghdr msg;
3623 struct iovec iov;
3624 unsigned flags;
3625
Jens Axboebcda7ba2020-02-23 16:42:51 -07003626 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3627 if (IS_ERR(kbuf))
3628 return PTR_ERR(kbuf);
3629 else if (kbuf)
3630 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003631
Jens Axboebcda7ba2020-02-23 16:42:51 -07003632 ret = import_single_range(READ, buf, sr->len, &iov,
3633 &msg.msg_iter);
3634 if (ret) {
3635 kfree(kbuf);
3636 return ret;
3637 }
3638
3639 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003640 msg.msg_name = NULL;
3641 msg.msg_control = NULL;
3642 msg.msg_controllen = 0;
3643 msg.msg_namelen = 0;
3644 msg.msg_iocb = NULL;
3645 msg.msg_flags = 0;
3646
3647 flags = req->sr_msg.msg_flags;
3648 if (flags & MSG_DONTWAIT)
3649 req->flags |= REQ_F_NOWAIT;
3650 else if (force_nonblock)
3651 flags |= MSG_DONTWAIT;
3652
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003653 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003654 if (force_nonblock && ret == -EAGAIN)
3655 return -EAGAIN;
3656 if (ret == -ERESTARTSYS)
3657 ret = -EINTR;
3658 }
3659
Jens Axboebcda7ba2020-02-23 16:42:51 -07003660 kfree(kbuf);
3661 req->flags &= ~REQ_F_NEED_CLEANUP;
3662 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003663 if (ret < 0)
3664 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003665 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003666 return 0;
3667#else
3668 return -EOPNOTSUPP;
3669#endif
3670}
3671
3672
Jens Axboe3529d8c2019-12-19 18:24:38 -07003673static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003674{
3675#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003676 struct io_accept *accept = &req->accept;
3677
Jens Axboe17f2fe32019-10-17 14:42:58 -06003678 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3679 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003680 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003681 return -EINVAL;
3682
Jens Axboed55e5f52019-12-11 16:12:15 -07003683 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3684 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003685 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003686 return 0;
3687#else
3688 return -EOPNOTSUPP;
3689#endif
3690}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003691
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003692#if defined(CONFIG_NET)
Pavel Begunkov014db002020-03-03 21:33:12 +03003693static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003694{
3695 struct io_accept *accept = &req->accept;
3696 unsigned file_flags;
3697 int ret;
3698
3699 file_flags = force_nonblock ? O_NONBLOCK : 0;
3700 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3701 accept->addr_len, accept->flags);
3702 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003703 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003704 if (ret == -ERESTARTSYS)
3705 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003706 if (ret < 0)
3707 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003708 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003709 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003710 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003711}
3712
3713static void io_accept_finish(struct io_wq_work **workptr)
3714{
3715 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003716
3717 if (io_req_cancelled(req))
3718 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003719 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003720 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003721}
3722#endif
3723
Pavel Begunkov014db002020-03-03 21:33:12 +03003724static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003725{
3726#if defined(CONFIG_NET)
3727 int ret;
3728
Pavel Begunkov014db002020-03-03 21:33:12 +03003729 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003730 if (ret == -EAGAIN && force_nonblock) {
3731 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003732 return -EAGAIN;
3733 }
3734 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003735#else
3736 return -EOPNOTSUPP;
3737#endif
3738}
3739
Jens Axboe3529d8c2019-12-19 18:24:38 -07003740static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003741{
3742#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003743 struct io_connect *conn = &req->connect;
3744 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003745
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003746 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3747 return -EINVAL;
3748 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3749 return -EINVAL;
3750
Jens Axboe3529d8c2019-12-19 18:24:38 -07003751 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3752 conn->addr_len = READ_ONCE(sqe->addr2);
3753
3754 if (!io)
3755 return 0;
3756
3757 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003758 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003759#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003760 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003761#endif
3762}
3763
Pavel Begunkov014db002020-03-03 21:33:12 +03003764static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003765{
3766#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003767 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003768 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003769 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003770
Jens Axboef499a022019-12-02 16:28:46 -07003771 if (req->io) {
3772 io = req->io;
3773 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003774 ret = move_addr_to_kernel(req->connect.addr,
3775 req->connect.addr_len,
3776 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003777 if (ret)
3778 goto out;
3779 io = &__io;
3780 }
3781
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003782 file_flags = force_nonblock ? O_NONBLOCK : 0;
3783
3784 ret = __sys_connect_file(req->file, &io->connect.address,
3785 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003786 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003787 if (req->io)
3788 return -EAGAIN;
3789 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003790 ret = -ENOMEM;
3791 goto out;
3792 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003793 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003794 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003795 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003796 if (ret == -ERESTARTSYS)
3797 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003798out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003799 if (ret < 0)
3800 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003801 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003802 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003803 return 0;
3804#else
3805 return -EOPNOTSUPP;
3806#endif
3807}
3808
Jens Axboed7718a92020-02-14 22:23:12 -07003809struct io_poll_table {
3810 struct poll_table_struct pt;
3811 struct io_kiocb *req;
3812 int error;
3813};
3814
3815static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
3816 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003817{
Jens Axboed7718a92020-02-14 22:23:12 -07003818 if (unlikely(poll->head)) {
3819 pt->error = -EINVAL;
3820 return;
3821 }
3822
3823 pt->error = 0;
3824 poll->head = head;
3825 add_wait_queue(head, &poll->wait);
3826}
3827
3828static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
3829 struct poll_table_struct *p)
3830{
3831 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3832
3833 __io_queue_proc(&pt->req->apoll->poll, pt, head);
3834}
3835
3836static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
3837 __poll_t mask, task_work_func_t func)
3838{
3839 struct task_struct *tsk;
3840
3841 /* for instances that support it check for an event match first: */
3842 if (mask && !(mask & poll->events))
3843 return 0;
3844
3845 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
3846
3847 list_del_init(&poll->wait.entry);
3848
3849 tsk = req->task;
3850 req->result = mask;
3851 init_task_work(&req->task_work, func);
3852 /*
3853 * If this fails, then the task is exiting. If that is the case, then
3854 * the exit check will ultimately cancel these work items. Hence we
3855 * don't need to check here and handle it specifically.
3856 */
3857 task_work_add(tsk, &req->task_work, true);
3858 wake_up_process(tsk);
3859 return 1;
3860}
3861
3862static void io_async_task_func(struct callback_head *cb)
3863{
3864 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3865 struct async_poll *apoll = req->apoll;
3866 struct io_ring_ctx *ctx = req->ctx;
3867
3868 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
3869
3870 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
3871
3872 if (hash_hashed(&req->hash_node)) {
3873 spin_lock_irq(&ctx->completion_lock);
3874 hash_del(&req->hash_node);
3875 spin_unlock_irq(&ctx->completion_lock);
3876 }
3877
3878 /* restore ->work in case we need to retry again */
3879 memcpy(&req->work, &apoll->work, sizeof(req->work));
3880
3881 __set_current_state(TASK_RUNNING);
3882 mutex_lock(&ctx->uring_lock);
3883 __io_queue_sqe(req, NULL);
3884 mutex_unlock(&ctx->uring_lock);
3885
3886 kfree(apoll);
3887}
3888
3889static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3890 void *key)
3891{
3892 struct io_kiocb *req = wait->private;
3893 struct io_poll_iocb *poll = &req->apoll->poll;
3894
3895 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
3896 key_to_poll(key));
3897
3898 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
3899}
3900
3901static void io_poll_req_insert(struct io_kiocb *req)
3902{
3903 struct io_ring_ctx *ctx = req->ctx;
3904 struct hlist_head *list;
3905
3906 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3907 hlist_add_head(&req->hash_node, list);
3908}
3909
3910static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
3911 struct io_poll_iocb *poll,
3912 struct io_poll_table *ipt, __poll_t mask,
3913 wait_queue_func_t wake_func)
3914 __acquires(&ctx->completion_lock)
3915{
3916 struct io_ring_ctx *ctx = req->ctx;
3917 bool cancel = false;
3918
3919 poll->file = req->file;
3920 poll->head = NULL;
3921 poll->done = poll->canceled = false;
3922 poll->events = mask;
3923
3924 ipt->pt._key = mask;
3925 ipt->req = req;
3926 ipt->error = -EINVAL;
3927
3928 INIT_LIST_HEAD(&poll->wait.entry);
3929 init_waitqueue_func_entry(&poll->wait, wake_func);
3930 poll->wait.private = req;
3931
3932 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
3933
3934 spin_lock_irq(&ctx->completion_lock);
3935 if (likely(poll->head)) {
3936 spin_lock(&poll->head->lock);
3937 if (unlikely(list_empty(&poll->wait.entry))) {
3938 if (ipt->error)
3939 cancel = true;
3940 ipt->error = 0;
3941 mask = 0;
3942 }
3943 if (mask || ipt->error)
3944 list_del_init(&poll->wait.entry);
3945 else if (cancel)
3946 WRITE_ONCE(poll->canceled, true);
3947 else if (!poll->done) /* actually waiting for an event */
3948 io_poll_req_insert(req);
3949 spin_unlock(&poll->head->lock);
3950 }
3951
3952 return mask;
3953}
3954
3955static bool io_arm_poll_handler(struct io_kiocb *req)
3956{
3957 const struct io_op_def *def = &io_op_defs[req->opcode];
3958 struct io_ring_ctx *ctx = req->ctx;
3959 struct async_poll *apoll;
3960 struct io_poll_table ipt;
3961 __poll_t mask, ret;
3962
3963 if (!req->file || !file_can_poll(req->file))
3964 return false;
3965 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
3966 return false;
3967 if (!def->pollin && !def->pollout)
3968 return false;
3969
3970 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
3971 if (unlikely(!apoll))
3972 return false;
3973
3974 req->flags |= REQ_F_POLLED;
3975 memcpy(&apoll->work, &req->work, sizeof(req->work));
3976
3977 /*
3978 * Don't need a reference here, as we're adding it to the task
3979 * task_works list. If the task exits, the list is pruned.
3980 */
3981 req->task = current;
3982 req->apoll = apoll;
3983 INIT_HLIST_NODE(&req->hash_node);
3984
Nathan Chancellor8755d972020-03-02 16:01:19 -07003985 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07003986 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07003987 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07003988 if (def->pollout)
3989 mask |= POLLOUT | POLLWRNORM;
3990 mask |= POLLERR | POLLPRI;
3991
3992 ipt.pt._qproc = io_async_queue_proc;
3993
3994 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
3995 io_async_wake);
3996 if (ret) {
3997 ipt.error = 0;
3998 apoll->poll.done = true;
3999 spin_unlock_irq(&ctx->completion_lock);
4000 memcpy(&req->work, &apoll->work, sizeof(req->work));
4001 kfree(apoll);
4002 return false;
4003 }
4004 spin_unlock_irq(&ctx->completion_lock);
4005 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4006 apoll->poll.events);
4007 return true;
4008}
4009
4010static bool __io_poll_remove_one(struct io_kiocb *req,
4011 struct io_poll_iocb *poll)
4012{
Jens Axboeb41e9852020-02-17 09:52:41 -07004013 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004014
4015 spin_lock(&poll->head->lock);
4016 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004017 if (!list_empty(&poll->wait.entry)) {
4018 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004019 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004020 }
4021 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004022 return do_complete;
4023}
4024
4025static bool io_poll_remove_one(struct io_kiocb *req)
4026{
4027 bool do_complete;
4028
4029 if (req->opcode == IORING_OP_POLL_ADD) {
4030 do_complete = __io_poll_remove_one(req, &req->poll);
4031 } else {
4032 /* non-poll requests have submit ref still */
4033 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4034 if (do_complete)
4035 io_put_req(req);
4036 }
4037
Jens Axboe78076bb2019-12-04 19:56:40 -07004038 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004039
Jens Axboeb41e9852020-02-17 09:52:41 -07004040 if (do_complete) {
4041 io_cqring_fill_event(req, -ECANCELED);
4042 io_commit_cqring(req->ctx);
4043 req->flags |= REQ_F_COMP_LOCKED;
4044 io_put_req(req);
4045 }
4046
4047 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004048}
4049
4050static void io_poll_remove_all(struct io_ring_ctx *ctx)
4051{
Jens Axboe78076bb2019-12-04 19:56:40 -07004052 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004053 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07004054 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004055
4056 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004057 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4058 struct hlist_head *list;
4059
4060 list = &ctx->cancel_hash[i];
4061 hlist_for_each_entry_safe(req, tmp, list, hash_node)
4062 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004063 }
4064 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004065
4066 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004067}
4068
Jens Axboe47f46762019-11-09 17:43:02 -07004069static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4070{
Jens Axboe78076bb2019-12-04 19:56:40 -07004071 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004072 struct io_kiocb *req;
4073
Jens Axboe78076bb2019-12-04 19:56:40 -07004074 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4075 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004076 if (sqe_addr != req->user_data)
4077 continue;
4078 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004079 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004080 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004081 }
4082
4083 return -ENOENT;
4084}
4085
Jens Axboe3529d8c2019-12-19 18:24:38 -07004086static int io_poll_remove_prep(struct io_kiocb *req,
4087 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004088{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004089 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4090 return -EINVAL;
4091 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4092 sqe->poll_events)
4093 return -EINVAL;
4094
Jens Axboe0969e782019-12-17 18:40:57 -07004095 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004096 return 0;
4097}
4098
4099/*
4100 * Find a running poll command that matches one specified in sqe->addr,
4101 * and remove it if found.
4102 */
4103static int io_poll_remove(struct io_kiocb *req)
4104{
4105 struct io_ring_ctx *ctx = req->ctx;
4106 u64 addr;
4107 int ret;
4108
Jens Axboe0969e782019-12-17 18:40:57 -07004109 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004110 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004111 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004112 spin_unlock_irq(&ctx->completion_lock);
4113
Jens Axboe78e19bb2019-11-06 15:21:34 -07004114 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004115 if (ret < 0)
4116 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004117 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004118 return 0;
4119}
4120
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004121static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004122{
Jackie Liua197f662019-11-08 08:09:12 -07004123 struct io_ring_ctx *ctx = req->ctx;
4124
Jens Axboe8c838782019-03-12 15:48:16 -06004125 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004126 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004127 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004128}
4129
Jens Axboeb41e9852020-02-17 09:52:41 -07004130static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004131{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004132 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004133
Jens Axboe221c5eb2019-01-17 09:41:58 -07004134 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004135 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004136 io_poll_complete(req, req->result, 0);
4137 req->flags |= REQ_F_COMP_LOCKED;
4138 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004139 spin_unlock_irq(&ctx->completion_lock);
4140
Jens Axboe8c838782019-03-12 15:48:16 -06004141 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07004142}
Jens Axboe89723d02019-11-05 15:32:58 -07004143
Jens Axboeb41e9852020-02-17 09:52:41 -07004144static void io_poll_task_func(struct callback_head *cb)
4145{
4146 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4147 struct io_kiocb *nxt = NULL;
4148
4149 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004150 if (nxt) {
4151 struct io_ring_ctx *ctx = nxt->ctx;
4152
4153 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004154 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004155 mutex_unlock(&ctx->uring_lock);
4156 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004157}
4158
Jens Axboe221c5eb2019-01-17 09:41:58 -07004159static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4160 void *key)
4161{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004162 struct io_kiocb *req = wait->private;
4163 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004164
Jens Axboed7718a92020-02-14 22:23:12 -07004165 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004166}
4167
Jens Axboe221c5eb2019-01-17 09:41:58 -07004168static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4169 struct poll_table_struct *p)
4170{
4171 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4172
Jens Axboed7718a92020-02-14 22:23:12 -07004173 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004174}
4175
Jens Axboe3529d8c2019-12-19 18:24:38 -07004176static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004177{
4178 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004179 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004180
4181 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4182 return -EINVAL;
4183 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4184 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004185 if (!poll->file)
4186 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004187
Jens Axboe221c5eb2019-01-17 09:41:58 -07004188 events = READ_ONCE(sqe->poll_events);
4189 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004190
Jens Axboed7718a92020-02-14 22:23:12 -07004191 /*
4192 * Don't need a reference here, as we're adding it to the task
4193 * task_works list. If the task exits, the list is pruned.
4194 */
Jens Axboeb41e9852020-02-17 09:52:41 -07004195 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004196 return 0;
4197}
4198
Pavel Begunkov014db002020-03-03 21:33:12 +03004199static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004200{
4201 struct io_poll_iocb *poll = &req->poll;
4202 struct io_ring_ctx *ctx = req->ctx;
4203 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004204 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004205
Jens Axboe78076bb2019-12-04 19:56:40 -07004206 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004207 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004208 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004209
Jens Axboed7718a92020-02-14 22:23:12 -07004210 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4211 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004212
Jens Axboe8c838782019-03-12 15:48:16 -06004213 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004214 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004215 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004216 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004217 spin_unlock_irq(&ctx->completion_lock);
4218
Jens Axboe8c838782019-03-12 15:48:16 -06004219 if (mask) {
4220 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004221 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004222 }
Jens Axboe8c838782019-03-12 15:48:16 -06004223 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004224}
4225
Jens Axboe5262f562019-09-17 12:26:57 -06004226static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4227{
Jens Axboead8a48a2019-11-15 08:49:11 -07004228 struct io_timeout_data *data = container_of(timer,
4229 struct io_timeout_data, timer);
4230 struct io_kiocb *req = data->req;
4231 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004232 unsigned long flags;
4233
Jens Axboe5262f562019-09-17 12:26:57 -06004234 atomic_inc(&ctx->cq_timeouts);
4235
4236 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004237 /*
Jens Axboe11365042019-10-16 09:08:32 -06004238 * We could be racing with timeout deletion. If the list is empty,
4239 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004240 */
Jens Axboe842f9612019-10-29 12:34:10 -06004241 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004242 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004243
Jens Axboe11365042019-10-16 09:08:32 -06004244 /*
4245 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004246 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004247 * pointer will be increased, otherwise other timeout reqs may
4248 * return in advance without waiting for enough wait_nr.
4249 */
4250 prev = req;
4251 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4252 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004253 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004254 }
Jens Axboe842f9612019-10-29 12:34:10 -06004255
Jens Axboe78e19bb2019-11-06 15:21:34 -07004256 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004257 io_commit_cqring(ctx);
4258 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4259
4260 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004261 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004262 io_put_req(req);
4263 return HRTIMER_NORESTART;
4264}
4265
Jens Axboe47f46762019-11-09 17:43:02 -07004266static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4267{
4268 struct io_kiocb *req;
4269 int ret = -ENOENT;
4270
4271 list_for_each_entry(req, &ctx->timeout_list, list) {
4272 if (user_data == req->user_data) {
4273 list_del_init(&req->list);
4274 ret = 0;
4275 break;
4276 }
4277 }
4278
4279 if (ret == -ENOENT)
4280 return ret;
4281
Jens Axboe2d283902019-12-04 11:08:05 -07004282 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004283 if (ret == -1)
4284 return -EALREADY;
4285
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004286 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004287 io_cqring_fill_event(req, -ECANCELED);
4288 io_put_req(req);
4289 return 0;
4290}
4291
Jens Axboe3529d8c2019-12-19 18:24:38 -07004292static int io_timeout_remove_prep(struct io_kiocb *req,
4293 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004294{
Jens Axboeb29472e2019-12-17 18:50:29 -07004295 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4296 return -EINVAL;
4297 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4298 return -EINVAL;
4299
4300 req->timeout.addr = READ_ONCE(sqe->addr);
4301 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4302 if (req->timeout.flags)
4303 return -EINVAL;
4304
Jens Axboeb29472e2019-12-17 18:50:29 -07004305 return 0;
4306}
4307
Jens Axboe11365042019-10-16 09:08:32 -06004308/*
4309 * Remove or update an existing timeout command
4310 */
Jens Axboefc4df992019-12-10 14:38:45 -07004311static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004312{
4313 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004314 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004315
Jens Axboe11365042019-10-16 09:08:32 -06004316 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004317 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004318
Jens Axboe47f46762019-11-09 17:43:02 -07004319 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004320 io_commit_cqring(ctx);
4321 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004322 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004323 if (ret < 0)
4324 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004325 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004326 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004327}
4328
Jens Axboe3529d8c2019-12-19 18:24:38 -07004329static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004330 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004331{
Jens Axboead8a48a2019-11-15 08:49:11 -07004332 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004333 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004334
Jens Axboead8a48a2019-11-15 08:49:11 -07004335 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004336 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004337 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004338 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004339 if (sqe->off && is_timeout_link)
4340 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004341 flags = READ_ONCE(sqe->timeout_flags);
4342 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004343 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004344
Jens Axboe26a61672019-12-20 09:02:01 -07004345 req->timeout.count = READ_ONCE(sqe->off);
4346
Jens Axboe3529d8c2019-12-19 18:24:38 -07004347 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004348 return -ENOMEM;
4349
4350 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004351 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004352 req->flags |= REQ_F_TIMEOUT;
4353
4354 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004355 return -EFAULT;
4356
Jens Axboe11365042019-10-16 09:08:32 -06004357 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004358 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004359 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004360 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004361
Jens Axboead8a48a2019-11-15 08:49:11 -07004362 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4363 return 0;
4364}
4365
Jens Axboefc4df992019-12-10 14:38:45 -07004366static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004367{
4368 unsigned count;
4369 struct io_ring_ctx *ctx = req->ctx;
4370 struct io_timeout_data *data;
4371 struct list_head *entry;
4372 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004373
Jens Axboe2d283902019-12-04 11:08:05 -07004374 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004375
Jens Axboe5262f562019-09-17 12:26:57 -06004376 /*
4377 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004378 * timeout event to be satisfied. If it isn't set, then this is
4379 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004380 */
Jens Axboe26a61672019-12-20 09:02:01 -07004381 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004382 if (!count) {
4383 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4384 spin_lock_irq(&ctx->completion_lock);
4385 entry = ctx->timeout_list.prev;
4386 goto add;
4387 }
Jens Axboe5262f562019-09-17 12:26:57 -06004388
4389 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004390 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004391
4392 /*
4393 * Insertion sort, ensuring the first entry in the list is always
4394 * the one we need first.
4395 */
Jens Axboe5262f562019-09-17 12:26:57 -06004396 spin_lock_irq(&ctx->completion_lock);
4397 list_for_each_prev(entry, &ctx->timeout_list) {
4398 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004399 unsigned nxt_sq_head;
4400 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004401 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004402
Jens Axboe93bd25b2019-11-11 23:34:31 -07004403 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4404 continue;
4405
yangerkun5da0fb12019-10-15 21:59:29 +08004406 /*
4407 * Since cached_sq_head + count - 1 can overflow, use type long
4408 * long to store it.
4409 */
4410 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004411 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4412 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004413
4414 /*
4415 * cached_sq_head may overflow, and it will never overflow twice
4416 * once there is some timeout req still be valid.
4417 */
4418 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004419 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004420
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004421 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004422 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004423
4424 /*
4425 * Sequence of reqs after the insert one and itself should
4426 * be adjusted because each timeout req consumes a slot.
4427 */
4428 span++;
4429 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004430 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004431 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004432add:
Jens Axboe5262f562019-09-17 12:26:57 -06004433 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004434 data->timer.function = io_timeout_fn;
4435 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004436 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004437 return 0;
4438}
4439
Jens Axboe62755e32019-10-28 21:49:21 -06004440static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004441{
Jens Axboe62755e32019-10-28 21:49:21 -06004442 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004443
Jens Axboe62755e32019-10-28 21:49:21 -06004444 return req->user_data == (unsigned long) data;
4445}
4446
Jens Axboee977d6d2019-11-05 12:39:45 -07004447static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004448{
Jens Axboe62755e32019-10-28 21:49:21 -06004449 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004450 int ret = 0;
4451
Jens Axboe62755e32019-10-28 21:49:21 -06004452 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4453 switch (cancel_ret) {
4454 case IO_WQ_CANCEL_OK:
4455 ret = 0;
4456 break;
4457 case IO_WQ_CANCEL_RUNNING:
4458 ret = -EALREADY;
4459 break;
4460 case IO_WQ_CANCEL_NOTFOUND:
4461 ret = -ENOENT;
4462 break;
4463 }
4464
Jens Axboee977d6d2019-11-05 12:39:45 -07004465 return ret;
4466}
4467
Jens Axboe47f46762019-11-09 17:43:02 -07004468static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4469 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004470 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004471{
4472 unsigned long flags;
4473 int ret;
4474
4475 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4476 if (ret != -ENOENT) {
4477 spin_lock_irqsave(&ctx->completion_lock, flags);
4478 goto done;
4479 }
4480
4481 spin_lock_irqsave(&ctx->completion_lock, flags);
4482 ret = io_timeout_cancel(ctx, sqe_addr);
4483 if (ret != -ENOENT)
4484 goto done;
4485 ret = io_poll_cancel(ctx, sqe_addr);
4486done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004487 if (!ret)
4488 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004489 io_cqring_fill_event(req, ret);
4490 io_commit_cqring(ctx);
4491 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4492 io_cqring_ev_posted(ctx);
4493
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004494 if (ret < 0)
4495 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004496 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004497}
4498
Jens Axboe3529d8c2019-12-19 18:24:38 -07004499static int io_async_cancel_prep(struct io_kiocb *req,
4500 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004501{
Jens Axboefbf23842019-12-17 18:45:56 -07004502 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004503 return -EINVAL;
4504 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4505 sqe->cancel_flags)
4506 return -EINVAL;
4507
Jens Axboefbf23842019-12-17 18:45:56 -07004508 req->cancel.addr = READ_ONCE(sqe->addr);
4509 return 0;
4510}
4511
Pavel Begunkov014db002020-03-03 21:33:12 +03004512static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004513{
4514 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004515
Pavel Begunkov014db002020-03-03 21:33:12 +03004516 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004517 return 0;
4518}
4519
Jens Axboe05f3fb32019-12-09 11:22:50 -07004520static int io_files_update_prep(struct io_kiocb *req,
4521 const struct io_uring_sqe *sqe)
4522{
4523 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4524 return -EINVAL;
4525
4526 req->files_update.offset = READ_ONCE(sqe->off);
4527 req->files_update.nr_args = READ_ONCE(sqe->len);
4528 if (!req->files_update.nr_args)
4529 return -EINVAL;
4530 req->files_update.arg = READ_ONCE(sqe->addr);
4531 return 0;
4532}
4533
4534static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4535{
4536 struct io_ring_ctx *ctx = req->ctx;
4537 struct io_uring_files_update up;
4538 int ret;
4539
Jens Axboef86cd202020-01-29 13:46:44 -07004540 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004541 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004542
4543 up.offset = req->files_update.offset;
4544 up.fds = req->files_update.arg;
4545
4546 mutex_lock(&ctx->uring_lock);
4547 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4548 mutex_unlock(&ctx->uring_lock);
4549
4550 if (ret < 0)
4551 req_set_fail_links(req);
4552 io_cqring_add_event(req, ret);
4553 io_put_req(req);
4554 return 0;
4555}
4556
Jens Axboe3529d8c2019-12-19 18:24:38 -07004557static int io_req_defer_prep(struct io_kiocb *req,
4558 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004559{
Jens Axboee7815732019-12-17 19:45:06 -07004560 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004561
Jens Axboef86cd202020-01-29 13:46:44 -07004562 if (io_op_defs[req->opcode].file_table) {
4563 ret = io_grab_files(req);
4564 if (unlikely(ret))
4565 return ret;
4566 }
4567
Jens Axboecccf0ee2020-01-27 16:34:48 -07004568 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4569
Jens Axboed625c6e2019-12-17 19:53:05 -07004570 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004571 case IORING_OP_NOP:
4572 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004573 case IORING_OP_READV:
4574 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004575 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004576 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004577 break;
4578 case IORING_OP_WRITEV:
4579 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004580 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004581 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004582 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004583 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004584 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004585 break;
4586 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004587 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004588 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004589 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004590 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004591 break;
4592 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004593 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004594 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004595 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004596 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004597 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004598 break;
4599 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004600 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004601 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004602 break;
Jens Axboef499a022019-12-02 16:28:46 -07004603 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004604 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004605 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004606 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004607 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004608 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004609 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004610 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004611 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004612 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004613 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004614 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004615 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004616 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004617 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004618 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004619 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004620 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004621 case IORING_OP_FALLOCATE:
4622 ret = io_fallocate_prep(req, sqe);
4623 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004624 case IORING_OP_OPENAT:
4625 ret = io_openat_prep(req, sqe);
4626 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004627 case IORING_OP_CLOSE:
4628 ret = io_close_prep(req, sqe);
4629 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004630 case IORING_OP_FILES_UPDATE:
4631 ret = io_files_update_prep(req, sqe);
4632 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004633 case IORING_OP_STATX:
4634 ret = io_statx_prep(req, sqe);
4635 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004636 case IORING_OP_FADVISE:
4637 ret = io_fadvise_prep(req, sqe);
4638 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004639 case IORING_OP_MADVISE:
4640 ret = io_madvise_prep(req, sqe);
4641 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004642 case IORING_OP_OPENAT2:
4643 ret = io_openat2_prep(req, sqe);
4644 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004645 case IORING_OP_EPOLL_CTL:
4646 ret = io_epoll_ctl_prep(req, sqe);
4647 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004648 case IORING_OP_SPLICE:
4649 ret = io_splice_prep(req, sqe);
4650 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004651 case IORING_OP_PROVIDE_BUFFERS:
4652 ret = io_provide_buffers_prep(req, sqe);
4653 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004654 default:
Jens Axboee7815732019-12-17 19:45:06 -07004655 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4656 req->opcode);
4657 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004658 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004659 }
4660
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004661 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004662}
4663
Jens Axboe3529d8c2019-12-19 18:24:38 -07004664static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004665{
Jackie Liua197f662019-11-08 08:09:12 -07004666 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004667 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004668
Bob Liu9d858b22019-11-13 18:06:25 +08004669 /* Still need defer if there is pending req in defer list. */
4670 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004671 return 0;
4672
Jens Axboe3529d8c2019-12-19 18:24:38 -07004673 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004674 return -EAGAIN;
4675
Jens Axboe3529d8c2019-12-19 18:24:38 -07004676 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004677 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004678 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004679
Jens Axboede0617e2019-04-06 21:51:27 -06004680 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004681 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004682 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004683 return 0;
4684 }
4685
Jens Axboe915967f2019-11-21 09:01:20 -07004686 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004687 list_add_tail(&req->list, &ctx->defer_list);
4688 spin_unlock_irq(&ctx->completion_lock);
4689 return -EIOCBQUEUED;
4690}
4691
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004692static void io_cleanup_req(struct io_kiocb *req)
4693{
4694 struct io_async_ctx *io = req->io;
4695
4696 switch (req->opcode) {
4697 case IORING_OP_READV:
4698 case IORING_OP_READ_FIXED:
4699 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07004700 if (req->flags & REQ_F_BUFFER_SELECTED)
4701 kfree((void *)(unsigned long)req->rw.addr);
4702 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004703 case IORING_OP_WRITEV:
4704 case IORING_OP_WRITE_FIXED:
4705 case IORING_OP_WRITE:
4706 if (io->rw.iov != io->rw.fast_iov)
4707 kfree(io->rw.iov);
4708 break;
4709 case IORING_OP_SENDMSG:
4710 case IORING_OP_RECVMSG:
4711 if (io->msg.iov != io->msg.fast_iov)
4712 kfree(io->msg.iov);
4713 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004714 case IORING_OP_RECV:
4715 if (req->flags & REQ_F_BUFFER_SELECTED)
4716 kfree(req->sr_msg.kbuf);
4717 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004718 case IORING_OP_OPENAT:
4719 case IORING_OP_OPENAT2:
4720 case IORING_OP_STATX:
4721 putname(req->open.filename);
4722 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004723 case IORING_OP_SPLICE:
4724 io_put_file(req, req->splice.file_in,
4725 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4726 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004727 }
4728
4729 req->flags &= ~REQ_F_NEED_CLEANUP;
4730}
4731
Jens Axboe3529d8c2019-12-19 18:24:38 -07004732static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03004733 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004734{
Jackie Liua197f662019-11-08 08:09:12 -07004735 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004736 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004737
Jens Axboed625c6e2019-12-17 19:53:05 -07004738 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004739 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004740 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004741 break;
4742 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004743 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004744 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004745 if (sqe) {
4746 ret = io_read_prep(req, sqe, force_nonblock);
4747 if (ret < 0)
4748 break;
4749 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004750 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004751 break;
4752 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004753 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004754 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004755 if (sqe) {
4756 ret = io_write_prep(req, sqe, force_nonblock);
4757 if (ret < 0)
4758 break;
4759 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004760 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004761 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004762 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004763 if (sqe) {
4764 ret = io_prep_fsync(req, sqe);
4765 if (ret < 0)
4766 break;
4767 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004768 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004769 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004770 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004771 if (sqe) {
4772 ret = io_poll_add_prep(req, sqe);
4773 if (ret)
4774 break;
4775 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004776 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004777 break;
4778 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004779 if (sqe) {
4780 ret = io_poll_remove_prep(req, sqe);
4781 if (ret < 0)
4782 break;
4783 }
Jens Axboefc4df992019-12-10 14:38:45 -07004784 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004785 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004786 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004787 if (sqe) {
4788 ret = io_prep_sfr(req, sqe);
4789 if (ret < 0)
4790 break;
4791 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004792 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004793 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004794 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004795 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004796 if (sqe) {
4797 ret = io_sendmsg_prep(req, sqe);
4798 if (ret < 0)
4799 break;
4800 }
Jens Axboefddafac2020-01-04 20:19:44 -07004801 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004802 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004803 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004804 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004805 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004806 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004807 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004808 if (sqe) {
4809 ret = io_recvmsg_prep(req, sqe);
4810 if (ret)
4811 break;
4812 }
Jens Axboefddafac2020-01-04 20:19:44 -07004813 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004814 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004815 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004816 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004817 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004818 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004819 if (sqe) {
4820 ret = io_timeout_prep(req, sqe, false);
4821 if (ret)
4822 break;
4823 }
Jens Axboefc4df992019-12-10 14:38:45 -07004824 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004825 break;
Jens Axboe11365042019-10-16 09:08:32 -06004826 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004827 if (sqe) {
4828 ret = io_timeout_remove_prep(req, sqe);
4829 if (ret)
4830 break;
4831 }
Jens Axboefc4df992019-12-10 14:38:45 -07004832 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004833 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004834 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004835 if (sqe) {
4836 ret = io_accept_prep(req, sqe);
4837 if (ret)
4838 break;
4839 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004840 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004841 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004842 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004843 if (sqe) {
4844 ret = io_connect_prep(req, sqe);
4845 if (ret)
4846 break;
4847 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004848 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004849 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004850 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004851 if (sqe) {
4852 ret = io_async_cancel_prep(req, sqe);
4853 if (ret)
4854 break;
4855 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004856 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06004857 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004858 case IORING_OP_FALLOCATE:
4859 if (sqe) {
4860 ret = io_fallocate_prep(req, sqe);
4861 if (ret)
4862 break;
4863 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004864 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07004865 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004866 case IORING_OP_OPENAT:
4867 if (sqe) {
4868 ret = io_openat_prep(req, sqe);
4869 if (ret)
4870 break;
4871 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004872 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004873 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004874 case IORING_OP_CLOSE:
4875 if (sqe) {
4876 ret = io_close_prep(req, sqe);
4877 if (ret)
4878 break;
4879 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004880 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07004881 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004882 case IORING_OP_FILES_UPDATE:
4883 if (sqe) {
4884 ret = io_files_update_prep(req, sqe);
4885 if (ret)
4886 break;
4887 }
4888 ret = io_files_update(req, force_nonblock);
4889 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004890 case IORING_OP_STATX:
4891 if (sqe) {
4892 ret = io_statx_prep(req, sqe);
4893 if (ret)
4894 break;
4895 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004896 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004897 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004898 case IORING_OP_FADVISE:
4899 if (sqe) {
4900 ret = io_fadvise_prep(req, sqe);
4901 if (ret)
4902 break;
4903 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004904 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07004905 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004906 case IORING_OP_MADVISE:
4907 if (sqe) {
4908 ret = io_madvise_prep(req, sqe);
4909 if (ret)
4910 break;
4911 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004912 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07004913 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004914 case IORING_OP_OPENAT2:
4915 if (sqe) {
4916 ret = io_openat2_prep(req, sqe);
4917 if (ret)
4918 break;
4919 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004920 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07004921 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004922 case IORING_OP_EPOLL_CTL:
4923 if (sqe) {
4924 ret = io_epoll_ctl_prep(req, sqe);
4925 if (ret)
4926 break;
4927 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004928 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004929 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004930 case IORING_OP_SPLICE:
4931 if (sqe) {
4932 ret = io_splice_prep(req, sqe);
4933 if (ret < 0)
4934 break;
4935 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004936 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004937 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004938 case IORING_OP_PROVIDE_BUFFERS:
4939 if (sqe) {
4940 ret = io_provide_buffers_prep(req, sqe);
4941 if (ret)
4942 break;
4943 }
4944 ret = io_provide_buffers(req, force_nonblock);
4945 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004946 default:
4947 ret = -EINVAL;
4948 break;
4949 }
4950
Jens Axboedef596e2019-01-09 08:59:42 -07004951 if (ret)
4952 return ret;
4953
4954 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004955 const bool in_async = io_wq_current_is_worker();
4956
Jens Axboe9e645e112019-05-10 16:07:28 -06004957 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004958 return -EAGAIN;
4959
Jens Axboe11ba8202020-01-15 21:51:17 -07004960 /* workqueue context doesn't hold uring_lock, grab it now */
4961 if (in_async)
4962 mutex_lock(&ctx->uring_lock);
4963
Jens Axboedef596e2019-01-09 08:59:42 -07004964 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004965
4966 if (in_async)
4967 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004968 }
4969
4970 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004971}
4972
Jens Axboe561fb042019-10-24 07:25:42 -06004973static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004974{
Jens Axboe561fb042019-10-24 07:25:42 -06004975 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004976 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004977 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004978
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004979 /* if NO_CANCEL is set, we must still run the work */
4980 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4981 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004982 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004983 }
Jens Axboe31b51512019-01-18 22:56:34 -07004984
Jens Axboe561fb042019-10-24 07:25:42 -06004985 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06004986 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03004987 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004988 /*
4989 * We can get EAGAIN for polled IO even though we're
4990 * forcing a sync submission from here, since we can't
4991 * wait for request slots on the block side.
4992 */
4993 if (ret != -EAGAIN)
4994 break;
4995 cond_resched();
4996 } while (1);
4997 }
Jens Axboe31b51512019-01-18 22:56:34 -07004998
Jens Axboe561fb042019-10-24 07:25:42 -06004999 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005000 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005001 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005002 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005003 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005004
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005005 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005006}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005007
Jens Axboe15b71ab2019-12-11 11:20:36 -07005008static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005009{
Jens Axboed3656342019-12-18 09:50:26 -07005010 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005011 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07005012 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07005013 return 0;
5014 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06005015}
5016
Jens Axboe65e19f52019-10-26 07:20:21 -06005017static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5018 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005019{
Jens Axboe65e19f52019-10-26 07:20:21 -06005020 struct fixed_file_table *table;
5021
Jens Axboe05f3fb32019-12-09 11:22:50 -07005022 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5023 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06005024}
5025
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005026static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5027 int fd, struct file **out_file, bool fixed)
5028{
5029 struct io_ring_ctx *ctx = req->ctx;
5030 struct file *file;
5031
5032 if (fixed) {
5033 if (unlikely(!ctx->file_data ||
5034 (unsigned) fd >= ctx->nr_user_files))
5035 return -EBADF;
5036 fd = array_index_nospec(fd, ctx->nr_user_files);
5037 file = io_file_from_index(ctx, fd);
5038 if (!file)
5039 return -EBADF;
5040 percpu_ref_get(&ctx->file_data->refs);
5041 } else {
5042 trace_io_uring_file_get(ctx, fd);
5043 file = __io_file_get(state, fd);
5044 if (unlikely(!file))
5045 return -EBADF;
5046 }
5047
5048 *out_file = file;
5049 return 0;
5050}
5051
Jens Axboe3529d8c2019-12-19 18:24:38 -07005052static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
5053 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06005054{
5055 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07005056 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005057 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005058
Jens Axboe3529d8c2019-12-19 18:24:38 -07005059 flags = READ_ONCE(sqe->flags);
5060 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06005061
Jens Axboed3656342019-12-18 09:50:26 -07005062 if (!io_req_needs_file(req, fd))
5063 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06005064
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005065 fixed = (flags & IOSQE_FIXED_FILE);
5066 if (unlikely(!fixed && req->needs_fixed_file))
5067 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005068
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005069 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005070}
5071
Jackie Liua197f662019-11-08 08:09:12 -07005072static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005073{
Jens Axboefcb323c2019-10-24 12:39:47 -06005074 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005075 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005076
Jens Axboef86cd202020-01-29 13:46:44 -07005077 if (req->work.files)
5078 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005079 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005080 return -EBADF;
5081
Jens Axboefcb323c2019-10-24 12:39:47 -06005082 rcu_read_lock();
5083 spin_lock_irq(&ctx->inflight_lock);
5084 /*
5085 * We use the f_ops->flush() handler to ensure that we can flush
5086 * out work accessing these files if the fd is closed. Check if
5087 * the fd has changed since we started down this path, and disallow
5088 * this operation if it has.
5089 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005090 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005091 list_add(&req->inflight_entry, &ctx->inflight_list);
5092 req->flags |= REQ_F_INFLIGHT;
5093 req->work.files = current->files;
5094 ret = 0;
5095 }
5096 spin_unlock_irq(&ctx->inflight_lock);
5097 rcu_read_unlock();
5098
5099 return ret;
5100}
5101
Jens Axboe2665abf2019-11-05 12:40:47 -07005102static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5103{
Jens Axboead8a48a2019-11-15 08:49:11 -07005104 struct io_timeout_data *data = container_of(timer,
5105 struct io_timeout_data, timer);
5106 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005107 struct io_ring_ctx *ctx = req->ctx;
5108 struct io_kiocb *prev = NULL;
5109 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005110
5111 spin_lock_irqsave(&ctx->completion_lock, flags);
5112
5113 /*
5114 * We don't expect the list to be empty, that will only happen if we
5115 * race with the completion of the linked work.
5116 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005117 if (!list_empty(&req->link_list)) {
5118 prev = list_entry(req->link_list.prev, struct io_kiocb,
5119 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005120 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005121 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005122 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5123 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005124 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005125 }
5126
5127 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5128
5129 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005130 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005131 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005132 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005133 } else {
5134 io_cqring_add_event(req, -ETIME);
5135 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005136 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005137 return HRTIMER_NORESTART;
5138}
5139
Jens Axboead8a48a2019-11-15 08:49:11 -07005140static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005141{
Jens Axboe76a46e02019-11-10 23:34:16 -07005142 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005143
Jens Axboe76a46e02019-11-10 23:34:16 -07005144 /*
5145 * If the list is now empty, then our linked request finished before
5146 * we got a chance to setup the timer
5147 */
5148 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005149 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005150 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005151
Jens Axboead8a48a2019-11-15 08:49:11 -07005152 data->timer.function = io_link_timeout_fn;
5153 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5154 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005155 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005156 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005157
Jens Axboe2665abf2019-11-05 12:40:47 -07005158 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005159 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005160}
5161
Jens Axboead8a48a2019-11-15 08:49:11 -07005162static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005163{
5164 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005165
Jens Axboe2665abf2019-11-05 12:40:47 -07005166 if (!(req->flags & REQ_F_LINK))
5167 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005168 /* for polled retry, if flag is set, we already went through here */
5169 if (req->flags & REQ_F_POLLED)
5170 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005171
Pavel Begunkov44932332019-12-05 16:16:35 +03005172 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5173 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005174 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005175 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005176
Jens Axboe76a46e02019-11-10 23:34:16 -07005177 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005178 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005179}
5180
Jens Axboe3529d8c2019-12-19 18:24:38 -07005181static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005182{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005183 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005184 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005185 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005186 int ret;
5187
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005188again:
5189 linked_timeout = io_prep_linked_timeout(req);
5190
Jens Axboe193155c2020-02-22 23:22:19 -07005191 if (req->work.creds && req->work.creds != current_cred()) {
5192 if (old_creds)
5193 revert_creds(old_creds);
5194 if (old_creds == req->work.creds)
5195 old_creds = NULL; /* restored original creds */
5196 else
5197 old_creds = override_creds(req->work.creds);
5198 }
5199
Pavel Begunkov014db002020-03-03 21:33:12 +03005200 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005201
5202 /*
5203 * We async punt it if the file wasn't marked NOWAIT, or if the file
5204 * doesn't support non-blocking read/write attempts
5205 */
5206 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5207 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005208 if (io_arm_poll_handler(req)) {
5209 if (linked_timeout)
5210 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005211 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005212 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005213punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005214 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005215 ret = io_grab_files(req);
5216 if (ret)
5217 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005218 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005219
5220 /*
5221 * Queued up for async execution, worker will release
5222 * submit reference when the iocb is actually submitted.
5223 */
5224 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005225 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005226 }
Jens Axboee65ef562019-03-12 10:16:44 -06005227
Jens Axboefcb323c2019-10-24 12:39:47 -06005228err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005229 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005230 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005231 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005232
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005233 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005234 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005235 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005236 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005237 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005238 }
5239
Jens Axboee65ef562019-03-12 10:16:44 -06005240 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005241 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005242 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005243 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005244 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005245 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005246 if (nxt) {
5247 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005248
5249 if (req->flags & REQ_F_FORCE_ASYNC)
5250 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005251 goto again;
5252 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005253exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005254 if (old_creds)
5255 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005256}
5257
Jens Axboe3529d8c2019-12-19 18:24:38 -07005258static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005259{
5260 int ret;
5261
Jens Axboe3529d8c2019-12-19 18:24:38 -07005262 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005263 if (ret) {
5264 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005265fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005266 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005267 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005268 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005269 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005270 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005271 ret = io_req_defer_prep(req, sqe);
5272 if (unlikely(ret < 0))
5273 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005274 /*
5275 * Never try inline submit of IOSQE_ASYNC is set, go straight
5276 * to async execution.
5277 */
5278 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5279 io_queue_async_work(req);
5280 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005281 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005282 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005283}
5284
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005285static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005286{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005287 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005288 io_cqring_add_event(req, -ECANCELED);
5289 io_double_put_req(req);
5290 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005291 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005292}
5293
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005294#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboebcda7ba2020-02-23 16:42:51 -07005295 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5296 IOSQE_BUFFER_SELECT)
Jens Axboe9e645e112019-05-10 16:07:28 -06005297
Jens Axboe3529d8c2019-12-19 18:24:38 -07005298static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5299 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005300{
Jackie Liua197f662019-11-08 08:09:12 -07005301 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005302 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005303 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005304
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005305 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005306
5307 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005308 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005309 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005310 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005311 }
5312
Jens Axboebcda7ba2020-02-23 16:42:51 -07005313 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5314 !io_op_defs[req->opcode].buffer_select) {
5315 ret = -EOPNOTSUPP;
5316 goto err_req;
5317 }
5318
Jens Axboe75c6a032020-01-28 10:15:23 -07005319 id = READ_ONCE(sqe->personality);
5320 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005321 req->work.creds = idr_find(&ctx->personality_idr, id);
5322 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005323 ret = -EINVAL;
5324 goto err_req;
5325 }
Jens Axboe193155c2020-02-22 23:22:19 -07005326 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005327 }
5328
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005329 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005330 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
Jens Axboebcda7ba2020-02-23 16:42:51 -07005331 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5332 IOSQE_BUFFER_SELECT);
Jens Axboe9e645e112019-05-10 16:07:28 -06005333
Jens Axboe3529d8c2019-12-19 18:24:38 -07005334 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005335 if (unlikely(ret)) {
5336err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005337 io_cqring_add_event(req, ret);
5338 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005339 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005340 }
5341
Jens Axboe9e645e112019-05-10 16:07:28 -06005342 /*
5343 * If we already have a head request, queue this one for async
5344 * submittal once the head completes. If we don't have a head but
5345 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5346 * submitted sync once the chain is complete. If none of those
5347 * conditions are true (normal request), then just queue it.
5348 */
5349 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005350 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005351
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005352 /*
5353 * Taking sequential execution of a link, draining both sides
5354 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5355 * requests in the link. So, it drains the head and the
5356 * next after the link request. The last one is done via
5357 * drain_next flag to persist the effect across calls.
5358 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005359 if (sqe_flags & IOSQE_IO_DRAIN) {
5360 head->flags |= REQ_F_IO_DRAIN;
5361 ctx->drain_next = 1;
5362 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005363 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005364 ret = -EAGAIN;
5365 goto err_req;
5366 }
5367
Jens Axboe3529d8c2019-12-19 18:24:38 -07005368 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005369 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005370 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005371 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005372 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005373 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005374 trace_io_uring_link(ctx, req, head);
5375 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005376
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005377 /* last request of a link, enqueue the link */
5378 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5379 io_queue_link_head(head);
5380 *link = NULL;
5381 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005382 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005383 if (unlikely(ctx->drain_next)) {
5384 req->flags |= REQ_F_IO_DRAIN;
5385 req->ctx->drain_next = 0;
5386 }
5387 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5388 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005389 INIT_LIST_HEAD(&req->link_list);
5390 ret = io_req_defer_prep(req, sqe);
5391 if (ret)
5392 req->flags |= REQ_F_FAIL_LINK;
5393 *link = req;
5394 } else {
5395 io_queue_sqe(req, sqe);
5396 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005397 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005398
5399 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005400}
5401
Jens Axboe9a56a232019-01-09 09:06:50 -07005402/*
5403 * Batched submission is done, ensure local IO is flushed out.
5404 */
5405static void io_submit_state_end(struct io_submit_state *state)
5406{
5407 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005408 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005409 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005410 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005411}
5412
5413/*
5414 * Start submission side cache.
5415 */
5416static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005417 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005418{
5419 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005420 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005421 state->file = NULL;
5422 state->ios_left = max_ios;
5423}
5424
Jens Axboe2b188cc2019-01-07 10:46:33 -07005425static void io_commit_sqring(struct io_ring_ctx *ctx)
5426{
Hristo Venev75b28af2019-08-26 17:23:46 +00005427 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005428
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005429 /*
5430 * Ensure any loads from the SQEs are done at this point,
5431 * since once we write the new head, the application could
5432 * write new data to them.
5433 */
5434 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005435}
5436
5437/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005438 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005439 * that is mapped by userspace. This means that care needs to be taken to
5440 * ensure that reads are stable, as we cannot rely on userspace always
5441 * being a good citizen. If members of the sqe are validated and then later
5442 * used, it's important that those reads are done through READ_ONCE() to
5443 * prevent a re-load down the line.
5444 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005445static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5446 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005447{
Hristo Venev75b28af2019-08-26 17:23:46 +00005448 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005449 unsigned head;
5450
5451 /*
5452 * The cached sq head (or cq tail) serves two purposes:
5453 *
5454 * 1) allows us to batch the cost of updating the user visible
5455 * head updates.
5456 * 2) allows the kernel side to track the head on its own, even
5457 * though the application is the one updating it.
5458 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005459 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005460 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005461 /*
5462 * All io need record the previous position, if LINK vs DARIN,
5463 * it can be used to mark the position of the first IO in the
5464 * link list.
5465 */
5466 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005467 *sqe_ptr = &ctx->sq_sqes[head];
5468 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5469 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005470 ctx->cached_sq_head++;
5471 return true;
5472 }
5473
5474 /* drop invalid entries */
5475 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005476 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005477 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005478 return false;
5479}
5480
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005481static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005482 struct file *ring_file, int ring_fd,
5483 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005484{
5485 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005486 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005487 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005488 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005489
Jens Axboec4a2ed72019-11-21 21:01:26 -07005490 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005491 if (test_bit(0, &ctx->sq_check_overflow)) {
5492 if (!list_empty(&ctx->cq_overflow_list) &&
5493 !io_cqring_overflow_flush(ctx, false))
5494 return -EBUSY;
5495 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005496
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005497 /* make sure SQ entry isn't read before tail */
5498 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005499
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005500 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5501 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005502
5503 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005504 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005505 statep = &state;
5506 }
5507
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005508 ctx->ring_fd = ring_fd;
5509 ctx->ring_file = ring_file;
5510
Jens Axboe6c271ce2019-01-10 11:22:30 -07005511 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005512 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005513 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005514 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005515
Pavel Begunkov196be952019-11-07 01:41:06 +03005516 req = io_get_req(ctx, statep);
5517 if (unlikely(!req)) {
5518 if (!submitted)
5519 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005520 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005521 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005522 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005523 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005524 break;
5525 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005526
Jens Axboed3656342019-12-18 09:50:26 -07005527 /* will complete beyond this point, count as submitted */
5528 submitted++;
5529
5530 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005531 err = -EINVAL;
5532fail_req:
5533 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005534 io_double_put_req(req);
5535 break;
5536 }
5537
5538 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005539 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005540 if (unlikely(mm_fault)) {
5541 err = -EFAULT;
5542 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005543 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005544 use_mm(ctx->sqo_mm);
5545 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005546 }
5547
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005548 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005549 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5550 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005551 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005552 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005553 }
5554
Pavel Begunkov9466f432020-01-25 22:34:01 +03005555 if (unlikely(submitted != nr)) {
5556 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5557
5558 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5559 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005560 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005561 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005562 if (statep)
5563 io_submit_state_end(&state);
5564
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005565 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5566 io_commit_sqring(ctx);
5567
Jens Axboe6c271ce2019-01-10 11:22:30 -07005568 return submitted;
5569}
5570
5571static int io_sq_thread(void *data)
5572{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005573 struct io_ring_ctx *ctx = data;
5574 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005575 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005576 mm_segment_t old_fs;
5577 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005578 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005579 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005580
Jens Axboe206aefd2019-11-07 18:27:42 -07005581 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005582
Jens Axboe6c271ce2019-01-10 11:22:30 -07005583 old_fs = get_fs();
5584 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005585 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005586
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005587 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005588 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005589 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005590
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005591 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005592 unsigned nr_events = 0;
5593
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005594 mutex_lock(&ctx->uring_lock);
5595 if (!list_empty(&ctx->poll_list))
5596 io_iopoll_getevents(ctx, &nr_events, 0);
5597 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005598 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005599 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005600 }
5601
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005602 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005603
5604 /*
5605 * If submit got -EBUSY, flag us as needing the application
5606 * to enter the kernel to reap and flush events.
5607 */
5608 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005609 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005610 * Drop cur_mm before scheduling, we can't hold it for
5611 * long periods (or over schedule()). Do this before
5612 * adding ourselves to the waitqueue, as the unuse/drop
5613 * may sleep.
5614 */
5615 if (cur_mm) {
5616 unuse_mm(cur_mm);
5617 mmput(cur_mm);
5618 cur_mm = NULL;
5619 }
5620
5621 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005622 * We're polling. If we're within the defined idle
5623 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005624 * to sleep. The exception is if we got EBUSY doing
5625 * more IO, we should wait for the application to
5626 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005627 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005628 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005629 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5630 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005631 if (current->task_works)
5632 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005633 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005634 continue;
5635 }
5636
Jens Axboe6c271ce2019-01-10 11:22:30 -07005637 prepare_to_wait(&ctx->sqo_wait, &wait,
5638 TASK_INTERRUPTIBLE);
5639
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005640 /*
5641 * While doing polled IO, before going to sleep, we need
5642 * to check if there are new reqs added to poll_list, it
5643 * is because reqs may have been punted to io worker and
5644 * will be added to poll_list later, hence check the
5645 * poll_list again.
5646 */
5647 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5648 !list_empty_careful(&ctx->poll_list)) {
5649 finish_wait(&ctx->sqo_wait, &wait);
5650 continue;
5651 }
5652
Jens Axboe6c271ce2019-01-10 11:22:30 -07005653 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005654 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005655 /* make sure to read SQ tail after writing flags */
5656 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005657
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005658 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005659 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005660 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005661 finish_wait(&ctx->sqo_wait, &wait);
5662 break;
5663 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005664 if (current->task_works) {
5665 task_work_run();
5666 continue;
5667 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005668 if (signal_pending(current))
5669 flush_signals(current);
5670 schedule();
5671 finish_wait(&ctx->sqo_wait, &wait);
5672
Hristo Venev75b28af2019-08-26 17:23:46 +00005673 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005674 continue;
5675 }
5676 finish_wait(&ctx->sqo_wait, &wait);
5677
Hristo Venev75b28af2019-08-26 17:23:46 +00005678 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005679 }
5680
Jens Axboe8a4955f2019-12-09 14:52:35 -07005681 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005682 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005683 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005684 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005685 }
5686
Jens Axboeb41e9852020-02-17 09:52:41 -07005687 if (current->task_works)
5688 task_work_run();
5689
Jens Axboe6c271ce2019-01-10 11:22:30 -07005690 set_fs(old_fs);
5691 if (cur_mm) {
5692 unuse_mm(cur_mm);
5693 mmput(cur_mm);
5694 }
Jens Axboe181e4482019-11-25 08:52:30 -07005695 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005696
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005697 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005698
Jens Axboe6c271ce2019-01-10 11:22:30 -07005699 return 0;
5700}
5701
Jens Axboebda52162019-09-24 13:47:15 -06005702struct io_wait_queue {
5703 struct wait_queue_entry wq;
5704 struct io_ring_ctx *ctx;
5705 unsigned to_wait;
5706 unsigned nr_timeouts;
5707};
5708
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005709static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005710{
5711 struct io_ring_ctx *ctx = iowq->ctx;
5712
5713 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005714 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005715 * started waiting. For timeouts, we always want to return to userspace,
5716 * regardless of event count.
5717 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005718 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005719 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5720}
5721
5722static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5723 int wake_flags, void *key)
5724{
5725 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5726 wq);
5727
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005728 /* use noflush == true, as we can't safely rely on locking context */
5729 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005730 return -1;
5731
5732 return autoremove_wake_function(curr, mode, wake_flags, key);
5733}
5734
Jens Axboe2b188cc2019-01-07 10:46:33 -07005735/*
5736 * Wait until events become available, if we don't already have some. The
5737 * application must reap them itself, as they reside on the shared cq ring.
5738 */
5739static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5740 const sigset_t __user *sig, size_t sigsz)
5741{
Jens Axboebda52162019-09-24 13:47:15 -06005742 struct io_wait_queue iowq = {
5743 .wq = {
5744 .private = current,
5745 .func = io_wake_function,
5746 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5747 },
5748 .ctx = ctx,
5749 .to_wait = min_events,
5750 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005751 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005752 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005753
Jens Axboeb41e9852020-02-17 09:52:41 -07005754 do {
5755 if (io_cqring_events(ctx, false) >= min_events)
5756 return 0;
5757 if (!current->task_works)
5758 break;
5759 task_work_run();
5760 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005761
5762 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005763#ifdef CONFIG_COMPAT
5764 if (in_compat_syscall())
5765 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005766 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005767 else
5768#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005769 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005770
Jens Axboe2b188cc2019-01-07 10:46:33 -07005771 if (ret)
5772 return ret;
5773 }
5774
Jens Axboebda52162019-09-24 13:47:15 -06005775 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005776 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005777 do {
5778 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5779 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005780 if (current->task_works)
5781 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005782 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005783 break;
5784 schedule();
5785 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005786 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005787 break;
5788 }
5789 } while (1);
5790 finish_wait(&ctx->wait, &iowq.wq);
5791
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005792 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005793
Hristo Venev75b28af2019-08-26 17:23:46 +00005794 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005795}
5796
Jens Axboe6b063142019-01-10 22:13:58 -07005797static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5798{
5799#if defined(CONFIG_UNIX)
5800 if (ctx->ring_sock) {
5801 struct sock *sock = ctx->ring_sock->sk;
5802 struct sk_buff *skb;
5803
5804 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5805 kfree_skb(skb);
5806 }
5807#else
5808 int i;
5809
Jens Axboe65e19f52019-10-26 07:20:21 -06005810 for (i = 0; i < ctx->nr_user_files; i++) {
5811 struct file *file;
5812
5813 file = io_file_from_index(ctx, i);
5814 if (file)
5815 fput(file);
5816 }
Jens Axboe6b063142019-01-10 22:13:58 -07005817#endif
5818}
5819
Jens Axboe05f3fb32019-12-09 11:22:50 -07005820static void io_file_ref_kill(struct percpu_ref *ref)
5821{
5822 struct fixed_file_data *data;
5823
5824 data = container_of(ref, struct fixed_file_data, refs);
5825 complete(&data->done);
5826}
5827
Jens Axboe6b063142019-01-10 22:13:58 -07005828static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5829{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005830 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005831 unsigned nr_tables, i;
5832
Jens Axboe05f3fb32019-12-09 11:22:50 -07005833 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005834 return -ENXIO;
5835
Jens Axboe05f3fb32019-12-09 11:22:50 -07005836 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005837 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005838 wait_for_completion(&data->done);
5839 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005840 percpu_ref_exit(&data->refs);
5841
Jens Axboe6b063142019-01-10 22:13:58 -07005842 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005843 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5844 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005845 kfree(data->table[i].files);
5846 kfree(data->table);
5847 kfree(data);
5848 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005849 ctx->nr_user_files = 0;
5850 return 0;
5851}
5852
Jens Axboe6c271ce2019-01-10 11:22:30 -07005853static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5854{
5855 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005856 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005857 /*
5858 * The park is a bit of a work-around, without it we get
5859 * warning spews on shutdown with SQPOLL set and affinity
5860 * set to a single CPU.
5861 */
Jens Axboe06058632019-04-13 09:26:03 -06005862 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005863 kthread_stop(ctx->sqo_thread);
5864 ctx->sqo_thread = NULL;
5865 }
5866}
5867
Jens Axboe6b063142019-01-10 22:13:58 -07005868static void io_finish_async(struct io_ring_ctx *ctx)
5869{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005870 io_sq_thread_stop(ctx);
5871
Jens Axboe561fb042019-10-24 07:25:42 -06005872 if (ctx->io_wq) {
5873 io_wq_destroy(ctx->io_wq);
5874 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005875 }
5876}
5877
5878#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005879/*
5880 * Ensure the UNIX gc is aware of our file set, so we are certain that
5881 * the io_uring can be safely unregistered on process exit, even if we have
5882 * loops in the file referencing.
5883 */
5884static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5885{
5886 struct sock *sk = ctx->ring_sock->sk;
5887 struct scm_fp_list *fpl;
5888 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005889 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005890
5891 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5892 unsigned long inflight = ctx->user->unix_inflight + nr;
5893
5894 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5895 return -EMFILE;
5896 }
5897
5898 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5899 if (!fpl)
5900 return -ENOMEM;
5901
5902 skb = alloc_skb(0, GFP_KERNEL);
5903 if (!skb) {
5904 kfree(fpl);
5905 return -ENOMEM;
5906 }
5907
5908 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005909
Jens Axboe08a45172019-10-03 08:11:03 -06005910 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005911 fpl->user = get_uid(ctx->user);
5912 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005913 struct file *file = io_file_from_index(ctx, i + offset);
5914
5915 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005916 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005917 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005918 unix_inflight(fpl->user, fpl->fp[nr_files]);
5919 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005920 }
5921
Jens Axboe08a45172019-10-03 08:11:03 -06005922 if (nr_files) {
5923 fpl->max = SCM_MAX_FD;
5924 fpl->count = nr_files;
5925 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005926 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005927 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5928 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005929
Jens Axboe08a45172019-10-03 08:11:03 -06005930 for (i = 0; i < nr_files; i++)
5931 fput(fpl->fp[i]);
5932 } else {
5933 kfree_skb(skb);
5934 kfree(fpl);
5935 }
Jens Axboe6b063142019-01-10 22:13:58 -07005936
5937 return 0;
5938}
5939
5940/*
5941 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5942 * causes regular reference counting to break down. We rely on the UNIX
5943 * garbage collection to take care of this problem for us.
5944 */
5945static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5946{
5947 unsigned left, total;
5948 int ret = 0;
5949
5950 total = 0;
5951 left = ctx->nr_user_files;
5952 while (left) {
5953 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005954
5955 ret = __io_sqe_files_scm(ctx, this_files, total);
5956 if (ret)
5957 break;
5958 left -= this_files;
5959 total += this_files;
5960 }
5961
5962 if (!ret)
5963 return 0;
5964
5965 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005966 struct file *file = io_file_from_index(ctx, total);
5967
5968 if (file)
5969 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005970 total++;
5971 }
5972
5973 return ret;
5974}
5975#else
5976static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5977{
5978 return 0;
5979}
5980#endif
5981
Jens Axboe65e19f52019-10-26 07:20:21 -06005982static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5983 unsigned nr_files)
5984{
5985 int i;
5986
5987 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005988 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005989 unsigned this_files;
5990
5991 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5992 table->files = kcalloc(this_files, sizeof(struct file *),
5993 GFP_KERNEL);
5994 if (!table->files)
5995 break;
5996 nr_files -= this_files;
5997 }
5998
5999 if (i == nr_tables)
6000 return 0;
6001
6002 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006003 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006004 kfree(table->files);
6005 }
6006 return 1;
6007}
6008
Jens Axboe05f3fb32019-12-09 11:22:50 -07006009static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006010{
6011#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006012 struct sock *sock = ctx->ring_sock->sk;
6013 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6014 struct sk_buff *skb;
6015 int i;
6016
6017 __skb_queue_head_init(&list);
6018
6019 /*
6020 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6021 * remove this entry and rearrange the file array.
6022 */
6023 skb = skb_dequeue(head);
6024 while (skb) {
6025 struct scm_fp_list *fp;
6026
6027 fp = UNIXCB(skb).fp;
6028 for (i = 0; i < fp->count; i++) {
6029 int left;
6030
6031 if (fp->fp[i] != file)
6032 continue;
6033
6034 unix_notinflight(fp->user, fp->fp[i]);
6035 left = fp->count - 1 - i;
6036 if (left) {
6037 memmove(&fp->fp[i], &fp->fp[i + 1],
6038 left * sizeof(struct file *));
6039 }
6040 fp->count--;
6041 if (!fp->count) {
6042 kfree_skb(skb);
6043 skb = NULL;
6044 } else {
6045 __skb_queue_tail(&list, skb);
6046 }
6047 fput(file);
6048 file = NULL;
6049 break;
6050 }
6051
6052 if (!file)
6053 break;
6054
6055 __skb_queue_tail(&list, skb);
6056
6057 skb = skb_dequeue(head);
6058 }
6059
6060 if (skb_peek(&list)) {
6061 spin_lock_irq(&head->lock);
6062 while ((skb = __skb_dequeue(&list)) != NULL)
6063 __skb_queue_tail(head, skb);
6064 spin_unlock_irq(&head->lock);
6065 }
6066#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006067 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006068#endif
6069}
6070
Jens Axboe05f3fb32019-12-09 11:22:50 -07006071struct io_file_put {
6072 struct llist_node llist;
6073 struct file *file;
6074 struct completion *done;
6075};
6076
Jens Axboe2faf8522020-02-04 19:54:55 -07006077static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006078{
6079 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006080 struct llist_node *node;
6081
Jens Axboe05f3fb32019-12-09 11:22:50 -07006082 while ((node = llist_del_all(&data->put_llist)) != NULL) {
6083 llist_for_each_entry_safe(pfile, tmp, node, llist) {
6084 io_ring_file_put(data->ctx, pfile->file);
6085 if (pfile->done)
6086 complete(pfile->done);
6087 else
6088 kfree(pfile);
6089 }
6090 }
Jens Axboe2faf8522020-02-04 19:54:55 -07006091}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006092
Jens Axboe2faf8522020-02-04 19:54:55 -07006093static void io_ring_file_ref_switch(struct work_struct *work)
6094{
6095 struct fixed_file_data *data;
6096
6097 data = container_of(work, struct fixed_file_data, ref_work);
6098 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006099 percpu_ref_switch_to_percpu(&data->refs);
6100}
6101
6102static void io_file_data_ref_zero(struct percpu_ref *ref)
6103{
6104 struct fixed_file_data *data;
6105
6106 data = container_of(ref, struct fixed_file_data, refs);
6107
Jens Axboe2faf8522020-02-04 19:54:55 -07006108 /*
6109 * We can't safely switch from inside this context, punt to wq. If
6110 * the table ref is going away, the table is being unregistered.
6111 * Don't queue up the async work for that case, the caller will
6112 * handle it.
6113 */
6114 if (!percpu_ref_is_dying(&data->refs))
6115 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006116}
6117
6118static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6119 unsigned nr_args)
6120{
6121 __s32 __user *fds = (__s32 __user *) arg;
6122 unsigned nr_tables;
6123 struct file *file;
6124 int fd, ret = 0;
6125 unsigned i;
6126
6127 if (ctx->file_data)
6128 return -EBUSY;
6129 if (!nr_args)
6130 return -EINVAL;
6131 if (nr_args > IORING_MAX_FIXED_FILES)
6132 return -EMFILE;
6133
6134 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6135 if (!ctx->file_data)
6136 return -ENOMEM;
6137 ctx->file_data->ctx = ctx;
6138 init_completion(&ctx->file_data->done);
6139
6140 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6141 ctx->file_data->table = kcalloc(nr_tables,
6142 sizeof(struct fixed_file_table),
6143 GFP_KERNEL);
6144 if (!ctx->file_data->table) {
6145 kfree(ctx->file_data);
6146 ctx->file_data = NULL;
6147 return -ENOMEM;
6148 }
6149
6150 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
6151 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6152 kfree(ctx->file_data->table);
6153 kfree(ctx->file_data);
6154 ctx->file_data = NULL;
6155 return -ENOMEM;
6156 }
6157 ctx->file_data->put_llist.first = NULL;
6158 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
6159
6160 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6161 percpu_ref_exit(&ctx->file_data->refs);
6162 kfree(ctx->file_data->table);
6163 kfree(ctx->file_data);
6164 ctx->file_data = NULL;
6165 return -ENOMEM;
6166 }
6167
6168 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6169 struct fixed_file_table *table;
6170 unsigned index;
6171
6172 ret = -EFAULT;
6173 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6174 break;
6175 /* allow sparse sets */
6176 if (fd == -1) {
6177 ret = 0;
6178 continue;
6179 }
6180
6181 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6182 index = i & IORING_FILE_TABLE_MASK;
6183 file = fget(fd);
6184
6185 ret = -EBADF;
6186 if (!file)
6187 break;
6188
6189 /*
6190 * Don't allow io_uring instances to be registered. If UNIX
6191 * isn't enabled, then this causes a reference cycle and this
6192 * instance can never get freed. If UNIX is enabled we'll
6193 * handle it just fine, but there's still no point in allowing
6194 * a ring fd as it doesn't support regular read/write anyway.
6195 */
6196 if (file->f_op == &io_uring_fops) {
6197 fput(file);
6198 break;
6199 }
6200 ret = 0;
6201 table->files[index] = file;
6202 }
6203
6204 if (ret) {
6205 for (i = 0; i < ctx->nr_user_files; i++) {
6206 file = io_file_from_index(ctx, i);
6207 if (file)
6208 fput(file);
6209 }
6210 for (i = 0; i < nr_tables; i++)
6211 kfree(ctx->file_data->table[i].files);
6212
6213 kfree(ctx->file_data->table);
6214 kfree(ctx->file_data);
6215 ctx->file_data = NULL;
6216 ctx->nr_user_files = 0;
6217 return ret;
6218 }
6219
6220 ret = io_sqe_files_scm(ctx);
6221 if (ret)
6222 io_sqe_files_unregister(ctx);
6223
6224 return ret;
6225}
6226
Jens Axboec3a31e62019-10-03 13:59:56 -06006227static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6228 int index)
6229{
6230#if defined(CONFIG_UNIX)
6231 struct sock *sock = ctx->ring_sock->sk;
6232 struct sk_buff_head *head = &sock->sk_receive_queue;
6233 struct sk_buff *skb;
6234
6235 /*
6236 * See if we can merge this file into an existing skb SCM_RIGHTS
6237 * file set. If there's no room, fall back to allocating a new skb
6238 * and filling it in.
6239 */
6240 spin_lock_irq(&head->lock);
6241 skb = skb_peek(head);
6242 if (skb) {
6243 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6244
6245 if (fpl->count < SCM_MAX_FD) {
6246 __skb_unlink(skb, head);
6247 spin_unlock_irq(&head->lock);
6248 fpl->fp[fpl->count] = get_file(file);
6249 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6250 fpl->count++;
6251 spin_lock_irq(&head->lock);
6252 __skb_queue_head(head, skb);
6253 } else {
6254 skb = NULL;
6255 }
6256 }
6257 spin_unlock_irq(&head->lock);
6258
6259 if (skb) {
6260 fput(file);
6261 return 0;
6262 }
6263
6264 return __io_sqe_files_scm(ctx, 1, index);
6265#else
6266 return 0;
6267#endif
6268}
6269
Jens Axboe05f3fb32019-12-09 11:22:50 -07006270static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06006271{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006272 struct fixed_file_data *data;
6273
Jens Axboedd3db2a2020-02-26 10:23:43 -07006274 /*
6275 * Juggle reference to ensure we hit zero, if needed, so we can
6276 * switch back to percpu mode
6277 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006278 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07006279 percpu_ref_put(&data->refs);
6280 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006281}
6282
6283static bool io_queue_file_removal(struct fixed_file_data *data,
6284 struct file *file)
6285{
6286 struct io_file_put *pfile, pfile_stack;
6287 DECLARE_COMPLETION_ONSTACK(done);
6288
6289 /*
6290 * If we fail allocating the struct we need for doing async reomval
6291 * of this file, just punt to sync and wait for it.
6292 */
6293 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
6294 if (!pfile) {
6295 pfile = &pfile_stack;
6296 pfile->done = &done;
6297 }
6298
6299 pfile->file = file;
6300 llist_add(&pfile->llist, &data->put_llist);
6301
6302 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07006303 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006304 wait_for_completion(&done);
6305 flush_work(&data->ref_work);
6306 return false;
6307 }
6308
6309 return true;
6310}
6311
6312static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6313 struct io_uring_files_update *up,
6314 unsigned nr_args)
6315{
6316 struct fixed_file_data *data = ctx->file_data;
6317 bool ref_switch = false;
6318 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006319 __s32 __user *fds;
6320 int fd, i, err;
6321 __u32 done;
6322
Jens Axboe05f3fb32019-12-09 11:22:50 -07006323 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006324 return -EOVERFLOW;
6325 if (done > ctx->nr_user_files)
6326 return -EINVAL;
6327
6328 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006329 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006330 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006331 struct fixed_file_table *table;
6332 unsigned index;
6333
Jens Axboec3a31e62019-10-03 13:59:56 -06006334 err = 0;
6335 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6336 err = -EFAULT;
6337 break;
6338 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006339 i = array_index_nospec(up->offset, ctx->nr_user_files);
6340 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006341 index = i & IORING_FILE_TABLE_MASK;
6342 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006343 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006344 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006345 if (io_queue_file_removal(data, file))
6346 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006347 }
6348 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006349 file = fget(fd);
6350 if (!file) {
6351 err = -EBADF;
6352 break;
6353 }
6354 /*
6355 * Don't allow io_uring instances to be registered. If
6356 * UNIX isn't enabled, then this causes a reference
6357 * cycle and this instance can never get freed. If UNIX
6358 * is enabled we'll handle it just fine, but there's
6359 * still no point in allowing a ring fd as it doesn't
6360 * support regular read/write anyway.
6361 */
6362 if (file->f_op == &io_uring_fops) {
6363 fput(file);
6364 err = -EBADF;
6365 break;
6366 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006367 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006368 err = io_sqe_file_register(ctx, file, i);
6369 if (err)
6370 break;
6371 }
6372 nr_args--;
6373 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006374 up->offset++;
6375 }
6376
Jens Axboedd3db2a2020-02-26 10:23:43 -07006377 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006378 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006379
6380 return done ? done : err;
6381}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006382static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6383 unsigned nr_args)
6384{
6385 struct io_uring_files_update up;
6386
6387 if (!ctx->file_data)
6388 return -ENXIO;
6389 if (!nr_args)
6390 return -EINVAL;
6391 if (copy_from_user(&up, arg, sizeof(up)))
6392 return -EFAULT;
6393 if (up.resv)
6394 return -EINVAL;
6395
6396 return __io_sqe_files_update(ctx, &up, nr_args);
6397}
Jens Axboec3a31e62019-10-03 13:59:56 -06006398
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006399static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006400{
6401 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6402
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006403 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006404 io_put_req(req);
6405}
6406
Pavel Begunkov24369c22020-01-28 03:15:48 +03006407static int io_init_wq_offload(struct io_ring_ctx *ctx,
6408 struct io_uring_params *p)
6409{
6410 struct io_wq_data data;
6411 struct fd f;
6412 struct io_ring_ctx *ctx_attach;
6413 unsigned int concurrency;
6414 int ret = 0;
6415
6416 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006417 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006418
6419 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6420 /* Do QD, or 4 * CPUS, whatever is smallest */
6421 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6422
6423 ctx->io_wq = io_wq_create(concurrency, &data);
6424 if (IS_ERR(ctx->io_wq)) {
6425 ret = PTR_ERR(ctx->io_wq);
6426 ctx->io_wq = NULL;
6427 }
6428 return ret;
6429 }
6430
6431 f = fdget(p->wq_fd);
6432 if (!f.file)
6433 return -EBADF;
6434
6435 if (f.file->f_op != &io_uring_fops) {
6436 ret = -EINVAL;
6437 goto out_fput;
6438 }
6439
6440 ctx_attach = f.file->private_data;
6441 /* @io_wq is protected by holding the fd */
6442 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6443 ret = -EINVAL;
6444 goto out_fput;
6445 }
6446
6447 ctx->io_wq = ctx_attach->io_wq;
6448out_fput:
6449 fdput(f);
6450 return ret;
6451}
6452
Jens Axboe6c271ce2019-01-10 11:22:30 -07006453static int io_sq_offload_start(struct io_ring_ctx *ctx,
6454 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006455{
6456 int ret;
6457
Jens Axboe6c271ce2019-01-10 11:22:30 -07006458 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006459 mmgrab(current->mm);
6460 ctx->sqo_mm = current->mm;
6461
Jens Axboe6c271ce2019-01-10 11:22:30 -07006462 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006463 ret = -EPERM;
6464 if (!capable(CAP_SYS_ADMIN))
6465 goto err;
6466
Jens Axboe917257d2019-04-13 09:28:55 -06006467 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6468 if (!ctx->sq_thread_idle)
6469 ctx->sq_thread_idle = HZ;
6470
Jens Axboe6c271ce2019-01-10 11:22:30 -07006471 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006472 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006473
Jens Axboe917257d2019-04-13 09:28:55 -06006474 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006475 if (cpu >= nr_cpu_ids)
6476 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006477 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006478 goto err;
6479
Jens Axboe6c271ce2019-01-10 11:22:30 -07006480 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6481 ctx, cpu,
6482 "io_uring-sq");
6483 } else {
6484 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6485 "io_uring-sq");
6486 }
6487 if (IS_ERR(ctx->sqo_thread)) {
6488 ret = PTR_ERR(ctx->sqo_thread);
6489 ctx->sqo_thread = NULL;
6490 goto err;
6491 }
6492 wake_up_process(ctx->sqo_thread);
6493 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6494 /* Can't have SQ_AFF without SQPOLL */
6495 ret = -EINVAL;
6496 goto err;
6497 }
6498
Pavel Begunkov24369c22020-01-28 03:15:48 +03006499 ret = io_init_wq_offload(ctx, p);
6500 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006501 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006502
6503 return 0;
6504err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006505 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006506 mmdrop(ctx->sqo_mm);
6507 ctx->sqo_mm = NULL;
6508 return ret;
6509}
6510
6511static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6512{
6513 atomic_long_sub(nr_pages, &user->locked_vm);
6514}
6515
6516static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6517{
6518 unsigned long page_limit, cur_pages, new_pages;
6519
6520 /* Don't allow more pages than we can safely lock */
6521 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6522
6523 do {
6524 cur_pages = atomic_long_read(&user->locked_vm);
6525 new_pages = cur_pages + nr_pages;
6526 if (new_pages > page_limit)
6527 return -ENOMEM;
6528 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6529 new_pages) != cur_pages);
6530
6531 return 0;
6532}
6533
6534static void io_mem_free(void *ptr)
6535{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006536 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006537
Mark Rutland52e04ef2019-04-30 17:30:21 +01006538 if (!ptr)
6539 return;
6540
6541 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006542 if (put_page_testzero(page))
6543 free_compound_page(page);
6544}
6545
6546static void *io_mem_alloc(size_t size)
6547{
6548 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6549 __GFP_NORETRY;
6550
6551 return (void *) __get_free_pages(gfp_flags, get_order(size));
6552}
6553
Hristo Venev75b28af2019-08-26 17:23:46 +00006554static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6555 size_t *sq_offset)
6556{
6557 struct io_rings *rings;
6558 size_t off, sq_array_size;
6559
6560 off = struct_size(rings, cqes, cq_entries);
6561 if (off == SIZE_MAX)
6562 return SIZE_MAX;
6563
6564#ifdef CONFIG_SMP
6565 off = ALIGN(off, SMP_CACHE_BYTES);
6566 if (off == 0)
6567 return SIZE_MAX;
6568#endif
6569
6570 sq_array_size = array_size(sizeof(u32), sq_entries);
6571 if (sq_array_size == SIZE_MAX)
6572 return SIZE_MAX;
6573
6574 if (check_add_overflow(off, sq_array_size, &off))
6575 return SIZE_MAX;
6576
6577 if (sq_offset)
6578 *sq_offset = off;
6579
6580 return off;
6581}
6582
Jens Axboe2b188cc2019-01-07 10:46:33 -07006583static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6584{
Hristo Venev75b28af2019-08-26 17:23:46 +00006585 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006586
Hristo Venev75b28af2019-08-26 17:23:46 +00006587 pages = (size_t)1 << get_order(
6588 rings_size(sq_entries, cq_entries, NULL));
6589 pages += (size_t)1 << get_order(
6590 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006591
Hristo Venev75b28af2019-08-26 17:23:46 +00006592 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006593}
6594
Jens Axboeedafcce2019-01-09 09:16:05 -07006595static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6596{
6597 int i, j;
6598
6599 if (!ctx->user_bufs)
6600 return -ENXIO;
6601
6602 for (i = 0; i < ctx->nr_user_bufs; i++) {
6603 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6604
6605 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006606 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006607
6608 if (ctx->account_mem)
6609 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006610 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006611 imu->nr_bvecs = 0;
6612 }
6613
6614 kfree(ctx->user_bufs);
6615 ctx->user_bufs = NULL;
6616 ctx->nr_user_bufs = 0;
6617 return 0;
6618}
6619
6620static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6621 void __user *arg, unsigned index)
6622{
6623 struct iovec __user *src;
6624
6625#ifdef CONFIG_COMPAT
6626 if (ctx->compat) {
6627 struct compat_iovec __user *ciovs;
6628 struct compat_iovec ciov;
6629
6630 ciovs = (struct compat_iovec __user *) arg;
6631 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6632 return -EFAULT;
6633
Jens Axboed55e5f52019-12-11 16:12:15 -07006634 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006635 dst->iov_len = ciov.iov_len;
6636 return 0;
6637 }
6638#endif
6639 src = (struct iovec __user *) arg;
6640 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6641 return -EFAULT;
6642 return 0;
6643}
6644
6645static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6646 unsigned nr_args)
6647{
6648 struct vm_area_struct **vmas = NULL;
6649 struct page **pages = NULL;
6650 int i, j, got_pages = 0;
6651 int ret = -EINVAL;
6652
6653 if (ctx->user_bufs)
6654 return -EBUSY;
6655 if (!nr_args || nr_args > UIO_MAXIOV)
6656 return -EINVAL;
6657
6658 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6659 GFP_KERNEL);
6660 if (!ctx->user_bufs)
6661 return -ENOMEM;
6662
6663 for (i = 0; i < nr_args; i++) {
6664 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6665 unsigned long off, start, end, ubuf;
6666 int pret, nr_pages;
6667 struct iovec iov;
6668 size_t size;
6669
6670 ret = io_copy_iov(ctx, &iov, arg, i);
6671 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006672 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006673
6674 /*
6675 * Don't impose further limits on the size and buffer
6676 * constraints here, we'll -EINVAL later when IO is
6677 * submitted if they are wrong.
6678 */
6679 ret = -EFAULT;
6680 if (!iov.iov_base || !iov.iov_len)
6681 goto err;
6682
6683 /* arbitrary limit, but we need something */
6684 if (iov.iov_len > SZ_1G)
6685 goto err;
6686
6687 ubuf = (unsigned long) iov.iov_base;
6688 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6689 start = ubuf >> PAGE_SHIFT;
6690 nr_pages = end - start;
6691
6692 if (ctx->account_mem) {
6693 ret = io_account_mem(ctx->user, nr_pages);
6694 if (ret)
6695 goto err;
6696 }
6697
6698 ret = 0;
6699 if (!pages || nr_pages > got_pages) {
6700 kfree(vmas);
6701 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006702 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006703 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006704 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006705 sizeof(struct vm_area_struct *),
6706 GFP_KERNEL);
6707 if (!pages || !vmas) {
6708 ret = -ENOMEM;
6709 if (ctx->account_mem)
6710 io_unaccount_mem(ctx->user, nr_pages);
6711 goto err;
6712 }
6713 got_pages = nr_pages;
6714 }
6715
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006716 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006717 GFP_KERNEL);
6718 ret = -ENOMEM;
6719 if (!imu->bvec) {
6720 if (ctx->account_mem)
6721 io_unaccount_mem(ctx->user, nr_pages);
6722 goto err;
6723 }
6724
6725 ret = 0;
6726 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006727 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006728 FOLL_WRITE | FOLL_LONGTERM,
6729 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006730 if (pret == nr_pages) {
6731 /* don't support file backed memory */
6732 for (j = 0; j < nr_pages; j++) {
6733 struct vm_area_struct *vma = vmas[j];
6734
6735 if (vma->vm_file &&
6736 !is_file_hugepages(vma->vm_file)) {
6737 ret = -EOPNOTSUPP;
6738 break;
6739 }
6740 }
6741 } else {
6742 ret = pret < 0 ? pret : -EFAULT;
6743 }
6744 up_read(&current->mm->mmap_sem);
6745 if (ret) {
6746 /*
6747 * if we did partial map, or found file backed vmas,
6748 * release any pages we did get
6749 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006750 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006751 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006752 if (ctx->account_mem)
6753 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006754 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006755 goto err;
6756 }
6757
6758 off = ubuf & ~PAGE_MASK;
6759 size = iov.iov_len;
6760 for (j = 0; j < nr_pages; j++) {
6761 size_t vec_len;
6762
6763 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6764 imu->bvec[j].bv_page = pages[j];
6765 imu->bvec[j].bv_len = vec_len;
6766 imu->bvec[j].bv_offset = off;
6767 off = 0;
6768 size -= vec_len;
6769 }
6770 /* store original address for later verification */
6771 imu->ubuf = ubuf;
6772 imu->len = iov.iov_len;
6773 imu->nr_bvecs = nr_pages;
6774
6775 ctx->nr_user_bufs++;
6776 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006777 kvfree(pages);
6778 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006779 return 0;
6780err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006781 kvfree(pages);
6782 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006783 io_sqe_buffer_unregister(ctx);
6784 return ret;
6785}
6786
Jens Axboe9b402842019-04-11 11:45:41 -06006787static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6788{
6789 __s32 __user *fds = arg;
6790 int fd;
6791
6792 if (ctx->cq_ev_fd)
6793 return -EBUSY;
6794
6795 if (copy_from_user(&fd, fds, sizeof(*fds)))
6796 return -EFAULT;
6797
6798 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6799 if (IS_ERR(ctx->cq_ev_fd)) {
6800 int ret = PTR_ERR(ctx->cq_ev_fd);
6801 ctx->cq_ev_fd = NULL;
6802 return ret;
6803 }
6804
6805 return 0;
6806}
6807
6808static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6809{
6810 if (ctx->cq_ev_fd) {
6811 eventfd_ctx_put(ctx->cq_ev_fd);
6812 ctx->cq_ev_fd = NULL;
6813 return 0;
6814 }
6815
6816 return -ENXIO;
6817}
6818
Jens Axboe5a2e7452020-02-23 16:23:11 -07006819static int __io_destroy_buffers(int id, void *p, void *data)
6820{
6821 struct io_ring_ctx *ctx = data;
6822 struct io_buffer *buf = p;
6823
6824 /* the head kbuf is the list itself */
6825 while (!list_empty(&buf->list)) {
6826 struct io_buffer *nxt;
6827
6828 nxt = list_first_entry(&buf->list, struct io_buffer, list);
6829 list_del(&nxt->list);
6830 kfree(nxt);
6831 }
6832 kfree(buf);
6833 idr_remove(&ctx->io_buffer_idr, id);
6834 return 0;
6835}
6836
6837static void io_destroy_buffers(struct io_ring_ctx *ctx)
6838{
6839 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
6840 idr_destroy(&ctx->io_buffer_idr);
6841}
6842
Jens Axboe2b188cc2019-01-07 10:46:33 -07006843static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6844{
Jens Axboe6b063142019-01-10 22:13:58 -07006845 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006846 if (ctx->sqo_mm)
6847 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006848
6849 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006850 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006851 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006852 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07006853 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006854 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006855
Jens Axboe2b188cc2019-01-07 10:46:33 -07006856#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006857 if (ctx->ring_sock) {
6858 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006859 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006860 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006861#endif
6862
Hristo Venev75b28af2019-08-26 17:23:46 +00006863 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006864 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006865
6866 percpu_ref_exit(&ctx->refs);
6867 if (ctx->account_mem)
6868 io_unaccount_mem(ctx->user,
6869 ring_pages(ctx->sq_entries, ctx->cq_entries));
6870 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006871 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006872 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006873 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006874 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006875 kfree(ctx);
6876}
6877
6878static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6879{
6880 struct io_ring_ctx *ctx = file->private_data;
6881 __poll_t mask = 0;
6882
6883 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006884 /*
6885 * synchronizes with barrier from wq_has_sleeper call in
6886 * io_commit_cqring
6887 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006888 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006889 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6890 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006891 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006892 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006893 mask |= EPOLLIN | EPOLLRDNORM;
6894
6895 return mask;
6896}
6897
6898static int io_uring_fasync(int fd, struct file *file, int on)
6899{
6900 struct io_ring_ctx *ctx = file->private_data;
6901
6902 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6903}
6904
Jens Axboe071698e2020-01-28 10:04:42 -07006905static int io_remove_personalities(int id, void *p, void *data)
6906{
6907 struct io_ring_ctx *ctx = data;
6908 const struct cred *cred;
6909
6910 cred = idr_remove(&ctx->personality_idr, id);
6911 if (cred)
6912 put_cred(cred);
6913 return 0;
6914}
6915
Jens Axboe2b188cc2019-01-07 10:46:33 -07006916static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6917{
6918 mutex_lock(&ctx->uring_lock);
6919 percpu_ref_kill(&ctx->refs);
6920 mutex_unlock(&ctx->uring_lock);
6921
Jens Axboedf069d82020-02-04 16:48:34 -07006922 /*
6923 * Wait for sq thread to idle, if we have one. It won't spin on new
6924 * work after we've killed the ctx ref above. This is important to do
6925 * before we cancel existing commands, as the thread could otherwise
6926 * be queueing new work post that. If that's work we need to cancel,
6927 * it could cause shutdown to hang.
6928 */
6929 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6930 cpu_relax();
6931
Jens Axboe5262f562019-09-17 12:26:57 -06006932 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006933 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006934
6935 if (ctx->io_wq)
6936 io_wq_cancel_all(ctx->io_wq);
6937
Jens Axboedef596e2019-01-09 08:59:42 -07006938 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006939 /* if we failed setting up the ctx, we might not have any rings */
6940 if (ctx->rings)
6941 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006942 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006943 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006944 io_ring_ctx_free(ctx);
6945}
6946
6947static int io_uring_release(struct inode *inode, struct file *file)
6948{
6949 struct io_ring_ctx *ctx = file->private_data;
6950
6951 file->private_data = NULL;
6952 io_ring_ctx_wait_and_kill(ctx);
6953 return 0;
6954}
6955
Jens Axboefcb323c2019-10-24 12:39:47 -06006956static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6957 struct files_struct *files)
6958{
6959 struct io_kiocb *req;
6960 DEFINE_WAIT(wait);
6961
6962 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006963 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006964
6965 spin_lock_irq(&ctx->inflight_lock);
6966 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006967 if (req->work.files != files)
6968 continue;
6969 /* req is being completed, ignore */
6970 if (!refcount_inc_not_zero(&req->refs))
6971 continue;
6972 cancel_req = req;
6973 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006974 }
Jens Axboe768134d2019-11-10 20:30:53 -07006975 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006976 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006977 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006978 spin_unlock_irq(&ctx->inflight_lock);
6979
Jens Axboe768134d2019-11-10 20:30:53 -07006980 /* We need to keep going until we don't find a matching req */
6981 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006982 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006983
Jens Axboe2ca10252020-02-13 17:17:35 -07006984 if (cancel_req->flags & REQ_F_OVERFLOW) {
6985 spin_lock_irq(&ctx->completion_lock);
6986 list_del(&cancel_req->list);
6987 cancel_req->flags &= ~REQ_F_OVERFLOW;
6988 if (list_empty(&ctx->cq_overflow_list)) {
6989 clear_bit(0, &ctx->sq_check_overflow);
6990 clear_bit(0, &ctx->cq_check_overflow);
6991 }
6992 spin_unlock_irq(&ctx->completion_lock);
6993
6994 WRITE_ONCE(ctx->rings->cq_overflow,
6995 atomic_inc_return(&ctx->cached_cq_overflow));
6996
6997 /*
6998 * Put inflight ref and overflow ref. If that's
6999 * all we had, then we're done with this request.
7000 */
7001 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7002 io_put_req(cancel_req);
7003 continue;
7004 }
7005 }
7006
Bob Liu2f6d9b92019-11-13 18:06:24 +08007007 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7008 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007009 schedule();
7010 }
Jens Axboe768134d2019-11-10 20:30:53 -07007011 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007012}
7013
7014static int io_uring_flush(struct file *file, void *data)
7015{
7016 struct io_ring_ctx *ctx = file->private_data;
7017
7018 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007019
7020 /*
7021 * If the task is going away, cancel work it may have pending
7022 */
7023 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7024 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7025
Jens Axboefcb323c2019-10-24 12:39:47 -06007026 return 0;
7027}
7028
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007029static void *io_uring_validate_mmap_request(struct file *file,
7030 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007031{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007032 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007033 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007034 struct page *page;
7035 void *ptr;
7036
7037 switch (offset) {
7038 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007039 case IORING_OFF_CQ_RING:
7040 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007041 break;
7042 case IORING_OFF_SQES:
7043 ptr = ctx->sq_sqes;
7044 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007045 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007046 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007047 }
7048
7049 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007050 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007051 return ERR_PTR(-EINVAL);
7052
7053 return ptr;
7054}
7055
7056#ifdef CONFIG_MMU
7057
7058static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7059{
7060 size_t sz = vma->vm_end - vma->vm_start;
7061 unsigned long pfn;
7062 void *ptr;
7063
7064 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7065 if (IS_ERR(ptr))
7066 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007067
7068 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7069 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7070}
7071
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007072#else /* !CONFIG_MMU */
7073
7074static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7075{
7076 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7077}
7078
7079static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7080{
7081 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7082}
7083
7084static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7085 unsigned long addr, unsigned long len,
7086 unsigned long pgoff, unsigned long flags)
7087{
7088 void *ptr;
7089
7090 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7091 if (IS_ERR(ptr))
7092 return PTR_ERR(ptr);
7093
7094 return (unsigned long) ptr;
7095}
7096
7097#endif /* !CONFIG_MMU */
7098
Jens Axboe2b188cc2019-01-07 10:46:33 -07007099SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7100 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7101 size_t, sigsz)
7102{
7103 struct io_ring_ctx *ctx;
7104 long ret = -EBADF;
7105 int submitted = 0;
7106 struct fd f;
7107
Jens Axboeb41e9852020-02-17 09:52:41 -07007108 if (current->task_works)
7109 task_work_run();
7110
Jens Axboe6c271ce2019-01-10 11:22:30 -07007111 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007112 return -EINVAL;
7113
7114 f = fdget(fd);
7115 if (!f.file)
7116 return -EBADF;
7117
7118 ret = -EOPNOTSUPP;
7119 if (f.file->f_op != &io_uring_fops)
7120 goto out_fput;
7121
7122 ret = -ENXIO;
7123 ctx = f.file->private_data;
7124 if (!percpu_ref_tryget(&ctx->refs))
7125 goto out_fput;
7126
Jens Axboe6c271ce2019-01-10 11:22:30 -07007127 /*
7128 * For SQ polling, the thread will do all submissions and completions.
7129 * Just return the requested submit count, and wake the thread if
7130 * we were asked to.
7131 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007132 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007133 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007134 if (!list_empty_careful(&ctx->cq_overflow_list))
7135 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007136 if (flags & IORING_ENTER_SQ_WAKEUP)
7137 wake_up(&ctx->sqo_wait);
7138 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007139 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007140 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007141
7142 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007143 /* already have mm, so io_submit_sqes() won't try to grab it */
7144 cur_mm = ctx->sqo_mm;
7145 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
7146 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007147 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007148
7149 if (submitted != to_submit)
7150 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007151 }
7152 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007153 unsigned nr_events = 0;
7154
Jens Axboe2b188cc2019-01-07 10:46:33 -07007155 min_complete = min(min_complete, ctx->cq_entries);
7156
Jens Axboedef596e2019-01-09 08:59:42 -07007157 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07007158 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007159 } else {
7160 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7161 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007162 }
7163
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007164out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007165 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007166out_fput:
7167 fdput(f);
7168 return submitted ? submitted : ret;
7169}
7170
Tobias Klauserbebdb652020-02-26 18:38:32 +01007171#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007172static int io_uring_show_cred(int id, void *p, void *data)
7173{
7174 const struct cred *cred = p;
7175 struct seq_file *m = data;
7176 struct user_namespace *uns = seq_user_ns(m);
7177 struct group_info *gi;
7178 kernel_cap_t cap;
7179 unsigned __capi;
7180 int g;
7181
7182 seq_printf(m, "%5d\n", id);
7183 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7184 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7185 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7186 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7187 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7188 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7189 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7190 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7191 seq_puts(m, "\n\tGroups:\t");
7192 gi = cred->group_info;
7193 for (g = 0; g < gi->ngroups; g++) {
7194 seq_put_decimal_ull(m, g ? " " : "",
7195 from_kgid_munged(uns, gi->gid[g]));
7196 }
7197 seq_puts(m, "\n\tCapEff:\t");
7198 cap = cred->cap_effective;
7199 CAP_FOR_EACH_U32(__capi)
7200 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7201 seq_putc(m, '\n');
7202 return 0;
7203}
7204
7205static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7206{
7207 int i;
7208
7209 mutex_lock(&ctx->uring_lock);
7210 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7211 for (i = 0; i < ctx->nr_user_files; i++) {
7212 struct fixed_file_table *table;
7213 struct file *f;
7214
7215 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7216 f = table->files[i & IORING_FILE_TABLE_MASK];
7217 if (f)
7218 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7219 else
7220 seq_printf(m, "%5u: <none>\n", i);
7221 }
7222 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7223 for (i = 0; i < ctx->nr_user_bufs; i++) {
7224 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7225
7226 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7227 (unsigned int) buf->len);
7228 }
7229 if (!idr_is_empty(&ctx->personality_idr)) {
7230 seq_printf(m, "Personalities:\n");
7231 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7232 }
Jens Axboed7718a92020-02-14 22:23:12 -07007233 seq_printf(m, "PollList:\n");
7234 spin_lock_irq(&ctx->completion_lock);
7235 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7236 struct hlist_head *list = &ctx->cancel_hash[i];
7237 struct io_kiocb *req;
7238
7239 hlist_for_each_entry(req, list, hash_node)
7240 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7241 req->task->task_works != NULL);
7242 }
7243 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007244 mutex_unlock(&ctx->uring_lock);
7245}
7246
7247static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7248{
7249 struct io_ring_ctx *ctx = f->private_data;
7250
7251 if (percpu_ref_tryget(&ctx->refs)) {
7252 __io_uring_show_fdinfo(ctx, m);
7253 percpu_ref_put(&ctx->refs);
7254 }
7255}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007256#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007257
Jens Axboe2b188cc2019-01-07 10:46:33 -07007258static const struct file_operations io_uring_fops = {
7259 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007260 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007261 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007262#ifndef CONFIG_MMU
7263 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7264 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7265#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007266 .poll = io_uring_poll,
7267 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007268#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007269 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007270#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007271};
7272
7273static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7274 struct io_uring_params *p)
7275{
Hristo Venev75b28af2019-08-26 17:23:46 +00007276 struct io_rings *rings;
7277 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007278
Hristo Venev75b28af2019-08-26 17:23:46 +00007279 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7280 if (size == SIZE_MAX)
7281 return -EOVERFLOW;
7282
7283 rings = io_mem_alloc(size);
7284 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007285 return -ENOMEM;
7286
Hristo Venev75b28af2019-08-26 17:23:46 +00007287 ctx->rings = rings;
7288 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7289 rings->sq_ring_mask = p->sq_entries - 1;
7290 rings->cq_ring_mask = p->cq_entries - 1;
7291 rings->sq_ring_entries = p->sq_entries;
7292 rings->cq_ring_entries = p->cq_entries;
7293 ctx->sq_mask = rings->sq_ring_mask;
7294 ctx->cq_mask = rings->cq_ring_mask;
7295 ctx->sq_entries = rings->sq_ring_entries;
7296 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007297
7298 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007299 if (size == SIZE_MAX) {
7300 io_mem_free(ctx->rings);
7301 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007302 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007303 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007304
7305 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007306 if (!ctx->sq_sqes) {
7307 io_mem_free(ctx->rings);
7308 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007309 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007310 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007311
Jens Axboe2b188cc2019-01-07 10:46:33 -07007312 return 0;
7313}
7314
7315/*
7316 * Allocate an anonymous fd, this is what constitutes the application
7317 * visible backing of an io_uring instance. The application mmaps this
7318 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7319 * we have to tie this fd to a socket for file garbage collection purposes.
7320 */
7321static int io_uring_get_fd(struct io_ring_ctx *ctx)
7322{
7323 struct file *file;
7324 int ret;
7325
7326#if defined(CONFIG_UNIX)
7327 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7328 &ctx->ring_sock);
7329 if (ret)
7330 return ret;
7331#endif
7332
7333 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7334 if (ret < 0)
7335 goto err;
7336
7337 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7338 O_RDWR | O_CLOEXEC);
7339 if (IS_ERR(file)) {
7340 put_unused_fd(ret);
7341 ret = PTR_ERR(file);
7342 goto err;
7343 }
7344
7345#if defined(CONFIG_UNIX)
7346 ctx->ring_sock->file = file;
7347#endif
7348 fd_install(ret, file);
7349 return ret;
7350err:
7351#if defined(CONFIG_UNIX)
7352 sock_release(ctx->ring_sock);
7353 ctx->ring_sock = NULL;
7354#endif
7355 return ret;
7356}
7357
7358static int io_uring_create(unsigned entries, struct io_uring_params *p)
7359{
7360 struct user_struct *user = NULL;
7361 struct io_ring_ctx *ctx;
7362 bool account_mem;
7363 int ret;
7364
Jens Axboe8110c1a2019-12-28 15:39:54 -07007365 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007366 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007367 if (entries > IORING_MAX_ENTRIES) {
7368 if (!(p->flags & IORING_SETUP_CLAMP))
7369 return -EINVAL;
7370 entries = IORING_MAX_ENTRIES;
7371 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007372
7373 /*
7374 * Use twice as many entries for the CQ ring. It's possible for the
7375 * application to drive a higher depth than the size of the SQ ring,
7376 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007377 * some flexibility in overcommitting a bit. If the application has
7378 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7379 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007380 */
7381 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007382 if (p->flags & IORING_SETUP_CQSIZE) {
7383 /*
7384 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7385 * to a power-of-two, if it isn't already. We do NOT impose
7386 * any cq vs sq ring sizing.
7387 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007388 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007389 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007390 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7391 if (!(p->flags & IORING_SETUP_CLAMP))
7392 return -EINVAL;
7393 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7394 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007395 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7396 } else {
7397 p->cq_entries = 2 * p->sq_entries;
7398 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007399
7400 user = get_uid(current_user());
7401 account_mem = !capable(CAP_IPC_LOCK);
7402
7403 if (account_mem) {
7404 ret = io_account_mem(user,
7405 ring_pages(p->sq_entries, p->cq_entries));
7406 if (ret) {
7407 free_uid(user);
7408 return ret;
7409 }
7410 }
7411
7412 ctx = io_ring_ctx_alloc(p);
7413 if (!ctx) {
7414 if (account_mem)
7415 io_unaccount_mem(user, ring_pages(p->sq_entries,
7416 p->cq_entries));
7417 free_uid(user);
7418 return -ENOMEM;
7419 }
7420 ctx->compat = in_compat_syscall();
7421 ctx->account_mem = account_mem;
7422 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007423 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007424
7425 ret = io_allocate_scq_urings(ctx, p);
7426 if (ret)
7427 goto err;
7428
Jens Axboe6c271ce2019-01-10 11:22:30 -07007429 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007430 if (ret)
7431 goto err;
7432
Jens Axboe2b188cc2019-01-07 10:46:33 -07007433 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007434 p->sq_off.head = offsetof(struct io_rings, sq.head);
7435 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7436 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7437 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7438 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7439 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7440 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007441
7442 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007443 p->cq_off.head = offsetof(struct io_rings, cq.head);
7444 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7445 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7446 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7447 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7448 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007449
Jens Axboe044c1ab2019-10-28 09:15:33 -06007450 /*
7451 * Install ring fd as the very last thing, so we don't risk someone
7452 * having closed it before we finish setup
7453 */
7454 ret = io_uring_get_fd(ctx);
7455 if (ret < 0)
7456 goto err;
7457
Jens Axboeda8c9692019-12-02 18:51:26 -07007458 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007459 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007460 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007461 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007462 return ret;
7463err:
7464 io_ring_ctx_wait_and_kill(ctx);
7465 return ret;
7466}
7467
7468/*
7469 * Sets up an aio uring context, and returns the fd. Applications asks for a
7470 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7471 * params structure passed in.
7472 */
7473static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7474{
7475 struct io_uring_params p;
7476 long ret;
7477 int i;
7478
7479 if (copy_from_user(&p, params, sizeof(p)))
7480 return -EFAULT;
7481 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7482 if (p.resv[i])
7483 return -EINVAL;
7484 }
7485
Jens Axboe6c271ce2019-01-10 11:22:30 -07007486 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007487 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007488 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007489 return -EINVAL;
7490
7491 ret = io_uring_create(entries, &p);
7492 if (ret < 0)
7493 return ret;
7494
7495 if (copy_to_user(params, &p, sizeof(p)))
7496 return -EFAULT;
7497
7498 return ret;
7499}
7500
7501SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7502 struct io_uring_params __user *, params)
7503{
7504 return io_uring_setup(entries, params);
7505}
7506
Jens Axboe66f4af92020-01-16 15:36:52 -07007507static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7508{
7509 struct io_uring_probe *p;
7510 size_t size;
7511 int i, ret;
7512
7513 size = struct_size(p, ops, nr_args);
7514 if (size == SIZE_MAX)
7515 return -EOVERFLOW;
7516 p = kzalloc(size, GFP_KERNEL);
7517 if (!p)
7518 return -ENOMEM;
7519
7520 ret = -EFAULT;
7521 if (copy_from_user(p, arg, size))
7522 goto out;
7523 ret = -EINVAL;
7524 if (memchr_inv(p, 0, size))
7525 goto out;
7526
7527 p->last_op = IORING_OP_LAST - 1;
7528 if (nr_args > IORING_OP_LAST)
7529 nr_args = IORING_OP_LAST;
7530
7531 for (i = 0; i < nr_args; i++) {
7532 p->ops[i].op = i;
7533 if (!io_op_defs[i].not_supported)
7534 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7535 }
7536 p->ops_len = i;
7537
7538 ret = 0;
7539 if (copy_to_user(arg, p, size))
7540 ret = -EFAULT;
7541out:
7542 kfree(p);
7543 return ret;
7544}
7545
Jens Axboe071698e2020-01-28 10:04:42 -07007546static int io_register_personality(struct io_ring_ctx *ctx)
7547{
7548 const struct cred *creds = get_current_cred();
7549 int id;
7550
7551 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7552 USHRT_MAX, GFP_KERNEL);
7553 if (id < 0)
7554 put_cred(creds);
7555 return id;
7556}
7557
7558static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7559{
7560 const struct cred *old_creds;
7561
7562 old_creds = idr_remove(&ctx->personality_idr, id);
7563 if (old_creds) {
7564 put_cred(old_creds);
7565 return 0;
7566 }
7567
7568 return -EINVAL;
7569}
7570
7571static bool io_register_op_must_quiesce(int op)
7572{
7573 switch (op) {
7574 case IORING_UNREGISTER_FILES:
7575 case IORING_REGISTER_FILES_UPDATE:
7576 case IORING_REGISTER_PROBE:
7577 case IORING_REGISTER_PERSONALITY:
7578 case IORING_UNREGISTER_PERSONALITY:
7579 return false;
7580 default:
7581 return true;
7582 }
7583}
7584
Jens Axboeedafcce2019-01-09 09:16:05 -07007585static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7586 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007587 __releases(ctx->uring_lock)
7588 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007589{
7590 int ret;
7591
Jens Axboe35fa71a2019-04-22 10:23:23 -06007592 /*
7593 * We're inside the ring mutex, if the ref is already dying, then
7594 * someone else killed the ctx or is already going through
7595 * io_uring_register().
7596 */
7597 if (percpu_ref_is_dying(&ctx->refs))
7598 return -ENXIO;
7599
Jens Axboe071698e2020-01-28 10:04:42 -07007600 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007601 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007602
Jens Axboe05f3fb32019-12-09 11:22:50 -07007603 /*
7604 * Drop uring mutex before waiting for references to exit. If
7605 * another thread is currently inside io_uring_enter() it might
7606 * need to grab the uring_lock to make progress. If we hold it
7607 * here across the drain wait, then we can deadlock. It's safe
7608 * to drop the mutex here, since no new references will come in
7609 * after we've killed the percpu ref.
7610 */
7611 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007612 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007613 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007614 if (ret) {
7615 percpu_ref_resurrect(&ctx->refs);
7616 ret = -EINTR;
7617 goto out;
7618 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007619 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007620
7621 switch (opcode) {
7622 case IORING_REGISTER_BUFFERS:
7623 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7624 break;
7625 case IORING_UNREGISTER_BUFFERS:
7626 ret = -EINVAL;
7627 if (arg || nr_args)
7628 break;
7629 ret = io_sqe_buffer_unregister(ctx);
7630 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007631 case IORING_REGISTER_FILES:
7632 ret = io_sqe_files_register(ctx, arg, nr_args);
7633 break;
7634 case IORING_UNREGISTER_FILES:
7635 ret = -EINVAL;
7636 if (arg || nr_args)
7637 break;
7638 ret = io_sqe_files_unregister(ctx);
7639 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007640 case IORING_REGISTER_FILES_UPDATE:
7641 ret = io_sqe_files_update(ctx, arg, nr_args);
7642 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007643 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007644 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007645 ret = -EINVAL;
7646 if (nr_args != 1)
7647 break;
7648 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007649 if (ret)
7650 break;
7651 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7652 ctx->eventfd_async = 1;
7653 else
7654 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007655 break;
7656 case IORING_UNREGISTER_EVENTFD:
7657 ret = -EINVAL;
7658 if (arg || nr_args)
7659 break;
7660 ret = io_eventfd_unregister(ctx);
7661 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007662 case IORING_REGISTER_PROBE:
7663 ret = -EINVAL;
7664 if (!arg || nr_args > 256)
7665 break;
7666 ret = io_probe(ctx, arg, nr_args);
7667 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007668 case IORING_REGISTER_PERSONALITY:
7669 ret = -EINVAL;
7670 if (arg || nr_args)
7671 break;
7672 ret = io_register_personality(ctx);
7673 break;
7674 case IORING_UNREGISTER_PERSONALITY:
7675 ret = -EINVAL;
7676 if (arg)
7677 break;
7678 ret = io_unregister_personality(ctx, nr_args);
7679 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007680 default:
7681 ret = -EINVAL;
7682 break;
7683 }
7684
Jens Axboe071698e2020-01-28 10:04:42 -07007685 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007686 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007687 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007688out:
7689 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007690 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007691 return ret;
7692}
7693
7694SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7695 void __user *, arg, unsigned int, nr_args)
7696{
7697 struct io_ring_ctx *ctx;
7698 long ret = -EBADF;
7699 struct fd f;
7700
7701 f = fdget(fd);
7702 if (!f.file)
7703 return -EBADF;
7704
7705 ret = -EOPNOTSUPP;
7706 if (f.file->f_op != &io_uring_fops)
7707 goto out_fput;
7708
7709 ctx = f.file->private_data;
7710
7711 mutex_lock(&ctx->uring_lock);
7712 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7713 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007714 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7715 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007716out_fput:
7717 fdput(f);
7718 return ret;
7719}
7720
Jens Axboe2b188cc2019-01-07 10:46:33 -07007721static int __init io_uring_init(void)
7722{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007723#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7724 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7725 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7726} while (0)
7727
7728#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7729 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7730 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7731 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7732 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7733 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7734 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7735 BUILD_BUG_SQE_ELEM(8, __u64, off);
7736 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7737 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007738 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007739 BUILD_BUG_SQE_ELEM(24, __u32, len);
7740 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7741 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7742 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7743 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7744 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7745 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7746 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7747 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7748 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7749 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7750 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7751 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7752 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007753 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007754 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7755 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7756 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007757 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007758
Jens Axboed3656342019-12-18 09:50:26 -07007759 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007760 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7761 return 0;
7762};
7763__initcall(io_uring_init);