blob: 455d53fd840f0f168c2211fe3efe78e8aca85774 [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>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
58#include <linux/mmu_context.h>
59#include <linux/percpu.h>
60#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070061#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070063#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070064#include <linux/net.h>
65#include <net/sock.h>
66#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070067#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070068#include <linux/anon_inodes.h>
69#include <linux/sched/mm.h>
70#include <linux/uaccess.h>
71#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070072#include <linux/sizes.h>
73#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070074#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070075#include <linux/namei.h>
76#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070077#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070078#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070079#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030080#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070081#include <linux/task_work.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060093
94/*
95 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
96 */
97#define IORING_FILE_TABLE_SHIFT 9
98#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
99#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
100#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700101
102struct io_uring {
103 u32 head ____cacheline_aligned_in_smp;
104 u32 tail ____cacheline_aligned_in_smp;
105};
106
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * This data is shared with the application through the mmap at offsets
109 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 *
111 * The offsets to the member fields are published through struct
112 * io_sqring_offsets when calling io_uring_setup.
113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
116 * Head and tail offsets into the ring; the offsets need to be
117 * masked to get valid indices.
118 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * The kernel controls head of the sq ring and the tail of the cq ring,
120 * and the application controls tail of the sq ring and the head of the
121 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 * ring_entries - 1)
127 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 u32 sq_ring_mask, cq_ring_mask;
129 /* Ring sizes (constant, power of 2) */
130 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
132 * Number of invalid entries dropped by the kernel due to
133 * invalid index stored in array
134 *
135 * Written by the kernel, shouldn't be modified by the
136 * application (i.e. get number of "new events" by comparing to
137 * cached value).
138 *
139 * After a new SQ head value was read by the application this
140 * counter includes all submissions that were dropped reaching
141 * the new SQ head (and possibly more).
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
145 * Runtime flags
146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application.
149 *
150 * The application needs a full memory barrier before checking
151 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
152 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000153 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 /*
155 * Number of completion events lost because the queue was full;
156 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800157 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 * the completion queue.
159 *
160 * Written by the kernel, shouldn't be modified by the
161 * application (i.e. get number of "new events" by comparing to
162 * cached value).
163 *
164 * As completion events come in out of order this counter is not
165 * ordered with any other data.
166 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000167 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200168 /*
169 * Ring buffer of completion events.
170 *
171 * The kernel writes completion events fresh every time they are
172 * produced, so the application is allowed to modify pending
173 * entries.
174 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000175 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700176};
177
Jens Axboeedafcce2019-01-09 09:16:05 -0700178struct io_mapped_ubuf {
179 u64 ubuf;
180 size_t len;
181 struct bio_vec *bvec;
182 unsigned int nr_bvecs;
183};
184
Jens Axboe65e19f52019-10-26 07:20:21 -0600185struct fixed_file_table {
186 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700187};
188
Jens Axboe05f3fb32019-12-09 11:22:50 -0700189struct fixed_file_data {
190 struct fixed_file_table *table;
191 struct io_ring_ctx *ctx;
192
193 struct percpu_ref refs;
194 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700195 struct work_struct ref_work;
196 struct completion done;
197};
198
Jens Axboe5a2e7452020-02-23 16:23:11 -0700199struct io_buffer {
200 struct list_head list;
201 __u64 addr;
202 __s32 len;
203 __u16 bid;
204};
205
Jens Axboe2b188cc2019-01-07 10:46:33 -0700206struct io_ring_ctx {
207 struct {
208 struct percpu_ref refs;
209 } ____cacheline_aligned_in_smp;
210
211 struct {
212 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800213 unsigned int compat: 1;
214 unsigned int account_mem: 1;
215 unsigned int cq_overflow_flushed: 1;
216 unsigned int drain_next: 1;
217 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700218
Hristo Venev75b28af2019-08-26 17:23:46 +0000219 /*
220 * Ring buffer of indices into array of io_uring_sqe, which is
221 * mmapped by the application using the IORING_OFF_SQES offset.
222 *
223 * This indirection could e.g. be used to assign fixed
224 * io_uring_sqe entries to operations and only submit them to
225 * the queue when needed.
226 *
227 * The kernel modifies neither the indices array nor the entries
228 * array.
229 */
230 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700231 unsigned cached_sq_head;
232 unsigned sq_entries;
233 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700234 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600235 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700236 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700237 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600238
239 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600240 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700241 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700242
Jens Axboefcb323c2019-10-24 12:39:47 -0600243 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700244 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700245 } ____cacheline_aligned_in_smp;
246
Hristo Venev75b28af2019-08-26 17:23:46 +0000247 struct io_rings *rings;
248
Jens Axboe2b188cc2019-01-07 10:46:33 -0700249 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600250 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700251 struct task_struct *sqo_thread; /* if using sq thread polling */
252 struct mm_struct *sqo_mm;
253 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700254
Jens Axboe6b063142019-01-10 22:13:58 -0700255 /*
256 * If used, fixed file set. Writers must ensure that ->refs is dead,
257 * readers must ensure that ->refs is alive as long as the file* is
258 * used. Only updated through io_uring_register(2).
259 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700260 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700261 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300262 int ring_fd;
263 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700264
Jens Axboeedafcce2019-01-09 09:16:05 -0700265 /* if used, fixed mapped user buffers */
266 unsigned nr_user_bufs;
267 struct io_mapped_ubuf *user_bufs;
268
Jens Axboe2b188cc2019-01-07 10:46:33 -0700269 struct user_struct *user;
270
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700271 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700272
Jens Axboe206aefd2019-11-07 18:27:42 -0700273 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
274 struct completion *completions;
275
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700276 /* if all else fails... */
277 struct io_kiocb *fallback_req;
278
Jens Axboe206aefd2019-11-07 18:27:42 -0700279#if defined(CONFIG_UNIX)
280 struct socket *ring_sock;
281#endif
282
Jens Axboe5a2e7452020-02-23 16:23:11 -0700283 struct idr io_buffer_idr;
284
Jens Axboe071698e2020-01-28 10:04:42 -0700285 struct idr personality_idr;
286
Jens Axboe206aefd2019-11-07 18:27:42 -0700287 struct {
288 unsigned cached_cq_tail;
289 unsigned cq_entries;
290 unsigned cq_mask;
291 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700292 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700293 struct wait_queue_head cq_wait;
294 struct fasync_struct *cq_fasync;
295 struct eventfd_ctx *cq_ev_fd;
296 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700297
298 struct {
299 struct mutex uring_lock;
300 wait_queue_head_t wait;
301 } ____cacheline_aligned_in_smp;
302
303 struct {
304 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700305
Jens Axboedef596e2019-01-09 08:59:42 -0700306 /*
307 * ->poll_list is protected by the ctx->uring_lock for
308 * io_uring instances that don't use IORING_SETUP_SQPOLL.
309 * For SQPOLL, only the single threaded io_sq_thread() will
310 * manipulate the list, hence no extra locking is needed there.
311 */
312 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700313 struct hlist_head *cancel_hash;
314 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700315 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600316
317 spinlock_t inflight_lock;
318 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700319 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700320};
321
Jens Axboe09bb8392019-03-13 12:39:28 -0600322/*
323 * First field must be the file pointer in all the
324 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
325 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326struct io_poll_iocb {
327 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700328 union {
329 struct wait_queue_head *head;
330 u64 addr;
331 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700332 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600333 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700334 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700335 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700336};
337
Jens Axboeb5dba592019-12-11 14:02:38 -0700338struct io_close {
339 struct file *file;
340 struct file *put_file;
341 int fd;
342};
343
Jens Axboead8a48a2019-11-15 08:49:11 -0700344struct io_timeout_data {
345 struct io_kiocb *req;
346 struct hrtimer timer;
347 struct timespec64 ts;
348 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300349 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700350};
351
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700352struct io_accept {
353 struct file *file;
354 struct sockaddr __user *addr;
355 int __user *addr_len;
356 int flags;
357};
358
359struct io_sync {
360 struct file *file;
361 loff_t len;
362 loff_t off;
363 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700364 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700365};
366
Jens Axboefbf23842019-12-17 18:45:56 -0700367struct io_cancel {
368 struct file *file;
369 u64 addr;
370};
371
Jens Axboeb29472e2019-12-17 18:50:29 -0700372struct io_timeout {
373 struct file *file;
374 u64 addr;
375 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700376 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700377};
378
Jens Axboe9adbd452019-12-20 08:45:55 -0700379struct io_rw {
380 /* NOTE: kiocb has the file as the first member, so don't do it here */
381 struct kiocb kiocb;
382 u64 addr;
383 u64 len;
384};
385
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700386struct io_connect {
387 struct file *file;
388 struct sockaddr __user *addr;
389 int addr_len;
390};
391
Jens Axboee47293f2019-12-20 08:58:21 -0700392struct io_sr_msg {
393 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700394 union {
395 struct user_msghdr __user *msg;
396 void __user *buf;
397 };
Jens Axboee47293f2019-12-20 08:58:21 -0700398 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700399 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700400 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700401 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700402};
403
Jens Axboe15b71ab2019-12-11 11:20:36 -0700404struct io_open {
405 struct file *file;
406 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700407 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700408 unsigned mask;
409 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700410 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700411 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700412 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700413};
414
Jens Axboe05f3fb32019-12-09 11:22:50 -0700415struct io_files_update {
416 struct file *file;
417 u64 arg;
418 u32 nr_args;
419 u32 offset;
420};
421
Jens Axboe4840e412019-12-25 22:03:45 -0700422struct io_fadvise {
423 struct file *file;
424 u64 offset;
425 u32 len;
426 u32 advice;
427};
428
Jens Axboec1ca7572019-12-25 22:18:28 -0700429struct io_madvise {
430 struct file *file;
431 u64 addr;
432 u32 len;
433 u32 advice;
434};
435
Jens Axboe3e4827b2020-01-08 15:18:09 -0700436struct io_epoll {
437 struct file *file;
438 int epfd;
439 int op;
440 int fd;
441 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700442};
443
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300444struct io_splice {
445 struct file *file_out;
446 struct file *file_in;
447 loff_t off_out;
448 loff_t off_in;
449 u64 len;
450 unsigned int flags;
451};
452
Jens Axboeddf0322d2020-02-23 16:41:33 -0700453struct io_provide_buf {
454 struct file *file;
455 __u64 addr;
456 __s32 len;
457 __u32 bgid;
458 __u16 nbufs;
459 __u16 bid;
460};
461
Jens Axboef499a022019-12-02 16:28:46 -0700462struct io_async_connect {
463 struct sockaddr_storage address;
464};
465
Jens Axboe03b12302019-12-02 18:50:25 -0700466struct io_async_msghdr {
467 struct iovec fast_iov[UIO_FASTIOV];
468 struct iovec *iov;
469 struct sockaddr __user *uaddr;
470 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700471 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700472};
473
Jens Axboef67676d2019-12-02 11:03:47 -0700474struct io_async_rw {
475 struct iovec fast_iov[UIO_FASTIOV];
476 struct iovec *iov;
477 ssize_t nr_segs;
478 ssize_t size;
479};
480
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700481struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700482 union {
483 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700484 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700485 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700486 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700487 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700488};
489
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300490enum {
491 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
492 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
493 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
494 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
495 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700496 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300497
498 REQ_F_LINK_NEXT_BIT,
499 REQ_F_FAIL_LINK_BIT,
500 REQ_F_INFLIGHT_BIT,
501 REQ_F_CUR_POS_BIT,
502 REQ_F_NOWAIT_BIT,
503 REQ_F_IOPOLL_COMPLETED_BIT,
504 REQ_F_LINK_TIMEOUT_BIT,
505 REQ_F_TIMEOUT_BIT,
506 REQ_F_ISREG_BIT,
507 REQ_F_MUST_PUNT_BIT,
508 REQ_F_TIMEOUT_NOSEQ_BIT,
509 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300510 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700511 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700512 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700513 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300514};
515
516enum {
517 /* ctx owns file */
518 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
519 /* drain existing IO first */
520 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
521 /* linked sqes */
522 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
523 /* doesn't sever on completion < 0 */
524 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
525 /* IOSQE_ASYNC */
526 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700527 /* IOSQE_BUFFER_SELECT */
528 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300529
530 /* already grabbed next link */
531 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
532 /* fail rest of links */
533 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
534 /* on inflight list */
535 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
536 /* read/write uses file position */
537 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
538 /* must not punt to workers */
539 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
540 /* polled IO has completed */
541 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
542 /* has linked timeout */
543 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
544 /* timeout request */
545 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
546 /* regular file */
547 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
548 /* must be punted even for NONBLOCK */
549 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
550 /* no timeout sequence */
551 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
552 /* completion under lock */
553 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300554 /* needs cleanup */
555 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700556 /* in overflow list */
557 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700558 /* already went through poll handler */
559 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700560 /* buffer already selected */
561 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700562};
563
564struct async_poll {
565 struct io_poll_iocb poll;
566 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300567};
568
Jens Axboe09bb8392019-03-13 12:39:28 -0600569/*
570 * NOTE! Each of the iocb union members has the file pointer
571 * as the first entry in their struct definition. So you can
572 * access the file pointer through any of the sub-structs,
573 * or directly as just 'ki_filp' in this struct.
574 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700575struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700576 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600577 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700578 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700579 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700580 struct io_accept accept;
581 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700582 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700583 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700584 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700585 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700586 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700587 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700588 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700589 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700590 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700591 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300592 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700593 struct io_provide_buf pbuf;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700594 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700595
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700596 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300597 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700598 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700599
600 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700601 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700602 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700603 refcount_t refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700604 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700605 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600606 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600607 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608
Jens Axboed7718a92020-02-14 22:23:12 -0700609 struct list_head link_list;
610
Jens Axboefcb323c2019-10-24 12:39:47 -0600611 struct list_head inflight_entry;
612
Jens Axboeb41e9852020-02-17 09:52:41 -0700613 union {
614 /*
615 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700616 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
617 * async armed poll handlers for regular commands. The latter
618 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700619 */
620 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700621 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700622 struct hlist_node hash_node;
623 struct async_poll *apoll;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700624 int cflags;
Jens Axboeb41e9852020-02-17 09:52:41 -0700625 };
626 struct io_wq_work work;
627 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700628};
629
630#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700631#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632
Jens Axboe9a56a232019-01-09 09:06:50 -0700633struct io_submit_state {
634 struct blk_plug plug;
635
636 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700637 * io_kiocb alloc cache
638 */
639 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300640 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700641
642 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700643 * File reference cache
644 */
645 struct file *file;
646 unsigned int fd;
647 unsigned int has_refs;
648 unsigned int used_refs;
649 unsigned int ios_left;
650};
651
Jens Axboed3656342019-12-18 09:50:26 -0700652struct io_op_def {
653 /* needs req->io allocated for deferral/async */
654 unsigned async_ctx : 1;
655 /* needs current->mm setup, does mm access */
656 unsigned needs_mm : 1;
657 /* needs req->file assigned */
658 unsigned needs_file : 1;
659 /* needs req->file assigned IFF fd is >= 0 */
660 unsigned fd_non_neg : 1;
661 /* hash wq insertion if file is a regular file */
662 unsigned hash_reg_file : 1;
663 /* unbound wq insertion if file is a non-regular file */
664 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700665 /* opcode is not supported by this kernel */
666 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700667 /* needs file table */
668 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700669 /* needs ->fs */
670 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700671 /* set if opcode supports polled "wait" */
672 unsigned pollin : 1;
673 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700674 /* op supports buffer selection */
675 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700676};
677
678static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300679 [IORING_OP_NOP] = {},
680 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700681 .async_ctx = 1,
682 .needs_mm = 1,
683 .needs_file = 1,
684 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700685 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700686 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700687 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300688 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700689 .async_ctx = 1,
690 .needs_mm = 1,
691 .needs_file = 1,
692 .hash_reg_file = 1,
693 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700694 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700695 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300696 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700697 .needs_file = 1,
698 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300699 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700700 .needs_file = 1,
701 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700702 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700703 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300704 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700705 .needs_file = 1,
706 .hash_reg_file = 1,
707 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700708 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700709 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300710 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700711 .needs_file = 1,
712 .unbound_nonreg_file = 1,
713 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300714 [IORING_OP_POLL_REMOVE] = {},
715 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700716 .needs_file = 1,
717 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300718 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700719 .async_ctx = 1,
720 .needs_mm = 1,
721 .needs_file = 1,
722 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700723 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700724 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700727 .async_ctx = 1,
728 .needs_mm = 1,
729 .needs_file = 1,
730 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700731 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700732 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700733 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700736 .async_ctx = 1,
737 .needs_mm = 1,
738 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300739 [IORING_OP_TIMEOUT_REMOVE] = {},
740 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700741 .needs_mm = 1,
742 .needs_file = 1,
743 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700744 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700745 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700746 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300747 [IORING_OP_ASYNC_CANCEL] = {},
748 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700749 .async_ctx = 1,
750 .needs_mm = 1,
751 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300752 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700753 .async_ctx = 1,
754 .needs_mm = 1,
755 .needs_file = 1,
756 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700757 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700758 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300759 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700760 .needs_file = 1,
761 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300762 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700763 .needs_file = 1,
764 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700765 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700766 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700767 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300768 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700769 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700770 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700771 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300772 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700773 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700774 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700775 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300776 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700777 .needs_mm = 1,
778 .needs_file = 1,
779 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700780 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700781 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300782 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700783 .needs_mm = 1,
784 .needs_file = 1,
785 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700786 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700787 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700788 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300789 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700790 .needs_mm = 1,
791 .needs_file = 1,
792 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700793 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700796 .needs_file = 1,
797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700799 .needs_mm = 1,
800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700802 .needs_mm = 1,
803 .needs_file = 1,
804 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700805 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700806 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300807 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700808 .needs_mm = 1,
809 .needs_file = 1,
810 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700811 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700812 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700813 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300814 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700815 .needs_file = 1,
816 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700817 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700818 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700819 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700820 [IORING_OP_EPOLL_CTL] = {
821 .unbound_nonreg_file = 1,
822 .file_table = 1,
823 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300824 [IORING_OP_SPLICE] = {
825 .needs_file = 1,
826 .hash_reg_file = 1,
827 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700828 },
829 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700830};
831
Jens Axboe561fb042019-10-24 07:25:42 -0600832static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700833static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800834static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700835static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700836static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
837static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700838static int __io_sqe_files_update(struct io_ring_ctx *ctx,
839 struct io_uring_files_update *ip,
840 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700841static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700842static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300843static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700844static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
845 int fd, struct file **out_file, bool fixed);
846static void __io_queue_sqe(struct io_kiocb *req,
847 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600848
Jens Axboe2b188cc2019-01-07 10:46:33 -0700849static struct kmem_cache *req_cachep;
850
851static const struct file_operations io_uring_fops;
852
853struct sock *io_uring_get_socket(struct file *file)
854{
855#if defined(CONFIG_UNIX)
856 if (file->f_op == &io_uring_fops) {
857 struct io_ring_ctx *ctx = file->private_data;
858
859 return ctx->ring_sock->sk;
860 }
861#endif
862 return NULL;
863}
864EXPORT_SYMBOL(io_uring_get_socket);
865
866static void io_ring_ctx_ref_free(struct percpu_ref *ref)
867{
868 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
869
Jens Axboe206aefd2019-11-07 18:27:42 -0700870 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700871}
872
873static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
874{
875 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700876 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700877
878 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
879 if (!ctx)
880 return NULL;
881
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700882 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
883 if (!ctx->fallback_req)
884 goto err;
885
Jens Axboe206aefd2019-11-07 18:27:42 -0700886 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
887 if (!ctx->completions)
888 goto err;
889
Jens Axboe78076bb2019-12-04 19:56:40 -0700890 /*
891 * Use 5 bits less than the max cq entries, that should give us around
892 * 32 entries per hash list if totally full and uniformly spread.
893 */
894 hash_bits = ilog2(p->cq_entries);
895 hash_bits -= 5;
896 if (hash_bits <= 0)
897 hash_bits = 1;
898 ctx->cancel_hash_bits = hash_bits;
899 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
900 GFP_KERNEL);
901 if (!ctx->cancel_hash)
902 goto err;
903 __hash_init(ctx->cancel_hash, 1U << hash_bits);
904
Roman Gushchin21482892019-05-07 10:01:48 -0700905 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700906 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
907 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700908
909 ctx->flags = p->flags;
910 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700911 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700912 init_completion(&ctx->completions[0]);
913 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700914 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700915 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700916 mutex_init(&ctx->uring_lock);
917 init_waitqueue_head(&ctx->wait);
918 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700919 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600920 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600921 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600922 init_waitqueue_head(&ctx->inflight_wait);
923 spin_lock_init(&ctx->inflight_lock);
924 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700925 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700926err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700927 if (ctx->fallback_req)
928 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700929 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700930 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700931 kfree(ctx);
932 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700933}
934
Bob Liu9d858b22019-11-13 18:06:25 +0800935static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600936{
Jackie Liua197f662019-11-08 08:09:12 -0700937 struct io_ring_ctx *ctx = req->ctx;
938
Jens Axboe498ccd92019-10-25 10:04:25 -0600939 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
940 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600941}
942
Bob Liu9d858b22019-11-13 18:06:25 +0800943static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600944{
Pavel Begunkov87987892020-01-18 01:22:30 +0300945 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800946 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600947
Bob Liu9d858b22019-11-13 18:06:25 +0800948 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600949}
950
951static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600952{
953 struct io_kiocb *req;
954
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600955 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800956 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600957 list_del_init(&req->list);
958 return req;
959 }
960
961 return NULL;
962}
963
Jens Axboe5262f562019-09-17 12:26:57 -0600964static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
965{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600966 struct io_kiocb *req;
967
968 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700969 if (req) {
970 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
971 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800972 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700973 list_del_init(&req->list);
974 return req;
975 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600976 }
977
978 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600979}
980
Jens Axboede0617e2019-04-06 21:51:27 -0600981static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700982{
Hristo Venev75b28af2019-08-26 17:23:46 +0000983 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700984
Pavel Begunkov07910152020-01-17 03:52:46 +0300985 /* order cqe stores with ring update */
986 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700987
Pavel Begunkov07910152020-01-17 03:52:46 +0300988 if (wq_has_sleeper(&ctx->cq_wait)) {
989 wake_up_interruptible(&ctx->cq_wait);
990 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700991 }
992}
993
Jens Axboecccf0ee2020-01-27 16:34:48 -0700994static inline void io_req_work_grab_env(struct io_kiocb *req,
995 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600996{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700997 if (!req->work.mm && def->needs_mm) {
998 mmgrab(current->mm);
999 req->work.mm = current->mm;
1000 }
1001 if (!req->work.creds)
1002 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001003 if (!req->work.fs && def->needs_fs) {
1004 spin_lock(&current->fs->lock);
1005 if (!current->fs->in_exec) {
1006 req->work.fs = current->fs;
1007 req->work.fs->users++;
1008 } else {
1009 req->work.flags |= IO_WQ_WORK_CANCEL;
1010 }
1011 spin_unlock(&current->fs->lock);
1012 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001013 if (!req->work.task_pid)
1014 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001015}
1016
1017static inline void io_req_work_drop_env(struct io_kiocb *req)
1018{
1019 if (req->work.mm) {
1020 mmdrop(req->work.mm);
1021 req->work.mm = NULL;
1022 }
1023 if (req->work.creds) {
1024 put_cred(req->work.creds);
1025 req->work.creds = NULL;
1026 }
Jens Axboeff002b32020-02-07 16:05:21 -07001027 if (req->work.fs) {
1028 struct fs_struct *fs = req->work.fs;
1029
1030 spin_lock(&req->work.fs->lock);
1031 if (--fs->users)
1032 fs = NULL;
1033 spin_unlock(&req->work.fs->lock);
1034 if (fs)
1035 free_fs_struct(fs);
1036 }
Jens Axboe561fb042019-10-24 07:25:42 -06001037}
1038
Jens Axboe94ae5e72019-11-14 19:39:52 -07001039static inline bool io_prep_async_work(struct io_kiocb *req,
1040 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001041{
Jens Axboed3656342019-12-18 09:50:26 -07001042 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -06001043 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -06001044
Jens Axboed3656342019-12-18 09:50:26 -07001045 if (req->flags & REQ_F_ISREG) {
1046 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001047 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -07001048 } else {
1049 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001050 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001051 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001052
1053 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001054
Jens Axboe94ae5e72019-11-14 19:39:52 -07001055 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001056 return do_hashed;
1057}
1058
Jackie Liua197f662019-11-08 08:09:12 -07001059static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001060{
Jackie Liua197f662019-11-08 08:09:12 -07001061 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001062 struct io_kiocb *link;
1063 bool do_hashed;
1064
1065 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001066
1067 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1068 req->flags);
1069 if (!do_hashed) {
1070 io_wq_enqueue(ctx->io_wq, &req->work);
1071 } else {
1072 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1073 file_inode(req->file));
1074 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001075
1076 if (link)
1077 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001078}
1079
Jens Axboe5262f562019-09-17 12:26:57 -06001080static void io_kill_timeout(struct io_kiocb *req)
1081{
1082 int ret;
1083
Jens Axboe2d283902019-12-04 11:08:05 -07001084 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001085 if (ret != -1) {
1086 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001087 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001088 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001089 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001090 }
1091}
1092
1093static void io_kill_timeouts(struct io_ring_ctx *ctx)
1094{
1095 struct io_kiocb *req, *tmp;
1096
1097 spin_lock_irq(&ctx->completion_lock);
1098 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1099 io_kill_timeout(req);
1100 spin_unlock_irq(&ctx->completion_lock);
1101}
1102
Jens Axboede0617e2019-04-06 21:51:27 -06001103static void io_commit_cqring(struct io_ring_ctx *ctx)
1104{
1105 struct io_kiocb *req;
1106
Jens Axboe5262f562019-09-17 12:26:57 -06001107 while ((req = io_get_timeout_req(ctx)) != NULL)
1108 io_kill_timeout(req);
1109
Jens Axboede0617e2019-04-06 21:51:27 -06001110 __io_commit_cqring(ctx);
1111
Pavel Begunkov87987892020-01-18 01:22:30 +03001112 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001113 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001114}
1115
Jens Axboe2b188cc2019-01-07 10:46:33 -07001116static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1117{
Hristo Venev75b28af2019-08-26 17:23:46 +00001118 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001119 unsigned tail;
1120
1121 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001122 /*
1123 * writes to the cq entry need to come after reading head; the
1124 * control dependency is enough as we're using WRITE_ONCE to
1125 * fill the cq entry
1126 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001127 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001128 return NULL;
1129
1130 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001131 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001132}
1133
Jens Axboef2842ab2020-01-08 11:04:00 -07001134static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1135{
Jens Axboef0b493e2020-02-01 21:30:11 -07001136 if (!ctx->cq_ev_fd)
1137 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001138 if (!ctx->eventfd_async)
1139 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001140 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001141}
1142
Jens Axboeb41e9852020-02-17 09:52:41 -07001143static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001144{
1145 if (waitqueue_active(&ctx->wait))
1146 wake_up(&ctx->wait);
1147 if (waitqueue_active(&ctx->sqo_wait))
1148 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001149 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001150 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001151}
1152
Jens Axboec4a2ed72019-11-21 21:01:26 -07001153/* Returns true if there are no backlogged entries after the flush */
1154static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001155{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001156 struct io_rings *rings = ctx->rings;
1157 struct io_uring_cqe *cqe;
1158 struct io_kiocb *req;
1159 unsigned long flags;
1160 LIST_HEAD(list);
1161
1162 if (!force) {
1163 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001164 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001165 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1166 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001167 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001168 }
1169
1170 spin_lock_irqsave(&ctx->completion_lock, flags);
1171
1172 /* if force is set, the ring is going away. always drop after that */
1173 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001174 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001175
Jens Axboec4a2ed72019-11-21 21:01:26 -07001176 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001177 while (!list_empty(&ctx->cq_overflow_list)) {
1178 cqe = io_get_cqring(ctx);
1179 if (!cqe && !force)
1180 break;
1181
1182 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1183 list);
1184 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001185 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001186 if (cqe) {
1187 WRITE_ONCE(cqe->user_data, req->user_data);
1188 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001189 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001190 } else {
1191 WRITE_ONCE(ctx->rings->cq_overflow,
1192 atomic_inc_return(&ctx->cached_cq_overflow));
1193 }
1194 }
1195
1196 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001197 if (cqe) {
1198 clear_bit(0, &ctx->sq_check_overflow);
1199 clear_bit(0, &ctx->cq_check_overflow);
1200 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001201 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1202 io_cqring_ev_posted(ctx);
1203
1204 while (!list_empty(&list)) {
1205 req = list_first_entry(&list, struct io_kiocb, list);
1206 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001207 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001208 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001209
1210 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001211}
1212
Jens Axboebcda7ba2020-02-23 16:42:51 -07001213static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001214{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001215 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001216 struct io_uring_cqe *cqe;
1217
Jens Axboe78e19bb2019-11-06 15:21:34 -07001218 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001219
Jens Axboe2b188cc2019-01-07 10:46:33 -07001220 /*
1221 * If we can't get a cq entry, userspace overflowed the
1222 * submission (by quite a lot). Increment the overflow count in
1223 * the ring.
1224 */
1225 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001226 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001227 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001229 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001230 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001231 WRITE_ONCE(ctx->rings->cq_overflow,
1232 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001233 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001234 if (list_empty(&ctx->cq_overflow_list)) {
1235 set_bit(0, &ctx->sq_check_overflow);
1236 set_bit(0, &ctx->cq_check_overflow);
1237 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001238 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001239 refcount_inc(&req->refs);
1240 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001241 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001242 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001243 }
1244}
1245
Jens Axboebcda7ba2020-02-23 16:42:51 -07001246static void io_cqring_fill_event(struct io_kiocb *req, long res)
1247{
1248 __io_cqring_fill_event(req, res, 0);
1249}
1250
1251static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001252{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001253 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001254 unsigned long flags;
1255
1256 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001257 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001258 io_commit_cqring(ctx);
1259 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1260
Jens Axboe8c838782019-03-12 15:48:16 -06001261 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262}
1263
Jens Axboebcda7ba2020-02-23 16:42:51 -07001264static void io_cqring_add_event(struct io_kiocb *req, long res)
1265{
1266 __io_cqring_add_event(req, res, 0);
1267}
1268
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001269static inline bool io_is_fallback_req(struct io_kiocb *req)
1270{
1271 return req == (struct io_kiocb *)
1272 ((unsigned long) req->ctx->fallback_req & ~1UL);
1273}
1274
1275static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1276{
1277 struct io_kiocb *req;
1278
1279 req = ctx->fallback_req;
1280 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1281 return req;
1282
1283 return NULL;
1284}
1285
Jens Axboe2579f912019-01-09 09:10:43 -07001286static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1287 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288{
Jens Axboefd6fab22019-03-14 16:30:06 -06001289 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001290 struct io_kiocb *req;
1291
Jens Axboe2579f912019-01-09 09:10:43 -07001292 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001293 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001294 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001295 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001296 } else if (!state->free_reqs) {
1297 size_t sz;
1298 int ret;
1299
1300 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001301 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1302
1303 /*
1304 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1305 * retry single alloc to be on the safe side.
1306 */
1307 if (unlikely(ret <= 0)) {
1308 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1309 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001310 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001311 ret = 1;
1312 }
Jens Axboe2579f912019-01-09 09:10:43 -07001313 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001314 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001315 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001316 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001317 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318 }
1319
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001320got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001321 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001322 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001323 req->ctx = ctx;
1324 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001325 /* one is dropped after submission, the other at completion */
1326 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001327 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001328 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001329 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001330fallback:
1331 req = io_get_fallback_req(ctx);
1332 if (req)
1333 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001334 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001335 return NULL;
1336}
1337
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001338static inline void io_put_file(struct io_kiocb *req, struct file *file,
1339 bool fixed)
1340{
1341 if (fixed)
1342 percpu_ref_put(&req->ctx->file_data->refs);
1343 else
1344 fput(file);
1345}
1346
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001347static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001348{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001349 if (likely(!io_is_fallback_req(req)))
1350 kmem_cache_free(req_cachep, req);
1351 else
1352 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1353}
1354
Jens Axboec6ca97b302019-12-28 12:11:08 -07001355static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001356{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001357 if (req->flags & REQ_F_NEED_CLEANUP)
1358 io_cleanup_req(req);
1359
YueHaibing96fd84d2020-01-07 22:22:44 +08001360 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001361 if (req->file)
1362 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001363
1364 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001365}
1366
1367static void __io_free_req(struct io_kiocb *req)
1368{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001369 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001370
Jens Axboefcb323c2019-10-24 12:39:47 -06001371 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001372 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001373 unsigned long flags;
1374
1375 spin_lock_irqsave(&ctx->inflight_lock, flags);
1376 list_del(&req->inflight_entry);
1377 if (waitqueue_active(&ctx->inflight_wait))
1378 wake_up(&ctx->inflight_wait);
1379 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1380 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001381
1382 percpu_ref_put(&req->ctx->refs);
1383 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001384}
1385
Jens Axboec6ca97b302019-12-28 12:11:08 -07001386struct req_batch {
1387 void *reqs[IO_IOPOLL_BATCH];
1388 int to_free;
1389 int need_iter;
1390};
1391
1392static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1393{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001394 int fixed_refs = rb->to_free;
1395
Jens Axboec6ca97b302019-12-28 12:11:08 -07001396 if (!rb->to_free)
1397 return;
1398 if (rb->need_iter) {
1399 int i, inflight = 0;
1400 unsigned long flags;
1401
Jens Axboe10fef4b2020-01-09 07:52:28 -07001402 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001403 for (i = 0; i < rb->to_free; i++) {
1404 struct io_kiocb *req = rb->reqs[i];
1405
Jens Axboe10fef4b2020-01-09 07:52:28 -07001406 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001407 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001408 fixed_refs++;
1409 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001410 if (req->flags & REQ_F_INFLIGHT)
1411 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001412 __io_req_aux_free(req);
1413 }
1414 if (!inflight)
1415 goto do_free;
1416
1417 spin_lock_irqsave(&ctx->inflight_lock, flags);
1418 for (i = 0; i < rb->to_free; i++) {
1419 struct io_kiocb *req = rb->reqs[i];
1420
Jens Axboe10fef4b2020-01-09 07:52:28 -07001421 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001422 list_del(&req->inflight_entry);
1423 if (!--inflight)
1424 break;
1425 }
1426 }
1427 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1428
1429 if (waitqueue_active(&ctx->inflight_wait))
1430 wake_up(&ctx->inflight_wait);
1431 }
1432do_free:
1433 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001434 if (fixed_refs)
1435 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001436 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001437 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001438}
1439
Jackie Liua197f662019-11-08 08:09:12 -07001440static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001441{
Jackie Liua197f662019-11-08 08:09:12 -07001442 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001443 int ret;
1444
Jens Axboe2d283902019-12-04 11:08:05 -07001445 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001446 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001447 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001448 io_commit_cqring(ctx);
1449 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001450 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001451 return true;
1452 }
1453
1454 return false;
1455}
1456
Jens Axboeba816ad2019-09-28 11:36:45 -06001457static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001458{
Jens Axboe2665abf2019-11-05 12:40:47 -07001459 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001460 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001461
Jens Axboe4d7dd462019-11-20 13:03:52 -07001462 /* Already got next link */
1463 if (req->flags & REQ_F_LINK_NEXT)
1464 return;
1465
Jens Axboe9e645e112019-05-10 16:07:28 -06001466 /*
1467 * The list should never be empty when we are called here. But could
1468 * potentially happen if the chain is messed up, check to be on the
1469 * safe side.
1470 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001471 while (!list_empty(&req->link_list)) {
1472 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1473 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001474
Pavel Begunkov44932332019-12-05 16:16:35 +03001475 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1476 (nxt->flags & REQ_F_TIMEOUT))) {
1477 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001478 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001479 req->flags &= ~REQ_F_LINK_TIMEOUT;
1480 continue;
1481 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001482
Pavel Begunkov44932332019-12-05 16:16:35 +03001483 list_del_init(&req->link_list);
1484 if (!list_empty(&nxt->link_list))
1485 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001486 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001487 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001488 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001489
Jens Axboe4d7dd462019-11-20 13:03:52 -07001490 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001491 if (wake_ev)
1492 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001493}
1494
1495/*
1496 * Called if REQ_F_LINK is set, and we fail the head request
1497 */
1498static void io_fail_links(struct io_kiocb *req)
1499{
Jens Axboe2665abf2019-11-05 12:40:47 -07001500 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001501 unsigned long flags;
1502
1503 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001504
1505 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001506 struct io_kiocb *link = list_first_entry(&req->link_list,
1507 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001508
Pavel Begunkov44932332019-12-05 16:16:35 +03001509 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001510 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001511
1512 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001513 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001514 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001515 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001516 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001517 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001518 }
Jens Axboe5d960722019-11-19 15:31:28 -07001519 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001520 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001521
1522 io_commit_cqring(ctx);
1523 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1524 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001525}
1526
Jens Axboe4d7dd462019-11-20 13:03:52 -07001527static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001528{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001529 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001530 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001531
Jens Axboe9e645e112019-05-10 16:07:28 -06001532 /*
1533 * If LINK is set, we have dependent requests in this chain. If we
1534 * didn't fail this request, queue the first one up, moving any other
1535 * dependencies to the next request. In case of failure, fail the rest
1536 * of the chain.
1537 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001538 if (req->flags & REQ_F_FAIL_LINK) {
1539 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001540 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1541 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001542 struct io_ring_ctx *ctx = req->ctx;
1543 unsigned long flags;
1544
1545 /*
1546 * If this is a timeout link, we could be racing with the
1547 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001548 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001549 */
1550 spin_lock_irqsave(&ctx->completion_lock, flags);
1551 io_req_link_next(req, nxt);
1552 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1553 } else {
1554 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001555 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001556}
Jens Axboe9e645e112019-05-10 16:07:28 -06001557
Jackie Liuc69f8db2019-11-09 11:00:08 +08001558static void io_free_req(struct io_kiocb *req)
1559{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001560 struct io_kiocb *nxt = NULL;
1561
1562 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001563 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001564
1565 if (nxt)
1566 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001567}
1568
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001569static void io_link_work_cb(struct io_wq_work **workptr)
1570{
1571 struct io_wq_work *work = *workptr;
1572 struct io_kiocb *link = work->data;
1573
1574 io_queue_linked_timeout(link);
1575 io_wq_submit_work(workptr);
1576}
1577
1578static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1579{
1580 struct io_kiocb *link;
1581
1582 *workptr = &nxt->work;
1583 link = io_prep_linked_timeout(nxt);
1584 if (link) {
1585 nxt->work.func = io_link_work_cb;
1586 nxt->work.data = link;
1587 }
1588}
1589
Jens Axboeba816ad2019-09-28 11:36:45 -06001590/*
1591 * Drop reference to request, return next in chain (if there is one) if this
1592 * was the last reference to this request.
1593 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001594__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001595static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001596{
Jens Axboe2a44f462020-02-25 13:25:41 -07001597 if (refcount_dec_and_test(&req->refs)) {
1598 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001599 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001600 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001601}
1602
Jens Axboe2b188cc2019-01-07 10:46:33 -07001603static void io_put_req(struct io_kiocb *req)
1604{
Jens Axboedef596e2019-01-09 08:59:42 -07001605 if (refcount_dec_and_test(&req->refs))
1606 io_free_req(req);
1607}
1608
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001609static void io_steal_work(struct io_kiocb *req,
1610 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001611{
1612 /*
1613 * It's in an io-wq worker, so there always should be at least
1614 * one reference, which will be dropped in io_put_work() just
1615 * after the current handler returns.
1616 *
1617 * It also means, that if the counter dropped to 1, then there is
1618 * no asynchronous users left, so it's safe to steal the next work.
1619 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001620 if (refcount_read(&req->refs) == 1) {
1621 struct io_kiocb *nxt = NULL;
1622
1623 io_req_find_next(req, &nxt);
1624 if (nxt)
1625 io_wq_assign_next(workptr, nxt);
1626 }
1627}
1628
Jens Axboe978db572019-11-14 22:39:04 -07001629/*
1630 * Must only be used if we don't need to care about links, usually from
1631 * within the completion handling itself.
1632 */
1633static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001634{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001635 /* drop both submit and complete references */
1636 if (refcount_sub_and_test(2, &req->refs))
1637 __io_free_req(req);
1638}
1639
Jens Axboe978db572019-11-14 22:39:04 -07001640static void io_double_put_req(struct io_kiocb *req)
1641{
1642 /* drop both submit and complete references */
1643 if (refcount_sub_and_test(2, &req->refs))
1644 io_free_req(req);
1645}
1646
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001647static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001648{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001649 struct io_rings *rings = ctx->rings;
1650
Jens Axboead3eb2c2019-12-18 17:12:20 -07001651 if (test_bit(0, &ctx->cq_check_overflow)) {
1652 /*
1653 * noflush == true is from the waitqueue handler, just ensure
1654 * we wake up the task, and the next invocation will flush the
1655 * entries. We cannot safely to it from here.
1656 */
1657 if (noflush && !list_empty(&ctx->cq_overflow_list))
1658 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001659
Jens Axboead3eb2c2019-12-18 17:12:20 -07001660 io_cqring_overflow_flush(ctx, false);
1661 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001662
Jens Axboea3a0e432019-08-20 11:03:11 -06001663 /* See comment at the top of this file */
1664 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001665 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001666}
1667
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001668static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1669{
1670 struct io_rings *rings = ctx->rings;
1671
1672 /* make sure SQ entry isn't read before tail */
1673 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1674}
1675
Jens Axboe8237e042019-12-28 10:48:22 -07001676static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001677{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001678 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1679 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001680
Jens Axboec6ca97b302019-12-28 12:11:08 -07001681 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1682 rb->need_iter++;
1683
1684 rb->reqs[rb->to_free++] = req;
1685 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1686 io_free_req_many(req->ctx, rb);
1687 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001688}
1689
Jens Axboebcda7ba2020-02-23 16:42:51 -07001690static int io_put_kbuf(struct io_kiocb *req)
1691{
Jens Axboe4d954c22020-02-27 07:31:19 -07001692 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001693 int cflags;
1694
Jens Axboe4d954c22020-02-27 07:31:19 -07001695 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001696 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1697 cflags |= IORING_CQE_F_BUFFER;
1698 req->rw.addr = 0;
1699 kfree(kbuf);
1700 return cflags;
1701}
1702
Jens Axboedef596e2019-01-09 08:59:42 -07001703/*
1704 * Find and free completed poll iocbs
1705 */
1706static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1707 struct list_head *done)
1708{
Jens Axboe8237e042019-12-28 10:48:22 -07001709 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001710 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001711
Jens Axboec6ca97b302019-12-28 12:11:08 -07001712 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001713 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001714 int cflags = 0;
1715
Jens Axboedef596e2019-01-09 08:59:42 -07001716 req = list_first_entry(done, struct io_kiocb, list);
1717 list_del(&req->list);
1718
Jens Axboebcda7ba2020-02-23 16:42:51 -07001719 if (req->flags & REQ_F_BUFFER_SELECTED)
1720 cflags = io_put_kbuf(req);
1721
1722 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001723 (*nr_events)++;
1724
Jens Axboe8237e042019-12-28 10:48:22 -07001725 if (refcount_dec_and_test(&req->refs) &&
1726 !io_req_multi_free(&rb, req))
1727 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001728 }
Jens Axboedef596e2019-01-09 08:59:42 -07001729
Jens Axboe09bb8392019-03-13 12:39:28 -06001730 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001731 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001732}
1733
1734static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1735 long min)
1736{
1737 struct io_kiocb *req, *tmp;
1738 LIST_HEAD(done);
1739 bool spin;
1740 int ret;
1741
1742 /*
1743 * Only spin for completions if we don't have multiple devices hanging
1744 * off our complete list, and we're under the requested amount.
1745 */
1746 spin = !ctx->poll_multi_file && *nr_events < min;
1747
1748 ret = 0;
1749 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001750 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001751
1752 /*
1753 * Move completed entries to our local list. If we find a
1754 * request that requires polling, break out and complete
1755 * the done list first, if we have entries there.
1756 */
1757 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1758 list_move_tail(&req->list, &done);
1759 continue;
1760 }
1761 if (!list_empty(&done))
1762 break;
1763
1764 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1765 if (ret < 0)
1766 break;
1767
1768 if (ret && spin)
1769 spin = false;
1770 ret = 0;
1771 }
1772
1773 if (!list_empty(&done))
1774 io_iopoll_complete(ctx, nr_events, &done);
1775
1776 return ret;
1777}
1778
1779/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001780 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001781 * non-spinning poll check - we'll still enter the driver poll loop, but only
1782 * as a non-spinning completion check.
1783 */
1784static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1785 long min)
1786{
Jens Axboe08f54392019-08-21 22:19:11 -06001787 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001788 int ret;
1789
1790 ret = io_do_iopoll(ctx, nr_events, min);
1791 if (ret < 0)
1792 return ret;
1793 if (!min || *nr_events >= min)
1794 return 0;
1795 }
1796
1797 return 1;
1798}
1799
1800/*
1801 * We can't just wait for polled events to come to us, we have to actively
1802 * find and complete them.
1803 */
1804static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1805{
1806 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1807 return;
1808
1809 mutex_lock(&ctx->uring_lock);
1810 while (!list_empty(&ctx->poll_list)) {
1811 unsigned int nr_events = 0;
1812
1813 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001814
1815 /*
1816 * Ensure we allow local-to-the-cpu processing to take place,
1817 * in this case we need to ensure that we reap all events.
1818 */
1819 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001820 }
1821 mutex_unlock(&ctx->uring_lock);
1822}
1823
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001824static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1825 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001826{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001827 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001828
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001829 /*
1830 * We disallow the app entering submit/complete with polling, but we
1831 * still need to lock the ring to prevent racing with polled issue
1832 * that got punted to a workqueue.
1833 */
1834 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001835 do {
1836 int tmin = 0;
1837
Jens Axboe500f9fb2019-08-19 12:15:59 -06001838 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001839 * Don't enter poll loop if we already have events pending.
1840 * If we do, we can potentially be spinning for commands that
1841 * already triggered a CQE (eg in error).
1842 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001843 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001844 break;
1845
1846 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001847 * If a submit got punted to a workqueue, we can have the
1848 * application entering polling for a command before it gets
1849 * issued. That app will hold the uring_lock for the duration
1850 * of the poll right here, so we need to take a breather every
1851 * now and then to ensure that the issue has a chance to add
1852 * the poll to the issued list. Otherwise we can spin here
1853 * forever, while the workqueue is stuck trying to acquire the
1854 * very same mutex.
1855 */
1856 if (!(++iters & 7)) {
1857 mutex_unlock(&ctx->uring_lock);
1858 mutex_lock(&ctx->uring_lock);
1859 }
1860
Jens Axboedef596e2019-01-09 08:59:42 -07001861 if (*nr_events < min)
1862 tmin = min - *nr_events;
1863
1864 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1865 if (ret <= 0)
1866 break;
1867 ret = 0;
1868 } while (min && !*nr_events && !need_resched());
1869
Jens Axboe500f9fb2019-08-19 12:15:59 -06001870 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001871 return ret;
1872}
1873
Jens Axboe491381ce2019-10-17 09:20:46 -06001874static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001875{
Jens Axboe491381ce2019-10-17 09:20:46 -06001876 /*
1877 * Tell lockdep we inherited freeze protection from submission
1878 * thread.
1879 */
1880 if (req->flags & REQ_F_ISREG) {
1881 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001882
Jens Axboe491381ce2019-10-17 09:20:46 -06001883 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001884 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001885 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001886}
1887
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001888static inline void req_set_fail_links(struct io_kiocb *req)
1889{
1890 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1891 req->flags |= REQ_F_FAIL_LINK;
1892}
1893
Jens Axboeba816ad2019-09-28 11:36:45 -06001894static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001895{
Jens Axboe9adbd452019-12-20 08:45:55 -07001896 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001897 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898
Jens Axboe491381ce2019-10-17 09:20:46 -06001899 if (kiocb->ki_flags & IOCB_WRITE)
1900 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001902 if (res != req->result)
1903 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001904 if (req->flags & REQ_F_BUFFER_SELECTED)
1905 cflags = io_put_kbuf(req);
1906 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001907}
1908
1909static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1910{
Jens Axboe9adbd452019-12-20 08:45:55 -07001911 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001912
1913 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001914 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001915}
1916
Jens Axboedef596e2019-01-09 08:59:42 -07001917static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1918{
Jens Axboe9adbd452019-12-20 08:45:55 -07001919 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001920
Jens Axboe491381ce2019-10-17 09:20:46 -06001921 if (kiocb->ki_flags & IOCB_WRITE)
1922 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001923
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001924 if (res != req->result)
1925 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001926 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001927 if (res != -EAGAIN)
1928 req->flags |= REQ_F_IOPOLL_COMPLETED;
1929}
1930
1931/*
1932 * After the iocb has been issued, it's safe to be found on the poll list.
1933 * Adding the kiocb to the list AFTER submission ensures that we don't
1934 * find it from a io_iopoll_getevents() thread before the issuer is done
1935 * accessing the kiocb cookie.
1936 */
1937static void io_iopoll_req_issued(struct io_kiocb *req)
1938{
1939 struct io_ring_ctx *ctx = req->ctx;
1940
1941 /*
1942 * Track whether we have multiple files in our lists. This will impact
1943 * how we do polling eventually, not spinning if we're on potentially
1944 * different devices.
1945 */
1946 if (list_empty(&ctx->poll_list)) {
1947 ctx->poll_multi_file = false;
1948 } else if (!ctx->poll_multi_file) {
1949 struct io_kiocb *list_req;
1950
1951 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1952 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001953 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001954 ctx->poll_multi_file = true;
1955 }
1956
1957 /*
1958 * For fast devices, IO may have already completed. If it has, add
1959 * it to the front so we find it first.
1960 */
1961 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1962 list_add(&req->list, &ctx->poll_list);
1963 else
1964 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001965
1966 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1967 wq_has_sleeper(&ctx->sqo_wait))
1968 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001969}
1970
Jens Axboe3d6770f2019-04-13 11:50:54 -06001971static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001972{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001973 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001974 int diff = state->has_refs - state->used_refs;
1975
1976 if (diff)
1977 fput_many(state->file, diff);
1978 state->file = NULL;
1979 }
1980}
1981
1982/*
1983 * Get as many references to a file as we have IOs left in this submission,
1984 * assuming most submissions are for one file, or at least that each file
1985 * has more than one submission.
1986 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001987static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001988{
1989 if (!state)
1990 return fget(fd);
1991
1992 if (state->file) {
1993 if (state->fd == fd) {
1994 state->used_refs++;
1995 state->ios_left--;
1996 return state->file;
1997 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001998 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001999 }
2000 state->file = fget_many(fd, state->ios_left);
2001 if (!state->file)
2002 return NULL;
2003
2004 state->fd = fd;
2005 state->has_refs = state->ios_left;
2006 state->used_refs = 1;
2007 state->ios_left--;
2008 return state->file;
2009}
2010
Jens Axboe2b188cc2019-01-07 10:46:33 -07002011/*
2012 * If we tracked the file through the SCM inflight mechanism, we could support
2013 * any file. For now, just ensure that anything potentially problematic is done
2014 * inline.
2015 */
2016static bool io_file_supports_async(struct file *file)
2017{
2018 umode_t mode = file_inode(file)->i_mode;
2019
Jens Axboe10d59342019-12-09 20:16:22 -07002020 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002021 return true;
2022 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2023 return true;
2024
2025 return false;
2026}
2027
Jens Axboe3529d8c2019-12-19 18:24:38 -07002028static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2029 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002030{
Jens Axboedef596e2019-01-09 08:59:42 -07002031 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002032 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002033 unsigned ioprio;
2034 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002035
Jens Axboe491381ce2019-10-17 09:20:46 -06002036 if (S_ISREG(file_inode(req->file)->i_mode))
2037 req->flags |= REQ_F_ISREG;
2038
Jens Axboe2b188cc2019-01-07 10:46:33 -07002039 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002040 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2041 req->flags |= REQ_F_CUR_POS;
2042 kiocb->ki_pos = req->file->f_pos;
2043 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002044 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002045 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2046 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2047 if (unlikely(ret))
2048 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002049
2050 ioprio = READ_ONCE(sqe->ioprio);
2051 if (ioprio) {
2052 ret = ioprio_check_cap(ioprio);
2053 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002054 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002055
2056 kiocb->ki_ioprio = ioprio;
2057 } else
2058 kiocb->ki_ioprio = get_current_ioprio();
2059
Stefan Bühler8449eed2019-04-27 20:34:19 +02002060 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002061 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2062 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002063 req->flags |= REQ_F_NOWAIT;
2064
2065 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002066 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002067
Jens Axboedef596e2019-01-09 08:59:42 -07002068 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002069 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2070 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002071 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002072
Jens Axboedef596e2019-01-09 08:59:42 -07002073 kiocb->ki_flags |= IOCB_HIPRI;
2074 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002075 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002076 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002077 if (kiocb->ki_flags & IOCB_HIPRI)
2078 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002079 kiocb->ki_complete = io_complete_rw;
2080 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002081
Jens Axboe3529d8c2019-12-19 18:24:38 -07002082 req->rw.addr = READ_ONCE(sqe->addr);
2083 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002084 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002085 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002086 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002087 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002088}
2089
2090static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2091{
2092 switch (ret) {
2093 case -EIOCBQUEUED:
2094 break;
2095 case -ERESTARTSYS:
2096 case -ERESTARTNOINTR:
2097 case -ERESTARTNOHAND:
2098 case -ERESTART_RESTARTBLOCK:
2099 /*
2100 * We can't just restart the syscall, since previously
2101 * submitted sqes may already be in progress. Just fail this
2102 * IO with EINTR.
2103 */
2104 ret = -EINTR;
2105 /* fall through */
2106 default:
2107 kiocb->ki_complete(kiocb, ret, 0);
2108 }
2109}
2110
Pavel Begunkov014db002020-03-03 21:33:12 +03002111static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002112{
Jens Axboeba042912019-12-25 16:33:42 -07002113 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2114
2115 if (req->flags & REQ_F_CUR_POS)
2116 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002117 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002118 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002119 else
2120 io_rw_done(kiocb, ret);
2121}
2122
Jens Axboe9adbd452019-12-20 08:45:55 -07002123static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002124 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002125{
Jens Axboe9adbd452019-12-20 08:45:55 -07002126 struct io_ring_ctx *ctx = req->ctx;
2127 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002128 struct io_mapped_ubuf *imu;
2129 unsigned index, buf_index;
2130 size_t offset;
2131 u64 buf_addr;
2132
2133 /* attempt to use fixed buffers without having provided iovecs */
2134 if (unlikely(!ctx->user_bufs))
2135 return -EFAULT;
2136
Jens Axboe9adbd452019-12-20 08:45:55 -07002137 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002138 if (unlikely(buf_index >= ctx->nr_user_bufs))
2139 return -EFAULT;
2140
2141 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2142 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002143 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002144
2145 /* overflow */
2146 if (buf_addr + len < buf_addr)
2147 return -EFAULT;
2148 /* not inside the mapped region */
2149 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2150 return -EFAULT;
2151
2152 /*
2153 * May not be a start of buffer, set size appropriately
2154 * and advance us to the beginning.
2155 */
2156 offset = buf_addr - imu->ubuf;
2157 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002158
2159 if (offset) {
2160 /*
2161 * Don't use iov_iter_advance() here, as it's really slow for
2162 * using the latter parts of a big fixed buffer - it iterates
2163 * over each segment manually. We can cheat a bit here, because
2164 * we know that:
2165 *
2166 * 1) it's a BVEC iter, we set it up
2167 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2168 * first and last bvec
2169 *
2170 * So just find our index, and adjust the iterator afterwards.
2171 * If the offset is within the first bvec (or the whole first
2172 * bvec, just use iov_iter_advance(). This makes it easier
2173 * since we can just skip the first segment, which may not
2174 * be PAGE_SIZE aligned.
2175 */
2176 const struct bio_vec *bvec = imu->bvec;
2177
2178 if (offset <= bvec->bv_len) {
2179 iov_iter_advance(iter, offset);
2180 } else {
2181 unsigned long seg_skip;
2182
2183 /* skip first vec */
2184 offset -= bvec->bv_len;
2185 seg_skip = 1 + (offset >> PAGE_SHIFT);
2186
2187 iter->bvec = bvec + seg_skip;
2188 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002189 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002190 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002191 }
2192 }
2193
Jens Axboe5e559562019-11-13 16:12:46 -07002194 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002195}
2196
Jens Axboebcda7ba2020-02-23 16:42:51 -07002197static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2198{
2199 if (needs_lock)
2200 mutex_unlock(&ctx->uring_lock);
2201}
2202
2203static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2204{
2205 /*
2206 * "Normal" inline submissions always hold the uring_lock, since we
2207 * grab it from the system call. Same is true for the SQPOLL offload.
2208 * The only exception is when we've detached the request and issue it
2209 * from an async worker thread, grab the lock for that case.
2210 */
2211 if (needs_lock)
2212 mutex_lock(&ctx->uring_lock);
2213}
2214
2215static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2216 int bgid, struct io_buffer *kbuf,
2217 bool needs_lock)
2218{
2219 struct io_buffer *head;
2220
2221 if (req->flags & REQ_F_BUFFER_SELECTED)
2222 return kbuf;
2223
2224 io_ring_submit_lock(req->ctx, needs_lock);
2225
2226 lockdep_assert_held(&req->ctx->uring_lock);
2227
2228 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2229 if (head) {
2230 if (!list_empty(&head->list)) {
2231 kbuf = list_last_entry(&head->list, struct io_buffer,
2232 list);
2233 list_del(&kbuf->list);
2234 } else {
2235 kbuf = head;
2236 idr_remove(&req->ctx->io_buffer_idr, bgid);
2237 }
2238 if (*len > kbuf->len)
2239 *len = kbuf->len;
2240 } else {
2241 kbuf = ERR_PTR(-ENOBUFS);
2242 }
2243
2244 io_ring_submit_unlock(req->ctx, needs_lock);
2245
2246 return kbuf;
2247}
2248
Jens Axboe4d954c22020-02-27 07:31:19 -07002249static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2250 bool needs_lock)
2251{
2252 struct io_buffer *kbuf;
2253 int bgid;
2254
2255 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2256 bgid = (int) (unsigned long) req->rw.kiocb.private;
2257 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2258 if (IS_ERR(kbuf))
2259 return kbuf;
2260 req->rw.addr = (u64) (unsigned long) kbuf;
2261 req->flags |= REQ_F_BUFFER_SELECTED;
2262 return u64_to_user_ptr(kbuf->addr);
2263}
2264
2265#ifdef CONFIG_COMPAT
2266static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2267 bool needs_lock)
2268{
2269 struct compat_iovec __user *uiov;
2270 compat_ssize_t clen;
2271 void __user *buf;
2272 ssize_t len;
2273
2274 uiov = u64_to_user_ptr(req->rw.addr);
2275 if (!access_ok(uiov, sizeof(*uiov)))
2276 return -EFAULT;
2277 if (__get_user(clen, &uiov->iov_len))
2278 return -EFAULT;
2279 if (clen < 0)
2280 return -EINVAL;
2281
2282 len = clen;
2283 buf = io_rw_buffer_select(req, &len, needs_lock);
2284 if (IS_ERR(buf))
2285 return PTR_ERR(buf);
2286 iov[0].iov_base = buf;
2287 iov[0].iov_len = (compat_size_t) len;
2288 return 0;
2289}
2290#endif
2291
2292static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2293 bool needs_lock)
2294{
2295 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2296 void __user *buf;
2297 ssize_t len;
2298
2299 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2300 return -EFAULT;
2301
2302 len = iov[0].iov_len;
2303 if (len < 0)
2304 return -EINVAL;
2305 buf = io_rw_buffer_select(req, &len, needs_lock);
2306 if (IS_ERR(buf))
2307 return PTR_ERR(buf);
2308 iov[0].iov_base = buf;
2309 iov[0].iov_len = len;
2310 return 0;
2311}
2312
2313static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2314 bool needs_lock)
2315{
2316 if (req->flags & REQ_F_BUFFER_SELECTED)
2317 return 0;
2318 if (!req->rw.len)
2319 return 0;
2320 else if (req->rw.len > 1)
2321 return -EINVAL;
2322
2323#ifdef CONFIG_COMPAT
2324 if (req->ctx->compat)
2325 return io_compat_import(req, iov, needs_lock);
2326#endif
2327
2328 return __io_iov_buffer_select(req, iov, needs_lock);
2329}
2330
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002331static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002332 struct iovec **iovec, struct iov_iter *iter,
2333 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002334{
Jens Axboe9adbd452019-12-20 08:45:55 -07002335 void __user *buf = u64_to_user_ptr(req->rw.addr);
2336 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002337 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002338 u8 opcode;
2339
Jens Axboed625c6e2019-12-17 19:53:05 -07002340 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002341 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002342 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002343 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002344 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002345
Jens Axboebcda7ba2020-02-23 16:42:51 -07002346 /* buffer index only valid with fixed read/write, or buffer select */
2347 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002348 return -EINVAL;
2349
Jens Axboe3a6820f2019-12-22 15:19:35 -07002350 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002351 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002352 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2353 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002354 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002355 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002356 }
Jens Axboebcda7ba2020-02-23 16:42:51 -07002357 }
2358
Jens Axboe3a6820f2019-12-22 15:19:35 -07002359 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2360 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002361 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002362 }
2363
Jens Axboef67676d2019-12-02 11:03:47 -07002364 if (req->io) {
2365 struct io_async_rw *iorw = &req->io->rw;
2366
2367 *iovec = iorw->iov;
2368 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2369 if (iorw->iov == iorw->fast_iov)
2370 *iovec = NULL;
2371 return iorw->size;
2372 }
2373
Jens Axboe4d954c22020-02-27 07:31:19 -07002374 if (req->flags & REQ_F_BUFFER_SELECT) {
2375 ret = io_iov_buffer_select(req, *iovec, needs_lock);
2376 if (!ret)
2377 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
2378 *iovec = NULL;
2379 return ret;
2380 }
2381
Jens Axboe2b188cc2019-01-07 10:46:33 -07002382#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002383 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002384 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2385 iovec, iter);
2386#endif
2387
2388 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2389}
2390
Jens Axboe32960612019-09-23 11:05:34 -06002391/*
2392 * For files that don't have ->read_iter() and ->write_iter(), handle them
2393 * by looping over ->read() or ->write() manually.
2394 */
2395static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2396 struct iov_iter *iter)
2397{
2398 ssize_t ret = 0;
2399
2400 /*
2401 * Don't support polled IO through this interface, and we can't
2402 * support non-blocking either. For the latter, this just causes
2403 * the kiocb to be handled from an async context.
2404 */
2405 if (kiocb->ki_flags & IOCB_HIPRI)
2406 return -EOPNOTSUPP;
2407 if (kiocb->ki_flags & IOCB_NOWAIT)
2408 return -EAGAIN;
2409
2410 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002411 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002412 ssize_t nr;
2413
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002414 if (!iov_iter_is_bvec(iter)) {
2415 iovec = iov_iter_iovec(iter);
2416 } else {
2417 /* fixed buffers import bvec */
2418 iovec.iov_base = kmap(iter->bvec->bv_page)
2419 + iter->iov_offset;
2420 iovec.iov_len = min(iter->count,
2421 iter->bvec->bv_len - iter->iov_offset);
2422 }
2423
Jens Axboe32960612019-09-23 11:05:34 -06002424 if (rw == READ) {
2425 nr = file->f_op->read(file, iovec.iov_base,
2426 iovec.iov_len, &kiocb->ki_pos);
2427 } else {
2428 nr = file->f_op->write(file, iovec.iov_base,
2429 iovec.iov_len, &kiocb->ki_pos);
2430 }
2431
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002432 if (iov_iter_is_bvec(iter))
2433 kunmap(iter->bvec->bv_page);
2434
Jens Axboe32960612019-09-23 11:05:34 -06002435 if (nr < 0) {
2436 if (!ret)
2437 ret = nr;
2438 break;
2439 }
2440 ret += nr;
2441 if (nr != iovec.iov_len)
2442 break;
2443 iov_iter_advance(iter, nr);
2444 }
2445
2446 return ret;
2447}
2448
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002449static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002450 struct iovec *iovec, struct iovec *fast_iov,
2451 struct iov_iter *iter)
2452{
2453 req->io->rw.nr_segs = iter->nr_segs;
2454 req->io->rw.size = io_size;
2455 req->io->rw.iov = iovec;
2456 if (!req->io->rw.iov) {
2457 req->io->rw.iov = req->io->rw.fast_iov;
2458 memcpy(req->io->rw.iov, fast_iov,
2459 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002460 } else {
2461 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002462 }
2463}
2464
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002465static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002466{
Jens Axboed3656342019-12-18 09:50:26 -07002467 if (!io_op_defs[req->opcode].async_ctx)
2468 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002469 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002470 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002471}
2472
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002473static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2474 struct iovec *iovec, struct iovec *fast_iov,
2475 struct iov_iter *iter)
2476{
Jens Axboe980ad262020-01-24 23:08:54 -07002477 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002478 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002479 if (!req->io) {
2480 if (io_alloc_async_ctx(req))
2481 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002482
Jens Axboe5d204bc2020-01-31 12:06:52 -07002483 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2484 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002485 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002486}
2487
Jens Axboe3529d8c2019-12-19 18:24:38 -07002488static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2489 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002490{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002491 struct io_async_ctx *io;
2492 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002493 ssize_t ret;
2494
Jens Axboe3529d8c2019-12-19 18:24:38 -07002495 ret = io_prep_rw(req, sqe, force_nonblock);
2496 if (ret)
2497 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002498
Jens Axboe3529d8c2019-12-19 18:24:38 -07002499 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2500 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002501
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002502 /* either don't need iovec imported or already have it */
2503 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002504 return 0;
2505
2506 io = req->io;
2507 io->rw.iov = io->rw.fast_iov;
2508 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002509 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002510 req->io = io;
2511 if (ret < 0)
2512 return ret;
2513
2514 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2515 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002516}
2517
Pavel Begunkov014db002020-03-03 21:33:12 +03002518static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002519{
2520 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002521 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002522 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002523 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002524 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002525
Jens Axboebcda7ba2020-02-23 16:42:51 -07002526 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002527 if (ret < 0)
2528 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002529
Jens Axboefd6c2e42019-12-18 12:19:41 -07002530 /* Ensure we clear previously set non-block flag */
2531 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002532 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002533
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002534 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002535 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002536 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002537 req->result = io_size;
2538
2539 /*
2540 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2541 * we know to async punt it even if it was opened O_NONBLOCK
2542 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002543 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002544 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002545
Jens Axboe31b51512019-01-18 22:56:34 -07002546 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002547 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002548 if (!ret) {
2549 ssize_t ret2;
2550
Jens Axboe9adbd452019-12-20 08:45:55 -07002551 if (req->file->f_op->read_iter)
2552 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002553 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002554 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002555
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002556 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002557 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002558 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002559 } else {
2560copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002561 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002562 inline_vecs, &iter);
2563 if (ret)
2564 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002565 /* any defer here is final, must blocking retry */
2566 if (!(req->flags & REQ_F_NOWAIT))
2567 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002568 return -EAGAIN;
2569 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002570 }
Jens Axboef67676d2019-12-02 11:03:47 -07002571out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002572 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002573 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002574 return ret;
2575}
2576
Jens Axboe3529d8c2019-12-19 18:24:38 -07002577static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2578 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002579{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002580 struct io_async_ctx *io;
2581 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002582 ssize_t ret;
2583
Jens Axboe3529d8c2019-12-19 18:24:38 -07002584 ret = io_prep_rw(req, sqe, force_nonblock);
2585 if (ret)
2586 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002587
Jens Axboe3529d8c2019-12-19 18:24:38 -07002588 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2589 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002590
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002591 /* either don't need iovec imported or already have it */
2592 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002593 return 0;
2594
2595 io = req->io;
2596 io->rw.iov = io->rw.fast_iov;
2597 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002598 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002599 req->io = io;
2600 if (ret < 0)
2601 return ret;
2602
2603 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2604 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002605}
2606
Pavel Begunkov014db002020-03-03 21:33:12 +03002607static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002608{
2609 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002610 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002611 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002612 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002613 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002614
Jens Axboebcda7ba2020-02-23 16:42:51 -07002615 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002616 if (ret < 0)
2617 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002618
Jens Axboefd6c2e42019-12-18 12:19:41 -07002619 /* Ensure we clear previously set non-block flag */
2620 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002621 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002622
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002623 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002624 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002625 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002626 req->result = io_size;
2627
2628 /*
2629 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2630 * we know to async punt it even if it was opened O_NONBLOCK
2631 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002632 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002633 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002634
Jens Axboe10d59342019-12-09 20:16:22 -07002635 /* file path doesn't support NOWAIT for non-direct_IO */
2636 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2637 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002638 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002639
Jens Axboe31b51512019-01-18 22:56:34 -07002640 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002641 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002642 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002643 ssize_t ret2;
2644
Jens Axboe2b188cc2019-01-07 10:46:33 -07002645 /*
2646 * Open-code file_start_write here to grab freeze protection,
2647 * which will be released by another thread in
2648 * io_complete_rw(). Fool lockdep by telling it the lock got
2649 * released so that it doesn't complain about the held lock when
2650 * we return to userspace.
2651 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002652 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002653 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002655 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002656 SB_FREEZE_WRITE);
2657 }
2658 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002659
Jens Axboe9adbd452019-12-20 08:45:55 -07002660 if (req->file->f_op->write_iter)
2661 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002662 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002663 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002664 /*
2665 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2666 * retry them without IOCB_NOWAIT.
2667 */
2668 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2669 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002670 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002671 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002672 } else {
2673copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002674 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002675 inline_vecs, &iter);
2676 if (ret)
2677 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002678 /* any defer here is final, must blocking retry */
2679 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002680 return -EAGAIN;
2681 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002682 }
Jens Axboe31b51512019-01-18 22:56:34 -07002683out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002684 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002685 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002686 return ret;
2687}
2688
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002689static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2690{
2691 struct io_splice* sp = &req->splice;
2692 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2693 int ret;
2694
2695 if (req->flags & REQ_F_NEED_CLEANUP)
2696 return 0;
2697
2698 sp->file_in = NULL;
2699 sp->off_in = READ_ONCE(sqe->splice_off_in);
2700 sp->off_out = READ_ONCE(sqe->off);
2701 sp->len = READ_ONCE(sqe->len);
2702 sp->flags = READ_ONCE(sqe->splice_flags);
2703
2704 if (unlikely(sp->flags & ~valid_flags))
2705 return -EINVAL;
2706
2707 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2708 (sp->flags & SPLICE_F_FD_IN_FIXED));
2709 if (ret)
2710 return ret;
2711 req->flags |= REQ_F_NEED_CLEANUP;
2712
2713 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2714 req->work.flags |= IO_WQ_WORK_UNBOUND;
2715
2716 return 0;
2717}
2718
2719static bool io_splice_punt(struct file *file)
2720{
2721 if (get_pipe_info(file))
2722 return false;
2723 if (!io_file_supports_async(file))
2724 return true;
2725 return !(file->f_mode & O_NONBLOCK);
2726}
2727
Pavel Begunkov014db002020-03-03 21:33:12 +03002728static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002729{
2730 struct io_splice *sp = &req->splice;
2731 struct file *in = sp->file_in;
2732 struct file *out = sp->file_out;
2733 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2734 loff_t *poff_in, *poff_out;
2735 long ret;
2736
2737 if (force_nonblock) {
2738 if (io_splice_punt(in) || io_splice_punt(out))
2739 return -EAGAIN;
2740 flags |= SPLICE_F_NONBLOCK;
2741 }
2742
2743 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2744 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2745 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2746 if (force_nonblock && ret == -EAGAIN)
2747 return -EAGAIN;
2748
2749 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2750 req->flags &= ~REQ_F_NEED_CLEANUP;
2751
2752 io_cqring_add_event(req, ret);
2753 if (ret != sp->len)
2754 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002755 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002756 return 0;
2757}
2758
Jens Axboe2b188cc2019-01-07 10:46:33 -07002759/*
2760 * IORING_OP_NOP just posts a completion event, nothing else.
2761 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002762static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002763{
2764 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002765
Jens Axboedef596e2019-01-09 08:59:42 -07002766 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2767 return -EINVAL;
2768
Jens Axboe78e19bb2019-11-06 15:21:34 -07002769 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002770 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002771 return 0;
2772}
2773
Jens Axboe3529d8c2019-12-19 18:24:38 -07002774static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002775{
Jens Axboe6b063142019-01-10 22:13:58 -07002776 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002777
Jens Axboe09bb8392019-03-13 12:39:28 -06002778 if (!req->file)
2779 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002780
Jens Axboe6b063142019-01-10 22:13:58 -07002781 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002782 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002783 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002784 return -EINVAL;
2785
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002786 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2787 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2788 return -EINVAL;
2789
2790 req->sync.off = READ_ONCE(sqe->off);
2791 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002792 return 0;
2793}
2794
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002795static bool io_req_cancelled(struct io_kiocb *req)
2796{
2797 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2798 req_set_fail_links(req);
2799 io_cqring_add_event(req, -ECANCELED);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002800 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002801 return true;
2802 }
2803
2804 return false;
2805}
2806
Pavel Begunkov014db002020-03-03 21:33:12 +03002807static void __io_fsync(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002808{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002809 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002810 int ret;
2811
Jens Axboe9adbd452019-12-20 08:45:55 -07002812 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002813 end > 0 ? end : LLONG_MAX,
2814 req->sync.flags & IORING_FSYNC_DATASYNC);
2815 if (ret < 0)
2816 req_set_fail_links(req);
2817 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002818 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002819}
2820
2821static void io_fsync_finish(struct io_wq_work **workptr)
2822{
2823 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002824
2825 if (io_req_cancelled(req))
2826 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002827 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002828 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002829}
2830
Pavel Begunkov014db002020-03-03 21:33:12 +03002831static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002832{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002833 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002834 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002835 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002836 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002837 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002838 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002839 return 0;
2840}
2841
Pavel Begunkov014db002020-03-03 21:33:12 +03002842static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002843{
Jens Axboed63d1b52019-12-10 10:38:56 -07002844 int ret;
2845
2846 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2847 req->sync.len);
2848 if (ret < 0)
2849 req_set_fail_links(req);
2850 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002851 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002852}
2853
2854static void io_fallocate_finish(struct io_wq_work **workptr)
2855{
2856 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002857
Pavel Begunkov594506f2020-03-03 21:33:11 +03002858 if (io_req_cancelled(req))
2859 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002860 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002861 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002862}
2863
2864static int io_fallocate_prep(struct io_kiocb *req,
2865 const struct io_uring_sqe *sqe)
2866{
2867 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2868 return -EINVAL;
2869
2870 req->sync.off = READ_ONCE(sqe->off);
2871 req->sync.len = READ_ONCE(sqe->addr);
2872 req->sync.mode = READ_ONCE(sqe->len);
2873 return 0;
2874}
2875
Pavel Begunkov014db002020-03-03 21:33:12 +03002876static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002877{
Jens Axboed63d1b52019-12-10 10:38:56 -07002878 /* fallocate always requiring blocking context */
2879 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002880 req->work.func = io_fallocate_finish;
2881 return -EAGAIN;
2882 }
2883
Pavel Begunkov014db002020-03-03 21:33:12 +03002884 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002885 return 0;
2886}
2887
Jens Axboe15b71ab2019-12-11 11:20:36 -07002888static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2889{
Jens Axboef8748882020-01-08 17:47:02 -07002890 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002891 int ret;
2892
2893 if (sqe->ioprio || sqe->buf_index)
2894 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002895 if (sqe->flags & IOSQE_FIXED_FILE)
2896 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002897 if (req->flags & REQ_F_NEED_CLEANUP)
2898 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002899
2900 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002901 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002902 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002903 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002904
Jens Axboef8748882020-01-08 17:47:02 -07002905 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002906 if (IS_ERR(req->open.filename)) {
2907 ret = PTR_ERR(req->open.filename);
2908 req->open.filename = NULL;
2909 return ret;
2910 }
2911
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002912 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002913 return 0;
2914}
2915
Jens Axboecebdb982020-01-08 17:59:24 -07002916static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2917{
2918 struct open_how __user *how;
2919 const char __user *fname;
2920 size_t len;
2921 int ret;
2922
2923 if (sqe->ioprio || sqe->buf_index)
2924 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002925 if (sqe->flags & IOSQE_FIXED_FILE)
2926 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002927 if (req->flags & REQ_F_NEED_CLEANUP)
2928 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002929
2930 req->open.dfd = READ_ONCE(sqe->fd);
2931 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2932 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2933 len = READ_ONCE(sqe->len);
2934
2935 if (len < OPEN_HOW_SIZE_VER0)
2936 return -EINVAL;
2937
2938 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2939 len);
2940 if (ret)
2941 return ret;
2942
2943 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2944 req->open.how.flags |= O_LARGEFILE;
2945
2946 req->open.filename = getname(fname);
2947 if (IS_ERR(req->open.filename)) {
2948 ret = PTR_ERR(req->open.filename);
2949 req->open.filename = NULL;
2950 return ret;
2951 }
2952
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002953 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002954 return 0;
2955}
2956
Pavel Begunkov014db002020-03-03 21:33:12 +03002957static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002958{
2959 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002960 struct file *file;
2961 int ret;
2962
Jens Axboef86cd202020-01-29 13:46:44 -07002963 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002964 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002965
Jens Axboecebdb982020-01-08 17:59:24 -07002966 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002967 if (ret)
2968 goto err;
2969
Jens Axboecebdb982020-01-08 17:59:24 -07002970 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002971 if (ret < 0)
2972 goto err;
2973
2974 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2975 if (IS_ERR(file)) {
2976 put_unused_fd(ret);
2977 ret = PTR_ERR(file);
2978 } else {
2979 fsnotify_open(file);
2980 fd_install(ret, file);
2981 }
2982err:
2983 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002984 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002985 if (ret < 0)
2986 req_set_fail_links(req);
2987 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002988 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002989 return 0;
2990}
2991
Pavel Begunkov014db002020-03-03 21:33:12 +03002992static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07002993{
2994 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03002995 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07002996}
2997
Jens Axboeddf0322d2020-02-23 16:41:33 -07002998static int io_provide_buffers_prep(struct io_kiocb *req,
2999 const struct io_uring_sqe *sqe)
3000{
3001 struct io_provide_buf *p = &req->pbuf;
3002 u64 tmp;
3003
3004 if (sqe->ioprio || sqe->rw_flags)
3005 return -EINVAL;
3006
3007 tmp = READ_ONCE(sqe->fd);
3008 if (!tmp || tmp > USHRT_MAX)
3009 return -E2BIG;
3010 p->nbufs = tmp;
3011 p->addr = READ_ONCE(sqe->addr);
3012 p->len = READ_ONCE(sqe->len);
3013
3014 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3015 return -EFAULT;
3016
3017 p->bgid = READ_ONCE(sqe->buf_group);
3018 tmp = READ_ONCE(sqe->off);
3019 if (tmp > USHRT_MAX)
3020 return -E2BIG;
3021 p->bid = tmp;
3022 return 0;
3023}
3024
3025static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3026{
3027 struct io_buffer *buf;
3028 u64 addr = pbuf->addr;
3029 int i, bid = pbuf->bid;
3030
3031 for (i = 0; i < pbuf->nbufs; i++) {
3032 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3033 if (!buf)
3034 break;
3035
3036 buf->addr = addr;
3037 buf->len = pbuf->len;
3038 buf->bid = bid;
3039 addr += pbuf->len;
3040 bid++;
3041 if (!*head) {
3042 INIT_LIST_HEAD(&buf->list);
3043 *head = buf;
3044 } else {
3045 list_add_tail(&buf->list, &(*head)->list);
3046 }
3047 }
3048
3049 return i ? i : -ENOMEM;
3050}
3051
Jens Axboeddf0322d2020-02-23 16:41:33 -07003052static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3053{
3054 struct io_provide_buf *p = &req->pbuf;
3055 struct io_ring_ctx *ctx = req->ctx;
3056 struct io_buffer *head, *list;
3057 int ret = 0;
3058
3059 io_ring_submit_lock(ctx, !force_nonblock);
3060
3061 lockdep_assert_held(&ctx->uring_lock);
3062
3063 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3064
3065 ret = io_add_buffers(p, &head);
3066 if (ret < 0)
3067 goto out;
3068
3069 if (!list) {
3070 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3071 GFP_KERNEL);
3072 if (ret < 0) {
3073 while (!list_empty(&head->list)) {
3074 struct io_buffer *buf;
3075
3076 buf = list_first_entry(&head->list,
3077 struct io_buffer, list);
3078 list_del(&buf->list);
3079 kfree(buf);
3080 }
3081 kfree(head);
3082 goto out;
3083 }
3084 }
3085out:
3086 io_ring_submit_unlock(ctx, !force_nonblock);
3087 if (ret < 0)
3088 req_set_fail_links(req);
3089 io_cqring_add_event(req, ret);
3090 io_put_req(req);
3091 return 0;
3092}
3093
Jens Axboe3e4827b2020-01-08 15:18:09 -07003094static int io_epoll_ctl_prep(struct io_kiocb *req,
3095 const struct io_uring_sqe *sqe)
3096{
3097#if defined(CONFIG_EPOLL)
3098 if (sqe->ioprio || sqe->buf_index)
3099 return -EINVAL;
3100
3101 req->epoll.epfd = READ_ONCE(sqe->fd);
3102 req->epoll.op = READ_ONCE(sqe->len);
3103 req->epoll.fd = READ_ONCE(sqe->off);
3104
3105 if (ep_op_has_event(req->epoll.op)) {
3106 struct epoll_event __user *ev;
3107
3108 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3109 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3110 return -EFAULT;
3111 }
3112
3113 return 0;
3114#else
3115 return -EOPNOTSUPP;
3116#endif
3117}
3118
Pavel Begunkov014db002020-03-03 21:33:12 +03003119static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003120{
3121#if defined(CONFIG_EPOLL)
3122 struct io_epoll *ie = &req->epoll;
3123 int ret;
3124
3125 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3126 if (force_nonblock && ret == -EAGAIN)
3127 return -EAGAIN;
3128
3129 if (ret < 0)
3130 req_set_fail_links(req);
3131 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003132 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003133 return 0;
3134#else
3135 return -EOPNOTSUPP;
3136#endif
3137}
3138
Jens Axboec1ca7572019-12-25 22:18:28 -07003139static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3140{
3141#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3142 if (sqe->ioprio || sqe->buf_index || sqe->off)
3143 return -EINVAL;
3144
3145 req->madvise.addr = READ_ONCE(sqe->addr);
3146 req->madvise.len = READ_ONCE(sqe->len);
3147 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3148 return 0;
3149#else
3150 return -EOPNOTSUPP;
3151#endif
3152}
3153
Pavel Begunkov014db002020-03-03 21:33:12 +03003154static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003155{
3156#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3157 struct io_madvise *ma = &req->madvise;
3158 int ret;
3159
3160 if (force_nonblock)
3161 return -EAGAIN;
3162
3163 ret = do_madvise(ma->addr, ma->len, ma->advice);
3164 if (ret < 0)
3165 req_set_fail_links(req);
3166 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003167 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003168 return 0;
3169#else
3170 return -EOPNOTSUPP;
3171#endif
3172}
3173
Jens Axboe4840e412019-12-25 22:03:45 -07003174static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3175{
3176 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3177 return -EINVAL;
3178
3179 req->fadvise.offset = READ_ONCE(sqe->off);
3180 req->fadvise.len = READ_ONCE(sqe->len);
3181 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3182 return 0;
3183}
3184
Pavel Begunkov014db002020-03-03 21:33:12 +03003185static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003186{
3187 struct io_fadvise *fa = &req->fadvise;
3188 int ret;
3189
Jens Axboe3e694262020-02-01 09:22:49 -07003190 if (force_nonblock) {
3191 switch (fa->advice) {
3192 case POSIX_FADV_NORMAL:
3193 case POSIX_FADV_RANDOM:
3194 case POSIX_FADV_SEQUENTIAL:
3195 break;
3196 default:
3197 return -EAGAIN;
3198 }
3199 }
Jens Axboe4840e412019-12-25 22:03:45 -07003200
3201 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3202 if (ret < 0)
3203 req_set_fail_links(req);
3204 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003205 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003206 return 0;
3207}
3208
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003209static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3210{
Jens Axboef8748882020-01-08 17:47:02 -07003211 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003212 unsigned lookup_flags;
3213 int ret;
3214
3215 if (sqe->ioprio || sqe->buf_index)
3216 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07003217 if (sqe->flags & IOSQE_FIXED_FILE)
3218 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003219 if (req->flags & REQ_F_NEED_CLEANUP)
3220 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003221
3222 req->open.dfd = READ_ONCE(sqe->fd);
3223 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003224 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003225 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003226 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003227
Jens Axboec12cedf2020-01-08 17:41:21 -07003228 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003229 return -EINVAL;
3230
Jens Axboef8748882020-01-08 17:47:02 -07003231 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003232 if (IS_ERR(req->open.filename)) {
3233 ret = PTR_ERR(req->open.filename);
3234 req->open.filename = NULL;
3235 return ret;
3236 }
3237
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003238 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003239 return 0;
3240}
3241
Pavel Begunkov014db002020-03-03 21:33:12 +03003242static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003243{
3244 struct io_open *ctx = &req->open;
3245 unsigned lookup_flags;
3246 struct path path;
3247 struct kstat stat;
3248 int ret;
3249
3250 if (force_nonblock)
3251 return -EAGAIN;
3252
Jens Axboec12cedf2020-01-08 17:41:21 -07003253 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003254 return -EINVAL;
3255
3256retry:
3257 /* filename_lookup() drops it, keep a reference */
3258 ctx->filename->refcnt++;
3259
3260 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3261 NULL);
3262 if (ret)
3263 goto err;
3264
Jens Axboec12cedf2020-01-08 17:41:21 -07003265 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003266 path_put(&path);
3267 if (retry_estale(ret, lookup_flags)) {
3268 lookup_flags |= LOOKUP_REVAL;
3269 goto retry;
3270 }
3271 if (!ret)
3272 ret = cp_statx(&stat, ctx->buffer);
3273err:
3274 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003275 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003276 if (ret < 0)
3277 req_set_fail_links(req);
3278 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003279 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003280 return 0;
3281}
3282
Jens Axboeb5dba592019-12-11 14:02:38 -07003283static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3284{
3285 /*
3286 * If we queue this for async, it must not be cancellable. That would
3287 * leave the 'file' in an undeterminate state.
3288 */
3289 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3290
3291 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3292 sqe->rw_flags || sqe->buf_index)
3293 return -EINVAL;
3294 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003295 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003296
3297 req->close.fd = READ_ONCE(sqe->fd);
3298 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003299 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003300 return -EBADF;
3301
3302 return 0;
3303}
3304
Pavel Begunkova93b3332020-02-08 14:04:34 +03003305/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003306static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003307{
3308 int ret;
3309
3310 ret = filp_close(req->close.put_file, req->work.files);
3311 if (ret < 0)
3312 req_set_fail_links(req);
3313 io_cqring_add_event(req, ret);
3314 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003315 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003316}
3317
Jens Axboeb5dba592019-12-11 14:02:38 -07003318static void io_close_finish(struct io_wq_work **workptr)
3319{
3320 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003321
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003322 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003323 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003324 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003325}
3326
Pavel Begunkov014db002020-03-03 21:33:12 +03003327static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003328{
3329 int ret;
3330
3331 req->close.put_file = NULL;
3332 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3333 if (ret < 0)
3334 return ret;
3335
3336 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003337 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003338 /* submission ref will be dropped, take it for async */
3339 refcount_inc(&req->refs);
3340
Pavel Begunkova2100672020-03-02 23:45:16 +03003341 req->work.func = io_close_finish;
3342 /*
3343 * Do manual async queue here to avoid grabbing files - we don't
3344 * need the files, and it'll cause io_close_finish() to close
3345 * the file again and cause a double CQE entry for this request
3346 */
3347 io_queue_async_work(req);
3348 return 0;
3349 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003350
3351 /*
3352 * No ->flush(), safely close from here and just punt the
3353 * fput() to async context.
3354 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003355 __io_close_finish(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003356 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003357}
3358
Jens Axboe3529d8c2019-12-19 18:24:38 -07003359static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003360{
3361 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003362
3363 if (!req->file)
3364 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003365
3366 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3367 return -EINVAL;
3368 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3369 return -EINVAL;
3370
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003371 req->sync.off = READ_ONCE(sqe->off);
3372 req->sync.len = READ_ONCE(sqe->len);
3373 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003374 return 0;
3375}
3376
Pavel Begunkov014db002020-03-03 21:33:12 +03003377static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003378{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003379 int ret;
3380
Jens Axboe9adbd452019-12-20 08:45:55 -07003381 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003382 req->sync.flags);
3383 if (ret < 0)
3384 req_set_fail_links(req);
3385 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003386 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003387}
3388
3389
3390static void io_sync_file_range_finish(struct io_wq_work **workptr)
3391{
3392 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3393 struct io_kiocb *nxt = NULL;
3394
3395 if (io_req_cancelled(req))
3396 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003397 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003398 io_put_req(req); /* put submission ref */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003399 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003400 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003401}
3402
Pavel Begunkov014db002020-03-03 21:33:12 +03003403static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003404{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003405 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003406 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003407 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003408 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003409 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003410
Pavel Begunkov014db002020-03-03 21:33:12 +03003411 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003412 return 0;
3413}
3414
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003415static int io_setup_async_msg(struct io_kiocb *req,
3416 struct io_async_msghdr *kmsg)
3417{
3418 if (req->io)
3419 return -EAGAIN;
3420 if (io_alloc_async_ctx(req)) {
3421 if (kmsg->iov != kmsg->fast_iov)
3422 kfree(kmsg->iov);
3423 return -ENOMEM;
3424 }
3425 req->flags |= REQ_F_NEED_CLEANUP;
3426 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3427 return -EAGAIN;
3428}
3429
Jens Axboe3529d8c2019-12-19 18:24:38 -07003430static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003431{
Jens Axboe03b12302019-12-02 18:50:25 -07003432#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003433 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003434 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003435 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003436
Jens Axboee47293f2019-12-20 08:58:21 -07003437 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3438 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003439 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003440
Jens Axboed8768362020-02-27 14:17:49 -07003441#ifdef CONFIG_COMPAT
3442 if (req->ctx->compat)
3443 sr->msg_flags |= MSG_CMSG_COMPAT;
3444#endif
3445
Jens Axboefddafac2020-01-04 20:19:44 -07003446 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003447 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003448 /* iovec is already imported */
3449 if (req->flags & REQ_F_NEED_CLEANUP)
3450 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003451
Jens Axboed9688562019-12-09 19:35:20 -07003452 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003453 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003454 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003455 if (!ret)
3456 req->flags |= REQ_F_NEED_CLEANUP;
3457 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003458#else
Jens Axboee47293f2019-12-20 08:58:21 -07003459 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003460#endif
3461}
3462
Pavel Begunkov014db002020-03-03 21:33:12 +03003463static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003464{
3465#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003466 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003467 struct socket *sock;
3468 int ret;
3469
3470 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3471 return -EINVAL;
3472
3473 sock = sock_from_file(req->file, &ret);
3474 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003475 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003476 unsigned flags;
3477
Jens Axboe03b12302019-12-02 18:50:25 -07003478 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003479 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003480 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003481 /* if iov is set, it's allocated already */
3482 if (!kmsg->iov)
3483 kmsg->iov = kmsg->fast_iov;
3484 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003485 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003486 struct io_sr_msg *sr = &req->sr_msg;
3487
Jens Axboe0b416c32019-12-15 10:57:46 -07003488 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003489 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003490
3491 io.msg.iov = io.msg.fast_iov;
3492 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3493 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003494 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003495 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003496 }
3497
Jens Axboee47293f2019-12-20 08:58:21 -07003498 flags = req->sr_msg.msg_flags;
3499 if (flags & MSG_DONTWAIT)
3500 req->flags |= REQ_F_NOWAIT;
3501 else if (force_nonblock)
3502 flags |= MSG_DONTWAIT;
3503
Jens Axboe0b416c32019-12-15 10:57:46 -07003504 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003505 if (force_nonblock && ret == -EAGAIN)
3506 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003507 if (ret == -ERESTARTSYS)
3508 ret = -EINTR;
3509 }
3510
Pavel Begunkov1e950812020-02-06 19:51:16 +03003511 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003512 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003513 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003514 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003515 if (ret < 0)
3516 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003517 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003518 return 0;
3519#else
3520 return -EOPNOTSUPP;
3521#endif
3522}
3523
Pavel Begunkov014db002020-03-03 21:33:12 +03003524static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003525{
3526#if defined(CONFIG_NET)
3527 struct socket *sock;
3528 int ret;
3529
3530 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3531 return -EINVAL;
3532
3533 sock = sock_from_file(req->file, &ret);
3534 if (sock) {
3535 struct io_sr_msg *sr = &req->sr_msg;
3536 struct msghdr msg;
3537 struct iovec iov;
3538 unsigned flags;
3539
3540 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3541 &msg.msg_iter);
3542 if (ret)
3543 return ret;
3544
3545 msg.msg_name = NULL;
3546 msg.msg_control = NULL;
3547 msg.msg_controllen = 0;
3548 msg.msg_namelen = 0;
3549
3550 flags = req->sr_msg.msg_flags;
3551 if (flags & MSG_DONTWAIT)
3552 req->flags |= REQ_F_NOWAIT;
3553 else if (force_nonblock)
3554 flags |= MSG_DONTWAIT;
3555
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003556 msg.msg_flags = flags;
3557 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003558 if (force_nonblock && ret == -EAGAIN)
3559 return -EAGAIN;
3560 if (ret == -ERESTARTSYS)
3561 ret = -EINTR;
3562 }
3563
3564 io_cqring_add_event(req, ret);
3565 if (ret < 0)
3566 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003567 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003568 return 0;
3569#else
3570 return -EOPNOTSUPP;
3571#endif
3572}
3573
Jens Axboe52de1fe2020-02-27 10:15:42 -07003574static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3575{
3576 struct io_sr_msg *sr = &req->sr_msg;
3577 struct iovec __user *uiov;
3578 size_t iov_len;
3579 int ret;
3580
3581 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3582 &uiov, &iov_len);
3583 if (ret)
3584 return ret;
3585
3586 if (req->flags & REQ_F_BUFFER_SELECT) {
3587 if (iov_len > 1)
3588 return -EINVAL;
3589 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3590 return -EFAULT;
3591 sr->len = io->msg.iov[0].iov_len;
3592 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3593 sr->len);
3594 io->msg.iov = NULL;
3595 } else {
3596 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3597 &io->msg.iov, &io->msg.msg.msg_iter);
3598 if (ret > 0)
3599 ret = 0;
3600 }
3601
3602 return ret;
3603}
3604
3605#ifdef CONFIG_COMPAT
3606static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3607 struct io_async_ctx *io)
3608{
3609 struct compat_msghdr __user *msg_compat;
3610 struct io_sr_msg *sr = &req->sr_msg;
3611 struct compat_iovec __user *uiov;
3612 compat_uptr_t ptr;
3613 compat_size_t len;
3614 int ret;
3615
3616 msg_compat = (struct compat_msghdr __user *) sr->msg;
3617 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3618 &ptr, &len);
3619 if (ret)
3620 return ret;
3621
3622 uiov = compat_ptr(ptr);
3623 if (req->flags & REQ_F_BUFFER_SELECT) {
3624 compat_ssize_t clen;
3625
3626 if (len > 1)
3627 return -EINVAL;
3628 if (!access_ok(uiov, sizeof(*uiov)))
3629 return -EFAULT;
3630 if (__get_user(clen, &uiov->iov_len))
3631 return -EFAULT;
3632 if (clen < 0)
3633 return -EINVAL;
3634 sr->len = io->msg.iov[0].iov_len;
3635 io->msg.iov = NULL;
3636 } else {
3637 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3638 &io->msg.iov,
3639 &io->msg.msg.msg_iter);
3640 if (ret < 0)
3641 return ret;
3642 }
3643
3644 return 0;
3645}
3646#endif
3647
3648static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3649{
3650 io->msg.iov = io->msg.fast_iov;
3651
3652#ifdef CONFIG_COMPAT
3653 if (req->ctx->compat)
3654 return __io_compat_recvmsg_copy_hdr(req, io);
3655#endif
3656
3657 return __io_recvmsg_copy_hdr(req, io);
3658}
3659
Jens Axboebcda7ba2020-02-23 16:42:51 -07003660static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3661 int *cflags, bool needs_lock)
3662{
3663 struct io_sr_msg *sr = &req->sr_msg;
3664 struct io_buffer *kbuf;
3665
3666 if (!(req->flags & REQ_F_BUFFER_SELECT))
3667 return NULL;
3668
3669 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3670 if (IS_ERR(kbuf))
3671 return kbuf;
3672
3673 sr->kbuf = kbuf;
3674 req->flags |= REQ_F_BUFFER_SELECTED;
3675
3676 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3677 *cflags |= IORING_CQE_F_BUFFER;
3678 return kbuf;
3679}
3680
Jens Axboe3529d8c2019-12-19 18:24:38 -07003681static int io_recvmsg_prep(struct io_kiocb *req,
3682 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003683{
3684#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003685 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003686 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003687 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003688
Jens Axboe3529d8c2019-12-19 18:24:38 -07003689 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3690 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003691 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003692 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003693
Jens Axboed8768362020-02-27 14:17:49 -07003694#ifdef CONFIG_COMPAT
3695 if (req->ctx->compat)
3696 sr->msg_flags |= MSG_CMSG_COMPAT;
3697#endif
3698
Jens Axboefddafac2020-01-04 20:19:44 -07003699 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003700 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003701 /* iovec is already imported */
3702 if (req->flags & REQ_F_NEED_CLEANUP)
3703 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003704
Jens Axboe52de1fe2020-02-27 10:15:42 -07003705 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003706 if (!ret)
3707 req->flags |= REQ_F_NEED_CLEANUP;
3708 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003709#else
Jens Axboee47293f2019-12-20 08:58:21 -07003710 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003711#endif
3712}
3713
Pavel Begunkov014db002020-03-03 21:33:12 +03003714static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003715{
3716#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003717 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003718 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003719 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003720
3721 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3722 return -EINVAL;
3723
3724 sock = sock_from_file(req->file, &ret);
3725 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003726 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003727 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003728 unsigned flags;
3729
Jens Axboe03b12302019-12-02 18:50:25 -07003730 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003731 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003732 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003733 /* if iov is set, it's allocated already */
3734 if (!kmsg->iov)
3735 kmsg->iov = kmsg->fast_iov;
3736 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003737 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003738 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003739 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003740
Jens Axboe52de1fe2020-02-27 10:15:42 -07003741 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003742 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003743 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003744 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003745
Jens Axboe52de1fe2020-02-27 10:15:42 -07003746 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3747 if (IS_ERR(kbuf)) {
3748 return PTR_ERR(kbuf);
3749 } else if (kbuf) {
3750 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3751 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3752 1, req->sr_msg.len);
3753 }
3754
Jens Axboee47293f2019-12-20 08:58:21 -07003755 flags = req->sr_msg.msg_flags;
3756 if (flags & MSG_DONTWAIT)
3757 req->flags |= REQ_F_NOWAIT;
3758 else if (force_nonblock)
3759 flags |= MSG_DONTWAIT;
3760
3761 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3762 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003763 if (force_nonblock && ret == -EAGAIN)
3764 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003765 if (ret == -ERESTARTSYS)
3766 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003767 }
3768
Pavel Begunkov1e950812020-02-06 19:51:16 +03003769 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003770 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003771 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003772 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003773 if (ret < 0)
3774 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003775 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003776 return 0;
3777#else
3778 return -EOPNOTSUPP;
3779#endif
3780}
3781
Pavel Begunkov014db002020-03-03 21:33:12 +03003782static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003783{
3784#if defined(CONFIG_NET)
Jens Axboebcda7ba2020-02-23 16:42:51 -07003785 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003786 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003787 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003788
3789 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3790 return -EINVAL;
3791
3792 sock = sock_from_file(req->file, &ret);
3793 if (sock) {
3794 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003795 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003796 struct msghdr msg;
3797 struct iovec iov;
3798 unsigned flags;
3799
Jens Axboebcda7ba2020-02-23 16:42:51 -07003800 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3801 if (IS_ERR(kbuf))
3802 return PTR_ERR(kbuf);
3803 else if (kbuf)
3804 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003805
Jens Axboebcda7ba2020-02-23 16:42:51 -07003806 ret = import_single_range(READ, buf, sr->len, &iov,
3807 &msg.msg_iter);
3808 if (ret) {
3809 kfree(kbuf);
3810 return ret;
3811 }
3812
3813 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003814 msg.msg_name = NULL;
3815 msg.msg_control = NULL;
3816 msg.msg_controllen = 0;
3817 msg.msg_namelen = 0;
3818 msg.msg_iocb = NULL;
3819 msg.msg_flags = 0;
3820
3821 flags = req->sr_msg.msg_flags;
3822 if (flags & MSG_DONTWAIT)
3823 req->flags |= REQ_F_NOWAIT;
3824 else if (force_nonblock)
3825 flags |= MSG_DONTWAIT;
3826
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003827 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003828 if (force_nonblock && ret == -EAGAIN)
3829 return -EAGAIN;
3830 if (ret == -ERESTARTSYS)
3831 ret = -EINTR;
3832 }
3833
Jens Axboebcda7ba2020-02-23 16:42:51 -07003834 kfree(kbuf);
3835 req->flags &= ~REQ_F_NEED_CLEANUP;
3836 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003837 if (ret < 0)
3838 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003839 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003840 return 0;
3841#else
3842 return -EOPNOTSUPP;
3843#endif
3844}
3845
3846
Jens Axboe3529d8c2019-12-19 18:24:38 -07003847static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003848{
3849#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003850 struct io_accept *accept = &req->accept;
3851
Jens Axboe17f2fe32019-10-17 14:42:58 -06003852 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3853 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003854 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003855 return -EINVAL;
3856
Jens Axboed55e5f52019-12-11 16:12:15 -07003857 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3858 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003859 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003860 return 0;
3861#else
3862 return -EOPNOTSUPP;
3863#endif
3864}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003865
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003866#if defined(CONFIG_NET)
Pavel Begunkov014db002020-03-03 21:33:12 +03003867static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003868{
3869 struct io_accept *accept = &req->accept;
3870 unsigned file_flags;
3871 int ret;
3872
3873 file_flags = force_nonblock ? O_NONBLOCK : 0;
3874 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3875 accept->addr_len, accept->flags);
3876 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003877 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003878 if (ret == -ERESTARTSYS)
3879 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003880 if (ret < 0)
3881 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003882 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003883 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003884 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003885}
3886
3887static void io_accept_finish(struct io_wq_work **workptr)
3888{
3889 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003890
3891 if (io_req_cancelled(req))
3892 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003893 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003894 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003895}
3896#endif
3897
Pavel Begunkov014db002020-03-03 21:33:12 +03003898static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003899{
3900#if defined(CONFIG_NET)
3901 int ret;
3902
Pavel Begunkov014db002020-03-03 21:33:12 +03003903 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003904 if (ret == -EAGAIN && force_nonblock) {
3905 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003906 return -EAGAIN;
3907 }
3908 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003909#else
3910 return -EOPNOTSUPP;
3911#endif
3912}
3913
Jens Axboe3529d8c2019-12-19 18:24:38 -07003914static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003915{
3916#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003917 struct io_connect *conn = &req->connect;
3918 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003919
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003920 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3921 return -EINVAL;
3922 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3923 return -EINVAL;
3924
Jens Axboe3529d8c2019-12-19 18:24:38 -07003925 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3926 conn->addr_len = READ_ONCE(sqe->addr2);
3927
3928 if (!io)
3929 return 0;
3930
3931 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003932 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003933#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003934 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003935#endif
3936}
3937
Pavel Begunkov014db002020-03-03 21:33:12 +03003938static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003939{
3940#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003941 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003942 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003943 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003944
Jens Axboef499a022019-12-02 16:28:46 -07003945 if (req->io) {
3946 io = req->io;
3947 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003948 ret = move_addr_to_kernel(req->connect.addr,
3949 req->connect.addr_len,
3950 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003951 if (ret)
3952 goto out;
3953 io = &__io;
3954 }
3955
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003956 file_flags = force_nonblock ? O_NONBLOCK : 0;
3957
3958 ret = __sys_connect_file(req->file, &io->connect.address,
3959 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003960 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003961 if (req->io)
3962 return -EAGAIN;
3963 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003964 ret = -ENOMEM;
3965 goto out;
3966 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003967 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003968 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003969 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003970 if (ret == -ERESTARTSYS)
3971 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003972out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003973 if (ret < 0)
3974 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003975 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003976 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003977 return 0;
3978#else
3979 return -EOPNOTSUPP;
3980#endif
3981}
3982
Jens Axboed7718a92020-02-14 22:23:12 -07003983struct io_poll_table {
3984 struct poll_table_struct pt;
3985 struct io_kiocb *req;
3986 int error;
3987};
3988
3989static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
3990 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003991{
Jens Axboed7718a92020-02-14 22:23:12 -07003992 if (unlikely(poll->head)) {
3993 pt->error = -EINVAL;
3994 return;
3995 }
3996
3997 pt->error = 0;
3998 poll->head = head;
3999 add_wait_queue(head, &poll->wait);
4000}
4001
4002static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4003 struct poll_table_struct *p)
4004{
4005 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4006
4007 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4008}
4009
4010static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4011 __poll_t mask, task_work_func_t func)
4012{
4013 struct task_struct *tsk;
4014
4015 /* for instances that support it check for an event match first: */
4016 if (mask && !(mask & poll->events))
4017 return 0;
4018
4019 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4020
4021 list_del_init(&poll->wait.entry);
4022
4023 tsk = req->task;
4024 req->result = mask;
4025 init_task_work(&req->task_work, func);
4026 /*
4027 * If this fails, then the task is exiting. If that is the case, then
4028 * the exit check will ultimately cancel these work items. Hence we
4029 * don't need to check here and handle it specifically.
4030 */
4031 task_work_add(tsk, &req->task_work, true);
4032 wake_up_process(tsk);
4033 return 1;
4034}
4035
4036static void io_async_task_func(struct callback_head *cb)
4037{
4038 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4039 struct async_poll *apoll = req->apoll;
4040 struct io_ring_ctx *ctx = req->ctx;
4041
4042 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4043
4044 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
4045
4046 if (hash_hashed(&req->hash_node)) {
4047 spin_lock_irq(&ctx->completion_lock);
4048 hash_del(&req->hash_node);
4049 spin_unlock_irq(&ctx->completion_lock);
4050 }
4051
4052 /* restore ->work in case we need to retry again */
4053 memcpy(&req->work, &apoll->work, sizeof(req->work));
4054
4055 __set_current_state(TASK_RUNNING);
4056 mutex_lock(&ctx->uring_lock);
4057 __io_queue_sqe(req, NULL);
4058 mutex_unlock(&ctx->uring_lock);
4059
4060 kfree(apoll);
4061}
4062
4063static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4064 void *key)
4065{
4066 struct io_kiocb *req = wait->private;
4067 struct io_poll_iocb *poll = &req->apoll->poll;
4068
4069 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4070 key_to_poll(key));
4071
4072 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4073}
4074
4075static void io_poll_req_insert(struct io_kiocb *req)
4076{
4077 struct io_ring_ctx *ctx = req->ctx;
4078 struct hlist_head *list;
4079
4080 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4081 hlist_add_head(&req->hash_node, list);
4082}
4083
4084static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4085 struct io_poll_iocb *poll,
4086 struct io_poll_table *ipt, __poll_t mask,
4087 wait_queue_func_t wake_func)
4088 __acquires(&ctx->completion_lock)
4089{
4090 struct io_ring_ctx *ctx = req->ctx;
4091 bool cancel = false;
4092
4093 poll->file = req->file;
4094 poll->head = NULL;
4095 poll->done = poll->canceled = false;
4096 poll->events = mask;
4097
4098 ipt->pt._key = mask;
4099 ipt->req = req;
4100 ipt->error = -EINVAL;
4101
4102 INIT_LIST_HEAD(&poll->wait.entry);
4103 init_waitqueue_func_entry(&poll->wait, wake_func);
4104 poll->wait.private = req;
4105
4106 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4107
4108 spin_lock_irq(&ctx->completion_lock);
4109 if (likely(poll->head)) {
4110 spin_lock(&poll->head->lock);
4111 if (unlikely(list_empty(&poll->wait.entry))) {
4112 if (ipt->error)
4113 cancel = true;
4114 ipt->error = 0;
4115 mask = 0;
4116 }
4117 if (mask || ipt->error)
4118 list_del_init(&poll->wait.entry);
4119 else if (cancel)
4120 WRITE_ONCE(poll->canceled, true);
4121 else if (!poll->done) /* actually waiting for an event */
4122 io_poll_req_insert(req);
4123 spin_unlock(&poll->head->lock);
4124 }
4125
4126 return mask;
4127}
4128
4129static bool io_arm_poll_handler(struct io_kiocb *req)
4130{
4131 const struct io_op_def *def = &io_op_defs[req->opcode];
4132 struct io_ring_ctx *ctx = req->ctx;
4133 struct async_poll *apoll;
4134 struct io_poll_table ipt;
4135 __poll_t mask, ret;
4136
4137 if (!req->file || !file_can_poll(req->file))
4138 return false;
4139 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4140 return false;
4141 if (!def->pollin && !def->pollout)
4142 return false;
4143
4144 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4145 if (unlikely(!apoll))
4146 return false;
4147
4148 req->flags |= REQ_F_POLLED;
4149 memcpy(&apoll->work, &req->work, sizeof(req->work));
4150
4151 /*
4152 * Don't need a reference here, as we're adding it to the task
4153 * task_works list. If the task exits, the list is pruned.
4154 */
4155 req->task = current;
4156 req->apoll = apoll;
4157 INIT_HLIST_NODE(&req->hash_node);
4158
Nathan Chancellor8755d972020-03-02 16:01:19 -07004159 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004160 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004161 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004162 if (def->pollout)
4163 mask |= POLLOUT | POLLWRNORM;
4164 mask |= POLLERR | POLLPRI;
4165
4166 ipt.pt._qproc = io_async_queue_proc;
4167
4168 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4169 io_async_wake);
4170 if (ret) {
4171 ipt.error = 0;
4172 apoll->poll.done = true;
4173 spin_unlock_irq(&ctx->completion_lock);
4174 memcpy(&req->work, &apoll->work, sizeof(req->work));
4175 kfree(apoll);
4176 return false;
4177 }
4178 spin_unlock_irq(&ctx->completion_lock);
4179 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4180 apoll->poll.events);
4181 return true;
4182}
4183
4184static bool __io_poll_remove_one(struct io_kiocb *req,
4185 struct io_poll_iocb *poll)
4186{
Jens Axboeb41e9852020-02-17 09:52:41 -07004187 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004188
4189 spin_lock(&poll->head->lock);
4190 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004191 if (!list_empty(&poll->wait.entry)) {
4192 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004193 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004194 }
4195 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004196 return do_complete;
4197}
4198
4199static bool io_poll_remove_one(struct io_kiocb *req)
4200{
4201 bool do_complete;
4202
4203 if (req->opcode == IORING_OP_POLL_ADD) {
4204 do_complete = __io_poll_remove_one(req, &req->poll);
4205 } else {
4206 /* non-poll requests have submit ref still */
4207 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4208 if (do_complete)
4209 io_put_req(req);
4210 }
4211
Jens Axboe78076bb2019-12-04 19:56:40 -07004212 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004213
Jens Axboeb41e9852020-02-17 09:52:41 -07004214 if (do_complete) {
4215 io_cqring_fill_event(req, -ECANCELED);
4216 io_commit_cqring(req->ctx);
4217 req->flags |= REQ_F_COMP_LOCKED;
4218 io_put_req(req);
4219 }
4220
4221 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004222}
4223
4224static void io_poll_remove_all(struct io_ring_ctx *ctx)
4225{
Jens Axboe78076bb2019-12-04 19:56:40 -07004226 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004227 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07004228 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004229
4230 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004231 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4232 struct hlist_head *list;
4233
4234 list = &ctx->cancel_hash[i];
4235 hlist_for_each_entry_safe(req, tmp, list, hash_node)
4236 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004237 }
4238 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004239
4240 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004241}
4242
Jens Axboe47f46762019-11-09 17:43:02 -07004243static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4244{
Jens Axboe78076bb2019-12-04 19:56:40 -07004245 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004246 struct io_kiocb *req;
4247
Jens Axboe78076bb2019-12-04 19:56:40 -07004248 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4249 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004250 if (sqe_addr != req->user_data)
4251 continue;
4252 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004253 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004254 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004255 }
4256
4257 return -ENOENT;
4258}
4259
Jens Axboe3529d8c2019-12-19 18:24:38 -07004260static int io_poll_remove_prep(struct io_kiocb *req,
4261 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004262{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004263 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4264 return -EINVAL;
4265 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4266 sqe->poll_events)
4267 return -EINVAL;
4268
Jens Axboe0969e782019-12-17 18:40:57 -07004269 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004270 return 0;
4271}
4272
4273/*
4274 * Find a running poll command that matches one specified in sqe->addr,
4275 * and remove it if found.
4276 */
4277static int io_poll_remove(struct io_kiocb *req)
4278{
4279 struct io_ring_ctx *ctx = req->ctx;
4280 u64 addr;
4281 int ret;
4282
Jens Axboe0969e782019-12-17 18:40:57 -07004283 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004284 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004285 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004286 spin_unlock_irq(&ctx->completion_lock);
4287
Jens Axboe78e19bb2019-11-06 15:21:34 -07004288 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004289 if (ret < 0)
4290 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004291 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004292 return 0;
4293}
4294
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004295static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004296{
Jackie Liua197f662019-11-08 08:09:12 -07004297 struct io_ring_ctx *ctx = req->ctx;
4298
Jens Axboe8c838782019-03-12 15:48:16 -06004299 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004300 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004301 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004302}
4303
Jens Axboeb41e9852020-02-17 09:52:41 -07004304static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004305{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004306 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004307
Jens Axboe221c5eb2019-01-17 09:41:58 -07004308 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004309 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004310 io_poll_complete(req, req->result, 0);
4311 req->flags |= REQ_F_COMP_LOCKED;
4312 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004313 spin_unlock_irq(&ctx->completion_lock);
4314
Jens Axboe8c838782019-03-12 15:48:16 -06004315 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07004316}
Jens Axboe89723d02019-11-05 15:32:58 -07004317
Jens Axboeb41e9852020-02-17 09:52:41 -07004318static void io_poll_task_func(struct callback_head *cb)
4319{
4320 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4321 struct io_kiocb *nxt = NULL;
4322
4323 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004324 if (nxt) {
4325 struct io_ring_ctx *ctx = nxt->ctx;
4326
4327 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004328 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004329 mutex_unlock(&ctx->uring_lock);
4330 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004331}
4332
Jens Axboe221c5eb2019-01-17 09:41:58 -07004333static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4334 void *key)
4335{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004336 struct io_kiocb *req = wait->private;
4337 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004338
Jens Axboed7718a92020-02-14 22:23:12 -07004339 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004340}
4341
Jens Axboe221c5eb2019-01-17 09:41:58 -07004342static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4343 struct poll_table_struct *p)
4344{
4345 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4346
Jens Axboed7718a92020-02-14 22:23:12 -07004347 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004348}
4349
Jens Axboe3529d8c2019-12-19 18:24:38 -07004350static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004351{
4352 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004353 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004354
4355 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4356 return -EINVAL;
4357 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4358 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004359 if (!poll->file)
4360 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004361
Jens Axboe221c5eb2019-01-17 09:41:58 -07004362 events = READ_ONCE(sqe->poll_events);
4363 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004364
Jens Axboed7718a92020-02-14 22:23:12 -07004365 /*
4366 * Don't need a reference here, as we're adding it to the task
4367 * task_works list. If the task exits, the list is pruned.
4368 */
Jens Axboeb41e9852020-02-17 09:52:41 -07004369 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004370 return 0;
4371}
4372
Pavel Begunkov014db002020-03-03 21:33:12 +03004373static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004374{
4375 struct io_poll_iocb *poll = &req->poll;
4376 struct io_ring_ctx *ctx = req->ctx;
4377 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004378 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004379
Jens Axboe78076bb2019-12-04 19:56:40 -07004380 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004381 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004382 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004383
Jens Axboed7718a92020-02-14 22:23:12 -07004384 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4385 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004386
Jens Axboe8c838782019-03-12 15:48:16 -06004387 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004388 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004389 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004390 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004391 spin_unlock_irq(&ctx->completion_lock);
4392
Jens Axboe8c838782019-03-12 15:48:16 -06004393 if (mask) {
4394 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004395 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004396 }
Jens Axboe8c838782019-03-12 15:48:16 -06004397 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004398}
4399
Jens Axboe5262f562019-09-17 12:26:57 -06004400static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4401{
Jens Axboead8a48a2019-11-15 08:49:11 -07004402 struct io_timeout_data *data = container_of(timer,
4403 struct io_timeout_data, timer);
4404 struct io_kiocb *req = data->req;
4405 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004406 unsigned long flags;
4407
Jens Axboe5262f562019-09-17 12:26:57 -06004408 atomic_inc(&ctx->cq_timeouts);
4409
4410 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004411 /*
Jens Axboe11365042019-10-16 09:08:32 -06004412 * We could be racing with timeout deletion. If the list is empty,
4413 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004414 */
Jens Axboe842f9612019-10-29 12:34:10 -06004415 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004416 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004417
Jens Axboe11365042019-10-16 09:08:32 -06004418 /*
4419 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004420 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004421 * pointer will be increased, otherwise other timeout reqs may
4422 * return in advance without waiting for enough wait_nr.
4423 */
4424 prev = req;
4425 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4426 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004427 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004428 }
Jens Axboe842f9612019-10-29 12:34:10 -06004429
Jens Axboe78e19bb2019-11-06 15:21:34 -07004430 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004431 io_commit_cqring(ctx);
4432 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4433
4434 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004435 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004436 io_put_req(req);
4437 return HRTIMER_NORESTART;
4438}
4439
Jens Axboe47f46762019-11-09 17:43:02 -07004440static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4441{
4442 struct io_kiocb *req;
4443 int ret = -ENOENT;
4444
4445 list_for_each_entry(req, &ctx->timeout_list, list) {
4446 if (user_data == req->user_data) {
4447 list_del_init(&req->list);
4448 ret = 0;
4449 break;
4450 }
4451 }
4452
4453 if (ret == -ENOENT)
4454 return ret;
4455
Jens Axboe2d283902019-12-04 11:08:05 -07004456 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004457 if (ret == -1)
4458 return -EALREADY;
4459
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004460 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004461 io_cqring_fill_event(req, -ECANCELED);
4462 io_put_req(req);
4463 return 0;
4464}
4465
Jens Axboe3529d8c2019-12-19 18:24:38 -07004466static int io_timeout_remove_prep(struct io_kiocb *req,
4467 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004468{
Jens Axboeb29472e2019-12-17 18:50:29 -07004469 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4470 return -EINVAL;
4471 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4472 return -EINVAL;
4473
4474 req->timeout.addr = READ_ONCE(sqe->addr);
4475 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4476 if (req->timeout.flags)
4477 return -EINVAL;
4478
Jens Axboeb29472e2019-12-17 18:50:29 -07004479 return 0;
4480}
4481
Jens Axboe11365042019-10-16 09:08:32 -06004482/*
4483 * Remove or update an existing timeout command
4484 */
Jens Axboefc4df992019-12-10 14:38:45 -07004485static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004486{
4487 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004488 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004489
Jens Axboe11365042019-10-16 09:08:32 -06004490 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004491 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004492
Jens Axboe47f46762019-11-09 17:43:02 -07004493 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004494 io_commit_cqring(ctx);
4495 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004496 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004497 if (ret < 0)
4498 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004499 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004500 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004501}
4502
Jens Axboe3529d8c2019-12-19 18:24:38 -07004503static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004504 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004505{
Jens Axboead8a48a2019-11-15 08:49:11 -07004506 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004507 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004508
Jens Axboead8a48a2019-11-15 08:49:11 -07004509 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004510 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004511 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004512 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004513 if (sqe->off && is_timeout_link)
4514 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004515 flags = READ_ONCE(sqe->timeout_flags);
4516 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004517 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004518
Jens Axboe26a61672019-12-20 09:02:01 -07004519 req->timeout.count = READ_ONCE(sqe->off);
4520
Jens Axboe3529d8c2019-12-19 18:24:38 -07004521 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004522 return -ENOMEM;
4523
4524 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004525 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004526 req->flags |= REQ_F_TIMEOUT;
4527
4528 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004529 return -EFAULT;
4530
Jens Axboe11365042019-10-16 09:08:32 -06004531 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004532 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004533 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004534 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004535
Jens Axboead8a48a2019-11-15 08:49:11 -07004536 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4537 return 0;
4538}
4539
Jens Axboefc4df992019-12-10 14:38:45 -07004540static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004541{
4542 unsigned count;
4543 struct io_ring_ctx *ctx = req->ctx;
4544 struct io_timeout_data *data;
4545 struct list_head *entry;
4546 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004547
Jens Axboe2d283902019-12-04 11:08:05 -07004548 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004549
Jens Axboe5262f562019-09-17 12:26:57 -06004550 /*
4551 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004552 * timeout event to be satisfied. If it isn't set, then this is
4553 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004554 */
Jens Axboe26a61672019-12-20 09:02:01 -07004555 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004556 if (!count) {
4557 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4558 spin_lock_irq(&ctx->completion_lock);
4559 entry = ctx->timeout_list.prev;
4560 goto add;
4561 }
Jens Axboe5262f562019-09-17 12:26:57 -06004562
4563 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004564 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004565
4566 /*
4567 * Insertion sort, ensuring the first entry in the list is always
4568 * the one we need first.
4569 */
Jens Axboe5262f562019-09-17 12:26:57 -06004570 spin_lock_irq(&ctx->completion_lock);
4571 list_for_each_prev(entry, &ctx->timeout_list) {
4572 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004573 unsigned nxt_sq_head;
4574 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004575 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004576
Jens Axboe93bd25b2019-11-11 23:34:31 -07004577 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4578 continue;
4579
yangerkun5da0fb12019-10-15 21:59:29 +08004580 /*
4581 * Since cached_sq_head + count - 1 can overflow, use type long
4582 * long to store it.
4583 */
4584 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004585 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4586 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004587
4588 /*
4589 * cached_sq_head may overflow, and it will never overflow twice
4590 * once there is some timeout req still be valid.
4591 */
4592 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004593 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004594
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004595 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004596 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004597
4598 /*
4599 * Sequence of reqs after the insert one and itself should
4600 * be adjusted because each timeout req consumes a slot.
4601 */
4602 span++;
4603 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004604 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004605 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004606add:
Jens Axboe5262f562019-09-17 12:26:57 -06004607 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004608 data->timer.function = io_timeout_fn;
4609 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004610 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004611 return 0;
4612}
4613
Jens Axboe62755e32019-10-28 21:49:21 -06004614static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004615{
Jens Axboe62755e32019-10-28 21:49:21 -06004616 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004617
Jens Axboe62755e32019-10-28 21:49:21 -06004618 return req->user_data == (unsigned long) data;
4619}
4620
Jens Axboee977d6d2019-11-05 12:39:45 -07004621static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004622{
Jens Axboe62755e32019-10-28 21:49:21 -06004623 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004624 int ret = 0;
4625
Jens Axboe62755e32019-10-28 21:49:21 -06004626 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4627 switch (cancel_ret) {
4628 case IO_WQ_CANCEL_OK:
4629 ret = 0;
4630 break;
4631 case IO_WQ_CANCEL_RUNNING:
4632 ret = -EALREADY;
4633 break;
4634 case IO_WQ_CANCEL_NOTFOUND:
4635 ret = -ENOENT;
4636 break;
4637 }
4638
Jens Axboee977d6d2019-11-05 12:39:45 -07004639 return ret;
4640}
4641
Jens Axboe47f46762019-11-09 17:43:02 -07004642static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4643 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004644 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004645{
4646 unsigned long flags;
4647 int ret;
4648
4649 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4650 if (ret != -ENOENT) {
4651 spin_lock_irqsave(&ctx->completion_lock, flags);
4652 goto done;
4653 }
4654
4655 spin_lock_irqsave(&ctx->completion_lock, flags);
4656 ret = io_timeout_cancel(ctx, sqe_addr);
4657 if (ret != -ENOENT)
4658 goto done;
4659 ret = io_poll_cancel(ctx, sqe_addr);
4660done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004661 if (!ret)
4662 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004663 io_cqring_fill_event(req, ret);
4664 io_commit_cqring(ctx);
4665 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4666 io_cqring_ev_posted(ctx);
4667
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004668 if (ret < 0)
4669 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004670 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004671}
4672
Jens Axboe3529d8c2019-12-19 18:24:38 -07004673static int io_async_cancel_prep(struct io_kiocb *req,
4674 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004675{
Jens Axboefbf23842019-12-17 18:45:56 -07004676 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004677 return -EINVAL;
4678 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4679 sqe->cancel_flags)
4680 return -EINVAL;
4681
Jens Axboefbf23842019-12-17 18:45:56 -07004682 req->cancel.addr = READ_ONCE(sqe->addr);
4683 return 0;
4684}
4685
Pavel Begunkov014db002020-03-03 21:33:12 +03004686static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004687{
4688 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004689
Pavel Begunkov014db002020-03-03 21:33:12 +03004690 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004691 return 0;
4692}
4693
Jens Axboe05f3fb32019-12-09 11:22:50 -07004694static int io_files_update_prep(struct io_kiocb *req,
4695 const struct io_uring_sqe *sqe)
4696{
4697 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4698 return -EINVAL;
4699
4700 req->files_update.offset = READ_ONCE(sqe->off);
4701 req->files_update.nr_args = READ_ONCE(sqe->len);
4702 if (!req->files_update.nr_args)
4703 return -EINVAL;
4704 req->files_update.arg = READ_ONCE(sqe->addr);
4705 return 0;
4706}
4707
4708static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4709{
4710 struct io_ring_ctx *ctx = req->ctx;
4711 struct io_uring_files_update up;
4712 int ret;
4713
Jens Axboef86cd202020-01-29 13:46:44 -07004714 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004715 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004716
4717 up.offset = req->files_update.offset;
4718 up.fds = req->files_update.arg;
4719
4720 mutex_lock(&ctx->uring_lock);
4721 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4722 mutex_unlock(&ctx->uring_lock);
4723
4724 if (ret < 0)
4725 req_set_fail_links(req);
4726 io_cqring_add_event(req, ret);
4727 io_put_req(req);
4728 return 0;
4729}
4730
Jens Axboe3529d8c2019-12-19 18:24:38 -07004731static int io_req_defer_prep(struct io_kiocb *req,
4732 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004733{
Jens Axboee7815732019-12-17 19:45:06 -07004734 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004735
Jens Axboef86cd202020-01-29 13:46:44 -07004736 if (io_op_defs[req->opcode].file_table) {
4737 ret = io_grab_files(req);
4738 if (unlikely(ret))
4739 return ret;
4740 }
4741
Jens Axboecccf0ee2020-01-27 16:34:48 -07004742 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4743
Jens Axboed625c6e2019-12-17 19:53:05 -07004744 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004745 case IORING_OP_NOP:
4746 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004747 case IORING_OP_READV:
4748 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004749 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004750 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004751 break;
4752 case IORING_OP_WRITEV:
4753 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004754 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004755 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004756 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004757 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004758 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004759 break;
4760 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004761 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004762 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004763 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004764 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004765 break;
4766 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004767 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004768 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004769 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004770 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004771 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004772 break;
4773 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004774 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004775 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004776 break;
Jens Axboef499a022019-12-02 16:28:46 -07004777 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004778 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004779 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004780 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004781 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004782 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004783 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004784 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004785 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004786 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004787 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004788 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004789 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004790 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004791 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004792 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004793 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004794 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004795 case IORING_OP_FALLOCATE:
4796 ret = io_fallocate_prep(req, sqe);
4797 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004798 case IORING_OP_OPENAT:
4799 ret = io_openat_prep(req, sqe);
4800 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004801 case IORING_OP_CLOSE:
4802 ret = io_close_prep(req, sqe);
4803 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004804 case IORING_OP_FILES_UPDATE:
4805 ret = io_files_update_prep(req, sqe);
4806 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004807 case IORING_OP_STATX:
4808 ret = io_statx_prep(req, sqe);
4809 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004810 case IORING_OP_FADVISE:
4811 ret = io_fadvise_prep(req, sqe);
4812 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004813 case IORING_OP_MADVISE:
4814 ret = io_madvise_prep(req, sqe);
4815 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004816 case IORING_OP_OPENAT2:
4817 ret = io_openat2_prep(req, sqe);
4818 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004819 case IORING_OP_EPOLL_CTL:
4820 ret = io_epoll_ctl_prep(req, sqe);
4821 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004822 case IORING_OP_SPLICE:
4823 ret = io_splice_prep(req, sqe);
4824 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004825 case IORING_OP_PROVIDE_BUFFERS:
4826 ret = io_provide_buffers_prep(req, sqe);
4827 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004828 default:
Jens Axboee7815732019-12-17 19:45:06 -07004829 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4830 req->opcode);
4831 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004832 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004833 }
4834
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004835 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004836}
4837
Jens Axboe3529d8c2019-12-19 18:24:38 -07004838static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004839{
Jackie Liua197f662019-11-08 08:09:12 -07004840 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004841 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004842
Bob Liu9d858b22019-11-13 18:06:25 +08004843 /* Still need defer if there is pending req in defer list. */
4844 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004845 return 0;
4846
Jens Axboe3529d8c2019-12-19 18:24:38 -07004847 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004848 return -EAGAIN;
4849
Jens Axboe3529d8c2019-12-19 18:24:38 -07004850 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004851 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004852 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004853
Jens Axboede0617e2019-04-06 21:51:27 -06004854 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004855 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004856 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004857 return 0;
4858 }
4859
Jens Axboe915967f2019-11-21 09:01:20 -07004860 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004861 list_add_tail(&req->list, &ctx->defer_list);
4862 spin_unlock_irq(&ctx->completion_lock);
4863 return -EIOCBQUEUED;
4864}
4865
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004866static void io_cleanup_req(struct io_kiocb *req)
4867{
4868 struct io_async_ctx *io = req->io;
4869
4870 switch (req->opcode) {
4871 case IORING_OP_READV:
4872 case IORING_OP_READ_FIXED:
4873 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07004874 if (req->flags & REQ_F_BUFFER_SELECTED)
4875 kfree((void *)(unsigned long)req->rw.addr);
4876 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004877 case IORING_OP_WRITEV:
4878 case IORING_OP_WRITE_FIXED:
4879 case IORING_OP_WRITE:
4880 if (io->rw.iov != io->rw.fast_iov)
4881 kfree(io->rw.iov);
4882 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004883 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07004884 if (req->flags & REQ_F_BUFFER_SELECTED)
4885 kfree(req->sr_msg.kbuf);
4886 /* fallthrough */
4887 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004888 if (io->msg.iov != io->msg.fast_iov)
4889 kfree(io->msg.iov);
4890 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004891 case IORING_OP_RECV:
4892 if (req->flags & REQ_F_BUFFER_SELECTED)
4893 kfree(req->sr_msg.kbuf);
4894 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004895 case IORING_OP_OPENAT:
4896 case IORING_OP_OPENAT2:
4897 case IORING_OP_STATX:
4898 putname(req->open.filename);
4899 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004900 case IORING_OP_SPLICE:
4901 io_put_file(req, req->splice.file_in,
4902 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4903 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004904 }
4905
4906 req->flags &= ~REQ_F_NEED_CLEANUP;
4907}
4908
Jens Axboe3529d8c2019-12-19 18:24:38 -07004909static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03004910 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004911{
Jackie Liua197f662019-11-08 08:09:12 -07004912 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004913 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004914
Jens Axboed625c6e2019-12-17 19:53:05 -07004915 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004916 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004917 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004918 break;
4919 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004920 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004921 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004922 if (sqe) {
4923 ret = io_read_prep(req, sqe, force_nonblock);
4924 if (ret < 0)
4925 break;
4926 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004927 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004928 break;
4929 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004930 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004931 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004932 if (sqe) {
4933 ret = io_write_prep(req, sqe, force_nonblock);
4934 if (ret < 0)
4935 break;
4936 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004937 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004938 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004939 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004940 if (sqe) {
4941 ret = io_prep_fsync(req, sqe);
4942 if (ret < 0)
4943 break;
4944 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004945 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004946 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004947 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004948 if (sqe) {
4949 ret = io_poll_add_prep(req, sqe);
4950 if (ret)
4951 break;
4952 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004953 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004954 break;
4955 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004956 if (sqe) {
4957 ret = io_poll_remove_prep(req, sqe);
4958 if (ret < 0)
4959 break;
4960 }
Jens Axboefc4df992019-12-10 14:38:45 -07004961 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004962 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004963 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004964 if (sqe) {
4965 ret = io_prep_sfr(req, sqe);
4966 if (ret < 0)
4967 break;
4968 }
Pavel Begunkov014db002020-03-03 21:33:12 +03004969 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004970 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004971 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004972 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004973 if (sqe) {
4974 ret = io_sendmsg_prep(req, sqe);
4975 if (ret < 0)
4976 break;
4977 }
Jens Axboefddafac2020-01-04 20:19:44 -07004978 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004979 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004980 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004981 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004982 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004983 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004984 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004985 if (sqe) {
4986 ret = io_recvmsg_prep(req, sqe);
4987 if (ret)
4988 break;
4989 }
Jens Axboefddafac2020-01-04 20:19:44 -07004990 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03004991 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07004992 else
Pavel Begunkov014db002020-03-03 21:33:12 +03004993 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004994 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004995 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004996 if (sqe) {
4997 ret = io_timeout_prep(req, sqe, false);
4998 if (ret)
4999 break;
5000 }
Jens Axboefc4df992019-12-10 14:38:45 -07005001 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005002 break;
Jens Axboe11365042019-10-16 09:08:32 -06005003 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005004 if (sqe) {
5005 ret = io_timeout_remove_prep(req, sqe);
5006 if (ret)
5007 break;
5008 }
Jens Axboefc4df992019-12-10 14:38:45 -07005009 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005010 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005011 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005012 if (sqe) {
5013 ret = io_accept_prep(req, sqe);
5014 if (ret)
5015 break;
5016 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005017 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005018 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005019 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005020 if (sqe) {
5021 ret = io_connect_prep(req, sqe);
5022 if (ret)
5023 break;
5024 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005025 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005026 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005027 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005028 if (sqe) {
5029 ret = io_async_cancel_prep(req, sqe);
5030 if (ret)
5031 break;
5032 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005033 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005034 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005035 case IORING_OP_FALLOCATE:
5036 if (sqe) {
5037 ret = io_fallocate_prep(req, sqe);
5038 if (ret)
5039 break;
5040 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005041 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005042 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005043 case IORING_OP_OPENAT:
5044 if (sqe) {
5045 ret = io_openat_prep(req, sqe);
5046 if (ret)
5047 break;
5048 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005049 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005050 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005051 case IORING_OP_CLOSE:
5052 if (sqe) {
5053 ret = io_close_prep(req, sqe);
5054 if (ret)
5055 break;
5056 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005057 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005058 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005059 case IORING_OP_FILES_UPDATE:
5060 if (sqe) {
5061 ret = io_files_update_prep(req, sqe);
5062 if (ret)
5063 break;
5064 }
5065 ret = io_files_update(req, force_nonblock);
5066 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005067 case IORING_OP_STATX:
5068 if (sqe) {
5069 ret = io_statx_prep(req, sqe);
5070 if (ret)
5071 break;
5072 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005073 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005074 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005075 case IORING_OP_FADVISE:
5076 if (sqe) {
5077 ret = io_fadvise_prep(req, sqe);
5078 if (ret)
5079 break;
5080 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005081 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005082 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005083 case IORING_OP_MADVISE:
5084 if (sqe) {
5085 ret = io_madvise_prep(req, sqe);
5086 if (ret)
5087 break;
5088 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005089 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005090 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005091 case IORING_OP_OPENAT2:
5092 if (sqe) {
5093 ret = io_openat2_prep(req, sqe);
5094 if (ret)
5095 break;
5096 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005097 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005098 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005099 case IORING_OP_EPOLL_CTL:
5100 if (sqe) {
5101 ret = io_epoll_ctl_prep(req, sqe);
5102 if (ret)
5103 break;
5104 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005105 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005106 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005107 case IORING_OP_SPLICE:
5108 if (sqe) {
5109 ret = io_splice_prep(req, sqe);
5110 if (ret < 0)
5111 break;
5112 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005113 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005114 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005115 case IORING_OP_PROVIDE_BUFFERS:
5116 if (sqe) {
5117 ret = io_provide_buffers_prep(req, sqe);
5118 if (ret)
5119 break;
5120 }
5121 ret = io_provide_buffers(req, force_nonblock);
5122 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005123 default:
5124 ret = -EINVAL;
5125 break;
5126 }
5127
Jens Axboedef596e2019-01-09 08:59:42 -07005128 if (ret)
5129 return ret;
5130
5131 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005132 const bool in_async = io_wq_current_is_worker();
5133
Jens Axboe9e645e112019-05-10 16:07:28 -06005134 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07005135 return -EAGAIN;
5136
Jens Axboe11ba8202020-01-15 21:51:17 -07005137 /* workqueue context doesn't hold uring_lock, grab it now */
5138 if (in_async)
5139 mutex_lock(&ctx->uring_lock);
5140
Jens Axboedef596e2019-01-09 08:59:42 -07005141 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005142
5143 if (in_async)
5144 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005145 }
5146
5147 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005148}
5149
Jens Axboe561fb042019-10-24 07:25:42 -06005150static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07005151{
Jens Axboe561fb042019-10-24 07:25:42 -06005152 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005153 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005154 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005155
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005156 /* if NO_CANCEL is set, we must still run the work */
5157 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5158 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005159 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005160 }
Jens Axboe31b51512019-01-18 22:56:34 -07005161
Jens Axboe561fb042019-10-24 07:25:42 -06005162 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005163 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005164 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005165 /*
5166 * We can get EAGAIN for polled IO even though we're
5167 * forcing a sync submission from here, since we can't
5168 * wait for request slots on the block side.
5169 */
5170 if (ret != -EAGAIN)
5171 break;
5172 cond_resched();
5173 } while (1);
5174 }
Jens Axboe31b51512019-01-18 22:56:34 -07005175
Jens Axboe561fb042019-10-24 07:25:42 -06005176 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005177 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005178 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005179 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005180 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005181
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005182 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005183}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005184
Jens Axboe15b71ab2019-12-11 11:20:36 -07005185static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005186{
Jens Axboed3656342019-12-18 09:50:26 -07005187 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005188 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07005189 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07005190 return 0;
5191 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06005192}
5193
Jens Axboe65e19f52019-10-26 07:20:21 -06005194static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5195 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005196{
Jens Axboe65e19f52019-10-26 07:20:21 -06005197 struct fixed_file_table *table;
5198
Jens Axboe05f3fb32019-12-09 11:22:50 -07005199 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5200 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06005201}
5202
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005203static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5204 int fd, struct file **out_file, bool fixed)
5205{
5206 struct io_ring_ctx *ctx = req->ctx;
5207 struct file *file;
5208
5209 if (fixed) {
5210 if (unlikely(!ctx->file_data ||
5211 (unsigned) fd >= ctx->nr_user_files))
5212 return -EBADF;
5213 fd = array_index_nospec(fd, ctx->nr_user_files);
5214 file = io_file_from_index(ctx, fd);
5215 if (!file)
5216 return -EBADF;
5217 percpu_ref_get(&ctx->file_data->refs);
5218 } else {
5219 trace_io_uring_file_get(ctx, fd);
5220 file = __io_file_get(state, fd);
5221 if (unlikely(!file))
5222 return -EBADF;
5223 }
5224
5225 *out_file = file;
5226 return 0;
5227}
5228
Jens Axboe3529d8c2019-12-19 18:24:38 -07005229static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
5230 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06005231{
5232 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07005233 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005234 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005235
Jens Axboe3529d8c2019-12-19 18:24:38 -07005236 flags = READ_ONCE(sqe->flags);
5237 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06005238
Jens Axboed3656342019-12-18 09:50:26 -07005239 if (!io_req_needs_file(req, fd))
5240 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06005241
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005242 fixed = (flags & IOSQE_FIXED_FILE);
5243 if (unlikely(!fixed && req->needs_fixed_file))
5244 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005245
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005246 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005247}
5248
Jackie Liua197f662019-11-08 08:09:12 -07005249static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005250{
Jens Axboefcb323c2019-10-24 12:39:47 -06005251 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005252 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005253
Jens Axboef86cd202020-01-29 13:46:44 -07005254 if (req->work.files)
5255 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005256 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005257 return -EBADF;
5258
Jens Axboefcb323c2019-10-24 12:39:47 -06005259 rcu_read_lock();
5260 spin_lock_irq(&ctx->inflight_lock);
5261 /*
5262 * We use the f_ops->flush() handler to ensure that we can flush
5263 * out work accessing these files if the fd is closed. Check if
5264 * the fd has changed since we started down this path, and disallow
5265 * this operation if it has.
5266 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005267 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005268 list_add(&req->inflight_entry, &ctx->inflight_list);
5269 req->flags |= REQ_F_INFLIGHT;
5270 req->work.files = current->files;
5271 ret = 0;
5272 }
5273 spin_unlock_irq(&ctx->inflight_lock);
5274 rcu_read_unlock();
5275
5276 return ret;
5277}
5278
Jens Axboe2665abf2019-11-05 12:40:47 -07005279static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5280{
Jens Axboead8a48a2019-11-15 08:49:11 -07005281 struct io_timeout_data *data = container_of(timer,
5282 struct io_timeout_data, timer);
5283 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005284 struct io_ring_ctx *ctx = req->ctx;
5285 struct io_kiocb *prev = NULL;
5286 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005287
5288 spin_lock_irqsave(&ctx->completion_lock, flags);
5289
5290 /*
5291 * We don't expect the list to be empty, that will only happen if we
5292 * race with the completion of the linked work.
5293 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005294 if (!list_empty(&req->link_list)) {
5295 prev = list_entry(req->link_list.prev, struct io_kiocb,
5296 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005297 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005298 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005299 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5300 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005301 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005302 }
5303
5304 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5305
5306 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005307 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005308 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005309 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005310 } else {
5311 io_cqring_add_event(req, -ETIME);
5312 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005313 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005314 return HRTIMER_NORESTART;
5315}
5316
Jens Axboead8a48a2019-11-15 08:49:11 -07005317static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005318{
Jens Axboe76a46e02019-11-10 23:34:16 -07005319 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005320
Jens Axboe76a46e02019-11-10 23:34:16 -07005321 /*
5322 * If the list is now empty, then our linked request finished before
5323 * we got a chance to setup the timer
5324 */
5325 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005326 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005327 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005328
Jens Axboead8a48a2019-11-15 08:49:11 -07005329 data->timer.function = io_link_timeout_fn;
5330 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5331 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005332 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005333 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005334
Jens Axboe2665abf2019-11-05 12:40:47 -07005335 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005336 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005337}
5338
Jens Axboead8a48a2019-11-15 08:49:11 -07005339static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005340{
5341 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005342
Jens Axboe2665abf2019-11-05 12:40:47 -07005343 if (!(req->flags & REQ_F_LINK))
5344 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005345 /* for polled retry, if flag is set, we already went through here */
5346 if (req->flags & REQ_F_POLLED)
5347 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005348
Pavel Begunkov44932332019-12-05 16:16:35 +03005349 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5350 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005351 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005352 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005353
Jens Axboe76a46e02019-11-10 23:34:16 -07005354 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005355 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005356}
5357
Jens Axboe3529d8c2019-12-19 18:24:38 -07005358static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005359{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005360 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005361 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005362 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005363 int ret;
5364
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005365again:
5366 linked_timeout = io_prep_linked_timeout(req);
5367
Jens Axboe193155c2020-02-22 23:22:19 -07005368 if (req->work.creds && req->work.creds != current_cred()) {
5369 if (old_creds)
5370 revert_creds(old_creds);
5371 if (old_creds == req->work.creds)
5372 old_creds = NULL; /* restored original creds */
5373 else
5374 old_creds = override_creds(req->work.creds);
5375 }
5376
Pavel Begunkov014db002020-03-03 21:33:12 +03005377 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005378
5379 /*
5380 * We async punt it if the file wasn't marked NOWAIT, or if the file
5381 * doesn't support non-blocking read/write attempts
5382 */
5383 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5384 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005385 if (io_arm_poll_handler(req)) {
5386 if (linked_timeout)
5387 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005388 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005389 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005390punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005391 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005392 ret = io_grab_files(req);
5393 if (ret)
5394 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005395 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005396
5397 /*
5398 * Queued up for async execution, worker will release
5399 * submit reference when the iocb is actually submitted.
5400 */
5401 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005402 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005403 }
Jens Axboee65ef562019-03-12 10:16:44 -06005404
Jens Axboefcb323c2019-10-24 12:39:47 -06005405err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005406 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005407 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005408 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005409
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005410 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005411 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005412 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005413 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005414 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005415 }
5416
Jens Axboee65ef562019-03-12 10:16:44 -06005417 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005418 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005419 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005420 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005421 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005422 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005423 if (nxt) {
5424 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005425
5426 if (req->flags & REQ_F_FORCE_ASYNC)
5427 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005428 goto again;
5429 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005430exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005431 if (old_creds)
5432 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005433}
5434
Jens Axboe3529d8c2019-12-19 18:24:38 -07005435static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005436{
5437 int ret;
5438
Jens Axboe3529d8c2019-12-19 18:24:38 -07005439 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005440 if (ret) {
5441 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005442fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005443 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005444 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005445 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005446 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005447 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005448 ret = io_req_defer_prep(req, sqe);
5449 if (unlikely(ret < 0))
5450 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005451 /*
5452 * Never try inline submit of IOSQE_ASYNC is set, go straight
5453 * to async execution.
5454 */
5455 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5456 io_queue_async_work(req);
5457 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005458 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005459 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005460}
5461
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005462static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005463{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005464 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005465 io_cqring_add_event(req, -ECANCELED);
5466 io_double_put_req(req);
5467 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005468 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005469}
5470
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005471#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboebcda7ba2020-02-23 16:42:51 -07005472 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5473 IOSQE_BUFFER_SELECT)
Jens Axboe9e645e112019-05-10 16:07:28 -06005474
Jens Axboe3529d8c2019-12-19 18:24:38 -07005475static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5476 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005477{
Jackie Liua197f662019-11-08 08:09:12 -07005478 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005479 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005480 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005481
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005482 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005483
5484 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005485 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005486 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005487 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005488 }
5489
Jens Axboebcda7ba2020-02-23 16:42:51 -07005490 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5491 !io_op_defs[req->opcode].buffer_select) {
5492 ret = -EOPNOTSUPP;
5493 goto err_req;
5494 }
5495
Jens Axboe75c6a032020-01-28 10:15:23 -07005496 id = READ_ONCE(sqe->personality);
5497 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005498 req->work.creds = idr_find(&ctx->personality_idr, id);
5499 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005500 ret = -EINVAL;
5501 goto err_req;
5502 }
Jens Axboe193155c2020-02-22 23:22:19 -07005503 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005504 }
5505
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005506 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005507 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
Jens Axboebcda7ba2020-02-23 16:42:51 -07005508 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5509 IOSQE_BUFFER_SELECT);
Jens Axboe9e645e112019-05-10 16:07:28 -06005510
Jens Axboe3529d8c2019-12-19 18:24:38 -07005511 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005512 if (unlikely(ret)) {
5513err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005514 io_cqring_add_event(req, ret);
5515 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005516 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005517 }
5518
Jens Axboe9e645e112019-05-10 16:07:28 -06005519 /*
5520 * If we already have a head request, queue this one for async
5521 * submittal once the head completes. If we don't have a head but
5522 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5523 * submitted sync once the chain is complete. If none of those
5524 * conditions are true (normal request), then just queue it.
5525 */
5526 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005527 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005528
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005529 /*
5530 * Taking sequential execution of a link, draining both sides
5531 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5532 * requests in the link. So, it drains the head and the
5533 * next after the link request. The last one is done via
5534 * drain_next flag to persist the effect across calls.
5535 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005536 if (sqe_flags & IOSQE_IO_DRAIN) {
5537 head->flags |= REQ_F_IO_DRAIN;
5538 ctx->drain_next = 1;
5539 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005540 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005541 ret = -EAGAIN;
5542 goto err_req;
5543 }
5544
Jens Axboe3529d8c2019-12-19 18:24:38 -07005545 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005546 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005547 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005548 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005549 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005550 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005551 trace_io_uring_link(ctx, req, head);
5552 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005553
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005554 /* last request of a link, enqueue the link */
5555 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5556 io_queue_link_head(head);
5557 *link = NULL;
5558 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005559 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005560 if (unlikely(ctx->drain_next)) {
5561 req->flags |= REQ_F_IO_DRAIN;
5562 req->ctx->drain_next = 0;
5563 }
5564 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5565 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005566 INIT_LIST_HEAD(&req->link_list);
5567 ret = io_req_defer_prep(req, sqe);
5568 if (ret)
5569 req->flags |= REQ_F_FAIL_LINK;
5570 *link = req;
5571 } else {
5572 io_queue_sqe(req, sqe);
5573 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005574 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005575
5576 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005577}
5578
Jens Axboe9a56a232019-01-09 09:06:50 -07005579/*
5580 * Batched submission is done, ensure local IO is flushed out.
5581 */
5582static void io_submit_state_end(struct io_submit_state *state)
5583{
5584 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005585 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005586 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005587 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005588}
5589
5590/*
5591 * Start submission side cache.
5592 */
5593static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005594 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005595{
5596 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005597 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005598 state->file = NULL;
5599 state->ios_left = max_ios;
5600}
5601
Jens Axboe2b188cc2019-01-07 10:46:33 -07005602static void io_commit_sqring(struct io_ring_ctx *ctx)
5603{
Hristo Venev75b28af2019-08-26 17:23:46 +00005604 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005605
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005606 /*
5607 * Ensure any loads from the SQEs are done at this point,
5608 * since once we write the new head, the application could
5609 * write new data to them.
5610 */
5611 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005612}
5613
5614/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005615 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005616 * that is mapped by userspace. This means that care needs to be taken to
5617 * ensure that reads are stable, as we cannot rely on userspace always
5618 * being a good citizen. If members of the sqe are validated and then later
5619 * used, it's important that those reads are done through READ_ONCE() to
5620 * prevent a re-load down the line.
5621 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005622static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5623 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005624{
Hristo Venev75b28af2019-08-26 17:23:46 +00005625 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005626 unsigned head;
5627
5628 /*
5629 * The cached sq head (or cq tail) serves two purposes:
5630 *
5631 * 1) allows us to batch the cost of updating the user visible
5632 * head updates.
5633 * 2) allows the kernel side to track the head on its own, even
5634 * though the application is the one updating it.
5635 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005636 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005637 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005638 /*
5639 * All io need record the previous position, if LINK vs DARIN,
5640 * it can be used to mark the position of the first IO in the
5641 * link list.
5642 */
5643 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005644 *sqe_ptr = &ctx->sq_sqes[head];
5645 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5646 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005647 ctx->cached_sq_head++;
5648 return true;
5649 }
5650
5651 /* drop invalid entries */
5652 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005653 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005654 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005655 return false;
5656}
5657
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005658static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005659 struct file *ring_file, int ring_fd,
5660 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005661{
5662 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005663 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005664 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005665 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005666
Jens Axboec4a2ed72019-11-21 21:01:26 -07005667 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005668 if (test_bit(0, &ctx->sq_check_overflow)) {
5669 if (!list_empty(&ctx->cq_overflow_list) &&
5670 !io_cqring_overflow_flush(ctx, false))
5671 return -EBUSY;
5672 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005673
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005674 /* make sure SQ entry isn't read before tail */
5675 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005676
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005677 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5678 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005679
5680 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005681 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005682 statep = &state;
5683 }
5684
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005685 ctx->ring_fd = ring_fd;
5686 ctx->ring_file = ring_file;
5687
Jens Axboe6c271ce2019-01-10 11:22:30 -07005688 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005689 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005690 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005691 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005692
Pavel Begunkov196be952019-11-07 01:41:06 +03005693 req = io_get_req(ctx, statep);
5694 if (unlikely(!req)) {
5695 if (!submitted)
5696 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005697 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005698 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005699 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005700 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005701 break;
5702 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005703
Jens Axboed3656342019-12-18 09:50:26 -07005704 /* will complete beyond this point, count as submitted */
5705 submitted++;
5706
5707 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005708 err = -EINVAL;
5709fail_req:
5710 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005711 io_double_put_req(req);
5712 break;
5713 }
5714
5715 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005716 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005717 if (unlikely(mm_fault)) {
5718 err = -EFAULT;
5719 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005720 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005721 use_mm(ctx->sqo_mm);
5722 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005723 }
5724
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005725 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005726 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5727 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005728 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005729 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005730 }
5731
Pavel Begunkov9466f432020-01-25 22:34:01 +03005732 if (unlikely(submitted != nr)) {
5733 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5734
5735 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5736 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005737 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005738 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005739 if (statep)
5740 io_submit_state_end(&state);
5741
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005742 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5743 io_commit_sqring(ctx);
5744
Jens Axboe6c271ce2019-01-10 11:22:30 -07005745 return submitted;
5746}
5747
5748static int io_sq_thread(void *data)
5749{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005750 struct io_ring_ctx *ctx = data;
5751 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005752 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005753 mm_segment_t old_fs;
5754 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005755 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005756 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005757
Jens Axboe206aefd2019-11-07 18:27:42 -07005758 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005759
Jens Axboe6c271ce2019-01-10 11:22:30 -07005760 old_fs = get_fs();
5761 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005762 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005763
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005764 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005765 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005766 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005767
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005768 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005769 unsigned nr_events = 0;
5770
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005771 mutex_lock(&ctx->uring_lock);
5772 if (!list_empty(&ctx->poll_list))
5773 io_iopoll_getevents(ctx, &nr_events, 0);
5774 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005775 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005776 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005777 }
5778
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005779 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005780
5781 /*
5782 * If submit got -EBUSY, flag us as needing the application
5783 * to enter the kernel to reap and flush events.
5784 */
5785 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005786 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005787 * Drop cur_mm before scheduling, we can't hold it for
5788 * long periods (or over schedule()). Do this before
5789 * adding ourselves to the waitqueue, as the unuse/drop
5790 * may sleep.
5791 */
5792 if (cur_mm) {
5793 unuse_mm(cur_mm);
5794 mmput(cur_mm);
5795 cur_mm = NULL;
5796 }
5797
5798 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005799 * We're polling. If we're within the defined idle
5800 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005801 * to sleep. The exception is if we got EBUSY doing
5802 * more IO, we should wait for the application to
5803 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005804 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005805 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005806 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5807 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005808 if (current->task_works)
5809 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005810 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005811 continue;
5812 }
5813
Jens Axboe6c271ce2019-01-10 11:22:30 -07005814 prepare_to_wait(&ctx->sqo_wait, &wait,
5815 TASK_INTERRUPTIBLE);
5816
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005817 /*
5818 * While doing polled IO, before going to sleep, we need
5819 * to check if there are new reqs added to poll_list, it
5820 * is because reqs may have been punted to io worker and
5821 * will be added to poll_list later, hence check the
5822 * poll_list again.
5823 */
5824 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5825 !list_empty_careful(&ctx->poll_list)) {
5826 finish_wait(&ctx->sqo_wait, &wait);
5827 continue;
5828 }
5829
Jens Axboe6c271ce2019-01-10 11:22:30 -07005830 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005831 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005832 /* make sure to read SQ tail after writing flags */
5833 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005834
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005835 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005836 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005837 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005838 finish_wait(&ctx->sqo_wait, &wait);
5839 break;
5840 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005841 if (current->task_works) {
5842 task_work_run();
5843 continue;
5844 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005845 if (signal_pending(current))
5846 flush_signals(current);
5847 schedule();
5848 finish_wait(&ctx->sqo_wait, &wait);
5849
Hristo Venev75b28af2019-08-26 17:23:46 +00005850 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005851 continue;
5852 }
5853 finish_wait(&ctx->sqo_wait, &wait);
5854
Hristo Venev75b28af2019-08-26 17:23:46 +00005855 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005856 }
5857
Jens Axboe8a4955f2019-12-09 14:52:35 -07005858 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005859 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005860 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005861 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005862 }
5863
Jens Axboeb41e9852020-02-17 09:52:41 -07005864 if (current->task_works)
5865 task_work_run();
5866
Jens Axboe6c271ce2019-01-10 11:22:30 -07005867 set_fs(old_fs);
5868 if (cur_mm) {
5869 unuse_mm(cur_mm);
5870 mmput(cur_mm);
5871 }
Jens Axboe181e4482019-11-25 08:52:30 -07005872 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005873
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005874 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005875
Jens Axboe6c271ce2019-01-10 11:22:30 -07005876 return 0;
5877}
5878
Jens Axboebda52162019-09-24 13:47:15 -06005879struct io_wait_queue {
5880 struct wait_queue_entry wq;
5881 struct io_ring_ctx *ctx;
5882 unsigned to_wait;
5883 unsigned nr_timeouts;
5884};
5885
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005886static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005887{
5888 struct io_ring_ctx *ctx = iowq->ctx;
5889
5890 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005891 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005892 * started waiting. For timeouts, we always want to return to userspace,
5893 * regardless of event count.
5894 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005895 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005896 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5897}
5898
5899static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5900 int wake_flags, void *key)
5901{
5902 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5903 wq);
5904
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005905 /* use noflush == true, as we can't safely rely on locking context */
5906 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005907 return -1;
5908
5909 return autoremove_wake_function(curr, mode, wake_flags, key);
5910}
5911
Jens Axboe2b188cc2019-01-07 10:46:33 -07005912/*
5913 * Wait until events become available, if we don't already have some. The
5914 * application must reap them itself, as they reside on the shared cq ring.
5915 */
5916static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5917 const sigset_t __user *sig, size_t sigsz)
5918{
Jens Axboebda52162019-09-24 13:47:15 -06005919 struct io_wait_queue iowq = {
5920 .wq = {
5921 .private = current,
5922 .func = io_wake_function,
5923 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5924 },
5925 .ctx = ctx,
5926 .to_wait = min_events,
5927 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005928 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005929 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005930
Jens Axboeb41e9852020-02-17 09:52:41 -07005931 do {
5932 if (io_cqring_events(ctx, false) >= min_events)
5933 return 0;
5934 if (!current->task_works)
5935 break;
5936 task_work_run();
5937 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005938
5939 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005940#ifdef CONFIG_COMPAT
5941 if (in_compat_syscall())
5942 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005943 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005944 else
5945#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005946 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005947
Jens Axboe2b188cc2019-01-07 10:46:33 -07005948 if (ret)
5949 return ret;
5950 }
5951
Jens Axboebda52162019-09-24 13:47:15 -06005952 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005953 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005954 do {
5955 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5956 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07005957 if (current->task_works)
5958 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005959 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005960 break;
5961 schedule();
5962 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005963 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005964 break;
5965 }
5966 } while (1);
5967 finish_wait(&ctx->wait, &iowq.wq);
5968
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005969 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005970
Hristo Venev75b28af2019-08-26 17:23:46 +00005971 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005972}
5973
Jens Axboe6b063142019-01-10 22:13:58 -07005974static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5975{
5976#if defined(CONFIG_UNIX)
5977 if (ctx->ring_sock) {
5978 struct sock *sock = ctx->ring_sock->sk;
5979 struct sk_buff *skb;
5980
5981 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5982 kfree_skb(skb);
5983 }
5984#else
5985 int i;
5986
Jens Axboe65e19f52019-10-26 07:20:21 -06005987 for (i = 0; i < ctx->nr_user_files; i++) {
5988 struct file *file;
5989
5990 file = io_file_from_index(ctx, i);
5991 if (file)
5992 fput(file);
5993 }
Jens Axboe6b063142019-01-10 22:13:58 -07005994#endif
5995}
5996
Jens Axboe05f3fb32019-12-09 11:22:50 -07005997static void io_file_ref_kill(struct percpu_ref *ref)
5998{
5999 struct fixed_file_data *data;
6000
6001 data = container_of(ref, struct fixed_file_data, refs);
6002 complete(&data->done);
6003}
6004
Jens Axboe6b063142019-01-10 22:13:58 -07006005static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6006{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006007 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06006008 unsigned nr_tables, i;
6009
Jens Axboe05f3fb32019-12-09 11:22:50 -07006010 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006011 return -ENXIO;
6012
Jens Axboe05f3fb32019-12-09 11:22:50 -07006013 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07006014 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006015 wait_for_completion(&data->done);
6016 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006017 percpu_ref_exit(&data->refs);
6018
Jens Axboe6b063142019-01-10 22:13:58 -07006019 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006020 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6021 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006022 kfree(data->table[i].files);
6023 kfree(data->table);
6024 kfree(data);
6025 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006026 ctx->nr_user_files = 0;
6027 return 0;
6028}
6029
Jens Axboe6c271ce2019-01-10 11:22:30 -07006030static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6031{
6032 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07006033 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006034 /*
6035 * The park is a bit of a work-around, without it we get
6036 * warning spews on shutdown with SQPOLL set and affinity
6037 * set to a single CPU.
6038 */
Jens Axboe06058632019-04-13 09:26:03 -06006039 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006040 kthread_stop(ctx->sqo_thread);
6041 ctx->sqo_thread = NULL;
6042 }
6043}
6044
Jens Axboe6b063142019-01-10 22:13:58 -07006045static void io_finish_async(struct io_ring_ctx *ctx)
6046{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006047 io_sq_thread_stop(ctx);
6048
Jens Axboe561fb042019-10-24 07:25:42 -06006049 if (ctx->io_wq) {
6050 io_wq_destroy(ctx->io_wq);
6051 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006052 }
6053}
6054
6055#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006056/*
6057 * Ensure the UNIX gc is aware of our file set, so we are certain that
6058 * the io_uring can be safely unregistered on process exit, even if we have
6059 * loops in the file referencing.
6060 */
6061static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6062{
6063 struct sock *sk = ctx->ring_sock->sk;
6064 struct scm_fp_list *fpl;
6065 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006066 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006067
6068 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
6069 unsigned long inflight = ctx->user->unix_inflight + nr;
6070
6071 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
6072 return -EMFILE;
6073 }
6074
6075 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6076 if (!fpl)
6077 return -ENOMEM;
6078
6079 skb = alloc_skb(0, GFP_KERNEL);
6080 if (!skb) {
6081 kfree(fpl);
6082 return -ENOMEM;
6083 }
6084
6085 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006086
Jens Axboe08a45172019-10-03 08:11:03 -06006087 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006088 fpl->user = get_uid(ctx->user);
6089 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006090 struct file *file = io_file_from_index(ctx, i + offset);
6091
6092 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006093 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006094 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006095 unix_inflight(fpl->user, fpl->fp[nr_files]);
6096 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006097 }
6098
Jens Axboe08a45172019-10-03 08:11:03 -06006099 if (nr_files) {
6100 fpl->max = SCM_MAX_FD;
6101 fpl->count = nr_files;
6102 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006103 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006104 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6105 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006106
Jens Axboe08a45172019-10-03 08:11:03 -06006107 for (i = 0; i < nr_files; i++)
6108 fput(fpl->fp[i]);
6109 } else {
6110 kfree_skb(skb);
6111 kfree(fpl);
6112 }
Jens Axboe6b063142019-01-10 22:13:58 -07006113
6114 return 0;
6115}
6116
6117/*
6118 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6119 * causes regular reference counting to break down. We rely on the UNIX
6120 * garbage collection to take care of this problem for us.
6121 */
6122static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6123{
6124 unsigned left, total;
6125 int ret = 0;
6126
6127 total = 0;
6128 left = ctx->nr_user_files;
6129 while (left) {
6130 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006131
6132 ret = __io_sqe_files_scm(ctx, this_files, total);
6133 if (ret)
6134 break;
6135 left -= this_files;
6136 total += this_files;
6137 }
6138
6139 if (!ret)
6140 return 0;
6141
6142 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006143 struct file *file = io_file_from_index(ctx, total);
6144
6145 if (file)
6146 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006147 total++;
6148 }
6149
6150 return ret;
6151}
6152#else
6153static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6154{
6155 return 0;
6156}
6157#endif
6158
Jens Axboe65e19f52019-10-26 07:20:21 -06006159static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6160 unsigned nr_files)
6161{
6162 int i;
6163
6164 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006165 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006166 unsigned this_files;
6167
6168 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6169 table->files = kcalloc(this_files, sizeof(struct file *),
6170 GFP_KERNEL);
6171 if (!table->files)
6172 break;
6173 nr_files -= this_files;
6174 }
6175
6176 if (i == nr_tables)
6177 return 0;
6178
6179 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006180 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006181 kfree(table->files);
6182 }
6183 return 1;
6184}
6185
Jens Axboe05f3fb32019-12-09 11:22:50 -07006186static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006187{
6188#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006189 struct sock *sock = ctx->ring_sock->sk;
6190 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6191 struct sk_buff *skb;
6192 int i;
6193
6194 __skb_queue_head_init(&list);
6195
6196 /*
6197 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6198 * remove this entry and rearrange the file array.
6199 */
6200 skb = skb_dequeue(head);
6201 while (skb) {
6202 struct scm_fp_list *fp;
6203
6204 fp = UNIXCB(skb).fp;
6205 for (i = 0; i < fp->count; i++) {
6206 int left;
6207
6208 if (fp->fp[i] != file)
6209 continue;
6210
6211 unix_notinflight(fp->user, fp->fp[i]);
6212 left = fp->count - 1 - i;
6213 if (left) {
6214 memmove(&fp->fp[i], &fp->fp[i + 1],
6215 left * sizeof(struct file *));
6216 }
6217 fp->count--;
6218 if (!fp->count) {
6219 kfree_skb(skb);
6220 skb = NULL;
6221 } else {
6222 __skb_queue_tail(&list, skb);
6223 }
6224 fput(file);
6225 file = NULL;
6226 break;
6227 }
6228
6229 if (!file)
6230 break;
6231
6232 __skb_queue_tail(&list, skb);
6233
6234 skb = skb_dequeue(head);
6235 }
6236
6237 if (skb_peek(&list)) {
6238 spin_lock_irq(&head->lock);
6239 while ((skb = __skb_dequeue(&list)) != NULL)
6240 __skb_queue_tail(head, skb);
6241 spin_unlock_irq(&head->lock);
6242 }
6243#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006244 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006245#endif
6246}
6247
Jens Axboe05f3fb32019-12-09 11:22:50 -07006248struct io_file_put {
6249 struct llist_node llist;
6250 struct file *file;
6251 struct completion *done;
6252};
6253
Jens Axboe2faf8522020-02-04 19:54:55 -07006254static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006255{
6256 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006257 struct llist_node *node;
6258
Jens Axboe05f3fb32019-12-09 11:22:50 -07006259 while ((node = llist_del_all(&data->put_llist)) != NULL) {
6260 llist_for_each_entry_safe(pfile, tmp, node, llist) {
6261 io_ring_file_put(data->ctx, pfile->file);
6262 if (pfile->done)
6263 complete(pfile->done);
6264 else
6265 kfree(pfile);
6266 }
6267 }
Jens Axboe2faf8522020-02-04 19:54:55 -07006268}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006269
Jens Axboe2faf8522020-02-04 19:54:55 -07006270static void io_ring_file_ref_switch(struct work_struct *work)
6271{
6272 struct fixed_file_data *data;
6273
6274 data = container_of(work, struct fixed_file_data, ref_work);
6275 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006276 percpu_ref_switch_to_percpu(&data->refs);
6277}
6278
6279static void io_file_data_ref_zero(struct percpu_ref *ref)
6280{
6281 struct fixed_file_data *data;
6282
6283 data = container_of(ref, struct fixed_file_data, refs);
6284
Jens Axboe2faf8522020-02-04 19:54:55 -07006285 /*
6286 * We can't safely switch from inside this context, punt to wq. If
6287 * the table ref is going away, the table is being unregistered.
6288 * Don't queue up the async work for that case, the caller will
6289 * handle it.
6290 */
6291 if (!percpu_ref_is_dying(&data->refs))
6292 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006293}
6294
6295static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6296 unsigned nr_args)
6297{
6298 __s32 __user *fds = (__s32 __user *) arg;
6299 unsigned nr_tables;
6300 struct file *file;
6301 int fd, ret = 0;
6302 unsigned i;
6303
6304 if (ctx->file_data)
6305 return -EBUSY;
6306 if (!nr_args)
6307 return -EINVAL;
6308 if (nr_args > IORING_MAX_FIXED_FILES)
6309 return -EMFILE;
6310
6311 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6312 if (!ctx->file_data)
6313 return -ENOMEM;
6314 ctx->file_data->ctx = ctx;
6315 init_completion(&ctx->file_data->done);
6316
6317 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6318 ctx->file_data->table = kcalloc(nr_tables,
6319 sizeof(struct fixed_file_table),
6320 GFP_KERNEL);
6321 if (!ctx->file_data->table) {
6322 kfree(ctx->file_data);
6323 ctx->file_data = NULL;
6324 return -ENOMEM;
6325 }
6326
6327 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
6328 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6329 kfree(ctx->file_data->table);
6330 kfree(ctx->file_data);
6331 ctx->file_data = NULL;
6332 return -ENOMEM;
6333 }
6334 ctx->file_data->put_llist.first = NULL;
6335 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
6336
6337 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6338 percpu_ref_exit(&ctx->file_data->refs);
6339 kfree(ctx->file_data->table);
6340 kfree(ctx->file_data);
6341 ctx->file_data = NULL;
6342 return -ENOMEM;
6343 }
6344
6345 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6346 struct fixed_file_table *table;
6347 unsigned index;
6348
6349 ret = -EFAULT;
6350 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6351 break;
6352 /* allow sparse sets */
6353 if (fd == -1) {
6354 ret = 0;
6355 continue;
6356 }
6357
6358 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6359 index = i & IORING_FILE_TABLE_MASK;
6360 file = fget(fd);
6361
6362 ret = -EBADF;
6363 if (!file)
6364 break;
6365
6366 /*
6367 * Don't allow io_uring instances to be registered. If UNIX
6368 * isn't enabled, then this causes a reference cycle and this
6369 * instance can never get freed. If UNIX is enabled we'll
6370 * handle it just fine, but there's still no point in allowing
6371 * a ring fd as it doesn't support regular read/write anyway.
6372 */
6373 if (file->f_op == &io_uring_fops) {
6374 fput(file);
6375 break;
6376 }
6377 ret = 0;
6378 table->files[index] = file;
6379 }
6380
6381 if (ret) {
6382 for (i = 0; i < ctx->nr_user_files; i++) {
6383 file = io_file_from_index(ctx, i);
6384 if (file)
6385 fput(file);
6386 }
6387 for (i = 0; i < nr_tables; i++)
6388 kfree(ctx->file_data->table[i].files);
6389
6390 kfree(ctx->file_data->table);
6391 kfree(ctx->file_data);
6392 ctx->file_data = NULL;
6393 ctx->nr_user_files = 0;
6394 return ret;
6395 }
6396
6397 ret = io_sqe_files_scm(ctx);
6398 if (ret)
6399 io_sqe_files_unregister(ctx);
6400
6401 return ret;
6402}
6403
Jens Axboec3a31e62019-10-03 13:59:56 -06006404static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6405 int index)
6406{
6407#if defined(CONFIG_UNIX)
6408 struct sock *sock = ctx->ring_sock->sk;
6409 struct sk_buff_head *head = &sock->sk_receive_queue;
6410 struct sk_buff *skb;
6411
6412 /*
6413 * See if we can merge this file into an existing skb SCM_RIGHTS
6414 * file set. If there's no room, fall back to allocating a new skb
6415 * and filling it in.
6416 */
6417 spin_lock_irq(&head->lock);
6418 skb = skb_peek(head);
6419 if (skb) {
6420 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6421
6422 if (fpl->count < SCM_MAX_FD) {
6423 __skb_unlink(skb, head);
6424 spin_unlock_irq(&head->lock);
6425 fpl->fp[fpl->count] = get_file(file);
6426 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6427 fpl->count++;
6428 spin_lock_irq(&head->lock);
6429 __skb_queue_head(head, skb);
6430 } else {
6431 skb = NULL;
6432 }
6433 }
6434 spin_unlock_irq(&head->lock);
6435
6436 if (skb) {
6437 fput(file);
6438 return 0;
6439 }
6440
6441 return __io_sqe_files_scm(ctx, 1, index);
6442#else
6443 return 0;
6444#endif
6445}
6446
Jens Axboe05f3fb32019-12-09 11:22:50 -07006447static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06006448{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006449 struct fixed_file_data *data;
6450
Jens Axboedd3db2a2020-02-26 10:23:43 -07006451 /*
6452 * Juggle reference to ensure we hit zero, if needed, so we can
6453 * switch back to percpu mode
6454 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006455 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07006456 percpu_ref_put(&data->refs);
6457 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006458}
6459
6460static bool io_queue_file_removal(struct fixed_file_data *data,
6461 struct file *file)
6462{
6463 struct io_file_put *pfile, pfile_stack;
6464 DECLARE_COMPLETION_ONSTACK(done);
6465
6466 /*
6467 * If we fail allocating the struct we need for doing async reomval
6468 * of this file, just punt to sync and wait for it.
6469 */
6470 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
6471 if (!pfile) {
6472 pfile = &pfile_stack;
6473 pfile->done = &done;
6474 }
6475
6476 pfile->file = file;
6477 llist_add(&pfile->llist, &data->put_llist);
6478
6479 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07006480 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006481 wait_for_completion(&done);
6482 flush_work(&data->ref_work);
6483 return false;
6484 }
6485
6486 return true;
6487}
6488
6489static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6490 struct io_uring_files_update *up,
6491 unsigned nr_args)
6492{
6493 struct fixed_file_data *data = ctx->file_data;
6494 bool ref_switch = false;
6495 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006496 __s32 __user *fds;
6497 int fd, i, err;
6498 __u32 done;
6499
Jens Axboe05f3fb32019-12-09 11:22:50 -07006500 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006501 return -EOVERFLOW;
6502 if (done > ctx->nr_user_files)
6503 return -EINVAL;
6504
6505 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006506 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006507 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006508 struct fixed_file_table *table;
6509 unsigned index;
6510
Jens Axboec3a31e62019-10-03 13:59:56 -06006511 err = 0;
6512 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6513 err = -EFAULT;
6514 break;
6515 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006516 i = array_index_nospec(up->offset, ctx->nr_user_files);
6517 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006518 index = i & IORING_FILE_TABLE_MASK;
6519 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006520 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006521 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006522 if (io_queue_file_removal(data, file))
6523 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006524 }
6525 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006526 file = fget(fd);
6527 if (!file) {
6528 err = -EBADF;
6529 break;
6530 }
6531 /*
6532 * Don't allow io_uring instances to be registered. If
6533 * UNIX isn't enabled, then this causes a reference
6534 * cycle and this instance can never get freed. If UNIX
6535 * is enabled we'll handle it just fine, but there's
6536 * still no point in allowing a ring fd as it doesn't
6537 * support regular read/write anyway.
6538 */
6539 if (file->f_op == &io_uring_fops) {
6540 fput(file);
6541 err = -EBADF;
6542 break;
6543 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006544 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006545 err = io_sqe_file_register(ctx, file, i);
6546 if (err)
6547 break;
6548 }
6549 nr_args--;
6550 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006551 up->offset++;
6552 }
6553
Jens Axboedd3db2a2020-02-26 10:23:43 -07006554 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006555 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006556
6557 return done ? done : err;
6558}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006559static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6560 unsigned nr_args)
6561{
6562 struct io_uring_files_update up;
6563
6564 if (!ctx->file_data)
6565 return -ENXIO;
6566 if (!nr_args)
6567 return -EINVAL;
6568 if (copy_from_user(&up, arg, sizeof(up)))
6569 return -EFAULT;
6570 if (up.resv)
6571 return -EINVAL;
6572
6573 return __io_sqe_files_update(ctx, &up, nr_args);
6574}
Jens Axboec3a31e62019-10-03 13:59:56 -06006575
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006576static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006577{
6578 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6579
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006580 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006581 io_put_req(req);
6582}
6583
Pavel Begunkov24369c22020-01-28 03:15:48 +03006584static int io_init_wq_offload(struct io_ring_ctx *ctx,
6585 struct io_uring_params *p)
6586{
6587 struct io_wq_data data;
6588 struct fd f;
6589 struct io_ring_ctx *ctx_attach;
6590 unsigned int concurrency;
6591 int ret = 0;
6592
6593 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006594 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006595
6596 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6597 /* Do QD, or 4 * CPUS, whatever is smallest */
6598 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6599
6600 ctx->io_wq = io_wq_create(concurrency, &data);
6601 if (IS_ERR(ctx->io_wq)) {
6602 ret = PTR_ERR(ctx->io_wq);
6603 ctx->io_wq = NULL;
6604 }
6605 return ret;
6606 }
6607
6608 f = fdget(p->wq_fd);
6609 if (!f.file)
6610 return -EBADF;
6611
6612 if (f.file->f_op != &io_uring_fops) {
6613 ret = -EINVAL;
6614 goto out_fput;
6615 }
6616
6617 ctx_attach = f.file->private_data;
6618 /* @io_wq is protected by holding the fd */
6619 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6620 ret = -EINVAL;
6621 goto out_fput;
6622 }
6623
6624 ctx->io_wq = ctx_attach->io_wq;
6625out_fput:
6626 fdput(f);
6627 return ret;
6628}
6629
Jens Axboe6c271ce2019-01-10 11:22:30 -07006630static int io_sq_offload_start(struct io_ring_ctx *ctx,
6631 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006632{
6633 int ret;
6634
Jens Axboe6c271ce2019-01-10 11:22:30 -07006635 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006636 mmgrab(current->mm);
6637 ctx->sqo_mm = current->mm;
6638
Jens Axboe6c271ce2019-01-10 11:22:30 -07006639 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006640 ret = -EPERM;
6641 if (!capable(CAP_SYS_ADMIN))
6642 goto err;
6643
Jens Axboe917257d2019-04-13 09:28:55 -06006644 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6645 if (!ctx->sq_thread_idle)
6646 ctx->sq_thread_idle = HZ;
6647
Jens Axboe6c271ce2019-01-10 11:22:30 -07006648 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006649 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006650
Jens Axboe917257d2019-04-13 09:28:55 -06006651 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006652 if (cpu >= nr_cpu_ids)
6653 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006654 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006655 goto err;
6656
Jens Axboe6c271ce2019-01-10 11:22:30 -07006657 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6658 ctx, cpu,
6659 "io_uring-sq");
6660 } else {
6661 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6662 "io_uring-sq");
6663 }
6664 if (IS_ERR(ctx->sqo_thread)) {
6665 ret = PTR_ERR(ctx->sqo_thread);
6666 ctx->sqo_thread = NULL;
6667 goto err;
6668 }
6669 wake_up_process(ctx->sqo_thread);
6670 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6671 /* Can't have SQ_AFF without SQPOLL */
6672 ret = -EINVAL;
6673 goto err;
6674 }
6675
Pavel Begunkov24369c22020-01-28 03:15:48 +03006676 ret = io_init_wq_offload(ctx, p);
6677 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006678 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006679
6680 return 0;
6681err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006682 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006683 mmdrop(ctx->sqo_mm);
6684 ctx->sqo_mm = NULL;
6685 return ret;
6686}
6687
6688static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6689{
6690 atomic_long_sub(nr_pages, &user->locked_vm);
6691}
6692
6693static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6694{
6695 unsigned long page_limit, cur_pages, new_pages;
6696
6697 /* Don't allow more pages than we can safely lock */
6698 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6699
6700 do {
6701 cur_pages = atomic_long_read(&user->locked_vm);
6702 new_pages = cur_pages + nr_pages;
6703 if (new_pages > page_limit)
6704 return -ENOMEM;
6705 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6706 new_pages) != cur_pages);
6707
6708 return 0;
6709}
6710
6711static void io_mem_free(void *ptr)
6712{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006713 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006714
Mark Rutland52e04ef2019-04-30 17:30:21 +01006715 if (!ptr)
6716 return;
6717
6718 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006719 if (put_page_testzero(page))
6720 free_compound_page(page);
6721}
6722
6723static void *io_mem_alloc(size_t size)
6724{
6725 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6726 __GFP_NORETRY;
6727
6728 return (void *) __get_free_pages(gfp_flags, get_order(size));
6729}
6730
Hristo Venev75b28af2019-08-26 17:23:46 +00006731static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6732 size_t *sq_offset)
6733{
6734 struct io_rings *rings;
6735 size_t off, sq_array_size;
6736
6737 off = struct_size(rings, cqes, cq_entries);
6738 if (off == SIZE_MAX)
6739 return SIZE_MAX;
6740
6741#ifdef CONFIG_SMP
6742 off = ALIGN(off, SMP_CACHE_BYTES);
6743 if (off == 0)
6744 return SIZE_MAX;
6745#endif
6746
6747 sq_array_size = array_size(sizeof(u32), sq_entries);
6748 if (sq_array_size == SIZE_MAX)
6749 return SIZE_MAX;
6750
6751 if (check_add_overflow(off, sq_array_size, &off))
6752 return SIZE_MAX;
6753
6754 if (sq_offset)
6755 *sq_offset = off;
6756
6757 return off;
6758}
6759
Jens Axboe2b188cc2019-01-07 10:46:33 -07006760static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6761{
Hristo Venev75b28af2019-08-26 17:23:46 +00006762 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006763
Hristo Venev75b28af2019-08-26 17:23:46 +00006764 pages = (size_t)1 << get_order(
6765 rings_size(sq_entries, cq_entries, NULL));
6766 pages += (size_t)1 << get_order(
6767 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006768
Hristo Venev75b28af2019-08-26 17:23:46 +00006769 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006770}
6771
Jens Axboeedafcce2019-01-09 09:16:05 -07006772static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6773{
6774 int i, j;
6775
6776 if (!ctx->user_bufs)
6777 return -ENXIO;
6778
6779 for (i = 0; i < ctx->nr_user_bufs; i++) {
6780 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6781
6782 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006783 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006784
6785 if (ctx->account_mem)
6786 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006787 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006788 imu->nr_bvecs = 0;
6789 }
6790
6791 kfree(ctx->user_bufs);
6792 ctx->user_bufs = NULL;
6793 ctx->nr_user_bufs = 0;
6794 return 0;
6795}
6796
6797static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6798 void __user *arg, unsigned index)
6799{
6800 struct iovec __user *src;
6801
6802#ifdef CONFIG_COMPAT
6803 if (ctx->compat) {
6804 struct compat_iovec __user *ciovs;
6805 struct compat_iovec ciov;
6806
6807 ciovs = (struct compat_iovec __user *) arg;
6808 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6809 return -EFAULT;
6810
Jens Axboed55e5f52019-12-11 16:12:15 -07006811 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006812 dst->iov_len = ciov.iov_len;
6813 return 0;
6814 }
6815#endif
6816 src = (struct iovec __user *) arg;
6817 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6818 return -EFAULT;
6819 return 0;
6820}
6821
6822static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6823 unsigned nr_args)
6824{
6825 struct vm_area_struct **vmas = NULL;
6826 struct page **pages = NULL;
6827 int i, j, got_pages = 0;
6828 int ret = -EINVAL;
6829
6830 if (ctx->user_bufs)
6831 return -EBUSY;
6832 if (!nr_args || nr_args > UIO_MAXIOV)
6833 return -EINVAL;
6834
6835 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6836 GFP_KERNEL);
6837 if (!ctx->user_bufs)
6838 return -ENOMEM;
6839
6840 for (i = 0; i < nr_args; i++) {
6841 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6842 unsigned long off, start, end, ubuf;
6843 int pret, nr_pages;
6844 struct iovec iov;
6845 size_t size;
6846
6847 ret = io_copy_iov(ctx, &iov, arg, i);
6848 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006849 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006850
6851 /*
6852 * Don't impose further limits on the size and buffer
6853 * constraints here, we'll -EINVAL later when IO is
6854 * submitted if they are wrong.
6855 */
6856 ret = -EFAULT;
6857 if (!iov.iov_base || !iov.iov_len)
6858 goto err;
6859
6860 /* arbitrary limit, but we need something */
6861 if (iov.iov_len > SZ_1G)
6862 goto err;
6863
6864 ubuf = (unsigned long) iov.iov_base;
6865 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6866 start = ubuf >> PAGE_SHIFT;
6867 nr_pages = end - start;
6868
6869 if (ctx->account_mem) {
6870 ret = io_account_mem(ctx->user, nr_pages);
6871 if (ret)
6872 goto err;
6873 }
6874
6875 ret = 0;
6876 if (!pages || nr_pages > got_pages) {
6877 kfree(vmas);
6878 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006879 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006880 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006881 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006882 sizeof(struct vm_area_struct *),
6883 GFP_KERNEL);
6884 if (!pages || !vmas) {
6885 ret = -ENOMEM;
6886 if (ctx->account_mem)
6887 io_unaccount_mem(ctx->user, nr_pages);
6888 goto err;
6889 }
6890 got_pages = nr_pages;
6891 }
6892
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006893 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006894 GFP_KERNEL);
6895 ret = -ENOMEM;
6896 if (!imu->bvec) {
6897 if (ctx->account_mem)
6898 io_unaccount_mem(ctx->user, nr_pages);
6899 goto err;
6900 }
6901
6902 ret = 0;
6903 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006904 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006905 FOLL_WRITE | FOLL_LONGTERM,
6906 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006907 if (pret == nr_pages) {
6908 /* don't support file backed memory */
6909 for (j = 0; j < nr_pages; j++) {
6910 struct vm_area_struct *vma = vmas[j];
6911
6912 if (vma->vm_file &&
6913 !is_file_hugepages(vma->vm_file)) {
6914 ret = -EOPNOTSUPP;
6915 break;
6916 }
6917 }
6918 } else {
6919 ret = pret < 0 ? pret : -EFAULT;
6920 }
6921 up_read(&current->mm->mmap_sem);
6922 if (ret) {
6923 /*
6924 * if we did partial map, or found file backed vmas,
6925 * release any pages we did get
6926 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006927 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006928 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006929 if (ctx->account_mem)
6930 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006931 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006932 goto err;
6933 }
6934
6935 off = ubuf & ~PAGE_MASK;
6936 size = iov.iov_len;
6937 for (j = 0; j < nr_pages; j++) {
6938 size_t vec_len;
6939
6940 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6941 imu->bvec[j].bv_page = pages[j];
6942 imu->bvec[j].bv_len = vec_len;
6943 imu->bvec[j].bv_offset = off;
6944 off = 0;
6945 size -= vec_len;
6946 }
6947 /* store original address for later verification */
6948 imu->ubuf = ubuf;
6949 imu->len = iov.iov_len;
6950 imu->nr_bvecs = nr_pages;
6951
6952 ctx->nr_user_bufs++;
6953 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006954 kvfree(pages);
6955 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006956 return 0;
6957err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006958 kvfree(pages);
6959 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006960 io_sqe_buffer_unregister(ctx);
6961 return ret;
6962}
6963
Jens Axboe9b402842019-04-11 11:45:41 -06006964static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6965{
6966 __s32 __user *fds = arg;
6967 int fd;
6968
6969 if (ctx->cq_ev_fd)
6970 return -EBUSY;
6971
6972 if (copy_from_user(&fd, fds, sizeof(*fds)))
6973 return -EFAULT;
6974
6975 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6976 if (IS_ERR(ctx->cq_ev_fd)) {
6977 int ret = PTR_ERR(ctx->cq_ev_fd);
6978 ctx->cq_ev_fd = NULL;
6979 return ret;
6980 }
6981
6982 return 0;
6983}
6984
6985static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6986{
6987 if (ctx->cq_ev_fd) {
6988 eventfd_ctx_put(ctx->cq_ev_fd);
6989 ctx->cq_ev_fd = NULL;
6990 return 0;
6991 }
6992
6993 return -ENXIO;
6994}
6995
Jens Axboe5a2e7452020-02-23 16:23:11 -07006996static int __io_destroy_buffers(int id, void *p, void *data)
6997{
6998 struct io_ring_ctx *ctx = data;
6999 struct io_buffer *buf = p;
7000
7001 /* the head kbuf is the list itself */
7002 while (!list_empty(&buf->list)) {
7003 struct io_buffer *nxt;
7004
7005 nxt = list_first_entry(&buf->list, struct io_buffer, list);
7006 list_del(&nxt->list);
7007 kfree(nxt);
7008 }
7009 kfree(buf);
7010 idr_remove(&ctx->io_buffer_idr, id);
7011 return 0;
7012}
7013
7014static void io_destroy_buffers(struct io_ring_ctx *ctx)
7015{
7016 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7017 idr_destroy(&ctx->io_buffer_idr);
7018}
7019
Jens Axboe2b188cc2019-01-07 10:46:33 -07007020static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7021{
Jens Axboe6b063142019-01-10 22:13:58 -07007022 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007023 if (ctx->sqo_mm)
7024 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007025
7026 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007027 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007028 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007029 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007030 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007031 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007032
Jens Axboe2b188cc2019-01-07 10:46:33 -07007033#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007034 if (ctx->ring_sock) {
7035 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007036 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007037 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007038#endif
7039
Hristo Venev75b28af2019-08-26 17:23:46 +00007040 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007041 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007042
7043 percpu_ref_exit(&ctx->refs);
7044 if (ctx->account_mem)
7045 io_unaccount_mem(ctx->user,
7046 ring_pages(ctx->sq_entries, ctx->cq_entries));
7047 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007048 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07007049 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07007050 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007051 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007052 kfree(ctx);
7053}
7054
7055static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7056{
7057 struct io_ring_ctx *ctx = file->private_data;
7058 __poll_t mask = 0;
7059
7060 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007061 /*
7062 * synchronizes with barrier from wq_has_sleeper call in
7063 * io_commit_cqring
7064 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007065 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007066 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7067 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007068 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007069 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007070 mask |= EPOLLIN | EPOLLRDNORM;
7071
7072 return mask;
7073}
7074
7075static int io_uring_fasync(int fd, struct file *file, int on)
7076{
7077 struct io_ring_ctx *ctx = file->private_data;
7078
7079 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7080}
7081
Jens Axboe071698e2020-01-28 10:04:42 -07007082static int io_remove_personalities(int id, void *p, void *data)
7083{
7084 struct io_ring_ctx *ctx = data;
7085 const struct cred *cred;
7086
7087 cred = idr_remove(&ctx->personality_idr, id);
7088 if (cred)
7089 put_cred(cred);
7090 return 0;
7091}
7092
Jens Axboe2b188cc2019-01-07 10:46:33 -07007093static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7094{
7095 mutex_lock(&ctx->uring_lock);
7096 percpu_ref_kill(&ctx->refs);
7097 mutex_unlock(&ctx->uring_lock);
7098
Jens Axboedf069d82020-02-04 16:48:34 -07007099 /*
7100 * Wait for sq thread to idle, if we have one. It won't spin on new
7101 * work after we've killed the ctx ref above. This is important to do
7102 * before we cancel existing commands, as the thread could otherwise
7103 * be queueing new work post that. If that's work we need to cancel,
7104 * it could cause shutdown to hang.
7105 */
7106 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
7107 cpu_relax();
7108
Jens Axboe5262f562019-09-17 12:26:57 -06007109 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007110 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007111
7112 if (ctx->io_wq)
7113 io_wq_cancel_all(ctx->io_wq);
7114
Jens Axboedef596e2019-01-09 08:59:42 -07007115 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007116 /* if we failed setting up the ctx, we might not have any rings */
7117 if (ctx->rings)
7118 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007119 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07007120 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007121 io_ring_ctx_free(ctx);
7122}
7123
7124static int io_uring_release(struct inode *inode, struct file *file)
7125{
7126 struct io_ring_ctx *ctx = file->private_data;
7127
7128 file->private_data = NULL;
7129 io_ring_ctx_wait_and_kill(ctx);
7130 return 0;
7131}
7132
Jens Axboefcb323c2019-10-24 12:39:47 -06007133static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7134 struct files_struct *files)
7135{
7136 struct io_kiocb *req;
7137 DEFINE_WAIT(wait);
7138
7139 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07007140 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06007141
7142 spin_lock_irq(&ctx->inflight_lock);
7143 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007144 if (req->work.files != files)
7145 continue;
7146 /* req is being completed, ignore */
7147 if (!refcount_inc_not_zero(&req->refs))
7148 continue;
7149 cancel_req = req;
7150 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007151 }
Jens Axboe768134d2019-11-10 20:30:53 -07007152 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007153 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007154 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007155 spin_unlock_irq(&ctx->inflight_lock);
7156
Jens Axboe768134d2019-11-10 20:30:53 -07007157 /* We need to keep going until we don't find a matching req */
7158 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007159 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007160
Jens Axboe2ca10252020-02-13 17:17:35 -07007161 if (cancel_req->flags & REQ_F_OVERFLOW) {
7162 spin_lock_irq(&ctx->completion_lock);
7163 list_del(&cancel_req->list);
7164 cancel_req->flags &= ~REQ_F_OVERFLOW;
7165 if (list_empty(&ctx->cq_overflow_list)) {
7166 clear_bit(0, &ctx->sq_check_overflow);
7167 clear_bit(0, &ctx->cq_check_overflow);
7168 }
7169 spin_unlock_irq(&ctx->completion_lock);
7170
7171 WRITE_ONCE(ctx->rings->cq_overflow,
7172 atomic_inc_return(&ctx->cached_cq_overflow));
7173
7174 /*
7175 * Put inflight ref and overflow ref. If that's
7176 * all we had, then we're done with this request.
7177 */
7178 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7179 io_put_req(cancel_req);
7180 continue;
7181 }
7182 }
7183
Bob Liu2f6d9b92019-11-13 18:06:24 +08007184 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7185 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007186 schedule();
7187 }
Jens Axboe768134d2019-11-10 20:30:53 -07007188 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007189}
7190
7191static int io_uring_flush(struct file *file, void *data)
7192{
7193 struct io_ring_ctx *ctx = file->private_data;
7194
7195 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007196
7197 /*
7198 * If the task is going away, cancel work it may have pending
7199 */
7200 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7201 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7202
Jens Axboefcb323c2019-10-24 12:39:47 -06007203 return 0;
7204}
7205
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007206static void *io_uring_validate_mmap_request(struct file *file,
7207 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007208{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007209 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007210 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007211 struct page *page;
7212 void *ptr;
7213
7214 switch (offset) {
7215 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007216 case IORING_OFF_CQ_RING:
7217 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007218 break;
7219 case IORING_OFF_SQES:
7220 ptr = ctx->sq_sqes;
7221 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007222 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007223 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007224 }
7225
7226 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007227 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007228 return ERR_PTR(-EINVAL);
7229
7230 return ptr;
7231}
7232
7233#ifdef CONFIG_MMU
7234
7235static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7236{
7237 size_t sz = vma->vm_end - vma->vm_start;
7238 unsigned long pfn;
7239 void *ptr;
7240
7241 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7242 if (IS_ERR(ptr))
7243 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007244
7245 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7246 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7247}
7248
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007249#else /* !CONFIG_MMU */
7250
7251static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7252{
7253 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7254}
7255
7256static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7257{
7258 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7259}
7260
7261static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7262 unsigned long addr, unsigned long len,
7263 unsigned long pgoff, unsigned long flags)
7264{
7265 void *ptr;
7266
7267 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7268 if (IS_ERR(ptr))
7269 return PTR_ERR(ptr);
7270
7271 return (unsigned long) ptr;
7272}
7273
7274#endif /* !CONFIG_MMU */
7275
Jens Axboe2b188cc2019-01-07 10:46:33 -07007276SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7277 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7278 size_t, sigsz)
7279{
7280 struct io_ring_ctx *ctx;
7281 long ret = -EBADF;
7282 int submitted = 0;
7283 struct fd f;
7284
Jens Axboeb41e9852020-02-17 09:52:41 -07007285 if (current->task_works)
7286 task_work_run();
7287
Jens Axboe6c271ce2019-01-10 11:22:30 -07007288 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007289 return -EINVAL;
7290
7291 f = fdget(fd);
7292 if (!f.file)
7293 return -EBADF;
7294
7295 ret = -EOPNOTSUPP;
7296 if (f.file->f_op != &io_uring_fops)
7297 goto out_fput;
7298
7299 ret = -ENXIO;
7300 ctx = f.file->private_data;
7301 if (!percpu_ref_tryget(&ctx->refs))
7302 goto out_fput;
7303
Jens Axboe6c271ce2019-01-10 11:22:30 -07007304 /*
7305 * For SQ polling, the thread will do all submissions and completions.
7306 * Just return the requested submit count, and wake the thread if
7307 * we were asked to.
7308 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007309 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007310 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007311 if (!list_empty_careful(&ctx->cq_overflow_list))
7312 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007313 if (flags & IORING_ENTER_SQ_WAKEUP)
7314 wake_up(&ctx->sqo_wait);
7315 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007316 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007317 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007318
7319 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007320 /* already have mm, so io_submit_sqes() won't try to grab it */
7321 cur_mm = ctx->sqo_mm;
7322 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
7323 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007324 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007325
7326 if (submitted != to_submit)
7327 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007328 }
7329 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007330 unsigned nr_events = 0;
7331
Jens Axboe2b188cc2019-01-07 10:46:33 -07007332 min_complete = min(min_complete, ctx->cq_entries);
7333
Jens Axboedef596e2019-01-09 08:59:42 -07007334 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07007335 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007336 } else {
7337 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7338 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007339 }
7340
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007341out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007342 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007343out_fput:
7344 fdput(f);
7345 return submitted ? submitted : ret;
7346}
7347
Tobias Klauserbebdb652020-02-26 18:38:32 +01007348#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007349static int io_uring_show_cred(int id, void *p, void *data)
7350{
7351 const struct cred *cred = p;
7352 struct seq_file *m = data;
7353 struct user_namespace *uns = seq_user_ns(m);
7354 struct group_info *gi;
7355 kernel_cap_t cap;
7356 unsigned __capi;
7357 int g;
7358
7359 seq_printf(m, "%5d\n", id);
7360 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7361 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7362 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7363 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7364 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7365 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7366 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7367 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7368 seq_puts(m, "\n\tGroups:\t");
7369 gi = cred->group_info;
7370 for (g = 0; g < gi->ngroups; g++) {
7371 seq_put_decimal_ull(m, g ? " " : "",
7372 from_kgid_munged(uns, gi->gid[g]));
7373 }
7374 seq_puts(m, "\n\tCapEff:\t");
7375 cap = cred->cap_effective;
7376 CAP_FOR_EACH_U32(__capi)
7377 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7378 seq_putc(m, '\n');
7379 return 0;
7380}
7381
7382static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7383{
7384 int i;
7385
7386 mutex_lock(&ctx->uring_lock);
7387 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7388 for (i = 0; i < ctx->nr_user_files; i++) {
7389 struct fixed_file_table *table;
7390 struct file *f;
7391
7392 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7393 f = table->files[i & IORING_FILE_TABLE_MASK];
7394 if (f)
7395 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7396 else
7397 seq_printf(m, "%5u: <none>\n", i);
7398 }
7399 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7400 for (i = 0; i < ctx->nr_user_bufs; i++) {
7401 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7402
7403 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7404 (unsigned int) buf->len);
7405 }
7406 if (!idr_is_empty(&ctx->personality_idr)) {
7407 seq_printf(m, "Personalities:\n");
7408 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7409 }
Jens Axboed7718a92020-02-14 22:23:12 -07007410 seq_printf(m, "PollList:\n");
7411 spin_lock_irq(&ctx->completion_lock);
7412 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7413 struct hlist_head *list = &ctx->cancel_hash[i];
7414 struct io_kiocb *req;
7415
7416 hlist_for_each_entry(req, list, hash_node)
7417 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7418 req->task->task_works != NULL);
7419 }
7420 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007421 mutex_unlock(&ctx->uring_lock);
7422}
7423
7424static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7425{
7426 struct io_ring_ctx *ctx = f->private_data;
7427
7428 if (percpu_ref_tryget(&ctx->refs)) {
7429 __io_uring_show_fdinfo(ctx, m);
7430 percpu_ref_put(&ctx->refs);
7431 }
7432}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007433#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007434
Jens Axboe2b188cc2019-01-07 10:46:33 -07007435static const struct file_operations io_uring_fops = {
7436 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007437 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007438 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007439#ifndef CONFIG_MMU
7440 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7441 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7442#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007443 .poll = io_uring_poll,
7444 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007445#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007446 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007447#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007448};
7449
7450static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7451 struct io_uring_params *p)
7452{
Hristo Venev75b28af2019-08-26 17:23:46 +00007453 struct io_rings *rings;
7454 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007455
Hristo Venev75b28af2019-08-26 17:23:46 +00007456 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7457 if (size == SIZE_MAX)
7458 return -EOVERFLOW;
7459
7460 rings = io_mem_alloc(size);
7461 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007462 return -ENOMEM;
7463
Hristo Venev75b28af2019-08-26 17:23:46 +00007464 ctx->rings = rings;
7465 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7466 rings->sq_ring_mask = p->sq_entries - 1;
7467 rings->cq_ring_mask = p->cq_entries - 1;
7468 rings->sq_ring_entries = p->sq_entries;
7469 rings->cq_ring_entries = p->cq_entries;
7470 ctx->sq_mask = rings->sq_ring_mask;
7471 ctx->cq_mask = rings->cq_ring_mask;
7472 ctx->sq_entries = rings->sq_ring_entries;
7473 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007474
7475 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007476 if (size == SIZE_MAX) {
7477 io_mem_free(ctx->rings);
7478 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007479 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007480 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007481
7482 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007483 if (!ctx->sq_sqes) {
7484 io_mem_free(ctx->rings);
7485 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007486 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007487 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007488
Jens Axboe2b188cc2019-01-07 10:46:33 -07007489 return 0;
7490}
7491
7492/*
7493 * Allocate an anonymous fd, this is what constitutes the application
7494 * visible backing of an io_uring instance. The application mmaps this
7495 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7496 * we have to tie this fd to a socket for file garbage collection purposes.
7497 */
7498static int io_uring_get_fd(struct io_ring_ctx *ctx)
7499{
7500 struct file *file;
7501 int ret;
7502
7503#if defined(CONFIG_UNIX)
7504 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7505 &ctx->ring_sock);
7506 if (ret)
7507 return ret;
7508#endif
7509
7510 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7511 if (ret < 0)
7512 goto err;
7513
7514 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7515 O_RDWR | O_CLOEXEC);
7516 if (IS_ERR(file)) {
7517 put_unused_fd(ret);
7518 ret = PTR_ERR(file);
7519 goto err;
7520 }
7521
7522#if defined(CONFIG_UNIX)
7523 ctx->ring_sock->file = file;
7524#endif
7525 fd_install(ret, file);
7526 return ret;
7527err:
7528#if defined(CONFIG_UNIX)
7529 sock_release(ctx->ring_sock);
7530 ctx->ring_sock = NULL;
7531#endif
7532 return ret;
7533}
7534
7535static int io_uring_create(unsigned entries, struct io_uring_params *p)
7536{
7537 struct user_struct *user = NULL;
7538 struct io_ring_ctx *ctx;
7539 bool account_mem;
7540 int ret;
7541
Jens Axboe8110c1a2019-12-28 15:39:54 -07007542 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007543 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007544 if (entries > IORING_MAX_ENTRIES) {
7545 if (!(p->flags & IORING_SETUP_CLAMP))
7546 return -EINVAL;
7547 entries = IORING_MAX_ENTRIES;
7548 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007549
7550 /*
7551 * Use twice as many entries for the CQ ring. It's possible for the
7552 * application to drive a higher depth than the size of the SQ ring,
7553 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007554 * some flexibility in overcommitting a bit. If the application has
7555 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7556 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007557 */
7558 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007559 if (p->flags & IORING_SETUP_CQSIZE) {
7560 /*
7561 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7562 * to a power-of-two, if it isn't already. We do NOT impose
7563 * any cq vs sq ring sizing.
7564 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007565 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007566 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007567 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7568 if (!(p->flags & IORING_SETUP_CLAMP))
7569 return -EINVAL;
7570 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7571 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007572 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7573 } else {
7574 p->cq_entries = 2 * p->sq_entries;
7575 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007576
7577 user = get_uid(current_user());
7578 account_mem = !capable(CAP_IPC_LOCK);
7579
7580 if (account_mem) {
7581 ret = io_account_mem(user,
7582 ring_pages(p->sq_entries, p->cq_entries));
7583 if (ret) {
7584 free_uid(user);
7585 return ret;
7586 }
7587 }
7588
7589 ctx = io_ring_ctx_alloc(p);
7590 if (!ctx) {
7591 if (account_mem)
7592 io_unaccount_mem(user, ring_pages(p->sq_entries,
7593 p->cq_entries));
7594 free_uid(user);
7595 return -ENOMEM;
7596 }
7597 ctx->compat = in_compat_syscall();
7598 ctx->account_mem = account_mem;
7599 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007600 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007601
7602 ret = io_allocate_scq_urings(ctx, p);
7603 if (ret)
7604 goto err;
7605
Jens Axboe6c271ce2019-01-10 11:22:30 -07007606 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007607 if (ret)
7608 goto err;
7609
Jens Axboe2b188cc2019-01-07 10:46:33 -07007610 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007611 p->sq_off.head = offsetof(struct io_rings, sq.head);
7612 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7613 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7614 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7615 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7616 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7617 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007618
7619 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007620 p->cq_off.head = offsetof(struct io_rings, cq.head);
7621 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7622 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7623 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7624 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7625 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007626
Jens Axboe044c1ab2019-10-28 09:15:33 -06007627 /*
7628 * Install ring fd as the very last thing, so we don't risk someone
7629 * having closed it before we finish setup
7630 */
7631 ret = io_uring_get_fd(ctx);
7632 if (ret < 0)
7633 goto err;
7634
Jens Axboeda8c9692019-12-02 18:51:26 -07007635 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007636 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007637 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007638 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007639 return ret;
7640err:
7641 io_ring_ctx_wait_and_kill(ctx);
7642 return ret;
7643}
7644
7645/*
7646 * Sets up an aio uring context, and returns the fd. Applications asks for a
7647 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7648 * params structure passed in.
7649 */
7650static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7651{
7652 struct io_uring_params p;
7653 long ret;
7654 int i;
7655
7656 if (copy_from_user(&p, params, sizeof(p)))
7657 return -EFAULT;
7658 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7659 if (p.resv[i])
7660 return -EINVAL;
7661 }
7662
Jens Axboe6c271ce2019-01-10 11:22:30 -07007663 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007664 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007665 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007666 return -EINVAL;
7667
7668 ret = io_uring_create(entries, &p);
7669 if (ret < 0)
7670 return ret;
7671
7672 if (copy_to_user(params, &p, sizeof(p)))
7673 return -EFAULT;
7674
7675 return ret;
7676}
7677
7678SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7679 struct io_uring_params __user *, params)
7680{
7681 return io_uring_setup(entries, params);
7682}
7683
Jens Axboe66f4af92020-01-16 15:36:52 -07007684static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7685{
7686 struct io_uring_probe *p;
7687 size_t size;
7688 int i, ret;
7689
7690 size = struct_size(p, ops, nr_args);
7691 if (size == SIZE_MAX)
7692 return -EOVERFLOW;
7693 p = kzalloc(size, GFP_KERNEL);
7694 if (!p)
7695 return -ENOMEM;
7696
7697 ret = -EFAULT;
7698 if (copy_from_user(p, arg, size))
7699 goto out;
7700 ret = -EINVAL;
7701 if (memchr_inv(p, 0, size))
7702 goto out;
7703
7704 p->last_op = IORING_OP_LAST - 1;
7705 if (nr_args > IORING_OP_LAST)
7706 nr_args = IORING_OP_LAST;
7707
7708 for (i = 0; i < nr_args; i++) {
7709 p->ops[i].op = i;
7710 if (!io_op_defs[i].not_supported)
7711 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7712 }
7713 p->ops_len = i;
7714
7715 ret = 0;
7716 if (copy_to_user(arg, p, size))
7717 ret = -EFAULT;
7718out:
7719 kfree(p);
7720 return ret;
7721}
7722
Jens Axboe071698e2020-01-28 10:04:42 -07007723static int io_register_personality(struct io_ring_ctx *ctx)
7724{
7725 const struct cred *creds = get_current_cred();
7726 int id;
7727
7728 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7729 USHRT_MAX, GFP_KERNEL);
7730 if (id < 0)
7731 put_cred(creds);
7732 return id;
7733}
7734
7735static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7736{
7737 const struct cred *old_creds;
7738
7739 old_creds = idr_remove(&ctx->personality_idr, id);
7740 if (old_creds) {
7741 put_cred(old_creds);
7742 return 0;
7743 }
7744
7745 return -EINVAL;
7746}
7747
7748static bool io_register_op_must_quiesce(int op)
7749{
7750 switch (op) {
7751 case IORING_UNREGISTER_FILES:
7752 case IORING_REGISTER_FILES_UPDATE:
7753 case IORING_REGISTER_PROBE:
7754 case IORING_REGISTER_PERSONALITY:
7755 case IORING_UNREGISTER_PERSONALITY:
7756 return false;
7757 default:
7758 return true;
7759 }
7760}
7761
Jens Axboeedafcce2019-01-09 09:16:05 -07007762static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7763 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007764 __releases(ctx->uring_lock)
7765 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007766{
7767 int ret;
7768
Jens Axboe35fa71a2019-04-22 10:23:23 -06007769 /*
7770 * We're inside the ring mutex, if the ref is already dying, then
7771 * someone else killed the ctx or is already going through
7772 * io_uring_register().
7773 */
7774 if (percpu_ref_is_dying(&ctx->refs))
7775 return -ENXIO;
7776
Jens Axboe071698e2020-01-28 10:04:42 -07007777 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007778 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007779
Jens Axboe05f3fb32019-12-09 11:22:50 -07007780 /*
7781 * Drop uring mutex before waiting for references to exit. If
7782 * another thread is currently inside io_uring_enter() it might
7783 * need to grab the uring_lock to make progress. If we hold it
7784 * here across the drain wait, then we can deadlock. It's safe
7785 * to drop the mutex here, since no new references will come in
7786 * after we've killed the percpu ref.
7787 */
7788 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007789 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007790 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007791 if (ret) {
7792 percpu_ref_resurrect(&ctx->refs);
7793 ret = -EINTR;
7794 goto out;
7795 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007796 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007797
7798 switch (opcode) {
7799 case IORING_REGISTER_BUFFERS:
7800 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7801 break;
7802 case IORING_UNREGISTER_BUFFERS:
7803 ret = -EINVAL;
7804 if (arg || nr_args)
7805 break;
7806 ret = io_sqe_buffer_unregister(ctx);
7807 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007808 case IORING_REGISTER_FILES:
7809 ret = io_sqe_files_register(ctx, arg, nr_args);
7810 break;
7811 case IORING_UNREGISTER_FILES:
7812 ret = -EINVAL;
7813 if (arg || nr_args)
7814 break;
7815 ret = io_sqe_files_unregister(ctx);
7816 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007817 case IORING_REGISTER_FILES_UPDATE:
7818 ret = io_sqe_files_update(ctx, arg, nr_args);
7819 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007820 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007821 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007822 ret = -EINVAL;
7823 if (nr_args != 1)
7824 break;
7825 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007826 if (ret)
7827 break;
7828 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7829 ctx->eventfd_async = 1;
7830 else
7831 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007832 break;
7833 case IORING_UNREGISTER_EVENTFD:
7834 ret = -EINVAL;
7835 if (arg || nr_args)
7836 break;
7837 ret = io_eventfd_unregister(ctx);
7838 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007839 case IORING_REGISTER_PROBE:
7840 ret = -EINVAL;
7841 if (!arg || nr_args > 256)
7842 break;
7843 ret = io_probe(ctx, arg, nr_args);
7844 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007845 case IORING_REGISTER_PERSONALITY:
7846 ret = -EINVAL;
7847 if (arg || nr_args)
7848 break;
7849 ret = io_register_personality(ctx);
7850 break;
7851 case IORING_UNREGISTER_PERSONALITY:
7852 ret = -EINVAL;
7853 if (arg)
7854 break;
7855 ret = io_unregister_personality(ctx, nr_args);
7856 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007857 default:
7858 ret = -EINVAL;
7859 break;
7860 }
7861
Jens Axboe071698e2020-01-28 10:04:42 -07007862 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007863 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007864 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007865out:
7866 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007867 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007868 return ret;
7869}
7870
7871SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7872 void __user *, arg, unsigned int, nr_args)
7873{
7874 struct io_ring_ctx *ctx;
7875 long ret = -EBADF;
7876 struct fd f;
7877
7878 f = fdget(fd);
7879 if (!f.file)
7880 return -EBADF;
7881
7882 ret = -EOPNOTSUPP;
7883 if (f.file->f_op != &io_uring_fops)
7884 goto out_fput;
7885
7886 ctx = f.file->private_data;
7887
7888 mutex_lock(&ctx->uring_lock);
7889 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7890 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007891 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7892 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007893out_fput:
7894 fdput(f);
7895 return ret;
7896}
7897
Jens Axboe2b188cc2019-01-07 10:46:33 -07007898static int __init io_uring_init(void)
7899{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007900#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7901 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7902 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7903} while (0)
7904
7905#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7906 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7907 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7908 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7909 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7910 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7911 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7912 BUILD_BUG_SQE_ELEM(8, __u64, off);
7913 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7914 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007915 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007916 BUILD_BUG_SQE_ELEM(24, __u32, len);
7917 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7918 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7919 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7920 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7921 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7922 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7923 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7924 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7925 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7926 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7927 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7928 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7929 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007930 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007931 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7932 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7933 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007934 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007935
Jens Axboed3656342019-12-18 09:50:26 -07007936 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007937 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7938 return 0;
7939};
7940__initcall(io_uring_init);