blob: 1f3ae208f6a66502d8115868f0624e6a19bfd4f8 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020082#define CREATE_TRACE_POINTS
83#include <trace/events/io_uring.h>
84
Jens Axboe2b188cc2019-01-07 10:46:33 -070085#include <uapi/linux/io_uring.h>
86
87#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060088#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070089
Daniel Xu5277dea2019-09-14 14:23:45 -070090#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060091#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060092
93/*
94 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
95 */
96#define IORING_FILE_TABLE_SHIFT 9
97#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
98#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
99#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700100
101struct io_uring {
102 u32 head ____cacheline_aligned_in_smp;
103 u32 tail ____cacheline_aligned_in_smp;
104};
105
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000107 * This data is shared with the application through the mmap at offsets
108 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 *
110 * The offsets to the member fields are published through struct
111 * io_sqring_offsets when calling io_uring_setup.
112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
115 * Head and tail offsets into the ring; the offsets need to be
116 * masked to get valid indices.
117 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 * The kernel controls head of the sq ring and the tail of the cq ring,
119 * and the application controls tail of the sq ring and the head of the
120 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 * ring_entries - 1)
126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 u32 sq_ring_mask, cq_ring_mask;
128 /* Ring sizes (constant, power of 2) */
129 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 /*
131 * Number of invalid entries dropped by the kernel due to
132 * invalid index stored in array
133 *
134 * Written by the kernel, shouldn't be modified by the
135 * application (i.e. get number of "new events" by comparing to
136 * cached value).
137 *
138 * After a new SQ head value was read by the application this
139 * counter includes all submissions that were dropped reaching
140 * the new SQ head (and possibly more).
141 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000142 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200143 /*
144 * Runtime flags
145 *
146 * Written by the kernel, shouldn't be modified by the
147 * application.
148 *
149 * The application needs a full memory barrier before checking
150 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
151 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000152 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 /*
154 * Number of completion events lost because the queue was full;
155 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800156 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 * the completion queue.
158 *
159 * Written by the kernel, shouldn't be modified by the
160 * application (i.e. get number of "new events" by comparing to
161 * cached value).
162 *
163 * As completion events come in out of order this counter is not
164 * ordered with any other data.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 /*
168 * Ring buffer of completion events.
169 *
170 * The kernel writes completion events fresh every time they are
171 * produced, so the application is allowed to modify pending
172 * entries.
173 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000174 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700175};
176
Jens Axboeedafcce2019-01-09 09:16:05 -0700177struct io_mapped_ubuf {
178 u64 ubuf;
179 size_t len;
180 struct bio_vec *bvec;
181 unsigned int nr_bvecs;
182};
183
Jens Axboe65e19f52019-10-26 07:20:21 -0600184struct fixed_file_table {
185 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700186};
187
Jens Axboe05f3fb32019-12-09 11:22:50 -0700188struct fixed_file_data {
189 struct fixed_file_table *table;
190 struct io_ring_ctx *ctx;
191
192 struct percpu_ref refs;
193 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700194 struct work_struct ref_work;
195 struct completion done;
196};
197
Jens Axboe5a2e7452020-02-23 16:23:11 -0700198struct io_buffer {
199 struct list_head list;
200 __u64 addr;
201 __s32 len;
202 __u16 bid;
203};
204
Jens Axboe2b188cc2019-01-07 10:46:33 -0700205struct io_ring_ctx {
206 struct {
207 struct percpu_ref refs;
208 } ____cacheline_aligned_in_smp;
209
210 struct {
211 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800212 unsigned int compat: 1;
213 unsigned int account_mem: 1;
214 unsigned int cq_overflow_flushed: 1;
215 unsigned int drain_next: 1;
216 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217
Hristo Venev75b28af2019-08-26 17:23:46 +0000218 /*
219 * Ring buffer of indices into array of io_uring_sqe, which is
220 * mmapped by the application using the IORING_OFF_SQES offset.
221 *
222 * This indirection could e.g. be used to assign fixed
223 * io_uring_sqe entries to operations and only submit them to
224 * the queue when needed.
225 *
226 * The kernel modifies neither the indices array nor the entries
227 * array.
228 */
229 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700230 unsigned cached_sq_head;
231 unsigned sq_entries;
232 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700233 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600234 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700235 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700236 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600237
238 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600239 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700240 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241
Jens Axboefcb323c2019-10-24 12:39:47 -0600242 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700243 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244 } ____cacheline_aligned_in_smp;
245
Hristo Venev75b28af2019-08-26 17:23:46 +0000246 struct io_rings *rings;
247
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600249 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700250 struct task_struct *sqo_thread; /* if using sq thread polling */
251 struct mm_struct *sqo_mm;
252 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700253
Jens Axboe6b063142019-01-10 22:13:58 -0700254 /*
255 * If used, fixed file set. Writers must ensure that ->refs is dead,
256 * readers must ensure that ->refs is alive as long as the file* is
257 * used. Only updated through io_uring_register(2).
258 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700259 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700260 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300261 int ring_fd;
262 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700263
Jens Axboeedafcce2019-01-09 09:16:05 -0700264 /* if used, fixed mapped user buffers */
265 unsigned nr_user_bufs;
266 struct io_mapped_ubuf *user_bufs;
267
Jens Axboe2b188cc2019-01-07 10:46:33 -0700268 struct user_struct *user;
269
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700270 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700271
Jens Axboe206aefd2019-11-07 18:27:42 -0700272 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
273 struct completion *completions;
274
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700275 /* if all else fails... */
276 struct io_kiocb *fallback_req;
277
Jens Axboe206aefd2019-11-07 18:27:42 -0700278#if defined(CONFIG_UNIX)
279 struct socket *ring_sock;
280#endif
281
Jens Axboe5a2e7452020-02-23 16:23:11 -0700282 struct idr io_buffer_idr;
283
Jens Axboe071698e2020-01-28 10:04:42 -0700284 struct idr personality_idr;
285
Jens Axboe206aefd2019-11-07 18:27:42 -0700286 struct {
287 unsigned cached_cq_tail;
288 unsigned cq_entries;
289 unsigned cq_mask;
290 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700291 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700292 struct wait_queue_head cq_wait;
293 struct fasync_struct *cq_fasync;
294 struct eventfd_ctx *cq_ev_fd;
295 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700296
297 struct {
298 struct mutex uring_lock;
299 wait_queue_head_t wait;
300 } ____cacheline_aligned_in_smp;
301
302 struct {
303 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700304
Jens Axboedef596e2019-01-09 08:59:42 -0700305 /*
306 * ->poll_list is protected by the ctx->uring_lock for
307 * io_uring instances that don't use IORING_SETUP_SQPOLL.
308 * For SQPOLL, only the single threaded io_sq_thread() will
309 * manipulate the list, hence no extra locking is needed there.
310 */
311 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700312 struct hlist_head *cancel_hash;
313 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700314 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600315
316 spinlock_t inflight_lock;
317 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700318 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700319};
320
Jens Axboe09bb8392019-03-13 12:39:28 -0600321/*
322 * First field must be the file pointer in all the
323 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
324 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700325struct io_poll_iocb {
326 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700327 union {
328 struct wait_queue_head *head;
329 u64 addr;
330 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700331 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600332 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700333 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700334 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700335};
336
Jens Axboeb5dba592019-12-11 14:02:38 -0700337struct io_close {
338 struct file *file;
339 struct file *put_file;
340 int fd;
341};
342
Jens Axboead8a48a2019-11-15 08:49:11 -0700343struct io_timeout_data {
344 struct io_kiocb *req;
345 struct hrtimer timer;
346 struct timespec64 ts;
347 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300348 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700349};
350
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700351struct io_accept {
352 struct file *file;
353 struct sockaddr __user *addr;
354 int __user *addr_len;
355 int flags;
356};
357
358struct io_sync {
359 struct file *file;
360 loff_t len;
361 loff_t off;
362 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700363 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700364};
365
Jens Axboefbf23842019-12-17 18:45:56 -0700366struct io_cancel {
367 struct file *file;
368 u64 addr;
369};
370
Jens Axboeb29472e2019-12-17 18:50:29 -0700371struct io_timeout {
372 struct file *file;
373 u64 addr;
374 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700375 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700376};
377
Jens Axboe9adbd452019-12-20 08:45:55 -0700378struct io_rw {
379 /* NOTE: kiocb has the file as the first member, so don't do it here */
380 struct kiocb kiocb;
381 u64 addr;
382 u64 len;
383};
384
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700385struct io_connect {
386 struct file *file;
387 struct sockaddr __user *addr;
388 int addr_len;
389};
390
Jens Axboee47293f2019-12-20 08:58:21 -0700391struct io_sr_msg {
392 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700393 union {
394 struct user_msghdr __user *msg;
395 void __user *buf;
396 };
Jens Axboee47293f2019-12-20 08:58:21 -0700397 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700398 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700399};
400
Jens Axboe15b71ab2019-12-11 11:20:36 -0700401struct io_open {
402 struct file *file;
403 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700404 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700405 unsigned mask;
406 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700407 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700408 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700409 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700410};
411
Jens Axboe05f3fb32019-12-09 11:22:50 -0700412struct io_files_update {
413 struct file *file;
414 u64 arg;
415 u32 nr_args;
416 u32 offset;
417};
418
Jens Axboe4840e412019-12-25 22:03:45 -0700419struct io_fadvise {
420 struct file *file;
421 u64 offset;
422 u32 len;
423 u32 advice;
424};
425
Jens Axboec1ca7572019-12-25 22:18:28 -0700426struct io_madvise {
427 struct file *file;
428 u64 addr;
429 u32 len;
430 u32 advice;
431};
432
Jens Axboe3e4827b2020-01-08 15:18:09 -0700433struct io_epoll {
434 struct file *file;
435 int epfd;
436 int op;
437 int fd;
438 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700439};
440
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300441struct io_splice {
442 struct file *file_out;
443 struct file *file_in;
444 loff_t off_out;
445 loff_t off_in;
446 u64 len;
447 unsigned int flags;
448};
449
Jens Axboef499a022019-12-02 16:28:46 -0700450struct io_async_connect {
451 struct sockaddr_storage address;
452};
453
Jens Axboe03b12302019-12-02 18:50:25 -0700454struct io_async_msghdr {
455 struct iovec fast_iov[UIO_FASTIOV];
456 struct iovec *iov;
457 struct sockaddr __user *uaddr;
458 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700459 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700460};
461
Jens Axboef67676d2019-12-02 11:03:47 -0700462struct io_async_rw {
463 struct iovec fast_iov[UIO_FASTIOV];
464 struct iovec *iov;
465 ssize_t nr_segs;
466 ssize_t size;
467};
468
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700469struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700470 union {
471 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700472 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700473 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700474 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700475 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700476};
477
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300478enum {
479 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
480 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
481 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
482 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
483 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
484
485 REQ_F_LINK_NEXT_BIT,
486 REQ_F_FAIL_LINK_BIT,
487 REQ_F_INFLIGHT_BIT,
488 REQ_F_CUR_POS_BIT,
489 REQ_F_NOWAIT_BIT,
490 REQ_F_IOPOLL_COMPLETED_BIT,
491 REQ_F_LINK_TIMEOUT_BIT,
492 REQ_F_TIMEOUT_BIT,
493 REQ_F_ISREG_BIT,
494 REQ_F_MUST_PUNT_BIT,
495 REQ_F_TIMEOUT_NOSEQ_BIT,
496 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300497 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700498 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700499 REQ_F_POLLED_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300500};
501
502enum {
503 /* ctx owns file */
504 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
505 /* drain existing IO first */
506 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
507 /* linked sqes */
508 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
509 /* doesn't sever on completion < 0 */
510 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
511 /* IOSQE_ASYNC */
512 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
513
514 /* already grabbed next link */
515 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
516 /* fail rest of links */
517 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
518 /* on inflight list */
519 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
520 /* read/write uses file position */
521 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
522 /* must not punt to workers */
523 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
524 /* polled IO has completed */
525 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
526 /* has linked timeout */
527 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
528 /* timeout request */
529 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
530 /* regular file */
531 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
532 /* must be punted even for NONBLOCK */
533 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
534 /* no timeout sequence */
535 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
536 /* completion under lock */
537 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300538 /* needs cleanup */
539 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700540 /* in overflow list */
541 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700542 /* already went through poll handler */
543 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
544};
545
546struct async_poll {
547 struct io_poll_iocb poll;
548 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300549};
550
Jens Axboe09bb8392019-03-13 12:39:28 -0600551/*
552 * NOTE! Each of the iocb union members has the file pointer
553 * as the first entry in their struct definition. So you can
554 * access the file pointer through any of the sub-structs,
555 * or directly as just 'ki_filp' in this struct.
556 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700557struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700558 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600559 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700560 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700561 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700562 struct io_accept accept;
563 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700564 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700565 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700566 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700567 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700568 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700569 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700570 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700571 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700572 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700573 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300574 struct io_splice splice;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700575 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700576
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700577 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300578 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700579 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700580
581 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700582 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700583 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700584 refcount_t refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700585 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700586 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600587 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600588 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700589
Jens Axboed7718a92020-02-14 22:23:12 -0700590 struct list_head link_list;
591
Jens Axboefcb323c2019-10-24 12:39:47 -0600592 struct list_head inflight_entry;
593
Jens Axboeb41e9852020-02-17 09:52:41 -0700594 union {
595 /*
596 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700597 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
598 * async armed poll handlers for regular commands. The latter
599 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700600 */
601 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700602 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700603 struct hlist_node hash_node;
604 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700605 };
606 struct io_wq_work work;
607 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608};
609
610#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700611#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700612
Jens Axboe9a56a232019-01-09 09:06:50 -0700613struct io_submit_state {
614 struct blk_plug plug;
615
616 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700617 * io_kiocb alloc cache
618 */
619 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300620 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700621
622 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700623 * File reference cache
624 */
625 struct file *file;
626 unsigned int fd;
627 unsigned int has_refs;
628 unsigned int used_refs;
629 unsigned int ios_left;
630};
631
Jens Axboed3656342019-12-18 09:50:26 -0700632struct io_op_def {
633 /* needs req->io allocated for deferral/async */
634 unsigned async_ctx : 1;
635 /* needs current->mm setup, does mm access */
636 unsigned needs_mm : 1;
637 /* needs req->file assigned */
638 unsigned needs_file : 1;
639 /* needs req->file assigned IFF fd is >= 0 */
640 unsigned fd_non_neg : 1;
641 /* hash wq insertion if file is a regular file */
642 unsigned hash_reg_file : 1;
643 /* unbound wq insertion if file is a non-regular file */
644 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700645 /* opcode is not supported by this kernel */
646 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700647 /* needs file table */
648 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700649 /* needs ->fs */
650 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700651 /* set if opcode supports polled "wait" */
652 unsigned pollin : 1;
653 unsigned pollout : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700654};
655
656static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300657 [IORING_OP_NOP] = {},
658 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700659 .async_ctx = 1,
660 .needs_mm = 1,
661 .needs_file = 1,
662 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700663 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700664 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300665 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700666 .async_ctx = 1,
667 .needs_mm = 1,
668 .needs_file = 1,
669 .hash_reg_file = 1,
670 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700671 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700672 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300673 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700674 .needs_file = 1,
675 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300676 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700677 .needs_file = 1,
678 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700679 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700680 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300681 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700682 .needs_file = 1,
683 .hash_reg_file = 1,
684 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700685 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700686 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300687 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700688 .needs_file = 1,
689 .unbound_nonreg_file = 1,
690 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300691 [IORING_OP_POLL_REMOVE] = {},
692 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700693 .needs_file = 1,
694 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300695 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700696 .async_ctx = 1,
697 .needs_mm = 1,
698 .needs_file = 1,
699 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700700 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700701 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700702 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300703 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700704 .async_ctx = 1,
705 .needs_mm = 1,
706 .needs_file = 1,
707 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700708 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700709 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700710 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300711 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700712 .async_ctx = 1,
713 .needs_mm = 1,
714 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300715 [IORING_OP_TIMEOUT_REMOVE] = {},
716 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700717 .needs_mm = 1,
718 .needs_file = 1,
719 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700720 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700721 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700722 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300723 [IORING_OP_ASYNC_CANCEL] = {},
724 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700725 .async_ctx = 1,
726 .needs_mm = 1,
727 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300728 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700729 .async_ctx = 1,
730 .needs_mm = 1,
731 .needs_file = 1,
732 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700733 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700736 .needs_file = 1,
737 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700739 .needs_file = 1,
740 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700741 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700742 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700743 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300744 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700745 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700746 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700747 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300748 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700749 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700750 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700751 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300752 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700753 .needs_mm = 1,
754 .needs_file = 1,
755 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700756 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700757 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300758 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700759 .needs_mm = 1,
760 .needs_file = 1,
761 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700762 .pollin = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700763 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300764 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700765 .needs_mm = 1,
766 .needs_file = 1,
767 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700768 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700769 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300770 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700771 .needs_file = 1,
772 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300773 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700774 .needs_mm = 1,
775 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300776 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700777 .needs_mm = 1,
778 .needs_file = 1,
779 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700780 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700781 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300782 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700783 .needs_mm = 1,
784 .needs_file = 1,
785 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700786 .pollin = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700787 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300788 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700789 .needs_file = 1,
790 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700791 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700792 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700793 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700794 [IORING_OP_EPOLL_CTL] = {
795 .unbound_nonreg_file = 1,
796 .file_table = 1,
797 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300798 [IORING_OP_SPLICE] = {
799 .needs_file = 1,
800 .hash_reg_file = 1,
801 .unbound_nonreg_file = 1,
802 }
Jens Axboed3656342019-12-18 09:50:26 -0700803};
804
Jens Axboe561fb042019-10-24 07:25:42 -0600805static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700806static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800807static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700808static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700809static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
810static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700811static int __io_sqe_files_update(struct io_ring_ctx *ctx,
812 struct io_uring_files_update *ip,
813 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700814static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700815static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300816static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700817static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
818 int fd, struct file **out_file, bool fixed);
819static void __io_queue_sqe(struct io_kiocb *req,
820 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600821
Jens Axboe2b188cc2019-01-07 10:46:33 -0700822static struct kmem_cache *req_cachep;
823
824static const struct file_operations io_uring_fops;
825
826struct sock *io_uring_get_socket(struct file *file)
827{
828#if defined(CONFIG_UNIX)
829 if (file->f_op == &io_uring_fops) {
830 struct io_ring_ctx *ctx = file->private_data;
831
832 return ctx->ring_sock->sk;
833 }
834#endif
835 return NULL;
836}
837EXPORT_SYMBOL(io_uring_get_socket);
838
839static void io_ring_ctx_ref_free(struct percpu_ref *ref)
840{
841 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
842
Jens Axboe206aefd2019-11-07 18:27:42 -0700843 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700844}
845
846static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
847{
848 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700849 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700850
851 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
852 if (!ctx)
853 return NULL;
854
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700855 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
856 if (!ctx->fallback_req)
857 goto err;
858
Jens Axboe206aefd2019-11-07 18:27:42 -0700859 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
860 if (!ctx->completions)
861 goto err;
862
Jens Axboe78076bb2019-12-04 19:56:40 -0700863 /*
864 * Use 5 bits less than the max cq entries, that should give us around
865 * 32 entries per hash list if totally full and uniformly spread.
866 */
867 hash_bits = ilog2(p->cq_entries);
868 hash_bits -= 5;
869 if (hash_bits <= 0)
870 hash_bits = 1;
871 ctx->cancel_hash_bits = hash_bits;
872 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
873 GFP_KERNEL);
874 if (!ctx->cancel_hash)
875 goto err;
876 __hash_init(ctx->cancel_hash, 1U << hash_bits);
877
Roman Gushchin21482892019-05-07 10:01:48 -0700878 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700879 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
880 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700881
882 ctx->flags = p->flags;
883 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700884 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700885 init_completion(&ctx->completions[0]);
886 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700887 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700888 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700889 mutex_init(&ctx->uring_lock);
890 init_waitqueue_head(&ctx->wait);
891 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700892 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600893 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600894 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600895 init_waitqueue_head(&ctx->inflight_wait);
896 spin_lock_init(&ctx->inflight_lock);
897 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700898 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700899err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700900 if (ctx->fallback_req)
901 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700902 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700903 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700904 kfree(ctx);
905 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700906}
907
Bob Liu9d858b22019-11-13 18:06:25 +0800908static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600909{
Jackie Liua197f662019-11-08 08:09:12 -0700910 struct io_ring_ctx *ctx = req->ctx;
911
Jens Axboe498ccd92019-10-25 10:04:25 -0600912 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
913 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600914}
915
Bob Liu9d858b22019-11-13 18:06:25 +0800916static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600917{
Pavel Begunkov87987892020-01-18 01:22:30 +0300918 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800919 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600920
Bob Liu9d858b22019-11-13 18:06:25 +0800921 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600922}
923
924static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600925{
926 struct io_kiocb *req;
927
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600928 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800929 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600930 list_del_init(&req->list);
931 return req;
932 }
933
934 return NULL;
935}
936
Jens Axboe5262f562019-09-17 12:26:57 -0600937static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
938{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600939 struct io_kiocb *req;
940
941 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700942 if (req) {
943 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
944 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800945 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700946 list_del_init(&req->list);
947 return req;
948 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600949 }
950
951 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600952}
953
Jens Axboede0617e2019-04-06 21:51:27 -0600954static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700955{
Hristo Venev75b28af2019-08-26 17:23:46 +0000956 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700957
Pavel Begunkov07910152020-01-17 03:52:46 +0300958 /* order cqe stores with ring update */
959 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700960
Pavel Begunkov07910152020-01-17 03:52:46 +0300961 if (wq_has_sleeper(&ctx->cq_wait)) {
962 wake_up_interruptible(&ctx->cq_wait);
963 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700964 }
965}
966
Jens Axboecccf0ee2020-01-27 16:34:48 -0700967static inline void io_req_work_grab_env(struct io_kiocb *req,
968 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600969{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700970 if (!req->work.mm && def->needs_mm) {
971 mmgrab(current->mm);
972 req->work.mm = current->mm;
973 }
974 if (!req->work.creds)
975 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700976 if (!req->work.fs && def->needs_fs) {
977 spin_lock(&current->fs->lock);
978 if (!current->fs->in_exec) {
979 req->work.fs = current->fs;
980 req->work.fs->users++;
981 } else {
982 req->work.flags |= IO_WQ_WORK_CANCEL;
983 }
984 spin_unlock(&current->fs->lock);
985 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700986 if (!req->work.task_pid)
987 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700988}
989
990static inline void io_req_work_drop_env(struct io_kiocb *req)
991{
992 if (req->work.mm) {
993 mmdrop(req->work.mm);
994 req->work.mm = NULL;
995 }
996 if (req->work.creds) {
997 put_cred(req->work.creds);
998 req->work.creds = NULL;
999 }
Jens Axboeff002b32020-02-07 16:05:21 -07001000 if (req->work.fs) {
1001 struct fs_struct *fs = req->work.fs;
1002
1003 spin_lock(&req->work.fs->lock);
1004 if (--fs->users)
1005 fs = NULL;
1006 spin_unlock(&req->work.fs->lock);
1007 if (fs)
1008 free_fs_struct(fs);
1009 }
Jens Axboe561fb042019-10-24 07:25:42 -06001010}
1011
Jens Axboe94ae5e72019-11-14 19:39:52 -07001012static inline bool io_prep_async_work(struct io_kiocb *req,
1013 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001014{
Jens Axboed3656342019-12-18 09:50:26 -07001015 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -06001016 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -06001017
Jens Axboed3656342019-12-18 09:50:26 -07001018 if (req->flags & REQ_F_ISREG) {
1019 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001020 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -07001021 } else {
1022 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001023 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001024 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001025
1026 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001027
Jens Axboe94ae5e72019-11-14 19:39:52 -07001028 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001029 return do_hashed;
1030}
1031
Jackie Liua197f662019-11-08 08:09:12 -07001032static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001033{
Jackie Liua197f662019-11-08 08:09:12 -07001034 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001035 struct io_kiocb *link;
1036 bool do_hashed;
1037
1038 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001039
1040 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1041 req->flags);
1042 if (!do_hashed) {
1043 io_wq_enqueue(ctx->io_wq, &req->work);
1044 } else {
1045 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1046 file_inode(req->file));
1047 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001048
1049 if (link)
1050 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001051}
1052
Jens Axboe5262f562019-09-17 12:26:57 -06001053static void io_kill_timeout(struct io_kiocb *req)
1054{
1055 int ret;
1056
Jens Axboe2d283902019-12-04 11:08:05 -07001057 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001058 if (ret != -1) {
1059 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001060 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001061 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001062 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001063 }
1064}
1065
1066static void io_kill_timeouts(struct io_ring_ctx *ctx)
1067{
1068 struct io_kiocb *req, *tmp;
1069
1070 spin_lock_irq(&ctx->completion_lock);
1071 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1072 io_kill_timeout(req);
1073 spin_unlock_irq(&ctx->completion_lock);
1074}
1075
Jens Axboede0617e2019-04-06 21:51:27 -06001076static void io_commit_cqring(struct io_ring_ctx *ctx)
1077{
1078 struct io_kiocb *req;
1079
Jens Axboe5262f562019-09-17 12:26:57 -06001080 while ((req = io_get_timeout_req(ctx)) != NULL)
1081 io_kill_timeout(req);
1082
Jens Axboede0617e2019-04-06 21:51:27 -06001083 __io_commit_cqring(ctx);
1084
Pavel Begunkov87987892020-01-18 01:22:30 +03001085 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001086 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001087}
1088
Jens Axboe2b188cc2019-01-07 10:46:33 -07001089static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1090{
Hristo Venev75b28af2019-08-26 17:23:46 +00001091 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001092 unsigned tail;
1093
1094 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001095 /*
1096 * writes to the cq entry need to come after reading head; the
1097 * control dependency is enough as we're using WRITE_ONCE to
1098 * fill the cq entry
1099 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001100 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101 return NULL;
1102
1103 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001104 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105}
1106
Jens Axboef2842ab2020-01-08 11:04:00 -07001107static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1108{
Jens Axboef0b493e2020-02-01 21:30:11 -07001109 if (!ctx->cq_ev_fd)
1110 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001111 if (!ctx->eventfd_async)
1112 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001113 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001114}
1115
Jens Axboeb41e9852020-02-17 09:52:41 -07001116static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001117{
1118 if (waitqueue_active(&ctx->wait))
1119 wake_up(&ctx->wait);
1120 if (waitqueue_active(&ctx->sqo_wait))
1121 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001122 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001123 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001124}
1125
Jens Axboec4a2ed72019-11-21 21:01:26 -07001126/* Returns true if there are no backlogged entries after the flush */
1127static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001128{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001129 struct io_rings *rings = ctx->rings;
1130 struct io_uring_cqe *cqe;
1131 struct io_kiocb *req;
1132 unsigned long flags;
1133 LIST_HEAD(list);
1134
1135 if (!force) {
1136 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001137 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001138 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1139 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001140 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001141 }
1142
1143 spin_lock_irqsave(&ctx->completion_lock, flags);
1144
1145 /* if force is set, the ring is going away. always drop after that */
1146 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001147 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001148
Jens Axboec4a2ed72019-11-21 21:01:26 -07001149 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001150 while (!list_empty(&ctx->cq_overflow_list)) {
1151 cqe = io_get_cqring(ctx);
1152 if (!cqe && !force)
1153 break;
1154
1155 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1156 list);
1157 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001158 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001159 if (cqe) {
1160 WRITE_ONCE(cqe->user_data, req->user_data);
1161 WRITE_ONCE(cqe->res, req->result);
1162 WRITE_ONCE(cqe->flags, 0);
1163 } else {
1164 WRITE_ONCE(ctx->rings->cq_overflow,
1165 atomic_inc_return(&ctx->cached_cq_overflow));
1166 }
1167 }
1168
1169 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001170 if (cqe) {
1171 clear_bit(0, &ctx->sq_check_overflow);
1172 clear_bit(0, &ctx->cq_check_overflow);
1173 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001174 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1175 io_cqring_ev_posted(ctx);
1176
1177 while (!list_empty(&list)) {
1178 req = list_first_entry(&list, struct io_kiocb, list);
1179 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001180 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001181 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001182
1183 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001184}
1185
Jens Axboe78e19bb2019-11-06 15:21:34 -07001186static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001187{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001188 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001189 struct io_uring_cqe *cqe;
1190
Jens Axboe78e19bb2019-11-06 15:21:34 -07001191 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001192
Jens Axboe2b188cc2019-01-07 10:46:33 -07001193 /*
1194 * If we can't get a cq entry, userspace overflowed the
1195 * submission (by quite a lot). Increment the overflow count in
1196 * the ring.
1197 */
1198 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001199 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001200 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001201 WRITE_ONCE(cqe->res, res);
1202 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001203 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001204 WRITE_ONCE(ctx->rings->cq_overflow,
1205 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001206 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001207 if (list_empty(&ctx->cq_overflow_list)) {
1208 set_bit(0, &ctx->sq_check_overflow);
1209 set_bit(0, &ctx->cq_check_overflow);
1210 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001211 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001212 refcount_inc(&req->refs);
1213 req->result = res;
1214 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215 }
1216}
1217
Jens Axboe78e19bb2019-11-06 15:21:34 -07001218static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001219{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001220 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001221 unsigned long flags;
1222
1223 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001224 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001225 io_commit_cqring(ctx);
1226 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1227
Jens Axboe8c838782019-03-12 15:48:16 -06001228 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001229}
1230
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001231static inline bool io_is_fallback_req(struct io_kiocb *req)
1232{
1233 return req == (struct io_kiocb *)
1234 ((unsigned long) req->ctx->fallback_req & ~1UL);
1235}
1236
1237static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1238{
1239 struct io_kiocb *req;
1240
1241 req = ctx->fallback_req;
1242 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1243 return req;
1244
1245 return NULL;
1246}
1247
Jens Axboe2579f912019-01-09 09:10:43 -07001248static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1249 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250{
Jens Axboefd6fab22019-03-14 16:30:06 -06001251 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001252 struct io_kiocb *req;
1253
Jens Axboe2579f912019-01-09 09:10:43 -07001254 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001255 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001256 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001257 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001258 } else if (!state->free_reqs) {
1259 size_t sz;
1260 int ret;
1261
1262 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001263 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1264
1265 /*
1266 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1267 * retry single alloc to be on the safe side.
1268 */
1269 if (unlikely(ret <= 0)) {
1270 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1271 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001272 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001273 ret = 1;
1274 }
Jens Axboe2579f912019-01-09 09:10:43 -07001275 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001276 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001277 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001278 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001279 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001280 }
1281
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001282got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001283 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001284 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001285 req->ctx = ctx;
1286 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001287 /* one is dropped after submission, the other at completion */
1288 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001289 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001290 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001291 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001292fallback:
1293 req = io_get_fallback_req(ctx);
1294 if (req)
1295 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001296 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001297 return NULL;
1298}
1299
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001300static inline void io_put_file(struct io_kiocb *req, struct file *file,
1301 bool fixed)
1302{
1303 if (fixed)
1304 percpu_ref_put(&req->ctx->file_data->refs);
1305 else
1306 fput(file);
1307}
1308
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001309static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001310{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001311 if (likely(!io_is_fallback_req(req)))
1312 kmem_cache_free(req_cachep, req);
1313 else
1314 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1315}
1316
Jens Axboec6ca97b302019-12-28 12:11:08 -07001317static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001319 if (req->flags & REQ_F_NEED_CLEANUP)
1320 io_cleanup_req(req);
1321
YueHaibing96fd84d2020-01-07 22:22:44 +08001322 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001323 if (req->file)
1324 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001325
1326 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001327}
1328
1329static void __io_free_req(struct io_kiocb *req)
1330{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001331 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001332
Jens Axboefcb323c2019-10-24 12:39:47 -06001333 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001334 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001335 unsigned long flags;
1336
1337 spin_lock_irqsave(&ctx->inflight_lock, flags);
1338 list_del(&req->inflight_entry);
1339 if (waitqueue_active(&ctx->inflight_wait))
1340 wake_up(&ctx->inflight_wait);
1341 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1342 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001343
1344 percpu_ref_put(&req->ctx->refs);
1345 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001346}
1347
Jens Axboec6ca97b302019-12-28 12:11:08 -07001348struct req_batch {
1349 void *reqs[IO_IOPOLL_BATCH];
1350 int to_free;
1351 int need_iter;
1352};
1353
1354static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1355{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001356 int fixed_refs = rb->to_free;
1357
Jens Axboec6ca97b302019-12-28 12:11:08 -07001358 if (!rb->to_free)
1359 return;
1360 if (rb->need_iter) {
1361 int i, inflight = 0;
1362 unsigned long flags;
1363
Jens Axboe10fef4b2020-01-09 07:52:28 -07001364 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001365 for (i = 0; i < rb->to_free; i++) {
1366 struct io_kiocb *req = rb->reqs[i];
1367
Jens Axboe10fef4b2020-01-09 07:52:28 -07001368 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001369 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001370 fixed_refs++;
1371 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001372 if (req->flags & REQ_F_INFLIGHT)
1373 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001374 __io_req_aux_free(req);
1375 }
1376 if (!inflight)
1377 goto do_free;
1378
1379 spin_lock_irqsave(&ctx->inflight_lock, flags);
1380 for (i = 0; i < rb->to_free; i++) {
1381 struct io_kiocb *req = rb->reqs[i];
1382
Jens Axboe10fef4b2020-01-09 07:52:28 -07001383 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001384 list_del(&req->inflight_entry);
1385 if (!--inflight)
1386 break;
1387 }
1388 }
1389 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1390
1391 if (waitqueue_active(&ctx->inflight_wait))
1392 wake_up(&ctx->inflight_wait);
1393 }
1394do_free:
1395 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001396 if (fixed_refs)
1397 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001398 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001399 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001400}
1401
Jackie Liua197f662019-11-08 08:09:12 -07001402static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001403{
Jackie Liua197f662019-11-08 08:09:12 -07001404 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001405 int ret;
1406
Jens Axboe2d283902019-12-04 11:08:05 -07001407 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001408 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001409 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001410 io_commit_cqring(ctx);
1411 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001412 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001413 return true;
1414 }
1415
1416 return false;
1417}
1418
Jens Axboeba816ad2019-09-28 11:36:45 -06001419static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001420{
Jens Axboe2665abf2019-11-05 12:40:47 -07001421 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001422 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001423
Jens Axboe4d7dd462019-11-20 13:03:52 -07001424 /* Already got next link */
1425 if (req->flags & REQ_F_LINK_NEXT)
1426 return;
1427
Jens Axboe9e645e112019-05-10 16:07:28 -06001428 /*
1429 * The list should never be empty when we are called here. But could
1430 * potentially happen if the chain is messed up, check to be on the
1431 * safe side.
1432 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001433 while (!list_empty(&req->link_list)) {
1434 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1435 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001436
Pavel Begunkov44932332019-12-05 16:16:35 +03001437 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1438 (nxt->flags & REQ_F_TIMEOUT))) {
1439 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001440 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001441 req->flags &= ~REQ_F_LINK_TIMEOUT;
1442 continue;
1443 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001444
Pavel Begunkov44932332019-12-05 16:16:35 +03001445 list_del_init(&req->link_list);
1446 if (!list_empty(&nxt->link_list))
1447 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001448 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001449 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001450 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001451
Jens Axboe4d7dd462019-11-20 13:03:52 -07001452 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001453 if (wake_ev)
1454 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001455}
1456
1457/*
1458 * Called if REQ_F_LINK is set, and we fail the head request
1459 */
1460static void io_fail_links(struct io_kiocb *req)
1461{
Jens Axboe2665abf2019-11-05 12:40:47 -07001462 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001463 unsigned long flags;
1464
1465 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001466
1467 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001468 struct io_kiocb *link = list_first_entry(&req->link_list,
1469 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001470
Pavel Begunkov44932332019-12-05 16:16:35 +03001471 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001472 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001473
1474 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001475 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001476 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001477 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001478 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001479 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001480 }
Jens Axboe5d960722019-11-19 15:31:28 -07001481 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001482 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001483
1484 io_commit_cqring(ctx);
1485 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1486 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001487}
1488
Jens Axboe4d7dd462019-11-20 13:03:52 -07001489static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001490{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001491 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001492 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001493
Jens Axboe9e645e112019-05-10 16:07:28 -06001494 /*
1495 * If LINK is set, we have dependent requests in this chain. If we
1496 * didn't fail this request, queue the first one up, moving any other
1497 * dependencies to the next request. In case of failure, fail the rest
1498 * of the chain.
1499 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001500 if (req->flags & REQ_F_FAIL_LINK) {
1501 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001502 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1503 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001504 struct io_ring_ctx *ctx = req->ctx;
1505 unsigned long flags;
1506
1507 /*
1508 * If this is a timeout link, we could be racing with the
1509 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001510 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001511 */
1512 spin_lock_irqsave(&ctx->completion_lock, flags);
1513 io_req_link_next(req, nxt);
1514 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1515 } else {
1516 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001517 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001518}
Jens Axboe9e645e112019-05-10 16:07:28 -06001519
Jackie Liuc69f8db2019-11-09 11:00:08 +08001520static void io_free_req(struct io_kiocb *req)
1521{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001522 struct io_kiocb *nxt = NULL;
1523
1524 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001525 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001526
1527 if (nxt)
1528 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001529}
1530
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001531static void io_link_work_cb(struct io_wq_work **workptr)
1532{
1533 struct io_wq_work *work = *workptr;
1534 struct io_kiocb *link = work->data;
1535
1536 io_queue_linked_timeout(link);
1537 io_wq_submit_work(workptr);
1538}
1539
1540static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1541{
1542 struct io_kiocb *link;
1543
1544 *workptr = &nxt->work;
1545 link = io_prep_linked_timeout(nxt);
1546 if (link) {
1547 nxt->work.func = io_link_work_cb;
1548 nxt->work.data = link;
1549 }
1550}
1551
Jens Axboeba816ad2019-09-28 11:36:45 -06001552/*
1553 * Drop reference to request, return next in chain (if there is one) if this
1554 * was the last reference to this request.
1555 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001556__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001557static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001558{
Jens Axboe2a44f462020-02-25 13:25:41 -07001559 if (refcount_dec_and_test(&req->refs)) {
1560 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001561 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001562 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001563}
1564
Jens Axboe2b188cc2019-01-07 10:46:33 -07001565static void io_put_req(struct io_kiocb *req)
1566{
Jens Axboedef596e2019-01-09 08:59:42 -07001567 if (refcount_dec_and_test(&req->refs))
1568 io_free_req(req);
1569}
1570
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001571static void io_steal_work(struct io_kiocb *req,
1572 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001573{
1574 /*
1575 * It's in an io-wq worker, so there always should be at least
1576 * one reference, which will be dropped in io_put_work() just
1577 * after the current handler returns.
1578 *
1579 * It also means, that if the counter dropped to 1, then there is
1580 * no asynchronous users left, so it's safe to steal the next work.
1581 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001582 if (refcount_read(&req->refs) == 1) {
1583 struct io_kiocb *nxt = NULL;
1584
1585 io_req_find_next(req, &nxt);
1586 if (nxt)
1587 io_wq_assign_next(workptr, nxt);
1588 }
1589}
1590
Jens Axboe978db572019-11-14 22:39:04 -07001591/*
1592 * Must only be used if we don't need to care about links, usually from
1593 * within the completion handling itself.
1594 */
1595static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001596{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001597 /* drop both submit and complete references */
1598 if (refcount_sub_and_test(2, &req->refs))
1599 __io_free_req(req);
1600}
1601
Jens Axboe978db572019-11-14 22:39:04 -07001602static void io_double_put_req(struct io_kiocb *req)
1603{
1604 /* drop both submit and complete references */
1605 if (refcount_sub_and_test(2, &req->refs))
1606 io_free_req(req);
1607}
1608
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001609static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001610{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001611 struct io_rings *rings = ctx->rings;
1612
Jens Axboead3eb2c2019-12-18 17:12:20 -07001613 if (test_bit(0, &ctx->cq_check_overflow)) {
1614 /*
1615 * noflush == true is from the waitqueue handler, just ensure
1616 * we wake up the task, and the next invocation will flush the
1617 * entries. We cannot safely to it from here.
1618 */
1619 if (noflush && !list_empty(&ctx->cq_overflow_list))
1620 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001621
Jens Axboead3eb2c2019-12-18 17:12:20 -07001622 io_cqring_overflow_flush(ctx, false);
1623 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001624
Jens Axboea3a0e432019-08-20 11:03:11 -06001625 /* See comment at the top of this file */
1626 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001627 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001628}
1629
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001630static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1631{
1632 struct io_rings *rings = ctx->rings;
1633
1634 /* make sure SQ entry isn't read before tail */
1635 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1636}
1637
Jens Axboe8237e042019-12-28 10:48:22 -07001638static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001639{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001640 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1641 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001642
Jens Axboec6ca97b302019-12-28 12:11:08 -07001643 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1644 rb->need_iter++;
1645
1646 rb->reqs[rb->to_free++] = req;
1647 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1648 io_free_req_many(req->ctx, rb);
1649 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001650}
1651
Jens Axboedef596e2019-01-09 08:59:42 -07001652/*
1653 * Find and free completed poll iocbs
1654 */
1655static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1656 struct list_head *done)
1657{
Jens Axboe8237e042019-12-28 10:48:22 -07001658 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001659 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001660
Jens Axboec6ca97b302019-12-28 12:11:08 -07001661 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001662 while (!list_empty(done)) {
1663 req = list_first_entry(done, struct io_kiocb, list);
1664 list_del(&req->list);
1665
Jens Axboe78e19bb2019-11-06 15:21:34 -07001666 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001667 (*nr_events)++;
1668
Jens Axboe8237e042019-12-28 10:48:22 -07001669 if (refcount_dec_and_test(&req->refs) &&
1670 !io_req_multi_free(&rb, req))
1671 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001672 }
Jens Axboedef596e2019-01-09 08:59:42 -07001673
Jens Axboe09bb8392019-03-13 12:39:28 -06001674 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001675 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001676}
1677
1678static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1679 long min)
1680{
1681 struct io_kiocb *req, *tmp;
1682 LIST_HEAD(done);
1683 bool spin;
1684 int ret;
1685
1686 /*
1687 * Only spin for completions if we don't have multiple devices hanging
1688 * off our complete list, and we're under the requested amount.
1689 */
1690 spin = !ctx->poll_multi_file && *nr_events < min;
1691
1692 ret = 0;
1693 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001694 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001695
1696 /*
1697 * Move completed entries to our local list. If we find a
1698 * request that requires polling, break out and complete
1699 * the done list first, if we have entries there.
1700 */
1701 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1702 list_move_tail(&req->list, &done);
1703 continue;
1704 }
1705 if (!list_empty(&done))
1706 break;
1707
1708 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1709 if (ret < 0)
1710 break;
1711
1712 if (ret && spin)
1713 spin = false;
1714 ret = 0;
1715 }
1716
1717 if (!list_empty(&done))
1718 io_iopoll_complete(ctx, nr_events, &done);
1719
1720 return ret;
1721}
1722
1723/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001724 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001725 * non-spinning poll check - we'll still enter the driver poll loop, but only
1726 * as a non-spinning completion check.
1727 */
1728static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1729 long min)
1730{
Jens Axboe08f54392019-08-21 22:19:11 -06001731 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001732 int ret;
1733
1734 ret = io_do_iopoll(ctx, nr_events, min);
1735 if (ret < 0)
1736 return ret;
1737 if (!min || *nr_events >= min)
1738 return 0;
1739 }
1740
1741 return 1;
1742}
1743
1744/*
1745 * We can't just wait for polled events to come to us, we have to actively
1746 * find and complete them.
1747 */
1748static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1749{
1750 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1751 return;
1752
1753 mutex_lock(&ctx->uring_lock);
1754 while (!list_empty(&ctx->poll_list)) {
1755 unsigned int nr_events = 0;
1756
1757 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001758
1759 /*
1760 * Ensure we allow local-to-the-cpu processing to take place,
1761 * in this case we need to ensure that we reap all events.
1762 */
1763 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001764 }
1765 mutex_unlock(&ctx->uring_lock);
1766}
1767
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001768static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1769 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001770{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001771 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001772
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001773 /*
1774 * We disallow the app entering submit/complete with polling, but we
1775 * still need to lock the ring to prevent racing with polled issue
1776 * that got punted to a workqueue.
1777 */
1778 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001779 do {
1780 int tmin = 0;
1781
Jens Axboe500f9fb2019-08-19 12:15:59 -06001782 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001783 * Don't enter poll loop if we already have events pending.
1784 * If we do, we can potentially be spinning for commands that
1785 * already triggered a CQE (eg in error).
1786 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001787 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001788 break;
1789
1790 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001791 * If a submit got punted to a workqueue, we can have the
1792 * application entering polling for a command before it gets
1793 * issued. That app will hold the uring_lock for the duration
1794 * of the poll right here, so we need to take a breather every
1795 * now and then to ensure that the issue has a chance to add
1796 * the poll to the issued list. Otherwise we can spin here
1797 * forever, while the workqueue is stuck trying to acquire the
1798 * very same mutex.
1799 */
1800 if (!(++iters & 7)) {
1801 mutex_unlock(&ctx->uring_lock);
1802 mutex_lock(&ctx->uring_lock);
1803 }
1804
Jens Axboedef596e2019-01-09 08:59:42 -07001805 if (*nr_events < min)
1806 tmin = min - *nr_events;
1807
1808 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1809 if (ret <= 0)
1810 break;
1811 ret = 0;
1812 } while (min && !*nr_events && !need_resched());
1813
Jens Axboe500f9fb2019-08-19 12:15:59 -06001814 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001815 return ret;
1816}
1817
Jens Axboe491381ce2019-10-17 09:20:46 -06001818static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001819{
Jens Axboe491381ce2019-10-17 09:20:46 -06001820 /*
1821 * Tell lockdep we inherited freeze protection from submission
1822 * thread.
1823 */
1824 if (req->flags & REQ_F_ISREG) {
1825 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001826
Jens Axboe491381ce2019-10-17 09:20:46 -06001827 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001828 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001829 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001830}
1831
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001832static inline void req_set_fail_links(struct io_kiocb *req)
1833{
1834 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1835 req->flags |= REQ_F_FAIL_LINK;
1836}
1837
Jens Axboeba816ad2019-09-28 11:36:45 -06001838static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001839{
Jens Axboe9adbd452019-12-20 08:45:55 -07001840 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001841
Jens Axboe491381ce2019-10-17 09:20:46 -06001842 if (kiocb->ki_flags & IOCB_WRITE)
1843 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001844
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001845 if (res != req->result)
1846 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001847 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001848}
1849
1850static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1851{
Jens Axboe9adbd452019-12-20 08:45:55 -07001852 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001853
1854 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001855 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001856}
1857
Jens Axboedef596e2019-01-09 08:59:42 -07001858static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1859{
Jens Axboe9adbd452019-12-20 08:45:55 -07001860 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001861
Jens Axboe491381ce2019-10-17 09:20:46 -06001862 if (kiocb->ki_flags & IOCB_WRITE)
1863 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001864
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001865 if (res != req->result)
1866 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001867 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001868 if (res != -EAGAIN)
1869 req->flags |= REQ_F_IOPOLL_COMPLETED;
1870}
1871
1872/*
1873 * After the iocb has been issued, it's safe to be found on the poll list.
1874 * Adding the kiocb to the list AFTER submission ensures that we don't
1875 * find it from a io_iopoll_getevents() thread before the issuer is done
1876 * accessing the kiocb cookie.
1877 */
1878static void io_iopoll_req_issued(struct io_kiocb *req)
1879{
1880 struct io_ring_ctx *ctx = req->ctx;
1881
1882 /*
1883 * Track whether we have multiple files in our lists. This will impact
1884 * how we do polling eventually, not spinning if we're on potentially
1885 * different devices.
1886 */
1887 if (list_empty(&ctx->poll_list)) {
1888 ctx->poll_multi_file = false;
1889 } else if (!ctx->poll_multi_file) {
1890 struct io_kiocb *list_req;
1891
1892 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1893 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001894 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001895 ctx->poll_multi_file = true;
1896 }
1897
1898 /*
1899 * For fast devices, IO may have already completed. If it has, add
1900 * it to the front so we find it first.
1901 */
1902 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1903 list_add(&req->list, &ctx->poll_list);
1904 else
1905 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001906
1907 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1908 wq_has_sleeper(&ctx->sqo_wait))
1909 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001910}
1911
Jens Axboe3d6770f2019-04-13 11:50:54 -06001912static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001913{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001914 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001915 int diff = state->has_refs - state->used_refs;
1916
1917 if (diff)
1918 fput_many(state->file, diff);
1919 state->file = NULL;
1920 }
1921}
1922
1923/*
1924 * Get as many references to a file as we have IOs left in this submission,
1925 * assuming most submissions are for one file, or at least that each file
1926 * has more than one submission.
1927 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001928static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001929{
1930 if (!state)
1931 return fget(fd);
1932
1933 if (state->file) {
1934 if (state->fd == fd) {
1935 state->used_refs++;
1936 state->ios_left--;
1937 return state->file;
1938 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001939 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001940 }
1941 state->file = fget_many(fd, state->ios_left);
1942 if (!state->file)
1943 return NULL;
1944
1945 state->fd = fd;
1946 state->has_refs = state->ios_left;
1947 state->used_refs = 1;
1948 state->ios_left--;
1949 return state->file;
1950}
1951
Jens Axboe2b188cc2019-01-07 10:46:33 -07001952/*
1953 * If we tracked the file through the SCM inflight mechanism, we could support
1954 * any file. For now, just ensure that anything potentially problematic is done
1955 * inline.
1956 */
1957static bool io_file_supports_async(struct file *file)
1958{
1959 umode_t mode = file_inode(file)->i_mode;
1960
Jens Axboe10d59342019-12-09 20:16:22 -07001961 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001962 return true;
1963 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1964 return true;
1965
1966 return false;
1967}
1968
Jens Axboe3529d8c2019-12-19 18:24:38 -07001969static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1970 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001971{
Jens Axboedef596e2019-01-09 08:59:42 -07001972 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001973 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001974 unsigned ioprio;
1975 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001976
Jens Axboe491381ce2019-10-17 09:20:46 -06001977 if (S_ISREG(file_inode(req->file)->i_mode))
1978 req->flags |= REQ_F_ISREG;
1979
Jens Axboe2b188cc2019-01-07 10:46:33 -07001980 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001981 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1982 req->flags |= REQ_F_CUR_POS;
1983 kiocb->ki_pos = req->file->f_pos;
1984 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001985 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001986 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1987 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1988 if (unlikely(ret))
1989 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001990
1991 ioprio = READ_ONCE(sqe->ioprio);
1992 if (ioprio) {
1993 ret = ioprio_check_cap(ioprio);
1994 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001995 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001996
1997 kiocb->ki_ioprio = ioprio;
1998 } else
1999 kiocb->ki_ioprio = get_current_ioprio();
2000
Stefan Bühler8449eed2019-04-27 20:34:19 +02002001 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002002 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2003 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002004 req->flags |= REQ_F_NOWAIT;
2005
2006 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002007 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002008
Jens Axboedef596e2019-01-09 08:59:42 -07002009 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002010 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2011 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002012 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002013
Jens Axboedef596e2019-01-09 08:59:42 -07002014 kiocb->ki_flags |= IOCB_HIPRI;
2015 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002016 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002017 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002018 if (kiocb->ki_flags & IOCB_HIPRI)
2019 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002020 kiocb->ki_complete = io_complete_rw;
2021 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002022
Jens Axboe3529d8c2019-12-19 18:24:38 -07002023 req->rw.addr = READ_ONCE(sqe->addr);
2024 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07002025 /* we own ->private, reuse it for the buffer index */
2026 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002027 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002028 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002029}
2030
2031static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2032{
2033 switch (ret) {
2034 case -EIOCBQUEUED:
2035 break;
2036 case -ERESTARTSYS:
2037 case -ERESTARTNOINTR:
2038 case -ERESTARTNOHAND:
2039 case -ERESTART_RESTARTBLOCK:
2040 /*
2041 * We can't just restart the syscall, since previously
2042 * submitted sqes may already be in progress. Just fail this
2043 * IO with EINTR.
2044 */
2045 ret = -EINTR;
2046 /* fall through */
2047 default:
2048 kiocb->ki_complete(kiocb, ret, 0);
2049 }
2050}
2051
Pavel Begunkov014db002020-03-03 21:33:12 +03002052static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002053{
Jens Axboeba042912019-12-25 16:33:42 -07002054 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2055
2056 if (req->flags & REQ_F_CUR_POS)
2057 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002058 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002059 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002060 else
2061 io_rw_done(kiocb, ret);
2062}
2063
Jens Axboe9adbd452019-12-20 08:45:55 -07002064static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002065 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002066{
Jens Axboe9adbd452019-12-20 08:45:55 -07002067 struct io_ring_ctx *ctx = req->ctx;
2068 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002069 struct io_mapped_ubuf *imu;
2070 unsigned index, buf_index;
2071 size_t offset;
2072 u64 buf_addr;
2073
2074 /* attempt to use fixed buffers without having provided iovecs */
2075 if (unlikely(!ctx->user_bufs))
2076 return -EFAULT;
2077
Jens Axboe9adbd452019-12-20 08:45:55 -07002078 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002079 if (unlikely(buf_index >= ctx->nr_user_bufs))
2080 return -EFAULT;
2081
2082 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2083 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002084 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002085
2086 /* overflow */
2087 if (buf_addr + len < buf_addr)
2088 return -EFAULT;
2089 /* not inside the mapped region */
2090 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2091 return -EFAULT;
2092
2093 /*
2094 * May not be a start of buffer, set size appropriately
2095 * and advance us to the beginning.
2096 */
2097 offset = buf_addr - imu->ubuf;
2098 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002099
2100 if (offset) {
2101 /*
2102 * Don't use iov_iter_advance() here, as it's really slow for
2103 * using the latter parts of a big fixed buffer - it iterates
2104 * over each segment manually. We can cheat a bit here, because
2105 * we know that:
2106 *
2107 * 1) it's a BVEC iter, we set it up
2108 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2109 * first and last bvec
2110 *
2111 * So just find our index, and adjust the iterator afterwards.
2112 * If the offset is within the first bvec (or the whole first
2113 * bvec, just use iov_iter_advance(). This makes it easier
2114 * since we can just skip the first segment, which may not
2115 * be PAGE_SIZE aligned.
2116 */
2117 const struct bio_vec *bvec = imu->bvec;
2118
2119 if (offset <= bvec->bv_len) {
2120 iov_iter_advance(iter, offset);
2121 } else {
2122 unsigned long seg_skip;
2123
2124 /* skip first vec */
2125 offset -= bvec->bv_len;
2126 seg_skip = 1 + (offset >> PAGE_SHIFT);
2127
2128 iter->bvec = bvec + seg_skip;
2129 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002130 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002131 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002132 }
2133 }
2134
Jens Axboe5e559562019-11-13 16:12:46 -07002135 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002136}
2137
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002138static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2139 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002140{
Jens Axboe9adbd452019-12-20 08:45:55 -07002141 void __user *buf = u64_to_user_ptr(req->rw.addr);
2142 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002143 u8 opcode;
2144
Jens Axboed625c6e2019-12-17 19:53:05 -07002145 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002146 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002147 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002148 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002149 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002150
Jens Axboe9adbd452019-12-20 08:45:55 -07002151 /* buffer index only valid with fixed read/write */
2152 if (req->rw.kiocb.private)
2153 return -EINVAL;
2154
Jens Axboe3a6820f2019-12-22 15:19:35 -07002155 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2156 ssize_t ret;
2157 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2158 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002159 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002160 }
2161
Jens Axboef67676d2019-12-02 11:03:47 -07002162 if (req->io) {
2163 struct io_async_rw *iorw = &req->io->rw;
2164
2165 *iovec = iorw->iov;
2166 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2167 if (iorw->iov == iorw->fast_iov)
2168 *iovec = NULL;
2169 return iorw->size;
2170 }
2171
Jens Axboe2b188cc2019-01-07 10:46:33 -07002172#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002173 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002174 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2175 iovec, iter);
2176#endif
2177
2178 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2179}
2180
Jens Axboe32960612019-09-23 11:05:34 -06002181/*
2182 * For files that don't have ->read_iter() and ->write_iter(), handle them
2183 * by looping over ->read() or ->write() manually.
2184 */
2185static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2186 struct iov_iter *iter)
2187{
2188 ssize_t ret = 0;
2189
2190 /*
2191 * Don't support polled IO through this interface, and we can't
2192 * support non-blocking either. For the latter, this just causes
2193 * the kiocb to be handled from an async context.
2194 */
2195 if (kiocb->ki_flags & IOCB_HIPRI)
2196 return -EOPNOTSUPP;
2197 if (kiocb->ki_flags & IOCB_NOWAIT)
2198 return -EAGAIN;
2199
2200 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002201 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002202 ssize_t nr;
2203
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002204 if (!iov_iter_is_bvec(iter)) {
2205 iovec = iov_iter_iovec(iter);
2206 } else {
2207 /* fixed buffers import bvec */
2208 iovec.iov_base = kmap(iter->bvec->bv_page)
2209 + iter->iov_offset;
2210 iovec.iov_len = min(iter->count,
2211 iter->bvec->bv_len - iter->iov_offset);
2212 }
2213
Jens Axboe32960612019-09-23 11:05:34 -06002214 if (rw == READ) {
2215 nr = file->f_op->read(file, iovec.iov_base,
2216 iovec.iov_len, &kiocb->ki_pos);
2217 } else {
2218 nr = file->f_op->write(file, iovec.iov_base,
2219 iovec.iov_len, &kiocb->ki_pos);
2220 }
2221
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002222 if (iov_iter_is_bvec(iter))
2223 kunmap(iter->bvec->bv_page);
2224
Jens Axboe32960612019-09-23 11:05:34 -06002225 if (nr < 0) {
2226 if (!ret)
2227 ret = nr;
2228 break;
2229 }
2230 ret += nr;
2231 if (nr != iovec.iov_len)
2232 break;
2233 iov_iter_advance(iter, nr);
2234 }
2235
2236 return ret;
2237}
2238
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002239static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002240 struct iovec *iovec, struct iovec *fast_iov,
2241 struct iov_iter *iter)
2242{
2243 req->io->rw.nr_segs = iter->nr_segs;
2244 req->io->rw.size = io_size;
2245 req->io->rw.iov = iovec;
2246 if (!req->io->rw.iov) {
2247 req->io->rw.iov = req->io->rw.fast_iov;
2248 memcpy(req->io->rw.iov, fast_iov,
2249 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002250 } else {
2251 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002252 }
2253}
2254
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002255static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002256{
Jens Axboed3656342019-12-18 09:50:26 -07002257 if (!io_op_defs[req->opcode].async_ctx)
2258 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002259 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002260 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002261}
2262
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002263static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2264 struct iovec *iovec, struct iovec *fast_iov,
2265 struct iov_iter *iter)
2266{
Jens Axboe980ad262020-01-24 23:08:54 -07002267 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002268 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002269 if (!req->io) {
2270 if (io_alloc_async_ctx(req))
2271 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002272
Jens Axboe5d204bc2020-01-31 12:06:52 -07002273 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2274 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002275 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002276}
2277
Jens Axboe3529d8c2019-12-19 18:24:38 -07002278static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2279 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002280{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002281 struct io_async_ctx *io;
2282 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002283 ssize_t ret;
2284
Jens Axboe3529d8c2019-12-19 18:24:38 -07002285 ret = io_prep_rw(req, sqe, force_nonblock);
2286 if (ret)
2287 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002288
Jens Axboe3529d8c2019-12-19 18:24:38 -07002289 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2290 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002291
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002292 /* either don't need iovec imported or already have it */
2293 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002294 return 0;
2295
2296 io = req->io;
2297 io->rw.iov = io->rw.fast_iov;
2298 req->io = NULL;
2299 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2300 req->io = io;
2301 if (ret < 0)
2302 return ret;
2303
2304 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2305 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002306}
2307
Pavel Begunkov014db002020-03-03 21:33:12 +03002308static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002309{
2310 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002311 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002312 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002313 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002314 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002315
Jens Axboe3529d8c2019-12-19 18:24:38 -07002316 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002317 if (ret < 0)
2318 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002319
Jens Axboefd6c2e42019-12-18 12:19:41 -07002320 /* Ensure we clear previously set non-block flag */
2321 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002322 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002323
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002324 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002325 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002326 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002327 req->result = io_size;
2328
2329 /*
2330 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2331 * we know to async punt it even if it was opened O_NONBLOCK
2332 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002333 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002334 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002335
Jens Axboe31b51512019-01-18 22:56:34 -07002336 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002337 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002338 if (!ret) {
2339 ssize_t ret2;
2340
Jens Axboe9adbd452019-12-20 08:45:55 -07002341 if (req->file->f_op->read_iter)
2342 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002343 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002344 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002345
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002346 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002347 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002348 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002349 } else {
2350copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002351 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002352 inline_vecs, &iter);
2353 if (ret)
2354 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002355 /* any defer here is final, must blocking retry */
2356 if (!(req->flags & REQ_F_NOWAIT))
2357 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002358 return -EAGAIN;
2359 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002360 }
Jens Axboef67676d2019-12-02 11:03:47 -07002361out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002362 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002363 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002364 return ret;
2365}
2366
Jens Axboe3529d8c2019-12-19 18:24:38 -07002367static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2368 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002369{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002370 struct io_async_ctx *io;
2371 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002372 ssize_t ret;
2373
Jens Axboe3529d8c2019-12-19 18:24:38 -07002374 ret = io_prep_rw(req, sqe, force_nonblock);
2375 if (ret)
2376 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002377
Jens Axboe3529d8c2019-12-19 18:24:38 -07002378 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2379 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002380
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002381 /* either don't need iovec imported or already have it */
2382 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002383 return 0;
2384
2385 io = req->io;
2386 io->rw.iov = io->rw.fast_iov;
2387 req->io = NULL;
2388 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2389 req->io = io;
2390 if (ret < 0)
2391 return ret;
2392
2393 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2394 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002395}
2396
Pavel Begunkov014db002020-03-03 21:33:12 +03002397static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002398{
2399 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002400 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002401 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002402 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002403 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002404
Jens Axboe3529d8c2019-12-19 18:24:38 -07002405 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002406 if (ret < 0)
2407 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002408
Jens Axboefd6c2e42019-12-18 12:19:41 -07002409 /* Ensure we clear previously set non-block flag */
2410 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002411 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002412
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002413 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002414 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002415 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002416 req->result = io_size;
2417
2418 /*
2419 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2420 * we know to async punt it even if it was opened O_NONBLOCK
2421 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002422 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002423 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002424
Jens Axboe10d59342019-12-09 20:16:22 -07002425 /* file path doesn't support NOWAIT for non-direct_IO */
2426 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2427 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002428 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002429
Jens Axboe31b51512019-01-18 22:56:34 -07002430 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002431 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002432 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002433 ssize_t ret2;
2434
Jens Axboe2b188cc2019-01-07 10:46:33 -07002435 /*
2436 * Open-code file_start_write here to grab freeze protection,
2437 * which will be released by another thread in
2438 * io_complete_rw(). Fool lockdep by telling it the lock got
2439 * released so that it doesn't complain about the held lock when
2440 * we return to userspace.
2441 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002442 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002443 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002444 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002445 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002446 SB_FREEZE_WRITE);
2447 }
2448 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002449
Jens Axboe9adbd452019-12-20 08:45:55 -07002450 if (req->file->f_op->write_iter)
2451 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002452 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002453 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002454 /*
2455 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2456 * retry them without IOCB_NOWAIT.
2457 */
2458 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2459 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002460 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002461 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002462 } else {
2463copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002464 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002465 inline_vecs, &iter);
2466 if (ret)
2467 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002468 /* any defer here is final, must blocking retry */
2469 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002470 return -EAGAIN;
2471 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002472 }
Jens Axboe31b51512019-01-18 22:56:34 -07002473out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002474 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002475 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002476 return ret;
2477}
2478
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002479static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2480{
2481 struct io_splice* sp = &req->splice;
2482 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2483 int ret;
2484
2485 if (req->flags & REQ_F_NEED_CLEANUP)
2486 return 0;
2487
2488 sp->file_in = NULL;
2489 sp->off_in = READ_ONCE(sqe->splice_off_in);
2490 sp->off_out = READ_ONCE(sqe->off);
2491 sp->len = READ_ONCE(sqe->len);
2492 sp->flags = READ_ONCE(sqe->splice_flags);
2493
2494 if (unlikely(sp->flags & ~valid_flags))
2495 return -EINVAL;
2496
2497 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2498 (sp->flags & SPLICE_F_FD_IN_FIXED));
2499 if (ret)
2500 return ret;
2501 req->flags |= REQ_F_NEED_CLEANUP;
2502
2503 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2504 req->work.flags |= IO_WQ_WORK_UNBOUND;
2505
2506 return 0;
2507}
2508
2509static bool io_splice_punt(struct file *file)
2510{
2511 if (get_pipe_info(file))
2512 return false;
2513 if (!io_file_supports_async(file))
2514 return true;
2515 return !(file->f_mode & O_NONBLOCK);
2516}
2517
Pavel Begunkov014db002020-03-03 21:33:12 +03002518static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002519{
2520 struct io_splice *sp = &req->splice;
2521 struct file *in = sp->file_in;
2522 struct file *out = sp->file_out;
2523 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2524 loff_t *poff_in, *poff_out;
2525 long ret;
2526
2527 if (force_nonblock) {
2528 if (io_splice_punt(in) || io_splice_punt(out))
2529 return -EAGAIN;
2530 flags |= SPLICE_F_NONBLOCK;
2531 }
2532
2533 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2534 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2535 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2536 if (force_nonblock && ret == -EAGAIN)
2537 return -EAGAIN;
2538
2539 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2540 req->flags &= ~REQ_F_NEED_CLEANUP;
2541
2542 io_cqring_add_event(req, ret);
2543 if (ret != sp->len)
2544 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002545 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002546 return 0;
2547}
2548
Jens Axboe2b188cc2019-01-07 10:46:33 -07002549/*
2550 * IORING_OP_NOP just posts a completion event, nothing else.
2551 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002552static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002553{
2554 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002555
Jens Axboedef596e2019-01-09 08:59:42 -07002556 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2557 return -EINVAL;
2558
Jens Axboe78e19bb2019-11-06 15:21:34 -07002559 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002560 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002561 return 0;
2562}
2563
Jens Axboe3529d8c2019-12-19 18:24:38 -07002564static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002565{
Jens Axboe6b063142019-01-10 22:13:58 -07002566 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002567
Jens Axboe09bb8392019-03-13 12:39:28 -06002568 if (!req->file)
2569 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002570
Jens Axboe6b063142019-01-10 22:13:58 -07002571 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002572 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002573 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002574 return -EINVAL;
2575
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002576 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2577 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2578 return -EINVAL;
2579
2580 req->sync.off = READ_ONCE(sqe->off);
2581 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002582 return 0;
2583}
2584
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002585static bool io_req_cancelled(struct io_kiocb *req)
2586{
2587 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2588 req_set_fail_links(req);
2589 io_cqring_add_event(req, -ECANCELED);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002590 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002591 return true;
2592 }
2593
2594 return false;
2595}
2596
Pavel Begunkov014db002020-03-03 21:33:12 +03002597static void __io_fsync(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002598{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002599 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002600 int ret;
2601
Jens Axboe9adbd452019-12-20 08:45:55 -07002602 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002603 end > 0 ? end : LLONG_MAX,
2604 req->sync.flags & IORING_FSYNC_DATASYNC);
2605 if (ret < 0)
2606 req_set_fail_links(req);
2607 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002608 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002609}
2610
2611static void io_fsync_finish(struct io_wq_work **workptr)
2612{
2613 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002614
2615 if (io_req_cancelled(req))
2616 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002617 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002618 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002619}
2620
Pavel Begunkov014db002020-03-03 21:33:12 +03002621static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002622{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002623 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002624 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002625 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002626 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002627 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002628 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002629 return 0;
2630}
2631
Pavel Begunkov014db002020-03-03 21:33:12 +03002632static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002633{
Jens Axboed63d1b52019-12-10 10:38:56 -07002634 int ret;
2635
2636 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2637 req->sync.len);
2638 if (ret < 0)
2639 req_set_fail_links(req);
2640 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002641 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002642}
2643
2644static void io_fallocate_finish(struct io_wq_work **workptr)
2645{
2646 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002647
Pavel Begunkov594506f2020-03-03 21:33:11 +03002648 if (io_req_cancelled(req))
2649 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002650 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002651 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002652}
2653
2654static int io_fallocate_prep(struct io_kiocb *req,
2655 const struct io_uring_sqe *sqe)
2656{
2657 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2658 return -EINVAL;
2659
2660 req->sync.off = READ_ONCE(sqe->off);
2661 req->sync.len = READ_ONCE(sqe->addr);
2662 req->sync.mode = READ_ONCE(sqe->len);
2663 return 0;
2664}
2665
Pavel Begunkov014db002020-03-03 21:33:12 +03002666static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002667{
Jens Axboed63d1b52019-12-10 10:38:56 -07002668 /* fallocate always requiring blocking context */
2669 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002670 req->work.func = io_fallocate_finish;
2671 return -EAGAIN;
2672 }
2673
Pavel Begunkov014db002020-03-03 21:33:12 +03002674 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002675 return 0;
2676}
2677
Jens Axboe15b71ab2019-12-11 11:20:36 -07002678static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2679{
Jens Axboef8748882020-01-08 17:47:02 -07002680 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002681 int ret;
2682
2683 if (sqe->ioprio || sqe->buf_index)
2684 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002685 if (sqe->flags & IOSQE_FIXED_FILE)
2686 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002687 if (req->flags & REQ_F_NEED_CLEANUP)
2688 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002689
2690 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002691 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002692 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002693 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002694
Jens Axboef8748882020-01-08 17:47:02 -07002695 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002696 if (IS_ERR(req->open.filename)) {
2697 ret = PTR_ERR(req->open.filename);
2698 req->open.filename = NULL;
2699 return ret;
2700 }
2701
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002702 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002703 return 0;
2704}
2705
Jens Axboecebdb982020-01-08 17:59:24 -07002706static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2707{
2708 struct open_how __user *how;
2709 const char __user *fname;
2710 size_t len;
2711 int ret;
2712
2713 if (sqe->ioprio || sqe->buf_index)
2714 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002715 if (sqe->flags & IOSQE_FIXED_FILE)
2716 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002717 if (req->flags & REQ_F_NEED_CLEANUP)
2718 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002719
2720 req->open.dfd = READ_ONCE(sqe->fd);
2721 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2722 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2723 len = READ_ONCE(sqe->len);
2724
2725 if (len < OPEN_HOW_SIZE_VER0)
2726 return -EINVAL;
2727
2728 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2729 len);
2730 if (ret)
2731 return ret;
2732
2733 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2734 req->open.how.flags |= O_LARGEFILE;
2735
2736 req->open.filename = getname(fname);
2737 if (IS_ERR(req->open.filename)) {
2738 ret = PTR_ERR(req->open.filename);
2739 req->open.filename = NULL;
2740 return ret;
2741 }
2742
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002743 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002744 return 0;
2745}
2746
Pavel Begunkov014db002020-03-03 21:33:12 +03002747static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002748{
2749 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002750 struct file *file;
2751 int ret;
2752
Jens Axboef86cd202020-01-29 13:46:44 -07002753 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002754 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002755
Jens Axboecebdb982020-01-08 17:59:24 -07002756 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002757 if (ret)
2758 goto err;
2759
Jens Axboecebdb982020-01-08 17:59:24 -07002760 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002761 if (ret < 0)
2762 goto err;
2763
2764 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2765 if (IS_ERR(file)) {
2766 put_unused_fd(ret);
2767 ret = PTR_ERR(file);
2768 } else {
2769 fsnotify_open(file);
2770 fd_install(ret, file);
2771 }
2772err:
2773 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002774 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002775 if (ret < 0)
2776 req_set_fail_links(req);
2777 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002778 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002779 return 0;
2780}
2781
Pavel Begunkov014db002020-03-03 21:33:12 +03002782static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07002783{
2784 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03002785 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07002786}
2787
Jens Axboe3e4827b2020-01-08 15:18:09 -07002788static int io_epoll_ctl_prep(struct io_kiocb *req,
2789 const struct io_uring_sqe *sqe)
2790{
2791#if defined(CONFIG_EPOLL)
2792 if (sqe->ioprio || sqe->buf_index)
2793 return -EINVAL;
2794
2795 req->epoll.epfd = READ_ONCE(sqe->fd);
2796 req->epoll.op = READ_ONCE(sqe->len);
2797 req->epoll.fd = READ_ONCE(sqe->off);
2798
2799 if (ep_op_has_event(req->epoll.op)) {
2800 struct epoll_event __user *ev;
2801
2802 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2803 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2804 return -EFAULT;
2805 }
2806
2807 return 0;
2808#else
2809 return -EOPNOTSUPP;
2810#endif
2811}
2812
Pavel Begunkov014db002020-03-03 21:33:12 +03002813static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07002814{
2815#if defined(CONFIG_EPOLL)
2816 struct io_epoll *ie = &req->epoll;
2817 int ret;
2818
2819 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2820 if (force_nonblock && ret == -EAGAIN)
2821 return -EAGAIN;
2822
2823 if (ret < 0)
2824 req_set_fail_links(req);
2825 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002826 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07002827 return 0;
2828#else
2829 return -EOPNOTSUPP;
2830#endif
2831}
2832
Jens Axboec1ca7572019-12-25 22:18:28 -07002833static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2834{
2835#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2836 if (sqe->ioprio || sqe->buf_index || sqe->off)
2837 return -EINVAL;
2838
2839 req->madvise.addr = READ_ONCE(sqe->addr);
2840 req->madvise.len = READ_ONCE(sqe->len);
2841 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2842 return 0;
2843#else
2844 return -EOPNOTSUPP;
2845#endif
2846}
2847
Pavel Begunkov014db002020-03-03 21:33:12 +03002848static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07002849{
2850#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2851 struct io_madvise *ma = &req->madvise;
2852 int ret;
2853
2854 if (force_nonblock)
2855 return -EAGAIN;
2856
2857 ret = do_madvise(ma->addr, ma->len, ma->advice);
2858 if (ret < 0)
2859 req_set_fail_links(req);
2860 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002861 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07002862 return 0;
2863#else
2864 return -EOPNOTSUPP;
2865#endif
2866}
2867
Jens Axboe4840e412019-12-25 22:03:45 -07002868static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2869{
2870 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2871 return -EINVAL;
2872
2873 req->fadvise.offset = READ_ONCE(sqe->off);
2874 req->fadvise.len = READ_ONCE(sqe->len);
2875 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2876 return 0;
2877}
2878
Pavel Begunkov014db002020-03-03 21:33:12 +03002879static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07002880{
2881 struct io_fadvise *fa = &req->fadvise;
2882 int ret;
2883
Jens Axboe3e694262020-02-01 09:22:49 -07002884 if (force_nonblock) {
2885 switch (fa->advice) {
2886 case POSIX_FADV_NORMAL:
2887 case POSIX_FADV_RANDOM:
2888 case POSIX_FADV_SEQUENTIAL:
2889 break;
2890 default:
2891 return -EAGAIN;
2892 }
2893 }
Jens Axboe4840e412019-12-25 22:03:45 -07002894
2895 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2896 if (ret < 0)
2897 req_set_fail_links(req);
2898 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002899 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07002900 return 0;
2901}
2902
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002903static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2904{
Jens Axboef8748882020-01-08 17:47:02 -07002905 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002906 unsigned lookup_flags;
2907 int ret;
2908
2909 if (sqe->ioprio || sqe->buf_index)
2910 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002911 if (sqe->flags & IOSQE_FIXED_FILE)
2912 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002913 if (req->flags & REQ_F_NEED_CLEANUP)
2914 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002915
2916 req->open.dfd = READ_ONCE(sqe->fd);
2917 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002918 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002919 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002920 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002921
Jens Axboec12cedf2020-01-08 17:41:21 -07002922 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002923 return -EINVAL;
2924
Jens Axboef8748882020-01-08 17:47:02 -07002925 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002926 if (IS_ERR(req->open.filename)) {
2927 ret = PTR_ERR(req->open.filename);
2928 req->open.filename = NULL;
2929 return ret;
2930 }
2931
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002932 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002933 return 0;
2934}
2935
Pavel Begunkov014db002020-03-03 21:33:12 +03002936static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002937{
2938 struct io_open *ctx = &req->open;
2939 unsigned lookup_flags;
2940 struct path path;
2941 struct kstat stat;
2942 int ret;
2943
2944 if (force_nonblock)
2945 return -EAGAIN;
2946
Jens Axboec12cedf2020-01-08 17:41:21 -07002947 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002948 return -EINVAL;
2949
2950retry:
2951 /* filename_lookup() drops it, keep a reference */
2952 ctx->filename->refcnt++;
2953
2954 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2955 NULL);
2956 if (ret)
2957 goto err;
2958
Jens Axboec12cedf2020-01-08 17:41:21 -07002959 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002960 path_put(&path);
2961 if (retry_estale(ret, lookup_flags)) {
2962 lookup_flags |= LOOKUP_REVAL;
2963 goto retry;
2964 }
2965 if (!ret)
2966 ret = cp_statx(&stat, ctx->buffer);
2967err:
2968 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002969 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002970 if (ret < 0)
2971 req_set_fail_links(req);
2972 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002973 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002974 return 0;
2975}
2976
Jens Axboeb5dba592019-12-11 14:02:38 -07002977static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2978{
2979 /*
2980 * If we queue this for async, it must not be cancellable. That would
2981 * leave the 'file' in an undeterminate state.
2982 */
2983 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2984
2985 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2986 sqe->rw_flags || sqe->buf_index)
2987 return -EINVAL;
2988 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002989 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002990
2991 req->close.fd = READ_ONCE(sqe->fd);
2992 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002993 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002994 return -EBADF;
2995
2996 return 0;
2997}
2998
Pavel Begunkova93b3332020-02-08 14:04:34 +03002999/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003000static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003001{
3002 int ret;
3003
3004 ret = filp_close(req->close.put_file, req->work.files);
3005 if (ret < 0)
3006 req_set_fail_links(req);
3007 io_cqring_add_event(req, ret);
3008 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003009 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003010}
3011
Jens Axboeb5dba592019-12-11 14:02:38 -07003012static void io_close_finish(struct io_wq_work **workptr)
3013{
3014 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003015
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003016 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003017 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003018 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003019}
3020
Pavel Begunkov014db002020-03-03 21:33:12 +03003021static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003022{
3023 int ret;
3024
3025 req->close.put_file = NULL;
3026 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3027 if (ret < 0)
3028 return ret;
3029
3030 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003031 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003032 /* submission ref will be dropped, take it for async */
3033 refcount_inc(&req->refs);
3034
Pavel Begunkova2100672020-03-02 23:45:16 +03003035 req->work.func = io_close_finish;
3036 /*
3037 * Do manual async queue here to avoid grabbing files - we don't
3038 * need the files, and it'll cause io_close_finish() to close
3039 * the file again and cause a double CQE entry for this request
3040 */
3041 io_queue_async_work(req);
3042 return 0;
3043 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003044
3045 /*
3046 * No ->flush(), safely close from here and just punt the
3047 * fput() to async context.
3048 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003049 __io_close_finish(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003050 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003051}
3052
Jens Axboe3529d8c2019-12-19 18:24:38 -07003053static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003054{
3055 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003056
3057 if (!req->file)
3058 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003059
3060 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3061 return -EINVAL;
3062 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3063 return -EINVAL;
3064
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003065 req->sync.off = READ_ONCE(sqe->off);
3066 req->sync.len = READ_ONCE(sqe->len);
3067 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003068 return 0;
3069}
3070
Pavel Begunkov014db002020-03-03 21:33:12 +03003071static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003072{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003073 int ret;
3074
Jens Axboe9adbd452019-12-20 08:45:55 -07003075 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003076 req->sync.flags);
3077 if (ret < 0)
3078 req_set_fail_links(req);
3079 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003080 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003081}
3082
3083
3084static void io_sync_file_range_finish(struct io_wq_work **workptr)
3085{
3086 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3087 struct io_kiocb *nxt = NULL;
3088
3089 if (io_req_cancelled(req))
3090 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003091 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003092 io_put_req(req); /* put submission ref */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003093 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003094 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003095}
3096
Pavel Begunkov014db002020-03-03 21:33:12 +03003097static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003098{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003099 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003100 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003101 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003102 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003103 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003104
Pavel Begunkov014db002020-03-03 21:33:12 +03003105 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003106 return 0;
3107}
3108
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003109static int io_setup_async_msg(struct io_kiocb *req,
3110 struct io_async_msghdr *kmsg)
3111{
3112 if (req->io)
3113 return -EAGAIN;
3114 if (io_alloc_async_ctx(req)) {
3115 if (kmsg->iov != kmsg->fast_iov)
3116 kfree(kmsg->iov);
3117 return -ENOMEM;
3118 }
3119 req->flags |= REQ_F_NEED_CLEANUP;
3120 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3121 return -EAGAIN;
3122}
3123
Jens Axboe3529d8c2019-12-19 18:24:38 -07003124static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003125{
Jens Axboe03b12302019-12-02 18:50:25 -07003126#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003127 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003128 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003129 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003130
Jens Axboee47293f2019-12-20 08:58:21 -07003131 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3132 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003133 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003134
Jens Axboed8768362020-02-27 14:17:49 -07003135#ifdef CONFIG_COMPAT
3136 if (req->ctx->compat)
3137 sr->msg_flags |= MSG_CMSG_COMPAT;
3138#endif
3139
Jens Axboefddafac2020-01-04 20:19:44 -07003140 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003141 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003142 /* iovec is already imported */
3143 if (req->flags & REQ_F_NEED_CLEANUP)
3144 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003145
Jens Axboed9688562019-12-09 19:35:20 -07003146 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003147 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003148 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003149 if (!ret)
3150 req->flags |= REQ_F_NEED_CLEANUP;
3151 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003152#else
Jens Axboee47293f2019-12-20 08:58:21 -07003153 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003154#endif
3155}
3156
Pavel Begunkov014db002020-03-03 21:33:12 +03003157static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003158{
3159#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003160 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003161 struct socket *sock;
3162 int ret;
3163
3164 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3165 return -EINVAL;
3166
3167 sock = sock_from_file(req->file, &ret);
3168 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003169 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003170 unsigned flags;
3171
Jens Axboe03b12302019-12-02 18:50:25 -07003172 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003173 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003174 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003175 /* if iov is set, it's allocated already */
3176 if (!kmsg->iov)
3177 kmsg->iov = kmsg->fast_iov;
3178 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003179 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003180 struct io_sr_msg *sr = &req->sr_msg;
3181
Jens Axboe0b416c32019-12-15 10:57:46 -07003182 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003183 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003184
3185 io.msg.iov = io.msg.fast_iov;
3186 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3187 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003188 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003189 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003190 }
3191
Jens Axboee47293f2019-12-20 08:58:21 -07003192 flags = req->sr_msg.msg_flags;
3193 if (flags & MSG_DONTWAIT)
3194 req->flags |= REQ_F_NOWAIT;
3195 else if (force_nonblock)
3196 flags |= MSG_DONTWAIT;
3197
Jens Axboe0b416c32019-12-15 10:57:46 -07003198 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003199 if (force_nonblock && ret == -EAGAIN)
3200 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003201 if (ret == -ERESTARTSYS)
3202 ret = -EINTR;
3203 }
3204
Pavel Begunkov1e950812020-02-06 19:51:16 +03003205 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003206 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003207 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003208 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003209 if (ret < 0)
3210 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003211 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003212 return 0;
3213#else
3214 return -EOPNOTSUPP;
3215#endif
3216}
3217
Pavel Begunkov014db002020-03-03 21:33:12 +03003218static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003219{
3220#if defined(CONFIG_NET)
3221 struct socket *sock;
3222 int ret;
3223
3224 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3225 return -EINVAL;
3226
3227 sock = sock_from_file(req->file, &ret);
3228 if (sock) {
3229 struct io_sr_msg *sr = &req->sr_msg;
3230 struct msghdr msg;
3231 struct iovec iov;
3232 unsigned flags;
3233
3234 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3235 &msg.msg_iter);
3236 if (ret)
3237 return ret;
3238
3239 msg.msg_name = NULL;
3240 msg.msg_control = NULL;
3241 msg.msg_controllen = 0;
3242 msg.msg_namelen = 0;
3243
3244 flags = req->sr_msg.msg_flags;
3245 if (flags & MSG_DONTWAIT)
3246 req->flags |= REQ_F_NOWAIT;
3247 else if (force_nonblock)
3248 flags |= MSG_DONTWAIT;
3249
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003250 msg.msg_flags = flags;
3251 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003252 if (force_nonblock && ret == -EAGAIN)
3253 return -EAGAIN;
3254 if (ret == -ERESTARTSYS)
3255 ret = -EINTR;
3256 }
3257
3258 io_cqring_add_event(req, ret);
3259 if (ret < 0)
3260 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003261 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003262 return 0;
3263#else
3264 return -EOPNOTSUPP;
3265#endif
3266}
3267
Jens Axboe3529d8c2019-12-19 18:24:38 -07003268static int io_recvmsg_prep(struct io_kiocb *req,
3269 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003270{
3271#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003272 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003273 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003274 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003275
Jens Axboe3529d8c2019-12-19 18:24:38 -07003276 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3277 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003278 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003279
Jens Axboed8768362020-02-27 14:17:49 -07003280#ifdef CONFIG_COMPAT
3281 if (req->ctx->compat)
3282 sr->msg_flags |= MSG_CMSG_COMPAT;
3283#endif
3284
Jens Axboefddafac2020-01-04 20:19:44 -07003285 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003286 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003287 /* iovec is already imported */
3288 if (req->flags & REQ_F_NEED_CLEANUP)
3289 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003290
Jens Axboed9688562019-12-09 19:35:20 -07003291 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003292 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003293 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003294 if (!ret)
3295 req->flags |= REQ_F_NEED_CLEANUP;
3296 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003297#else
Jens Axboee47293f2019-12-20 08:58:21 -07003298 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003299#endif
3300}
3301
Pavel Begunkov014db002020-03-03 21:33:12 +03003302static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003303{
3304#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003305 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003306 struct socket *sock;
3307 int ret;
3308
3309 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3310 return -EINVAL;
3311
3312 sock = sock_from_file(req->file, &ret);
3313 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003314 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003315 unsigned flags;
3316
Jens Axboe03b12302019-12-02 18:50:25 -07003317 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003318 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003319 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003320 /* if iov is set, it's allocated already */
3321 if (!kmsg->iov)
3322 kmsg->iov = kmsg->fast_iov;
3323 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003324 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003325 struct io_sr_msg *sr = &req->sr_msg;
3326
Jens Axboe0b416c32019-12-15 10:57:46 -07003327 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003328 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003329
3330 io.msg.iov = io.msg.fast_iov;
3331 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3332 sr->msg_flags, &io.msg.uaddr,
3333 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003334 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003335 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003336 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003337
Jens Axboee47293f2019-12-20 08:58:21 -07003338 flags = req->sr_msg.msg_flags;
3339 if (flags & MSG_DONTWAIT)
3340 req->flags |= REQ_F_NOWAIT;
3341 else if (force_nonblock)
3342 flags |= MSG_DONTWAIT;
3343
3344 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3345 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003346 if (force_nonblock && ret == -EAGAIN)
3347 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003348 if (ret == -ERESTARTSYS)
3349 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003350 }
3351
Pavel Begunkov1e950812020-02-06 19:51:16 +03003352 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003353 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003354 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003355 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003356 if (ret < 0)
3357 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003358 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003359 return 0;
3360#else
3361 return -EOPNOTSUPP;
3362#endif
3363}
3364
Pavel Begunkov014db002020-03-03 21:33:12 +03003365static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003366{
3367#if defined(CONFIG_NET)
3368 struct socket *sock;
3369 int ret;
3370
3371 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3372 return -EINVAL;
3373
3374 sock = sock_from_file(req->file, &ret);
3375 if (sock) {
3376 struct io_sr_msg *sr = &req->sr_msg;
3377 struct msghdr msg;
3378 struct iovec iov;
3379 unsigned flags;
3380
3381 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3382 &msg.msg_iter);
3383 if (ret)
3384 return ret;
3385
3386 msg.msg_name = NULL;
3387 msg.msg_control = NULL;
3388 msg.msg_controllen = 0;
3389 msg.msg_namelen = 0;
3390 msg.msg_iocb = NULL;
3391 msg.msg_flags = 0;
3392
3393 flags = req->sr_msg.msg_flags;
3394 if (flags & MSG_DONTWAIT)
3395 req->flags |= REQ_F_NOWAIT;
3396 else if (force_nonblock)
3397 flags |= MSG_DONTWAIT;
3398
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003399 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003400 if (force_nonblock && ret == -EAGAIN)
3401 return -EAGAIN;
3402 if (ret == -ERESTARTSYS)
3403 ret = -EINTR;
3404 }
3405
3406 io_cqring_add_event(req, ret);
3407 if (ret < 0)
3408 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003409 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003410 return 0;
3411#else
3412 return -EOPNOTSUPP;
3413#endif
3414}
3415
3416
Jens Axboe3529d8c2019-12-19 18:24:38 -07003417static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003418{
3419#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003420 struct io_accept *accept = &req->accept;
3421
Jens Axboe17f2fe32019-10-17 14:42:58 -06003422 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3423 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003424 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003425 return -EINVAL;
3426
Jens Axboed55e5f52019-12-11 16:12:15 -07003427 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3428 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003429 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003430 return 0;
3431#else
3432 return -EOPNOTSUPP;
3433#endif
3434}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003435
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003436#if defined(CONFIG_NET)
Pavel Begunkov014db002020-03-03 21:33:12 +03003437static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003438{
3439 struct io_accept *accept = &req->accept;
3440 unsigned file_flags;
3441 int ret;
3442
3443 file_flags = force_nonblock ? O_NONBLOCK : 0;
3444 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3445 accept->addr_len, accept->flags);
3446 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003447 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003448 if (ret == -ERESTARTSYS)
3449 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003450 if (ret < 0)
3451 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003452 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003453 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003454 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003455}
3456
3457static void io_accept_finish(struct io_wq_work **workptr)
3458{
3459 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003460
3461 if (io_req_cancelled(req))
3462 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003463 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003464 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003465}
3466#endif
3467
Pavel Begunkov014db002020-03-03 21:33:12 +03003468static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003469{
3470#if defined(CONFIG_NET)
3471 int ret;
3472
Pavel Begunkov014db002020-03-03 21:33:12 +03003473 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003474 if (ret == -EAGAIN && force_nonblock) {
3475 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003476 return -EAGAIN;
3477 }
3478 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003479#else
3480 return -EOPNOTSUPP;
3481#endif
3482}
3483
Jens Axboe3529d8c2019-12-19 18:24:38 -07003484static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003485{
3486#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003487 struct io_connect *conn = &req->connect;
3488 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003489
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003490 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3491 return -EINVAL;
3492 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3493 return -EINVAL;
3494
Jens Axboe3529d8c2019-12-19 18:24:38 -07003495 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3496 conn->addr_len = READ_ONCE(sqe->addr2);
3497
3498 if (!io)
3499 return 0;
3500
3501 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003502 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003503#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003504 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003505#endif
3506}
3507
Pavel Begunkov014db002020-03-03 21:33:12 +03003508static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003509{
3510#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003511 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003512 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003513 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003514
Jens Axboef499a022019-12-02 16:28:46 -07003515 if (req->io) {
3516 io = req->io;
3517 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003518 ret = move_addr_to_kernel(req->connect.addr,
3519 req->connect.addr_len,
3520 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003521 if (ret)
3522 goto out;
3523 io = &__io;
3524 }
3525
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003526 file_flags = force_nonblock ? O_NONBLOCK : 0;
3527
3528 ret = __sys_connect_file(req->file, &io->connect.address,
3529 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003530 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003531 if (req->io)
3532 return -EAGAIN;
3533 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003534 ret = -ENOMEM;
3535 goto out;
3536 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003537 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003538 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003539 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003540 if (ret == -ERESTARTSYS)
3541 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003542out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003543 if (ret < 0)
3544 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003545 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003546 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003547 return 0;
3548#else
3549 return -EOPNOTSUPP;
3550#endif
3551}
3552
Jens Axboed7718a92020-02-14 22:23:12 -07003553struct io_poll_table {
3554 struct poll_table_struct pt;
3555 struct io_kiocb *req;
3556 int error;
3557};
3558
3559static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
3560 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003561{
Jens Axboed7718a92020-02-14 22:23:12 -07003562 if (unlikely(poll->head)) {
3563 pt->error = -EINVAL;
3564 return;
3565 }
3566
3567 pt->error = 0;
3568 poll->head = head;
3569 add_wait_queue(head, &poll->wait);
3570}
3571
3572static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
3573 struct poll_table_struct *p)
3574{
3575 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3576
3577 __io_queue_proc(&pt->req->apoll->poll, pt, head);
3578}
3579
3580static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
3581 __poll_t mask, task_work_func_t func)
3582{
3583 struct task_struct *tsk;
3584
3585 /* for instances that support it check for an event match first: */
3586 if (mask && !(mask & poll->events))
3587 return 0;
3588
3589 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
3590
3591 list_del_init(&poll->wait.entry);
3592
3593 tsk = req->task;
3594 req->result = mask;
3595 init_task_work(&req->task_work, func);
3596 /*
3597 * If this fails, then the task is exiting. If that is the case, then
3598 * the exit check will ultimately cancel these work items. Hence we
3599 * don't need to check here and handle it specifically.
3600 */
3601 task_work_add(tsk, &req->task_work, true);
3602 wake_up_process(tsk);
3603 return 1;
3604}
3605
3606static void io_async_task_func(struct callback_head *cb)
3607{
3608 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3609 struct async_poll *apoll = req->apoll;
3610 struct io_ring_ctx *ctx = req->ctx;
3611
3612 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
3613
3614 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
3615
3616 if (hash_hashed(&req->hash_node)) {
3617 spin_lock_irq(&ctx->completion_lock);
3618 hash_del(&req->hash_node);
3619 spin_unlock_irq(&ctx->completion_lock);
3620 }
3621
3622 /* restore ->work in case we need to retry again */
3623 memcpy(&req->work, &apoll->work, sizeof(req->work));
3624
3625 __set_current_state(TASK_RUNNING);
3626 mutex_lock(&ctx->uring_lock);
3627 __io_queue_sqe(req, NULL);
3628 mutex_unlock(&ctx->uring_lock);
3629
3630 kfree(apoll);
3631}
3632
3633static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3634 void *key)
3635{
3636 struct io_kiocb *req = wait->private;
3637 struct io_poll_iocb *poll = &req->apoll->poll;
3638
3639 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
3640 key_to_poll(key));
3641
3642 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
3643}
3644
3645static void io_poll_req_insert(struct io_kiocb *req)
3646{
3647 struct io_ring_ctx *ctx = req->ctx;
3648 struct hlist_head *list;
3649
3650 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3651 hlist_add_head(&req->hash_node, list);
3652}
3653
3654static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
3655 struct io_poll_iocb *poll,
3656 struct io_poll_table *ipt, __poll_t mask,
3657 wait_queue_func_t wake_func)
3658 __acquires(&ctx->completion_lock)
3659{
3660 struct io_ring_ctx *ctx = req->ctx;
3661 bool cancel = false;
3662
3663 poll->file = req->file;
3664 poll->head = NULL;
3665 poll->done = poll->canceled = false;
3666 poll->events = mask;
3667
3668 ipt->pt._key = mask;
3669 ipt->req = req;
3670 ipt->error = -EINVAL;
3671
3672 INIT_LIST_HEAD(&poll->wait.entry);
3673 init_waitqueue_func_entry(&poll->wait, wake_func);
3674 poll->wait.private = req;
3675
3676 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
3677
3678 spin_lock_irq(&ctx->completion_lock);
3679 if (likely(poll->head)) {
3680 spin_lock(&poll->head->lock);
3681 if (unlikely(list_empty(&poll->wait.entry))) {
3682 if (ipt->error)
3683 cancel = true;
3684 ipt->error = 0;
3685 mask = 0;
3686 }
3687 if (mask || ipt->error)
3688 list_del_init(&poll->wait.entry);
3689 else if (cancel)
3690 WRITE_ONCE(poll->canceled, true);
3691 else if (!poll->done) /* actually waiting for an event */
3692 io_poll_req_insert(req);
3693 spin_unlock(&poll->head->lock);
3694 }
3695
3696 return mask;
3697}
3698
3699static bool io_arm_poll_handler(struct io_kiocb *req)
3700{
3701 const struct io_op_def *def = &io_op_defs[req->opcode];
3702 struct io_ring_ctx *ctx = req->ctx;
3703 struct async_poll *apoll;
3704 struct io_poll_table ipt;
3705 __poll_t mask, ret;
3706
3707 if (!req->file || !file_can_poll(req->file))
3708 return false;
3709 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
3710 return false;
3711 if (!def->pollin && !def->pollout)
3712 return false;
3713
3714 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
3715 if (unlikely(!apoll))
3716 return false;
3717
3718 req->flags |= REQ_F_POLLED;
3719 memcpy(&apoll->work, &req->work, sizeof(req->work));
3720
3721 /*
3722 * Don't need a reference here, as we're adding it to the task
3723 * task_works list. If the task exits, the list is pruned.
3724 */
3725 req->task = current;
3726 req->apoll = apoll;
3727 INIT_HLIST_NODE(&req->hash_node);
3728
Nathan Chancellor8755d972020-03-02 16:01:19 -07003729 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07003730 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07003731 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07003732 if (def->pollout)
3733 mask |= POLLOUT | POLLWRNORM;
3734 mask |= POLLERR | POLLPRI;
3735
3736 ipt.pt._qproc = io_async_queue_proc;
3737
3738 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
3739 io_async_wake);
3740 if (ret) {
3741 ipt.error = 0;
3742 apoll->poll.done = true;
3743 spin_unlock_irq(&ctx->completion_lock);
3744 memcpy(&req->work, &apoll->work, sizeof(req->work));
3745 kfree(apoll);
3746 return false;
3747 }
3748 spin_unlock_irq(&ctx->completion_lock);
3749 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
3750 apoll->poll.events);
3751 return true;
3752}
3753
3754static bool __io_poll_remove_one(struct io_kiocb *req,
3755 struct io_poll_iocb *poll)
3756{
Jens Axboeb41e9852020-02-17 09:52:41 -07003757 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003758
3759 spin_lock(&poll->head->lock);
3760 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003761 if (!list_empty(&poll->wait.entry)) {
3762 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07003763 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003764 }
3765 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07003766 return do_complete;
3767}
3768
3769static bool io_poll_remove_one(struct io_kiocb *req)
3770{
3771 bool do_complete;
3772
3773 if (req->opcode == IORING_OP_POLL_ADD) {
3774 do_complete = __io_poll_remove_one(req, &req->poll);
3775 } else {
3776 /* non-poll requests have submit ref still */
3777 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
3778 if (do_complete)
3779 io_put_req(req);
3780 }
3781
Jens Axboe78076bb2019-12-04 19:56:40 -07003782 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07003783
Jens Axboeb41e9852020-02-17 09:52:41 -07003784 if (do_complete) {
3785 io_cqring_fill_event(req, -ECANCELED);
3786 io_commit_cqring(req->ctx);
3787 req->flags |= REQ_F_COMP_LOCKED;
3788 io_put_req(req);
3789 }
3790
3791 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003792}
3793
3794static void io_poll_remove_all(struct io_ring_ctx *ctx)
3795{
Jens Axboe78076bb2019-12-04 19:56:40 -07003796 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003797 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003798 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003799
3800 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003801 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3802 struct hlist_head *list;
3803
3804 list = &ctx->cancel_hash[i];
3805 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3806 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003807 }
3808 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003809
3810 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003811}
3812
Jens Axboe47f46762019-11-09 17:43:02 -07003813static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3814{
Jens Axboe78076bb2019-12-04 19:56:40 -07003815 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003816 struct io_kiocb *req;
3817
Jens Axboe78076bb2019-12-04 19:56:40 -07003818 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3819 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07003820 if (sqe_addr != req->user_data)
3821 continue;
3822 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07003823 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07003824 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07003825 }
3826
3827 return -ENOENT;
3828}
3829
Jens Axboe3529d8c2019-12-19 18:24:38 -07003830static int io_poll_remove_prep(struct io_kiocb *req,
3831 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003832{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003833 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3834 return -EINVAL;
3835 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3836 sqe->poll_events)
3837 return -EINVAL;
3838
Jens Axboe0969e782019-12-17 18:40:57 -07003839 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003840 return 0;
3841}
3842
3843/*
3844 * Find a running poll command that matches one specified in sqe->addr,
3845 * and remove it if found.
3846 */
3847static int io_poll_remove(struct io_kiocb *req)
3848{
3849 struct io_ring_ctx *ctx = req->ctx;
3850 u64 addr;
3851 int ret;
3852
Jens Axboe0969e782019-12-17 18:40:57 -07003853 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003854 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003855 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003856 spin_unlock_irq(&ctx->completion_lock);
3857
Jens Axboe78e19bb2019-11-06 15:21:34 -07003858 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003859 if (ret < 0)
3860 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003861 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003862 return 0;
3863}
3864
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003865static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003866{
Jackie Liua197f662019-11-08 08:09:12 -07003867 struct io_ring_ctx *ctx = req->ctx;
3868
Jens Axboe8c838782019-03-12 15:48:16 -06003869 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03003870 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003871 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003872}
3873
Jens Axboeb41e9852020-02-17 09:52:41 -07003874static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003875{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003876 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003877
Jens Axboe221c5eb2019-01-17 09:41:58 -07003878 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003879 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07003880 io_poll_complete(req, req->result, 0);
3881 req->flags |= REQ_F_COMP_LOCKED;
3882 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003883 spin_unlock_irq(&ctx->completion_lock);
3884
Jens Axboe8c838782019-03-12 15:48:16 -06003885 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07003886}
Jens Axboe89723d02019-11-05 15:32:58 -07003887
Jens Axboeb41e9852020-02-17 09:52:41 -07003888static void io_poll_task_func(struct callback_head *cb)
3889{
3890 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
3891 struct io_kiocb *nxt = NULL;
3892
3893 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07003894 if (nxt) {
3895 struct io_ring_ctx *ctx = nxt->ctx;
3896
3897 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07003898 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07003899 mutex_unlock(&ctx->uring_lock);
3900 }
Jens Axboef0b493e2020-02-01 21:30:11 -07003901}
3902
Jens Axboe221c5eb2019-01-17 09:41:58 -07003903static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3904 void *key)
3905{
Jens Axboec2f2eb72020-02-10 09:07:05 -07003906 struct io_kiocb *req = wait->private;
3907 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003908
Jens Axboed7718a92020-02-14 22:23:12 -07003909 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003910}
3911
Jens Axboe221c5eb2019-01-17 09:41:58 -07003912static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3913 struct poll_table_struct *p)
3914{
3915 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3916
Jens Axboed7718a92020-02-14 22:23:12 -07003917 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07003918}
3919
Jens Axboe3529d8c2019-12-19 18:24:38 -07003920static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003921{
3922 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003923 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003924
3925 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3926 return -EINVAL;
3927 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3928 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003929 if (!poll->file)
3930 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003931
Jens Axboe221c5eb2019-01-17 09:41:58 -07003932 events = READ_ONCE(sqe->poll_events);
3933 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07003934
Jens Axboed7718a92020-02-14 22:23:12 -07003935 /*
3936 * Don't need a reference here, as we're adding it to the task
3937 * task_works list. If the task exits, the list is pruned.
3938 */
Jens Axboeb41e9852020-02-17 09:52:41 -07003939 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07003940 return 0;
3941}
3942
Pavel Begunkov014db002020-03-03 21:33:12 +03003943static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07003944{
3945 struct io_poll_iocb *poll = &req->poll;
3946 struct io_ring_ctx *ctx = req->ctx;
3947 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07003948 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003949
Jens Axboe78076bb2019-12-04 19:56:40 -07003950 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06003951 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07003952 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06003953
Jens Axboed7718a92020-02-14 22:23:12 -07003954 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
3955 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003956
Jens Axboe8c838782019-03-12 15:48:16 -06003957 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003958 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003959 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003960 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003961 spin_unlock_irq(&ctx->completion_lock);
3962
Jens Axboe8c838782019-03-12 15:48:16 -06003963 if (mask) {
3964 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03003965 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003966 }
Jens Axboe8c838782019-03-12 15:48:16 -06003967 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003968}
3969
Jens Axboe5262f562019-09-17 12:26:57 -06003970static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3971{
Jens Axboead8a48a2019-11-15 08:49:11 -07003972 struct io_timeout_data *data = container_of(timer,
3973 struct io_timeout_data, timer);
3974 struct io_kiocb *req = data->req;
3975 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003976 unsigned long flags;
3977
Jens Axboe5262f562019-09-17 12:26:57 -06003978 atomic_inc(&ctx->cq_timeouts);
3979
3980 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003981 /*
Jens Axboe11365042019-10-16 09:08:32 -06003982 * We could be racing with timeout deletion. If the list is empty,
3983 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003984 */
Jens Axboe842f9612019-10-29 12:34:10 -06003985 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003986 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003987
Jens Axboe11365042019-10-16 09:08:32 -06003988 /*
3989 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003990 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003991 * pointer will be increased, otherwise other timeout reqs may
3992 * return in advance without waiting for enough wait_nr.
3993 */
3994 prev = req;
3995 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3996 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003997 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003998 }
Jens Axboe842f9612019-10-29 12:34:10 -06003999
Jens Axboe78e19bb2019-11-06 15:21:34 -07004000 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004001 io_commit_cqring(ctx);
4002 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4003
4004 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004005 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004006 io_put_req(req);
4007 return HRTIMER_NORESTART;
4008}
4009
Jens Axboe47f46762019-11-09 17:43:02 -07004010static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4011{
4012 struct io_kiocb *req;
4013 int ret = -ENOENT;
4014
4015 list_for_each_entry(req, &ctx->timeout_list, list) {
4016 if (user_data == req->user_data) {
4017 list_del_init(&req->list);
4018 ret = 0;
4019 break;
4020 }
4021 }
4022
4023 if (ret == -ENOENT)
4024 return ret;
4025
Jens Axboe2d283902019-12-04 11:08:05 -07004026 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004027 if (ret == -1)
4028 return -EALREADY;
4029
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004030 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004031 io_cqring_fill_event(req, -ECANCELED);
4032 io_put_req(req);
4033 return 0;
4034}
4035
Jens Axboe3529d8c2019-12-19 18:24:38 -07004036static int io_timeout_remove_prep(struct io_kiocb *req,
4037 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004038{
Jens Axboeb29472e2019-12-17 18:50:29 -07004039 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4040 return -EINVAL;
4041 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4042 return -EINVAL;
4043
4044 req->timeout.addr = READ_ONCE(sqe->addr);
4045 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4046 if (req->timeout.flags)
4047 return -EINVAL;
4048
Jens Axboeb29472e2019-12-17 18:50:29 -07004049 return 0;
4050}
4051
Jens Axboe11365042019-10-16 09:08:32 -06004052/*
4053 * Remove or update an existing timeout command
4054 */
Jens Axboefc4df992019-12-10 14:38:45 -07004055static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004056{
4057 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004058 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004059
Jens Axboe11365042019-10-16 09:08:32 -06004060 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004061 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004062
Jens Axboe47f46762019-11-09 17:43:02 -07004063 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004064 io_commit_cqring(ctx);
4065 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004066 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004067 if (ret < 0)
4068 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004069 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004070 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004071}
4072
Jens Axboe3529d8c2019-12-19 18:24:38 -07004073static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004074 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004075{
Jens Axboead8a48a2019-11-15 08:49:11 -07004076 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004077 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004078
Jens Axboead8a48a2019-11-15 08:49:11 -07004079 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004080 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004081 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004082 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004083 if (sqe->off && is_timeout_link)
4084 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004085 flags = READ_ONCE(sqe->timeout_flags);
4086 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004087 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004088
Jens Axboe26a61672019-12-20 09:02:01 -07004089 req->timeout.count = READ_ONCE(sqe->off);
4090
Jens Axboe3529d8c2019-12-19 18:24:38 -07004091 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004092 return -ENOMEM;
4093
4094 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004095 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004096 req->flags |= REQ_F_TIMEOUT;
4097
4098 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004099 return -EFAULT;
4100
Jens Axboe11365042019-10-16 09:08:32 -06004101 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004102 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004103 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004104 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004105
Jens Axboead8a48a2019-11-15 08:49:11 -07004106 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4107 return 0;
4108}
4109
Jens Axboefc4df992019-12-10 14:38:45 -07004110static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004111{
4112 unsigned count;
4113 struct io_ring_ctx *ctx = req->ctx;
4114 struct io_timeout_data *data;
4115 struct list_head *entry;
4116 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004117
Jens Axboe2d283902019-12-04 11:08:05 -07004118 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004119
Jens Axboe5262f562019-09-17 12:26:57 -06004120 /*
4121 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004122 * timeout event to be satisfied. If it isn't set, then this is
4123 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004124 */
Jens Axboe26a61672019-12-20 09:02:01 -07004125 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004126 if (!count) {
4127 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4128 spin_lock_irq(&ctx->completion_lock);
4129 entry = ctx->timeout_list.prev;
4130 goto add;
4131 }
Jens Axboe5262f562019-09-17 12:26:57 -06004132
4133 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004134 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004135
4136 /*
4137 * Insertion sort, ensuring the first entry in the list is always
4138 * the one we need first.
4139 */
Jens Axboe5262f562019-09-17 12:26:57 -06004140 spin_lock_irq(&ctx->completion_lock);
4141 list_for_each_prev(entry, &ctx->timeout_list) {
4142 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004143 unsigned nxt_sq_head;
4144 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004145 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004146
Jens Axboe93bd25b2019-11-11 23:34:31 -07004147 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4148 continue;
4149
yangerkun5da0fb12019-10-15 21:59:29 +08004150 /*
4151 * Since cached_sq_head + count - 1 can overflow, use type long
4152 * long to store it.
4153 */
4154 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004155 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4156 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004157
4158 /*
4159 * cached_sq_head may overflow, and it will never overflow twice
4160 * once there is some timeout req still be valid.
4161 */
4162 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004163 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004164
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004165 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004166 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004167
4168 /*
4169 * Sequence of reqs after the insert one and itself should
4170 * be adjusted because each timeout req consumes a slot.
4171 */
4172 span++;
4173 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004174 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004175 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004176add:
Jens Axboe5262f562019-09-17 12:26:57 -06004177 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004178 data->timer.function = io_timeout_fn;
4179 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004180 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004181 return 0;
4182}
4183
Jens Axboe62755e32019-10-28 21:49:21 -06004184static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004185{
Jens Axboe62755e32019-10-28 21:49:21 -06004186 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004187
Jens Axboe62755e32019-10-28 21:49:21 -06004188 return req->user_data == (unsigned long) data;
4189}
4190
Jens Axboee977d6d2019-11-05 12:39:45 -07004191static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004192{
Jens Axboe62755e32019-10-28 21:49:21 -06004193 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004194 int ret = 0;
4195
Jens Axboe62755e32019-10-28 21:49:21 -06004196 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4197 switch (cancel_ret) {
4198 case IO_WQ_CANCEL_OK:
4199 ret = 0;
4200 break;
4201 case IO_WQ_CANCEL_RUNNING:
4202 ret = -EALREADY;
4203 break;
4204 case IO_WQ_CANCEL_NOTFOUND:
4205 ret = -ENOENT;
4206 break;
4207 }
4208
Jens Axboee977d6d2019-11-05 12:39:45 -07004209 return ret;
4210}
4211
Jens Axboe47f46762019-11-09 17:43:02 -07004212static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4213 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004214 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004215{
4216 unsigned long flags;
4217 int ret;
4218
4219 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4220 if (ret != -ENOENT) {
4221 spin_lock_irqsave(&ctx->completion_lock, flags);
4222 goto done;
4223 }
4224
4225 spin_lock_irqsave(&ctx->completion_lock, flags);
4226 ret = io_timeout_cancel(ctx, sqe_addr);
4227 if (ret != -ENOENT)
4228 goto done;
4229 ret = io_poll_cancel(ctx, sqe_addr);
4230done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004231 if (!ret)
4232 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004233 io_cqring_fill_event(req, ret);
4234 io_commit_cqring(ctx);
4235 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4236 io_cqring_ev_posted(ctx);
4237
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004238 if (ret < 0)
4239 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004240 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004241}
4242
Jens Axboe3529d8c2019-12-19 18:24:38 -07004243static int io_async_cancel_prep(struct io_kiocb *req,
4244 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004245{
Jens Axboefbf23842019-12-17 18:45:56 -07004246 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004247 return -EINVAL;
4248 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4249 sqe->cancel_flags)
4250 return -EINVAL;
4251
Jens Axboefbf23842019-12-17 18:45:56 -07004252 req->cancel.addr = READ_ONCE(sqe->addr);
4253 return 0;
4254}
4255
Pavel Begunkov014db002020-03-03 21:33:12 +03004256static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004257{
4258 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004259
Pavel Begunkov014db002020-03-03 21:33:12 +03004260 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004261 return 0;
4262}
4263
Jens Axboe05f3fb32019-12-09 11:22:50 -07004264static int io_files_update_prep(struct io_kiocb *req,
4265 const struct io_uring_sqe *sqe)
4266{
4267 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4268 return -EINVAL;
4269
4270 req->files_update.offset = READ_ONCE(sqe->off);
4271 req->files_update.nr_args = READ_ONCE(sqe->len);
4272 if (!req->files_update.nr_args)
4273 return -EINVAL;
4274 req->files_update.arg = READ_ONCE(sqe->addr);
4275 return 0;
4276}
4277
4278static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4279{
4280 struct io_ring_ctx *ctx = req->ctx;
4281 struct io_uring_files_update up;
4282 int ret;
4283
Jens Axboef86cd202020-01-29 13:46:44 -07004284 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004285 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004286
4287 up.offset = req->files_update.offset;
4288 up.fds = req->files_update.arg;
4289
4290 mutex_lock(&ctx->uring_lock);
4291 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4292 mutex_unlock(&ctx->uring_lock);
4293
4294 if (ret < 0)
4295 req_set_fail_links(req);
4296 io_cqring_add_event(req, ret);
4297 io_put_req(req);
4298 return 0;
4299}
4300
Jens Axboe3529d8c2019-12-19 18:24:38 -07004301static int io_req_defer_prep(struct io_kiocb *req,
4302 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004303{
Jens Axboee7815732019-12-17 19:45:06 -07004304 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004305
Jens Axboef86cd202020-01-29 13:46:44 -07004306 if (io_op_defs[req->opcode].file_table) {
4307 ret = io_grab_files(req);
4308 if (unlikely(ret))
4309 return ret;
4310 }
4311
Jens Axboecccf0ee2020-01-27 16:34:48 -07004312 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4313
Jens Axboed625c6e2019-12-17 19:53:05 -07004314 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004315 case IORING_OP_NOP:
4316 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004317 case IORING_OP_READV:
4318 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004319 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004320 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004321 break;
4322 case IORING_OP_WRITEV:
4323 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004324 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004325 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004326 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004327 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004328 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004329 break;
4330 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004331 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004332 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004333 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004334 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004335 break;
4336 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004337 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004338 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004339 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004340 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004341 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004342 break;
4343 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004344 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004345 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004346 break;
Jens Axboef499a022019-12-02 16:28:46 -07004347 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004348 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004349 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004350 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004351 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004352 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004353 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004354 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004355 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004356 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004357 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004358 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004359 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004360 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004361 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004362 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004363 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004364 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004365 case IORING_OP_FALLOCATE:
4366 ret = io_fallocate_prep(req, sqe);
4367 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004368 case IORING_OP_OPENAT:
4369 ret = io_openat_prep(req, sqe);
4370 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004371 case IORING_OP_CLOSE:
4372 ret = io_close_prep(req, sqe);
4373 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004374 case IORING_OP_FILES_UPDATE:
4375 ret = io_files_update_prep(req, sqe);
4376 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004377 case IORING_OP_STATX:
4378 ret = io_statx_prep(req, sqe);
4379 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004380 case IORING_OP_FADVISE:
4381 ret = io_fadvise_prep(req, sqe);
4382 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004383 case IORING_OP_MADVISE:
4384 ret = io_madvise_prep(req, sqe);
4385 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004386 case IORING_OP_OPENAT2:
4387 ret = io_openat2_prep(req, sqe);
4388 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004389 case IORING_OP_EPOLL_CTL:
4390 ret = io_epoll_ctl_prep(req, sqe);
4391 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004392 case IORING_OP_SPLICE:
4393 ret = io_splice_prep(req, sqe);
4394 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004395 default:
Jens Axboee7815732019-12-17 19:45:06 -07004396 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4397 req->opcode);
4398 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004399 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004400 }
4401
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004402 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004403}
4404
Jens Axboe3529d8c2019-12-19 18:24:38 -07004405static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004406{
Jackie Liua197f662019-11-08 08:09:12 -07004407 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004408 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004409
Bob Liu9d858b22019-11-13 18:06:25 +08004410 /* Still need defer if there is pending req in defer list. */
4411 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004412 return 0;
4413
Jens Axboe3529d8c2019-12-19 18:24:38 -07004414 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004415 return -EAGAIN;
4416
Jens Axboe3529d8c2019-12-19 18:24:38 -07004417 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004418 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004419 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004420
Jens Axboede0617e2019-04-06 21:51:27 -06004421 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004422 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004423 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004424 return 0;
4425 }
4426
Jens Axboe915967f2019-11-21 09:01:20 -07004427 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004428 list_add_tail(&req->list, &ctx->defer_list);
4429 spin_unlock_irq(&ctx->completion_lock);
4430 return -EIOCBQUEUED;
4431}
4432
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004433static void io_cleanup_req(struct io_kiocb *req)
4434{
4435 struct io_async_ctx *io = req->io;
4436
4437 switch (req->opcode) {
4438 case IORING_OP_READV:
4439 case IORING_OP_READ_FIXED:
4440 case IORING_OP_READ:
4441 case IORING_OP_WRITEV:
4442 case IORING_OP_WRITE_FIXED:
4443 case IORING_OP_WRITE:
4444 if (io->rw.iov != io->rw.fast_iov)
4445 kfree(io->rw.iov);
4446 break;
4447 case IORING_OP_SENDMSG:
4448 case IORING_OP_RECVMSG:
4449 if (io->msg.iov != io->msg.fast_iov)
4450 kfree(io->msg.iov);
4451 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004452 case IORING_OP_OPENAT:
4453 case IORING_OP_OPENAT2:
4454 case IORING_OP_STATX:
4455 putname(req->open.filename);
4456 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004457 case IORING_OP_SPLICE:
4458 io_put_file(req, req->splice.file_in,
4459 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4460 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004461 }
4462
4463 req->flags &= ~REQ_F_NEED_CLEANUP;
4464}
4465
Jens Axboe3529d8c2019-12-19 18:24:38 -07004466static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03004467 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004468{
Jackie Liua197f662019-11-08 08:09:12 -07004469 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004470 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004471
Jens Axboed625c6e2019-12-17 19:53:05 -07004472 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004473 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004474 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004475 break;
4476 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004477 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004478 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004479 if (sqe) {
4480 ret = io_read_prep(req, sqe, force_nonblock);
4481 if (ret < 0)
4482 break;
4483 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004484 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004485 break;
4486 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004487 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004488 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004489 if (sqe) {
4490 ret = io_write_prep(req, sqe, force_nonblock);
4491 if (ret < 0)
4492 break;
4493 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004494 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004495 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004496 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004497 if (sqe) {
4498 ret = io_prep_fsync(req, sqe);
4499 if (ret < 0)
4500 break;
4501 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004502 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004503 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004504 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004505 if (sqe) {
4506 ret = io_poll_add_prep(req, sqe);
4507 if (ret)
4508 break;
4509 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004510 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004511 break;
4512 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004513 if (sqe) {
4514 ret = io_poll_remove_prep(req, sqe);
4515 if (ret < 0)
4516 break;
4517 }
Jens Axboefc4df992019-12-10 14:38:45 -07004518 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004519 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004520 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004521 if (sqe) {
4522 ret = io_prep_sfr(req, sqe);
4523 if (ret < 0)
4524 break;
4525 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004526 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004527 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004528 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004529 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004530 if (sqe) {
4531 ret = io_sendmsg_prep(req, sqe);
4532 if (ret < 0)
4533 break;
4534 }
Jens Axboefddafac2020-01-04 20:19:44 -07004535 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004536 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004537 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004538 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004539 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004540 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004541 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004542 if (sqe) {
4543 ret = io_recvmsg_prep(req, sqe);
4544 if (ret)
4545 break;
4546 }
Jens Axboefddafac2020-01-04 20:19:44 -07004547 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004548 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004549 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004550 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004551 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004552 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004553 if (sqe) {
4554 ret = io_timeout_prep(req, sqe, false);
4555 if (ret)
4556 break;
4557 }
Jens Axboefc4df992019-12-10 14:38:45 -07004558 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004559 break;
Jens Axboe11365042019-10-16 09:08:32 -06004560 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004561 if (sqe) {
4562 ret = io_timeout_remove_prep(req, sqe);
4563 if (ret)
4564 break;
4565 }
Jens Axboefc4df992019-12-10 14:38:45 -07004566 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004567 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004568 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004569 if (sqe) {
4570 ret = io_accept_prep(req, sqe);
4571 if (ret)
4572 break;
4573 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004574 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004575 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004576 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004577 if (sqe) {
4578 ret = io_connect_prep(req, sqe);
4579 if (ret)
4580 break;
4581 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004582 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004583 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004584 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004585 if (sqe) {
4586 ret = io_async_cancel_prep(req, sqe);
4587 if (ret)
4588 break;
4589 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004590 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06004591 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004592 case IORING_OP_FALLOCATE:
4593 if (sqe) {
4594 ret = io_fallocate_prep(req, sqe);
4595 if (ret)
4596 break;
4597 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004598 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07004599 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004600 case IORING_OP_OPENAT:
4601 if (sqe) {
4602 ret = io_openat_prep(req, sqe);
4603 if (ret)
4604 break;
4605 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004606 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004607 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004608 case IORING_OP_CLOSE:
4609 if (sqe) {
4610 ret = io_close_prep(req, sqe);
4611 if (ret)
4612 break;
4613 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004614 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07004615 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004616 case IORING_OP_FILES_UPDATE:
4617 if (sqe) {
4618 ret = io_files_update_prep(req, sqe);
4619 if (ret)
4620 break;
4621 }
4622 ret = io_files_update(req, force_nonblock);
4623 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004624 case IORING_OP_STATX:
4625 if (sqe) {
4626 ret = io_statx_prep(req, sqe);
4627 if (ret)
4628 break;
4629 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004630 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004631 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004632 case IORING_OP_FADVISE:
4633 if (sqe) {
4634 ret = io_fadvise_prep(req, sqe);
4635 if (ret)
4636 break;
4637 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004638 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07004639 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004640 case IORING_OP_MADVISE:
4641 if (sqe) {
4642 ret = io_madvise_prep(req, sqe);
4643 if (ret)
4644 break;
4645 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004646 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07004647 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004648 case IORING_OP_OPENAT2:
4649 if (sqe) {
4650 ret = io_openat2_prep(req, sqe);
4651 if (ret)
4652 break;
4653 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004654 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07004655 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004656 case IORING_OP_EPOLL_CTL:
4657 if (sqe) {
4658 ret = io_epoll_ctl_prep(req, sqe);
4659 if (ret)
4660 break;
4661 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004662 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004663 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004664 case IORING_OP_SPLICE:
4665 if (sqe) {
4666 ret = io_splice_prep(req, sqe);
4667 if (ret < 0)
4668 break;
4669 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004670 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004671 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004672 default:
4673 ret = -EINVAL;
4674 break;
4675 }
4676
Jens Axboedef596e2019-01-09 08:59:42 -07004677 if (ret)
4678 return ret;
4679
4680 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004681 const bool in_async = io_wq_current_is_worker();
4682
Jens Axboe9e645e112019-05-10 16:07:28 -06004683 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004684 return -EAGAIN;
4685
Jens Axboe11ba8202020-01-15 21:51:17 -07004686 /* workqueue context doesn't hold uring_lock, grab it now */
4687 if (in_async)
4688 mutex_lock(&ctx->uring_lock);
4689
Jens Axboedef596e2019-01-09 08:59:42 -07004690 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004691
4692 if (in_async)
4693 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004694 }
4695
4696 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004697}
4698
Jens Axboe561fb042019-10-24 07:25:42 -06004699static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004700{
Jens Axboe561fb042019-10-24 07:25:42 -06004701 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004702 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004703 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004704
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004705 /* if NO_CANCEL is set, we must still run the work */
4706 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4707 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004708 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004709 }
Jens Axboe31b51512019-01-18 22:56:34 -07004710
Jens Axboe561fb042019-10-24 07:25:42 -06004711 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06004712 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03004713 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004714 /*
4715 * We can get EAGAIN for polled IO even though we're
4716 * forcing a sync submission from here, since we can't
4717 * wait for request slots on the block side.
4718 */
4719 if (ret != -EAGAIN)
4720 break;
4721 cond_resched();
4722 } while (1);
4723 }
Jens Axboe31b51512019-01-18 22:56:34 -07004724
Jens Axboe561fb042019-10-24 07:25:42 -06004725 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004726 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004727 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004728 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004729 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004730
Pavel Begunkove9fd9392020-03-04 16:14:12 +03004731 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07004732}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004733
Jens Axboe15b71ab2019-12-11 11:20:36 -07004734static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004735{
Jens Axboed3656342019-12-18 09:50:26 -07004736 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004737 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004738 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004739 return 0;
4740 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004741}
4742
Jens Axboe65e19f52019-10-26 07:20:21 -06004743static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4744 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004745{
Jens Axboe65e19f52019-10-26 07:20:21 -06004746 struct fixed_file_table *table;
4747
Jens Axboe05f3fb32019-12-09 11:22:50 -07004748 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4749 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004750}
4751
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004752static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
4753 int fd, struct file **out_file, bool fixed)
4754{
4755 struct io_ring_ctx *ctx = req->ctx;
4756 struct file *file;
4757
4758 if (fixed) {
4759 if (unlikely(!ctx->file_data ||
4760 (unsigned) fd >= ctx->nr_user_files))
4761 return -EBADF;
4762 fd = array_index_nospec(fd, ctx->nr_user_files);
4763 file = io_file_from_index(ctx, fd);
4764 if (!file)
4765 return -EBADF;
4766 percpu_ref_get(&ctx->file_data->refs);
4767 } else {
4768 trace_io_uring_file_get(ctx, fd);
4769 file = __io_file_get(state, fd);
4770 if (unlikely(!file))
4771 return -EBADF;
4772 }
4773
4774 *out_file = file;
4775 return 0;
4776}
4777
Jens Axboe3529d8c2019-12-19 18:24:38 -07004778static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4779 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004780{
4781 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004782 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004783 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06004784
Jens Axboe3529d8c2019-12-19 18:24:38 -07004785 flags = READ_ONCE(sqe->flags);
4786 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004787
Jens Axboed3656342019-12-18 09:50:26 -07004788 if (!io_req_needs_file(req, fd))
4789 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004790
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004791 fixed = (flags & IOSQE_FIXED_FILE);
4792 if (unlikely(!fixed && req->needs_fixed_file))
4793 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004794
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004795 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06004796}
4797
Jackie Liua197f662019-11-08 08:09:12 -07004798static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004799{
Jens Axboefcb323c2019-10-24 12:39:47 -06004800 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004801 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004802
Jens Axboef86cd202020-01-29 13:46:44 -07004803 if (req->work.files)
4804 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004805 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004806 return -EBADF;
4807
Jens Axboefcb323c2019-10-24 12:39:47 -06004808 rcu_read_lock();
4809 spin_lock_irq(&ctx->inflight_lock);
4810 /*
4811 * We use the f_ops->flush() handler to ensure that we can flush
4812 * out work accessing these files if the fd is closed. Check if
4813 * the fd has changed since we started down this path, and disallow
4814 * this operation if it has.
4815 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004816 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004817 list_add(&req->inflight_entry, &ctx->inflight_list);
4818 req->flags |= REQ_F_INFLIGHT;
4819 req->work.files = current->files;
4820 ret = 0;
4821 }
4822 spin_unlock_irq(&ctx->inflight_lock);
4823 rcu_read_unlock();
4824
4825 return ret;
4826}
4827
Jens Axboe2665abf2019-11-05 12:40:47 -07004828static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4829{
Jens Axboead8a48a2019-11-15 08:49:11 -07004830 struct io_timeout_data *data = container_of(timer,
4831 struct io_timeout_data, timer);
4832 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004833 struct io_ring_ctx *ctx = req->ctx;
4834 struct io_kiocb *prev = NULL;
4835 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004836
4837 spin_lock_irqsave(&ctx->completion_lock, flags);
4838
4839 /*
4840 * We don't expect the list to be empty, that will only happen if we
4841 * race with the completion of the linked work.
4842 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004843 if (!list_empty(&req->link_list)) {
4844 prev = list_entry(req->link_list.prev, struct io_kiocb,
4845 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004846 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004847 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004848 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4849 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004850 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004851 }
4852
4853 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4854
4855 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004856 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03004857 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004858 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004859 } else {
4860 io_cqring_add_event(req, -ETIME);
4861 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004862 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004863 return HRTIMER_NORESTART;
4864}
4865
Jens Axboead8a48a2019-11-15 08:49:11 -07004866static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004867{
Jens Axboe76a46e02019-11-10 23:34:16 -07004868 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004869
Jens Axboe76a46e02019-11-10 23:34:16 -07004870 /*
4871 * If the list is now empty, then our linked request finished before
4872 * we got a chance to setup the timer
4873 */
4874 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004875 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004876 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004877
Jens Axboead8a48a2019-11-15 08:49:11 -07004878 data->timer.function = io_link_timeout_fn;
4879 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4880 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004881 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004882 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004883
Jens Axboe2665abf2019-11-05 12:40:47 -07004884 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004885 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004886}
4887
Jens Axboead8a48a2019-11-15 08:49:11 -07004888static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004889{
4890 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004891
Jens Axboe2665abf2019-11-05 12:40:47 -07004892 if (!(req->flags & REQ_F_LINK))
4893 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004894 /* for polled retry, if flag is set, we already went through here */
4895 if (req->flags & REQ_F_POLLED)
4896 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004897
Pavel Begunkov44932332019-12-05 16:16:35 +03004898 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4899 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004900 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004901 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004902
Jens Axboe76a46e02019-11-10 23:34:16 -07004903 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004904 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004905}
4906
Jens Axboe3529d8c2019-12-19 18:24:38 -07004907static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004908{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004909 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004910 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07004911 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004912 int ret;
4913
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004914again:
4915 linked_timeout = io_prep_linked_timeout(req);
4916
Jens Axboe193155c2020-02-22 23:22:19 -07004917 if (req->work.creds && req->work.creds != current_cred()) {
4918 if (old_creds)
4919 revert_creds(old_creds);
4920 if (old_creds == req->work.creds)
4921 old_creds = NULL; /* restored original creds */
4922 else
4923 old_creds = override_creds(req->work.creds);
4924 }
4925
Pavel Begunkov014db002020-03-03 21:33:12 +03004926 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004927
4928 /*
4929 * We async punt it if the file wasn't marked NOWAIT, or if the file
4930 * doesn't support non-blocking read/write attempts
4931 */
4932 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4933 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07004934 if (io_arm_poll_handler(req)) {
4935 if (linked_timeout)
4936 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004937 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07004938 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004939punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004940 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004941 ret = io_grab_files(req);
4942 if (ret)
4943 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004944 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004945
4946 /*
4947 * Queued up for async execution, worker will release
4948 * submit reference when the iocb is actually submitted.
4949 */
4950 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004951 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004952 }
Jens Axboee65ef562019-03-12 10:16:44 -06004953
Jens Axboefcb323c2019-10-24 12:39:47 -06004954err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004955 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06004956 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004957 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004958
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004959 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004960 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004961 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004962 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004963 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004964 }
4965
Jens Axboee65ef562019-03-12 10:16:44 -06004966 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004967 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004968 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004969 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004970 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004971 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004972 if (nxt) {
4973 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004974
4975 if (req->flags & REQ_F_FORCE_ASYNC)
4976 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004977 goto again;
4978 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03004979exit:
Jens Axboe193155c2020-02-22 23:22:19 -07004980 if (old_creds)
4981 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004982}
4983
Jens Axboe3529d8c2019-12-19 18:24:38 -07004984static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004985{
4986 int ret;
4987
Jens Axboe3529d8c2019-12-19 18:24:38 -07004988 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004989 if (ret) {
4990 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004991fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004992 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004993 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004994 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004995 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004996 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004997 ret = io_req_defer_prep(req, sqe);
4998 if (unlikely(ret < 0))
4999 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005000 /*
5001 * Never try inline submit of IOSQE_ASYNC is set, go straight
5002 * to async execution.
5003 */
5004 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5005 io_queue_async_work(req);
5006 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005007 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005008 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005009}
5010
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005011static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005012{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005013 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005014 io_cqring_add_event(req, -ECANCELED);
5015 io_double_put_req(req);
5016 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005017 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005018}
5019
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005020#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07005021 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06005022
Jens Axboe3529d8c2019-12-19 18:24:38 -07005023static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5024 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005025{
Jackie Liua197f662019-11-08 08:09:12 -07005026 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005027 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005028 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005029
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005030 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005031
5032 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005033 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005034 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005035 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005036 }
5037
Jens Axboe75c6a032020-01-28 10:15:23 -07005038 id = READ_ONCE(sqe->personality);
5039 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005040 req->work.creds = idr_find(&ctx->personality_idr, id);
5041 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005042 ret = -EINVAL;
5043 goto err_req;
5044 }
Jens Axboe193155c2020-02-22 23:22:19 -07005045 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005046 }
5047
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005048 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005049 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
5050 IOSQE_ASYNC | IOSQE_FIXED_FILE);
Jens Axboe9e645e112019-05-10 16:07:28 -06005051
Jens Axboe3529d8c2019-12-19 18:24:38 -07005052 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005053 if (unlikely(ret)) {
5054err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005055 io_cqring_add_event(req, ret);
5056 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005057 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005058 }
5059
Jens Axboe9e645e112019-05-10 16:07:28 -06005060 /*
5061 * If we already have a head request, queue this one for async
5062 * submittal once the head completes. If we don't have a head but
5063 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5064 * submitted sync once the chain is complete. If none of those
5065 * conditions are true (normal request), then just queue it.
5066 */
5067 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005068 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005069
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005070 /*
5071 * Taking sequential execution of a link, draining both sides
5072 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5073 * requests in the link. So, it drains the head and the
5074 * next after the link request. The last one is done via
5075 * drain_next flag to persist the effect across calls.
5076 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005077 if (sqe_flags & IOSQE_IO_DRAIN) {
5078 head->flags |= REQ_F_IO_DRAIN;
5079 ctx->drain_next = 1;
5080 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005081 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005082 ret = -EAGAIN;
5083 goto err_req;
5084 }
5085
Jens Axboe3529d8c2019-12-19 18:24:38 -07005086 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005087 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005088 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005089 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005090 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005091 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005092 trace_io_uring_link(ctx, req, head);
5093 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005094
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005095 /* last request of a link, enqueue the link */
5096 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5097 io_queue_link_head(head);
5098 *link = NULL;
5099 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005100 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005101 if (unlikely(ctx->drain_next)) {
5102 req->flags |= REQ_F_IO_DRAIN;
5103 req->ctx->drain_next = 0;
5104 }
5105 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5106 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005107 INIT_LIST_HEAD(&req->link_list);
5108 ret = io_req_defer_prep(req, sqe);
5109 if (ret)
5110 req->flags |= REQ_F_FAIL_LINK;
5111 *link = req;
5112 } else {
5113 io_queue_sqe(req, sqe);
5114 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005115 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005116
5117 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005118}
5119
Jens Axboe9a56a232019-01-09 09:06:50 -07005120/*
5121 * Batched submission is done, ensure local IO is flushed out.
5122 */
5123static void io_submit_state_end(struct io_submit_state *state)
5124{
5125 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005126 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005127 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005128 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005129}
5130
5131/*
5132 * Start submission side cache.
5133 */
5134static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005135 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005136{
5137 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005138 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005139 state->file = NULL;
5140 state->ios_left = max_ios;
5141}
5142
Jens Axboe2b188cc2019-01-07 10:46:33 -07005143static void io_commit_sqring(struct io_ring_ctx *ctx)
5144{
Hristo Venev75b28af2019-08-26 17:23:46 +00005145 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005146
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005147 /*
5148 * Ensure any loads from the SQEs are done at this point,
5149 * since once we write the new head, the application could
5150 * write new data to them.
5151 */
5152 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005153}
5154
5155/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005156 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005157 * that is mapped by userspace. This means that care needs to be taken to
5158 * ensure that reads are stable, as we cannot rely on userspace always
5159 * being a good citizen. If members of the sqe are validated and then later
5160 * used, it's important that those reads are done through READ_ONCE() to
5161 * prevent a re-load down the line.
5162 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005163static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5164 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005165{
Hristo Venev75b28af2019-08-26 17:23:46 +00005166 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005167 unsigned head;
5168
5169 /*
5170 * The cached sq head (or cq tail) serves two purposes:
5171 *
5172 * 1) allows us to batch the cost of updating the user visible
5173 * head updates.
5174 * 2) allows the kernel side to track the head on its own, even
5175 * though the application is the one updating it.
5176 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005177 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005178 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005179 /*
5180 * All io need record the previous position, if LINK vs DARIN,
5181 * it can be used to mark the position of the first IO in the
5182 * link list.
5183 */
5184 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005185 *sqe_ptr = &ctx->sq_sqes[head];
5186 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5187 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005188 ctx->cached_sq_head++;
5189 return true;
5190 }
5191
5192 /* drop invalid entries */
5193 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005194 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005195 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005196 return false;
5197}
5198
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005199static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005200 struct file *ring_file, int ring_fd,
5201 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005202{
5203 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005204 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005205 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005206 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005207
Jens Axboec4a2ed72019-11-21 21:01:26 -07005208 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005209 if (test_bit(0, &ctx->sq_check_overflow)) {
5210 if (!list_empty(&ctx->cq_overflow_list) &&
5211 !io_cqring_overflow_flush(ctx, false))
5212 return -EBUSY;
5213 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005214
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005215 /* make sure SQ entry isn't read before tail */
5216 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005217
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005218 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5219 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005220
5221 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005222 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005223 statep = &state;
5224 }
5225
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005226 ctx->ring_fd = ring_fd;
5227 ctx->ring_file = ring_file;
5228
Jens Axboe6c271ce2019-01-10 11:22:30 -07005229 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005230 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005231 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005232 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005233
Pavel Begunkov196be952019-11-07 01:41:06 +03005234 req = io_get_req(ctx, statep);
5235 if (unlikely(!req)) {
5236 if (!submitted)
5237 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005238 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005239 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005240 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005241 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005242 break;
5243 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005244
Jens Axboed3656342019-12-18 09:50:26 -07005245 /* will complete beyond this point, count as submitted */
5246 submitted++;
5247
5248 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005249 err = -EINVAL;
5250fail_req:
5251 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005252 io_double_put_req(req);
5253 break;
5254 }
5255
5256 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005257 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005258 if (unlikely(mm_fault)) {
5259 err = -EFAULT;
5260 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005261 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005262 use_mm(ctx->sqo_mm);
5263 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005264 }
5265
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005266 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005267 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5268 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005269 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005270 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005271 }
5272
Pavel Begunkov9466f432020-01-25 22:34:01 +03005273 if (unlikely(submitted != nr)) {
5274 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5275
5276 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5277 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005278 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005279 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005280 if (statep)
5281 io_submit_state_end(&state);
5282
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005283 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5284 io_commit_sqring(ctx);
5285
Jens Axboe6c271ce2019-01-10 11:22:30 -07005286 return submitted;
5287}
5288
5289static int io_sq_thread(void *data)
5290{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005291 struct io_ring_ctx *ctx = data;
5292 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005293 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005294 mm_segment_t old_fs;
5295 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005296 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005297 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005298
Jens Axboe206aefd2019-11-07 18:27:42 -07005299 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005300
Jens Axboe6c271ce2019-01-10 11:22:30 -07005301 old_fs = get_fs();
5302 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005303 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005304
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005305 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005306 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005307 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005308
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005309 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005310 unsigned nr_events = 0;
5311
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005312 mutex_lock(&ctx->uring_lock);
5313 if (!list_empty(&ctx->poll_list))
5314 io_iopoll_getevents(ctx, &nr_events, 0);
5315 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005316 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005317 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005318 }
5319
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005320 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005321
5322 /*
5323 * If submit got -EBUSY, flag us as needing the application
5324 * to enter the kernel to reap and flush events.
5325 */
5326 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005327 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005328 * Drop cur_mm before scheduling, we can't hold it for
5329 * long periods (or over schedule()). Do this before
5330 * adding ourselves to the waitqueue, as the unuse/drop
5331 * may sleep.
5332 */
5333 if (cur_mm) {
5334 unuse_mm(cur_mm);
5335 mmput(cur_mm);
5336 cur_mm = NULL;
5337 }
5338
5339 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005340 * We're polling. If we're within the defined idle
5341 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005342 * to sleep. The exception is if we got EBUSY doing
5343 * more IO, we should wait for the application to
5344 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005345 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005346 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005347 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5348 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005349 if (current->task_works)
5350 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005351 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005352 continue;
5353 }
5354
Jens Axboe6c271ce2019-01-10 11:22:30 -07005355 prepare_to_wait(&ctx->sqo_wait, &wait,
5356 TASK_INTERRUPTIBLE);
5357
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005358 /*
5359 * While doing polled IO, before going to sleep, we need
5360 * to check if there are new reqs added to poll_list, it
5361 * is because reqs may have been punted to io worker and
5362 * will be added to poll_list later, hence check the
5363 * poll_list again.
5364 */
5365 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5366 !list_empty_careful(&ctx->poll_list)) {
5367 finish_wait(&ctx->sqo_wait, &wait);
5368 continue;
5369 }
5370
Jens Axboe6c271ce2019-01-10 11:22:30 -07005371 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005372 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005373 /* make sure to read SQ tail after writing flags */
5374 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005375
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005376 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005377 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005378 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005379 finish_wait(&ctx->sqo_wait, &wait);
5380 break;
5381 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005382 if (current->task_works) {
5383 task_work_run();
5384 continue;
5385 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005386 if (signal_pending(current))
5387 flush_signals(current);
5388 schedule();
5389 finish_wait(&ctx->sqo_wait, &wait);
5390
Hristo Venev75b28af2019-08-26 17:23:46 +00005391 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005392 continue;
5393 }
5394 finish_wait(&ctx->sqo_wait, &wait);
5395
Hristo Venev75b28af2019-08-26 17:23:46 +00005396 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005397 }
5398
Jens Axboe8a4955f2019-12-09 14:52:35 -07005399 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005400 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005401 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005402 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005403 }
5404
Jens Axboeb41e9852020-02-17 09:52:41 -07005405 if (current->task_works)
5406 task_work_run();
5407
Jens Axboe6c271ce2019-01-10 11:22:30 -07005408 set_fs(old_fs);
5409 if (cur_mm) {
5410 unuse_mm(cur_mm);
5411 mmput(cur_mm);
5412 }
Jens Axboe181e4482019-11-25 08:52:30 -07005413 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005414
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005415 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005416
Jens Axboe6c271ce2019-01-10 11:22:30 -07005417 return 0;
5418}
5419
Jens Axboebda52162019-09-24 13:47:15 -06005420struct io_wait_queue {
5421 struct wait_queue_entry wq;
5422 struct io_ring_ctx *ctx;
5423 unsigned to_wait;
5424 unsigned nr_timeouts;
5425};
5426
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005427static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005428{
5429 struct io_ring_ctx *ctx = iowq->ctx;
5430
5431 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005432 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005433 * started waiting. For timeouts, we always want to return to userspace,
5434 * regardless of event count.
5435 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005436 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005437 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5438}
5439
5440static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5441 int wake_flags, void *key)
5442{
5443 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5444 wq);
5445
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005446 /* use noflush == true, as we can't safely rely on locking context */
5447 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005448 return -1;
5449
5450 return autoremove_wake_function(curr, mode, wake_flags, key);
5451}
5452
Jens Axboe2b188cc2019-01-07 10:46:33 -07005453/*
5454 * Wait until events become available, if we don't already have some. The
5455 * application must reap them itself, as they reside on the shared cq ring.
5456 */
5457static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5458 const sigset_t __user *sig, size_t sigsz)
5459{
Jens Axboebda52162019-09-24 13:47:15 -06005460 struct io_wait_queue iowq = {
5461 .wq = {
5462 .private = current,
5463 .func = io_wake_function,
5464 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5465 },
5466 .ctx = ctx,
5467 .to_wait = min_events,
5468 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005469 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005470 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005471
Jens Axboeb41e9852020-02-17 09:52:41 -07005472 do {
5473 if (io_cqring_events(ctx, false) >= min_events)
5474 return 0;
5475 if (!current->task_works)
5476 break;
5477 task_work_run();
5478 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005479
5480 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005481#ifdef CONFIG_COMPAT
5482 if (in_compat_syscall())
5483 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005484 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005485 else
5486#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005487 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005488
Jens Axboe2b188cc2019-01-07 10:46:33 -07005489 if (ret)
5490 return ret;
5491 }
5492
Jens Axboebda52162019-09-24 13:47:15 -06005493 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005494 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005495 do {
5496 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5497 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005498 if (current->task_works)
5499 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005500 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005501 break;
5502 schedule();
5503 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005504 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005505 break;
5506 }
5507 } while (1);
5508 finish_wait(&ctx->wait, &iowq.wq);
5509
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005510 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005511
Hristo Venev75b28af2019-08-26 17:23:46 +00005512 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005513}
5514
Jens Axboe6b063142019-01-10 22:13:58 -07005515static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5516{
5517#if defined(CONFIG_UNIX)
5518 if (ctx->ring_sock) {
5519 struct sock *sock = ctx->ring_sock->sk;
5520 struct sk_buff *skb;
5521
5522 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5523 kfree_skb(skb);
5524 }
5525#else
5526 int i;
5527
Jens Axboe65e19f52019-10-26 07:20:21 -06005528 for (i = 0; i < ctx->nr_user_files; i++) {
5529 struct file *file;
5530
5531 file = io_file_from_index(ctx, i);
5532 if (file)
5533 fput(file);
5534 }
Jens Axboe6b063142019-01-10 22:13:58 -07005535#endif
5536}
5537
Jens Axboe05f3fb32019-12-09 11:22:50 -07005538static void io_file_ref_kill(struct percpu_ref *ref)
5539{
5540 struct fixed_file_data *data;
5541
5542 data = container_of(ref, struct fixed_file_data, refs);
5543 complete(&data->done);
5544}
5545
Jens Axboe6b063142019-01-10 22:13:58 -07005546static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5547{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005548 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005549 unsigned nr_tables, i;
5550
Jens Axboe05f3fb32019-12-09 11:22:50 -07005551 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005552 return -ENXIO;
5553
Jens Axboe05f3fb32019-12-09 11:22:50 -07005554 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005555 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005556 wait_for_completion(&data->done);
5557 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005558 percpu_ref_exit(&data->refs);
5559
Jens Axboe6b063142019-01-10 22:13:58 -07005560 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005561 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5562 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005563 kfree(data->table[i].files);
5564 kfree(data->table);
5565 kfree(data);
5566 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005567 ctx->nr_user_files = 0;
5568 return 0;
5569}
5570
Jens Axboe6c271ce2019-01-10 11:22:30 -07005571static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5572{
5573 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005574 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005575 /*
5576 * The park is a bit of a work-around, without it we get
5577 * warning spews on shutdown with SQPOLL set and affinity
5578 * set to a single CPU.
5579 */
Jens Axboe06058632019-04-13 09:26:03 -06005580 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005581 kthread_stop(ctx->sqo_thread);
5582 ctx->sqo_thread = NULL;
5583 }
5584}
5585
Jens Axboe6b063142019-01-10 22:13:58 -07005586static void io_finish_async(struct io_ring_ctx *ctx)
5587{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005588 io_sq_thread_stop(ctx);
5589
Jens Axboe561fb042019-10-24 07:25:42 -06005590 if (ctx->io_wq) {
5591 io_wq_destroy(ctx->io_wq);
5592 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005593 }
5594}
5595
5596#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005597/*
5598 * Ensure the UNIX gc is aware of our file set, so we are certain that
5599 * the io_uring can be safely unregistered on process exit, even if we have
5600 * loops in the file referencing.
5601 */
5602static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5603{
5604 struct sock *sk = ctx->ring_sock->sk;
5605 struct scm_fp_list *fpl;
5606 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005607 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005608
5609 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5610 unsigned long inflight = ctx->user->unix_inflight + nr;
5611
5612 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5613 return -EMFILE;
5614 }
5615
5616 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5617 if (!fpl)
5618 return -ENOMEM;
5619
5620 skb = alloc_skb(0, GFP_KERNEL);
5621 if (!skb) {
5622 kfree(fpl);
5623 return -ENOMEM;
5624 }
5625
5626 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005627
Jens Axboe08a45172019-10-03 08:11:03 -06005628 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005629 fpl->user = get_uid(ctx->user);
5630 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005631 struct file *file = io_file_from_index(ctx, i + offset);
5632
5633 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005634 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005635 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005636 unix_inflight(fpl->user, fpl->fp[nr_files]);
5637 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005638 }
5639
Jens Axboe08a45172019-10-03 08:11:03 -06005640 if (nr_files) {
5641 fpl->max = SCM_MAX_FD;
5642 fpl->count = nr_files;
5643 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005644 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005645 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5646 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005647
Jens Axboe08a45172019-10-03 08:11:03 -06005648 for (i = 0; i < nr_files; i++)
5649 fput(fpl->fp[i]);
5650 } else {
5651 kfree_skb(skb);
5652 kfree(fpl);
5653 }
Jens Axboe6b063142019-01-10 22:13:58 -07005654
5655 return 0;
5656}
5657
5658/*
5659 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5660 * causes regular reference counting to break down. We rely on the UNIX
5661 * garbage collection to take care of this problem for us.
5662 */
5663static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5664{
5665 unsigned left, total;
5666 int ret = 0;
5667
5668 total = 0;
5669 left = ctx->nr_user_files;
5670 while (left) {
5671 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005672
5673 ret = __io_sqe_files_scm(ctx, this_files, total);
5674 if (ret)
5675 break;
5676 left -= this_files;
5677 total += this_files;
5678 }
5679
5680 if (!ret)
5681 return 0;
5682
5683 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005684 struct file *file = io_file_from_index(ctx, total);
5685
5686 if (file)
5687 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005688 total++;
5689 }
5690
5691 return ret;
5692}
5693#else
5694static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5695{
5696 return 0;
5697}
5698#endif
5699
Jens Axboe65e19f52019-10-26 07:20:21 -06005700static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5701 unsigned nr_files)
5702{
5703 int i;
5704
5705 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005706 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005707 unsigned this_files;
5708
5709 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5710 table->files = kcalloc(this_files, sizeof(struct file *),
5711 GFP_KERNEL);
5712 if (!table->files)
5713 break;
5714 nr_files -= this_files;
5715 }
5716
5717 if (i == nr_tables)
5718 return 0;
5719
5720 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005721 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005722 kfree(table->files);
5723 }
5724 return 1;
5725}
5726
Jens Axboe05f3fb32019-12-09 11:22:50 -07005727static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005728{
5729#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005730 struct sock *sock = ctx->ring_sock->sk;
5731 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5732 struct sk_buff *skb;
5733 int i;
5734
5735 __skb_queue_head_init(&list);
5736
5737 /*
5738 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5739 * remove this entry and rearrange the file array.
5740 */
5741 skb = skb_dequeue(head);
5742 while (skb) {
5743 struct scm_fp_list *fp;
5744
5745 fp = UNIXCB(skb).fp;
5746 for (i = 0; i < fp->count; i++) {
5747 int left;
5748
5749 if (fp->fp[i] != file)
5750 continue;
5751
5752 unix_notinflight(fp->user, fp->fp[i]);
5753 left = fp->count - 1 - i;
5754 if (left) {
5755 memmove(&fp->fp[i], &fp->fp[i + 1],
5756 left * sizeof(struct file *));
5757 }
5758 fp->count--;
5759 if (!fp->count) {
5760 kfree_skb(skb);
5761 skb = NULL;
5762 } else {
5763 __skb_queue_tail(&list, skb);
5764 }
5765 fput(file);
5766 file = NULL;
5767 break;
5768 }
5769
5770 if (!file)
5771 break;
5772
5773 __skb_queue_tail(&list, skb);
5774
5775 skb = skb_dequeue(head);
5776 }
5777
5778 if (skb_peek(&list)) {
5779 spin_lock_irq(&head->lock);
5780 while ((skb = __skb_dequeue(&list)) != NULL)
5781 __skb_queue_tail(head, skb);
5782 spin_unlock_irq(&head->lock);
5783 }
5784#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005785 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005786#endif
5787}
5788
Jens Axboe05f3fb32019-12-09 11:22:50 -07005789struct io_file_put {
5790 struct llist_node llist;
5791 struct file *file;
5792 struct completion *done;
5793};
5794
Jens Axboe2faf8522020-02-04 19:54:55 -07005795static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005796{
5797 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005798 struct llist_node *node;
5799
Jens Axboe05f3fb32019-12-09 11:22:50 -07005800 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5801 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5802 io_ring_file_put(data->ctx, pfile->file);
5803 if (pfile->done)
5804 complete(pfile->done);
5805 else
5806 kfree(pfile);
5807 }
5808 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005809}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005810
Jens Axboe2faf8522020-02-04 19:54:55 -07005811static void io_ring_file_ref_switch(struct work_struct *work)
5812{
5813 struct fixed_file_data *data;
5814
5815 data = container_of(work, struct fixed_file_data, ref_work);
5816 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005817 percpu_ref_switch_to_percpu(&data->refs);
5818}
5819
5820static void io_file_data_ref_zero(struct percpu_ref *ref)
5821{
5822 struct fixed_file_data *data;
5823
5824 data = container_of(ref, struct fixed_file_data, refs);
5825
Jens Axboe2faf8522020-02-04 19:54:55 -07005826 /*
5827 * We can't safely switch from inside this context, punt to wq. If
5828 * the table ref is going away, the table is being unregistered.
5829 * Don't queue up the async work for that case, the caller will
5830 * handle it.
5831 */
5832 if (!percpu_ref_is_dying(&data->refs))
5833 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005834}
5835
5836static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5837 unsigned nr_args)
5838{
5839 __s32 __user *fds = (__s32 __user *) arg;
5840 unsigned nr_tables;
5841 struct file *file;
5842 int fd, ret = 0;
5843 unsigned i;
5844
5845 if (ctx->file_data)
5846 return -EBUSY;
5847 if (!nr_args)
5848 return -EINVAL;
5849 if (nr_args > IORING_MAX_FIXED_FILES)
5850 return -EMFILE;
5851
5852 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5853 if (!ctx->file_data)
5854 return -ENOMEM;
5855 ctx->file_data->ctx = ctx;
5856 init_completion(&ctx->file_data->done);
5857
5858 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5859 ctx->file_data->table = kcalloc(nr_tables,
5860 sizeof(struct fixed_file_table),
5861 GFP_KERNEL);
5862 if (!ctx->file_data->table) {
5863 kfree(ctx->file_data);
5864 ctx->file_data = NULL;
5865 return -ENOMEM;
5866 }
5867
5868 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5869 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5870 kfree(ctx->file_data->table);
5871 kfree(ctx->file_data);
5872 ctx->file_data = NULL;
5873 return -ENOMEM;
5874 }
5875 ctx->file_data->put_llist.first = NULL;
5876 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5877
5878 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5879 percpu_ref_exit(&ctx->file_data->refs);
5880 kfree(ctx->file_data->table);
5881 kfree(ctx->file_data);
5882 ctx->file_data = NULL;
5883 return -ENOMEM;
5884 }
5885
5886 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5887 struct fixed_file_table *table;
5888 unsigned index;
5889
5890 ret = -EFAULT;
5891 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5892 break;
5893 /* allow sparse sets */
5894 if (fd == -1) {
5895 ret = 0;
5896 continue;
5897 }
5898
5899 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5900 index = i & IORING_FILE_TABLE_MASK;
5901 file = fget(fd);
5902
5903 ret = -EBADF;
5904 if (!file)
5905 break;
5906
5907 /*
5908 * Don't allow io_uring instances to be registered. If UNIX
5909 * isn't enabled, then this causes a reference cycle and this
5910 * instance can never get freed. If UNIX is enabled we'll
5911 * handle it just fine, but there's still no point in allowing
5912 * a ring fd as it doesn't support regular read/write anyway.
5913 */
5914 if (file->f_op == &io_uring_fops) {
5915 fput(file);
5916 break;
5917 }
5918 ret = 0;
5919 table->files[index] = file;
5920 }
5921
5922 if (ret) {
5923 for (i = 0; i < ctx->nr_user_files; i++) {
5924 file = io_file_from_index(ctx, i);
5925 if (file)
5926 fput(file);
5927 }
5928 for (i = 0; i < nr_tables; i++)
5929 kfree(ctx->file_data->table[i].files);
5930
5931 kfree(ctx->file_data->table);
5932 kfree(ctx->file_data);
5933 ctx->file_data = NULL;
5934 ctx->nr_user_files = 0;
5935 return ret;
5936 }
5937
5938 ret = io_sqe_files_scm(ctx);
5939 if (ret)
5940 io_sqe_files_unregister(ctx);
5941
5942 return ret;
5943}
5944
Jens Axboec3a31e62019-10-03 13:59:56 -06005945static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5946 int index)
5947{
5948#if defined(CONFIG_UNIX)
5949 struct sock *sock = ctx->ring_sock->sk;
5950 struct sk_buff_head *head = &sock->sk_receive_queue;
5951 struct sk_buff *skb;
5952
5953 /*
5954 * See if we can merge this file into an existing skb SCM_RIGHTS
5955 * file set. If there's no room, fall back to allocating a new skb
5956 * and filling it in.
5957 */
5958 spin_lock_irq(&head->lock);
5959 skb = skb_peek(head);
5960 if (skb) {
5961 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5962
5963 if (fpl->count < SCM_MAX_FD) {
5964 __skb_unlink(skb, head);
5965 spin_unlock_irq(&head->lock);
5966 fpl->fp[fpl->count] = get_file(file);
5967 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5968 fpl->count++;
5969 spin_lock_irq(&head->lock);
5970 __skb_queue_head(head, skb);
5971 } else {
5972 skb = NULL;
5973 }
5974 }
5975 spin_unlock_irq(&head->lock);
5976
5977 if (skb) {
5978 fput(file);
5979 return 0;
5980 }
5981
5982 return __io_sqe_files_scm(ctx, 1, index);
5983#else
5984 return 0;
5985#endif
5986}
5987
Jens Axboe05f3fb32019-12-09 11:22:50 -07005988static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005989{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005990 struct fixed_file_data *data;
5991
Jens Axboedd3db2a2020-02-26 10:23:43 -07005992 /*
5993 * Juggle reference to ensure we hit zero, if needed, so we can
5994 * switch back to percpu mode
5995 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005996 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005997 percpu_ref_put(&data->refs);
5998 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005999}
6000
6001static bool io_queue_file_removal(struct fixed_file_data *data,
6002 struct file *file)
6003{
6004 struct io_file_put *pfile, pfile_stack;
6005 DECLARE_COMPLETION_ONSTACK(done);
6006
6007 /*
6008 * If we fail allocating the struct we need for doing async reomval
6009 * of this file, just punt to sync and wait for it.
6010 */
6011 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
6012 if (!pfile) {
6013 pfile = &pfile_stack;
6014 pfile->done = &done;
6015 }
6016
6017 pfile->file = file;
6018 llist_add(&pfile->llist, &data->put_llist);
6019
6020 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07006021 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006022 wait_for_completion(&done);
6023 flush_work(&data->ref_work);
6024 return false;
6025 }
6026
6027 return true;
6028}
6029
6030static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6031 struct io_uring_files_update *up,
6032 unsigned nr_args)
6033{
6034 struct fixed_file_data *data = ctx->file_data;
6035 bool ref_switch = false;
6036 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006037 __s32 __user *fds;
6038 int fd, i, err;
6039 __u32 done;
6040
Jens Axboe05f3fb32019-12-09 11:22:50 -07006041 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006042 return -EOVERFLOW;
6043 if (done > ctx->nr_user_files)
6044 return -EINVAL;
6045
6046 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006047 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006048 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006049 struct fixed_file_table *table;
6050 unsigned index;
6051
Jens Axboec3a31e62019-10-03 13:59:56 -06006052 err = 0;
6053 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6054 err = -EFAULT;
6055 break;
6056 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006057 i = array_index_nospec(up->offset, ctx->nr_user_files);
6058 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006059 index = i & IORING_FILE_TABLE_MASK;
6060 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006061 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006062 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006063 if (io_queue_file_removal(data, file))
6064 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006065 }
6066 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006067 file = fget(fd);
6068 if (!file) {
6069 err = -EBADF;
6070 break;
6071 }
6072 /*
6073 * Don't allow io_uring instances to be registered. If
6074 * UNIX isn't enabled, then this causes a reference
6075 * cycle and this instance can never get freed. If UNIX
6076 * is enabled we'll handle it just fine, but there's
6077 * still no point in allowing a ring fd as it doesn't
6078 * support regular read/write anyway.
6079 */
6080 if (file->f_op == &io_uring_fops) {
6081 fput(file);
6082 err = -EBADF;
6083 break;
6084 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006085 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006086 err = io_sqe_file_register(ctx, file, i);
6087 if (err)
6088 break;
6089 }
6090 nr_args--;
6091 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006092 up->offset++;
6093 }
6094
Jens Axboedd3db2a2020-02-26 10:23:43 -07006095 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006096 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006097
6098 return done ? done : err;
6099}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006100static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6101 unsigned nr_args)
6102{
6103 struct io_uring_files_update up;
6104
6105 if (!ctx->file_data)
6106 return -ENXIO;
6107 if (!nr_args)
6108 return -EINVAL;
6109 if (copy_from_user(&up, arg, sizeof(up)))
6110 return -EFAULT;
6111 if (up.resv)
6112 return -EINVAL;
6113
6114 return __io_sqe_files_update(ctx, &up, nr_args);
6115}
Jens Axboec3a31e62019-10-03 13:59:56 -06006116
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006117static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006118{
6119 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6120
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006121 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006122 io_put_req(req);
6123}
6124
Pavel Begunkov24369c22020-01-28 03:15:48 +03006125static int io_init_wq_offload(struct io_ring_ctx *ctx,
6126 struct io_uring_params *p)
6127{
6128 struct io_wq_data data;
6129 struct fd f;
6130 struct io_ring_ctx *ctx_attach;
6131 unsigned int concurrency;
6132 int ret = 0;
6133
6134 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006135 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006136
6137 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6138 /* Do QD, or 4 * CPUS, whatever is smallest */
6139 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6140
6141 ctx->io_wq = io_wq_create(concurrency, &data);
6142 if (IS_ERR(ctx->io_wq)) {
6143 ret = PTR_ERR(ctx->io_wq);
6144 ctx->io_wq = NULL;
6145 }
6146 return ret;
6147 }
6148
6149 f = fdget(p->wq_fd);
6150 if (!f.file)
6151 return -EBADF;
6152
6153 if (f.file->f_op != &io_uring_fops) {
6154 ret = -EINVAL;
6155 goto out_fput;
6156 }
6157
6158 ctx_attach = f.file->private_data;
6159 /* @io_wq is protected by holding the fd */
6160 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6161 ret = -EINVAL;
6162 goto out_fput;
6163 }
6164
6165 ctx->io_wq = ctx_attach->io_wq;
6166out_fput:
6167 fdput(f);
6168 return ret;
6169}
6170
Jens Axboe6c271ce2019-01-10 11:22:30 -07006171static int io_sq_offload_start(struct io_ring_ctx *ctx,
6172 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006173{
6174 int ret;
6175
Jens Axboe6c271ce2019-01-10 11:22:30 -07006176 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006177 mmgrab(current->mm);
6178 ctx->sqo_mm = current->mm;
6179
Jens Axboe6c271ce2019-01-10 11:22:30 -07006180 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006181 ret = -EPERM;
6182 if (!capable(CAP_SYS_ADMIN))
6183 goto err;
6184
Jens Axboe917257d2019-04-13 09:28:55 -06006185 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6186 if (!ctx->sq_thread_idle)
6187 ctx->sq_thread_idle = HZ;
6188
Jens Axboe6c271ce2019-01-10 11:22:30 -07006189 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006190 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006191
Jens Axboe917257d2019-04-13 09:28:55 -06006192 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006193 if (cpu >= nr_cpu_ids)
6194 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006195 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006196 goto err;
6197
Jens Axboe6c271ce2019-01-10 11:22:30 -07006198 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6199 ctx, cpu,
6200 "io_uring-sq");
6201 } else {
6202 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6203 "io_uring-sq");
6204 }
6205 if (IS_ERR(ctx->sqo_thread)) {
6206 ret = PTR_ERR(ctx->sqo_thread);
6207 ctx->sqo_thread = NULL;
6208 goto err;
6209 }
6210 wake_up_process(ctx->sqo_thread);
6211 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6212 /* Can't have SQ_AFF without SQPOLL */
6213 ret = -EINVAL;
6214 goto err;
6215 }
6216
Pavel Begunkov24369c22020-01-28 03:15:48 +03006217 ret = io_init_wq_offload(ctx, p);
6218 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006219 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006220
6221 return 0;
6222err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006223 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006224 mmdrop(ctx->sqo_mm);
6225 ctx->sqo_mm = NULL;
6226 return ret;
6227}
6228
6229static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6230{
6231 atomic_long_sub(nr_pages, &user->locked_vm);
6232}
6233
6234static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6235{
6236 unsigned long page_limit, cur_pages, new_pages;
6237
6238 /* Don't allow more pages than we can safely lock */
6239 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6240
6241 do {
6242 cur_pages = atomic_long_read(&user->locked_vm);
6243 new_pages = cur_pages + nr_pages;
6244 if (new_pages > page_limit)
6245 return -ENOMEM;
6246 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6247 new_pages) != cur_pages);
6248
6249 return 0;
6250}
6251
6252static void io_mem_free(void *ptr)
6253{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006254 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006255
Mark Rutland52e04ef2019-04-30 17:30:21 +01006256 if (!ptr)
6257 return;
6258
6259 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006260 if (put_page_testzero(page))
6261 free_compound_page(page);
6262}
6263
6264static void *io_mem_alloc(size_t size)
6265{
6266 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6267 __GFP_NORETRY;
6268
6269 return (void *) __get_free_pages(gfp_flags, get_order(size));
6270}
6271
Hristo Venev75b28af2019-08-26 17:23:46 +00006272static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6273 size_t *sq_offset)
6274{
6275 struct io_rings *rings;
6276 size_t off, sq_array_size;
6277
6278 off = struct_size(rings, cqes, cq_entries);
6279 if (off == SIZE_MAX)
6280 return SIZE_MAX;
6281
6282#ifdef CONFIG_SMP
6283 off = ALIGN(off, SMP_CACHE_BYTES);
6284 if (off == 0)
6285 return SIZE_MAX;
6286#endif
6287
6288 sq_array_size = array_size(sizeof(u32), sq_entries);
6289 if (sq_array_size == SIZE_MAX)
6290 return SIZE_MAX;
6291
6292 if (check_add_overflow(off, sq_array_size, &off))
6293 return SIZE_MAX;
6294
6295 if (sq_offset)
6296 *sq_offset = off;
6297
6298 return off;
6299}
6300
Jens Axboe2b188cc2019-01-07 10:46:33 -07006301static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6302{
Hristo Venev75b28af2019-08-26 17:23:46 +00006303 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006304
Hristo Venev75b28af2019-08-26 17:23:46 +00006305 pages = (size_t)1 << get_order(
6306 rings_size(sq_entries, cq_entries, NULL));
6307 pages += (size_t)1 << get_order(
6308 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006309
Hristo Venev75b28af2019-08-26 17:23:46 +00006310 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006311}
6312
Jens Axboeedafcce2019-01-09 09:16:05 -07006313static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6314{
6315 int i, j;
6316
6317 if (!ctx->user_bufs)
6318 return -ENXIO;
6319
6320 for (i = 0; i < ctx->nr_user_bufs; i++) {
6321 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6322
6323 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006324 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006325
6326 if (ctx->account_mem)
6327 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006328 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006329 imu->nr_bvecs = 0;
6330 }
6331
6332 kfree(ctx->user_bufs);
6333 ctx->user_bufs = NULL;
6334 ctx->nr_user_bufs = 0;
6335 return 0;
6336}
6337
6338static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6339 void __user *arg, unsigned index)
6340{
6341 struct iovec __user *src;
6342
6343#ifdef CONFIG_COMPAT
6344 if (ctx->compat) {
6345 struct compat_iovec __user *ciovs;
6346 struct compat_iovec ciov;
6347
6348 ciovs = (struct compat_iovec __user *) arg;
6349 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6350 return -EFAULT;
6351
Jens Axboed55e5f52019-12-11 16:12:15 -07006352 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006353 dst->iov_len = ciov.iov_len;
6354 return 0;
6355 }
6356#endif
6357 src = (struct iovec __user *) arg;
6358 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6359 return -EFAULT;
6360 return 0;
6361}
6362
6363static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6364 unsigned nr_args)
6365{
6366 struct vm_area_struct **vmas = NULL;
6367 struct page **pages = NULL;
6368 int i, j, got_pages = 0;
6369 int ret = -EINVAL;
6370
6371 if (ctx->user_bufs)
6372 return -EBUSY;
6373 if (!nr_args || nr_args > UIO_MAXIOV)
6374 return -EINVAL;
6375
6376 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6377 GFP_KERNEL);
6378 if (!ctx->user_bufs)
6379 return -ENOMEM;
6380
6381 for (i = 0; i < nr_args; i++) {
6382 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6383 unsigned long off, start, end, ubuf;
6384 int pret, nr_pages;
6385 struct iovec iov;
6386 size_t size;
6387
6388 ret = io_copy_iov(ctx, &iov, arg, i);
6389 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006390 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006391
6392 /*
6393 * Don't impose further limits on the size and buffer
6394 * constraints here, we'll -EINVAL later when IO is
6395 * submitted if they are wrong.
6396 */
6397 ret = -EFAULT;
6398 if (!iov.iov_base || !iov.iov_len)
6399 goto err;
6400
6401 /* arbitrary limit, but we need something */
6402 if (iov.iov_len > SZ_1G)
6403 goto err;
6404
6405 ubuf = (unsigned long) iov.iov_base;
6406 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6407 start = ubuf >> PAGE_SHIFT;
6408 nr_pages = end - start;
6409
6410 if (ctx->account_mem) {
6411 ret = io_account_mem(ctx->user, nr_pages);
6412 if (ret)
6413 goto err;
6414 }
6415
6416 ret = 0;
6417 if (!pages || nr_pages > got_pages) {
6418 kfree(vmas);
6419 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006420 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006421 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006422 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006423 sizeof(struct vm_area_struct *),
6424 GFP_KERNEL);
6425 if (!pages || !vmas) {
6426 ret = -ENOMEM;
6427 if (ctx->account_mem)
6428 io_unaccount_mem(ctx->user, nr_pages);
6429 goto err;
6430 }
6431 got_pages = nr_pages;
6432 }
6433
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006434 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006435 GFP_KERNEL);
6436 ret = -ENOMEM;
6437 if (!imu->bvec) {
6438 if (ctx->account_mem)
6439 io_unaccount_mem(ctx->user, nr_pages);
6440 goto err;
6441 }
6442
6443 ret = 0;
6444 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006445 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006446 FOLL_WRITE | FOLL_LONGTERM,
6447 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006448 if (pret == nr_pages) {
6449 /* don't support file backed memory */
6450 for (j = 0; j < nr_pages; j++) {
6451 struct vm_area_struct *vma = vmas[j];
6452
6453 if (vma->vm_file &&
6454 !is_file_hugepages(vma->vm_file)) {
6455 ret = -EOPNOTSUPP;
6456 break;
6457 }
6458 }
6459 } else {
6460 ret = pret < 0 ? pret : -EFAULT;
6461 }
6462 up_read(&current->mm->mmap_sem);
6463 if (ret) {
6464 /*
6465 * if we did partial map, or found file backed vmas,
6466 * release any pages we did get
6467 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006468 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006469 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006470 if (ctx->account_mem)
6471 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006472 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006473 goto err;
6474 }
6475
6476 off = ubuf & ~PAGE_MASK;
6477 size = iov.iov_len;
6478 for (j = 0; j < nr_pages; j++) {
6479 size_t vec_len;
6480
6481 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6482 imu->bvec[j].bv_page = pages[j];
6483 imu->bvec[j].bv_len = vec_len;
6484 imu->bvec[j].bv_offset = off;
6485 off = 0;
6486 size -= vec_len;
6487 }
6488 /* store original address for later verification */
6489 imu->ubuf = ubuf;
6490 imu->len = iov.iov_len;
6491 imu->nr_bvecs = nr_pages;
6492
6493 ctx->nr_user_bufs++;
6494 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006495 kvfree(pages);
6496 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006497 return 0;
6498err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006499 kvfree(pages);
6500 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006501 io_sqe_buffer_unregister(ctx);
6502 return ret;
6503}
6504
Jens Axboe9b402842019-04-11 11:45:41 -06006505static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6506{
6507 __s32 __user *fds = arg;
6508 int fd;
6509
6510 if (ctx->cq_ev_fd)
6511 return -EBUSY;
6512
6513 if (copy_from_user(&fd, fds, sizeof(*fds)))
6514 return -EFAULT;
6515
6516 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6517 if (IS_ERR(ctx->cq_ev_fd)) {
6518 int ret = PTR_ERR(ctx->cq_ev_fd);
6519 ctx->cq_ev_fd = NULL;
6520 return ret;
6521 }
6522
6523 return 0;
6524}
6525
6526static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6527{
6528 if (ctx->cq_ev_fd) {
6529 eventfd_ctx_put(ctx->cq_ev_fd);
6530 ctx->cq_ev_fd = NULL;
6531 return 0;
6532 }
6533
6534 return -ENXIO;
6535}
6536
Jens Axboe5a2e7452020-02-23 16:23:11 -07006537static int __io_destroy_buffers(int id, void *p, void *data)
6538{
6539 struct io_ring_ctx *ctx = data;
6540 struct io_buffer *buf = p;
6541
6542 /* the head kbuf is the list itself */
6543 while (!list_empty(&buf->list)) {
6544 struct io_buffer *nxt;
6545
6546 nxt = list_first_entry(&buf->list, struct io_buffer, list);
6547 list_del(&nxt->list);
6548 kfree(nxt);
6549 }
6550 kfree(buf);
6551 idr_remove(&ctx->io_buffer_idr, id);
6552 return 0;
6553}
6554
6555static void io_destroy_buffers(struct io_ring_ctx *ctx)
6556{
6557 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
6558 idr_destroy(&ctx->io_buffer_idr);
6559}
6560
Jens Axboe2b188cc2019-01-07 10:46:33 -07006561static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6562{
Jens Axboe6b063142019-01-10 22:13:58 -07006563 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006564 if (ctx->sqo_mm)
6565 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006566
6567 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006568 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006569 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006570 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07006571 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006572 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006573
Jens Axboe2b188cc2019-01-07 10:46:33 -07006574#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006575 if (ctx->ring_sock) {
6576 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006577 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006578 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006579#endif
6580
Hristo Venev75b28af2019-08-26 17:23:46 +00006581 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006582 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006583
6584 percpu_ref_exit(&ctx->refs);
6585 if (ctx->account_mem)
6586 io_unaccount_mem(ctx->user,
6587 ring_pages(ctx->sq_entries, ctx->cq_entries));
6588 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006589 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006590 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006591 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006592 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006593 kfree(ctx);
6594}
6595
6596static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6597{
6598 struct io_ring_ctx *ctx = file->private_data;
6599 __poll_t mask = 0;
6600
6601 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006602 /*
6603 * synchronizes with barrier from wq_has_sleeper call in
6604 * io_commit_cqring
6605 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006606 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006607 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6608 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006609 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006610 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006611 mask |= EPOLLIN | EPOLLRDNORM;
6612
6613 return mask;
6614}
6615
6616static int io_uring_fasync(int fd, struct file *file, int on)
6617{
6618 struct io_ring_ctx *ctx = file->private_data;
6619
6620 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6621}
6622
Jens Axboe071698e2020-01-28 10:04:42 -07006623static int io_remove_personalities(int id, void *p, void *data)
6624{
6625 struct io_ring_ctx *ctx = data;
6626 const struct cred *cred;
6627
6628 cred = idr_remove(&ctx->personality_idr, id);
6629 if (cred)
6630 put_cred(cred);
6631 return 0;
6632}
6633
Jens Axboe2b188cc2019-01-07 10:46:33 -07006634static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6635{
6636 mutex_lock(&ctx->uring_lock);
6637 percpu_ref_kill(&ctx->refs);
6638 mutex_unlock(&ctx->uring_lock);
6639
Jens Axboedf069d82020-02-04 16:48:34 -07006640 /*
6641 * Wait for sq thread to idle, if we have one. It won't spin on new
6642 * work after we've killed the ctx ref above. This is important to do
6643 * before we cancel existing commands, as the thread could otherwise
6644 * be queueing new work post that. If that's work we need to cancel,
6645 * it could cause shutdown to hang.
6646 */
6647 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6648 cpu_relax();
6649
Jens Axboe5262f562019-09-17 12:26:57 -06006650 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006651 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006652
6653 if (ctx->io_wq)
6654 io_wq_cancel_all(ctx->io_wq);
6655
Jens Axboedef596e2019-01-09 08:59:42 -07006656 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006657 /* if we failed setting up the ctx, we might not have any rings */
6658 if (ctx->rings)
6659 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006660 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006661 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006662 io_ring_ctx_free(ctx);
6663}
6664
6665static int io_uring_release(struct inode *inode, struct file *file)
6666{
6667 struct io_ring_ctx *ctx = file->private_data;
6668
6669 file->private_data = NULL;
6670 io_ring_ctx_wait_and_kill(ctx);
6671 return 0;
6672}
6673
Jens Axboefcb323c2019-10-24 12:39:47 -06006674static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6675 struct files_struct *files)
6676{
6677 struct io_kiocb *req;
6678 DEFINE_WAIT(wait);
6679
6680 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006681 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006682
6683 spin_lock_irq(&ctx->inflight_lock);
6684 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006685 if (req->work.files != files)
6686 continue;
6687 /* req is being completed, ignore */
6688 if (!refcount_inc_not_zero(&req->refs))
6689 continue;
6690 cancel_req = req;
6691 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006692 }
Jens Axboe768134d2019-11-10 20:30:53 -07006693 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006694 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006695 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006696 spin_unlock_irq(&ctx->inflight_lock);
6697
Jens Axboe768134d2019-11-10 20:30:53 -07006698 /* We need to keep going until we don't find a matching req */
6699 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006700 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006701
Jens Axboe2ca10252020-02-13 17:17:35 -07006702 if (cancel_req->flags & REQ_F_OVERFLOW) {
6703 spin_lock_irq(&ctx->completion_lock);
6704 list_del(&cancel_req->list);
6705 cancel_req->flags &= ~REQ_F_OVERFLOW;
6706 if (list_empty(&ctx->cq_overflow_list)) {
6707 clear_bit(0, &ctx->sq_check_overflow);
6708 clear_bit(0, &ctx->cq_check_overflow);
6709 }
6710 spin_unlock_irq(&ctx->completion_lock);
6711
6712 WRITE_ONCE(ctx->rings->cq_overflow,
6713 atomic_inc_return(&ctx->cached_cq_overflow));
6714
6715 /*
6716 * Put inflight ref and overflow ref. If that's
6717 * all we had, then we're done with this request.
6718 */
6719 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6720 io_put_req(cancel_req);
6721 continue;
6722 }
6723 }
6724
Bob Liu2f6d9b92019-11-13 18:06:24 +08006725 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6726 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006727 schedule();
6728 }
Jens Axboe768134d2019-11-10 20:30:53 -07006729 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006730}
6731
6732static int io_uring_flush(struct file *file, void *data)
6733{
6734 struct io_ring_ctx *ctx = file->private_data;
6735
6736 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006737
6738 /*
6739 * If the task is going away, cancel work it may have pending
6740 */
6741 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6742 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6743
Jens Axboefcb323c2019-10-24 12:39:47 -06006744 return 0;
6745}
6746
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006747static void *io_uring_validate_mmap_request(struct file *file,
6748 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006749{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006750 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006751 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006752 struct page *page;
6753 void *ptr;
6754
6755 switch (offset) {
6756 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006757 case IORING_OFF_CQ_RING:
6758 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006759 break;
6760 case IORING_OFF_SQES:
6761 ptr = ctx->sq_sqes;
6762 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006763 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006764 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006765 }
6766
6767 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006768 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006769 return ERR_PTR(-EINVAL);
6770
6771 return ptr;
6772}
6773
6774#ifdef CONFIG_MMU
6775
6776static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6777{
6778 size_t sz = vma->vm_end - vma->vm_start;
6779 unsigned long pfn;
6780 void *ptr;
6781
6782 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6783 if (IS_ERR(ptr))
6784 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006785
6786 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6787 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6788}
6789
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006790#else /* !CONFIG_MMU */
6791
6792static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6793{
6794 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6795}
6796
6797static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6798{
6799 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6800}
6801
6802static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6803 unsigned long addr, unsigned long len,
6804 unsigned long pgoff, unsigned long flags)
6805{
6806 void *ptr;
6807
6808 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6809 if (IS_ERR(ptr))
6810 return PTR_ERR(ptr);
6811
6812 return (unsigned long) ptr;
6813}
6814
6815#endif /* !CONFIG_MMU */
6816
Jens Axboe2b188cc2019-01-07 10:46:33 -07006817SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6818 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6819 size_t, sigsz)
6820{
6821 struct io_ring_ctx *ctx;
6822 long ret = -EBADF;
6823 int submitted = 0;
6824 struct fd f;
6825
Jens Axboeb41e9852020-02-17 09:52:41 -07006826 if (current->task_works)
6827 task_work_run();
6828
Jens Axboe6c271ce2019-01-10 11:22:30 -07006829 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006830 return -EINVAL;
6831
6832 f = fdget(fd);
6833 if (!f.file)
6834 return -EBADF;
6835
6836 ret = -EOPNOTSUPP;
6837 if (f.file->f_op != &io_uring_fops)
6838 goto out_fput;
6839
6840 ret = -ENXIO;
6841 ctx = f.file->private_data;
6842 if (!percpu_ref_tryget(&ctx->refs))
6843 goto out_fput;
6844
Jens Axboe6c271ce2019-01-10 11:22:30 -07006845 /*
6846 * For SQ polling, the thread will do all submissions and completions.
6847 * Just return the requested submit count, and wake the thread if
6848 * we were asked to.
6849 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006850 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006851 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006852 if (!list_empty_careful(&ctx->cq_overflow_list))
6853 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006854 if (flags & IORING_ENTER_SQ_WAKEUP)
6855 wake_up(&ctx->sqo_wait);
6856 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006857 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006858 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006859
6860 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006861 /* already have mm, so io_submit_sqes() won't try to grab it */
6862 cur_mm = ctx->sqo_mm;
6863 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6864 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006865 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006866
6867 if (submitted != to_submit)
6868 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006869 }
6870 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006871 unsigned nr_events = 0;
6872
Jens Axboe2b188cc2019-01-07 10:46:33 -07006873 min_complete = min(min_complete, ctx->cq_entries);
6874
Jens Axboedef596e2019-01-09 08:59:42 -07006875 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006876 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006877 } else {
6878 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6879 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006880 }
6881
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006882out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006883 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006884out_fput:
6885 fdput(f);
6886 return submitted ? submitted : ret;
6887}
6888
Tobias Klauserbebdb652020-02-26 18:38:32 +01006889#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006890static int io_uring_show_cred(int id, void *p, void *data)
6891{
6892 const struct cred *cred = p;
6893 struct seq_file *m = data;
6894 struct user_namespace *uns = seq_user_ns(m);
6895 struct group_info *gi;
6896 kernel_cap_t cap;
6897 unsigned __capi;
6898 int g;
6899
6900 seq_printf(m, "%5d\n", id);
6901 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6902 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6903 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6904 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6905 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6906 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6907 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6908 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6909 seq_puts(m, "\n\tGroups:\t");
6910 gi = cred->group_info;
6911 for (g = 0; g < gi->ngroups; g++) {
6912 seq_put_decimal_ull(m, g ? " " : "",
6913 from_kgid_munged(uns, gi->gid[g]));
6914 }
6915 seq_puts(m, "\n\tCapEff:\t");
6916 cap = cred->cap_effective;
6917 CAP_FOR_EACH_U32(__capi)
6918 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6919 seq_putc(m, '\n');
6920 return 0;
6921}
6922
6923static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6924{
6925 int i;
6926
6927 mutex_lock(&ctx->uring_lock);
6928 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6929 for (i = 0; i < ctx->nr_user_files; i++) {
6930 struct fixed_file_table *table;
6931 struct file *f;
6932
6933 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6934 f = table->files[i & IORING_FILE_TABLE_MASK];
6935 if (f)
6936 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6937 else
6938 seq_printf(m, "%5u: <none>\n", i);
6939 }
6940 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6941 for (i = 0; i < ctx->nr_user_bufs; i++) {
6942 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6943
6944 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6945 (unsigned int) buf->len);
6946 }
6947 if (!idr_is_empty(&ctx->personality_idr)) {
6948 seq_printf(m, "Personalities:\n");
6949 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6950 }
Jens Axboed7718a92020-02-14 22:23:12 -07006951 seq_printf(m, "PollList:\n");
6952 spin_lock_irq(&ctx->completion_lock);
6953 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
6954 struct hlist_head *list = &ctx->cancel_hash[i];
6955 struct io_kiocb *req;
6956
6957 hlist_for_each_entry(req, list, hash_node)
6958 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
6959 req->task->task_works != NULL);
6960 }
6961 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07006962 mutex_unlock(&ctx->uring_lock);
6963}
6964
6965static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6966{
6967 struct io_ring_ctx *ctx = f->private_data;
6968
6969 if (percpu_ref_tryget(&ctx->refs)) {
6970 __io_uring_show_fdinfo(ctx, m);
6971 percpu_ref_put(&ctx->refs);
6972 }
6973}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006974#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006975
Jens Axboe2b188cc2019-01-07 10:46:33 -07006976static const struct file_operations io_uring_fops = {
6977 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006978 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006979 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006980#ifndef CONFIG_MMU
6981 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6982 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6983#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006984 .poll = io_uring_poll,
6985 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006986#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006987 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006988#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006989};
6990
6991static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6992 struct io_uring_params *p)
6993{
Hristo Venev75b28af2019-08-26 17:23:46 +00006994 struct io_rings *rings;
6995 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006996
Hristo Venev75b28af2019-08-26 17:23:46 +00006997 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6998 if (size == SIZE_MAX)
6999 return -EOVERFLOW;
7000
7001 rings = io_mem_alloc(size);
7002 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007003 return -ENOMEM;
7004
Hristo Venev75b28af2019-08-26 17:23:46 +00007005 ctx->rings = rings;
7006 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7007 rings->sq_ring_mask = p->sq_entries - 1;
7008 rings->cq_ring_mask = p->cq_entries - 1;
7009 rings->sq_ring_entries = p->sq_entries;
7010 rings->cq_ring_entries = p->cq_entries;
7011 ctx->sq_mask = rings->sq_ring_mask;
7012 ctx->cq_mask = rings->cq_ring_mask;
7013 ctx->sq_entries = rings->sq_ring_entries;
7014 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007015
7016 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007017 if (size == SIZE_MAX) {
7018 io_mem_free(ctx->rings);
7019 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007020 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007021 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007022
7023 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007024 if (!ctx->sq_sqes) {
7025 io_mem_free(ctx->rings);
7026 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007027 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007028 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007029
Jens Axboe2b188cc2019-01-07 10:46:33 -07007030 return 0;
7031}
7032
7033/*
7034 * Allocate an anonymous fd, this is what constitutes the application
7035 * visible backing of an io_uring instance. The application mmaps this
7036 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7037 * we have to tie this fd to a socket for file garbage collection purposes.
7038 */
7039static int io_uring_get_fd(struct io_ring_ctx *ctx)
7040{
7041 struct file *file;
7042 int ret;
7043
7044#if defined(CONFIG_UNIX)
7045 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7046 &ctx->ring_sock);
7047 if (ret)
7048 return ret;
7049#endif
7050
7051 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7052 if (ret < 0)
7053 goto err;
7054
7055 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7056 O_RDWR | O_CLOEXEC);
7057 if (IS_ERR(file)) {
7058 put_unused_fd(ret);
7059 ret = PTR_ERR(file);
7060 goto err;
7061 }
7062
7063#if defined(CONFIG_UNIX)
7064 ctx->ring_sock->file = file;
7065#endif
7066 fd_install(ret, file);
7067 return ret;
7068err:
7069#if defined(CONFIG_UNIX)
7070 sock_release(ctx->ring_sock);
7071 ctx->ring_sock = NULL;
7072#endif
7073 return ret;
7074}
7075
7076static int io_uring_create(unsigned entries, struct io_uring_params *p)
7077{
7078 struct user_struct *user = NULL;
7079 struct io_ring_ctx *ctx;
7080 bool account_mem;
7081 int ret;
7082
Jens Axboe8110c1a2019-12-28 15:39:54 -07007083 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007084 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007085 if (entries > IORING_MAX_ENTRIES) {
7086 if (!(p->flags & IORING_SETUP_CLAMP))
7087 return -EINVAL;
7088 entries = IORING_MAX_ENTRIES;
7089 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007090
7091 /*
7092 * Use twice as many entries for the CQ ring. It's possible for the
7093 * application to drive a higher depth than the size of the SQ ring,
7094 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007095 * some flexibility in overcommitting a bit. If the application has
7096 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7097 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007098 */
7099 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007100 if (p->flags & IORING_SETUP_CQSIZE) {
7101 /*
7102 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7103 * to a power-of-two, if it isn't already. We do NOT impose
7104 * any cq vs sq ring sizing.
7105 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007106 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007107 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007108 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7109 if (!(p->flags & IORING_SETUP_CLAMP))
7110 return -EINVAL;
7111 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7112 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007113 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7114 } else {
7115 p->cq_entries = 2 * p->sq_entries;
7116 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007117
7118 user = get_uid(current_user());
7119 account_mem = !capable(CAP_IPC_LOCK);
7120
7121 if (account_mem) {
7122 ret = io_account_mem(user,
7123 ring_pages(p->sq_entries, p->cq_entries));
7124 if (ret) {
7125 free_uid(user);
7126 return ret;
7127 }
7128 }
7129
7130 ctx = io_ring_ctx_alloc(p);
7131 if (!ctx) {
7132 if (account_mem)
7133 io_unaccount_mem(user, ring_pages(p->sq_entries,
7134 p->cq_entries));
7135 free_uid(user);
7136 return -ENOMEM;
7137 }
7138 ctx->compat = in_compat_syscall();
7139 ctx->account_mem = account_mem;
7140 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007141 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007142
7143 ret = io_allocate_scq_urings(ctx, p);
7144 if (ret)
7145 goto err;
7146
Jens Axboe6c271ce2019-01-10 11:22:30 -07007147 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007148 if (ret)
7149 goto err;
7150
Jens Axboe2b188cc2019-01-07 10:46:33 -07007151 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007152 p->sq_off.head = offsetof(struct io_rings, sq.head);
7153 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7154 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7155 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7156 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7157 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7158 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007159
7160 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007161 p->cq_off.head = offsetof(struct io_rings, cq.head);
7162 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7163 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7164 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7165 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7166 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007167
Jens Axboe044c1ab2019-10-28 09:15:33 -06007168 /*
7169 * Install ring fd as the very last thing, so we don't risk someone
7170 * having closed it before we finish setup
7171 */
7172 ret = io_uring_get_fd(ctx);
7173 if (ret < 0)
7174 goto err;
7175
Jens Axboeda8c9692019-12-02 18:51:26 -07007176 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007177 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007178 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007179 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007180 return ret;
7181err:
7182 io_ring_ctx_wait_and_kill(ctx);
7183 return ret;
7184}
7185
7186/*
7187 * Sets up an aio uring context, and returns the fd. Applications asks for a
7188 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7189 * params structure passed in.
7190 */
7191static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7192{
7193 struct io_uring_params p;
7194 long ret;
7195 int i;
7196
7197 if (copy_from_user(&p, params, sizeof(p)))
7198 return -EFAULT;
7199 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7200 if (p.resv[i])
7201 return -EINVAL;
7202 }
7203
Jens Axboe6c271ce2019-01-10 11:22:30 -07007204 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007205 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007206 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007207 return -EINVAL;
7208
7209 ret = io_uring_create(entries, &p);
7210 if (ret < 0)
7211 return ret;
7212
7213 if (copy_to_user(params, &p, sizeof(p)))
7214 return -EFAULT;
7215
7216 return ret;
7217}
7218
7219SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7220 struct io_uring_params __user *, params)
7221{
7222 return io_uring_setup(entries, params);
7223}
7224
Jens Axboe66f4af92020-01-16 15:36:52 -07007225static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7226{
7227 struct io_uring_probe *p;
7228 size_t size;
7229 int i, ret;
7230
7231 size = struct_size(p, ops, nr_args);
7232 if (size == SIZE_MAX)
7233 return -EOVERFLOW;
7234 p = kzalloc(size, GFP_KERNEL);
7235 if (!p)
7236 return -ENOMEM;
7237
7238 ret = -EFAULT;
7239 if (copy_from_user(p, arg, size))
7240 goto out;
7241 ret = -EINVAL;
7242 if (memchr_inv(p, 0, size))
7243 goto out;
7244
7245 p->last_op = IORING_OP_LAST - 1;
7246 if (nr_args > IORING_OP_LAST)
7247 nr_args = IORING_OP_LAST;
7248
7249 for (i = 0; i < nr_args; i++) {
7250 p->ops[i].op = i;
7251 if (!io_op_defs[i].not_supported)
7252 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7253 }
7254 p->ops_len = i;
7255
7256 ret = 0;
7257 if (copy_to_user(arg, p, size))
7258 ret = -EFAULT;
7259out:
7260 kfree(p);
7261 return ret;
7262}
7263
Jens Axboe071698e2020-01-28 10:04:42 -07007264static int io_register_personality(struct io_ring_ctx *ctx)
7265{
7266 const struct cred *creds = get_current_cred();
7267 int id;
7268
7269 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7270 USHRT_MAX, GFP_KERNEL);
7271 if (id < 0)
7272 put_cred(creds);
7273 return id;
7274}
7275
7276static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7277{
7278 const struct cred *old_creds;
7279
7280 old_creds = idr_remove(&ctx->personality_idr, id);
7281 if (old_creds) {
7282 put_cred(old_creds);
7283 return 0;
7284 }
7285
7286 return -EINVAL;
7287}
7288
7289static bool io_register_op_must_quiesce(int op)
7290{
7291 switch (op) {
7292 case IORING_UNREGISTER_FILES:
7293 case IORING_REGISTER_FILES_UPDATE:
7294 case IORING_REGISTER_PROBE:
7295 case IORING_REGISTER_PERSONALITY:
7296 case IORING_UNREGISTER_PERSONALITY:
7297 return false;
7298 default:
7299 return true;
7300 }
7301}
7302
Jens Axboeedafcce2019-01-09 09:16:05 -07007303static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7304 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007305 __releases(ctx->uring_lock)
7306 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007307{
7308 int ret;
7309
Jens Axboe35fa71a2019-04-22 10:23:23 -06007310 /*
7311 * We're inside the ring mutex, if the ref is already dying, then
7312 * someone else killed the ctx or is already going through
7313 * io_uring_register().
7314 */
7315 if (percpu_ref_is_dying(&ctx->refs))
7316 return -ENXIO;
7317
Jens Axboe071698e2020-01-28 10:04:42 -07007318 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007319 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007320
Jens Axboe05f3fb32019-12-09 11:22:50 -07007321 /*
7322 * Drop uring mutex before waiting for references to exit. If
7323 * another thread is currently inside io_uring_enter() it might
7324 * need to grab the uring_lock to make progress. If we hold it
7325 * here across the drain wait, then we can deadlock. It's safe
7326 * to drop the mutex here, since no new references will come in
7327 * after we've killed the percpu ref.
7328 */
7329 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007330 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007331 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007332 if (ret) {
7333 percpu_ref_resurrect(&ctx->refs);
7334 ret = -EINTR;
7335 goto out;
7336 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007337 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007338
7339 switch (opcode) {
7340 case IORING_REGISTER_BUFFERS:
7341 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7342 break;
7343 case IORING_UNREGISTER_BUFFERS:
7344 ret = -EINVAL;
7345 if (arg || nr_args)
7346 break;
7347 ret = io_sqe_buffer_unregister(ctx);
7348 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007349 case IORING_REGISTER_FILES:
7350 ret = io_sqe_files_register(ctx, arg, nr_args);
7351 break;
7352 case IORING_UNREGISTER_FILES:
7353 ret = -EINVAL;
7354 if (arg || nr_args)
7355 break;
7356 ret = io_sqe_files_unregister(ctx);
7357 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007358 case IORING_REGISTER_FILES_UPDATE:
7359 ret = io_sqe_files_update(ctx, arg, nr_args);
7360 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007361 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007362 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007363 ret = -EINVAL;
7364 if (nr_args != 1)
7365 break;
7366 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007367 if (ret)
7368 break;
7369 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7370 ctx->eventfd_async = 1;
7371 else
7372 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007373 break;
7374 case IORING_UNREGISTER_EVENTFD:
7375 ret = -EINVAL;
7376 if (arg || nr_args)
7377 break;
7378 ret = io_eventfd_unregister(ctx);
7379 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007380 case IORING_REGISTER_PROBE:
7381 ret = -EINVAL;
7382 if (!arg || nr_args > 256)
7383 break;
7384 ret = io_probe(ctx, arg, nr_args);
7385 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007386 case IORING_REGISTER_PERSONALITY:
7387 ret = -EINVAL;
7388 if (arg || nr_args)
7389 break;
7390 ret = io_register_personality(ctx);
7391 break;
7392 case IORING_UNREGISTER_PERSONALITY:
7393 ret = -EINVAL;
7394 if (arg)
7395 break;
7396 ret = io_unregister_personality(ctx, nr_args);
7397 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007398 default:
7399 ret = -EINVAL;
7400 break;
7401 }
7402
Jens Axboe071698e2020-01-28 10:04:42 -07007403 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007404 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007405 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007406out:
7407 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007408 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007409 return ret;
7410}
7411
7412SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7413 void __user *, arg, unsigned int, nr_args)
7414{
7415 struct io_ring_ctx *ctx;
7416 long ret = -EBADF;
7417 struct fd f;
7418
7419 f = fdget(fd);
7420 if (!f.file)
7421 return -EBADF;
7422
7423 ret = -EOPNOTSUPP;
7424 if (f.file->f_op != &io_uring_fops)
7425 goto out_fput;
7426
7427 ctx = f.file->private_data;
7428
7429 mutex_lock(&ctx->uring_lock);
7430 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7431 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007432 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7433 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007434out_fput:
7435 fdput(f);
7436 return ret;
7437}
7438
Jens Axboe2b188cc2019-01-07 10:46:33 -07007439static int __init io_uring_init(void)
7440{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007441#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7442 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7443 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7444} while (0)
7445
7446#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7447 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7448 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7449 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7450 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7451 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7452 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7453 BUILD_BUG_SQE_ELEM(8, __u64, off);
7454 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7455 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007456 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007457 BUILD_BUG_SQE_ELEM(24, __u32, len);
7458 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7459 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7460 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7461 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7462 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7463 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7464 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7465 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7466 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7467 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7468 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7469 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7470 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007471 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007472 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7473 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7474 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007475 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007476
Jens Axboed3656342019-12-18 09:50:26 -07007477 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007478 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7479 return 0;
7480};
7481__initcall(io_uring_init);