blob: 0977017823395ec8662448ca74be1e1557f1af58 [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;
445};
446
Jens Axboef67676d2019-12-02 11:03:47 -0700447struct io_async_rw {
448 struct iovec fast_iov[UIO_FASTIOV];
449 struct iovec *iov;
450 ssize_t nr_segs;
451 ssize_t size;
452};
453
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700454struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700455 union {
456 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700457 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700458 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700459 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700460 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700461};
462
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300463enum {
464 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
465 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
466 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
467 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
468 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
469
470 REQ_F_LINK_NEXT_BIT,
471 REQ_F_FAIL_LINK_BIT,
472 REQ_F_INFLIGHT_BIT,
473 REQ_F_CUR_POS_BIT,
474 REQ_F_NOWAIT_BIT,
475 REQ_F_IOPOLL_COMPLETED_BIT,
476 REQ_F_LINK_TIMEOUT_BIT,
477 REQ_F_TIMEOUT_BIT,
478 REQ_F_ISREG_BIT,
479 REQ_F_MUST_PUNT_BIT,
480 REQ_F_TIMEOUT_NOSEQ_BIT,
481 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300482 REQ_F_NEED_CLEANUP_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300483};
484
485enum {
486 /* ctx owns file */
487 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
488 /* drain existing IO first */
489 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
490 /* linked sqes */
491 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
492 /* doesn't sever on completion < 0 */
493 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
494 /* IOSQE_ASYNC */
495 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
496
497 /* already grabbed next link */
498 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
499 /* fail rest of links */
500 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
501 /* on inflight list */
502 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
503 /* read/write uses file position */
504 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
505 /* must not punt to workers */
506 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
507 /* polled IO has completed */
508 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
509 /* has linked timeout */
510 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
511 /* timeout request */
512 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
513 /* regular file */
514 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
515 /* must be punted even for NONBLOCK */
516 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
517 /* no timeout sequence */
518 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
519 /* completion under lock */
520 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300521 /* needs cleanup */
522 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300523};
524
Jens Axboe09bb8392019-03-13 12:39:28 -0600525/*
526 * NOTE! Each of the iocb union members has the file pointer
527 * as the first entry in their struct definition. So you can
528 * access the file pointer through any of the sub-structs,
529 * or directly as just 'ki_filp' in this struct.
530 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700531struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700532 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600533 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700534 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700535 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700536 struct io_accept accept;
537 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700538 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700539 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700540 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700541 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700542 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700543 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700544 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700545 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700546 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700547 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700548 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700549
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700550 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300551 /*
552 * llist_node is only used for poll deferred completions
553 */
554 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300555 bool in_async;
556 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700557 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700558
559 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700560 union {
561 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700562 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700563 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600564 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700565 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700566 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700567 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600568 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600569 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700570
Jens Axboefcb323c2019-10-24 12:39:47 -0600571 struct list_head inflight_entry;
572
Jens Axboe561fb042019-10-24 07:25:42 -0600573 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574};
575
576#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700577#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700578
Jens Axboe9a56a232019-01-09 09:06:50 -0700579struct io_submit_state {
580 struct blk_plug plug;
581
582 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700583 * io_kiocb alloc cache
584 */
585 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300586 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700587
588 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700589 * File reference cache
590 */
591 struct file *file;
592 unsigned int fd;
593 unsigned int has_refs;
594 unsigned int used_refs;
595 unsigned int ios_left;
596};
597
Jens Axboed3656342019-12-18 09:50:26 -0700598struct io_op_def {
599 /* needs req->io allocated for deferral/async */
600 unsigned async_ctx : 1;
601 /* needs current->mm setup, does mm access */
602 unsigned needs_mm : 1;
603 /* needs req->file assigned */
604 unsigned needs_file : 1;
605 /* needs req->file assigned IFF fd is >= 0 */
606 unsigned fd_non_neg : 1;
607 /* hash wq insertion if file is a regular file */
608 unsigned hash_reg_file : 1;
609 /* unbound wq insertion if file is a non-regular file */
610 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700611 /* opcode is not supported by this kernel */
612 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700613 /* needs file table */
614 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700615 /* needs ->fs */
616 unsigned needs_fs : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700617};
618
619static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300620 [IORING_OP_NOP] = {},
621 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700622 .async_ctx = 1,
623 .needs_mm = 1,
624 .needs_file = 1,
625 .unbound_nonreg_file = 1,
626 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300627 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700628 .async_ctx = 1,
629 .needs_mm = 1,
630 .needs_file = 1,
631 .hash_reg_file = 1,
632 .unbound_nonreg_file = 1,
633 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300634 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700635 .needs_file = 1,
636 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300637 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700638 .needs_file = 1,
639 .unbound_nonreg_file = 1,
640 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300641 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700642 .needs_file = 1,
643 .hash_reg_file = 1,
644 .unbound_nonreg_file = 1,
645 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300646 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700647 .needs_file = 1,
648 .unbound_nonreg_file = 1,
649 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300650 [IORING_OP_POLL_REMOVE] = {},
651 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700652 .needs_file = 1,
653 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300654 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700655 .async_ctx = 1,
656 .needs_mm = 1,
657 .needs_file = 1,
658 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700659 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700660 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300661 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700662 .async_ctx = 1,
663 .needs_mm = 1,
664 .needs_file = 1,
665 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700666 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700667 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300668 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700669 .async_ctx = 1,
670 .needs_mm = 1,
671 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300672 [IORING_OP_TIMEOUT_REMOVE] = {},
673 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700674 .needs_mm = 1,
675 .needs_file = 1,
676 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700677 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700678 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300679 [IORING_OP_ASYNC_CANCEL] = {},
680 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700681 .async_ctx = 1,
682 .needs_mm = 1,
683 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300684 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700685 .async_ctx = 1,
686 .needs_mm = 1,
687 .needs_file = 1,
688 .unbound_nonreg_file = 1,
689 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300690 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700691 .needs_file = 1,
692 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300693 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700694 .needs_file = 1,
695 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700696 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700697 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700698 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300699 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700700 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700701 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700702 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300703 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700704 .needs_mm = 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_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700708 .needs_mm = 1,
709 .needs_file = 1,
710 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700711 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700712 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300713 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700714 .needs_mm = 1,
715 .needs_file = 1,
716 .unbound_nonreg_file = 1,
717 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300718 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700719 .needs_mm = 1,
720 .needs_file = 1,
721 .unbound_nonreg_file = 1,
722 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300723 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700724 .needs_file = 1,
725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700727 .needs_mm = 1,
728 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700730 .needs_mm = 1,
731 .needs_file = 1,
732 .unbound_nonreg_file = 1,
733 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300734 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700735 .needs_mm = 1,
736 .needs_file = 1,
737 .unbound_nonreg_file = 1,
738 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300739 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700740 .needs_file = 1,
741 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700742 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700743 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700744 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700745 [IORING_OP_EPOLL_CTL] = {
746 .unbound_nonreg_file = 1,
747 .file_table = 1,
748 },
Jens Axboed3656342019-12-18 09:50:26 -0700749};
750
Jens Axboe561fb042019-10-24 07:25:42 -0600751static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700752static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800753static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700754static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700755static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
756static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700757static int __io_sqe_files_update(struct io_ring_ctx *ctx,
758 struct io_uring_files_update *ip,
759 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700760static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700761static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300762static void io_cleanup_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600763
Jens Axboe2b188cc2019-01-07 10:46:33 -0700764static struct kmem_cache *req_cachep;
765
766static const struct file_operations io_uring_fops;
767
768struct sock *io_uring_get_socket(struct file *file)
769{
770#if defined(CONFIG_UNIX)
771 if (file->f_op == &io_uring_fops) {
772 struct io_ring_ctx *ctx = file->private_data;
773
774 return ctx->ring_sock->sk;
775 }
776#endif
777 return NULL;
778}
779EXPORT_SYMBOL(io_uring_get_socket);
780
781static void io_ring_ctx_ref_free(struct percpu_ref *ref)
782{
783 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
784
Jens Axboe206aefd2019-11-07 18:27:42 -0700785 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700786}
787
788static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
789{
790 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700791 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700792
793 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
794 if (!ctx)
795 return NULL;
796
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700797 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
798 if (!ctx->fallback_req)
799 goto err;
800
Jens Axboe206aefd2019-11-07 18:27:42 -0700801 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
802 if (!ctx->completions)
803 goto err;
804
Jens Axboe78076bb2019-12-04 19:56:40 -0700805 /*
806 * Use 5 bits less than the max cq entries, that should give us around
807 * 32 entries per hash list if totally full and uniformly spread.
808 */
809 hash_bits = ilog2(p->cq_entries);
810 hash_bits -= 5;
811 if (hash_bits <= 0)
812 hash_bits = 1;
813 ctx->cancel_hash_bits = hash_bits;
814 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
815 GFP_KERNEL);
816 if (!ctx->cancel_hash)
817 goto err;
818 __hash_init(ctx->cancel_hash, 1U << hash_bits);
819
Roman Gushchin21482892019-05-07 10:01:48 -0700820 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700821 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
822 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700823
824 ctx->flags = p->flags;
825 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700826 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700827 init_completion(&ctx->completions[0]);
828 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700829 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700830 mutex_init(&ctx->uring_lock);
831 init_waitqueue_head(&ctx->wait);
832 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700833 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700834 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600835 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600836 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600837 init_waitqueue_head(&ctx->inflight_wait);
838 spin_lock_init(&ctx->inflight_lock);
839 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700840 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700841err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700842 if (ctx->fallback_req)
843 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700844 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700845 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700846 kfree(ctx);
847 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700848}
849
Bob Liu9d858b22019-11-13 18:06:25 +0800850static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600851{
Jackie Liua197f662019-11-08 08:09:12 -0700852 struct io_ring_ctx *ctx = req->ctx;
853
Jens Axboe498ccd92019-10-25 10:04:25 -0600854 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
855 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600856}
857
Bob Liu9d858b22019-11-13 18:06:25 +0800858static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600859{
Pavel Begunkov87987892020-01-18 01:22:30 +0300860 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800861 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600862
Bob Liu9d858b22019-11-13 18:06:25 +0800863 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600864}
865
866static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600867{
868 struct io_kiocb *req;
869
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600870 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800871 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600872 list_del_init(&req->list);
873 return req;
874 }
875
876 return NULL;
877}
878
Jens Axboe5262f562019-09-17 12:26:57 -0600879static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
880{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600881 struct io_kiocb *req;
882
883 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700884 if (req) {
885 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
886 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800887 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700888 list_del_init(&req->list);
889 return req;
890 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600891 }
892
893 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600894}
895
Jens Axboede0617e2019-04-06 21:51:27 -0600896static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700897{
Hristo Venev75b28af2019-08-26 17:23:46 +0000898 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700899
Pavel Begunkov07910152020-01-17 03:52:46 +0300900 /* order cqe stores with ring update */
901 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700902
Pavel Begunkov07910152020-01-17 03:52:46 +0300903 if (wq_has_sleeper(&ctx->cq_wait)) {
904 wake_up_interruptible(&ctx->cq_wait);
905 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700906 }
907}
908
Jens Axboecccf0ee2020-01-27 16:34:48 -0700909static inline void io_req_work_grab_env(struct io_kiocb *req,
910 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600911{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700912 if (!req->work.mm && def->needs_mm) {
913 mmgrab(current->mm);
914 req->work.mm = current->mm;
915 }
916 if (!req->work.creds)
917 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700918 if (!req->work.fs && def->needs_fs) {
919 spin_lock(&current->fs->lock);
920 if (!current->fs->in_exec) {
921 req->work.fs = current->fs;
922 req->work.fs->users++;
923 } else {
924 req->work.flags |= IO_WQ_WORK_CANCEL;
925 }
926 spin_unlock(&current->fs->lock);
927 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700928}
929
930static inline void io_req_work_drop_env(struct io_kiocb *req)
931{
932 if (req->work.mm) {
933 mmdrop(req->work.mm);
934 req->work.mm = NULL;
935 }
936 if (req->work.creds) {
937 put_cred(req->work.creds);
938 req->work.creds = NULL;
939 }
Jens Axboeff002b32020-02-07 16:05:21 -0700940 if (req->work.fs) {
941 struct fs_struct *fs = req->work.fs;
942
943 spin_lock(&req->work.fs->lock);
944 if (--fs->users)
945 fs = NULL;
946 spin_unlock(&req->work.fs->lock);
947 if (fs)
948 free_fs_struct(fs);
949 }
Jens Axboe561fb042019-10-24 07:25:42 -0600950}
951
Jens Axboe94ae5e72019-11-14 19:39:52 -0700952static inline bool io_prep_async_work(struct io_kiocb *req,
953 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600954{
Jens Axboed3656342019-12-18 09:50:26 -0700955 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600956 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600957
Jens Axboed3656342019-12-18 09:50:26 -0700958 if (req->flags & REQ_F_ISREG) {
959 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700960 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700961 } else {
962 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700963 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600964 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700965
966 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600967
Jens Axboe94ae5e72019-11-14 19:39:52 -0700968 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600969 return do_hashed;
970}
971
Jackie Liua197f662019-11-08 08:09:12 -0700972static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600973{
Jackie Liua197f662019-11-08 08:09:12 -0700974 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700975 struct io_kiocb *link;
976 bool do_hashed;
977
978 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600979
980 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
981 req->flags);
982 if (!do_hashed) {
983 io_wq_enqueue(ctx->io_wq, &req->work);
984 } else {
985 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
986 file_inode(req->file));
987 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700988
989 if (link)
990 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600991}
992
Jens Axboe5262f562019-09-17 12:26:57 -0600993static void io_kill_timeout(struct io_kiocb *req)
994{
995 int ret;
996
Jens Axboe2d283902019-12-04 11:08:05 -0700997 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600998 if (ret != -1) {
999 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001000 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001001 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001002 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001003 }
1004}
1005
1006static void io_kill_timeouts(struct io_ring_ctx *ctx)
1007{
1008 struct io_kiocb *req, *tmp;
1009
1010 spin_lock_irq(&ctx->completion_lock);
1011 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1012 io_kill_timeout(req);
1013 spin_unlock_irq(&ctx->completion_lock);
1014}
1015
Jens Axboede0617e2019-04-06 21:51:27 -06001016static void io_commit_cqring(struct io_ring_ctx *ctx)
1017{
1018 struct io_kiocb *req;
1019
Jens Axboe5262f562019-09-17 12:26:57 -06001020 while ((req = io_get_timeout_req(ctx)) != NULL)
1021 io_kill_timeout(req);
1022
Jens Axboede0617e2019-04-06 21:51:27 -06001023 __io_commit_cqring(ctx);
1024
Pavel Begunkov87987892020-01-18 01:22:30 +03001025 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001026 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001027}
1028
Jens Axboe2b188cc2019-01-07 10:46:33 -07001029static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1030{
Hristo Venev75b28af2019-08-26 17:23:46 +00001031 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001032 unsigned tail;
1033
1034 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001035 /*
1036 * writes to the cq entry need to come after reading head; the
1037 * control dependency is enough as we're using WRITE_ONCE to
1038 * fill the cq entry
1039 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001040 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001041 return NULL;
1042
1043 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001044 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001045}
1046
Jens Axboef2842ab2020-01-08 11:04:00 -07001047static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1048{
Jens Axboef0b493e2020-02-01 21:30:11 -07001049 if (!ctx->cq_ev_fd)
1050 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001051 if (!ctx->eventfd_async)
1052 return true;
1053 return io_wq_current_is_worker() || in_interrupt();
1054}
1055
Jens Axboef0b493e2020-02-01 21:30:11 -07001056static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001057{
1058 if (waitqueue_active(&ctx->wait))
1059 wake_up(&ctx->wait);
1060 if (waitqueue_active(&ctx->sqo_wait))
1061 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001062 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001063 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001064}
1065
Jens Axboef0b493e2020-02-01 21:30:11 -07001066static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1067{
1068 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1069}
1070
Jens Axboec4a2ed72019-11-21 21:01:26 -07001071/* Returns true if there are no backlogged entries after the flush */
1072static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001074 struct io_rings *rings = ctx->rings;
1075 struct io_uring_cqe *cqe;
1076 struct io_kiocb *req;
1077 unsigned long flags;
1078 LIST_HEAD(list);
1079
1080 if (!force) {
1081 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001082 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001083 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1084 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001085 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001086 }
1087
1088 spin_lock_irqsave(&ctx->completion_lock, flags);
1089
1090 /* if force is set, the ring is going away. always drop after that */
1091 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001092 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001093
Jens Axboec4a2ed72019-11-21 21:01:26 -07001094 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001095 while (!list_empty(&ctx->cq_overflow_list)) {
1096 cqe = io_get_cqring(ctx);
1097 if (!cqe && !force)
1098 break;
1099
1100 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1101 list);
1102 list_move(&req->list, &list);
1103 if (cqe) {
1104 WRITE_ONCE(cqe->user_data, req->user_data);
1105 WRITE_ONCE(cqe->res, req->result);
1106 WRITE_ONCE(cqe->flags, 0);
1107 } else {
1108 WRITE_ONCE(ctx->rings->cq_overflow,
1109 atomic_inc_return(&ctx->cached_cq_overflow));
1110 }
1111 }
1112
1113 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001114 if (cqe) {
1115 clear_bit(0, &ctx->sq_check_overflow);
1116 clear_bit(0, &ctx->cq_check_overflow);
1117 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001118 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1119 io_cqring_ev_posted(ctx);
1120
1121 while (!list_empty(&list)) {
1122 req = list_first_entry(&list, struct io_kiocb, list);
1123 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001124 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001125 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001126
1127 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001128}
1129
Jens Axboe78e19bb2019-11-06 15:21:34 -07001130static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001131{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001132 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001133 struct io_uring_cqe *cqe;
1134
Jens Axboe78e19bb2019-11-06 15:21:34 -07001135 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001136
Jens Axboe2b188cc2019-01-07 10:46:33 -07001137 /*
1138 * If we can't get a cq entry, userspace overflowed the
1139 * submission (by quite a lot). Increment the overflow count in
1140 * the ring.
1141 */
1142 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001143 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001144 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001145 WRITE_ONCE(cqe->res, res);
1146 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001147 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001148 WRITE_ONCE(ctx->rings->cq_overflow,
1149 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001150 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001151 if (list_empty(&ctx->cq_overflow_list)) {
1152 set_bit(0, &ctx->sq_check_overflow);
1153 set_bit(0, &ctx->cq_check_overflow);
1154 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001155 refcount_inc(&req->refs);
1156 req->result = res;
1157 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001158 }
1159}
1160
Jens Axboe78e19bb2019-11-06 15:21:34 -07001161static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001162{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001163 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001164 unsigned long flags;
1165
1166 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001167 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001168 io_commit_cqring(ctx);
1169 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1170
Jens Axboe8c838782019-03-12 15:48:16 -06001171 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001172}
1173
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001174static inline bool io_is_fallback_req(struct io_kiocb *req)
1175{
1176 return req == (struct io_kiocb *)
1177 ((unsigned long) req->ctx->fallback_req & ~1UL);
1178}
1179
1180static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1181{
1182 struct io_kiocb *req;
1183
1184 req = ctx->fallback_req;
1185 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1186 return req;
1187
1188 return NULL;
1189}
1190
Jens Axboe2579f912019-01-09 09:10:43 -07001191static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1192 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001193{
Jens Axboefd6fab22019-03-14 16:30:06 -06001194 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001195 struct io_kiocb *req;
1196
Jens Axboe2579f912019-01-09 09:10:43 -07001197 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001198 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001199 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001200 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001201 } else if (!state->free_reqs) {
1202 size_t sz;
1203 int ret;
1204
1205 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001206 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1207
1208 /*
1209 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1210 * retry single alloc to be on the safe side.
1211 */
1212 if (unlikely(ret <= 0)) {
1213 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1214 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001215 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001216 ret = 1;
1217 }
Jens Axboe2579f912019-01-09 09:10:43 -07001218 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001219 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001220 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001221 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001222 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001223 }
1224
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001225got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001226 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001227 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001228 req->ctx = ctx;
1229 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001230 /* one is dropped after submission, the other at completion */
1231 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001232 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001233 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001234 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001235fallback:
1236 req = io_get_fallback_req(ctx);
1237 if (req)
1238 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001239 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001240 return NULL;
1241}
1242
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001243static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001244{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001245 if (likely(!io_is_fallback_req(req)))
1246 kmem_cache_free(req_cachep, req);
1247 else
1248 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1249}
1250
Jens Axboec6ca97b302019-12-28 12:11:08 -07001251static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001252{
Jens Axboefcb323c2019-10-24 12:39:47 -06001253 struct io_ring_ctx *ctx = req->ctx;
1254
YueHaibing96fd84d2020-01-07 22:22:44 +08001255 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001256 if (req->file) {
1257 if (req->flags & REQ_F_FIXED_FILE)
1258 percpu_ref_put(&ctx->file_data->refs);
1259 else
1260 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001261 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001262
1263 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264}
1265
1266static void __io_free_req(struct io_kiocb *req)
1267{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001268 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03001270 if (req->flags & REQ_F_NEED_CLEANUP)
1271 io_cleanup_req(req);
1272
Jens Axboefcb323c2019-10-24 12:39:47 -06001273 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001274 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001275 unsigned long flags;
1276
1277 spin_lock_irqsave(&ctx->inflight_lock, flags);
1278 list_del(&req->inflight_entry);
1279 if (waitqueue_active(&ctx->inflight_wait))
1280 wake_up(&ctx->inflight_wait);
1281 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1282 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001283
1284 percpu_ref_put(&req->ctx->refs);
1285 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001286}
1287
Jens Axboec6ca97b302019-12-28 12:11:08 -07001288struct req_batch {
1289 void *reqs[IO_IOPOLL_BATCH];
1290 int to_free;
1291 int need_iter;
1292};
1293
1294static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1295{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001296 int fixed_refs = rb->to_free;
1297
Jens Axboec6ca97b302019-12-28 12:11:08 -07001298 if (!rb->to_free)
1299 return;
1300 if (rb->need_iter) {
1301 int i, inflight = 0;
1302 unsigned long flags;
1303
Jens Axboe10fef4b2020-01-09 07:52:28 -07001304 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001305 for (i = 0; i < rb->to_free; i++) {
1306 struct io_kiocb *req = rb->reqs[i];
1307
Jens Axboe10fef4b2020-01-09 07:52:28 -07001308 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001309 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001310 fixed_refs++;
1311 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001312 if (req->flags & REQ_F_INFLIGHT)
1313 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001314 __io_req_aux_free(req);
1315 }
1316 if (!inflight)
1317 goto do_free;
1318
1319 spin_lock_irqsave(&ctx->inflight_lock, flags);
1320 for (i = 0; i < rb->to_free; i++) {
1321 struct io_kiocb *req = rb->reqs[i];
1322
Jens Axboe10fef4b2020-01-09 07:52:28 -07001323 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001324 list_del(&req->inflight_entry);
1325 if (!--inflight)
1326 break;
1327 }
1328 }
1329 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1330
1331 if (waitqueue_active(&ctx->inflight_wait))
1332 wake_up(&ctx->inflight_wait);
1333 }
1334do_free:
1335 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001336 if (fixed_refs)
1337 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001338 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001339 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001340}
1341
Jackie Liua197f662019-11-08 08:09:12 -07001342static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001343{
Jackie Liua197f662019-11-08 08:09:12 -07001344 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001345 int ret;
1346
Jens Axboe2d283902019-12-04 11:08:05 -07001347 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001348 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001349 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001350 io_commit_cqring(ctx);
1351 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001352 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001353 return true;
1354 }
1355
1356 return false;
1357}
1358
Jens Axboeba816ad2019-09-28 11:36:45 -06001359static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001360{
Jens Axboe2665abf2019-11-05 12:40:47 -07001361 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001362 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001363
Jens Axboe4d7dd462019-11-20 13:03:52 -07001364 /* Already got next link */
1365 if (req->flags & REQ_F_LINK_NEXT)
1366 return;
1367
Jens Axboe9e645e112019-05-10 16:07:28 -06001368 /*
1369 * The list should never be empty when we are called here. But could
1370 * potentially happen if the chain is messed up, check to be on the
1371 * safe side.
1372 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001373 while (!list_empty(&req->link_list)) {
1374 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1375 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001376
Pavel Begunkov44932332019-12-05 16:16:35 +03001377 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1378 (nxt->flags & REQ_F_TIMEOUT))) {
1379 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001380 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001381 req->flags &= ~REQ_F_LINK_TIMEOUT;
1382 continue;
1383 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001384
Pavel Begunkov44932332019-12-05 16:16:35 +03001385 list_del_init(&req->link_list);
1386 if (!list_empty(&nxt->link_list))
1387 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001388 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001389 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001390 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001391
Jens Axboe4d7dd462019-11-20 13:03:52 -07001392 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001393 if (wake_ev)
1394 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001395}
1396
1397/*
1398 * Called if REQ_F_LINK is set, and we fail the head request
1399 */
1400static void io_fail_links(struct io_kiocb *req)
1401{
Jens Axboe2665abf2019-11-05 12:40:47 -07001402 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001403 unsigned long flags;
1404
1405 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001406
1407 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001408 struct io_kiocb *link = list_first_entry(&req->link_list,
1409 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001410
Pavel Begunkov44932332019-12-05 16:16:35 +03001411 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001412 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001413
1414 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001415 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001416 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001417 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001418 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001419 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001420 }
Jens Axboe5d960722019-11-19 15:31:28 -07001421 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001422 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001423
1424 io_commit_cqring(ctx);
1425 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1426 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001427}
1428
Jens Axboe4d7dd462019-11-20 13:03:52 -07001429static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001430{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001431 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001432 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001433
Jens Axboe9e645e112019-05-10 16:07:28 -06001434 /*
1435 * If LINK is set, we have dependent requests in this chain. If we
1436 * didn't fail this request, queue the first one up, moving any other
1437 * dependencies to the next request. In case of failure, fail the rest
1438 * of the chain.
1439 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001440 if (req->flags & REQ_F_FAIL_LINK) {
1441 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001442 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1443 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001444 struct io_ring_ctx *ctx = req->ctx;
1445 unsigned long flags;
1446
1447 /*
1448 * If this is a timeout link, we could be racing with the
1449 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001450 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001451 */
1452 spin_lock_irqsave(&ctx->completion_lock, flags);
1453 io_req_link_next(req, nxt);
1454 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1455 } else {
1456 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001457 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001458}
Jens Axboe9e645e112019-05-10 16:07:28 -06001459
Jackie Liuc69f8db2019-11-09 11:00:08 +08001460static void io_free_req(struct io_kiocb *req)
1461{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001462 struct io_kiocb *nxt = NULL;
1463
1464 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001465 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001466
1467 if (nxt)
1468 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001469}
1470
Jens Axboeba816ad2019-09-28 11:36:45 -06001471/*
1472 * Drop reference to request, return next in chain (if there is one) if this
1473 * was the last reference to this request.
1474 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001475__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001476static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001477{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001478 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001479
Jens Axboee65ef562019-03-12 10:16:44 -06001480 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001481 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001482}
1483
Jens Axboe2b188cc2019-01-07 10:46:33 -07001484static void io_put_req(struct io_kiocb *req)
1485{
Jens Axboedef596e2019-01-09 08:59:42 -07001486 if (refcount_dec_and_test(&req->refs))
1487 io_free_req(req);
1488}
1489
Jens Axboe978db572019-11-14 22:39:04 -07001490/*
1491 * Must only be used if we don't need to care about links, usually from
1492 * within the completion handling itself.
1493 */
1494static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001495{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001496 /* drop both submit and complete references */
1497 if (refcount_sub_and_test(2, &req->refs))
1498 __io_free_req(req);
1499}
1500
Jens Axboe978db572019-11-14 22:39:04 -07001501static void io_double_put_req(struct io_kiocb *req)
1502{
1503 /* drop both submit and complete references */
1504 if (refcount_sub_and_test(2, &req->refs))
1505 io_free_req(req);
1506}
1507
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001508static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001509{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001510 struct io_rings *rings = ctx->rings;
1511
Jens Axboead3eb2c2019-12-18 17:12:20 -07001512 if (test_bit(0, &ctx->cq_check_overflow)) {
1513 /*
1514 * noflush == true is from the waitqueue handler, just ensure
1515 * we wake up the task, and the next invocation will flush the
1516 * entries. We cannot safely to it from here.
1517 */
1518 if (noflush && !list_empty(&ctx->cq_overflow_list))
1519 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001520
Jens Axboead3eb2c2019-12-18 17:12:20 -07001521 io_cqring_overflow_flush(ctx, false);
1522 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001523
Jens Axboea3a0e432019-08-20 11:03:11 -06001524 /* See comment at the top of this file */
1525 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001526 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001527}
1528
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001529static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1530{
1531 struct io_rings *rings = ctx->rings;
1532
1533 /* make sure SQ entry isn't read before tail */
1534 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1535}
1536
Jens Axboe8237e042019-12-28 10:48:22 -07001537static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001538{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001539 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1540 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001541
Jens Axboec6ca97b302019-12-28 12:11:08 -07001542 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1543 rb->need_iter++;
1544
1545 rb->reqs[rb->to_free++] = req;
1546 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1547 io_free_req_many(req->ctx, rb);
1548 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001549}
1550
Jens Axboedef596e2019-01-09 08:59:42 -07001551/*
1552 * Find and free completed poll iocbs
1553 */
1554static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1555 struct list_head *done)
1556{
Jens Axboe8237e042019-12-28 10:48:22 -07001557 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001558 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001559
Jens Axboec6ca97b302019-12-28 12:11:08 -07001560 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001561 while (!list_empty(done)) {
1562 req = list_first_entry(done, struct io_kiocb, list);
1563 list_del(&req->list);
1564
Jens Axboe78e19bb2019-11-06 15:21:34 -07001565 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001566 (*nr_events)++;
1567
Jens Axboe8237e042019-12-28 10:48:22 -07001568 if (refcount_dec_and_test(&req->refs) &&
1569 !io_req_multi_free(&rb, req))
1570 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001571 }
Jens Axboedef596e2019-01-09 08:59:42 -07001572
Jens Axboe09bb8392019-03-13 12:39:28 -06001573 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001574 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001575}
1576
1577static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1578 long min)
1579{
1580 struct io_kiocb *req, *tmp;
1581 LIST_HEAD(done);
1582 bool spin;
1583 int ret;
1584
1585 /*
1586 * Only spin for completions if we don't have multiple devices hanging
1587 * off our complete list, and we're under the requested amount.
1588 */
1589 spin = !ctx->poll_multi_file && *nr_events < min;
1590
1591 ret = 0;
1592 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001593 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001594
1595 /*
1596 * Move completed entries to our local list. If we find a
1597 * request that requires polling, break out and complete
1598 * the done list first, if we have entries there.
1599 */
1600 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1601 list_move_tail(&req->list, &done);
1602 continue;
1603 }
1604 if (!list_empty(&done))
1605 break;
1606
1607 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1608 if (ret < 0)
1609 break;
1610
1611 if (ret && spin)
1612 spin = false;
1613 ret = 0;
1614 }
1615
1616 if (!list_empty(&done))
1617 io_iopoll_complete(ctx, nr_events, &done);
1618
1619 return ret;
1620}
1621
1622/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001623 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001624 * non-spinning poll check - we'll still enter the driver poll loop, but only
1625 * as a non-spinning completion check.
1626 */
1627static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1628 long min)
1629{
Jens Axboe08f54392019-08-21 22:19:11 -06001630 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001631 int ret;
1632
1633 ret = io_do_iopoll(ctx, nr_events, min);
1634 if (ret < 0)
1635 return ret;
1636 if (!min || *nr_events >= min)
1637 return 0;
1638 }
1639
1640 return 1;
1641}
1642
1643/*
1644 * We can't just wait for polled events to come to us, we have to actively
1645 * find and complete them.
1646 */
1647static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1648{
1649 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1650 return;
1651
1652 mutex_lock(&ctx->uring_lock);
1653 while (!list_empty(&ctx->poll_list)) {
1654 unsigned int nr_events = 0;
1655
1656 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001657
1658 /*
1659 * Ensure we allow local-to-the-cpu processing to take place,
1660 * in this case we need to ensure that we reap all events.
1661 */
1662 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001663 }
1664 mutex_unlock(&ctx->uring_lock);
1665}
1666
Jens Axboe2b2ed972019-10-25 10:06:15 -06001667static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1668 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001669{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001670 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001671
1672 do {
1673 int tmin = 0;
1674
Jens Axboe500f9fb2019-08-19 12:15:59 -06001675 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001676 * Don't enter poll loop if we already have events pending.
1677 * If we do, we can potentially be spinning for commands that
1678 * already triggered a CQE (eg in error).
1679 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001680 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001681 break;
1682
1683 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001684 * If a submit got punted to a workqueue, we can have the
1685 * application entering polling for a command before it gets
1686 * issued. That app will hold the uring_lock for the duration
1687 * of the poll right here, so we need to take a breather every
1688 * now and then to ensure that the issue has a chance to add
1689 * the poll to the issued list. Otherwise we can spin here
1690 * forever, while the workqueue is stuck trying to acquire the
1691 * very same mutex.
1692 */
1693 if (!(++iters & 7)) {
1694 mutex_unlock(&ctx->uring_lock);
1695 mutex_lock(&ctx->uring_lock);
1696 }
1697
Jens Axboedef596e2019-01-09 08:59:42 -07001698 if (*nr_events < min)
1699 tmin = min - *nr_events;
1700
1701 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1702 if (ret <= 0)
1703 break;
1704 ret = 0;
1705 } while (min && !*nr_events && !need_resched());
1706
Jens Axboe2b2ed972019-10-25 10:06:15 -06001707 return ret;
1708}
1709
1710static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1711 long min)
1712{
1713 int ret;
1714
1715 /*
1716 * We disallow the app entering submit/complete with polling, but we
1717 * still need to lock the ring to prevent racing with polled issue
1718 * that got punted to a workqueue.
1719 */
1720 mutex_lock(&ctx->uring_lock);
1721 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001722 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001723 return ret;
1724}
1725
Jens Axboe491381ce2019-10-17 09:20:46 -06001726static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727{
Jens Axboe491381ce2019-10-17 09:20:46 -06001728 /*
1729 * Tell lockdep we inherited freeze protection from submission
1730 * thread.
1731 */
1732 if (req->flags & REQ_F_ISREG) {
1733 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734
Jens Axboe491381ce2019-10-17 09:20:46 -06001735 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001737 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001738}
1739
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001740static inline void req_set_fail_links(struct io_kiocb *req)
1741{
1742 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1743 req->flags |= REQ_F_FAIL_LINK;
1744}
1745
Jens Axboeba816ad2019-09-28 11:36:45 -06001746static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001747{
Jens Axboe9adbd452019-12-20 08:45:55 -07001748 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001749
Jens Axboe491381ce2019-10-17 09:20:46 -06001750 if (kiocb->ki_flags & IOCB_WRITE)
1751 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001752
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001753 if (res != req->result)
1754 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001755 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001756}
1757
1758static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1759{
Jens Axboe9adbd452019-12-20 08:45:55 -07001760 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001761
1762 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001763 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001764}
1765
Jens Axboeba816ad2019-09-28 11:36:45 -06001766static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1767{
Jens Axboe9adbd452019-12-20 08:45:55 -07001768 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001769 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001770
1771 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001772 io_put_req_find_next(req, &nxt);
1773
1774 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001775}
1776
Jens Axboedef596e2019-01-09 08:59:42 -07001777static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1778{
Jens Axboe9adbd452019-12-20 08:45:55 -07001779 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001780
Jens Axboe491381ce2019-10-17 09:20:46 -06001781 if (kiocb->ki_flags & IOCB_WRITE)
1782 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001783
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001784 if (res != req->result)
1785 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001786 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001787 if (res != -EAGAIN)
1788 req->flags |= REQ_F_IOPOLL_COMPLETED;
1789}
1790
1791/*
1792 * After the iocb has been issued, it's safe to be found on the poll list.
1793 * Adding the kiocb to the list AFTER submission ensures that we don't
1794 * find it from a io_iopoll_getevents() thread before the issuer is done
1795 * accessing the kiocb cookie.
1796 */
1797static void io_iopoll_req_issued(struct io_kiocb *req)
1798{
1799 struct io_ring_ctx *ctx = req->ctx;
1800
1801 /*
1802 * Track whether we have multiple files in our lists. This will impact
1803 * how we do polling eventually, not spinning if we're on potentially
1804 * different devices.
1805 */
1806 if (list_empty(&ctx->poll_list)) {
1807 ctx->poll_multi_file = false;
1808 } else if (!ctx->poll_multi_file) {
1809 struct io_kiocb *list_req;
1810
1811 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1812 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001813 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001814 ctx->poll_multi_file = true;
1815 }
1816
1817 /*
1818 * For fast devices, IO may have already completed. If it has, add
1819 * it to the front so we find it first.
1820 */
1821 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1822 list_add(&req->list, &ctx->poll_list);
1823 else
1824 list_add_tail(&req->list, &ctx->poll_list);
1825}
1826
Jens Axboe3d6770f2019-04-13 11:50:54 -06001827static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001828{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001829 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001830 int diff = state->has_refs - state->used_refs;
1831
1832 if (diff)
1833 fput_many(state->file, diff);
1834 state->file = NULL;
1835 }
1836}
1837
1838/*
1839 * Get as many references to a file as we have IOs left in this submission,
1840 * assuming most submissions are for one file, or at least that each file
1841 * has more than one submission.
1842 */
1843static struct file *io_file_get(struct io_submit_state *state, int fd)
1844{
1845 if (!state)
1846 return fget(fd);
1847
1848 if (state->file) {
1849 if (state->fd == fd) {
1850 state->used_refs++;
1851 state->ios_left--;
1852 return state->file;
1853 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001854 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001855 }
1856 state->file = fget_many(fd, state->ios_left);
1857 if (!state->file)
1858 return NULL;
1859
1860 state->fd = fd;
1861 state->has_refs = state->ios_left;
1862 state->used_refs = 1;
1863 state->ios_left--;
1864 return state->file;
1865}
1866
Jens Axboe2b188cc2019-01-07 10:46:33 -07001867/*
1868 * If we tracked the file through the SCM inflight mechanism, we could support
1869 * any file. For now, just ensure that anything potentially problematic is done
1870 * inline.
1871 */
1872static bool io_file_supports_async(struct file *file)
1873{
1874 umode_t mode = file_inode(file)->i_mode;
1875
Jens Axboe10d59342019-12-09 20:16:22 -07001876 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001877 return true;
1878 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1879 return true;
1880
1881 return false;
1882}
1883
Jens Axboe3529d8c2019-12-19 18:24:38 -07001884static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1885 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001886{
Jens Axboedef596e2019-01-09 08:59:42 -07001887 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001888 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001889 unsigned ioprio;
1890 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891
Jens Axboe491381ce2019-10-17 09:20:46 -06001892 if (S_ISREG(file_inode(req->file)->i_mode))
1893 req->flags |= REQ_F_ISREG;
1894
Jens Axboe2b188cc2019-01-07 10:46:33 -07001895 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001896 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1897 req->flags |= REQ_F_CUR_POS;
1898 kiocb->ki_pos = req->file->f_pos;
1899 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001900 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001901 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1902 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1903 if (unlikely(ret))
1904 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001905
1906 ioprio = READ_ONCE(sqe->ioprio);
1907 if (ioprio) {
1908 ret = ioprio_check_cap(ioprio);
1909 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001910 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911
1912 kiocb->ki_ioprio = ioprio;
1913 } else
1914 kiocb->ki_ioprio = get_current_ioprio();
1915
Stefan Bühler8449eed2019-04-27 20:34:19 +02001916 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001917 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1918 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001919 req->flags |= REQ_F_NOWAIT;
1920
1921 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001923
Jens Axboedef596e2019-01-09 08:59:42 -07001924 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001925 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1926 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001927 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001928
Jens Axboedef596e2019-01-09 08:59:42 -07001929 kiocb->ki_flags |= IOCB_HIPRI;
1930 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001931 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001932 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001933 if (kiocb->ki_flags & IOCB_HIPRI)
1934 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001935 kiocb->ki_complete = io_complete_rw;
1936 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001937
Jens Axboe3529d8c2019-12-19 18:24:38 -07001938 req->rw.addr = READ_ONCE(sqe->addr);
1939 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001940 /* we own ->private, reuse it for the buffer index */
1941 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001942 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001943 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001944}
1945
1946static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1947{
1948 switch (ret) {
1949 case -EIOCBQUEUED:
1950 break;
1951 case -ERESTARTSYS:
1952 case -ERESTARTNOINTR:
1953 case -ERESTARTNOHAND:
1954 case -ERESTART_RESTARTBLOCK:
1955 /*
1956 * We can't just restart the syscall, since previously
1957 * submitted sqes may already be in progress. Just fail this
1958 * IO with EINTR.
1959 */
1960 ret = -EINTR;
1961 /* fall through */
1962 default:
1963 kiocb->ki_complete(kiocb, ret, 0);
1964 }
1965}
1966
Jens Axboeba816ad2019-09-28 11:36:45 -06001967static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1968 bool in_async)
1969{
Jens Axboeba042912019-12-25 16:33:42 -07001970 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1971
1972 if (req->flags & REQ_F_CUR_POS)
1973 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001974 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001975 *nxt = __io_complete_rw(kiocb, ret);
1976 else
1977 io_rw_done(kiocb, ret);
1978}
1979
Jens Axboe9adbd452019-12-20 08:45:55 -07001980static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001981 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001982{
Jens Axboe9adbd452019-12-20 08:45:55 -07001983 struct io_ring_ctx *ctx = req->ctx;
1984 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001985 struct io_mapped_ubuf *imu;
1986 unsigned index, buf_index;
1987 size_t offset;
1988 u64 buf_addr;
1989
1990 /* attempt to use fixed buffers without having provided iovecs */
1991 if (unlikely(!ctx->user_bufs))
1992 return -EFAULT;
1993
Jens Axboe9adbd452019-12-20 08:45:55 -07001994 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001995 if (unlikely(buf_index >= ctx->nr_user_bufs))
1996 return -EFAULT;
1997
1998 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1999 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002000 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002001
2002 /* overflow */
2003 if (buf_addr + len < buf_addr)
2004 return -EFAULT;
2005 /* not inside the mapped region */
2006 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2007 return -EFAULT;
2008
2009 /*
2010 * May not be a start of buffer, set size appropriately
2011 * and advance us to the beginning.
2012 */
2013 offset = buf_addr - imu->ubuf;
2014 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002015
2016 if (offset) {
2017 /*
2018 * Don't use iov_iter_advance() here, as it's really slow for
2019 * using the latter parts of a big fixed buffer - it iterates
2020 * over each segment manually. We can cheat a bit here, because
2021 * we know that:
2022 *
2023 * 1) it's a BVEC iter, we set it up
2024 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2025 * first and last bvec
2026 *
2027 * So just find our index, and adjust the iterator afterwards.
2028 * If the offset is within the first bvec (or the whole first
2029 * bvec, just use iov_iter_advance(). This makes it easier
2030 * since we can just skip the first segment, which may not
2031 * be PAGE_SIZE aligned.
2032 */
2033 const struct bio_vec *bvec = imu->bvec;
2034
2035 if (offset <= bvec->bv_len) {
2036 iov_iter_advance(iter, offset);
2037 } else {
2038 unsigned long seg_skip;
2039
2040 /* skip first vec */
2041 offset -= bvec->bv_len;
2042 seg_skip = 1 + (offset >> PAGE_SHIFT);
2043
2044 iter->bvec = bvec + seg_skip;
2045 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002046 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002047 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002048 }
2049 }
2050
Jens Axboe5e559562019-11-13 16:12:46 -07002051 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002052}
2053
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002054static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2055 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002056{
Jens Axboe9adbd452019-12-20 08:45:55 -07002057 void __user *buf = u64_to_user_ptr(req->rw.addr);
2058 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002059 u8 opcode;
2060
Jens Axboed625c6e2019-12-17 19:53:05 -07002061 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002062 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002063 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002064 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002065 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002066
Jens Axboe9adbd452019-12-20 08:45:55 -07002067 /* buffer index only valid with fixed read/write */
2068 if (req->rw.kiocb.private)
2069 return -EINVAL;
2070
Jens Axboe3a6820f2019-12-22 15:19:35 -07002071 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2072 ssize_t ret;
2073 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2074 *iovec = NULL;
2075 return ret;
2076 }
2077
Jens Axboef67676d2019-12-02 11:03:47 -07002078 if (req->io) {
2079 struct io_async_rw *iorw = &req->io->rw;
2080
2081 *iovec = iorw->iov;
2082 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2083 if (iorw->iov == iorw->fast_iov)
2084 *iovec = NULL;
2085 return iorw->size;
2086 }
2087
Jens Axboe2b188cc2019-01-07 10:46:33 -07002088#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002089 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002090 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2091 iovec, iter);
2092#endif
2093
2094 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2095}
2096
Jens Axboe32960612019-09-23 11:05:34 -06002097/*
2098 * For files that don't have ->read_iter() and ->write_iter(), handle them
2099 * by looping over ->read() or ->write() manually.
2100 */
2101static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2102 struct iov_iter *iter)
2103{
2104 ssize_t ret = 0;
2105
2106 /*
2107 * Don't support polled IO through this interface, and we can't
2108 * support non-blocking either. For the latter, this just causes
2109 * the kiocb to be handled from an async context.
2110 */
2111 if (kiocb->ki_flags & IOCB_HIPRI)
2112 return -EOPNOTSUPP;
2113 if (kiocb->ki_flags & IOCB_NOWAIT)
2114 return -EAGAIN;
2115
2116 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002117 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002118 ssize_t nr;
2119
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002120 if (!iov_iter_is_bvec(iter)) {
2121 iovec = iov_iter_iovec(iter);
2122 } else {
2123 /* fixed buffers import bvec */
2124 iovec.iov_base = kmap(iter->bvec->bv_page)
2125 + iter->iov_offset;
2126 iovec.iov_len = min(iter->count,
2127 iter->bvec->bv_len - iter->iov_offset);
2128 }
2129
Jens Axboe32960612019-09-23 11:05:34 -06002130 if (rw == READ) {
2131 nr = file->f_op->read(file, iovec.iov_base,
2132 iovec.iov_len, &kiocb->ki_pos);
2133 } else {
2134 nr = file->f_op->write(file, iovec.iov_base,
2135 iovec.iov_len, &kiocb->ki_pos);
2136 }
2137
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002138 if (iov_iter_is_bvec(iter))
2139 kunmap(iter->bvec->bv_page);
2140
Jens Axboe32960612019-09-23 11:05:34 -06002141 if (nr < 0) {
2142 if (!ret)
2143 ret = nr;
2144 break;
2145 }
2146 ret += nr;
2147 if (nr != iovec.iov_len)
2148 break;
2149 iov_iter_advance(iter, nr);
2150 }
2151
2152 return ret;
2153}
2154
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002155static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002156 struct iovec *iovec, struct iovec *fast_iov,
2157 struct iov_iter *iter)
2158{
2159 req->io->rw.nr_segs = iter->nr_segs;
2160 req->io->rw.size = io_size;
2161 req->io->rw.iov = iovec;
2162 if (!req->io->rw.iov) {
2163 req->io->rw.iov = req->io->rw.fast_iov;
2164 memcpy(req->io->rw.iov, fast_iov,
2165 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002166 } else {
2167 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002168 }
2169}
2170
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002171static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002172{
Jens Axboed3656342019-12-18 09:50:26 -07002173 if (!io_op_defs[req->opcode].async_ctx)
2174 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002175 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002176 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002177}
2178
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002179static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2180 struct iovec *iovec, struct iovec *fast_iov,
2181 struct iov_iter *iter)
2182{
Jens Axboe980ad262020-01-24 23:08:54 -07002183 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002184 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002185 if (!req->io) {
2186 if (io_alloc_async_ctx(req))
2187 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002188
Jens Axboe5d204bc2020-01-31 12:06:52 -07002189 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2190 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002191 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002192}
2193
Jens Axboe3529d8c2019-12-19 18:24:38 -07002194static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2195 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002196{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002197 struct io_async_ctx *io;
2198 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002199 ssize_t ret;
2200
Jens Axboe3529d8c2019-12-19 18:24:38 -07002201 ret = io_prep_rw(req, sqe, force_nonblock);
2202 if (ret)
2203 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002204
Jens Axboe3529d8c2019-12-19 18:24:38 -07002205 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2206 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002207
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002208 /* either don't need iovec imported or already have it */
2209 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002210 return 0;
2211
2212 io = req->io;
2213 io->rw.iov = io->rw.fast_iov;
2214 req->io = NULL;
2215 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2216 req->io = io;
2217 if (ret < 0)
2218 return ret;
2219
2220 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2221 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002222}
2223
Pavel Begunkov267bc902019-11-07 01:41:08 +03002224static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002225 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002226{
2227 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002228 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002229 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002230 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002231 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002232
Jens Axboe3529d8c2019-12-19 18:24:38 -07002233 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002234 if (ret < 0)
2235 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002236
Jens Axboefd6c2e42019-12-18 12:19:41 -07002237 /* Ensure we clear previously set non-block flag */
2238 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002239 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002240
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002241 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002242 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002243 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002244 req->result = io_size;
2245
2246 /*
2247 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2248 * we know to async punt it even if it was opened O_NONBLOCK
2249 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002250 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002251 req->flags |= REQ_F_MUST_PUNT;
2252 goto copy_iov;
2253 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002254
Jens Axboe31b51512019-01-18 22:56:34 -07002255 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002256 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002257 if (!ret) {
2258 ssize_t ret2;
2259
Jens Axboe9adbd452019-12-20 08:45:55 -07002260 if (req->file->f_op->read_iter)
2261 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002262 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002263 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002264
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002265 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002266 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002267 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002268 } else {
2269copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002270 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002271 inline_vecs, &iter);
2272 if (ret)
2273 goto out_free;
2274 return -EAGAIN;
2275 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002276 }
Jens Axboef67676d2019-12-02 11:03:47 -07002277out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002278 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002279 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002280 return ret;
2281}
2282
Jens Axboe3529d8c2019-12-19 18:24:38 -07002283static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2284 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002285{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002286 struct io_async_ctx *io;
2287 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002288 ssize_t ret;
2289
Jens Axboe3529d8c2019-12-19 18:24:38 -07002290 ret = io_prep_rw(req, sqe, force_nonblock);
2291 if (ret)
2292 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002293
Jens Axboe3529d8c2019-12-19 18:24:38 -07002294 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2295 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002296
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002297 /* either don't need iovec imported or already have it */
2298 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002299 return 0;
2300
2301 io = req->io;
2302 io->rw.iov = io->rw.fast_iov;
2303 req->io = NULL;
2304 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2305 req->io = io;
2306 if (ret < 0)
2307 return ret;
2308
2309 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2310 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002311}
2312
Pavel Begunkov267bc902019-11-07 01:41:08 +03002313static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002314 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002315{
2316 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002317 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002318 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002319 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002320 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321
Jens Axboe3529d8c2019-12-19 18:24:38 -07002322 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002323 if (ret < 0)
2324 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002325
Jens Axboefd6c2e42019-12-18 12:19:41 -07002326 /* Ensure we clear previously set non-block flag */
2327 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002328 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002329
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002330 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002331 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002332 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002333 req->result = io_size;
2334
2335 /*
2336 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2337 * we know to async punt it even if it was opened O_NONBLOCK
2338 */
2339 if (force_nonblock && !io_file_supports_async(req->file)) {
2340 req->flags |= REQ_F_MUST_PUNT;
2341 goto copy_iov;
2342 }
2343
Jens Axboe10d59342019-12-09 20:16:22 -07002344 /* file path doesn't support NOWAIT for non-direct_IO */
2345 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2346 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002347 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002348
Jens Axboe31b51512019-01-18 22:56:34 -07002349 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002350 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002351 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002352 ssize_t ret2;
2353
Jens Axboe2b188cc2019-01-07 10:46:33 -07002354 /*
2355 * Open-code file_start_write here to grab freeze protection,
2356 * which will be released by another thread in
2357 * io_complete_rw(). Fool lockdep by telling it the lock got
2358 * released so that it doesn't complain about the held lock when
2359 * we return to userspace.
2360 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002361 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002362 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002363 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002364 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002365 SB_FREEZE_WRITE);
2366 }
2367 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002368
Jens Axboe9adbd452019-12-20 08:45:55 -07002369 if (req->file->f_op->write_iter)
2370 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002371 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002372 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002373 /*
2374 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2375 * retry them without IOCB_NOWAIT.
2376 */
2377 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2378 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002379 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002380 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002381 } else {
2382copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002383 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002384 inline_vecs, &iter);
2385 if (ret)
2386 goto out_free;
2387 return -EAGAIN;
2388 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002389 }
Jens Axboe31b51512019-01-18 22:56:34 -07002390out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002391 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002392 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002393 return ret;
2394}
2395
2396/*
2397 * IORING_OP_NOP just posts a completion event, nothing else.
2398 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002399static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400{
2401 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002402
Jens Axboedef596e2019-01-09 08:59:42 -07002403 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2404 return -EINVAL;
2405
Jens Axboe78e19bb2019-11-06 15:21:34 -07002406 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002407 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002408 return 0;
2409}
2410
Jens Axboe3529d8c2019-12-19 18:24:38 -07002411static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002412{
Jens Axboe6b063142019-01-10 22:13:58 -07002413 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002414
Jens Axboe09bb8392019-03-13 12:39:28 -06002415 if (!req->file)
2416 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002417
Jens Axboe6b063142019-01-10 22:13:58 -07002418 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002419 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002420 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002421 return -EINVAL;
2422
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002423 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2424 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2425 return -EINVAL;
2426
2427 req->sync.off = READ_ONCE(sqe->off);
2428 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002429 return 0;
2430}
2431
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002432static bool io_req_cancelled(struct io_kiocb *req)
2433{
2434 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2435 req_set_fail_links(req);
2436 io_cqring_add_event(req, -ECANCELED);
2437 io_put_req(req);
2438 return true;
2439 }
2440
2441 return false;
2442}
2443
Jens Axboe78912932020-01-14 22:09:06 -07002444static void io_link_work_cb(struct io_wq_work **workptr)
2445{
2446 struct io_wq_work *work = *workptr;
2447 struct io_kiocb *link = work->data;
2448
2449 io_queue_linked_timeout(link);
2450 work->func = io_wq_submit_work;
2451}
2452
2453static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2454{
2455 struct io_kiocb *link;
2456
2457 io_prep_async_work(nxt, &link);
2458 *workptr = &nxt->work;
2459 if (link) {
2460 nxt->work.flags |= IO_WQ_WORK_CB;
2461 nxt->work.func = io_link_work_cb;
2462 nxt->work.data = link;
2463 }
2464}
2465
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002466static void io_fsync_finish(struct io_wq_work **workptr)
2467{
2468 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2469 loff_t end = req->sync.off + req->sync.len;
2470 struct io_kiocb *nxt = NULL;
2471 int ret;
2472
2473 if (io_req_cancelled(req))
2474 return;
2475
Jens Axboe9adbd452019-12-20 08:45:55 -07002476 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002477 end > 0 ? end : LLONG_MAX,
2478 req->sync.flags & IORING_FSYNC_DATASYNC);
2479 if (ret < 0)
2480 req_set_fail_links(req);
2481 io_cqring_add_event(req, ret);
2482 io_put_req_find_next(req, &nxt);
2483 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002484 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002485}
2486
Jens Axboefc4df992019-12-10 14:38:45 -07002487static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2488 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002489{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002490 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002491
2492 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002493 if (force_nonblock) {
2494 io_put_req(req);
2495 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002496 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002497 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002498
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002499 work = old_work = &req->work;
2500 io_fsync_finish(&work);
2501 if (work && work != old_work)
2502 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002503 return 0;
2504}
2505
Jens Axboed63d1b52019-12-10 10:38:56 -07002506static void io_fallocate_finish(struct io_wq_work **workptr)
2507{
2508 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2509 struct io_kiocb *nxt = NULL;
2510 int ret;
2511
2512 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2513 req->sync.len);
2514 if (ret < 0)
2515 req_set_fail_links(req);
2516 io_cqring_add_event(req, ret);
2517 io_put_req_find_next(req, &nxt);
2518 if (nxt)
2519 io_wq_assign_next(workptr, nxt);
2520}
2521
2522static int io_fallocate_prep(struct io_kiocb *req,
2523 const struct io_uring_sqe *sqe)
2524{
2525 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2526 return -EINVAL;
2527
2528 req->sync.off = READ_ONCE(sqe->off);
2529 req->sync.len = READ_ONCE(sqe->addr);
2530 req->sync.mode = READ_ONCE(sqe->len);
2531 return 0;
2532}
2533
2534static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2535 bool force_nonblock)
2536{
2537 struct io_wq_work *work, *old_work;
2538
2539 /* fallocate always requiring blocking context */
2540 if (force_nonblock) {
2541 io_put_req(req);
2542 req->work.func = io_fallocate_finish;
2543 return -EAGAIN;
2544 }
2545
2546 work = old_work = &req->work;
2547 io_fallocate_finish(&work);
2548 if (work && work != old_work)
2549 *nxt = container_of(work, struct io_kiocb, work);
2550
2551 return 0;
2552}
2553
Jens Axboe15b71ab2019-12-11 11:20:36 -07002554static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2555{
Jens Axboef8748882020-01-08 17:47:02 -07002556 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002557 int ret;
2558
2559 if (sqe->ioprio || sqe->buf_index)
2560 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002561 if (sqe->flags & IOSQE_FIXED_FILE)
2562 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002563
2564 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002565 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002566 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002567 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002568
Jens Axboef8748882020-01-08 17:47:02 -07002569 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002570 if (IS_ERR(req->open.filename)) {
2571 ret = PTR_ERR(req->open.filename);
2572 req->open.filename = NULL;
2573 return ret;
2574 }
2575
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002576 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002577 return 0;
2578}
2579
Jens Axboecebdb982020-01-08 17:59:24 -07002580static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2581{
2582 struct open_how __user *how;
2583 const char __user *fname;
2584 size_t len;
2585 int ret;
2586
2587 if (sqe->ioprio || sqe->buf_index)
2588 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002589 if (sqe->flags & IOSQE_FIXED_FILE)
2590 return -EBADF;
Jens Axboecebdb982020-01-08 17:59:24 -07002591
2592 req->open.dfd = READ_ONCE(sqe->fd);
2593 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2594 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2595 len = READ_ONCE(sqe->len);
2596
2597 if (len < OPEN_HOW_SIZE_VER0)
2598 return -EINVAL;
2599
2600 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2601 len);
2602 if (ret)
2603 return ret;
2604
2605 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2606 req->open.how.flags |= O_LARGEFILE;
2607
2608 req->open.filename = getname(fname);
2609 if (IS_ERR(req->open.filename)) {
2610 ret = PTR_ERR(req->open.filename);
2611 req->open.filename = NULL;
2612 return ret;
2613 }
2614
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002615 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002616 return 0;
2617}
2618
2619static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2620 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002621{
2622 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002623 struct file *file;
2624 int ret;
2625
Jens Axboef86cd202020-01-29 13:46:44 -07002626 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002627 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002628
Jens Axboecebdb982020-01-08 17:59:24 -07002629 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002630 if (ret)
2631 goto err;
2632
Jens Axboecebdb982020-01-08 17:59:24 -07002633 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002634 if (ret < 0)
2635 goto err;
2636
2637 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2638 if (IS_ERR(file)) {
2639 put_unused_fd(ret);
2640 ret = PTR_ERR(file);
2641 } else {
2642 fsnotify_open(file);
2643 fd_install(ret, file);
2644 }
2645err:
2646 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002647 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002648 if (ret < 0)
2649 req_set_fail_links(req);
2650 io_cqring_add_event(req, ret);
2651 io_put_req_find_next(req, nxt);
2652 return 0;
2653}
2654
Jens Axboecebdb982020-01-08 17:59:24 -07002655static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2656 bool force_nonblock)
2657{
2658 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2659 return io_openat2(req, nxt, force_nonblock);
2660}
2661
Jens Axboe3e4827b2020-01-08 15:18:09 -07002662static int io_epoll_ctl_prep(struct io_kiocb *req,
2663 const struct io_uring_sqe *sqe)
2664{
2665#if defined(CONFIG_EPOLL)
2666 if (sqe->ioprio || sqe->buf_index)
2667 return -EINVAL;
2668
2669 req->epoll.epfd = READ_ONCE(sqe->fd);
2670 req->epoll.op = READ_ONCE(sqe->len);
2671 req->epoll.fd = READ_ONCE(sqe->off);
2672
2673 if (ep_op_has_event(req->epoll.op)) {
2674 struct epoll_event __user *ev;
2675
2676 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2677 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2678 return -EFAULT;
2679 }
2680
2681 return 0;
2682#else
2683 return -EOPNOTSUPP;
2684#endif
2685}
2686
2687static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2688 bool force_nonblock)
2689{
2690#if defined(CONFIG_EPOLL)
2691 struct io_epoll *ie = &req->epoll;
2692 int ret;
2693
2694 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2695 if (force_nonblock && ret == -EAGAIN)
2696 return -EAGAIN;
2697
2698 if (ret < 0)
2699 req_set_fail_links(req);
2700 io_cqring_add_event(req, ret);
2701 io_put_req_find_next(req, nxt);
2702 return 0;
2703#else
2704 return -EOPNOTSUPP;
2705#endif
2706}
2707
Jens Axboec1ca7572019-12-25 22:18:28 -07002708static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2709{
2710#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2711 if (sqe->ioprio || sqe->buf_index || sqe->off)
2712 return -EINVAL;
2713
2714 req->madvise.addr = READ_ONCE(sqe->addr);
2715 req->madvise.len = READ_ONCE(sqe->len);
2716 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2717 return 0;
2718#else
2719 return -EOPNOTSUPP;
2720#endif
2721}
2722
2723static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2724 bool force_nonblock)
2725{
2726#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2727 struct io_madvise *ma = &req->madvise;
2728 int ret;
2729
2730 if (force_nonblock)
2731 return -EAGAIN;
2732
2733 ret = do_madvise(ma->addr, ma->len, ma->advice);
2734 if (ret < 0)
2735 req_set_fail_links(req);
2736 io_cqring_add_event(req, ret);
2737 io_put_req_find_next(req, nxt);
2738 return 0;
2739#else
2740 return -EOPNOTSUPP;
2741#endif
2742}
2743
Jens Axboe4840e412019-12-25 22:03:45 -07002744static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2745{
2746 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2747 return -EINVAL;
2748
2749 req->fadvise.offset = READ_ONCE(sqe->off);
2750 req->fadvise.len = READ_ONCE(sqe->len);
2751 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2752 return 0;
2753}
2754
2755static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2756 bool force_nonblock)
2757{
2758 struct io_fadvise *fa = &req->fadvise;
2759 int ret;
2760
Jens Axboe3e694262020-02-01 09:22:49 -07002761 if (force_nonblock) {
2762 switch (fa->advice) {
2763 case POSIX_FADV_NORMAL:
2764 case POSIX_FADV_RANDOM:
2765 case POSIX_FADV_SEQUENTIAL:
2766 break;
2767 default:
2768 return -EAGAIN;
2769 }
2770 }
Jens Axboe4840e412019-12-25 22:03:45 -07002771
2772 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2773 if (ret < 0)
2774 req_set_fail_links(req);
2775 io_cqring_add_event(req, ret);
2776 io_put_req_find_next(req, nxt);
2777 return 0;
2778}
2779
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002780static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2781{
Jens Axboef8748882020-01-08 17:47:02 -07002782 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002783 unsigned lookup_flags;
2784 int ret;
2785
2786 if (sqe->ioprio || sqe->buf_index)
2787 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002788 if (sqe->flags & IOSQE_FIXED_FILE)
2789 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002790
2791 req->open.dfd = READ_ONCE(sqe->fd);
2792 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002793 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002794 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002795 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002796
Jens Axboec12cedf2020-01-08 17:41:21 -07002797 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002798 return -EINVAL;
2799
Jens Axboef8748882020-01-08 17:47:02 -07002800 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002801 if (IS_ERR(req->open.filename)) {
2802 ret = PTR_ERR(req->open.filename);
2803 req->open.filename = NULL;
2804 return ret;
2805 }
2806
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002807 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002808 return 0;
2809}
2810
2811static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2812 bool force_nonblock)
2813{
2814 struct io_open *ctx = &req->open;
2815 unsigned lookup_flags;
2816 struct path path;
2817 struct kstat stat;
2818 int ret;
2819
2820 if (force_nonblock)
2821 return -EAGAIN;
2822
Jens Axboec12cedf2020-01-08 17:41:21 -07002823 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002824 return -EINVAL;
2825
2826retry:
2827 /* filename_lookup() drops it, keep a reference */
2828 ctx->filename->refcnt++;
2829
2830 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2831 NULL);
2832 if (ret)
2833 goto err;
2834
Jens Axboec12cedf2020-01-08 17:41:21 -07002835 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002836 path_put(&path);
2837 if (retry_estale(ret, lookup_flags)) {
2838 lookup_flags |= LOOKUP_REVAL;
2839 goto retry;
2840 }
2841 if (!ret)
2842 ret = cp_statx(&stat, ctx->buffer);
2843err:
2844 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002845 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002846 if (ret < 0)
2847 req_set_fail_links(req);
2848 io_cqring_add_event(req, ret);
2849 io_put_req_find_next(req, nxt);
2850 return 0;
2851}
2852
Jens Axboeb5dba592019-12-11 14:02:38 -07002853static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2854{
2855 /*
2856 * If we queue this for async, it must not be cancellable. That would
2857 * leave the 'file' in an undeterminate state.
2858 */
2859 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2860
2861 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2862 sqe->rw_flags || sqe->buf_index)
2863 return -EINVAL;
2864 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002865 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002866
2867 req->close.fd = READ_ONCE(sqe->fd);
2868 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002869 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002870 return -EBADF;
2871
2872 return 0;
2873}
2874
Pavel Begunkova93b3332020-02-08 14:04:34 +03002875/* only called when __close_fd_get_file() is done */
2876static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
2877{
2878 int ret;
2879
2880 ret = filp_close(req->close.put_file, req->work.files);
2881 if (ret < 0)
2882 req_set_fail_links(req);
2883 io_cqring_add_event(req, ret);
2884 fput(req->close.put_file);
2885 io_put_req_find_next(req, nxt);
2886}
2887
Jens Axboeb5dba592019-12-11 14:02:38 -07002888static void io_close_finish(struct io_wq_work **workptr)
2889{
2890 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2891 struct io_kiocb *nxt = NULL;
2892
Pavel Begunkova93b3332020-02-08 14:04:34 +03002893 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07002894 if (nxt)
2895 io_wq_assign_next(workptr, nxt);
2896}
2897
2898static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2899 bool force_nonblock)
2900{
2901 int ret;
2902
2903 req->close.put_file = NULL;
2904 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2905 if (ret < 0)
2906 return ret;
2907
2908 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002909 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002910 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002911
2912 /*
2913 * No ->flush(), safely close from here and just punt the
2914 * fput() to async context.
2915 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002916 __io_close_finish(req, nxt);
2917 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002918eagain:
2919 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002920 /*
2921 * Do manual async queue here to avoid grabbing files - we don't
2922 * need the files, and it'll cause io_close_finish() to close
2923 * the file again and cause a double CQE entry for this request
2924 */
2925 io_queue_async_work(req);
2926 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002927}
2928
Jens Axboe3529d8c2019-12-19 18:24:38 -07002929static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002930{
2931 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002932
2933 if (!req->file)
2934 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002935
2936 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2937 return -EINVAL;
2938 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2939 return -EINVAL;
2940
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002941 req->sync.off = READ_ONCE(sqe->off);
2942 req->sync.len = READ_ONCE(sqe->len);
2943 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002944 return 0;
2945}
2946
2947static void io_sync_file_range_finish(struct io_wq_work **workptr)
2948{
2949 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2950 struct io_kiocb *nxt = NULL;
2951 int ret;
2952
2953 if (io_req_cancelled(req))
2954 return;
2955
Jens Axboe9adbd452019-12-20 08:45:55 -07002956 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002957 req->sync.flags);
2958 if (ret < 0)
2959 req_set_fail_links(req);
2960 io_cqring_add_event(req, ret);
2961 io_put_req_find_next(req, &nxt);
2962 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002963 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002964}
2965
Jens Axboefc4df992019-12-10 14:38:45 -07002966static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002967 bool force_nonblock)
2968{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002969 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002970
2971 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002972 if (force_nonblock) {
2973 io_put_req(req);
2974 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002975 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002976 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002977
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002978 work = old_work = &req->work;
2979 io_sync_file_range_finish(&work);
2980 if (work && work != old_work)
2981 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002982 return 0;
2983}
2984
Jens Axboe3529d8c2019-12-19 18:24:38 -07002985static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002986{
Jens Axboe03b12302019-12-02 18:50:25 -07002987#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002988 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002989 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002990 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002991
Jens Axboee47293f2019-12-20 08:58:21 -07002992 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2993 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002994 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002995
Jens Axboefddafac2020-01-04 20:19:44 -07002996 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002997 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002998 /* iovec is already imported */
2999 if (req->flags & REQ_F_NEED_CLEANUP)
3000 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003001
Jens Axboed9688562019-12-09 19:35:20 -07003002 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003003 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003004 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003005 if (!ret)
3006 req->flags |= REQ_F_NEED_CLEANUP;
3007 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003008#else
Jens Axboee47293f2019-12-20 08:58:21 -07003009 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003010#endif
3011}
3012
Jens Axboefc4df992019-12-10 14:38:45 -07003013static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3014 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003015{
3016#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003017 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003018 struct socket *sock;
3019 int ret;
3020
3021 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3022 return -EINVAL;
3023
3024 sock = sock_from_file(req->file, &ret);
3025 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003026 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003027 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07003028 unsigned flags;
3029
Jens Axboe03b12302019-12-02 18:50:25 -07003030 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003031 kmsg = &req->io->msg;
3032 kmsg->msg.msg_name = &addr;
3033 /* if iov is set, it's allocated already */
3034 if (!kmsg->iov)
3035 kmsg->iov = kmsg->fast_iov;
3036 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003037 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003038 struct io_sr_msg *sr = &req->sr_msg;
3039
Jens Axboe0b416c32019-12-15 10:57:46 -07003040 kmsg = &io.msg;
3041 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003042
3043 io.msg.iov = io.msg.fast_iov;
3044 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3045 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003046 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003047 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003048 }
3049
Jens Axboee47293f2019-12-20 08:58:21 -07003050 flags = req->sr_msg.msg_flags;
3051 if (flags & MSG_DONTWAIT)
3052 req->flags |= REQ_F_NOWAIT;
3053 else if (force_nonblock)
3054 flags |= MSG_DONTWAIT;
3055
Jens Axboe0b416c32019-12-15 10:57:46 -07003056 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003057 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003058 if (req->io)
3059 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003060 if (io_alloc_async_ctx(req)) {
3061 if (kmsg && kmsg->iov != kmsg->fast_iov)
3062 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003063 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003064 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003065 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003066 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003067 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003068 }
3069 if (ret == -ERESTARTSYS)
3070 ret = -EINTR;
3071 }
3072
Pavel Begunkov1e950812020-02-06 19:51:16 +03003073 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003074 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003075 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003076 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003077 if (ret < 0)
3078 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003079 io_put_req_find_next(req, nxt);
3080 return 0;
3081#else
3082 return -EOPNOTSUPP;
3083#endif
3084}
3085
Jens Axboefddafac2020-01-04 20:19:44 -07003086static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3087 bool force_nonblock)
3088{
3089#if defined(CONFIG_NET)
3090 struct socket *sock;
3091 int ret;
3092
3093 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3094 return -EINVAL;
3095
3096 sock = sock_from_file(req->file, &ret);
3097 if (sock) {
3098 struct io_sr_msg *sr = &req->sr_msg;
3099 struct msghdr msg;
3100 struct iovec iov;
3101 unsigned flags;
3102
3103 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3104 &msg.msg_iter);
3105 if (ret)
3106 return ret;
3107
3108 msg.msg_name = NULL;
3109 msg.msg_control = NULL;
3110 msg.msg_controllen = 0;
3111 msg.msg_namelen = 0;
3112
3113 flags = req->sr_msg.msg_flags;
3114 if (flags & MSG_DONTWAIT)
3115 req->flags |= REQ_F_NOWAIT;
3116 else if (force_nonblock)
3117 flags |= MSG_DONTWAIT;
3118
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003119 msg.msg_flags = flags;
3120 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003121 if (force_nonblock && ret == -EAGAIN)
3122 return -EAGAIN;
3123 if (ret == -ERESTARTSYS)
3124 ret = -EINTR;
3125 }
3126
3127 io_cqring_add_event(req, ret);
3128 if (ret < 0)
3129 req_set_fail_links(req);
3130 io_put_req_find_next(req, nxt);
3131 return 0;
3132#else
3133 return -EOPNOTSUPP;
3134#endif
3135}
3136
Jens Axboe3529d8c2019-12-19 18:24:38 -07003137static int io_recvmsg_prep(struct io_kiocb *req,
3138 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003139{
3140#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003141 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003142 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003143 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003144
Jens Axboe3529d8c2019-12-19 18:24:38 -07003145 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3146 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003147 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003148
Jens Axboefddafac2020-01-04 20:19:44 -07003149 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003150 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003151 /* iovec is already imported */
3152 if (req->flags & REQ_F_NEED_CLEANUP)
3153 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003154
Jens Axboed9688562019-12-09 19:35:20 -07003155 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003156 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003157 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003158 if (!ret)
3159 req->flags |= REQ_F_NEED_CLEANUP;
3160 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003161#else
Jens Axboee47293f2019-12-20 08:58:21 -07003162 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003163#endif
3164}
3165
Jens Axboefc4df992019-12-10 14:38:45 -07003166static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3167 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003168{
3169#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003170 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003171 struct socket *sock;
3172 int ret;
3173
3174 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3175 return -EINVAL;
3176
3177 sock = sock_from_file(req->file, &ret);
3178 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003179 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003180 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003181 unsigned flags;
3182
Jens Axboe03b12302019-12-02 18:50:25 -07003183 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003184 kmsg = &req->io->msg;
3185 kmsg->msg.msg_name = &addr;
3186 /* if iov is set, it's allocated already */
3187 if (!kmsg->iov)
3188 kmsg->iov = kmsg->fast_iov;
3189 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003190 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003191 struct io_sr_msg *sr = &req->sr_msg;
3192
Jens Axboe0b416c32019-12-15 10:57:46 -07003193 kmsg = &io.msg;
3194 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003195
3196 io.msg.iov = io.msg.fast_iov;
3197 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3198 sr->msg_flags, &io.msg.uaddr,
3199 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003200 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003201 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003202 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003203
Jens Axboee47293f2019-12-20 08:58:21 -07003204 flags = req->sr_msg.msg_flags;
3205 if (flags & MSG_DONTWAIT)
3206 req->flags |= REQ_F_NOWAIT;
3207 else if (force_nonblock)
3208 flags |= MSG_DONTWAIT;
3209
3210 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3211 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003212 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003213 if (req->io)
3214 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003215 if (io_alloc_async_ctx(req)) {
3216 if (kmsg && kmsg->iov != kmsg->fast_iov)
3217 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003218 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003219 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003220 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003221 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003222 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003223 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003224 if (ret == -ERESTARTSYS)
3225 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003226 }
3227
Pavel Begunkov1e950812020-02-06 19:51:16 +03003228 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003229 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003230 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003231 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003232 if (ret < 0)
3233 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003234 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003235 return 0;
3236#else
3237 return -EOPNOTSUPP;
3238#endif
3239}
3240
Jens Axboefddafac2020-01-04 20:19:44 -07003241static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3242 bool force_nonblock)
3243{
3244#if defined(CONFIG_NET)
3245 struct socket *sock;
3246 int ret;
3247
3248 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3249 return -EINVAL;
3250
3251 sock = sock_from_file(req->file, &ret);
3252 if (sock) {
3253 struct io_sr_msg *sr = &req->sr_msg;
3254 struct msghdr msg;
3255 struct iovec iov;
3256 unsigned flags;
3257
3258 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3259 &msg.msg_iter);
3260 if (ret)
3261 return ret;
3262
3263 msg.msg_name = NULL;
3264 msg.msg_control = NULL;
3265 msg.msg_controllen = 0;
3266 msg.msg_namelen = 0;
3267 msg.msg_iocb = NULL;
3268 msg.msg_flags = 0;
3269
3270 flags = req->sr_msg.msg_flags;
3271 if (flags & MSG_DONTWAIT)
3272 req->flags |= REQ_F_NOWAIT;
3273 else if (force_nonblock)
3274 flags |= MSG_DONTWAIT;
3275
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003276 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003277 if (force_nonblock && ret == -EAGAIN)
3278 return -EAGAIN;
3279 if (ret == -ERESTARTSYS)
3280 ret = -EINTR;
3281 }
3282
3283 io_cqring_add_event(req, ret);
3284 if (ret < 0)
3285 req_set_fail_links(req);
3286 io_put_req_find_next(req, nxt);
3287 return 0;
3288#else
3289 return -EOPNOTSUPP;
3290#endif
3291}
3292
3293
Jens Axboe3529d8c2019-12-19 18:24:38 -07003294static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003295{
3296#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003297 struct io_accept *accept = &req->accept;
3298
Jens Axboe17f2fe32019-10-17 14:42:58 -06003299 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3300 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003301 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003302 return -EINVAL;
3303
Jens Axboed55e5f52019-12-11 16:12:15 -07003304 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3305 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003306 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003307 return 0;
3308#else
3309 return -EOPNOTSUPP;
3310#endif
3311}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003312
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003313#if defined(CONFIG_NET)
3314static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3315 bool force_nonblock)
3316{
3317 struct io_accept *accept = &req->accept;
3318 unsigned file_flags;
3319 int ret;
3320
3321 file_flags = force_nonblock ? O_NONBLOCK : 0;
3322 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3323 accept->addr_len, accept->flags);
3324 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003325 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003326 if (ret == -ERESTARTSYS)
3327 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003328 if (ret < 0)
3329 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003330 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003331 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003332 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003333}
3334
3335static void io_accept_finish(struct io_wq_work **workptr)
3336{
3337 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3338 struct io_kiocb *nxt = NULL;
3339
3340 if (io_req_cancelled(req))
3341 return;
3342 __io_accept(req, &nxt, false);
3343 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003344 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003345}
3346#endif
3347
3348static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3349 bool force_nonblock)
3350{
3351#if defined(CONFIG_NET)
3352 int ret;
3353
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003354 ret = __io_accept(req, nxt, force_nonblock);
3355 if (ret == -EAGAIN && force_nonblock) {
3356 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003357 io_put_req(req);
3358 return -EAGAIN;
3359 }
3360 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003361#else
3362 return -EOPNOTSUPP;
3363#endif
3364}
3365
Jens Axboe3529d8c2019-12-19 18:24:38 -07003366static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003367{
3368#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003369 struct io_connect *conn = &req->connect;
3370 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003371
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003372 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3373 return -EINVAL;
3374 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3375 return -EINVAL;
3376
Jens Axboe3529d8c2019-12-19 18:24:38 -07003377 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3378 conn->addr_len = READ_ONCE(sqe->addr2);
3379
3380 if (!io)
3381 return 0;
3382
3383 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003384 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003385#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003386 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003387#endif
3388}
3389
Jens Axboefc4df992019-12-10 14:38:45 -07003390static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3391 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003392{
3393#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003394 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003395 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003396 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003397
Jens Axboef499a022019-12-02 16:28:46 -07003398 if (req->io) {
3399 io = req->io;
3400 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003401 ret = move_addr_to_kernel(req->connect.addr,
3402 req->connect.addr_len,
3403 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003404 if (ret)
3405 goto out;
3406 io = &__io;
3407 }
3408
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003409 file_flags = force_nonblock ? O_NONBLOCK : 0;
3410
3411 ret = __sys_connect_file(req->file, &io->connect.address,
3412 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003413 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003414 if (req->io)
3415 return -EAGAIN;
3416 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003417 ret = -ENOMEM;
3418 goto out;
3419 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003420 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003421 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003422 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003423 if (ret == -ERESTARTSYS)
3424 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003425out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003426 if (ret < 0)
3427 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003428 io_cqring_add_event(req, ret);
3429 io_put_req_find_next(req, nxt);
3430 return 0;
3431#else
3432 return -EOPNOTSUPP;
3433#endif
3434}
3435
Jens Axboe221c5eb2019-01-17 09:41:58 -07003436static void io_poll_remove_one(struct io_kiocb *req)
3437{
3438 struct io_poll_iocb *poll = &req->poll;
3439
3440 spin_lock(&poll->head->lock);
3441 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003442 if (!list_empty(&poll->wait.entry)) {
3443 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003444 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003445 }
3446 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003447 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003448}
3449
3450static void io_poll_remove_all(struct io_ring_ctx *ctx)
3451{
Jens Axboe78076bb2019-12-04 19:56:40 -07003452 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003453 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003454 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003455
3456 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003457 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3458 struct hlist_head *list;
3459
3460 list = &ctx->cancel_hash[i];
3461 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3462 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003463 }
3464 spin_unlock_irq(&ctx->completion_lock);
3465}
3466
Jens Axboe47f46762019-11-09 17:43:02 -07003467static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3468{
Jens Axboe78076bb2019-12-04 19:56:40 -07003469 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003470 struct io_kiocb *req;
3471
Jens Axboe78076bb2019-12-04 19:56:40 -07003472 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3473 hlist_for_each_entry(req, list, hash_node) {
3474 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003475 io_poll_remove_one(req);
3476 return 0;
3477 }
Jens Axboe47f46762019-11-09 17:43:02 -07003478 }
3479
3480 return -ENOENT;
3481}
3482
Jens Axboe3529d8c2019-12-19 18:24:38 -07003483static int io_poll_remove_prep(struct io_kiocb *req,
3484 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003485{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003486 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3487 return -EINVAL;
3488 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3489 sqe->poll_events)
3490 return -EINVAL;
3491
Jens Axboe0969e782019-12-17 18:40:57 -07003492 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003493 return 0;
3494}
3495
3496/*
3497 * Find a running poll command that matches one specified in sqe->addr,
3498 * and remove it if found.
3499 */
3500static int io_poll_remove(struct io_kiocb *req)
3501{
3502 struct io_ring_ctx *ctx = req->ctx;
3503 u64 addr;
3504 int ret;
3505
Jens Axboe0969e782019-12-17 18:40:57 -07003506 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003507 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003508 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003509 spin_unlock_irq(&ctx->completion_lock);
3510
Jens Axboe78e19bb2019-11-06 15:21:34 -07003511 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003512 if (ret < 0)
3513 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003514 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003515 return 0;
3516}
3517
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003518static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003519{
Jackie Liua197f662019-11-08 08:09:12 -07003520 struct io_ring_ctx *ctx = req->ctx;
3521
Jens Axboe8c838782019-03-12 15:48:16 -06003522 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003523 if (error)
3524 io_cqring_fill_event(req, error);
3525 else
3526 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003527 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003528}
3529
Jens Axboe561fb042019-10-24 07:25:42 -06003530static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003531{
Jens Axboe561fb042019-10-24 07:25:42 -06003532 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003533 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3534 struct io_poll_iocb *poll = &req->poll;
3535 struct poll_table_struct pt = { ._key = poll->events };
3536 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003537 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003538 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003539 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003540
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003541 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003542 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003543 ret = -ECANCELED;
3544 } else if (READ_ONCE(poll->canceled)) {
3545 ret = -ECANCELED;
3546 }
Jens Axboe561fb042019-10-24 07:25:42 -06003547
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003548 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003549 mask = vfs_poll(poll->file, &pt) & poll->events;
3550
3551 /*
3552 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3553 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3554 * synchronize with them. In the cancellation case the list_del_init
3555 * itself is not actually needed, but harmless so we keep it in to
3556 * avoid further branches in the fast path.
3557 */
3558 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003559 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003560 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003561 spin_unlock_irq(&ctx->completion_lock);
3562 return;
3563 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003564 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003565 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003566 spin_unlock_irq(&ctx->completion_lock);
3567
Jens Axboe8c838782019-03-12 15:48:16 -06003568 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003569
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003570 if (ret < 0)
3571 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003572 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003573 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003574 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003575}
3576
Jens Axboee94f1412019-12-19 12:06:02 -07003577static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3578{
Jens Axboee94f1412019-12-19 12:06:02 -07003579 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003580 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003581
Jens Axboec6ca97b302019-12-28 12:11:08 -07003582 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003583 spin_lock_irq(&ctx->completion_lock);
3584 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3585 hash_del(&req->hash_node);
3586 io_poll_complete(req, req->result, 0);
3587
Jens Axboe8237e042019-12-28 10:48:22 -07003588 if (refcount_dec_and_test(&req->refs) &&
3589 !io_req_multi_free(&rb, req)) {
3590 req->flags |= REQ_F_COMP_LOCKED;
3591 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003592 }
3593 }
3594 spin_unlock_irq(&ctx->completion_lock);
3595
3596 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003597 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003598}
3599
3600static void io_poll_flush(struct io_wq_work **workptr)
3601{
3602 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3603 struct llist_node *nodes;
3604
3605 nodes = llist_del_all(&req->ctx->poll_llist);
3606 if (nodes)
3607 __io_poll_flush(req->ctx, nodes);
3608}
3609
Jens Axboef0b493e2020-02-01 21:30:11 -07003610static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3611{
3612 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3613
3614 eventfd_signal(req->ctx->cq_ev_fd, 1);
3615 io_put_req(req);
3616}
3617
Jens Axboe221c5eb2019-01-17 09:41:58 -07003618static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3619 void *key)
3620{
Jens Axboee9444752019-11-26 15:02:04 -07003621 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003622 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3623 struct io_ring_ctx *ctx = req->ctx;
3624 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003625
3626 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003627 if (mask && !(mask & poll->events))
3628 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003629
Jens Axboe392edb42019-12-09 17:52:20 -07003630 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003631
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003632 /*
3633 * Run completion inline if we can. We're using trylock here because
3634 * we are violating the completion_lock -> poll wq lock ordering.
3635 * If we have a link timeout we're going to need the completion_lock
3636 * for finalizing the request, mark us as having grabbed that already.
3637 */
Jens Axboee94f1412019-12-19 12:06:02 -07003638 if (mask) {
3639 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003640
Jens Axboee94f1412019-12-19 12:06:02 -07003641 if (llist_empty(&ctx->poll_llist) &&
3642 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003643 bool trigger_ev;
3644
Jens Axboee94f1412019-12-19 12:06:02 -07003645 hash_del(&req->hash_node);
3646 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003647
Jens Axboef0b493e2020-02-01 21:30:11 -07003648 trigger_ev = io_should_trigger_evfd(ctx);
3649 if (trigger_ev && eventfd_signal_count()) {
3650 trigger_ev = false;
3651 req->work.func = io_poll_trigger_evfd;
3652 } else {
3653 req->flags |= REQ_F_COMP_LOCKED;
3654 io_put_req(req);
3655 req = NULL;
3656 }
3657 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3658 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003659 } else {
3660 req->result = mask;
3661 req->llist_node.next = NULL;
3662 /* if the list wasn't empty, we're done */
3663 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3664 req = NULL;
3665 else
3666 req->work.func = io_poll_flush;
3667 }
Jens Axboe8c838782019-03-12 15:48:16 -06003668 }
Jens Axboee94f1412019-12-19 12:06:02 -07003669 if (req)
3670 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003671
Jens Axboe221c5eb2019-01-17 09:41:58 -07003672 return 1;
3673}
3674
3675struct io_poll_table {
3676 struct poll_table_struct pt;
3677 struct io_kiocb *req;
3678 int error;
3679};
3680
3681static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3682 struct poll_table_struct *p)
3683{
3684 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3685
3686 if (unlikely(pt->req->poll.head)) {
3687 pt->error = -EINVAL;
3688 return;
3689 }
3690
3691 pt->error = 0;
3692 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003693 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003694}
3695
Jens Axboeeac406c2019-11-14 12:09:58 -07003696static void io_poll_req_insert(struct io_kiocb *req)
3697{
3698 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003699 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003700
Jens Axboe78076bb2019-12-04 19:56:40 -07003701 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3702 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003703}
3704
Jens Axboe3529d8c2019-12-19 18:24:38 -07003705static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003706{
3707 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003708 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003709
3710 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3711 return -EINVAL;
3712 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3713 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003714 if (!poll->file)
3715 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003716
Jens Axboe221c5eb2019-01-17 09:41:58 -07003717 events = READ_ONCE(sqe->poll_events);
3718 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003719 return 0;
3720}
3721
3722static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3723{
3724 struct io_poll_iocb *poll = &req->poll;
3725 struct io_ring_ctx *ctx = req->ctx;
3726 struct io_poll_table ipt;
3727 bool cancel = false;
3728 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003729
3730 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003731 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003732
Jens Axboe221c5eb2019-01-17 09:41:58 -07003733 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003734 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003735 poll->canceled = false;
3736
3737 ipt.pt._qproc = io_poll_queue_proc;
3738 ipt.pt._key = poll->events;
3739 ipt.req = req;
3740 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3741
3742 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003743 INIT_LIST_HEAD(&poll->wait.entry);
3744 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3745 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003746
Jens Axboe36703242019-07-25 10:20:18 -06003747 INIT_LIST_HEAD(&req->list);
3748
Jens Axboe221c5eb2019-01-17 09:41:58 -07003749 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003750
3751 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003752 if (likely(poll->head)) {
3753 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003754 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003755 if (ipt.error)
3756 cancel = true;
3757 ipt.error = 0;
3758 mask = 0;
3759 }
3760 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003761 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003762 else if (cancel)
3763 WRITE_ONCE(poll->canceled, true);
3764 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003765 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003766 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003767 }
Jens Axboe8c838782019-03-12 15:48:16 -06003768 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003769 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003770 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003771 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003772 spin_unlock_irq(&ctx->completion_lock);
3773
Jens Axboe8c838782019-03-12 15:48:16 -06003774 if (mask) {
3775 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003776 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003777 }
Jens Axboe8c838782019-03-12 15:48:16 -06003778 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003779}
3780
Jens Axboe5262f562019-09-17 12:26:57 -06003781static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3782{
Jens Axboead8a48a2019-11-15 08:49:11 -07003783 struct io_timeout_data *data = container_of(timer,
3784 struct io_timeout_data, timer);
3785 struct io_kiocb *req = data->req;
3786 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003787 unsigned long flags;
3788
Jens Axboe5262f562019-09-17 12:26:57 -06003789 atomic_inc(&ctx->cq_timeouts);
3790
3791 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003792 /*
Jens Axboe11365042019-10-16 09:08:32 -06003793 * We could be racing with timeout deletion. If the list is empty,
3794 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003795 */
Jens Axboe842f9612019-10-29 12:34:10 -06003796 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003797 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003798
Jens Axboe11365042019-10-16 09:08:32 -06003799 /*
3800 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003801 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003802 * pointer will be increased, otherwise other timeout reqs may
3803 * return in advance without waiting for enough wait_nr.
3804 */
3805 prev = req;
3806 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3807 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003808 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003809 }
Jens Axboe842f9612019-10-29 12:34:10 -06003810
Jens Axboe78e19bb2019-11-06 15:21:34 -07003811 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003812 io_commit_cqring(ctx);
3813 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3814
3815 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003816 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003817 io_put_req(req);
3818 return HRTIMER_NORESTART;
3819}
3820
Jens Axboe47f46762019-11-09 17:43:02 -07003821static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3822{
3823 struct io_kiocb *req;
3824 int ret = -ENOENT;
3825
3826 list_for_each_entry(req, &ctx->timeout_list, list) {
3827 if (user_data == req->user_data) {
3828 list_del_init(&req->list);
3829 ret = 0;
3830 break;
3831 }
3832 }
3833
3834 if (ret == -ENOENT)
3835 return ret;
3836
Jens Axboe2d283902019-12-04 11:08:05 -07003837 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003838 if (ret == -1)
3839 return -EALREADY;
3840
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003841 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003842 io_cqring_fill_event(req, -ECANCELED);
3843 io_put_req(req);
3844 return 0;
3845}
3846
Jens Axboe3529d8c2019-12-19 18:24:38 -07003847static int io_timeout_remove_prep(struct io_kiocb *req,
3848 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003849{
Jens Axboeb29472e2019-12-17 18:50:29 -07003850 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3851 return -EINVAL;
3852 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3853 return -EINVAL;
3854
3855 req->timeout.addr = READ_ONCE(sqe->addr);
3856 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3857 if (req->timeout.flags)
3858 return -EINVAL;
3859
Jens Axboeb29472e2019-12-17 18:50:29 -07003860 return 0;
3861}
3862
Jens Axboe11365042019-10-16 09:08:32 -06003863/*
3864 * Remove or update an existing timeout command
3865 */
Jens Axboefc4df992019-12-10 14:38:45 -07003866static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003867{
3868 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003869 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003870
Jens Axboe11365042019-10-16 09:08:32 -06003871 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003872 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003873
Jens Axboe47f46762019-11-09 17:43:02 -07003874 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003875 io_commit_cqring(ctx);
3876 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003877 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003878 if (ret < 0)
3879 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003880 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003881 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003882}
3883
Jens Axboe3529d8c2019-12-19 18:24:38 -07003884static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003885 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003886{
Jens Axboead8a48a2019-11-15 08:49:11 -07003887 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003888 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003889
Jens Axboead8a48a2019-11-15 08:49:11 -07003890 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003891 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003892 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003893 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003894 if (sqe->off && is_timeout_link)
3895 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003896 flags = READ_ONCE(sqe->timeout_flags);
3897 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003898 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003899
Jens Axboe26a61672019-12-20 09:02:01 -07003900 req->timeout.count = READ_ONCE(sqe->off);
3901
Jens Axboe3529d8c2019-12-19 18:24:38 -07003902 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003903 return -ENOMEM;
3904
3905 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003906 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003907 req->flags |= REQ_F_TIMEOUT;
3908
3909 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003910 return -EFAULT;
3911
Jens Axboe11365042019-10-16 09:08:32 -06003912 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003913 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003914 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003915 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003916
Jens Axboead8a48a2019-11-15 08:49:11 -07003917 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3918 return 0;
3919}
3920
Jens Axboefc4df992019-12-10 14:38:45 -07003921static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003922{
3923 unsigned count;
3924 struct io_ring_ctx *ctx = req->ctx;
3925 struct io_timeout_data *data;
3926 struct list_head *entry;
3927 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003928
Jens Axboe2d283902019-12-04 11:08:05 -07003929 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003930
Jens Axboe5262f562019-09-17 12:26:57 -06003931 /*
3932 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003933 * timeout event to be satisfied. If it isn't set, then this is
3934 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003935 */
Jens Axboe26a61672019-12-20 09:02:01 -07003936 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003937 if (!count) {
3938 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3939 spin_lock_irq(&ctx->completion_lock);
3940 entry = ctx->timeout_list.prev;
3941 goto add;
3942 }
Jens Axboe5262f562019-09-17 12:26:57 -06003943
3944 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003945 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003946
3947 /*
3948 * Insertion sort, ensuring the first entry in the list is always
3949 * the one we need first.
3950 */
Jens Axboe5262f562019-09-17 12:26:57 -06003951 spin_lock_irq(&ctx->completion_lock);
3952 list_for_each_prev(entry, &ctx->timeout_list) {
3953 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003954 unsigned nxt_sq_head;
3955 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003956 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003957
Jens Axboe93bd25b2019-11-11 23:34:31 -07003958 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3959 continue;
3960
yangerkun5da0fb12019-10-15 21:59:29 +08003961 /*
3962 * Since cached_sq_head + count - 1 can overflow, use type long
3963 * long to store it.
3964 */
3965 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003966 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3967 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003968
3969 /*
3970 * cached_sq_head may overflow, and it will never overflow twice
3971 * once there is some timeout req still be valid.
3972 */
3973 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003974 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003975
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003976 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003977 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003978
3979 /*
3980 * Sequence of reqs after the insert one and itself should
3981 * be adjusted because each timeout req consumes a slot.
3982 */
3983 span++;
3984 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003985 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003986 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003987add:
Jens Axboe5262f562019-09-17 12:26:57 -06003988 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003989 data->timer.function = io_timeout_fn;
3990 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003991 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003992 return 0;
3993}
3994
Jens Axboe62755e32019-10-28 21:49:21 -06003995static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003996{
Jens Axboe62755e32019-10-28 21:49:21 -06003997 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003998
Jens Axboe62755e32019-10-28 21:49:21 -06003999 return req->user_data == (unsigned long) data;
4000}
4001
Jens Axboee977d6d2019-11-05 12:39:45 -07004002static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004003{
Jens Axboe62755e32019-10-28 21:49:21 -06004004 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004005 int ret = 0;
4006
Jens Axboe62755e32019-10-28 21:49:21 -06004007 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4008 switch (cancel_ret) {
4009 case IO_WQ_CANCEL_OK:
4010 ret = 0;
4011 break;
4012 case IO_WQ_CANCEL_RUNNING:
4013 ret = -EALREADY;
4014 break;
4015 case IO_WQ_CANCEL_NOTFOUND:
4016 ret = -ENOENT;
4017 break;
4018 }
4019
Jens Axboee977d6d2019-11-05 12:39:45 -07004020 return ret;
4021}
4022
Jens Axboe47f46762019-11-09 17:43:02 -07004023static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4024 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004025 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004026{
4027 unsigned long flags;
4028 int ret;
4029
4030 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4031 if (ret != -ENOENT) {
4032 spin_lock_irqsave(&ctx->completion_lock, flags);
4033 goto done;
4034 }
4035
4036 spin_lock_irqsave(&ctx->completion_lock, flags);
4037 ret = io_timeout_cancel(ctx, sqe_addr);
4038 if (ret != -ENOENT)
4039 goto done;
4040 ret = io_poll_cancel(ctx, sqe_addr);
4041done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004042 if (!ret)
4043 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004044 io_cqring_fill_event(req, ret);
4045 io_commit_cqring(ctx);
4046 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4047 io_cqring_ev_posted(ctx);
4048
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004049 if (ret < 0)
4050 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004051 io_put_req_find_next(req, nxt);
4052}
4053
Jens Axboe3529d8c2019-12-19 18:24:38 -07004054static int io_async_cancel_prep(struct io_kiocb *req,
4055 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004056{
Jens Axboefbf23842019-12-17 18:45:56 -07004057 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004058 return -EINVAL;
4059 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4060 sqe->cancel_flags)
4061 return -EINVAL;
4062
Jens Axboefbf23842019-12-17 18:45:56 -07004063 req->cancel.addr = READ_ONCE(sqe->addr);
4064 return 0;
4065}
4066
4067static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4068{
4069 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004070
4071 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004072 return 0;
4073}
4074
Jens Axboe05f3fb32019-12-09 11:22:50 -07004075static int io_files_update_prep(struct io_kiocb *req,
4076 const struct io_uring_sqe *sqe)
4077{
4078 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4079 return -EINVAL;
4080
4081 req->files_update.offset = READ_ONCE(sqe->off);
4082 req->files_update.nr_args = READ_ONCE(sqe->len);
4083 if (!req->files_update.nr_args)
4084 return -EINVAL;
4085 req->files_update.arg = READ_ONCE(sqe->addr);
4086 return 0;
4087}
4088
4089static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4090{
4091 struct io_ring_ctx *ctx = req->ctx;
4092 struct io_uring_files_update up;
4093 int ret;
4094
Jens Axboef86cd202020-01-29 13:46:44 -07004095 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004096 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004097
4098 up.offset = req->files_update.offset;
4099 up.fds = req->files_update.arg;
4100
4101 mutex_lock(&ctx->uring_lock);
4102 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4103 mutex_unlock(&ctx->uring_lock);
4104
4105 if (ret < 0)
4106 req_set_fail_links(req);
4107 io_cqring_add_event(req, ret);
4108 io_put_req(req);
4109 return 0;
4110}
4111
Jens Axboe3529d8c2019-12-19 18:24:38 -07004112static int io_req_defer_prep(struct io_kiocb *req,
4113 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004114{
Jens Axboee7815732019-12-17 19:45:06 -07004115 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004116
Jens Axboef86cd202020-01-29 13:46:44 -07004117 if (io_op_defs[req->opcode].file_table) {
4118 ret = io_grab_files(req);
4119 if (unlikely(ret))
4120 return ret;
4121 }
4122
Jens Axboecccf0ee2020-01-27 16:34:48 -07004123 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4124
Jens Axboed625c6e2019-12-17 19:53:05 -07004125 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004126 case IORING_OP_NOP:
4127 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004128 case IORING_OP_READV:
4129 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004130 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004131 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004132 break;
4133 case IORING_OP_WRITEV:
4134 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004135 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004136 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004137 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004138 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004139 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004140 break;
4141 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004142 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004143 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004144 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004145 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004146 break;
4147 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004148 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004149 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004150 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004151 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004152 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004153 break;
4154 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004155 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004156 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004157 break;
Jens Axboef499a022019-12-02 16:28:46 -07004158 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004159 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004160 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004161 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004162 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004163 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004164 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004165 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004166 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004167 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004168 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004169 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004170 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004171 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004172 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004173 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004174 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004175 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004176 case IORING_OP_FALLOCATE:
4177 ret = io_fallocate_prep(req, sqe);
4178 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004179 case IORING_OP_OPENAT:
4180 ret = io_openat_prep(req, sqe);
4181 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004182 case IORING_OP_CLOSE:
4183 ret = io_close_prep(req, sqe);
4184 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004185 case IORING_OP_FILES_UPDATE:
4186 ret = io_files_update_prep(req, sqe);
4187 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004188 case IORING_OP_STATX:
4189 ret = io_statx_prep(req, sqe);
4190 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004191 case IORING_OP_FADVISE:
4192 ret = io_fadvise_prep(req, sqe);
4193 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004194 case IORING_OP_MADVISE:
4195 ret = io_madvise_prep(req, sqe);
4196 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004197 case IORING_OP_OPENAT2:
4198 ret = io_openat2_prep(req, sqe);
4199 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004200 case IORING_OP_EPOLL_CTL:
4201 ret = io_epoll_ctl_prep(req, sqe);
4202 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004203 default:
Jens Axboee7815732019-12-17 19:45:06 -07004204 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4205 req->opcode);
4206 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004207 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004208 }
4209
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004210 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004211}
4212
Jens Axboe3529d8c2019-12-19 18:24:38 -07004213static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004214{
Jackie Liua197f662019-11-08 08:09:12 -07004215 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004216 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004217
Bob Liu9d858b22019-11-13 18:06:25 +08004218 /* Still need defer if there is pending req in defer list. */
4219 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004220 return 0;
4221
Jens Axboe3529d8c2019-12-19 18:24:38 -07004222 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004223 return -EAGAIN;
4224
Jens Axboe3529d8c2019-12-19 18:24:38 -07004225 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004226 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004227 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004228
Jens Axboede0617e2019-04-06 21:51:27 -06004229 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004230 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004231 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004232 return 0;
4233 }
4234
Jens Axboe915967f2019-11-21 09:01:20 -07004235 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004236 list_add_tail(&req->list, &ctx->defer_list);
4237 spin_unlock_irq(&ctx->completion_lock);
4238 return -EIOCBQUEUED;
4239}
4240
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004241static void io_cleanup_req(struct io_kiocb *req)
4242{
4243 struct io_async_ctx *io = req->io;
4244
4245 switch (req->opcode) {
4246 case IORING_OP_READV:
4247 case IORING_OP_READ_FIXED:
4248 case IORING_OP_READ:
4249 case IORING_OP_WRITEV:
4250 case IORING_OP_WRITE_FIXED:
4251 case IORING_OP_WRITE:
4252 if (io->rw.iov != io->rw.fast_iov)
4253 kfree(io->rw.iov);
4254 break;
4255 case IORING_OP_SENDMSG:
4256 case IORING_OP_RECVMSG:
4257 if (io->msg.iov != io->msg.fast_iov)
4258 kfree(io->msg.iov);
4259 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004260 case IORING_OP_OPENAT:
4261 case IORING_OP_OPENAT2:
4262 case IORING_OP_STATX:
4263 putname(req->open.filename);
4264 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004265 }
4266
4267 req->flags &= ~REQ_F_NEED_CLEANUP;
4268}
4269
Jens Axboe3529d8c2019-12-19 18:24:38 -07004270static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4271 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004272{
Jackie Liua197f662019-11-08 08:09:12 -07004273 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004274 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004275
Jens Axboed625c6e2019-12-17 19:53:05 -07004276 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004277 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004278 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004279 break;
4280 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004281 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004282 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004283 if (sqe) {
4284 ret = io_read_prep(req, sqe, force_nonblock);
4285 if (ret < 0)
4286 break;
4287 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004288 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004289 break;
4290 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004291 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004292 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004293 if (sqe) {
4294 ret = io_write_prep(req, sqe, force_nonblock);
4295 if (ret < 0)
4296 break;
4297 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004298 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004299 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004300 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004301 if (sqe) {
4302 ret = io_prep_fsync(req, sqe);
4303 if (ret < 0)
4304 break;
4305 }
Jens Axboefc4df992019-12-10 14:38:45 -07004306 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004307 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004308 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004309 if (sqe) {
4310 ret = io_poll_add_prep(req, sqe);
4311 if (ret)
4312 break;
4313 }
Jens Axboefc4df992019-12-10 14:38:45 -07004314 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004315 break;
4316 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004317 if (sqe) {
4318 ret = io_poll_remove_prep(req, sqe);
4319 if (ret < 0)
4320 break;
4321 }
Jens Axboefc4df992019-12-10 14:38:45 -07004322 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004323 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004324 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004325 if (sqe) {
4326 ret = io_prep_sfr(req, sqe);
4327 if (ret < 0)
4328 break;
4329 }
Jens Axboefc4df992019-12-10 14:38:45 -07004330 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004331 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004332 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004333 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004334 if (sqe) {
4335 ret = io_sendmsg_prep(req, sqe);
4336 if (ret < 0)
4337 break;
4338 }
Jens Axboefddafac2020-01-04 20:19:44 -07004339 if (req->opcode == IORING_OP_SENDMSG)
4340 ret = io_sendmsg(req, nxt, force_nonblock);
4341 else
4342 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004343 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004344 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004345 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004346 if (sqe) {
4347 ret = io_recvmsg_prep(req, sqe);
4348 if (ret)
4349 break;
4350 }
Jens Axboefddafac2020-01-04 20:19:44 -07004351 if (req->opcode == IORING_OP_RECVMSG)
4352 ret = io_recvmsg(req, nxt, force_nonblock);
4353 else
4354 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004355 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004356 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004357 if (sqe) {
4358 ret = io_timeout_prep(req, sqe, false);
4359 if (ret)
4360 break;
4361 }
Jens Axboefc4df992019-12-10 14:38:45 -07004362 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004363 break;
Jens Axboe11365042019-10-16 09:08:32 -06004364 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004365 if (sqe) {
4366 ret = io_timeout_remove_prep(req, sqe);
4367 if (ret)
4368 break;
4369 }
Jens Axboefc4df992019-12-10 14:38:45 -07004370 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004371 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004372 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004373 if (sqe) {
4374 ret = io_accept_prep(req, sqe);
4375 if (ret)
4376 break;
4377 }
Jens Axboefc4df992019-12-10 14:38:45 -07004378 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004379 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004380 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004381 if (sqe) {
4382 ret = io_connect_prep(req, sqe);
4383 if (ret)
4384 break;
4385 }
Jens Axboefc4df992019-12-10 14:38:45 -07004386 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004387 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004388 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004389 if (sqe) {
4390 ret = io_async_cancel_prep(req, sqe);
4391 if (ret)
4392 break;
4393 }
Jens Axboefc4df992019-12-10 14:38:45 -07004394 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004395 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004396 case IORING_OP_FALLOCATE:
4397 if (sqe) {
4398 ret = io_fallocate_prep(req, sqe);
4399 if (ret)
4400 break;
4401 }
4402 ret = io_fallocate(req, nxt, force_nonblock);
4403 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004404 case IORING_OP_OPENAT:
4405 if (sqe) {
4406 ret = io_openat_prep(req, sqe);
4407 if (ret)
4408 break;
4409 }
4410 ret = io_openat(req, nxt, force_nonblock);
4411 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004412 case IORING_OP_CLOSE:
4413 if (sqe) {
4414 ret = io_close_prep(req, sqe);
4415 if (ret)
4416 break;
4417 }
4418 ret = io_close(req, nxt, force_nonblock);
4419 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004420 case IORING_OP_FILES_UPDATE:
4421 if (sqe) {
4422 ret = io_files_update_prep(req, sqe);
4423 if (ret)
4424 break;
4425 }
4426 ret = io_files_update(req, force_nonblock);
4427 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004428 case IORING_OP_STATX:
4429 if (sqe) {
4430 ret = io_statx_prep(req, sqe);
4431 if (ret)
4432 break;
4433 }
4434 ret = io_statx(req, nxt, force_nonblock);
4435 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004436 case IORING_OP_FADVISE:
4437 if (sqe) {
4438 ret = io_fadvise_prep(req, sqe);
4439 if (ret)
4440 break;
4441 }
4442 ret = io_fadvise(req, nxt, force_nonblock);
4443 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004444 case IORING_OP_MADVISE:
4445 if (sqe) {
4446 ret = io_madvise_prep(req, sqe);
4447 if (ret)
4448 break;
4449 }
4450 ret = io_madvise(req, nxt, force_nonblock);
4451 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004452 case IORING_OP_OPENAT2:
4453 if (sqe) {
4454 ret = io_openat2_prep(req, sqe);
4455 if (ret)
4456 break;
4457 }
4458 ret = io_openat2(req, nxt, force_nonblock);
4459 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004460 case IORING_OP_EPOLL_CTL:
4461 if (sqe) {
4462 ret = io_epoll_ctl_prep(req, sqe);
4463 if (ret)
4464 break;
4465 }
4466 ret = io_epoll_ctl(req, nxt, force_nonblock);
4467 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004468 default:
4469 ret = -EINVAL;
4470 break;
4471 }
4472
Jens Axboedef596e2019-01-09 08:59:42 -07004473 if (ret)
4474 return ret;
4475
4476 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004477 const bool in_async = io_wq_current_is_worker();
4478
Jens Axboe9e645e112019-05-10 16:07:28 -06004479 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004480 return -EAGAIN;
4481
Jens Axboe11ba8202020-01-15 21:51:17 -07004482 /* workqueue context doesn't hold uring_lock, grab it now */
4483 if (in_async)
4484 mutex_lock(&ctx->uring_lock);
4485
Jens Axboedef596e2019-01-09 08:59:42 -07004486 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004487
4488 if (in_async)
4489 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004490 }
4491
4492 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004493}
4494
Jens Axboe561fb042019-10-24 07:25:42 -06004495static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004496{
Jens Axboe561fb042019-10-24 07:25:42 -06004497 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004498 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004499 struct io_kiocb *nxt = NULL;
4500 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004501
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004502 /* if NO_CANCEL is set, we must still run the work */
4503 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4504 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004505 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004506 }
Jens Axboe31b51512019-01-18 22:56:34 -07004507
Jens Axboe561fb042019-10-24 07:25:42 -06004508 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004509 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004510 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004511 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004512 /*
4513 * We can get EAGAIN for polled IO even though we're
4514 * forcing a sync submission from here, since we can't
4515 * wait for request slots on the block side.
4516 */
4517 if (ret != -EAGAIN)
4518 break;
4519 cond_resched();
4520 } while (1);
4521 }
Jens Axboe31b51512019-01-18 22:56:34 -07004522
Jens Axboe561fb042019-10-24 07:25:42 -06004523 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004524 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004525
Jens Axboe561fb042019-10-24 07:25:42 -06004526 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004527 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004528 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004529 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004530 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004531
Jens Axboe561fb042019-10-24 07:25:42 -06004532 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004533 if (!ret && nxt)
4534 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004535}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004536
Jens Axboe15b71ab2019-12-11 11:20:36 -07004537static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004538{
Jens Axboed3656342019-12-18 09:50:26 -07004539 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004540 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004541 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004542 return 0;
4543 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004544}
4545
Jens Axboe65e19f52019-10-26 07:20:21 -06004546static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4547 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004548{
Jens Axboe65e19f52019-10-26 07:20:21 -06004549 struct fixed_file_table *table;
4550
Jens Axboe05f3fb32019-12-09 11:22:50 -07004551 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4552 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004553}
4554
Jens Axboe3529d8c2019-12-19 18:24:38 -07004555static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4556 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004557{
Jackie Liua197f662019-11-08 08:09:12 -07004558 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004559 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004560 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004561
Jens Axboe3529d8c2019-12-19 18:24:38 -07004562 flags = READ_ONCE(sqe->flags);
4563 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004564
Jens Axboed3656342019-12-18 09:50:26 -07004565 if (!io_req_needs_file(req, fd))
4566 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004567
4568 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004569 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004570 (unsigned) fd >= ctx->nr_user_files))
4571 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004572 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004573 req->file = io_file_from_index(ctx, fd);
4574 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004575 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004576 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004577 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004578 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004579 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004580 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004581 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004582 req->file = io_file_get(state, fd);
4583 if (unlikely(!req->file))
4584 return -EBADF;
4585 }
4586
4587 return 0;
4588}
4589
Jackie Liua197f662019-11-08 08:09:12 -07004590static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004591{
Jens Axboefcb323c2019-10-24 12:39:47 -06004592 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004593 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004594
Jens Axboef86cd202020-01-29 13:46:44 -07004595 if (req->work.files)
4596 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004597 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004598 return -EBADF;
4599
Jens Axboefcb323c2019-10-24 12:39:47 -06004600 rcu_read_lock();
4601 spin_lock_irq(&ctx->inflight_lock);
4602 /*
4603 * We use the f_ops->flush() handler to ensure that we can flush
4604 * out work accessing these files if the fd is closed. Check if
4605 * the fd has changed since we started down this path, and disallow
4606 * this operation if it has.
4607 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004608 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004609 list_add(&req->inflight_entry, &ctx->inflight_list);
4610 req->flags |= REQ_F_INFLIGHT;
4611 req->work.files = current->files;
4612 ret = 0;
4613 }
4614 spin_unlock_irq(&ctx->inflight_lock);
4615 rcu_read_unlock();
4616
4617 return ret;
4618}
4619
Jens Axboe2665abf2019-11-05 12:40:47 -07004620static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4621{
Jens Axboead8a48a2019-11-15 08:49:11 -07004622 struct io_timeout_data *data = container_of(timer,
4623 struct io_timeout_data, timer);
4624 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004625 struct io_ring_ctx *ctx = req->ctx;
4626 struct io_kiocb *prev = NULL;
4627 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004628
4629 spin_lock_irqsave(&ctx->completion_lock, flags);
4630
4631 /*
4632 * We don't expect the list to be empty, that will only happen if we
4633 * race with the completion of the linked work.
4634 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004635 if (!list_empty(&req->link_list)) {
4636 prev = list_entry(req->link_list.prev, struct io_kiocb,
4637 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004638 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004639 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004640 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4641 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004642 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004643 }
4644
4645 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4646
4647 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004648 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004649 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4650 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004651 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004652 } else {
4653 io_cqring_add_event(req, -ETIME);
4654 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004655 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004656 return HRTIMER_NORESTART;
4657}
4658
Jens Axboead8a48a2019-11-15 08:49:11 -07004659static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004660{
Jens Axboe76a46e02019-11-10 23:34:16 -07004661 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004662
Jens Axboe76a46e02019-11-10 23:34:16 -07004663 /*
4664 * If the list is now empty, then our linked request finished before
4665 * we got a chance to setup the timer
4666 */
4667 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004668 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004669 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004670
Jens Axboead8a48a2019-11-15 08:49:11 -07004671 data->timer.function = io_link_timeout_fn;
4672 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4673 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004674 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004675 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004676
Jens Axboe2665abf2019-11-05 12:40:47 -07004677 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004678 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004679}
4680
Jens Axboead8a48a2019-11-15 08:49:11 -07004681static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004682{
4683 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004684
Jens Axboe2665abf2019-11-05 12:40:47 -07004685 if (!(req->flags & REQ_F_LINK))
4686 return NULL;
4687
Pavel Begunkov44932332019-12-05 16:16:35 +03004688 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4689 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004690 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004691 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004692
Jens Axboe76a46e02019-11-10 23:34:16 -07004693 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004694 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004695}
4696
Jens Axboe3529d8c2019-12-19 18:24:38 -07004697static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004698{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004699 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004700 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004701 int ret;
4702
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004703again:
4704 linked_timeout = io_prep_linked_timeout(req);
4705
Jens Axboe3529d8c2019-12-19 18:24:38 -07004706 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004707
4708 /*
4709 * We async punt it if the file wasn't marked NOWAIT, or if the file
4710 * doesn't support non-blocking read/write attempts
4711 */
4712 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4713 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004714punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004715 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004716 ret = io_grab_files(req);
4717 if (ret)
4718 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004719 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004720
4721 /*
4722 * Queued up for async execution, worker will release
4723 * submit reference when the iocb is actually submitted.
4724 */
4725 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004726 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004727 }
Jens Axboee65ef562019-03-12 10:16:44 -06004728
Jens Axboefcb323c2019-10-24 12:39:47 -06004729err:
Jens Axboee65ef562019-03-12 10:16:44 -06004730 /* drop submission reference */
4731 io_put_req(req);
4732
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004733 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004734 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004735 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004736 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004737 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004738 }
4739
Jens Axboee65ef562019-03-12 10:16:44 -06004740 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004741 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004742 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004743 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004744 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004745 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004746done_req:
4747 if (nxt) {
4748 req = nxt;
4749 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004750
4751 if (req->flags & REQ_F_FORCE_ASYNC)
4752 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004753 goto again;
4754 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004755}
4756
Jens Axboe3529d8c2019-12-19 18:24:38 -07004757static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004758{
4759 int ret;
4760
Jens Axboe3529d8c2019-12-19 18:24:38 -07004761 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004762 if (ret) {
4763 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004764fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004765 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004766 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004767 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004768 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004769 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004770 ret = io_req_defer_prep(req, sqe);
4771 if (unlikely(ret < 0))
4772 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004773 /*
4774 * Never try inline submit of IOSQE_ASYNC is set, go straight
4775 * to async execution.
4776 */
4777 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4778 io_queue_async_work(req);
4779 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004780 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004781 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004782}
4783
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004784static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004785{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004786 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004787 io_cqring_add_event(req, -ECANCELED);
4788 io_double_put_req(req);
4789 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004790 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004791}
4792
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004793#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004794 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004795
Jens Axboe3529d8c2019-12-19 18:24:38 -07004796static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4797 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004798{
Jens Axboe75c6a032020-01-28 10:15:23 -07004799 const struct cred *old_creds = NULL;
Jackie Liua197f662019-11-08 08:09:12 -07004800 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004801 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004802 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004803
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004804 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004805
4806 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004807 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004808 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004809 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004810 }
4811
Jens Axboe75c6a032020-01-28 10:15:23 -07004812 id = READ_ONCE(sqe->personality);
4813 if (id) {
4814 const struct cred *personality_creds;
4815
4816 personality_creds = idr_find(&ctx->personality_idr, id);
4817 if (unlikely(!personality_creds)) {
4818 ret = -EINVAL;
4819 goto err_req;
4820 }
4821 old_creds = override_creds(personality_creds);
4822 }
4823
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004824 /* same numerical values with corresponding REQ_F_*, safe to copy */
4825 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4826 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004827
Jens Axboe3529d8c2019-12-19 18:24:38 -07004828 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004829 if (unlikely(ret)) {
4830err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004831 io_cqring_add_event(req, ret);
4832 io_double_put_req(req);
Jens Axboe75c6a032020-01-28 10:15:23 -07004833 if (old_creds)
4834 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004835 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004836 }
4837
Jens Axboe9e645e112019-05-10 16:07:28 -06004838 /*
4839 * If we already have a head request, queue this one for async
4840 * submittal once the head completes. If we don't have a head but
4841 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4842 * submitted sync once the chain is complete. If none of those
4843 * conditions are true (normal request), then just queue it.
4844 */
4845 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004846 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004847
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004848 /*
4849 * Taking sequential execution of a link, draining both sides
4850 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4851 * requests in the link. So, it drains the head and the
4852 * next after the link request. The last one is done via
4853 * drain_next flag to persist the effect across calls.
4854 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004855 if (sqe_flags & IOSQE_IO_DRAIN) {
4856 head->flags |= REQ_F_IO_DRAIN;
4857 ctx->drain_next = 1;
4858 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004859 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004860 ret = -EAGAIN;
4861 goto err_req;
4862 }
4863
Jens Axboe3529d8c2019-12-19 18:24:38 -07004864 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004865 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004866 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004867 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004868 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004869 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004870 trace_io_uring_link(ctx, req, head);
4871 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004872
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004873 /* last request of a link, enqueue the link */
4874 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4875 io_queue_link_head(head);
4876 *link = NULL;
4877 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004878 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004879 if (unlikely(ctx->drain_next)) {
4880 req->flags |= REQ_F_IO_DRAIN;
4881 req->ctx->drain_next = 0;
4882 }
4883 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4884 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004885 INIT_LIST_HEAD(&req->link_list);
4886 ret = io_req_defer_prep(req, sqe);
4887 if (ret)
4888 req->flags |= REQ_F_FAIL_LINK;
4889 *link = req;
4890 } else {
4891 io_queue_sqe(req, sqe);
4892 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004893 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004894
Jens Axboe75c6a032020-01-28 10:15:23 -07004895 if (old_creds)
4896 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004897 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004898}
4899
Jens Axboe9a56a232019-01-09 09:06:50 -07004900/*
4901 * Batched submission is done, ensure local IO is flushed out.
4902 */
4903static void io_submit_state_end(struct io_submit_state *state)
4904{
4905 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004906 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004907 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004908 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004909}
4910
4911/*
4912 * Start submission side cache.
4913 */
4914static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004915 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004916{
4917 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004918 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004919 state->file = NULL;
4920 state->ios_left = max_ios;
4921}
4922
Jens Axboe2b188cc2019-01-07 10:46:33 -07004923static void io_commit_sqring(struct io_ring_ctx *ctx)
4924{
Hristo Venev75b28af2019-08-26 17:23:46 +00004925 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004926
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004927 /*
4928 * Ensure any loads from the SQEs are done at this point,
4929 * since once we write the new head, the application could
4930 * write new data to them.
4931 */
4932 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004933}
4934
4935/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004936 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004937 * that is mapped by userspace. This means that care needs to be taken to
4938 * ensure that reads are stable, as we cannot rely on userspace always
4939 * being a good citizen. If members of the sqe are validated and then later
4940 * used, it's important that those reads are done through READ_ONCE() to
4941 * prevent a re-load down the line.
4942 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004943static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4944 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004945{
Hristo Venev75b28af2019-08-26 17:23:46 +00004946 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004947 unsigned head;
4948
4949 /*
4950 * The cached sq head (or cq tail) serves two purposes:
4951 *
4952 * 1) allows us to batch the cost of updating the user visible
4953 * head updates.
4954 * 2) allows the kernel side to track the head on its own, even
4955 * though the application is the one updating it.
4956 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004957 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004958 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004959 /*
4960 * All io need record the previous position, if LINK vs DARIN,
4961 * it can be used to mark the position of the first IO in the
4962 * link list.
4963 */
4964 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004965 *sqe_ptr = &ctx->sq_sqes[head];
4966 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4967 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004968 ctx->cached_sq_head++;
4969 return true;
4970 }
4971
4972 /* drop invalid entries */
4973 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004974 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004975 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004976 return false;
4977}
4978
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004979static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004980 struct file *ring_file, int ring_fd,
4981 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004982{
4983 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004984 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004985 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004986 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004987
Jens Axboec4a2ed72019-11-21 21:01:26 -07004988 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004989 if (test_bit(0, &ctx->sq_check_overflow)) {
4990 if (!list_empty(&ctx->cq_overflow_list) &&
4991 !io_cqring_overflow_flush(ctx, false))
4992 return -EBUSY;
4993 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004994
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004995 /* make sure SQ entry isn't read before tail */
4996 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004997
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004998 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4999 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005000
5001 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005002 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005003 statep = &state;
5004 }
5005
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005006 ctx->ring_fd = ring_fd;
5007 ctx->ring_file = ring_file;
5008
Jens Axboe6c271ce2019-01-10 11:22:30 -07005009 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005010 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005011 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005012 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005013
Pavel Begunkov196be952019-11-07 01:41:06 +03005014 req = io_get_req(ctx, statep);
5015 if (unlikely(!req)) {
5016 if (!submitted)
5017 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005018 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005019 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005020 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005021 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005022 break;
5023 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005024
Jens Axboed3656342019-12-18 09:50:26 -07005025 /* will complete beyond this point, count as submitted */
5026 submitted++;
5027
5028 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005029 err = -EINVAL;
5030fail_req:
5031 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005032 io_double_put_req(req);
5033 break;
5034 }
5035
5036 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005037 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005038 if (unlikely(mm_fault)) {
5039 err = -EFAULT;
5040 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005041 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005042 use_mm(ctx->sqo_mm);
5043 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005044 }
5045
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005046 req->in_async = async;
5047 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005048 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5049 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005050 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005051 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005052 }
5053
Pavel Begunkov9466f432020-01-25 22:34:01 +03005054 if (unlikely(submitted != nr)) {
5055 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5056
5057 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5058 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005059 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005060 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005061 if (statep)
5062 io_submit_state_end(&state);
5063
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005064 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5065 io_commit_sqring(ctx);
5066
Jens Axboe6c271ce2019-01-10 11:22:30 -07005067 return submitted;
5068}
5069
5070static int io_sq_thread(void *data)
5071{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005072 struct io_ring_ctx *ctx = data;
5073 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005074 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005075 mm_segment_t old_fs;
5076 DEFINE_WAIT(wait);
5077 unsigned inflight;
5078 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07005079 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005080
Jens Axboe206aefd2019-11-07 18:27:42 -07005081 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005082
Jens Axboe6c271ce2019-01-10 11:22:30 -07005083 old_fs = get_fs();
5084 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005085 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005086
Jens Axboec1edbf52019-11-10 16:56:04 -07005087 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005088 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005089 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005090
5091 if (inflight) {
5092 unsigned nr_events = 0;
5093
5094 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06005095 /*
5096 * inflight is the count of the maximum possible
5097 * entries we submitted, but it can be smaller
5098 * if we dropped some of them. If we don't have
5099 * poll entries available, then we know that we
5100 * have nothing left to poll for. Reset the
5101 * inflight count to zero in that case.
5102 */
5103 mutex_lock(&ctx->uring_lock);
5104 if (!list_empty(&ctx->poll_list))
5105 __io_iopoll_check(ctx, &nr_events, 0);
5106 else
5107 inflight = 0;
5108 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005109 } else {
5110 /*
5111 * Normal IO, just pretend everything completed.
5112 * We don't have to poll completions for that.
5113 */
5114 nr_events = inflight;
5115 }
5116
5117 inflight -= nr_events;
5118 if (!inflight)
5119 timeout = jiffies + ctx->sq_thread_idle;
5120 }
5121
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005122 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005123
5124 /*
5125 * If submit got -EBUSY, flag us as needing the application
5126 * to enter the kernel to reap and flush events.
5127 */
5128 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005129 /*
5130 * We're polling. If we're within the defined idle
5131 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005132 * to sleep. The exception is if we got EBUSY doing
5133 * more IO, we should wait for the application to
5134 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005135 */
Jens Axboec1edbf52019-11-10 16:56:04 -07005136 if (inflight ||
Jens Axboedf069d82020-02-04 16:48:34 -07005137 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5138 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005139 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005140 continue;
5141 }
5142
5143 /*
5144 * Drop cur_mm before scheduling, we can't hold it for
5145 * long periods (or over schedule()). Do this before
5146 * adding ourselves to the waitqueue, as the unuse/drop
5147 * may sleep.
5148 */
5149 if (cur_mm) {
5150 unuse_mm(cur_mm);
5151 mmput(cur_mm);
5152 cur_mm = NULL;
5153 }
5154
5155 prepare_to_wait(&ctx->sqo_wait, &wait,
5156 TASK_INTERRUPTIBLE);
5157
5158 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005159 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005160 /* make sure to read SQ tail after writing flags */
5161 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005162
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005163 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005164 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005165 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005166 finish_wait(&ctx->sqo_wait, &wait);
5167 break;
5168 }
5169 if (signal_pending(current))
5170 flush_signals(current);
5171 schedule();
5172 finish_wait(&ctx->sqo_wait, &wait);
5173
Hristo Venev75b28af2019-08-26 17:23:46 +00005174 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005175 continue;
5176 }
5177 finish_wait(&ctx->sqo_wait, &wait);
5178
Hristo Venev75b28af2019-08-26 17:23:46 +00005179 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005180 }
5181
Jens Axboe8a4955f2019-12-09 14:52:35 -07005182 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005183 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005184 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005185 if (ret > 0)
5186 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005187 }
5188
5189 set_fs(old_fs);
5190 if (cur_mm) {
5191 unuse_mm(cur_mm);
5192 mmput(cur_mm);
5193 }
Jens Axboe181e4482019-11-25 08:52:30 -07005194 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005195
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005196 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005197
Jens Axboe6c271ce2019-01-10 11:22:30 -07005198 return 0;
5199}
5200
Jens Axboebda52162019-09-24 13:47:15 -06005201struct io_wait_queue {
5202 struct wait_queue_entry wq;
5203 struct io_ring_ctx *ctx;
5204 unsigned to_wait;
5205 unsigned nr_timeouts;
5206};
5207
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005208static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005209{
5210 struct io_ring_ctx *ctx = iowq->ctx;
5211
5212 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005213 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005214 * started waiting. For timeouts, we always want to return to userspace,
5215 * regardless of event count.
5216 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005217 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005218 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5219}
5220
5221static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5222 int wake_flags, void *key)
5223{
5224 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5225 wq);
5226
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005227 /* use noflush == true, as we can't safely rely on locking context */
5228 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005229 return -1;
5230
5231 return autoremove_wake_function(curr, mode, wake_flags, key);
5232}
5233
Jens Axboe2b188cc2019-01-07 10:46:33 -07005234/*
5235 * Wait until events become available, if we don't already have some. The
5236 * application must reap them itself, as they reside on the shared cq ring.
5237 */
5238static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5239 const sigset_t __user *sig, size_t sigsz)
5240{
Jens Axboebda52162019-09-24 13:47:15 -06005241 struct io_wait_queue iowq = {
5242 .wq = {
5243 .private = current,
5244 .func = io_wake_function,
5245 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5246 },
5247 .ctx = ctx,
5248 .to_wait = min_events,
5249 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005250 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005251 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005252
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005253 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005254 return 0;
5255
5256 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005257#ifdef CONFIG_COMPAT
5258 if (in_compat_syscall())
5259 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005260 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005261 else
5262#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005263 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005264
Jens Axboe2b188cc2019-01-07 10:46:33 -07005265 if (ret)
5266 return ret;
5267 }
5268
Jens Axboebda52162019-09-24 13:47:15 -06005269 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005270 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005271 do {
5272 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5273 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005274 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005275 break;
5276 schedule();
5277 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005278 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005279 break;
5280 }
5281 } while (1);
5282 finish_wait(&ctx->wait, &iowq.wq);
5283
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005284 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005285
Hristo Venev75b28af2019-08-26 17:23:46 +00005286 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005287}
5288
Jens Axboe6b063142019-01-10 22:13:58 -07005289static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5290{
5291#if defined(CONFIG_UNIX)
5292 if (ctx->ring_sock) {
5293 struct sock *sock = ctx->ring_sock->sk;
5294 struct sk_buff *skb;
5295
5296 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5297 kfree_skb(skb);
5298 }
5299#else
5300 int i;
5301
Jens Axboe65e19f52019-10-26 07:20:21 -06005302 for (i = 0; i < ctx->nr_user_files; i++) {
5303 struct file *file;
5304
5305 file = io_file_from_index(ctx, i);
5306 if (file)
5307 fput(file);
5308 }
Jens Axboe6b063142019-01-10 22:13:58 -07005309#endif
5310}
5311
Jens Axboe05f3fb32019-12-09 11:22:50 -07005312static void io_file_ref_kill(struct percpu_ref *ref)
5313{
5314 struct fixed_file_data *data;
5315
5316 data = container_of(ref, struct fixed_file_data, refs);
5317 complete(&data->done);
5318}
5319
Jens Axboe6b063142019-01-10 22:13:58 -07005320static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5321{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005322 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005323 unsigned nr_tables, i;
5324
Jens Axboe05f3fb32019-12-09 11:22:50 -07005325 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005326 return -ENXIO;
5327
Jens Axboe05f3fb32019-12-09 11:22:50 -07005328 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005329 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005330 wait_for_completion(&data->done);
5331 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005332 percpu_ref_exit(&data->refs);
5333
Jens Axboe6b063142019-01-10 22:13:58 -07005334 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005335 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5336 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005337 kfree(data->table[i].files);
5338 kfree(data->table);
5339 kfree(data);
5340 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005341 ctx->nr_user_files = 0;
5342 return 0;
5343}
5344
Jens Axboe6c271ce2019-01-10 11:22:30 -07005345static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5346{
5347 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005348 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005349 /*
5350 * The park is a bit of a work-around, without it we get
5351 * warning spews on shutdown with SQPOLL set and affinity
5352 * set to a single CPU.
5353 */
Jens Axboe06058632019-04-13 09:26:03 -06005354 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005355 kthread_stop(ctx->sqo_thread);
5356 ctx->sqo_thread = NULL;
5357 }
5358}
5359
Jens Axboe6b063142019-01-10 22:13:58 -07005360static void io_finish_async(struct io_ring_ctx *ctx)
5361{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005362 io_sq_thread_stop(ctx);
5363
Jens Axboe561fb042019-10-24 07:25:42 -06005364 if (ctx->io_wq) {
5365 io_wq_destroy(ctx->io_wq);
5366 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005367 }
5368}
5369
5370#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005371/*
5372 * Ensure the UNIX gc is aware of our file set, so we are certain that
5373 * the io_uring can be safely unregistered on process exit, even if we have
5374 * loops in the file referencing.
5375 */
5376static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5377{
5378 struct sock *sk = ctx->ring_sock->sk;
5379 struct scm_fp_list *fpl;
5380 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005381 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005382
5383 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5384 unsigned long inflight = ctx->user->unix_inflight + nr;
5385
5386 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5387 return -EMFILE;
5388 }
5389
5390 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5391 if (!fpl)
5392 return -ENOMEM;
5393
5394 skb = alloc_skb(0, GFP_KERNEL);
5395 if (!skb) {
5396 kfree(fpl);
5397 return -ENOMEM;
5398 }
5399
5400 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005401
Jens Axboe08a45172019-10-03 08:11:03 -06005402 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005403 fpl->user = get_uid(ctx->user);
5404 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005405 struct file *file = io_file_from_index(ctx, i + offset);
5406
5407 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005408 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005409 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005410 unix_inflight(fpl->user, fpl->fp[nr_files]);
5411 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005412 }
5413
Jens Axboe08a45172019-10-03 08:11:03 -06005414 if (nr_files) {
5415 fpl->max = SCM_MAX_FD;
5416 fpl->count = nr_files;
5417 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005418 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005419 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5420 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005421
Jens Axboe08a45172019-10-03 08:11:03 -06005422 for (i = 0; i < nr_files; i++)
5423 fput(fpl->fp[i]);
5424 } else {
5425 kfree_skb(skb);
5426 kfree(fpl);
5427 }
Jens Axboe6b063142019-01-10 22:13:58 -07005428
5429 return 0;
5430}
5431
5432/*
5433 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5434 * causes regular reference counting to break down. We rely on the UNIX
5435 * garbage collection to take care of this problem for us.
5436 */
5437static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5438{
5439 unsigned left, total;
5440 int ret = 0;
5441
5442 total = 0;
5443 left = ctx->nr_user_files;
5444 while (left) {
5445 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005446
5447 ret = __io_sqe_files_scm(ctx, this_files, total);
5448 if (ret)
5449 break;
5450 left -= this_files;
5451 total += this_files;
5452 }
5453
5454 if (!ret)
5455 return 0;
5456
5457 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005458 struct file *file = io_file_from_index(ctx, total);
5459
5460 if (file)
5461 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005462 total++;
5463 }
5464
5465 return ret;
5466}
5467#else
5468static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5469{
5470 return 0;
5471}
5472#endif
5473
Jens Axboe65e19f52019-10-26 07:20:21 -06005474static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5475 unsigned nr_files)
5476{
5477 int i;
5478
5479 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005480 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005481 unsigned this_files;
5482
5483 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5484 table->files = kcalloc(this_files, sizeof(struct file *),
5485 GFP_KERNEL);
5486 if (!table->files)
5487 break;
5488 nr_files -= this_files;
5489 }
5490
5491 if (i == nr_tables)
5492 return 0;
5493
5494 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005495 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005496 kfree(table->files);
5497 }
5498 return 1;
5499}
5500
Jens Axboe05f3fb32019-12-09 11:22:50 -07005501static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005502{
5503#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005504 struct sock *sock = ctx->ring_sock->sk;
5505 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5506 struct sk_buff *skb;
5507 int i;
5508
5509 __skb_queue_head_init(&list);
5510
5511 /*
5512 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5513 * remove this entry and rearrange the file array.
5514 */
5515 skb = skb_dequeue(head);
5516 while (skb) {
5517 struct scm_fp_list *fp;
5518
5519 fp = UNIXCB(skb).fp;
5520 for (i = 0; i < fp->count; i++) {
5521 int left;
5522
5523 if (fp->fp[i] != file)
5524 continue;
5525
5526 unix_notinflight(fp->user, fp->fp[i]);
5527 left = fp->count - 1 - i;
5528 if (left) {
5529 memmove(&fp->fp[i], &fp->fp[i + 1],
5530 left * sizeof(struct file *));
5531 }
5532 fp->count--;
5533 if (!fp->count) {
5534 kfree_skb(skb);
5535 skb = NULL;
5536 } else {
5537 __skb_queue_tail(&list, skb);
5538 }
5539 fput(file);
5540 file = NULL;
5541 break;
5542 }
5543
5544 if (!file)
5545 break;
5546
5547 __skb_queue_tail(&list, skb);
5548
5549 skb = skb_dequeue(head);
5550 }
5551
5552 if (skb_peek(&list)) {
5553 spin_lock_irq(&head->lock);
5554 while ((skb = __skb_dequeue(&list)) != NULL)
5555 __skb_queue_tail(head, skb);
5556 spin_unlock_irq(&head->lock);
5557 }
5558#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005559 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005560#endif
5561}
5562
Jens Axboe05f3fb32019-12-09 11:22:50 -07005563struct io_file_put {
5564 struct llist_node llist;
5565 struct file *file;
5566 struct completion *done;
5567};
5568
Jens Axboe2faf8522020-02-04 19:54:55 -07005569static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005570{
5571 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005572 struct llist_node *node;
5573
Jens Axboe05f3fb32019-12-09 11:22:50 -07005574 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5575 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5576 io_ring_file_put(data->ctx, pfile->file);
5577 if (pfile->done)
5578 complete(pfile->done);
5579 else
5580 kfree(pfile);
5581 }
5582 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005583}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005584
Jens Axboe2faf8522020-02-04 19:54:55 -07005585static void io_ring_file_ref_switch(struct work_struct *work)
5586{
5587 struct fixed_file_data *data;
5588
5589 data = container_of(work, struct fixed_file_data, ref_work);
5590 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005591 percpu_ref_get(&data->refs);
5592 percpu_ref_switch_to_percpu(&data->refs);
5593}
5594
5595static void io_file_data_ref_zero(struct percpu_ref *ref)
5596{
5597 struct fixed_file_data *data;
5598
5599 data = container_of(ref, struct fixed_file_data, refs);
5600
Jens Axboe2faf8522020-02-04 19:54:55 -07005601 /*
5602 * We can't safely switch from inside this context, punt to wq. If
5603 * the table ref is going away, the table is being unregistered.
5604 * Don't queue up the async work for that case, the caller will
5605 * handle it.
5606 */
5607 if (!percpu_ref_is_dying(&data->refs))
5608 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005609}
5610
5611static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5612 unsigned nr_args)
5613{
5614 __s32 __user *fds = (__s32 __user *) arg;
5615 unsigned nr_tables;
5616 struct file *file;
5617 int fd, ret = 0;
5618 unsigned i;
5619
5620 if (ctx->file_data)
5621 return -EBUSY;
5622 if (!nr_args)
5623 return -EINVAL;
5624 if (nr_args > IORING_MAX_FIXED_FILES)
5625 return -EMFILE;
5626
5627 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5628 if (!ctx->file_data)
5629 return -ENOMEM;
5630 ctx->file_data->ctx = ctx;
5631 init_completion(&ctx->file_data->done);
5632
5633 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5634 ctx->file_data->table = kcalloc(nr_tables,
5635 sizeof(struct fixed_file_table),
5636 GFP_KERNEL);
5637 if (!ctx->file_data->table) {
5638 kfree(ctx->file_data);
5639 ctx->file_data = NULL;
5640 return -ENOMEM;
5641 }
5642
5643 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5644 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5645 kfree(ctx->file_data->table);
5646 kfree(ctx->file_data);
5647 ctx->file_data = NULL;
5648 return -ENOMEM;
5649 }
5650 ctx->file_data->put_llist.first = NULL;
5651 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5652
5653 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5654 percpu_ref_exit(&ctx->file_data->refs);
5655 kfree(ctx->file_data->table);
5656 kfree(ctx->file_data);
5657 ctx->file_data = NULL;
5658 return -ENOMEM;
5659 }
5660
5661 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5662 struct fixed_file_table *table;
5663 unsigned index;
5664
5665 ret = -EFAULT;
5666 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5667 break;
5668 /* allow sparse sets */
5669 if (fd == -1) {
5670 ret = 0;
5671 continue;
5672 }
5673
5674 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5675 index = i & IORING_FILE_TABLE_MASK;
5676 file = fget(fd);
5677
5678 ret = -EBADF;
5679 if (!file)
5680 break;
5681
5682 /*
5683 * Don't allow io_uring instances to be registered. If UNIX
5684 * isn't enabled, then this causes a reference cycle and this
5685 * instance can never get freed. If UNIX is enabled we'll
5686 * handle it just fine, but there's still no point in allowing
5687 * a ring fd as it doesn't support regular read/write anyway.
5688 */
5689 if (file->f_op == &io_uring_fops) {
5690 fput(file);
5691 break;
5692 }
5693 ret = 0;
5694 table->files[index] = file;
5695 }
5696
5697 if (ret) {
5698 for (i = 0; i < ctx->nr_user_files; i++) {
5699 file = io_file_from_index(ctx, i);
5700 if (file)
5701 fput(file);
5702 }
5703 for (i = 0; i < nr_tables; i++)
5704 kfree(ctx->file_data->table[i].files);
5705
5706 kfree(ctx->file_data->table);
5707 kfree(ctx->file_data);
5708 ctx->file_data = NULL;
5709 ctx->nr_user_files = 0;
5710 return ret;
5711 }
5712
5713 ret = io_sqe_files_scm(ctx);
5714 if (ret)
5715 io_sqe_files_unregister(ctx);
5716
5717 return ret;
5718}
5719
Jens Axboec3a31e62019-10-03 13:59:56 -06005720static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5721 int index)
5722{
5723#if defined(CONFIG_UNIX)
5724 struct sock *sock = ctx->ring_sock->sk;
5725 struct sk_buff_head *head = &sock->sk_receive_queue;
5726 struct sk_buff *skb;
5727
5728 /*
5729 * See if we can merge this file into an existing skb SCM_RIGHTS
5730 * file set. If there's no room, fall back to allocating a new skb
5731 * and filling it in.
5732 */
5733 spin_lock_irq(&head->lock);
5734 skb = skb_peek(head);
5735 if (skb) {
5736 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5737
5738 if (fpl->count < SCM_MAX_FD) {
5739 __skb_unlink(skb, head);
5740 spin_unlock_irq(&head->lock);
5741 fpl->fp[fpl->count] = get_file(file);
5742 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5743 fpl->count++;
5744 spin_lock_irq(&head->lock);
5745 __skb_queue_head(head, skb);
5746 } else {
5747 skb = NULL;
5748 }
5749 }
5750 spin_unlock_irq(&head->lock);
5751
5752 if (skb) {
5753 fput(file);
5754 return 0;
5755 }
5756
5757 return __io_sqe_files_scm(ctx, 1, index);
5758#else
5759 return 0;
5760#endif
5761}
5762
Jens Axboe05f3fb32019-12-09 11:22:50 -07005763static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005764{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005765 struct fixed_file_data *data;
5766
5767 data = container_of(ref, struct fixed_file_data, refs);
5768 clear_bit(FFD_F_ATOMIC, &data->state);
5769}
5770
5771static bool io_queue_file_removal(struct fixed_file_data *data,
5772 struct file *file)
5773{
5774 struct io_file_put *pfile, pfile_stack;
5775 DECLARE_COMPLETION_ONSTACK(done);
5776
5777 /*
5778 * If we fail allocating the struct we need for doing async reomval
5779 * of this file, just punt to sync and wait for it.
5780 */
5781 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5782 if (!pfile) {
5783 pfile = &pfile_stack;
5784 pfile->done = &done;
5785 }
5786
5787 pfile->file = file;
5788 llist_add(&pfile->llist, &data->put_llist);
5789
5790 if (pfile == &pfile_stack) {
5791 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5792 percpu_ref_put(&data->refs);
5793 percpu_ref_switch_to_atomic(&data->refs,
5794 io_atomic_switch);
5795 }
5796 wait_for_completion(&done);
5797 flush_work(&data->ref_work);
5798 return false;
5799 }
5800
5801 return true;
5802}
5803
5804static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5805 struct io_uring_files_update *up,
5806 unsigned nr_args)
5807{
5808 struct fixed_file_data *data = ctx->file_data;
5809 bool ref_switch = false;
5810 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005811 __s32 __user *fds;
5812 int fd, i, err;
5813 __u32 done;
5814
Jens Axboe05f3fb32019-12-09 11:22:50 -07005815 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005816 return -EOVERFLOW;
5817 if (done > ctx->nr_user_files)
5818 return -EINVAL;
5819
5820 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005821 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005822 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005823 struct fixed_file_table *table;
5824 unsigned index;
5825
Jens Axboec3a31e62019-10-03 13:59:56 -06005826 err = 0;
5827 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5828 err = -EFAULT;
5829 break;
5830 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005831 i = array_index_nospec(up->offset, ctx->nr_user_files);
5832 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005833 index = i & IORING_FILE_TABLE_MASK;
5834 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005835 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005836 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005837 if (io_queue_file_removal(data, file))
5838 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005839 }
5840 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005841 file = fget(fd);
5842 if (!file) {
5843 err = -EBADF;
5844 break;
5845 }
5846 /*
5847 * Don't allow io_uring instances to be registered. If
5848 * UNIX isn't enabled, then this causes a reference
5849 * cycle and this instance can never get freed. If UNIX
5850 * is enabled we'll handle it just fine, but there's
5851 * still no point in allowing a ring fd as it doesn't
5852 * support regular read/write anyway.
5853 */
5854 if (file->f_op == &io_uring_fops) {
5855 fput(file);
5856 err = -EBADF;
5857 break;
5858 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005859 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005860 err = io_sqe_file_register(ctx, file, i);
5861 if (err)
5862 break;
5863 }
5864 nr_args--;
5865 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005866 up->offset++;
5867 }
5868
5869 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5870 percpu_ref_put(&data->refs);
5871 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005872 }
5873
5874 return done ? done : err;
5875}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005876static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5877 unsigned nr_args)
5878{
5879 struct io_uring_files_update up;
5880
5881 if (!ctx->file_data)
5882 return -ENXIO;
5883 if (!nr_args)
5884 return -EINVAL;
5885 if (copy_from_user(&up, arg, sizeof(up)))
5886 return -EFAULT;
5887 if (up.resv)
5888 return -EINVAL;
5889
5890 return __io_sqe_files_update(ctx, &up, nr_args);
5891}
Jens Axboec3a31e62019-10-03 13:59:56 -06005892
Jens Axboe7d723062019-11-12 22:31:31 -07005893static void io_put_work(struct io_wq_work *work)
5894{
5895 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5896
5897 io_put_req(req);
5898}
5899
5900static void io_get_work(struct io_wq_work *work)
5901{
5902 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5903
5904 refcount_inc(&req->refs);
5905}
5906
Pavel Begunkov24369c22020-01-28 03:15:48 +03005907static int io_init_wq_offload(struct io_ring_ctx *ctx,
5908 struct io_uring_params *p)
5909{
5910 struct io_wq_data data;
5911 struct fd f;
5912 struct io_ring_ctx *ctx_attach;
5913 unsigned int concurrency;
5914 int ret = 0;
5915
5916 data.user = ctx->user;
5917 data.get_work = io_get_work;
5918 data.put_work = io_put_work;
5919
5920 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5921 /* Do QD, or 4 * CPUS, whatever is smallest */
5922 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5923
5924 ctx->io_wq = io_wq_create(concurrency, &data);
5925 if (IS_ERR(ctx->io_wq)) {
5926 ret = PTR_ERR(ctx->io_wq);
5927 ctx->io_wq = NULL;
5928 }
5929 return ret;
5930 }
5931
5932 f = fdget(p->wq_fd);
5933 if (!f.file)
5934 return -EBADF;
5935
5936 if (f.file->f_op != &io_uring_fops) {
5937 ret = -EINVAL;
5938 goto out_fput;
5939 }
5940
5941 ctx_attach = f.file->private_data;
5942 /* @io_wq is protected by holding the fd */
5943 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5944 ret = -EINVAL;
5945 goto out_fput;
5946 }
5947
5948 ctx->io_wq = ctx_attach->io_wq;
5949out_fput:
5950 fdput(f);
5951 return ret;
5952}
5953
Jens Axboe6c271ce2019-01-10 11:22:30 -07005954static int io_sq_offload_start(struct io_ring_ctx *ctx,
5955 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005956{
5957 int ret;
5958
Jens Axboe6c271ce2019-01-10 11:22:30 -07005959 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005960 mmgrab(current->mm);
5961 ctx->sqo_mm = current->mm;
5962
Jens Axboe6c271ce2019-01-10 11:22:30 -07005963 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005964 ret = -EPERM;
5965 if (!capable(CAP_SYS_ADMIN))
5966 goto err;
5967
Jens Axboe917257d2019-04-13 09:28:55 -06005968 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5969 if (!ctx->sq_thread_idle)
5970 ctx->sq_thread_idle = HZ;
5971
Jens Axboe6c271ce2019-01-10 11:22:30 -07005972 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005973 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005974
Jens Axboe917257d2019-04-13 09:28:55 -06005975 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005976 if (cpu >= nr_cpu_ids)
5977 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005978 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005979 goto err;
5980
Jens Axboe6c271ce2019-01-10 11:22:30 -07005981 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5982 ctx, cpu,
5983 "io_uring-sq");
5984 } else {
5985 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5986 "io_uring-sq");
5987 }
5988 if (IS_ERR(ctx->sqo_thread)) {
5989 ret = PTR_ERR(ctx->sqo_thread);
5990 ctx->sqo_thread = NULL;
5991 goto err;
5992 }
5993 wake_up_process(ctx->sqo_thread);
5994 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5995 /* Can't have SQ_AFF without SQPOLL */
5996 ret = -EINVAL;
5997 goto err;
5998 }
5999
Pavel Begunkov24369c22020-01-28 03:15:48 +03006000 ret = io_init_wq_offload(ctx, p);
6001 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006002 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006003
6004 return 0;
6005err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006006 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006007 mmdrop(ctx->sqo_mm);
6008 ctx->sqo_mm = NULL;
6009 return ret;
6010}
6011
6012static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6013{
6014 atomic_long_sub(nr_pages, &user->locked_vm);
6015}
6016
6017static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6018{
6019 unsigned long page_limit, cur_pages, new_pages;
6020
6021 /* Don't allow more pages than we can safely lock */
6022 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6023
6024 do {
6025 cur_pages = atomic_long_read(&user->locked_vm);
6026 new_pages = cur_pages + nr_pages;
6027 if (new_pages > page_limit)
6028 return -ENOMEM;
6029 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6030 new_pages) != cur_pages);
6031
6032 return 0;
6033}
6034
6035static void io_mem_free(void *ptr)
6036{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006037 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006038
Mark Rutland52e04ef2019-04-30 17:30:21 +01006039 if (!ptr)
6040 return;
6041
6042 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006043 if (put_page_testzero(page))
6044 free_compound_page(page);
6045}
6046
6047static void *io_mem_alloc(size_t size)
6048{
6049 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6050 __GFP_NORETRY;
6051
6052 return (void *) __get_free_pages(gfp_flags, get_order(size));
6053}
6054
Hristo Venev75b28af2019-08-26 17:23:46 +00006055static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6056 size_t *sq_offset)
6057{
6058 struct io_rings *rings;
6059 size_t off, sq_array_size;
6060
6061 off = struct_size(rings, cqes, cq_entries);
6062 if (off == SIZE_MAX)
6063 return SIZE_MAX;
6064
6065#ifdef CONFIG_SMP
6066 off = ALIGN(off, SMP_CACHE_BYTES);
6067 if (off == 0)
6068 return SIZE_MAX;
6069#endif
6070
6071 sq_array_size = array_size(sizeof(u32), sq_entries);
6072 if (sq_array_size == SIZE_MAX)
6073 return SIZE_MAX;
6074
6075 if (check_add_overflow(off, sq_array_size, &off))
6076 return SIZE_MAX;
6077
6078 if (sq_offset)
6079 *sq_offset = off;
6080
6081 return off;
6082}
6083
Jens Axboe2b188cc2019-01-07 10:46:33 -07006084static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6085{
Hristo Venev75b28af2019-08-26 17:23:46 +00006086 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006087
Hristo Venev75b28af2019-08-26 17:23:46 +00006088 pages = (size_t)1 << get_order(
6089 rings_size(sq_entries, cq_entries, NULL));
6090 pages += (size_t)1 << get_order(
6091 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006092
Hristo Venev75b28af2019-08-26 17:23:46 +00006093 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006094}
6095
Jens Axboeedafcce2019-01-09 09:16:05 -07006096static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6097{
6098 int i, j;
6099
6100 if (!ctx->user_bufs)
6101 return -ENXIO;
6102
6103 for (i = 0; i < ctx->nr_user_bufs; i++) {
6104 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6105
6106 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006107 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006108
6109 if (ctx->account_mem)
6110 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006111 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006112 imu->nr_bvecs = 0;
6113 }
6114
6115 kfree(ctx->user_bufs);
6116 ctx->user_bufs = NULL;
6117 ctx->nr_user_bufs = 0;
6118 return 0;
6119}
6120
6121static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6122 void __user *arg, unsigned index)
6123{
6124 struct iovec __user *src;
6125
6126#ifdef CONFIG_COMPAT
6127 if (ctx->compat) {
6128 struct compat_iovec __user *ciovs;
6129 struct compat_iovec ciov;
6130
6131 ciovs = (struct compat_iovec __user *) arg;
6132 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6133 return -EFAULT;
6134
Jens Axboed55e5f52019-12-11 16:12:15 -07006135 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006136 dst->iov_len = ciov.iov_len;
6137 return 0;
6138 }
6139#endif
6140 src = (struct iovec __user *) arg;
6141 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6142 return -EFAULT;
6143 return 0;
6144}
6145
6146static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6147 unsigned nr_args)
6148{
6149 struct vm_area_struct **vmas = NULL;
6150 struct page **pages = NULL;
6151 int i, j, got_pages = 0;
6152 int ret = -EINVAL;
6153
6154 if (ctx->user_bufs)
6155 return -EBUSY;
6156 if (!nr_args || nr_args > UIO_MAXIOV)
6157 return -EINVAL;
6158
6159 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6160 GFP_KERNEL);
6161 if (!ctx->user_bufs)
6162 return -ENOMEM;
6163
6164 for (i = 0; i < nr_args; i++) {
6165 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6166 unsigned long off, start, end, ubuf;
6167 int pret, nr_pages;
6168 struct iovec iov;
6169 size_t size;
6170
6171 ret = io_copy_iov(ctx, &iov, arg, i);
6172 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006173 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006174
6175 /*
6176 * Don't impose further limits on the size and buffer
6177 * constraints here, we'll -EINVAL later when IO is
6178 * submitted if they are wrong.
6179 */
6180 ret = -EFAULT;
6181 if (!iov.iov_base || !iov.iov_len)
6182 goto err;
6183
6184 /* arbitrary limit, but we need something */
6185 if (iov.iov_len > SZ_1G)
6186 goto err;
6187
6188 ubuf = (unsigned long) iov.iov_base;
6189 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6190 start = ubuf >> PAGE_SHIFT;
6191 nr_pages = end - start;
6192
6193 if (ctx->account_mem) {
6194 ret = io_account_mem(ctx->user, nr_pages);
6195 if (ret)
6196 goto err;
6197 }
6198
6199 ret = 0;
6200 if (!pages || nr_pages > got_pages) {
6201 kfree(vmas);
6202 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006203 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006204 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006205 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006206 sizeof(struct vm_area_struct *),
6207 GFP_KERNEL);
6208 if (!pages || !vmas) {
6209 ret = -ENOMEM;
6210 if (ctx->account_mem)
6211 io_unaccount_mem(ctx->user, nr_pages);
6212 goto err;
6213 }
6214 got_pages = nr_pages;
6215 }
6216
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006217 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006218 GFP_KERNEL);
6219 ret = -ENOMEM;
6220 if (!imu->bvec) {
6221 if (ctx->account_mem)
6222 io_unaccount_mem(ctx->user, nr_pages);
6223 goto err;
6224 }
6225
6226 ret = 0;
6227 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006228 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006229 FOLL_WRITE | FOLL_LONGTERM,
6230 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006231 if (pret == nr_pages) {
6232 /* don't support file backed memory */
6233 for (j = 0; j < nr_pages; j++) {
6234 struct vm_area_struct *vma = vmas[j];
6235
6236 if (vma->vm_file &&
6237 !is_file_hugepages(vma->vm_file)) {
6238 ret = -EOPNOTSUPP;
6239 break;
6240 }
6241 }
6242 } else {
6243 ret = pret < 0 ? pret : -EFAULT;
6244 }
6245 up_read(&current->mm->mmap_sem);
6246 if (ret) {
6247 /*
6248 * if we did partial map, or found file backed vmas,
6249 * release any pages we did get
6250 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006251 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006252 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006253 if (ctx->account_mem)
6254 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006255 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006256 goto err;
6257 }
6258
6259 off = ubuf & ~PAGE_MASK;
6260 size = iov.iov_len;
6261 for (j = 0; j < nr_pages; j++) {
6262 size_t vec_len;
6263
6264 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6265 imu->bvec[j].bv_page = pages[j];
6266 imu->bvec[j].bv_len = vec_len;
6267 imu->bvec[j].bv_offset = off;
6268 off = 0;
6269 size -= vec_len;
6270 }
6271 /* store original address for later verification */
6272 imu->ubuf = ubuf;
6273 imu->len = iov.iov_len;
6274 imu->nr_bvecs = nr_pages;
6275
6276 ctx->nr_user_bufs++;
6277 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006278 kvfree(pages);
6279 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006280 return 0;
6281err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006282 kvfree(pages);
6283 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006284 io_sqe_buffer_unregister(ctx);
6285 return ret;
6286}
6287
Jens Axboe9b402842019-04-11 11:45:41 -06006288static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6289{
6290 __s32 __user *fds = arg;
6291 int fd;
6292
6293 if (ctx->cq_ev_fd)
6294 return -EBUSY;
6295
6296 if (copy_from_user(&fd, fds, sizeof(*fds)))
6297 return -EFAULT;
6298
6299 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6300 if (IS_ERR(ctx->cq_ev_fd)) {
6301 int ret = PTR_ERR(ctx->cq_ev_fd);
6302 ctx->cq_ev_fd = NULL;
6303 return ret;
6304 }
6305
6306 return 0;
6307}
6308
6309static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6310{
6311 if (ctx->cq_ev_fd) {
6312 eventfd_ctx_put(ctx->cq_ev_fd);
6313 ctx->cq_ev_fd = NULL;
6314 return 0;
6315 }
6316
6317 return -ENXIO;
6318}
6319
Jens Axboe2b188cc2019-01-07 10:46:33 -07006320static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6321{
Jens Axboe6b063142019-01-10 22:13:58 -07006322 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006323 if (ctx->sqo_mm)
6324 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006325
6326 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006327 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006328 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006329 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006330
Jens Axboe2b188cc2019-01-07 10:46:33 -07006331#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006332 if (ctx->ring_sock) {
6333 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006334 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006335 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006336#endif
6337
Hristo Venev75b28af2019-08-26 17:23:46 +00006338 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006339 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006340
6341 percpu_ref_exit(&ctx->refs);
6342 if (ctx->account_mem)
6343 io_unaccount_mem(ctx->user,
6344 ring_pages(ctx->sq_entries, ctx->cq_entries));
6345 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006346 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006347 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006348 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006349 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006350 kfree(ctx);
6351}
6352
6353static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6354{
6355 struct io_ring_ctx *ctx = file->private_data;
6356 __poll_t mask = 0;
6357
6358 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006359 /*
6360 * synchronizes with barrier from wq_has_sleeper call in
6361 * io_commit_cqring
6362 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006363 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006364 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6365 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006366 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006367 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006368 mask |= EPOLLIN | EPOLLRDNORM;
6369
6370 return mask;
6371}
6372
6373static int io_uring_fasync(int fd, struct file *file, int on)
6374{
6375 struct io_ring_ctx *ctx = file->private_data;
6376
6377 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6378}
6379
Jens Axboe071698e2020-01-28 10:04:42 -07006380static int io_remove_personalities(int id, void *p, void *data)
6381{
6382 struct io_ring_ctx *ctx = data;
6383 const struct cred *cred;
6384
6385 cred = idr_remove(&ctx->personality_idr, id);
6386 if (cred)
6387 put_cred(cred);
6388 return 0;
6389}
6390
Jens Axboe2b188cc2019-01-07 10:46:33 -07006391static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6392{
6393 mutex_lock(&ctx->uring_lock);
6394 percpu_ref_kill(&ctx->refs);
6395 mutex_unlock(&ctx->uring_lock);
6396
Jens Axboedf069d82020-02-04 16:48:34 -07006397 /*
6398 * Wait for sq thread to idle, if we have one. It won't spin on new
6399 * work after we've killed the ctx ref above. This is important to do
6400 * before we cancel existing commands, as the thread could otherwise
6401 * be queueing new work post that. If that's work we need to cancel,
6402 * it could cause shutdown to hang.
6403 */
6404 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6405 cpu_relax();
6406
Jens Axboe5262f562019-09-17 12:26:57 -06006407 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006408 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006409
6410 if (ctx->io_wq)
6411 io_wq_cancel_all(ctx->io_wq);
6412
Jens Axboedef596e2019-01-09 08:59:42 -07006413 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006414 /* if we failed setting up the ctx, we might not have any rings */
6415 if (ctx->rings)
6416 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006417 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006418 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006419 io_ring_ctx_free(ctx);
6420}
6421
6422static int io_uring_release(struct inode *inode, struct file *file)
6423{
6424 struct io_ring_ctx *ctx = file->private_data;
6425
6426 file->private_data = NULL;
6427 io_ring_ctx_wait_and_kill(ctx);
6428 return 0;
6429}
6430
Jens Axboefcb323c2019-10-24 12:39:47 -06006431static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6432 struct files_struct *files)
6433{
6434 struct io_kiocb *req;
6435 DEFINE_WAIT(wait);
6436
6437 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006438 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006439
6440 spin_lock_irq(&ctx->inflight_lock);
6441 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006442 if (req->work.files != files)
6443 continue;
6444 /* req is being completed, ignore */
6445 if (!refcount_inc_not_zero(&req->refs))
6446 continue;
6447 cancel_req = req;
6448 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006449 }
Jens Axboe768134d2019-11-10 20:30:53 -07006450 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006451 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006452 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006453 spin_unlock_irq(&ctx->inflight_lock);
6454
Jens Axboe768134d2019-11-10 20:30:53 -07006455 /* We need to keep going until we don't find a matching req */
6456 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006457 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006458
6459 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6460 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006461 schedule();
6462 }
Jens Axboe768134d2019-11-10 20:30:53 -07006463 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006464}
6465
6466static int io_uring_flush(struct file *file, void *data)
6467{
6468 struct io_ring_ctx *ctx = file->private_data;
6469
6470 io_uring_cancel_files(ctx, data);
Jens Axboefcb323c2019-10-24 12:39:47 -06006471 return 0;
6472}
6473
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006474static void *io_uring_validate_mmap_request(struct file *file,
6475 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006476{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006477 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006478 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006479 struct page *page;
6480 void *ptr;
6481
6482 switch (offset) {
6483 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006484 case IORING_OFF_CQ_RING:
6485 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006486 break;
6487 case IORING_OFF_SQES:
6488 ptr = ctx->sq_sqes;
6489 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006490 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006491 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006492 }
6493
6494 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006495 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006496 return ERR_PTR(-EINVAL);
6497
6498 return ptr;
6499}
6500
6501#ifdef CONFIG_MMU
6502
6503static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6504{
6505 size_t sz = vma->vm_end - vma->vm_start;
6506 unsigned long pfn;
6507 void *ptr;
6508
6509 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6510 if (IS_ERR(ptr))
6511 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006512
6513 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6514 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6515}
6516
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006517#else /* !CONFIG_MMU */
6518
6519static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6520{
6521 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6522}
6523
6524static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6525{
6526 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6527}
6528
6529static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6530 unsigned long addr, unsigned long len,
6531 unsigned long pgoff, unsigned long flags)
6532{
6533 void *ptr;
6534
6535 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6536 if (IS_ERR(ptr))
6537 return PTR_ERR(ptr);
6538
6539 return (unsigned long) ptr;
6540}
6541
6542#endif /* !CONFIG_MMU */
6543
Jens Axboe2b188cc2019-01-07 10:46:33 -07006544SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6545 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6546 size_t, sigsz)
6547{
6548 struct io_ring_ctx *ctx;
6549 long ret = -EBADF;
6550 int submitted = 0;
6551 struct fd f;
6552
Jens Axboe6c271ce2019-01-10 11:22:30 -07006553 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006554 return -EINVAL;
6555
6556 f = fdget(fd);
6557 if (!f.file)
6558 return -EBADF;
6559
6560 ret = -EOPNOTSUPP;
6561 if (f.file->f_op != &io_uring_fops)
6562 goto out_fput;
6563
6564 ret = -ENXIO;
6565 ctx = f.file->private_data;
6566 if (!percpu_ref_tryget(&ctx->refs))
6567 goto out_fput;
6568
Jens Axboe6c271ce2019-01-10 11:22:30 -07006569 /*
6570 * For SQ polling, the thread will do all submissions and completions.
6571 * Just return the requested submit count, and wake the thread if
6572 * we were asked to.
6573 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006574 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006575 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006576 if (!list_empty_careful(&ctx->cq_overflow_list))
6577 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006578 if (flags & IORING_ENTER_SQ_WAKEUP)
6579 wake_up(&ctx->sqo_wait);
6580 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006581 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006582 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006583
6584 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006585 /* already have mm, so io_submit_sqes() won't try to grab it */
6586 cur_mm = ctx->sqo_mm;
6587 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6588 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006589 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006590
6591 if (submitted != to_submit)
6592 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006593 }
6594 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006595 unsigned nr_events = 0;
6596
Jens Axboe2b188cc2019-01-07 10:46:33 -07006597 min_complete = min(min_complete, ctx->cq_entries);
6598
Jens Axboedef596e2019-01-09 08:59:42 -07006599 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006600 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006601 } else {
6602 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6603 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006604 }
6605
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006606out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006607 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006608out_fput:
6609 fdput(f);
6610 return submitted ? submitted : ret;
6611}
6612
Jens Axboe87ce9552020-01-30 08:25:34 -07006613static int io_uring_show_cred(int id, void *p, void *data)
6614{
6615 const struct cred *cred = p;
6616 struct seq_file *m = data;
6617 struct user_namespace *uns = seq_user_ns(m);
6618 struct group_info *gi;
6619 kernel_cap_t cap;
6620 unsigned __capi;
6621 int g;
6622
6623 seq_printf(m, "%5d\n", id);
6624 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6625 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6626 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6627 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6628 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6629 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6630 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6631 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6632 seq_puts(m, "\n\tGroups:\t");
6633 gi = cred->group_info;
6634 for (g = 0; g < gi->ngroups; g++) {
6635 seq_put_decimal_ull(m, g ? " " : "",
6636 from_kgid_munged(uns, gi->gid[g]));
6637 }
6638 seq_puts(m, "\n\tCapEff:\t");
6639 cap = cred->cap_effective;
6640 CAP_FOR_EACH_U32(__capi)
6641 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6642 seq_putc(m, '\n');
6643 return 0;
6644}
6645
6646static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6647{
6648 int i;
6649
6650 mutex_lock(&ctx->uring_lock);
6651 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6652 for (i = 0; i < ctx->nr_user_files; i++) {
6653 struct fixed_file_table *table;
6654 struct file *f;
6655
6656 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6657 f = table->files[i & IORING_FILE_TABLE_MASK];
6658 if (f)
6659 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6660 else
6661 seq_printf(m, "%5u: <none>\n", i);
6662 }
6663 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6664 for (i = 0; i < ctx->nr_user_bufs; i++) {
6665 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6666
6667 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6668 (unsigned int) buf->len);
6669 }
6670 if (!idr_is_empty(&ctx->personality_idr)) {
6671 seq_printf(m, "Personalities:\n");
6672 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6673 }
6674 mutex_unlock(&ctx->uring_lock);
6675}
6676
6677static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6678{
6679 struct io_ring_ctx *ctx = f->private_data;
6680
6681 if (percpu_ref_tryget(&ctx->refs)) {
6682 __io_uring_show_fdinfo(ctx, m);
6683 percpu_ref_put(&ctx->refs);
6684 }
6685}
6686
Jens Axboe2b188cc2019-01-07 10:46:33 -07006687static const struct file_operations io_uring_fops = {
6688 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006689 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006690 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006691#ifndef CONFIG_MMU
6692 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6693 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6694#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006695 .poll = io_uring_poll,
6696 .fasync = io_uring_fasync,
Jens Axboe87ce9552020-01-30 08:25:34 -07006697 .show_fdinfo = io_uring_show_fdinfo,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006698};
6699
6700static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6701 struct io_uring_params *p)
6702{
Hristo Venev75b28af2019-08-26 17:23:46 +00006703 struct io_rings *rings;
6704 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006705
Hristo Venev75b28af2019-08-26 17:23:46 +00006706 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6707 if (size == SIZE_MAX)
6708 return -EOVERFLOW;
6709
6710 rings = io_mem_alloc(size);
6711 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006712 return -ENOMEM;
6713
Hristo Venev75b28af2019-08-26 17:23:46 +00006714 ctx->rings = rings;
6715 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6716 rings->sq_ring_mask = p->sq_entries - 1;
6717 rings->cq_ring_mask = p->cq_entries - 1;
6718 rings->sq_ring_entries = p->sq_entries;
6719 rings->cq_ring_entries = p->cq_entries;
6720 ctx->sq_mask = rings->sq_ring_mask;
6721 ctx->cq_mask = rings->cq_ring_mask;
6722 ctx->sq_entries = rings->sq_ring_entries;
6723 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006724
6725 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006726 if (size == SIZE_MAX) {
6727 io_mem_free(ctx->rings);
6728 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006729 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006730 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006731
6732 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006733 if (!ctx->sq_sqes) {
6734 io_mem_free(ctx->rings);
6735 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006736 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006737 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006738
Jens Axboe2b188cc2019-01-07 10:46:33 -07006739 return 0;
6740}
6741
6742/*
6743 * Allocate an anonymous fd, this is what constitutes the application
6744 * visible backing of an io_uring instance. The application mmaps this
6745 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6746 * we have to tie this fd to a socket for file garbage collection purposes.
6747 */
6748static int io_uring_get_fd(struct io_ring_ctx *ctx)
6749{
6750 struct file *file;
6751 int ret;
6752
6753#if defined(CONFIG_UNIX)
6754 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6755 &ctx->ring_sock);
6756 if (ret)
6757 return ret;
6758#endif
6759
6760 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6761 if (ret < 0)
6762 goto err;
6763
6764 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6765 O_RDWR | O_CLOEXEC);
6766 if (IS_ERR(file)) {
6767 put_unused_fd(ret);
6768 ret = PTR_ERR(file);
6769 goto err;
6770 }
6771
6772#if defined(CONFIG_UNIX)
6773 ctx->ring_sock->file = file;
6774#endif
6775 fd_install(ret, file);
6776 return ret;
6777err:
6778#if defined(CONFIG_UNIX)
6779 sock_release(ctx->ring_sock);
6780 ctx->ring_sock = NULL;
6781#endif
6782 return ret;
6783}
6784
6785static int io_uring_create(unsigned entries, struct io_uring_params *p)
6786{
6787 struct user_struct *user = NULL;
6788 struct io_ring_ctx *ctx;
6789 bool account_mem;
6790 int ret;
6791
Jens Axboe8110c1a2019-12-28 15:39:54 -07006792 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006793 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006794 if (entries > IORING_MAX_ENTRIES) {
6795 if (!(p->flags & IORING_SETUP_CLAMP))
6796 return -EINVAL;
6797 entries = IORING_MAX_ENTRIES;
6798 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006799
6800 /*
6801 * Use twice as many entries for the CQ ring. It's possible for the
6802 * application to drive a higher depth than the size of the SQ ring,
6803 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006804 * some flexibility in overcommitting a bit. If the application has
6805 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6806 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006807 */
6808 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006809 if (p->flags & IORING_SETUP_CQSIZE) {
6810 /*
6811 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6812 * to a power-of-two, if it isn't already. We do NOT impose
6813 * any cq vs sq ring sizing.
6814 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006815 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006816 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006817 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6818 if (!(p->flags & IORING_SETUP_CLAMP))
6819 return -EINVAL;
6820 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6821 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006822 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6823 } else {
6824 p->cq_entries = 2 * p->sq_entries;
6825 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006826
6827 user = get_uid(current_user());
6828 account_mem = !capable(CAP_IPC_LOCK);
6829
6830 if (account_mem) {
6831 ret = io_account_mem(user,
6832 ring_pages(p->sq_entries, p->cq_entries));
6833 if (ret) {
6834 free_uid(user);
6835 return ret;
6836 }
6837 }
6838
6839 ctx = io_ring_ctx_alloc(p);
6840 if (!ctx) {
6841 if (account_mem)
6842 io_unaccount_mem(user, ring_pages(p->sq_entries,
6843 p->cq_entries));
6844 free_uid(user);
6845 return -ENOMEM;
6846 }
6847 ctx->compat = in_compat_syscall();
6848 ctx->account_mem = account_mem;
6849 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006850 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006851
6852 ret = io_allocate_scq_urings(ctx, p);
6853 if (ret)
6854 goto err;
6855
Jens Axboe6c271ce2019-01-10 11:22:30 -07006856 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006857 if (ret)
6858 goto err;
6859
Jens Axboe2b188cc2019-01-07 10:46:33 -07006860 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006861 p->sq_off.head = offsetof(struct io_rings, sq.head);
6862 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6863 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6864 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6865 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6866 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6867 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006868
6869 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006870 p->cq_off.head = offsetof(struct io_rings, cq.head);
6871 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6872 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6873 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6874 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6875 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006876
Jens Axboe044c1ab2019-10-28 09:15:33 -06006877 /*
6878 * Install ring fd as the very last thing, so we don't risk someone
6879 * having closed it before we finish setup
6880 */
6881 ret = io_uring_get_fd(ctx);
6882 if (ret < 0)
6883 goto err;
6884
Jens Axboeda8c9692019-12-02 18:51:26 -07006885 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006886 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6887 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006888 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006889 return ret;
6890err:
6891 io_ring_ctx_wait_and_kill(ctx);
6892 return ret;
6893}
6894
6895/*
6896 * Sets up an aio uring context, and returns the fd. Applications asks for a
6897 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6898 * params structure passed in.
6899 */
6900static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6901{
6902 struct io_uring_params p;
6903 long ret;
6904 int i;
6905
6906 if (copy_from_user(&p, params, sizeof(p)))
6907 return -EFAULT;
6908 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6909 if (p.resv[i])
6910 return -EINVAL;
6911 }
6912
Jens Axboe6c271ce2019-01-10 11:22:30 -07006913 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006914 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006915 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006916 return -EINVAL;
6917
6918 ret = io_uring_create(entries, &p);
6919 if (ret < 0)
6920 return ret;
6921
6922 if (copy_to_user(params, &p, sizeof(p)))
6923 return -EFAULT;
6924
6925 return ret;
6926}
6927
6928SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6929 struct io_uring_params __user *, params)
6930{
6931 return io_uring_setup(entries, params);
6932}
6933
Jens Axboe66f4af92020-01-16 15:36:52 -07006934static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6935{
6936 struct io_uring_probe *p;
6937 size_t size;
6938 int i, ret;
6939
6940 size = struct_size(p, ops, nr_args);
6941 if (size == SIZE_MAX)
6942 return -EOVERFLOW;
6943 p = kzalloc(size, GFP_KERNEL);
6944 if (!p)
6945 return -ENOMEM;
6946
6947 ret = -EFAULT;
6948 if (copy_from_user(p, arg, size))
6949 goto out;
6950 ret = -EINVAL;
6951 if (memchr_inv(p, 0, size))
6952 goto out;
6953
6954 p->last_op = IORING_OP_LAST - 1;
6955 if (nr_args > IORING_OP_LAST)
6956 nr_args = IORING_OP_LAST;
6957
6958 for (i = 0; i < nr_args; i++) {
6959 p->ops[i].op = i;
6960 if (!io_op_defs[i].not_supported)
6961 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6962 }
6963 p->ops_len = i;
6964
6965 ret = 0;
6966 if (copy_to_user(arg, p, size))
6967 ret = -EFAULT;
6968out:
6969 kfree(p);
6970 return ret;
6971}
6972
Jens Axboe071698e2020-01-28 10:04:42 -07006973static int io_register_personality(struct io_ring_ctx *ctx)
6974{
6975 const struct cred *creds = get_current_cred();
6976 int id;
6977
6978 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
6979 USHRT_MAX, GFP_KERNEL);
6980 if (id < 0)
6981 put_cred(creds);
6982 return id;
6983}
6984
6985static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
6986{
6987 const struct cred *old_creds;
6988
6989 old_creds = idr_remove(&ctx->personality_idr, id);
6990 if (old_creds) {
6991 put_cred(old_creds);
6992 return 0;
6993 }
6994
6995 return -EINVAL;
6996}
6997
6998static bool io_register_op_must_quiesce(int op)
6999{
7000 switch (op) {
7001 case IORING_UNREGISTER_FILES:
7002 case IORING_REGISTER_FILES_UPDATE:
7003 case IORING_REGISTER_PROBE:
7004 case IORING_REGISTER_PERSONALITY:
7005 case IORING_UNREGISTER_PERSONALITY:
7006 return false;
7007 default:
7008 return true;
7009 }
7010}
7011
Jens Axboeedafcce2019-01-09 09:16:05 -07007012static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7013 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007014 __releases(ctx->uring_lock)
7015 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007016{
7017 int ret;
7018
Jens Axboe35fa71a2019-04-22 10:23:23 -06007019 /*
7020 * We're inside the ring mutex, if the ref is already dying, then
7021 * someone else killed the ctx or is already going through
7022 * io_uring_register().
7023 */
7024 if (percpu_ref_is_dying(&ctx->refs))
7025 return -ENXIO;
7026
Jens Axboe071698e2020-01-28 10:04:42 -07007027 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007028 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007029
Jens Axboe05f3fb32019-12-09 11:22:50 -07007030 /*
7031 * Drop uring mutex before waiting for references to exit. If
7032 * another thread is currently inside io_uring_enter() it might
7033 * need to grab the uring_lock to make progress. If we hold it
7034 * here across the drain wait, then we can deadlock. It's safe
7035 * to drop the mutex here, since no new references will come in
7036 * after we've killed the percpu ref.
7037 */
7038 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007039 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007040 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007041 if (ret) {
7042 percpu_ref_resurrect(&ctx->refs);
7043 ret = -EINTR;
7044 goto out;
7045 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007046 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007047
7048 switch (opcode) {
7049 case IORING_REGISTER_BUFFERS:
7050 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7051 break;
7052 case IORING_UNREGISTER_BUFFERS:
7053 ret = -EINVAL;
7054 if (arg || nr_args)
7055 break;
7056 ret = io_sqe_buffer_unregister(ctx);
7057 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007058 case IORING_REGISTER_FILES:
7059 ret = io_sqe_files_register(ctx, arg, nr_args);
7060 break;
7061 case IORING_UNREGISTER_FILES:
7062 ret = -EINVAL;
7063 if (arg || nr_args)
7064 break;
7065 ret = io_sqe_files_unregister(ctx);
7066 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007067 case IORING_REGISTER_FILES_UPDATE:
7068 ret = io_sqe_files_update(ctx, arg, nr_args);
7069 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007070 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007071 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007072 ret = -EINVAL;
7073 if (nr_args != 1)
7074 break;
7075 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007076 if (ret)
7077 break;
7078 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7079 ctx->eventfd_async = 1;
7080 else
7081 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007082 break;
7083 case IORING_UNREGISTER_EVENTFD:
7084 ret = -EINVAL;
7085 if (arg || nr_args)
7086 break;
7087 ret = io_eventfd_unregister(ctx);
7088 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007089 case IORING_REGISTER_PROBE:
7090 ret = -EINVAL;
7091 if (!arg || nr_args > 256)
7092 break;
7093 ret = io_probe(ctx, arg, nr_args);
7094 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007095 case IORING_REGISTER_PERSONALITY:
7096 ret = -EINVAL;
7097 if (arg || nr_args)
7098 break;
7099 ret = io_register_personality(ctx);
7100 break;
7101 case IORING_UNREGISTER_PERSONALITY:
7102 ret = -EINVAL;
7103 if (arg)
7104 break;
7105 ret = io_unregister_personality(ctx, nr_args);
7106 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007107 default:
7108 ret = -EINVAL;
7109 break;
7110 }
7111
Jens Axboe071698e2020-01-28 10:04:42 -07007112 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007113 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007114 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007115out:
7116 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007117 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007118 return ret;
7119}
7120
7121SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7122 void __user *, arg, unsigned int, nr_args)
7123{
7124 struct io_ring_ctx *ctx;
7125 long ret = -EBADF;
7126 struct fd f;
7127
7128 f = fdget(fd);
7129 if (!f.file)
7130 return -EBADF;
7131
7132 ret = -EOPNOTSUPP;
7133 if (f.file->f_op != &io_uring_fops)
7134 goto out_fput;
7135
7136 ctx = f.file->private_data;
7137
7138 mutex_lock(&ctx->uring_lock);
7139 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7140 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007141 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7142 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007143out_fput:
7144 fdput(f);
7145 return ret;
7146}
7147
Jens Axboe2b188cc2019-01-07 10:46:33 -07007148static int __init io_uring_init(void)
7149{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007150#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7151 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7152 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7153} while (0)
7154
7155#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7156 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7157 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7158 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7159 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7160 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7161 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7162 BUILD_BUG_SQE_ELEM(8, __u64, off);
7163 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7164 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7165 BUILD_BUG_SQE_ELEM(24, __u32, len);
7166 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7167 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7168 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7169 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7170 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7171 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7172 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7173 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7174 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7175 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7176 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7177 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7178 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7179 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7180 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7181 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7182
Jens Axboed3656342019-12-18 09:50:26 -07007183 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007184 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7185 return 0;
7186};
7187__initcall(io_uring_init);