blob: 7c855a038a1bd4b6779f872a4d87c7b897d1ae03 [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 Axboe4d954c22020-02-27 07:31:19 -0700685 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700686 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300687 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700688 .async_ctx = 1,
689 .needs_mm = 1,
690 .needs_file = 1,
691 .hash_reg_file = 1,
692 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700693 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700694 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300695 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700696 .needs_file = 1,
697 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300698 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700699 .needs_file = 1,
700 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700701 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700702 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300703 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700704 .needs_file = 1,
705 .hash_reg_file = 1,
706 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700707 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700708 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300709 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700710 .needs_file = 1,
711 .unbound_nonreg_file = 1,
712 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300713 [IORING_OP_POLL_REMOVE] = {},
714 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700715 .needs_file = 1,
716 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300717 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700718 .async_ctx = 1,
719 .needs_mm = 1,
720 .needs_file = 1,
721 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700722 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700723 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700724 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300725 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700726 .async_ctx = 1,
727 .needs_mm = 1,
728 .needs_file = 1,
729 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700730 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700731 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700732 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300733 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700734 .async_ctx = 1,
735 .needs_mm = 1,
736 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300737 [IORING_OP_TIMEOUT_REMOVE] = {},
738 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700739 .needs_mm = 1,
740 .needs_file = 1,
741 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700742 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700743 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700744 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300745 [IORING_OP_ASYNC_CANCEL] = {},
746 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700747 .async_ctx = 1,
748 .needs_mm = 1,
749 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300750 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700751 .async_ctx = 1,
752 .needs_mm = 1,
753 .needs_file = 1,
754 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700755 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700756 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300757 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700758 .needs_file = 1,
759 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300760 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700761 .needs_file = 1,
762 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700763 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700764 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700765 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300766 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700767 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700768 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700769 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300770 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700771 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700772 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_mm = 1,
776 .needs_file = 1,
777 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700778 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700779 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300780 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700781 .needs_mm = 1,
782 .needs_file = 1,
783 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700784 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700785 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700786 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300787 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700788 .needs_mm = 1,
789 .needs_file = 1,
790 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700791 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700792 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300793 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700794 .needs_file = 1,
795 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300796 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700797 .needs_mm = 1,
798 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300799 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700800 .needs_mm = 1,
801 .needs_file = 1,
802 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700803 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700804 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300805 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700806 .needs_mm = 1,
807 .needs_file = 1,
808 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700809 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700810 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700811 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300812 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700813 .needs_file = 1,
814 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700815 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700816 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700817 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700818 [IORING_OP_EPOLL_CTL] = {
819 .unbound_nonreg_file = 1,
820 .file_table = 1,
821 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300822 [IORING_OP_SPLICE] = {
823 .needs_file = 1,
824 .hash_reg_file = 1,
825 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700826 },
827 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700828};
829
Jens Axboe561fb042019-10-24 07:25:42 -0600830static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700831static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800832static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700833static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700834static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
835static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700836static int __io_sqe_files_update(struct io_ring_ctx *ctx,
837 struct io_uring_files_update *ip,
838 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700839static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700840static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300841static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700842static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
843 int fd, struct file **out_file, bool fixed);
844static void __io_queue_sqe(struct io_kiocb *req,
845 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600846
Jens Axboe2b188cc2019-01-07 10:46:33 -0700847static struct kmem_cache *req_cachep;
848
849static const struct file_operations io_uring_fops;
850
851struct sock *io_uring_get_socket(struct file *file)
852{
853#if defined(CONFIG_UNIX)
854 if (file->f_op == &io_uring_fops) {
855 struct io_ring_ctx *ctx = file->private_data;
856
857 return ctx->ring_sock->sk;
858 }
859#endif
860 return NULL;
861}
862EXPORT_SYMBOL(io_uring_get_socket);
863
864static void io_ring_ctx_ref_free(struct percpu_ref *ref)
865{
866 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
867
Jens Axboe206aefd2019-11-07 18:27:42 -0700868 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700869}
870
871static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
872{
873 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700874 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700875
876 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
877 if (!ctx)
878 return NULL;
879
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700880 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
881 if (!ctx->fallback_req)
882 goto err;
883
Jens Axboe206aefd2019-11-07 18:27:42 -0700884 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
885 if (!ctx->completions)
886 goto err;
887
Jens Axboe78076bb2019-12-04 19:56:40 -0700888 /*
889 * Use 5 bits less than the max cq entries, that should give us around
890 * 32 entries per hash list if totally full and uniformly spread.
891 */
892 hash_bits = ilog2(p->cq_entries);
893 hash_bits -= 5;
894 if (hash_bits <= 0)
895 hash_bits = 1;
896 ctx->cancel_hash_bits = hash_bits;
897 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
898 GFP_KERNEL);
899 if (!ctx->cancel_hash)
900 goto err;
901 __hash_init(ctx->cancel_hash, 1U << hash_bits);
902
Roman Gushchin21482892019-05-07 10:01:48 -0700903 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700904 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
905 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700906
907 ctx->flags = p->flags;
908 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700909 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700910 init_completion(&ctx->completions[0]);
911 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700912 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700913 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700914 mutex_init(&ctx->uring_lock);
915 init_waitqueue_head(&ctx->wait);
916 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700917 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600918 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600919 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600920 init_waitqueue_head(&ctx->inflight_wait);
921 spin_lock_init(&ctx->inflight_lock);
922 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700923 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700924err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700925 if (ctx->fallback_req)
926 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700927 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700928 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700929 kfree(ctx);
930 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700931}
932
Bob Liu9d858b22019-11-13 18:06:25 +0800933static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600934{
Jackie Liua197f662019-11-08 08:09:12 -0700935 struct io_ring_ctx *ctx = req->ctx;
936
Jens Axboe498ccd92019-10-25 10:04:25 -0600937 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
938 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600939}
940
Bob Liu9d858b22019-11-13 18:06:25 +0800941static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600942{
Pavel Begunkov87987892020-01-18 01:22:30 +0300943 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800944 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600945
Bob Liu9d858b22019-11-13 18:06:25 +0800946 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600947}
948
949static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600950{
951 struct io_kiocb *req;
952
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600953 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800954 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600955 list_del_init(&req->list);
956 return req;
957 }
958
959 return NULL;
960}
961
Jens Axboe5262f562019-09-17 12:26:57 -0600962static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
963{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600964 struct io_kiocb *req;
965
966 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700967 if (req) {
968 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
969 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800970 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700971 list_del_init(&req->list);
972 return req;
973 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600974 }
975
976 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600977}
978
Jens Axboede0617e2019-04-06 21:51:27 -0600979static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700980{
Hristo Venev75b28af2019-08-26 17:23:46 +0000981 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700982
Pavel Begunkov07910152020-01-17 03:52:46 +0300983 /* order cqe stores with ring update */
984 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700985
Pavel Begunkov07910152020-01-17 03:52:46 +0300986 if (wq_has_sleeper(&ctx->cq_wait)) {
987 wake_up_interruptible(&ctx->cq_wait);
988 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989 }
990}
991
Jens Axboecccf0ee2020-01-27 16:34:48 -0700992static inline void io_req_work_grab_env(struct io_kiocb *req,
993 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600994{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700995 if (!req->work.mm && def->needs_mm) {
996 mmgrab(current->mm);
997 req->work.mm = current->mm;
998 }
999 if (!req->work.creds)
1000 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001001 if (!req->work.fs && def->needs_fs) {
1002 spin_lock(&current->fs->lock);
1003 if (!current->fs->in_exec) {
1004 req->work.fs = current->fs;
1005 req->work.fs->users++;
1006 } else {
1007 req->work.flags |= IO_WQ_WORK_CANCEL;
1008 }
1009 spin_unlock(&current->fs->lock);
1010 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001011 if (!req->work.task_pid)
1012 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001013}
1014
1015static inline void io_req_work_drop_env(struct io_kiocb *req)
1016{
1017 if (req->work.mm) {
1018 mmdrop(req->work.mm);
1019 req->work.mm = NULL;
1020 }
1021 if (req->work.creds) {
1022 put_cred(req->work.creds);
1023 req->work.creds = NULL;
1024 }
Jens Axboeff002b32020-02-07 16:05:21 -07001025 if (req->work.fs) {
1026 struct fs_struct *fs = req->work.fs;
1027
1028 spin_lock(&req->work.fs->lock);
1029 if (--fs->users)
1030 fs = NULL;
1031 spin_unlock(&req->work.fs->lock);
1032 if (fs)
1033 free_fs_struct(fs);
1034 }
Jens Axboe561fb042019-10-24 07:25:42 -06001035}
1036
Jens Axboe94ae5e72019-11-14 19:39:52 -07001037static inline bool io_prep_async_work(struct io_kiocb *req,
1038 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001039{
Jens Axboed3656342019-12-18 09:50:26 -07001040 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -06001041 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -06001042
Jens Axboed3656342019-12-18 09:50:26 -07001043 if (req->flags & REQ_F_ISREG) {
1044 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001045 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -07001046 } else {
1047 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001048 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001049 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001050
1051 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001052
Jens Axboe94ae5e72019-11-14 19:39:52 -07001053 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001054 return do_hashed;
1055}
1056
Jackie Liua197f662019-11-08 08:09:12 -07001057static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001058{
Jackie Liua197f662019-11-08 08:09:12 -07001059 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001060 struct io_kiocb *link;
1061 bool do_hashed;
1062
1063 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001064
1065 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1066 req->flags);
1067 if (!do_hashed) {
1068 io_wq_enqueue(ctx->io_wq, &req->work);
1069 } else {
1070 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1071 file_inode(req->file));
1072 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001073
1074 if (link)
1075 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001076}
1077
Jens Axboe5262f562019-09-17 12:26:57 -06001078static void io_kill_timeout(struct io_kiocb *req)
1079{
1080 int ret;
1081
Jens Axboe2d283902019-12-04 11:08:05 -07001082 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001083 if (ret != -1) {
1084 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001085 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001086 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001087 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001088 }
1089}
1090
1091static void io_kill_timeouts(struct io_ring_ctx *ctx)
1092{
1093 struct io_kiocb *req, *tmp;
1094
1095 spin_lock_irq(&ctx->completion_lock);
1096 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1097 io_kill_timeout(req);
1098 spin_unlock_irq(&ctx->completion_lock);
1099}
1100
Jens Axboede0617e2019-04-06 21:51:27 -06001101static void io_commit_cqring(struct io_ring_ctx *ctx)
1102{
1103 struct io_kiocb *req;
1104
Jens Axboe5262f562019-09-17 12:26:57 -06001105 while ((req = io_get_timeout_req(ctx)) != NULL)
1106 io_kill_timeout(req);
1107
Jens Axboede0617e2019-04-06 21:51:27 -06001108 __io_commit_cqring(ctx);
1109
Pavel Begunkov87987892020-01-18 01:22:30 +03001110 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001111 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001112}
1113
Jens Axboe2b188cc2019-01-07 10:46:33 -07001114static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1115{
Hristo Venev75b28af2019-08-26 17:23:46 +00001116 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117 unsigned tail;
1118
1119 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001120 /*
1121 * writes to the cq entry need to come after reading head; the
1122 * control dependency is enough as we're using WRITE_ONCE to
1123 * fill the cq entry
1124 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001125 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001126 return NULL;
1127
1128 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001129 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001130}
1131
Jens Axboef2842ab2020-01-08 11:04:00 -07001132static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1133{
Jens Axboef0b493e2020-02-01 21:30:11 -07001134 if (!ctx->cq_ev_fd)
1135 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001136 if (!ctx->eventfd_async)
1137 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001138 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001139}
1140
Jens Axboeb41e9852020-02-17 09:52:41 -07001141static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001142{
1143 if (waitqueue_active(&ctx->wait))
1144 wake_up(&ctx->wait);
1145 if (waitqueue_active(&ctx->sqo_wait))
1146 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001147 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001148 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001149}
1150
Jens Axboec4a2ed72019-11-21 21:01:26 -07001151/* Returns true if there are no backlogged entries after the flush */
1152static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001153{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001154 struct io_rings *rings = ctx->rings;
1155 struct io_uring_cqe *cqe;
1156 struct io_kiocb *req;
1157 unsigned long flags;
1158 LIST_HEAD(list);
1159
1160 if (!force) {
1161 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001162 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001163 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1164 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001165 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001166 }
1167
1168 spin_lock_irqsave(&ctx->completion_lock, flags);
1169
1170 /* if force is set, the ring is going away. always drop after that */
1171 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001172 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001173
Jens Axboec4a2ed72019-11-21 21:01:26 -07001174 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001175 while (!list_empty(&ctx->cq_overflow_list)) {
1176 cqe = io_get_cqring(ctx);
1177 if (!cqe && !force)
1178 break;
1179
1180 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1181 list);
1182 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001183 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001184 if (cqe) {
1185 WRITE_ONCE(cqe->user_data, req->user_data);
1186 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001187 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001188 } else {
1189 WRITE_ONCE(ctx->rings->cq_overflow,
1190 atomic_inc_return(&ctx->cached_cq_overflow));
1191 }
1192 }
1193
1194 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001195 if (cqe) {
1196 clear_bit(0, &ctx->sq_check_overflow);
1197 clear_bit(0, &ctx->cq_check_overflow);
1198 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001199 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1200 io_cqring_ev_posted(ctx);
1201
1202 while (!list_empty(&list)) {
1203 req = list_first_entry(&list, struct io_kiocb, list);
1204 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001205 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001206 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001207
1208 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001209}
1210
Jens Axboebcda7ba2020-02-23 16:42:51 -07001211static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001212{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001213 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001214 struct io_uring_cqe *cqe;
1215
Jens Axboe78e19bb2019-11-06 15:21:34 -07001216 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001217
Jens Axboe2b188cc2019-01-07 10:46:33 -07001218 /*
1219 * If we can't get a cq entry, userspace overflowed the
1220 * submission (by quite a lot). Increment the overflow count in
1221 * the ring.
1222 */
1223 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001224 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001225 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001226 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001227 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001228 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001229 WRITE_ONCE(ctx->rings->cq_overflow,
1230 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001231 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001232 if (list_empty(&ctx->cq_overflow_list)) {
1233 set_bit(0, &ctx->sq_check_overflow);
1234 set_bit(0, &ctx->cq_check_overflow);
1235 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001236 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001237 refcount_inc(&req->refs);
1238 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001239 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001240 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001241 }
1242}
1243
Jens Axboebcda7ba2020-02-23 16:42:51 -07001244static void io_cqring_fill_event(struct io_kiocb *req, long res)
1245{
1246 __io_cqring_fill_event(req, res, 0);
1247}
1248
1249static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001251 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001252 unsigned long flags;
1253
1254 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001255 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001256 io_commit_cqring(ctx);
1257 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1258
Jens Axboe8c838782019-03-12 15:48:16 -06001259 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001260}
1261
Jens Axboebcda7ba2020-02-23 16:42:51 -07001262static void io_cqring_add_event(struct io_kiocb *req, long res)
1263{
1264 __io_cqring_add_event(req, res, 0);
1265}
1266
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001267static inline bool io_is_fallback_req(struct io_kiocb *req)
1268{
1269 return req == (struct io_kiocb *)
1270 ((unsigned long) req->ctx->fallback_req & ~1UL);
1271}
1272
1273static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1274{
1275 struct io_kiocb *req;
1276
1277 req = ctx->fallback_req;
1278 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1279 return req;
1280
1281 return NULL;
1282}
1283
Jens Axboe2579f912019-01-09 09:10:43 -07001284static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1285 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001286{
Jens Axboefd6fab22019-03-14 16:30:06 -06001287 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288 struct io_kiocb *req;
1289
Jens Axboe2579f912019-01-09 09:10:43 -07001290 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001291 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001292 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001293 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001294 } else if (!state->free_reqs) {
1295 size_t sz;
1296 int ret;
1297
1298 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001299 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1300
1301 /*
1302 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1303 * retry single alloc to be on the safe side.
1304 */
1305 if (unlikely(ret <= 0)) {
1306 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1307 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001308 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001309 ret = 1;
1310 }
Jens Axboe2579f912019-01-09 09:10:43 -07001311 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001312 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001313 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001314 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001315 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001316 }
1317
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001318got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001319 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001320 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001321 req->ctx = ctx;
1322 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001323 /* one is dropped after submission, the other at completion */
1324 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001325 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001326 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001327 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001328fallback:
1329 req = io_get_fallback_req(ctx);
1330 if (req)
1331 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001332 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001333 return NULL;
1334}
1335
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001336static inline void io_put_file(struct io_kiocb *req, struct file *file,
1337 bool fixed)
1338{
1339 if (fixed)
1340 percpu_ref_put(&req->ctx->file_data->refs);
1341 else
1342 fput(file);
1343}
1344
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001345static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001346{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001347 if (likely(!io_is_fallback_req(req)))
1348 kmem_cache_free(req_cachep, req);
1349 else
1350 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1351}
1352
Jens Axboec6ca97b302019-12-28 12:11:08 -07001353static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001354{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001355 if (req->flags & REQ_F_NEED_CLEANUP)
1356 io_cleanup_req(req);
1357
YueHaibing96fd84d2020-01-07 22:22:44 +08001358 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001359 if (req->file)
1360 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001361
1362 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001363}
1364
1365static void __io_free_req(struct io_kiocb *req)
1366{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001367 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001368
Jens Axboefcb323c2019-10-24 12:39:47 -06001369 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001370 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001371 unsigned long flags;
1372
1373 spin_lock_irqsave(&ctx->inflight_lock, flags);
1374 list_del(&req->inflight_entry);
1375 if (waitqueue_active(&ctx->inflight_wait))
1376 wake_up(&ctx->inflight_wait);
1377 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1378 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001379
1380 percpu_ref_put(&req->ctx->refs);
1381 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001382}
1383
Jens Axboec6ca97b302019-12-28 12:11:08 -07001384struct req_batch {
1385 void *reqs[IO_IOPOLL_BATCH];
1386 int to_free;
1387 int need_iter;
1388};
1389
1390static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1391{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001392 int fixed_refs = rb->to_free;
1393
Jens Axboec6ca97b302019-12-28 12:11:08 -07001394 if (!rb->to_free)
1395 return;
1396 if (rb->need_iter) {
1397 int i, inflight = 0;
1398 unsigned long flags;
1399
Jens Axboe10fef4b2020-01-09 07:52:28 -07001400 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001401 for (i = 0; i < rb->to_free; i++) {
1402 struct io_kiocb *req = rb->reqs[i];
1403
Jens Axboe10fef4b2020-01-09 07:52:28 -07001404 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001405 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001406 fixed_refs++;
1407 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001408 if (req->flags & REQ_F_INFLIGHT)
1409 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001410 __io_req_aux_free(req);
1411 }
1412 if (!inflight)
1413 goto do_free;
1414
1415 spin_lock_irqsave(&ctx->inflight_lock, flags);
1416 for (i = 0; i < rb->to_free; i++) {
1417 struct io_kiocb *req = rb->reqs[i];
1418
Jens Axboe10fef4b2020-01-09 07:52:28 -07001419 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001420 list_del(&req->inflight_entry);
1421 if (!--inflight)
1422 break;
1423 }
1424 }
1425 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1426
1427 if (waitqueue_active(&ctx->inflight_wait))
1428 wake_up(&ctx->inflight_wait);
1429 }
1430do_free:
1431 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001432 if (fixed_refs)
1433 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001434 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001435 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001436}
1437
Jackie Liua197f662019-11-08 08:09:12 -07001438static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001439{
Jackie Liua197f662019-11-08 08:09:12 -07001440 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001441 int ret;
1442
Jens Axboe2d283902019-12-04 11:08:05 -07001443 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001444 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001445 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001446 io_commit_cqring(ctx);
1447 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001448 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001449 return true;
1450 }
1451
1452 return false;
1453}
1454
Jens Axboeba816ad2019-09-28 11:36:45 -06001455static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001456{
Jens Axboe2665abf2019-11-05 12:40:47 -07001457 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001458 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001459
Jens Axboe4d7dd462019-11-20 13:03:52 -07001460 /* Already got next link */
1461 if (req->flags & REQ_F_LINK_NEXT)
1462 return;
1463
Jens Axboe9e645e112019-05-10 16:07:28 -06001464 /*
1465 * The list should never be empty when we are called here. But could
1466 * potentially happen if the chain is messed up, check to be on the
1467 * safe side.
1468 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001469 while (!list_empty(&req->link_list)) {
1470 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1471 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001472
Pavel Begunkov44932332019-12-05 16:16:35 +03001473 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1474 (nxt->flags & REQ_F_TIMEOUT))) {
1475 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001476 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001477 req->flags &= ~REQ_F_LINK_TIMEOUT;
1478 continue;
1479 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001480
Pavel Begunkov44932332019-12-05 16:16:35 +03001481 list_del_init(&req->link_list);
1482 if (!list_empty(&nxt->link_list))
1483 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001484 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001485 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001486 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001487
Jens Axboe4d7dd462019-11-20 13:03:52 -07001488 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001489 if (wake_ev)
1490 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001491}
1492
1493/*
1494 * Called if REQ_F_LINK is set, and we fail the head request
1495 */
1496static void io_fail_links(struct io_kiocb *req)
1497{
Jens Axboe2665abf2019-11-05 12:40:47 -07001498 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001499 unsigned long flags;
1500
1501 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001502
1503 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001504 struct io_kiocb *link = list_first_entry(&req->link_list,
1505 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001506
Pavel Begunkov44932332019-12-05 16:16:35 +03001507 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001508 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001509
1510 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001511 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001512 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001513 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001514 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001515 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001516 }
Jens Axboe5d960722019-11-19 15:31:28 -07001517 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001518 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001519
1520 io_commit_cqring(ctx);
1521 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1522 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001523}
1524
Jens Axboe4d7dd462019-11-20 13:03:52 -07001525static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001526{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001527 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001528 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001529
Jens Axboe9e645e112019-05-10 16:07:28 -06001530 /*
1531 * If LINK is set, we have dependent requests in this chain. If we
1532 * didn't fail this request, queue the first one up, moving any other
1533 * dependencies to the next request. In case of failure, fail the rest
1534 * of the chain.
1535 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001536 if (req->flags & REQ_F_FAIL_LINK) {
1537 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001538 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1539 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001540 struct io_ring_ctx *ctx = req->ctx;
1541 unsigned long flags;
1542
1543 /*
1544 * If this is a timeout link, we could be racing with the
1545 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001546 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001547 */
1548 spin_lock_irqsave(&ctx->completion_lock, flags);
1549 io_req_link_next(req, nxt);
1550 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1551 } else {
1552 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001553 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001554}
Jens Axboe9e645e112019-05-10 16:07:28 -06001555
Jackie Liuc69f8db2019-11-09 11:00:08 +08001556static void io_free_req(struct io_kiocb *req)
1557{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001558 struct io_kiocb *nxt = NULL;
1559
1560 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001561 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001562
1563 if (nxt)
1564 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001565}
1566
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001567static void io_link_work_cb(struct io_wq_work **workptr)
1568{
1569 struct io_wq_work *work = *workptr;
1570 struct io_kiocb *link = work->data;
1571
1572 io_queue_linked_timeout(link);
1573 io_wq_submit_work(workptr);
1574}
1575
1576static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1577{
1578 struct io_kiocb *link;
1579
1580 *workptr = &nxt->work;
1581 link = io_prep_linked_timeout(nxt);
1582 if (link) {
1583 nxt->work.func = io_link_work_cb;
1584 nxt->work.data = link;
1585 }
1586}
1587
Jens Axboeba816ad2019-09-28 11:36:45 -06001588/*
1589 * Drop reference to request, return next in chain (if there is one) if this
1590 * was the last reference to this request.
1591 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001592__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001593static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001594{
Jens Axboe2a44f462020-02-25 13:25:41 -07001595 if (refcount_dec_and_test(&req->refs)) {
1596 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001597 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001598 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001599}
1600
Jens Axboe2b188cc2019-01-07 10:46:33 -07001601static void io_put_req(struct io_kiocb *req)
1602{
Jens Axboedef596e2019-01-09 08:59:42 -07001603 if (refcount_dec_and_test(&req->refs))
1604 io_free_req(req);
1605}
1606
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001607static void io_steal_work(struct io_kiocb *req,
1608 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001609{
1610 /*
1611 * It's in an io-wq worker, so there always should be at least
1612 * one reference, which will be dropped in io_put_work() just
1613 * after the current handler returns.
1614 *
1615 * It also means, that if the counter dropped to 1, then there is
1616 * no asynchronous users left, so it's safe to steal the next work.
1617 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001618 if (refcount_read(&req->refs) == 1) {
1619 struct io_kiocb *nxt = NULL;
1620
1621 io_req_find_next(req, &nxt);
1622 if (nxt)
1623 io_wq_assign_next(workptr, nxt);
1624 }
1625}
1626
Jens Axboe978db572019-11-14 22:39:04 -07001627/*
1628 * Must only be used if we don't need to care about links, usually from
1629 * within the completion handling itself.
1630 */
1631static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001632{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001633 /* drop both submit and complete references */
1634 if (refcount_sub_and_test(2, &req->refs))
1635 __io_free_req(req);
1636}
1637
Jens Axboe978db572019-11-14 22:39:04 -07001638static void io_double_put_req(struct io_kiocb *req)
1639{
1640 /* drop both submit and complete references */
1641 if (refcount_sub_and_test(2, &req->refs))
1642 io_free_req(req);
1643}
1644
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001645static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001646{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001647 struct io_rings *rings = ctx->rings;
1648
Jens Axboead3eb2c2019-12-18 17:12:20 -07001649 if (test_bit(0, &ctx->cq_check_overflow)) {
1650 /*
1651 * noflush == true is from the waitqueue handler, just ensure
1652 * we wake up the task, and the next invocation will flush the
1653 * entries. We cannot safely to it from here.
1654 */
1655 if (noflush && !list_empty(&ctx->cq_overflow_list))
1656 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001657
Jens Axboead3eb2c2019-12-18 17:12:20 -07001658 io_cqring_overflow_flush(ctx, false);
1659 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001660
Jens Axboea3a0e432019-08-20 11:03:11 -06001661 /* See comment at the top of this file */
1662 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001663 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001664}
1665
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001666static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1667{
1668 struct io_rings *rings = ctx->rings;
1669
1670 /* make sure SQ entry isn't read before tail */
1671 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1672}
1673
Jens Axboe8237e042019-12-28 10:48:22 -07001674static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001675{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001676 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1677 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001678
Jens Axboec6ca97b302019-12-28 12:11:08 -07001679 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1680 rb->need_iter++;
1681
1682 rb->reqs[rb->to_free++] = req;
1683 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1684 io_free_req_many(req->ctx, rb);
1685 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001686}
1687
Jens Axboebcda7ba2020-02-23 16:42:51 -07001688static int io_put_kbuf(struct io_kiocb *req)
1689{
Jens Axboe4d954c22020-02-27 07:31:19 -07001690 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001691 int cflags;
1692
Jens Axboe4d954c22020-02-27 07:31:19 -07001693 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001694 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1695 cflags |= IORING_CQE_F_BUFFER;
1696 req->rw.addr = 0;
1697 kfree(kbuf);
1698 return cflags;
1699}
1700
Jens Axboedef596e2019-01-09 08:59:42 -07001701/*
1702 * Find and free completed poll iocbs
1703 */
1704static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1705 struct list_head *done)
1706{
Jens Axboe8237e042019-12-28 10:48:22 -07001707 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001708 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001709
Jens Axboec6ca97b302019-12-28 12:11:08 -07001710 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001711 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001712 int cflags = 0;
1713
Jens Axboedef596e2019-01-09 08:59:42 -07001714 req = list_first_entry(done, struct io_kiocb, list);
1715 list_del(&req->list);
1716
Jens Axboebcda7ba2020-02-23 16:42:51 -07001717 if (req->flags & REQ_F_BUFFER_SELECTED)
1718 cflags = io_put_kbuf(req);
1719
1720 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001721 (*nr_events)++;
1722
Jens Axboe8237e042019-12-28 10:48:22 -07001723 if (refcount_dec_and_test(&req->refs) &&
1724 !io_req_multi_free(&rb, req))
1725 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001726 }
Jens Axboedef596e2019-01-09 08:59:42 -07001727
Jens Axboe09bb8392019-03-13 12:39:28 -06001728 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001729 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001730}
1731
1732static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1733 long min)
1734{
1735 struct io_kiocb *req, *tmp;
1736 LIST_HEAD(done);
1737 bool spin;
1738 int ret;
1739
1740 /*
1741 * Only spin for completions if we don't have multiple devices hanging
1742 * off our complete list, and we're under the requested amount.
1743 */
1744 spin = !ctx->poll_multi_file && *nr_events < min;
1745
1746 ret = 0;
1747 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001748 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001749
1750 /*
1751 * Move completed entries to our local list. If we find a
1752 * request that requires polling, break out and complete
1753 * the done list first, if we have entries there.
1754 */
1755 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1756 list_move_tail(&req->list, &done);
1757 continue;
1758 }
1759 if (!list_empty(&done))
1760 break;
1761
1762 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1763 if (ret < 0)
1764 break;
1765
1766 if (ret && spin)
1767 spin = false;
1768 ret = 0;
1769 }
1770
1771 if (!list_empty(&done))
1772 io_iopoll_complete(ctx, nr_events, &done);
1773
1774 return ret;
1775}
1776
1777/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001778 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001779 * non-spinning poll check - we'll still enter the driver poll loop, but only
1780 * as a non-spinning completion check.
1781 */
1782static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1783 long min)
1784{
Jens Axboe08f54392019-08-21 22:19:11 -06001785 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001786 int ret;
1787
1788 ret = io_do_iopoll(ctx, nr_events, min);
1789 if (ret < 0)
1790 return ret;
1791 if (!min || *nr_events >= min)
1792 return 0;
1793 }
1794
1795 return 1;
1796}
1797
1798/*
1799 * We can't just wait for polled events to come to us, we have to actively
1800 * find and complete them.
1801 */
1802static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1803{
1804 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1805 return;
1806
1807 mutex_lock(&ctx->uring_lock);
1808 while (!list_empty(&ctx->poll_list)) {
1809 unsigned int nr_events = 0;
1810
1811 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001812
1813 /*
1814 * Ensure we allow local-to-the-cpu processing to take place,
1815 * in this case we need to ensure that we reap all events.
1816 */
1817 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001818 }
1819 mutex_unlock(&ctx->uring_lock);
1820}
1821
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001822static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1823 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001824{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001825 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001826
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001827 /*
1828 * We disallow the app entering submit/complete with polling, but we
1829 * still need to lock the ring to prevent racing with polled issue
1830 * that got punted to a workqueue.
1831 */
1832 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001833 do {
1834 int tmin = 0;
1835
Jens Axboe500f9fb2019-08-19 12:15:59 -06001836 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001837 * Don't enter poll loop if we already have events pending.
1838 * If we do, we can potentially be spinning for commands that
1839 * already triggered a CQE (eg in error).
1840 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001841 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001842 break;
1843
1844 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001845 * If a submit got punted to a workqueue, we can have the
1846 * application entering polling for a command before it gets
1847 * issued. That app will hold the uring_lock for the duration
1848 * of the poll right here, so we need to take a breather every
1849 * now and then to ensure that the issue has a chance to add
1850 * the poll to the issued list. Otherwise we can spin here
1851 * forever, while the workqueue is stuck trying to acquire the
1852 * very same mutex.
1853 */
1854 if (!(++iters & 7)) {
1855 mutex_unlock(&ctx->uring_lock);
1856 mutex_lock(&ctx->uring_lock);
1857 }
1858
Jens Axboedef596e2019-01-09 08:59:42 -07001859 if (*nr_events < min)
1860 tmin = min - *nr_events;
1861
1862 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1863 if (ret <= 0)
1864 break;
1865 ret = 0;
1866 } while (min && !*nr_events && !need_resched());
1867
Jens Axboe500f9fb2019-08-19 12:15:59 -06001868 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001869 return ret;
1870}
1871
Jens Axboe491381ce2019-10-17 09:20:46 -06001872static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001873{
Jens Axboe491381ce2019-10-17 09:20:46 -06001874 /*
1875 * Tell lockdep we inherited freeze protection from submission
1876 * thread.
1877 */
1878 if (req->flags & REQ_F_ISREG) {
1879 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001880
Jens Axboe491381ce2019-10-17 09:20:46 -06001881 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001882 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001883 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001884}
1885
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001886static inline void req_set_fail_links(struct io_kiocb *req)
1887{
1888 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1889 req->flags |= REQ_F_FAIL_LINK;
1890}
1891
Jens Axboeba816ad2019-09-28 11:36:45 -06001892static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001893{
Jens Axboe9adbd452019-12-20 08:45:55 -07001894 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001895 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001896
Jens Axboe491381ce2019-10-17 09:20:46 -06001897 if (kiocb->ki_flags & IOCB_WRITE)
1898 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001900 if (res != req->result)
1901 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001902 if (req->flags & REQ_F_BUFFER_SELECTED)
1903 cflags = io_put_kbuf(req);
1904 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001905}
1906
1907static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1908{
Jens Axboe9adbd452019-12-20 08:45:55 -07001909 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001910
1911 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001912 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001913}
1914
Jens Axboedef596e2019-01-09 08:59:42 -07001915static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1916{
Jens Axboe9adbd452019-12-20 08:45:55 -07001917 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001918
Jens Axboe491381ce2019-10-17 09:20:46 -06001919 if (kiocb->ki_flags & IOCB_WRITE)
1920 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001921
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001922 if (res != req->result)
1923 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001924 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001925 if (res != -EAGAIN)
1926 req->flags |= REQ_F_IOPOLL_COMPLETED;
1927}
1928
1929/*
1930 * After the iocb has been issued, it's safe to be found on the poll list.
1931 * Adding the kiocb to the list AFTER submission ensures that we don't
1932 * find it from a io_iopoll_getevents() thread before the issuer is done
1933 * accessing the kiocb cookie.
1934 */
1935static void io_iopoll_req_issued(struct io_kiocb *req)
1936{
1937 struct io_ring_ctx *ctx = req->ctx;
1938
1939 /*
1940 * Track whether we have multiple files in our lists. This will impact
1941 * how we do polling eventually, not spinning if we're on potentially
1942 * different devices.
1943 */
1944 if (list_empty(&ctx->poll_list)) {
1945 ctx->poll_multi_file = false;
1946 } else if (!ctx->poll_multi_file) {
1947 struct io_kiocb *list_req;
1948
1949 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1950 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001951 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001952 ctx->poll_multi_file = true;
1953 }
1954
1955 /*
1956 * For fast devices, IO may have already completed. If it has, add
1957 * it to the front so we find it first.
1958 */
1959 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1960 list_add(&req->list, &ctx->poll_list);
1961 else
1962 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001963
1964 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1965 wq_has_sleeper(&ctx->sqo_wait))
1966 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001967}
1968
Jens Axboe3d6770f2019-04-13 11:50:54 -06001969static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001970{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001971 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001972 int diff = state->has_refs - state->used_refs;
1973
1974 if (diff)
1975 fput_many(state->file, diff);
1976 state->file = NULL;
1977 }
1978}
1979
1980/*
1981 * Get as many references to a file as we have IOs left in this submission,
1982 * assuming most submissions are for one file, or at least that each file
1983 * has more than one submission.
1984 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001985static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001986{
1987 if (!state)
1988 return fget(fd);
1989
1990 if (state->file) {
1991 if (state->fd == fd) {
1992 state->used_refs++;
1993 state->ios_left--;
1994 return state->file;
1995 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001996 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001997 }
1998 state->file = fget_many(fd, state->ios_left);
1999 if (!state->file)
2000 return NULL;
2001
2002 state->fd = fd;
2003 state->has_refs = state->ios_left;
2004 state->used_refs = 1;
2005 state->ios_left--;
2006 return state->file;
2007}
2008
Jens Axboe2b188cc2019-01-07 10:46:33 -07002009/*
2010 * If we tracked the file through the SCM inflight mechanism, we could support
2011 * any file. For now, just ensure that anything potentially problematic is done
2012 * inline.
2013 */
2014static bool io_file_supports_async(struct file *file)
2015{
2016 umode_t mode = file_inode(file)->i_mode;
2017
Jens Axboe10d59342019-12-09 20:16:22 -07002018 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002019 return true;
2020 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2021 return true;
2022
2023 return false;
2024}
2025
Jens Axboe3529d8c2019-12-19 18:24:38 -07002026static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2027 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002028{
Jens Axboedef596e2019-01-09 08:59:42 -07002029 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002030 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002031 unsigned ioprio;
2032 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002033
Jens Axboe491381ce2019-10-17 09:20:46 -06002034 if (S_ISREG(file_inode(req->file)->i_mode))
2035 req->flags |= REQ_F_ISREG;
2036
Jens Axboe2b188cc2019-01-07 10:46:33 -07002037 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002038 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2039 req->flags |= REQ_F_CUR_POS;
2040 kiocb->ki_pos = req->file->f_pos;
2041 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002042 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002043 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2044 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2045 if (unlikely(ret))
2046 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002047
2048 ioprio = READ_ONCE(sqe->ioprio);
2049 if (ioprio) {
2050 ret = ioprio_check_cap(ioprio);
2051 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002052 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002053
2054 kiocb->ki_ioprio = ioprio;
2055 } else
2056 kiocb->ki_ioprio = get_current_ioprio();
2057
Stefan Bühler8449eed2019-04-27 20:34:19 +02002058 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002059 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2060 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002061 req->flags |= REQ_F_NOWAIT;
2062
2063 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002064 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002065
Jens Axboedef596e2019-01-09 08:59:42 -07002066 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002067 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2068 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002069 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002070
Jens Axboedef596e2019-01-09 08:59:42 -07002071 kiocb->ki_flags |= IOCB_HIPRI;
2072 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002073 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002074 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002075 if (kiocb->ki_flags & IOCB_HIPRI)
2076 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002077 kiocb->ki_complete = io_complete_rw;
2078 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002079
Jens Axboe3529d8c2019-12-19 18:24:38 -07002080 req->rw.addr = READ_ONCE(sqe->addr);
2081 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002082 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002083 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002084 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002085 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002086}
2087
2088static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2089{
2090 switch (ret) {
2091 case -EIOCBQUEUED:
2092 break;
2093 case -ERESTARTSYS:
2094 case -ERESTARTNOINTR:
2095 case -ERESTARTNOHAND:
2096 case -ERESTART_RESTARTBLOCK:
2097 /*
2098 * We can't just restart the syscall, since previously
2099 * submitted sqes may already be in progress. Just fail this
2100 * IO with EINTR.
2101 */
2102 ret = -EINTR;
2103 /* fall through */
2104 default:
2105 kiocb->ki_complete(kiocb, ret, 0);
2106 }
2107}
2108
Pavel Begunkov014db002020-03-03 21:33:12 +03002109static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002110{
Jens Axboeba042912019-12-25 16:33:42 -07002111 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2112
2113 if (req->flags & REQ_F_CUR_POS)
2114 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002115 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002116 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002117 else
2118 io_rw_done(kiocb, ret);
2119}
2120
Jens Axboe9adbd452019-12-20 08:45:55 -07002121static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002122 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002123{
Jens Axboe9adbd452019-12-20 08:45:55 -07002124 struct io_ring_ctx *ctx = req->ctx;
2125 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002126 struct io_mapped_ubuf *imu;
2127 unsigned index, buf_index;
2128 size_t offset;
2129 u64 buf_addr;
2130
2131 /* attempt to use fixed buffers without having provided iovecs */
2132 if (unlikely(!ctx->user_bufs))
2133 return -EFAULT;
2134
Jens Axboe9adbd452019-12-20 08:45:55 -07002135 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002136 if (unlikely(buf_index >= ctx->nr_user_bufs))
2137 return -EFAULT;
2138
2139 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2140 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002141 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002142
2143 /* overflow */
2144 if (buf_addr + len < buf_addr)
2145 return -EFAULT;
2146 /* not inside the mapped region */
2147 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2148 return -EFAULT;
2149
2150 /*
2151 * May not be a start of buffer, set size appropriately
2152 * and advance us to the beginning.
2153 */
2154 offset = buf_addr - imu->ubuf;
2155 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002156
2157 if (offset) {
2158 /*
2159 * Don't use iov_iter_advance() here, as it's really slow for
2160 * using the latter parts of a big fixed buffer - it iterates
2161 * over each segment manually. We can cheat a bit here, because
2162 * we know that:
2163 *
2164 * 1) it's a BVEC iter, we set it up
2165 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2166 * first and last bvec
2167 *
2168 * So just find our index, and adjust the iterator afterwards.
2169 * If the offset is within the first bvec (or the whole first
2170 * bvec, just use iov_iter_advance(). This makes it easier
2171 * since we can just skip the first segment, which may not
2172 * be PAGE_SIZE aligned.
2173 */
2174 const struct bio_vec *bvec = imu->bvec;
2175
2176 if (offset <= bvec->bv_len) {
2177 iov_iter_advance(iter, offset);
2178 } else {
2179 unsigned long seg_skip;
2180
2181 /* skip first vec */
2182 offset -= bvec->bv_len;
2183 seg_skip = 1 + (offset >> PAGE_SHIFT);
2184
2185 iter->bvec = bvec + seg_skip;
2186 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002187 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002188 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002189 }
2190 }
2191
Jens Axboe5e559562019-11-13 16:12:46 -07002192 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002193}
2194
Jens Axboebcda7ba2020-02-23 16:42:51 -07002195static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2196{
2197 if (needs_lock)
2198 mutex_unlock(&ctx->uring_lock);
2199}
2200
2201static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2202{
2203 /*
2204 * "Normal" inline submissions always hold the uring_lock, since we
2205 * grab it from the system call. Same is true for the SQPOLL offload.
2206 * The only exception is when we've detached the request and issue it
2207 * from an async worker thread, grab the lock for that case.
2208 */
2209 if (needs_lock)
2210 mutex_lock(&ctx->uring_lock);
2211}
2212
2213static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2214 int bgid, struct io_buffer *kbuf,
2215 bool needs_lock)
2216{
2217 struct io_buffer *head;
2218
2219 if (req->flags & REQ_F_BUFFER_SELECTED)
2220 return kbuf;
2221
2222 io_ring_submit_lock(req->ctx, needs_lock);
2223
2224 lockdep_assert_held(&req->ctx->uring_lock);
2225
2226 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2227 if (head) {
2228 if (!list_empty(&head->list)) {
2229 kbuf = list_last_entry(&head->list, struct io_buffer,
2230 list);
2231 list_del(&kbuf->list);
2232 } else {
2233 kbuf = head;
2234 idr_remove(&req->ctx->io_buffer_idr, bgid);
2235 }
2236 if (*len > kbuf->len)
2237 *len = kbuf->len;
2238 } else {
2239 kbuf = ERR_PTR(-ENOBUFS);
2240 }
2241
2242 io_ring_submit_unlock(req->ctx, needs_lock);
2243
2244 return kbuf;
2245}
2246
Jens Axboe4d954c22020-02-27 07:31:19 -07002247static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2248 bool needs_lock)
2249{
2250 struct io_buffer *kbuf;
2251 int bgid;
2252
2253 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2254 bgid = (int) (unsigned long) req->rw.kiocb.private;
2255 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2256 if (IS_ERR(kbuf))
2257 return kbuf;
2258 req->rw.addr = (u64) (unsigned long) kbuf;
2259 req->flags |= REQ_F_BUFFER_SELECTED;
2260 return u64_to_user_ptr(kbuf->addr);
2261}
2262
2263#ifdef CONFIG_COMPAT
2264static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2265 bool needs_lock)
2266{
2267 struct compat_iovec __user *uiov;
2268 compat_ssize_t clen;
2269 void __user *buf;
2270 ssize_t len;
2271
2272 uiov = u64_to_user_ptr(req->rw.addr);
2273 if (!access_ok(uiov, sizeof(*uiov)))
2274 return -EFAULT;
2275 if (__get_user(clen, &uiov->iov_len))
2276 return -EFAULT;
2277 if (clen < 0)
2278 return -EINVAL;
2279
2280 len = clen;
2281 buf = io_rw_buffer_select(req, &len, needs_lock);
2282 if (IS_ERR(buf))
2283 return PTR_ERR(buf);
2284 iov[0].iov_base = buf;
2285 iov[0].iov_len = (compat_size_t) len;
2286 return 0;
2287}
2288#endif
2289
2290static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2291 bool needs_lock)
2292{
2293 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2294 void __user *buf;
2295 ssize_t len;
2296
2297 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2298 return -EFAULT;
2299
2300 len = iov[0].iov_len;
2301 if (len < 0)
2302 return -EINVAL;
2303 buf = io_rw_buffer_select(req, &len, needs_lock);
2304 if (IS_ERR(buf))
2305 return PTR_ERR(buf);
2306 iov[0].iov_base = buf;
2307 iov[0].iov_len = len;
2308 return 0;
2309}
2310
2311static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2312 bool needs_lock)
2313{
2314 if (req->flags & REQ_F_BUFFER_SELECTED)
2315 return 0;
2316 if (!req->rw.len)
2317 return 0;
2318 else if (req->rw.len > 1)
2319 return -EINVAL;
2320
2321#ifdef CONFIG_COMPAT
2322 if (req->ctx->compat)
2323 return io_compat_import(req, iov, needs_lock);
2324#endif
2325
2326 return __io_iov_buffer_select(req, iov, needs_lock);
2327}
2328
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002329static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002330 struct iovec **iovec, struct iov_iter *iter,
2331 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002332{
Jens Axboe9adbd452019-12-20 08:45:55 -07002333 void __user *buf = u64_to_user_ptr(req->rw.addr);
2334 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002335 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002336 u8 opcode;
2337
Jens Axboed625c6e2019-12-17 19:53:05 -07002338 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002339 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002340 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002341 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002342 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002343
Jens Axboebcda7ba2020-02-23 16:42:51 -07002344 /* buffer index only valid with fixed read/write, or buffer select */
2345 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002346 return -EINVAL;
2347
Jens Axboe3a6820f2019-12-22 15:19:35 -07002348 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002349 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002350 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2351 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002352 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002353 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002354 }
Jens Axboebcda7ba2020-02-23 16:42:51 -07002355 }
2356
Jens Axboe3a6820f2019-12-22 15:19:35 -07002357 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2358 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002359 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002360 }
2361
Jens Axboef67676d2019-12-02 11:03:47 -07002362 if (req->io) {
2363 struct io_async_rw *iorw = &req->io->rw;
2364
2365 *iovec = iorw->iov;
2366 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2367 if (iorw->iov == iorw->fast_iov)
2368 *iovec = NULL;
2369 return iorw->size;
2370 }
2371
Jens Axboe4d954c22020-02-27 07:31:19 -07002372 if (req->flags & REQ_F_BUFFER_SELECT) {
2373 ret = io_iov_buffer_select(req, *iovec, needs_lock);
2374 if (!ret)
2375 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
2376 *iovec = NULL;
2377 return ret;
2378 }
2379
Jens Axboe2b188cc2019-01-07 10:46:33 -07002380#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002381 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002382 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2383 iovec, iter);
2384#endif
2385
2386 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2387}
2388
Jens Axboe32960612019-09-23 11:05:34 -06002389/*
2390 * For files that don't have ->read_iter() and ->write_iter(), handle them
2391 * by looping over ->read() or ->write() manually.
2392 */
2393static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2394 struct iov_iter *iter)
2395{
2396 ssize_t ret = 0;
2397
2398 /*
2399 * Don't support polled IO through this interface, and we can't
2400 * support non-blocking either. For the latter, this just causes
2401 * the kiocb to be handled from an async context.
2402 */
2403 if (kiocb->ki_flags & IOCB_HIPRI)
2404 return -EOPNOTSUPP;
2405 if (kiocb->ki_flags & IOCB_NOWAIT)
2406 return -EAGAIN;
2407
2408 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002409 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002410 ssize_t nr;
2411
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002412 if (!iov_iter_is_bvec(iter)) {
2413 iovec = iov_iter_iovec(iter);
2414 } else {
2415 /* fixed buffers import bvec */
2416 iovec.iov_base = kmap(iter->bvec->bv_page)
2417 + iter->iov_offset;
2418 iovec.iov_len = min(iter->count,
2419 iter->bvec->bv_len - iter->iov_offset);
2420 }
2421
Jens Axboe32960612019-09-23 11:05:34 -06002422 if (rw == READ) {
2423 nr = file->f_op->read(file, iovec.iov_base,
2424 iovec.iov_len, &kiocb->ki_pos);
2425 } else {
2426 nr = file->f_op->write(file, iovec.iov_base,
2427 iovec.iov_len, &kiocb->ki_pos);
2428 }
2429
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002430 if (iov_iter_is_bvec(iter))
2431 kunmap(iter->bvec->bv_page);
2432
Jens Axboe32960612019-09-23 11:05:34 -06002433 if (nr < 0) {
2434 if (!ret)
2435 ret = nr;
2436 break;
2437 }
2438 ret += nr;
2439 if (nr != iovec.iov_len)
2440 break;
2441 iov_iter_advance(iter, nr);
2442 }
2443
2444 return ret;
2445}
2446
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002447static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002448 struct iovec *iovec, struct iovec *fast_iov,
2449 struct iov_iter *iter)
2450{
2451 req->io->rw.nr_segs = iter->nr_segs;
2452 req->io->rw.size = io_size;
2453 req->io->rw.iov = iovec;
2454 if (!req->io->rw.iov) {
2455 req->io->rw.iov = req->io->rw.fast_iov;
2456 memcpy(req->io->rw.iov, fast_iov,
2457 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002458 } else {
2459 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002460 }
2461}
2462
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002463static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002464{
Jens Axboed3656342019-12-18 09:50:26 -07002465 if (!io_op_defs[req->opcode].async_ctx)
2466 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002467 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002468 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002469}
2470
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002471static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2472 struct iovec *iovec, struct iovec *fast_iov,
2473 struct iov_iter *iter)
2474{
Jens Axboe980ad262020-01-24 23:08:54 -07002475 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002476 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002477 if (!req->io) {
2478 if (io_alloc_async_ctx(req))
2479 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002480
Jens Axboe5d204bc2020-01-31 12:06:52 -07002481 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2482 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002483 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002484}
2485
Jens Axboe3529d8c2019-12-19 18:24:38 -07002486static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2487 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002488{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002489 struct io_async_ctx *io;
2490 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002491 ssize_t ret;
2492
Jens Axboe3529d8c2019-12-19 18:24:38 -07002493 ret = io_prep_rw(req, sqe, force_nonblock);
2494 if (ret)
2495 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002496
Jens Axboe3529d8c2019-12-19 18:24:38 -07002497 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2498 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002499
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002500 /* either don't need iovec imported or already have it */
2501 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002502 return 0;
2503
2504 io = req->io;
2505 io->rw.iov = io->rw.fast_iov;
2506 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002507 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002508 req->io = io;
2509 if (ret < 0)
2510 return ret;
2511
2512 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2513 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002514}
2515
Pavel Begunkov014db002020-03-03 21:33:12 +03002516static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002517{
2518 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002519 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002520 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002521 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002522 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523
Jens Axboebcda7ba2020-02-23 16:42:51 -07002524 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002525 if (ret < 0)
2526 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002527
Jens Axboefd6c2e42019-12-18 12:19:41 -07002528 /* Ensure we clear previously set non-block flag */
2529 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002530 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002531
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002532 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002533 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002534 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002535 req->result = io_size;
2536
2537 /*
2538 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2539 * we know to async punt it even if it was opened O_NONBLOCK
2540 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002541 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002542 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002543
Jens Axboe31b51512019-01-18 22:56:34 -07002544 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002545 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002546 if (!ret) {
2547 ssize_t ret2;
2548
Jens Axboe9adbd452019-12-20 08:45:55 -07002549 if (req->file->f_op->read_iter)
2550 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002551 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002552 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002553
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002554 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002555 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002556 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002557 } else {
2558copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002559 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002560 inline_vecs, &iter);
2561 if (ret)
2562 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002563 /* any defer here is final, must blocking retry */
2564 if (!(req->flags & REQ_F_NOWAIT))
2565 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002566 return -EAGAIN;
2567 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002568 }
Jens Axboef67676d2019-12-02 11:03:47 -07002569out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002570 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002571 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002572 return ret;
2573}
2574
Jens Axboe3529d8c2019-12-19 18:24:38 -07002575static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2576 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002577{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002578 struct io_async_ctx *io;
2579 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002580 ssize_t ret;
2581
Jens Axboe3529d8c2019-12-19 18:24:38 -07002582 ret = io_prep_rw(req, sqe, force_nonblock);
2583 if (ret)
2584 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002585
Jens Axboe3529d8c2019-12-19 18:24:38 -07002586 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2587 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002588
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002589 /* either don't need iovec imported or already have it */
2590 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002591 return 0;
2592
2593 io = req->io;
2594 io->rw.iov = io->rw.fast_iov;
2595 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002596 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002597 req->io = io;
2598 if (ret < 0)
2599 return ret;
2600
2601 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2602 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002603}
2604
Pavel Begunkov014db002020-03-03 21:33:12 +03002605static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002606{
2607 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002608 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002609 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002610 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002611 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002612
Jens Axboebcda7ba2020-02-23 16:42:51 -07002613 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002614 if (ret < 0)
2615 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002616
Jens Axboefd6c2e42019-12-18 12:19:41 -07002617 /* Ensure we clear previously set non-block flag */
2618 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002619 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002620
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002621 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002622 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002623 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002624 req->result = io_size;
2625
2626 /*
2627 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2628 * we know to async punt it even if it was opened O_NONBLOCK
2629 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002630 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002631 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002632
Jens Axboe10d59342019-12-09 20:16:22 -07002633 /* file path doesn't support NOWAIT for non-direct_IO */
2634 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2635 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002636 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002637
Jens Axboe31b51512019-01-18 22:56:34 -07002638 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002639 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002640 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002641 ssize_t ret2;
2642
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643 /*
2644 * Open-code file_start_write here to grab freeze protection,
2645 * which will be released by another thread in
2646 * io_complete_rw(). Fool lockdep by telling it the lock got
2647 * released so that it doesn't complain about the held lock when
2648 * we return to userspace.
2649 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002650 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002651 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002652 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002653 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654 SB_FREEZE_WRITE);
2655 }
2656 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002657
Jens Axboe9adbd452019-12-20 08:45:55 -07002658 if (req->file->f_op->write_iter)
2659 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002660 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002661 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002662 /*
2663 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2664 * retry them without IOCB_NOWAIT.
2665 */
2666 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2667 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002668 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002669 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002670 } else {
2671copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002672 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002673 inline_vecs, &iter);
2674 if (ret)
2675 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002676 /* any defer here is final, must blocking retry */
2677 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002678 return -EAGAIN;
2679 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002680 }
Jens Axboe31b51512019-01-18 22:56:34 -07002681out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002682 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002683 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002684 return ret;
2685}
2686
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002687static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2688{
2689 struct io_splice* sp = &req->splice;
2690 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2691 int ret;
2692
2693 if (req->flags & REQ_F_NEED_CLEANUP)
2694 return 0;
2695
2696 sp->file_in = NULL;
2697 sp->off_in = READ_ONCE(sqe->splice_off_in);
2698 sp->off_out = READ_ONCE(sqe->off);
2699 sp->len = READ_ONCE(sqe->len);
2700 sp->flags = READ_ONCE(sqe->splice_flags);
2701
2702 if (unlikely(sp->flags & ~valid_flags))
2703 return -EINVAL;
2704
2705 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2706 (sp->flags & SPLICE_F_FD_IN_FIXED));
2707 if (ret)
2708 return ret;
2709 req->flags |= REQ_F_NEED_CLEANUP;
2710
2711 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2712 req->work.flags |= IO_WQ_WORK_UNBOUND;
2713
2714 return 0;
2715}
2716
2717static bool io_splice_punt(struct file *file)
2718{
2719 if (get_pipe_info(file))
2720 return false;
2721 if (!io_file_supports_async(file))
2722 return true;
2723 return !(file->f_mode & O_NONBLOCK);
2724}
2725
Pavel Begunkov014db002020-03-03 21:33:12 +03002726static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002727{
2728 struct io_splice *sp = &req->splice;
2729 struct file *in = sp->file_in;
2730 struct file *out = sp->file_out;
2731 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2732 loff_t *poff_in, *poff_out;
2733 long ret;
2734
2735 if (force_nonblock) {
2736 if (io_splice_punt(in) || io_splice_punt(out))
2737 return -EAGAIN;
2738 flags |= SPLICE_F_NONBLOCK;
2739 }
2740
2741 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2742 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2743 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2744 if (force_nonblock && ret == -EAGAIN)
2745 return -EAGAIN;
2746
2747 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2748 req->flags &= ~REQ_F_NEED_CLEANUP;
2749
2750 io_cqring_add_event(req, ret);
2751 if (ret != sp->len)
2752 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002753 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002754 return 0;
2755}
2756
Jens Axboe2b188cc2019-01-07 10:46:33 -07002757/*
2758 * IORING_OP_NOP just posts a completion event, nothing else.
2759 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002760static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002761{
2762 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002763
Jens Axboedef596e2019-01-09 08:59:42 -07002764 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2765 return -EINVAL;
2766
Jens Axboe78e19bb2019-11-06 15:21:34 -07002767 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002768 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002769 return 0;
2770}
2771
Jens Axboe3529d8c2019-12-19 18:24:38 -07002772static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002773{
Jens Axboe6b063142019-01-10 22:13:58 -07002774 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002775
Jens Axboe09bb8392019-03-13 12:39:28 -06002776 if (!req->file)
2777 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002778
Jens Axboe6b063142019-01-10 22:13:58 -07002779 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002780 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002781 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002782 return -EINVAL;
2783
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002784 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2785 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2786 return -EINVAL;
2787
2788 req->sync.off = READ_ONCE(sqe->off);
2789 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002790 return 0;
2791}
2792
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002793static bool io_req_cancelled(struct io_kiocb *req)
2794{
2795 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2796 req_set_fail_links(req);
2797 io_cqring_add_event(req, -ECANCELED);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002798 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002799 return true;
2800 }
2801
2802 return false;
2803}
2804
Pavel Begunkov014db002020-03-03 21:33:12 +03002805static void __io_fsync(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002806{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002807 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002808 int ret;
2809
Jens Axboe9adbd452019-12-20 08:45:55 -07002810 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002811 end > 0 ? end : LLONG_MAX,
2812 req->sync.flags & IORING_FSYNC_DATASYNC);
2813 if (ret < 0)
2814 req_set_fail_links(req);
2815 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002816 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002817}
2818
2819static void io_fsync_finish(struct io_wq_work **workptr)
2820{
2821 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002822
2823 if (io_req_cancelled(req))
2824 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002825 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002826 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002827}
2828
Pavel Begunkov014db002020-03-03 21:33:12 +03002829static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002830{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002831 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002832 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002833 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002834 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002835 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002836 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002837 return 0;
2838}
2839
Pavel Begunkov014db002020-03-03 21:33:12 +03002840static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002841{
Jens Axboed63d1b52019-12-10 10:38:56 -07002842 int ret;
2843
2844 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2845 req->sync.len);
2846 if (ret < 0)
2847 req_set_fail_links(req);
2848 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002849 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002850}
2851
2852static void io_fallocate_finish(struct io_wq_work **workptr)
2853{
2854 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002855
Pavel Begunkov594506f2020-03-03 21:33:11 +03002856 if (io_req_cancelled(req))
2857 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002858 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002859 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002860}
2861
2862static int io_fallocate_prep(struct io_kiocb *req,
2863 const struct io_uring_sqe *sqe)
2864{
2865 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2866 return -EINVAL;
2867
2868 req->sync.off = READ_ONCE(sqe->off);
2869 req->sync.len = READ_ONCE(sqe->addr);
2870 req->sync.mode = READ_ONCE(sqe->len);
2871 return 0;
2872}
2873
Pavel Begunkov014db002020-03-03 21:33:12 +03002874static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002875{
Jens Axboed63d1b52019-12-10 10:38:56 -07002876 /* fallocate always requiring blocking context */
2877 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002878 req->work.func = io_fallocate_finish;
2879 return -EAGAIN;
2880 }
2881
Pavel Begunkov014db002020-03-03 21:33:12 +03002882 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002883 return 0;
2884}
2885
Jens Axboe15b71ab2019-12-11 11:20:36 -07002886static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2887{
Jens Axboef8748882020-01-08 17:47:02 -07002888 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002889 int ret;
2890
2891 if (sqe->ioprio || sqe->buf_index)
2892 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002893 if (sqe->flags & IOSQE_FIXED_FILE)
2894 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002895 if (req->flags & REQ_F_NEED_CLEANUP)
2896 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002897
2898 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002899 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002900 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002901 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002902
Jens Axboef8748882020-01-08 17:47:02 -07002903 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002904 if (IS_ERR(req->open.filename)) {
2905 ret = PTR_ERR(req->open.filename);
2906 req->open.filename = NULL;
2907 return ret;
2908 }
2909
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002910 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002911 return 0;
2912}
2913
Jens Axboecebdb982020-01-08 17:59:24 -07002914static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2915{
2916 struct open_how __user *how;
2917 const char __user *fname;
2918 size_t len;
2919 int ret;
2920
2921 if (sqe->ioprio || sqe->buf_index)
2922 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002923 if (sqe->flags & IOSQE_FIXED_FILE)
2924 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002925 if (req->flags & REQ_F_NEED_CLEANUP)
2926 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002927
2928 req->open.dfd = READ_ONCE(sqe->fd);
2929 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2930 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2931 len = READ_ONCE(sqe->len);
2932
2933 if (len < OPEN_HOW_SIZE_VER0)
2934 return -EINVAL;
2935
2936 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2937 len);
2938 if (ret)
2939 return ret;
2940
2941 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2942 req->open.how.flags |= O_LARGEFILE;
2943
2944 req->open.filename = getname(fname);
2945 if (IS_ERR(req->open.filename)) {
2946 ret = PTR_ERR(req->open.filename);
2947 req->open.filename = NULL;
2948 return ret;
2949 }
2950
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002951 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002952 return 0;
2953}
2954
Pavel Begunkov014db002020-03-03 21:33:12 +03002955static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002956{
2957 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002958 struct file *file;
2959 int ret;
2960
Jens Axboef86cd202020-01-29 13:46:44 -07002961 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002962 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002963
Jens Axboecebdb982020-01-08 17:59:24 -07002964 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002965 if (ret)
2966 goto err;
2967
Jens Axboecebdb982020-01-08 17:59:24 -07002968 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002969 if (ret < 0)
2970 goto err;
2971
2972 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2973 if (IS_ERR(file)) {
2974 put_unused_fd(ret);
2975 ret = PTR_ERR(file);
2976 } else {
2977 fsnotify_open(file);
2978 fd_install(ret, file);
2979 }
2980err:
2981 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002982 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002983 if (ret < 0)
2984 req_set_fail_links(req);
2985 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002986 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002987 return 0;
2988}
2989
Pavel Begunkov014db002020-03-03 21:33:12 +03002990static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07002991{
2992 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03002993 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07002994}
2995
Jens Axboeddf0322d2020-02-23 16:41:33 -07002996static int io_provide_buffers_prep(struct io_kiocb *req,
2997 const struct io_uring_sqe *sqe)
2998{
2999 struct io_provide_buf *p = &req->pbuf;
3000 u64 tmp;
3001
3002 if (sqe->ioprio || sqe->rw_flags)
3003 return -EINVAL;
3004
3005 tmp = READ_ONCE(sqe->fd);
3006 if (!tmp || tmp > USHRT_MAX)
3007 return -E2BIG;
3008 p->nbufs = tmp;
3009 p->addr = READ_ONCE(sqe->addr);
3010 p->len = READ_ONCE(sqe->len);
3011
3012 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3013 return -EFAULT;
3014
3015 p->bgid = READ_ONCE(sqe->buf_group);
3016 tmp = READ_ONCE(sqe->off);
3017 if (tmp > USHRT_MAX)
3018 return -E2BIG;
3019 p->bid = tmp;
3020 return 0;
3021}
3022
3023static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3024{
3025 struct io_buffer *buf;
3026 u64 addr = pbuf->addr;
3027 int i, bid = pbuf->bid;
3028
3029 for (i = 0; i < pbuf->nbufs; i++) {
3030 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3031 if (!buf)
3032 break;
3033
3034 buf->addr = addr;
3035 buf->len = pbuf->len;
3036 buf->bid = bid;
3037 addr += pbuf->len;
3038 bid++;
3039 if (!*head) {
3040 INIT_LIST_HEAD(&buf->list);
3041 *head = buf;
3042 } else {
3043 list_add_tail(&buf->list, &(*head)->list);
3044 }
3045 }
3046
3047 return i ? i : -ENOMEM;
3048}
3049
Jens Axboeddf0322d2020-02-23 16:41:33 -07003050static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3051{
3052 struct io_provide_buf *p = &req->pbuf;
3053 struct io_ring_ctx *ctx = req->ctx;
3054 struct io_buffer *head, *list;
3055 int ret = 0;
3056
3057 io_ring_submit_lock(ctx, !force_nonblock);
3058
3059 lockdep_assert_held(&ctx->uring_lock);
3060
3061 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3062
3063 ret = io_add_buffers(p, &head);
3064 if (ret < 0)
3065 goto out;
3066
3067 if (!list) {
3068 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3069 GFP_KERNEL);
3070 if (ret < 0) {
3071 while (!list_empty(&head->list)) {
3072 struct io_buffer *buf;
3073
3074 buf = list_first_entry(&head->list,
3075 struct io_buffer, list);
3076 list_del(&buf->list);
3077 kfree(buf);
3078 }
3079 kfree(head);
3080 goto out;
3081 }
3082 }
3083out:
3084 io_ring_submit_unlock(ctx, !force_nonblock);
3085 if (ret < 0)
3086 req_set_fail_links(req);
3087 io_cqring_add_event(req, ret);
3088 io_put_req(req);
3089 return 0;
3090}
3091
Jens Axboe3e4827b2020-01-08 15:18:09 -07003092static int io_epoll_ctl_prep(struct io_kiocb *req,
3093 const struct io_uring_sqe *sqe)
3094{
3095#if defined(CONFIG_EPOLL)
3096 if (sqe->ioprio || sqe->buf_index)
3097 return -EINVAL;
3098
3099 req->epoll.epfd = READ_ONCE(sqe->fd);
3100 req->epoll.op = READ_ONCE(sqe->len);
3101 req->epoll.fd = READ_ONCE(sqe->off);
3102
3103 if (ep_op_has_event(req->epoll.op)) {
3104 struct epoll_event __user *ev;
3105
3106 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3107 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3108 return -EFAULT;
3109 }
3110
3111 return 0;
3112#else
3113 return -EOPNOTSUPP;
3114#endif
3115}
3116
Pavel Begunkov014db002020-03-03 21:33:12 +03003117static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003118{
3119#if defined(CONFIG_EPOLL)
3120 struct io_epoll *ie = &req->epoll;
3121 int ret;
3122
3123 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3124 if (force_nonblock && ret == -EAGAIN)
3125 return -EAGAIN;
3126
3127 if (ret < 0)
3128 req_set_fail_links(req);
3129 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003130 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003131 return 0;
3132#else
3133 return -EOPNOTSUPP;
3134#endif
3135}
3136
Jens Axboec1ca7572019-12-25 22:18:28 -07003137static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3138{
3139#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3140 if (sqe->ioprio || sqe->buf_index || sqe->off)
3141 return -EINVAL;
3142
3143 req->madvise.addr = READ_ONCE(sqe->addr);
3144 req->madvise.len = READ_ONCE(sqe->len);
3145 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3146 return 0;
3147#else
3148 return -EOPNOTSUPP;
3149#endif
3150}
3151
Pavel Begunkov014db002020-03-03 21:33:12 +03003152static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003153{
3154#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3155 struct io_madvise *ma = &req->madvise;
3156 int ret;
3157
3158 if (force_nonblock)
3159 return -EAGAIN;
3160
3161 ret = do_madvise(ma->addr, ma->len, ma->advice);
3162 if (ret < 0)
3163 req_set_fail_links(req);
3164 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003165 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003166 return 0;
3167#else
3168 return -EOPNOTSUPP;
3169#endif
3170}
3171
Jens Axboe4840e412019-12-25 22:03:45 -07003172static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3173{
3174 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3175 return -EINVAL;
3176
3177 req->fadvise.offset = READ_ONCE(sqe->off);
3178 req->fadvise.len = READ_ONCE(sqe->len);
3179 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3180 return 0;
3181}
3182
Pavel Begunkov014db002020-03-03 21:33:12 +03003183static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003184{
3185 struct io_fadvise *fa = &req->fadvise;
3186 int ret;
3187
Jens Axboe3e694262020-02-01 09:22:49 -07003188 if (force_nonblock) {
3189 switch (fa->advice) {
3190 case POSIX_FADV_NORMAL:
3191 case POSIX_FADV_RANDOM:
3192 case POSIX_FADV_SEQUENTIAL:
3193 break;
3194 default:
3195 return -EAGAIN;
3196 }
3197 }
Jens Axboe4840e412019-12-25 22:03:45 -07003198
3199 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3200 if (ret < 0)
3201 req_set_fail_links(req);
3202 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003203 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003204 return 0;
3205}
3206
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003207static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3208{
Jens Axboef8748882020-01-08 17:47:02 -07003209 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003210 unsigned lookup_flags;
3211 int ret;
3212
3213 if (sqe->ioprio || sqe->buf_index)
3214 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07003215 if (sqe->flags & IOSQE_FIXED_FILE)
3216 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003217 if (req->flags & REQ_F_NEED_CLEANUP)
3218 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003219
3220 req->open.dfd = READ_ONCE(sqe->fd);
3221 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003222 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003223 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003224 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003225
Jens Axboec12cedf2020-01-08 17:41:21 -07003226 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003227 return -EINVAL;
3228
Jens Axboef8748882020-01-08 17:47:02 -07003229 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003230 if (IS_ERR(req->open.filename)) {
3231 ret = PTR_ERR(req->open.filename);
3232 req->open.filename = NULL;
3233 return ret;
3234 }
3235
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003236 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003237 return 0;
3238}
3239
Pavel Begunkov014db002020-03-03 21:33:12 +03003240static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003241{
3242 struct io_open *ctx = &req->open;
3243 unsigned lookup_flags;
3244 struct path path;
3245 struct kstat stat;
3246 int ret;
3247
3248 if (force_nonblock)
3249 return -EAGAIN;
3250
Jens Axboec12cedf2020-01-08 17:41:21 -07003251 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003252 return -EINVAL;
3253
3254retry:
3255 /* filename_lookup() drops it, keep a reference */
3256 ctx->filename->refcnt++;
3257
3258 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3259 NULL);
3260 if (ret)
3261 goto err;
3262
Jens Axboec12cedf2020-01-08 17:41:21 -07003263 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003264 path_put(&path);
3265 if (retry_estale(ret, lookup_flags)) {
3266 lookup_flags |= LOOKUP_REVAL;
3267 goto retry;
3268 }
3269 if (!ret)
3270 ret = cp_statx(&stat, ctx->buffer);
3271err:
3272 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003273 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003274 if (ret < 0)
3275 req_set_fail_links(req);
3276 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003277 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003278 return 0;
3279}
3280
Jens Axboeb5dba592019-12-11 14:02:38 -07003281static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3282{
3283 /*
3284 * If we queue this for async, it must not be cancellable. That would
3285 * leave the 'file' in an undeterminate state.
3286 */
3287 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3288
3289 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3290 sqe->rw_flags || sqe->buf_index)
3291 return -EINVAL;
3292 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003293 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003294
3295 req->close.fd = READ_ONCE(sqe->fd);
3296 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003297 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003298 return -EBADF;
3299
3300 return 0;
3301}
3302
Pavel Begunkova93b3332020-02-08 14:04:34 +03003303/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003304static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003305{
3306 int ret;
3307
3308 ret = filp_close(req->close.put_file, req->work.files);
3309 if (ret < 0)
3310 req_set_fail_links(req);
3311 io_cqring_add_event(req, ret);
3312 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003313 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003314}
3315
Jens Axboeb5dba592019-12-11 14:02:38 -07003316static void io_close_finish(struct io_wq_work **workptr)
3317{
3318 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003319
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003320 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003321 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003322 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003323}
3324
Pavel Begunkov014db002020-03-03 21:33:12 +03003325static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003326{
3327 int ret;
3328
3329 req->close.put_file = NULL;
3330 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3331 if (ret < 0)
3332 return ret;
3333
3334 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003335 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003336 /* submission ref will be dropped, take it for async */
3337 refcount_inc(&req->refs);
3338
Pavel Begunkova2100672020-03-02 23:45:16 +03003339 req->work.func = io_close_finish;
3340 /*
3341 * Do manual async queue here to avoid grabbing files - we don't
3342 * need the files, and it'll cause io_close_finish() to close
3343 * the file again and cause a double CQE entry for this request
3344 */
3345 io_queue_async_work(req);
3346 return 0;
3347 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003348
3349 /*
3350 * No ->flush(), safely close from here and just punt the
3351 * fput() to async context.
3352 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003353 __io_close_finish(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003354 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003355}
3356
Jens Axboe3529d8c2019-12-19 18:24:38 -07003357static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003358{
3359 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003360
3361 if (!req->file)
3362 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003363
3364 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3365 return -EINVAL;
3366 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3367 return -EINVAL;
3368
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003369 req->sync.off = READ_ONCE(sqe->off);
3370 req->sync.len = READ_ONCE(sqe->len);
3371 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003372 return 0;
3373}
3374
Pavel Begunkov014db002020-03-03 21:33:12 +03003375static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003376{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003377 int ret;
3378
Jens Axboe9adbd452019-12-20 08:45:55 -07003379 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003380 req->sync.flags);
3381 if (ret < 0)
3382 req_set_fail_links(req);
3383 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003384 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003385}
3386
3387
3388static void io_sync_file_range_finish(struct io_wq_work **workptr)
3389{
3390 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3391 struct io_kiocb *nxt = NULL;
3392
3393 if (io_req_cancelled(req))
3394 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003395 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003396 io_put_req(req); /* put submission ref */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003397 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003398 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003399}
3400
Pavel Begunkov014db002020-03-03 21:33:12 +03003401static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003402{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003403 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003404 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003405 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003406 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003407 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003408
Pavel Begunkov014db002020-03-03 21:33:12 +03003409 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003410 return 0;
3411}
3412
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003413static int io_setup_async_msg(struct io_kiocb *req,
3414 struct io_async_msghdr *kmsg)
3415{
3416 if (req->io)
3417 return -EAGAIN;
3418 if (io_alloc_async_ctx(req)) {
3419 if (kmsg->iov != kmsg->fast_iov)
3420 kfree(kmsg->iov);
3421 return -ENOMEM;
3422 }
3423 req->flags |= REQ_F_NEED_CLEANUP;
3424 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3425 return -EAGAIN;
3426}
3427
Jens Axboe3529d8c2019-12-19 18:24:38 -07003428static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003429{
Jens Axboe03b12302019-12-02 18:50:25 -07003430#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003431 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003432 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003433 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003434
Jens Axboee47293f2019-12-20 08:58:21 -07003435 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3436 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003437 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003438
Jens Axboed8768362020-02-27 14:17:49 -07003439#ifdef CONFIG_COMPAT
3440 if (req->ctx->compat)
3441 sr->msg_flags |= MSG_CMSG_COMPAT;
3442#endif
3443
Jens Axboefddafac2020-01-04 20:19:44 -07003444 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003445 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003446 /* iovec is already imported */
3447 if (req->flags & REQ_F_NEED_CLEANUP)
3448 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003449
Jens Axboed9688562019-12-09 19:35:20 -07003450 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003451 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003452 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003453 if (!ret)
3454 req->flags |= REQ_F_NEED_CLEANUP;
3455 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003456#else
Jens Axboee47293f2019-12-20 08:58:21 -07003457 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003458#endif
3459}
3460
Pavel Begunkov014db002020-03-03 21:33:12 +03003461static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003462{
3463#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003464 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003465 struct socket *sock;
3466 int ret;
3467
3468 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3469 return -EINVAL;
3470
3471 sock = sock_from_file(req->file, &ret);
3472 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003473 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003474 unsigned flags;
3475
Jens Axboe03b12302019-12-02 18:50:25 -07003476 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003477 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003478 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003479 /* if iov is set, it's allocated already */
3480 if (!kmsg->iov)
3481 kmsg->iov = kmsg->fast_iov;
3482 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003483 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003484 struct io_sr_msg *sr = &req->sr_msg;
3485
Jens Axboe0b416c32019-12-15 10:57:46 -07003486 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003487 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003488
3489 io.msg.iov = io.msg.fast_iov;
3490 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3491 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003492 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003493 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003494 }
3495
Jens Axboee47293f2019-12-20 08:58:21 -07003496 flags = req->sr_msg.msg_flags;
3497 if (flags & MSG_DONTWAIT)
3498 req->flags |= REQ_F_NOWAIT;
3499 else if (force_nonblock)
3500 flags |= MSG_DONTWAIT;
3501
Jens Axboe0b416c32019-12-15 10:57:46 -07003502 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003503 if (force_nonblock && ret == -EAGAIN)
3504 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003505 if (ret == -ERESTARTSYS)
3506 ret = -EINTR;
3507 }
3508
Pavel Begunkov1e950812020-02-06 19:51:16 +03003509 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003510 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003511 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003512 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003513 if (ret < 0)
3514 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003515 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003516 return 0;
3517#else
3518 return -EOPNOTSUPP;
3519#endif
3520}
3521
Pavel Begunkov014db002020-03-03 21:33:12 +03003522static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003523{
3524#if defined(CONFIG_NET)
3525 struct socket *sock;
3526 int ret;
3527
3528 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3529 return -EINVAL;
3530
3531 sock = sock_from_file(req->file, &ret);
3532 if (sock) {
3533 struct io_sr_msg *sr = &req->sr_msg;
3534 struct msghdr msg;
3535 struct iovec iov;
3536 unsigned flags;
3537
3538 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3539 &msg.msg_iter);
3540 if (ret)
3541 return ret;
3542
3543 msg.msg_name = NULL;
3544 msg.msg_control = NULL;
3545 msg.msg_controllen = 0;
3546 msg.msg_namelen = 0;
3547
3548 flags = req->sr_msg.msg_flags;
3549 if (flags & MSG_DONTWAIT)
3550 req->flags |= REQ_F_NOWAIT;
3551 else if (force_nonblock)
3552 flags |= MSG_DONTWAIT;
3553
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003554 msg.msg_flags = flags;
3555 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003556 if (force_nonblock && ret == -EAGAIN)
3557 return -EAGAIN;
3558 if (ret == -ERESTARTSYS)
3559 ret = -EINTR;
3560 }
3561
3562 io_cqring_add_event(req, ret);
3563 if (ret < 0)
3564 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003565 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003566 return 0;
3567#else
3568 return -EOPNOTSUPP;
3569#endif
3570}
3571
Jens Axboebcda7ba2020-02-23 16:42:51 -07003572static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3573 int *cflags, bool needs_lock)
3574{
3575 struct io_sr_msg *sr = &req->sr_msg;
3576 struct io_buffer *kbuf;
3577
3578 if (!(req->flags & REQ_F_BUFFER_SELECT))
3579 return NULL;
3580
3581 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3582 if (IS_ERR(kbuf))
3583 return kbuf;
3584
3585 sr->kbuf = kbuf;
3586 req->flags |= REQ_F_BUFFER_SELECTED;
3587
3588 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3589 *cflags |= IORING_CQE_F_BUFFER;
3590 return kbuf;
3591}
3592
Jens Axboe3529d8c2019-12-19 18:24:38 -07003593static int io_recvmsg_prep(struct io_kiocb *req,
3594 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003595{
3596#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003597 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003598 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003599 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003600
Jens Axboe3529d8c2019-12-19 18:24:38 -07003601 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3602 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003603 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003604 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003605
Jens Axboed8768362020-02-27 14:17:49 -07003606#ifdef CONFIG_COMPAT
3607 if (req->ctx->compat)
3608 sr->msg_flags |= MSG_CMSG_COMPAT;
3609#endif
3610
Jens Axboefddafac2020-01-04 20:19:44 -07003611 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003612 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003613 /* iovec is already imported */
3614 if (req->flags & REQ_F_NEED_CLEANUP)
3615 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003616
Jens Axboed9688562019-12-09 19:35:20 -07003617 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003618 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003619 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003620 if (!ret)
3621 req->flags |= REQ_F_NEED_CLEANUP;
3622 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003623#else
Jens Axboee47293f2019-12-20 08:58:21 -07003624 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003625#endif
3626}
3627
Pavel Begunkov014db002020-03-03 21:33:12 +03003628static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003629{
3630#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003631 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003632 struct socket *sock;
3633 int ret;
3634
3635 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3636 return -EINVAL;
3637
3638 sock = sock_from_file(req->file, &ret);
3639 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003640 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003641 unsigned flags;
3642
Jens Axboe03b12302019-12-02 18:50:25 -07003643 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003644 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003645 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003646 /* if iov is set, it's allocated already */
3647 if (!kmsg->iov)
3648 kmsg->iov = kmsg->fast_iov;
3649 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003650 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003651 struct io_sr_msg *sr = &req->sr_msg;
3652
Jens Axboe0b416c32019-12-15 10:57:46 -07003653 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003654 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003655
3656 io.msg.iov = io.msg.fast_iov;
3657 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3658 sr->msg_flags, &io.msg.uaddr,
3659 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003660 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003661 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003662 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003663
Jens Axboee47293f2019-12-20 08:58:21 -07003664 flags = req->sr_msg.msg_flags;
3665 if (flags & MSG_DONTWAIT)
3666 req->flags |= REQ_F_NOWAIT;
3667 else if (force_nonblock)
3668 flags |= MSG_DONTWAIT;
3669
3670 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3671 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003672 if (force_nonblock && ret == -EAGAIN)
3673 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003674 if (ret == -ERESTARTSYS)
3675 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003676 }
3677
Pavel Begunkov1e950812020-02-06 19:51:16 +03003678 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003679 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003680 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003681 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003682 if (ret < 0)
3683 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003684 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003685 return 0;
3686#else
3687 return -EOPNOTSUPP;
3688#endif
3689}
3690
Pavel Begunkov014db002020-03-03 21:33:12 +03003691static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003692{
3693#if defined(CONFIG_NET)
Jens Axboebcda7ba2020-02-23 16:42:51 -07003694 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003695 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003696 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003697
3698 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3699 return -EINVAL;
3700
3701 sock = sock_from_file(req->file, &ret);
3702 if (sock) {
3703 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003704 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003705 struct msghdr msg;
3706 struct iovec iov;
3707 unsigned flags;
3708
Jens Axboebcda7ba2020-02-23 16:42:51 -07003709 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3710 if (IS_ERR(kbuf))
3711 return PTR_ERR(kbuf);
3712 else if (kbuf)
3713 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003714
Jens Axboebcda7ba2020-02-23 16:42:51 -07003715 ret = import_single_range(READ, buf, sr->len, &iov,
3716 &msg.msg_iter);
3717 if (ret) {
3718 kfree(kbuf);
3719 return ret;
3720 }
3721
3722 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003723 msg.msg_name = NULL;
3724 msg.msg_control = NULL;
3725 msg.msg_controllen = 0;
3726 msg.msg_namelen = 0;
3727 msg.msg_iocb = NULL;
3728 msg.msg_flags = 0;
3729
3730 flags = req->sr_msg.msg_flags;
3731 if (flags & MSG_DONTWAIT)
3732 req->flags |= REQ_F_NOWAIT;
3733 else if (force_nonblock)
3734 flags |= MSG_DONTWAIT;
3735
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003736 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003737 if (force_nonblock && ret == -EAGAIN)
3738 return -EAGAIN;
3739 if (ret == -ERESTARTSYS)
3740 ret = -EINTR;
3741 }
3742
Jens Axboebcda7ba2020-02-23 16:42:51 -07003743 kfree(kbuf);
3744 req->flags &= ~REQ_F_NEED_CLEANUP;
3745 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003746 if (ret < 0)
3747 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003748 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003749 return 0;
3750#else
3751 return -EOPNOTSUPP;
3752#endif
3753}
3754
3755
Jens Axboe3529d8c2019-12-19 18:24:38 -07003756static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003757{
3758#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003759 struct io_accept *accept = &req->accept;
3760
Jens Axboe17f2fe32019-10-17 14:42:58 -06003761 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3762 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003763 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003764 return -EINVAL;
3765
Jens Axboed55e5f52019-12-11 16:12:15 -07003766 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3767 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003768 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003769 return 0;
3770#else
3771 return -EOPNOTSUPP;
3772#endif
3773}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003774
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003775#if defined(CONFIG_NET)
Pavel Begunkov014db002020-03-03 21:33:12 +03003776static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003777{
3778 struct io_accept *accept = &req->accept;
3779 unsigned file_flags;
3780 int ret;
3781
3782 file_flags = force_nonblock ? O_NONBLOCK : 0;
3783 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3784 accept->addr_len, accept->flags);
3785 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003786 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003787 if (ret == -ERESTARTSYS)
3788 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003789 if (ret < 0)
3790 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003791 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003792 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003793 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003794}
3795
3796static void io_accept_finish(struct io_wq_work **workptr)
3797{
3798 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003799
3800 if (io_req_cancelled(req))
3801 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003802 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003803 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003804}
3805#endif
3806
Pavel Begunkov014db002020-03-03 21:33:12 +03003807static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003808{
3809#if defined(CONFIG_NET)
3810 int ret;
3811
Pavel Begunkov014db002020-03-03 21:33:12 +03003812 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003813 if (ret == -EAGAIN && force_nonblock) {
3814 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003815 return -EAGAIN;
3816 }
3817 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003818#else
3819 return -EOPNOTSUPP;
3820#endif
3821}
3822
Jens Axboe3529d8c2019-12-19 18:24:38 -07003823static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003824{
3825#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003826 struct io_connect *conn = &req->connect;
3827 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003828
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003829 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3830 return -EINVAL;
3831 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3832 return -EINVAL;
3833
Jens Axboe3529d8c2019-12-19 18:24:38 -07003834 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3835 conn->addr_len = READ_ONCE(sqe->addr2);
3836
3837 if (!io)
3838 return 0;
3839
3840 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003841 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003842#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003843 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003844#endif
3845}
3846
Pavel Begunkov014db002020-03-03 21:33:12 +03003847static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003848{
3849#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003850 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003851 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003852 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003853
Jens Axboef499a022019-12-02 16:28:46 -07003854 if (req->io) {
3855 io = req->io;
3856 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003857 ret = move_addr_to_kernel(req->connect.addr,
3858 req->connect.addr_len,
3859 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003860 if (ret)
3861 goto out;
3862 io = &__io;
3863 }
3864
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003865 file_flags = force_nonblock ? O_NONBLOCK : 0;
3866
3867 ret = __sys_connect_file(req->file, &io->connect.address,
3868 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003869 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003870 if (req->io)
3871 return -EAGAIN;
3872 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003873 ret = -ENOMEM;
3874 goto out;
3875 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003876 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003877 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003878 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003879 if (ret == -ERESTARTSYS)
3880 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003881out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003882 if (ret < 0)
3883 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003884 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003885 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003886 return 0;
3887#else
3888 return -EOPNOTSUPP;
3889#endif
3890}
3891
Jens Axboed7718a92020-02-14 22:23:12 -07003892struct io_poll_table {
3893 struct poll_table_struct pt;
3894 struct io_kiocb *req;
3895 int error;
3896};
3897
3898static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
3899 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003900{
Jens Axboed7718a92020-02-14 22:23:12 -07003901 if (unlikely(poll->head)) {
3902 pt->error = -EINVAL;
3903 return;
3904 }
3905
3906 pt->error = 0;
3907 poll->head = head;
3908 add_wait_queue(head, &poll->wait);
3909}
3910
3911static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
3912 struct poll_table_struct *p)
3913{
3914 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3915
3916 __io_queue_proc(&pt->req->apoll->poll, pt, head);
3917}
3918
3919static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
3920 __poll_t mask, task_work_func_t func)
3921{
3922 struct task_struct *tsk;
3923
3924 /* for instances that support it check for an event match first: */
3925 if (mask && !(mask & poll->events))
3926 return 0;
3927
3928 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
3929
3930 list_del_init(&poll->wait.entry);
3931
3932 tsk = req->task;
3933 req->result = mask;
3934 init_task_work(&req->task_work, func);
3935 /*
3936 * If this fails, then the task is exiting. If that is the case, then
3937 * the exit check will ultimately cancel these work items. Hence we
3938 * don't need to check here and handle it specifically.
3939 */
3940 task_work_add(tsk, &req->task_work, true);
3941 wake_up_process(tsk);
3942 return 1;
3943}
3944
3945static void io_async_task_func(struct callback_head *cb)
3946{
3947 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3948 struct async_poll *apoll = req->apoll;
3949 struct io_ring_ctx *ctx = req->ctx;
3950
3951 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
3952
3953 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
3954
3955 if (hash_hashed(&req->hash_node)) {
3956 spin_lock_irq(&ctx->completion_lock);
3957 hash_del(&req->hash_node);
3958 spin_unlock_irq(&ctx->completion_lock);
3959 }
3960
3961 /* restore ->work in case we need to retry again */
3962 memcpy(&req->work, &apoll->work, sizeof(req->work));
3963
3964 __set_current_state(TASK_RUNNING);
3965 mutex_lock(&ctx->uring_lock);
3966 __io_queue_sqe(req, NULL);
3967 mutex_unlock(&ctx->uring_lock);
3968
3969 kfree(apoll);
3970}
3971
3972static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3973 void *key)
3974{
3975 struct io_kiocb *req = wait->private;
3976 struct io_poll_iocb *poll = &req->apoll->poll;
3977
3978 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
3979 key_to_poll(key));
3980
3981 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
3982}
3983
3984static void io_poll_req_insert(struct io_kiocb *req)
3985{
3986 struct io_ring_ctx *ctx = req->ctx;
3987 struct hlist_head *list;
3988
3989 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3990 hlist_add_head(&req->hash_node, list);
3991}
3992
3993static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
3994 struct io_poll_iocb *poll,
3995 struct io_poll_table *ipt, __poll_t mask,
3996 wait_queue_func_t wake_func)
3997 __acquires(&ctx->completion_lock)
3998{
3999 struct io_ring_ctx *ctx = req->ctx;
4000 bool cancel = false;
4001
4002 poll->file = req->file;
4003 poll->head = NULL;
4004 poll->done = poll->canceled = false;
4005 poll->events = mask;
4006
4007 ipt->pt._key = mask;
4008 ipt->req = req;
4009 ipt->error = -EINVAL;
4010
4011 INIT_LIST_HEAD(&poll->wait.entry);
4012 init_waitqueue_func_entry(&poll->wait, wake_func);
4013 poll->wait.private = req;
4014
4015 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4016
4017 spin_lock_irq(&ctx->completion_lock);
4018 if (likely(poll->head)) {
4019 spin_lock(&poll->head->lock);
4020 if (unlikely(list_empty(&poll->wait.entry))) {
4021 if (ipt->error)
4022 cancel = true;
4023 ipt->error = 0;
4024 mask = 0;
4025 }
4026 if (mask || ipt->error)
4027 list_del_init(&poll->wait.entry);
4028 else if (cancel)
4029 WRITE_ONCE(poll->canceled, true);
4030 else if (!poll->done) /* actually waiting for an event */
4031 io_poll_req_insert(req);
4032 spin_unlock(&poll->head->lock);
4033 }
4034
4035 return mask;
4036}
4037
4038static bool io_arm_poll_handler(struct io_kiocb *req)
4039{
4040 const struct io_op_def *def = &io_op_defs[req->opcode];
4041 struct io_ring_ctx *ctx = req->ctx;
4042 struct async_poll *apoll;
4043 struct io_poll_table ipt;
4044 __poll_t mask, ret;
4045
4046 if (!req->file || !file_can_poll(req->file))
4047 return false;
4048 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4049 return false;
4050 if (!def->pollin && !def->pollout)
4051 return false;
4052
4053 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4054 if (unlikely(!apoll))
4055 return false;
4056
4057 req->flags |= REQ_F_POLLED;
4058 memcpy(&apoll->work, &req->work, sizeof(req->work));
4059
4060 /*
4061 * Don't need a reference here, as we're adding it to the task
4062 * task_works list. If the task exits, the list is pruned.
4063 */
4064 req->task = current;
4065 req->apoll = apoll;
4066 INIT_HLIST_NODE(&req->hash_node);
4067
Nathan Chancellor8755d972020-03-02 16:01:19 -07004068 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004069 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004070 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004071 if (def->pollout)
4072 mask |= POLLOUT | POLLWRNORM;
4073 mask |= POLLERR | POLLPRI;
4074
4075 ipt.pt._qproc = io_async_queue_proc;
4076
4077 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4078 io_async_wake);
4079 if (ret) {
4080 ipt.error = 0;
4081 apoll->poll.done = true;
4082 spin_unlock_irq(&ctx->completion_lock);
4083 memcpy(&req->work, &apoll->work, sizeof(req->work));
4084 kfree(apoll);
4085 return false;
4086 }
4087 spin_unlock_irq(&ctx->completion_lock);
4088 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4089 apoll->poll.events);
4090 return true;
4091}
4092
4093static bool __io_poll_remove_one(struct io_kiocb *req,
4094 struct io_poll_iocb *poll)
4095{
Jens Axboeb41e9852020-02-17 09:52:41 -07004096 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004097
4098 spin_lock(&poll->head->lock);
4099 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004100 if (!list_empty(&poll->wait.entry)) {
4101 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004102 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004103 }
4104 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004105 return do_complete;
4106}
4107
4108static bool io_poll_remove_one(struct io_kiocb *req)
4109{
4110 bool do_complete;
4111
4112 if (req->opcode == IORING_OP_POLL_ADD) {
4113 do_complete = __io_poll_remove_one(req, &req->poll);
4114 } else {
4115 /* non-poll requests have submit ref still */
4116 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4117 if (do_complete)
4118 io_put_req(req);
4119 }
4120
Jens Axboe78076bb2019-12-04 19:56:40 -07004121 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004122
Jens Axboeb41e9852020-02-17 09:52:41 -07004123 if (do_complete) {
4124 io_cqring_fill_event(req, -ECANCELED);
4125 io_commit_cqring(req->ctx);
4126 req->flags |= REQ_F_COMP_LOCKED;
4127 io_put_req(req);
4128 }
4129
4130 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004131}
4132
4133static void io_poll_remove_all(struct io_ring_ctx *ctx)
4134{
Jens Axboe78076bb2019-12-04 19:56:40 -07004135 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004136 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07004137 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004138
4139 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004140 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4141 struct hlist_head *list;
4142
4143 list = &ctx->cancel_hash[i];
4144 hlist_for_each_entry_safe(req, tmp, list, hash_node)
4145 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004146 }
4147 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004148
4149 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004150}
4151
Jens Axboe47f46762019-11-09 17:43:02 -07004152static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4153{
Jens Axboe78076bb2019-12-04 19:56:40 -07004154 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004155 struct io_kiocb *req;
4156
Jens Axboe78076bb2019-12-04 19:56:40 -07004157 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4158 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004159 if (sqe_addr != req->user_data)
4160 continue;
4161 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004162 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004163 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004164 }
4165
4166 return -ENOENT;
4167}
4168
Jens Axboe3529d8c2019-12-19 18:24:38 -07004169static int io_poll_remove_prep(struct io_kiocb *req,
4170 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004171{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004172 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4173 return -EINVAL;
4174 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4175 sqe->poll_events)
4176 return -EINVAL;
4177
Jens Axboe0969e782019-12-17 18:40:57 -07004178 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004179 return 0;
4180}
4181
4182/*
4183 * Find a running poll command that matches one specified in sqe->addr,
4184 * and remove it if found.
4185 */
4186static int io_poll_remove(struct io_kiocb *req)
4187{
4188 struct io_ring_ctx *ctx = req->ctx;
4189 u64 addr;
4190 int ret;
4191
Jens Axboe0969e782019-12-17 18:40:57 -07004192 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004193 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004194 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004195 spin_unlock_irq(&ctx->completion_lock);
4196
Jens Axboe78e19bb2019-11-06 15:21:34 -07004197 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004198 if (ret < 0)
4199 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004200 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004201 return 0;
4202}
4203
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004204static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004205{
Jackie Liua197f662019-11-08 08:09:12 -07004206 struct io_ring_ctx *ctx = req->ctx;
4207
Jens Axboe8c838782019-03-12 15:48:16 -06004208 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004209 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004210 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004211}
4212
Jens Axboeb41e9852020-02-17 09:52:41 -07004213static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004214{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004215 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004216
Jens Axboe221c5eb2019-01-17 09:41:58 -07004217 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004218 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004219 io_poll_complete(req, req->result, 0);
4220 req->flags |= REQ_F_COMP_LOCKED;
4221 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004222 spin_unlock_irq(&ctx->completion_lock);
4223
Jens Axboe8c838782019-03-12 15:48:16 -06004224 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07004225}
Jens Axboe89723d02019-11-05 15:32:58 -07004226
Jens Axboeb41e9852020-02-17 09:52:41 -07004227static void io_poll_task_func(struct callback_head *cb)
4228{
4229 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4230 struct io_kiocb *nxt = NULL;
4231
4232 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004233 if (nxt) {
4234 struct io_ring_ctx *ctx = nxt->ctx;
4235
4236 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004237 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004238 mutex_unlock(&ctx->uring_lock);
4239 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004240}
4241
Jens Axboe221c5eb2019-01-17 09:41:58 -07004242static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4243 void *key)
4244{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004245 struct io_kiocb *req = wait->private;
4246 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004247
Jens Axboed7718a92020-02-14 22:23:12 -07004248 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004249}
4250
Jens Axboe221c5eb2019-01-17 09:41:58 -07004251static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4252 struct poll_table_struct *p)
4253{
4254 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4255
Jens Axboed7718a92020-02-14 22:23:12 -07004256 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004257}
4258
Jens Axboe3529d8c2019-12-19 18:24:38 -07004259static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004260{
4261 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004262 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004263
4264 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4265 return -EINVAL;
4266 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4267 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004268 if (!poll->file)
4269 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004270
Jens Axboe221c5eb2019-01-17 09:41:58 -07004271 events = READ_ONCE(sqe->poll_events);
4272 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004273
Jens Axboed7718a92020-02-14 22:23:12 -07004274 /*
4275 * Don't need a reference here, as we're adding it to the task
4276 * task_works list. If the task exits, the list is pruned.
4277 */
Jens Axboeb41e9852020-02-17 09:52:41 -07004278 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004279 return 0;
4280}
4281
Pavel Begunkov014db002020-03-03 21:33:12 +03004282static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004283{
4284 struct io_poll_iocb *poll = &req->poll;
4285 struct io_ring_ctx *ctx = req->ctx;
4286 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004287 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004288
Jens Axboe78076bb2019-12-04 19:56:40 -07004289 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004290 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004291 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004292
Jens Axboed7718a92020-02-14 22:23:12 -07004293 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4294 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004295
Jens Axboe8c838782019-03-12 15:48:16 -06004296 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004297 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004298 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004299 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004300 spin_unlock_irq(&ctx->completion_lock);
4301
Jens Axboe8c838782019-03-12 15:48:16 -06004302 if (mask) {
4303 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004304 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004305 }
Jens Axboe8c838782019-03-12 15:48:16 -06004306 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004307}
4308
Jens Axboe5262f562019-09-17 12:26:57 -06004309static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4310{
Jens Axboead8a48a2019-11-15 08:49:11 -07004311 struct io_timeout_data *data = container_of(timer,
4312 struct io_timeout_data, timer);
4313 struct io_kiocb *req = data->req;
4314 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004315 unsigned long flags;
4316
Jens Axboe5262f562019-09-17 12:26:57 -06004317 atomic_inc(&ctx->cq_timeouts);
4318
4319 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004320 /*
Jens Axboe11365042019-10-16 09:08:32 -06004321 * We could be racing with timeout deletion. If the list is empty,
4322 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004323 */
Jens Axboe842f9612019-10-29 12:34:10 -06004324 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004325 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004326
Jens Axboe11365042019-10-16 09:08:32 -06004327 /*
4328 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004329 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004330 * pointer will be increased, otherwise other timeout reqs may
4331 * return in advance without waiting for enough wait_nr.
4332 */
4333 prev = req;
4334 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4335 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004336 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004337 }
Jens Axboe842f9612019-10-29 12:34:10 -06004338
Jens Axboe78e19bb2019-11-06 15:21:34 -07004339 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004340 io_commit_cqring(ctx);
4341 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4342
4343 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004344 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004345 io_put_req(req);
4346 return HRTIMER_NORESTART;
4347}
4348
Jens Axboe47f46762019-11-09 17:43:02 -07004349static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4350{
4351 struct io_kiocb *req;
4352 int ret = -ENOENT;
4353
4354 list_for_each_entry(req, &ctx->timeout_list, list) {
4355 if (user_data == req->user_data) {
4356 list_del_init(&req->list);
4357 ret = 0;
4358 break;
4359 }
4360 }
4361
4362 if (ret == -ENOENT)
4363 return ret;
4364
Jens Axboe2d283902019-12-04 11:08:05 -07004365 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004366 if (ret == -1)
4367 return -EALREADY;
4368
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004369 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004370 io_cqring_fill_event(req, -ECANCELED);
4371 io_put_req(req);
4372 return 0;
4373}
4374
Jens Axboe3529d8c2019-12-19 18:24:38 -07004375static int io_timeout_remove_prep(struct io_kiocb *req,
4376 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004377{
Jens Axboeb29472e2019-12-17 18:50:29 -07004378 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4379 return -EINVAL;
4380 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4381 return -EINVAL;
4382
4383 req->timeout.addr = READ_ONCE(sqe->addr);
4384 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4385 if (req->timeout.flags)
4386 return -EINVAL;
4387
Jens Axboeb29472e2019-12-17 18:50:29 -07004388 return 0;
4389}
4390
Jens Axboe11365042019-10-16 09:08:32 -06004391/*
4392 * Remove or update an existing timeout command
4393 */
Jens Axboefc4df992019-12-10 14:38:45 -07004394static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004395{
4396 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004397 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004398
Jens Axboe11365042019-10-16 09:08:32 -06004399 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004400 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004401
Jens Axboe47f46762019-11-09 17:43:02 -07004402 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004403 io_commit_cqring(ctx);
4404 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004405 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004406 if (ret < 0)
4407 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004408 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004409 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004410}
4411
Jens Axboe3529d8c2019-12-19 18:24:38 -07004412static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004413 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004414{
Jens Axboead8a48a2019-11-15 08:49:11 -07004415 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004416 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004417
Jens Axboead8a48a2019-11-15 08:49:11 -07004418 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004419 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004420 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004421 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004422 if (sqe->off && is_timeout_link)
4423 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004424 flags = READ_ONCE(sqe->timeout_flags);
4425 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004426 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004427
Jens Axboe26a61672019-12-20 09:02:01 -07004428 req->timeout.count = READ_ONCE(sqe->off);
4429
Jens Axboe3529d8c2019-12-19 18:24:38 -07004430 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004431 return -ENOMEM;
4432
4433 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004434 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004435 req->flags |= REQ_F_TIMEOUT;
4436
4437 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004438 return -EFAULT;
4439
Jens Axboe11365042019-10-16 09:08:32 -06004440 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004441 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004442 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004443 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004444
Jens Axboead8a48a2019-11-15 08:49:11 -07004445 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4446 return 0;
4447}
4448
Jens Axboefc4df992019-12-10 14:38:45 -07004449static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004450{
4451 unsigned count;
4452 struct io_ring_ctx *ctx = req->ctx;
4453 struct io_timeout_data *data;
4454 struct list_head *entry;
4455 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004456
Jens Axboe2d283902019-12-04 11:08:05 -07004457 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004458
Jens Axboe5262f562019-09-17 12:26:57 -06004459 /*
4460 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004461 * timeout event to be satisfied. If it isn't set, then this is
4462 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004463 */
Jens Axboe26a61672019-12-20 09:02:01 -07004464 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004465 if (!count) {
4466 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4467 spin_lock_irq(&ctx->completion_lock);
4468 entry = ctx->timeout_list.prev;
4469 goto add;
4470 }
Jens Axboe5262f562019-09-17 12:26:57 -06004471
4472 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004473 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004474
4475 /*
4476 * Insertion sort, ensuring the first entry in the list is always
4477 * the one we need first.
4478 */
Jens Axboe5262f562019-09-17 12:26:57 -06004479 spin_lock_irq(&ctx->completion_lock);
4480 list_for_each_prev(entry, &ctx->timeout_list) {
4481 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004482 unsigned nxt_sq_head;
4483 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004484 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004485
Jens Axboe93bd25b2019-11-11 23:34:31 -07004486 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4487 continue;
4488
yangerkun5da0fb12019-10-15 21:59:29 +08004489 /*
4490 * Since cached_sq_head + count - 1 can overflow, use type long
4491 * long to store it.
4492 */
4493 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004494 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4495 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004496
4497 /*
4498 * cached_sq_head may overflow, and it will never overflow twice
4499 * once there is some timeout req still be valid.
4500 */
4501 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004502 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004503
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004504 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004505 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004506
4507 /*
4508 * Sequence of reqs after the insert one and itself should
4509 * be adjusted because each timeout req consumes a slot.
4510 */
4511 span++;
4512 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004513 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004514 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004515add:
Jens Axboe5262f562019-09-17 12:26:57 -06004516 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004517 data->timer.function = io_timeout_fn;
4518 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004519 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004520 return 0;
4521}
4522
Jens Axboe62755e32019-10-28 21:49:21 -06004523static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004524{
Jens Axboe62755e32019-10-28 21:49:21 -06004525 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004526
Jens Axboe62755e32019-10-28 21:49:21 -06004527 return req->user_data == (unsigned long) data;
4528}
4529
Jens Axboee977d6d2019-11-05 12:39:45 -07004530static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004531{
Jens Axboe62755e32019-10-28 21:49:21 -06004532 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004533 int ret = 0;
4534
Jens Axboe62755e32019-10-28 21:49:21 -06004535 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4536 switch (cancel_ret) {
4537 case IO_WQ_CANCEL_OK:
4538 ret = 0;
4539 break;
4540 case IO_WQ_CANCEL_RUNNING:
4541 ret = -EALREADY;
4542 break;
4543 case IO_WQ_CANCEL_NOTFOUND:
4544 ret = -ENOENT;
4545 break;
4546 }
4547
Jens Axboee977d6d2019-11-05 12:39:45 -07004548 return ret;
4549}
4550
Jens Axboe47f46762019-11-09 17:43:02 -07004551static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4552 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004553 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004554{
4555 unsigned long flags;
4556 int ret;
4557
4558 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4559 if (ret != -ENOENT) {
4560 spin_lock_irqsave(&ctx->completion_lock, flags);
4561 goto done;
4562 }
4563
4564 spin_lock_irqsave(&ctx->completion_lock, flags);
4565 ret = io_timeout_cancel(ctx, sqe_addr);
4566 if (ret != -ENOENT)
4567 goto done;
4568 ret = io_poll_cancel(ctx, sqe_addr);
4569done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004570 if (!ret)
4571 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004572 io_cqring_fill_event(req, ret);
4573 io_commit_cqring(ctx);
4574 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4575 io_cqring_ev_posted(ctx);
4576
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004577 if (ret < 0)
4578 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004579 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004580}
4581
Jens Axboe3529d8c2019-12-19 18:24:38 -07004582static int io_async_cancel_prep(struct io_kiocb *req,
4583 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004584{
Jens Axboefbf23842019-12-17 18:45:56 -07004585 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004586 return -EINVAL;
4587 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4588 sqe->cancel_flags)
4589 return -EINVAL;
4590
Jens Axboefbf23842019-12-17 18:45:56 -07004591 req->cancel.addr = READ_ONCE(sqe->addr);
4592 return 0;
4593}
4594
Pavel Begunkov014db002020-03-03 21:33:12 +03004595static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004596{
4597 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004598
Pavel Begunkov014db002020-03-03 21:33:12 +03004599 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004600 return 0;
4601}
4602
Jens Axboe05f3fb32019-12-09 11:22:50 -07004603static int io_files_update_prep(struct io_kiocb *req,
4604 const struct io_uring_sqe *sqe)
4605{
4606 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4607 return -EINVAL;
4608
4609 req->files_update.offset = READ_ONCE(sqe->off);
4610 req->files_update.nr_args = READ_ONCE(sqe->len);
4611 if (!req->files_update.nr_args)
4612 return -EINVAL;
4613 req->files_update.arg = READ_ONCE(sqe->addr);
4614 return 0;
4615}
4616
4617static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4618{
4619 struct io_ring_ctx *ctx = req->ctx;
4620 struct io_uring_files_update up;
4621 int ret;
4622
Jens Axboef86cd202020-01-29 13:46:44 -07004623 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004624 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004625
4626 up.offset = req->files_update.offset;
4627 up.fds = req->files_update.arg;
4628
4629 mutex_lock(&ctx->uring_lock);
4630 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4631 mutex_unlock(&ctx->uring_lock);
4632
4633 if (ret < 0)
4634 req_set_fail_links(req);
4635 io_cqring_add_event(req, ret);
4636 io_put_req(req);
4637 return 0;
4638}
4639
Jens Axboe3529d8c2019-12-19 18:24:38 -07004640static int io_req_defer_prep(struct io_kiocb *req,
4641 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004642{
Jens Axboee7815732019-12-17 19:45:06 -07004643 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004644
Jens Axboef86cd202020-01-29 13:46:44 -07004645 if (io_op_defs[req->opcode].file_table) {
4646 ret = io_grab_files(req);
4647 if (unlikely(ret))
4648 return ret;
4649 }
4650
Jens Axboecccf0ee2020-01-27 16:34:48 -07004651 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4652
Jens Axboed625c6e2019-12-17 19:53:05 -07004653 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004654 case IORING_OP_NOP:
4655 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004656 case IORING_OP_READV:
4657 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004658 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004659 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004660 break;
4661 case IORING_OP_WRITEV:
4662 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004663 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004664 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004665 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004666 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004667 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004668 break;
4669 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004670 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004671 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004672 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004673 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004674 break;
4675 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004676 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004677 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004678 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004679 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004680 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004681 break;
4682 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004683 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004684 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004685 break;
Jens Axboef499a022019-12-02 16:28:46 -07004686 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004687 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004688 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004689 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004690 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004691 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004692 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004693 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004694 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004695 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004696 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004697 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004698 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004699 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004700 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004701 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004702 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004703 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004704 case IORING_OP_FALLOCATE:
4705 ret = io_fallocate_prep(req, sqe);
4706 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004707 case IORING_OP_OPENAT:
4708 ret = io_openat_prep(req, sqe);
4709 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004710 case IORING_OP_CLOSE:
4711 ret = io_close_prep(req, sqe);
4712 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004713 case IORING_OP_FILES_UPDATE:
4714 ret = io_files_update_prep(req, sqe);
4715 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004716 case IORING_OP_STATX:
4717 ret = io_statx_prep(req, sqe);
4718 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004719 case IORING_OP_FADVISE:
4720 ret = io_fadvise_prep(req, sqe);
4721 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004722 case IORING_OP_MADVISE:
4723 ret = io_madvise_prep(req, sqe);
4724 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004725 case IORING_OP_OPENAT2:
4726 ret = io_openat2_prep(req, sqe);
4727 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004728 case IORING_OP_EPOLL_CTL:
4729 ret = io_epoll_ctl_prep(req, sqe);
4730 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004731 case IORING_OP_SPLICE:
4732 ret = io_splice_prep(req, sqe);
4733 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004734 case IORING_OP_PROVIDE_BUFFERS:
4735 ret = io_provide_buffers_prep(req, sqe);
4736 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004737 default:
Jens Axboee7815732019-12-17 19:45:06 -07004738 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4739 req->opcode);
4740 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004741 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004742 }
4743
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004744 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004745}
4746
Jens Axboe3529d8c2019-12-19 18:24:38 -07004747static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004748{
Jackie Liua197f662019-11-08 08:09:12 -07004749 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004750 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004751
Bob Liu9d858b22019-11-13 18:06:25 +08004752 /* Still need defer if there is pending req in defer list. */
4753 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004754 return 0;
4755
Jens Axboe3529d8c2019-12-19 18:24:38 -07004756 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004757 return -EAGAIN;
4758
Jens Axboe3529d8c2019-12-19 18:24:38 -07004759 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004760 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004761 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004762
Jens Axboede0617e2019-04-06 21:51:27 -06004763 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004764 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004765 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004766 return 0;
4767 }
4768
Jens Axboe915967f2019-11-21 09:01:20 -07004769 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004770 list_add_tail(&req->list, &ctx->defer_list);
4771 spin_unlock_irq(&ctx->completion_lock);
4772 return -EIOCBQUEUED;
4773}
4774
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004775static void io_cleanup_req(struct io_kiocb *req)
4776{
4777 struct io_async_ctx *io = req->io;
4778
4779 switch (req->opcode) {
4780 case IORING_OP_READV:
4781 case IORING_OP_READ_FIXED:
4782 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07004783 if (req->flags & REQ_F_BUFFER_SELECTED)
4784 kfree((void *)(unsigned long)req->rw.addr);
4785 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004786 case IORING_OP_WRITEV:
4787 case IORING_OP_WRITE_FIXED:
4788 case IORING_OP_WRITE:
4789 if (io->rw.iov != io->rw.fast_iov)
4790 kfree(io->rw.iov);
4791 break;
4792 case IORING_OP_SENDMSG:
4793 case IORING_OP_RECVMSG:
4794 if (io->msg.iov != io->msg.fast_iov)
4795 kfree(io->msg.iov);
4796 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004797 case IORING_OP_RECV:
4798 if (req->flags & REQ_F_BUFFER_SELECTED)
4799 kfree(req->sr_msg.kbuf);
4800 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004801 case IORING_OP_OPENAT:
4802 case IORING_OP_OPENAT2:
4803 case IORING_OP_STATX:
4804 putname(req->open.filename);
4805 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004806 case IORING_OP_SPLICE:
4807 io_put_file(req, req->splice.file_in,
4808 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4809 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004810 }
4811
4812 req->flags &= ~REQ_F_NEED_CLEANUP;
4813}
4814
Jens Axboe3529d8c2019-12-19 18:24:38 -07004815static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03004816 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004817{
Jackie Liua197f662019-11-08 08:09:12 -07004818 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004819 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004820
Jens Axboed625c6e2019-12-17 19:53:05 -07004821 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004822 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004823 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004824 break;
4825 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004826 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004827 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004828 if (sqe) {
4829 ret = io_read_prep(req, sqe, force_nonblock);
4830 if (ret < 0)
4831 break;
4832 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004833 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004834 break;
4835 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004836 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004837 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004838 if (sqe) {
4839 ret = io_write_prep(req, sqe, force_nonblock);
4840 if (ret < 0)
4841 break;
4842 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004843 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004844 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004845 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004846 if (sqe) {
4847 ret = io_prep_fsync(req, sqe);
4848 if (ret < 0)
4849 break;
4850 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004851 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004852 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004853 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004854 if (sqe) {
4855 ret = io_poll_add_prep(req, sqe);
4856 if (ret)
4857 break;
4858 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004859 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004860 break;
4861 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004862 if (sqe) {
4863 ret = io_poll_remove_prep(req, sqe);
4864 if (ret < 0)
4865 break;
4866 }
Jens Axboefc4df992019-12-10 14:38:45 -07004867 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004868 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004869 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004870 if (sqe) {
4871 ret = io_prep_sfr(req, sqe);
4872 if (ret < 0)
4873 break;
4874 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004875 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004876 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004877 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004878 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004879 if (sqe) {
4880 ret = io_sendmsg_prep(req, sqe);
4881 if (ret < 0)
4882 break;
4883 }
Jens Axboefddafac2020-01-04 20:19:44 -07004884 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004885 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004886 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004887 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004888 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004889 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004890 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004891 if (sqe) {
4892 ret = io_recvmsg_prep(req, sqe);
4893 if (ret)
4894 break;
4895 }
Jens Axboefddafac2020-01-04 20:19:44 -07004896 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004897 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004898 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004899 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004900 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004901 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004902 if (sqe) {
4903 ret = io_timeout_prep(req, sqe, false);
4904 if (ret)
4905 break;
4906 }
Jens Axboefc4df992019-12-10 14:38:45 -07004907 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004908 break;
Jens Axboe11365042019-10-16 09:08:32 -06004909 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004910 if (sqe) {
4911 ret = io_timeout_remove_prep(req, sqe);
4912 if (ret)
4913 break;
4914 }
Jens Axboefc4df992019-12-10 14:38:45 -07004915 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004916 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004917 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004918 if (sqe) {
4919 ret = io_accept_prep(req, sqe);
4920 if (ret)
4921 break;
4922 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004923 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004924 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004925 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004926 if (sqe) {
4927 ret = io_connect_prep(req, sqe);
4928 if (ret)
4929 break;
4930 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004931 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004932 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004933 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004934 if (sqe) {
4935 ret = io_async_cancel_prep(req, sqe);
4936 if (ret)
4937 break;
4938 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004939 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06004940 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004941 case IORING_OP_FALLOCATE:
4942 if (sqe) {
4943 ret = io_fallocate_prep(req, sqe);
4944 if (ret)
4945 break;
4946 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004947 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07004948 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004949 case IORING_OP_OPENAT:
4950 if (sqe) {
4951 ret = io_openat_prep(req, sqe);
4952 if (ret)
4953 break;
4954 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004955 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004956 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004957 case IORING_OP_CLOSE:
4958 if (sqe) {
4959 ret = io_close_prep(req, sqe);
4960 if (ret)
4961 break;
4962 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004963 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07004964 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004965 case IORING_OP_FILES_UPDATE:
4966 if (sqe) {
4967 ret = io_files_update_prep(req, sqe);
4968 if (ret)
4969 break;
4970 }
4971 ret = io_files_update(req, force_nonblock);
4972 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004973 case IORING_OP_STATX:
4974 if (sqe) {
4975 ret = io_statx_prep(req, sqe);
4976 if (ret)
4977 break;
4978 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004979 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004980 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004981 case IORING_OP_FADVISE:
4982 if (sqe) {
4983 ret = io_fadvise_prep(req, sqe);
4984 if (ret)
4985 break;
4986 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004987 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07004988 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004989 case IORING_OP_MADVISE:
4990 if (sqe) {
4991 ret = io_madvise_prep(req, sqe);
4992 if (ret)
4993 break;
4994 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004995 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07004996 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004997 case IORING_OP_OPENAT2:
4998 if (sqe) {
4999 ret = io_openat2_prep(req, sqe);
5000 if (ret)
5001 break;
5002 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005003 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005004 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005005 case IORING_OP_EPOLL_CTL:
5006 if (sqe) {
5007 ret = io_epoll_ctl_prep(req, sqe);
5008 if (ret)
5009 break;
5010 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005011 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005012 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005013 case IORING_OP_SPLICE:
5014 if (sqe) {
5015 ret = io_splice_prep(req, sqe);
5016 if (ret < 0)
5017 break;
5018 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005019 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005020 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005021 case IORING_OP_PROVIDE_BUFFERS:
5022 if (sqe) {
5023 ret = io_provide_buffers_prep(req, sqe);
5024 if (ret)
5025 break;
5026 }
5027 ret = io_provide_buffers(req, force_nonblock);
5028 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005029 default:
5030 ret = -EINVAL;
5031 break;
5032 }
5033
Jens Axboedef596e2019-01-09 08:59:42 -07005034 if (ret)
5035 return ret;
5036
5037 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005038 const bool in_async = io_wq_current_is_worker();
5039
Jens Axboe9e645e112019-05-10 16:07:28 -06005040 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07005041 return -EAGAIN;
5042
Jens Axboe11ba8202020-01-15 21:51:17 -07005043 /* workqueue context doesn't hold uring_lock, grab it now */
5044 if (in_async)
5045 mutex_lock(&ctx->uring_lock);
5046
Jens Axboedef596e2019-01-09 08:59:42 -07005047 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005048
5049 if (in_async)
5050 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005051 }
5052
5053 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005054}
5055
Jens Axboe561fb042019-10-24 07:25:42 -06005056static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07005057{
Jens Axboe561fb042019-10-24 07:25:42 -06005058 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005059 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005060 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005061
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005062 /* if NO_CANCEL is set, we must still run the work */
5063 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5064 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005065 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005066 }
Jens Axboe31b51512019-01-18 22:56:34 -07005067
Jens Axboe561fb042019-10-24 07:25:42 -06005068 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005069 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005070 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005071 /*
5072 * We can get EAGAIN for polled IO even though we're
5073 * forcing a sync submission from here, since we can't
5074 * wait for request slots on the block side.
5075 */
5076 if (ret != -EAGAIN)
5077 break;
5078 cond_resched();
5079 } while (1);
5080 }
Jens Axboe31b51512019-01-18 22:56:34 -07005081
Jens Axboe561fb042019-10-24 07:25:42 -06005082 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005083 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005084 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005085 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005086 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005087
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005088 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005089}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005090
Jens Axboe15b71ab2019-12-11 11:20:36 -07005091static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005092{
Jens Axboed3656342019-12-18 09:50:26 -07005093 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005094 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07005095 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07005096 return 0;
5097 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06005098}
5099
Jens Axboe65e19f52019-10-26 07:20:21 -06005100static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5101 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005102{
Jens Axboe65e19f52019-10-26 07:20:21 -06005103 struct fixed_file_table *table;
5104
Jens Axboe05f3fb32019-12-09 11:22:50 -07005105 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5106 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06005107}
5108
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005109static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5110 int fd, struct file **out_file, bool fixed)
5111{
5112 struct io_ring_ctx *ctx = req->ctx;
5113 struct file *file;
5114
5115 if (fixed) {
5116 if (unlikely(!ctx->file_data ||
5117 (unsigned) fd >= ctx->nr_user_files))
5118 return -EBADF;
5119 fd = array_index_nospec(fd, ctx->nr_user_files);
5120 file = io_file_from_index(ctx, fd);
5121 if (!file)
5122 return -EBADF;
5123 percpu_ref_get(&ctx->file_data->refs);
5124 } else {
5125 trace_io_uring_file_get(ctx, fd);
5126 file = __io_file_get(state, fd);
5127 if (unlikely(!file))
5128 return -EBADF;
5129 }
5130
5131 *out_file = file;
5132 return 0;
5133}
5134
Jens Axboe3529d8c2019-12-19 18:24:38 -07005135static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
5136 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06005137{
5138 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07005139 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005140 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005141
Jens Axboe3529d8c2019-12-19 18:24:38 -07005142 flags = READ_ONCE(sqe->flags);
5143 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06005144
Jens Axboed3656342019-12-18 09:50:26 -07005145 if (!io_req_needs_file(req, fd))
5146 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06005147
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005148 fixed = (flags & IOSQE_FIXED_FILE);
5149 if (unlikely(!fixed && req->needs_fixed_file))
5150 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005151
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005152 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005153}
5154
Jackie Liua197f662019-11-08 08:09:12 -07005155static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005156{
Jens Axboefcb323c2019-10-24 12:39:47 -06005157 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005158 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005159
Jens Axboef86cd202020-01-29 13:46:44 -07005160 if (req->work.files)
5161 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005162 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005163 return -EBADF;
5164
Jens Axboefcb323c2019-10-24 12:39:47 -06005165 rcu_read_lock();
5166 spin_lock_irq(&ctx->inflight_lock);
5167 /*
5168 * We use the f_ops->flush() handler to ensure that we can flush
5169 * out work accessing these files if the fd is closed. Check if
5170 * the fd has changed since we started down this path, and disallow
5171 * this operation if it has.
5172 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005173 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005174 list_add(&req->inflight_entry, &ctx->inflight_list);
5175 req->flags |= REQ_F_INFLIGHT;
5176 req->work.files = current->files;
5177 ret = 0;
5178 }
5179 spin_unlock_irq(&ctx->inflight_lock);
5180 rcu_read_unlock();
5181
5182 return ret;
5183}
5184
Jens Axboe2665abf2019-11-05 12:40:47 -07005185static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5186{
Jens Axboead8a48a2019-11-15 08:49:11 -07005187 struct io_timeout_data *data = container_of(timer,
5188 struct io_timeout_data, timer);
5189 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005190 struct io_ring_ctx *ctx = req->ctx;
5191 struct io_kiocb *prev = NULL;
5192 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005193
5194 spin_lock_irqsave(&ctx->completion_lock, flags);
5195
5196 /*
5197 * We don't expect the list to be empty, that will only happen if we
5198 * race with the completion of the linked work.
5199 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005200 if (!list_empty(&req->link_list)) {
5201 prev = list_entry(req->link_list.prev, struct io_kiocb,
5202 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005203 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005204 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005205 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5206 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005207 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005208 }
5209
5210 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5211
5212 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005213 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005214 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005215 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005216 } else {
5217 io_cqring_add_event(req, -ETIME);
5218 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005219 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005220 return HRTIMER_NORESTART;
5221}
5222
Jens Axboead8a48a2019-11-15 08:49:11 -07005223static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005224{
Jens Axboe76a46e02019-11-10 23:34:16 -07005225 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005226
Jens Axboe76a46e02019-11-10 23:34:16 -07005227 /*
5228 * If the list is now empty, then our linked request finished before
5229 * we got a chance to setup the timer
5230 */
5231 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005232 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005233 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005234
Jens Axboead8a48a2019-11-15 08:49:11 -07005235 data->timer.function = io_link_timeout_fn;
5236 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5237 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005238 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005239 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005240
Jens Axboe2665abf2019-11-05 12:40:47 -07005241 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005242 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005243}
5244
Jens Axboead8a48a2019-11-15 08:49:11 -07005245static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005246{
5247 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005248
Jens Axboe2665abf2019-11-05 12:40:47 -07005249 if (!(req->flags & REQ_F_LINK))
5250 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005251 /* for polled retry, if flag is set, we already went through here */
5252 if (req->flags & REQ_F_POLLED)
5253 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005254
Pavel Begunkov44932332019-12-05 16:16:35 +03005255 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5256 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005257 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005258 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005259
Jens Axboe76a46e02019-11-10 23:34:16 -07005260 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005261 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005262}
5263
Jens Axboe3529d8c2019-12-19 18:24:38 -07005264static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005265{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005266 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005267 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005268 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005269 int ret;
5270
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005271again:
5272 linked_timeout = io_prep_linked_timeout(req);
5273
Jens Axboe193155c2020-02-22 23:22:19 -07005274 if (req->work.creds && req->work.creds != current_cred()) {
5275 if (old_creds)
5276 revert_creds(old_creds);
5277 if (old_creds == req->work.creds)
5278 old_creds = NULL; /* restored original creds */
5279 else
5280 old_creds = override_creds(req->work.creds);
5281 }
5282
Pavel Begunkov014db002020-03-03 21:33:12 +03005283 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005284
5285 /*
5286 * We async punt it if the file wasn't marked NOWAIT, or if the file
5287 * doesn't support non-blocking read/write attempts
5288 */
5289 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5290 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005291 if (io_arm_poll_handler(req)) {
5292 if (linked_timeout)
5293 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005294 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005295 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005296punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005297 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005298 ret = io_grab_files(req);
5299 if (ret)
5300 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005301 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005302
5303 /*
5304 * Queued up for async execution, worker will release
5305 * submit reference when the iocb is actually submitted.
5306 */
5307 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005308 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005309 }
Jens Axboee65ef562019-03-12 10:16:44 -06005310
Jens Axboefcb323c2019-10-24 12:39:47 -06005311err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005312 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005313 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005314 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005315
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005316 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005317 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005318 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005319 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005320 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005321 }
5322
Jens Axboee65ef562019-03-12 10:16:44 -06005323 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005324 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005325 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005326 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005327 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005328 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005329 if (nxt) {
5330 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005331
5332 if (req->flags & REQ_F_FORCE_ASYNC)
5333 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005334 goto again;
5335 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005336exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005337 if (old_creds)
5338 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005339}
5340
Jens Axboe3529d8c2019-12-19 18:24:38 -07005341static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005342{
5343 int ret;
5344
Jens Axboe3529d8c2019-12-19 18:24:38 -07005345 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005346 if (ret) {
5347 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005348fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005349 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005350 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005351 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005352 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005353 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005354 ret = io_req_defer_prep(req, sqe);
5355 if (unlikely(ret < 0))
5356 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005357 /*
5358 * Never try inline submit of IOSQE_ASYNC is set, go straight
5359 * to async execution.
5360 */
5361 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5362 io_queue_async_work(req);
5363 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005364 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005365 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005366}
5367
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005368static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005369{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005370 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005371 io_cqring_add_event(req, -ECANCELED);
5372 io_double_put_req(req);
5373 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005374 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005375}
5376
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005377#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboebcda7ba2020-02-23 16:42:51 -07005378 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5379 IOSQE_BUFFER_SELECT)
Jens Axboe9e645e112019-05-10 16:07:28 -06005380
Jens Axboe3529d8c2019-12-19 18:24:38 -07005381static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5382 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005383{
Jackie Liua197f662019-11-08 08:09:12 -07005384 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005385 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005386 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005387
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005388 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005389
5390 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005391 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005392 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005393 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005394 }
5395
Jens Axboebcda7ba2020-02-23 16:42:51 -07005396 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5397 !io_op_defs[req->opcode].buffer_select) {
5398 ret = -EOPNOTSUPP;
5399 goto err_req;
5400 }
5401
Jens Axboe75c6a032020-01-28 10:15:23 -07005402 id = READ_ONCE(sqe->personality);
5403 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005404 req->work.creds = idr_find(&ctx->personality_idr, id);
5405 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005406 ret = -EINVAL;
5407 goto err_req;
5408 }
Jens Axboe193155c2020-02-22 23:22:19 -07005409 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005410 }
5411
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005412 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005413 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
Jens Axboebcda7ba2020-02-23 16:42:51 -07005414 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5415 IOSQE_BUFFER_SELECT);
Jens Axboe9e645e112019-05-10 16:07:28 -06005416
Jens Axboe3529d8c2019-12-19 18:24:38 -07005417 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005418 if (unlikely(ret)) {
5419err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005420 io_cqring_add_event(req, ret);
5421 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005422 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005423 }
5424
Jens Axboe9e645e112019-05-10 16:07:28 -06005425 /*
5426 * If we already have a head request, queue this one for async
5427 * submittal once the head completes. If we don't have a head but
5428 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5429 * submitted sync once the chain is complete. If none of those
5430 * conditions are true (normal request), then just queue it.
5431 */
5432 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005433 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005434
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005435 /*
5436 * Taking sequential execution of a link, draining both sides
5437 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5438 * requests in the link. So, it drains the head and the
5439 * next after the link request. The last one is done via
5440 * drain_next flag to persist the effect across calls.
5441 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005442 if (sqe_flags & IOSQE_IO_DRAIN) {
5443 head->flags |= REQ_F_IO_DRAIN;
5444 ctx->drain_next = 1;
5445 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005446 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005447 ret = -EAGAIN;
5448 goto err_req;
5449 }
5450
Jens Axboe3529d8c2019-12-19 18:24:38 -07005451 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005452 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005453 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005454 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005455 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005456 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005457 trace_io_uring_link(ctx, req, head);
5458 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005459
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005460 /* last request of a link, enqueue the link */
5461 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5462 io_queue_link_head(head);
5463 *link = NULL;
5464 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005465 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005466 if (unlikely(ctx->drain_next)) {
5467 req->flags |= REQ_F_IO_DRAIN;
5468 req->ctx->drain_next = 0;
5469 }
5470 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5471 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005472 INIT_LIST_HEAD(&req->link_list);
5473 ret = io_req_defer_prep(req, sqe);
5474 if (ret)
5475 req->flags |= REQ_F_FAIL_LINK;
5476 *link = req;
5477 } else {
5478 io_queue_sqe(req, sqe);
5479 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005480 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005481
5482 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005483}
5484
Jens Axboe9a56a232019-01-09 09:06:50 -07005485/*
5486 * Batched submission is done, ensure local IO is flushed out.
5487 */
5488static void io_submit_state_end(struct io_submit_state *state)
5489{
5490 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005491 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005492 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005493 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005494}
5495
5496/*
5497 * Start submission side cache.
5498 */
5499static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005500 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005501{
5502 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005503 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005504 state->file = NULL;
5505 state->ios_left = max_ios;
5506}
5507
Jens Axboe2b188cc2019-01-07 10:46:33 -07005508static void io_commit_sqring(struct io_ring_ctx *ctx)
5509{
Hristo Venev75b28af2019-08-26 17:23:46 +00005510 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005511
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005512 /*
5513 * Ensure any loads from the SQEs are done at this point,
5514 * since once we write the new head, the application could
5515 * write new data to them.
5516 */
5517 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005518}
5519
5520/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005521 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005522 * that is mapped by userspace. This means that care needs to be taken to
5523 * ensure that reads are stable, as we cannot rely on userspace always
5524 * being a good citizen. If members of the sqe are validated and then later
5525 * used, it's important that those reads are done through READ_ONCE() to
5526 * prevent a re-load down the line.
5527 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005528static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5529 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005530{
Hristo Venev75b28af2019-08-26 17:23:46 +00005531 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005532 unsigned head;
5533
5534 /*
5535 * The cached sq head (or cq tail) serves two purposes:
5536 *
5537 * 1) allows us to batch the cost of updating the user visible
5538 * head updates.
5539 * 2) allows the kernel side to track the head on its own, even
5540 * though the application is the one updating it.
5541 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005542 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005543 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005544 /*
5545 * All io need record the previous position, if LINK vs DARIN,
5546 * it can be used to mark the position of the first IO in the
5547 * link list.
5548 */
5549 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005550 *sqe_ptr = &ctx->sq_sqes[head];
5551 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5552 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005553 ctx->cached_sq_head++;
5554 return true;
5555 }
5556
5557 /* drop invalid entries */
5558 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005559 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005560 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005561 return false;
5562}
5563
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005564static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005565 struct file *ring_file, int ring_fd,
5566 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005567{
5568 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005569 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005570 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005571 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005572
Jens Axboec4a2ed72019-11-21 21:01:26 -07005573 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005574 if (test_bit(0, &ctx->sq_check_overflow)) {
5575 if (!list_empty(&ctx->cq_overflow_list) &&
5576 !io_cqring_overflow_flush(ctx, false))
5577 return -EBUSY;
5578 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005579
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005580 /* make sure SQ entry isn't read before tail */
5581 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005582
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005583 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5584 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005585
5586 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005587 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005588 statep = &state;
5589 }
5590
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005591 ctx->ring_fd = ring_fd;
5592 ctx->ring_file = ring_file;
5593
Jens Axboe6c271ce2019-01-10 11:22:30 -07005594 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005595 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005596 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005597 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005598
Pavel Begunkov196be952019-11-07 01:41:06 +03005599 req = io_get_req(ctx, statep);
5600 if (unlikely(!req)) {
5601 if (!submitted)
5602 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005603 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005604 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005605 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005606 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005607 break;
5608 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005609
Jens Axboed3656342019-12-18 09:50:26 -07005610 /* will complete beyond this point, count as submitted */
5611 submitted++;
5612
5613 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005614 err = -EINVAL;
5615fail_req:
5616 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005617 io_double_put_req(req);
5618 break;
5619 }
5620
5621 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005622 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005623 if (unlikely(mm_fault)) {
5624 err = -EFAULT;
5625 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005626 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005627 use_mm(ctx->sqo_mm);
5628 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005629 }
5630
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005631 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005632 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5633 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005634 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005635 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005636 }
5637
Pavel Begunkov9466f432020-01-25 22:34:01 +03005638 if (unlikely(submitted != nr)) {
5639 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5640
5641 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5642 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005643 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005644 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005645 if (statep)
5646 io_submit_state_end(&state);
5647
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005648 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5649 io_commit_sqring(ctx);
5650
Jens Axboe6c271ce2019-01-10 11:22:30 -07005651 return submitted;
5652}
5653
5654static int io_sq_thread(void *data)
5655{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005656 struct io_ring_ctx *ctx = data;
5657 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005658 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005659 mm_segment_t old_fs;
5660 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005661 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005662 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005663
Jens Axboe206aefd2019-11-07 18:27:42 -07005664 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005665
Jens Axboe6c271ce2019-01-10 11:22:30 -07005666 old_fs = get_fs();
5667 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005668 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005669
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005670 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005671 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005672 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005673
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005674 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005675 unsigned nr_events = 0;
5676
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005677 mutex_lock(&ctx->uring_lock);
5678 if (!list_empty(&ctx->poll_list))
5679 io_iopoll_getevents(ctx, &nr_events, 0);
5680 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005681 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005682 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005683 }
5684
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005685 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005686
5687 /*
5688 * If submit got -EBUSY, flag us as needing the application
5689 * to enter the kernel to reap and flush events.
5690 */
5691 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005692 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005693 * Drop cur_mm before scheduling, we can't hold it for
5694 * long periods (or over schedule()). Do this before
5695 * adding ourselves to the waitqueue, as the unuse/drop
5696 * may sleep.
5697 */
5698 if (cur_mm) {
5699 unuse_mm(cur_mm);
5700 mmput(cur_mm);
5701 cur_mm = NULL;
5702 }
5703
5704 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005705 * We're polling. If we're within the defined idle
5706 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005707 * to sleep. The exception is if we got EBUSY doing
5708 * more IO, we should wait for the application to
5709 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005710 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005711 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005712 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5713 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005714 if (current->task_works)
5715 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005716 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005717 continue;
5718 }
5719
Jens Axboe6c271ce2019-01-10 11:22:30 -07005720 prepare_to_wait(&ctx->sqo_wait, &wait,
5721 TASK_INTERRUPTIBLE);
5722
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005723 /*
5724 * While doing polled IO, before going to sleep, we need
5725 * to check if there are new reqs added to poll_list, it
5726 * is because reqs may have been punted to io worker and
5727 * will be added to poll_list later, hence check the
5728 * poll_list again.
5729 */
5730 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5731 !list_empty_careful(&ctx->poll_list)) {
5732 finish_wait(&ctx->sqo_wait, &wait);
5733 continue;
5734 }
5735
Jens Axboe6c271ce2019-01-10 11:22:30 -07005736 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005737 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005738 /* make sure to read SQ tail after writing flags */
5739 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005740
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005741 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005742 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005743 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005744 finish_wait(&ctx->sqo_wait, &wait);
5745 break;
5746 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005747 if (current->task_works) {
5748 task_work_run();
5749 continue;
5750 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005751 if (signal_pending(current))
5752 flush_signals(current);
5753 schedule();
5754 finish_wait(&ctx->sqo_wait, &wait);
5755
Hristo Venev75b28af2019-08-26 17:23:46 +00005756 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005757 continue;
5758 }
5759 finish_wait(&ctx->sqo_wait, &wait);
5760
Hristo Venev75b28af2019-08-26 17:23:46 +00005761 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005762 }
5763
Jens Axboe8a4955f2019-12-09 14:52:35 -07005764 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005765 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005766 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005767 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005768 }
5769
Jens Axboeb41e9852020-02-17 09:52:41 -07005770 if (current->task_works)
5771 task_work_run();
5772
Jens Axboe6c271ce2019-01-10 11:22:30 -07005773 set_fs(old_fs);
5774 if (cur_mm) {
5775 unuse_mm(cur_mm);
5776 mmput(cur_mm);
5777 }
Jens Axboe181e4482019-11-25 08:52:30 -07005778 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005779
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005780 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005781
Jens Axboe6c271ce2019-01-10 11:22:30 -07005782 return 0;
5783}
5784
Jens Axboebda52162019-09-24 13:47:15 -06005785struct io_wait_queue {
5786 struct wait_queue_entry wq;
5787 struct io_ring_ctx *ctx;
5788 unsigned to_wait;
5789 unsigned nr_timeouts;
5790};
5791
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005792static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005793{
5794 struct io_ring_ctx *ctx = iowq->ctx;
5795
5796 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005797 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005798 * started waiting. For timeouts, we always want to return to userspace,
5799 * regardless of event count.
5800 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005801 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005802 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5803}
5804
5805static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5806 int wake_flags, void *key)
5807{
5808 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5809 wq);
5810
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005811 /* use noflush == true, as we can't safely rely on locking context */
5812 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005813 return -1;
5814
5815 return autoremove_wake_function(curr, mode, wake_flags, key);
5816}
5817
Jens Axboe2b188cc2019-01-07 10:46:33 -07005818/*
5819 * Wait until events become available, if we don't already have some. The
5820 * application must reap them itself, as they reside on the shared cq ring.
5821 */
5822static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5823 const sigset_t __user *sig, size_t sigsz)
5824{
Jens Axboebda52162019-09-24 13:47:15 -06005825 struct io_wait_queue iowq = {
5826 .wq = {
5827 .private = current,
5828 .func = io_wake_function,
5829 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5830 },
5831 .ctx = ctx,
5832 .to_wait = min_events,
5833 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005834 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005835 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005836
Jens Axboeb41e9852020-02-17 09:52:41 -07005837 do {
5838 if (io_cqring_events(ctx, false) >= min_events)
5839 return 0;
5840 if (!current->task_works)
5841 break;
5842 task_work_run();
5843 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005844
5845 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005846#ifdef CONFIG_COMPAT
5847 if (in_compat_syscall())
5848 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005849 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005850 else
5851#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005852 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005853
Jens Axboe2b188cc2019-01-07 10:46:33 -07005854 if (ret)
5855 return ret;
5856 }
5857
Jens Axboebda52162019-09-24 13:47:15 -06005858 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005859 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005860 do {
5861 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5862 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005863 if (current->task_works)
5864 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005865 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005866 break;
5867 schedule();
5868 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005869 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005870 break;
5871 }
5872 } while (1);
5873 finish_wait(&ctx->wait, &iowq.wq);
5874
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005875 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005876
Hristo Venev75b28af2019-08-26 17:23:46 +00005877 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005878}
5879
Jens Axboe6b063142019-01-10 22:13:58 -07005880static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5881{
5882#if defined(CONFIG_UNIX)
5883 if (ctx->ring_sock) {
5884 struct sock *sock = ctx->ring_sock->sk;
5885 struct sk_buff *skb;
5886
5887 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5888 kfree_skb(skb);
5889 }
5890#else
5891 int i;
5892
Jens Axboe65e19f52019-10-26 07:20:21 -06005893 for (i = 0; i < ctx->nr_user_files; i++) {
5894 struct file *file;
5895
5896 file = io_file_from_index(ctx, i);
5897 if (file)
5898 fput(file);
5899 }
Jens Axboe6b063142019-01-10 22:13:58 -07005900#endif
5901}
5902
Jens Axboe05f3fb32019-12-09 11:22:50 -07005903static void io_file_ref_kill(struct percpu_ref *ref)
5904{
5905 struct fixed_file_data *data;
5906
5907 data = container_of(ref, struct fixed_file_data, refs);
5908 complete(&data->done);
5909}
5910
Jens Axboe6b063142019-01-10 22:13:58 -07005911static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5912{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005913 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005914 unsigned nr_tables, i;
5915
Jens Axboe05f3fb32019-12-09 11:22:50 -07005916 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005917 return -ENXIO;
5918
Jens Axboe05f3fb32019-12-09 11:22:50 -07005919 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005920 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005921 wait_for_completion(&data->done);
5922 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005923 percpu_ref_exit(&data->refs);
5924
Jens Axboe6b063142019-01-10 22:13:58 -07005925 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005926 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5927 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005928 kfree(data->table[i].files);
5929 kfree(data->table);
5930 kfree(data);
5931 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005932 ctx->nr_user_files = 0;
5933 return 0;
5934}
5935
Jens Axboe6c271ce2019-01-10 11:22:30 -07005936static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5937{
5938 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005939 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005940 /*
5941 * The park is a bit of a work-around, without it we get
5942 * warning spews on shutdown with SQPOLL set and affinity
5943 * set to a single CPU.
5944 */
Jens Axboe06058632019-04-13 09:26:03 -06005945 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005946 kthread_stop(ctx->sqo_thread);
5947 ctx->sqo_thread = NULL;
5948 }
5949}
5950
Jens Axboe6b063142019-01-10 22:13:58 -07005951static void io_finish_async(struct io_ring_ctx *ctx)
5952{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005953 io_sq_thread_stop(ctx);
5954
Jens Axboe561fb042019-10-24 07:25:42 -06005955 if (ctx->io_wq) {
5956 io_wq_destroy(ctx->io_wq);
5957 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005958 }
5959}
5960
5961#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005962/*
5963 * Ensure the UNIX gc is aware of our file set, so we are certain that
5964 * the io_uring can be safely unregistered on process exit, even if we have
5965 * loops in the file referencing.
5966 */
5967static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5968{
5969 struct sock *sk = ctx->ring_sock->sk;
5970 struct scm_fp_list *fpl;
5971 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005972 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005973
5974 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5975 unsigned long inflight = ctx->user->unix_inflight + nr;
5976
5977 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5978 return -EMFILE;
5979 }
5980
5981 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5982 if (!fpl)
5983 return -ENOMEM;
5984
5985 skb = alloc_skb(0, GFP_KERNEL);
5986 if (!skb) {
5987 kfree(fpl);
5988 return -ENOMEM;
5989 }
5990
5991 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005992
Jens Axboe08a45172019-10-03 08:11:03 -06005993 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005994 fpl->user = get_uid(ctx->user);
5995 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005996 struct file *file = io_file_from_index(ctx, i + offset);
5997
5998 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005999 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006000 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006001 unix_inflight(fpl->user, fpl->fp[nr_files]);
6002 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006003 }
6004
Jens Axboe08a45172019-10-03 08:11:03 -06006005 if (nr_files) {
6006 fpl->max = SCM_MAX_FD;
6007 fpl->count = nr_files;
6008 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006009 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006010 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6011 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006012
Jens Axboe08a45172019-10-03 08:11:03 -06006013 for (i = 0; i < nr_files; i++)
6014 fput(fpl->fp[i]);
6015 } else {
6016 kfree_skb(skb);
6017 kfree(fpl);
6018 }
Jens Axboe6b063142019-01-10 22:13:58 -07006019
6020 return 0;
6021}
6022
6023/*
6024 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6025 * causes regular reference counting to break down. We rely on the UNIX
6026 * garbage collection to take care of this problem for us.
6027 */
6028static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6029{
6030 unsigned left, total;
6031 int ret = 0;
6032
6033 total = 0;
6034 left = ctx->nr_user_files;
6035 while (left) {
6036 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006037
6038 ret = __io_sqe_files_scm(ctx, this_files, total);
6039 if (ret)
6040 break;
6041 left -= this_files;
6042 total += this_files;
6043 }
6044
6045 if (!ret)
6046 return 0;
6047
6048 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006049 struct file *file = io_file_from_index(ctx, total);
6050
6051 if (file)
6052 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006053 total++;
6054 }
6055
6056 return ret;
6057}
6058#else
6059static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6060{
6061 return 0;
6062}
6063#endif
6064
Jens Axboe65e19f52019-10-26 07:20:21 -06006065static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6066 unsigned nr_files)
6067{
6068 int i;
6069
6070 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006071 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006072 unsigned this_files;
6073
6074 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6075 table->files = kcalloc(this_files, sizeof(struct file *),
6076 GFP_KERNEL);
6077 if (!table->files)
6078 break;
6079 nr_files -= this_files;
6080 }
6081
6082 if (i == nr_tables)
6083 return 0;
6084
6085 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006086 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006087 kfree(table->files);
6088 }
6089 return 1;
6090}
6091
Jens Axboe05f3fb32019-12-09 11:22:50 -07006092static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006093{
6094#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006095 struct sock *sock = ctx->ring_sock->sk;
6096 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6097 struct sk_buff *skb;
6098 int i;
6099
6100 __skb_queue_head_init(&list);
6101
6102 /*
6103 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6104 * remove this entry and rearrange the file array.
6105 */
6106 skb = skb_dequeue(head);
6107 while (skb) {
6108 struct scm_fp_list *fp;
6109
6110 fp = UNIXCB(skb).fp;
6111 for (i = 0; i < fp->count; i++) {
6112 int left;
6113
6114 if (fp->fp[i] != file)
6115 continue;
6116
6117 unix_notinflight(fp->user, fp->fp[i]);
6118 left = fp->count - 1 - i;
6119 if (left) {
6120 memmove(&fp->fp[i], &fp->fp[i + 1],
6121 left * sizeof(struct file *));
6122 }
6123 fp->count--;
6124 if (!fp->count) {
6125 kfree_skb(skb);
6126 skb = NULL;
6127 } else {
6128 __skb_queue_tail(&list, skb);
6129 }
6130 fput(file);
6131 file = NULL;
6132 break;
6133 }
6134
6135 if (!file)
6136 break;
6137
6138 __skb_queue_tail(&list, skb);
6139
6140 skb = skb_dequeue(head);
6141 }
6142
6143 if (skb_peek(&list)) {
6144 spin_lock_irq(&head->lock);
6145 while ((skb = __skb_dequeue(&list)) != NULL)
6146 __skb_queue_tail(head, skb);
6147 spin_unlock_irq(&head->lock);
6148 }
6149#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006150 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006151#endif
6152}
6153
Jens Axboe05f3fb32019-12-09 11:22:50 -07006154struct io_file_put {
6155 struct llist_node llist;
6156 struct file *file;
6157 struct completion *done;
6158};
6159
Jens Axboe2faf8522020-02-04 19:54:55 -07006160static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006161{
6162 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006163 struct llist_node *node;
6164
Jens Axboe05f3fb32019-12-09 11:22:50 -07006165 while ((node = llist_del_all(&data->put_llist)) != NULL) {
6166 llist_for_each_entry_safe(pfile, tmp, node, llist) {
6167 io_ring_file_put(data->ctx, pfile->file);
6168 if (pfile->done)
6169 complete(pfile->done);
6170 else
6171 kfree(pfile);
6172 }
6173 }
Jens Axboe2faf8522020-02-04 19:54:55 -07006174}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006175
Jens Axboe2faf8522020-02-04 19:54:55 -07006176static void io_ring_file_ref_switch(struct work_struct *work)
6177{
6178 struct fixed_file_data *data;
6179
6180 data = container_of(work, struct fixed_file_data, ref_work);
6181 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006182 percpu_ref_switch_to_percpu(&data->refs);
6183}
6184
6185static void io_file_data_ref_zero(struct percpu_ref *ref)
6186{
6187 struct fixed_file_data *data;
6188
6189 data = container_of(ref, struct fixed_file_data, refs);
6190
Jens Axboe2faf8522020-02-04 19:54:55 -07006191 /*
6192 * We can't safely switch from inside this context, punt to wq. If
6193 * the table ref is going away, the table is being unregistered.
6194 * Don't queue up the async work for that case, the caller will
6195 * handle it.
6196 */
6197 if (!percpu_ref_is_dying(&data->refs))
6198 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006199}
6200
6201static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6202 unsigned nr_args)
6203{
6204 __s32 __user *fds = (__s32 __user *) arg;
6205 unsigned nr_tables;
6206 struct file *file;
6207 int fd, ret = 0;
6208 unsigned i;
6209
6210 if (ctx->file_data)
6211 return -EBUSY;
6212 if (!nr_args)
6213 return -EINVAL;
6214 if (nr_args > IORING_MAX_FIXED_FILES)
6215 return -EMFILE;
6216
6217 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6218 if (!ctx->file_data)
6219 return -ENOMEM;
6220 ctx->file_data->ctx = ctx;
6221 init_completion(&ctx->file_data->done);
6222
6223 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6224 ctx->file_data->table = kcalloc(nr_tables,
6225 sizeof(struct fixed_file_table),
6226 GFP_KERNEL);
6227 if (!ctx->file_data->table) {
6228 kfree(ctx->file_data);
6229 ctx->file_data = NULL;
6230 return -ENOMEM;
6231 }
6232
6233 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
6234 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6235 kfree(ctx->file_data->table);
6236 kfree(ctx->file_data);
6237 ctx->file_data = NULL;
6238 return -ENOMEM;
6239 }
6240 ctx->file_data->put_llist.first = NULL;
6241 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
6242
6243 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6244 percpu_ref_exit(&ctx->file_data->refs);
6245 kfree(ctx->file_data->table);
6246 kfree(ctx->file_data);
6247 ctx->file_data = NULL;
6248 return -ENOMEM;
6249 }
6250
6251 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6252 struct fixed_file_table *table;
6253 unsigned index;
6254
6255 ret = -EFAULT;
6256 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6257 break;
6258 /* allow sparse sets */
6259 if (fd == -1) {
6260 ret = 0;
6261 continue;
6262 }
6263
6264 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6265 index = i & IORING_FILE_TABLE_MASK;
6266 file = fget(fd);
6267
6268 ret = -EBADF;
6269 if (!file)
6270 break;
6271
6272 /*
6273 * Don't allow io_uring instances to be registered. If UNIX
6274 * isn't enabled, then this causes a reference cycle and this
6275 * instance can never get freed. If UNIX is enabled we'll
6276 * handle it just fine, but there's still no point in allowing
6277 * a ring fd as it doesn't support regular read/write anyway.
6278 */
6279 if (file->f_op == &io_uring_fops) {
6280 fput(file);
6281 break;
6282 }
6283 ret = 0;
6284 table->files[index] = file;
6285 }
6286
6287 if (ret) {
6288 for (i = 0; i < ctx->nr_user_files; i++) {
6289 file = io_file_from_index(ctx, i);
6290 if (file)
6291 fput(file);
6292 }
6293 for (i = 0; i < nr_tables; i++)
6294 kfree(ctx->file_data->table[i].files);
6295
6296 kfree(ctx->file_data->table);
6297 kfree(ctx->file_data);
6298 ctx->file_data = NULL;
6299 ctx->nr_user_files = 0;
6300 return ret;
6301 }
6302
6303 ret = io_sqe_files_scm(ctx);
6304 if (ret)
6305 io_sqe_files_unregister(ctx);
6306
6307 return ret;
6308}
6309
Jens Axboec3a31e62019-10-03 13:59:56 -06006310static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6311 int index)
6312{
6313#if defined(CONFIG_UNIX)
6314 struct sock *sock = ctx->ring_sock->sk;
6315 struct sk_buff_head *head = &sock->sk_receive_queue;
6316 struct sk_buff *skb;
6317
6318 /*
6319 * See if we can merge this file into an existing skb SCM_RIGHTS
6320 * file set. If there's no room, fall back to allocating a new skb
6321 * and filling it in.
6322 */
6323 spin_lock_irq(&head->lock);
6324 skb = skb_peek(head);
6325 if (skb) {
6326 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6327
6328 if (fpl->count < SCM_MAX_FD) {
6329 __skb_unlink(skb, head);
6330 spin_unlock_irq(&head->lock);
6331 fpl->fp[fpl->count] = get_file(file);
6332 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6333 fpl->count++;
6334 spin_lock_irq(&head->lock);
6335 __skb_queue_head(head, skb);
6336 } else {
6337 skb = NULL;
6338 }
6339 }
6340 spin_unlock_irq(&head->lock);
6341
6342 if (skb) {
6343 fput(file);
6344 return 0;
6345 }
6346
6347 return __io_sqe_files_scm(ctx, 1, index);
6348#else
6349 return 0;
6350#endif
6351}
6352
Jens Axboe05f3fb32019-12-09 11:22:50 -07006353static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06006354{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006355 struct fixed_file_data *data;
6356
Jens Axboedd3db2a2020-02-26 10:23:43 -07006357 /*
6358 * Juggle reference to ensure we hit zero, if needed, so we can
6359 * switch back to percpu mode
6360 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006361 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07006362 percpu_ref_put(&data->refs);
6363 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006364}
6365
6366static bool io_queue_file_removal(struct fixed_file_data *data,
6367 struct file *file)
6368{
6369 struct io_file_put *pfile, pfile_stack;
6370 DECLARE_COMPLETION_ONSTACK(done);
6371
6372 /*
6373 * If we fail allocating the struct we need for doing async reomval
6374 * of this file, just punt to sync and wait for it.
6375 */
6376 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
6377 if (!pfile) {
6378 pfile = &pfile_stack;
6379 pfile->done = &done;
6380 }
6381
6382 pfile->file = file;
6383 llist_add(&pfile->llist, &data->put_llist);
6384
6385 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07006386 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006387 wait_for_completion(&done);
6388 flush_work(&data->ref_work);
6389 return false;
6390 }
6391
6392 return true;
6393}
6394
6395static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6396 struct io_uring_files_update *up,
6397 unsigned nr_args)
6398{
6399 struct fixed_file_data *data = ctx->file_data;
6400 bool ref_switch = false;
6401 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006402 __s32 __user *fds;
6403 int fd, i, err;
6404 __u32 done;
6405
Jens Axboe05f3fb32019-12-09 11:22:50 -07006406 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006407 return -EOVERFLOW;
6408 if (done > ctx->nr_user_files)
6409 return -EINVAL;
6410
6411 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006412 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006413 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006414 struct fixed_file_table *table;
6415 unsigned index;
6416
Jens Axboec3a31e62019-10-03 13:59:56 -06006417 err = 0;
6418 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6419 err = -EFAULT;
6420 break;
6421 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006422 i = array_index_nospec(up->offset, ctx->nr_user_files);
6423 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006424 index = i & IORING_FILE_TABLE_MASK;
6425 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006426 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006427 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006428 if (io_queue_file_removal(data, file))
6429 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006430 }
6431 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006432 file = fget(fd);
6433 if (!file) {
6434 err = -EBADF;
6435 break;
6436 }
6437 /*
6438 * Don't allow io_uring instances to be registered. If
6439 * UNIX isn't enabled, then this causes a reference
6440 * cycle and this instance can never get freed. If UNIX
6441 * is enabled we'll handle it just fine, but there's
6442 * still no point in allowing a ring fd as it doesn't
6443 * support regular read/write anyway.
6444 */
6445 if (file->f_op == &io_uring_fops) {
6446 fput(file);
6447 err = -EBADF;
6448 break;
6449 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006450 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006451 err = io_sqe_file_register(ctx, file, i);
6452 if (err)
6453 break;
6454 }
6455 nr_args--;
6456 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006457 up->offset++;
6458 }
6459
Jens Axboedd3db2a2020-02-26 10:23:43 -07006460 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006461 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006462
6463 return done ? done : err;
6464}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006465static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6466 unsigned nr_args)
6467{
6468 struct io_uring_files_update up;
6469
6470 if (!ctx->file_data)
6471 return -ENXIO;
6472 if (!nr_args)
6473 return -EINVAL;
6474 if (copy_from_user(&up, arg, sizeof(up)))
6475 return -EFAULT;
6476 if (up.resv)
6477 return -EINVAL;
6478
6479 return __io_sqe_files_update(ctx, &up, nr_args);
6480}
Jens Axboec3a31e62019-10-03 13:59:56 -06006481
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006482static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006483{
6484 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6485
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006486 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006487 io_put_req(req);
6488}
6489
Pavel Begunkov24369c22020-01-28 03:15:48 +03006490static int io_init_wq_offload(struct io_ring_ctx *ctx,
6491 struct io_uring_params *p)
6492{
6493 struct io_wq_data data;
6494 struct fd f;
6495 struct io_ring_ctx *ctx_attach;
6496 unsigned int concurrency;
6497 int ret = 0;
6498
6499 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006500 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006501
6502 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6503 /* Do QD, or 4 * CPUS, whatever is smallest */
6504 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6505
6506 ctx->io_wq = io_wq_create(concurrency, &data);
6507 if (IS_ERR(ctx->io_wq)) {
6508 ret = PTR_ERR(ctx->io_wq);
6509 ctx->io_wq = NULL;
6510 }
6511 return ret;
6512 }
6513
6514 f = fdget(p->wq_fd);
6515 if (!f.file)
6516 return -EBADF;
6517
6518 if (f.file->f_op != &io_uring_fops) {
6519 ret = -EINVAL;
6520 goto out_fput;
6521 }
6522
6523 ctx_attach = f.file->private_data;
6524 /* @io_wq is protected by holding the fd */
6525 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6526 ret = -EINVAL;
6527 goto out_fput;
6528 }
6529
6530 ctx->io_wq = ctx_attach->io_wq;
6531out_fput:
6532 fdput(f);
6533 return ret;
6534}
6535
Jens Axboe6c271ce2019-01-10 11:22:30 -07006536static int io_sq_offload_start(struct io_ring_ctx *ctx,
6537 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006538{
6539 int ret;
6540
Jens Axboe6c271ce2019-01-10 11:22:30 -07006541 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006542 mmgrab(current->mm);
6543 ctx->sqo_mm = current->mm;
6544
Jens Axboe6c271ce2019-01-10 11:22:30 -07006545 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006546 ret = -EPERM;
6547 if (!capable(CAP_SYS_ADMIN))
6548 goto err;
6549
Jens Axboe917257d2019-04-13 09:28:55 -06006550 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6551 if (!ctx->sq_thread_idle)
6552 ctx->sq_thread_idle = HZ;
6553
Jens Axboe6c271ce2019-01-10 11:22:30 -07006554 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006555 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006556
Jens Axboe917257d2019-04-13 09:28:55 -06006557 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006558 if (cpu >= nr_cpu_ids)
6559 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006560 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006561 goto err;
6562
Jens Axboe6c271ce2019-01-10 11:22:30 -07006563 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6564 ctx, cpu,
6565 "io_uring-sq");
6566 } else {
6567 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6568 "io_uring-sq");
6569 }
6570 if (IS_ERR(ctx->sqo_thread)) {
6571 ret = PTR_ERR(ctx->sqo_thread);
6572 ctx->sqo_thread = NULL;
6573 goto err;
6574 }
6575 wake_up_process(ctx->sqo_thread);
6576 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6577 /* Can't have SQ_AFF without SQPOLL */
6578 ret = -EINVAL;
6579 goto err;
6580 }
6581
Pavel Begunkov24369c22020-01-28 03:15:48 +03006582 ret = io_init_wq_offload(ctx, p);
6583 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006584 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006585
6586 return 0;
6587err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006588 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006589 mmdrop(ctx->sqo_mm);
6590 ctx->sqo_mm = NULL;
6591 return ret;
6592}
6593
6594static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6595{
6596 atomic_long_sub(nr_pages, &user->locked_vm);
6597}
6598
6599static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6600{
6601 unsigned long page_limit, cur_pages, new_pages;
6602
6603 /* Don't allow more pages than we can safely lock */
6604 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6605
6606 do {
6607 cur_pages = atomic_long_read(&user->locked_vm);
6608 new_pages = cur_pages + nr_pages;
6609 if (new_pages > page_limit)
6610 return -ENOMEM;
6611 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6612 new_pages) != cur_pages);
6613
6614 return 0;
6615}
6616
6617static void io_mem_free(void *ptr)
6618{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006619 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006620
Mark Rutland52e04ef2019-04-30 17:30:21 +01006621 if (!ptr)
6622 return;
6623
6624 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006625 if (put_page_testzero(page))
6626 free_compound_page(page);
6627}
6628
6629static void *io_mem_alloc(size_t size)
6630{
6631 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6632 __GFP_NORETRY;
6633
6634 return (void *) __get_free_pages(gfp_flags, get_order(size));
6635}
6636
Hristo Venev75b28af2019-08-26 17:23:46 +00006637static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6638 size_t *sq_offset)
6639{
6640 struct io_rings *rings;
6641 size_t off, sq_array_size;
6642
6643 off = struct_size(rings, cqes, cq_entries);
6644 if (off == SIZE_MAX)
6645 return SIZE_MAX;
6646
6647#ifdef CONFIG_SMP
6648 off = ALIGN(off, SMP_CACHE_BYTES);
6649 if (off == 0)
6650 return SIZE_MAX;
6651#endif
6652
6653 sq_array_size = array_size(sizeof(u32), sq_entries);
6654 if (sq_array_size == SIZE_MAX)
6655 return SIZE_MAX;
6656
6657 if (check_add_overflow(off, sq_array_size, &off))
6658 return SIZE_MAX;
6659
6660 if (sq_offset)
6661 *sq_offset = off;
6662
6663 return off;
6664}
6665
Jens Axboe2b188cc2019-01-07 10:46:33 -07006666static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6667{
Hristo Venev75b28af2019-08-26 17:23:46 +00006668 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006669
Hristo Venev75b28af2019-08-26 17:23:46 +00006670 pages = (size_t)1 << get_order(
6671 rings_size(sq_entries, cq_entries, NULL));
6672 pages += (size_t)1 << get_order(
6673 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006674
Hristo Venev75b28af2019-08-26 17:23:46 +00006675 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006676}
6677
Jens Axboeedafcce2019-01-09 09:16:05 -07006678static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6679{
6680 int i, j;
6681
6682 if (!ctx->user_bufs)
6683 return -ENXIO;
6684
6685 for (i = 0; i < ctx->nr_user_bufs; i++) {
6686 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6687
6688 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006689 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006690
6691 if (ctx->account_mem)
6692 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006693 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006694 imu->nr_bvecs = 0;
6695 }
6696
6697 kfree(ctx->user_bufs);
6698 ctx->user_bufs = NULL;
6699 ctx->nr_user_bufs = 0;
6700 return 0;
6701}
6702
6703static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6704 void __user *arg, unsigned index)
6705{
6706 struct iovec __user *src;
6707
6708#ifdef CONFIG_COMPAT
6709 if (ctx->compat) {
6710 struct compat_iovec __user *ciovs;
6711 struct compat_iovec ciov;
6712
6713 ciovs = (struct compat_iovec __user *) arg;
6714 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6715 return -EFAULT;
6716
Jens Axboed55e5f52019-12-11 16:12:15 -07006717 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006718 dst->iov_len = ciov.iov_len;
6719 return 0;
6720 }
6721#endif
6722 src = (struct iovec __user *) arg;
6723 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6724 return -EFAULT;
6725 return 0;
6726}
6727
6728static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6729 unsigned nr_args)
6730{
6731 struct vm_area_struct **vmas = NULL;
6732 struct page **pages = NULL;
6733 int i, j, got_pages = 0;
6734 int ret = -EINVAL;
6735
6736 if (ctx->user_bufs)
6737 return -EBUSY;
6738 if (!nr_args || nr_args > UIO_MAXIOV)
6739 return -EINVAL;
6740
6741 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6742 GFP_KERNEL);
6743 if (!ctx->user_bufs)
6744 return -ENOMEM;
6745
6746 for (i = 0; i < nr_args; i++) {
6747 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6748 unsigned long off, start, end, ubuf;
6749 int pret, nr_pages;
6750 struct iovec iov;
6751 size_t size;
6752
6753 ret = io_copy_iov(ctx, &iov, arg, i);
6754 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006755 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006756
6757 /*
6758 * Don't impose further limits on the size and buffer
6759 * constraints here, we'll -EINVAL later when IO is
6760 * submitted if they are wrong.
6761 */
6762 ret = -EFAULT;
6763 if (!iov.iov_base || !iov.iov_len)
6764 goto err;
6765
6766 /* arbitrary limit, but we need something */
6767 if (iov.iov_len > SZ_1G)
6768 goto err;
6769
6770 ubuf = (unsigned long) iov.iov_base;
6771 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6772 start = ubuf >> PAGE_SHIFT;
6773 nr_pages = end - start;
6774
6775 if (ctx->account_mem) {
6776 ret = io_account_mem(ctx->user, nr_pages);
6777 if (ret)
6778 goto err;
6779 }
6780
6781 ret = 0;
6782 if (!pages || nr_pages > got_pages) {
6783 kfree(vmas);
6784 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006785 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006786 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006787 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006788 sizeof(struct vm_area_struct *),
6789 GFP_KERNEL);
6790 if (!pages || !vmas) {
6791 ret = -ENOMEM;
6792 if (ctx->account_mem)
6793 io_unaccount_mem(ctx->user, nr_pages);
6794 goto err;
6795 }
6796 got_pages = nr_pages;
6797 }
6798
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006799 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006800 GFP_KERNEL);
6801 ret = -ENOMEM;
6802 if (!imu->bvec) {
6803 if (ctx->account_mem)
6804 io_unaccount_mem(ctx->user, nr_pages);
6805 goto err;
6806 }
6807
6808 ret = 0;
6809 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006810 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006811 FOLL_WRITE | FOLL_LONGTERM,
6812 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006813 if (pret == nr_pages) {
6814 /* don't support file backed memory */
6815 for (j = 0; j < nr_pages; j++) {
6816 struct vm_area_struct *vma = vmas[j];
6817
6818 if (vma->vm_file &&
6819 !is_file_hugepages(vma->vm_file)) {
6820 ret = -EOPNOTSUPP;
6821 break;
6822 }
6823 }
6824 } else {
6825 ret = pret < 0 ? pret : -EFAULT;
6826 }
6827 up_read(&current->mm->mmap_sem);
6828 if (ret) {
6829 /*
6830 * if we did partial map, or found file backed vmas,
6831 * release any pages we did get
6832 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006833 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006834 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006835 if (ctx->account_mem)
6836 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006837 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006838 goto err;
6839 }
6840
6841 off = ubuf & ~PAGE_MASK;
6842 size = iov.iov_len;
6843 for (j = 0; j < nr_pages; j++) {
6844 size_t vec_len;
6845
6846 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6847 imu->bvec[j].bv_page = pages[j];
6848 imu->bvec[j].bv_len = vec_len;
6849 imu->bvec[j].bv_offset = off;
6850 off = 0;
6851 size -= vec_len;
6852 }
6853 /* store original address for later verification */
6854 imu->ubuf = ubuf;
6855 imu->len = iov.iov_len;
6856 imu->nr_bvecs = nr_pages;
6857
6858 ctx->nr_user_bufs++;
6859 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006860 kvfree(pages);
6861 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006862 return 0;
6863err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006864 kvfree(pages);
6865 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006866 io_sqe_buffer_unregister(ctx);
6867 return ret;
6868}
6869
Jens Axboe9b402842019-04-11 11:45:41 -06006870static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6871{
6872 __s32 __user *fds = arg;
6873 int fd;
6874
6875 if (ctx->cq_ev_fd)
6876 return -EBUSY;
6877
6878 if (copy_from_user(&fd, fds, sizeof(*fds)))
6879 return -EFAULT;
6880
6881 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6882 if (IS_ERR(ctx->cq_ev_fd)) {
6883 int ret = PTR_ERR(ctx->cq_ev_fd);
6884 ctx->cq_ev_fd = NULL;
6885 return ret;
6886 }
6887
6888 return 0;
6889}
6890
6891static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6892{
6893 if (ctx->cq_ev_fd) {
6894 eventfd_ctx_put(ctx->cq_ev_fd);
6895 ctx->cq_ev_fd = NULL;
6896 return 0;
6897 }
6898
6899 return -ENXIO;
6900}
6901
Jens Axboe5a2e7452020-02-23 16:23:11 -07006902static int __io_destroy_buffers(int id, void *p, void *data)
6903{
6904 struct io_ring_ctx *ctx = data;
6905 struct io_buffer *buf = p;
6906
6907 /* the head kbuf is the list itself */
6908 while (!list_empty(&buf->list)) {
6909 struct io_buffer *nxt;
6910
6911 nxt = list_first_entry(&buf->list, struct io_buffer, list);
6912 list_del(&nxt->list);
6913 kfree(nxt);
6914 }
6915 kfree(buf);
6916 idr_remove(&ctx->io_buffer_idr, id);
6917 return 0;
6918}
6919
6920static void io_destroy_buffers(struct io_ring_ctx *ctx)
6921{
6922 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
6923 idr_destroy(&ctx->io_buffer_idr);
6924}
6925
Jens Axboe2b188cc2019-01-07 10:46:33 -07006926static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6927{
Jens Axboe6b063142019-01-10 22:13:58 -07006928 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006929 if (ctx->sqo_mm)
6930 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006931
6932 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006933 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006934 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006935 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07006936 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006937 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006938
Jens Axboe2b188cc2019-01-07 10:46:33 -07006939#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006940 if (ctx->ring_sock) {
6941 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006942 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006943 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006944#endif
6945
Hristo Venev75b28af2019-08-26 17:23:46 +00006946 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006947 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006948
6949 percpu_ref_exit(&ctx->refs);
6950 if (ctx->account_mem)
6951 io_unaccount_mem(ctx->user,
6952 ring_pages(ctx->sq_entries, ctx->cq_entries));
6953 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006954 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006955 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006956 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006957 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006958 kfree(ctx);
6959}
6960
6961static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6962{
6963 struct io_ring_ctx *ctx = file->private_data;
6964 __poll_t mask = 0;
6965
6966 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006967 /*
6968 * synchronizes with barrier from wq_has_sleeper call in
6969 * io_commit_cqring
6970 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006971 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006972 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6973 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006974 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006975 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006976 mask |= EPOLLIN | EPOLLRDNORM;
6977
6978 return mask;
6979}
6980
6981static int io_uring_fasync(int fd, struct file *file, int on)
6982{
6983 struct io_ring_ctx *ctx = file->private_data;
6984
6985 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6986}
6987
Jens Axboe071698e2020-01-28 10:04:42 -07006988static int io_remove_personalities(int id, void *p, void *data)
6989{
6990 struct io_ring_ctx *ctx = data;
6991 const struct cred *cred;
6992
6993 cred = idr_remove(&ctx->personality_idr, id);
6994 if (cred)
6995 put_cred(cred);
6996 return 0;
6997}
6998
Jens Axboe2b188cc2019-01-07 10:46:33 -07006999static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7000{
7001 mutex_lock(&ctx->uring_lock);
7002 percpu_ref_kill(&ctx->refs);
7003 mutex_unlock(&ctx->uring_lock);
7004
Jens Axboedf069d82020-02-04 16:48:34 -07007005 /*
7006 * Wait for sq thread to idle, if we have one. It won't spin on new
7007 * work after we've killed the ctx ref above. This is important to do
7008 * before we cancel existing commands, as the thread could otherwise
7009 * be queueing new work post that. If that's work we need to cancel,
7010 * it could cause shutdown to hang.
7011 */
7012 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
7013 cpu_relax();
7014
Jens Axboe5262f562019-09-17 12:26:57 -06007015 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007016 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007017
7018 if (ctx->io_wq)
7019 io_wq_cancel_all(ctx->io_wq);
7020
Jens Axboedef596e2019-01-09 08:59:42 -07007021 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007022 /* if we failed setting up the ctx, we might not have any rings */
7023 if (ctx->rings)
7024 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007025 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07007026 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007027 io_ring_ctx_free(ctx);
7028}
7029
7030static int io_uring_release(struct inode *inode, struct file *file)
7031{
7032 struct io_ring_ctx *ctx = file->private_data;
7033
7034 file->private_data = NULL;
7035 io_ring_ctx_wait_and_kill(ctx);
7036 return 0;
7037}
7038
Jens Axboefcb323c2019-10-24 12:39:47 -06007039static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7040 struct files_struct *files)
7041{
7042 struct io_kiocb *req;
7043 DEFINE_WAIT(wait);
7044
7045 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07007046 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06007047
7048 spin_lock_irq(&ctx->inflight_lock);
7049 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007050 if (req->work.files != files)
7051 continue;
7052 /* req is being completed, ignore */
7053 if (!refcount_inc_not_zero(&req->refs))
7054 continue;
7055 cancel_req = req;
7056 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007057 }
Jens Axboe768134d2019-11-10 20:30:53 -07007058 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007059 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007060 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007061 spin_unlock_irq(&ctx->inflight_lock);
7062
Jens Axboe768134d2019-11-10 20:30:53 -07007063 /* We need to keep going until we don't find a matching req */
7064 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007065 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007066
Jens Axboe2ca10252020-02-13 17:17:35 -07007067 if (cancel_req->flags & REQ_F_OVERFLOW) {
7068 spin_lock_irq(&ctx->completion_lock);
7069 list_del(&cancel_req->list);
7070 cancel_req->flags &= ~REQ_F_OVERFLOW;
7071 if (list_empty(&ctx->cq_overflow_list)) {
7072 clear_bit(0, &ctx->sq_check_overflow);
7073 clear_bit(0, &ctx->cq_check_overflow);
7074 }
7075 spin_unlock_irq(&ctx->completion_lock);
7076
7077 WRITE_ONCE(ctx->rings->cq_overflow,
7078 atomic_inc_return(&ctx->cached_cq_overflow));
7079
7080 /*
7081 * Put inflight ref and overflow ref. If that's
7082 * all we had, then we're done with this request.
7083 */
7084 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7085 io_put_req(cancel_req);
7086 continue;
7087 }
7088 }
7089
Bob Liu2f6d9b92019-11-13 18:06:24 +08007090 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7091 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007092 schedule();
7093 }
Jens Axboe768134d2019-11-10 20:30:53 -07007094 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007095}
7096
7097static int io_uring_flush(struct file *file, void *data)
7098{
7099 struct io_ring_ctx *ctx = file->private_data;
7100
7101 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007102
7103 /*
7104 * If the task is going away, cancel work it may have pending
7105 */
7106 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7107 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7108
Jens Axboefcb323c2019-10-24 12:39:47 -06007109 return 0;
7110}
7111
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007112static void *io_uring_validate_mmap_request(struct file *file,
7113 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007114{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007115 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007116 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007117 struct page *page;
7118 void *ptr;
7119
7120 switch (offset) {
7121 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007122 case IORING_OFF_CQ_RING:
7123 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007124 break;
7125 case IORING_OFF_SQES:
7126 ptr = ctx->sq_sqes;
7127 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007128 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007129 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007130 }
7131
7132 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007133 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007134 return ERR_PTR(-EINVAL);
7135
7136 return ptr;
7137}
7138
7139#ifdef CONFIG_MMU
7140
7141static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7142{
7143 size_t sz = vma->vm_end - vma->vm_start;
7144 unsigned long pfn;
7145 void *ptr;
7146
7147 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7148 if (IS_ERR(ptr))
7149 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007150
7151 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7152 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7153}
7154
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007155#else /* !CONFIG_MMU */
7156
7157static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7158{
7159 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7160}
7161
7162static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7163{
7164 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7165}
7166
7167static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7168 unsigned long addr, unsigned long len,
7169 unsigned long pgoff, unsigned long flags)
7170{
7171 void *ptr;
7172
7173 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7174 if (IS_ERR(ptr))
7175 return PTR_ERR(ptr);
7176
7177 return (unsigned long) ptr;
7178}
7179
7180#endif /* !CONFIG_MMU */
7181
Jens Axboe2b188cc2019-01-07 10:46:33 -07007182SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7183 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7184 size_t, sigsz)
7185{
7186 struct io_ring_ctx *ctx;
7187 long ret = -EBADF;
7188 int submitted = 0;
7189 struct fd f;
7190
Jens Axboeb41e9852020-02-17 09:52:41 -07007191 if (current->task_works)
7192 task_work_run();
7193
Jens Axboe6c271ce2019-01-10 11:22:30 -07007194 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007195 return -EINVAL;
7196
7197 f = fdget(fd);
7198 if (!f.file)
7199 return -EBADF;
7200
7201 ret = -EOPNOTSUPP;
7202 if (f.file->f_op != &io_uring_fops)
7203 goto out_fput;
7204
7205 ret = -ENXIO;
7206 ctx = f.file->private_data;
7207 if (!percpu_ref_tryget(&ctx->refs))
7208 goto out_fput;
7209
Jens Axboe6c271ce2019-01-10 11:22:30 -07007210 /*
7211 * For SQ polling, the thread will do all submissions and completions.
7212 * Just return the requested submit count, and wake the thread if
7213 * we were asked to.
7214 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007215 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007216 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007217 if (!list_empty_careful(&ctx->cq_overflow_list))
7218 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007219 if (flags & IORING_ENTER_SQ_WAKEUP)
7220 wake_up(&ctx->sqo_wait);
7221 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007222 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007223 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007224
7225 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007226 /* already have mm, so io_submit_sqes() won't try to grab it */
7227 cur_mm = ctx->sqo_mm;
7228 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
7229 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007230 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007231
7232 if (submitted != to_submit)
7233 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007234 }
7235 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007236 unsigned nr_events = 0;
7237
Jens Axboe2b188cc2019-01-07 10:46:33 -07007238 min_complete = min(min_complete, ctx->cq_entries);
7239
Jens Axboedef596e2019-01-09 08:59:42 -07007240 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07007241 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007242 } else {
7243 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7244 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007245 }
7246
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007247out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007248 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007249out_fput:
7250 fdput(f);
7251 return submitted ? submitted : ret;
7252}
7253
Tobias Klauserbebdb652020-02-26 18:38:32 +01007254#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007255static int io_uring_show_cred(int id, void *p, void *data)
7256{
7257 const struct cred *cred = p;
7258 struct seq_file *m = data;
7259 struct user_namespace *uns = seq_user_ns(m);
7260 struct group_info *gi;
7261 kernel_cap_t cap;
7262 unsigned __capi;
7263 int g;
7264
7265 seq_printf(m, "%5d\n", id);
7266 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7267 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7268 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7269 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7270 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7271 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7272 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7273 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7274 seq_puts(m, "\n\tGroups:\t");
7275 gi = cred->group_info;
7276 for (g = 0; g < gi->ngroups; g++) {
7277 seq_put_decimal_ull(m, g ? " " : "",
7278 from_kgid_munged(uns, gi->gid[g]));
7279 }
7280 seq_puts(m, "\n\tCapEff:\t");
7281 cap = cred->cap_effective;
7282 CAP_FOR_EACH_U32(__capi)
7283 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7284 seq_putc(m, '\n');
7285 return 0;
7286}
7287
7288static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7289{
7290 int i;
7291
7292 mutex_lock(&ctx->uring_lock);
7293 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7294 for (i = 0; i < ctx->nr_user_files; i++) {
7295 struct fixed_file_table *table;
7296 struct file *f;
7297
7298 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7299 f = table->files[i & IORING_FILE_TABLE_MASK];
7300 if (f)
7301 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7302 else
7303 seq_printf(m, "%5u: <none>\n", i);
7304 }
7305 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7306 for (i = 0; i < ctx->nr_user_bufs; i++) {
7307 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7308
7309 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7310 (unsigned int) buf->len);
7311 }
7312 if (!idr_is_empty(&ctx->personality_idr)) {
7313 seq_printf(m, "Personalities:\n");
7314 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7315 }
Jens Axboed7718a92020-02-14 22:23:12 -07007316 seq_printf(m, "PollList:\n");
7317 spin_lock_irq(&ctx->completion_lock);
7318 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7319 struct hlist_head *list = &ctx->cancel_hash[i];
7320 struct io_kiocb *req;
7321
7322 hlist_for_each_entry(req, list, hash_node)
7323 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7324 req->task->task_works != NULL);
7325 }
7326 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007327 mutex_unlock(&ctx->uring_lock);
7328}
7329
7330static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7331{
7332 struct io_ring_ctx *ctx = f->private_data;
7333
7334 if (percpu_ref_tryget(&ctx->refs)) {
7335 __io_uring_show_fdinfo(ctx, m);
7336 percpu_ref_put(&ctx->refs);
7337 }
7338}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007339#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007340
Jens Axboe2b188cc2019-01-07 10:46:33 -07007341static const struct file_operations io_uring_fops = {
7342 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007343 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007344 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007345#ifndef CONFIG_MMU
7346 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7347 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7348#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007349 .poll = io_uring_poll,
7350 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007351#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007352 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007353#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007354};
7355
7356static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7357 struct io_uring_params *p)
7358{
Hristo Venev75b28af2019-08-26 17:23:46 +00007359 struct io_rings *rings;
7360 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007361
Hristo Venev75b28af2019-08-26 17:23:46 +00007362 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7363 if (size == SIZE_MAX)
7364 return -EOVERFLOW;
7365
7366 rings = io_mem_alloc(size);
7367 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007368 return -ENOMEM;
7369
Hristo Venev75b28af2019-08-26 17:23:46 +00007370 ctx->rings = rings;
7371 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7372 rings->sq_ring_mask = p->sq_entries - 1;
7373 rings->cq_ring_mask = p->cq_entries - 1;
7374 rings->sq_ring_entries = p->sq_entries;
7375 rings->cq_ring_entries = p->cq_entries;
7376 ctx->sq_mask = rings->sq_ring_mask;
7377 ctx->cq_mask = rings->cq_ring_mask;
7378 ctx->sq_entries = rings->sq_ring_entries;
7379 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007380
7381 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007382 if (size == SIZE_MAX) {
7383 io_mem_free(ctx->rings);
7384 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007385 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007386 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007387
7388 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007389 if (!ctx->sq_sqes) {
7390 io_mem_free(ctx->rings);
7391 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007392 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007393 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007394
Jens Axboe2b188cc2019-01-07 10:46:33 -07007395 return 0;
7396}
7397
7398/*
7399 * Allocate an anonymous fd, this is what constitutes the application
7400 * visible backing of an io_uring instance. The application mmaps this
7401 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7402 * we have to tie this fd to a socket for file garbage collection purposes.
7403 */
7404static int io_uring_get_fd(struct io_ring_ctx *ctx)
7405{
7406 struct file *file;
7407 int ret;
7408
7409#if defined(CONFIG_UNIX)
7410 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7411 &ctx->ring_sock);
7412 if (ret)
7413 return ret;
7414#endif
7415
7416 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7417 if (ret < 0)
7418 goto err;
7419
7420 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7421 O_RDWR | O_CLOEXEC);
7422 if (IS_ERR(file)) {
7423 put_unused_fd(ret);
7424 ret = PTR_ERR(file);
7425 goto err;
7426 }
7427
7428#if defined(CONFIG_UNIX)
7429 ctx->ring_sock->file = file;
7430#endif
7431 fd_install(ret, file);
7432 return ret;
7433err:
7434#if defined(CONFIG_UNIX)
7435 sock_release(ctx->ring_sock);
7436 ctx->ring_sock = NULL;
7437#endif
7438 return ret;
7439}
7440
7441static int io_uring_create(unsigned entries, struct io_uring_params *p)
7442{
7443 struct user_struct *user = NULL;
7444 struct io_ring_ctx *ctx;
7445 bool account_mem;
7446 int ret;
7447
Jens Axboe8110c1a2019-12-28 15:39:54 -07007448 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007449 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007450 if (entries > IORING_MAX_ENTRIES) {
7451 if (!(p->flags & IORING_SETUP_CLAMP))
7452 return -EINVAL;
7453 entries = IORING_MAX_ENTRIES;
7454 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007455
7456 /*
7457 * Use twice as many entries for the CQ ring. It's possible for the
7458 * application to drive a higher depth than the size of the SQ ring,
7459 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007460 * some flexibility in overcommitting a bit. If the application has
7461 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7462 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007463 */
7464 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007465 if (p->flags & IORING_SETUP_CQSIZE) {
7466 /*
7467 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7468 * to a power-of-two, if it isn't already. We do NOT impose
7469 * any cq vs sq ring sizing.
7470 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007471 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007472 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007473 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7474 if (!(p->flags & IORING_SETUP_CLAMP))
7475 return -EINVAL;
7476 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7477 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007478 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7479 } else {
7480 p->cq_entries = 2 * p->sq_entries;
7481 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007482
7483 user = get_uid(current_user());
7484 account_mem = !capable(CAP_IPC_LOCK);
7485
7486 if (account_mem) {
7487 ret = io_account_mem(user,
7488 ring_pages(p->sq_entries, p->cq_entries));
7489 if (ret) {
7490 free_uid(user);
7491 return ret;
7492 }
7493 }
7494
7495 ctx = io_ring_ctx_alloc(p);
7496 if (!ctx) {
7497 if (account_mem)
7498 io_unaccount_mem(user, ring_pages(p->sq_entries,
7499 p->cq_entries));
7500 free_uid(user);
7501 return -ENOMEM;
7502 }
7503 ctx->compat = in_compat_syscall();
7504 ctx->account_mem = account_mem;
7505 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007506 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007507
7508 ret = io_allocate_scq_urings(ctx, p);
7509 if (ret)
7510 goto err;
7511
Jens Axboe6c271ce2019-01-10 11:22:30 -07007512 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007513 if (ret)
7514 goto err;
7515
Jens Axboe2b188cc2019-01-07 10:46:33 -07007516 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007517 p->sq_off.head = offsetof(struct io_rings, sq.head);
7518 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7519 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7520 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7521 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7522 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7523 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007524
7525 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007526 p->cq_off.head = offsetof(struct io_rings, cq.head);
7527 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7528 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7529 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7530 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7531 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007532
Jens Axboe044c1ab2019-10-28 09:15:33 -06007533 /*
7534 * Install ring fd as the very last thing, so we don't risk someone
7535 * having closed it before we finish setup
7536 */
7537 ret = io_uring_get_fd(ctx);
7538 if (ret < 0)
7539 goto err;
7540
Jens Axboeda8c9692019-12-02 18:51:26 -07007541 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007542 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007543 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007544 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007545 return ret;
7546err:
7547 io_ring_ctx_wait_and_kill(ctx);
7548 return ret;
7549}
7550
7551/*
7552 * Sets up an aio uring context, and returns the fd. Applications asks for a
7553 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7554 * params structure passed in.
7555 */
7556static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7557{
7558 struct io_uring_params p;
7559 long ret;
7560 int i;
7561
7562 if (copy_from_user(&p, params, sizeof(p)))
7563 return -EFAULT;
7564 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7565 if (p.resv[i])
7566 return -EINVAL;
7567 }
7568
Jens Axboe6c271ce2019-01-10 11:22:30 -07007569 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007570 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007571 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007572 return -EINVAL;
7573
7574 ret = io_uring_create(entries, &p);
7575 if (ret < 0)
7576 return ret;
7577
7578 if (copy_to_user(params, &p, sizeof(p)))
7579 return -EFAULT;
7580
7581 return ret;
7582}
7583
7584SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7585 struct io_uring_params __user *, params)
7586{
7587 return io_uring_setup(entries, params);
7588}
7589
Jens Axboe66f4af92020-01-16 15:36:52 -07007590static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7591{
7592 struct io_uring_probe *p;
7593 size_t size;
7594 int i, ret;
7595
7596 size = struct_size(p, ops, nr_args);
7597 if (size == SIZE_MAX)
7598 return -EOVERFLOW;
7599 p = kzalloc(size, GFP_KERNEL);
7600 if (!p)
7601 return -ENOMEM;
7602
7603 ret = -EFAULT;
7604 if (copy_from_user(p, arg, size))
7605 goto out;
7606 ret = -EINVAL;
7607 if (memchr_inv(p, 0, size))
7608 goto out;
7609
7610 p->last_op = IORING_OP_LAST - 1;
7611 if (nr_args > IORING_OP_LAST)
7612 nr_args = IORING_OP_LAST;
7613
7614 for (i = 0; i < nr_args; i++) {
7615 p->ops[i].op = i;
7616 if (!io_op_defs[i].not_supported)
7617 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7618 }
7619 p->ops_len = i;
7620
7621 ret = 0;
7622 if (copy_to_user(arg, p, size))
7623 ret = -EFAULT;
7624out:
7625 kfree(p);
7626 return ret;
7627}
7628
Jens Axboe071698e2020-01-28 10:04:42 -07007629static int io_register_personality(struct io_ring_ctx *ctx)
7630{
7631 const struct cred *creds = get_current_cred();
7632 int id;
7633
7634 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7635 USHRT_MAX, GFP_KERNEL);
7636 if (id < 0)
7637 put_cred(creds);
7638 return id;
7639}
7640
7641static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7642{
7643 const struct cred *old_creds;
7644
7645 old_creds = idr_remove(&ctx->personality_idr, id);
7646 if (old_creds) {
7647 put_cred(old_creds);
7648 return 0;
7649 }
7650
7651 return -EINVAL;
7652}
7653
7654static bool io_register_op_must_quiesce(int op)
7655{
7656 switch (op) {
7657 case IORING_UNREGISTER_FILES:
7658 case IORING_REGISTER_FILES_UPDATE:
7659 case IORING_REGISTER_PROBE:
7660 case IORING_REGISTER_PERSONALITY:
7661 case IORING_UNREGISTER_PERSONALITY:
7662 return false;
7663 default:
7664 return true;
7665 }
7666}
7667
Jens Axboeedafcce2019-01-09 09:16:05 -07007668static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7669 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007670 __releases(ctx->uring_lock)
7671 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007672{
7673 int ret;
7674
Jens Axboe35fa71a2019-04-22 10:23:23 -06007675 /*
7676 * We're inside the ring mutex, if the ref is already dying, then
7677 * someone else killed the ctx or is already going through
7678 * io_uring_register().
7679 */
7680 if (percpu_ref_is_dying(&ctx->refs))
7681 return -ENXIO;
7682
Jens Axboe071698e2020-01-28 10:04:42 -07007683 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007684 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007685
Jens Axboe05f3fb32019-12-09 11:22:50 -07007686 /*
7687 * Drop uring mutex before waiting for references to exit. If
7688 * another thread is currently inside io_uring_enter() it might
7689 * need to grab the uring_lock to make progress. If we hold it
7690 * here across the drain wait, then we can deadlock. It's safe
7691 * to drop the mutex here, since no new references will come in
7692 * after we've killed the percpu ref.
7693 */
7694 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007695 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007696 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007697 if (ret) {
7698 percpu_ref_resurrect(&ctx->refs);
7699 ret = -EINTR;
7700 goto out;
7701 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007702 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007703
7704 switch (opcode) {
7705 case IORING_REGISTER_BUFFERS:
7706 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7707 break;
7708 case IORING_UNREGISTER_BUFFERS:
7709 ret = -EINVAL;
7710 if (arg || nr_args)
7711 break;
7712 ret = io_sqe_buffer_unregister(ctx);
7713 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007714 case IORING_REGISTER_FILES:
7715 ret = io_sqe_files_register(ctx, arg, nr_args);
7716 break;
7717 case IORING_UNREGISTER_FILES:
7718 ret = -EINVAL;
7719 if (arg || nr_args)
7720 break;
7721 ret = io_sqe_files_unregister(ctx);
7722 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007723 case IORING_REGISTER_FILES_UPDATE:
7724 ret = io_sqe_files_update(ctx, arg, nr_args);
7725 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007726 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007727 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007728 ret = -EINVAL;
7729 if (nr_args != 1)
7730 break;
7731 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007732 if (ret)
7733 break;
7734 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7735 ctx->eventfd_async = 1;
7736 else
7737 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007738 break;
7739 case IORING_UNREGISTER_EVENTFD:
7740 ret = -EINVAL;
7741 if (arg || nr_args)
7742 break;
7743 ret = io_eventfd_unregister(ctx);
7744 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007745 case IORING_REGISTER_PROBE:
7746 ret = -EINVAL;
7747 if (!arg || nr_args > 256)
7748 break;
7749 ret = io_probe(ctx, arg, nr_args);
7750 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007751 case IORING_REGISTER_PERSONALITY:
7752 ret = -EINVAL;
7753 if (arg || nr_args)
7754 break;
7755 ret = io_register_personality(ctx);
7756 break;
7757 case IORING_UNREGISTER_PERSONALITY:
7758 ret = -EINVAL;
7759 if (arg)
7760 break;
7761 ret = io_unregister_personality(ctx, nr_args);
7762 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007763 default:
7764 ret = -EINVAL;
7765 break;
7766 }
7767
Jens Axboe071698e2020-01-28 10:04:42 -07007768 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007769 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007770 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007771out:
7772 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007773 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007774 return ret;
7775}
7776
7777SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7778 void __user *, arg, unsigned int, nr_args)
7779{
7780 struct io_ring_ctx *ctx;
7781 long ret = -EBADF;
7782 struct fd f;
7783
7784 f = fdget(fd);
7785 if (!f.file)
7786 return -EBADF;
7787
7788 ret = -EOPNOTSUPP;
7789 if (f.file->f_op != &io_uring_fops)
7790 goto out_fput;
7791
7792 ctx = f.file->private_data;
7793
7794 mutex_lock(&ctx->uring_lock);
7795 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7796 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007797 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7798 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007799out_fput:
7800 fdput(f);
7801 return ret;
7802}
7803
Jens Axboe2b188cc2019-01-07 10:46:33 -07007804static int __init io_uring_init(void)
7805{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007806#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7807 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7808 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7809} while (0)
7810
7811#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7812 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7813 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7814 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7815 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7816 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7817 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7818 BUILD_BUG_SQE_ELEM(8, __u64, off);
7819 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7820 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007821 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007822 BUILD_BUG_SQE_ELEM(24, __u32, len);
7823 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7824 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7825 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7826 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7827 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7828 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7829 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7830 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7831 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7832 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7833 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7834 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7835 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007836 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007837 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7838 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7839 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007840 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007841
Jens Axboed3656342019-12-18 09:50:26 -07007842 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007843 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7844 return 0;
7845};
7846__initcall(io_uring_init);