blob: 36917c0101fdca32407d9349d9b504a1fefb45a3 [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>
Jens Axboe2b188cc2019-01-07 10:46:33 -070079
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020080#define CREATE_TRACE_POINTS
81#include <trace/events/io_uring.h>
82
Jens Axboe2b188cc2019-01-07 10:46:33 -070083#include <uapi/linux/io_uring.h>
84
85#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060086#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070087
Daniel Xu5277dea2019-09-14 14:23:45 -070088#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060089#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060090
91/*
92 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
93 */
94#define IORING_FILE_TABLE_SHIFT 9
95#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
96#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
97#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070098
99struct io_uring {
100 u32 head ____cacheline_aligned_in_smp;
101 u32 tail ____cacheline_aligned_in_smp;
102};
103
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000105 * This data is shared with the application through the mmap at offsets
106 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107 *
108 * The offsets to the member fields are published through struct
109 * io_sqring_offsets when calling io_uring_setup.
110 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000111struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 /*
113 * Head and tail offsets into the ring; the offsets need to be
114 * masked to get valid indices.
115 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * The kernel controls head of the sq ring and the tail of the cq ring,
117 * and the application controls tail of the sq ring and the head of the
118 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 * ring_entries - 1)
124 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 u32 sq_ring_mask, cq_ring_mask;
126 /* Ring sizes (constant, power of 2) */
127 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
129 * Number of invalid entries dropped by the kernel due to
130 * invalid index stored in array
131 *
132 * Written by the kernel, shouldn't be modified by the
133 * application (i.e. get number of "new events" by comparing to
134 * cached value).
135 *
136 * After a new SQ head value was read by the application this
137 * counter includes all submissions that were dropped reaching
138 * the new SQ head (and possibly more).
139 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200141 /*
142 * Runtime flags
143 *
144 * Written by the kernel, shouldn't be modified by the
145 * application.
146 *
147 * The application needs a full memory barrier before checking
148 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
149 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000150 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 /*
152 * Number of completion events lost because the queue was full;
153 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800154 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200155 * the completion queue.
156 *
157 * Written by the kernel, shouldn't be modified by the
158 * application (i.e. get number of "new events" by comparing to
159 * cached value).
160 *
161 * As completion events come in out of order this counter is not
162 * ordered with any other data.
163 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000164 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 /*
166 * Ring buffer of completion events.
167 *
168 * The kernel writes completion events fresh every time they are
169 * produced, so the application is allowed to modify pending
170 * entries.
171 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000172 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700173};
174
Jens Axboeedafcce2019-01-09 09:16:05 -0700175struct io_mapped_ubuf {
176 u64 ubuf;
177 size_t len;
178 struct bio_vec *bvec;
179 unsigned int nr_bvecs;
180};
181
Jens Axboe65e19f52019-10-26 07:20:21 -0600182struct fixed_file_table {
183 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700184};
185
Jens Axboe05f3fb32019-12-09 11:22:50 -0700186enum {
187 FFD_F_ATOMIC,
188};
189
190struct fixed_file_data {
191 struct fixed_file_table *table;
192 struct io_ring_ctx *ctx;
193
194 struct percpu_ref refs;
195 struct llist_head put_llist;
196 unsigned long state;
197 struct work_struct ref_work;
198 struct completion done;
199};
200
Jens Axboe2b188cc2019-01-07 10:46:33 -0700201struct io_ring_ctx {
202 struct {
203 struct percpu_ref refs;
204 } ____cacheline_aligned_in_smp;
205
206 struct {
207 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800208 unsigned int compat: 1;
209 unsigned int account_mem: 1;
210 unsigned int cq_overflow_flushed: 1;
211 unsigned int drain_next: 1;
212 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700213
Hristo Venev75b28af2019-08-26 17:23:46 +0000214 /*
215 * Ring buffer of indices into array of io_uring_sqe, which is
216 * mmapped by the application using the IORING_OFF_SQES offset.
217 *
218 * This indirection could e.g. be used to assign fixed
219 * io_uring_sqe entries to operations and only submit them to
220 * the queue when needed.
221 *
222 * The kernel modifies neither the indices array nor the entries
223 * array.
224 */
225 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700226 unsigned cached_sq_head;
227 unsigned sq_entries;
228 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700229 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600230 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700231 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700232 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600233
234 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600235 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700236 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237
Jens Axboefcb323c2019-10-24 12:39:47 -0600238 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700239 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700240 } ____cacheline_aligned_in_smp;
241
Hristo Venev75b28af2019-08-26 17:23:46 +0000242 struct io_rings *rings;
243
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600245 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246 struct task_struct *sqo_thread; /* if using sq thread polling */
247 struct mm_struct *sqo_mm;
248 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700249
Jens Axboe6b063142019-01-10 22:13:58 -0700250 /*
251 * If used, fixed file set. Writers must ensure that ->refs is dead,
252 * readers must ensure that ->refs is alive as long as the file* is
253 * used. Only updated through io_uring_register(2).
254 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700255 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700256 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300257 int ring_fd;
258 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700259
Jens Axboeedafcce2019-01-09 09:16:05 -0700260 /* if used, fixed mapped user buffers */
261 unsigned nr_user_bufs;
262 struct io_mapped_ubuf *user_bufs;
263
Jens Axboe2b188cc2019-01-07 10:46:33 -0700264 struct user_struct *user;
265
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700266 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700267
Jens Axboe206aefd2019-11-07 18:27:42 -0700268 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
269 struct completion *completions;
270
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700271 /* if all else fails... */
272 struct io_kiocb *fallback_req;
273
Jens Axboe206aefd2019-11-07 18:27:42 -0700274#if defined(CONFIG_UNIX)
275 struct socket *ring_sock;
276#endif
277
Jens Axboe071698e2020-01-28 10:04:42 -0700278 struct idr personality_idr;
279
Jens Axboe206aefd2019-11-07 18:27:42 -0700280 struct {
281 unsigned cached_cq_tail;
282 unsigned cq_entries;
283 unsigned cq_mask;
284 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700285 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700286 struct wait_queue_head cq_wait;
287 struct fasync_struct *cq_fasync;
288 struct eventfd_ctx *cq_ev_fd;
289 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290
291 struct {
292 struct mutex uring_lock;
293 wait_queue_head_t wait;
294 } ____cacheline_aligned_in_smp;
295
296 struct {
297 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700298 struct llist_head poll_llist;
299
Jens Axboedef596e2019-01-09 08:59:42 -0700300 /*
301 * ->poll_list is protected by the ctx->uring_lock for
302 * io_uring instances that don't use IORING_SETUP_SQPOLL.
303 * For SQPOLL, only the single threaded io_sq_thread() will
304 * manipulate the list, hence no extra locking is needed there.
305 */
306 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700307 struct hlist_head *cancel_hash;
308 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700309 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600310
311 spinlock_t inflight_lock;
312 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700314};
315
Jens Axboe09bb8392019-03-13 12:39:28 -0600316/*
317 * First field must be the file pointer in all the
318 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
319 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700320struct io_poll_iocb {
321 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700322 union {
323 struct wait_queue_head *head;
324 u64 addr;
325 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600327 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700328 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700329 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700330};
331
Jens Axboeb5dba592019-12-11 14:02:38 -0700332struct io_close {
333 struct file *file;
334 struct file *put_file;
335 int fd;
336};
337
Jens Axboead8a48a2019-11-15 08:49:11 -0700338struct io_timeout_data {
339 struct io_kiocb *req;
340 struct hrtimer timer;
341 struct timespec64 ts;
342 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300343 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700344};
345
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700346struct io_accept {
347 struct file *file;
348 struct sockaddr __user *addr;
349 int __user *addr_len;
350 int flags;
351};
352
353struct io_sync {
354 struct file *file;
355 loff_t len;
356 loff_t off;
357 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700358 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700359};
360
Jens Axboefbf23842019-12-17 18:45:56 -0700361struct io_cancel {
362 struct file *file;
363 u64 addr;
364};
365
Jens Axboeb29472e2019-12-17 18:50:29 -0700366struct io_timeout {
367 struct file *file;
368 u64 addr;
369 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700370 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700371};
372
Jens Axboe9adbd452019-12-20 08:45:55 -0700373struct io_rw {
374 /* NOTE: kiocb has the file as the first member, so don't do it here */
375 struct kiocb kiocb;
376 u64 addr;
377 u64 len;
378};
379
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700380struct io_connect {
381 struct file *file;
382 struct sockaddr __user *addr;
383 int addr_len;
384};
385
Jens Axboee47293f2019-12-20 08:58:21 -0700386struct io_sr_msg {
387 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700388 union {
389 struct user_msghdr __user *msg;
390 void __user *buf;
391 };
Jens Axboee47293f2019-12-20 08:58:21 -0700392 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700393 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700394};
395
Jens Axboe15b71ab2019-12-11 11:20:36 -0700396struct io_open {
397 struct file *file;
398 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700399 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700400 unsigned mask;
401 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700402 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700403 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700404 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700405};
406
Jens Axboe05f3fb32019-12-09 11:22:50 -0700407struct io_files_update {
408 struct file *file;
409 u64 arg;
410 u32 nr_args;
411 u32 offset;
412};
413
Jens Axboe4840e412019-12-25 22:03:45 -0700414struct io_fadvise {
415 struct file *file;
416 u64 offset;
417 u32 len;
418 u32 advice;
419};
420
Jens Axboec1ca7572019-12-25 22:18:28 -0700421struct io_madvise {
422 struct file *file;
423 u64 addr;
424 u32 len;
425 u32 advice;
426};
427
Jens Axboe3e4827b2020-01-08 15:18:09 -0700428struct io_epoll {
429 struct file *file;
430 int epfd;
431 int op;
432 int fd;
433 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700434};
435
Jens Axboef499a022019-12-02 16:28:46 -0700436struct io_async_connect {
437 struct sockaddr_storage address;
438};
439
Jens Axboe03b12302019-12-02 18:50:25 -0700440struct io_async_msghdr {
441 struct iovec fast_iov[UIO_FASTIOV];
442 struct iovec *iov;
443 struct sockaddr __user *uaddr;
444 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700445 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700446};
447
Jens Axboef67676d2019-12-02 11:03:47 -0700448struct io_async_rw {
449 struct iovec fast_iov[UIO_FASTIOV];
450 struct iovec *iov;
451 ssize_t nr_segs;
452 ssize_t size;
453};
454
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700455struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700456 union {
457 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700458 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700459 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700460 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700461 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700462};
463
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300464enum {
465 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
466 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
467 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
468 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
469 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
470
471 REQ_F_LINK_NEXT_BIT,
472 REQ_F_FAIL_LINK_BIT,
473 REQ_F_INFLIGHT_BIT,
474 REQ_F_CUR_POS_BIT,
475 REQ_F_NOWAIT_BIT,
476 REQ_F_IOPOLL_COMPLETED_BIT,
477 REQ_F_LINK_TIMEOUT_BIT,
478 REQ_F_TIMEOUT_BIT,
479 REQ_F_ISREG_BIT,
480 REQ_F_MUST_PUNT_BIT,
481 REQ_F_TIMEOUT_NOSEQ_BIT,
482 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300483 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700484 REQ_F_OVERFLOW_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300485};
486
487enum {
488 /* ctx owns file */
489 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
490 /* drain existing IO first */
491 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
492 /* linked sqes */
493 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
494 /* doesn't sever on completion < 0 */
495 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
496 /* IOSQE_ASYNC */
497 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
498
499 /* already grabbed next link */
500 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
501 /* fail rest of links */
502 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
503 /* on inflight list */
504 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
505 /* read/write uses file position */
506 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
507 /* must not punt to workers */
508 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
509 /* polled IO has completed */
510 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
511 /* has linked timeout */
512 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
513 /* timeout request */
514 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
515 /* regular file */
516 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
517 /* must be punted even for NONBLOCK */
518 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
519 /* no timeout sequence */
520 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
521 /* completion under lock */
522 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300523 /* needs cleanup */
524 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700525 /* in overflow list */
526 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300527};
528
Jens Axboe09bb8392019-03-13 12:39:28 -0600529/*
530 * NOTE! Each of the iocb union members has the file pointer
531 * as the first entry in their struct definition. So you can
532 * access the file pointer through any of the sub-structs,
533 * or directly as just 'ki_filp' in this struct.
534 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700535struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700536 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600537 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700538 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700539 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700540 struct io_accept accept;
541 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700542 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700543 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700544 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700545 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700546 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700547 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700548 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700549 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700550 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700551 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700552 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700553
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700554 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300555 /*
556 * llist_node is only used for poll deferred completions
557 */
558 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300559 bool in_async;
560 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700561 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700562
563 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700564 union {
565 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700566 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700567 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600568 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700569 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700570 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700571 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600572 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600573 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574
Jens Axboefcb323c2019-10-24 12:39:47 -0600575 struct list_head inflight_entry;
576
Jens Axboe561fb042019-10-24 07:25:42 -0600577 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700578};
579
580#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700581#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700582
Jens Axboe9a56a232019-01-09 09:06:50 -0700583struct io_submit_state {
584 struct blk_plug plug;
585
586 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700587 * io_kiocb alloc cache
588 */
589 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300590 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700591
592 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700593 * File reference cache
594 */
595 struct file *file;
596 unsigned int fd;
597 unsigned int has_refs;
598 unsigned int used_refs;
599 unsigned int ios_left;
600};
601
Jens Axboed3656342019-12-18 09:50:26 -0700602struct io_op_def {
603 /* needs req->io allocated for deferral/async */
604 unsigned async_ctx : 1;
605 /* needs current->mm setup, does mm access */
606 unsigned needs_mm : 1;
607 /* needs req->file assigned */
608 unsigned needs_file : 1;
609 /* needs req->file assigned IFF fd is >= 0 */
610 unsigned fd_non_neg : 1;
611 /* hash wq insertion if file is a regular file */
612 unsigned hash_reg_file : 1;
613 /* unbound wq insertion if file is a non-regular file */
614 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700615 /* opcode is not supported by this kernel */
616 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700617 /* needs file table */
618 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700619 /* needs ->fs */
620 unsigned needs_fs : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700621};
622
623static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300624 [IORING_OP_NOP] = {},
625 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700626 .async_ctx = 1,
627 .needs_mm = 1,
628 .needs_file = 1,
629 .unbound_nonreg_file = 1,
630 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300631 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700632 .async_ctx = 1,
633 .needs_mm = 1,
634 .needs_file = 1,
635 .hash_reg_file = 1,
636 .unbound_nonreg_file = 1,
637 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300638 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700639 .needs_file = 1,
640 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300641 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700642 .needs_file = 1,
643 .unbound_nonreg_file = 1,
644 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300645 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700646 .needs_file = 1,
647 .hash_reg_file = 1,
648 .unbound_nonreg_file = 1,
649 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300650 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700651 .needs_file = 1,
652 .unbound_nonreg_file = 1,
653 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300654 [IORING_OP_POLL_REMOVE] = {},
655 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700656 .needs_file = 1,
657 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300658 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700659 .async_ctx = 1,
660 .needs_mm = 1,
661 .needs_file = 1,
662 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700663 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700664 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300665 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700666 .async_ctx = 1,
667 .needs_mm = 1,
668 .needs_file = 1,
669 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700670 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700671 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300672 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700673 .async_ctx = 1,
674 .needs_mm = 1,
675 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300676 [IORING_OP_TIMEOUT_REMOVE] = {},
677 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700678 .needs_mm = 1,
679 .needs_file = 1,
680 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700681 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700682 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300683 [IORING_OP_ASYNC_CANCEL] = {},
684 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700685 .async_ctx = 1,
686 .needs_mm = 1,
687 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300688 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700689 .async_ctx = 1,
690 .needs_mm = 1,
691 .needs_file = 1,
692 .unbound_nonreg_file = 1,
693 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300694 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700695 .needs_file = 1,
696 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300697 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700698 .needs_file = 1,
699 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700700 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700701 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700702 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300703 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700704 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700705 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700706 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300707 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700708 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700709 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700710 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300711 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700712 .needs_mm = 1,
713 .needs_file = 1,
714 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700715 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700716 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300717 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700718 .needs_mm = 1,
719 .needs_file = 1,
720 .unbound_nonreg_file = 1,
721 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300722 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700723 .needs_mm = 1,
724 .needs_file = 1,
725 .unbound_nonreg_file = 1,
726 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300727 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700728 .needs_file = 1,
729 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300730 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700731 .needs_mm = 1,
732 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300733 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700734 .needs_mm = 1,
735 .needs_file = 1,
736 .unbound_nonreg_file = 1,
737 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700739 .needs_mm = 1,
740 .needs_file = 1,
741 .unbound_nonreg_file = 1,
742 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300743 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700744 .needs_file = 1,
745 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700746 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700747 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700748 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700749 [IORING_OP_EPOLL_CTL] = {
750 .unbound_nonreg_file = 1,
751 .file_table = 1,
752 },
Jens Axboed3656342019-12-18 09:50:26 -0700753};
754
Jens Axboe561fb042019-10-24 07:25:42 -0600755static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700756static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800757static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700758static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700759static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
760static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700761static int __io_sqe_files_update(struct io_ring_ctx *ctx,
762 struct io_uring_files_update *ip,
763 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700764static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700765static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300766static void io_cleanup_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600767
Jens Axboe2b188cc2019-01-07 10:46:33 -0700768static struct kmem_cache *req_cachep;
769
770static const struct file_operations io_uring_fops;
771
772struct sock *io_uring_get_socket(struct file *file)
773{
774#if defined(CONFIG_UNIX)
775 if (file->f_op == &io_uring_fops) {
776 struct io_ring_ctx *ctx = file->private_data;
777
778 return ctx->ring_sock->sk;
779 }
780#endif
781 return NULL;
782}
783EXPORT_SYMBOL(io_uring_get_socket);
784
785static void io_ring_ctx_ref_free(struct percpu_ref *ref)
786{
787 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
788
Jens Axboe206aefd2019-11-07 18:27:42 -0700789 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700790}
791
792static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
793{
794 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700795 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700796
797 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
798 if (!ctx)
799 return NULL;
800
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700801 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
802 if (!ctx->fallback_req)
803 goto err;
804
Jens Axboe206aefd2019-11-07 18:27:42 -0700805 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
806 if (!ctx->completions)
807 goto err;
808
Jens Axboe78076bb2019-12-04 19:56:40 -0700809 /*
810 * Use 5 bits less than the max cq entries, that should give us around
811 * 32 entries per hash list if totally full and uniformly spread.
812 */
813 hash_bits = ilog2(p->cq_entries);
814 hash_bits -= 5;
815 if (hash_bits <= 0)
816 hash_bits = 1;
817 ctx->cancel_hash_bits = hash_bits;
818 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
819 GFP_KERNEL);
820 if (!ctx->cancel_hash)
821 goto err;
822 __hash_init(ctx->cancel_hash, 1U << hash_bits);
823
Roman Gushchin21482892019-05-07 10:01:48 -0700824 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700825 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
826 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700827
828 ctx->flags = p->flags;
829 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700830 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700831 init_completion(&ctx->completions[0]);
832 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700833 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700834 mutex_init(&ctx->uring_lock);
835 init_waitqueue_head(&ctx->wait);
836 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700837 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700838 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600839 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600840 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600841 init_waitqueue_head(&ctx->inflight_wait);
842 spin_lock_init(&ctx->inflight_lock);
843 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700844 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700845err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700846 if (ctx->fallback_req)
847 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700848 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700849 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700850 kfree(ctx);
851 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700852}
853
Bob Liu9d858b22019-11-13 18:06:25 +0800854static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600855{
Jackie Liua197f662019-11-08 08:09:12 -0700856 struct io_ring_ctx *ctx = req->ctx;
857
Jens Axboe498ccd92019-10-25 10:04:25 -0600858 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
859 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600860}
861
Bob Liu9d858b22019-11-13 18:06:25 +0800862static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600863{
Pavel Begunkov87987892020-01-18 01:22:30 +0300864 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800865 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600866
Bob Liu9d858b22019-11-13 18:06:25 +0800867 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600868}
869
870static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600871{
872 struct io_kiocb *req;
873
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600874 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800875 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600876 list_del_init(&req->list);
877 return req;
878 }
879
880 return NULL;
881}
882
Jens Axboe5262f562019-09-17 12:26:57 -0600883static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
884{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600885 struct io_kiocb *req;
886
887 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700888 if (req) {
889 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
890 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800891 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700892 list_del_init(&req->list);
893 return req;
894 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600895 }
896
897 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600898}
899
Jens Axboede0617e2019-04-06 21:51:27 -0600900static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700901{
Hristo Venev75b28af2019-08-26 17:23:46 +0000902 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700903
Pavel Begunkov07910152020-01-17 03:52:46 +0300904 /* order cqe stores with ring update */
905 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700906
Pavel Begunkov07910152020-01-17 03:52:46 +0300907 if (wq_has_sleeper(&ctx->cq_wait)) {
908 wake_up_interruptible(&ctx->cq_wait);
909 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700910 }
911}
912
Jens Axboecccf0ee2020-01-27 16:34:48 -0700913static inline void io_req_work_grab_env(struct io_kiocb *req,
914 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600915{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700916 if (!req->work.mm && def->needs_mm) {
917 mmgrab(current->mm);
918 req->work.mm = current->mm;
919 }
920 if (!req->work.creds)
921 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700922 if (!req->work.fs && def->needs_fs) {
923 spin_lock(&current->fs->lock);
924 if (!current->fs->in_exec) {
925 req->work.fs = current->fs;
926 req->work.fs->users++;
927 } else {
928 req->work.flags |= IO_WQ_WORK_CANCEL;
929 }
930 spin_unlock(&current->fs->lock);
931 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700932 if (!req->work.task_pid)
933 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700934}
935
936static inline void io_req_work_drop_env(struct io_kiocb *req)
937{
938 if (req->work.mm) {
939 mmdrop(req->work.mm);
940 req->work.mm = NULL;
941 }
942 if (req->work.creds) {
943 put_cred(req->work.creds);
944 req->work.creds = NULL;
945 }
Jens Axboeff002b32020-02-07 16:05:21 -0700946 if (req->work.fs) {
947 struct fs_struct *fs = req->work.fs;
948
949 spin_lock(&req->work.fs->lock);
950 if (--fs->users)
951 fs = NULL;
952 spin_unlock(&req->work.fs->lock);
953 if (fs)
954 free_fs_struct(fs);
955 }
Jens Axboe561fb042019-10-24 07:25:42 -0600956}
957
Jens Axboe94ae5e72019-11-14 19:39:52 -0700958static inline bool io_prep_async_work(struct io_kiocb *req,
959 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600960{
Jens Axboed3656342019-12-18 09:50:26 -0700961 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600962 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600963
Jens Axboed3656342019-12-18 09:50:26 -0700964 if (req->flags & REQ_F_ISREG) {
965 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700966 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700967 } else {
968 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700969 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600970 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700971
972 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600973
Jens Axboe94ae5e72019-11-14 19:39:52 -0700974 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600975 return do_hashed;
976}
977
Jackie Liua197f662019-11-08 08:09:12 -0700978static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600979{
Jackie Liua197f662019-11-08 08:09:12 -0700980 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700981 struct io_kiocb *link;
982 bool do_hashed;
983
984 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600985
986 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
987 req->flags);
988 if (!do_hashed) {
989 io_wq_enqueue(ctx->io_wq, &req->work);
990 } else {
991 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
992 file_inode(req->file));
993 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700994
995 if (link)
996 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600997}
998
Jens Axboe5262f562019-09-17 12:26:57 -0600999static void io_kill_timeout(struct io_kiocb *req)
1000{
1001 int ret;
1002
Jens Axboe2d283902019-12-04 11:08:05 -07001003 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001004 if (ret != -1) {
1005 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001006 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001007 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001008 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001009 }
1010}
1011
1012static void io_kill_timeouts(struct io_ring_ctx *ctx)
1013{
1014 struct io_kiocb *req, *tmp;
1015
1016 spin_lock_irq(&ctx->completion_lock);
1017 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1018 io_kill_timeout(req);
1019 spin_unlock_irq(&ctx->completion_lock);
1020}
1021
Jens Axboede0617e2019-04-06 21:51:27 -06001022static void io_commit_cqring(struct io_ring_ctx *ctx)
1023{
1024 struct io_kiocb *req;
1025
Jens Axboe5262f562019-09-17 12:26:57 -06001026 while ((req = io_get_timeout_req(ctx)) != NULL)
1027 io_kill_timeout(req);
1028
Jens Axboede0617e2019-04-06 21:51:27 -06001029 __io_commit_cqring(ctx);
1030
Pavel Begunkov87987892020-01-18 01:22:30 +03001031 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001032 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001033}
1034
Jens Axboe2b188cc2019-01-07 10:46:33 -07001035static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1036{
Hristo Venev75b28af2019-08-26 17:23:46 +00001037 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001038 unsigned tail;
1039
1040 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001041 /*
1042 * writes to the cq entry need to come after reading head; the
1043 * control dependency is enough as we're using WRITE_ONCE to
1044 * fill the cq entry
1045 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001046 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047 return NULL;
1048
1049 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001050 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001051}
1052
Jens Axboef2842ab2020-01-08 11:04:00 -07001053static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1054{
Jens Axboef0b493e2020-02-01 21:30:11 -07001055 if (!ctx->cq_ev_fd)
1056 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001057 if (!ctx->eventfd_async)
1058 return true;
1059 return io_wq_current_is_worker() || in_interrupt();
1060}
1061
Jens Axboef0b493e2020-02-01 21:30:11 -07001062static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001063{
1064 if (waitqueue_active(&ctx->wait))
1065 wake_up(&ctx->wait);
1066 if (waitqueue_active(&ctx->sqo_wait))
1067 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001068 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001069 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001070}
1071
Jens Axboef0b493e2020-02-01 21:30:11 -07001072static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1073{
1074 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1075}
1076
Jens Axboec4a2ed72019-11-21 21:01:26 -07001077/* Returns true if there are no backlogged entries after the flush */
1078static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001079{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001080 struct io_rings *rings = ctx->rings;
1081 struct io_uring_cqe *cqe;
1082 struct io_kiocb *req;
1083 unsigned long flags;
1084 LIST_HEAD(list);
1085
1086 if (!force) {
1087 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001088 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001089 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1090 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001091 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001092 }
1093
1094 spin_lock_irqsave(&ctx->completion_lock, flags);
1095
1096 /* if force is set, the ring is going away. always drop after that */
1097 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001098 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001099
Jens Axboec4a2ed72019-11-21 21:01:26 -07001100 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001101 while (!list_empty(&ctx->cq_overflow_list)) {
1102 cqe = io_get_cqring(ctx);
1103 if (!cqe && !force)
1104 break;
1105
1106 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1107 list);
1108 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001109 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001110 if (cqe) {
1111 WRITE_ONCE(cqe->user_data, req->user_data);
1112 WRITE_ONCE(cqe->res, req->result);
1113 WRITE_ONCE(cqe->flags, 0);
1114 } else {
1115 WRITE_ONCE(ctx->rings->cq_overflow,
1116 atomic_inc_return(&ctx->cached_cq_overflow));
1117 }
1118 }
1119
1120 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001121 if (cqe) {
1122 clear_bit(0, &ctx->sq_check_overflow);
1123 clear_bit(0, &ctx->cq_check_overflow);
1124 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001125 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1126 io_cqring_ev_posted(ctx);
1127
1128 while (!list_empty(&list)) {
1129 req = list_first_entry(&list, struct io_kiocb, list);
1130 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001131 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001132 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001133
1134 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001135}
1136
Jens Axboe78e19bb2019-11-06 15:21:34 -07001137static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001138{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001139 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001140 struct io_uring_cqe *cqe;
1141
Jens Axboe78e19bb2019-11-06 15:21:34 -07001142 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001143
Jens Axboe2b188cc2019-01-07 10:46:33 -07001144 /*
1145 * If we can't get a cq entry, userspace overflowed the
1146 * submission (by quite a lot). Increment the overflow count in
1147 * the ring.
1148 */
1149 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001150 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001151 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001152 WRITE_ONCE(cqe->res, res);
1153 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001154 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001155 WRITE_ONCE(ctx->rings->cq_overflow,
1156 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001157 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001158 if (list_empty(&ctx->cq_overflow_list)) {
1159 set_bit(0, &ctx->sq_check_overflow);
1160 set_bit(0, &ctx->cq_check_overflow);
1161 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001162 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001163 refcount_inc(&req->refs);
1164 req->result = res;
1165 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001166 }
1167}
1168
Jens Axboe78e19bb2019-11-06 15:21:34 -07001169static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001170{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001171 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001172 unsigned long flags;
1173
1174 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001175 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001176 io_commit_cqring(ctx);
1177 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1178
Jens Axboe8c838782019-03-12 15:48:16 -06001179 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001180}
1181
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001182static inline bool io_is_fallback_req(struct io_kiocb *req)
1183{
1184 return req == (struct io_kiocb *)
1185 ((unsigned long) req->ctx->fallback_req & ~1UL);
1186}
1187
1188static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1189{
1190 struct io_kiocb *req;
1191
1192 req = ctx->fallback_req;
1193 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1194 return req;
1195
1196 return NULL;
1197}
1198
Jens Axboe2579f912019-01-09 09:10:43 -07001199static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1200 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001201{
Jens Axboefd6fab22019-03-14 16:30:06 -06001202 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001203 struct io_kiocb *req;
1204
Jens Axboe2579f912019-01-09 09:10:43 -07001205 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001206 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001207 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001208 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001209 } else if (!state->free_reqs) {
1210 size_t sz;
1211 int ret;
1212
1213 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001214 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1215
1216 /*
1217 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1218 * retry single alloc to be on the safe side.
1219 */
1220 if (unlikely(ret <= 0)) {
1221 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1222 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001223 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001224 ret = 1;
1225 }
Jens Axboe2579f912019-01-09 09:10:43 -07001226 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001227 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001228 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001229 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001230 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001231 }
1232
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001233got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001234 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001235 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001236 req->ctx = ctx;
1237 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001238 /* one is dropped after submission, the other at completion */
1239 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001240 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001241 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001242 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001243fallback:
1244 req = io_get_fallback_req(ctx);
1245 if (req)
1246 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001247 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248 return NULL;
1249}
1250
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001251static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001252{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001253 if (likely(!io_is_fallback_req(req)))
1254 kmem_cache_free(req_cachep, req);
1255 else
1256 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1257}
1258
Jens Axboec6ca97b302019-12-28 12:11:08 -07001259static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001260{
Jens Axboefcb323c2019-10-24 12:39:47 -06001261 struct io_ring_ctx *ctx = req->ctx;
1262
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001263 if (req->flags & REQ_F_NEED_CLEANUP)
1264 io_cleanup_req(req);
1265
YueHaibing96fd84d2020-01-07 22:22:44 +08001266 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001267 if (req->file) {
1268 if (req->flags & REQ_F_FIXED_FILE)
1269 percpu_ref_put(&ctx->file_data->refs);
1270 else
1271 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001272 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001273
1274 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001275}
1276
1277static void __io_free_req(struct io_kiocb *req)
1278{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001279 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001280
Jens Axboefcb323c2019-10-24 12:39:47 -06001281 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001282 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001283 unsigned long flags;
1284
1285 spin_lock_irqsave(&ctx->inflight_lock, flags);
1286 list_del(&req->inflight_entry);
1287 if (waitqueue_active(&ctx->inflight_wait))
1288 wake_up(&ctx->inflight_wait);
1289 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1290 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001291
1292 percpu_ref_put(&req->ctx->refs);
1293 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001294}
1295
Jens Axboec6ca97b302019-12-28 12:11:08 -07001296struct req_batch {
1297 void *reqs[IO_IOPOLL_BATCH];
1298 int to_free;
1299 int need_iter;
1300};
1301
1302static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1303{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001304 int fixed_refs = rb->to_free;
1305
Jens Axboec6ca97b302019-12-28 12:11:08 -07001306 if (!rb->to_free)
1307 return;
1308 if (rb->need_iter) {
1309 int i, inflight = 0;
1310 unsigned long flags;
1311
Jens Axboe10fef4b2020-01-09 07:52:28 -07001312 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001313 for (i = 0; i < rb->to_free; i++) {
1314 struct io_kiocb *req = rb->reqs[i];
1315
Jens Axboe10fef4b2020-01-09 07:52:28 -07001316 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001317 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001318 fixed_refs++;
1319 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001320 if (req->flags & REQ_F_INFLIGHT)
1321 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001322 __io_req_aux_free(req);
1323 }
1324 if (!inflight)
1325 goto do_free;
1326
1327 spin_lock_irqsave(&ctx->inflight_lock, flags);
1328 for (i = 0; i < rb->to_free; i++) {
1329 struct io_kiocb *req = rb->reqs[i];
1330
Jens Axboe10fef4b2020-01-09 07:52:28 -07001331 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001332 list_del(&req->inflight_entry);
1333 if (!--inflight)
1334 break;
1335 }
1336 }
1337 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1338
1339 if (waitqueue_active(&ctx->inflight_wait))
1340 wake_up(&ctx->inflight_wait);
1341 }
1342do_free:
1343 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001344 if (fixed_refs)
1345 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001346 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001347 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001348}
1349
Jackie Liua197f662019-11-08 08:09:12 -07001350static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001351{
Jackie Liua197f662019-11-08 08:09:12 -07001352 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001353 int ret;
1354
Jens Axboe2d283902019-12-04 11:08:05 -07001355 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001356 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001357 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001358 io_commit_cqring(ctx);
1359 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001360 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001361 return true;
1362 }
1363
1364 return false;
1365}
1366
Jens Axboeba816ad2019-09-28 11:36:45 -06001367static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001368{
Jens Axboe2665abf2019-11-05 12:40:47 -07001369 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001370 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001371
Jens Axboe4d7dd462019-11-20 13:03:52 -07001372 /* Already got next link */
1373 if (req->flags & REQ_F_LINK_NEXT)
1374 return;
1375
Jens Axboe9e645e112019-05-10 16:07:28 -06001376 /*
1377 * The list should never be empty when we are called here. But could
1378 * potentially happen if the chain is messed up, check to be on the
1379 * safe side.
1380 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001381 while (!list_empty(&req->link_list)) {
1382 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1383 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001384
Pavel Begunkov44932332019-12-05 16:16:35 +03001385 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1386 (nxt->flags & REQ_F_TIMEOUT))) {
1387 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001388 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001389 req->flags &= ~REQ_F_LINK_TIMEOUT;
1390 continue;
1391 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001392
Pavel Begunkov44932332019-12-05 16:16:35 +03001393 list_del_init(&req->link_list);
1394 if (!list_empty(&nxt->link_list))
1395 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001396 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001397 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001398 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001399
Jens Axboe4d7dd462019-11-20 13:03:52 -07001400 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001401 if (wake_ev)
1402 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001403}
1404
1405/*
1406 * Called if REQ_F_LINK is set, and we fail the head request
1407 */
1408static void io_fail_links(struct io_kiocb *req)
1409{
Jens Axboe2665abf2019-11-05 12:40:47 -07001410 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001411 unsigned long flags;
1412
1413 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001414
1415 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001416 struct io_kiocb *link = list_first_entry(&req->link_list,
1417 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001418
Pavel Begunkov44932332019-12-05 16:16:35 +03001419 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001420 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001421
1422 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001423 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001424 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001425 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001426 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001427 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001428 }
Jens Axboe5d960722019-11-19 15:31:28 -07001429 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001430 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001431
1432 io_commit_cqring(ctx);
1433 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1434 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001435}
1436
Jens Axboe4d7dd462019-11-20 13:03:52 -07001437static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001438{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001439 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001440 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001441
Jens Axboe9e645e112019-05-10 16:07:28 -06001442 /*
1443 * If LINK is set, we have dependent requests in this chain. If we
1444 * didn't fail this request, queue the first one up, moving any other
1445 * dependencies to the next request. In case of failure, fail the rest
1446 * of the chain.
1447 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001448 if (req->flags & REQ_F_FAIL_LINK) {
1449 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001450 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1451 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001452 struct io_ring_ctx *ctx = req->ctx;
1453 unsigned long flags;
1454
1455 /*
1456 * If this is a timeout link, we could be racing with the
1457 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001458 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001459 */
1460 spin_lock_irqsave(&ctx->completion_lock, flags);
1461 io_req_link_next(req, nxt);
1462 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1463 } else {
1464 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001465 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001466}
Jens Axboe9e645e112019-05-10 16:07:28 -06001467
Jackie Liuc69f8db2019-11-09 11:00:08 +08001468static void io_free_req(struct io_kiocb *req)
1469{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001470 struct io_kiocb *nxt = NULL;
1471
1472 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001473 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001474
1475 if (nxt)
1476 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001477}
1478
Jens Axboeba816ad2019-09-28 11:36:45 -06001479/*
1480 * Drop reference to request, return next in chain (if there is one) if this
1481 * was the last reference to this request.
1482 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001483__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001484static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001485{
Jens Axboe2a44f462020-02-25 13:25:41 -07001486 if (refcount_dec_and_test(&req->refs)) {
1487 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001488 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001489 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001490}
1491
Jens Axboe2b188cc2019-01-07 10:46:33 -07001492static void io_put_req(struct io_kiocb *req)
1493{
Jens Axboedef596e2019-01-09 08:59:42 -07001494 if (refcount_dec_and_test(&req->refs))
1495 io_free_req(req);
1496}
1497
Jens Axboe978db572019-11-14 22:39:04 -07001498/*
1499 * Must only be used if we don't need to care about links, usually from
1500 * within the completion handling itself.
1501 */
1502static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001503{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001504 /* drop both submit and complete references */
1505 if (refcount_sub_and_test(2, &req->refs))
1506 __io_free_req(req);
1507}
1508
Jens Axboe978db572019-11-14 22:39:04 -07001509static void io_double_put_req(struct io_kiocb *req)
1510{
1511 /* drop both submit and complete references */
1512 if (refcount_sub_and_test(2, &req->refs))
1513 io_free_req(req);
1514}
1515
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001516static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001517{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001518 struct io_rings *rings = ctx->rings;
1519
Jens Axboead3eb2c2019-12-18 17:12:20 -07001520 if (test_bit(0, &ctx->cq_check_overflow)) {
1521 /*
1522 * noflush == true is from the waitqueue handler, just ensure
1523 * we wake up the task, and the next invocation will flush the
1524 * entries. We cannot safely to it from here.
1525 */
1526 if (noflush && !list_empty(&ctx->cq_overflow_list))
1527 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001528
Jens Axboead3eb2c2019-12-18 17:12:20 -07001529 io_cqring_overflow_flush(ctx, false);
1530 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001531
Jens Axboea3a0e432019-08-20 11:03:11 -06001532 /* See comment at the top of this file */
1533 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001534 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001535}
1536
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001537static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1538{
1539 struct io_rings *rings = ctx->rings;
1540
1541 /* make sure SQ entry isn't read before tail */
1542 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1543}
1544
Jens Axboe8237e042019-12-28 10:48:22 -07001545static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001546{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001547 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1548 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001549
Jens Axboec6ca97b302019-12-28 12:11:08 -07001550 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1551 rb->need_iter++;
1552
1553 rb->reqs[rb->to_free++] = req;
1554 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1555 io_free_req_many(req->ctx, rb);
1556 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001557}
1558
Jens Axboedef596e2019-01-09 08:59:42 -07001559/*
1560 * Find and free completed poll iocbs
1561 */
1562static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1563 struct list_head *done)
1564{
Jens Axboe8237e042019-12-28 10:48:22 -07001565 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001566 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001567
Jens Axboec6ca97b302019-12-28 12:11:08 -07001568 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001569 while (!list_empty(done)) {
1570 req = list_first_entry(done, struct io_kiocb, list);
1571 list_del(&req->list);
1572
Jens Axboe78e19bb2019-11-06 15:21:34 -07001573 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001574 (*nr_events)++;
1575
Jens Axboe8237e042019-12-28 10:48:22 -07001576 if (refcount_dec_and_test(&req->refs) &&
1577 !io_req_multi_free(&rb, req))
1578 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001579 }
Jens Axboedef596e2019-01-09 08:59:42 -07001580
Jens Axboe09bb8392019-03-13 12:39:28 -06001581 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001582 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001583}
1584
1585static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1586 long min)
1587{
1588 struct io_kiocb *req, *tmp;
1589 LIST_HEAD(done);
1590 bool spin;
1591 int ret;
1592
1593 /*
1594 * Only spin for completions if we don't have multiple devices hanging
1595 * off our complete list, and we're under the requested amount.
1596 */
1597 spin = !ctx->poll_multi_file && *nr_events < min;
1598
1599 ret = 0;
1600 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001601 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001602
1603 /*
1604 * Move completed entries to our local list. If we find a
1605 * request that requires polling, break out and complete
1606 * the done list first, if we have entries there.
1607 */
1608 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1609 list_move_tail(&req->list, &done);
1610 continue;
1611 }
1612 if (!list_empty(&done))
1613 break;
1614
1615 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1616 if (ret < 0)
1617 break;
1618
1619 if (ret && spin)
1620 spin = false;
1621 ret = 0;
1622 }
1623
1624 if (!list_empty(&done))
1625 io_iopoll_complete(ctx, nr_events, &done);
1626
1627 return ret;
1628}
1629
1630/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001631 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001632 * non-spinning poll check - we'll still enter the driver poll loop, but only
1633 * as a non-spinning completion check.
1634 */
1635static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1636 long min)
1637{
Jens Axboe08f54392019-08-21 22:19:11 -06001638 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001639 int ret;
1640
1641 ret = io_do_iopoll(ctx, nr_events, min);
1642 if (ret < 0)
1643 return ret;
1644 if (!min || *nr_events >= min)
1645 return 0;
1646 }
1647
1648 return 1;
1649}
1650
1651/*
1652 * We can't just wait for polled events to come to us, we have to actively
1653 * find and complete them.
1654 */
1655static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1656{
1657 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1658 return;
1659
1660 mutex_lock(&ctx->uring_lock);
1661 while (!list_empty(&ctx->poll_list)) {
1662 unsigned int nr_events = 0;
1663
1664 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001665
1666 /*
1667 * Ensure we allow local-to-the-cpu processing to take place,
1668 * in this case we need to ensure that we reap all events.
1669 */
1670 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001671 }
1672 mutex_unlock(&ctx->uring_lock);
1673}
1674
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001675static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1676 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001677{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001678 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001679
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001680 /*
1681 * We disallow the app entering submit/complete with polling, but we
1682 * still need to lock the ring to prevent racing with polled issue
1683 * that got punted to a workqueue.
1684 */
1685 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001686 do {
1687 int tmin = 0;
1688
Jens Axboe500f9fb2019-08-19 12:15:59 -06001689 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001690 * Don't enter poll loop if we already have events pending.
1691 * If we do, we can potentially be spinning for commands that
1692 * already triggered a CQE (eg in error).
1693 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001694 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001695 break;
1696
1697 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001698 * If a submit got punted to a workqueue, we can have the
1699 * application entering polling for a command before it gets
1700 * issued. That app will hold the uring_lock for the duration
1701 * of the poll right here, so we need to take a breather every
1702 * now and then to ensure that the issue has a chance to add
1703 * the poll to the issued list. Otherwise we can spin here
1704 * forever, while the workqueue is stuck trying to acquire the
1705 * very same mutex.
1706 */
1707 if (!(++iters & 7)) {
1708 mutex_unlock(&ctx->uring_lock);
1709 mutex_lock(&ctx->uring_lock);
1710 }
1711
Jens Axboedef596e2019-01-09 08:59:42 -07001712 if (*nr_events < min)
1713 tmin = min - *nr_events;
1714
1715 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1716 if (ret <= 0)
1717 break;
1718 ret = 0;
1719 } while (min && !*nr_events && !need_resched());
1720
Jens Axboe500f9fb2019-08-19 12:15:59 -06001721 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001722 return ret;
1723}
1724
Jens Axboe491381ce2019-10-17 09:20:46 -06001725static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726{
Jens Axboe491381ce2019-10-17 09:20:46 -06001727 /*
1728 * Tell lockdep we inherited freeze protection from submission
1729 * thread.
1730 */
1731 if (req->flags & REQ_F_ISREG) {
1732 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733
Jens Axboe491381ce2019-10-17 09:20:46 -06001734 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001736 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737}
1738
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001739static inline void req_set_fail_links(struct io_kiocb *req)
1740{
1741 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1742 req->flags |= REQ_F_FAIL_LINK;
1743}
1744
Jens Axboeba816ad2019-09-28 11:36:45 -06001745static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001746{
Jens Axboe9adbd452019-12-20 08:45:55 -07001747 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001748
Jens Axboe491381ce2019-10-17 09:20:46 -06001749 if (kiocb->ki_flags & IOCB_WRITE)
1750 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001751
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001752 if (res != req->result)
1753 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001754 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001755}
1756
1757static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1758{
Jens Axboe9adbd452019-12-20 08:45:55 -07001759 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001760
1761 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001762 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001763}
1764
Jens Axboeba816ad2019-09-28 11:36:45 -06001765static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1766{
Jens Axboe9adbd452019-12-20 08:45:55 -07001767 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001768 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001769
1770 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001771 io_put_req_find_next(req, &nxt);
1772
1773 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001774}
1775
Jens Axboedef596e2019-01-09 08:59:42 -07001776static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1777{
Jens Axboe9adbd452019-12-20 08:45:55 -07001778 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001779
Jens Axboe491381ce2019-10-17 09:20:46 -06001780 if (kiocb->ki_flags & IOCB_WRITE)
1781 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001782
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001783 if (res != req->result)
1784 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001785 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001786 if (res != -EAGAIN)
1787 req->flags |= REQ_F_IOPOLL_COMPLETED;
1788}
1789
1790/*
1791 * After the iocb has been issued, it's safe to be found on the poll list.
1792 * Adding the kiocb to the list AFTER submission ensures that we don't
1793 * find it from a io_iopoll_getevents() thread before the issuer is done
1794 * accessing the kiocb cookie.
1795 */
1796static void io_iopoll_req_issued(struct io_kiocb *req)
1797{
1798 struct io_ring_ctx *ctx = req->ctx;
1799
1800 /*
1801 * Track whether we have multiple files in our lists. This will impact
1802 * how we do polling eventually, not spinning if we're on potentially
1803 * different devices.
1804 */
1805 if (list_empty(&ctx->poll_list)) {
1806 ctx->poll_multi_file = false;
1807 } else if (!ctx->poll_multi_file) {
1808 struct io_kiocb *list_req;
1809
1810 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1811 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001812 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001813 ctx->poll_multi_file = true;
1814 }
1815
1816 /*
1817 * For fast devices, IO may have already completed. If it has, add
1818 * it to the front so we find it first.
1819 */
1820 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1821 list_add(&req->list, &ctx->poll_list);
1822 else
1823 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001824
1825 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1826 wq_has_sleeper(&ctx->sqo_wait))
1827 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001828}
1829
Jens Axboe3d6770f2019-04-13 11:50:54 -06001830static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001831{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001832 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001833 int diff = state->has_refs - state->used_refs;
1834
1835 if (diff)
1836 fput_many(state->file, diff);
1837 state->file = NULL;
1838 }
1839}
1840
1841/*
1842 * Get as many references to a file as we have IOs left in this submission,
1843 * assuming most submissions are for one file, or at least that each file
1844 * has more than one submission.
1845 */
1846static struct file *io_file_get(struct io_submit_state *state, int fd)
1847{
1848 if (!state)
1849 return fget(fd);
1850
1851 if (state->file) {
1852 if (state->fd == fd) {
1853 state->used_refs++;
1854 state->ios_left--;
1855 return state->file;
1856 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001857 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001858 }
1859 state->file = fget_many(fd, state->ios_left);
1860 if (!state->file)
1861 return NULL;
1862
1863 state->fd = fd;
1864 state->has_refs = state->ios_left;
1865 state->used_refs = 1;
1866 state->ios_left--;
1867 return state->file;
1868}
1869
Jens Axboe2b188cc2019-01-07 10:46:33 -07001870/*
1871 * If we tracked the file through the SCM inflight mechanism, we could support
1872 * any file. For now, just ensure that anything potentially problematic is done
1873 * inline.
1874 */
1875static bool io_file_supports_async(struct file *file)
1876{
1877 umode_t mode = file_inode(file)->i_mode;
1878
Jens Axboe10d59342019-12-09 20:16:22 -07001879 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001880 return true;
1881 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1882 return true;
1883
1884 return false;
1885}
1886
Jens Axboe3529d8c2019-12-19 18:24:38 -07001887static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1888 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889{
Jens Axboedef596e2019-01-09 08:59:42 -07001890 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001891 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001892 unsigned ioprio;
1893 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001894
Jens Axboe491381ce2019-10-17 09:20:46 -06001895 if (S_ISREG(file_inode(req->file)->i_mode))
1896 req->flags |= REQ_F_ISREG;
1897
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001899 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1900 req->flags |= REQ_F_CUR_POS;
1901 kiocb->ki_pos = req->file->f_pos;
1902 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001904 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1905 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1906 if (unlikely(ret))
1907 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908
1909 ioprio = READ_ONCE(sqe->ioprio);
1910 if (ioprio) {
1911 ret = ioprio_check_cap(ioprio);
1912 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001913 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001914
1915 kiocb->ki_ioprio = ioprio;
1916 } else
1917 kiocb->ki_ioprio = get_current_ioprio();
1918
Stefan Bühler8449eed2019-04-27 20:34:19 +02001919 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001920 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1921 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001922 req->flags |= REQ_F_NOWAIT;
1923
1924 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001925 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001926
Jens Axboedef596e2019-01-09 08:59:42 -07001927 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001928 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1929 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001930 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001931
Jens Axboedef596e2019-01-09 08:59:42 -07001932 kiocb->ki_flags |= IOCB_HIPRI;
1933 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001934 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001935 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001936 if (kiocb->ki_flags & IOCB_HIPRI)
1937 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001938 kiocb->ki_complete = io_complete_rw;
1939 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001940
Jens Axboe3529d8c2019-12-19 18:24:38 -07001941 req->rw.addr = READ_ONCE(sqe->addr);
1942 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001943 /* we own ->private, reuse it for the buffer index */
1944 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001945 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001946 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001947}
1948
1949static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1950{
1951 switch (ret) {
1952 case -EIOCBQUEUED:
1953 break;
1954 case -ERESTARTSYS:
1955 case -ERESTARTNOINTR:
1956 case -ERESTARTNOHAND:
1957 case -ERESTART_RESTARTBLOCK:
1958 /*
1959 * We can't just restart the syscall, since previously
1960 * submitted sqes may already be in progress. Just fail this
1961 * IO with EINTR.
1962 */
1963 ret = -EINTR;
1964 /* fall through */
1965 default:
1966 kiocb->ki_complete(kiocb, ret, 0);
1967 }
1968}
1969
Jens Axboeba816ad2019-09-28 11:36:45 -06001970static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1971 bool in_async)
1972{
Jens Axboeba042912019-12-25 16:33:42 -07001973 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1974
1975 if (req->flags & REQ_F_CUR_POS)
1976 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001977 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001978 *nxt = __io_complete_rw(kiocb, ret);
1979 else
1980 io_rw_done(kiocb, ret);
1981}
1982
Jens Axboe9adbd452019-12-20 08:45:55 -07001983static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001984 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001985{
Jens Axboe9adbd452019-12-20 08:45:55 -07001986 struct io_ring_ctx *ctx = req->ctx;
1987 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001988 struct io_mapped_ubuf *imu;
1989 unsigned index, buf_index;
1990 size_t offset;
1991 u64 buf_addr;
1992
1993 /* attempt to use fixed buffers without having provided iovecs */
1994 if (unlikely(!ctx->user_bufs))
1995 return -EFAULT;
1996
Jens Axboe9adbd452019-12-20 08:45:55 -07001997 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001998 if (unlikely(buf_index >= ctx->nr_user_bufs))
1999 return -EFAULT;
2000
2001 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2002 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002003 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002004
2005 /* overflow */
2006 if (buf_addr + len < buf_addr)
2007 return -EFAULT;
2008 /* not inside the mapped region */
2009 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2010 return -EFAULT;
2011
2012 /*
2013 * May not be a start of buffer, set size appropriately
2014 * and advance us to the beginning.
2015 */
2016 offset = buf_addr - imu->ubuf;
2017 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002018
2019 if (offset) {
2020 /*
2021 * Don't use iov_iter_advance() here, as it's really slow for
2022 * using the latter parts of a big fixed buffer - it iterates
2023 * over each segment manually. We can cheat a bit here, because
2024 * we know that:
2025 *
2026 * 1) it's a BVEC iter, we set it up
2027 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2028 * first and last bvec
2029 *
2030 * So just find our index, and adjust the iterator afterwards.
2031 * If the offset is within the first bvec (or the whole first
2032 * bvec, just use iov_iter_advance(). This makes it easier
2033 * since we can just skip the first segment, which may not
2034 * be PAGE_SIZE aligned.
2035 */
2036 const struct bio_vec *bvec = imu->bvec;
2037
2038 if (offset <= bvec->bv_len) {
2039 iov_iter_advance(iter, offset);
2040 } else {
2041 unsigned long seg_skip;
2042
2043 /* skip first vec */
2044 offset -= bvec->bv_len;
2045 seg_skip = 1 + (offset >> PAGE_SHIFT);
2046
2047 iter->bvec = bvec + seg_skip;
2048 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002049 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002050 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002051 }
2052 }
2053
Jens Axboe5e559562019-11-13 16:12:46 -07002054 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002055}
2056
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002057static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2058 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002059{
Jens Axboe9adbd452019-12-20 08:45:55 -07002060 void __user *buf = u64_to_user_ptr(req->rw.addr);
2061 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002062 u8 opcode;
2063
Jens Axboed625c6e2019-12-17 19:53:05 -07002064 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002065 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002066 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002067 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002068 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002069
Jens Axboe9adbd452019-12-20 08:45:55 -07002070 /* buffer index only valid with fixed read/write */
2071 if (req->rw.kiocb.private)
2072 return -EINVAL;
2073
Jens Axboe3a6820f2019-12-22 15:19:35 -07002074 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2075 ssize_t ret;
2076 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2077 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002078 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002079 }
2080
Jens Axboef67676d2019-12-02 11:03:47 -07002081 if (req->io) {
2082 struct io_async_rw *iorw = &req->io->rw;
2083
2084 *iovec = iorw->iov;
2085 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2086 if (iorw->iov == iorw->fast_iov)
2087 *iovec = NULL;
2088 return iorw->size;
2089 }
2090
Jens Axboe2b188cc2019-01-07 10:46:33 -07002091#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002092 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002093 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2094 iovec, iter);
2095#endif
2096
2097 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2098}
2099
Jens Axboe32960612019-09-23 11:05:34 -06002100/*
2101 * For files that don't have ->read_iter() and ->write_iter(), handle them
2102 * by looping over ->read() or ->write() manually.
2103 */
2104static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2105 struct iov_iter *iter)
2106{
2107 ssize_t ret = 0;
2108
2109 /*
2110 * Don't support polled IO through this interface, and we can't
2111 * support non-blocking either. For the latter, this just causes
2112 * the kiocb to be handled from an async context.
2113 */
2114 if (kiocb->ki_flags & IOCB_HIPRI)
2115 return -EOPNOTSUPP;
2116 if (kiocb->ki_flags & IOCB_NOWAIT)
2117 return -EAGAIN;
2118
2119 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002120 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002121 ssize_t nr;
2122
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002123 if (!iov_iter_is_bvec(iter)) {
2124 iovec = iov_iter_iovec(iter);
2125 } else {
2126 /* fixed buffers import bvec */
2127 iovec.iov_base = kmap(iter->bvec->bv_page)
2128 + iter->iov_offset;
2129 iovec.iov_len = min(iter->count,
2130 iter->bvec->bv_len - iter->iov_offset);
2131 }
2132
Jens Axboe32960612019-09-23 11:05:34 -06002133 if (rw == READ) {
2134 nr = file->f_op->read(file, iovec.iov_base,
2135 iovec.iov_len, &kiocb->ki_pos);
2136 } else {
2137 nr = file->f_op->write(file, iovec.iov_base,
2138 iovec.iov_len, &kiocb->ki_pos);
2139 }
2140
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002141 if (iov_iter_is_bvec(iter))
2142 kunmap(iter->bvec->bv_page);
2143
Jens Axboe32960612019-09-23 11:05:34 -06002144 if (nr < 0) {
2145 if (!ret)
2146 ret = nr;
2147 break;
2148 }
2149 ret += nr;
2150 if (nr != iovec.iov_len)
2151 break;
2152 iov_iter_advance(iter, nr);
2153 }
2154
2155 return ret;
2156}
2157
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002158static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002159 struct iovec *iovec, struct iovec *fast_iov,
2160 struct iov_iter *iter)
2161{
2162 req->io->rw.nr_segs = iter->nr_segs;
2163 req->io->rw.size = io_size;
2164 req->io->rw.iov = iovec;
2165 if (!req->io->rw.iov) {
2166 req->io->rw.iov = req->io->rw.fast_iov;
2167 memcpy(req->io->rw.iov, fast_iov,
2168 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002169 } else {
2170 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002171 }
2172}
2173
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002174static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002175{
Jens Axboed3656342019-12-18 09:50:26 -07002176 if (!io_op_defs[req->opcode].async_ctx)
2177 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002178 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002179 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002180}
2181
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002182static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2183 struct iovec *iovec, struct iovec *fast_iov,
2184 struct iov_iter *iter)
2185{
Jens Axboe980ad262020-01-24 23:08:54 -07002186 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002187 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002188 if (!req->io) {
2189 if (io_alloc_async_ctx(req))
2190 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002191
Jens Axboe5d204bc2020-01-31 12:06:52 -07002192 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2193 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002194 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002195}
2196
Jens Axboe3529d8c2019-12-19 18:24:38 -07002197static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2198 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002199{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002200 struct io_async_ctx *io;
2201 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002202 ssize_t ret;
2203
Jens Axboe3529d8c2019-12-19 18:24:38 -07002204 ret = io_prep_rw(req, sqe, force_nonblock);
2205 if (ret)
2206 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002207
Jens Axboe3529d8c2019-12-19 18:24:38 -07002208 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2209 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002210
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002211 /* either don't need iovec imported or already have it */
2212 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002213 return 0;
2214
2215 io = req->io;
2216 io->rw.iov = io->rw.fast_iov;
2217 req->io = NULL;
2218 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2219 req->io = io;
2220 if (ret < 0)
2221 return ret;
2222
2223 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2224 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002225}
2226
Pavel Begunkov267bc902019-11-07 01:41:08 +03002227static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002228 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002229{
2230 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002231 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002232 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002233 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002234 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002235
Jens Axboe3529d8c2019-12-19 18:24:38 -07002236 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002237 if (ret < 0)
2238 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002239
Jens Axboefd6c2e42019-12-18 12:19:41 -07002240 /* Ensure we clear previously set non-block flag */
2241 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002242 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002243
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002244 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002245 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002246 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002247 req->result = io_size;
2248
2249 /*
2250 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2251 * we know to async punt it even if it was opened O_NONBLOCK
2252 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002253 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002254 req->flags |= REQ_F_MUST_PUNT;
2255 goto copy_iov;
2256 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002257
Jens Axboe31b51512019-01-18 22:56:34 -07002258 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002259 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002260 if (!ret) {
2261 ssize_t ret2;
2262
Jens Axboe9adbd452019-12-20 08:45:55 -07002263 if (req->file->f_op->read_iter)
2264 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002265 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002266 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002267
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002268 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002269 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002270 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002271 } else {
2272copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002273 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002274 inline_vecs, &iter);
2275 if (ret)
2276 goto out_free;
2277 return -EAGAIN;
2278 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002279 }
Jens Axboef67676d2019-12-02 11:03:47 -07002280out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002281 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002282 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002283 return ret;
2284}
2285
Jens Axboe3529d8c2019-12-19 18:24:38 -07002286static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2287 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002288{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002289 struct io_async_ctx *io;
2290 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002291 ssize_t ret;
2292
Jens Axboe3529d8c2019-12-19 18:24:38 -07002293 ret = io_prep_rw(req, sqe, force_nonblock);
2294 if (ret)
2295 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002296
Jens Axboe3529d8c2019-12-19 18:24:38 -07002297 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2298 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002299
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002300 /* either don't need iovec imported or already have it */
2301 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002302 return 0;
2303
2304 io = req->io;
2305 io->rw.iov = io->rw.fast_iov;
2306 req->io = NULL;
2307 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2308 req->io = io;
2309 if (ret < 0)
2310 return ret;
2311
2312 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2313 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002314}
2315
Pavel Begunkov267bc902019-11-07 01:41:08 +03002316static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002317 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002318{
2319 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002320 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002322 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002323 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002324
Jens Axboe3529d8c2019-12-19 18:24:38 -07002325 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002326 if (ret < 0)
2327 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002328
Jens Axboefd6c2e42019-12-18 12:19:41 -07002329 /* Ensure we clear previously set non-block flag */
2330 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002331 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002332
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002333 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002334 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002335 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002336 req->result = io_size;
2337
2338 /*
2339 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2340 * we know to async punt it even if it was opened O_NONBLOCK
2341 */
2342 if (force_nonblock && !io_file_supports_async(req->file)) {
2343 req->flags |= REQ_F_MUST_PUNT;
2344 goto copy_iov;
2345 }
2346
Jens Axboe10d59342019-12-09 20:16:22 -07002347 /* file path doesn't support NOWAIT for non-direct_IO */
2348 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2349 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002350 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002351
Jens Axboe31b51512019-01-18 22:56:34 -07002352 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002353 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002354 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002355 ssize_t ret2;
2356
Jens Axboe2b188cc2019-01-07 10:46:33 -07002357 /*
2358 * Open-code file_start_write here to grab freeze protection,
2359 * which will be released by another thread in
2360 * io_complete_rw(). Fool lockdep by telling it the lock got
2361 * released so that it doesn't complain about the held lock when
2362 * we return to userspace.
2363 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002364 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002365 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002366 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002367 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002368 SB_FREEZE_WRITE);
2369 }
2370 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002371
Jens Axboe9adbd452019-12-20 08:45:55 -07002372 if (req->file->f_op->write_iter)
2373 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002374 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002375 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002376 /*
2377 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2378 * retry them without IOCB_NOWAIT.
2379 */
2380 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2381 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002382 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002383 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002384 } else {
2385copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002386 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002387 inline_vecs, &iter);
2388 if (ret)
2389 goto out_free;
2390 return -EAGAIN;
2391 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002392 }
Jens Axboe31b51512019-01-18 22:56:34 -07002393out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002394 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002395 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002396 return ret;
2397}
2398
2399/*
2400 * IORING_OP_NOP just posts a completion event, nothing else.
2401 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002402static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002403{
2404 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002405
Jens Axboedef596e2019-01-09 08:59:42 -07002406 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2407 return -EINVAL;
2408
Jens Axboe78e19bb2019-11-06 15:21:34 -07002409 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002410 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002411 return 0;
2412}
2413
Jens Axboe3529d8c2019-12-19 18:24:38 -07002414static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002415{
Jens Axboe6b063142019-01-10 22:13:58 -07002416 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002417
Jens Axboe09bb8392019-03-13 12:39:28 -06002418 if (!req->file)
2419 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002420
Jens Axboe6b063142019-01-10 22:13:58 -07002421 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002422 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002423 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002424 return -EINVAL;
2425
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002426 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2427 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2428 return -EINVAL;
2429
2430 req->sync.off = READ_ONCE(sqe->off);
2431 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002432 return 0;
2433}
2434
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002435static bool io_req_cancelled(struct io_kiocb *req)
2436{
2437 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2438 req_set_fail_links(req);
2439 io_cqring_add_event(req, -ECANCELED);
2440 io_put_req(req);
2441 return true;
2442 }
2443
2444 return false;
2445}
2446
Jens Axboe78912932020-01-14 22:09:06 -07002447static void io_link_work_cb(struct io_wq_work **workptr)
2448{
2449 struct io_wq_work *work = *workptr;
2450 struct io_kiocb *link = work->data;
2451
2452 io_queue_linked_timeout(link);
2453 work->func = io_wq_submit_work;
2454}
2455
2456static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2457{
2458 struct io_kiocb *link;
2459
2460 io_prep_async_work(nxt, &link);
2461 *workptr = &nxt->work;
2462 if (link) {
2463 nxt->work.flags |= IO_WQ_WORK_CB;
2464 nxt->work.func = io_link_work_cb;
2465 nxt->work.data = link;
2466 }
2467}
2468
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002469static void io_fsync_finish(struct io_wq_work **workptr)
2470{
2471 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2472 loff_t end = req->sync.off + req->sync.len;
2473 struct io_kiocb *nxt = NULL;
2474 int ret;
2475
2476 if (io_req_cancelled(req))
2477 return;
2478
Jens Axboe9adbd452019-12-20 08:45:55 -07002479 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002480 end > 0 ? end : LLONG_MAX,
2481 req->sync.flags & IORING_FSYNC_DATASYNC);
2482 if (ret < 0)
2483 req_set_fail_links(req);
2484 io_cqring_add_event(req, ret);
2485 io_put_req_find_next(req, &nxt);
2486 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002487 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002488}
2489
Jens Axboefc4df992019-12-10 14:38:45 -07002490static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2491 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002492{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002493 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002494
2495 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002496 if (force_nonblock) {
2497 io_put_req(req);
2498 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002499 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002500 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002501
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002502 work = old_work = &req->work;
2503 io_fsync_finish(&work);
2504 if (work && work != old_work)
2505 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002506 return 0;
2507}
2508
Jens Axboed63d1b52019-12-10 10:38:56 -07002509static void io_fallocate_finish(struct io_wq_work **workptr)
2510{
2511 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2512 struct io_kiocb *nxt = NULL;
2513 int ret;
2514
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002515 if (io_req_cancelled(req))
2516 return;
2517
Jens Axboed63d1b52019-12-10 10:38:56 -07002518 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2519 req->sync.len);
2520 if (ret < 0)
2521 req_set_fail_links(req);
2522 io_cqring_add_event(req, ret);
2523 io_put_req_find_next(req, &nxt);
2524 if (nxt)
2525 io_wq_assign_next(workptr, nxt);
2526}
2527
2528static int io_fallocate_prep(struct io_kiocb *req,
2529 const struct io_uring_sqe *sqe)
2530{
2531 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2532 return -EINVAL;
2533
2534 req->sync.off = READ_ONCE(sqe->off);
2535 req->sync.len = READ_ONCE(sqe->addr);
2536 req->sync.mode = READ_ONCE(sqe->len);
2537 return 0;
2538}
2539
2540static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2541 bool force_nonblock)
2542{
2543 struct io_wq_work *work, *old_work;
2544
2545 /* fallocate always requiring blocking context */
2546 if (force_nonblock) {
2547 io_put_req(req);
2548 req->work.func = io_fallocate_finish;
2549 return -EAGAIN;
2550 }
2551
2552 work = old_work = &req->work;
2553 io_fallocate_finish(&work);
2554 if (work && work != old_work)
2555 *nxt = container_of(work, struct io_kiocb, work);
2556
2557 return 0;
2558}
2559
Jens Axboe15b71ab2019-12-11 11:20:36 -07002560static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2561{
Jens Axboef8748882020-01-08 17:47:02 -07002562 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002563 int ret;
2564
2565 if (sqe->ioprio || sqe->buf_index)
2566 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002567 if (sqe->flags & IOSQE_FIXED_FILE)
2568 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002569 if (req->flags & REQ_F_NEED_CLEANUP)
2570 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002571
2572 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002573 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002574 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002575 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002576
Jens Axboef8748882020-01-08 17:47:02 -07002577 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002578 if (IS_ERR(req->open.filename)) {
2579 ret = PTR_ERR(req->open.filename);
2580 req->open.filename = NULL;
2581 return ret;
2582 }
2583
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002584 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002585 return 0;
2586}
2587
Jens Axboecebdb982020-01-08 17:59:24 -07002588static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2589{
2590 struct open_how __user *how;
2591 const char __user *fname;
2592 size_t len;
2593 int ret;
2594
2595 if (sqe->ioprio || sqe->buf_index)
2596 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002597 if (sqe->flags & IOSQE_FIXED_FILE)
2598 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002599 if (req->flags & REQ_F_NEED_CLEANUP)
2600 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002601
2602 req->open.dfd = READ_ONCE(sqe->fd);
2603 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2604 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2605 len = READ_ONCE(sqe->len);
2606
2607 if (len < OPEN_HOW_SIZE_VER0)
2608 return -EINVAL;
2609
2610 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2611 len);
2612 if (ret)
2613 return ret;
2614
2615 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2616 req->open.how.flags |= O_LARGEFILE;
2617
2618 req->open.filename = getname(fname);
2619 if (IS_ERR(req->open.filename)) {
2620 ret = PTR_ERR(req->open.filename);
2621 req->open.filename = NULL;
2622 return ret;
2623 }
2624
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002625 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002626 return 0;
2627}
2628
2629static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2630 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002631{
2632 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002633 struct file *file;
2634 int ret;
2635
Jens Axboef86cd202020-01-29 13:46:44 -07002636 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002637 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002638
Jens Axboecebdb982020-01-08 17:59:24 -07002639 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002640 if (ret)
2641 goto err;
2642
Jens Axboecebdb982020-01-08 17:59:24 -07002643 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002644 if (ret < 0)
2645 goto err;
2646
2647 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2648 if (IS_ERR(file)) {
2649 put_unused_fd(ret);
2650 ret = PTR_ERR(file);
2651 } else {
2652 fsnotify_open(file);
2653 fd_install(ret, file);
2654 }
2655err:
2656 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002657 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002658 if (ret < 0)
2659 req_set_fail_links(req);
2660 io_cqring_add_event(req, ret);
2661 io_put_req_find_next(req, nxt);
2662 return 0;
2663}
2664
Jens Axboecebdb982020-01-08 17:59:24 -07002665static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2666 bool force_nonblock)
2667{
2668 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2669 return io_openat2(req, nxt, force_nonblock);
2670}
2671
Jens Axboe3e4827b2020-01-08 15:18:09 -07002672static int io_epoll_ctl_prep(struct io_kiocb *req,
2673 const struct io_uring_sqe *sqe)
2674{
2675#if defined(CONFIG_EPOLL)
2676 if (sqe->ioprio || sqe->buf_index)
2677 return -EINVAL;
2678
2679 req->epoll.epfd = READ_ONCE(sqe->fd);
2680 req->epoll.op = READ_ONCE(sqe->len);
2681 req->epoll.fd = READ_ONCE(sqe->off);
2682
2683 if (ep_op_has_event(req->epoll.op)) {
2684 struct epoll_event __user *ev;
2685
2686 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2687 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2688 return -EFAULT;
2689 }
2690
2691 return 0;
2692#else
2693 return -EOPNOTSUPP;
2694#endif
2695}
2696
2697static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2698 bool force_nonblock)
2699{
2700#if defined(CONFIG_EPOLL)
2701 struct io_epoll *ie = &req->epoll;
2702 int ret;
2703
2704 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2705 if (force_nonblock && ret == -EAGAIN)
2706 return -EAGAIN;
2707
2708 if (ret < 0)
2709 req_set_fail_links(req);
2710 io_cqring_add_event(req, ret);
2711 io_put_req_find_next(req, nxt);
2712 return 0;
2713#else
2714 return -EOPNOTSUPP;
2715#endif
2716}
2717
Jens Axboec1ca7572019-12-25 22:18:28 -07002718static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2719{
2720#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2721 if (sqe->ioprio || sqe->buf_index || sqe->off)
2722 return -EINVAL;
2723
2724 req->madvise.addr = READ_ONCE(sqe->addr);
2725 req->madvise.len = READ_ONCE(sqe->len);
2726 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2727 return 0;
2728#else
2729 return -EOPNOTSUPP;
2730#endif
2731}
2732
2733static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2734 bool force_nonblock)
2735{
2736#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2737 struct io_madvise *ma = &req->madvise;
2738 int ret;
2739
2740 if (force_nonblock)
2741 return -EAGAIN;
2742
2743 ret = do_madvise(ma->addr, ma->len, ma->advice);
2744 if (ret < 0)
2745 req_set_fail_links(req);
2746 io_cqring_add_event(req, ret);
2747 io_put_req_find_next(req, nxt);
2748 return 0;
2749#else
2750 return -EOPNOTSUPP;
2751#endif
2752}
2753
Jens Axboe4840e412019-12-25 22:03:45 -07002754static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2755{
2756 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2757 return -EINVAL;
2758
2759 req->fadvise.offset = READ_ONCE(sqe->off);
2760 req->fadvise.len = READ_ONCE(sqe->len);
2761 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2762 return 0;
2763}
2764
2765static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2766 bool force_nonblock)
2767{
2768 struct io_fadvise *fa = &req->fadvise;
2769 int ret;
2770
Jens Axboe3e694262020-02-01 09:22:49 -07002771 if (force_nonblock) {
2772 switch (fa->advice) {
2773 case POSIX_FADV_NORMAL:
2774 case POSIX_FADV_RANDOM:
2775 case POSIX_FADV_SEQUENTIAL:
2776 break;
2777 default:
2778 return -EAGAIN;
2779 }
2780 }
Jens Axboe4840e412019-12-25 22:03:45 -07002781
2782 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2783 if (ret < 0)
2784 req_set_fail_links(req);
2785 io_cqring_add_event(req, ret);
2786 io_put_req_find_next(req, nxt);
2787 return 0;
2788}
2789
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002790static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2791{
Jens Axboef8748882020-01-08 17:47:02 -07002792 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002793 unsigned lookup_flags;
2794 int ret;
2795
2796 if (sqe->ioprio || sqe->buf_index)
2797 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002798 if (sqe->flags & IOSQE_FIXED_FILE)
2799 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002800 if (req->flags & REQ_F_NEED_CLEANUP)
2801 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002802
2803 req->open.dfd = READ_ONCE(sqe->fd);
2804 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002805 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002806 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002807 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002808
Jens Axboec12cedf2020-01-08 17:41:21 -07002809 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002810 return -EINVAL;
2811
Jens Axboef8748882020-01-08 17:47:02 -07002812 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002813 if (IS_ERR(req->open.filename)) {
2814 ret = PTR_ERR(req->open.filename);
2815 req->open.filename = NULL;
2816 return ret;
2817 }
2818
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002819 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002820 return 0;
2821}
2822
2823static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2824 bool force_nonblock)
2825{
2826 struct io_open *ctx = &req->open;
2827 unsigned lookup_flags;
2828 struct path path;
2829 struct kstat stat;
2830 int ret;
2831
2832 if (force_nonblock)
2833 return -EAGAIN;
2834
Jens Axboec12cedf2020-01-08 17:41:21 -07002835 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002836 return -EINVAL;
2837
2838retry:
2839 /* filename_lookup() drops it, keep a reference */
2840 ctx->filename->refcnt++;
2841
2842 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2843 NULL);
2844 if (ret)
2845 goto err;
2846
Jens Axboec12cedf2020-01-08 17:41:21 -07002847 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002848 path_put(&path);
2849 if (retry_estale(ret, lookup_flags)) {
2850 lookup_flags |= LOOKUP_REVAL;
2851 goto retry;
2852 }
2853 if (!ret)
2854 ret = cp_statx(&stat, ctx->buffer);
2855err:
2856 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002857 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002858 if (ret < 0)
2859 req_set_fail_links(req);
2860 io_cqring_add_event(req, ret);
2861 io_put_req_find_next(req, nxt);
2862 return 0;
2863}
2864
Jens Axboeb5dba592019-12-11 14:02:38 -07002865static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2866{
2867 /*
2868 * If we queue this for async, it must not be cancellable. That would
2869 * leave the 'file' in an undeterminate state.
2870 */
2871 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2872
2873 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2874 sqe->rw_flags || sqe->buf_index)
2875 return -EINVAL;
2876 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002877 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002878
2879 req->close.fd = READ_ONCE(sqe->fd);
2880 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002881 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002882 return -EBADF;
2883
2884 return 0;
2885}
2886
Pavel Begunkova93b3332020-02-08 14:04:34 +03002887/* only called when __close_fd_get_file() is done */
2888static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
2889{
2890 int ret;
2891
2892 ret = filp_close(req->close.put_file, req->work.files);
2893 if (ret < 0)
2894 req_set_fail_links(req);
2895 io_cqring_add_event(req, ret);
2896 fput(req->close.put_file);
2897 io_put_req_find_next(req, nxt);
2898}
2899
Jens Axboeb5dba592019-12-11 14:02:38 -07002900static void io_close_finish(struct io_wq_work **workptr)
2901{
2902 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2903 struct io_kiocb *nxt = NULL;
2904
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002905 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002906 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07002907 if (nxt)
2908 io_wq_assign_next(workptr, nxt);
2909}
2910
2911static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2912 bool force_nonblock)
2913{
2914 int ret;
2915
2916 req->close.put_file = NULL;
2917 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2918 if (ret < 0)
2919 return ret;
2920
2921 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002922 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002923 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002924
2925 /*
2926 * No ->flush(), safely close from here and just punt the
2927 * fput() to async context.
2928 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002929 __io_close_finish(req, nxt);
2930 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002931eagain:
2932 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002933 /*
2934 * Do manual async queue here to avoid grabbing files - we don't
2935 * need the files, and it'll cause io_close_finish() to close
2936 * the file again and cause a double CQE entry for this request
2937 */
2938 io_queue_async_work(req);
2939 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002940}
2941
Jens Axboe3529d8c2019-12-19 18:24:38 -07002942static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002943{
2944 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002945
2946 if (!req->file)
2947 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002948
2949 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2950 return -EINVAL;
2951 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2952 return -EINVAL;
2953
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002954 req->sync.off = READ_ONCE(sqe->off);
2955 req->sync.len = READ_ONCE(sqe->len);
2956 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002957 return 0;
2958}
2959
2960static void io_sync_file_range_finish(struct io_wq_work **workptr)
2961{
2962 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2963 struct io_kiocb *nxt = NULL;
2964 int ret;
2965
2966 if (io_req_cancelled(req))
2967 return;
2968
Jens Axboe9adbd452019-12-20 08:45:55 -07002969 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002970 req->sync.flags);
2971 if (ret < 0)
2972 req_set_fail_links(req);
2973 io_cqring_add_event(req, ret);
2974 io_put_req_find_next(req, &nxt);
2975 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002976 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002977}
2978
Jens Axboefc4df992019-12-10 14:38:45 -07002979static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002980 bool force_nonblock)
2981{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002982 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002983
2984 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002985 if (force_nonblock) {
2986 io_put_req(req);
2987 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002988 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002989 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002990
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002991 work = old_work = &req->work;
2992 io_sync_file_range_finish(&work);
2993 if (work && work != old_work)
2994 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002995 return 0;
2996}
2997
Jens Axboe3529d8c2019-12-19 18:24:38 -07002998static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002999{
Jens Axboe03b12302019-12-02 18:50:25 -07003000#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003001 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003002 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003003 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003004
Jens Axboee47293f2019-12-20 08:58:21 -07003005 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3006 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003007 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003008
Jens Axboefddafac2020-01-04 20:19:44 -07003009 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003010 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003011 /* iovec is already imported */
3012 if (req->flags & REQ_F_NEED_CLEANUP)
3013 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003014
Jens Axboed9688562019-12-09 19:35:20 -07003015 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003016 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003017 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003018 if (!ret)
3019 req->flags |= REQ_F_NEED_CLEANUP;
3020 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003021#else
Jens Axboee47293f2019-12-20 08:58:21 -07003022 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003023#endif
3024}
3025
Jens Axboefc4df992019-12-10 14:38:45 -07003026static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3027 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003028{
3029#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003030 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003031 struct socket *sock;
3032 int ret;
3033
3034 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3035 return -EINVAL;
3036
3037 sock = sock_from_file(req->file, &ret);
3038 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003039 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003040 unsigned flags;
3041
Jens Axboe03b12302019-12-02 18:50:25 -07003042 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003043 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003044 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003045 /* if iov is set, it's allocated already */
3046 if (!kmsg->iov)
3047 kmsg->iov = kmsg->fast_iov;
3048 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003049 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003050 struct io_sr_msg *sr = &req->sr_msg;
3051
Jens Axboe0b416c32019-12-15 10:57:46 -07003052 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003053 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003054
3055 io.msg.iov = io.msg.fast_iov;
3056 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3057 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003058 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003059 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003060 }
3061
Jens Axboee47293f2019-12-20 08:58:21 -07003062 flags = req->sr_msg.msg_flags;
3063 if (flags & MSG_DONTWAIT)
3064 req->flags |= REQ_F_NOWAIT;
3065 else if (force_nonblock)
3066 flags |= MSG_DONTWAIT;
3067
Jens Axboe0b416c32019-12-15 10:57:46 -07003068 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003069 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003070 if (req->io)
3071 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003072 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003073 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003074 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003075 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003076 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003077 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003078 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003079 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003080 }
3081 if (ret == -ERESTARTSYS)
3082 ret = -EINTR;
3083 }
3084
Pavel Begunkov1e950812020-02-06 19:51:16 +03003085 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003086 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003087 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003088 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003089 if (ret < 0)
3090 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003091 io_put_req_find_next(req, nxt);
3092 return 0;
3093#else
3094 return -EOPNOTSUPP;
3095#endif
3096}
3097
Jens Axboefddafac2020-01-04 20:19:44 -07003098static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3099 bool force_nonblock)
3100{
3101#if defined(CONFIG_NET)
3102 struct socket *sock;
3103 int ret;
3104
3105 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3106 return -EINVAL;
3107
3108 sock = sock_from_file(req->file, &ret);
3109 if (sock) {
3110 struct io_sr_msg *sr = &req->sr_msg;
3111 struct msghdr msg;
3112 struct iovec iov;
3113 unsigned flags;
3114
3115 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3116 &msg.msg_iter);
3117 if (ret)
3118 return ret;
3119
3120 msg.msg_name = NULL;
3121 msg.msg_control = NULL;
3122 msg.msg_controllen = 0;
3123 msg.msg_namelen = 0;
3124
3125 flags = req->sr_msg.msg_flags;
3126 if (flags & MSG_DONTWAIT)
3127 req->flags |= REQ_F_NOWAIT;
3128 else if (force_nonblock)
3129 flags |= MSG_DONTWAIT;
3130
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003131 msg.msg_flags = flags;
3132 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003133 if (force_nonblock && ret == -EAGAIN)
3134 return -EAGAIN;
3135 if (ret == -ERESTARTSYS)
3136 ret = -EINTR;
3137 }
3138
3139 io_cqring_add_event(req, ret);
3140 if (ret < 0)
3141 req_set_fail_links(req);
3142 io_put_req_find_next(req, nxt);
3143 return 0;
3144#else
3145 return -EOPNOTSUPP;
3146#endif
3147}
3148
Jens Axboe3529d8c2019-12-19 18:24:38 -07003149static int io_recvmsg_prep(struct io_kiocb *req,
3150 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003151{
3152#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003153 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003154 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003155 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003156
Jens Axboe3529d8c2019-12-19 18:24:38 -07003157 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3158 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003159 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003160
Jens Axboefddafac2020-01-04 20:19:44 -07003161 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003162 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003163 /* iovec is already imported */
3164 if (req->flags & REQ_F_NEED_CLEANUP)
3165 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003166
Jens Axboed9688562019-12-09 19:35:20 -07003167 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003168 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003169 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003170 if (!ret)
3171 req->flags |= REQ_F_NEED_CLEANUP;
3172 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003173#else
Jens Axboee47293f2019-12-20 08:58:21 -07003174 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003175#endif
3176}
3177
Jens Axboefc4df992019-12-10 14:38:45 -07003178static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3179 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003180{
3181#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003182 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003183 struct socket *sock;
3184 int ret;
3185
3186 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3187 return -EINVAL;
3188
3189 sock = sock_from_file(req->file, &ret);
3190 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003191 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003192 unsigned flags;
3193
Jens Axboe03b12302019-12-02 18:50:25 -07003194 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003195 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003196 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003197 /* if iov is set, it's allocated already */
3198 if (!kmsg->iov)
3199 kmsg->iov = kmsg->fast_iov;
3200 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003201 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003202 struct io_sr_msg *sr = &req->sr_msg;
3203
Jens Axboe0b416c32019-12-15 10:57:46 -07003204 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003205 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003206
3207 io.msg.iov = io.msg.fast_iov;
3208 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3209 sr->msg_flags, &io.msg.uaddr,
3210 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003211 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003212 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003213 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003214
Jens Axboee47293f2019-12-20 08:58:21 -07003215 flags = req->sr_msg.msg_flags;
3216 if (flags & MSG_DONTWAIT)
3217 req->flags |= REQ_F_NOWAIT;
3218 else if (force_nonblock)
3219 flags |= MSG_DONTWAIT;
3220
3221 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3222 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003223 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003224 if (req->io)
3225 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003226 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003227 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003228 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003229 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003230 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003231 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003232 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003233 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003234 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003235 if (ret == -ERESTARTSYS)
3236 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003237 }
3238
Pavel Begunkov1e950812020-02-06 19:51:16 +03003239 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003240 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003241 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003242 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003243 if (ret < 0)
3244 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003245 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003246 return 0;
3247#else
3248 return -EOPNOTSUPP;
3249#endif
3250}
3251
Jens Axboefddafac2020-01-04 20:19:44 -07003252static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3253 bool force_nonblock)
3254{
3255#if defined(CONFIG_NET)
3256 struct socket *sock;
3257 int ret;
3258
3259 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3260 return -EINVAL;
3261
3262 sock = sock_from_file(req->file, &ret);
3263 if (sock) {
3264 struct io_sr_msg *sr = &req->sr_msg;
3265 struct msghdr msg;
3266 struct iovec iov;
3267 unsigned flags;
3268
3269 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3270 &msg.msg_iter);
3271 if (ret)
3272 return ret;
3273
3274 msg.msg_name = NULL;
3275 msg.msg_control = NULL;
3276 msg.msg_controllen = 0;
3277 msg.msg_namelen = 0;
3278 msg.msg_iocb = NULL;
3279 msg.msg_flags = 0;
3280
3281 flags = req->sr_msg.msg_flags;
3282 if (flags & MSG_DONTWAIT)
3283 req->flags |= REQ_F_NOWAIT;
3284 else if (force_nonblock)
3285 flags |= MSG_DONTWAIT;
3286
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003287 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003288 if (force_nonblock && ret == -EAGAIN)
3289 return -EAGAIN;
3290 if (ret == -ERESTARTSYS)
3291 ret = -EINTR;
3292 }
3293
3294 io_cqring_add_event(req, ret);
3295 if (ret < 0)
3296 req_set_fail_links(req);
3297 io_put_req_find_next(req, nxt);
3298 return 0;
3299#else
3300 return -EOPNOTSUPP;
3301#endif
3302}
3303
3304
Jens Axboe3529d8c2019-12-19 18:24:38 -07003305static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003306{
3307#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003308 struct io_accept *accept = &req->accept;
3309
Jens Axboe17f2fe32019-10-17 14:42:58 -06003310 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3311 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003312 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003313 return -EINVAL;
3314
Jens Axboed55e5f52019-12-11 16:12:15 -07003315 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3316 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003317 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003318 return 0;
3319#else
3320 return -EOPNOTSUPP;
3321#endif
3322}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003323
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003324#if defined(CONFIG_NET)
3325static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3326 bool force_nonblock)
3327{
3328 struct io_accept *accept = &req->accept;
3329 unsigned file_flags;
3330 int ret;
3331
3332 file_flags = force_nonblock ? O_NONBLOCK : 0;
3333 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3334 accept->addr_len, accept->flags);
3335 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003336 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003337 if (ret == -ERESTARTSYS)
3338 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003339 if (ret < 0)
3340 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003341 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003342 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003343 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003344}
3345
3346static void io_accept_finish(struct io_wq_work **workptr)
3347{
3348 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3349 struct io_kiocb *nxt = NULL;
3350
3351 if (io_req_cancelled(req))
3352 return;
3353 __io_accept(req, &nxt, false);
3354 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003355 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003356}
3357#endif
3358
3359static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3360 bool force_nonblock)
3361{
3362#if defined(CONFIG_NET)
3363 int ret;
3364
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003365 ret = __io_accept(req, nxt, force_nonblock);
3366 if (ret == -EAGAIN && force_nonblock) {
3367 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003368 io_put_req(req);
3369 return -EAGAIN;
3370 }
3371 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003372#else
3373 return -EOPNOTSUPP;
3374#endif
3375}
3376
Jens Axboe3529d8c2019-12-19 18:24:38 -07003377static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003378{
3379#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003380 struct io_connect *conn = &req->connect;
3381 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003382
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003383 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3384 return -EINVAL;
3385 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3386 return -EINVAL;
3387
Jens Axboe3529d8c2019-12-19 18:24:38 -07003388 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3389 conn->addr_len = READ_ONCE(sqe->addr2);
3390
3391 if (!io)
3392 return 0;
3393
3394 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003395 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003396#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003397 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003398#endif
3399}
3400
Jens Axboefc4df992019-12-10 14:38:45 -07003401static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3402 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003403{
3404#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003405 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003406 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003407 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003408
Jens Axboef499a022019-12-02 16:28:46 -07003409 if (req->io) {
3410 io = req->io;
3411 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003412 ret = move_addr_to_kernel(req->connect.addr,
3413 req->connect.addr_len,
3414 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003415 if (ret)
3416 goto out;
3417 io = &__io;
3418 }
3419
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003420 file_flags = force_nonblock ? O_NONBLOCK : 0;
3421
3422 ret = __sys_connect_file(req->file, &io->connect.address,
3423 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003424 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003425 if (req->io)
3426 return -EAGAIN;
3427 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003428 ret = -ENOMEM;
3429 goto out;
3430 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003431 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003432 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003433 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003434 if (ret == -ERESTARTSYS)
3435 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003436out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003437 if (ret < 0)
3438 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003439 io_cqring_add_event(req, ret);
3440 io_put_req_find_next(req, nxt);
3441 return 0;
3442#else
3443 return -EOPNOTSUPP;
3444#endif
3445}
3446
Jens Axboe221c5eb2019-01-17 09:41:58 -07003447static void io_poll_remove_one(struct io_kiocb *req)
3448{
3449 struct io_poll_iocb *poll = &req->poll;
3450
3451 spin_lock(&poll->head->lock);
3452 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003453 if (!list_empty(&poll->wait.entry)) {
3454 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003455 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003456 }
3457 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003458 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003459}
3460
3461static void io_poll_remove_all(struct io_ring_ctx *ctx)
3462{
Jens Axboe78076bb2019-12-04 19:56:40 -07003463 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003464 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003465 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003466
3467 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003468 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3469 struct hlist_head *list;
3470
3471 list = &ctx->cancel_hash[i];
3472 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3473 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003474 }
3475 spin_unlock_irq(&ctx->completion_lock);
3476}
3477
Jens Axboe47f46762019-11-09 17:43:02 -07003478static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3479{
Jens Axboe78076bb2019-12-04 19:56:40 -07003480 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003481 struct io_kiocb *req;
3482
Jens Axboe78076bb2019-12-04 19:56:40 -07003483 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3484 hlist_for_each_entry(req, list, hash_node) {
3485 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003486 io_poll_remove_one(req);
3487 return 0;
3488 }
Jens Axboe47f46762019-11-09 17:43:02 -07003489 }
3490
3491 return -ENOENT;
3492}
3493
Jens Axboe3529d8c2019-12-19 18:24:38 -07003494static int io_poll_remove_prep(struct io_kiocb *req,
3495 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003496{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003497 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3498 return -EINVAL;
3499 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3500 sqe->poll_events)
3501 return -EINVAL;
3502
Jens Axboe0969e782019-12-17 18:40:57 -07003503 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003504 return 0;
3505}
3506
3507/*
3508 * Find a running poll command that matches one specified in sqe->addr,
3509 * and remove it if found.
3510 */
3511static int io_poll_remove(struct io_kiocb *req)
3512{
3513 struct io_ring_ctx *ctx = req->ctx;
3514 u64 addr;
3515 int ret;
3516
Jens Axboe0969e782019-12-17 18:40:57 -07003517 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003518 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003519 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003520 spin_unlock_irq(&ctx->completion_lock);
3521
Jens Axboe78e19bb2019-11-06 15:21:34 -07003522 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003523 if (ret < 0)
3524 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003525 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003526 return 0;
3527}
3528
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003529static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003530{
Jackie Liua197f662019-11-08 08:09:12 -07003531 struct io_ring_ctx *ctx = req->ctx;
3532
Jens Axboe8c838782019-03-12 15:48:16 -06003533 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003534 if (error)
3535 io_cqring_fill_event(req, error);
3536 else
3537 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003538 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003539}
3540
Jens Axboe561fb042019-10-24 07:25:42 -06003541static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003542{
Jens Axboe561fb042019-10-24 07:25:42 -06003543 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003544 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3545 struct io_poll_iocb *poll = &req->poll;
3546 struct poll_table_struct pt = { ._key = poll->events };
3547 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003548 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003549 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003550 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003551
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003552 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003553 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003554 ret = -ECANCELED;
3555 } else if (READ_ONCE(poll->canceled)) {
3556 ret = -ECANCELED;
3557 }
Jens Axboe561fb042019-10-24 07:25:42 -06003558
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003559 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003560 mask = vfs_poll(poll->file, &pt) & poll->events;
3561
3562 /*
3563 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3564 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3565 * synchronize with them. In the cancellation case the list_del_init
3566 * itself is not actually needed, but harmless so we keep it in to
3567 * avoid further branches in the fast path.
3568 */
3569 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003570 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003571 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003572 spin_unlock_irq(&ctx->completion_lock);
3573 return;
3574 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003575 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003576 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003577 spin_unlock_irq(&ctx->completion_lock);
3578
Jens Axboe8c838782019-03-12 15:48:16 -06003579 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003580
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003581 if (ret < 0)
3582 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003583 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003584 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003585 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003586}
3587
Jens Axboee94f1412019-12-19 12:06:02 -07003588static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3589{
Jens Axboee94f1412019-12-19 12:06:02 -07003590 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003591 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003592
Jens Axboec6ca97b302019-12-28 12:11:08 -07003593 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003594 spin_lock_irq(&ctx->completion_lock);
3595 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3596 hash_del(&req->hash_node);
3597 io_poll_complete(req, req->result, 0);
3598
Jens Axboe8237e042019-12-28 10:48:22 -07003599 if (refcount_dec_and_test(&req->refs) &&
3600 !io_req_multi_free(&rb, req)) {
3601 req->flags |= REQ_F_COMP_LOCKED;
3602 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003603 }
3604 }
3605 spin_unlock_irq(&ctx->completion_lock);
3606
3607 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003608 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003609}
3610
3611static void io_poll_flush(struct io_wq_work **workptr)
3612{
3613 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3614 struct llist_node *nodes;
3615
3616 nodes = llist_del_all(&req->ctx->poll_llist);
3617 if (nodes)
3618 __io_poll_flush(req->ctx, nodes);
3619}
3620
Jens Axboef0b493e2020-02-01 21:30:11 -07003621static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3622{
3623 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3624
3625 eventfd_signal(req->ctx->cq_ev_fd, 1);
3626 io_put_req(req);
3627}
3628
Jens Axboe221c5eb2019-01-17 09:41:58 -07003629static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3630 void *key)
3631{
Jens Axboee9444752019-11-26 15:02:04 -07003632 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003633 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3634 struct io_ring_ctx *ctx = req->ctx;
3635 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003636
3637 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003638 if (mask && !(mask & poll->events))
3639 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003640
Jens Axboe392edb42019-12-09 17:52:20 -07003641 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003642
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003643 /*
3644 * Run completion inline if we can. We're using trylock here because
3645 * we are violating the completion_lock -> poll wq lock ordering.
3646 * If we have a link timeout we're going to need the completion_lock
3647 * for finalizing the request, mark us as having grabbed that already.
3648 */
Jens Axboee94f1412019-12-19 12:06:02 -07003649 if (mask) {
3650 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003651
Jens Axboee94f1412019-12-19 12:06:02 -07003652 if (llist_empty(&ctx->poll_llist) &&
3653 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003654 bool trigger_ev;
3655
Jens Axboee94f1412019-12-19 12:06:02 -07003656 hash_del(&req->hash_node);
3657 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003658
Jens Axboef0b493e2020-02-01 21:30:11 -07003659 trigger_ev = io_should_trigger_evfd(ctx);
3660 if (trigger_ev && eventfd_signal_count()) {
3661 trigger_ev = false;
3662 req->work.func = io_poll_trigger_evfd;
3663 } else {
3664 req->flags |= REQ_F_COMP_LOCKED;
3665 io_put_req(req);
3666 req = NULL;
3667 }
3668 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3669 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003670 } else {
3671 req->result = mask;
3672 req->llist_node.next = NULL;
3673 /* if the list wasn't empty, we're done */
3674 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3675 req = NULL;
3676 else
3677 req->work.func = io_poll_flush;
3678 }
Jens Axboe8c838782019-03-12 15:48:16 -06003679 }
Jens Axboee94f1412019-12-19 12:06:02 -07003680 if (req)
3681 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003682
Jens Axboe221c5eb2019-01-17 09:41:58 -07003683 return 1;
3684}
3685
3686struct io_poll_table {
3687 struct poll_table_struct pt;
3688 struct io_kiocb *req;
3689 int error;
3690};
3691
3692static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3693 struct poll_table_struct *p)
3694{
3695 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3696
3697 if (unlikely(pt->req->poll.head)) {
3698 pt->error = -EINVAL;
3699 return;
3700 }
3701
3702 pt->error = 0;
3703 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003704 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003705}
3706
Jens Axboeeac406c2019-11-14 12:09:58 -07003707static void io_poll_req_insert(struct io_kiocb *req)
3708{
3709 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003710 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003711
Jens Axboe78076bb2019-12-04 19:56:40 -07003712 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3713 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003714}
3715
Jens Axboe3529d8c2019-12-19 18:24:38 -07003716static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003717{
3718 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003719 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003720
3721 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3722 return -EINVAL;
3723 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3724 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003725 if (!poll->file)
3726 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003727
Jens Axboe221c5eb2019-01-17 09:41:58 -07003728 events = READ_ONCE(sqe->poll_events);
3729 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003730 return 0;
3731}
3732
3733static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3734{
3735 struct io_poll_iocb *poll = &req->poll;
3736 struct io_ring_ctx *ctx = req->ctx;
3737 struct io_poll_table ipt;
3738 bool cancel = false;
3739 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003740
3741 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003742 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003743
Jens Axboe221c5eb2019-01-17 09:41:58 -07003744 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003745 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003746 poll->canceled = false;
3747
3748 ipt.pt._qproc = io_poll_queue_proc;
3749 ipt.pt._key = poll->events;
3750 ipt.req = req;
3751 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3752
3753 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003754 INIT_LIST_HEAD(&poll->wait.entry);
3755 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3756 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003757
Jens Axboe36703242019-07-25 10:20:18 -06003758 INIT_LIST_HEAD(&req->list);
3759
Jens Axboe221c5eb2019-01-17 09:41:58 -07003760 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003761
3762 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003763 if (likely(poll->head)) {
3764 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003765 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003766 if (ipt.error)
3767 cancel = true;
3768 ipt.error = 0;
3769 mask = 0;
3770 }
3771 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003772 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003773 else if (cancel)
3774 WRITE_ONCE(poll->canceled, true);
3775 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003776 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003777 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003778 }
Jens Axboe8c838782019-03-12 15:48:16 -06003779 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003780 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003781 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003782 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003783 spin_unlock_irq(&ctx->completion_lock);
3784
Jens Axboe8c838782019-03-12 15:48:16 -06003785 if (mask) {
3786 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003787 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003788 }
Jens Axboe8c838782019-03-12 15:48:16 -06003789 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003790}
3791
Jens Axboe5262f562019-09-17 12:26:57 -06003792static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3793{
Jens Axboead8a48a2019-11-15 08:49:11 -07003794 struct io_timeout_data *data = container_of(timer,
3795 struct io_timeout_data, timer);
3796 struct io_kiocb *req = data->req;
3797 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003798 unsigned long flags;
3799
Jens Axboe5262f562019-09-17 12:26:57 -06003800 atomic_inc(&ctx->cq_timeouts);
3801
3802 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003803 /*
Jens Axboe11365042019-10-16 09:08:32 -06003804 * We could be racing with timeout deletion. If the list is empty,
3805 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003806 */
Jens Axboe842f9612019-10-29 12:34:10 -06003807 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003808 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003809
Jens Axboe11365042019-10-16 09:08:32 -06003810 /*
3811 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003812 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003813 * pointer will be increased, otherwise other timeout reqs may
3814 * return in advance without waiting for enough wait_nr.
3815 */
3816 prev = req;
3817 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3818 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003819 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003820 }
Jens Axboe842f9612019-10-29 12:34:10 -06003821
Jens Axboe78e19bb2019-11-06 15:21:34 -07003822 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003823 io_commit_cqring(ctx);
3824 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3825
3826 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003827 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003828 io_put_req(req);
3829 return HRTIMER_NORESTART;
3830}
3831
Jens Axboe47f46762019-11-09 17:43:02 -07003832static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3833{
3834 struct io_kiocb *req;
3835 int ret = -ENOENT;
3836
3837 list_for_each_entry(req, &ctx->timeout_list, list) {
3838 if (user_data == req->user_data) {
3839 list_del_init(&req->list);
3840 ret = 0;
3841 break;
3842 }
3843 }
3844
3845 if (ret == -ENOENT)
3846 return ret;
3847
Jens Axboe2d283902019-12-04 11:08:05 -07003848 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003849 if (ret == -1)
3850 return -EALREADY;
3851
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003852 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003853 io_cqring_fill_event(req, -ECANCELED);
3854 io_put_req(req);
3855 return 0;
3856}
3857
Jens Axboe3529d8c2019-12-19 18:24:38 -07003858static int io_timeout_remove_prep(struct io_kiocb *req,
3859 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003860{
Jens Axboeb29472e2019-12-17 18:50:29 -07003861 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3862 return -EINVAL;
3863 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3864 return -EINVAL;
3865
3866 req->timeout.addr = READ_ONCE(sqe->addr);
3867 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3868 if (req->timeout.flags)
3869 return -EINVAL;
3870
Jens Axboeb29472e2019-12-17 18:50:29 -07003871 return 0;
3872}
3873
Jens Axboe11365042019-10-16 09:08:32 -06003874/*
3875 * Remove or update an existing timeout command
3876 */
Jens Axboefc4df992019-12-10 14:38:45 -07003877static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003878{
3879 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003880 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003881
Jens Axboe11365042019-10-16 09:08:32 -06003882 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003883 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003884
Jens Axboe47f46762019-11-09 17:43:02 -07003885 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003886 io_commit_cqring(ctx);
3887 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003888 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003889 if (ret < 0)
3890 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003891 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003892 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003893}
3894
Jens Axboe3529d8c2019-12-19 18:24:38 -07003895static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003896 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003897{
Jens Axboead8a48a2019-11-15 08:49:11 -07003898 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003899 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003900
Jens Axboead8a48a2019-11-15 08:49:11 -07003901 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003902 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003903 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003904 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003905 if (sqe->off && is_timeout_link)
3906 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003907 flags = READ_ONCE(sqe->timeout_flags);
3908 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003909 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003910
Jens Axboe26a61672019-12-20 09:02:01 -07003911 req->timeout.count = READ_ONCE(sqe->off);
3912
Jens Axboe3529d8c2019-12-19 18:24:38 -07003913 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003914 return -ENOMEM;
3915
3916 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003917 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003918 req->flags |= REQ_F_TIMEOUT;
3919
3920 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003921 return -EFAULT;
3922
Jens Axboe11365042019-10-16 09:08:32 -06003923 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003924 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003925 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003926 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003927
Jens Axboead8a48a2019-11-15 08:49:11 -07003928 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3929 return 0;
3930}
3931
Jens Axboefc4df992019-12-10 14:38:45 -07003932static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003933{
3934 unsigned count;
3935 struct io_ring_ctx *ctx = req->ctx;
3936 struct io_timeout_data *data;
3937 struct list_head *entry;
3938 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003939
Jens Axboe2d283902019-12-04 11:08:05 -07003940 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003941
Jens Axboe5262f562019-09-17 12:26:57 -06003942 /*
3943 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003944 * timeout event to be satisfied. If it isn't set, then this is
3945 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003946 */
Jens Axboe26a61672019-12-20 09:02:01 -07003947 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003948 if (!count) {
3949 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3950 spin_lock_irq(&ctx->completion_lock);
3951 entry = ctx->timeout_list.prev;
3952 goto add;
3953 }
Jens Axboe5262f562019-09-17 12:26:57 -06003954
3955 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003956 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003957
3958 /*
3959 * Insertion sort, ensuring the first entry in the list is always
3960 * the one we need first.
3961 */
Jens Axboe5262f562019-09-17 12:26:57 -06003962 spin_lock_irq(&ctx->completion_lock);
3963 list_for_each_prev(entry, &ctx->timeout_list) {
3964 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003965 unsigned nxt_sq_head;
3966 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003967 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003968
Jens Axboe93bd25b2019-11-11 23:34:31 -07003969 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3970 continue;
3971
yangerkun5da0fb12019-10-15 21:59:29 +08003972 /*
3973 * Since cached_sq_head + count - 1 can overflow, use type long
3974 * long to store it.
3975 */
3976 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003977 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3978 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003979
3980 /*
3981 * cached_sq_head may overflow, and it will never overflow twice
3982 * once there is some timeout req still be valid.
3983 */
3984 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003985 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003986
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003987 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003988 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003989
3990 /*
3991 * Sequence of reqs after the insert one and itself should
3992 * be adjusted because each timeout req consumes a slot.
3993 */
3994 span++;
3995 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003996 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003997 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003998add:
Jens Axboe5262f562019-09-17 12:26:57 -06003999 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004000 data->timer.function = io_timeout_fn;
4001 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004002 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004003 return 0;
4004}
4005
Jens Axboe62755e32019-10-28 21:49:21 -06004006static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004007{
Jens Axboe62755e32019-10-28 21:49:21 -06004008 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004009
Jens Axboe62755e32019-10-28 21:49:21 -06004010 return req->user_data == (unsigned long) data;
4011}
4012
Jens Axboee977d6d2019-11-05 12:39:45 -07004013static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004014{
Jens Axboe62755e32019-10-28 21:49:21 -06004015 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004016 int ret = 0;
4017
Jens Axboe62755e32019-10-28 21:49:21 -06004018 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4019 switch (cancel_ret) {
4020 case IO_WQ_CANCEL_OK:
4021 ret = 0;
4022 break;
4023 case IO_WQ_CANCEL_RUNNING:
4024 ret = -EALREADY;
4025 break;
4026 case IO_WQ_CANCEL_NOTFOUND:
4027 ret = -ENOENT;
4028 break;
4029 }
4030
Jens Axboee977d6d2019-11-05 12:39:45 -07004031 return ret;
4032}
4033
Jens Axboe47f46762019-11-09 17:43:02 -07004034static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4035 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004036 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004037{
4038 unsigned long flags;
4039 int ret;
4040
4041 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4042 if (ret != -ENOENT) {
4043 spin_lock_irqsave(&ctx->completion_lock, flags);
4044 goto done;
4045 }
4046
4047 spin_lock_irqsave(&ctx->completion_lock, flags);
4048 ret = io_timeout_cancel(ctx, sqe_addr);
4049 if (ret != -ENOENT)
4050 goto done;
4051 ret = io_poll_cancel(ctx, sqe_addr);
4052done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004053 if (!ret)
4054 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004055 io_cqring_fill_event(req, ret);
4056 io_commit_cqring(ctx);
4057 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4058 io_cqring_ev_posted(ctx);
4059
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004060 if (ret < 0)
4061 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004062 io_put_req_find_next(req, nxt);
4063}
4064
Jens Axboe3529d8c2019-12-19 18:24:38 -07004065static int io_async_cancel_prep(struct io_kiocb *req,
4066 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004067{
Jens Axboefbf23842019-12-17 18:45:56 -07004068 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004069 return -EINVAL;
4070 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4071 sqe->cancel_flags)
4072 return -EINVAL;
4073
Jens Axboefbf23842019-12-17 18:45:56 -07004074 req->cancel.addr = READ_ONCE(sqe->addr);
4075 return 0;
4076}
4077
4078static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4079{
4080 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004081
4082 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004083 return 0;
4084}
4085
Jens Axboe05f3fb32019-12-09 11:22:50 -07004086static int io_files_update_prep(struct io_kiocb *req,
4087 const struct io_uring_sqe *sqe)
4088{
4089 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4090 return -EINVAL;
4091
4092 req->files_update.offset = READ_ONCE(sqe->off);
4093 req->files_update.nr_args = READ_ONCE(sqe->len);
4094 if (!req->files_update.nr_args)
4095 return -EINVAL;
4096 req->files_update.arg = READ_ONCE(sqe->addr);
4097 return 0;
4098}
4099
4100static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4101{
4102 struct io_ring_ctx *ctx = req->ctx;
4103 struct io_uring_files_update up;
4104 int ret;
4105
Jens Axboef86cd202020-01-29 13:46:44 -07004106 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004107 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004108
4109 up.offset = req->files_update.offset;
4110 up.fds = req->files_update.arg;
4111
4112 mutex_lock(&ctx->uring_lock);
4113 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4114 mutex_unlock(&ctx->uring_lock);
4115
4116 if (ret < 0)
4117 req_set_fail_links(req);
4118 io_cqring_add_event(req, ret);
4119 io_put_req(req);
4120 return 0;
4121}
4122
Jens Axboe3529d8c2019-12-19 18:24:38 -07004123static int io_req_defer_prep(struct io_kiocb *req,
4124 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004125{
Jens Axboee7815732019-12-17 19:45:06 -07004126 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004127
Jens Axboef86cd202020-01-29 13:46:44 -07004128 if (io_op_defs[req->opcode].file_table) {
4129 ret = io_grab_files(req);
4130 if (unlikely(ret))
4131 return ret;
4132 }
4133
Jens Axboecccf0ee2020-01-27 16:34:48 -07004134 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4135
Jens Axboed625c6e2019-12-17 19:53:05 -07004136 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004137 case IORING_OP_NOP:
4138 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004139 case IORING_OP_READV:
4140 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004141 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004142 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004143 break;
4144 case IORING_OP_WRITEV:
4145 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004146 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004147 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004148 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004149 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004150 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004151 break;
4152 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004153 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004154 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004155 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004156 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004157 break;
4158 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004159 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004160 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004161 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004162 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004163 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004164 break;
4165 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004166 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004167 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004168 break;
Jens Axboef499a022019-12-02 16:28:46 -07004169 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004170 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004171 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004172 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004173 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004174 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004175 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004176 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004177 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004178 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004179 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004180 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004181 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004182 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004183 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004184 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004185 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004186 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004187 case IORING_OP_FALLOCATE:
4188 ret = io_fallocate_prep(req, sqe);
4189 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004190 case IORING_OP_OPENAT:
4191 ret = io_openat_prep(req, sqe);
4192 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004193 case IORING_OP_CLOSE:
4194 ret = io_close_prep(req, sqe);
4195 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004196 case IORING_OP_FILES_UPDATE:
4197 ret = io_files_update_prep(req, sqe);
4198 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004199 case IORING_OP_STATX:
4200 ret = io_statx_prep(req, sqe);
4201 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004202 case IORING_OP_FADVISE:
4203 ret = io_fadvise_prep(req, sqe);
4204 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004205 case IORING_OP_MADVISE:
4206 ret = io_madvise_prep(req, sqe);
4207 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004208 case IORING_OP_OPENAT2:
4209 ret = io_openat2_prep(req, sqe);
4210 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004211 case IORING_OP_EPOLL_CTL:
4212 ret = io_epoll_ctl_prep(req, sqe);
4213 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004214 default:
Jens Axboee7815732019-12-17 19:45:06 -07004215 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4216 req->opcode);
4217 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004218 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004219 }
4220
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004221 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004222}
4223
Jens Axboe3529d8c2019-12-19 18:24:38 -07004224static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004225{
Jackie Liua197f662019-11-08 08:09:12 -07004226 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004227 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004228
Bob Liu9d858b22019-11-13 18:06:25 +08004229 /* Still need defer if there is pending req in defer list. */
4230 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004231 return 0;
4232
Jens Axboe3529d8c2019-12-19 18:24:38 -07004233 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004234 return -EAGAIN;
4235
Jens Axboe3529d8c2019-12-19 18:24:38 -07004236 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004237 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004238 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004239
Jens Axboede0617e2019-04-06 21:51:27 -06004240 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004241 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004242 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004243 return 0;
4244 }
4245
Jens Axboe915967f2019-11-21 09:01:20 -07004246 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004247 list_add_tail(&req->list, &ctx->defer_list);
4248 spin_unlock_irq(&ctx->completion_lock);
4249 return -EIOCBQUEUED;
4250}
4251
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004252static void io_cleanup_req(struct io_kiocb *req)
4253{
4254 struct io_async_ctx *io = req->io;
4255
4256 switch (req->opcode) {
4257 case IORING_OP_READV:
4258 case IORING_OP_READ_FIXED:
4259 case IORING_OP_READ:
4260 case IORING_OP_WRITEV:
4261 case IORING_OP_WRITE_FIXED:
4262 case IORING_OP_WRITE:
4263 if (io->rw.iov != io->rw.fast_iov)
4264 kfree(io->rw.iov);
4265 break;
4266 case IORING_OP_SENDMSG:
4267 case IORING_OP_RECVMSG:
4268 if (io->msg.iov != io->msg.fast_iov)
4269 kfree(io->msg.iov);
4270 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004271 case IORING_OP_OPENAT:
4272 case IORING_OP_OPENAT2:
4273 case IORING_OP_STATX:
4274 putname(req->open.filename);
4275 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004276 }
4277
4278 req->flags &= ~REQ_F_NEED_CLEANUP;
4279}
4280
Jens Axboe3529d8c2019-12-19 18:24:38 -07004281static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4282 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004283{
Jackie Liua197f662019-11-08 08:09:12 -07004284 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004285 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004286
Jens Axboed625c6e2019-12-17 19:53:05 -07004287 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004288 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004289 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004290 break;
4291 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004292 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004293 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004294 if (sqe) {
4295 ret = io_read_prep(req, sqe, force_nonblock);
4296 if (ret < 0)
4297 break;
4298 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004299 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004300 break;
4301 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004302 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004303 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004304 if (sqe) {
4305 ret = io_write_prep(req, sqe, force_nonblock);
4306 if (ret < 0)
4307 break;
4308 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004309 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004310 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004311 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004312 if (sqe) {
4313 ret = io_prep_fsync(req, sqe);
4314 if (ret < 0)
4315 break;
4316 }
Jens Axboefc4df992019-12-10 14:38:45 -07004317 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004318 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004319 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004320 if (sqe) {
4321 ret = io_poll_add_prep(req, sqe);
4322 if (ret)
4323 break;
4324 }
Jens Axboefc4df992019-12-10 14:38:45 -07004325 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004326 break;
4327 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004328 if (sqe) {
4329 ret = io_poll_remove_prep(req, sqe);
4330 if (ret < 0)
4331 break;
4332 }
Jens Axboefc4df992019-12-10 14:38:45 -07004333 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004334 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004335 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004336 if (sqe) {
4337 ret = io_prep_sfr(req, sqe);
4338 if (ret < 0)
4339 break;
4340 }
Jens Axboefc4df992019-12-10 14:38:45 -07004341 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004342 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004343 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004344 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004345 if (sqe) {
4346 ret = io_sendmsg_prep(req, sqe);
4347 if (ret < 0)
4348 break;
4349 }
Jens Axboefddafac2020-01-04 20:19:44 -07004350 if (req->opcode == IORING_OP_SENDMSG)
4351 ret = io_sendmsg(req, nxt, force_nonblock);
4352 else
4353 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004354 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004355 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004356 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004357 if (sqe) {
4358 ret = io_recvmsg_prep(req, sqe);
4359 if (ret)
4360 break;
4361 }
Jens Axboefddafac2020-01-04 20:19:44 -07004362 if (req->opcode == IORING_OP_RECVMSG)
4363 ret = io_recvmsg(req, nxt, force_nonblock);
4364 else
4365 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004366 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004367 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004368 if (sqe) {
4369 ret = io_timeout_prep(req, sqe, false);
4370 if (ret)
4371 break;
4372 }
Jens Axboefc4df992019-12-10 14:38:45 -07004373 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004374 break;
Jens Axboe11365042019-10-16 09:08:32 -06004375 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004376 if (sqe) {
4377 ret = io_timeout_remove_prep(req, sqe);
4378 if (ret)
4379 break;
4380 }
Jens Axboefc4df992019-12-10 14:38:45 -07004381 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004382 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004383 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004384 if (sqe) {
4385 ret = io_accept_prep(req, sqe);
4386 if (ret)
4387 break;
4388 }
Jens Axboefc4df992019-12-10 14:38:45 -07004389 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004390 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004391 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004392 if (sqe) {
4393 ret = io_connect_prep(req, sqe);
4394 if (ret)
4395 break;
4396 }
Jens Axboefc4df992019-12-10 14:38:45 -07004397 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004398 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004399 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004400 if (sqe) {
4401 ret = io_async_cancel_prep(req, sqe);
4402 if (ret)
4403 break;
4404 }
Jens Axboefc4df992019-12-10 14:38:45 -07004405 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004406 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004407 case IORING_OP_FALLOCATE:
4408 if (sqe) {
4409 ret = io_fallocate_prep(req, sqe);
4410 if (ret)
4411 break;
4412 }
4413 ret = io_fallocate(req, nxt, force_nonblock);
4414 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004415 case IORING_OP_OPENAT:
4416 if (sqe) {
4417 ret = io_openat_prep(req, sqe);
4418 if (ret)
4419 break;
4420 }
4421 ret = io_openat(req, nxt, force_nonblock);
4422 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004423 case IORING_OP_CLOSE:
4424 if (sqe) {
4425 ret = io_close_prep(req, sqe);
4426 if (ret)
4427 break;
4428 }
4429 ret = io_close(req, nxt, force_nonblock);
4430 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004431 case IORING_OP_FILES_UPDATE:
4432 if (sqe) {
4433 ret = io_files_update_prep(req, sqe);
4434 if (ret)
4435 break;
4436 }
4437 ret = io_files_update(req, force_nonblock);
4438 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004439 case IORING_OP_STATX:
4440 if (sqe) {
4441 ret = io_statx_prep(req, sqe);
4442 if (ret)
4443 break;
4444 }
4445 ret = io_statx(req, nxt, force_nonblock);
4446 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004447 case IORING_OP_FADVISE:
4448 if (sqe) {
4449 ret = io_fadvise_prep(req, sqe);
4450 if (ret)
4451 break;
4452 }
4453 ret = io_fadvise(req, nxt, force_nonblock);
4454 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004455 case IORING_OP_MADVISE:
4456 if (sqe) {
4457 ret = io_madvise_prep(req, sqe);
4458 if (ret)
4459 break;
4460 }
4461 ret = io_madvise(req, nxt, force_nonblock);
4462 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004463 case IORING_OP_OPENAT2:
4464 if (sqe) {
4465 ret = io_openat2_prep(req, sqe);
4466 if (ret)
4467 break;
4468 }
4469 ret = io_openat2(req, nxt, force_nonblock);
4470 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004471 case IORING_OP_EPOLL_CTL:
4472 if (sqe) {
4473 ret = io_epoll_ctl_prep(req, sqe);
4474 if (ret)
4475 break;
4476 }
4477 ret = io_epoll_ctl(req, nxt, force_nonblock);
4478 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004479 default:
4480 ret = -EINVAL;
4481 break;
4482 }
4483
Jens Axboedef596e2019-01-09 08:59:42 -07004484 if (ret)
4485 return ret;
4486
4487 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004488 const bool in_async = io_wq_current_is_worker();
4489
Jens Axboe9e645e112019-05-10 16:07:28 -06004490 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004491 return -EAGAIN;
4492
Jens Axboe11ba8202020-01-15 21:51:17 -07004493 /* workqueue context doesn't hold uring_lock, grab it now */
4494 if (in_async)
4495 mutex_lock(&ctx->uring_lock);
4496
Jens Axboedef596e2019-01-09 08:59:42 -07004497 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004498
4499 if (in_async)
4500 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004501 }
4502
4503 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004504}
4505
Jens Axboe561fb042019-10-24 07:25:42 -06004506static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004507{
Jens Axboe561fb042019-10-24 07:25:42 -06004508 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004509 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004510 struct io_kiocb *nxt = NULL;
4511 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004512
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004513 /* if NO_CANCEL is set, we must still run the work */
4514 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4515 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004516 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004517 }
Jens Axboe31b51512019-01-18 22:56:34 -07004518
Jens Axboe561fb042019-10-24 07:25:42 -06004519 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004520 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004521 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004522 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004523 /*
4524 * We can get EAGAIN for polled IO even though we're
4525 * forcing a sync submission from here, since we can't
4526 * wait for request slots on the block side.
4527 */
4528 if (ret != -EAGAIN)
4529 break;
4530 cond_resched();
4531 } while (1);
4532 }
Jens Axboe31b51512019-01-18 22:56:34 -07004533
Jens Axboe561fb042019-10-24 07:25:42 -06004534 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004535 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004536
Jens Axboe561fb042019-10-24 07:25:42 -06004537 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004538 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004539 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004540 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004541 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004542
Jens Axboe561fb042019-10-24 07:25:42 -06004543 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004544 if (!ret && nxt)
4545 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004546}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004547
Jens Axboe15b71ab2019-12-11 11:20:36 -07004548static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004549{
Jens Axboed3656342019-12-18 09:50:26 -07004550 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004551 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004552 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004553 return 0;
4554 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004555}
4556
Jens Axboe65e19f52019-10-26 07:20:21 -06004557static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4558 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004559{
Jens Axboe65e19f52019-10-26 07:20:21 -06004560 struct fixed_file_table *table;
4561
Jens Axboe05f3fb32019-12-09 11:22:50 -07004562 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4563 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004564}
4565
Jens Axboe3529d8c2019-12-19 18:24:38 -07004566static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4567 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004568{
Jackie Liua197f662019-11-08 08:09:12 -07004569 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004570 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004571 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004572
Jens Axboe3529d8c2019-12-19 18:24:38 -07004573 flags = READ_ONCE(sqe->flags);
4574 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004575
Jens Axboed3656342019-12-18 09:50:26 -07004576 if (!io_req_needs_file(req, fd))
4577 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004578
4579 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004580 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004581 (unsigned) fd >= ctx->nr_user_files))
4582 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004583 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004584 req->file = io_file_from_index(ctx, fd);
4585 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004586 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004587 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004588 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004589 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004590 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004591 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004592 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004593 req->file = io_file_get(state, fd);
4594 if (unlikely(!req->file))
4595 return -EBADF;
4596 }
4597
4598 return 0;
4599}
4600
Jackie Liua197f662019-11-08 08:09:12 -07004601static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004602{
Jens Axboefcb323c2019-10-24 12:39:47 -06004603 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004604 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004605
Jens Axboef86cd202020-01-29 13:46:44 -07004606 if (req->work.files)
4607 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004608 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004609 return -EBADF;
4610
Jens Axboefcb323c2019-10-24 12:39:47 -06004611 rcu_read_lock();
4612 spin_lock_irq(&ctx->inflight_lock);
4613 /*
4614 * We use the f_ops->flush() handler to ensure that we can flush
4615 * out work accessing these files if the fd is closed. Check if
4616 * the fd has changed since we started down this path, and disallow
4617 * this operation if it has.
4618 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004619 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004620 list_add(&req->inflight_entry, &ctx->inflight_list);
4621 req->flags |= REQ_F_INFLIGHT;
4622 req->work.files = current->files;
4623 ret = 0;
4624 }
4625 spin_unlock_irq(&ctx->inflight_lock);
4626 rcu_read_unlock();
4627
4628 return ret;
4629}
4630
Jens Axboe2665abf2019-11-05 12:40:47 -07004631static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4632{
Jens Axboead8a48a2019-11-15 08:49:11 -07004633 struct io_timeout_data *data = container_of(timer,
4634 struct io_timeout_data, timer);
4635 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004636 struct io_ring_ctx *ctx = req->ctx;
4637 struct io_kiocb *prev = NULL;
4638 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004639
4640 spin_lock_irqsave(&ctx->completion_lock, flags);
4641
4642 /*
4643 * We don't expect the list to be empty, that will only happen if we
4644 * race with the completion of the linked work.
4645 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004646 if (!list_empty(&req->link_list)) {
4647 prev = list_entry(req->link_list.prev, struct io_kiocb,
4648 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004649 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004650 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004651 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4652 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004653 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004654 }
4655
4656 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4657
4658 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004659 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004660 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4661 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004662 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004663 } else {
4664 io_cqring_add_event(req, -ETIME);
4665 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004666 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004667 return HRTIMER_NORESTART;
4668}
4669
Jens Axboead8a48a2019-11-15 08:49:11 -07004670static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004671{
Jens Axboe76a46e02019-11-10 23:34:16 -07004672 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004673
Jens Axboe76a46e02019-11-10 23:34:16 -07004674 /*
4675 * If the list is now empty, then our linked request finished before
4676 * we got a chance to setup the timer
4677 */
4678 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004679 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004680 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004681
Jens Axboead8a48a2019-11-15 08:49:11 -07004682 data->timer.function = io_link_timeout_fn;
4683 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4684 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004685 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004686 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004687
Jens Axboe2665abf2019-11-05 12:40:47 -07004688 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004689 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004690}
4691
Jens Axboead8a48a2019-11-15 08:49:11 -07004692static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004693{
4694 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004695
Jens Axboe2665abf2019-11-05 12:40:47 -07004696 if (!(req->flags & REQ_F_LINK))
4697 return NULL;
4698
Pavel Begunkov44932332019-12-05 16:16:35 +03004699 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4700 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004701 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004702 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004703
Jens Axboe76a46e02019-11-10 23:34:16 -07004704 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004705 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004706}
4707
Jens Axboe3529d8c2019-12-19 18:24:38 -07004708static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004709{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004710 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004711 struct io_kiocb *nxt = NULL;
Jens Axboe193155c2020-02-22 23:22:19 -07004712 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004713 int ret;
4714
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004715again:
4716 linked_timeout = io_prep_linked_timeout(req);
4717
Jens Axboe193155c2020-02-22 23:22:19 -07004718 if (req->work.creds && req->work.creds != current_cred()) {
4719 if (old_creds)
4720 revert_creds(old_creds);
4721 if (old_creds == req->work.creds)
4722 old_creds = NULL; /* restored original creds */
4723 else
4724 old_creds = override_creds(req->work.creds);
4725 }
4726
Jens Axboe3529d8c2019-12-19 18:24:38 -07004727 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004728
4729 /*
4730 * We async punt it if the file wasn't marked NOWAIT, or if the file
4731 * doesn't support non-blocking read/write attempts
4732 */
4733 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4734 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004735punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004736 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004737 ret = io_grab_files(req);
4738 if (ret)
4739 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004740 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004741
4742 /*
4743 * Queued up for async execution, worker will release
4744 * submit reference when the iocb is actually submitted.
4745 */
4746 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004747 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004748 }
Jens Axboee65ef562019-03-12 10:16:44 -06004749
Jens Axboefcb323c2019-10-24 12:39:47 -06004750err:
Jens Axboee65ef562019-03-12 10:16:44 -06004751 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004752 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004753
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004754 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004755 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004756 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004757 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004758 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004759 }
4760
Jens Axboee65ef562019-03-12 10:16:44 -06004761 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004762 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004763 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004764 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004765 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004766 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004767done_req:
4768 if (nxt) {
4769 req = nxt;
4770 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004771
4772 if (req->flags & REQ_F_FORCE_ASYNC)
4773 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004774 goto again;
4775 }
Jens Axboe193155c2020-02-22 23:22:19 -07004776 if (old_creds)
4777 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004778}
4779
Jens Axboe3529d8c2019-12-19 18:24:38 -07004780static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004781{
4782 int ret;
4783
Jens Axboe3529d8c2019-12-19 18:24:38 -07004784 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004785 if (ret) {
4786 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004787fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004788 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004789 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004790 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004791 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004792 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004793 ret = io_req_defer_prep(req, sqe);
4794 if (unlikely(ret < 0))
4795 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004796 /*
4797 * Never try inline submit of IOSQE_ASYNC is set, go straight
4798 * to async execution.
4799 */
4800 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4801 io_queue_async_work(req);
4802 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004803 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004804 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004805}
4806
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004807static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004808{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004809 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004810 io_cqring_add_event(req, -ECANCELED);
4811 io_double_put_req(req);
4812 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004813 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004814}
4815
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004816#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004817 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004818
Jens Axboe3529d8c2019-12-19 18:24:38 -07004819static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4820 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004821{
Jackie Liua197f662019-11-08 08:09:12 -07004822 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004823 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004824 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004825
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004826 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004827
4828 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004829 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004830 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004831 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004832 }
4833
Jens Axboe75c6a032020-01-28 10:15:23 -07004834 id = READ_ONCE(sqe->personality);
4835 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07004836 req->work.creds = idr_find(&ctx->personality_idr, id);
4837 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07004838 ret = -EINVAL;
4839 goto err_req;
4840 }
Jens Axboe193155c2020-02-22 23:22:19 -07004841 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07004842 }
4843
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004844 /* same numerical values with corresponding REQ_F_*, safe to copy */
4845 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4846 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004847
Jens Axboe3529d8c2019-12-19 18:24:38 -07004848 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004849 if (unlikely(ret)) {
4850err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004851 io_cqring_add_event(req, ret);
4852 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004853 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004854 }
4855
Jens Axboe9e645e112019-05-10 16:07:28 -06004856 /*
4857 * If we already have a head request, queue this one for async
4858 * submittal once the head completes. If we don't have a head but
4859 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4860 * submitted sync once the chain is complete. If none of those
4861 * conditions are true (normal request), then just queue it.
4862 */
4863 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004864 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004865
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004866 /*
4867 * Taking sequential execution of a link, draining both sides
4868 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4869 * requests in the link. So, it drains the head and the
4870 * next after the link request. The last one is done via
4871 * drain_next flag to persist the effect across calls.
4872 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004873 if (sqe_flags & IOSQE_IO_DRAIN) {
4874 head->flags |= REQ_F_IO_DRAIN;
4875 ctx->drain_next = 1;
4876 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004877 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004878 ret = -EAGAIN;
4879 goto err_req;
4880 }
4881
Jens Axboe3529d8c2019-12-19 18:24:38 -07004882 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004883 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004884 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004885 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004886 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004887 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004888 trace_io_uring_link(ctx, req, head);
4889 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004890
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004891 /* last request of a link, enqueue the link */
4892 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4893 io_queue_link_head(head);
4894 *link = NULL;
4895 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004896 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004897 if (unlikely(ctx->drain_next)) {
4898 req->flags |= REQ_F_IO_DRAIN;
4899 req->ctx->drain_next = 0;
4900 }
4901 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4902 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004903 INIT_LIST_HEAD(&req->link_list);
4904 ret = io_req_defer_prep(req, sqe);
4905 if (ret)
4906 req->flags |= REQ_F_FAIL_LINK;
4907 *link = req;
4908 } else {
4909 io_queue_sqe(req, sqe);
4910 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004911 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004912
4913 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004914}
4915
Jens Axboe9a56a232019-01-09 09:06:50 -07004916/*
4917 * Batched submission is done, ensure local IO is flushed out.
4918 */
4919static void io_submit_state_end(struct io_submit_state *state)
4920{
4921 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004922 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004923 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004924 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004925}
4926
4927/*
4928 * Start submission side cache.
4929 */
4930static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004931 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004932{
4933 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004934 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004935 state->file = NULL;
4936 state->ios_left = max_ios;
4937}
4938
Jens Axboe2b188cc2019-01-07 10:46:33 -07004939static void io_commit_sqring(struct io_ring_ctx *ctx)
4940{
Hristo Venev75b28af2019-08-26 17:23:46 +00004941 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004942
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004943 /*
4944 * Ensure any loads from the SQEs are done at this point,
4945 * since once we write the new head, the application could
4946 * write new data to them.
4947 */
4948 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004949}
4950
4951/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004952 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004953 * that is mapped by userspace. This means that care needs to be taken to
4954 * ensure that reads are stable, as we cannot rely on userspace always
4955 * being a good citizen. If members of the sqe are validated and then later
4956 * used, it's important that those reads are done through READ_ONCE() to
4957 * prevent a re-load down the line.
4958 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004959static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4960 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004961{
Hristo Venev75b28af2019-08-26 17:23:46 +00004962 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004963 unsigned head;
4964
4965 /*
4966 * The cached sq head (or cq tail) serves two purposes:
4967 *
4968 * 1) allows us to batch the cost of updating the user visible
4969 * head updates.
4970 * 2) allows the kernel side to track the head on its own, even
4971 * though the application is the one updating it.
4972 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004973 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004974 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004975 /*
4976 * All io need record the previous position, if LINK vs DARIN,
4977 * it can be used to mark the position of the first IO in the
4978 * link list.
4979 */
4980 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004981 *sqe_ptr = &ctx->sq_sqes[head];
4982 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4983 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004984 ctx->cached_sq_head++;
4985 return true;
4986 }
4987
4988 /* drop invalid entries */
4989 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004990 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004991 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004992 return false;
4993}
4994
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004995static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004996 struct file *ring_file, int ring_fd,
4997 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004998{
4999 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005000 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005001 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005002 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005003
Jens Axboec4a2ed72019-11-21 21:01:26 -07005004 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005005 if (test_bit(0, &ctx->sq_check_overflow)) {
5006 if (!list_empty(&ctx->cq_overflow_list) &&
5007 !io_cqring_overflow_flush(ctx, false))
5008 return -EBUSY;
5009 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005010
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005011 /* make sure SQ entry isn't read before tail */
5012 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005013
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005014 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5015 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005016
5017 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005018 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005019 statep = &state;
5020 }
5021
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005022 ctx->ring_fd = ring_fd;
5023 ctx->ring_file = ring_file;
5024
Jens Axboe6c271ce2019-01-10 11:22:30 -07005025 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005026 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005027 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005028 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005029
Pavel Begunkov196be952019-11-07 01:41:06 +03005030 req = io_get_req(ctx, statep);
5031 if (unlikely(!req)) {
5032 if (!submitted)
5033 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005034 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005035 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005036 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005037 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005038 break;
5039 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005040
Jens Axboed3656342019-12-18 09:50:26 -07005041 /* will complete beyond this point, count as submitted */
5042 submitted++;
5043
5044 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005045 err = -EINVAL;
5046fail_req:
5047 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005048 io_double_put_req(req);
5049 break;
5050 }
5051
5052 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005053 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005054 if (unlikely(mm_fault)) {
5055 err = -EFAULT;
5056 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005057 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005058 use_mm(ctx->sqo_mm);
5059 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005060 }
5061
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005062 req->in_async = async;
5063 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005064 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5065 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005066 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005067 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005068 }
5069
Pavel Begunkov9466f432020-01-25 22:34:01 +03005070 if (unlikely(submitted != nr)) {
5071 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5072
5073 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5074 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005075 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005076 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005077 if (statep)
5078 io_submit_state_end(&state);
5079
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005080 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5081 io_commit_sqring(ctx);
5082
Jens Axboe6c271ce2019-01-10 11:22:30 -07005083 return submitted;
5084}
5085
5086static int io_sq_thread(void *data)
5087{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005088 struct io_ring_ctx *ctx = data;
5089 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005090 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005091 mm_segment_t old_fs;
5092 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005093 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005094 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005095
Jens Axboe206aefd2019-11-07 18:27:42 -07005096 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005097
Jens Axboe6c271ce2019-01-10 11:22:30 -07005098 old_fs = get_fs();
5099 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005100 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005101
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005102 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005103 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005104 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005105
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005106 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005107 unsigned nr_events = 0;
5108
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005109 mutex_lock(&ctx->uring_lock);
5110 if (!list_empty(&ctx->poll_list))
5111 io_iopoll_getevents(ctx, &nr_events, 0);
5112 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005113 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005114 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005115 }
5116
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005117 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005118
5119 /*
5120 * If submit got -EBUSY, flag us as needing the application
5121 * to enter the kernel to reap and flush events.
5122 */
5123 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005124 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005125 * Drop cur_mm before scheduling, we can't hold it for
5126 * long periods (or over schedule()). Do this before
5127 * adding ourselves to the waitqueue, as the unuse/drop
5128 * may sleep.
5129 */
5130 if (cur_mm) {
5131 unuse_mm(cur_mm);
5132 mmput(cur_mm);
5133 cur_mm = NULL;
5134 }
5135
5136 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005137 * We're polling. If we're within the defined idle
5138 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005139 * to sleep. The exception is if we got EBUSY doing
5140 * more IO, we should wait for the application to
5141 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005142 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005143 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005144 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5145 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005146 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005147 continue;
5148 }
5149
Jens Axboe6c271ce2019-01-10 11:22:30 -07005150 prepare_to_wait(&ctx->sqo_wait, &wait,
5151 TASK_INTERRUPTIBLE);
5152
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005153 /*
5154 * While doing polled IO, before going to sleep, we need
5155 * to check if there are new reqs added to poll_list, it
5156 * is because reqs may have been punted to io worker and
5157 * will be added to poll_list later, hence check the
5158 * poll_list again.
5159 */
5160 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5161 !list_empty_careful(&ctx->poll_list)) {
5162 finish_wait(&ctx->sqo_wait, &wait);
5163 continue;
5164 }
5165
Jens Axboe6c271ce2019-01-10 11:22:30 -07005166 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005167 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005168 /* make sure to read SQ tail after writing flags */
5169 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005170
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005171 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005172 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005173 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005174 finish_wait(&ctx->sqo_wait, &wait);
5175 break;
5176 }
5177 if (signal_pending(current))
5178 flush_signals(current);
5179 schedule();
5180 finish_wait(&ctx->sqo_wait, &wait);
5181
Hristo Venev75b28af2019-08-26 17:23:46 +00005182 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005183 continue;
5184 }
5185 finish_wait(&ctx->sqo_wait, &wait);
5186
Hristo Venev75b28af2019-08-26 17:23:46 +00005187 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005188 }
5189
Jens Axboe8a4955f2019-12-09 14:52:35 -07005190 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005191 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005192 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005193 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005194 }
5195
5196 set_fs(old_fs);
5197 if (cur_mm) {
5198 unuse_mm(cur_mm);
5199 mmput(cur_mm);
5200 }
Jens Axboe181e4482019-11-25 08:52:30 -07005201 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005202
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005203 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005204
Jens Axboe6c271ce2019-01-10 11:22:30 -07005205 return 0;
5206}
5207
Jens Axboebda52162019-09-24 13:47:15 -06005208struct io_wait_queue {
5209 struct wait_queue_entry wq;
5210 struct io_ring_ctx *ctx;
5211 unsigned to_wait;
5212 unsigned nr_timeouts;
5213};
5214
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005215static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005216{
5217 struct io_ring_ctx *ctx = iowq->ctx;
5218
5219 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005220 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005221 * started waiting. For timeouts, we always want to return to userspace,
5222 * regardless of event count.
5223 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005224 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005225 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5226}
5227
5228static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5229 int wake_flags, void *key)
5230{
5231 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5232 wq);
5233
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005234 /* use noflush == true, as we can't safely rely on locking context */
5235 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005236 return -1;
5237
5238 return autoremove_wake_function(curr, mode, wake_flags, key);
5239}
5240
Jens Axboe2b188cc2019-01-07 10:46:33 -07005241/*
5242 * Wait until events become available, if we don't already have some. The
5243 * application must reap them itself, as they reside on the shared cq ring.
5244 */
5245static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5246 const sigset_t __user *sig, size_t sigsz)
5247{
Jens Axboebda52162019-09-24 13:47:15 -06005248 struct io_wait_queue iowq = {
5249 .wq = {
5250 .private = current,
5251 .func = io_wake_function,
5252 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5253 },
5254 .ctx = ctx,
5255 .to_wait = min_events,
5256 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005257 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005258 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005259
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005260 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005261 return 0;
5262
5263 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005264#ifdef CONFIG_COMPAT
5265 if (in_compat_syscall())
5266 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005267 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005268 else
5269#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005270 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005271
Jens Axboe2b188cc2019-01-07 10:46:33 -07005272 if (ret)
5273 return ret;
5274 }
5275
Jens Axboebda52162019-09-24 13:47:15 -06005276 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005277 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005278 do {
5279 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5280 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005281 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005282 break;
5283 schedule();
5284 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005285 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005286 break;
5287 }
5288 } while (1);
5289 finish_wait(&ctx->wait, &iowq.wq);
5290
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005291 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005292
Hristo Venev75b28af2019-08-26 17:23:46 +00005293 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005294}
5295
Jens Axboe6b063142019-01-10 22:13:58 -07005296static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5297{
5298#if defined(CONFIG_UNIX)
5299 if (ctx->ring_sock) {
5300 struct sock *sock = ctx->ring_sock->sk;
5301 struct sk_buff *skb;
5302
5303 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5304 kfree_skb(skb);
5305 }
5306#else
5307 int i;
5308
Jens Axboe65e19f52019-10-26 07:20:21 -06005309 for (i = 0; i < ctx->nr_user_files; i++) {
5310 struct file *file;
5311
5312 file = io_file_from_index(ctx, i);
5313 if (file)
5314 fput(file);
5315 }
Jens Axboe6b063142019-01-10 22:13:58 -07005316#endif
5317}
5318
Jens Axboe05f3fb32019-12-09 11:22:50 -07005319static void io_file_ref_kill(struct percpu_ref *ref)
5320{
5321 struct fixed_file_data *data;
5322
5323 data = container_of(ref, struct fixed_file_data, refs);
5324 complete(&data->done);
5325}
5326
Jens Axboe6b063142019-01-10 22:13:58 -07005327static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5328{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005329 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005330 unsigned nr_tables, i;
5331
Jens Axboe05f3fb32019-12-09 11:22:50 -07005332 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005333 return -ENXIO;
5334
Jens Axboe05f3fb32019-12-09 11:22:50 -07005335 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005336 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005337 wait_for_completion(&data->done);
5338 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005339 percpu_ref_exit(&data->refs);
5340
Jens Axboe6b063142019-01-10 22:13:58 -07005341 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005342 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5343 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005344 kfree(data->table[i].files);
5345 kfree(data->table);
5346 kfree(data);
5347 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005348 ctx->nr_user_files = 0;
5349 return 0;
5350}
5351
Jens Axboe6c271ce2019-01-10 11:22:30 -07005352static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5353{
5354 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005355 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005356 /*
5357 * The park is a bit of a work-around, without it we get
5358 * warning spews on shutdown with SQPOLL set and affinity
5359 * set to a single CPU.
5360 */
Jens Axboe06058632019-04-13 09:26:03 -06005361 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005362 kthread_stop(ctx->sqo_thread);
5363 ctx->sqo_thread = NULL;
5364 }
5365}
5366
Jens Axboe6b063142019-01-10 22:13:58 -07005367static void io_finish_async(struct io_ring_ctx *ctx)
5368{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005369 io_sq_thread_stop(ctx);
5370
Jens Axboe561fb042019-10-24 07:25:42 -06005371 if (ctx->io_wq) {
5372 io_wq_destroy(ctx->io_wq);
5373 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005374 }
5375}
5376
5377#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005378/*
5379 * Ensure the UNIX gc is aware of our file set, so we are certain that
5380 * the io_uring can be safely unregistered on process exit, even if we have
5381 * loops in the file referencing.
5382 */
5383static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5384{
5385 struct sock *sk = ctx->ring_sock->sk;
5386 struct scm_fp_list *fpl;
5387 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005388 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005389
5390 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5391 unsigned long inflight = ctx->user->unix_inflight + nr;
5392
5393 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5394 return -EMFILE;
5395 }
5396
5397 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5398 if (!fpl)
5399 return -ENOMEM;
5400
5401 skb = alloc_skb(0, GFP_KERNEL);
5402 if (!skb) {
5403 kfree(fpl);
5404 return -ENOMEM;
5405 }
5406
5407 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005408
Jens Axboe08a45172019-10-03 08:11:03 -06005409 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005410 fpl->user = get_uid(ctx->user);
5411 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005412 struct file *file = io_file_from_index(ctx, i + offset);
5413
5414 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005415 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005416 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005417 unix_inflight(fpl->user, fpl->fp[nr_files]);
5418 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005419 }
5420
Jens Axboe08a45172019-10-03 08:11:03 -06005421 if (nr_files) {
5422 fpl->max = SCM_MAX_FD;
5423 fpl->count = nr_files;
5424 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005425 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005426 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5427 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005428
Jens Axboe08a45172019-10-03 08:11:03 -06005429 for (i = 0; i < nr_files; i++)
5430 fput(fpl->fp[i]);
5431 } else {
5432 kfree_skb(skb);
5433 kfree(fpl);
5434 }
Jens Axboe6b063142019-01-10 22:13:58 -07005435
5436 return 0;
5437}
5438
5439/*
5440 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5441 * causes regular reference counting to break down. We rely on the UNIX
5442 * garbage collection to take care of this problem for us.
5443 */
5444static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5445{
5446 unsigned left, total;
5447 int ret = 0;
5448
5449 total = 0;
5450 left = ctx->nr_user_files;
5451 while (left) {
5452 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005453
5454 ret = __io_sqe_files_scm(ctx, this_files, total);
5455 if (ret)
5456 break;
5457 left -= this_files;
5458 total += this_files;
5459 }
5460
5461 if (!ret)
5462 return 0;
5463
5464 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005465 struct file *file = io_file_from_index(ctx, total);
5466
5467 if (file)
5468 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005469 total++;
5470 }
5471
5472 return ret;
5473}
5474#else
5475static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5476{
5477 return 0;
5478}
5479#endif
5480
Jens Axboe65e19f52019-10-26 07:20:21 -06005481static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5482 unsigned nr_files)
5483{
5484 int i;
5485
5486 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005487 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005488 unsigned this_files;
5489
5490 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5491 table->files = kcalloc(this_files, sizeof(struct file *),
5492 GFP_KERNEL);
5493 if (!table->files)
5494 break;
5495 nr_files -= this_files;
5496 }
5497
5498 if (i == nr_tables)
5499 return 0;
5500
5501 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005502 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005503 kfree(table->files);
5504 }
5505 return 1;
5506}
5507
Jens Axboe05f3fb32019-12-09 11:22:50 -07005508static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005509{
5510#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005511 struct sock *sock = ctx->ring_sock->sk;
5512 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5513 struct sk_buff *skb;
5514 int i;
5515
5516 __skb_queue_head_init(&list);
5517
5518 /*
5519 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5520 * remove this entry and rearrange the file array.
5521 */
5522 skb = skb_dequeue(head);
5523 while (skb) {
5524 struct scm_fp_list *fp;
5525
5526 fp = UNIXCB(skb).fp;
5527 for (i = 0; i < fp->count; i++) {
5528 int left;
5529
5530 if (fp->fp[i] != file)
5531 continue;
5532
5533 unix_notinflight(fp->user, fp->fp[i]);
5534 left = fp->count - 1 - i;
5535 if (left) {
5536 memmove(&fp->fp[i], &fp->fp[i + 1],
5537 left * sizeof(struct file *));
5538 }
5539 fp->count--;
5540 if (!fp->count) {
5541 kfree_skb(skb);
5542 skb = NULL;
5543 } else {
5544 __skb_queue_tail(&list, skb);
5545 }
5546 fput(file);
5547 file = NULL;
5548 break;
5549 }
5550
5551 if (!file)
5552 break;
5553
5554 __skb_queue_tail(&list, skb);
5555
5556 skb = skb_dequeue(head);
5557 }
5558
5559 if (skb_peek(&list)) {
5560 spin_lock_irq(&head->lock);
5561 while ((skb = __skb_dequeue(&list)) != NULL)
5562 __skb_queue_tail(head, skb);
5563 spin_unlock_irq(&head->lock);
5564 }
5565#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005566 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005567#endif
5568}
5569
Jens Axboe05f3fb32019-12-09 11:22:50 -07005570struct io_file_put {
5571 struct llist_node llist;
5572 struct file *file;
5573 struct completion *done;
5574};
5575
Jens Axboe2faf8522020-02-04 19:54:55 -07005576static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005577{
5578 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005579 struct llist_node *node;
5580
Jens Axboe05f3fb32019-12-09 11:22:50 -07005581 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5582 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5583 io_ring_file_put(data->ctx, pfile->file);
5584 if (pfile->done)
5585 complete(pfile->done);
5586 else
5587 kfree(pfile);
5588 }
5589 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005590}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005591
Jens Axboe2faf8522020-02-04 19:54:55 -07005592static void io_ring_file_ref_switch(struct work_struct *work)
5593{
5594 struct fixed_file_data *data;
5595
5596 data = container_of(work, struct fixed_file_data, ref_work);
5597 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005598 percpu_ref_get(&data->refs);
5599 percpu_ref_switch_to_percpu(&data->refs);
5600}
5601
5602static void io_file_data_ref_zero(struct percpu_ref *ref)
5603{
5604 struct fixed_file_data *data;
5605
5606 data = container_of(ref, struct fixed_file_data, refs);
5607
Jens Axboe2faf8522020-02-04 19:54:55 -07005608 /*
5609 * We can't safely switch from inside this context, punt to wq. If
5610 * the table ref is going away, the table is being unregistered.
5611 * Don't queue up the async work for that case, the caller will
5612 * handle it.
5613 */
5614 if (!percpu_ref_is_dying(&data->refs))
5615 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005616}
5617
5618static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5619 unsigned nr_args)
5620{
5621 __s32 __user *fds = (__s32 __user *) arg;
5622 unsigned nr_tables;
5623 struct file *file;
5624 int fd, ret = 0;
5625 unsigned i;
5626
5627 if (ctx->file_data)
5628 return -EBUSY;
5629 if (!nr_args)
5630 return -EINVAL;
5631 if (nr_args > IORING_MAX_FIXED_FILES)
5632 return -EMFILE;
5633
5634 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5635 if (!ctx->file_data)
5636 return -ENOMEM;
5637 ctx->file_data->ctx = ctx;
5638 init_completion(&ctx->file_data->done);
5639
5640 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5641 ctx->file_data->table = kcalloc(nr_tables,
5642 sizeof(struct fixed_file_table),
5643 GFP_KERNEL);
5644 if (!ctx->file_data->table) {
5645 kfree(ctx->file_data);
5646 ctx->file_data = NULL;
5647 return -ENOMEM;
5648 }
5649
5650 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5651 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5652 kfree(ctx->file_data->table);
5653 kfree(ctx->file_data);
5654 ctx->file_data = NULL;
5655 return -ENOMEM;
5656 }
5657 ctx->file_data->put_llist.first = NULL;
5658 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5659
5660 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5661 percpu_ref_exit(&ctx->file_data->refs);
5662 kfree(ctx->file_data->table);
5663 kfree(ctx->file_data);
5664 ctx->file_data = NULL;
5665 return -ENOMEM;
5666 }
5667
5668 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5669 struct fixed_file_table *table;
5670 unsigned index;
5671
5672 ret = -EFAULT;
5673 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5674 break;
5675 /* allow sparse sets */
5676 if (fd == -1) {
5677 ret = 0;
5678 continue;
5679 }
5680
5681 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5682 index = i & IORING_FILE_TABLE_MASK;
5683 file = fget(fd);
5684
5685 ret = -EBADF;
5686 if (!file)
5687 break;
5688
5689 /*
5690 * Don't allow io_uring instances to be registered. If UNIX
5691 * isn't enabled, then this causes a reference cycle and this
5692 * instance can never get freed. If UNIX is enabled we'll
5693 * handle it just fine, but there's still no point in allowing
5694 * a ring fd as it doesn't support regular read/write anyway.
5695 */
5696 if (file->f_op == &io_uring_fops) {
5697 fput(file);
5698 break;
5699 }
5700 ret = 0;
5701 table->files[index] = file;
5702 }
5703
5704 if (ret) {
5705 for (i = 0; i < ctx->nr_user_files; i++) {
5706 file = io_file_from_index(ctx, i);
5707 if (file)
5708 fput(file);
5709 }
5710 for (i = 0; i < nr_tables; i++)
5711 kfree(ctx->file_data->table[i].files);
5712
5713 kfree(ctx->file_data->table);
5714 kfree(ctx->file_data);
5715 ctx->file_data = NULL;
5716 ctx->nr_user_files = 0;
5717 return ret;
5718 }
5719
5720 ret = io_sqe_files_scm(ctx);
5721 if (ret)
5722 io_sqe_files_unregister(ctx);
5723
5724 return ret;
5725}
5726
Jens Axboec3a31e62019-10-03 13:59:56 -06005727static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5728 int index)
5729{
5730#if defined(CONFIG_UNIX)
5731 struct sock *sock = ctx->ring_sock->sk;
5732 struct sk_buff_head *head = &sock->sk_receive_queue;
5733 struct sk_buff *skb;
5734
5735 /*
5736 * See if we can merge this file into an existing skb SCM_RIGHTS
5737 * file set. If there's no room, fall back to allocating a new skb
5738 * and filling it in.
5739 */
5740 spin_lock_irq(&head->lock);
5741 skb = skb_peek(head);
5742 if (skb) {
5743 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5744
5745 if (fpl->count < SCM_MAX_FD) {
5746 __skb_unlink(skb, head);
5747 spin_unlock_irq(&head->lock);
5748 fpl->fp[fpl->count] = get_file(file);
5749 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5750 fpl->count++;
5751 spin_lock_irq(&head->lock);
5752 __skb_queue_head(head, skb);
5753 } else {
5754 skb = NULL;
5755 }
5756 }
5757 spin_unlock_irq(&head->lock);
5758
5759 if (skb) {
5760 fput(file);
5761 return 0;
5762 }
5763
5764 return __io_sqe_files_scm(ctx, 1, index);
5765#else
5766 return 0;
5767#endif
5768}
5769
Jens Axboe05f3fb32019-12-09 11:22:50 -07005770static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005771{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005772 struct fixed_file_data *data;
5773
5774 data = container_of(ref, struct fixed_file_data, refs);
5775 clear_bit(FFD_F_ATOMIC, &data->state);
5776}
5777
5778static bool io_queue_file_removal(struct fixed_file_data *data,
5779 struct file *file)
5780{
5781 struct io_file_put *pfile, pfile_stack;
5782 DECLARE_COMPLETION_ONSTACK(done);
5783
5784 /*
5785 * If we fail allocating the struct we need for doing async reomval
5786 * of this file, just punt to sync and wait for it.
5787 */
5788 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5789 if (!pfile) {
5790 pfile = &pfile_stack;
5791 pfile->done = &done;
5792 }
5793
5794 pfile->file = file;
5795 llist_add(&pfile->llist, &data->put_llist);
5796
5797 if (pfile == &pfile_stack) {
5798 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5799 percpu_ref_put(&data->refs);
5800 percpu_ref_switch_to_atomic(&data->refs,
5801 io_atomic_switch);
5802 }
5803 wait_for_completion(&done);
5804 flush_work(&data->ref_work);
5805 return false;
5806 }
5807
5808 return true;
5809}
5810
5811static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5812 struct io_uring_files_update *up,
5813 unsigned nr_args)
5814{
5815 struct fixed_file_data *data = ctx->file_data;
5816 bool ref_switch = false;
5817 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005818 __s32 __user *fds;
5819 int fd, i, err;
5820 __u32 done;
5821
Jens Axboe05f3fb32019-12-09 11:22:50 -07005822 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005823 return -EOVERFLOW;
5824 if (done > ctx->nr_user_files)
5825 return -EINVAL;
5826
5827 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005828 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005829 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005830 struct fixed_file_table *table;
5831 unsigned index;
5832
Jens Axboec3a31e62019-10-03 13:59:56 -06005833 err = 0;
5834 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5835 err = -EFAULT;
5836 break;
5837 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005838 i = array_index_nospec(up->offset, ctx->nr_user_files);
5839 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005840 index = i & IORING_FILE_TABLE_MASK;
5841 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005842 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005843 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005844 if (io_queue_file_removal(data, file))
5845 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005846 }
5847 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005848 file = fget(fd);
5849 if (!file) {
5850 err = -EBADF;
5851 break;
5852 }
5853 /*
5854 * Don't allow io_uring instances to be registered. If
5855 * UNIX isn't enabled, then this causes a reference
5856 * cycle and this instance can never get freed. If UNIX
5857 * is enabled we'll handle it just fine, but there's
5858 * still no point in allowing a ring fd as it doesn't
5859 * support regular read/write anyway.
5860 */
5861 if (file->f_op == &io_uring_fops) {
5862 fput(file);
5863 err = -EBADF;
5864 break;
5865 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005866 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005867 err = io_sqe_file_register(ctx, file, i);
5868 if (err)
5869 break;
5870 }
5871 nr_args--;
5872 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005873 up->offset++;
5874 }
5875
5876 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5877 percpu_ref_put(&data->refs);
5878 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005879 }
5880
5881 return done ? done : err;
5882}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005883static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5884 unsigned nr_args)
5885{
5886 struct io_uring_files_update up;
5887
5888 if (!ctx->file_data)
5889 return -ENXIO;
5890 if (!nr_args)
5891 return -EINVAL;
5892 if (copy_from_user(&up, arg, sizeof(up)))
5893 return -EFAULT;
5894 if (up.resv)
5895 return -EINVAL;
5896
5897 return __io_sqe_files_update(ctx, &up, nr_args);
5898}
Jens Axboec3a31e62019-10-03 13:59:56 -06005899
Jens Axboe7d723062019-11-12 22:31:31 -07005900static void io_put_work(struct io_wq_work *work)
5901{
5902 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5903
5904 io_put_req(req);
5905}
5906
5907static void io_get_work(struct io_wq_work *work)
5908{
5909 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5910
5911 refcount_inc(&req->refs);
5912}
5913
Pavel Begunkov24369c22020-01-28 03:15:48 +03005914static int io_init_wq_offload(struct io_ring_ctx *ctx,
5915 struct io_uring_params *p)
5916{
5917 struct io_wq_data data;
5918 struct fd f;
5919 struct io_ring_ctx *ctx_attach;
5920 unsigned int concurrency;
5921 int ret = 0;
5922
5923 data.user = ctx->user;
5924 data.get_work = io_get_work;
5925 data.put_work = io_put_work;
5926
5927 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5928 /* Do QD, or 4 * CPUS, whatever is smallest */
5929 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5930
5931 ctx->io_wq = io_wq_create(concurrency, &data);
5932 if (IS_ERR(ctx->io_wq)) {
5933 ret = PTR_ERR(ctx->io_wq);
5934 ctx->io_wq = NULL;
5935 }
5936 return ret;
5937 }
5938
5939 f = fdget(p->wq_fd);
5940 if (!f.file)
5941 return -EBADF;
5942
5943 if (f.file->f_op != &io_uring_fops) {
5944 ret = -EINVAL;
5945 goto out_fput;
5946 }
5947
5948 ctx_attach = f.file->private_data;
5949 /* @io_wq is protected by holding the fd */
5950 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5951 ret = -EINVAL;
5952 goto out_fput;
5953 }
5954
5955 ctx->io_wq = ctx_attach->io_wq;
5956out_fput:
5957 fdput(f);
5958 return ret;
5959}
5960
Jens Axboe6c271ce2019-01-10 11:22:30 -07005961static int io_sq_offload_start(struct io_ring_ctx *ctx,
5962 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005963{
5964 int ret;
5965
Jens Axboe6c271ce2019-01-10 11:22:30 -07005966 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005967 mmgrab(current->mm);
5968 ctx->sqo_mm = current->mm;
5969
Jens Axboe6c271ce2019-01-10 11:22:30 -07005970 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005971 ret = -EPERM;
5972 if (!capable(CAP_SYS_ADMIN))
5973 goto err;
5974
Jens Axboe917257d2019-04-13 09:28:55 -06005975 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5976 if (!ctx->sq_thread_idle)
5977 ctx->sq_thread_idle = HZ;
5978
Jens Axboe6c271ce2019-01-10 11:22:30 -07005979 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005980 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005981
Jens Axboe917257d2019-04-13 09:28:55 -06005982 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005983 if (cpu >= nr_cpu_ids)
5984 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005985 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005986 goto err;
5987
Jens Axboe6c271ce2019-01-10 11:22:30 -07005988 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5989 ctx, cpu,
5990 "io_uring-sq");
5991 } else {
5992 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5993 "io_uring-sq");
5994 }
5995 if (IS_ERR(ctx->sqo_thread)) {
5996 ret = PTR_ERR(ctx->sqo_thread);
5997 ctx->sqo_thread = NULL;
5998 goto err;
5999 }
6000 wake_up_process(ctx->sqo_thread);
6001 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6002 /* Can't have SQ_AFF without SQPOLL */
6003 ret = -EINVAL;
6004 goto err;
6005 }
6006
Pavel Begunkov24369c22020-01-28 03:15:48 +03006007 ret = io_init_wq_offload(ctx, p);
6008 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006009 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006010
6011 return 0;
6012err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006013 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006014 mmdrop(ctx->sqo_mm);
6015 ctx->sqo_mm = NULL;
6016 return ret;
6017}
6018
6019static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6020{
6021 atomic_long_sub(nr_pages, &user->locked_vm);
6022}
6023
6024static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6025{
6026 unsigned long page_limit, cur_pages, new_pages;
6027
6028 /* Don't allow more pages than we can safely lock */
6029 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6030
6031 do {
6032 cur_pages = atomic_long_read(&user->locked_vm);
6033 new_pages = cur_pages + nr_pages;
6034 if (new_pages > page_limit)
6035 return -ENOMEM;
6036 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6037 new_pages) != cur_pages);
6038
6039 return 0;
6040}
6041
6042static void io_mem_free(void *ptr)
6043{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006044 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006045
Mark Rutland52e04ef2019-04-30 17:30:21 +01006046 if (!ptr)
6047 return;
6048
6049 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006050 if (put_page_testzero(page))
6051 free_compound_page(page);
6052}
6053
6054static void *io_mem_alloc(size_t size)
6055{
6056 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6057 __GFP_NORETRY;
6058
6059 return (void *) __get_free_pages(gfp_flags, get_order(size));
6060}
6061
Hristo Venev75b28af2019-08-26 17:23:46 +00006062static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6063 size_t *sq_offset)
6064{
6065 struct io_rings *rings;
6066 size_t off, sq_array_size;
6067
6068 off = struct_size(rings, cqes, cq_entries);
6069 if (off == SIZE_MAX)
6070 return SIZE_MAX;
6071
6072#ifdef CONFIG_SMP
6073 off = ALIGN(off, SMP_CACHE_BYTES);
6074 if (off == 0)
6075 return SIZE_MAX;
6076#endif
6077
6078 sq_array_size = array_size(sizeof(u32), sq_entries);
6079 if (sq_array_size == SIZE_MAX)
6080 return SIZE_MAX;
6081
6082 if (check_add_overflow(off, sq_array_size, &off))
6083 return SIZE_MAX;
6084
6085 if (sq_offset)
6086 *sq_offset = off;
6087
6088 return off;
6089}
6090
Jens Axboe2b188cc2019-01-07 10:46:33 -07006091static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6092{
Hristo Venev75b28af2019-08-26 17:23:46 +00006093 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006094
Hristo Venev75b28af2019-08-26 17:23:46 +00006095 pages = (size_t)1 << get_order(
6096 rings_size(sq_entries, cq_entries, NULL));
6097 pages += (size_t)1 << get_order(
6098 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006099
Hristo Venev75b28af2019-08-26 17:23:46 +00006100 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006101}
6102
Jens Axboeedafcce2019-01-09 09:16:05 -07006103static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6104{
6105 int i, j;
6106
6107 if (!ctx->user_bufs)
6108 return -ENXIO;
6109
6110 for (i = 0; i < ctx->nr_user_bufs; i++) {
6111 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6112
6113 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006114 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006115
6116 if (ctx->account_mem)
6117 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006118 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006119 imu->nr_bvecs = 0;
6120 }
6121
6122 kfree(ctx->user_bufs);
6123 ctx->user_bufs = NULL;
6124 ctx->nr_user_bufs = 0;
6125 return 0;
6126}
6127
6128static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6129 void __user *arg, unsigned index)
6130{
6131 struct iovec __user *src;
6132
6133#ifdef CONFIG_COMPAT
6134 if (ctx->compat) {
6135 struct compat_iovec __user *ciovs;
6136 struct compat_iovec ciov;
6137
6138 ciovs = (struct compat_iovec __user *) arg;
6139 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6140 return -EFAULT;
6141
Jens Axboed55e5f52019-12-11 16:12:15 -07006142 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006143 dst->iov_len = ciov.iov_len;
6144 return 0;
6145 }
6146#endif
6147 src = (struct iovec __user *) arg;
6148 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6149 return -EFAULT;
6150 return 0;
6151}
6152
6153static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6154 unsigned nr_args)
6155{
6156 struct vm_area_struct **vmas = NULL;
6157 struct page **pages = NULL;
6158 int i, j, got_pages = 0;
6159 int ret = -EINVAL;
6160
6161 if (ctx->user_bufs)
6162 return -EBUSY;
6163 if (!nr_args || nr_args > UIO_MAXIOV)
6164 return -EINVAL;
6165
6166 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6167 GFP_KERNEL);
6168 if (!ctx->user_bufs)
6169 return -ENOMEM;
6170
6171 for (i = 0; i < nr_args; i++) {
6172 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6173 unsigned long off, start, end, ubuf;
6174 int pret, nr_pages;
6175 struct iovec iov;
6176 size_t size;
6177
6178 ret = io_copy_iov(ctx, &iov, arg, i);
6179 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006180 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006181
6182 /*
6183 * Don't impose further limits on the size and buffer
6184 * constraints here, we'll -EINVAL later when IO is
6185 * submitted if they are wrong.
6186 */
6187 ret = -EFAULT;
6188 if (!iov.iov_base || !iov.iov_len)
6189 goto err;
6190
6191 /* arbitrary limit, but we need something */
6192 if (iov.iov_len > SZ_1G)
6193 goto err;
6194
6195 ubuf = (unsigned long) iov.iov_base;
6196 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6197 start = ubuf >> PAGE_SHIFT;
6198 nr_pages = end - start;
6199
6200 if (ctx->account_mem) {
6201 ret = io_account_mem(ctx->user, nr_pages);
6202 if (ret)
6203 goto err;
6204 }
6205
6206 ret = 0;
6207 if (!pages || nr_pages > got_pages) {
6208 kfree(vmas);
6209 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006210 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006211 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006212 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006213 sizeof(struct vm_area_struct *),
6214 GFP_KERNEL);
6215 if (!pages || !vmas) {
6216 ret = -ENOMEM;
6217 if (ctx->account_mem)
6218 io_unaccount_mem(ctx->user, nr_pages);
6219 goto err;
6220 }
6221 got_pages = nr_pages;
6222 }
6223
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006224 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006225 GFP_KERNEL);
6226 ret = -ENOMEM;
6227 if (!imu->bvec) {
6228 if (ctx->account_mem)
6229 io_unaccount_mem(ctx->user, nr_pages);
6230 goto err;
6231 }
6232
6233 ret = 0;
6234 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006235 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006236 FOLL_WRITE | FOLL_LONGTERM,
6237 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006238 if (pret == nr_pages) {
6239 /* don't support file backed memory */
6240 for (j = 0; j < nr_pages; j++) {
6241 struct vm_area_struct *vma = vmas[j];
6242
6243 if (vma->vm_file &&
6244 !is_file_hugepages(vma->vm_file)) {
6245 ret = -EOPNOTSUPP;
6246 break;
6247 }
6248 }
6249 } else {
6250 ret = pret < 0 ? pret : -EFAULT;
6251 }
6252 up_read(&current->mm->mmap_sem);
6253 if (ret) {
6254 /*
6255 * if we did partial map, or found file backed vmas,
6256 * release any pages we did get
6257 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006258 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006259 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006260 if (ctx->account_mem)
6261 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006262 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006263 goto err;
6264 }
6265
6266 off = ubuf & ~PAGE_MASK;
6267 size = iov.iov_len;
6268 for (j = 0; j < nr_pages; j++) {
6269 size_t vec_len;
6270
6271 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6272 imu->bvec[j].bv_page = pages[j];
6273 imu->bvec[j].bv_len = vec_len;
6274 imu->bvec[j].bv_offset = off;
6275 off = 0;
6276 size -= vec_len;
6277 }
6278 /* store original address for later verification */
6279 imu->ubuf = ubuf;
6280 imu->len = iov.iov_len;
6281 imu->nr_bvecs = nr_pages;
6282
6283 ctx->nr_user_bufs++;
6284 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006285 kvfree(pages);
6286 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006287 return 0;
6288err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006289 kvfree(pages);
6290 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006291 io_sqe_buffer_unregister(ctx);
6292 return ret;
6293}
6294
Jens Axboe9b402842019-04-11 11:45:41 -06006295static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6296{
6297 __s32 __user *fds = arg;
6298 int fd;
6299
6300 if (ctx->cq_ev_fd)
6301 return -EBUSY;
6302
6303 if (copy_from_user(&fd, fds, sizeof(*fds)))
6304 return -EFAULT;
6305
6306 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6307 if (IS_ERR(ctx->cq_ev_fd)) {
6308 int ret = PTR_ERR(ctx->cq_ev_fd);
6309 ctx->cq_ev_fd = NULL;
6310 return ret;
6311 }
6312
6313 return 0;
6314}
6315
6316static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6317{
6318 if (ctx->cq_ev_fd) {
6319 eventfd_ctx_put(ctx->cq_ev_fd);
6320 ctx->cq_ev_fd = NULL;
6321 return 0;
6322 }
6323
6324 return -ENXIO;
6325}
6326
Jens Axboe2b188cc2019-01-07 10:46:33 -07006327static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6328{
Jens Axboe6b063142019-01-10 22:13:58 -07006329 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006330 if (ctx->sqo_mm)
6331 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006332
6333 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006334 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006335 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006336 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006337 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006338
Jens Axboe2b188cc2019-01-07 10:46:33 -07006339#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006340 if (ctx->ring_sock) {
6341 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006342 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006343 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006344#endif
6345
Hristo Venev75b28af2019-08-26 17:23:46 +00006346 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006347 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006348
6349 percpu_ref_exit(&ctx->refs);
6350 if (ctx->account_mem)
6351 io_unaccount_mem(ctx->user,
6352 ring_pages(ctx->sq_entries, ctx->cq_entries));
6353 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006354 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006355 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006356 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006357 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006358 kfree(ctx);
6359}
6360
6361static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6362{
6363 struct io_ring_ctx *ctx = file->private_data;
6364 __poll_t mask = 0;
6365
6366 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006367 /*
6368 * synchronizes with barrier from wq_has_sleeper call in
6369 * io_commit_cqring
6370 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006371 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006372 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6373 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006374 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006375 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006376 mask |= EPOLLIN | EPOLLRDNORM;
6377
6378 return mask;
6379}
6380
6381static int io_uring_fasync(int fd, struct file *file, int on)
6382{
6383 struct io_ring_ctx *ctx = file->private_data;
6384
6385 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6386}
6387
Jens Axboe071698e2020-01-28 10:04:42 -07006388static int io_remove_personalities(int id, void *p, void *data)
6389{
6390 struct io_ring_ctx *ctx = data;
6391 const struct cred *cred;
6392
6393 cred = idr_remove(&ctx->personality_idr, id);
6394 if (cred)
6395 put_cred(cred);
6396 return 0;
6397}
6398
Jens Axboe2b188cc2019-01-07 10:46:33 -07006399static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6400{
6401 mutex_lock(&ctx->uring_lock);
6402 percpu_ref_kill(&ctx->refs);
6403 mutex_unlock(&ctx->uring_lock);
6404
Jens Axboedf069d82020-02-04 16:48:34 -07006405 /*
6406 * Wait for sq thread to idle, if we have one. It won't spin on new
6407 * work after we've killed the ctx ref above. This is important to do
6408 * before we cancel existing commands, as the thread could otherwise
6409 * be queueing new work post that. If that's work we need to cancel,
6410 * it could cause shutdown to hang.
6411 */
6412 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6413 cpu_relax();
6414
Jens Axboe5262f562019-09-17 12:26:57 -06006415 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006416 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006417
6418 if (ctx->io_wq)
6419 io_wq_cancel_all(ctx->io_wq);
6420
Jens Axboedef596e2019-01-09 08:59:42 -07006421 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006422 /* if we failed setting up the ctx, we might not have any rings */
6423 if (ctx->rings)
6424 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006425 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006426 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006427 io_ring_ctx_free(ctx);
6428}
6429
6430static int io_uring_release(struct inode *inode, struct file *file)
6431{
6432 struct io_ring_ctx *ctx = file->private_data;
6433
6434 file->private_data = NULL;
6435 io_ring_ctx_wait_and_kill(ctx);
6436 return 0;
6437}
6438
Jens Axboefcb323c2019-10-24 12:39:47 -06006439static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6440 struct files_struct *files)
6441{
6442 struct io_kiocb *req;
6443 DEFINE_WAIT(wait);
6444
6445 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006446 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006447
6448 spin_lock_irq(&ctx->inflight_lock);
6449 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006450 if (req->work.files != files)
6451 continue;
6452 /* req is being completed, ignore */
6453 if (!refcount_inc_not_zero(&req->refs))
6454 continue;
6455 cancel_req = req;
6456 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006457 }
Jens Axboe768134d2019-11-10 20:30:53 -07006458 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006459 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006460 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006461 spin_unlock_irq(&ctx->inflight_lock);
6462
Jens Axboe768134d2019-11-10 20:30:53 -07006463 /* We need to keep going until we don't find a matching req */
6464 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006465 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006466
Jens Axboe2ca10252020-02-13 17:17:35 -07006467 if (cancel_req->flags & REQ_F_OVERFLOW) {
6468 spin_lock_irq(&ctx->completion_lock);
6469 list_del(&cancel_req->list);
6470 cancel_req->flags &= ~REQ_F_OVERFLOW;
6471 if (list_empty(&ctx->cq_overflow_list)) {
6472 clear_bit(0, &ctx->sq_check_overflow);
6473 clear_bit(0, &ctx->cq_check_overflow);
6474 }
6475 spin_unlock_irq(&ctx->completion_lock);
6476
6477 WRITE_ONCE(ctx->rings->cq_overflow,
6478 atomic_inc_return(&ctx->cached_cq_overflow));
6479
6480 /*
6481 * Put inflight ref and overflow ref. If that's
6482 * all we had, then we're done with this request.
6483 */
6484 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6485 io_put_req(cancel_req);
6486 continue;
6487 }
6488 }
6489
Bob Liu2f6d9b92019-11-13 18:06:24 +08006490 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6491 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006492 schedule();
6493 }
Jens Axboe768134d2019-11-10 20:30:53 -07006494 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006495}
6496
6497static int io_uring_flush(struct file *file, void *data)
6498{
6499 struct io_ring_ctx *ctx = file->private_data;
6500
6501 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006502
6503 /*
6504 * If the task is going away, cancel work it may have pending
6505 */
6506 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6507 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6508
Jens Axboefcb323c2019-10-24 12:39:47 -06006509 return 0;
6510}
6511
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006512static void *io_uring_validate_mmap_request(struct file *file,
6513 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006514{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006515 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006516 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006517 struct page *page;
6518 void *ptr;
6519
6520 switch (offset) {
6521 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006522 case IORING_OFF_CQ_RING:
6523 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006524 break;
6525 case IORING_OFF_SQES:
6526 ptr = ctx->sq_sqes;
6527 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006528 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006529 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006530 }
6531
6532 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006533 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006534 return ERR_PTR(-EINVAL);
6535
6536 return ptr;
6537}
6538
6539#ifdef CONFIG_MMU
6540
6541static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6542{
6543 size_t sz = vma->vm_end - vma->vm_start;
6544 unsigned long pfn;
6545 void *ptr;
6546
6547 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6548 if (IS_ERR(ptr))
6549 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006550
6551 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6552 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6553}
6554
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006555#else /* !CONFIG_MMU */
6556
6557static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6558{
6559 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6560}
6561
6562static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6563{
6564 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6565}
6566
6567static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6568 unsigned long addr, unsigned long len,
6569 unsigned long pgoff, unsigned long flags)
6570{
6571 void *ptr;
6572
6573 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6574 if (IS_ERR(ptr))
6575 return PTR_ERR(ptr);
6576
6577 return (unsigned long) ptr;
6578}
6579
6580#endif /* !CONFIG_MMU */
6581
Jens Axboe2b188cc2019-01-07 10:46:33 -07006582SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6583 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6584 size_t, sigsz)
6585{
6586 struct io_ring_ctx *ctx;
6587 long ret = -EBADF;
6588 int submitted = 0;
6589 struct fd f;
6590
Jens Axboe6c271ce2019-01-10 11:22:30 -07006591 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006592 return -EINVAL;
6593
6594 f = fdget(fd);
6595 if (!f.file)
6596 return -EBADF;
6597
6598 ret = -EOPNOTSUPP;
6599 if (f.file->f_op != &io_uring_fops)
6600 goto out_fput;
6601
6602 ret = -ENXIO;
6603 ctx = f.file->private_data;
6604 if (!percpu_ref_tryget(&ctx->refs))
6605 goto out_fput;
6606
Jens Axboe6c271ce2019-01-10 11:22:30 -07006607 /*
6608 * For SQ polling, the thread will do all submissions and completions.
6609 * Just return the requested submit count, and wake the thread if
6610 * we were asked to.
6611 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006612 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006613 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006614 if (!list_empty_careful(&ctx->cq_overflow_list))
6615 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006616 if (flags & IORING_ENTER_SQ_WAKEUP)
6617 wake_up(&ctx->sqo_wait);
6618 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006619 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006620 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006621
6622 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006623 /* already have mm, so io_submit_sqes() won't try to grab it */
6624 cur_mm = ctx->sqo_mm;
6625 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6626 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006627 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006628
6629 if (submitted != to_submit)
6630 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006631 }
6632 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006633 unsigned nr_events = 0;
6634
Jens Axboe2b188cc2019-01-07 10:46:33 -07006635 min_complete = min(min_complete, ctx->cq_entries);
6636
Jens Axboedef596e2019-01-09 08:59:42 -07006637 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006638 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006639 } else {
6640 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6641 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006642 }
6643
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006644out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006645 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006646out_fput:
6647 fdput(f);
6648 return submitted ? submitted : ret;
6649}
6650
Jens Axboe87ce9552020-01-30 08:25:34 -07006651static int io_uring_show_cred(int id, void *p, void *data)
6652{
6653 const struct cred *cred = p;
6654 struct seq_file *m = data;
6655 struct user_namespace *uns = seq_user_ns(m);
6656 struct group_info *gi;
6657 kernel_cap_t cap;
6658 unsigned __capi;
6659 int g;
6660
6661 seq_printf(m, "%5d\n", id);
6662 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6663 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6664 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6665 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6666 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6667 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6668 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6669 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6670 seq_puts(m, "\n\tGroups:\t");
6671 gi = cred->group_info;
6672 for (g = 0; g < gi->ngroups; g++) {
6673 seq_put_decimal_ull(m, g ? " " : "",
6674 from_kgid_munged(uns, gi->gid[g]));
6675 }
6676 seq_puts(m, "\n\tCapEff:\t");
6677 cap = cred->cap_effective;
6678 CAP_FOR_EACH_U32(__capi)
6679 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6680 seq_putc(m, '\n');
6681 return 0;
6682}
6683
6684static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6685{
6686 int i;
6687
6688 mutex_lock(&ctx->uring_lock);
6689 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6690 for (i = 0; i < ctx->nr_user_files; i++) {
6691 struct fixed_file_table *table;
6692 struct file *f;
6693
6694 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6695 f = table->files[i & IORING_FILE_TABLE_MASK];
6696 if (f)
6697 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6698 else
6699 seq_printf(m, "%5u: <none>\n", i);
6700 }
6701 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6702 for (i = 0; i < ctx->nr_user_bufs; i++) {
6703 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6704
6705 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6706 (unsigned int) buf->len);
6707 }
6708 if (!idr_is_empty(&ctx->personality_idr)) {
6709 seq_printf(m, "Personalities:\n");
6710 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6711 }
6712 mutex_unlock(&ctx->uring_lock);
6713}
6714
6715static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6716{
6717 struct io_ring_ctx *ctx = f->private_data;
6718
6719 if (percpu_ref_tryget(&ctx->refs)) {
6720 __io_uring_show_fdinfo(ctx, m);
6721 percpu_ref_put(&ctx->refs);
6722 }
6723}
6724
Jens Axboe2b188cc2019-01-07 10:46:33 -07006725static const struct file_operations io_uring_fops = {
6726 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006727 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006728 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006729#ifndef CONFIG_MMU
6730 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6731 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6732#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006733 .poll = io_uring_poll,
6734 .fasync = io_uring_fasync,
Jens Axboe87ce9552020-01-30 08:25:34 -07006735 .show_fdinfo = io_uring_show_fdinfo,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006736};
6737
6738static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6739 struct io_uring_params *p)
6740{
Hristo Venev75b28af2019-08-26 17:23:46 +00006741 struct io_rings *rings;
6742 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006743
Hristo Venev75b28af2019-08-26 17:23:46 +00006744 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6745 if (size == SIZE_MAX)
6746 return -EOVERFLOW;
6747
6748 rings = io_mem_alloc(size);
6749 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006750 return -ENOMEM;
6751
Hristo Venev75b28af2019-08-26 17:23:46 +00006752 ctx->rings = rings;
6753 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6754 rings->sq_ring_mask = p->sq_entries - 1;
6755 rings->cq_ring_mask = p->cq_entries - 1;
6756 rings->sq_ring_entries = p->sq_entries;
6757 rings->cq_ring_entries = p->cq_entries;
6758 ctx->sq_mask = rings->sq_ring_mask;
6759 ctx->cq_mask = rings->cq_ring_mask;
6760 ctx->sq_entries = rings->sq_ring_entries;
6761 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006762
6763 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006764 if (size == SIZE_MAX) {
6765 io_mem_free(ctx->rings);
6766 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006767 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006768 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006769
6770 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006771 if (!ctx->sq_sqes) {
6772 io_mem_free(ctx->rings);
6773 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006774 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006775 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006776
Jens Axboe2b188cc2019-01-07 10:46:33 -07006777 return 0;
6778}
6779
6780/*
6781 * Allocate an anonymous fd, this is what constitutes the application
6782 * visible backing of an io_uring instance. The application mmaps this
6783 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6784 * we have to tie this fd to a socket for file garbage collection purposes.
6785 */
6786static int io_uring_get_fd(struct io_ring_ctx *ctx)
6787{
6788 struct file *file;
6789 int ret;
6790
6791#if defined(CONFIG_UNIX)
6792 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6793 &ctx->ring_sock);
6794 if (ret)
6795 return ret;
6796#endif
6797
6798 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6799 if (ret < 0)
6800 goto err;
6801
6802 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6803 O_RDWR | O_CLOEXEC);
6804 if (IS_ERR(file)) {
6805 put_unused_fd(ret);
6806 ret = PTR_ERR(file);
6807 goto err;
6808 }
6809
6810#if defined(CONFIG_UNIX)
6811 ctx->ring_sock->file = file;
6812#endif
6813 fd_install(ret, file);
6814 return ret;
6815err:
6816#if defined(CONFIG_UNIX)
6817 sock_release(ctx->ring_sock);
6818 ctx->ring_sock = NULL;
6819#endif
6820 return ret;
6821}
6822
6823static int io_uring_create(unsigned entries, struct io_uring_params *p)
6824{
6825 struct user_struct *user = NULL;
6826 struct io_ring_ctx *ctx;
6827 bool account_mem;
6828 int ret;
6829
Jens Axboe8110c1a2019-12-28 15:39:54 -07006830 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006831 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006832 if (entries > IORING_MAX_ENTRIES) {
6833 if (!(p->flags & IORING_SETUP_CLAMP))
6834 return -EINVAL;
6835 entries = IORING_MAX_ENTRIES;
6836 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006837
6838 /*
6839 * Use twice as many entries for the CQ ring. It's possible for the
6840 * application to drive a higher depth than the size of the SQ ring,
6841 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006842 * some flexibility in overcommitting a bit. If the application has
6843 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6844 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006845 */
6846 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006847 if (p->flags & IORING_SETUP_CQSIZE) {
6848 /*
6849 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6850 * to a power-of-two, if it isn't already. We do NOT impose
6851 * any cq vs sq ring sizing.
6852 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006853 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006854 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006855 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6856 if (!(p->flags & IORING_SETUP_CLAMP))
6857 return -EINVAL;
6858 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6859 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006860 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6861 } else {
6862 p->cq_entries = 2 * p->sq_entries;
6863 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006864
6865 user = get_uid(current_user());
6866 account_mem = !capable(CAP_IPC_LOCK);
6867
6868 if (account_mem) {
6869 ret = io_account_mem(user,
6870 ring_pages(p->sq_entries, p->cq_entries));
6871 if (ret) {
6872 free_uid(user);
6873 return ret;
6874 }
6875 }
6876
6877 ctx = io_ring_ctx_alloc(p);
6878 if (!ctx) {
6879 if (account_mem)
6880 io_unaccount_mem(user, ring_pages(p->sq_entries,
6881 p->cq_entries));
6882 free_uid(user);
6883 return -ENOMEM;
6884 }
6885 ctx->compat = in_compat_syscall();
6886 ctx->account_mem = account_mem;
6887 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006888 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006889
6890 ret = io_allocate_scq_urings(ctx, p);
6891 if (ret)
6892 goto err;
6893
Jens Axboe6c271ce2019-01-10 11:22:30 -07006894 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006895 if (ret)
6896 goto err;
6897
Jens Axboe2b188cc2019-01-07 10:46:33 -07006898 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006899 p->sq_off.head = offsetof(struct io_rings, sq.head);
6900 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6901 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6902 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6903 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6904 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6905 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006906
6907 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006908 p->cq_off.head = offsetof(struct io_rings, cq.head);
6909 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6910 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6911 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6912 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6913 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006914
Jens Axboe044c1ab2019-10-28 09:15:33 -06006915 /*
6916 * Install ring fd as the very last thing, so we don't risk someone
6917 * having closed it before we finish setup
6918 */
6919 ret = io_uring_get_fd(ctx);
6920 if (ret < 0)
6921 goto err;
6922
Jens Axboeda8c9692019-12-02 18:51:26 -07006923 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006924 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6925 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006926 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006927 return ret;
6928err:
6929 io_ring_ctx_wait_and_kill(ctx);
6930 return ret;
6931}
6932
6933/*
6934 * Sets up an aio uring context, and returns the fd. Applications asks for a
6935 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6936 * params structure passed in.
6937 */
6938static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6939{
6940 struct io_uring_params p;
6941 long ret;
6942 int i;
6943
6944 if (copy_from_user(&p, params, sizeof(p)))
6945 return -EFAULT;
6946 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6947 if (p.resv[i])
6948 return -EINVAL;
6949 }
6950
Jens Axboe6c271ce2019-01-10 11:22:30 -07006951 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006952 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006953 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006954 return -EINVAL;
6955
6956 ret = io_uring_create(entries, &p);
6957 if (ret < 0)
6958 return ret;
6959
6960 if (copy_to_user(params, &p, sizeof(p)))
6961 return -EFAULT;
6962
6963 return ret;
6964}
6965
6966SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6967 struct io_uring_params __user *, params)
6968{
6969 return io_uring_setup(entries, params);
6970}
6971
Jens Axboe66f4af92020-01-16 15:36:52 -07006972static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6973{
6974 struct io_uring_probe *p;
6975 size_t size;
6976 int i, ret;
6977
6978 size = struct_size(p, ops, nr_args);
6979 if (size == SIZE_MAX)
6980 return -EOVERFLOW;
6981 p = kzalloc(size, GFP_KERNEL);
6982 if (!p)
6983 return -ENOMEM;
6984
6985 ret = -EFAULT;
6986 if (copy_from_user(p, arg, size))
6987 goto out;
6988 ret = -EINVAL;
6989 if (memchr_inv(p, 0, size))
6990 goto out;
6991
6992 p->last_op = IORING_OP_LAST - 1;
6993 if (nr_args > IORING_OP_LAST)
6994 nr_args = IORING_OP_LAST;
6995
6996 for (i = 0; i < nr_args; i++) {
6997 p->ops[i].op = i;
6998 if (!io_op_defs[i].not_supported)
6999 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7000 }
7001 p->ops_len = i;
7002
7003 ret = 0;
7004 if (copy_to_user(arg, p, size))
7005 ret = -EFAULT;
7006out:
7007 kfree(p);
7008 return ret;
7009}
7010
Jens Axboe071698e2020-01-28 10:04:42 -07007011static int io_register_personality(struct io_ring_ctx *ctx)
7012{
7013 const struct cred *creds = get_current_cred();
7014 int id;
7015
7016 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7017 USHRT_MAX, GFP_KERNEL);
7018 if (id < 0)
7019 put_cred(creds);
7020 return id;
7021}
7022
7023static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7024{
7025 const struct cred *old_creds;
7026
7027 old_creds = idr_remove(&ctx->personality_idr, id);
7028 if (old_creds) {
7029 put_cred(old_creds);
7030 return 0;
7031 }
7032
7033 return -EINVAL;
7034}
7035
7036static bool io_register_op_must_quiesce(int op)
7037{
7038 switch (op) {
7039 case IORING_UNREGISTER_FILES:
7040 case IORING_REGISTER_FILES_UPDATE:
7041 case IORING_REGISTER_PROBE:
7042 case IORING_REGISTER_PERSONALITY:
7043 case IORING_UNREGISTER_PERSONALITY:
7044 return false;
7045 default:
7046 return true;
7047 }
7048}
7049
Jens Axboeedafcce2019-01-09 09:16:05 -07007050static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7051 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007052 __releases(ctx->uring_lock)
7053 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007054{
7055 int ret;
7056
Jens Axboe35fa71a2019-04-22 10:23:23 -06007057 /*
7058 * We're inside the ring mutex, if the ref is already dying, then
7059 * someone else killed the ctx or is already going through
7060 * io_uring_register().
7061 */
7062 if (percpu_ref_is_dying(&ctx->refs))
7063 return -ENXIO;
7064
Jens Axboe071698e2020-01-28 10:04:42 -07007065 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007066 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007067
Jens Axboe05f3fb32019-12-09 11:22:50 -07007068 /*
7069 * Drop uring mutex before waiting for references to exit. If
7070 * another thread is currently inside io_uring_enter() it might
7071 * need to grab the uring_lock to make progress. If we hold it
7072 * here across the drain wait, then we can deadlock. It's safe
7073 * to drop the mutex here, since no new references will come in
7074 * after we've killed the percpu ref.
7075 */
7076 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007077 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007078 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007079 if (ret) {
7080 percpu_ref_resurrect(&ctx->refs);
7081 ret = -EINTR;
7082 goto out;
7083 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007084 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007085
7086 switch (opcode) {
7087 case IORING_REGISTER_BUFFERS:
7088 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7089 break;
7090 case IORING_UNREGISTER_BUFFERS:
7091 ret = -EINVAL;
7092 if (arg || nr_args)
7093 break;
7094 ret = io_sqe_buffer_unregister(ctx);
7095 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007096 case IORING_REGISTER_FILES:
7097 ret = io_sqe_files_register(ctx, arg, nr_args);
7098 break;
7099 case IORING_UNREGISTER_FILES:
7100 ret = -EINVAL;
7101 if (arg || nr_args)
7102 break;
7103 ret = io_sqe_files_unregister(ctx);
7104 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007105 case IORING_REGISTER_FILES_UPDATE:
7106 ret = io_sqe_files_update(ctx, arg, nr_args);
7107 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007108 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007109 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007110 ret = -EINVAL;
7111 if (nr_args != 1)
7112 break;
7113 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007114 if (ret)
7115 break;
7116 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7117 ctx->eventfd_async = 1;
7118 else
7119 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007120 break;
7121 case IORING_UNREGISTER_EVENTFD:
7122 ret = -EINVAL;
7123 if (arg || nr_args)
7124 break;
7125 ret = io_eventfd_unregister(ctx);
7126 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007127 case IORING_REGISTER_PROBE:
7128 ret = -EINVAL;
7129 if (!arg || nr_args > 256)
7130 break;
7131 ret = io_probe(ctx, arg, nr_args);
7132 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007133 case IORING_REGISTER_PERSONALITY:
7134 ret = -EINVAL;
7135 if (arg || nr_args)
7136 break;
7137 ret = io_register_personality(ctx);
7138 break;
7139 case IORING_UNREGISTER_PERSONALITY:
7140 ret = -EINVAL;
7141 if (arg)
7142 break;
7143 ret = io_unregister_personality(ctx, nr_args);
7144 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007145 default:
7146 ret = -EINVAL;
7147 break;
7148 }
7149
Jens Axboe071698e2020-01-28 10:04:42 -07007150 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007151 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007152 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007153out:
7154 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007155 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007156 return ret;
7157}
7158
7159SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7160 void __user *, arg, unsigned int, nr_args)
7161{
7162 struct io_ring_ctx *ctx;
7163 long ret = -EBADF;
7164 struct fd f;
7165
7166 f = fdget(fd);
7167 if (!f.file)
7168 return -EBADF;
7169
7170 ret = -EOPNOTSUPP;
7171 if (f.file->f_op != &io_uring_fops)
7172 goto out_fput;
7173
7174 ctx = f.file->private_data;
7175
7176 mutex_lock(&ctx->uring_lock);
7177 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7178 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007179 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7180 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007181out_fput:
7182 fdput(f);
7183 return ret;
7184}
7185
Jens Axboe2b188cc2019-01-07 10:46:33 -07007186static int __init io_uring_init(void)
7187{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007188#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7189 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7190 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7191} while (0)
7192
7193#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7194 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7195 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7196 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7197 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7198 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7199 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7200 BUILD_BUG_SQE_ELEM(8, __u64, off);
7201 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7202 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7203 BUILD_BUG_SQE_ELEM(24, __u32, len);
7204 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7205 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7206 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7207 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7208 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7209 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7210 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7211 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7212 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7213 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7214 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7215 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7216 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7217 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7218 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7219 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7220
Jens Axboed3656342019-12-18 09:50:26 -07007221 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007222 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7223 return 0;
7224};
7225__initcall(io_uring_init);