blob: dfe40bf80adc8f995754fc3325b597cd8d737c1f [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,
Jens Axboe84557872020-03-03 15:28:17 -0700514
515 /* not a real bit, just to check we're not overflowing the space */
516 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300517};
518
519enum {
520 /* ctx owns file */
521 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
522 /* drain existing IO first */
523 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
524 /* linked sqes */
525 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
526 /* doesn't sever on completion < 0 */
527 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
528 /* IOSQE_ASYNC */
529 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700530 /* IOSQE_BUFFER_SELECT */
531 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300532
533 /* already grabbed next link */
534 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
535 /* fail rest of links */
536 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
537 /* on inflight list */
538 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
539 /* read/write uses file position */
540 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
541 /* must not punt to workers */
542 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
543 /* polled IO has completed */
544 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
545 /* has linked timeout */
546 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
547 /* timeout request */
548 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
549 /* regular file */
550 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
551 /* must be punted even for NONBLOCK */
552 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
553 /* no timeout sequence */
554 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
555 /* completion under lock */
556 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300557 /* needs cleanup */
558 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700559 /* in overflow list */
560 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700561 /* already went through poll handler */
562 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700563 /* buffer already selected */
564 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700565};
566
567struct async_poll {
568 struct io_poll_iocb poll;
569 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300570};
571
Jens Axboe09bb8392019-03-13 12:39:28 -0600572/*
573 * NOTE! Each of the iocb union members has the file pointer
574 * as the first entry in their struct definition. So you can
575 * access the file pointer through any of the sub-structs,
576 * or directly as just 'ki_filp' in this struct.
577 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700578struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700579 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600580 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700581 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700582 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700583 struct io_accept accept;
584 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700585 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700586 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700587 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700588 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700589 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700590 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700591 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700592 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700593 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700594 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300595 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700596 struct io_provide_buf pbuf;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700597 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700598
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700599 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300600 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700601 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700602
603 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700604 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700605 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700606 refcount_t refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700607 struct task_struct *task;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600609 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600610 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700611
Jens Axboed7718a92020-02-14 22:23:12 -0700612 struct list_head link_list;
613
Jens Axboefcb323c2019-10-24 12:39:47 -0600614 struct list_head inflight_entry;
615
Jens Axboeb41e9852020-02-17 09:52:41 -0700616 union {
617 /*
618 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700619 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
620 * async armed poll handlers for regular commands. The latter
621 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700622 */
623 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700624 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700625 struct hlist_node hash_node;
626 struct async_poll *apoll;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700627 int cflags;
Jens Axboeb41e9852020-02-17 09:52:41 -0700628 };
629 struct io_wq_work work;
630 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631};
632
633#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700634#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700635
Jens Axboe9a56a232019-01-09 09:06:50 -0700636struct io_submit_state {
637 struct blk_plug plug;
638
639 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700640 * io_kiocb alloc cache
641 */
642 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300643 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700644
645 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700646 * File reference cache
647 */
648 struct file *file;
649 unsigned int fd;
650 unsigned int has_refs;
651 unsigned int used_refs;
652 unsigned int ios_left;
653};
654
Jens Axboed3656342019-12-18 09:50:26 -0700655struct io_op_def {
656 /* needs req->io allocated for deferral/async */
657 unsigned async_ctx : 1;
658 /* needs current->mm setup, does mm access */
659 unsigned needs_mm : 1;
660 /* needs req->file assigned */
661 unsigned needs_file : 1;
662 /* needs req->file assigned IFF fd is >= 0 */
663 unsigned fd_non_neg : 1;
664 /* hash wq insertion if file is a regular file */
665 unsigned hash_reg_file : 1;
666 /* unbound wq insertion if file is a non-regular file */
667 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700668 /* opcode is not supported by this kernel */
669 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700670 /* needs file table */
671 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700672 /* needs ->fs */
673 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700674 /* set if opcode supports polled "wait" */
675 unsigned pollin : 1;
676 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700677 /* op supports buffer selection */
678 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700679};
680
681static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300682 [IORING_OP_NOP] = {},
683 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700684 .async_ctx = 1,
685 .needs_mm = 1,
686 .needs_file = 1,
687 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700688 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700689 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700690 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300691 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700692 .async_ctx = 1,
693 .needs_mm = 1,
694 .needs_file = 1,
695 .hash_reg_file = 1,
696 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700697 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700698 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300699 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700700 .needs_file = 1,
701 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300702 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700703 .needs_file = 1,
704 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700705 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700706 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300707 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700708 .needs_file = 1,
709 .hash_reg_file = 1,
710 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700711 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700712 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300713 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700714 .needs_file = 1,
715 .unbound_nonreg_file = 1,
716 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300717 [IORING_OP_POLL_REMOVE] = {},
718 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700719 .needs_file = 1,
720 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300721 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700722 .async_ctx = 1,
723 .needs_mm = 1,
724 .needs_file = 1,
725 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700726 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700727 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700728 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700730 .async_ctx = 1,
731 .needs_mm = 1,
732 .needs_file = 1,
733 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700734 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700735 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700736 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700737 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700739 .async_ctx = 1,
740 .needs_mm = 1,
741 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300742 [IORING_OP_TIMEOUT_REMOVE] = {},
743 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700744 .needs_mm = 1,
745 .needs_file = 1,
746 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700747 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700748 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700749 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300750 [IORING_OP_ASYNC_CANCEL] = {},
751 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700752 .async_ctx = 1,
753 .needs_mm = 1,
754 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300755 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700756 .async_ctx = 1,
757 .needs_mm = 1,
758 .needs_file = 1,
759 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700760 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700761 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300762 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700763 .needs_file = 1,
764 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300765 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700766 .needs_file = 1,
767 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700768 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700769 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700770 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300771 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700772 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700773 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700774 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300775 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700776 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700777 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700778 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300779 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700780 .needs_mm = 1,
781 .needs_file = 1,
782 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700783 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700784 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300785 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700786 .needs_mm = 1,
787 .needs_file = 1,
788 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700789 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700790 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700791 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300792 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700793 .needs_mm = 1,
794 .needs_file = 1,
795 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700796 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700799 .needs_file = 1,
800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700802 .needs_mm = 1,
803 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300804 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700805 .needs_mm = 1,
806 .needs_file = 1,
807 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700808 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700809 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300810 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700811 .needs_mm = 1,
812 .needs_file = 1,
813 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700814 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700815 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700816 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300817 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700818 .needs_file = 1,
819 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700820 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700821 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700822 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700823 [IORING_OP_EPOLL_CTL] = {
824 .unbound_nonreg_file = 1,
825 .file_table = 1,
826 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300827 [IORING_OP_SPLICE] = {
828 .needs_file = 1,
829 .hash_reg_file = 1,
830 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700831 },
832 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700833 [IORING_OP_REMOVE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700834};
835
Jens Axboe561fb042019-10-24 07:25:42 -0600836static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700837static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800838static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700839static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700840static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
841static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700842static int __io_sqe_files_update(struct io_ring_ctx *ctx,
843 struct io_uring_files_update *ip,
844 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700845static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700846static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300847static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700848static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
849 int fd, struct file **out_file, bool fixed);
850static void __io_queue_sqe(struct io_kiocb *req,
851 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600852
Jens Axboe2b188cc2019-01-07 10:46:33 -0700853static struct kmem_cache *req_cachep;
854
855static const struct file_operations io_uring_fops;
856
857struct sock *io_uring_get_socket(struct file *file)
858{
859#if defined(CONFIG_UNIX)
860 if (file->f_op == &io_uring_fops) {
861 struct io_ring_ctx *ctx = file->private_data;
862
863 return ctx->ring_sock->sk;
864 }
865#endif
866 return NULL;
867}
868EXPORT_SYMBOL(io_uring_get_socket);
869
870static void io_ring_ctx_ref_free(struct percpu_ref *ref)
871{
872 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
873
Jens Axboe206aefd2019-11-07 18:27:42 -0700874 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700875}
876
877static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
878{
879 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700880 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700881
882 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
883 if (!ctx)
884 return NULL;
885
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700886 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
887 if (!ctx->fallback_req)
888 goto err;
889
Jens Axboe206aefd2019-11-07 18:27:42 -0700890 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
891 if (!ctx->completions)
892 goto err;
893
Jens Axboe78076bb2019-12-04 19:56:40 -0700894 /*
895 * Use 5 bits less than the max cq entries, that should give us around
896 * 32 entries per hash list if totally full and uniformly spread.
897 */
898 hash_bits = ilog2(p->cq_entries);
899 hash_bits -= 5;
900 if (hash_bits <= 0)
901 hash_bits = 1;
902 ctx->cancel_hash_bits = hash_bits;
903 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
904 GFP_KERNEL);
905 if (!ctx->cancel_hash)
906 goto err;
907 __hash_init(ctx->cancel_hash, 1U << hash_bits);
908
Roman Gushchin21482892019-05-07 10:01:48 -0700909 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700910 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
911 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700912
913 ctx->flags = p->flags;
914 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700915 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700916 init_completion(&ctx->completions[0]);
917 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700918 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700919 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700920 mutex_init(&ctx->uring_lock);
921 init_waitqueue_head(&ctx->wait);
922 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700923 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600924 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600925 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600926 init_waitqueue_head(&ctx->inflight_wait);
927 spin_lock_init(&ctx->inflight_lock);
928 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700929 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700930err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700931 if (ctx->fallback_req)
932 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700933 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700934 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700935 kfree(ctx);
936 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700937}
938
Bob Liu9d858b22019-11-13 18:06:25 +0800939static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600940{
Jackie Liua197f662019-11-08 08:09:12 -0700941 struct io_ring_ctx *ctx = req->ctx;
942
Jens Axboe498ccd92019-10-25 10:04:25 -0600943 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
944 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600945}
946
Bob Liu9d858b22019-11-13 18:06:25 +0800947static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600948{
Pavel Begunkov87987892020-01-18 01:22:30 +0300949 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800950 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600951
Bob Liu9d858b22019-11-13 18:06:25 +0800952 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600953}
954
955static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600956{
957 struct io_kiocb *req;
958
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600959 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800960 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600961 list_del_init(&req->list);
962 return req;
963 }
964
965 return NULL;
966}
967
Jens Axboe5262f562019-09-17 12:26:57 -0600968static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
969{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600970 struct io_kiocb *req;
971
972 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700973 if (req) {
974 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
975 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800976 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700977 list_del_init(&req->list);
978 return req;
979 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600980 }
981
982 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600983}
984
Jens Axboede0617e2019-04-06 21:51:27 -0600985static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700986{
Hristo Venev75b28af2019-08-26 17:23:46 +0000987 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700988
Pavel Begunkov07910152020-01-17 03:52:46 +0300989 /* order cqe stores with ring update */
990 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700991
Pavel Begunkov07910152020-01-17 03:52:46 +0300992 if (wq_has_sleeper(&ctx->cq_wait)) {
993 wake_up_interruptible(&ctx->cq_wait);
994 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700995 }
996}
997
Jens Axboecccf0ee2020-01-27 16:34:48 -0700998static inline void io_req_work_grab_env(struct io_kiocb *req,
999 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001000{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001001 if (!req->work.mm && def->needs_mm) {
1002 mmgrab(current->mm);
1003 req->work.mm = current->mm;
1004 }
1005 if (!req->work.creds)
1006 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001007 if (!req->work.fs && def->needs_fs) {
1008 spin_lock(&current->fs->lock);
1009 if (!current->fs->in_exec) {
1010 req->work.fs = current->fs;
1011 req->work.fs->users++;
1012 } else {
1013 req->work.flags |= IO_WQ_WORK_CANCEL;
1014 }
1015 spin_unlock(&current->fs->lock);
1016 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001017 if (!req->work.task_pid)
1018 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001019}
1020
1021static inline void io_req_work_drop_env(struct io_kiocb *req)
1022{
1023 if (req->work.mm) {
1024 mmdrop(req->work.mm);
1025 req->work.mm = NULL;
1026 }
1027 if (req->work.creds) {
1028 put_cred(req->work.creds);
1029 req->work.creds = NULL;
1030 }
Jens Axboeff002b32020-02-07 16:05:21 -07001031 if (req->work.fs) {
1032 struct fs_struct *fs = req->work.fs;
1033
1034 spin_lock(&req->work.fs->lock);
1035 if (--fs->users)
1036 fs = NULL;
1037 spin_unlock(&req->work.fs->lock);
1038 if (fs)
1039 free_fs_struct(fs);
1040 }
Jens Axboe561fb042019-10-24 07:25:42 -06001041}
1042
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001043static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001044 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001045{
Jens Axboed3656342019-12-18 09:50:26 -07001046 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001047
Jens Axboed3656342019-12-18 09:50:26 -07001048 if (req->flags & REQ_F_ISREG) {
1049 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001050 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001051 } else {
1052 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001053 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001054 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001055
1056 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001057
Jens Axboe94ae5e72019-11-14 19:39:52 -07001058 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001059}
1060
Jackie Liua197f662019-11-08 08:09:12 -07001061static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001062{
Jackie Liua197f662019-11-08 08:09:12 -07001063 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001064 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001065
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001066 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001067
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001068 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1069 &req->work, req->flags);
1070 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001071
1072 if (link)
1073 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001074}
1075
Jens Axboe5262f562019-09-17 12:26:57 -06001076static void io_kill_timeout(struct io_kiocb *req)
1077{
1078 int ret;
1079
Jens Axboe2d283902019-12-04 11:08:05 -07001080 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001081 if (ret != -1) {
1082 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001083 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001084 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001085 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001086 }
1087}
1088
1089static void io_kill_timeouts(struct io_ring_ctx *ctx)
1090{
1091 struct io_kiocb *req, *tmp;
1092
1093 spin_lock_irq(&ctx->completion_lock);
1094 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1095 io_kill_timeout(req);
1096 spin_unlock_irq(&ctx->completion_lock);
1097}
1098
Jens Axboede0617e2019-04-06 21:51:27 -06001099static void io_commit_cqring(struct io_ring_ctx *ctx)
1100{
1101 struct io_kiocb *req;
1102
Jens Axboe5262f562019-09-17 12:26:57 -06001103 while ((req = io_get_timeout_req(ctx)) != NULL)
1104 io_kill_timeout(req);
1105
Jens Axboede0617e2019-04-06 21:51:27 -06001106 __io_commit_cqring(ctx);
1107
Pavel Begunkov87987892020-01-18 01:22:30 +03001108 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001109 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001110}
1111
Jens Axboe2b188cc2019-01-07 10:46:33 -07001112static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1113{
Hristo Venev75b28af2019-08-26 17:23:46 +00001114 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001115 unsigned tail;
1116
1117 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001118 /*
1119 * writes to the cq entry need to come after reading head; the
1120 * control dependency is enough as we're using WRITE_ONCE to
1121 * fill the cq entry
1122 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001123 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001124 return NULL;
1125
1126 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001127 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001128}
1129
Jens Axboef2842ab2020-01-08 11:04:00 -07001130static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1131{
Jens Axboef0b493e2020-02-01 21:30:11 -07001132 if (!ctx->cq_ev_fd)
1133 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001134 if (!ctx->eventfd_async)
1135 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001136 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001137}
1138
Jens Axboeb41e9852020-02-17 09:52:41 -07001139static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001140{
1141 if (waitqueue_active(&ctx->wait))
1142 wake_up(&ctx->wait);
1143 if (waitqueue_active(&ctx->sqo_wait))
1144 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001145 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001146 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001147}
1148
Jens Axboec4a2ed72019-11-21 21:01:26 -07001149/* Returns true if there are no backlogged entries after the flush */
1150static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001151{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001152 struct io_rings *rings = ctx->rings;
1153 struct io_uring_cqe *cqe;
1154 struct io_kiocb *req;
1155 unsigned long flags;
1156 LIST_HEAD(list);
1157
1158 if (!force) {
1159 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001160 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001161 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1162 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001163 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001164 }
1165
1166 spin_lock_irqsave(&ctx->completion_lock, flags);
1167
1168 /* if force is set, the ring is going away. always drop after that */
1169 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001170 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001171
Jens Axboec4a2ed72019-11-21 21:01:26 -07001172 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001173 while (!list_empty(&ctx->cq_overflow_list)) {
1174 cqe = io_get_cqring(ctx);
1175 if (!cqe && !force)
1176 break;
1177
1178 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1179 list);
1180 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001181 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001182 if (cqe) {
1183 WRITE_ONCE(cqe->user_data, req->user_data);
1184 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001185 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001186 } else {
1187 WRITE_ONCE(ctx->rings->cq_overflow,
1188 atomic_inc_return(&ctx->cached_cq_overflow));
1189 }
1190 }
1191
1192 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001193 if (cqe) {
1194 clear_bit(0, &ctx->sq_check_overflow);
1195 clear_bit(0, &ctx->cq_check_overflow);
1196 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001197 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1198 io_cqring_ev_posted(ctx);
1199
1200 while (!list_empty(&list)) {
1201 req = list_first_entry(&list, struct io_kiocb, list);
1202 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001203 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001204 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001205
1206 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001207}
1208
Jens Axboebcda7ba2020-02-23 16:42:51 -07001209static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001210{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001211 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001212 struct io_uring_cqe *cqe;
1213
Jens Axboe78e19bb2019-11-06 15:21:34 -07001214 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001215
Jens Axboe2b188cc2019-01-07 10:46:33 -07001216 /*
1217 * If we can't get a cq entry, userspace overflowed the
1218 * submission (by quite a lot). Increment the overflow count in
1219 * the ring.
1220 */
1221 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001222 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001223 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001224 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001225 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001226 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001227 WRITE_ONCE(ctx->rings->cq_overflow,
1228 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001229 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001230 if (list_empty(&ctx->cq_overflow_list)) {
1231 set_bit(0, &ctx->sq_check_overflow);
1232 set_bit(0, &ctx->cq_check_overflow);
1233 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001234 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001235 refcount_inc(&req->refs);
1236 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001237 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001238 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001239 }
1240}
1241
Jens Axboebcda7ba2020-02-23 16:42:51 -07001242static void io_cqring_fill_event(struct io_kiocb *req, long res)
1243{
1244 __io_cqring_fill_event(req, res, 0);
1245}
1246
1247static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001249 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250 unsigned long flags;
1251
1252 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001253 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001254 io_commit_cqring(ctx);
1255 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1256
Jens Axboe8c838782019-03-12 15:48:16 -06001257 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001258}
1259
Jens Axboebcda7ba2020-02-23 16:42:51 -07001260static void io_cqring_add_event(struct io_kiocb *req, long res)
1261{
1262 __io_cqring_add_event(req, res, 0);
1263}
1264
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001265static inline bool io_is_fallback_req(struct io_kiocb *req)
1266{
1267 return req == (struct io_kiocb *)
1268 ((unsigned long) req->ctx->fallback_req & ~1UL);
1269}
1270
1271static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1272{
1273 struct io_kiocb *req;
1274
1275 req = ctx->fallback_req;
1276 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1277 return req;
1278
1279 return NULL;
1280}
1281
Jens Axboe2579f912019-01-09 09:10:43 -07001282static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1283 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001284{
Jens Axboefd6fab22019-03-14 16:30:06 -06001285 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001286 struct io_kiocb *req;
1287
Jens Axboe2579f912019-01-09 09:10:43 -07001288 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001289 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001290 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001291 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001292 } else if (!state->free_reqs) {
1293 size_t sz;
1294 int ret;
1295
1296 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001297 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1298
1299 /*
1300 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1301 * retry single alloc to be on the safe side.
1302 */
1303 if (unlikely(ret <= 0)) {
1304 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1305 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001306 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001307 ret = 1;
1308 }
Jens Axboe2579f912019-01-09 09:10:43 -07001309 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001310 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001311 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001312 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001313 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001314 }
1315
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001316got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001317 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001318 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001319 req->ctx = ctx;
1320 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001321 /* one is dropped after submission, the other at completion */
1322 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001323 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001324 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001325 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001326fallback:
1327 req = io_get_fallback_req(ctx);
1328 if (req)
1329 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001330 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001331 return NULL;
1332}
1333
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001334static inline void io_put_file(struct io_kiocb *req, struct file *file,
1335 bool fixed)
1336{
1337 if (fixed)
1338 percpu_ref_put(&req->ctx->file_data->refs);
1339 else
1340 fput(file);
1341}
1342
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001343static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001344{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001345 if (likely(!io_is_fallback_req(req)))
1346 kmem_cache_free(req_cachep, req);
1347 else
1348 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1349}
1350
Jens Axboec6ca97b302019-12-28 12:11:08 -07001351static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001352{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001353 if (req->flags & REQ_F_NEED_CLEANUP)
1354 io_cleanup_req(req);
1355
YueHaibing96fd84d2020-01-07 22:22:44 +08001356 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001357 if (req->file)
1358 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001359
1360 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001361}
1362
1363static void __io_free_req(struct io_kiocb *req)
1364{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001365 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001366
Jens Axboefcb323c2019-10-24 12:39:47 -06001367 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001368 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001369 unsigned long flags;
1370
1371 spin_lock_irqsave(&ctx->inflight_lock, flags);
1372 list_del(&req->inflight_entry);
1373 if (waitqueue_active(&ctx->inflight_wait))
1374 wake_up(&ctx->inflight_wait);
1375 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1376 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001377
1378 percpu_ref_put(&req->ctx->refs);
1379 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001380}
1381
Jens Axboec6ca97b302019-12-28 12:11:08 -07001382struct req_batch {
1383 void *reqs[IO_IOPOLL_BATCH];
1384 int to_free;
1385 int need_iter;
1386};
1387
1388static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1389{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001390 int fixed_refs = rb->to_free;
1391
Jens Axboec6ca97b302019-12-28 12:11:08 -07001392 if (!rb->to_free)
1393 return;
1394 if (rb->need_iter) {
1395 int i, inflight = 0;
1396 unsigned long flags;
1397
Jens Axboe10fef4b2020-01-09 07:52:28 -07001398 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001399 for (i = 0; i < rb->to_free; i++) {
1400 struct io_kiocb *req = rb->reqs[i];
1401
Jens Axboe10fef4b2020-01-09 07:52:28 -07001402 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001403 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001404 fixed_refs++;
1405 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001406 if (req->flags & REQ_F_INFLIGHT)
1407 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001408 __io_req_aux_free(req);
1409 }
1410 if (!inflight)
1411 goto do_free;
1412
1413 spin_lock_irqsave(&ctx->inflight_lock, flags);
1414 for (i = 0; i < rb->to_free; i++) {
1415 struct io_kiocb *req = rb->reqs[i];
1416
Jens Axboe10fef4b2020-01-09 07:52:28 -07001417 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001418 list_del(&req->inflight_entry);
1419 if (!--inflight)
1420 break;
1421 }
1422 }
1423 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1424
1425 if (waitqueue_active(&ctx->inflight_wait))
1426 wake_up(&ctx->inflight_wait);
1427 }
1428do_free:
1429 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001430 if (fixed_refs)
1431 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001432 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001433 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001434}
1435
Jackie Liua197f662019-11-08 08:09:12 -07001436static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001437{
Jackie Liua197f662019-11-08 08:09:12 -07001438 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001439 int ret;
1440
Jens Axboe2d283902019-12-04 11:08:05 -07001441 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001442 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001443 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001444 io_commit_cqring(ctx);
1445 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001446 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001447 return true;
1448 }
1449
1450 return false;
1451}
1452
Jens Axboeba816ad2019-09-28 11:36:45 -06001453static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001454{
Jens Axboe2665abf2019-11-05 12:40:47 -07001455 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001456 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001457
Jens Axboe4d7dd462019-11-20 13:03:52 -07001458 /* Already got next link */
1459 if (req->flags & REQ_F_LINK_NEXT)
1460 return;
1461
Jens Axboe9e645e112019-05-10 16:07:28 -06001462 /*
1463 * The list should never be empty when we are called here. But could
1464 * potentially happen if the chain is messed up, check to be on the
1465 * safe side.
1466 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001467 while (!list_empty(&req->link_list)) {
1468 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1469 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001470
Pavel Begunkov44932332019-12-05 16:16:35 +03001471 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1472 (nxt->flags & REQ_F_TIMEOUT))) {
1473 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001474 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001475 req->flags &= ~REQ_F_LINK_TIMEOUT;
1476 continue;
1477 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001478
Pavel Begunkov44932332019-12-05 16:16:35 +03001479 list_del_init(&req->link_list);
1480 if (!list_empty(&nxt->link_list))
1481 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001482 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001483 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001484 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001485
Jens Axboe4d7dd462019-11-20 13:03:52 -07001486 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001487 if (wake_ev)
1488 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001489}
1490
1491/*
1492 * Called if REQ_F_LINK is set, and we fail the head request
1493 */
1494static void io_fail_links(struct io_kiocb *req)
1495{
Jens Axboe2665abf2019-11-05 12:40:47 -07001496 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001497 unsigned long flags;
1498
1499 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001500
1501 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001502 struct io_kiocb *link = list_first_entry(&req->link_list,
1503 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001504
Pavel Begunkov44932332019-12-05 16:16:35 +03001505 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001506 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001507
1508 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001509 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001510 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001511 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001512 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001513 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001514 }
Jens Axboe5d960722019-11-19 15:31:28 -07001515 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001516 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001517
1518 io_commit_cqring(ctx);
1519 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1520 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001521}
1522
Jens Axboe4d7dd462019-11-20 13:03:52 -07001523static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001524{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001525 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001526 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001527
Jens Axboe9e645e112019-05-10 16:07:28 -06001528 /*
1529 * If LINK is set, we have dependent requests in this chain. If we
1530 * didn't fail this request, queue the first one up, moving any other
1531 * dependencies to the next request. In case of failure, fail the rest
1532 * of the chain.
1533 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001534 if (req->flags & REQ_F_FAIL_LINK) {
1535 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001536 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1537 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001538 struct io_ring_ctx *ctx = req->ctx;
1539 unsigned long flags;
1540
1541 /*
1542 * If this is a timeout link, we could be racing with the
1543 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001544 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001545 */
1546 spin_lock_irqsave(&ctx->completion_lock, flags);
1547 io_req_link_next(req, nxt);
1548 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1549 } else {
1550 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001551 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001552}
Jens Axboe9e645e112019-05-10 16:07:28 -06001553
Jackie Liuc69f8db2019-11-09 11:00:08 +08001554static void io_free_req(struct io_kiocb *req)
1555{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001556 struct io_kiocb *nxt = NULL;
1557
1558 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001559 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001560
1561 if (nxt)
1562 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001563}
1564
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001565static void io_link_work_cb(struct io_wq_work **workptr)
1566{
1567 struct io_wq_work *work = *workptr;
1568 struct io_kiocb *link = work->data;
1569
1570 io_queue_linked_timeout(link);
1571 io_wq_submit_work(workptr);
1572}
1573
1574static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1575{
1576 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001577 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1578
1579 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1580 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001581
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);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001731 if (ctx->flags & IORING_SETUP_SQPOLL)
1732 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001733 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001734}
1735
1736static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1737 long min)
1738{
1739 struct io_kiocb *req, *tmp;
1740 LIST_HEAD(done);
1741 bool spin;
1742 int ret;
1743
1744 /*
1745 * Only spin for completions if we don't have multiple devices hanging
1746 * off our complete list, and we're under the requested amount.
1747 */
1748 spin = !ctx->poll_multi_file && *nr_events < min;
1749
1750 ret = 0;
1751 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001752 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001753
1754 /*
1755 * Move completed entries to our local list. If we find a
1756 * request that requires polling, break out and complete
1757 * the done list first, if we have entries there.
1758 */
1759 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1760 list_move_tail(&req->list, &done);
1761 continue;
1762 }
1763 if (!list_empty(&done))
1764 break;
1765
1766 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1767 if (ret < 0)
1768 break;
1769
1770 if (ret && spin)
1771 spin = false;
1772 ret = 0;
1773 }
1774
1775 if (!list_empty(&done))
1776 io_iopoll_complete(ctx, nr_events, &done);
1777
1778 return ret;
1779}
1780
1781/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001782 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001783 * non-spinning poll check - we'll still enter the driver poll loop, but only
1784 * as a non-spinning completion check.
1785 */
1786static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1787 long min)
1788{
Jens Axboe08f54392019-08-21 22:19:11 -06001789 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001790 int ret;
1791
1792 ret = io_do_iopoll(ctx, nr_events, min);
1793 if (ret < 0)
1794 return ret;
1795 if (!min || *nr_events >= min)
1796 return 0;
1797 }
1798
1799 return 1;
1800}
1801
1802/*
1803 * We can't just wait for polled events to come to us, we have to actively
1804 * find and complete them.
1805 */
1806static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1807{
1808 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1809 return;
1810
1811 mutex_lock(&ctx->uring_lock);
1812 while (!list_empty(&ctx->poll_list)) {
1813 unsigned int nr_events = 0;
1814
1815 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001816
1817 /*
1818 * Ensure we allow local-to-the-cpu processing to take place,
1819 * in this case we need to ensure that we reap all events.
1820 */
1821 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001822 }
1823 mutex_unlock(&ctx->uring_lock);
1824}
1825
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001826static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1827 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001828{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001829 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001830
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001831 /*
1832 * We disallow the app entering submit/complete with polling, but we
1833 * still need to lock the ring to prevent racing with polled issue
1834 * that got punted to a workqueue.
1835 */
1836 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001837 do {
1838 int tmin = 0;
1839
Jens Axboe500f9fb2019-08-19 12:15:59 -06001840 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001841 * Don't enter poll loop if we already have events pending.
1842 * If we do, we can potentially be spinning for commands that
1843 * already triggered a CQE (eg in error).
1844 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001845 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001846 break;
1847
1848 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001849 * If a submit got punted to a workqueue, we can have the
1850 * application entering polling for a command before it gets
1851 * issued. That app will hold the uring_lock for the duration
1852 * of the poll right here, so we need to take a breather every
1853 * now and then to ensure that the issue has a chance to add
1854 * the poll to the issued list. Otherwise we can spin here
1855 * forever, while the workqueue is stuck trying to acquire the
1856 * very same mutex.
1857 */
1858 if (!(++iters & 7)) {
1859 mutex_unlock(&ctx->uring_lock);
1860 mutex_lock(&ctx->uring_lock);
1861 }
1862
Jens Axboedef596e2019-01-09 08:59:42 -07001863 if (*nr_events < min)
1864 tmin = min - *nr_events;
1865
1866 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1867 if (ret <= 0)
1868 break;
1869 ret = 0;
1870 } while (min && !*nr_events && !need_resched());
1871
Jens Axboe500f9fb2019-08-19 12:15:59 -06001872 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001873 return ret;
1874}
1875
Jens Axboe491381ce2019-10-17 09:20:46 -06001876static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001877{
Jens Axboe491381ce2019-10-17 09:20:46 -06001878 /*
1879 * Tell lockdep we inherited freeze protection from submission
1880 * thread.
1881 */
1882 if (req->flags & REQ_F_ISREG) {
1883 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001884
Jens Axboe491381ce2019-10-17 09:20:46 -06001885 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001886 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001887 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001888}
1889
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001890static inline void req_set_fail_links(struct io_kiocb *req)
1891{
1892 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1893 req->flags |= REQ_F_FAIL_LINK;
1894}
1895
Jens Axboeba816ad2019-09-28 11:36:45 -06001896static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001897{
Jens Axboe9adbd452019-12-20 08:45:55 -07001898 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001899 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001900
Jens Axboe491381ce2019-10-17 09:20:46 -06001901 if (kiocb->ki_flags & IOCB_WRITE)
1902 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001904 if (res != req->result)
1905 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001906 if (req->flags & REQ_F_BUFFER_SELECTED)
1907 cflags = io_put_kbuf(req);
1908 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001909}
1910
1911static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1912{
Jens Axboe9adbd452019-12-20 08:45:55 -07001913 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001914
1915 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001916 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001917}
1918
Jens Axboedef596e2019-01-09 08:59:42 -07001919static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1920{
Jens Axboe9adbd452019-12-20 08:45:55 -07001921 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001922
Jens Axboe491381ce2019-10-17 09:20:46 -06001923 if (kiocb->ki_flags & IOCB_WRITE)
1924 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001925
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001926 if (res != req->result)
1927 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001928 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001929 if (res != -EAGAIN)
1930 req->flags |= REQ_F_IOPOLL_COMPLETED;
1931}
1932
1933/*
1934 * After the iocb has been issued, it's safe to be found on the poll list.
1935 * Adding the kiocb to the list AFTER submission ensures that we don't
1936 * find it from a io_iopoll_getevents() thread before the issuer is done
1937 * accessing the kiocb cookie.
1938 */
1939static void io_iopoll_req_issued(struct io_kiocb *req)
1940{
1941 struct io_ring_ctx *ctx = req->ctx;
1942
1943 /*
1944 * Track whether we have multiple files in our lists. This will impact
1945 * how we do polling eventually, not spinning if we're on potentially
1946 * different devices.
1947 */
1948 if (list_empty(&ctx->poll_list)) {
1949 ctx->poll_multi_file = false;
1950 } else if (!ctx->poll_multi_file) {
1951 struct io_kiocb *list_req;
1952
1953 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1954 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001955 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001956 ctx->poll_multi_file = true;
1957 }
1958
1959 /*
1960 * For fast devices, IO may have already completed. If it has, add
1961 * it to the front so we find it first.
1962 */
1963 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1964 list_add(&req->list, &ctx->poll_list);
1965 else
1966 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001967
1968 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1969 wq_has_sleeper(&ctx->sqo_wait))
1970 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001971}
1972
Jens Axboe3d6770f2019-04-13 11:50:54 -06001973static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001974{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001975 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001976 int diff = state->has_refs - state->used_refs;
1977
1978 if (diff)
1979 fput_many(state->file, diff);
1980 state->file = NULL;
1981 }
1982}
1983
1984/*
1985 * Get as many references to a file as we have IOs left in this submission,
1986 * assuming most submissions are for one file, or at least that each file
1987 * has more than one submission.
1988 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001989static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001990{
1991 if (!state)
1992 return fget(fd);
1993
1994 if (state->file) {
1995 if (state->fd == fd) {
1996 state->used_refs++;
1997 state->ios_left--;
1998 return state->file;
1999 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06002000 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002001 }
2002 state->file = fget_many(fd, state->ios_left);
2003 if (!state->file)
2004 return NULL;
2005
2006 state->fd = fd;
2007 state->has_refs = state->ios_left;
2008 state->used_refs = 1;
2009 state->ios_left--;
2010 return state->file;
2011}
2012
Jens Axboe2b188cc2019-01-07 10:46:33 -07002013/*
2014 * If we tracked the file through the SCM inflight mechanism, we could support
2015 * any file. For now, just ensure that anything potentially problematic is done
2016 * inline.
2017 */
2018static bool io_file_supports_async(struct file *file)
2019{
2020 umode_t mode = file_inode(file)->i_mode;
2021
Jens Axboe10d59342019-12-09 20:16:22 -07002022 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002023 return true;
2024 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2025 return true;
2026
2027 return false;
2028}
2029
Jens Axboe3529d8c2019-12-19 18:24:38 -07002030static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2031 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002032{
Jens Axboedef596e2019-01-09 08:59:42 -07002033 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002034 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002035 unsigned ioprio;
2036 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002037
Jens Axboe491381ce2019-10-17 09:20:46 -06002038 if (S_ISREG(file_inode(req->file)->i_mode))
2039 req->flags |= REQ_F_ISREG;
2040
Jens Axboe2b188cc2019-01-07 10:46:33 -07002041 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002042 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2043 req->flags |= REQ_F_CUR_POS;
2044 kiocb->ki_pos = req->file->f_pos;
2045 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002046 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002047 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2048 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2049 if (unlikely(ret))
2050 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002051
2052 ioprio = READ_ONCE(sqe->ioprio);
2053 if (ioprio) {
2054 ret = ioprio_check_cap(ioprio);
2055 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002056 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002057
2058 kiocb->ki_ioprio = ioprio;
2059 } else
2060 kiocb->ki_ioprio = get_current_ioprio();
2061
Stefan Bühler8449eed2019-04-27 20:34:19 +02002062 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002063 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2064 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002065 req->flags |= REQ_F_NOWAIT;
2066
2067 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002068 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002069
Jens Axboedef596e2019-01-09 08:59:42 -07002070 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002071 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2072 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002073 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002074
Jens Axboedef596e2019-01-09 08:59:42 -07002075 kiocb->ki_flags |= IOCB_HIPRI;
2076 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002077 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002078 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002079 if (kiocb->ki_flags & IOCB_HIPRI)
2080 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002081 kiocb->ki_complete = io_complete_rw;
2082 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002083
Jens Axboe3529d8c2019-12-19 18:24:38 -07002084 req->rw.addr = READ_ONCE(sqe->addr);
2085 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002086 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002087 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002088 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002089 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002090}
2091
2092static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2093{
2094 switch (ret) {
2095 case -EIOCBQUEUED:
2096 break;
2097 case -ERESTARTSYS:
2098 case -ERESTARTNOINTR:
2099 case -ERESTARTNOHAND:
2100 case -ERESTART_RESTARTBLOCK:
2101 /*
2102 * We can't just restart the syscall, since previously
2103 * submitted sqes may already be in progress. Just fail this
2104 * IO with EINTR.
2105 */
2106 ret = -EINTR;
2107 /* fall through */
2108 default:
2109 kiocb->ki_complete(kiocb, ret, 0);
2110 }
2111}
2112
Pavel Begunkov014db002020-03-03 21:33:12 +03002113static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002114{
Jens Axboeba042912019-12-25 16:33:42 -07002115 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2116
2117 if (req->flags & REQ_F_CUR_POS)
2118 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002119 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002120 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002121 else
2122 io_rw_done(kiocb, ret);
2123}
2124
Jens Axboe9adbd452019-12-20 08:45:55 -07002125static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002126 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002127{
Jens Axboe9adbd452019-12-20 08:45:55 -07002128 struct io_ring_ctx *ctx = req->ctx;
2129 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002130 struct io_mapped_ubuf *imu;
2131 unsigned index, buf_index;
2132 size_t offset;
2133 u64 buf_addr;
2134
2135 /* attempt to use fixed buffers without having provided iovecs */
2136 if (unlikely(!ctx->user_bufs))
2137 return -EFAULT;
2138
Jens Axboe9adbd452019-12-20 08:45:55 -07002139 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002140 if (unlikely(buf_index >= ctx->nr_user_bufs))
2141 return -EFAULT;
2142
2143 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2144 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002145 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002146
2147 /* overflow */
2148 if (buf_addr + len < buf_addr)
2149 return -EFAULT;
2150 /* not inside the mapped region */
2151 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2152 return -EFAULT;
2153
2154 /*
2155 * May not be a start of buffer, set size appropriately
2156 * and advance us to the beginning.
2157 */
2158 offset = buf_addr - imu->ubuf;
2159 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002160
2161 if (offset) {
2162 /*
2163 * Don't use iov_iter_advance() here, as it's really slow for
2164 * using the latter parts of a big fixed buffer - it iterates
2165 * over each segment manually. We can cheat a bit here, because
2166 * we know that:
2167 *
2168 * 1) it's a BVEC iter, we set it up
2169 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2170 * first and last bvec
2171 *
2172 * So just find our index, and adjust the iterator afterwards.
2173 * If the offset is within the first bvec (or the whole first
2174 * bvec, just use iov_iter_advance(). This makes it easier
2175 * since we can just skip the first segment, which may not
2176 * be PAGE_SIZE aligned.
2177 */
2178 const struct bio_vec *bvec = imu->bvec;
2179
2180 if (offset <= bvec->bv_len) {
2181 iov_iter_advance(iter, offset);
2182 } else {
2183 unsigned long seg_skip;
2184
2185 /* skip first vec */
2186 offset -= bvec->bv_len;
2187 seg_skip = 1 + (offset >> PAGE_SHIFT);
2188
2189 iter->bvec = bvec + seg_skip;
2190 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002191 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002192 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002193 }
2194 }
2195
Jens Axboe5e559562019-11-13 16:12:46 -07002196 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002197}
2198
Jens Axboebcda7ba2020-02-23 16:42:51 -07002199static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2200{
2201 if (needs_lock)
2202 mutex_unlock(&ctx->uring_lock);
2203}
2204
2205static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2206{
2207 /*
2208 * "Normal" inline submissions always hold the uring_lock, since we
2209 * grab it from the system call. Same is true for the SQPOLL offload.
2210 * The only exception is when we've detached the request and issue it
2211 * from an async worker thread, grab the lock for that case.
2212 */
2213 if (needs_lock)
2214 mutex_lock(&ctx->uring_lock);
2215}
2216
2217static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2218 int bgid, struct io_buffer *kbuf,
2219 bool needs_lock)
2220{
2221 struct io_buffer *head;
2222
2223 if (req->flags & REQ_F_BUFFER_SELECTED)
2224 return kbuf;
2225
2226 io_ring_submit_lock(req->ctx, needs_lock);
2227
2228 lockdep_assert_held(&req->ctx->uring_lock);
2229
2230 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2231 if (head) {
2232 if (!list_empty(&head->list)) {
2233 kbuf = list_last_entry(&head->list, struct io_buffer,
2234 list);
2235 list_del(&kbuf->list);
2236 } else {
2237 kbuf = head;
2238 idr_remove(&req->ctx->io_buffer_idr, bgid);
2239 }
2240 if (*len > kbuf->len)
2241 *len = kbuf->len;
2242 } else {
2243 kbuf = ERR_PTR(-ENOBUFS);
2244 }
2245
2246 io_ring_submit_unlock(req->ctx, needs_lock);
2247
2248 return kbuf;
2249}
2250
Jens Axboe4d954c22020-02-27 07:31:19 -07002251static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2252 bool needs_lock)
2253{
2254 struct io_buffer *kbuf;
2255 int bgid;
2256
2257 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2258 bgid = (int) (unsigned long) req->rw.kiocb.private;
2259 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2260 if (IS_ERR(kbuf))
2261 return kbuf;
2262 req->rw.addr = (u64) (unsigned long) kbuf;
2263 req->flags |= REQ_F_BUFFER_SELECTED;
2264 return u64_to_user_ptr(kbuf->addr);
2265}
2266
2267#ifdef CONFIG_COMPAT
2268static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2269 bool needs_lock)
2270{
2271 struct compat_iovec __user *uiov;
2272 compat_ssize_t clen;
2273 void __user *buf;
2274 ssize_t len;
2275
2276 uiov = u64_to_user_ptr(req->rw.addr);
2277 if (!access_ok(uiov, sizeof(*uiov)))
2278 return -EFAULT;
2279 if (__get_user(clen, &uiov->iov_len))
2280 return -EFAULT;
2281 if (clen < 0)
2282 return -EINVAL;
2283
2284 len = clen;
2285 buf = io_rw_buffer_select(req, &len, needs_lock);
2286 if (IS_ERR(buf))
2287 return PTR_ERR(buf);
2288 iov[0].iov_base = buf;
2289 iov[0].iov_len = (compat_size_t) len;
2290 return 0;
2291}
2292#endif
2293
2294static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2295 bool needs_lock)
2296{
2297 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2298 void __user *buf;
2299 ssize_t len;
2300
2301 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2302 return -EFAULT;
2303
2304 len = iov[0].iov_len;
2305 if (len < 0)
2306 return -EINVAL;
2307 buf = io_rw_buffer_select(req, &len, needs_lock);
2308 if (IS_ERR(buf))
2309 return PTR_ERR(buf);
2310 iov[0].iov_base = buf;
2311 iov[0].iov_len = len;
2312 return 0;
2313}
2314
2315static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2316 bool needs_lock)
2317{
2318 if (req->flags & REQ_F_BUFFER_SELECTED)
2319 return 0;
2320 if (!req->rw.len)
2321 return 0;
2322 else if (req->rw.len > 1)
2323 return -EINVAL;
2324
2325#ifdef CONFIG_COMPAT
2326 if (req->ctx->compat)
2327 return io_compat_import(req, iov, needs_lock);
2328#endif
2329
2330 return __io_iov_buffer_select(req, iov, needs_lock);
2331}
2332
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002333static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002334 struct iovec **iovec, struct iov_iter *iter,
2335 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002336{
Jens Axboe9adbd452019-12-20 08:45:55 -07002337 void __user *buf = u64_to_user_ptr(req->rw.addr);
2338 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002339 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002340 u8 opcode;
2341
Jens Axboed625c6e2019-12-17 19:53:05 -07002342 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002343 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002344 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002345 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002346 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002347
Jens Axboebcda7ba2020-02-23 16:42:51 -07002348 /* buffer index only valid with fixed read/write, or buffer select */
2349 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002350 return -EINVAL;
2351
Jens Axboe3a6820f2019-12-22 15:19:35 -07002352 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002353 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002354 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2355 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002356 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002357 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002358 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002359 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002360 }
2361
Jens Axboe3a6820f2019-12-22 15:19:35 -07002362 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2363 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002364 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002365 }
2366
Jens Axboef67676d2019-12-02 11:03:47 -07002367 if (req->io) {
2368 struct io_async_rw *iorw = &req->io->rw;
2369
2370 *iovec = iorw->iov;
2371 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2372 if (iorw->iov == iorw->fast_iov)
2373 *iovec = NULL;
2374 return iorw->size;
2375 }
2376
Jens Axboe4d954c22020-02-27 07:31:19 -07002377 if (req->flags & REQ_F_BUFFER_SELECT) {
2378 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002379 if (!ret) {
2380 ret = (*iovec)->iov_len;
2381 iov_iter_init(iter, rw, *iovec, 1, ret);
2382 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002383 *iovec = NULL;
2384 return ret;
2385 }
2386
Jens Axboe2b188cc2019-01-07 10:46:33 -07002387#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002388 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002389 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2390 iovec, iter);
2391#endif
2392
2393 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2394}
2395
Jens Axboe32960612019-09-23 11:05:34 -06002396/*
2397 * For files that don't have ->read_iter() and ->write_iter(), handle them
2398 * by looping over ->read() or ->write() manually.
2399 */
2400static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2401 struct iov_iter *iter)
2402{
2403 ssize_t ret = 0;
2404
2405 /*
2406 * Don't support polled IO through this interface, and we can't
2407 * support non-blocking either. For the latter, this just causes
2408 * the kiocb to be handled from an async context.
2409 */
2410 if (kiocb->ki_flags & IOCB_HIPRI)
2411 return -EOPNOTSUPP;
2412 if (kiocb->ki_flags & IOCB_NOWAIT)
2413 return -EAGAIN;
2414
2415 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002416 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002417 ssize_t nr;
2418
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002419 if (!iov_iter_is_bvec(iter)) {
2420 iovec = iov_iter_iovec(iter);
2421 } else {
2422 /* fixed buffers import bvec */
2423 iovec.iov_base = kmap(iter->bvec->bv_page)
2424 + iter->iov_offset;
2425 iovec.iov_len = min(iter->count,
2426 iter->bvec->bv_len - iter->iov_offset);
2427 }
2428
Jens Axboe32960612019-09-23 11:05:34 -06002429 if (rw == READ) {
2430 nr = file->f_op->read(file, iovec.iov_base,
2431 iovec.iov_len, &kiocb->ki_pos);
2432 } else {
2433 nr = file->f_op->write(file, iovec.iov_base,
2434 iovec.iov_len, &kiocb->ki_pos);
2435 }
2436
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002437 if (iov_iter_is_bvec(iter))
2438 kunmap(iter->bvec->bv_page);
2439
Jens Axboe32960612019-09-23 11:05:34 -06002440 if (nr < 0) {
2441 if (!ret)
2442 ret = nr;
2443 break;
2444 }
2445 ret += nr;
2446 if (nr != iovec.iov_len)
2447 break;
2448 iov_iter_advance(iter, nr);
2449 }
2450
2451 return ret;
2452}
2453
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002454static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002455 struct iovec *iovec, struct iovec *fast_iov,
2456 struct iov_iter *iter)
2457{
2458 req->io->rw.nr_segs = iter->nr_segs;
2459 req->io->rw.size = io_size;
2460 req->io->rw.iov = iovec;
2461 if (!req->io->rw.iov) {
2462 req->io->rw.iov = req->io->rw.fast_iov;
2463 memcpy(req->io->rw.iov, fast_iov,
2464 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002465 } else {
2466 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002467 }
2468}
2469
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002470static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002471{
Jens Axboed3656342019-12-18 09:50:26 -07002472 if (!io_op_defs[req->opcode].async_ctx)
2473 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002474 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002475 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002476}
2477
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002478static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2479 struct iovec *iovec, struct iovec *fast_iov,
2480 struct iov_iter *iter)
2481{
Jens Axboe980ad262020-01-24 23:08:54 -07002482 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002483 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002484 if (!req->io) {
2485 if (io_alloc_async_ctx(req))
2486 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002487
Jens Axboe5d204bc2020-01-31 12:06:52 -07002488 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2489 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002490 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002491}
2492
Jens Axboe3529d8c2019-12-19 18:24:38 -07002493static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2494 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002495{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002496 struct io_async_ctx *io;
2497 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002498 ssize_t ret;
2499
Jens Axboe3529d8c2019-12-19 18:24:38 -07002500 ret = io_prep_rw(req, sqe, force_nonblock);
2501 if (ret)
2502 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002503
Jens Axboe3529d8c2019-12-19 18:24:38 -07002504 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2505 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002506
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002507 /* either don't need iovec imported or already have it */
2508 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002509 return 0;
2510
2511 io = req->io;
2512 io->rw.iov = io->rw.fast_iov;
2513 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002514 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002515 req->io = io;
2516 if (ret < 0)
2517 return ret;
2518
2519 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2520 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002521}
2522
Pavel Begunkov014db002020-03-03 21:33:12 +03002523static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002524{
2525 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002526 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002527 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002528 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002529 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002530
Jens Axboebcda7ba2020-02-23 16:42:51 -07002531 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002532 if (ret < 0)
2533 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002534
Jens Axboefd6c2e42019-12-18 12:19:41 -07002535 /* Ensure we clear previously set non-block flag */
2536 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002537 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002538
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002539 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002540 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002541 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002542 req->result = io_size;
2543
2544 /*
2545 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2546 * we know to async punt it even if it was opened O_NONBLOCK
2547 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002548 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002549 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002550
Jens Axboe31b51512019-01-18 22:56:34 -07002551 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002552 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002553 if (!ret) {
2554 ssize_t ret2;
2555
Jens Axboe9adbd452019-12-20 08:45:55 -07002556 if (req->file->f_op->read_iter)
2557 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002558 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002559 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002560
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002561 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002562 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002563 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002564 } else {
2565copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002566 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002567 inline_vecs, &iter);
2568 if (ret)
2569 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002570 /* any defer here is final, must blocking retry */
2571 if (!(req->flags & REQ_F_NOWAIT))
2572 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002573 return -EAGAIN;
2574 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002575 }
Jens Axboef67676d2019-12-02 11:03:47 -07002576out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002577 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002578 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002579 return ret;
2580}
2581
Jens Axboe3529d8c2019-12-19 18:24:38 -07002582static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2583 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002584{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002585 struct io_async_ctx *io;
2586 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002587 ssize_t ret;
2588
Jens Axboe3529d8c2019-12-19 18:24:38 -07002589 ret = io_prep_rw(req, sqe, force_nonblock);
2590 if (ret)
2591 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002592
Jens Axboe3529d8c2019-12-19 18:24:38 -07002593 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2594 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002595
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002596 /* either don't need iovec imported or already have it */
2597 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002598 return 0;
2599
2600 io = req->io;
2601 io->rw.iov = io->rw.fast_iov;
2602 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002603 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002604 req->io = io;
2605 if (ret < 0)
2606 return ret;
2607
2608 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2609 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002610}
2611
Pavel Begunkov014db002020-03-03 21:33:12 +03002612static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002613{
2614 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002615 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002616 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002617 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002618 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002619
Jens Axboebcda7ba2020-02-23 16:42:51 -07002620 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002621 if (ret < 0)
2622 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002623
Jens Axboefd6c2e42019-12-18 12:19:41 -07002624 /* Ensure we clear previously set non-block flag */
2625 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002626 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002627
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002628 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002629 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002630 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002631 req->result = io_size;
2632
2633 /*
2634 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2635 * we know to async punt it even if it was opened O_NONBLOCK
2636 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002637 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002638 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002639
Jens Axboe10d59342019-12-09 20:16:22 -07002640 /* file path doesn't support NOWAIT for non-direct_IO */
2641 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2642 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002643 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002644
Jens Axboe31b51512019-01-18 22:56:34 -07002645 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002646 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002647 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002648 ssize_t ret2;
2649
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650 /*
2651 * Open-code file_start_write here to grab freeze protection,
2652 * which will be released by another thread in
2653 * io_complete_rw(). Fool lockdep by telling it the lock got
2654 * released so that it doesn't complain about the held lock when
2655 * we return to userspace.
2656 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002657 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002658 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002659 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002660 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002661 SB_FREEZE_WRITE);
2662 }
2663 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002664
Jens Axboe9adbd452019-12-20 08:45:55 -07002665 if (req->file->f_op->write_iter)
2666 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002667 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002668 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002669 /*
2670 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2671 * retry them without IOCB_NOWAIT.
2672 */
2673 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2674 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002675 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002676 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002677 } else {
2678copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002679 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002680 inline_vecs, &iter);
2681 if (ret)
2682 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002683 /* any defer here is final, must blocking retry */
2684 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002685 return -EAGAIN;
2686 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002687 }
Jens Axboe31b51512019-01-18 22:56:34 -07002688out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002689 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002690 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002691 return ret;
2692}
2693
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002694static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2695{
2696 struct io_splice* sp = &req->splice;
2697 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2698 int ret;
2699
2700 if (req->flags & REQ_F_NEED_CLEANUP)
2701 return 0;
2702
2703 sp->file_in = NULL;
2704 sp->off_in = READ_ONCE(sqe->splice_off_in);
2705 sp->off_out = READ_ONCE(sqe->off);
2706 sp->len = READ_ONCE(sqe->len);
2707 sp->flags = READ_ONCE(sqe->splice_flags);
2708
2709 if (unlikely(sp->flags & ~valid_flags))
2710 return -EINVAL;
2711
2712 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2713 (sp->flags & SPLICE_F_FD_IN_FIXED));
2714 if (ret)
2715 return ret;
2716 req->flags |= REQ_F_NEED_CLEANUP;
2717
2718 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2719 req->work.flags |= IO_WQ_WORK_UNBOUND;
2720
2721 return 0;
2722}
2723
2724static bool io_splice_punt(struct file *file)
2725{
2726 if (get_pipe_info(file))
2727 return false;
2728 if (!io_file_supports_async(file))
2729 return true;
2730 return !(file->f_mode & O_NONBLOCK);
2731}
2732
Pavel Begunkov014db002020-03-03 21:33:12 +03002733static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002734{
2735 struct io_splice *sp = &req->splice;
2736 struct file *in = sp->file_in;
2737 struct file *out = sp->file_out;
2738 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2739 loff_t *poff_in, *poff_out;
2740 long ret;
2741
2742 if (force_nonblock) {
2743 if (io_splice_punt(in) || io_splice_punt(out))
2744 return -EAGAIN;
2745 flags |= SPLICE_F_NONBLOCK;
2746 }
2747
2748 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2749 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2750 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2751 if (force_nonblock && ret == -EAGAIN)
2752 return -EAGAIN;
2753
2754 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2755 req->flags &= ~REQ_F_NEED_CLEANUP;
2756
2757 io_cqring_add_event(req, ret);
2758 if (ret != sp->len)
2759 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002760 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002761 return 0;
2762}
2763
Jens Axboe2b188cc2019-01-07 10:46:33 -07002764/*
2765 * IORING_OP_NOP just posts a completion event, nothing else.
2766 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002767static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002768{
2769 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002770
Jens Axboedef596e2019-01-09 08:59:42 -07002771 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2772 return -EINVAL;
2773
Jens Axboe78e19bb2019-11-06 15:21:34 -07002774 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002775 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002776 return 0;
2777}
2778
Jens Axboe3529d8c2019-12-19 18:24:38 -07002779static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002780{
Jens Axboe6b063142019-01-10 22:13:58 -07002781 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002782
Jens Axboe09bb8392019-03-13 12:39:28 -06002783 if (!req->file)
2784 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002785
Jens Axboe6b063142019-01-10 22:13:58 -07002786 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002787 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002788 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002789 return -EINVAL;
2790
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002791 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2792 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2793 return -EINVAL;
2794
2795 req->sync.off = READ_ONCE(sqe->off);
2796 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002797 return 0;
2798}
2799
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002800static bool io_req_cancelled(struct io_kiocb *req)
2801{
2802 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2803 req_set_fail_links(req);
2804 io_cqring_add_event(req, -ECANCELED);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002805 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002806 return true;
2807 }
2808
2809 return false;
2810}
2811
Pavel Begunkov014db002020-03-03 21:33:12 +03002812static void __io_fsync(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002813{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002814 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002815 int ret;
2816
Jens Axboe9adbd452019-12-20 08:45:55 -07002817 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002818 end > 0 ? end : LLONG_MAX,
2819 req->sync.flags & IORING_FSYNC_DATASYNC);
2820 if (ret < 0)
2821 req_set_fail_links(req);
2822 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002823 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002824}
2825
2826static void io_fsync_finish(struct io_wq_work **workptr)
2827{
2828 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002829
2830 if (io_req_cancelled(req))
2831 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002832 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002833 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002834}
2835
Pavel Begunkov014db002020-03-03 21:33:12 +03002836static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002837{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002838 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002839 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002840 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002841 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002842 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002843 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002844 return 0;
2845}
2846
Pavel Begunkov014db002020-03-03 21:33:12 +03002847static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002848{
Jens Axboed63d1b52019-12-10 10:38:56 -07002849 int ret;
2850
2851 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2852 req->sync.len);
2853 if (ret < 0)
2854 req_set_fail_links(req);
2855 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002856 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002857}
2858
2859static void io_fallocate_finish(struct io_wq_work **workptr)
2860{
2861 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002862
Pavel Begunkov594506f2020-03-03 21:33:11 +03002863 if (io_req_cancelled(req))
2864 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002865 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002866 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002867}
2868
2869static int io_fallocate_prep(struct io_kiocb *req,
2870 const struct io_uring_sqe *sqe)
2871{
2872 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2873 return -EINVAL;
2874
2875 req->sync.off = READ_ONCE(sqe->off);
2876 req->sync.len = READ_ONCE(sqe->addr);
2877 req->sync.mode = READ_ONCE(sqe->len);
2878 return 0;
2879}
2880
Pavel Begunkov014db002020-03-03 21:33:12 +03002881static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002882{
Jens Axboed63d1b52019-12-10 10:38:56 -07002883 /* fallocate always requiring blocking context */
2884 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002885 req->work.func = io_fallocate_finish;
2886 return -EAGAIN;
2887 }
2888
Pavel Begunkov014db002020-03-03 21:33:12 +03002889 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002890 return 0;
2891}
2892
Jens Axboe15b71ab2019-12-11 11:20:36 -07002893static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2894{
Jens Axboef8748882020-01-08 17:47:02 -07002895 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002896 int ret;
2897
2898 if (sqe->ioprio || sqe->buf_index)
2899 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002900 if (sqe->flags & IOSQE_FIXED_FILE)
2901 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002902 if (req->flags & REQ_F_NEED_CLEANUP)
2903 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002904
2905 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002906 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002907 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002908 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002909
Jens Axboef8748882020-01-08 17:47:02 -07002910 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002911 if (IS_ERR(req->open.filename)) {
2912 ret = PTR_ERR(req->open.filename);
2913 req->open.filename = NULL;
2914 return ret;
2915 }
2916
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002917 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002918 return 0;
2919}
2920
Jens Axboecebdb982020-01-08 17:59:24 -07002921static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2922{
2923 struct open_how __user *how;
2924 const char __user *fname;
2925 size_t len;
2926 int ret;
2927
2928 if (sqe->ioprio || sqe->buf_index)
2929 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002930 if (sqe->flags & IOSQE_FIXED_FILE)
2931 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002932 if (req->flags & REQ_F_NEED_CLEANUP)
2933 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002934
2935 req->open.dfd = READ_ONCE(sqe->fd);
2936 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2937 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2938 len = READ_ONCE(sqe->len);
2939
2940 if (len < OPEN_HOW_SIZE_VER0)
2941 return -EINVAL;
2942
2943 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2944 len);
2945 if (ret)
2946 return ret;
2947
2948 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2949 req->open.how.flags |= O_LARGEFILE;
2950
2951 req->open.filename = getname(fname);
2952 if (IS_ERR(req->open.filename)) {
2953 ret = PTR_ERR(req->open.filename);
2954 req->open.filename = NULL;
2955 return ret;
2956 }
2957
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002958 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002959 return 0;
2960}
2961
Pavel Begunkov014db002020-03-03 21:33:12 +03002962static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002963{
2964 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002965 struct file *file;
2966 int ret;
2967
Jens Axboef86cd202020-01-29 13:46:44 -07002968 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002969 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002970
Jens Axboecebdb982020-01-08 17:59:24 -07002971 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002972 if (ret)
2973 goto err;
2974
Jens Axboecebdb982020-01-08 17:59:24 -07002975 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002976 if (ret < 0)
2977 goto err;
2978
2979 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2980 if (IS_ERR(file)) {
2981 put_unused_fd(ret);
2982 ret = PTR_ERR(file);
2983 } else {
2984 fsnotify_open(file);
2985 fd_install(ret, file);
2986 }
2987err:
2988 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002989 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002990 if (ret < 0)
2991 req_set_fail_links(req);
2992 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002993 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002994 return 0;
2995}
2996
Pavel Begunkov014db002020-03-03 21:33:12 +03002997static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07002998{
2999 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03003000 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003001}
3002
Jens Axboe067524e2020-03-02 16:32:28 -07003003static int io_remove_buffers_prep(struct io_kiocb *req,
3004 const struct io_uring_sqe *sqe)
3005{
3006 struct io_provide_buf *p = &req->pbuf;
3007 u64 tmp;
3008
3009 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3010 return -EINVAL;
3011
3012 tmp = READ_ONCE(sqe->fd);
3013 if (!tmp || tmp > USHRT_MAX)
3014 return -EINVAL;
3015
3016 memset(p, 0, sizeof(*p));
3017 p->nbufs = tmp;
3018 p->bgid = READ_ONCE(sqe->buf_group);
3019 return 0;
3020}
3021
3022static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3023 int bgid, unsigned nbufs)
3024{
3025 unsigned i = 0;
3026
3027 /* shouldn't happen */
3028 if (!nbufs)
3029 return 0;
3030
3031 /* the head kbuf is the list itself */
3032 while (!list_empty(&buf->list)) {
3033 struct io_buffer *nxt;
3034
3035 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3036 list_del(&nxt->list);
3037 kfree(nxt);
3038 if (++i == nbufs)
3039 return i;
3040 }
3041 i++;
3042 kfree(buf);
3043 idr_remove(&ctx->io_buffer_idr, bgid);
3044
3045 return i;
3046}
3047
3048static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3049{
3050 struct io_provide_buf *p = &req->pbuf;
3051 struct io_ring_ctx *ctx = req->ctx;
3052 struct io_buffer *head;
3053 int ret = 0;
3054
3055 io_ring_submit_lock(ctx, !force_nonblock);
3056
3057 lockdep_assert_held(&ctx->uring_lock);
3058
3059 ret = -ENOENT;
3060 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3061 if (head)
3062 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3063
3064 io_ring_submit_lock(ctx, !force_nonblock);
3065 if (ret < 0)
3066 req_set_fail_links(req);
3067 io_cqring_add_event(req, ret);
3068 io_put_req(req);
3069 return 0;
3070}
3071
Jens Axboeddf0322d2020-02-23 16:41:33 -07003072static int io_provide_buffers_prep(struct io_kiocb *req,
3073 const struct io_uring_sqe *sqe)
3074{
3075 struct io_provide_buf *p = &req->pbuf;
3076 u64 tmp;
3077
3078 if (sqe->ioprio || sqe->rw_flags)
3079 return -EINVAL;
3080
3081 tmp = READ_ONCE(sqe->fd);
3082 if (!tmp || tmp > USHRT_MAX)
3083 return -E2BIG;
3084 p->nbufs = tmp;
3085 p->addr = READ_ONCE(sqe->addr);
3086 p->len = READ_ONCE(sqe->len);
3087
3088 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3089 return -EFAULT;
3090
3091 p->bgid = READ_ONCE(sqe->buf_group);
3092 tmp = READ_ONCE(sqe->off);
3093 if (tmp > USHRT_MAX)
3094 return -E2BIG;
3095 p->bid = tmp;
3096 return 0;
3097}
3098
3099static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3100{
3101 struct io_buffer *buf;
3102 u64 addr = pbuf->addr;
3103 int i, bid = pbuf->bid;
3104
3105 for (i = 0; i < pbuf->nbufs; i++) {
3106 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3107 if (!buf)
3108 break;
3109
3110 buf->addr = addr;
3111 buf->len = pbuf->len;
3112 buf->bid = bid;
3113 addr += pbuf->len;
3114 bid++;
3115 if (!*head) {
3116 INIT_LIST_HEAD(&buf->list);
3117 *head = buf;
3118 } else {
3119 list_add_tail(&buf->list, &(*head)->list);
3120 }
3121 }
3122
3123 return i ? i : -ENOMEM;
3124}
3125
Jens Axboeddf0322d2020-02-23 16:41:33 -07003126static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3127{
3128 struct io_provide_buf *p = &req->pbuf;
3129 struct io_ring_ctx *ctx = req->ctx;
3130 struct io_buffer *head, *list;
3131 int ret = 0;
3132
3133 io_ring_submit_lock(ctx, !force_nonblock);
3134
3135 lockdep_assert_held(&ctx->uring_lock);
3136
3137 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3138
3139 ret = io_add_buffers(p, &head);
3140 if (ret < 0)
3141 goto out;
3142
3143 if (!list) {
3144 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3145 GFP_KERNEL);
3146 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003147 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003148 goto out;
3149 }
3150 }
3151out:
3152 io_ring_submit_unlock(ctx, !force_nonblock);
3153 if (ret < 0)
3154 req_set_fail_links(req);
3155 io_cqring_add_event(req, ret);
3156 io_put_req(req);
3157 return 0;
3158}
3159
Jens Axboe3e4827b2020-01-08 15:18:09 -07003160static int io_epoll_ctl_prep(struct io_kiocb *req,
3161 const struct io_uring_sqe *sqe)
3162{
3163#if defined(CONFIG_EPOLL)
3164 if (sqe->ioprio || sqe->buf_index)
3165 return -EINVAL;
3166
3167 req->epoll.epfd = READ_ONCE(sqe->fd);
3168 req->epoll.op = READ_ONCE(sqe->len);
3169 req->epoll.fd = READ_ONCE(sqe->off);
3170
3171 if (ep_op_has_event(req->epoll.op)) {
3172 struct epoll_event __user *ev;
3173
3174 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3175 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3176 return -EFAULT;
3177 }
3178
3179 return 0;
3180#else
3181 return -EOPNOTSUPP;
3182#endif
3183}
3184
Pavel Begunkov014db002020-03-03 21:33:12 +03003185static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003186{
3187#if defined(CONFIG_EPOLL)
3188 struct io_epoll *ie = &req->epoll;
3189 int ret;
3190
3191 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3192 if (force_nonblock && ret == -EAGAIN)
3193 return -EAGAIN;
3194
3195 if (ret < 0)
3196 req_set_fail_links(req);
3197 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003198 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003199 return 0;
3200#else
3201 return -EOPNOTSUPP;
3202#endif
3203}
3204
Jens Axboec1ca7572019-12-25 22:18:28 -07003205static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3206{
3207#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3208 if (sqe->ioprio || sqe->buf_index || sqe->off)
3209 return -EINVAL;
3210
3211 req->madvise.addr = READ_ONCE(sqe->addr);
3212 req->madvise.len = READ_ONCE(sqe->len);
3213 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3214 return 0;
3215#else
3216 return -EOPNOTSUPP;
3217#endif
3218}
3219
Pavel Begunkov014db002020-03-03 21:33:12 +03003220static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003221{
3222#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3223 struct io_madvise *ma = &req->madvise;
3224 int ret;
3225
3226 if (force_nonblock)
3227 return -EAGAIN;
3228
3229 ret = do_madvise(ma->addr, ma->len, ma->advice);
3230 if (ret < 0)
3231 req_set_fail_links(req);
3232 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003233 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003234 return 0;
3235#else
3236 return -EOPNOTSUPP;
3237#endif
3238}
3239
Jens Axboe4840e412019-12-25 22:03:45 -07003240static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3241{
3242 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3243 return -EINVAL;
3244
3245 req->fadvise.offset = READ_ONCE(sqe->off);
3246 req->fadvise.len = READ_ONCE(sqe->len);
3247 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3248 return 0;
3249}
3250
Pavel Begunkov014db002020-03-03 21:33:12 +03003251static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003252{
3253 struct io_fadvise *fa = &req->fadvise;
3254 int ret;
3255
Jens Axboe3e694262020-02-01 09:22:49 -07003256 if (force_nonblock) {
3257 switch (fa->advice) {
3258 case POSIX_FADV_NORMAL:
3259 case POSIX_FADV_RANDOM:
3260 case POSIX_FADV_SEQUENTIAL:
3261 break;
3262 default:
3263 return -EAGAIN;
3264 }
3265 }
Jens Axboe4840e412019-12-25 22:03:45 -07003266
3267 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3268 if (ret < 0)
3269 req_set_fail_links(req);
3270 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003271 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003272 return 0;
3273}
3274
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003275static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3276{
Jens Axboef8748882020-01-08 17:47:02 -07003277 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003278 unsigned lookup_flags;
3279 int ret;
3280
3281 if (sqe->ioprio || sqe->buf_index)
3282 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07003283 if (sqe->flags & IOSQE_FIXED_FILE)
3284 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003285 if (req->flags & REQ_F_NEED_CLEANUP)
3286 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003287
3288 req->open.dfd = READ_ONCE(sqe->fd);
3289 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003290 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003291 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003292 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003293
Jens Axboec12cedf2020-01-08 17:41:21 -07003294 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003295 return -EINVAL;
3296
Jens Axboef8748882020-01-08 17:47:02 -07003297 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003298 if (IS_ERR(req->open.filename)) {
3299 ret = PTR_ERR(req->open.filename);
3300 req->open.filename = NULL;
3301 return ret;
3302 }
3303
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003304 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003305 return 0;
3306}
3307
Pavel Begunkov014db002020-03-03 21:33:12 +03003308static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003309{
3310 struct io_open *ctx = &req->open;
3311 unsigned lookup_flags;
3312 struct path path;
3313 struct kstat stat;
3314 int ret;
3315
3316 if (force_nonblock)
3317 return -EAGAIN;
3318
Jens Axboec12cedf2020-01-08 17:41:21 -07003319 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003320 return -EINVAL;
3321
3322retry:
3323 /* filename_lookup() drops it, keep a reference */
3324 ctx->filename->refcnt++;
3325
3326 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3327 NULL);
3328 if (ret)
3329 goto err;
3330
Jens Axboec12cedf2020-01-08 17:41:21 -07003331 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003332 path_put(&path);
3333 if (retry_estale(ret, lookup_flags)) {
3334 lookup_flags |= LOOKUP_REVAL;
3335 goto retry;
3336 }
3337 if (!ret)
3338 ret = cp_statx(&stat, ctx->buffer);
3339err:
3340 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003341 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003342 if (ret < 0)
3343 req_set_fail_links(req);
3344 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003345 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003346 return 0;
3347}
3348
Jens Axboeb5dba592019-12-11 14:02:38 -07003349static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3350{
3351 /*
3352 * If we queue this for async, it must not be cancellable. That would
3353 * leave the 'file' in an undeterminate state.
3354 */
3355 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3356
3357 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3358 sqe->rw_flags || sqe->buf_index)
3359 return -EINVAL;
3360 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003361 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003362
3363 req->close.fd = READ_ONCE(sqe->fd);
3364 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003365 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003366 return -EBADF;
3367
3368 return 0;
3369}
3370
Pavel Begunkova93b3332020-02-08 14:04:34 +03003371/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003372static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003373{
3374 int ret;
3375
3376 ret = filp_close(req->close.put_file, req->work.files);
3377 if (ret < 0)
3378 req_set_fail_links(req);
3379 io_cqring_add_event(req, ret);
3380 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003381 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003382}
3383
Jens Axboeb5dba592019-12-11 14:02:38 -07003384static void io_close_finish(struct io_wq_work **workptr)
3385{
3386 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003387
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003388 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003389 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003390 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003391}
3392
Pavel Begunkov014db002020-03-03 21:33:12 +03003393static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003394{
3395 int ret;
3396
3397 req->close.put_file = NULL;
3398 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3399 if (ret < 0)
3400 return ret;
3401
3402 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003403 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003404 /* submission ref will be dropped, take it for async */
3405 refcount_inc(&req->refs);
3406
Pavel Begunkova2100672020-03-02 23:45:16 +03003407 req->work.func = io_close_finish;
3408 /*
3409 * Do manual async queue here to avoid grabbing files - we don't
3410 * need the files, and it'll cause io_close_finish() to close
3411 * the file again and cause a double CQE entry for this request
3412 */
3413 io_queue_async_work(req);
3414 return 0;
3415 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003416
3417 /*
3418 * No ->flush(), safely close from here and just punt the
3419 * fput() to async context.
3420 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003421 __io_close_finish(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003422 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003423}
3424
Jens Axboe3529d8c2019-12-19 18:24:38 -07003425static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003426{
3427 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003428
3429 if (!req->file)
3430 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003431
3432 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3433 return -EINVAL;
3434 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3435 return -EINVAL;
3436
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003437 req->sync.off = READ_ONCE(sqe->off);
3438 req->sync.len = READ_ONCE(sqe->len);
3439 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003440 return 0;
3441}
3442
Pavel Begunkov014db002020-03-03 21:33:12 +03003443static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003444{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003445 int ret;
3446
Jens Axboe9adbd452019-12-20 08:45:55 -07003447 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003448 req->sync.flags);
3449 if (ret < 0)
3450 req_set_fail_links(req);
3451 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003452 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003453}
3454
3455
3456static void io_sync_file_range_finish(struct io_wq_work **workptr)
3457{
3458 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3459 struct io_kiocb *nxt = NULL;
3460
3461 if (io_req_cancelled(req))
3462 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003463 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003464 io_put_req(req); /* put submission ref */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003465 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003466 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003467}
3468
Pavel Begunkov014db002020-03-03 21:33:12 +03003469static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003470{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003471 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003472 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003473 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003474 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003475 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003476
Pavel Begunkov014db002020-03-03 21:33:12 +03003477 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003478 return 0;
3479}
3480
YueHaibing469956e2020-03-04 15:53:52 +08003481#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003482static int io_setup_async_msg(struct io_kiocb *req,
3483 struct io_async_msghdr *kmsg)
3484{
3485 if (req->io)
3486 return -EAGAIN;
3487 if (io_alloc_async_ctx(req)) {
3488 if (kmsg->iov != kmsg->fast_iov)
3489 kfree(kmsg->iov);
3490 return -ENOMEM;
3491 }
3492 req->flags |= REQ_F_NEED_CLEANUP;
3493 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3494 return -EAGAIN;
3495}
3496
Jens Axboe3529d8c2019-12-19 18:24:38 -07003497static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003498{
Jens Axboee47293f2019-12-20 08:58:21 -07003499 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003500 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003501 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003502
Jens Axboee47293f2019-12-20 08:58:21 -07003503 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3504 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003505 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003506
Jens Axboed8768362020-02-27 14:17:49 -07003507#ifdef CONFIG_COMPAT
3508 if (req->ctx->compat)
3509 sr->msg_flags |= MSG_CMSG_COMPAT;
3510#endif
3511
Jens Axboefddafac2020-01-04 20:19:44 -07003512 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003513 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003514 /* iovec is already imported */
3515 if (req->flags & REQ_F_NEED_CLEANUP)
3516 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003517
Jens Axboed9688562019-12-09 19:35:20 -07003518 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003519 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003520 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003521 if (!ret)
3522 req->flags |= REQ_F_NEED_CLEANUP;
3523 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003524}
3525
Pavel Begunkov014db002020-03-03 21:33:12 +03003526static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003527{
Jens Axboe0b416c32019-12-15 10:57:46 -07003528 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003529 struct socket *sock;
3530 int ret;
3531
3532 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3533 return -EINVAL;
3534
3535 sock = sock_from_file(req->file, &ret);
3536 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003537 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003538 unsigned flags;
3539
Jens Axboe03b12302019-12-02 18:50:25 -07003540 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003541 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003542 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003543 /* if iov is set, it's allocated already */
3544 if (!kmsg->iov)
3545 kmsg->iov = kmsg->fast_iov;
3546 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003547 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003548 struct io_sr_msg *sr = &req->sr_msg;
3549
Jens Axboe0b416c32019-12-15 10:57:46 -07003550 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003551 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003552
3553 io.msg.iov = io.msg.fast_iov;
3554 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3555 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003556 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003557 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003558 }
3559
Jens Axboee47293f2019-12-20 08:58:21 -07003560 flags = req->sr_msg.msg_flags;
3561 if (flags & MSG_DONTWAIT)
3562 req->flags |= REQ_F_NOWAIT;
3563 else if (force_nonblock)
3564 flags |= MSG_DONTWAIT;
3565
Jens Axboe0b416c32019-12-15 10:57:46 -07003566 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003567 if (force_nonblock && ret == -EAGAIN)
3568 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003569 if (ret == -ERESTARTSYS)
3570 ret = -EINTR;
3571 }
3572
Pavel Begunkov1e950812020-02-06 19:51:16 +03003573 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003574 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003575 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003576 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003577 if (ret < 0)
3578 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003579 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003580 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003581}
3582
Pavel Begunkov014db002020-03-03 21:33:12 +03003583static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003584{
Jens Axboefddafac2020-01-04 20:19:44 -07003585 struct socket *sock;
3586 int ret;
3587
3588 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3589 return -EINVAL;
3590
3591 sock = sock_from_file(req->file, &ret);
3592 if (sock) {
3593 struct io_sr_msg *sr = &req->sr_msg;
3594 struct msghdr msg;
3595 struct iovec iov;
3596 unsigned flags;
3597
3598 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3599 &msg.msg_iter);
3600 if (ret)
3601 return ret;
3602
3603 msg.msg_name = NULL;
3604 msg.msg_control = NULL;
3605 msg.msg_controllen = 0;
3606 msg.msg_namelen = 0;
3607
3608 flags = req->sr_msg.msg_flags;
3609 if (flags & MSG_DONTWAIT)
3610 req->flags |= REQ_F_NOWAIT;
3611 else if (force_nonblock)
3612 flags |= MSG_DONTWAIT;
3613
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003614 msg.msg_flags = flags;
3615 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003616 if (force_nonblock && ret == -EAGAIN)
3617 return -EAGAIN;
3618 if (ret == -ERESTARTSYS)
3619 ret = -EINTR;
3620 }
3621
3622 io_cqring_add_event(req, ret);
3623 if (ret < 0)
3624 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003625 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003626 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003627}
3628
Jens Axboe52de1fe2020-02-27 10:15:42 -07003629static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3630{
3631 struct io_sr_msg *sr = &req->sr_msg;
3632 struct iovec __user *uiov;
3633 size_t iov_len;
3634 int ret;
3635
3636 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3637 &uiov, &iov_len);
3638 if (ret)
3639 return ret;
3640
3641 if (req->flags & REQ_F_BUFFER_SELECT) {
3642 if (iov_len > 1)
3643 return -EINVAL;
3644 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3645 return -EFAULT;
3646 sr->len = io->msg.iov[0].iov_len;
3647 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3648 sr->len);
3649 io->msg.iov = NULL;
3650 } else {
3651 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3652 &io->msg.iov, &io->msg.msg.msg_iter);
3653 if (ret > 0)
3654 ret = 0;
3655 }
3656
3657 return ret;
3658}
3659
3660#ifdef CONFIG_COMPAT
3661static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3662 struct io_async_ctx *io)
3663{
3664 struct compat_msghdr __user *msg_compat;
3665 struct io_sr_msg *sr = &req->sr_msg;
3666 struct compat_iovec __user *uiov;
3667 compat_uptr_t ptr;
3668 compat_size_t len;
3669 int ret;
3670
3671 msg_compat = (struct compat_msghdr __user *) sr->msg;
3672 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3673 &ptr, &len);
3674 if (ret)
3675 return ret;
3676
3677 uiov = compat_ptr(ptr);
3678 if (req->flags & REQ_F_BUFFER_SELECT) {
3679 compat_ssize_t clen;
3680
3681 if (len > 1)
3682 return -EINVAL;
3683 if (!access_ok(uiov, sizeof(*uiov)))
3684 return -EFAULT;
3685 if (__get_user(clen, &uiov->iov_len))
3686 return -EFAULT;
3687 if (clen < 0)
3688 return -EINVAL;
3689 sr->len = io->msg.iov[0].iov_len;
3690 io->msg.iov = NULL;
3691 } else {
3692 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3693 &io->msg.iov,
3694 &io->msg.msg.msg_iter);
3695 if (ret < 0)
3696 return ret;
3697 }
3698
3699 return 0;
3700}
3701#endif
3702
3703static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3704{
3705 io->msg.iov = io->msg.fast_iov;
3706
3707#ifdef CONFIG_COMPAT
3708 if (req->ctx->compat)
3709 return __io_compat_recvmsg_copy_hdr(req, io);
3710#endif
3711
3712 return __io_recvmsg_copy_hdr(req, io);
3713}
3714
Jens Axboebcda7ba2020-02-23 16:42:51 -07003715static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3716 int *cflags, bool needs_lock)
3717{
3718 struct io_sr_msg *sr = &req->sr_msg;
3719 struct io_buffer *kbuf;
3720
3721 if (!(req->flags & REQ_F_BUFFER_SELECT))
3722 return NULL;
3723
3724 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3725 if (IS_ERR(kbuf))
3726 return kbuf;
3727
3728 sr->kbuf = kbuf;
3729 req->flags |= REQ_F_BUFFER_SELECTED;
3730
3731 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3732 *cflags |= IORING_CQE_F_BUFFER;
3733 return kbuf;
3734}
3735
Jens Axboe3529d8c2019-12-19 18:24:38 -07003736static int io_recvmsg_prep(struct io_kiocb *req,
3737 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003738{
Jens Axboee47293f2019-12-20 08:58:21 -07003739 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003740 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003741 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003742
Jens Axboe3529d8c2019-12-19 18:24:38 -07003743 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3744 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003745 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003746 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003747
Jens Axboed8768362020-02-27 14:17:49 -07003748#ifdef CONFIG_COMPAT
3749 if (req->ctx->compat)
3750 sr->msg_flags |= MSG_CMSG_COMPAT;
3751#endif
3752
Jens Axboefddafac2020-01-04 20:19:44 -07003753 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003754 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003755 /* iovec is already imported */
3756 if (req->flags & REQ_F_NEED_CLEANUP)
3757 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003758
Jens Axboe52de1fe2020-02-27 10:15:42 -07003759 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003760 if (!ret)
3761 req->flags |= REQ_F_NEED_CLEANUP;
3762 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003763}
3764
Pavel Begunkov014db002020-03-03 21:33:12 +03003765static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003766{
Jens Axboe0b416c32019-12-15 10:57:46 -07003767 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003768 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003769 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003770
3771 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3772 return -EINVAL;
3773
3774 sock = sock_from_file(req->file, &ret);
3775 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003776 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003777 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003778 unsigned flags;
3779
Jens Axboe03b12302019-12-02 18:50:25 -07003780 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003781 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003782 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003783 /* if iov is set, it's allocated already */
3784 if (!kmsg->iov)
3785 kmsg->iov = kmsg->fast_iov;
3786 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003787 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003788 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003789 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003790
Jens Axboe52de1fe2020-02-27 10:15:42 -07003791 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003792 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003793 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003794 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003795
Jens Axboe52de1fe2020-02-27 10:15:42 -07003796 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3797 if (IS_ERR(kbuf)) {
3798 return PTR_ERR(kbuf);
3799 } else if (kbuf) {
3800 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3801 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3802 1, req->sr_msg.len);
3803 }
3804
Jens Axboee47293f2019-12-20 08:58:21 -07003805 flags = req->sr_msg.msg_flags;
3806 if (flags & MSG_DONTWAIT)
3807 req->flags |= REQ_F_NOWAIT;
3808 else if (force_nonblock)
3809 flags |= MSG_DONTWAIT;
3810
3811 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3812 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003813 if (force_nonblock && ret == -EAGAIN)
3814 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003815 if (ret == -ERESTARTSYS)
3816 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003817 }
3818
Pavel Begunkov1e950812020-02-06 19:51:16 +03003819 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003820 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003821 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003822 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003823 if (ret < 0)
3824 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003825 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003826 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003827}
3828
Pavel Begunkov014db002020-03-03 21:33:12 +03003829static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003830{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003831 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003832 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003833 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003834
3835 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3836 return -EINVAL;
3837
3838 sock = sock_from_file(req->file, &ret);
3839 if (sock) {
3840 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003841 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003842 struct msghdr msg;
3843 struct iovec iov;
3844 unsigned flags;
3845
Jens Axboebcda7ba2020-02-23 16:42:51 -07003846 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3847 if (IS_ERR(kbuf))
3848 return PTR_ERR(kbuf);
3849 else if (kbuf)
3850 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003851
Jens Axboebcda7ba2020-02-23 16:42:51 -07003852 ret = import_single_range(READ, buf, sr->len, &iov,
3853 &msg.msg_iter);
3854 if (ret) {
3855 kfree(kbuf);
3856 return ret;
3857 }
3858
3859 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003860 msg.msg_name = NULL;
3861 msg.msg_control = NULL;
3862 msg.msg_controllen = 0;
3863 msg.msg_namelen = 0;
3864 msg.msg_iocb = NULL;
3865 msg.msg_flags = 0;
3866
3867 flags = req->sr_msg.msg_flags;
3868 if (flags & MSG_DONTWAIT)
3869 req->flags |= REQ_F_NOWAIT;
3870 else if (force_nonblock)
3871 flags |= MSG_DONTWAIT;
3872
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003873 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003874 if (force_nonblock && ret == -EAGAIN)
3875 return -EAGAIN;
3876 if (ret == -ERESTARTSYS)
3877 ret = -EINTR;
3878 }
3879
Jens Axboebcda7ba2020-02-23 16:42:51 -07003880 kfree(kbuf);
3881 req->flags &= ~REQ_F_NEED_CLEANUP;
3882 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003883 if (ret < 0)
3884 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003885 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003886 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003887}
3888
Jens Axboe3529d8c2019-12-19 18:24:38 -07003889static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003890{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003891 struct io_accept *accept = &req->accept;
3892
Jens Axboe17f2fe32019-10-17 14:42:58 -06003893 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3894 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003895 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003896 return -EINVAL;
3897
Jens Axboed55e5f52019-12-11 16:12:15 -07003898 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3899 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003900 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003901 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003902}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003903
Pavel Begunkov014db002020-03-03 21:33:12 +03003904static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003905{
3906 struct io_accept *accept = &req->accept;
3907 unsigned file_flags;
3908 int ret;
3909
3910 file_flags = force_nonblock ? O_NONBLOCK : 0;
3911 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3912 accept->addr_len, accept->flags);
3913 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003914 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003915 if (ret == -ERESTARTSYS)
3916 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003917 if (ret < 0)
3918 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003919 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003920 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003921 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003922}
3923
3924static void io_accept_finish(struct io_wq_work **workptr)
3925{
3926 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003927
3928 if (io_req_cancelled(req))
3929 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003930 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003931 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003932}
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003933
Pavel Begunkov014db002020-03-03 21:33:12 +03003934static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003935{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003936 int ret;
3937
Pavel Begunkov014db002020-03-03 21:33:12 +03003938 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003939 if (ret == -EAGAIN && force_nonblock) {
3940 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003941 return -EAGAIN;
3942 }
3943 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003944}
3945
Jens Axboe3529d8c2019-12-19 18:24:38 -07003946static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003947{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003948 struct io_connect *conn = &req->connect;
3949 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003950
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003951 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3952 return -EINVAL;
3953 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3954 return -EINVAL;
3955
Jens Axboe3529d8c2019-12-19 18:24:38 -07003956 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3957 conn->addr_len = READ_ONCE(sqe->addr2);
3958
3959 if (!io)
3960 return 0;
3961
3962 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003963 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003964}
3965
Pavel Begunkov014db002020-03-03 21:33:12 +03003966static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003967{
Jens Axboef499a022019-12-02 16:28:46 -07003968 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003969 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003970 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003971
Jens Axboef499a022019-12-02 16:28:46 -07003972 if (req->io) {
3973 io = req->io;
3974 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003975 ret = move_addr_to_kernel(req->connect.addr,
3976 req->connect.addr_len,
3977 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003978 if (ret)
3979 goto out;
3980 io = &__io;
3981 }
3982
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003983 file_flags = force_nonblock ? O_NONBLOCK : 0;
3984
3985 ret = __sys_connect_file(req->file, &io->connect.address,
3986 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003987 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003988 if (req->io)
3989 return -EAGAIN;
3990 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003991 ret = -ENOMEM;
3992 goto out;
3993 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003994 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003995 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003996 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003997 if (ret == -ERESTARTSYS)
3998 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003999out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004000 if (ret < 0)
4001 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004002 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004003 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004004 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004005}
YueHaibing469956e2020-03-04 15:53:52 +08004006#else /* !CONFIG_NET */
4007static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4008{
4009 return -EOPNOTSUPP;
4010}
4011
4012static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
4013{
4014 return -EOPNOTSUPP;
4015}
4016
4017static int io_send(struct io_kiocb *req, bool force_nonblock)
4018{
4019 return -EOPNOTSUPP;
4020}
4021
4022static int io_recvmsg_prep(struct io_kiocb *req,
4023 const struct io_uring_sqe *sqe)
4024{
4025 return -EOPNOTSUPP;
4026}
4027
4028static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4029{
4030 return -EOPNOTSUPP;
4031}
4032
4033static int io_recv(struct io_kiocb *req, bool force_nonblock)
4034{
4035 return -EOPNOTSUPP;
4036}
4037
4038static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4039{
4040 return -EOPNOTSUPP;
4041}
4042
4043static int io_accept(struct io_kiocb *req, bool force_nonblock)
4044{
4045 return -EOPNOTSUPP;
4046}
4047
4048static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4049{
4050 return -EOPNOTSUPP;
4051}
4052
4053static int io_connect(struct io_kiocb *req, bool force_nonblock)
4054{
4055 return -EOPNOTSUPP;
4056}
4057#endif /* CONFIG_NET */
Jens Axboef8e85cf2019-11-23 14:24:24 -07004058
Jens Axboed7718a92020-02-14 22:23:12 -07004059struct io_poll_table {
4060 struct poll_table_struct pt;
4061 struct io_kiocb *req;
4062 int error;
4063};
4064
4065static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4066 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004067{
Jens Axboed7718a92020-02-14 22:23:12 -07004068 if (unlikely(poll->head)) {
4069 pt->error = -EINVAL;
4070 return;
4071 }
4072
4073 pt->error = 0;
4074 poll->head = head;
4075 add_wait_queue(head, &poll->wait);
4076}
4077
4078static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4079 struct poll_table_struct *p)
4080{
4081 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4082
4083 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4084}
4085
4086static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4087 __poll_t mask, task_work_func_t func)
4088{
4089 struct task_struct *tsk;
4090
4091 /* for instances that support it check for an event match first: */
4092 if (mask && !(mask & poll->events))
4093 return 0;
4094
4095 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4096
4097 list_del_init(&poll->wait.entry);
4098
4099 tsk = req->task;
4100 req->result = mask;
4101 init_task_work(&req->task_work, func);
4102 /*
4103 * If this fails, then the task is exiting. If that is the case, then
4104 * the exit check will ultimately cancel these work items. Hence we
4105 * don't need to check here and handle it specifically.
4106 */
4107 task_work_add(tsk, &req->task_work, true);
4108 wake_up_process(tsk);
4109 return 1;
4110}
4111
4112static void io_async_task_func(struct callback_head *cb)
4113{
4114 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4115 struct async_poll *apoll = req->apoll;
4116 struct io_ring_ctx *ctx = req->ctx;
4117
4118 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4119
4120 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
4121
4122 if (hash_hashed(&req->hash_node)) {
4123 spin_lock_irq(&ctx->completion_lock);
4124 hash_del(&req->hash_node);
4125 spin_unlock_irq(&ctx->completion_lock);
4126 }
4127
4128 /* restore ->work in case we need to retry again */
4129 memcpy(&req->work, &apoll->work, sizeof(req->work));
4130
4131 __set_current_state(TASK_RUNNING);
4132 mutex_lock(&ctx->uring_lock);
4133 __io_queue_sqe(req, NULL);
4134 mutex_unlock(&ctx->uring_lock);
4135
4136 kfree(apoll);
4137}
4138
4139static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4140 void *key)
4141{
4142 struct io_kiocb *req = wait->private;
4143 struct io_poll_iocb *poll = &req->apoll->poll;
4144
4145 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4146 key_to_poll(key));
4147
4148 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4149}
4150
4151static void io_poll_req_insert(struct io_kiocb *req)
4152{
4153 struct io_ring_ctx *ctx = req->ctx;
4154 struct hlist_head *list;
4155
4156 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4157 hlist_add_head(&req->hash_node, list);
4158}
4159
4160static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4161 struct io_poll_iocb *poll,
4162 struct io_poll_table *ipt, __poll_t mask,
4163 wait_queue_func_t wake_func)
4164 __acquires(&ctx->completion_lock)
4165{
4166 struct io_ring_ctx *ctx = req->ctx;
4167 bool cancel = false;
4168
4169 poll->file = req->file;
4170 poll->head = NULL;
4171 poll->done = poll->canceled = false;
4172 poll->events = mask;
4173
4174 ipt->pt._key = mask;
4175 ipt->req = req;
4176 ipt->error = -EINVAL;
4177
4178 INIT_LIST_HEAD(&poll->wait.entry);
4179 init_waitqueue_func_entry(&poll->wait, wake_func);
4180 poll->wait.private = req;
4181
4182 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4183
4184 spin_lock_irq(&ctx->completion_lock);
4185 if (likely(poll->head)) {
4186 spin_lock(&poll->head->lock);
4187 if (unlikely(list_empty(&poll->wait.entry))) {
4188 if (ipt->error)
4189 cancel = true;
4190 ipt->error = 0;
4191 mask = 0;
4192 }
4193 if (mask || ipt->error)
4194 list_del_init(&poll->wait.entry);
4195 else if (cancel)
4196 WRITE_ONCE(poll->canceled, true);
4197 else if (!poll->done) /* actually waiting for an event */
4198 io_poll_req_insert(req);
4199 spin_unlock(&poll->head->lock);
4200 }
4201
4202 return mask;
4203}
4204
4205static bool io_arm_poll_handler(struct io_kiocb *req)
4206{
4207 const struct io_op_def *def = &io_op_defs[req->opcode];
4208 struct io_ring_ctx *ctx = req->ctx;
4209 struct async_poll *apoll;
4210 struct io_poll_table ipt;
4211 __poll_t mask, ret;
4212
4213 if (!req->file || !file_can_poll(req->file))
4214 return false;
4215 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4216 return false;
4217 if (!def->pollin && !def->pollout)
4218 return false;
4219
4220 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4221 if (unlikely(!apoll))
4222 return false;
4223
4224 req->flags |= REQ_F_POLLED;
4225 memcpy(&apoll->work, &req->work, sizeof(req->work));
4226
4227 /*
4228 * Don't need a reference here, as we're adding it to the task
4229 * task_works list. If the task exits, the list is pruned.
4230 */
4231 req->task = current;
4232 req->apoll = apoll;
4233 INIT_HLIST_NODE(&req->hash_node);
4234
Nathan Chancellor8755d972020-03-02 16:01:19 -07004235 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004236 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004237 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004238 if (def->pollout)
4239 mask |= POLLOUT | POLLWRNORM;
4240 mask |= POLLERR | POLLPRI;
4241
4242 ipt.pt._qproc = io_async_queue_proc;
4243
4244 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4245 io_async_wake);
4246 if (ret) {
4247 ipt.error = 0;
4248 apoll->poll.done = true;
4249 spin_unlock_irq(&ctx->completion_lock);
4250 memcpy(&req->work, &apoll->work, sizeof(req->work));
4251 kfree(apoll);
4252 return false;
4253 }
4254 spin_unlock_irq(&ctx->completion_lock);
4255 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4256 apoll->poll.events);
4257 return true;
4258}
4259
4260static bool __io_poll_remove_one(struct io_kiocb *req,
4261 struct io_poll_iocb *poll)
4262{
Jens Axboeb41e9852020-02-17 09:52:41 -07004263 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004264
4265 spin_lock(&poll->head->lock);
4266 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004267 if (!list_empty(&poll->wait.entry)) {
4268 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004269 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004270 }
4271 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004272 return do_complete;
4273}
4274
4275static bool io_poll_remove_one(struct io_kiocb *req)
4276{
4277 bool do_complete;
4278
4279 if (req->opcode == IORING_OP_POLL_ADD) {
4280 do_complete = __io_poll_remove_one(req, &req->poll);
4281 } else {
4282 /* non-poll requests have submit ref still */
4283 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4284 if (do_complete)
4285 io_put_req(req);
4286 }
4287
Jens Axboe78076bb2019-12-04 19:56:40 -07004288 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004289
Jens Axboeb41e9852020-02-17 09:52:41 -07004290 if (do_complete) {
4291 io_cqring_fill_event(req, -ECANCELED);
4292 io_commit_cqring(req->ctx);
4293 req->flags |= REQ_F_COMP_LOCKED;
4294 io_put_req(req);
4295 }
4296
4297 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004298}
4299
4300static void io_poll_remove_all(struct io_ring_ctx *ctx)
4301{
Jens Axboe78076bb2019-12-04 19:56:40 -07004302 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004303 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07004304 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004305
4306 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004307 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4308 struct hlist_head *list;
4309
4310 list = &ctx->cancel_hash[i];
4311 hlist_for_each_entry_safe(req, tmp, list, hash_node)
4312 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004313 }
4314 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004315
4316 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004317}
4318
Jens Axboe47f46762019-11-09 17:43:02 -07004319static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4320{
Jens Axboe78076bb2019-12-04 19:56:40 -07004321 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004322 struct io_kiocb *req;
4323
Jens Axboe78076bb2019-12-04 19:56:40 -07004324 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4325 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004326 if (sqe_addr != req->user_data)
4327 continue;
4328 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004329 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004330 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004331 }
4332
4333 return -ENOENT;
4334}
4335
Jens Axboe3529d8c2019-12-19 18:24:38 -07004336static int io_poll_remove_prep(struct io_kiocb *req,
4337 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004338{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004339 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4340 return -EINVAL;
4341 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4342 sqe->poll_events)
4343 return -EINVAL;
4344
Jens Axboe0969e782019-12-17 18:40:57 -07004345 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004346 return 0;
4347}
4348
4349/*
4350 * Find a running poll command that matches one specified in sqe->addr,
4351 * and remove it if found.
4352 */
4353static int io_poll_remove(struct io_kiocb *req)
4354{
4355 struct io_ring_ctx *ctx = req->ctx;
4356 u64 addr;
4357 int ret;
4358
Jens Axboe0969e782019-12-17 18:40:57 -07004359 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004360 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004361 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004362 spin_unlock_irq(&ctx->completion_lock);
4363
Jens Axboe78e19bb2019-11-06 15:21:34 -07004364 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004365 if (ret < 0)
4366 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004367 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004368 return 0;
4369}
4370
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004371static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004372{
Jackie Liua197f662019-11-08 08:09:12 -07004373 struct io_ring_ctx *ctx = req->ctx;
4374
Jens Axboe8c838782019-03-12 15:48:16 -06004375 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004376 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004377 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004378}
4379
Jens Axboeb41e9852020-02-17 09:52:41 -07004380static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004381{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004382 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004383
Jens Axboe221c5eb2019-01-17 09:41:58 -07004384 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004385 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004386 io_poll_complete(req, req->result, 0);
4387 req->flags |= REQ_F_COMP_LOCKED;
4388 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004389 spin_unlock_irq(&ctx->completion_lock);
4390
Jens Axboe8c838782019-03-12 15:48:16 -06004391 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07004392}
Jens Axboe89723d02019-11-05 15:32:58 -07004393
Jens Axboeb41e9852020-02-17 09:52:41 -07004394static void io_poll_task_func(struct callback_head *cb)
4395{
4396 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4397 struct io_kiocb *nxt = NULL;
4398
4399 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004400 if (nxt) {
4401 struct io_ring_ctx *ctx = nxt->ctx;
4402
4403 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004404 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004405 mutex_unlock(&ctx->uring_lock);
4406 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004407}
4408
Jens Axboe221c5eb2019-01-17 09:41:58 -07004409static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4410 void *key)
4411{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004412 struct io_kiocb *req = wait->private;
4413 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004414
Jens Axboed7718a92020-02-14 22:23:12 -07004415 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004416}
4417
Jens Axboe221c5eb2019-01-17 09:41:58 -07004418static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4419 struct poll_table_struct *p)
4420{
4421 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4422
Jens Axboed7718a92020-02-14 22:23:12 -07004423 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004424}
4425
Jens Axboe3529d8c2019-12-19 18:24:38 -07004426static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004427{
4428 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004429 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004430
4431 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4432 return -EINVAL;
4433 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4434 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004435 if (!poll->file)
4436 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004437
Jens Axboe221c5eb2019-01-17 09:41:58 -07004438 events = READ_ONCE(sqe->poll_events);
4439 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004440
Jens Axboed7718a92020-02-14 22:23:12 -07004441 /*
4442 * Don't need a reference here, as we're adding it to the task
4443 * task_works list. If the task exits, the list is pruned.
4444 */
Jens Axboeb41e9852020-02-17 09:52:41 -07004445 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004446 return 0;
4447}
4448
Pavel Begunkov014db002020-03-03 21:33:12 +03004449static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004450{
4451 struct io_poll_iocb *poll = &req->poll;
4452 struct io_ring_ctx *ctx = req->ctx;
4453 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004454 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004455
Jens Axboe78076bb2019-12-04 19:56:40 -07004456 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004457 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004458 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004459
Jens Axboed7718a92020-02-14 22:23:12 -07004460 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4461 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004462
Jens Axboe8c838782019-03-12 15:48:16 -06004463 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004464 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004465 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004466 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004467 spin_unlock_irq(&ctx->completion_lock);
4468
Jens Axboe8c838782019-03-12 15:48:16 -06004469 if (mask) {
4470 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004471 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004472 }
Jens Axboe8c838782019-03-12 15:48:16 -06004473 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004474}
4475
Jens Axboe5262f562019-09-17 12:26:57 -06004476static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4477{
Jens Axboead8a48a2019-11-15 08:49:11 -07004478 struct io_timeout_data *data = container_of(timer,
4479 struct io_timeout_data, timer);
4480 struct io_kiocb *req = data->req;
4481 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004482 unsigned long flags;
4483
Jens Axboe5262f562019-09-17 12:26:57 -06004484 atomic_inc(&ctx->cq_timeouts);
4485
4486 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004487 /*
Jens Axboe11365042019-10-16 09:08:32 -06004488 * We could be racing with timeout deletion. If the list is empty,
4489 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004490 */
Jens Axboe842f9612019-10-29 12:34:10 -06004491 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004492 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004493
Jens Axboe11365042019-10-16 09:08:32 -06004494 /*
4495 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004496 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004497 * pointer will be increased, otherwise other timeout reqs may
4498 * return in advance without waiting for enough wait_nr.
4499 */
4500 prev = req;
4501 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4502 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004503 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004504 }
Jens Axboe842f9612019-10-29 12:34:10 -06004505
Jens Axboe78e19bb2019-11-06 15:21:34 -07004506 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004507 io_commit_cqring(ctx);
4508 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4509
4510 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004511 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004512 io_put_req(req);
4513 return HRTIMER_NORESTART;
4514}
4515
Jens Axboe47f46762019-11-09 17:43:02 -07004516static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4517{
4518 struct io_kiocb *req;
4519 int ret = -ENOENT;
4520
4521 list_for_each_entry(req, &ctx->timeout_list, list) {
4522 if (user_data == req->user_data) {
4523 list_del_init(&req->list);
4524 ret = 0;
4525 break;
4526 }
4527 }
4528
4529 if (ret == -ENOENT)
4530 return ret;
4531
Jens Axboe2d283902019-12-04 11:08:05 -07004532 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004533 if (ret == -1)
4534 return -EALREADY;
4535
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004536 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004537 io_cqring_fill_event(req, -ECANCELED);
4538 io_put_req(req);
4539 return 0;
4540}
4541
Jens Axboe3529d8c2019-12-19 18:24:38 -07004542static int io_timeout_remove_prep(struct io_kiocb *req,
4543 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004544{
Jens Axboeb29472e2019-12-17 18:50:29 -07004545 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4546 return -EINVAL;
4547 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4548 return -EINVAL;
4549
4550 req->timeout.addr = READ_ONCE(sqe->addr);
4551 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4552 if (req->timeout.flags)
4553 return -EINVAL;
4554
Jens Axboeb29472e2019-12-17 18:50:29 -07004555 return 0;
4556}
4557
Jens Axboe11365042019-10-16 09:08:32 -06004558/*
4559 * Remove or update an existing timeout command
4560 */
Jens Axboefc4df992019-12-10 14:38:45 -07004561static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004562{
4563 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004564 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004565
Jens Axboe11365042019-10-16 09:08:32 -06004566 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004567 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004568
Jens Axboe47f46762019-11-09 17:43:02 -07004569 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004570 io_commit_cqring(ctx);
4571 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004572 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004573 if (ret < 0)
4574 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004575 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004576 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004577}
4578
Jens Axboe3529d8c2019-12-19 18:24:38 -07004579static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004580 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004581{
Jens Axboead8a48a2019-11-15 08:49:11 -07004582 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004583 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004584
Jens Axboead8a48a2019-11-15 08:49:11 -07004585 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004586 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004587 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004588 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004589 if (sqe->off && is_timeout_link)
4590 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004591 flags = READ_ONCE(sqe->timeout_flags);
4592 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004593 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004594
Jens Axboe26a61672019-12-20 09:02:01 -07004595 req->timeout.count = READ_ONCE(sqe->off);
4596
Jens Axboe3529d8c2019-12-19 18:24:38 -07004597 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004598 return -ENOMEM;
4599
4600 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004601 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004602 req->flags |= REQ_F_TIMEOUT;
4603
4604 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004605 return -EFAULT;
4606
Jens Axboe11365042019-10-16 09:08:32 -06004607 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004608 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004609 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004610 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004611
Jens Axboead8a48a2019-11-15 08:49:11 -07004612 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4613 return 0;
4614}
4615
Jens Axboefc4df992019-12-10 14:38:45 -07004616static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004617{
4618 unsigned count;
4619 struct io_ring_ctx *ctx = req->ctx;
4620 struct io_timeout_data *data;
4621 struct list_head *entry;
4622 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004623
Jens Axboe2d283902019-12-04 11:08:05 -07004624 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004625
Jens Axboe5262f562019-09-17 12:26:57 -06004626 /*
4627 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004628 * timeout event to be satisfied. If it isn't set, then this is
4629 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004630 */
Jens Axboe26a61672019-12-20 09:02:01 -07004631 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004632 if (!count) {
4633 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4634 spin_lock_irq(&ctx->completion_lock);
4635 entry = ctx->timeout_list.prev;
4636 goto add;
4637 }
Jens Axboe5262f562019-09-17 12:26:57 -06004638
4639 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004640 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004641
4642 /*
4643 * Insertion sort, ensuring the first entry in the list is always
4644 * the one we need first.
4645 */
Jens Axboe5262f562019-09-17 12:26:57 -06004646 spin_lock_irq(&ctx->completion_lock);
4647 list_for_each_prev(entry, &ctx->timeout_list) {
4648 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004649 unsigned nxt_sq_head;
4650 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004651 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004652
Jens Axboe93bd25b2019-11-11 23:34:31 -07004653 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4654 continue;
4655
yangerkun5da0fb12019-10-15 21:59:29 +08004656 /*
4657 * Since cached_sq_head + count - 1 can overflow, use type long
4658 * long to store it.
4659 */
4660 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004661 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4662 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004663
4664 /*
4665 * cached_sq_head may overflow, and it will never overflow twice
4666 * once there is some timeout req still be valid.
4667 */
4668 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004669 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004670
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004671 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004672 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004673
4674 /*
4675 * Sequence of reqs after the insert one and itself should
4676 * be adjusted because each timeout req consumes a slot.
4677 */
4678 span++;
4679 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004680 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004681 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004682add:
Jens Axboe5262f562019-09-17 12:26:57 -06004683 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004684 data->timer.function = io_timeout_fn;
4685 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004686 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004687 return 0;
4688}
4689
Jens Axboe62755e32019-10-28 21:49:21 -06004690static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004691{
Jens Axboe62755e32019-10-28 21:49:21 -06004692 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004693
Jens Axboe62755e32019-10-28 21:49:21 -06004694 return req->user_data == (unsigned long) data;
4695}
4696
Jens Axboee977d6d2019-11-05 12:39:45 -07004697static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004698{
Jens Axboe62755e32019-10-28 21:49:21 -06004699 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004700 int ret = 0;
4701
Jens Axboe62755e32019-10-28 21:49:21 -06004702 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4703 switch (cancel_ret) {
4704 case IO_WQ_CANCEL_OK:
4705 ret = 0;
4706 break;
4707 case IO_WQ_CANCEL_RUNNING:
4708 ret = -EALREADY;
4709 break;
4710 case IO_WQ_CANCEL_NOTFOUND:
4711 ret = -ENOENT;
4712 break;
4713 }
4714
Jens Axboee977d6d2019-11-05 12:39:45 -07004715 return ret;
4716}
4717
Jens Axboe47f46762019-11-09 17:43:02 -07004718static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4719 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004720 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004721{
4722 unsigned long flags;
4723 int ret;
4724
4725 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4726 if (ret != -ENOENT) {
4727 spin_lock_irqsave(&ctx->completion_lock, flags);
4728 goto done;
4729 }
4730
4731 spin_lock_irqsave(&ctx->completion_lock, flags);
4732 ret = io_timeout_cancel(ctx, sqe_addr);
4733 if (ret != -ENOENT)
4734 goto done;
4735 ret = io_poll_cancel(ctx, sqe_addr);
4736done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004737 if (!ret)
4738 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004739 io_cqring_fill_event(req, ret);
4740 io_commit_cqring(ctx);
4741 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4742 io_cqring_ev_posted(ctx);
4743
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004744 if (ret < 0)
4745 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004746 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004747}
4748
Jens Axboe3529d8c2019-12-19 18:24:38 -07004749static int io_async_cancel_prep(struct io_kiocb *req,
4750 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004751{
Jens Axboefbf23842019-12-17 18:45:56 -07004752 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004753 return -EINVAL;
4754 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4755 sqe->cancel_flags)
4756 return -EINVAL;
4757
Jens Axboefbf23842019-12-17 18:45:56 -07004758 req->cancel.addr = READ_ONCE(sqe->addr);
4759 return 0;
4760}
4761
Pavel Begunkov014db002020-03-03 21:33:12 +03004762static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004763{
4764 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004765
Pavel Begunkov014db002020-03-03 21:33:12 +03004766 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004767 return 0;
4768}
4769
Jens Axboe05f3fb32019-12-09 11:22:50 -07004770static int io_files_update_prep(struct io_kiocb *req,
4771 const struct io_uring_sqe *sqe)
4772{
4773 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4774 return -EINVAL;
4775
4776 req->files_update.offset = READ_ONCE(sqe->off);
4777 req->files_update.nr_args = READ_ONCE(sqe->len);
4778 if (!req->files_update.nr_args)
4779 return -EINVAL;
4780 req->files_update.arg = READ_ONCE(sqe->addr);
4781 return 0;
4782}
4783
4784static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4785{
4786 struct io_ring_ctx *ctx = req->ctx;
4787 struct io_uring_files_update up;
4788 int ret;
4789
Jens Axboef86cd202020-01-29 13:46:44 -07004790 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004791 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004792
4793 up.offset = req->files_update.offset;
4794 up.fds = req->files_update.arg;
4795
4796 mutex_lock(&ctx->uring_lock);
4797 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4798 mutex_unlock(&ctx->uring_lock);
4799
4800 if (ret < 0)
4801 req_set_fail_links(req);
4802 io_cqring_add_event(req, ret);
4803 io_put_req(req);
4804 return 0;
4805}
4806
Jens Axboe3529d8c2019-12-19 18:24:38 -07004807static int io_req_defer_prep(struct io_kiocb *req,
4808 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004809{
Jens Axboee7815732019-12-17 19:45:06 -07004810 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004811
Jens Axboef86cd202020-01-29 13:46:44 -07004812 if (io_op_defs[req->opcode].file_table) {
4813 ret = io_grab_files(req);
4814 if (unlikely(ret))
4815 return ret;
4816 }
4817
Jens Axboecccf0ee2020-01-27 16:34:48 -07004818 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4819
Jens Axboed625c6e2019-12-17 19:53:05 -07004820 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004821 case IORING_OP_NOP:
4822 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004823 case IORING_OP_READV:
4824 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004825 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004826 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004827 break;
4828 case IORING_OP_WRITEV:
4829 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004830 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004831 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004832 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004833 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004834 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004835 break;
4836 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004837 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004838 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004839 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004840 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004841 break;
4842 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004843 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004844 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004845 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004846 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004847 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004848 break;
4849 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004850 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004851 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004852 break;
Jens Axboef499a022019-12-02 16:28:46 -07004853 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004854 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004855 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004856 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004857 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004858 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004859 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004860 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004861 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004862 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004863 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004864 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004865 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004866 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004867 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004868 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004869 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004870 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004871 case IORING_OP_FALLOCATE:
4872 ret = io_fallocate_prep(req, sqe);
4873 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004874 case IORING_OP_OPENAT:
4875 ret = io_openat_prep(req, sqe);
4876 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004877 case IORING_OP_CLOSE:
4878 ret = io_close_prep(req, sqe);
4879 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004880 case IORING_OP_FILES_UPDATE:
4881 ret = io_files_update_prep(req, sqe);
4882 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004883 case IORING_OP_STATX:
4884 ret = io_statx_prep(req, sqe);
4885 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004886 case IORING_OP_FADVISE:
4887 ret = io_fadvise_prep(req, sqe);
4888 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004889 case IORING_OP_MADVISE:
4890 ret = io_madvise_prep(req, sqe);
4891 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004892 case IORING_OP_OPENAT2:
4893 ret = io_openat2_prep(req, sqe);
4894 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004895 case IORING_OP_EPOLL_CTL:
4896 ret = io_epoll_ctl_prep(req, sqe);
4897 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004898 case IORING_OP_SPLICE:
4899 ret = io_splice_prep(req, sqe);
4900 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004901 case IORING_OP_PROVIDE_BUFFERS:
4902 ret = io_provide_buffers_prep(req, sqe);
4903 break;
Jens Axboe067524e2020-03-02 16:32:28 -07004904 case IORING_OP_REMOVE_BUFFERS:
4905 ret = io_remove_buffers_prep(req, sqe);
4906 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004907 default:
Jens Axboee7815732019-12-17 19:45:06 -07004908 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4909 req->opcode);
4910 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004911 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004912 }
4913
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004914 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004915}
4916
Jens Axboe3529d8c2019-12-19 18:24:38 -07004917static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004918{
Jackie Liua197f662019-11-08 08:09:12 -07004919 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004920 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004921
Bob Liu9d858b22019-11-13 18:06:25 +08004922 /* Still need defer if there is pending req in defer list. */
4923 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004924 return 0;
4925
Jens Axboe3529d8c2019-12-19 18:24:38 -07004926 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004927 return -EAGAIN;
4928
Jens Axboe3529d8c2019-12-19 18:24:38 -07004929 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004930 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004931 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004932
Jens Axboede0617e2019-04-06 21:51:27 -06004933 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004934 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004935 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004936 return 0;
4937 }
4938
Jens Axboe915967f2019-11-21 09:01:20 -07004939 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004940 list_add_tail(&req->list, &ctx->defer_list);
4941 spin_unlock_irq(&ctx->completion_lock);
4942 return -EIOCBQUEUED;
4943}
4944
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004945static void io_cleanup_req(struct io_kiocb *req)
4946{
4947 struct io_async_ctx *io = req->io;
4948
4949 switch (req->opcode) {
4950 case IORING_OP_READV:
4951 case IORING_OP_READ_FIXED:
4952 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07004953 if (req->flags & REQ_F_BUFFER_SELECTED)
4954 kfree((void *)(unsigned long)req->rw.addr);
4955 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004956 case IORING_OP_WRITEV:
4957 case IORING_OP_WRITE_FIXED:
4958 case IORING_OP_WRITE:
4959 if (io->rw.iov != io->rw.fast_iov)
4960 kfree(io->rw.iov);
4961 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004962 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07004963 if (req->flags & REQ_F_BUFFER_SELECTED)
4964 kfree(req->sr_msg.kbuf);
4965 /* fallthrough */
4966 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004967 if (io->msg.iov != io->msg.fast_iov)
4968 kfree(io->msg.iov);
4969 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004970 case IORING_OP_RECV:
4971 if (req->flags & REQ_F_BUFFER_SELECTED)
4972 kfree(req->sr_msg.kbuf);
4973 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004974 case IORING_OP_OPENAT:
4975 case IORING_OP_OPENAT2:
4976 case IORING_OP_STATX:
4977 putname(req->open.filename);
4978 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004979 case IORING_OP_SPLICE:
4980 io_put_file(req, req->splice.file_in,
4981 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4982 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004983 }
4984
4985 req->flags &= ~REQ_F_NEED_CLEANUP;
4986}
4987
Jens Axboe3529d8c2019-12-19 18:24:38 -07004988static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03004989 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004990{
Jackie Liua197f662019-11-08 08:09:12 -07004991 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004992 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004993
Jens Axboed625c6e2019-12-17 19:53:05 -07004994 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004995 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004996 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004997 break;
4998 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004999 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005000 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005001 if (sqe) {
5002 ret = io_read_prep(req, sqe, force_nonblock);
5003 if (ret < 0)
5004 break;
5005 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005006 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005007 break;
5008 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005009 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005010 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005011 if (sqe) {
5012 ret = io_write_prep(req, sqe, force_nonblock);
5013 if (ret < 0)
5014 break;
5015 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005016 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005017 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005018 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005019 if (sqe) {
5020 ret = io_prep_fsync(req, sqe);
5021 if (ret < 0)
5022 break;
5023 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005024 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005025 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005026 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005027 if (sqe) {
5028 ret = io_poll_add_prep(req, sqe);
5029 if (ret)
5030 break;
5031 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005032 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005033 break;
5034 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005035 if (sqe) {
5036 ret = io_poll_remove_prep(req, sqe);
5037 if (ret < 0)
5038 break;
5039 }
Jens Axboefc4df992019-12-10 14:38:45 -07005040 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005041 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005042 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005043 if (sqe) {
5044 ret = io_prep_sfr(req, sqe);
5045 if (ret < 0)
5046 break;
5047 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005048 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005049 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005050 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005051 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005052 if (sqe) {
5053 ret = io_sendmsg_prep(req, sqe);
5054 if (ret < 0)
5055 break;
5056 }
Jens Axboefddafac2020-01-04 20:19:44 -07005057 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005058 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005059 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005060 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005061 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005062 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005063 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005064 if (sqe) {
5065 ret = io_recvmsg_prep(req, sqe);
5066 if (ret)
5067 break;
5068 }
Jens Axboefddafac2020-01-04 20:19:44 -07005069 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005070 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005071 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005072 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005073 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005074 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005075 if (sqe) {
5076 ret = io_timeout_prep(req, sqe, false);
5077 if (ret)
5078 break;
5079 }
Jens Axboefc4df992019-12-10 14:38:45 -07005080 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005081 break;
Jens Axboe11365042019-10-16 09:08:32 -06005082 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005083 if (sqe) {
5084 ret = io_timeout_remove_prep(req, sqe);
5085 if (ret)
5086 break;
5087 }
Jens Axboefc4df992019-12-10 14:38:45 -07005088 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005089 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005090 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005091 if (sqe) {
5092 ret = io_accept_prep(req, sqe);
5093 if (ret)
5094 break;
5095 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005096 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005097 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005098 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005099 if (sqe) {
5100 ret = io_connect_prep(req, sqe);
5101 if (ret)
5102 break;
5103 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005104 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005105 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005106 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005107 if (sqe) {
5108 ret = io_async_cancel_prep(req, sqe);
5109 if (ret)
5110 break;
5111 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005112 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005113 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005114 case IORING_OP_FALLOCATE:
5115 if (sqe) {
5116 ret = io_fallocate_prep(req, sqe);
5117 if (ret)
5118 break;
5119 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005120 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005121 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005122 case IORING_OP_OPENAT:
5123 if (sqe) {
5124 ret = io_openat_prep(req, sqe);
5125 if (ret)
5126 break;
5127 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005128 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005129 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005130 case IORING_OP_CLOSE:
5131 if (sqe) {
5132 ret = io_close_prep(req, sqe);
5133 if (ret)
5134 break;
5135 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005136 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005137 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005138 case IORING_OP_FILES_UPDATE:
5139 if (sqe) {
5140 ret = io_files_update_prep(req, sqe);
5141 if (ret)
5142 break;
5143 }
5144 ret = io_files_update(req, force_nonblock);
5145 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005146 case IORING_OP_STATX:
5147 if (sqe) {
5148 ret = io_statx_prep(req, sqe);
5149 if (ret)
5150 break;
5151 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005152 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005153 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005154 case IORING_OP_FADVISE:
5155 if (sqe) {
5156 ret = io_fadvise_prep(req, sqe);
5157 if (ret)
5158 break;
5159 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005160 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005161 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005162 case IORING_OP_MADVISE:
5163 if (sqe) {
5164 ret = io_madvise_prep(req, sqe);
5165 if (ret)
5166 break;
5167 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005168 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005169 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005170 case IORING_OP_OPENAT2:
5171 if (sqe) {
5172 ret = io_openat2_prep(req, sqe);
5173 if (ret)
5174 break;
5175 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005176 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005177 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005178 case IORING_OP_EPOLL_CTL:
5179 if (sqe) {
5180 ret = io_epoll_ctl_prep(req, sqe);
5181 if (ret)
5182 break;
5183 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005184 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005185 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005186 case IORING_OP_SPLICE:
5187 if (sqe) {
5188 ret = io_splice_prep(req, sqe);
5189 if (ret < 0)
5190 break;
5191 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005192 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005193 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005194 case IORING_OP_PROVIDE_BUFFERS:
5195 if (sqe) {
5196 ret = io_provide_buffers_prep(req, sqe);
5197 if (ret)
5198 break;
5199 }
5200 ret = io_provide_buffers(req, force_nonblock);
5201 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005202 case IORING_OP_REMOVE_BUFFERS:
5203 if (sqe) {
5204 ret = io_remove_buffers_prep(req, sqe);
5205 if (ret)
5206 break;
5207 }
5208 ret = io_remove_buffers(req, force_nonblock);
5209 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005210 default:
5211 ret = -EINVAL;
5212 break;
5213 }
5214
Jens Axboedef596e2019-01-09 08:59:42 -07005215 if (ret)
5216 return ret;
5217
5218 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005219 const bool in_async = io_wq_current_is_worker();
5220
Jens Axboe9e645e112019-05-10 16:07:28 -06005221 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07005222 return -EAGAIN;
5223
Jens Axboe11ba8202020-01-15 21:51:17 -07005224 /* workqueue context doesn't hold uring_lock, grab it now */
5225 if (in_async)
5226 mutex_lock(&ctx->uring_lock);
5227
Jens Axboedef596e2019-01-09 08:59:42 -07005228 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005229
5230 if (in_async)
5231 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005232 }
5233
5234 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005235}
5236
Jens Axboe561fb042019-10-24 07:25:42 -06005237static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07005238{
Jens Axboe561fb042019-10-24 07:25:42 -06005239 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005240 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005241 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005242
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005243 /* if NO_CANCEL is set, we must still run the work */
5244 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5245 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005246 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005247 }
Jens Axboe31b51512019-01-18 22:56:34 -07005248
Jens Axboe561fb042019-10-24 07:25:42 -06005249 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005250 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005251 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005252 /*
5253 * We can get EAGAIN for polled IO even though we're
5254 * forcing a sync submission from here, since we can't
5255 * wait for request slots on the block side.
5256 */
5257 if (ret != -EAGAIN)
5258 break;
5259 cond_resched();
5260 } while (1);
5261 }
Jens Axboe31b51512019-01-18 22:56:34 -07005262
Jens Axboe561fb042019-10-24 07:25:42 -06005263 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005264 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005265 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005266 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005267 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005268
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005269 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005270}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005271
Jens Axboe15b71ab2019-12-11 11:20:36 -07005272static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005273{
Jens Axboed3656342019-12-18 09:50:26 -07005274 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005275 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07005276 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07005277 return 0;
5278 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06005279}
5280
Jens Axboe65e19f52019-10-26 07:20:21 -06005281static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5282 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005283{
Jens Axboe65e19f52019-10-26 07:20:21 -06005284 struct fixed_file_table *table;
5285
Jens Axboe05f3fb32019-12-09 11:22:50 -07005286 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5287 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06005288}
5289
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005290static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5291 int fd, struct file **out_file, bool fixed)
5292{
5293 struct io_ring_ctx *ctx = req->ctx;
5294 struct file *file;
5295
5296 if (fixed) {
5297 if (unlikely(!ctx->file_data ||
5298 (unsigned) fd >= ctx->nr_user_files))
5299 return -EBADF;
5300 fd = array_index_nospec(fd, ctx->nr_user_files);
5301 file = io_file_from_index(ctx, fd);
5302 if (!file)
5303 return -EBADF;
5304 percpu_ref_get(&ctx->file_data->refs);
5305 } else {
5306 trace_io_uring_file_get(ctx, fd);
5307 file = __io_file_get(state, fd);
5308 if (unlikely(!file))
5309 return -EBADF;
5310 }
5311
5312 *out_file = file;
5313 return 0;
5314}
5315
Jens Axboe3529d8c2019-12-19 18:24:38 -07005316static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
5317 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06005318{
5319 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07005320 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005321 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005322
Jens Axboe3529d8c2019-12-19 18:24:38 -07005323 flags = READ_ONCE(sqe->flags);
5324 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06005325
Jens Axboed3656342019-12-18 09:50:26 -07005326 if (!io_req_needs_file(req, fd))
5327 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06005328
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005329 fixed = (flags & IOSQE_FIXED_FILE);
5330 if (unlikely(!fixed && req->needs_fixed_file))
5331 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005332
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005333 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005334}
5335
Jackie Liua197f662019-11-08 08:09:12 -07005336static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005337{
Jens Axboefcb323c2019-10-24 12:39:47 -06005338 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005339 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005340
Jens Axboef86cd202020-01-29 13:46:44 -07005341 if (req->work.files)
5342 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005343 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005344 return -EBADF;
5345
Jens Axboefcb323c2019-10-24 12:39:47 -06005346 rcu_read_lock();
5347 spin_lock_irq(&ctx->inflight_lock);
5348 /*
5349 * We use the f_ops->flush() handler to ensure that we can flush
5350 * out work accessing these files if the fd is closed. Check if
5351 * the fd has changed since we started down this path, and disallow
5352 * this operation if it has.
5353 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005354 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005355 list_add(&req->inflight_entry, &ctx->inflight_list);
5356 req->flags |= REQ_F_INFLIGHT;
5357 req->work.files = current->files;
5358 ret = 0;
5359 }
5360 spin_unlock_irq(&ctx->inflight_lock);
5361 rcu_read_unlock();
5362
5363 return ret;
5364}
5365
Jens Axboe2665abf2019-11-05 12:40:47 -07005366static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5367{
Jens Axboead8a48a2019-11-15 08:49:11 -07005368 struct io_timeout_data *data = container_of(timer,
5369 struct io_timeout_data, timer);
5370 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005371 struct io_ring_ctx *ctx = req->ctx;
5372 struct io_kiocb *prev = NULL;
5373 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005374
5375 spin_lock_irqsave(&ctx->completion_lock, flags);
5376
5377 /*
5378 * We don't expect the list to be empty, that will only happen if we
5379 * race with the completion of the linked work.
5380 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005381 if (!list_empty(&req->link_list)) {
5382 prev = list_entry(req->link_list.prev, struct io_kiocb,
5383 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005384 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005385 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005386 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5387 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005388 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005389 }
5390
5391 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5392
5393 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005394 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005395 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005396 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005397 } else {
5398 io_cqring_add_event(req, -ETIME);
5399 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005400 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005401 return HRTIMER_NORESTART;
5402}
5403
Jens Axboead8a48a2019-11-15 08:49:11 -07005404static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005405{
Jens Axboe76a46e02019-11-10 23:34:16 -07005406 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005407
Jens Axboe76a46e02019-11-10 23:34:16 -07005408 /*
5409 * If the list is now empty, then our linked request finished before
5410 * we got a chance to setup the timer
5411 */
5412 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005413 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005414 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005415
Jens Axboead8a48a2019-11-15 08:49:11 -07005416 data->timer.function = io_link_timeout_fn;
5417 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5418 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005419 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005420 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005421
Jens Axboe2665abf2019-11-05 12:40:47 -07005422 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005423 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005424}
5425
Jens Axboead8a48a2019-11-15 08:49:11 -07005426static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005427{
5428 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005429
Jens Axboe2665abf2019-11-05 12:40:47 -07005430 if (!(req->flags & REQ_F_LINK))
5431 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005432 /* for polled retry, if flag is set, we already went through here */
5433 if (req->flags & REQ_F_POLLED)
5434 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005435
Pavel Begunkov44932332019-12-05 16:16:35 +03005436 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5437 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005438 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005439 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005440
Jens Axboe76a46e02019-11-10 23:34:16 -07005441 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005442 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005443}
5444
Jens Axboe3529d8c2019-12-19 18:24:38 -07005445static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005446{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005447 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005448 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005449 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005450 int ret;
5451
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005452again:
5453 linked_timeout = io_prep_linked_timeout(req);
5454
Jens Axboe193155c2020-02-22 23:22:19 -07005455 if (req->work.creds && req->work.creds != current_cred()) {
5456 if (old_creds)
5457 revert_creds(old_creds);
5458 if (old_creds == req->work.creds)
5459 old_creds = NULL; /* restored original creds */
5460 else
5461 old_creds = override_creds(req->work.creds);
5462 }
5463
Pavel Begunkov014db002020-03-03 21:33:12 +03005464 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005465
5466 /*
5467 * We async punt it if the file wasn't marked NOWAIT, or if the file
5468 * doesn't support non-blocking read/write attempts
5469 */
5470 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5471 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005472 if (io_arm_poll_handler(req)) {
5473 if (linked_timeout)
5474 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005475 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005476 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005477punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005478 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005479 ret = io_grab_files(req);
5480 if (ret)
5481 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005482 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005483
5484 /*
5485 * Queued up for async execution, worker will release
5486 * submit reference when the iocb is actually submitted.
5487 */
5488 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005489 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005490 }
Jens Axboee65ef562019-03-12 10:16:44 -06005491
Jens Axboefcb323c2019-10-24 12:39:47 -06005492err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005493 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005494 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005495 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005496
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005497 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005498 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005499 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005500 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005501 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005502 }
5503
Jens Axboee65ef562019-03-12 10:16:44 -06005504 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005505 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005506 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005507 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005508 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005509 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005510 if (nxt) {
5511 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005512
5513 if (req->flags & REQ_F_FORCE_ASYNC)
5514 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005515 goto again;
5516 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005517exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005518 if (old_creds)
5519 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005520}
5521
Jens Axboe3529d8c2019-12-19 18:24:38 -07005522static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005523{
5524 int ret;
5525
Jens Axboe3529d8c2019-12-19 18:24:38 -07005526 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005527 if (ret) {
5528 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005529fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005530 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005531 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005532 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005533 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005534 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005535 ret = io_req_defer_prep(req, sqe);
5536 if (unlikely(ret < 0))
5537 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005538 /*
5539 * Never try inline submit of IOSQE_ASYNC is set, go straight
5540 * to async execution.
5541 */
5542 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5543 io_queue_async_work(req);
5544 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005545 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005546 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005547}
5548
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005549static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005550{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005551 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005552 io_cqring_add_event(req, -ECANCELED);
5553 io_double_put_req(req);
5554 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005555 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005556}
5557
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005558#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboebcda7ba2020-02-23 16:42:51 -07005559 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5560 IOSQE_BUFFER_SELECT)
Jens Axboe9e645e112019-05-10 16:07:28 -06005561
Jens Axboe3529d8c2019-12-19 18:24:38 -07005562static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5563 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005564{
Jackie Liua197f662019-11-08 08:09:12 -07005565 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005566 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005567 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005568
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005569 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005570
5571 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005572 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005573 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005574 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005575 }
5576
Jens Axboebcda7ba2020-02-23 16:42:51 -07005577 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5578 !io_op_defs[req->opcode].buffer_select) {
5579 ret = -EOPNOTSUPP;
5580 goto err_req;
5581 }
5582
Jens Axboe75c6a032020-01-28 10:15:23 -07005583 id = READ_ONCE(sqe->personality);
5584 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005585 req->work.creds = idr_find(&ctx->personality_idr, id);
5586 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005587 ret = -EINVAL;
5588 goto err_req;
5589 }
Jens Axboe193155c2020-02-22 23:22:19 -07005590 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005591 }
5592
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005593 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005594 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
Jens Axboebcda7ba2020-02-23 16:42:51 -07005595 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5596 IOSQE_BUFFER_SELECT);
Jens Axboe9e645e112019-05-10 16:07:28 -06005597
Jens Axboe3529d8c2019-12-19 18:24:38 -07005598 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005599 if (unlikely(ret)) {
5600err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005601 io_cqring_add_event(req, ret);
5602 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005603 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005604 }
5605
Jens Axboe9e645e112019-05-10 16:07:28 -06005606 /*
5607 * If we already have a head request, queue this one for async
5608 * submittal once the head completes. If we don't have a head but
5609 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5610 * submitted sync once the chain is complete. If none of those
5611 * conditions are true (normal request), then just queue it.
5612 */
5613 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005614 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005615
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005616 /*
5617 * Taking sequential execution of a link, draining both sides
5618 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5619 * requests in the link. So, it drains the head and the
5620 * next after the link request. The last one is done via
5621 * drain_next flag to persist the effect across calls.
5622 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005623 if (sqe_flags & IOSQE_IO_DRAIN) {
5624 head->flags |= REQ_F_IO_DRAIN;
5625 ctx->drain_next = 1;
5626 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005627 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005628 ret = -EAGAIN;
5629 goto err_req;
5630 }
5631
Jens Axboe3529d8c2019-12-19 18:24:38 -07005632 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005633 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005634 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005635 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005636 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005637 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005638 trace_io_uring_link(ctx, req, head);
5639 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005640
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005641 /* last request of a link, enqueue the link */
5642 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5643 io_queue_link_head(head);
5644 *link = NULL;
5645 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005646 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005647 if (unlikely(ctx->drain_next)) {
5648 req->flags |= REQ_F_IO_DRAIN;
5649 req->ctx->drain_next = 0;
5650 }
5651 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5652 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005653 INIT_LIST_HEAD(&req->link_list);
5654 ret = io_req_defer_prep(req, sqe);
5655 if (ret)
5656 req->flags |= REQ_F_FAIL_LINK;
5657 *link = req;
5658 } else {
5659 io_queue_sqe(req, sqe);
5660 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005661 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005662
5663 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005664}
5665
Jens Axboe9a56a232019-01-09 09:06:50 -07005666/*
5667 * Batched submission is done, ensure local IO is flushed out.
5668 */
5669static void io_submit_state_end(struct io_submit_state *state)
5670{
5671 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005672 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005673 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005674 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005675}
5676
5677/*
5678 * Start submission side cache.
5679 */
5680static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005681 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005682{
5683 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005684 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005685 state->file = NULL;
5686 state->ios_left = max_ios;
5687}
5688
Jens Axboe2b188cc2019-01-07 10:46:33 -07005689static void io_commit_sqring(struct io_ring_ctx *ctx)
5690{
Hristo Venev75b28af2019-08-26 17:23:46 +00005691 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005692
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005693 /*
5694 * Ensure any loads from the SQEs are done at this point,
5695 * since once we write the new head, the application could
5696 * write new data to them.
5697 */
5698 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005699}
5700
5701/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005702 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005703 * that is mapped by userspace. This means that care needs to be taken to
5704 * ensure that reads are stable, as we cannot rely on userspace always
5705 * being a good citizen. If members of the sqe are validated and then later
5706 * used, it's important that those reads are done through READ_ONCE() to
5707 * prevent a re-load down the line.
5708 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005709static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5710 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005711{
Hristo Venev75b28af2019-08-26 17:23:46 +00005712 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005713 unsigned head;
5714
5715 /*
5716 * The cached sq head (or cq tail) serves two purposes:
5717 *
5718 * 1) allows us to batch the cost of updating the user visible
5719 * head updates.
5720 * 2) allows the kernel side to track the head on its own, even
5721 * though the application is the one updating it.
5722 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005723 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005724 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005725 /*
5726 * All io need record the previous position, if LINK vs DARIN,
5727 * it can be used to mark the position of the first IO in the
5728 * link list.
5729 */
5730 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005731 *sqe_ptr = &ctx->sq_sqes[head];
5732 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5733 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005734 ctx->cached_sq_head++;
5735 return true;
5736 }
5737
5738 /* drop invalid entries */
5739 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005740 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005741 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005742 return false;
5743}
5744
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005745static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005746 struct file *ring_file, int ring_fd,
5747 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005748{
5749 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005750 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005751 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005752 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005753
Jens Axboec4a2ed72019-11-21 21:01:26 -07005754 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005755 if (test_bit(0, &ctx->sq_check_overflow)) {
5756 if (!list_empty(&ctx->cq_overflow_list) &&
5757 !io_cqring_overflow_flush(ctx, false))
5758 return -EBUSY;
5759 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005760
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005761 /* make sure SQ entry isn't read before tail */
5762 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005763
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005764 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5765 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005766
5767 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005768 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005769 statep = &state;
5770 }
5771
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005772 ctx->ring_fd = ring_fd;
5773 ctx->ring_file = ring_file;
5774
Jens Axboe6c271ce2019-01-10 11:22:30 -07005775 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005776 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005777 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005778 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005779
Pavel Begunkov196be952019-11-07 01:41:06 +03005780 req = io_get_req(ctx, statep);
5781 if (unlikely(!req)) {
5782 if (!submitted)
5783 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005784 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005785 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005786 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005787 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005788 break;
5789 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005790
Jens Axboed3656342019-12-18 09:50:26 -07005791 /* will complete beyond this point, count as submitted */
5792 submitted++;
5793
5794 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005795 err = -EINVAL;
5796fail_req:
5797 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005798 io_double_put_req(req);
5799 break;
5800 }
5801
5802 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005803 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005804 if (unlikely(mm_fault)) {
5805 err = -EFAULT;
5806 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005807 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005808 use_mm(ctx->sqo_mm);
5809 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005810 }
5811
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005812 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005813 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5814 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005815 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005816 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005817 }
5818
Pavel Begunkov9466f432020-01-25 22:34:01 +03005819 if (unlikely(submitted != nr)) {
5820 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5821
5822 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5823 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005824 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005825 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005826 if (statep)
5827 io_submit_state_end(&state);
5828
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005829 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5830 io_commit_sqring(ctx);
5831
Jens Axboe6c271ce2019-01-10 11:22:30 -07005832 return submitted;
5833}
5834
5835static int io_sq_thread(void *data)
5836{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005837 struct io_ring_ctx *ctx = data;
5838 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005839 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005840 mm_segment_t old_fs;
5841 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005842 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005843 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005844
Jens Axboe206aefd2019-11-07 18:27:42 -07005845 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005846
Jens Axboe6c271ce2019-01-10 11:22:30 -07005847 old_fs = get_fs();
5848 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005849 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005850
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005851 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005852 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005853 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005854
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005855 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005856 unsigned nr_events = 0;
5857
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005858 mutex_lock(&ctx->uring_lock);
5859 if (!list_empty(&ctx->poll_list))
5860 io_iopoll_getevents(ctx, &nr_events, 0);
5861 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005862 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005863 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005864 }
5865
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005866 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005867
5868 /*
5869 * If submit got -EBUSY, flag us as needing the application
5870 * to enter the kernel to reap and flush events.
5871 */
5872 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005873 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005874 * Drop cur_mm before scheduling, we can't hold it for
5875 * long periods (or over schedule()). Do this before
5876 * adding ourselves to the waitqueue, as the unuse/drop
5877 * may sleep.
5878 */
5879 if (cur_mm) {
5880 unuse_mm(cur_mm);
5881 mmput(cur_mm);
5882 cur_mm = NULL;
5883 }
5884
5885 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005886 * We're polling. If we're within the defined idle
5887 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005888 * to sleep. The exception is if we got EBUSY doing
5889 * more IO, we should wait for the application to
5890 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005891 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005892 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005893 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5894 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005895 if (current->task_works)
5896 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005897 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005898 continue;
5899 }
5900
Jens Axboe6c271ce2019-01-10 11:22:30 -07005901 prepare_to_wait(&ctx->sqo_wait, &wait,
5902 TASK_INTERRUPTIBLE);
5903
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005904 /*
5905 * While doing polled IO, before going to sleep, we need
5906 * to check if there are new reqs added to poll_list, it
5907 * is because reqs may have been punted to io worker and
5908 * will be added to poll_list later, hence check the
5909 * poll_list again.
5910 */
5911 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5912 !list_empty_careful(&ctx->poll_list)) {
5913 finish_wait(&ctx->sqo_wait, &wait);
5914 continue;
5915 }
5916
Jens Axboe6c271ce2019-01-10 11:22:30 -07005917 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005918 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005919 /* make sure to read SQ tail after writing flags */
5920 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005921
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005922 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005923 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005924 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005925 finish_wait(&ctx->sqo_wait, &wait);
5926 break;
5927 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005928 if (current->task_works) {
5929 task_work_run();
5930 continue;
5931 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005932 if (signal_pending(current))
5933 flush_signals(current);
5934 schedule();
5935 finish_wait(&ctx->sqo_wait, &wait);
5936
Hristo Venev75b28af2019-08-26 17:23:46 +00005937 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005938 continue;
5939 }
5940 finish_wait(&ctx->sqo_wait, &wait);
5941
Hristo Venev75b28af2019-08-26 17:23:46 +00005942 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005943 }
5944
Jens Axboe8a4955f2019-12-09 14:52:35 -07005945 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005946 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005947 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005948 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005949 }
5950
Jens Axboeb41e9852020-02-17 09:52:41 -07005951 if (current->task_works)
5952 task_work_run();
5953
Jens Axboe6c271ce2019-01-10 11:22:30 -07005954 set_fs(old_fs);
5955 if (cur_mm) {
5956 unuse_mm(cur_mm);
5957 mmput(cur_mm);
5958 }
Jens Axboe181e4482019-11-25 08:52:30 -07005959 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005960
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005961 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005962
Jens Axboe6c271ce2019-01-10 11:22:30 -07005963 return 0;
5964}
5965
Jens Axboebda52162019-09-24 13:47:15 -06005966struct io_wait_queue {
5967 struct wait_queue_entry wq;
5968 struct io_ring_ctx *ctx;
5969 unsigned to_wait;
5970 unsigned nr_timeouts;
5971};
5972
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005973static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005974{
5975 struct io_ring_ctx *ctx = iowq->ctx;
5976
5977 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005978 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005979 * started waiting. For timeouts, we always want to return to userspace,
5980 * regardless of event count.
5981 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005982 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005983 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5984}
5985
5986static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5987 int wake_flags, void *key)
5988{
5989 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5990 wq);
5991
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005992 /* use noflush == true, as we can't safely rely on locking context */
5993 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005994 return -1;
5995
5996 return autoremove_wake_function(curr, mode, wake_flags, key);
5997}
5998
Jens Axboe2b188cc2019-01-07 10:46:33 -07005999/*
6000 * Wait until events become available, if we don't already have some. The
6001 * application must reap them itself, as they reside on the shared cq ring.
6002 */
6003static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6004 const sigset_t __user *sig, size_t sigsz)
6005{
Jens Axboebda52162019-09-24 13:47:15 -06006006 struct io_wait_queue iowq = {
6007 .wq = {
6008 .private = current,
6009 .func = io_wake_function,
6010 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6011 },
6012 .ctx = ctx,
6013 .to_wait = min_events,
6014 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006015 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006016 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006017
Jens Axboeb41e9852020-02-17 09:52:41 -07006018 do {
6019 if (io_cqring_events(ctx, false) >= min_events)
6020 return 0;
6021 if (!current->task_works)
6022 break;
6023 task_work_run();
6024 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006025
6026 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006027#ifdef CONFIG_COMPAT
6028 if (in_compat_syscall())
6029 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006030 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006031 else
6032#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006033 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006034
Jens Axboe2b188cc2019-01-07 10:46:33 -07006035 if (ret)
6036 return ret;
6037 }
6038
Jens Axboebda52162019-09-24 13:47:15 -06006039 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006040 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006041 do {
6042 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6043 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006044 if (current->task_works)
6045 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006046 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006047 break;
6048 schedule();
6049 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006050 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006051 break;
6052 }
6053 } while (1);
6054 finish_wait(&ctx->wait, &iowq.wq);
6055
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006056 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006057
Hristo Venev75b28af2019-08-26 17:23:46 +00006058 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006059}
6060
Jens Axboe6b063142019-01-10 22:13:58 -07006061static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6062{
6063#if defined(CONFIG_UNIX)
6064 if (ctx->ring_sock) {
6065 struct sock *sock = ctx->ring_sock->sk;
6066 struct sk_buff *skb;
6067
6068 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6069 kfree_skb(skb);
6070 }
6071#else
6072 int i;
6073
Jens Axboe65e19f52019-10-26 07:20:21 -06006074 for (i = 0; i < ctx->nr_user_files; i++) {
6075 struct file *file;
6076
6077 file = io_file_from_index(ctx, i);
6078 if (file)
6079 fput(file);
6080 }
Jens Axboe6b063142019-01-10 22:13:58 -07006081#endif
6082}
6083
Jens Axboe05f3fb32019-12-09 11:22:50 -07006084static void io_file_ref_kill(struct percpu_ref *ref)
6085{
6086 struct fixed_file_data *data;
6087
6088 data = container_of(ref, struct fixed_file_data, refs);
6089 complete(&data->done);
6090}
6091
Jens Axboe6b063142019-01-10 22:13:58 -07006092static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6093{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006094 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06006095 unsigned nr_tables, i;
6096
Jens Axboe05f3fb32019-12-09 11:22:50 -07006097 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006098 return -ENXIO;
6099
Jens Axboe05f3fb32019-12-09 11:22:50 -07006100 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07006101 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006102 wait_for_completion(&data->done);
6103 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006104 percpu_ref_exit(&data->refs);
6105
Jens Axboe6b063142019-01-10 22:13:58 -07006106 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006107 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6108 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006109 kfree(data->table[i].files);
6110 kfree(data->table);
6111 kfree(data);
6112 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006113 ctx->nr_user_files = 0;
6114 return 0;
6115}
6116
Jens Axboe6c271ce2019-01-10 11:22:30 -07006117static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6118{
6119 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07006120 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006121 /*
6122 * The park is a bit of a work-around, without it we get
6123 * warning spews on shutdown with SQPOLL set and affinity
6124 * set to a single CPU.
6125 */
Jens Axboe06058632019-04-13 09:26:03 -06006126 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006127 kthread_stop(ctx->sqo_thread);
6128 ctx->sqo_thread = NULL;
6129 }
6130}
6131
Jens Axboe6b063142019-01-10 22:13:58 -07006132static void io_finish_async(struct io_ring_ctx *ctx)
6133{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006134 io_sq_thread_stop(ctx);
6135
Jens Axboe561fb042019-10-24 07:25:42 -06006136 if (ctx->io_wq) {
6137 io_wq_destroy(ctx->io_wq);
6138 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006139 }
6140}
6141
6142#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006143/*
6144 * Ensure the UNIX gc is aware of our file set, so we are certain that
6145 * the io_uring can be safely unregistered on process exit, even if we have
6146 * loops in the file referencing.
6147 */
6148static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6149{
6150 struct sock *sk = ctx->ring_sock->sk;
6151 struct scm_fp_list *fpl;
6152 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006153 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006154
6155 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
6156 unsigned long inflight = ctx->user->unix_inflight + nr;
6157
6158 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
6159 return -EMFILE;
6160 }
6161
6162 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6163 if (!fpl)
6164 return -ENOMEM;
6165
6166 skb = alloc_skb(0, GFP_KERNEL);
6167 if (!skb) {
6168 kfree(fpl);
6169 return -ENOMEM;
6170 }
6171
6172 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006173
Jens Axboe08a45172019-10-03 08:11:03 -06006174 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006175 fpl->user = get_uid(ctx->user);
6176 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006177 struct file *file = io_file_from_index(ctx, i + offset);
6178
6179 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006180 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006181 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006182 unix_inflight(fpl->user, fpl->fp[nr_files]);
6183 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006184 }
6185
Jens Axboe08a45172019-10-03 08:11:03 -06006186 if (nr_files) {
6187 fpl->max = SCM_MAX_FD;
6188 fpl->count = nr_files;
6189 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006190 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006191 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6192 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006193
Jens Axboe08a45172019-10-03 08:11:03 -06006194 for (i = 0; i < nr_files; i++)
6195 fput(fpl->fp[i]);
6196 } else {
6197 kfree_skb(skb);
6198 kfree(fpl);
6199 }
Jens Axboe6b063142019-01-10 22:13:58 -07006200
6201 return 0;
6202}
6203
6204/*
6205 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6206 * causes regular reference counting to break down. We rely on the UNIX
6207 * garbage collection to take care of this problem for us.
6208 */
6209static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6210{
6211 unsigned left, total;
6212 int ret = 0;
6213
6214 total = 0;
6215 left = ctx->nr_user_files;
6216 while (left) {
6217 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006218
6219 ret = __io_sqe_files_scm(ctx, this_files, total);
6220 if (ret)
6221 break;
6222 left -= this_files;
6223 total += this_files;
6224 }
6225
6226 if (!ret)
6227 return 0;
6228
6229 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006230 struct file *file = io_file_from_index(ctx, total);
6231
6232 if (file)
6233 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006234 total++;
6235 }
6236
6237 return ret;
6238}
6239#else
6240static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6241{
6242 return 0;
6243}
6244#endif
6245
Jens Axboe65e19f52019-10-26 07:20:21 -06006246static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6247 unsigned nr_files)
6248{
6249 int i;
6250
6251 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006252 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006253 unsigned this_files;
6254
6255 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6256 table->files = kcalloc(this_files, sizeof(struct file *),
6257 GFP_KERNEL);
6258 if (!table->files)
6259 break;
6260 nr_files -= this_files;
6261 }
6262
6263 if (i == nr_tables)
6264 return 0;
6265
6266 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006267 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006268 kfree(table->files);
6269 }
6270 return 1;
6271}
6272
Jens Axboe05f3fb32019-12-09 11:22:50 -07006273static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006274{
6275#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006276 struct sock *sock = ctx->ring_sock->sk;
6277 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6278 struct sk_buff *skb;
6279 int i;
6280
6281 __skb_queue_head_init(&list);
6282
6283 /*
6284 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6285 * remove this entry and rearrange the file array.
6286 */
6287 skb = skb_dequeue(head);
6288 while (skb) {
6289 struct scm_fp_list *fp;
6290
6291 fp = UNIXCB(skb).fp;
6292 for (i = 0; i < fp->count; i++) {
6293 int left;
6294
6295 if (fp->fp[i] != file)
6296 continue;
6297
6298 unix_notinflight(fp->user, fp->fp[i]);
6299 left = fp->count - 1 - i;
6300 if (left) {
6301 memmove(&fp->fp[i], &fp->fp[i + 1],
6302 left * sizeof(struct file *));
6303 }
6304 fp->count--;
6305 if (!fp->count) {
6306 kfree_skb(skb);
6307 skb = NULL;
6308 } else {
6309 __skb_queue_tail(&list, skb);
6310 }
6311 fput(file);
6312 file = NULL;
6313 break;
6314 }
6315
6316 if (!file)
6317 break;
6318
6319 __skb_queue_tail(&list, skb);
6320
6321 skb = skb_dequeue(head);
6322 }
6323
6324 if (skb_peek(&list)) {
6325 spin_lock_irq(&head->lock);
6326 while ((skb = __skb_dequeue(&list)) != NULL)
6327 __skb_queue_tail(head, skb);
6328 spin_unlock_irq(&head->lock);
6329 }
6330#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006331 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006332#endif
6333}
6334
Jens Axboe05f3fb32019-12-09 11:22:50 -07006335struct io_file_put {
6336 struct llist_node llist;
6337 struct file *file;
6338 struct completion *done;
6339};
6340
Jens Axboe2faf8522020-02-04 19:54:55 -07006341static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006342{
6343 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006344 struct llist_node *node;
6345
Jens Axboe05f3fb32019-12-09 11:22:50 -07006346 while ((node = llist_del_all(&data->put_llist)) != NULL) {
6347 llist_for_each_entry_safe(pfile, tmp, node, llist) {
6348 io_ring_file_put(data->ctx, pfile->file);
6349 if (pfile->done)
6350 complete(pfile->done);
6351 else
6352 kfree(pfile);
6353 }
6354 }
Jens Axboe2faf8522020-02-04 19:54:55 -07006355}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006356
Jens Axboe2faf8522020-02-04 19:54:55 -07006357static void io_ring_file_ref_switch(struct work_struct *work)
6358{
6359 struct fixed_file_data *data;
6360
6361 data = container_of(work, struct fixed_file_data, ref_work);
6362 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006363 percpu_ref_switch_to_percpu(&data->refs);
6364}
6365
6366static void io_file_data_ref_zero(struct percpu_ref *ref)
6367{
6368 struct fixed_file_data *data;
6369
6370 data = container_of(ref, struct fixed_file_data, refs);
6371
Jens Axboe2faf8522020-02-04 19:54:55 -07006372 /*
6373 * We can't safely switch from inside this context, punt to wq. If
6374 * the table ref is going away, the table is being unregistered.
6375 * Don't queue up the async work for that case, the caller will
6376 * handle it.
6377 */
6378 if (!percpu_ref_is_dying(&data->refs))
6379 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006380}
6381
6382static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6383 unsigned nr_args)
6384{
6385 __s32 __user *fds = (__s32 __user *) arg;
6386 unsigned nr_tables;
6387 struct file *file;
6388 int fd, ret = 0;
6389 unsigned i;
6390
6391 if (ctx->file_data)
6392 return -EBUSY;
6393 if (!nr_args)
6394 return -EINVAL;
6395 if (nr_args > IORING_MAX_FIXED_FILES)
6396 return -EMFILE;
6397
6398 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6399 if (!ctx->file_data)
6400 return -ENOMEM;
6401 ctx->file_data->ctx = ctx;
6402 init_completion(&ctx->file_data->done);
6403
6404 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6405 ctx->file_data->table = kcalloc(nr_tables,
6406 sizeof(struct fixed_file_table),
6407 GFP_KERNEL);
6408 if (!ctx->file_data->table) {
6409 kfree(ctx->file_data);
6410 ctx->file_data = NULL;
6411 return -ENOMEM;
6412 }
6413
6414 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
6415 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6416 kfree(ctx->file_data->table);
6417 kfree(ctx->file_data);
6418 ctx->file_data = NULL;
6419 return -ENOMEM;
6420 }
6421 ctx->file_data->put_llist.first = NULL;
6422 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
6423
6424 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6425 percpu_ref_exit(&ctx->file_data->refs);
6426 kfree(ctx->file_data->table);
6427 kfree(ctx->file_data);
6428 ctx->file_data = NULL;
6429 return -ENOMEM;
6430 }
6431
6432 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6433 struct fixed_file_table *table;
6434 unsigned index;
6435
6436 ret = -EFAULT;
6437 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6438 break;
6439 /* allow sparse sets */
6440 if (fd == -1) {
6441 ret = 0;
6442 continue;
6443 }
6444
6445 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6446 index = i & IORING_FILE_TABLE_MASK;
6447 file = fget(fd);
6448
6449 ret = -EBADF;
6450 if (!file)
6451 break;
6452
6453 /*
6454 * Don't allow io_uring instances to be registered. If UNIX
6455 * isn't enabled, then this causes a reference cycle and this
6456 * instance can never get freed. If UNIX is enabled we'll
6457 * handle it just fine, but there's still no point in allowing
6458 * a ring fd as it doesn't support regular read/write anyway.
6459 */
6460 if (file->f_op == &io_uring_fops) {
6461 fput(file);
6462 break;
6463 }
6464 ret = 0;
6465 table->files[index] = file;
6466 }
6467
6468 if (ret) {
6469 for (i = 0; i < ctx->nr_user_files; i++) {
6470 file = io_file_from_index(ctx, i);
6471 if (file)
6472 fput(file);
6473 }
6474 for (i = 0; i < nr_tables; i++)
6475 kfree(ctx->file_data->table[i].files);
6476
6477 kfree(ctx->file_data->table);
6478 kfree(ctx->file_data);
6479 ctx->file_data = NULL;
6480 ctx->nr_user_files = 0;
6481 return ret;
6482 }
6483
6484 ret = io_sqe_files_scm(ctx);
6485 if (ret)
6486 io_sqe_files_unregister(ctx);
6487
6488 return ret;
6489}
6490
Jens Axboec3a31e62019-10-03 13:59:56 -06006491static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6492 int index)
6493{
6494#if defined(CONFIG_UNIX)
6495 struct sock *sock = ctx->ring_sock->sk;
6496 struct sk_buff_head *head = &sock->sk_receive_queue;
6497 struct sk_buff *skb;
6498
6499 /*
6500 * See if we can merge this file into an existing skb SCM_RIGHTS
6501 * file set. If there's no room, fall back to allocating a new skb
6502 * and filling it in.
6503 */
6504 spin_lock_irq(&head->lock);
6505 skb = skb_peek(head);
6506 if (skb) {
6507 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6508
6509 if (fpl->count < SCM_MAX_FD) {
6510 __skb_unlink(skb, head);
6511 spin_unlock_irq(&head->lock);
6512 fpl->fp[fpl->count] = get_file(file);
6513 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6514 fpl->count++;
6515 spin_lock_irq(&head->lock);
6516 __skb_queue_head(head, skb);
6517 } else {
6518 skb = NULL;
6519 }
6520 }
6521 spin_unlock_irq(&head->lock);
6522
6523 if (skb) {
6524 fput(file);
6525 return 0;
6526 }
6527
6528 return __io_sqe_files_scm(ctx, 1, index);
6529#else
6530 return 0;
6531#endif
6532}
6533
Jens Axboe05f3fb32019-12-09 11:22:50 -07006534static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06006535{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006536 struct fixed_file_data *data;
6537
Jens Axboedd3db2a2020-02-26 10:23:43 -07006538 /*
6539 * Juggle reference to ensure we hit zero, if needed, so we can
6540 * switch back to percpu mode
6541 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006542 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07006543 percpu_ref_put(&data->refs);
6544 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006545}
6546
6547static bool io_queue_file_removal(struct fixed_file_data *data,
6548 struct file *file)
6549{
6550 struct io_file_put *pfile, pfile_stack;
6551 DECLARE_COMPLETION_ONSTACK(done);
6552
6553 /*
6554 * If we fail allocating the struct we need for doing async reomval
6555 * of this file, just punt to sync and wait for it.
6556 */
6557 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
6558 if (!pfile) {
6559 pfile = &pfile_stack;
6560 pfile->done = &done;
6561 }
6562
6563 pfile->file = file;
6564 llist_add(&pfile->llist, &data->put_llist);
6565
6566 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07006567 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006568 wait_for_completion(&done);
6569 flush_work(&data->ref_work);
6570 return false;
6571 }
6572
6573 return true;
6574}
6575
6576static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6577 struct io_uring_files_update *up,
6578 unsigned nr_args)
6579{
6580 struct fixed_file_data *data = ctx->file_data;
6581 bool ref_switch = false;
6582 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006583 __s32 __user *fds;
6584 int fd, i, err;
6585 __u32 done;
6586
Jens Axboe05f3fb32019-12-09 11:22:50 -07006587 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006588 return -EOVERFLOW;
6589 if (done > ctx->nr_user_files)
6590 return -EINVAL;
6591
6592 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006593 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006594 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006595 struct fixed_file_table *table;
6596 unsigned index;
6597
Jens Axboec3a31e62019-10-03 13:59:56 -06006598 err = 0;
6599 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6600 err = -EFAULT;
6601 break;
6602 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006603 i = array_index_nospec(up->offset, ctx->nr_user_files);
6604 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006605 index = i & IORING_FILE_TABLE_MASK;
6606 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006607 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006608 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006609 if (io_queue_file_removal(data, file))
6610 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006611 }
6612 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006613 file = fget(fd);
6614 if (!file) {
6615 err = -EBADF;
6616 break;
6617 }
6618 /*
6619 * Don't allow io_uring instances to be registered. If
6620 * UNIX isn't enabled, then this causes a reference
6621 * cycle and this instance can never get freed. If UNIX
6622 * is enabled we'll handle it just fine, but there's
6623 * still no point in allowing a ring fd as it doesn't
6624 * support regular read/write anyway.
6625 */
6626 if (file->f_op == &io_uring_fops) {
6627 fput(file);
6628 err = -EBADF;
6629 break;
6630 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006631 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006632 err = io_sqe_file_register(ctx, file, i);
6633 if (err)
6634 break;
6635 }
6636 nr_args--;
6637 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006638 up->offset++;
6639 }
6640
Jens Axboedd3db2a2020-02-26 10:23:43 -07006641 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006642 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006643
6644 return done ? done : err;
6645}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006646static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6647 unsigned nr_args)
6648{
6649 struct io_uring_files_update up;
6650
6651 if (!ctx->file_data)
6652 return -ENXIO;
6653 if (!nr_args)
6654 return -EINVAL;
6655 if (copy_from_user(&up, arg, sizeof(up)))
6656 return -EFAULT;
6657 if (up.resv)
6658 return -EINVAL;
6659
6660 return __io_sqe_files_update(ctx, &up, nr_args);
6661}
Jens Axboec3a31e62019-10-03 13:59:56 -06006662
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006663static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006664{
6665 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6666
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006667 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006668 io_put_req(req);
6669}
6670
Pavel Begunkov24369c22020-01-28 03:15:48 +03006671static int io_init_wq_offload(struct io_ring_ctx *ctx,
6672 struct io_uring_params *p)
6673{
6674 struct io_wq_data data;
6675 struct fd f;
6676 struct io_ring_ctx *ctx_attach;
6677 unsigned int concurrency;
6678 int ret = 0;
6679
6680 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006681 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006682
6683 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6684 /* Do QD, or 4 * CPUS, whatever is smallest */
6685 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6686
6687 ctx->io_wq = io_wq_create(concurrency, &data);
6688 if (IS_ERR(ctx->io_wq)) {
6689 ret = PTR_ERR(ctx->io_wq);
6690 ctx->io_wq = NULL;
6691 }
6692 return ret;
6693 }
6694
6695 f = fdget(p->wq_fd);
6696 if (!f.file)
6697 return -EBADF;
6698
6699 if (f.file->f_op != &io_uring_fops) {
6700 ret = -EINVAL;
6701 goto out_fput;
6702 }
6703
6704 ctx_attach = f.file->private_data;
6705 /* @io_wq is protected by holding the fd */
6706 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6707 ret = -EINVAL;
6708 goto out_fput;
6709 }
6710
6711 ctx->io_wq = ctx_attach->io_wq;
6712out_fput:
6713 fdput(f);
6714 return ret;
6715}
6716
Jens Axboe6c271ce2019-01-10 11:22:30 -07006717static int io_sq_offload_start(struct io_ring_ctx *ctx,
6718 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006719{
6720 int ret;
6721
Jens Axboe6c271ce2019-01-10 11:22:30 -07006722 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006723 mmgrab(current->mm);
6724 ctx->sqo_mm = current->mm;
6725
Jens Axboe6c271ce2019-01-10 11:22:30 -07006726 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006727 ret = -EPERM;
6728 if (!capable(CAP_SYS_ADMIN))
6729 goto err;
6730
Jens Axboe917257d2019-04-13 09:28:55 -06006731 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6732 if (!ctx->sq_thread_idle)
6733 ctx->sq_thread_idle = HZ;
6734
Jens Axboe6c271ce2019-01-10 11:22:30 -07006735 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006736 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006737
Jens Axboe917257d2019-04-13 09:28:55 -06006738 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006739 if (cpu >= nr_cpu_ids)
6740 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006741 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006742 goto err;
6743
Jens Axboe6c271ce2019-01-10 11:22:30 -07006744 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6745 ctx, cpu,
6746 "io_uring-sq");
6747 } else {
6748 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6749 "io_uring-sq");
6750 }
6751 if (IS_ERR(ctx->sqo_thread)) {
6752 ret = PTR_ERR(ctx->sqo_thread);
6753 ctx->sqo_thread = NULL;
6754 goto err;
6755 }
6756 wake_up_process(ctx->sqo_thread);
6757 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6758 /* Can't have SQ_AFF without SQPOLL */
6759 ret = -EINVAL;
6760 goto err;
6761 }
6762
Pavel Begunkov24369c22020-01-28 03:15:48 +03006763 ret = io_init_wq_offload(ctx, p);
6764 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006765 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006766
6767 return 0;
6768err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006769 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006770 mmdrop(ctx->sqo_mm);
6771 ctx->sqo_mm = NULL;
6772 return ret;
6773}
6774
6775static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6776{
6777 atomic_long_sub(nr_pages, &user->locked_vm);
6778}
6779
6780static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6781{
6782 unsigned long page_limit, cur_pages, new_pages;
6783
6784 /* Don't allow more pages than we can safely lock */
6785 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6786
6787 do {
6788 cur_pages = atomic_long_read(&user->locked_vm);
6789 new_pages = cur_pages + nr_pages;
6790 if (new_pages > page_limit)
6791 return -ENOMEM;
6792 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6793 new_pages) != cur_pages);
6794
6795 return 0;
6796}
6797
6798static void io_mem_free(void *ptr)
6799{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006800 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006801
Mark Rutland52e04ef2019-04-30 17:30:21 +01006802 if (!ptr)
6803 return;
6804
6805 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006806 if (put_page_testzero(page))
6807 free_compound_page(page);
6808}
6809
6810static void *io_mem_alloc(size_t size)
6811{
6812 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6813 __GFP_NORETRY;
6814
6815 return (void *) __get_free_pages(gfp_flags, get_order(size));
6816}
6817
Hristo Venev75b28af2019-08-26 17:23:46 +00006818static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6819 size_t *sq_offset)
6820{
6821 struct io_rings *rings;
6822 size_t off, sq_array_size;
6823
6824 off = struct_size(rings, cqes, cq_entries);
6825 if (off == SIZE_MAX)
6826 return SIZE_MAX;
6827
6828#ifdef CONFIG_SMP
6829 off = ALIGN(off, SMP_CACHE_BYTES);
6830 if (off == 0)
6831 return SIZE_MAX;
6832#endif
6833
6834 sq_array_size = array_size(sizeof(u32), sq_entries);
6835 if (sq_array_size == SIZE_MAX)
6836 return SIZE_MAX;
6837
6838 if (check_add_overflow(off, sq_array_size, &off))
6839 return SIZE_MAX;
6840
6841 if (sq_offset)
6842 *sq_offset = off;
6843
6844 return off;
6845}
6846
Jens Axboe2b188cc2019-01-07 10:46:33 -07006847static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6848{
Hristo Venev75b28af2019-08-26 17:23:46 +00006849 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006850
Hristo Venev75b28af2019-08-26 17:23:46 +00006851 pages = (size_t)1 << get_order(
6852 rings_size(sq_entries, cq_entries, NULL));
6853 pages += (size_t)1 << get_order(
6854 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006855
Hristo Venev75b28af2019-08-26 17:23:46 +00006856 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006857}
6858
Jens Axboeedafcce2019-01-09 09:16:05 -07006859static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6860{
6861 int i, j;
6862
6863 if (!ctx->user_bufs)
6864 return -ENXIO;
6865
6866 for (i = 0; i < ctx->nr_user_bufs; i++) {
6867 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6868
6869 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006870 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006871
6872 if (ctx->account_mem)
6873 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006874 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006875 imu->nr_bvecs = 0;
6876 }
6877
6878 kfree(ctx->user_bufs);
6879 ctx->user_bufs = NULL;
6880 ctx->nr_user_bufs = 0;
6881 return 0;
6882}
6883
6884static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6885 void __user *arg, unsigned index)
6886{
6887 struct iovec __user *src;
6888
6889#ifdef CONFIG_COMPAT
6890 if (ctx->compat) {
6891 struct compat_iovec __user *ciovs;
6892 struct compat_iovec ciov;
6893
6894 ciovs = (struct compat_iovec __user *) arg;
6895 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6896 return -EFAULT;
6897
Jens Axboed55e5f52019-12-11 16:12:15 -07006898 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006899 dst->iov_len = ciov.iov_len;
6900 return 0;
6901 }
6902#endif
6903 src = (struct iovec __user *) arg;
6904 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6905 return -EFAULT;
6906 return 0;
6907}
6908
6909static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6910 unsigned nr_args)
6911{
6912 struct vm_area_struct **vmas = NULL;
6913 struct page **pages = NULL;
6914 int i, j, got_pages = 0;
6915 int ret = -EINVAL;
6916
6917 if (ctx->user_bufs)
6918 return -EBUSY;
6919 if (!nr_args || nr_args > UIO_MAXIOV)
6920 return -EINVAL;
6921
6922 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6923 GFP_KERNEL);
6924 if (!ctx->user_bufs)
6925 return -ENOMEM;
6926
6927 for (i = 0; i < nr_args; i++) {
6928 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6929 unsigned long off, start, end, ubuf;
6930 int pret, nr_pages;
6931 struct iovec iov;
6932 size_t size;
6933
6934 ret = io_copy_iov(ctx, &iov, arg, i);
6935 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006936 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006937
6938 /*
6939 * Don't impose further limits on the size and buffer
6940 * constraints here, we'll -EINVAL later when IO is
6941 * submitted if they are wrong.
6942 */
6943 ret = -EFAULT;
6944 if (!iov.iov_base || !iov.iov_len)
6945 goto err;
6946
6947 /* arbitrary limit, but we need something */
6948 if (iov.iov_len > SZ_1G)
6949 goto err;
6950
6951 ubuf = (unsigned long) iov.iov_base;
6952 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6953 start = ubuf >> PAGE_SHIFT;
6954 nr_pages = end - start;
6955
6956 if (ctx->account_mem) {
6957 ret = io_account_mem(ctx->user, nr_pages);
6958 if (ret)
6959 goto err;
6960 }
6961
6962 ret = 0;
6963 if (!pages || nr_pages > got_pages) {
6964 kfree(vmas);
6965 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006966 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006967 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006968 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006969 sizeof(struct vm_area_struct *),
6970 GFP_KERNEL);
6971 if (!pages || !vmas) {
6972 ret = -ENOMEM;
6973 if (ctx->account_mem)
6974 io_unaccount_mem(ctx->user, nr_pages);
6975 goto err;
6976 }
6977 got_pages = nr_pages;
6978 }
6979
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006980 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006981 GFP_KERNEL);
6982 ret = -ENOMEM;
6983 if (!imu->bvec) {
6984 if (ctx->account_mem)
6985 io_unaccount_mem(ctx->user, nr_pages);
6986 goto err;
6987 }
6988
6989 ret = 0;
6990 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006991 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006992 FOLL_WRITE | FOLL_LONGTERM,
6993 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006994 if (pret == nr_pages) {
6995 /* don't support file backed memory */
6996 for (j = 0; j < nr_pages; j++) {
6997 struct vm_area_struct *vma = vmas[j];
6998
6999 if (vma->vm_file &&
7000 !is_file_hugepages(vma->vm_file)) {
7001 ret = -EOPNOTSUPP;
7002 break;
7003 }
7004 }
7005 } else {
7006 ret = pret < 0 ? pret : -EFAULT;
7007 }
7008 up_read(&current->mm->mmap_sem);
7009 if (ret) {
7010 /*
7011 * if we did partial map, or found file backed vmas,
7012 * release any pages we did get
7013 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007014 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007015 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007016 if (ctx->account_mem)
7017 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007018 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007019 goto err;
7020 }
7021
7022 off = ubuf & ~PAGE_MASK;
7023 size = iov.iov_len;
7024 for (j = 0; j < nr_pages; j++) {
7025 size_t vec_len;
7026
7027 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7028 imu->bvec[j].bv_page = pages[j];
7029 imu->bvec[j].bv_len = vec_len;
7030 imu->bvec[j].bv_offset = off;
7031 off = 0;
7032 size -= vec_len;
7033 }
7034 /* store original address for later verification */
7035 imu->ubuf = ubuf;
7036 imu->len = iov.iov_len;
7037 imu->nr_bvecs = nr_pages;
7038
7039 ctx->nr_user_bufs++;
7040 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007041 kvfree(pages);
7042 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007043 return 0;
7044err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007045 kvfree(pages);
7046 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007047 io_sqe_buffer_unregister(ctx);
7048 return ret;
7049}
7050
Jens Axboe9b402842019-04-11 11:45:41 -06007051static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7052{
7053 __s32 __user *fds = arg;
7054 int fd;
7055
7056 if (ctx->cq_ev_fd)
7057 return -EBUSY;
7058
7059 if (copy_from_user(&fd, fds, sizeof(*fds)))
7060 return -EFAULT;
7061
7062 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7063 if (IS_ERR(ctx->cq_ev_fd)) {
7064 int ret = PTR_ERR(ctx->cq_ev_fd);
7065 ctx->cq_ev_fd = NULL;
7066 return ret;
7067 }
7068
7069 return 0;
7070}
7071
7072static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7073{
7074 if (ctx->cq_ev_fd) {
7075 eventfd_ctx_put(ctx->cq_ev_fd);
7076 ctx->cq_ev_fd = NULL;
7077 return 0;
7078 }
7079
7080 return -ENXIO;
7081}
7082
Jens Axboe5a2e7452020-02-23 16:23:11 -07007083static int __io_destroy_buffers(int id, void *p, void *data)
7084{
7085 struct io_ring_ctx *ctx = data;
7086 struct io_buffer *buf = p;
7087
Jens Axboe067524e2020-03-02 16:32:28 -07007088 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007089 return 0;
7090}
7091
7092static void io_destroy_buffers(struct io_ring_ctx *ctx)
7093{
7094 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7095 idr_destroy(&ctx->io_buffer_idr);
7096}
7097
Jens Axboe2b188cc2019-01-07 10:46:33 -07007098static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7099{
Jens Axboe6b063142019-01-10 22:13:58 -07007100 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007101 if (ctx->sqo_mm)
7102 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007103
7104 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007105 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007106 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007107 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007108 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007109 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007110
Jens Axboe2b188cc2019-01-07 10:46:33 -07007111#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007112 if (ctx->ring_sock) {
7113 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007114 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007115 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007116#endif
7117
Hristo Venev75b28af2019-08-26 17:23:46 +00007118 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007119 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007120
7121 percpu_ref_exit(&ctx->refs);
7122 if (ctx->account_mem)
7123 io_unaccount_mem(ctx->user,
7124 ring_pages(ctx->sq_entries, ctx->cq_entries));
7125 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007126 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07007127 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07007128 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007129 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007130 kfree(ctx);
7131}
7132
7133static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7134{
7135 struct io_ring_ctx *ctx = file->private_data;
7136 __poll_t mask = 0;
7137
7138 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007139 /*
7140 * synchronizes with barrier from wq_has_sleeper call in
7141 * io_commit_cqring
7142 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007143 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007144 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7145 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007146 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007147 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007148 mask |= EPOLLIN | EPOLLRDNORM;
7149
7150 return mask;
7151}
7152
7153static int io_uring_fasync(int fd, struct file *file, int on)
7154{
7155 struct io_ring_ctx *ctx = file->private_data;
7156
7157 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7158}
7159
Jens Axboe071698e2020-01-28 10:04:42 -07007160static int io_remove_personalities(int id, void *p, void *data)
7161{
7162 struct io_ring_ctx *ctx = data;
7163 const struct cred *cred;
7164
7165 cred = idr_remove(&ctx->personality_idr, id);
7166 if (cred)
7167 put_cred(cred);
7168 return 0;
7169}
7170
Jens Axboe2b188cc2019-01-07 10:46:33 -07007171static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7172{
7173 mutex_lock(&ctx->uring_lock);
7174 percpu_ref_kill(&ctx->refs);
7175 mutex_unlock(&ctx->uring_lock);
7176
Jens Axboedf069d82020-02-04 16:48:34 -07007177 /*
7178 * Wait for sq thread to idle, if we have one. It won't spin on new
7179 * work after we've killed the ctx ref above. This is important to do
7180 * before we cancel existing commands, as the thread could otherwise
7181 * be queueing new work post that. If that's work we need to cancel,
7182 * it could cause shutdown to hang.
7183 */
7184 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
7185 cpu_relax();
7186
Jens Axboe5262f562019-09-17 12:26:57 -06007187 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007188 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007189
7190 if (ctx->io_wq)
7191 io_wq_cancel_all(ctx->io_wq);
7192
Jens Axboedef596e2019-01-09 08:59:42 -07007193 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007194 /* if we failed setting up the ctx, we might not have any rings */
7195 if (ctx->rings)
7196 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007197 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07007198 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007199 io_ring_ctx_free(ctx);
7200}
7201
7202static int io_uring_release(struct inode *inode, struct file *file)
7203{
7204 struct io_ring_ctx *ctx = file->private_data;
7205
7206 file->private_data = NULL;
7207 io_ring_ctx_wait_and_kill(ctx);
7208 return 0;
7209}
7210
Jens Axboefcb323c2019-10-24 12:39:47 -06007211static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7212 struct files_struct *files)
7213{
7214 struct io_kiocb *req;
7215 DEFINE_WAIT(wait);
7216
7217 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07007218 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06007219
7220 spin_lock_irq(&ctx->inflight_lock);
7221 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007222 if (req->work.files != files)
7223 continue;
7224 /* req is being completed, ignore */
7225 if (!refcount_inc_not_zero(&req->refs))
7226 continue;
7227 cancel_req = req;
7228 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007229 }
Jens Axboe768134d2019-11-10 20:30:53 -07007230 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007231 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007232 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007233 spin_unlock_irq(&ctx->inflight_lock);
7234
Jens Axboe768134d2019-11-10 20:30:53 -07007235 /* We need to keep going until we don't find a matching req */
7236 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007237 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007238
Jens Axboe2ca10252020-02-13 17:17:35 -07007239 if (cancel_req->flags & REQ_F_OVERFLOW) {
7240 spin_lock_irq(&ctx->completion_lock);
7241 list_del(&cancel_req->list);
7242 cancel_req->flags &= ~REQ_F_OVERFLOW;
7243 if (list_empty(&ctx->cq_overflow_list)) {
7244 clear_bit(0, &ctx->sq_check_overflow);
7245 clear_bit(0, &ctx->cq_check_overflow);
7246 }
7247 spin_unlock_irq(&ctx->completion_lock);
7248
7249 WRITE_ONCE(ctx->rings->cq_overflow,
7250 atomic_inc_return(&ctx->cached_cq_overflow));
7251
7252 /*
7253 * Put inflight ref and overflow ref. If that's
7254 * all we had, then we're done with this request.
7255 */
7256 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7257 io_put_req(cancel_req);
7258 continue;
7259 }
7260 }
7261
Bob Liu2f6d9b92019-11-13 18:06:24 +08007262 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7263 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007264 schedule();
7265 }
Jens Axboe768134d2019-11-10 20:30:53 -07007266 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007267}
7268
7269static int io_uring_flush(struct file *file, void *data)
7270{
7271 struct io_ring_ctx *ctx = file->private_data;
7272
7273 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007274
7275 /*
7276 * If the task is going away, cancel work it may have pending
7277 */
7278 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7279 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7280
Jens Axboefcb323c2019-10-24 12:39:47 -06007281 return 0;
7282}
7283
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007284static void *io_uring_validate_mmap_request(struct file *file,
7285 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007286{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007287 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007288 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007289 struct page *page;
7290 void *ptr;
7291
7292 switch (offset) {
7293 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007294 case IORING_OFF_CQ_RING:
7295 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007296 break;
7297 case IORING_OFF_SQES:
7298 ptr = ctx->sq_sqes;
7299 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007300 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007301 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007302 }
7303
7304 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007305 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007306 return ERR_PTR(-EINVAL);
7307
7308 return ptr;
7309}
7310
7311#ifdef CONFIG_MMU
7312
7313static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7314{
7315 size_t sz = vma->vm_end - vma->vm_start;
7316 unsigned long pfn;
7317 void *ptr;
7318
7319 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7320 if (IS_ERR(ptr))
7321 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007322
7323 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7324 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7325}
7326
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007327#else /* !CONFIG_MMU */
7328
7329static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7330{
7331 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7332}
7333
7334static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7335{
7336 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7337}
7338
7339static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7340 unsigned long addr, unsigned long len,
7341 unsigned long pgoff, unsigned long flags)
7342{
7343 void *ptr;
7344
7345 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7346 if (IS_ERR(ptr))
7347 return PTR_ERR(ptr);
7348
7349 return (unsigned long) ptr;
7350}
7351
7352#endif /* !CONFIG_MMU */
7353
Jens Axboe2b188cc2019-01-07 10:46:33 -07007354SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7355 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7356 size_t, sigsz)
7357{
7358 struct io_ring_ctx *ctx;
7359 long ret = -EBADF;
7360 int submitted = 0;
7361 struct fd f;
7362
Jens Axboeb41e9852020-02-17 09:52:41 -07007363 if (current->task_works)
7364 task_work_run();
7365
Jens Axboe6c271ce2019-01-10 11:22:30 -07007366 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007367 return -EINVAL;
7368
7369 f = fdget(fd);
7370 if (!f.file)
7371 return -EBADF;
7372
7373 ret = -EOPNOTSUPP;
7374 if (f.file->f_op != &io_uring_fops)
7375 goto out_fput;
7376
7377 ret = -ENXIO;
7378 ctx = f.file->private_data;
7379 if (!percpu_ref_tryget(&ctx->refs))
7380 goto out_fput;
7381
Jens Axboe6c271ce2019-01-10 11:22:30 -07007382 /*
7383 * For SQ polling, the thread will do all submissions and completions.
7384 * Just return the requested submit count, and wake the thread if
7385 * we were asked to.
7386 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007387 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007388 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007389 if (!list_empty_careful(&ctx->cq_overflow_list))
7390 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007391 if (flags & IORING_ENTER_SQ_WAKEUP)
7392 wake_up(&ctx->sqo_wait);
7393 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007394 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007395 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007396
7397 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007398 /* already have mm, so io_submit_sqes() won't try to grab it */
7399 cur_mm = ctx->sqo_mm;
7400 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
7401 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007402 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007403
7404 if (submitted != to_submit)
7405 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007406 }
7407 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007408 unsigned nr_events = 0;
7409
Jens Axboe2b188cc2019-01-07 10:46:33 -07007410 min_complete = min(min_complete, ctx->cq_entries);
7411
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007412 /*
7413 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7414 * space applications don't need to do io completion events
7415 * polling again, they can rely on io_sq_thread to do polling
7416 * work, which can reduce cpu usage and uring_lock contention.
7417 */
7418 if (ctx->flags & IORING_SETUP_IOPOLL &&
7419 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007420 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007421 } else {
7422 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7423 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007424 }
7425
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007426out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007427 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007428out_fput:
7429 fdput(f);
7430 return submitted ? submitted : ret;
7431}
7432
Tobias Klauserbebdb652020-02-26 18:38:32 +01007433#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007434static int io_uring_show_cred(int id, void *p, void *data)
7435{
7436 const struct cred *cred = p;
7437 struct seq_file *m = data;
7438 struct user_namespace *uns = seq_user_ns(m);
7439 struct group_info *gi;
7440 kernel_cap_t cap;
7441 unsigned __capi;
7442 int g;
7443
7444 seq_printf(m, "%5d\n", id);
7445 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7446 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7447 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7448 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7449 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7450 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7451 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7452 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7453 seq_puts(m, "\n\tGroups:\t");
7454 gi = cred->group_info;
7455 for (g = 0; g < gi->ngroups; g++) {
7456 seq_put_decimal_ull(m, g ? " " : "",
7457 from_kgid_munged(uns, gi->gid[g]));
7458 }
7459 seq_puts(m, "\n\tCapEff:\t");
7460 cap = cred->cap_effective;
7461 CAP_FOR_EACH_U32(__capi)
7462 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7463 seq_putc(m, '\n');
7464 return 0;
7465}
7466
7467static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7468{
7469 int i;
7470
7471 mutex_lock(&ctx->uring_lock);
7472 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7473 for (i = 0; i < ctx->nr_user_files; i++) {
7474 struct fixed_file_table *table;
7475 struct file *f;
7476
7477 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7478 f = table->files[i & IORING_FILE_TABLE_MASK];
7479 if (f)
7480 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7481 else
7482 seq_printf(m, "%5u: <none>\n", i);
7483 }
7484 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7485 for (i = 0; i < ctx->nr_user_bufs; i++) {
7486 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7487
7488 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7489 (unsigned int) buf->len);
7490 }
7491 if (!idr_is_empty(&ctx->personality_idr)) {
7492 seq_printf(m, "Personalities:\n");
7493 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7494 }
Jens Axboed7718a92020-02-14 22:23:12 -07007495 seq_printf(m, "PollList:\n");
7496 spin_lock_irq(&ctx->completion_lock);
7497 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7498 struct hlist_head *list = &ctx->cancel_hash[i];
7499 struct io_kiocb *req;
7500
7501 hlist_for_each_entry(req, list, hash_node)
7502 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7503 req->task->task_works != NULL);
7504 }
7505 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007506 mutex_unlock(&ctx->uring_lock);
7507}
7508
7509static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7510{
7511 struct io_ring_ctx *ctx = f->private_data;
7512
7513 if (percpu_ref_tryget(&ctx->refs)) {
7514 __io_uring_show_fdinfo(ctx, m);
7515 percpu_ref_put(&ctx->refs);
7516 }
7517}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007518#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007519
Jens Axboe2b188cc2019-01-07 10:46:33 -07007520static const struct file_operations io_uring_fops = {
7521 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007522 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007523 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007524#ifndef CONFIG_MMU
7525 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7526 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7527#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007528 .poll = io_uring_poll,
7529 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007530#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007531 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007532#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007533};
7534
7535static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7536 struct io_uring_params *p)
7537{
Hristo Venev75b28af2019-08-26 17:23:46 +00007538 struct io_rings *rings;
7539 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007540
Hristo Venev75b28af2019-08-26 17:23:46 +00007541 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7542 if (size == SIZE_MAX)
7543 return -EOVERFLOW;
7544
7545 rings = io_mem_alloc(size);
7546 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007547 return -ENOMEM;
7548
Hristo Venev75b28af2019-08-26 17:23:46 +00007549 ctx->rings = rings;
7550 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7551 rings->sq_ring_mask = p->sq_entries - 1;
7552 rings->cq_ring_mask = p->cq_entries - 1;
7553 rings->sq_ring_entries = p->sq_entries;
7554 rings->cq_ring_entries = p->cq_entries;
7555 ctx->sq_mask = rings->sq_ring_mask;
7556 ctx->cq_mask = rings->cq_ring_mask;
7557 ctx->sq_entries = rings->sq_ring_entries;
7558 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007559
7560 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007561 if (size == SIZE_MAX) {
7562 io_mem_free(ctx->rings);
7563 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007564 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007565 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007566
7567 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007568 if (!ctx->sq_sqes) {
7569 io_mem_free(ctx->rings);
7570 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007571 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007572 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007573
Jens Axboe2b188cc2019-01-07 10:46:33 -07007574 return 0;
7575}
7576
7577/*
7578 * Allocate an anonymous fd, this is what constitutes the application
7579 * visible backing of an io_uring instance. The application mmaps this
7580 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7581 * we have to tie this fd to a socket for file garbage collection purposes.
7582 */
7583static int io_uring_get_fd(struct io_ring_ctx *ctx)
7584{
7585 struct file *file;
7586 int ret;
7587
7588#if defined(CONFIG_UNIX)
7589 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7590 &ctx->ring_sock);
7591 if (ret)
7592 return ret;
7593#endif
7594
7595 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7596 if (ret < 0)
7597 goto err;
7598
7599 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7600 O_RDWR | O_CLOEXEC);
7601 if (IS_ERR(file)) {
7602 put_unused_fd(ret);
7603 ret = PTR_ERR(file);
7604 goto err;
7605 }
7606
7607#if defined(CONFIG_UNIX)
7608 ctx->ring_sock->file = file;
7609#endif
7610 fd_install(ret, file);
7611 return ret;
7612err:
7613#if defined(CONFIG_UNIX)
7614 sock_release(ctx->ring_sock);
7615 ctx->ring_sock = NULL;
7616#endif
7617 return ret;
7618}
7619
7620static int io_uring_create(unsigned entries, struct io_uring_params *p)
7621{
7622 struct user_struct *user = NULL;
7623 struct io_ring_ctx *ctx;
7624 bool account_mem;
7625 int ret;
7626
Jens Axboe8110c1a2019-12-28 15:39:54 -07007627 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007628 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007629 if (entries > IORING_MAX_ENTRIES) {
7630 if (!(p->flags & IORING_SETUP_CLAMP))
7631 return -EINVAL;
7632 entries = IORING_MAX_ENTRIES;
7633 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007634
7635 /*
7636 * Use twice as many entries for the CQ ring. It's possible for the
7637 * application to drive a higher depth than the size of the SQ ring,
7638 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007639 * some flexibility in overcommitting a bit. If the application has
7640 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7641 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007642 */
7643 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007644 if (p->flags & IORING_SETUP_CQSIZE) {
7645 /*
7646 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7647 * to a power-of-two, if it isn't already. We do NOT impose
7648 * any cq vs sq ring sizing.
7649 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007650 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007651 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007652 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7653 if (!(p->flags & IORING_SETUP_CLAMP))
7654 return -EINVAL;
7655 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7656 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007657 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7658 } else {
7659 p->cq_entries = 2 * p->sq_entries;
7660 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007661
7662 user = get_uid(current_user());
7663 account_mem = !capable(CAP_IPC_LOCK);
7664
7665 if (account_mem) {
7666 ret = io_account_mem(user,
7667 ring_pages(p->sq_entries, p->cq_entries));
7668 if (ret) {
7669 free_uid(user);
7670 return ret;
7671 }
7672 }
7673
7674 ctx = io_ring_ctx_alloc(p);
7675 if (!ctx) {
7676 if (account_mem)
7677 io_unaccount_mem(user, ring_pages(p->sq_entries,
7678 p->cq_entries));
7679 free_uid(user);
7680 return -ENOMEM;
7681 }
7682 ctx->compat = in_compat_syscall();
7683 ctx->account_mem = account_mem;
7684 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007685 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007686
7687 ret = io_allocate_scq_urings(ctx, p);
7688 if (ret)
7689 goto err;
7690
Jens Axboe6c271ce2019-01-10 11:22:30 -07007691 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007692 if (ret)
7693 goto err;
7694
Jens Axboe2b188cc2019-01-07 10:46:33 -07007695 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007696 p->sq_off.head = offsetof(struct io_rings, sq.head);
7697 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7698 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7699 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7700 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7701 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7702 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007703
7704 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007705 p->cq_off.head = offsetof(struct io_rings, cq.head);
7706 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7707 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7708 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7709 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7710 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007711
Jens Axboe044c1ab2019-10-28 09:15:33 -06007712 /*
7713 * Install ring fd as the very last thing, so we don't risk someone
7714 * having closed it before we finish setup
7715 */
7716 ret = io_uring_get_fd(ctx);
7717 if (ret < 0)
7718 goto err;
7719
Jens Axboeda8c9692019-12-02 18:51:26 -07007720 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007721 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007722 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007723 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007724 return ret;
7725err:
7726 io_ring_ctx_wait_and_kill(ctx);
7727 return ret;
7728}
7729
7730/*
7731 * Sets up an aio uring context, and returns the fd. Applications asks for a
7732 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7733 * params structure passed in.
7734 */
7735static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7736{
7737 struct io_uring_params p;
7738 long ret;
7739 int i;
7740
7741 if (copy_from_user(&p, params, sizeof(p)))
7742 return -EFAULT;
7743 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7744 if (p.resv[i])
7745 return -EINVAL;
7746 }
7747
Jens Axboe6c271ce2019-01-10 11:22:30 -07007748 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007749 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007750 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007751 return -EINVAL;
7752
7753 ret = io_uring_create(entries, &p);
7754 if (ret < 0)
7755 return ret;
7756
7757 if (copy_to_user(params, &p, sizeof(p)))
7758 return -EFAULT;
7759
7760 return ret;
7761}
7762
7763SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7764 struct io_uring_params __user *, params)
7765{
7766 return io_uring_setup(entries, params);
7767}
7768
Jens Axboe66f4af92020-01-16 15:36:52 -07007769static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7770{
7771 struct io_uring_probe *p;
7772 size_t size;
7773 int i, ret;
7774
7775 size = struct_size(p, ops, nr_args);
7776 if (size == SIZE_MAX)
7777 return -EOVERFLOW;
7778 p = kzalloc(size, GFP_KERNEL);
7779 if (!p)
7780 return -ENOMEM;
7781
7782 ret = -EFAULT;
7783 if (copy_from_user(p, arg, size))
7784 goto out;
7785 ret = -EINVAL;
7786 if (memchr_inv(p, 0, size))
7787 goto out;
7788
7789 p->last_op = IORING_OP_LAST - 1;
7790 if (nr_args > IORING_OP_LAST)
7791 nr_args = IORING_OP_LAST;
7792
7793 for (i = 0; i < nr_args; i++) {
7794 p->ops[i].op = i;
7795 if (!io_op_defs[i].not_supported)
7796 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7797 }
7798 p->ops_len = i;
7799
7800 ret = 0;
7801 if (copy_to_user(arg, p, size))
7802 ret = -EFAULT;
7803out:
7804 kfree(p);
7805 return ret;
7806}
7807
Jens Axboe071698e2020-01-28 10:04:42 -07007808static int io_register_personality(struct io_ring_ctx *ctx)
7809{
7810 const struct cred *creds = get_current_cred();
7811 int id;
7812
7813 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7814 USHRT_MAX, GFP_KERNEL);
7815 if (id < 0)
7816 put_cred(creds);
7817 return id;
7818}
7819
7820static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7821{
7822 const struct cred *old_creds;
7823
7824 old_creds = idr_remove(&ctx->personality_idr, id);
7825 if (old_creds) {
7826 put_cred(old_creds);
7827 return 0;
7828 }
7829
7830 return -EINVAL;
7831}
7832
7833static bool io_register_op_must_quiesce(int op)
7834{
7835 switch (op) {
7836 case IORING_UNREGISTER_FILES:
7837 case IORING_REGISTER_FILES_UPDATE:
7838 case IORING_REGISTER_PROBE:
7839 case IORING_REGISTER_PERSONALITY:
7840 case IORING_UNREGISTER_PERSONALITY:
7841 return false;
7842 default:
7843 return true;
7844 }
7845}
7846
Jens Axboeedafcce2019-01-09 09:16:05 -07007847static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7848 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007849 __releases(ctx->uring_lock)
7850 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007851{
7852 int ret;
7853
Jens Axboe35fa71a2019-04-22 10:23:23 -06007854 /*
7855 * We're inside the ring mutex, if the ref is already dying, then
7856 * someone else killed the ctx or is already going through
7857 * io_uring_register().
7858 */
7859 if (percpu_ref_is_dying(&ctx->refs))
7860 return -ENXIO;
7861
Jens Axboe071698e2020-01-28 10:04:42 -07007862 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007863 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007864
Jens Axboe05f3fb32019-12-09 11:22:50 -07007865 /*
7866 * Drop uring mutex before waiting for references to exit. If
7867 * another thread is currently inside io_uring_enter() it might
7868 * need to grab the uring_lock to make progress. If we hold it
7869 * here across the drain wait, then we can deadlock. It's safe
7870 * to drop the mutex here, since no new references will come in
7871 * after we've killed the percpu ref.
7872 */
7873 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007874 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007875 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007876 if (ret) {
7877 percpu_ref_resurrect(&ctx->refs);
7878 ret = -EINTR;
7879 goto out;
7880 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007881 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007882
7883 switch (opcode) {
7884 case IORING_REGISTER_BUFFERS:
7885 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7886 break;
7887 case IORING_UNREGISTER_BUFFERS:
7888 ret = -EINVAL;
7889 if (arg || nr_args)
7890 break;
7891 ret = io_sqe_buffer_unregister(ctx);
7892 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007893 case IORING_REGISTER_FILES:
7894 ret = io_sqe_files_register(ctx, arg, nr_args);
7895 break;
7896 case IORING_UNREGISTER_FILES:
7897 ret = -EINVAL;
7898 if (arg || nr_args)
7899 break;
7900 ret = io_sqe_files_unregister(ctx);
7901 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007902 case IORING_REGISTER_FILES_UPDATE:
7903 ret = io_sqe_files_update(ctx, arg, nr_args);
7904 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007905 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007906 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007907 ret = -EINVAL;
7908 if (nr_args != 1)
7909 break;
7910 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007911 if (ret)
7912 break;
7913 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7914 ctx->eventfd_async = 1;
7915 else
7916 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007917 break;
7918 case IORING_UNREGISTER_EVENTFD:
7919 ret = -EINVAL;
7920 if (arg || nr_args)
7921 break;
7922 ret = io_eventfd_unregister(ctx);
7923 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007924 case IORING_REGISTER_PROBE:
7925 ret = -EINVAL;
7926 if (!arg || nr_args > 256)
7927 break;
7928 ret = io_probe(ctx, arg, nr_args);
7929 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007930 case IORING_REGISTER_PERSONALITY:
7931 ret = -EINVAL;
7932 if (arg || nr_args)
7933 break;
7934 ret = io_register_personality(ctx);
7935 break;
7936 case IORING_UNREGISTER_PERSONALITY:
7937 ret = -EINVAL;
7938 if (arg)
7939 break;
7940 ret = io_unregister_personality(ctx, nr_args);
7941 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007942 default:
7943 ret = -EINVAL;
7944 break;
7945 }
7946
Jens Axboe071698e2020-01-28 10:04:42 -07007947 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007948 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007949 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007950out:
7951 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007952 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007953 return ret;
7954}
7955
7956SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7957 void __user *, arg, unsigned int, nr_args)
7958{
7959 struct io_ring_ctx *ctx;
7960 long ret = -EBADF;
7961 struct fd f;
7962
7963 f = fdget(fd);
7964 if (!f.file)
7965 return -EBADF;
7966
7967 ret = -EOPNOTSUPP;
7968 if (f.file->f_op != &io_uring_fops)
7969 goto out_fput;
7970
7971 ctx = f.file->private_data;
7972
7973 mutex_lock(&ctx->uring_lock);
7974 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7975 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007976 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7977 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007978out_fput:
7979 fdput(f);
7980 return ret;
7981}
7982
Jens Axboe2b188cc2019-01-07 10:46:33 -07007983static int __init io_uring_init(void)
7984{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007985#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7986 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7987 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7988} while (0)
7989
7990#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7991 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7992 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7993 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7994 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7995 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7996 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7997 BUILD_BUG_SQE_ELEM(8, __u64, off);
7998 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7999 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008000 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008001 BUILD_BUG_SQE_ELEM(24, __u32, len);
8002 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8003 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8004 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8005 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8006 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8007 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8008 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8009 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8010 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8011 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8012 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8013 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8014 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008015 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008016 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8017 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8018 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008019 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008020
Jens Axboed3656342019-12-18 09:50:26 -07008021 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008022 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008023 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8024 return 0;
8025};
8026__initcall(io_uring_init);