blob: 487e2742a9e87e0afcdeba65ac33a00a3165af72 [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 Axboe4ed734b2020-03-20 11:23:41 -0600607 union {
608 struct task_struct *task;
609 unsigned long fsize;
610 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700611 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600612 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600613 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700614
Jens Axboed7718a92020-02-14 22:23:12 -0700615 struct list_head link_list;
616
Jens Axboefcb323c2019-10-24 12:39:47 -0600617 struct list_head inflight_entry;
618
Jens Axboeb41e9852020-02-17 09:52:41 -0700619 union {
620 /*
621 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700622 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
623 * async armed poll handlers for regular commands. The latter
624 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700625 */
626 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700627 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700628 struct hlist_node hash_node;
629 struct async_poll *apoll;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700630 int cflags;
Jens Axboeb41e9852020-02-17 09:52:41 -0700631 };
632 struct io_wq_work work;
633 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700634};
635
636#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700637#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638
Jens Axboe9a56a232019-01-09 09:06:50 -0700639struct io_submit_state {
640 struct blk_plug plug;
641
642 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700643 * io_kiocb alloc cache
644 */
645 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300646 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700647
648 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700649 * File reference cache
650 */
651 struct file *file;
652 unsigned int fd;
653 unsigned int has_refs;
654 unsigned int used_refs;
655 unsigned int ios_left;
656};
657
Jens Axboed3656342019-12-18 09:50:26 -0700658struct io_op_def {
659 /* needs req->io allocated for deferral/async */
660 unsigned async_ctx : 1;
661 /* needs current->mm setup, does mm access */
662 unsigned needs_mm : 1;
663 /* needs req->file assigned */
664 unsigned needs_file : 1;
665 /* needs req->file assigned IFF fd is >= 0 */
666 unsigned fd_non_neg : 1;
667 /* hash wq insertion if file is a regular file */
668 unsigned hash_reg_file : 1;
669 /* unbound wq insertion if file is a non-regular file */
670 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700671 /* opcode is not supported by this kernel */
672 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700673 /* needs file table */
674 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700675 /* needs ->fs */
676 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700677 /* set if opcode supports polled "wait" */
678 unsigned pollin : 1;
679 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700680 /* op supports buffer selection */
681 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700682};
683
684static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300685 [IORING_OP_NOP] = {},
686 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700687 .async_ctx = 1,
688 .needs_mm = 1,
689 .needs_file = 1,
690 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700691 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700692 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700693 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300694 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700695 .async_ctx = 1,
696 .needs_mm = 1,
697 .needs_file = 1,
698 .hash_reg_file = 1,
699 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700700 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700701 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300702 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700703 .needs_file = 1,
704 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300705 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700706 .needs_file = 1,
707 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700708 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700709 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300710 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700711 .needs_file = 1,
712 .hash_reg_file = 1,
713 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700714 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700715 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300716 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700717 .needs_file = 1,
718 .unbound_nonreg_file = 1,
719 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300720 [IORING_OP_POLL_REMOVE] = {},
721 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700722 .needs_file = 1,
723 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300724 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700725 .async_ctx = 1,
726 .needs_mm = 1,
727 .needs_file = 1,
728 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700729 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700730 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700731 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300732 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700733 .async_ctx = 1,
734 .needs_mm = 1,
735 .needs_file = 1,
736 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700737 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700738 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700739 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700740 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300741 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700742 .async_ctx = 1,
743 .needs_mm = 1,
744 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300745 [IORING_OP_TIMEOUT_REMOVE] = {},
746 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700747 .needs_mm = 1,
748 .needs_file = 1,
749 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700750 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700751 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700752 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300753 [IORING_OP_ASYNC_CANCEL] = {},
754 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700755 .async_ctx = 1,
756 .needs_mm = 1,
757 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300758 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700759 .async_ctx = 1,
760 .needs_mm = 1,
761 .needs_file = 1,
762 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700763 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700764 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300765 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700766 .needs_file = 1,
767 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300768 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700769 .needs_file = 1,
770 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700771 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700772 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700776 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700777 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300778 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700779 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700780 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700781 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300782 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700783 .needs_mm = 1,
784 .needs_file = 1,
785 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700786 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700787 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300788 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700789 .needs_mm = 1,
790 .needs_file = 1,
791 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700792 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700793 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700796 .needs_mm = 1,
797 .needs_file = 1,
798 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700799 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700802 .needs_file = 1,
803 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300804 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700805 .needs_mm = 1,
806 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300807 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700808 .needs_mm = 1,
809 .needs_file = 1,
810 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700811 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700812 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300813 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700814 .needs_mm = 1,
815 .needs_file = 1,
816 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700817 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700818 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700819 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300820 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700821 .needs_file = 1,
822 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700823 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700824 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700825 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700826 [IORING_OP_EPOLL_CTL] = {
827 .unbound_nonreg_file = 1,
828 .file_table = 1,
829 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300830 [IORING_OP_SPLICE] = {
831 .needs_file = 1,
832 .hash_reg_file = 1,
833 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700834 },
835 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700836 [IORING_OP_REMOVE_BUFFERS] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700837};
838
Jens Axboe561fb042019-10-24 07:25:42 -0600839static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700840static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800841static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700842static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700843static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
844static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700845static int __io_sqe_files_update(struct io_ring_ctx *ctx,
846 struct io_uring_files_update *ip,
847 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700848static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700849static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300850static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700851static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
852 int fd, struct file **out_file, bool fixed);
853static void __io_queue_sqe(struct io_kiocb *req,
854 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600855
Jens Axboe2b188cc2019-01-07 10:46:33 -0700856static struct kmem_cache *req_cachep;
857
858static const struct file_operations io_uring_fops;
859
860struct sock *io_uring_get_socket(struct file *file)
861{
862#if defined(CONFIG_UNIX)
863 if (file->f_op == &io_uring_fops) {
864 struct io_ring_ctx *ctx = file->private_data;
865
866 return ctx->ring_sock->sk;
867 }
868#endif
869 return NULL;
870}
871EXPORT_SYMBOL(io_uring_get_socket);
872
873static void io_ring_ctx_ref_free(struct percpu_ref *ref)
874{
875 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
876
Jens Axboe206aefd2019-11-07 18:27:42 -0700877 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700878}
879
880static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
881{
882 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700883 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700884
885 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
886 if (!ctx)
887 return NULL;
888
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700889 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
890 if (!ctx->fallback_req)
891 goto err;
892
Jens Axboe206aefd2019-11-07 18:27:42 -0700893 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
894 if (!ctx->completions)
895 goto err;
896
Jens Axboe78076bb2019-12-04 19:56:40 -0700897 /*
898 * Use 5 bits less than the max cq entries, that should give us around
899 * 32 entries per hash list if totally full and uniformly spread.
900 */
901 hash_bits = ilog2(p->cq_entries);
902 hash_bits -= 5;
903 if (hash_bits <= 0)
904 hash_bits = 1;
905 ctx->cancel_hash_bits = hash_bits;
906 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
907 GFP_KERNEL);
908 if (!ctx->cancel_hash)
909 goto err;
910 __hash_init(ctx->cancel_hash, 1U << hash_bits);
911
Roman Gushchin21482892019-05-07 10:01:48 -0700912 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700913 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
914 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700915
916 ctx->flags = p->flags;
917 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700918 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700919 init_completion(&ctx->completions[0]);
920 init_completion(&ctx->completions[1]);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700921 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700922 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700923 mutex_init(&ctx->uring_lock);
924 init_waitqueue_head(&ctx->wait);
925 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700926 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600927 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600928 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600929 init_waitqueue_head(&ctx->inflight_wait);
930 spin_lock_init(&ctx->inflight_lock);
931 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700932 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700933err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700934 if (ctx->fallback_req)
935 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700936 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700937 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700938 kfree(ctx);
939 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700940}
941
Bob Liu9d858b22019-11-13 18:06:25 +0800942static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600943{
Jackie Liua197f662019-11-08 08:09:12 -0700944 struct io_ring_ctx *ctx = req->ctx;
945
Jens Axboe498ccd92019-10-25 10:04:25 -0600946 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
947 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600948}
949
Bob Liu9d858b22019-11-13 18:06:25 +0800950static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600951{
Pavel Begunkov87987892020-01-18 01:22:30 +0300952 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800953 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600954
Bob Liu9d858b22019-11-13 18:06:25 +0800955 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600956}
957
958static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600959{
960 struct io_kiocb *req;
961
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600962 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800963 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600964 list_del_init(&req->list);
965 return req;
966 }
967
968 return NULL;
969}
970
Jens Axboe5262f562019-09-17 12:26:57 -0600971static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
972{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600973 struct io_kiocb *req;
974
975 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700976 if (req) {
977 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
978 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800979 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700980 list_del_init(&req->list);
981 return req;
982 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600983 }
984
985 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600986}
987
Jens Axboede0617e2019-04-06 21:51:27 -0600988static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989{
Hristo Venev75b28af2019-08-26 17:23:46 +0000990 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700991
Pavel Begunkov07910152020-01-17 03:52:46 +0300992 /* order cqe stores with ring update */
993 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700994
Pavel Begunkov07910152020-01-17 03:52:46 +0300995 if (wq_has_sleeper(&ctx->cq_wait)) {
996 wake_up_interruptible(&ctx->cq_wait);
997 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700998 }
999}
1000
Jens Axboecccf0ee2020-01-27 16:34:48 -07001001static inline void io_req_work_grab_env(struct io_kiocb *req,
1002 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001003{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001004 if (!req->work.mm && def->needs_mm) {
1005 mmgrab(current->mm);
1006 req->work.mm = current->mm;
1007 }
1008 if (!req->work.creds)
1009 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001010 if (!req->work.fs && def->needs_fs) {
1011 spin_lock(&current->fs->lock);
1012 if (!current->fs->in_exec) {
1013 req->work.fs = current->fs;
1014 req->work.fs->users++;
1015 } else {
1016 req->work.flags |= IO_WQ_WORK_CANCEL;
1017 }
1018 spin_unlock(&current->fs->lock);
1019 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001020 if (!req->work.task_pid)
1021 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001022}
1023
1024static inline void io_req_work_drop_env(struct io_kiocb *req)
1025{
1026 if (req->work.mm) {
1027 mmdrop(req->work.mm);
1028 req->work.mm = NULL;
1029 }
1030 if (req->work.creds) {
1031 put_cred(req->work.creds);
1032 req->work.creds = NULL;
1033 }
Jens Axboeff002b32020-02-07 16:05:21 -07001034 if (req->work.fs) {
1035 struct fs_struct *fs = req->work.fs;
1036
1037 spin_lock(&req->work.fs->lock);
1038 if (--fs->users)
1039 fs = NULL;
1040 spin_unlock(&req->work.fs->lock);
1041 if (fs)
1042 free_fs_struct(fs);
1043 }
Jens Axboe561fb042019-10-24 07:25:42 -06001044}
1045
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001046static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001047 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001048{
Jens Axboed3656342019-12-18 09:50:26 -07001049 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001050
Jens Axboed3656342019-12-18 09:50:26 -07001051 if (req->flags & REQ_F_ISREG) {
1052 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001053 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001054 } else {
1055 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001056 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001057 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001058
1059 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001060
Jens Axboe94ae5e72019-11-14 19:39:52 -07001061 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001062}
1063
Jackie Liua197f662019-11-08 08:09:12 -07001064static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001065{
Jackie Liua197f662019-11-08 08:09:12 -07001066 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001067 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001068
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001069 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001070
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001071 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1072 &req->work, req->flags);
1073 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001074
1075 if (link)
1076 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001077}
1078
Jens Axboe5262f562019-09-17 12:26:57 -06001079static void io_kill_timeout(struct io_kiocb *req)
1080{
1081 int ret;
1082
Jens Axboe2d283902019-12-04 11:08:05 -07001083 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001084 if (ret != -1) {
1085 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001086 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001087 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001088 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001089 }
1090}
1091
1092static void io_kill_timeouts(struct io_ring_ctx *ctx)
1093{
1094 struct io_kiocb *req, *tmp;
1095
1096 spin_lock_irq(&ctx->completion_lock);
1097 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1098 io_kill_timeout(req);
1099 spin_unlock_irq(&ctx->completion_lock);
1100}
1101
Jens Axboede0617e2019-04-06 21:51:27 -06001102static void io_commit_cqring(struct io_ring_ctx *ctx)
1103{
1104 struct io_kiocb *req;
1105
Jens Axboe5262f562019-09-17 12:26:57 -06001106 while ((req = io_get_timeout_req(ctx)) != NULL)
1107 io_kill_timeout(req);
1108
Jens Axboede0617e2019-04-06 21:51:27 -06001109 __io_commit_cqring(ctx);
1110
Pavel Begunkov87987892020-01-18 01:22:30 +03001111 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001112 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001113}
1114
Jens Axboe2b188cc2019-01-07 10:46:33 -07001115static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1116{
Hristo Venev75b28af2019-08-26 17:23:46 +00001117 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001118 unsigned tail;
1119
1120 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001121 /*
1122 * writes to the cq entry need to come after reading head; the
1123 * control dependency is enough as we're using WRITE_ONCE to
1124 * fill the cq entry
1125 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001126 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001127 return NULL;
1128
1129 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001130 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001131}
1132
Jens Axboef2842ab2020-01-08 11:04:00 -07001133static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1134{
Jens Axboef0b493e2020-02-01 21:30:11 -07001135 if (!ctx->cq_ev_fd)
1136 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001137 if (!ctx->eventfd_async)
1138 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001139 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001140}
1141
Jens Axboeb41e9852020-02-17 09:52:41 -07001142static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001143{
1144 if (waitqueue_active(&ctx->wait))
1145 wake_up(&ctx->wait);
1146 if (waitqueue_active(&ctx->sqo_wait))
1147 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001148 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001149 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001150}
1151
Jens Axboec4a2ed72019-11-21 21:01:26 -07001152/* Returns true if there are no backlogged entries after the flush */
1153static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001154{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001155 struct io_rings *rings = ctx->rings;
1156 struct io_uring_cqe *cqe;
1157 struct io_kiocb *req;
1158 unsigned long flags;
1159 LIST_HEAD(list);
1160
1161 if (!force) {
1162 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001163 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001164 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1165 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001166 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001167 }
1168
1169 spin_lock_irqsave(&ctx->completion_lock, flags);
1170
1171 /* if force is set, the ring is going away. always drop after that */
1172 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001173 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001174
Jens Axboec4a2ed72019-11-21 21:01:26 -07001175 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001176 while (!list_empty(&ctx->cq_overflow_list)) {
1177 cqe = io_get_cqring(ctx);
1178 if (!cqe && !force)
1179 break;
1180
1181 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1182 list);
1183 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001184 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001185 if (cqe) {
1186 WRITE_ONCE(cqe->user_data, req->user_data);
1187 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001188 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001189 } else {
1190 WRITE_ONCE(ctx->rings->cq_overflow,
1191 atomic_inc_return(&ctx->cached_cq_overflow));
1192 }
1193 }
1194
1195 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001196 if (cqe) {
1197 clear_bit(0, &ctx->sq_check_overflow);
1198 clear_bit(0, &ctx->cq_check_overflow);
1199 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001200 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1201 io_cqring_ev_posted(ctx);
1202
1203 while (!list_empty(&list)) {
1204 req = list_first_entry(&list, struct io_kiocb, list);
1205 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001206 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001207 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001208
1209 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001210}
1211
Jens Axboebcda7ba2020-02-23 16:42:51 -07001212static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001213{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001214 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215 struct io_uring_cqe *cqe;
1216
Jens Axboe78e19bb2019-11-06 15:21:34 -07001217 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001218
Jens Axboe2b188cc2019-01-07 10:46:33 -07001219 /*
1220 * If we can't get a cq entry, userspace overflowed the
1221 * submission (by quite a lot). Increment the overflow count in
1222 * the ring.
1223 */
1224 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001225 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001226 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001227 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001228 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001229 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001230 WRITE_ONCE(ctx->rings->cq_overflow,
1231 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001232 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001233 if (list_empty(&ctx->cq_overflow_list)) {
1234 set_bit(0, &ctx->sq_check_overflow);
1235 set_bit(0, &ctx->cq_check_overflow);
1236 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001237 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001238 refcount_inc(&req->refs);
1239 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001240 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001241 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242 }
1243}
1244
Jens Axboebcda7ba2020-02-23 16:42:51 -07001245static void io_cqring_fill_event(struct io_kiocb *req, long res)
1246{
1247 __io_cqring_fill_event(req, res, 0);
1248}
1249
1250static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001251{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001252 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253 unsigned long flags;
1254
1255 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001256 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001257 io_commit_cqring(ctx);
1258 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1259
Jens Axboe8c838782019-03-12 15:48:16 -06001260 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001261}
1262
Jens Axboebcda7ba2020-02-23 16:42:51 -07001263static void io_cqring_add_event(struct io_kiocb *req, long res)
1264{
1265 __io_cqring_add_event(req, res, 0);
1266}
1267
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001268static inline bool io_is_fallback_req(struct io_kiocb *req)
1269{
1270 return req == (struct io_kiocb *)
1271 ((unsigned long) req->ctx->fallback_req & ~1UL);
1272}
1273
1274static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1275{
1276 struct io_kiocb *req;
1277
1278 req = ctx->fallback_req;
1279 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1280 return req;
1281
1282 return NULL;
1283}
1284
Jens Axboe2579f912019-01-09 09:10:43 -07001285static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1286 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287{
Jens Axboefd6fab22019-03-14 16:30:06 -06001288 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001289 struct io_kiocb *req;
1290
Jens Axboe2579f912019-01-09 09:10:43 -07001291 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001292 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001293 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001294 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001295 } else if (!state->free_reqs) {
1296 size_t sz;
1297 int ret;
1298
1299 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001300 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1301
1302 /*
1303 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1304 * retry single alloc to be on the safe side.
1305 */
1306 if (unlikely(ret <= 0)) {
1307 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1308 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001309 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001310 ret = 1;
1311 }
Jens Axboe2579f912019-01-09 09:10:43 -07001312 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001313 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001314 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001315 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001316 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001317 }
1318
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001319got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001320 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001321 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001322 req->ctx = ctx;
1323 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001324 /* one is dropped after submission, the other at completion */
1325 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001326 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001327 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001328 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001329fallback:
1330 req = io_get_fallback_req(ctx);
1331 if (req)
1332 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001333 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001334 return NULL;
1335}
1336
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001337static inline void io_put_file(struct io_kiocb *req, struct file *file,
1338 bool fixed)
1339{
1340 if (fixed)
1341 percpu_ref_put(&req->ctx->file_data->refs);
1342 else
1343 fput(file);
1344}
1345
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001346static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001347{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001348 if (likely(!io_is_fallback_req(req)))
1349 kmem_cache_free(req_cachep, req);
1350 else
1351 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1352}
1353
Jens Axboec6ca97b302019-12-28 12:11:08 -07001354static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001355{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001356 if (req->flags & REQ_F_NEED_CLEANUP)
1357 io_cleanup_req(req);
1358
YueHaibing96fd84d2020-01-07 22:22:44 +08001359 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001360 if (req->file)
1361 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001362
1363 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364}
1365
1366static void __io_free_req(struct io_kiocb *req)
1367{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001368 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001369
Jens Axboefcb323c2019-10-24 12:39:47 -06001370 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001371 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001372 unsigned long flags;
1373
1374 spin_lock_irqsave(&ctx->inflight_lock, flags);
1375 list_del(&req->inflight_entry);
1376 if (waitqueue_active(&ctx->inflight_wait))
1377 wake_up(&ctx->inflight_wait);
1378 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1379 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001380
1381 percpu_ref_put(&req->ctx->refs);
1382 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001383}
1384
Jens Axboec6ca97b302019-12-28 12:11:08 -07001385struct req_batch {
1386 void *reqs[IO_IOPOLL_BATCH];
1387 int to_free;
1388 int need_iter;
1389};
1390
1391static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1392{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001393 int fixed_refs = rb->to_free;
1394
Jens Axboec6ca97b302019-12-28 12:11:08 -07001395 if (!rb->to_free)
1396 return;
1397 if (rb->need_iter) {
1398 int i, inflight = 0;
1399 unsigned long flags;
1400
Jens Axboe10fef4b2020-01-09 07:52:28 -07001401 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001402 for (i = 0; i < rb->to_free; i++) {
1403 struct io_kiocb *req = rb->reqs[i];
1404
Jens Axboe10fef4b2020-01-09 07:52:28 -07001405 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001406 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001407 fixed_refs++;
1408 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001409 if (req->flags & REQ_F_INFLIGHT)
1410 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001411 __io_req_aux_free(req);
1412 }
1413 if (!inflight)
1414 goto do_free;
1415
1416 spin_lock_irqsave(&ctx->inflight_lock, flags);
1417 for (i = 0; i < rb->to_free; i++) {
1418 struct io_kiocb *req = rb->reqs[i];
1419
Jens Axboe10fef4b2020-01-09 07:52:28 -07001420 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001421 list_del(&req->inflight_entry);
1422 if (!--inflight)
1423 break;
1424 }
1425 }
1426 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1427
1428 if (waitqueue_active(&ctx->inflight_wait))
1429 wake_up(&ctx->inflight_wait);
1430 }
1431do_free:
1432 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001433 if (fixed_refs)
1434 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001435 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001436 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001437}
1438
Jackie Liua197f662019-11-08 08:09:12 -07001439static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001440{
Jackie Liua197f662019-11-08 08:09:12 -07001441 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001442 int ret;
1443
Jens Axboe2d283902019-12-04 11:08:05 -07001444 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001445 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001446 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001447 io_commit_cqring(ctx);
1448 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001449 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001450 return true;
1451 }
1452
1453 return false;
1454}
1455
Jens Axboeba816ad2019-09-28 11:36:45 -06001456static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001457{
Jens Axboe2665abf2019-11-05 12:40:47 -07001458 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001459 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001460
Jens Axboe4d7dd462019-11-20 13:03:52 -07001461 /* Already got next link */
1462 if (req->flags & REQ_F_LINK_NEXT)
1463 return;
1464
Jens Axboe9e645e112019-05-10 16:07:28 -06001465 /*
1466 * The list should never be empty when we are called here. But could
1467 * potentially happen if the chain is messed up, check to be on the
1468 * safe side.
1469 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001470 while (!list_empty(&req->link_list)) {
1471 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1472 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001473
Pavel Begunkov44932332019-12-05 16:16:35 +03001474 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1475 (nxt->flags & REQ_F_TIMEOUT))) {
1476 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001477 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001478 req->flags &= ~REQ_F_LINK_TIMEOUT;
1479 continue;
1480 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001481
Pavel Begunkov44932332019-12-05 16:16:35 +03001482 list_del_init(&req->link_list);
1483 if (!list_empty(&nxt->link_list))
1484 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001485 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001486 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001487 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001488
Jens Axboe4d7dd462019-11-20 13:03:52 -07001489 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001490 if (wake_ev)
1491 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001492}
1493
1494/*
1495 * Called if REQ_F_LINK is set, and we fail the head request
1496 */
1497static void io_fail_links(struct io_kiocb *req)
1498{
Jens Axboe2665abf2019-11-05 12:40:47 -07001499 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001500 unsigned long flags;
1501
1502 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001503
1504 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001505 struct io_kiocb *link = list_first_entry(&req->link_list,
1506 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001507
Pavel Begunkov44932332019-12-05 16:16:35 +03001508 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001509 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001510
1511 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001512 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001513 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001514 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001515 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001516 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001517 }
Jens Axboe5d960722019-11-19 15:31:28 -07001518 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001519 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001520
1521 io_commit_cqring(ctx);
1522 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1523 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001524}
1525
Jens Axboe4d7dd462019-11-20 13:03:52 -07001526static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001527{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001528 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001529 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001530
Jens Axboe9e645e112019-05-10 16:07:28 -06001531 /*
1532 * If LINK is set, we have dependent requests in this chain. If we
1533 * didn't fail this request, queue the first one up, moving any other
1534 * dependencies to the next request. In case of failure, fail the rest
1535 * of the chain.
1536 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001537 if (req->flags & REQ_F_FAIL_LINK) {
1538 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001539 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1540 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001541 struct io_ring_ctx *ctx = req->ctx;
1542 unsigned long flags;
1543
1544 /*
1545 * If this is a timeout link, we could be racing with the
1546 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001547 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001548 */
1549 spin_lock_irqsave(&ctx->completion_lock, flags);
1550 io_req_link_next(req, nxt);
1551 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1552 } else {
1553 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001554 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001555}
Jens Axboe9e645e112019-05-10 16:07:28 -06001556
Jackie Liuc69f8db2019-11-09 11:00:08 +08001557static void io_free_req(struct io_kiocb *req)
1558{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001559 struct io_kiocb *nxt = NULL;
1560
1561 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001562 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001563
1564 if (nxt)
1565 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001566}
1567
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001568static void io_link_work_cb(struct io_wq_work **workptr)
1569{
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001570 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1571 struct io_kiocb *link;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001572
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001573 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001574 io_queue_linked_timeout(link);
1575 io_wq_submit_work(workptr);
1576}
1577
1578static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1579{
1580 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001581 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1582
1583 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1584 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001585
1586 *workptr = &nxt->work;
1587 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001588 if (link)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001589 nxt->work.func = io_link_work_cb;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001590}
1591
Jens Axboeba816ad2019-09-28 11:36:45 -06001592/*
1593 * Drop reference to request, return next in chain (if there is one) if this
1594 * was the last reference to this request.
1595 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001596__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001597static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001598{
Jens Axboe2a44f462020-02-25 13:25:41 -07001599 if (refcount_dec_and_test(&req->refs)) {
1600 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001601 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001602 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001603}
1604
Jens Axboe2b188cc2019-01-07 10:46:33 -07001605static void io_put_req(struct io_kiocb *req)
1606{
Jens Axboedef596e2019-01-09 08:59:42 -07001607 if (refcount_dec_and_test(&req->refs))
1608 io_free_req(req);
1609}
1610
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001611static void io_steal_work(struct io_kiocb *req,
1612 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001613{
1614 /*
1615 * It's in an io-wq worker, so there always should be at least
1616 * one reference, which will be dropped in io_put_work() just
1617 * after the current handler returns.
1618 *
1619 * It also means, that if the counter dropped to 1, then there is
1620 * no asynchronous users left, so it's safe to steal the next work.
1621 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001622 if (refcount_read(&req->refs) == 1) {
1623 struct io_kiocb *nxt = NULL;
1624
1625 io_req_find_next(req, &nxt);
1626 if (nxt)
1627 io_wq_assign_next(workptr, nxt);
1628 }
1629}
1630
Jens Axboe978db572019-11-14 22:39:04 -07001631/*
1632 * Must only be used if we don't need to care about links, usually from
1633 * within the completion handling itself.
1634 */
1635static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001636{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001637 /* drop both submit and complete references */
1638 if (refcount_sub_and_test(2, &req->refs))
1639 __io_free_req(req);
1640}
1641
Jens Axboe978db572019-11-14 22:39:04 -07001642static void io_double_put_req(struct io_kiocb *req)
1643{
1644 /* drop both submit and complete references */
1645 if (refcount_sub_and_test(2, &req->refs))
1646 io_free_req(req);
1647}
1648
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001649static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001650{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001651 struct io_rings *rings = ctx->rings;
1652
Jens Axboead3eb2c2019-12-18 17:12:20 -07001653 if (test_bit(0, &ctx->cq_check_overflow)) {
1654 /*
1655 * noflush == true is from the waitqueue handler, just ensure
1656 * we wake up the task, and the next invocation will flush the
1657 * entries. We cannot safely to it from here.
1658 */
1659 if (noflush && !list_empty(&ctx->cq_overflow_list))
1660 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001661
Jens Axboead3eb2c2019-12-18 17:12:20 -07001662 io_cqring_overflow_flush(ctx, false);
1663 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001664
Jens Axboea3a0e432019-08-20 11:03:11 -06001665 /* See comment at the top of this file */
1666 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001667 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001668}
1669
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001670static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1671{
1672 struct io_rings *rings = ctx->rings;
1673
1674 /* make sure SQ entry isn't read before tail */
1675 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1676}
1677
Jens Axboe8237e042019-12-28 10:48:22 -07001678static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001679{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001680 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1681 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001682
Jens Axboec6ca97b302019-12-28 12:11:08 -07001683 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1684 rb->need_iter++;
1685
1686 rb->reqs[rb->to_free++] = req;
1687 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1688 io_free_req_many(req->ctx, rb);
1689 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001690}
1691
Jens Axboebcda7ba2020-02-23 16:42:51 -07001692static int io_put_kbuf(struct io_kiocb *req)
1693{
Jens Axboe4d954c22020-02-27 07:31:19 -07001694 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001695 int cflags;
1696
Jens Axboe4d954c22020-02-27 07:31:19 -07001697 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001698 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1699 cflags |= IORING_CQE_F_BUFFER;
1700 req->rw.addr = 0;
1701 kfree(kbuf);
1702 return cflags;
1703}
1704
Jens Axboedef596e2019-01-09 08:59:42 -07001705/*
1706 * Find and free completed poll iocbs
1707 */
1708static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1709 struct list_head *done)
1710{
Jens Axboe8237e042019-12-28 10:48:22 -07001711 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001712 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001713
Jens Axboec6ca97b302019-12-28 12:11:08 -07001714 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001715 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001716 int cflags = 0;
1717
Jens Axboedef596e2019-01-09 08:59:42 -07001718 req = list_first_entry(done, struct io_kiocb, list);
1719 list_del(&req->list);
1720
Jens Axboebcda7ba2020-02-23 16:42:51 -07001721 if (req->flags & REQ_F_BUFFER_SELECTED)
1722 cflags = io_put_kbuf(req);
1723
1724 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001725 (*nr_events)++;
1726
Jens Axboe8237e042019-12-28 10:48:22 -07001727 if (refcount_dec_and_test(&req->refs) &&
1728 !io_req_multi_free(&rb, req))
1729 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001730 }
Jens Axboedef596e2019-01-09 08:59:42 -07001731
Jens Axboe09bb8392019-03-13 12:39:28 -06001732 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001733 if (ctx->flags & IORING_SETUP_SQPOLL)
1734 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001735 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001736}
1737
1738static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1739 long min)
1740{
1741 struct io_kiocb *req, *tmp;
1742 LIST_HEAD(done);
1743 bool spin;
1744 int ret;
1745
1746 /*
1747 * Only spin for completions if we don't have multiple devices hanging
1748 * off our complete list, and we're under the requested amount.
1749 */
1750 spin = !ctx->poll_multi_file && *nr_events < min;
1751
1752 ret = 0;
1753 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001754 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001755
1756 /*
1757 * Move completed entries to our local list. If we find a
1758 * request that requires polling, break out and complete
1759 * the done list first, if we have entries there.
1760 */
1761 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1762 list_move_tail(&req->list, &done);
1763 continue;
1764 }
1765 if (!list_empty(&done))
1766 break;
1767
1768 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1769 if (ret < 0)
1770 break;
1771
1772 if (ret && spin)
1773 spin = false;
1774 ret = 0;
1775 }
1776
1777 if (!list_empty(&done))
1778 io_iopoll_complete(ctx, nr_events, &done);
1779
1780 return ret;
1781}
1782
1783/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001784 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001785 * non-spinning poll check - we'll still enter the driver poll loop, but only
1786 * as a non-spinning completion check.
1787 */
1788static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1789 long min)
1790{
Jens Axboe08f54392019-08-21 22:19:11 -06001791 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001792 int ret;
1793
1794 ret = io_do_iopoll(ctx, nr_events, min);
1795 if (ret < 0)
1796 return ret;
1797 if (!min || *nr_events >= min)
1798 return 0;
1799 }
1800
1801 return 1;
1802}
1803
1804/*
1805 * We can't just wait for polled events to come to us, we have to actively
1806 * find and complete them.
1807 */
1808static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1809{
1810 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1811 return;
1812
1813 mutex_lock(&ctx->uring_lock);
1814 while (!list_empty(&ctx->poll_list)) {
1815 unsigned int nr_events = 0;
1816
1817 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001818
1819 /*
1820 * Ensure we allow local-to-the-cpu processing to take place,
1821 * in this case we need to ensure that we reap all events.
1822 */
1823 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001824 }
1825 mutex_unlock(&ctx->uring_lock);
1826}
1827
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001828static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1829 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001830{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001831 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001832
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001833 /*
1834 * We disallow the app entering submit/complete with polling, but we
1835 * still need to lock the ring to prevent racing with polled issue
1836 * that got punted to a workqueue.
1837 */
1838 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001839 do {
1840 int tmin = 0;
1841
Jens Axboe500f9fb2019-08-19 12:15:59 -06001842 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001843 * Don't enter poll loop if we already have events pending.
1844 * If we do, we can potentially be spinning for commands that
1845 * already triggered a CQE (eg in error).
1846 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001847 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001848 break;
1849
1850 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001851 * If a submit got punted to a workqueue, we can have the
1852 * application entering polling for a command before it gets
1853 * issued. That app will hold the uring_lock for the duration
1854 * of the poll right here, so we need to take a breather every
1855 * now and then to ensure that the issue has a chance to add
1856 * the poll to the issued list. Otherwise we can spin here
1857 * forever, while the workqueue is stuck trying to acquire the
1858 * very same mutex.
1859 */
1860 if (!(++iters & 7)) {
1861 mutex_unlock(&ctx->uring_lock);
1862 mutex_lock(&ctx->uring_lock);
1863 }
1864
Jens Axboedef596e2019-01-09 08:59:42 -07001865 if (*nr_events < min)
1866 tmin = min - *nr_events;
1867
1868 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1869 if (ret <= 0)
1870 break;
1871 ret = 0;
1872 } while (min && !*nr_events && !need_resched());
1873
Jens Axboe500f9fb2019-08-19 12:15:59 -06001874 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001875 return ret;
1876}
1877
Jens Axboe491381ce2019-10-17 09:20:46 -06001878static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001879{
Jens Axboe491381ce2019-10-17 09:20:46 -06001880 /*
1881 * Tell lockdep we inherited freeze protection from submission
1882 * thread.
1883 */
1884 if (req->flags & REQ_F_ISREG) {
1885 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001886
Jens Axboe491381ce2019-10-17 09:20:46 -06001887 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001888 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001889 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001890}
1891
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001892static inline void req_set_fail_links(struct io_kiocb *req)
1893{
1894 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1895 req->flags |= REQ_F_FAIL_LINK;
1896}
1897
Jens Axboeba816ad2019-09-28 11:36:45 -06001898static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899{
Jens Axboe9adbd452019-12-20 08:45:55 -07001900 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001901 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902
Jens Axboe491381ce2019-10-17 09:20:46 -06001903 if (kiocb->ki_flags & IOCB_WRITE)
1904 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001905
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001906 if (res != req->result)
1907 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001908 if (req->flags & REQ_F_BUFFER_SELECTED)
1909 cflags = io_put_kbuf(req);
1910 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001911}
1912
1913static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1914{
Jens Axboe9adbd452019-12-20 08:45:55 -07001915 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001916
1917 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001918 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001919}
1920
Jens Axboedef596e2019-01-09 08:59:42 -07001921static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1922{
Jens Axboe9adbd452019-12-20 08:45:55 -07001923 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001924
Jens Axboe491381ce2019-10-17 09:20:46 -06001925 if (kiocb->ki_flags & IOCB_WRITE)
1926 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001927
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001928 if (res != req->result)
1929 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001930 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001931 if (res != -EAGAIN)
1932 req->flags |= REQ_F_IOPOLL_COMPLETED;
1933}
1934
1935/*
1936 * After the iocb has been issued, it's safe to be found on the poll list.
1937 * Adding the kiocb to the list AFTER submission ensures that we don't
1938 * find it from a io_iopoll_getevents() thread before the issuer is done
1939 * accessing the kiocb cookie.
1940 */
1941static void io_iopoll_req_issued(struct io_kiocb *req)
1942{
1943 struct io_ring_ctx *ctx = req->ctx;
1944
1945 /*
1946 * Track whether we have multiple files in our lists. This will impact
1947 * how we do polling eventually, not spinning if we're on potentially
1948 * different devices.
1949 */
1950 if (list_empty(&ctx->poll_list)) {
1951 ctx->poll_multi_file = false;
1952 } else if (!ctx->poll_multi_file) {
1953 struct io_kiocb *list_req;
1954
1955 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1956 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001957 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001958 ctx->poll_multi_file = true;
1959 }
1960
1961 /*
1962 * For fast devices, IO may have already completed. If it has, add
1963 * it to the front so we find it first.
1964 */
1965 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1966 list_add(&req->list, &ctx->poll_list);
1967 else
1968 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001969
1970 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1971 wq_has_sleeper(&ctx->sqo_wait))
1972 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001973}
1974
Jens Axboe3d6770f2019-04-13 11:50:54 -06001975static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001976{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001977 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001978 int diff = state->has_refs - state->used_refs;
1979
1980 if (diff)
1981 fput_many(state->file, diff);
1982 state->file = NULL;
1983 }
1984}
1985
1986/*
1987 * Get as many references to a file as we have IOs left in this submission,
1988 * assuming most submissions are for one file, or at least that each file
1989 * has more than one submission.
1990 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001991static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001992{
1993 if (!state)
1994 return fget(fd);
1995
1996 if (state->file) {
1997 if (state->fd == fd) {
1998 state->used_refs++;
1999 state->ios_left--;
2000 return state->file;
2001 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06002002 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002003 }
2004 state->file = fget_many(fd, state->ios_left);
2005 if (!state->file)
2006 return NULL;
2007
2008 state->fd = fd;
2009 state->has_refs = state->ios_left;
2010 state->used_refs = 1;
2011 state->ios_left--;
2012 return state->file;
2013}
2014
Jens Axboe2b188cc2019-01-07 10:46:33 -07002015/*
2016 * If we tracked the file through the SCM inflight mechanism, we could support
2017 * any file. For now, just ensure that anything potentially problematic is done
2018 * inline.
2019 */
2020static bool io_file_supports_async(struct file *file)
2021{
2022 umode_t mode = file_inode(file)->i_mode;
2023
Jens Axboe10d59342019-12-09 20:16:22 -07002024 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002025 return true;
2026 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2027 return true;
2028
2029 return false;
2030}
2031
Jens Axboe3529d8c2019-12-19 18:24:38 -07002032static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2033 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002034{
Jens Axboedef596e2019-01-09 08:59:42 -07002035 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002036 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002037 unsigned ioprio;
2038 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002039
Jens Axboe491381ce2019-10-17 09:20:46 -06002040 if (S_ISREG(file_inode(req->file)->i_mode))
2041 req->flags |= REQ_F_ISREG;
2042
Jens Axboe2b188cc2019-01-07 10:46:33 -07002043 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002044 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2045 req->flags |= REQ_F_CUR_POS;
2046 kiocb->ki_pos = req->file->f_pos;
2047 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002048 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002049 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2050 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2051 if (unlikely(ret))
2052 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002053
2054 ioprio = READ_ONCE(sqe->ioprio);
2055 if (ioprio) {
2056 ret = ioprio_check_cap(ioprio);
2057 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002058 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002059
2060 kiocb->ki_ioprio = ioprio;
2061 } else
2062 kiocb->ki_ioprio = get_current_ioprio();
2063
Stefan Bühler8449eed2019-04-27 20:34:19 +02002064 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002065 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2066 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002067 req->flags |= REQ_F_NOWAIT;
2068
2069 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002070 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002071
Jens Axboedef596e2019-01-09 08:59:42 -07002072 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002073 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2074 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002075 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002076
Jens Axboedef596e2019-01-09 08:59:42 -07002077 kiocb->ki_flags |= IOCB_HIPRI;
2078 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002079 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002080 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002081 if (kiocb->ki_flags & IOCB_HIPRI)
2082 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002083 kiocb->ki_complete = io_complete_rw;
2084 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002085
Jens Axboe3529d8c2019-12-19 18:24:38 -07002086 req->rw.addr = READ_ONCE(sqe->addr);
2087 req->rw.len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002088 /* we own ->private, reuse it for the buffer index / buffer ID */
Jens Axboe9adbd452019-12-20 08:45:55 -07002089 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002090 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002091 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002092}
2093
2094static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2095{
2096 switch (ret) {
2097 case -EIOCBQUEUED:
2098 break;
2099 case -ERESTARTSYS:
2100 case -ERESTARTNOINTR:
2101 case -ERESTARTNOHAND:
2102 case -ERESTART_RESTARTBLOCK:
2103 /*
2104 * We can't just restart the syscall, since previously
2105 * submitted sqes may already be in progress. Just fail this
2106 * IO with EINTR.
2107 */
2108 ret = -EINTR;
2109 /* fall through */
2110 default:
2111 kiocb->ki_complete(kiocb, ret, 0);
2112 }
2113}
2114
Pavel Begunkov014db002020-03-03 21:33:12 +03002115static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002116{
Jens Axboeba042912019-12-25 16:33:42 -07002117 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2118
2119 if (req->flags & REQ_F_CUR_POS)
2120 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002121 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002122 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002123 else
2124 io_rw_done(kiocb, ret);
2125}
2126
Jens Axboe9adbd452019-12-20 08:45:55 -07002127static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002128 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002129{
Jens Axboe9adbd452019-12-20 08:45:55 -07002130 struct io_ring_ctx *ctx = req->ctx;
2131 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002132 struct io_mapped_ubuf *imu;
2133 unsigned index, buf_index;
2134 size_t offset;
2135 u64 buf_addr;
2136
2137 /* attempt to use fixed buffers without having provided iovecs */
2138 if (unlikely(!ctx->user_bufs))
2139 return -EFAULT;
2140
Jens Axboe9adbd452019-12-20 08:45:55 -07002141 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002142 if (unlikely(buf_index >= ctx->nr_user_bufs))
2143 return -EFAULT;
2144
2145 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2146 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002147 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002148
2149 /* overflow */
2150 if (buf_addr + len < buf_addr)
2151 return -EFAULT;
2152 /* not inside the mapped region */
2153 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2154 return -EFAULT;
2155
2156 /*
2157 * May not be a start of buffer, set size appropriately
2158 * and advance us to the beginning.
2159 */
2160 offset = buf_addr - imu->ubuf;
2161 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002162
2163 if (offset) {
2164 /*
2165 * Don't use iov_iter_advance() here, as it's really slow for
2166 * using the latter parts of a big fixed buffer - it iterates
2167 * over each segment manually. We can cheat a bit here, because
2168 * we know that:
2169 *
2170 * 1) it's a BVEC iter, we set it up
2171 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2172 * first and last bvec
2173 *
2174 * So just find our index, and adjust the iterator afterwards.
2175 * If the offset is within the first bvec (or the whole first
2176 * bvec, just use iov_iter_advance(). This makes it easier
2177 * since we can just skip the first segment, which may not
2178 * be PAGE_SIZE aligned.
2179 */
2180 const struct bio_vec *bvec = imu->bvec;
2181
2182 if (offset <= bvec->bv_len) {
2183 iov_iter_advance(iter, offset);
2184 } else {
2185 unsigned long seg_skip;
2186
2187 /* skip first vec */
2188 offset -= bvec->bv_len;
2189 seg_skip = 1 + (offset >> PAGE_SHIFT);
2190
2191 iter->bvec = bvec + seg_skip;
2192 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002193 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002194 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002195 }
2196 }
2197
Jens Axboe5e559562019-11-13 16:12:46 -07002198 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002199}
2200
Jens Axboebcda7ba2020-02-23 16:42:51 -07002201static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2202{
2203 if (needs_lock)
2204 mutex_unlock(&ctx->uring_lock);
2205}
2206
2207static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2208{
2209 /*
2210 * "Normal" inline submissions always hold the uring_lock, since we
2211 * grab it from the system call. Same is true for the SQPOLL offload.
2212 * The only exception is when we've detached the request and issue it
2213 * from an async worker thread, grab the lock for that case.
2214 */
2215 if (needs_lock)
2216 mutex_lock(&ctx->uring_lock);
2217}
2218
2219static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2220 int bgid, struct io_buffer *kbuf,
2221 bool needs_lock)
2222{
2223 struct io_buffer *head;
2224
2225 if (req->flags & REQ_F_BUFFER_SELECTED)
2226 return kbuf;
2227
2228 io_ring_submit_lock(req->ctx, needs_lock);
2229
2230 lockdep_assert_held(&req->ctx->uring_lock);
2231
2232 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2233 if (head) {
2234 if (!list_empty(&head->list)) {
2235 kbuf = list_last_entry(&head->list, struct io_buffer,
2236 list);
2237 list_del(&kbuf->list);
2238 } else {
2239 kbuf = head;
2240 idr_remove(&req->ctx->io_buffer_idr, bgid);
2241 }
2242 if (*len > kbuf->len)
2243 *len = kbuf->len;
2244 } else {
2245 kbuf = ERR_PTR(-ENOBUFS);
2246 }
2247
2248 io_ring_submit_unlock(req->ctx, needs_lock);
2249
2250 return kbuf;
2251}
2252
Jens Axboe4d954c22020-02-27 07:31:19 -07002253static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2254 bool needs_lock)
2255{
2256 struct io_buffer *kbuf;
2257 int bgid;
2258
2259 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2260 bgid = (int) (unsigned long) req->rw.kiocb.private;
2261 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2262 if (IS_ERR(kbuf))
2263 return kbuf;
2264 req->rw.addr = (u64) (unsigned long) kbuf;
2265 req->flags |= REQ_F_BUFFER_SELECTED;
2266 return u64_to_user_ptr(kbuf->addr);
2267}
2268
2269#ifdef CONFIG_COMPAT
2270static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2271 bool needs_lock)
2272{
2273 struct compat_iovec __user *uiov;
2274 compat_ssize_t clen;
2275 void __user *buf;
2276 ssize_t len;
2277
2278 uiov = u64_to_user_ptr(req->rw.addr);
2279 if (!access_ok(uiov, sizeof(*uiov)))
2280 return -EFAULT;
2281 if (__get_user(clen, &uiov->iov_len))
2282 return -EFAULT;
2283 if (clen < 0)
2284 return -EINVAL;
2285
2286 len = clen;
2287 buf = io_rw_buffer_select(req, &len, needs_lock);
2288 if (IS_ERR(buf))
2289 return PTR_ERR(buf);
2290 iov[0].iov_base = buf;
2291 iov[0].iov_len = (compat_size_t) len;
2292 return 0;
2293}
2294#endif
2295
2296static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2297 bool needs_lock)
2298{
2299 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2300 void __user *buf;
2301 ssize_t len;
2302
2303 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2304 return -EFAULT;
2305
2306 len = iov[0].iov_len;
2307 if (len < 0)
2308 return -EINVAL;
2309 buf = io_rw_buffer_select(req, &len, needs_lock);
2310 if (IS_ERR(buf))
2311 return PTR_ERR(buf);
2312 iov[0].iov_base = buf;
2313 iov[0].iov_len = len;
2314 return 0;
2315}
2316
2317static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2318 bool needs_lock)
2319{
2320 if (req->flags & REQ_F_BUFFER_SELECTED)
2321 return 0;
2322 if (!req->rw.len)
2323 return 0;
2324 else if (req->rw.len > 1)
2325 return -EINVAL;
2326
2327#ifdef CONFIG_COMPAT
2328 if (req->ctx->compat)
2329 return io_compat_import(req, iov, needs_lock);
2330#endif
2331
2332 return __io_iov_buffer_select(req, iov, needs_lock);
2333}
2334
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002335static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002336 struct iovec **iovec, struct iov_iter *iter,
2337 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002338{
Jens Axboe9adbd452019-12-20 08:45:55 -07002339 void __user *buf = u64_to_user_ptr(req->rw.addr);
2340 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002341 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002342 u8 opcode;
2343
Jens Axboed625c6e2019-12-17 19:53:05 -07002344 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002345 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002346 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002347 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002348 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002349
Jens Axboebcda7ba2020-02-23 16:42:51 -07002350 /* buffer index only valid with fixed read/write, or buffer select */
2351 if (req->rw.kiocb.private && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002352 return -EINVAL;
2353
Jens Axboe3a6820f2019-12-22 15:19:35 -07002354 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002355 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002356 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2357 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002358 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002359 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002360 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002361 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002362 }
2363
Jens Axboe3a6820f2019-12-22 15:19:35 -07002364 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2365 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002366 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002367 }
2368
Jens Axboef67676d2019-12-02 11:03:47 -07002369 if (req->io) {
2370 struct io_async_rw *iorw = &req->io->rw;
2371
2372 *iovec = iorw->iov;
2373 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2374 if (iorw->iov == iorw->fast_iov)
2375 *iovec = NULL;
2376 return iorw->size;
2377 }
2378
Jens Axboe4d954c22020-02-27 07:31:19 -07002379 if (req->flags & REQ_F_BUFFER_SELECT) {
2380 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002381 if (!ret) {
2382 ret = (*iovec)->iov_len;
2383 iov_iter_init(iter, rw, *iovec, 1, ret);
2384 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002385 *iovec = NULL;
2386 return ret;
2387 }
2388
Jens Axboe2b188cc2019-01-07 10:46:33 -07002389#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002390 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002391 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2392 iovec, iter);
2393#endif
2394
2395 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2396}
2397
Jens Axboe32960612019-09-23 11:05:34 -06002398/*
2399 * For files that don't have ->read_iter() and ->write_iter(), handle them
2400 * by looping over ->read() or ->write() manually.
2401 */
2402static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2403 struct iov_iter *iter)
2404{
2405 ssize_t ret = 0;
2406
2407 /*
2408 * Don't support polled IO through this interface, and we can't
2409 * support non-blocking either. For the latter, this just causes
2410 * the kiocb to be handled from an async context.
2411 */
2412 if (kiocb->ki_flags & IOCB_HIPRI)
2413 return -EOPNOTSUPP;
2414 if (kiocb->ki_flags & IOCB_NOWAIT)
2415 return -EAGAIN;
2416
2417 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002418 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002419 ssize_t nr;
2420
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002421 if (!iov_iter_is_bvec(iter)) {
2422 iovec = iov_iter_iovec(iter);
2423 } else {
2424 /* fixed buffers import bvec */
2425 iovec.iov_base = kmap(iter->bvec->bv_page)
2426 + iter->iov_offset;
2427 iovec.iov_len = min(iter->count,
2428 iter->bvec->bv_len - iter->iov_offset);
2429 }
2430
Jens Axboe32960612019-09-23 11:05:34 -06002431 if (rw == READ) {
2432 nr = file->f_op->read(file, iovec.iov_base,
2433 iovec.iov_len, &kiocb->ki_pos);
2434 } else {
2435 nr = file->f_op->write(file, iovec.iov_base,
2436 iovec.iov_len, &kiocb->ki_pos);
2437 }
2438
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002439 if (iov_iter_is_bvec(iter))
2440 kunmap(iter->bvec->bv_page);
2441
Jens Axboe32960612019-09-23 11:05:34 -06002442 if (nr < 0) {
2443 if (!ret)
2444 ret = nr;
2445 break;
2446 }
2447 ret += nr;
2448 if (nr != iovec.iov_len)
2449 break;
2450 iov_iter_advance(iter, nr);
2451 }
2452
2453 return ret;
2454}
2455
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002456static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002457 struct iovec *iovec, struct iovec *fast_iov,
2458 struct iov_iter *iter)
2459{
2460 req->io->rw.nr_segs = iter->nr_segs;
2461 req->io->rw.size = io_size;
2462 req->io->rw.iov = iovec;
2463 if (!req->io->rw.iov) {
2464 req->io->rw.iov = req->io->rw.fast_iov;
2465 memcpy(req->io->rw.iov, fast_iov,
2466 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002467 } else {
2468 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002469 }
2470}
2471
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002472static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002473{
Jens Axboed3656342019-12-18 09:50:26 -07002474 if (!io_op_defs[req->opcode].async_ctx)
2475 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002476 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002477 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002478}
2479
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002480static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2481 struct iovec *iovec, struct iovec *fast_iov,
2482 struct iov_iter *iter)
2483{
Jens Axboe980ad262020-01-24 23:08:54 -07002484 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002485 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002486 if (!req->io) {
2487 if (io_alloc_async_ctx(req))
2488 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002489
Jens Axboe5d204bc2020-01-31 12:06:52 -07002490 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2491 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002492 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002493}
2494
Jens Axboe3529d8c2019-12-19 18:24:38 -07002495static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2496 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002497{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002498 struct io_async_ctx *io;
2499 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002500 ssize_t ret;
2501
Jens Axboe3529d8c2019-12-19 18:24:38 -07002502 ret = io_prep_rw(req, sqe, force_nonblock);
2503 if (ret)
2504 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002505
Jens Axboe3529d8c2019-12-19 18:24:38 -07002506 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2507 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002508
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002509 /* either don't need iovec imported or already have it */
2510 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002511 return 0;
2512
2513 io = req->io;
2514 io->rw.iov = io->rw.fast_iov;
2515 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002516 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002517 req->io = io;
2518 if (ret < 0)
2519 return ret;
2520
2521 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2522 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002523}
2524
Pavel Begunkov014db002020-03-03 21:33:12 +03002525static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002526{
2527 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002528 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002529 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002530 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002531 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002532
Jens Axboebcda7ba2020-02-23 16:42:51 -07002533 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002534 if (ret < 0)
2535 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002536
Jens Axboefd6c2e42019-12-18 12:19:41 -07002537 /* Ensure we clear previously set non-block flag */
2538 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002539 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002540
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002541 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002542 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002543 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002544 req->result = io_size;
2545
2546 /*
2547 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2548 * we know to async punt it even if it was opened O_NONBLOCK
2549 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002550 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002551 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002552
Jens Axboe31b51512019-01-18 22:56:34 -07002553 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002554 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002555 if (!ret) {
2556 ssize_t ret2;
2557
Jens Axboe9adbd452019-12-20 08:45:55 -07002558 if (req->file->f_op->read_iter)
2559 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002560 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002561 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002562
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002563 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002564 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002565 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002566 } else {
2567copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002568 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002569 inline_vecs, &iter);
2570 if (ret)
2571 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002572 /* any defer here is final, must blocking retry */
2573 if (!(req->flags & REQ_F_NOWAIT))
2574 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002575 return -EAGAIN;
2576 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002577 }
Jens Axboef67676d2019-12-02 11:03:47 -07002578out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002579 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002580 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002581 return ret;
2582}
2583
Jens Axboe3529d8c2019-12-19 18:24:38 -07002584static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2585 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002586{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002587 struct io_async_ctx *io;
2588 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002589 ssize_t ret;
2590
Jens Axboe3529d8c2019-12-19 18:24:38 -07002591 ret = io_prep_rw(req, sqe, force_nonblock);
2592 if (ret)
2593 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002594
Jens Axboe3529d8c2019-12-19 18:24:38 -07002595 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2596 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002597
Jens Axboe4ed734b2020-03-20 11:23:41 -06002598 req->fsize = rlimit(RLIMIT_FSIZE);
2599
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002600 /* either don't need iovec imported or already have it */
2601 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002602 return 0;
2603
2604 io = req->io;
2605 io->rw.iov = io->rw.fast_iov;
2606 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002607 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002608 req->io = io;
2609 if (ret < 0)
2610 return ret;
2611
2612 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2613 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002614}
2615
Pavel Begunkov014db002020-03-03 21:33:12 +03002616static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002617{
2618 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002619 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002620 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002621 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002622 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002623
Jens Axboebcda7ba2020-02-23 16:42:51 -07002624 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002625 if (ret < 0)
2626 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002627
Jens Axboefd6c2e42019-12-18 12:19:41 -07002628 /* Ensure we clear previously set non-block flag */
2629 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002630 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002631
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002632 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002633 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002634 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002635 req->result = io_size;
2636
2637 /*
2638 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2639 * we know to async punt it even if it was opened O_NONBLOCK
2640 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002641 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002642 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002643
Jens Axboe10d59342019-12-09 20:16:22 -07002644 /* file path doesn't support NOWAIT for non-direct_IO */
2645 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2646 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002647 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002648
Jens Axboe31b51512019-01-18 22:56:34 -07002649 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002650 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002651 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002652 ssize_t ret2;
2653
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654 /*
2655 * Open-code file_start_write here to grab freeze protection,
2656 * which will be released by another thread in
2657 * io_complete_rw(). Fool lockdep by telling it the lock got
2658 * released so that it doesn't complain about the held lock when
2659 * we return to userspace.
2660 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002661 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002662 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002663 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002664 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002665 SB_FREEZE_WRITE);
2666 }
2667 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002668
Jens Axboe4ed734b2020-03-20 11:23:41 -06002669 if (!force_nonblock)
2670 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2671
Jens Axboe9adbd452019-12-20 08:45:55 -07002672 if (req->file->f_op->write_iter)
2673 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002674 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002675 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002676
2677 if (!force_nonblock)
2678 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2679
Jens Axboefaac9962020-02-07 15:45:22 -07002680 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002681 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002682 * retry them without IOCB_NOWAIT.
2683 */
2684 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2685 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002686 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002687 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002688 } else {
2689copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002690 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002691 inline_vecs, &iter);
2692 if (ret)
2693 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002694 /* any defer here is final, must blocking retry */
2695 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002696 return -EAGAIN;
2697 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002698 }
Jens Axboe31b51512019-01-18 22:56:34 -07002699out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002700 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002701 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002702 return ret;
2703}
2704
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002705static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2706{
2707 struct io_splice* sp = &req->splice;
2708 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2709 int ret;
2710
2711 if (req->flags & REQ_F_NEED_CLEANUP)
2712 return 0;
2713
2714 sp->file_in = NULL;
2715 sp->off_in = READ_ONCE(sqe->splice_off_in);
2716 sp->off_out = READ_ONCE(sqe->off);
2717 sp->len = READ_ONCE(sqe->len);
2718 sp->flags = READ_ONCE(sqe->splice_flags);
2719
2720 if (unlikely(sp->flags & ~valid_flags))
2721 return -EINVAL;
2722
2723 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2724 (sp->flags & SPLICE_F_FD_IN_FIXED));
2725 if (ret)
2726 return ret;
2727 req->flags |= REQ_F_NEED_CLEANUP;
2728
2729 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2730 req->work.flags |= IO_WQ_WORK_UNBOUND;
2731
2732 return 0;
2733}
2734
2735static bool io_splice_punt(struct file *file)
2736{
2737 if (get_pipe_info(file))
2738 return false;
2739 if (!io_file_supports_async(file))
2740 return true;
2741 return !(file->f_mode & O_NONBLOCK);
2742}
2743
Pavel Begunkov014db002020-03-03 21:33:12 +03002744static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002745{
2746 struct io_splice *sp = &req->splice;
2747 struct file *in = sp->file_in;
2748 struct file *out = sp->file_out;
2749 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2750 loff_t *poff_in, *poff_out;
2751 long ret;
2752
2753 if (force_nonblock) {
2754 if (io_splice_punt(in) || io_splice_punt(out))
2755 return -EAGAIN;
2756 flags |= SPLICE_F_NONBLOCK;
2757 }
2758
2759 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2760 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2761 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2762 if (force_nonblock && ret == -EAGAIN)
2763 return -EAGAIN;
2764
2765 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2766 req->flags &= ~REQ_F_NEED_CLEANUP;
2767
2768 io_cqring_add_event(req, ret);
2769 if (ret != sp->len)
2770 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002771 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002772 return 0;
2773}
2774
Jens Axboe2b188cc2019-01-07 10:46:33 -07002775/*
2776 * IORING_OP_NOP just posts a completion event, nothing else.
2777 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002778static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002779{
2780 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002781
Jens Axboedef596e2019-01-09 08:59:42 -07002782 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2783 return -EINVAL;
2784
Jens Axboe78e19bb2019-11-06 15:21:34 -07002785 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002786 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002787 return 0;
2788}
2789
Jens Axboe3529d8c2019-12-19 18:24:38 -07002790static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002791{
Jens Axboe6b063142019-01-10 22:13:58 -07002792 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002793
Jens Axboe09bb8392019-03-13 12:39:28 -06002794 if (!req->file)
2795 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002796
Jens Axboe6b063142019-01-10 22:13:58 -07002797 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002798 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002799 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002800 return -EINVAL;
2801
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002802 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2803 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2804 return -EINVAL;
2805
2806 req->sync.off = READ_ONCE(sqe->off);
2807 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002808 return 0;
2809}
2810
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002811static bool io_req_cancelled(struct io_kiocb *req)
2812{
2813 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2814 req_set_fail_links(req);
2815 io_cqring_add_event(req, -ECANCELED);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002816 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002817 return true;
2818 }
2819
2820 return false;
2821}
2822
Pavel Begunkov014db002020-03-03 21:33:12 +03002823static void __io_fsync(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002824{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002825 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002826 int ret;
2827
Jens Axboe9adbd452019-12-20 08:45:55 -07002828 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002829 end > 0 ? end : LLONG_MAX,
2830 req->sync.flags & IORING_FSYNC_DATASYNC);
2831 if (ret < 0)
2832 req_set_fail_links(req);
2833 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002834 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002835}
2836
2837static void io_fsync_finish(struct io_wq_work **workptr)
2838{
2839 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002840
2841 if (io_req_cancelled(req))
2842 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002843 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002844 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002845}
2846
Pavel Begunkov014db002020-03-03 21:33:12 +03002847static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002848{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002849 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002850 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002851 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002852 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002853 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002854 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002855 return 0;
2856}
2857
Pavel Begunkov014db002020-03-03 21:33:12 +03002858static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002859{
Jens Axboed63d1b52019-12-10 10:38:56 -07002860 int ret;
2861
Jens Axboe4ed734b2020-03-20 11:23:41 -06002862 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
Jens Axboed63d1b52019-12-10 10:38:56 -07002863 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2864 req->sync.len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002865 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Jens Axboed63d1b52019-12-10 10:38:56 -07002866 if (ret < 0)
2867 req_set_fail_links(req);
2868 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002869 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002870}
2871
2872static void io_fallocate_finish(struct io_wq_work **workptr)
2873{
2874 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002875
Pavel Begunkov594506f2020-03-03 21:33:11 +03002876 if (io_req_cancelled(req))
2877 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002878 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002879 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002880}
2881
2882static int io_fallocate_prep(struct io_kiocb *req,
2883 const struct io_uring_sqe *sqe)
2884{
2885 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2886 return -EINVAL;
2887
2888 req->sync.off = READ_ONCE(sqe->off);
2889 req->sync.len = READ_ONCE(sqe->addr);
2890 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002891 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07002892 return 0;
2893}
2894
Pavel Begunkov014db002020-03-03 21:33:12 +03002895static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002896{
Jens Axboed63d1b52019-12-10 10:38:56 -07002897 /* fallocate always requiring blocking context */
2898 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002899 req->work.func = io_fallocate_finish;
2900 return -EAGAIN;
2901 }
2902
Pavel Begunkov014db002020-03-03 21:33:12 +03002903 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002904 return 0;
2905}
2906
Jens Axboe15b71ab2019-12-11 11:20:36 -07002907static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2908{
Jens Axboef8748882020-01-08 17:47:02 -07002909 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002910 int ret;
2911
2912 if (sqe->ioprio || sqe->buf_index)
2913 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002914 if (sqe->flags & IOSQE_FIXED_FILE)
2915 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002916 if (req->flags & REQ_F_NEED_CLEANUP)
2917 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002918
2919 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002920 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002921 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002922 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002923
Jens Axboef8748882020-01-08 17:47:02 -07002924 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002925 if (IS_ERR(req->open.filename)) {
2926 ret = PTR_ERR(req->open.filename);
2927 req->open.filename = NULL;
2928 return ret;
2929 }
2930
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002931 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002932 return 0;
2933}
2934
Jens Axboecebdb982020-01-08 17:59:24 -07002935static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2936{
2937 struct open_how __user *how;
2938 const char __user *fname;
2939 size_t len;
2940 int ret;
2941
2942 if (sqe->ioprio || sqe->buf_index)
2943 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002944 if (sqe->flags & IOSQE_FIXED_FILE)
2945 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002946 if (req->flags & REQ_F_NEED_CLEANUP)
2947 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002948
2949 req->open.dfd = READ_ONCE(sqe->fd);
2950 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2951 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2952 len = READ_ONCE(sqe->len);
2953
2954 if (len < OPEN_HOW_SIZE_VER0)
2955 return -EINVAL;
2956
2957 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2958 len);
2959 if (ret)
2960 return ret;
2961
2962 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2963 req->open.how.flags |= O_LARGEFILE;
2964
2965 req->open.filename = getname(fname);
2966 if (IS_ERR(req->open.filename)) {
2967 ret = PTR_ERR(req->open.filename);
2968 req->open.filename = NULL;
2969 return ret;
2970 }
2971
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002972 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002973 return 0;
2974}
2975
Pavel Begunkov014db002020-03-03 21:33:12 +03002976static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002977{
2978 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002979 struct file *file;
2980 int ret;
2981
Jens Axboef86cd202020-01-29 13:46:44 -07002982 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002983 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002984
Jens Axboecebdb982020-01-08 17:59:24 -07002985 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002986 if (ret)
2987 goto err;
2988
Jens Axboecebdb982020-01-08 17:59:24 -07002989 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002990 if (ret < 0)
2991 goto err;
2992
2993 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2994 if (IS_ERR(file)) {
2995 put_unused_fd(ret);
2996 ret = PTR_ERR(file);
2997 } else {
2998 fsnotify_open(file);
2999 fd_install(ret, file);
3000 }
3001err:
3002 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003003 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003004 if (ret < 0)
3005 req_set_fail_links(req);
3006 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003007 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003008 return 0;
3009}
3010
Pavel Begunkov014db002020-03-03 21:33:12 +03003011static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003012{
3013 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03003014 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003015}
3016
Jens Axboe067524e2020-03-02 16:32:28 -07003017static int io_remove_buffers_prep(struct io_kiocb *req,
3018 const struct io_uring_sqe *sqe)
3019{
3020 struct io_provide_buf *p = &req->pbuf;
3021 u64 tmp;
3022
3023 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3024 return -EINVAL;
3025
3026 tmp = READ_ONCE(sqe->fd);
3027 if (!tmp || tmp > USHRT_MAX)
3028 return -EINVAL;
3029
3030 memset(p, 0, sizeof(*p));
3031 p->nbufs = tmp;
3032 p->bgid = READ_ONCE(sqe->buf_group);
3033 return 0;
3034}
3035
3036static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3037 int bgid, unsigned nbufs)
3038{
3039 unsigned i = 0;
3040
3041 /* shouldn't happen */
3042 if (!nbufs)
3043 return 0;
3044
3045 /* the head kbuf is the list itself */
3046 while (!list_empty(&buf->list)) {
3047 struct io_buffer *nxt;
3048
3049 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3050 list_del(&nxt->list);
3051 kfree(nxt);
3052 if (++i == nbufs)
3053 return i;
3054 }
3055 i++;
3056 kfree(buf);
3057 idr_remove(&ctx->io_buffer_idr, bgid);
3058
3059 return i;
3060}
3061
3062static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3063{
3064 struct io_provide_buf *p = &req->pbuf;
3065 struct io_ring_ctx *ctx = req->ctx;
3066 struct io_buffer *head;
3067 int ret = 0;
3068
3069 io_ring_submit_lock(ctx, !force_nonblock);
3070
3071 lockdep_assert_held(&ctx->uring_lock);
3072
3073 ret = -ENOENT;
3074 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3075 if (head)
3076 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3077
3078 io_ring_submit_lock(ctx, !force_nonblock);
3079 if (ret < 0)
3080 req_set_fail_links(req);
3081 io_cqring_add_event(req, ret);
3082 io_put_req(req);
3083 return 0;
3084}
3085
Jens Axboeddf0322d2020-02-23 16:41:33 -07003086static int io_provide_buffers_prep(struct io_kiocb *req,
3087 const struct io_uring_sqe *sqe)
3088{
3089 struct io_provide_buf *p = &req->pbuf;
3090 u64 tmp;
3091
3092 if (sqe->ioprio || sqe->rw_flags)
3093 return -EINVAL;
3094
3095 tmp = READ_ONCE(sqe->fd);
3096 if (!tmp || tmp > USHRT_MAX)
3097 return -E2BIG;
3098 p->nbufs = tmp;
3099 p->addr = READ_ONCE(sqe->addr);
3100 p->len = READ_ONCE(sqe->len);
3101
3102 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3103 return -EFAULT;
3104
3105 p->bgid = READ_ONCE(sqe->buf_group);
3106 tmp = READ_ONCE(sqe->off);
3107 if (tmp > USHRT_MAX)
3108 return -E2BIG;
3109 p->bid = tmp;
3110 return 0;
3111}
3112
3113static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3114{
3115 struct io_buffer *buf;
3116 u64 addr = pbuf->addr;
3117 int i, bid = pbuf->bid;
3118
3119 for (i = 0; i < pbuf->nbufs; i++) {
3120 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3121 if (!buf)
3122 break;
3123
3124 buf->addr = addr;
3125 buf->len = pbuf->len;
3126 buf->bid = bid;
3127 addr += pbuf->len;
3128 bid++;
3129 if (!*head) {
3130 INIT_LIST_HEAD(&buf->list);
3131 *head = buf;
3132 } else {
3133 list_add_tail(&buf->list, &(*head)->list);
3134 }
3135 }
3136
3137 return i ? i : -ENOMEM;
3138}
3139
Jens Axboeddf0322d2020-02-23 16:41:33 -07003140static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3141{
3142 struct io_provide_buf *p = &req->pbuf;
3143 struct io_ring_ctx *ctx = req->ctx;
3144 struct io_buffer *head, *list;
3145 int ret = 0;
3146
3147 io_ring_submit_lock(ctx, !force_nonblock);
3148
3149 lockdep_assert_held(&ctx->uring_lock);
3150
3151 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3152
3153 ret = io_add_buffers(p, &head);
3154 if (ret < 0)
3155 goto out;
3156
3157 if (!list) {
3158 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3159 GFP_KERNEL);
3160 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003161 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003162 goto out;
3163 }
3164 }
3165out:
3166 io_ring_submit_unlock(ctx, !force_nonblock);
3167 if (ret < 0)
3168 req_set_fail_links(req);
3169 io_cqring_add_event(req, ret);
3170 io_put_req(req);
3171 return 0;
3172}
3173
Jens Axboe3e4827b2020-01-08 15:18:09 -07003174static int io_epoll_ctl_prep(struct io_kiocb *req,
3175 const struct io_uring_sqe *sqe)
3176{
3177#if defined(CONFIG_EPOLL)
3178 if (sqe->ioprio || sqe->buf_index)
3179 return -EINVAL;
3180
3181 req->epoll.epfd = READ_ONCE(sqe->fd);
3182 req->epoll.op = READ_ONCE(sqe->len);
3183 req->epoll.fd = READ_ONCE(sqe->off);
3184
3185 if (ep_op_has_event(req->epoll.op)) {
3186 struct epoll_event __user *ev;
3187
3188 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3189 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3190 return -EFAULT;
3191 }
3192
3193 return 0;
3194#else
3195 return -EOPNOTSUPP;
3196#endif
3197}
3198
Pavel Begunkov014db002020-03-03 21:33:12 +03003199static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003200{
3201#if defined(CONFIG_EPOLL)
3202 struct io_epoll *ie = &req->epoll;
3203 int ret;
3204
3205 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3206 if (force_nonblock && ret == -EAGAIN)
3207 return -EAGAIN;
3208
3209 if (ret < 0)
3210 req_set_fail_links(req);
3211 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003212 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003213 return 0;
3214#else
3215 return -EOPNOTSUPP;
3216#endif
3217}
3218
Jens Axboec1ca7572019-12-25 22:18:28 -07003219static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3220{
3221#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3222 if (sqe->ioprio || sqe->buf_index || sqe->off)
3223 return -EINVAL;
3224
3225 req->madvise.addr = READ_ONCE(sqe->addr);
3226 req->madvise.len = READ_ONCE(sqe->len);
3227 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3228 return 0;
3229#else
3230 return -EOPNOTSUPP;
3231#endif
3232}
3233
Pavel Begunkov014db002020-03-03 21:33:12 +03003234static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003235{
3236#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3237 struct io_madvise *ma = &req->madvise;
3238 int ret;
3239
3240 if (force_nonblock)
3241 return -EAGAIN;
3242
3243 ret = do_madvise(ma->addr, ma->len, ma->advice);
3244 if (ret < 0)
3245 req_set_fail_links(req);
3246 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003247 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003248 return 0;
3249#else
3250 return -EOPNOTSUPP;
3251#endif
3252}
3253
Jens Axboe4840e412019-12-25 22:03:45 -07003254static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3255{
3256 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3257 return -EINVAL;
3258
3259 req->fadvise.offset = READ_ONCE(sqe->off);
3260 req->fadvise.len = READ_ONCE(sqe->len);
3261 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3262 return 0;
3263}
3264
Pavel Begunkov014db002020-03-03 21:33:12 +03003265static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003266{
3267 struct io_fadvise *fa = &req->fadvise;
3268 int ret;
3269
Jens Axboe3e694262020-02-01 09:22:49 -07003270 if (force_nonblock) {
3271 switch (fa->advice) {
3272 case POSIX_FADV_NORMAL:
3273 case POSIX_FADV_RANDOM:
3274 case POSIX_FADV_SEQUENTIAL:
3275 break;
3276 default:
3277 return -EAGAIN;
3278 }
3279 }
Jens Axboe4840e412019-12-25 22:03:45 -07003280
3281 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3282 if (ret < 0)
3283 req_set_fail_links(req);
3284 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003285 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003286 return 0;
3287}
3288
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003289static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3290{
Jens Axboef8748882020-01-08 17:47:02 -07003291 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003292 unsigned lookup_flags;
3293 int ret;
3294
3295 if (sqe->ioprio || sqe->buf_index)
3296 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07003297 if (sqe->flags & IOSQE_FIXED_FILE)
3298 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003299 if (req->flags & REQ_F_NEED_CLEANUP)
3300 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003301
3302 req->open.dfd = READ_ONCE(sqe->fd);
3303 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07003304 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003305 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07003306 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003307
Jens Axboec12cedf2020-01-08 17:41:21 -07003308 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003309 return -EINVAL;
3310
Jens Axboef8748882020-01-08 17:47:02 -07003311 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003312 if (IS_ERR(req->open.filename)) {
3313 ret = PTR_ERR(req->open.filename);
3314 req->open.filename = NULL;
3315 return ret;
3316 }
3317
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003318 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003319 return 0;
3320}
3321
Pavel Begunkov014db002020-03-03 21:33:12 +03003322static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003323{
3324 struct io_open *ctx = &req->open;
3325 unsigned lookup_flags;
3326 struct path path;
3327 struct kstat stat;
3328 int ret;
3329
3330 if (force_nonblock)
3331 return -EAGAIN;
3332
Jens Axboec12cedf2020-01-08 17:41:21 -07003333 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003334 return -EINVAL;
3335
3336retry:
3337 /* filename_lookup() drops it, keep a reference */
3338 ctx->filename->refcnt++;
3339
3340 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
3341 NULL);
3342 if (ret)
3343 goto err;
3344
Jens Axboec12cedf2020-01-08 17:41:21 -07003345 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003346 path_put(&path);
3347 if (retry_estale(ret, lookup_flags)) {
3348 lookup_flags |= LOOKUP_REVAL;
3349 goto retry;
3350 }
3351 if (!ret)
3352 ret = cp_statx(&stat, ctx->buffer);
3353err:
3354 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003355 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003356 if (ret < 0)
3357 req_set_fail_links(req);
3358 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003359 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003360 return 0;
3361}
3362
Jens Axboeb5dba592019-12-11 14:02:38 -07003363static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3364{
3365 /*
3366 * If we queue this for async, it must not be cancellable. That would
3367 * leave the 'file' in an undeterminate state.
3368 */
3369 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3370
3371 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3372 sqe->rw_flags || sqe->buf_index)
3373 return -EINVAL;
3374 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003375 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003376
3377 req->close.fd = READ_ONCE(sqe->fd);
3378 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03003379 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07003380 return -EBADF;
3381
3382 return 0;
3383}
3384
Pavel Begunkova93b3332020-02-08 14:04:34 +03003385/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003386static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003387{
3388 int ret;
3389
3390 ret = filp_close(req->close.put_file, req->work.files);
3391 if (ret < 0)
3392 req_set_fail_links(req);
3393 io_cqring_add_event(req, ret);
3394 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003395 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003396}
3397
Jens Axboeb5dba592019-12-11 14:02:38 -07003398static void io_close_finish(struct io_wq_work **workptr)
3399{
3400 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003401
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003402 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003403 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003404 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003405}
3406
Pavel Begunkov014db002020-03-03 21:33:12 +03003407static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003408{
3409 int ret;
3410
3411 req->close.put_file = NULL;
3412 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3413 if (ret < 0)
3414 return ret;
3415
3416 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003417 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov594506f2020-03-03 21:33:11 +03003418 /* submission ref will be dropped, take it for async */
3419 refcount_inc(&req->refs);
3420
Pavel Begunkova2100672020-03-02 23:45:16 +03003421 req->work.func = io_close_finish;
3422 /*
3423 * Do manual async queue here to avoid grabbing files - we don't
3424 * need the files, and it'll cause io_close_finish() to close
3425 * the file again and cause a double CQE entry for this request
3426 */
3427 io_queue_async_work(req);
3428 return 0;
3429 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003430
3431 /*
3432 * No ->flush(), safely close from here and just punt the
3433 * fput() to async context.
3434 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003435 __io_close_finish(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003436 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003437}
3438
Jens Axboe3529d8c2019-12-19 18:24:38 -07003439static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003440{
3441 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003442
3443 if (!req->file)
3444 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003445
3446 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3447 return -EINVAL;
3448 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3449 return -EINVAL;
3450
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003451 req->sync.off = READ_ONCE(sqe->off);
3452 req->sync.len = READ_ONCE(sqe->len);
3453 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003454 return 0;
3455}
3456
Pavel Begunkov014db002020-03-03 21:33:12 +03003457static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003458{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003459 int ret;
3460
Jens Axboe9adbd452019-12-20 08:45:55 -07003461 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003462 req->sync.flags);
3463 if (ret < 0)
3464 req_set_fail_links(req);
3465 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003466 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003467}
3468
3469
3470static void io_sync_file_range_finish(struct io_wq_work **workptr)
3471{
3472 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3473 struct io_kiocb *nxt = NULL;
3474
3475 if (io_req_cancelled(req))
3476 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003477 __io_sync_file_range(req);
Pavel Begunkov594506f2020-03-03 21:33:11 +03003478 io_put_req(req); /* put submission ref */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003479 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003480 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003481}
3482
Pavel Begunkov014db002020-03-03 21:33:12 +03003483static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003484{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003485 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003486 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003487 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003488 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003489 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003490
Pavel Begunkov014db002020-03-03 21:33:12 +03003491 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003492 return 0;
3493}
3494
YueHaibing469956e2020-03-04 15:53:52 +08003495#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003496static int io_setup_async_msg(struct io_kiocb *req,
3497 struct io_async_msghdr *kmsg)
3498{
3499 if (req->io)
3500 return -EAGAIN;
3501 if (io_alloc_async_ctx(req)) {
3502 if (kmsg->iov != kmsg->fast_iov)
3503 kfree(kmsg->iov);
3504 return -ENOMEM;
3505 }
3506 req->flags |= REQ_F_NEED_CLEANUP;
3507 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3508 return -EAGAIN;
3509}
3510
Jens Axboe3529d8c2019-12-19 18:24:38 -07003511static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003512{
Jens Axboee47293f2019-12-20 08:58:21 -07003513 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003514 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003515 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003516
Jens Axboee47293f2019-12-20 08:58:21 -07003517 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3518 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003519 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003520
Jens Axboed8768362020-02-27 14:17:49 -07003521#ifdef CONFIG_COMPAT
3522 if (req->ctx->compat)
3523 sr->msg_flags |= MSG_CMSG_COMPAT;
3524#endif
3525
Jens Axboefddafac2020-01-04 20:19:44 -07003526 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003527 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003528 /* iovec is already imported */
3529 if (req->flags & REQ_F_NEED_CLEANUP)
3530 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003531
Jens Axboed9688562019-12-09 19:35:20 -07003532 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003533 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003534 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003535 if (!ret)
3536 req->flags |= REQ_F_NEED_CLEANUP;
3537 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003538}
3539
Pavel Begunkov014db002020-03-03 21:33:12 +03003540static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003541{
Jens Axboe0b416c32019-12-15 10:57:46 -07003542 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003543 struct socket *sock;
3544 int ret;
3545
3546 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3547 return -EINVAL;
3548
3549 sock = sock_from_file(req->file, &ret);
3550 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003551 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003552 unsigned flags;
3553
Jens Axboe03b12302019-12-02 18:50:25 -07003554 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003555 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003556 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003557 /* if iov is set, it's allocated already */
3558 if (!kmsg->iov)
3559 kmsg->iov = kmsg->fast_iov;
3560 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003561 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003562 struct io_sr_msg *sr = &req->sr_msg;
3563
Jens Axboe0b416c32019-12-15 10:57:46 -07003564 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003565 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003566
3567 io.msg.iov = io.msg.fast_iov;
3568 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3569 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003570 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003571 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003572 }
3573
Jens Axboee47293f2019-12-20 08:58:21 -07003574 flags = req->sr_msg.msg_flags;
3575 if (flags & MSG_DONTWAIT)
3576 req->flags |= REQ_F_NOWAIT;
3577 else if (force_nonblock)
3578 flags |= MSG_DONTWAIT;
3579
Jens Axboe0b416c32019-12-15 10:57:46 -07003580 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003581 if (force_nonblock && ret == -EAGAIN)
3582 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003583 if (ret == -ERESTARTSYS)
3584 ret = -EINTR;
3585 }
3586
Pavel Begunkov1e950812020-02-06 19:51:16 +03003587 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003588 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003589 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003590 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003591 if (ret < 0)
3592 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003593 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003594 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003595}
3596
Pavel Begunkov014db002020-03-03 21:33:12 +03003597static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003598{
Jens Axboefddafac2020-01-04 20:19:44 -07003599 struct socket *sock;
3600 int ret;
3601
3602 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3603 return -EINVAL;
3604
3605 sock = sock_from_file(req->file, &ret);
3606 if (sock) {
3607 struct io_sr_msg *sr = &req->sr_msg;
3608 struct msghdr msg;
3609 struct iovec iov;
3610 unsigned flags;
3611
3612 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3613 &msg.msg_iter);
3614 if (ret)
3615 return ret;
3616
3617 msg.msg_name = NULL;
3618 msg.msg_control = NULL;
3619 msg.msg_controllen = 0;
3620 msg.msg_namelen = 0;
3621
3622 flags = req->sr_msg.msg_flags;
3623 if (flags & MSG_DONTWAIT)
3624 req->flags |= REQ_F_NOWAIT;
3625 else if (force_nonblock)
3626 flags |= MSG_DONTWAIT;
3627
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003628 msg.msg_flags = flags;
3629 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003630 if (force_nonblock && ret == -EAGAIN)
3631 return -EAGAIN;
3632 if (ret == -ERESTARTSYS)
3633 ret = -EINTR;
3634 }
3635
3636 io_cqring_add_event(req, ret);
3637 if (ret < 0)
3638 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003639 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003640 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003641}
3642
Jens Axboe52de1fe2020-02-27 10:15:42 -07003643static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3644{
3645 struct io_sr_msg *sr = &req->sr_msg;
3646 struct iovec __user *uiov;
3647 size_t iov_len;
3648 int ret;
3649
3650 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3651 &uiov, &iov_len);
3652 if (ret)
3653 return ret;
3654
3655 if (req->flags & REQ_F_BUFFER_SELECT) {
3656 if (iov_len > 1)
3657 return -EINVAL;
3658 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3659 return -EFAULT;
3660 sr->len = io->msg.iov[0].iov_len;
3661 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3662 sr->len);
3663 io->msg.iov = NULL;
3664 } else {
3665 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3666 &io->msg.iov, &io->msg.msg.msg_iter);
3667 if (ret > 0)
3668 ret = 0;
3669 }
3670
3671 return ret;
3672}
3673
3674#ifdef CONFIG_COMPAT
3675static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3676 struct io_async_ctx *io)
3677{
3678 struct compat_msghdr __user *msg_compat;
3679 struct io_sr_msg *sr = &req->sr_msg;
3680 struct compat_iovec __user *uiov;
3681 compat_uptr_t ptr;
3682 compat_size_t len;
3683 int ret;
3684
3685 msg_compat = (struct compat_msghdr __user *) sr->msg;
3686 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3687 &ptr, &len);
3688 if (ret)
3689 return ret;
3690
3691 uiov = compat_ptr(ptr);
3692 if (req->flags & REQ_F_BUFFER_SELECT) {
3693 compat_ssize_t clen;
3694
3695 if (len > 1)
3696 return -EINVAL;
3697 if (!access_ok(uiov, sizeof(*uiov)))
3698 return -EFAULT;
3699 if (__get_user(clen, &uiov->iov_len))
3700 return -EFAULT;
3701 if (clen < 0)
3702 return -EINVAL;
3703 sr->len = io->msg.iov[0].iov_len;
3704 io->msg.iov = NULL;
3705 } else {
3706 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3707 &io->msg.iov,
3708 &io->msg.msg.msg_iter);
3709 if (ret < 0)
3710 return ret;
3711 }
3712
3713 return 0;
3714}
3715#endif
3716
3717static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3718{
3719 io->msg.iov = io->msg.fast_iov;
3720
3721#ifdef CONFIG_COMPAT
3722 if (req->ctx->compat)
3723 return __io_compat_recvmsg_copy_hdr(req, io);
3724#endif
3725
3726 return __io_recvmsg_copy_hdr(req, io);
3727}
3728
Jens Axboebcda7ba2020-02-23 16:42:51 -07003729static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3730 int *cflags, bool needs_lock)
3731{
3732 struct io_sr_msg *sr = &req->sr_msg;
3733 struct io_buffer *kbuf;
3734
3735 if (!(req->flags & REQ_F_BUFFER_SELECT))
3736 return NULL;
3737
3738 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3739 if (IS_ERR(kbuf))
3740 return kbuf;
3741
3742 sr->kbuf = kbuf;
3743 req->flags |= REQ_F_BUFFER_SELECTED;
3744
3745 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3746 *cflags |= IORING_CQE_F_BUFFER;
3747 return kbuf;
3748}
3749
Jens Axboe3529d8c2019-12-19 18:24:38 -07003750static int io_recvmsg_prep(struct io_kiocb *req,
3751 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003752{
Jens Axboee47293f2019-12-20 08:58:21 -07003753 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003754 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003755 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003756
Jens Axboe3529d8c2019-12-19 18:24:38 -07003757 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3758 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003759 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003760 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003761
Jens Axboed8768362020-02-27 14:17:49 -07003762#ifdef CONFIG_COMPAT
3763 if (req->ctx->compat)
3764 sr->msg_flags |= MSG_CMSG_COMPAT;
3765#endif
3766
Jens Axboefddafac2020-01-04 20:19:44 -07003767 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003768 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003769 /* iovec is already imported */
3770 if (req->flags & REQ_F_NEED_CLEANUP)
3771 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003772
Jens Axboe52de1fe2020-02-27 10:15:42 -07003773 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003774 if (!ret)
3775 req->flags |= REQ_F_NEED_CLEANUP;
3776 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003777}
3778
Pavel Begunkov014db002020-03-03 21:33:12 +03003779static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003780{
Jens Axboe0b416c32019-12-15 10:57:46 -07003781 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003782 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003783 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003784
3785 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3786 return -EINVAL;
3787
3788 sock = sock_from_file(req->file, &ret);
3789 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003790 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003791 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003792 unsigned flags;
3793
Jens Axboe03b12302019-12-02 18:50:25 -07003794 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003795 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003796 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003797 /* if iov is set, it's allocated already */
3798 if (!kmsg->iov)
3799 kmsg->iov = kmsg->fast_iov;
3800 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003801 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003802 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003803 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003804
Jens Axboe52de1fe2020-02-27 10:15:42 -07003805 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003806 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003807 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003808 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003809
Jens Axboe52de1fe2020-02-27 10:15:42 -07003810 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3811 if (IS_ERR(kbuf)) {
3812 return PTR_ERR(kbuf);
3813 } else if (kbuf) {
3814 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3815 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3816 1, req->sr_msg.len);
3817 }
3818
Jens Axboee47293f2019-12-20 08:58:21 -07003819 flags = req->sr_msg.msg_flags;
3820 if (flags & MSG_DONTWAIT)
3821 req->flags |= REQ_F_NOWAIT;
3822 else if (force_nonblock)
3823 flags |= MSG_DONTWAIT;
3824
3825 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3826 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003827 if (force_nonblock && ret == -EAGAIN)
3828 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003829 if (ret == -ERESTARTSYS)
3830 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003831 }
3832
Pavel Begunkov1e950812020-02-06 19:51:16 +03003833 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003834 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003835 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003836 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003837 if (ret < 0)
3838 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003839 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003840 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003841}
3842
Pavel Begunkov014db002020-03-03 21:33:12 +03003843static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003844{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003845 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003846 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003847 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003848
3849 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3850 return -EINVAL;
3851
3852 sock = sock_from_file(req->file, &ret);
3853 if (sock) {
3854 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003855 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003856 struct msghdr msg;
3857 struct iovec iov;
3858 unsigned flags;
3859
Jens Axboebcda7ba2020-02-23 16:42:51 -07003860 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3861 if (IS_ERR(kbuf))
3862 return PTR_ERR(kbuf);
3863 else if (kbuf)
3864 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003865
Jens Axboebcda7ba2020-02-23 16:42:51 -07003866 ret = import_single_range(READ, buf, sr->len, &iov,
3867 &msg.msg_iter);
3868 if (ret) {
3869 kfree(kbuf);
3870 return ret;
3871 }
3872
3873 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003874 msg.msg_name = NULL;
3875 msg.msg_control = NULL;
3876 msg.msg_controllen = 0;
3877 msg.msg_namelen = 0;
3878 msg.msg_iocb = NULL;
3879 msg.msg_flags = 0;
3880
3881 flags = req->sr_msg.msg_flags;
3882 if (flags & MSG_DONTWAIT)
3883 req->flags |= REQ_F_NOWAIT;
3884 else if (force_nonblock)
3885 flags |= MSG_DONTWAIT;
3886
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003887 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003888 if (force_nonblock && ret == -EAGAIN)
3889 return -EAGAIN;
3890 if (ret == -ERESTARTSYS)
3891 ret = -EINTR;
3892 }
3893
Jens Axboebcda7ba2020-02-23 16:42:51 -07003894 kfree(kbuf);
3895 req->flags &= ~REQ_F_NEED_CLEANUP;
3896 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003897 if (ret < 0)
3898 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003899 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003900 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003901}
3902
Jens Axboe3529d8c2019-12-19 18:24:38 -07003903static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003904{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003905 struct io_accept *accept = &req->accept;
3906
Jens Axboe17f2fe32019-10-17 14:42:58 -06003907 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3908 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003909 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003910 return -EINVAL;
3911
Jens Axboed55e5f52019-12-11 16:12:15 -07003912 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3913 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003914 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003915 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003916}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003917
Pavel Begunkov014db002020-03-03 21:33:12 +03003918static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003919{
3920 struct io_accept *accept = &req->accept;
3921 unsigned file_flags;
3922 int ret;
3923
3924 file_flags = force_nonblock ? O_NONBLOCK : 0;
3925 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3926 accept->addr_len, accept->flags);
3927 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003928 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003929 if (ret == -ERESTARTSYS)
3930 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003931 if (ret < 0)
3932 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003933 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003934 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003935 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003936}
3937
3938static void io_accept_finish(struct io_wq_work **workptr)
3939{
3940 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003941
3942 if (io_req_cancelled(req))
3943 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003944 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003945 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003946}
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003947
Pavel Begunkov014db002020-03-03 21:33:12 +03003948static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003949{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003950 int ret;
3951
Pavel Begunkov014db002020-03-03 21:33:12 +03003952 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003953 if (ret == -EAGAIN && force_nonblock) {
3954 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003955 return -EAGAIN;
3956 }
3957 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003958}
3959
Jens Axboe3529d8c2019-12-19 18:24:38 -07003960static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003961{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003962 struct io_connect *conn = &req->connect;
3963 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003964
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003965 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3966 return -EINVAL;
3967 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3968 return -EINVAL;
3969
Jens Axboe3529d8c2019-12-19 18:24:38 -07003970 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3971 conn->addr_len = READ_ONCE(sqe->addr2);
3972
3973 if (!io)
3974 return 0;
3975
3976 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003977 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003978}
3979
Pavel Begunkov014db002020-03-03 21:33:12 +03003980static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003981{
Jens Axboef499a022019-12-02 16:28:46 -07003982 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003983 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003984 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003985
Jens Axboef499a022019-12-02 16:28:46 -07003986 if (req->io) {
3987 io = req->io;
3988 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003989 ret = move_addr_to_kernel(req->connect.addr,
3990 req->connect.addr_len,
3991 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003992 if (ret)
3993 goto out;
3994 io = &__io;
3995 }
3996
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003997 file_flags = force_nonblock ? O_NONBLOCK : 0;
3998
3999 ret = __sys_connect_file(req->file, &io->connect.address,
4000 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004001 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004002 if (req->io)
4003 return -EAGAIN;
4004 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004005 ret = -ENOMEM;
4006 goto out;
4007 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004008 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004009 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004010 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004011 if (ret == -ERESTARTSYS)
4012 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004013out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004014 if (ret < 0)
4015 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004016 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004017 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004018 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004019}
YueHaibing469956e2020-03-04 15:53:52 +08004020#else /* !CONFIG_NET */
4021static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4022{
4023 return -EOPNOTSUPP;
4024}
4025
4026static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
4027{
4028 return -EOPNOTSUPP;
4029}
4030
4031static int io_send(struct io_kiocb *req, bool force_nonblock)
4032{
4033 return -EOPNOTSUPP;
4034}
4035
4036static int io_recvmsg_prep(struct io_kiocb *req,
4037 const struct io_uring_sqe *sqe)
4038{
4039 return -EOPNOTSUPP;
4040}
4041
4042static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4043{
4044 return -EOPNOTSUPP;
4045}
4046
4047static int io_recv(struct io_kiocb *req, bool force_nonblock)
4048{
4049 return -EOPNOTSUPP;
4050}
4051
4052static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4053{
4054 return -EOPNOTSUPP;
4055}
4056
4057static int io_accept(struct io_kiocb *req, bool force_nonblock)
4058{
4059 return -EOPNOTSUPP;
4060}
4061
4062static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4063{
4064 return -EOPNOTSUPP;
4065}
4066
4067static int io_connect(struct io_kiocb *req, bool force_nonblock)
4068{
4069 return -EOPNOTSUPP;
4070}
4071#endif /* CONFIG_NET */
Jens Axboef8e85cf2019-11-23 14:24:24 -07004072
Jens Axboed7718a92020-02-14 22:23:12 -07004073struct io_poll_table {
4074 struct poll_table_struct pt;
4075 struct io_kiocb *req;
4076 int error;
4077};
4078
4079static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4080 struct wait_queue_head *head)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004081{
Jens Axboed7718a92020-02-14 22:23:12 -07004082 if (unlikely(poll->head)) {
4083 pt->error = -EINVAL;
4084 return;
4085 }
4086
4087 pt->error = 0;
4088 poll->head = head;
4089 add_wait_queue(head, &poll->wait);
4090}
4091
4092static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4093 struct poll_table_struct *p)
4094{
4095 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4096
4097 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4098}
4099
4100static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4101 __poll_t mask, task_work_func_t func)
4102{
4103 struct task_struct *tsk;
4104
4105 /* for instances that support it check for an event match first: */
4106 if (mask && !(mask & poll->events))
4107 return 0;
4108
4109 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4110
4111 list_del_init(&poll->wait.entry);
4112
4113 tsk = req->task;
4114 req->result = mask;
4115 init_task_work(&req->task_work, func);
4116 /*
4117 * If this fails, then the task is exiting. If that is the case, then
4118 * the exit check will ultimately cancel these work items. Hence we
4119 * don't need to check here and handle it specifically.
4120 */
4121 task_work_add(tsk, &req->task_work, true);
4122 wake_up_process(tsk);
4123 return 1;
4124}
4125
4126static void io_async_task_func(struct callback_head *cb)
4127{
4128 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4129 struct async_poll *apoll = req->apoll;
4130 struct io_ring_ctx *ctx = req->ctx;
4131
4132 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4133
4134 WARN_ON_ONCE(!list_empty(&req->apoll->poll.wait.entry));
4135
4136 if (hash_hashed(&req->hash_node)) {
4137 spin_lock_irq(&ctx->completion_lock);
4138 hash_del(&req->hash_node);
4139 spin_unlock_irq(&ctx->completion_lock);
4140 }
4141
4142 /* restore ->work in case we need to retry again */
4143 memcpy(&req->work, &apoll->work, sizeof(req->work));
4144
4145 __set_current_state(TASK_RUNNING);
4146 mutex_lock(&ctx->uring_lock);
4147 __io_queue_sqe(req, NULL);
4148 mutex_unlock(&ctx->uring_lock);
4149
4150 kfree(apoll);
4151}
4152
4153static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4154 void *key)
4155{
4156 struct io_kiocb *req = wait->private;
4157 struct io_poll_iocb *poll = &req->apoll->poll;
4158
4159 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4160 key_to_poll(key));
4161
4162 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4163}
4164
4165static void io_poll_req_insert(struct io_kiocb *req)
4166{
4167 struct io_ring_ctx *ctx = req->ctx;
4168 struct hlist_head *list;
4169
4170 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4171 hlist_add_head(&req->hash_node, list);
4172}
4173
4174static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4175 struct io_poll_iocb *poll,
4176 struct io_poll_table *ipt, __poll_t mask,
4177 wait_queue_func_t wake_func)
4178 __acquires(&ctx->completion_lock)
4179{
4180 struct io_ring_ctx *ctx = req->ctx;
4181 bool cancel = false;
4182
4183 poll->file = req->file;
4184 poll->head = NULL;
4185 poll->done = poll->canceled = false;
4186 poll->events = mask;
4187
4188 ipt->pt._key = mask;
4189 ipt->req = req;
4190 ipt->error = -EINVAL;
4191
4192 INIT_LIST_HEAD(&poll->wait.entry);
4193 init_waitqueue_func_entry(&poll->wait, wake_func);
4194 poll->wait.private = req;
4195
4196 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4197
4198 spin_lock_irq(&ctx->completion_lock);
4199 if (likely(poll->head)) {
4200 spin_lock(&poll->head->lock);
4201 if (unlikely(list_empty(&poll->wait.entry))) {
4202 if (ipt->error)
4203 cancel = true;
4204 ipt->error = 0;
4205 mask = 0;
4206 }
4207 if (mask || ipt->error)
4208 list_del_init(&poll->wait.entry);
4209 else if (cancel)
4210 WRITE_ONCE(poll->canceled, true);
4211 else if (!poll->done) /* actually waiting for an event */
4212 io_poll_req_insert(req);
4213 spin_unlock(&poll->head->lock);
4214 }
4215
4216 return mask;
4217}
4218
4219static bool io_arm_poll_handler(struct io_kiocb *req)
4220{
4221 const struct io_op_def *def = &io_op_defs[req->opcode];
4222 struct io_ring_ctx *ctx = req->ctx;
4223 struct async_poll *apoll;
4224 struct io_poll_table ipt;
4225 __poll_t mask, ret;
4226
4227 if (!req->file || !file_can_poll(req->file))
4228 return false;
4229 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4230 return false;
4231 if (!def->pollin && !def->pollout)
4232 return false;
4233
4234 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4235 if (unlikely(!apoll))
4236 return false;
4237
4238 req->flags |= REQ_F_POLLED;
4239 memcpy(&apoll->work, &req->work, sizeof(req->work));
4240
4241 /*
4242 * Don't need a reference here, as we're adding it to the task
4243 * task_works list. If the task exits, the list is pruned.
4244 */
4245 req->task = current;
4246 req->apoll = apoll;
4247 INIT_HLIST_NODE(&req->hash_node);
4248
Nathan Chancellor8755d972020-03-02 16:01:19 -07004249 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004250 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004251 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004252 if (def->pollout)
4253 mask |= POLLOUT | POLLWRNORM;
4254 mask |= POLLERR | POLLPRI;
4255
4256 ipt.pt._qproc = io_async_queue_proc;
4257
4258 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4259 io_async_wake);
4260 if (ret) {
4261 ipt.error = 0;
4262 apoll->poll.done = true;
4263 spin_unlock_irq(&ctx->completion_lock);
4264 memcpy(&req->work, &apoll->work, sizeof(req->work));
4265 kfree(apoll);
4266 return false;
4267 }
4268 spin_unlock_irq(&ctx->completion_lock);
4269 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4270 apoll->poll.events);
4271 return true;
4272}
4273
4274static bool __io_poll_remove_one(struct io_kiocb *req,
4275 struct io_poll_iocb *poll)
4276{
Jens Axboeb41e9852020-02-17 09:52:41 -07004277 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004278
4279 spin_lock(&poll->head->lock);
4280 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004281 if (!list_empty(&poll->wait.entry)) {
4282 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004283 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004284 }
4285 spin_unlock(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07004286 return do_complete;
4287}
4288
4289static bool io_poll_remove_one(struct io_kiocb *req)
4290{
4291 bool do_complete;
4292
4293 if (req->opcode == IORING_OP_POLL_ADD) {
4294 do_complete = __io_poll_remove_one(req, &req->poll);
4295 } else {
4296 /* non-poll requests have submit ref still */
4297 do_complete = __io_poll_remove_one(req, &req->apoll->poll);
4298 if (do_complete)
4299 io_put_req(req);
4300 }
4301
Jens Axboe78076bb2019-12-04 19:56:40 -07004302 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004303
Jens Axboeb41e9852020-02-17 09:52:41 -07004304 if (do_complete) {
4305 io_cqring_fill_event(req, -ECANCELED);
4306 io_commit_cqring(req->ctx);
4307 req->flags |= REQ_F_COMP_LOCKED;
4308 io_put_req(req);
4309 }
4310
4311 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004312}
4313
4314static void io_poll_remove_all(struct io_ring_ctx *ctx)
4315{
Jens Axboe78076bb2019-12-04 19:56:40 -07004316 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004317 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07004318 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004319
4320 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004321 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4322 struct hlist_head *list;
4323
4324 list = &ctx->cancel_hash[i];
4325 hlist_for_each_entry_safe(req, tmp, list, hash_node)
4326 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004327 }
4328 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004329
4330 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004331}
4332
Jens Axboe47f46762019-11-09 17:43:02 -07004333static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4334{
Jens Axboe78076bb2019-12-04 19:56:40 -07004335 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004336 struct io_kiocb *req;
4337
Jens Axboe78076bb2019-12-04 19:56:40 -07004338 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4339 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004340 if (sqe_addr != req->user_data)
4341 continue;
4342 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004343 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004344 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004345 }
4346
4347 return -ENOENT;
4348}
4349
Jens Axboe3529d8c2019-12-19 18:24:38 -07004350static int io_poll_remove_prep(struct io_kiocb *req,
4351 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004352{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004353 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4354 return -EINVAL;
4355 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4356 sqe->poll_events)
4357 return -EINVAL;
4358
Jens Axboe0969e782019-12-17 18:40:57 -07004359 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004360 return 0;
4361}
4362
4363/*
4364 * Find a running poll command that matches one specified in sqe->addr,
4365 * and remove it if found.
4366 */
4367static int io_poll_remove(struct io_kiocb *req)
4368{
4369 struct io_ring_ctx *ctx = req->ctx;
4370 u64 addr;
4371 int ret;
4372
Jens Axboe0969e782019-12-17 18:40:57 -07004373 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004374 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004375 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004376 spin_unlock_irq(&ctx->completion_lock);
4377
Jens Axboe78e19bb2019-11-06 15:21:34 -07004378 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004379 if (ret < 0)
4380 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004381 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004382 return 0;
4383}
4384
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004385static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004386{
Jackie Liua197f662019-11-08 08:09:12 -07004387 struct io_ring_ctx *ctx = req->ctx;
4388
Jens Axboe8c838782019-03-12 15:48:16 -06004389 req->poll.done = true;
Pavel Begunkovb0a20342020-02-28 10:36:35 +03004390 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06004391 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004392}
4393
Jens Axboeb41e9852020-02-17 09:52:41 -07004394static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004395{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004396 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004397
Jens Axboe221c5eb2019-01-17 09:41:58 -07004398 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004399 hash_del(&req->hash_node);
Jens Axboeb41e9852020-02-17 09:52:41 -07004400 io_poll_complete(req, req->result, 0);
4401 req->flags |= REQ_F_COMP_LOCKED;
4402 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004403 spin_unlock_irq(&ctx->completion_lock);
4404
Jens Axboe8c838782019-03-12 15:48:16 -06004405 io_cqring_ev_posted(ctx);
Jens Axboeb41e9852020-02-17 09:52:41 -07004406}
Jens Axboe89723d02019-11-05 15:32:58 -07004407
Jens Axboeb41e9852020-02-17 09:52:41 -07004408static void io_poll_task_func(struct callback_head *cb)
4409{
4410 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4411 struct io_kiocb *nxt = NULL;
4412
4413 io_poll_task_handler(req, &nxt);
Jens Axboed7718a92020-02-14 22:23:12 -07004414 if (nxt) {
4415 struct io_ring_ctx *ctx = nxt->ctx;
4416
4417 mutex_lock(&ctx->uring_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004418 __io_queue_sqe(nxt, NULL);
Jens Axboed7718a92020-02-14 22:23:12 -07004419 mutex_unlock(&ctx->uring_lock);
4420 }
Jens Axboef0b493e2020-02-01 21:30:11 -07004421}
4422
Jens Axboe221c5eb2019-01-17 09:41:58 -07004423static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4424 void *key)
4425{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004426 struct io_kiocb *req = wait->private;
4427 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004428
Jens Axboed7718a92020-02-14 22:23:12 -07004429 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004430}
4431
Jens Axboe221c5eb2019-01-17 09:41:58 -07004432static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4433 struct poll_table_struct *p)
4434{
4435 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4436
Jens Axboed7718a92020-02-14 22:23:12 -07004437 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004438}
4439
Jens Axboe3529d8c2019-12-19 18:24:38 -07004440static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004441{
4442 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004443 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004444
4445 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4446 return -EINVAL;
4447 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4448 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004449 if (!poll->file)
4450 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004451
Jens Axboe221c5eb2019-01-17 09:41:58 -07004452 events = READ_ONCE(sqe->poll_events);
4453 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004454
Jens Axboed7718a92020-02-14 22:23:12 -07004455 /*
4456 * Don't need a reference here, as we're adding it to the task
4457 * task_works list. If the task exits, the list is pruned.
4458 */
Jens Axboeb41e9852020-02-17 09:52:41 -07004459 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004460 return 0;
4461}
4462
Pavel Begunkov014db002020-03-03 21:33:12 +03004463static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004464{
4465 struct io_poll_iocb *poll = &req->poll;
4466 struct io_ring_ctx *ctx = req->ctx;
4467 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004468 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004469
Jens Axboe78076bb2019-12-04 19:56:40 -07004470 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004471 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004472 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004473
Jens Axboed7718a92020-02-14 22:23:12 -07004474 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4475 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004476
Jens Axboe8c838782019-03-12 15:48:16 -06004477 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004478 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004479 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004480 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004481 spin_unlock_irq(&ctx->completion_lock);
4482
Jens Axboe8c838782019-03-12 15:48:16 -06004483 if (mask) {
4484 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004485 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004486 }
Jens Axboe8c838782019-03-12 15:48:16 -06004487 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004488}
4489
Jens Axboe5262f562019-09-17 12:26:57 -06004490static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4491{
Jens Axboead8a48a2019-11-15 08:49:11 -07004492 struct io_timeout_data *data = container_of(timer,
4493 struct io_timeout_data, timer);
4494 struct io_kiocb *req = data->req;
4495 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004496 unsigned long flags;
4497
Jens Axboe5262f562019-09-17 12:26:57 -06004498 atomic_inc(&ctx->cq_timeouts);
4499
4500 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004501 /*
Jens Axboe11365042019-10-16 09:08:32 -06004502 * We could be racing with timeout deletion. If the list is empty,
4503 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004504 */
Jens Axboe842f9612019-10-29 12:34:10 -06004505 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06004506 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06004507
Jens Axboe11365042019-10-16 09:08:32 -06004508 /*
4509 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08004510 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06004511 * pointer will be increased, otherwise other timeout reqs may
4512 * return in advance without waiting for enough wait_nr.
4513 */
4514 prev = req;
4515 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
4516 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06004517 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06004518 }
Jens Axboe842f9612019-10-29 12:34:10 -06004519
Jens Axboe78e19bb2019-11-06 15:21:34 -07004520 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004521 io_commit_cqring(ctx);
4522 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4523
4524 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004525 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004526 io_put_req(req);
4527 return HRTIMER_NORESTART;
4528}
4529
Jens Axboe47f46762019-11-09 17:43:02 -07004530static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4531{
4532 struct io_kiocb *req;
4533 int ret = -ENOENT;
4534
4535 list_for_each_entry(req, &ctx->timeout_list, list) {
4536 if (user_data == req->user_data) {
4537 list_del_init(&req->list);
4538 ret = 0;
4539 break;
4540 }
4541 }
4542
4543 if (ret == -ENOENT)
4544 return ret;
4545
Jens Axboe2d283902019-12-04 11:08:05 -07004546 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004547 if (ret == -1)
4548 return -EALREADY;
4549
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004550 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004551 io_cqring_fill_event(req, -ECANCELED);
4552 io_put_req(req);
4553 return 0;
4554}
4555
Jens Axboe3529d8c2019-12-19 18:24:38 -07004556static int io_timeout_remove_prep(struct io_kiocb *req,
4557 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004558{
Jens Axboeb29472e2019-12-17 18:50:29 -07004559 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4560 return -EINVAL;
4561 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4562 return -EINVAL;
4563
4564 req->timeout.addr = READ_ONCE(sqe->addr);
4565 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4566 if (req->timeout.flags)
4567 return -EINVAL;
4568
Jens Axboeb29472e2019-12-17 18:50:29 -07004569 return 0;
4570}
4571
Jens Axboe11365042019-10-16 09:08:32 -06004572/*
4573 * Remove or update an existing timeout command
4574 */
Jens Axboefc4df992019-12-10 14:38:45 -07004575static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004576{
4577 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004578 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004579
Jens Axboe11365042019-10-16 09:08:32 -06004580 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004581 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004582
Jens Axboe47f46762019-11-09 17:43:02 -07004583 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004584 io_commit_cqring(ctx);
4585 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004586 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004587 if (ret < 0)
4588 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004589 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004590 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004591}
4592
Jens Axboe3529d8c2019-12-19 18:24:38 -07004593static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004594 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004595{
Jens Axboead8a48a2019-11-15 08:49:11 -07004596 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004597 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004598
Jens Axboead8a48a2019-11-15 08:49:11 -07004599 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004600 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004601 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004602 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004603 if (sqe->off && is_timeout_link)
4604 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004605 flags = READ_ONCE(sqe->timeout_flags);
4606 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004607 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004608
Jens Axboe26a61672019-12-20 09:02:01 -07004609 req->timeout.count = READ_ONCE(sqe->off);
4610
Jens Axboe3529d8c2019-12-19 18:24:38 -07004611 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004612 return -ENOMEM;
4613
4614 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004615 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004616 req->flags |= REQ_F_TIMEOUT;
4617
4618 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004619 return -EFAULT;
4620
Jens Axboe11365042019-10-16 09:08:32 -06004621 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004622 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004623 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004624 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004625
Jens Axboead8a48a2019-11-15 08:49:11 -07004626 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4627 return 0;
4628}
4629
Jens Axboefc4df992019-12-10 14:38:45 -07004630static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004631{
4632 unsigned count;
4633 struct io_ring_ctx *ctx = req->ctx;
4634 struct io_timeout_data *data;
4635 struct list_head *entry;
4636 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004637
Jens Axboe2d283902019-12-04 11:08:05 -07004638 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004639
Jens Axboe5262f562019-09-17 12:26:57 -06004640 /*
4641 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004642 * timeout event to be satisfied. If it isn't set, then this is
4643 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004644 */
Jens Axboe26a61672019-12-20 09:02:01 -07004645 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004646 if (!count) {
4647 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4648 spin_lock_irq(&ctx->completion_lock);
4649 entry = ctx->timeout_list.prev;
4650 goto add;
4651 }
Jens Axboe5262f562019-09-17 12:26:57 -06004652
4653 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004654 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004655
4656 /*
4657 * Insertion sort, ensuring the first entry in the list is always
4658 * the one we need first.
4659 */
Jens Axboe5262f562019-09-17 12:26:57 -06004660 spin_lock_irq(&ctx->completion_lock);
4661 list_for_each_prev(entry, &ctx->timeout_list) {
4662 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004663 unsigned nxt_sq_head;
4664 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004665 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004666
Jens Axboe93bd25b2019-11-11 23:34:31 -07004667 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4668 continue;
4669
yangerkun5da0fb12019-10-15 21:59:29 +08004670 /*
4671 * Since cached_sq_head + count - 1 can overflow, use type long
4672 * long to store it.
4673 */
4674 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004675 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4676 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004677
4678 /*
4679 * cached_sq_head may overflow, and it will never overflow twice
4680 * once there is some timeout req still be valid.
4681 */
4682 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004683 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004684
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004685 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004686 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004687
4688 /*
4689 * Sequence of reqs after the insert one and itself should
4690 * be adjusted because each timeout req consumes a slot.
4691 */
4692 span++;
4693 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004694 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004695 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004696add:
Jens Axboe5262f562019-09-17 12:26:57 -06004697 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004698 data->timer.function = io_timeout_fn;
4699 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004700 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004701 return 0;
4702}
4703
Jens Axboe62755e32019-10-28 21:49:21 -06004704static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004705{
Jens Axboe62755e32019-10-28 21:49:21 -06004706 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004707
Jens Axboe62755e32019-10-28 21:49:21 -06004708 return req->user_data == (unsigned long) data;
4709}
4710
Jens Axboee977d6d2019-11-05 12:39:45 -07004711static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004712{
Jens Axboe62755e32019-10-28 21:49:21 -06004713 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004714 int ret = 0;
4715
Jens Axboe62755e32019-10-28 21:49:21 -06004716 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4717 switch (cancel_ret) {
4718 case IO_WQ_CANCEL_OK:
4719 ret = 0;
4720 break;
4721 case IO_WQ_CANCEL_RUNNING:
4722 ret = -EALREADY;
4723 break;
4724 case IO_WQ_CANCEL_NOTFOUND:
4725 ret = -ENOENT;
4726 break;
4727 }
4728
Jens Axboee977d6d2019-11-05 12:39:45 -07004729 return ret;
4730}
4731
Jens Axboe47f46762019-11-09 17:43:02 -07004732static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4733 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004734 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004735{
4736 unsigned long flags;
4737 int ret;
4738
4739 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4740 if (ret != -ENOENT) {
4741 spin_lock_irqsave(&ctx->completion_lock, flags);
4742 goto done;
4743 }
4744
4745 spin_lock_irqsave(&ctx->completion_lock, flags);
4746 ret = io_timeout_cancel(ctx, sqe_addr);
4747 if (ret != -ENOENT)
4748 goto done;
4749 ret = io_poll_cancel(ctx, sqe_addr);
4750done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004751 if (!ret)
4752 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004753 io_cqring_fill_event(req, ret);
4754 io_commit_cqring(ctx);
4755 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4756 io_cqring_ev_posted(ctx);
4757
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004758 if (ret < 0)
4759 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004760 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004761}
4762
Jens Axboe3529d8c2019-12-19 18:24:38 -07004763static int io_async_cancel_prep(struct io_kiocb *req,
4764 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004765{
Jens Axboefbf23842019-12-17 18:45:56 -07004766 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004767 return -EINVAL;
4768 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4769 sqe->cancel_flags)
4770 return -EINVAL;
4771
Jens Axboefbf23842019-12-17 18:45:56 -07004772 req->cancel.addr = READ_ONCE(sqe->addr);
4773 return 0;
4774}
4775
Pavel Begunkov014db002020-03-03 21:33:12 +03004776static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004777{
4778 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004779
Pavel Begunkov014db002020-03-03 21:33:12 +03004780 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004781 return 0;
4782}
4783
Jens Axboe05f3fb32019-12-09 11:22:50 -07004784static int io_files_update_prep(struct io_kiocb *req,
4785 const struct io_uring_sqe *sqe)
4786{
4787 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4788 return -EINVAL;
4789
4790 req->files_update.offset = READ_ONCE(sqe->off);
4791 req->files_update.nr_args = READ_ONCE(sqe->len);
4792 if (!req->files_update.nr_args)
4793 return -EINVAL;
4794 req->files_update.arg = READ_ONCE(sqe->addr);
4795 return 0;
4796}
4797
4798static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4799{
4800 struct io_ring_ctx *ctx = req->ctx;
4801 struct io_uring_files_update up;
4802 int ret;
4803
Jens Axboef86cd202020-01-29 13:46:44 -07004804 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004805 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004806
4807 up.offset = req->files_update.offset;
4808 up.fds = req->files_update.arg;
4809
4810 mutex_lock(&ctx->uring_lock);
4811 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4812 mutex_unlock(&ctx->uring_lock);
4813
4814 if (ret < 0)
4815 req_set_fail_links(req);
4816 io_cqring_add_event(req, ret);
4817 io_put_req(req);
4818 return 0;
4819}
4820
Jens Axboe3529d8c2019-12-19 18:24:38 -07004821static int io_req_defer_prep(struct io_kiocb *req,
4822 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004823{
Jens Axboee7815732019-12-17 19:45:06 -07004824 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004825
Jens Axboef86cd202020-01-29 13:46:44 -07004826 if (io_op_defs[req->opcode].file_table) {
4827 ret = io_grab_files(req);
4828 if (unlikely(ret))
4829 return ret;
4830 }
4831
Jens Axboecccf0ee2020-01-27 16:34:48 -07004832 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4833
Jens Axboed625c6e2019-12-17 19:53:05 -07004834 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004835 case IORING_OP_NOP:
4836 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004837 case IORING_OP_READV:
4838 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004839 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004840 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004841 break;
4842 case IORING_OP_WRITEV:
4843 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004844 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004845 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004846 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004847 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004848 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004849 break;
4850 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004851 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004852 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004853 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004854 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004855 break;
4856 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004857 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004858 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004859 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004860 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004861 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004862 break;
4863 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004864 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004865 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004866 break;
Jens Axboef499a022019-12-02 16:28:46 -07004867 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004868 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004869 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004870 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004871 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004872 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004873 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004874 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004875 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004876 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004877 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004878 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004879 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004880 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004881 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004882 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004883 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004884 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004885 case IORING_OP_FALLOCATE:
4886 ret = io_fallocate_prep(req, sqe);
4887 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004888 case IORING_OP_OPENAT:
4889 ret = io_openat_prep(req, sqe);
4890 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004891 case IORING_OP_CLOSE:
4892 ret = io_close_prep(req, sqe);
4893 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004894 case IORING_OP_FILES_UPDATE:
4895 ret = io_files_update_prep(req, sqe);
4896 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004897 case IORING_OP_STATX:
4898 ret = io_statx_prep(req, sqe);
4899 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004900 case IORING_OP_FADVISE:
4901 ret = io_fadvise_prep(req, sqe);
4902 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004903 case IORING_OP_MADVISE:
4904 ret = io_madvise_prep(req, sqe);
4905 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004906 case IORING_OP_OPENAT2:
4907 ret = io_openat2_prep(req, sqe);
4908 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004909 case IORING_OP_EPOLL_CTL:
4910 ret = io_epoll_ctl_prep(req, sqe);
4911 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004912 case IORING_OP_SPLICE:
4913 ret = io_splice_prep(req, sqe);
4914 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004915 case IORING_OP_PROVIDE_BUFFERS:
4916 ret = io_provide_buffers_prep(req, sqe);
4917 break;
Jens Axboe067524e2020-03-02 16:32:28 -07004918 case IORING_OP_REMOVE_BUFFERS:
4919 ret = io_remove_buffers_prep(req, sqe);
4920 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004921 default:
Jens Axboee7815732019-12-17 19:45:06 -07004922 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4923 req->opcode);
4924 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004925 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004926 }
4927
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004928 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004929}
4930
Jens Axboe3529d8c2019-12-19 18:24:38 -07004931static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004932{
Jackie Liua197f662019-11-08 08:09:12 -07004933 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004934 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004935
Bob Liu9d858b22019-11-13 18:06:25 +08004936 /* Still need defer if there is pending req in defer list. */
4937 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004938 return 0;
4939
Jens Axboe3529d8c2019-12-19 18:24:38 -07004940 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004941 return -EAGAIN;
4942
Jens Axboe3529d8c2019-12-19 18:24:38 -07004943 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004944 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004945 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004946
Jens Axboede0617e2019-04-06 21:51:27 -06004947 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004948 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004949 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004950 return 0;
4951 }
4952
Jens Axboe915967f2019-11-21 09:01:20 -07004953 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004954 list_add_tail(&req->list, &ctx->defer_list);
4955 spin_unlock_irq(&ctx->completion_lock);
4956 return -EIOCBQUEUED;
4957}
4958
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004959static void io_cleanup_req(struct io_kiocb *req)
4960{
4961 struct io_async_ctx *io = req->io;
4962
4963 switch (req->opcode) {
4964 case IORING_OP_READV:
4965 case IORING_OP_READ_FIXED:
4966 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07004967 if (req->flags & REQ_F_BUFFER_SELECTED)
4968 kfree((void *)(unsigned long)req->rw.addr);
4969 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004970 case IORING_OP_WRITEV:
4971 case IORING_OP_WRITE_FIXED:
4972 case IORING_OP_WRITE:
4973 if (io->rw.iov != io->rw.fast_iov)
4974 kfree(io->rw.iov);
4975 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004976 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07004977 if (req->flags & REQ_F_BUFFER_SELECTED)
4978 kfree(req->sr_msg.kbuf);
4979 /* fallthrough */
4980 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004981 if (io->msg.iov != io->msg.fast_iov)
4982 kfree(io->msg.iov);
4983 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004984 case IORING_OP_RECV:
4985 if (req->flags & REQ_F_BUFFER_SELECTED)
4986 kfree(req->sr_msg.kbuf);
4987 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004988 case IORING_OP_OPENAT:
4989 case IORING_OP_OPENAT2:
4990 case IORING_OP_STATX:
4991 putname(req->open.filename);
4992 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004993 case IORING_OP_SPLICE:
4994 io_put_file(req, req->splice.file_in,
4995 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4996 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004997 }
4998
4999 req->flags &= ~REQ_F_NEED_CLEANUP;
5000}
5001
Jens Axboe3529d8c2019-12-19 18:24:38 -07005002static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005003 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005004{
Jackie Liua197f662019-11-08 08:09:12 -07005005 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005006 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005007
Jens Axboed625c6e2019-12-17 19:53:05 -07005008 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005009 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005010 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005011 break;
5012 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005013 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005014 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005015 if (sqe) {
5016 ret = io_read_prep(req, sqe, force_nonblock);
5017 if (ret < 0)
5018 break;
5019 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005020 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005021 break;
5022 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005023 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005024 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005025 if (sqe) {
5026 ret = io_write_prep(req, sqe, force_nonblock);
5027 if (ret < 0)
5028 break;
5029 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005030 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005031 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005032 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005033 if (sqe) {
5034 ret = io_prep_fsync(req, sqe);
5035 if (ret < 0)
5036 break;
5037 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005038 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005039 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005040 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005041 if (sqe) {
5042 ret = io_poll_add_prep(req, sqe);
5043 if (ret)
5044 break;
5045 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005046 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005047 break;
5048 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005049 if (sqe) {
5050 ret = io_poll_remove_prep(req, sqe);
5051 if (ret < 0)
5052 break;
5053 }
Jens Axboefc4df992019-12-10 14:38:45 -07005054 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005055 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005056 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005057 if (sqe) {
5058 ret = io_prep_sfr(req, sqe);
5059 if (ret < 0)
5060 break;
5061 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005062 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005063 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005064 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005065 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005066 if (sqe) {
5067 ret = io_sendmsg_prep(req, sqe);
5068 if (ret < 0)
5069 break;
5070 }
Jens Axboefddafac2020-01-04 20:19:44 -07005071 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005072 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005073 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005074 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005075 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005076 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005077 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005078 if (sqe) {
5079 ret = io_recvmsg_prep(req, sqe);
5080 if (ret)
5081 break;
5082 }
Jens Axboefddafac2020-01-04 20:19:44 -07005083 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005084 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005085 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005086 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005087 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005088 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005089 if (sqe) {
5090 ret = io_timeout_prep(req, sqe, false);
5091 if (ret)
5092 break;
5093 }
Jens Axboefc4df992019-12-10 14:38:45 -07005094 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005095 break;
Jens Axboe11365042019-10-16 09:08:32 -06005096 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005097 if (sqe) {
5098 ret = io_timeout_remove_prep(req, sqe);
5099 if (ret)
5100 break;
5101 }
Jens Axboefc4df992019-12-10 14:38:45 -07005102 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005103 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005104 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005105 if (sqe) {
5106 ret = io_accept_prep(req, sqe);
5107 if (ret)
5108 break;
5109 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005110 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005111 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005112 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005113 if (sqe) {
5114 ret = io_connect_prep(req, sqe);
5115 if (ret)
5116 break;
5117 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005118 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005119 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005120 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005121 if (sqe) {
5122 ret = io_async_cancel_prep(req, sqe);
5123 if (ret)
5124 break;
5125 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005126 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005127 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005128 case IORING_OP_FALLOCATE:
5129 if (sqe) {
5130 ret = io_fallocate_prep(req, sqe);
5131 if (ret)
5132 break;
5133 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005134 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005135 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005136 case IORING_OP_OPENAT:
5137 if (sqe) {
5138 ret = io_openat_prep(req, sqe);
5139 if (ret)
5140 break;
5141 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005142 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005143 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005144 case IORING_OP_CLOSE:
5145 if (sqe) {
5146 ret = io_close_prep(req, sqe);
5147 if (ret)
5148 break;
5149 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005150 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005151 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005152 case IORING_OP_FILES_UPDATE:
5153 if (sqe) {
5154 ret = io_files_update_prep(req, sqe);
5155 if (ret)
5156 break;
5157 }
5158 ret = io_files_update(req, force_nonblock);
5159 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005160 case IORING_OP_STATX:
5161 if (sqe) {
5162 ret = io_statx_prep(req, sqe);
5163 if (ret)
5164 break;
5165 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005166 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005167 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005168 case IORING_OP_FADVISE:
5169 if (sqe) {
5170 ret = io_fadvise_prep(req, sqe);
5171 if (ret)
5172 break;
5173 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005174 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005175 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005176 case IORING_OP_MADVISE:
5177 if (sqe) {
5178 ret = io_madvise_prep(req, sqe);
5179 if (ret)
5180 break;
5181 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005182 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005183 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005184 case IORING_OP_OPENAT2:
5185 if (sqe) {
5186 ret = io_openat2_prep(req, sqe);
5187 if (ret)
5188 break;
5189 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005190 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005191 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005192 case IORING_OP_EPOLL_CTL:
5193 if (sqe) {
5194 ret = io_epoll_ctl_prep(req, sqe);
5195 if (ret)
5196 break;
5197 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005198 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005199 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005200 case IORING_OP_SPLICE:
5201 if (sqe) {
5202 ret = io_splice_prep(req, sqe);
5203 if (ret < 0)
5204 break;
5205 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005206 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005207 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005208 case IORING_OP_PROVIDE_BUFFERS:
5209 if (sqe) {
5210 ret = io_provide_buffers_prep(req, sqe);
5211 if (ret)
5212 break;
5213 }
5214 ret = io_provide_buffers(req, force_nonblock);
5215 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005216 case IORING_OP_REMOVE_BUFFERS:
5217 if (sqe) {
5218 ret = io_remove_buffers_prep(req, sqe);
5219 if (ret)
5220 break;
5221 }
5222 ret = io_remove_buffers(req, force_nonblock);
5223 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005224 default:
5225 ret = -EINVAL;
5226 break;
5227 }
5228
Jens Axboedef596e2019-01-09 08:59:42 -07005229 if (ret)
5230 return ret;
5231
5232 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005233 const bool in_async = io_wq_current_is_worker();
5234
Jens Axboe9e645e112019-05-10 16:07:28 -06005235 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07005236 return -EAGAIN;
5237
Jens Axboe11ba8202020-01-15 21:51:17 -07005238 /* workqueue context doesn't hold uring_lock, grab it now */
5239 if (in_async)
5240 mutex_lock(&ctx->uring_lock);
5241
Jens Axboedef596e2019-01-09 08:59:42 -07005242 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005243
5244 if (in_async)
5245 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005246 }
5247
5248 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005249}
5250
Jens Axboe561fb042019-10-24 07:25:42 -06005251static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07005252{
Jens Axboe561fb042019-10-24 07:25:42 -06005253 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005254 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005255 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005256
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005257 /* if NO_CANCEL is set, we must still run the work */
5258 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5259 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005260 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005261 }
Jens Axboe31b51512019-01-18 22:56:34 -07005262
Jens Axboe561fb042019-10-24 07:25:42 -06005263 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005264 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005265 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005266 /*
5267 * We can get EAGAIN for polled IO even though we're
5268 * forcing a sync submission from here, since we can't
5269 * wait for request slots on the block side.
5270 */
5271 if (ret != -EAGAIN)
5272 break;
5273 cond_resched();
5274 } while (1);
5275 }
Jens Axboe31b51512019-01-18 22:56:34 -07005276
Jens Axboe561fb042019-10-24 07:25:42 -06005277 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005278 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005279 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005280 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005281 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005282
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005283 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005284}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005285
Jens Axboe15b71ab2019-12-11 11:20:36 -07005286static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005287{
Jens Axboed3656342019-12-18 09:50:26 -07005288 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07005289 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07005290 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07005291 return 0;
5292 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06005293}
5294
Jens Axboe65e19f52019-10-26 07:20:21 -06005295static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5296 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005297{
Jens Axboe65e19f52019-10-26 07:20:21 -06005298 struct fixed_file_table *table;
5299
Jens Axboe05f3fb32019-12-09 11:22:50 -07005300 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
5301 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06005302}
5303
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005304static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5305 int fd, struct file **out_file, bool fixed)
5306{
5307 struct io_ring_ctx *ctx = req->ctx;
5308 struct file *file;
5309
5310 if (fixed) {
5311 if (unlikely(!ctx->file_data ||
5312 (unsigned) fd >= ctx->nr_user_files))
5313 return -EBADF;
5314 fd = array_index_nospec(fd, ctx->nr_user_files);
5315 file = io_file_from_index(ctx, fd);
5316 if (!file)
5317 return -EBADF;
5318 percpu_ref_get(&ctx->file_data->refs);
5319 } else {
5320 trace_io_uring_file_get(ctx, fd);
5321 file = __io_file_get(state, fd);
5322 if (unlikely(!file))
5323 return -EBADF;
5324 }
5325
5326 *out_file = file;
5327 return 0;
5328}
5329
Jens Axboe3529d8c2019-12-19 18:24:38 -07005330static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
5331 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06005332{
5333 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07005334 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005335 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005336
Jens Axboe3529d8c2019-12-19 18:24:38 -07005337 flags = READ_ONCE(sqe->flags);
5338 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06005339
Jens Axboed3656342019-12-18 09:50:26 -07005340 if (!io_req_needs_file(req, fd))
5341 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06005342
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005343 fixed = (flags & IOSQE_FIXED_FILE);
5344 if (unlikely(!fixed && req->needs_fixed_file))
5345 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005346
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005347 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005348}
5349
Jackie Liua197f662019-11-08 08:09:12 -07005350static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005351{
Jens Axboefcb323c2019-10-24 12:39:47 -06005352 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005353 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005354
Jens Axboef86cd202020-01-29 13:46:44 -07005355 if (req->work.files)
5356 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005357 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005358 return -EBADF;
5359
Jens Axboefcb323c2019-10-24 12:39:47 -06005360 rcu_read_lock();
5361 spin_lock_irq(&ctx->inflight_lock);
5362 /*
5363 * We use the f_ops->flush() handler to ensure that we can flush
5364 * out work accessing these files if the fd is closed. Check if
5365 * the fd has changed since we started down this path, and disallow
5366 * this operation if it has.
5367 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005368 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005369 list_add(&req->inflight_entry, &ctx->inflight_list);
5370 req->flags |= REQ_F_INFLIGHT;
5371 req->work.files = current->files;
5372 ret = 0;
5373 }
5374 spin_unlock_irq(&ctx->inflight_lock);
5375 rcu_read_unlock();
5376
5377 return ret;
5378}
5379
Jens Axboe2665abf2019-11-05 12:40:47 -07005380static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5381{
Jens Axboead8a48a2019-11-15 08:49:11 -07005382 struct io_timeout_data *data = container_of(timer,
5383 struct io_timeout_data, timer);
5384 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005385 struct io_ring_ctx *ctx = req->ctx;
5386 struct io_kiocb *prev = NULL;
5387 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005388
5389 spin_lock_irqsave(&ctx->completion_lock, flags);
5390
5391 /*
5392 * We don't expect the list to be empty, that will only happen if we
5393 * race with the completion of the linked work.
5394 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005395 if (!list_empty(&req->link_list)) {
5396 prev = list_entry(req->link_list.prev, struct io_kiocb,
5397 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005398 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005399 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005400 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5401 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005402 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005403 }
5404
5405 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5406
5407 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005408 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005409 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005410 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005411 } else {
5412 io_cqring_add_event(req, -ETIME);
5413 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005414 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005415 return HRTIMER_NORESTART;
5416}
5417
Jens Axboead8a48a2019-11-15 08:49:11 -07005418static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005419{
Jens Axboe76a46e02019-11-10 23:34:16 -07005420 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005421
Jens Axboe76a46e02019-11-10 23:34:16 -07005422 /*
5423 * If the list is now empty, then our linked request finished before
5424 * we got a chance to setup the timer
5425 */
5426 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005427 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005428 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005429
Jens Axboead8a48a2019-11-15 08:49:11 -07005430 data->timer.function = io_link_timeout_fn;
5431 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5432 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005433 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005434 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005435
Jens Axboe2665abf2019-11-05 12:40:47 -07005436 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005437 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005438}
5439
Jens Axboead8a48a2019-11-15 08:49:11 -07005440static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005441{
5442 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005443
Jens Axboe2665abf2019-11-05 12:40:47 -07005444 if (!(req->flags & REQ_F_LINK))
5445 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005446 /* for polled retry, if flag is set, we already went through here */
5447 if (req->flags & REQ_F_POLLED)
5448 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005449
Pavel Begunkov44932332019-12-05 16:16:35 +03005450 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5451 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005452 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005453 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005454
Jens Axboe76a46e02019-11-10 23:34:16 -07005455 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005456 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005457}
5458
Jens Axboe3529d8c2019-12-19 18:24:38 -07005459static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005460{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005461 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005462 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005463 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005464 int ret;
5465
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005466again:
5467 linked_timeout = io_prep_linked_timeout(req);
5468
Jens Axboe193155c2020-02-22 23:22:19 -07005469 if (req->work.creds && req->work.creds != current_cred()) {
5470 if (old_creds)
5471 revert_creds(old_creds);
5472 if (old_creds == req->work.creds)
5473 old_creds = NULL; /* restored original creds */
5474 else
5475 old_creds = override_creds(req->work.creds);
5476 }
5477
Pavel Begunkov014db002020-03-03 21:33:12 +03005478 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005479
5480 /*
5481 * We async punt it if the file wasn't marked NOWAIT, or if the file
5482 * doesn't support non-blocking read/write attempts
5483 */
5484 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5485 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005486 if (io_arm_poll_handler(req)) {
5487 if (linked_timeout)
5488 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005489 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005490 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005491punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005492 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005493 ret = io_grab_files(req);
5494 if (ret)
5495 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005496 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005497
5498 /*
5499 * Queued up for async execution, worker will release
5500 * submit reference when the iocb is actually submitted.
5501 */
5502 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005503 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005504 }
Jens Axboee65ef562019-03-12 10:16:44 -06005505
Jens Axboefcb323c2019-10-24 12:39:47 -06005506err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005507 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005508 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005509 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005510
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005511 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005512 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005513 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005514 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005515 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005516 }
5517
Jens Axboee65ef562019-03-12 10:16:44 -06005518 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005519 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005520 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005521 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005522 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005523 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005524 if (nxt) {
5525 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005526
5527 if (req->flags & REQ_F_FORCE_ASYNC)
5528 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005529 goto again;
5530 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005531exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005532 if (old_creds)
5533 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005534}
5535
Jens Axboe3529d8c2019-12-19 18:24:38 -07005536static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005537{
5538 int ret;
5539
Jens Axboe3529d8c2019-12-19 18:24:38 -07005540 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005541 if (ret) {
5542 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005543fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005544 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005545 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005546 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005547 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005548 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005549 ret = io_req_defer_prep(req, sqe);
5550 if (unlikely(ret < 0))
5551 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07005552 /*
5553 * Never try inline submit of IOSQE_ASYNC is set, go straight
5554 * to async execution.
5555 */
5556 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5557 io_queue_async_work(req);
5558 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005559 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005560 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005561}
5562
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005563static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005564{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005565 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005566 io_cqring_add_event(req, -ECANCELED);
5567 io_double_put_req(req);
5568 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005569 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005570}
5571
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005572#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboebcda7ba2020-02-23 16:42:51 -07005573 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5574 IOSQE_BUFFER_SELECT)
Jens Axboe9e645e112019-05-10 16:07:28 -06005575
Jens Axboe3529d8c2019-12-19 18:24:38 -07005576static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5577 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005578{
Jackie Liua197f662019-11-08 08:09:12 -07005579 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005580 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07005581 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06005582
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005583 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06005584
5585 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005586 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005587 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03005588 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06005589 }
5590
Jens Axboebcda7ba2020-02-23 16:42:51 -07005591 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5592 !io_op_defs[req->opcode].buffer_select) {
5593 ret = -EOPNOTSUPP;
5594 goto err_req;
5595 }
5596
Jens Axboe75c6a032020-01-28 10:15:23 -07005597 id = READ_ONCE(sqe->personality);
5598 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07005599 req->work.creds = idr_find(&ctx->personality_idr, id);
5600 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07005601 ret = -EINVAL;
5602 goto err_req;
5603 }
Jens Axboe193155c2020-02-22 23:22:19 -07005604 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07005605 }
5606
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03005607 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005608 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
Jens Axboebcda7ba2020-02-23 16:42:51 -07005609 IOSQE_ASYNC | IOSQE_FIXED_FILE |
5610 IOSQE_BUFFER_SELECT);
Jens Axboe9e645e112019-05-10 16:07:28 -06005611
Jens Axboe3529d8c2019-12-19 18:24:38 -07005612 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06005613 if (unlikely(ret)) {
5614err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005615 io_cqring_add_event(req, ret);
5616 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005617 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06005618 }
5619
Jens Axboe9e645e112019-05-10 16:07:28 -06005620 /*
5621 * If we already have a head request, queue this one for async
5622 * submittal once the head completes. If we don't have a head but
5623 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5624 * submitted sync once the chain is complete. If none of those
5625 * conditions are true (normal request), then just queue it.
5626 */
5627 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005628 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005629
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005630 /*
5631 * Taking sequential execution of a link, draining both sides
5632 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5633 * requests in the link. So, it drains the head and the
5634 * next after the link request. The last one is done via
5635 * drain_next flag to persist the effect across calls.
5636 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005637 if (sqe_flags & IOSQE_IO_DRAIN) {
5638 head->flags |= REQ_F_IO_DRAIN;
5639 ctx->drain_next = 1;
5640 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005641 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005642 ret = -EAGAIN;
5643 goto err_req;
5644 }
5645
Jens Axboe3529d8c2019-12-19 18:24:38 -07005646 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005647 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005648 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005649 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005650 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005651 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005652 trace_io_uring_link(ctx, req, head);
5653 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005654
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005655 /* last request of a link, enqueue the link */
5656 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5657 io_queue_link_head(head);
5658 *link = NULL;
5659 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005660 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005661 if (unlikely(ctx->drain_next)) {
5662 req->flags |= REQ_F_IO_DRAIN;
5663 req->ctx->drain_next = 0;
5664 }
5665 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5666 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005667 INIT_LIST_HEAD(&req->link_list);
5668 ret = io_req_defer_prep(req, sqe);
5669 if (ret)
5670 req->flags |= REQ_F_FAIL_LINK;
5671 *link = req;
5672 } else {
5673 io_queue_sqe(req, sqe);
5674 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005675 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005676
5677 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005678}
5679
Jens Axboe9a56a232019-01-09 09:06:50 -07005680/*
5681 * Batched submission is done, ensure local IO is flushed out.
5682 */
5683static void io_submit_state_end(struct io_submit_state *state)
5684{
5685 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005686 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005687 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005688 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005689}
5690
5691/*
5692 * Start submission side cache.
5693 */
5694static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005695 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005696{
5697 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005698 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005699 state->file = NULL;
5700 state->ios_left = max_ios;
5701}
5702
Jens Axboe2b188cc2019-01-07 10:46:33 -07005703static void io_commit_sqring(struct io_ring_ctx *ctx)
5704{
Hristo Venev75b28af2019-08-26 17:23:46 +00005705 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005706
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005707 /*
5708 * Ensure any loads from the SQEs are done at this point,
5709 * since once we write the new head, the application could
5710 * write new data to them.
5711 */
5712 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005713}
5714
5715/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005716 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005717 * that is mapped by userspace. This means that care needs to be taken to
5718 * ensure that reads are stable, as we cannot rely on userspace always
5719 * being a good citizen. If members of the sqe are validated and then later
5720 * used, it's important that those reads are done through READ_ONCE() to
5721 * prevent a re-load down the line.
5722 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005723static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5724 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005725{
Hristo Venev75b28af2019-08-26 17:23:46 +00005726 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005727 unsigned head;
5728
5729 /*
5730 * The cached sq head (or cq tail) serves two purposes:
5731 *
5732 * 1) allows us to batch the cost of updating the user visible
5733 * head updates.
5734 * 2) allows the kernel side to track the head on its own, even
5735 * though the application is the one updating it.
5736 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005737 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005738 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005739 /*
5740 * All io need record the previous position, if LINK vs DARIN,
5741 * it can be used to mark the position of the first IO in the
5742 * link list.
5743 */
5744 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005745 *sqe_ptr = &ctx->sq_sqes[head];
5746 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5747 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005748 ctx->cached_sq_head++;
5749 return true;
5750 }
5751
5752 /* drop invalid entries */
5753 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005754 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005755 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005756 return false;
5757}
5758
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005759static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005760 struct file *ring_file, int ring_fd,
5761 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005762{
5763 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005764 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005765 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005766 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005767
Jens Axboec4a2ed72019-11-21 21:01:26 -07005768 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005769 if (test_bit(0, &ctx->sq_check_overflow)) {
5770 if (!list_empty(&ctx->cq_overflow_list) &&
5771 !io_cqring_overflow_flush(ctx, false))
5772 return -EBUSY;
5773 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005774
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005775 /* make sure SQ entry isn't read before tail */
5776 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005777
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005778 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5779 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005780
5781 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005782 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005783 statep = &state;
5784 }
5785
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005786 ctx->ring_fd = ring_fd;
5787 ctx->ring_file = ring_file;
5788
Jens Axboe6c271ce2019-01-10 11:22:30 -07005789 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005790 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005791 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005792 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005793
Pavel Begunkov196be952019-11-07 01:41:06 +03005794 req = io_get_req(ctx, statep);
5795 if (unlikely(!req)) {
5796 if (!submitted)
5797 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005798 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005799 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005800 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005801 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005802 break;
5803 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005804
Jens Axboed3656342019-12-18 09:50:26 -07005805 /* will complete beyond this point, count as submitted */
5806 submitted++;
5807
5808 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005809 err = -EINVAL;
5810fail_req:
5811 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005812 io_double_put_req(req);
5813 break;
5814 }
5815
5816 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005817 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005818 if (unlikely(mm_fault)) {
5819 err = -EFAULT;
5820 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005821 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005822 use_mm(ctx->sqo_mm);
5823 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005824 }
5825
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005826 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005827 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5828 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005829 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005830 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005831 }
5832
Pavel Begunkov9466f432020-01-25 22:34:01 +03005833 if (unlikely(submitted != nr)) {
5834 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5835
5836 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5837 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005838 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005839 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005840 if (statep)
5841 io_submit_state_end(&state);
5842
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005843 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5844 io_commit_sqring(ctx);
5845
Jens Axboe6c271ce2019-01-10 11:22:30 -07005846 return submitted;
5847}
5848
5849static int io_sq_thread(void *data)
5850{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005851 struct io_ring_ctx *ctx = data;
5852 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005853 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005854 mm_segment_t old_fs;
5855 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005856 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005857 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005858
Jens Axboe206aefd2019-11-07 18:27:42 -07005859 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005860
Jens Axboe6c271ce2019-01-10 11:22:30 -07005861 old_fs = get_fs();
5862 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005863 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005864
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005865 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005866 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005867 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005868
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005869 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005870 unsigned nr_events = 0;
5871
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005872 mutex_lock(&ctx->uring_lock);
5873 if (!list_empty(&ctx->poll_list))
5874 io_iopoll_getevents(ctx, &nr_events, 0);
5875 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005876 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005877 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005878 }
5879
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005880 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005881
5882 /*
5883 * If submit got -EBUSY, flag us as needing the application
5884 * to enter the kernel to reap and flush events.
5885 */
5886 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005887 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005888 * Drop cur_mm before scheduling, we can't hold it for
5889 * long periods (or over schedule()). Do this before
5890 * adding ourselves to the waitqueue, as the unuse/drop
5891 * may sleep.
5892 */
5893 if (cur_mm) {
5894 unuse_mm(cur_mm);
5895 mmput(cur_mm);
5896 cur_mm = NULL;
5897 }
5898
5899 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005900 * We're polling. If we're within the defined idle
5901 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005902 * to sleep. The exception is if we got EBUSY doing
5903 * more IO, we should wait for the application to
5904 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005905 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005906 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005907 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5908 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005909 if (current->task_works)
5910 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06005911 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005912 continue;
5913 }
5914
Jens Axboe6c271ce2019-01-10 11:22:30 -07005915 prepare_to_wait(&ctx->sqo_wait, &wait,
5916 TASK_INTERRUPTIBLE);
5917
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005918 /*
5919 * While doing polled IO, before going to sleep, we need
5920 * to check if there are new reqs added to poll_list, it
5921 * is because reqs may have been punted to io worker and
5922 * will be added to poll_list later, hence check the
5923 * poll_list again.
5924 */
5925 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5926 !list_empty_careful(&ctx->poll_list)) {
5927 finish_wait(&ctx->sqo_wait, &wait);
5928 continue;
5929 }
5930
Jens Axboe6c271ce2019-01-10 11:22:30 -07005931 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005932 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005933 /* make sure to read SQ tail after writing flags */
5934 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005935
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005936 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005937 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005938 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005939 finish_wait(&ctx->sqo_wait, &wait);
5940 break;
5941 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005942 if (current->task_works) {
5943 task_work_run();
5944 continue;
5945 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005946 if (signal_pending(current))
5947 flush_signals(current);
5948 schedule();
5949 finish_wait(&ctx->sqo_wait, &wait);
5950
Hristo Venev75b28af2019-08-26 17:23:46 +00005951 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005952 continue;
5953 }
5954 finish_wait(&ctx->sqo_wait, &wait);
5955
Hristo Venev75b28af2019-08-26 17:23:46 +00005956 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005957 }
5958
Jens Axboe8a4955f2019-12-09 14:52:35 -07005959 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005960 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005961 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005962 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005963 }
5964
Jens Axboeb41e9852020-02-17 09:52:41 -07005965 if (current->task_works)
5966 task_work_run();
5967
Jens Axboe6c271ce2019-01-10 11:22:30 -07005968 set_fs(old_fs);
5969 if (cur_mm) {
5970 unuse_mm(cur_mm);
5971 mmput(cur_mm);
5972 }
Jens Axboe181e4482019-11-25 08:52:30 -07005973 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005974
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005975 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005976
Jens Axboe6c271ce2019-01-10 11:22:30 -07005977 return 0;
5978}
5979
Jens Axboebda52162019-09-24 13:47:15 -06005980struct io_wait_queue {
5981 struct wait_queue_entry wq;
5982 struct io_ring_ctx *ctx;
5983 unsigned to_wait;
5984 unsigned nr_timeouts;
5985};
5986
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005987static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005988{
5989 struct io_ring_ctx *ctx = iowq->ctx;
5990
5991 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005992 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005993 * started waiting. For timeouts, we always want to return to userspace,
5994 * regardless of event count.
5995 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005996 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005997 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5998}
5999
6000static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6001 int wake_flags, void *key)
6002{
6003 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6004 wq);
6005
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006006 /* use noflush == true, as we can't safely rely on locking context */
6007 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006008 return -1;
6009
6010 return autoremove_wake_function(curr, mode, wake_flags, key);
6011}
6012
Jens Axboe2b188cc2019-01-07 10:46:33 -07006013/*
6014 * Wait until events become available, if we don't already have some. The
6015 * application must reap them itself, as they reside on the shared cq ring.
6016 */
6017static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6018 const sigset_t __user *sig, size_t sigsz)
6019{
Jens Axboebda52162019-09-24 13:47:15 -06006020 struct io_wait_queue iowq = {
6021 .wq = {
6022 .private = current,
6023 .func = io_wake_function,
6024 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6025 },
6026 .ctx = ctx,
6027 .to_wait = min_events,
6028 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006029 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006030 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006031
Jens Axboeb41e9852020-02-17 09:52:41 -07006032 do {
6033 if (io_cqring_events(ctx, false) >= min_events)
6034 return 0;
6035 if (!current->task_works)
6036 break;
6037 task_work_run();
6038 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006039
6040 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006041#ifdef CONFIG_COMPAT
6042 if (in_compat_syscall())
6043 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006044 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006045 else
6046#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006047 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006048
Jens Axboe2b188cc2019-01-07 10:46:33 -07006049 if (ret)
6050 return ret;
6051 }
6052
Jens Axboebda52162019-09-24 13:47:15 -06006053 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006054 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006055 do {
6056 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6057 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006058 if (current->task_works)
6059 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006060 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006061 break;
6062 schedule();
6063 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006064 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006065 break;
6066 }
6067 } while (1);
6068 finish_wait(&ctx->wait, &iowq.wq);
6069
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006070 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006071
Hristo Venev75b28af2019-08-26 17:23:46 +00006072 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006073}
6074
Jens Axboe6b063142019-01-10 22:13:58 -07006075static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6076{
6077#if defined(CONFIG_UNIX)
6078 if (ctx->ring_sock) {
6079 struct sock *sock = ctx->ring_sock->sk;
6080 struct sk_buff *skb;
6081
6082 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6083 kfree_skb(skb);
6084 }
6085#else
6086 int i;
6087
Jens Axboe65e19f52019-10-26 07:20:21 -06006088 for (i = 0; i < ctx->nr_user_files; i++) {
6089 struct file *file;
6090
6091 file = io_file_from_index(ctx, i);
6092 if (file)
6093 fput(file);
6094 }
Jens Axboe6b063142019-01-10 22:13:58 -07006095#endif
6096}
6097
Jens Axboe05f3fb32019-12-09 11:22:50 -07006098static void io_file_ref_kill(struct percpu_ref *ref)
6099{
6100 struct fixed_file_data *data;
6101
6102 data = container_of(ref, struct fixed_file_data, refs);
6103 complete(&data->done);
6104}
6105
Jens Axboe6b063142019-01-10 22:13:58 -07006106static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6107{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006108 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06006109 unsigned nr_tables, i;
6110
Jens Axboe05f3fb32019-12-09 11:22:50 -07006111 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006112 return -ENXIO;
6113
Jens Axboe05f3fb32019-12-09 11:22:50 -07006114 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07006115 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006116 wait_for_completion(&data->done);
6117 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006118 percpu_ref_exit(&data->refs);
6119
Jens Axboe6b063142019-01-10 22:13:58 -07006120 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006121 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6122 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006123 kfree(data->table[i].files);
6124 kfree(data->table);
6125 kfree(data);
6126 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006127 ctx->nr_user_files = 0;
6128 return 0;
6129}
6130
Jens Axboe6c271ce2019-01-10 11:22:30 -07006131static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6132{
6133 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07006134 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006135 /*
6136 * The park is a bit of a work-around, without it we get
6137 * warning spews on shutdown with SQPOLL set and affinity
6138 * set to a single CPU.
6139 */
Jens Axboe06058632019-04-13 09:26:03 -06006140 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006141 kthread_stop(ctx->sqo_thread);
6142 ctx->sqo_thread = NULL;
6143 }
6144}
6145
Jens Axboe6b063142019-01-10 22:13:58 -07006146static void io_finish_async(struct io_ring_ctx *ctx)
6147{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006148 io_sq_thread_stop(ctx);
6149
Jens Axboe561fb042019-10-24 07:25:42 -06006150 if (ctx->io_wq) {
6151 io_wq_destroy(ctx->io_wq);
6152 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006153 }
6154}
6155
6156#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006157/*
6158 * Ensure the UNIX gc is aware of our file set, so we are certain that
6159 * the io_uring can be safely unregistered on process exit, even if we have
6160 * loops in the file referencing.
6161 */
6162static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6163{
6164 struct sock *sk = ctx->ring_sock->sk;
6165 struct scm_fp_list *fpl;
6166 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006167 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006168
6169 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
6170 unsigned long inflight = ctx->user->unix_inflight + nr;
6171
6172 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
6173 return -EMFILE;
6174 }
6175
6176 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6177 if (!fpl)
6178 return -ENOMEM;
6179
6180 skb = alloc_skb(0, GFP_KERNEL);
6181 if (!skb) {
6182 kfree(fpl);
6183 return -ENOMEM;
6184 }
6185
6186 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006187
Jens Axboe08a45172019-10-03 08:11:03 -06006188 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006189 fpl->user = get_uid(ctx->user);
6190 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006191 struct file *file = io_file_from_index(ctx, i + offset);
6192
6193 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006194 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006195 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006196 unix_inflight(fpl->user, fpl->fp[nr_files]);
6197 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006198 }
6199
Jens Axboe08a45172019-10-03 08:11:03 -06006200 if (nr_files) {
6201 fpl->max = SCM_MAX_FD;
6202 fpl->count = nr_files;
6203 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006204 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006205 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6206 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006207
Jens Axboe08a45172019-10-03 08:11:03 -06006208 for (i = 0; i < nr_files; i++)
6209 fput(fpl->fp[i]);
6210 } else {
6211 kfree_skb(skb);
6212 kfree(fpl);
6213 }
Jens Axboe6b063142019-01-10 22:13:58 -07006214
6215 return 0;
6216}
6217
6218/*
6219 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6220 * causes regular reference counting to break down. We rely on the UNIX
6221 * garbage collection to take care of this problem for us.
6222 */
6223static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6224{
6225 unsigned left, total;
6226 int ret = 0;
6227
6228 total = 0;
6229 left = ctx->nr_user_files;
6230 while (left) {
6231 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006232
6233 ret = __io_sqe_files_scm(ctx, this_files, total);
6234 if (ret)
6235 break;
6236 left -= this_files;
6237 total += this_files;
6238 }
6239
6240 if (!ret)
6241 return 0;
6242
6243 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006244 struct file *file = io_file_from_index(ctx, total);
6245
6246 if (file)
6247 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006248 total++;
6249 }
6250
6251 return ret;
6252}
6253#else
6254static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6255{
6256 return 0;
6257}
6258#endif
6259
Jens Axboe65e19f52019-10-26 07:20:21 -06006260static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6261 unsigned nr_files)
6262{
6263 int i;
6264
6265 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006266 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006267 unsigned this_files;
6268
6269 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6270 table->files = kcalloc(this_files, sizeof(struct file *),
6271 GFP_KERNEL);
6272 if (!table->files)
6273 break;
6274 nr_files -= this_files;
6275 }
6276
6277 if (i == nr_tables)
6278 return 0;
6279
6280 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006281 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006282 kfree(table->files);
6283 }
6284 return 1;
6285}
6286
Jens Axboe05f3fb32019-12-09 11:22:50 -07006287static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006288{
6289#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006290 struct sock *sock = ctx->ring_sock->sk;
6291 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6292 struct sk_buff *skb;
6293 int i;
6294
6295 __skb_queue_head_init(&list);
6296
6297 /*
6298 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6299 * remove this entry and rearrange the file array.
6300 */
6301 skb = skb_dequeue(head);
6302 while (skb) {
6303 struct scm_fp_list *fp;
6304
6305 fp = UNIXCB(skb).fp;
6306 for (i = 0; i < fp->count; i++) {
6307 int left;
6308
6309 if (fp->fp[i] != file)
6310 continue;
6311
6312 unix_notinflight(fp->user, fp->fp[i]);
6313 left = fp->count - 1 - i;
6314 if (left) {
6315 memmove(&fp->fp[i], &fp->fp[i + 1],
6316 left * sizeof(struct file *));
6317 }
6318 fp->count--;
6319 if (!fp->count) {
6320 kfree_skb(skb);
6321 skb = NULL;
6322 } else {
6323 __skb_queue_tail(&list, skb);
6324 }
6325 fput(file);
6326 file = NULL;
6327 break;
6328 }
6329
6330 if (!file)
6331 break;
6332
6333 __skb_queue_tail(&list, skb);
6334
6335 skb = skb_dequeue(head);
6336 }
6337
6338 if (skb_peek(&list)) {
6339 spin_lock_irq(&head->lock);
6340 while ((skb = __skb_dequeue(&list)) != NULL)
6341 __skb_queue_tail(head, skb);
6342 spin_unlock_irq(&head->lock);
6343 }
6344#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006345 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006346#endif
6347}
6348
Jens Axboe05f3fb32019-12-09 11:22:50 -07006349struct io_file_put {
6350 struct llist_node llist;
6351 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006352};
6353
Jens Axboe2faf8522020-02-04 19:54:55 -07006354static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006355{
6356 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006357 struct llist_node *node;
6358
Jens Axboe05f3fb32019-12-09 11:22:50 -07006359 while ((node = llist_del_all(&data->put_llist)) != NULL) {
6360 llist_for_each_entry_safe(pfile, tmp, node, llist) {
6361 io_ring_file_put(data->ctx, pfile->file);
Hillf Dantona5318d32020-03-23 17:47:15 +08006362 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006363 }
6364 }
Jens Axboe2faf8522020-02-04 19:54:55 -07006365}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006366
Jens Axboe2faf8522020-02-04 19:54:55 -07006367static void io_ring_file_ref_switch(struct work_struct *work)
6368{
6369 struct fixed_file_data *data;
6370
6371 data = container_of(work, struct fixed_file_data, ref_work);
6372 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006373 percpu_ref_switch_to_percpu(&data->refs);
6374}
6375
6376static void io_file_data_ref_zero(struct percpu_ref *ref)
6377{
6378 struct fixed_file_data *data;
6379
6380 data = container_of(ref, struct fixed_file_data, refs);
6381
Jens Axboe2faf8522020-02-04 19:54:55 -07006382 /*
6383 * We can't safely switch from inside this context, punt to wq. If
6384 * the table ref is going away, the table is being unregistered.
6385 * Don't queue up the async work for that case, the caller will
6386 * handle it.
6387 */
6388 if (!percpu_ref_is_dying(&data->refs))
6389 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006390}
6391
6392static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6393 unsigned nr_args)
6394{
6395 __s32 __user *fds = (__s32 __user *) arg;
6396 unsigned nr_tables;
6397 struct file *file;
6398 int fd, ret = 0;
6399 unsigned i;
6400
6401 if (ctx->file_data)
6402 return -EBUSY;
6403 if (!nr_args)
6404 return -EINVAL;
6405 if (nr_args > IORING_MAX_FIXED_FILES)
6406 return -EMFILE;
6407
6408 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6409 if (!ctx->file_data)
6410 return -ENOMEM;
6411 ctx->file_data->ctx = ctx;
6412 init_completion(&ctx->file_data->done);
6413
6414 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6415 ctx->file_data->table = kcalloc(nr_tables,
6416 sizeof(struct fixed_file_table),
6417 GFP_KERNEL);
6418 if (!ctx->file_data->table) {
6419 kfree(ctx->file_data);
6420 ctx->file_data = NULL;
6421 return -ENOMEM;
6422 }
6423
6424 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
6425 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6426 kfree(ctx->file_data->table);
6427 kfree(ctx->file_data);
6428 ctx->file_data = NULL;
6429 return -ENOMEM;
6430 }
6431 ctx->file_data->put_llist.first = NULL;
6432 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
6433
6434 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6435 percpu_ref_exit(&ctx->file_data->refs);
6436 kfree(ctx->file_data->table);
6437 kfree(ctx->file_data);
6438 ctx->file_data = NULL;
6439 return -ENOMEM;
6440 }
6441
6442 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6443 struct fixed_file_table *table;
6444 unsigned index;
6445
6446 ret = -EFAULT;
6447 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6448 break;
6449 /* allow sparse sets */
6450 if (fd == -1) {
6451 ret = 0;
6452 continue;
6453 }
6454
6455 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6456 index = i & IORING_FILE_TABLE_MASK;
6457 file = fget(fd);
6458
6459 ret = -EBADF;
6460 if (!file)
6461 break;
6462
6463 /*
6464 * Don't allow io_uring instances to be registered. If UNIX
6465 * isn't enabled, then this causes a reference cycle and this
6466 * instance can never get freed. If UNIX is enabled we'll
6467 * handle it just fine, but there's still no point in allowing
6468 * a ring fd as it doesn't support regular read/write anyway.
6469 */
6470 if (file->f_op == &io_uring_fops) {
6471 fput(file);
6472 break;
6473 }
6474 ret = 0;
6475 table->files[index] = file;
6476 }
6477
6478 if (ret) {
6479 for (i = 0; i < ctx->nr_user_files; i++) {
6480 file = io_file_from_index(ctx, i);
6481 if (file)
6482 fput(file);
6483 }
6484 for (i = 0; i < nr_tables; i++)
6485 kfree(ctx->file_data->table[i].files);
6486
6487 kfree(ctx->file_data->table);
6488 kfree(ctx->file_data);
6489 ctx->file_data = NULL;
6490 ctx->nr_user_files = 0;
6491 return ret;
6492 }
6493
6494 ret = io_sqe_files_scm(ctx);
6495 if (ret)
6496 io_sqe_files_unregister(ctx);
6497
6498 return ret;
6499}
6500
Jens Axboec3a31e62019-10-03 13:59:56 -06006501static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6502 int index)
6503{
6504#if defined(CONFIG_UNIX)
6505 struct sock *sock = ctx->ring_sock->sk;
6506 struct sk_buff_head *head = &sock->sk_receive_queue;
6507 struct sk_buff *skb;
6508
6509 /*
6510 * See if we can merge this file into an existing skb SCM_RIGHTS
6511 * file set. If there's no room, fall back to allocating a new skb
6512 * and filling it in.
6513 */
6514 spin_lock_irq(&head->lock);
6515 skb = skb_peek(head);
6516 if (skb) {
6517 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6518
6519 if (fpl->count < SCM_MAX_FD) {
6520 __skb_unlink(skb, head);
6521 spin_unlock_irq(&head->lock);
6522 fpl->fp[fpl->count] = get_file(file);
6523 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6524 fpl->count++;
6525 spin_lock_irq(&head->lock);
6526 __skb_queue_head(head, skb);
6527 } else {
6528 skb = NULL;
6529 }
6530 }
6531 spin_unlock_irq(&head->lock);
6532
6533 if (skb) {
6534 fput(file);
6535 return 0;
6536 }
6537
6538 return __io_sqe_files_scm(ctx, 1, index);
6539#else
6540 return 0;
6541#endif
6542}
6543
Jens Axboe05f3fb32019-12-09 11:22:50 -07006544static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06006545{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006546 struct fixed_file_data *data;
6547
Jens Axboedd3db2a2020-02-26 10:23:43 -07006548 /*
6549 * Juggle reference to ensure we hit zero, if needed, so we can
6550 * switch back to percpu mode
6551 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006552 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07006553 percpu_ref_put(&data->refs);
6554 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006555}
6556
Hillf Dantona5318d32020-03-23 17:47:15 +08006557static int io_queue_file_removal(struct fixed_file_data *data,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006558 struct file *file)
6559{
Hillf Dantona5318d32020-03-23 17:47:15 +08006560 struct io_file_put *pfile;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006561
Jens Axboe05f3fb32019-12-09 11:22:50 -07006562 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006563 if (!pfile)
6564 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006565
6566 pfile->file = file;
6567 llist_add(&pfile->llist, &data->put_llist);
Hillf Dantona5318d32020-03-23 17:47:15 +08006568 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006569}
6570
6571static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6572 struct io_uring_files_update *up,
6573 unsigned nr_args)
6574{
6575 struct fixed_file_data *data = ctx->file_data;
6576 bool ref_switch = false;
6577 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006578 __s32 __user *fds;
6579 int fd, i, err;
6580 __u32 done;
6581
Jens Axboe05f3fb32019-12-09 11:22:50 -07006582 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006583 return -EOVERFLOW;
6584 if (done > ctx->nr_user_files)
6585 return -EINVAL;
6586
6587 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006588 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006589 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006590 struct fixed_file_table *table;
6591 unsigned index;
6592
Jens Axboec3a31e62019-10-03 13:59:56 -06006593 err = 0;
6594 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6595 err = -EFAULT;
6596 break;
6597 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006598 i = array_index_nospec(up->offset, ctx->nr_user_files);
6599 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006600 index = i & IORING_FILE_TABLE_MASK;
6601 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006602 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006603 err = io_queue_file_removal(data, file);
6604 if (err)
6605 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006606 table->files[index] = NULL;
Hillf Dantona5318d32020-03-23 17:47:15 +08006607 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006608 }
6609 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006610 file = fget(fd);
6611 if (!file) {
6612 err = -EBADF;
6613 break;
6614 }
6615 /*
6616 * Don't allow io_uring instances to be registered. If
6617 * UNIX isn't enabled, then this causes a reference
6618 * cycle and this instance can never get freed. If UNIX
6619 * is enabled we'll handle it just fine, but there's
6620 * still no point in allowing a ring fd as it doesn't
6621 * support regular read/write anyway.
6622 */
6623 if (file->f_op == &io_uring_fops) {
6624 fput(file);
6625 err = -EBADF;
6626 break;
6627 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006628 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006629 err = io_sqe_file_register(ctx, file, i);
6630 if (err)
6631 break;
6632 }
6633 nr_args--;
6634 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006635 up->offset++;
6636 }
6637
Jens Axboedd3db2a2020-02-26 10:23:43 -07006638 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006639 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006640
6641 return done ? done : err;
6642}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006643static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6644 unsigned nr_args)
6645{
6646 struct io_uring_files_update up;
6647
6648 if (!ctx->file_data)
6649 return -ENXIO;
6650 if (!nr_args)
6651 return -EINVAL;
6652 if (copy_from_user(&up, arg, sizeof(up)))
6653 return -EFAULT;
6654 if (up.resv)
6655 return -EINVAL;
6656
6657 return __io_sqe_files_update(ctx, &up, nr_args);
6658}
Jens Axboec3a31e62019-10-03 13:59:56 -06006659
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006660static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006661{
6662 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6663
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006664 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006665 io_put_req(req);
6666}
6667
Pavel Begunkov24369c22020-01-28 03:15:48 +03006668static int io_init_wq_offload(struct io_ring_ctx *ctx,
6669 struct io_uring_params *p)
6670{
6671 struct io_wq_data data;
6672 struct fd f;
6673 struct io_ring_ctx *ctx_attach;
6674 unsigned int concurrency;
6675 int ret = 0;
6676
6677 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006678 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006679
6680 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6681 /* Do QD, or 4 * CPUS, whatever is smallest */
6682 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6683
6684 ctx->io_wq = io_wq_create(concurrency, &data);
6685 if (IS_ERR(ctx->io_wq)) {
6686 ret = PTR_ERR(ctx->io_wq);
6687 ctx->io_wq = NULL;
6688 }
6689 return ret;
6690 }
6691
6692 f = fdget(p->wq_fd);
6693 if (!f.file)
6694 return -EBADF;
6695
6696 if (f.file->f_op != &io_uring_fops) {
6697 ret = -EINVAL;
6698 goto out_fput;
6699 }
6700
6701 ctx_attach = f.file->private_data;
6702 /* @io_wq is protected by holding the fd */
6703 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6704 ret = -EINVAL;
6705 goto out_fput;
6706 }
6707
6708 ctx->io_wq = ctx_attach->io_wq;
6709out_fput:
6710 fdput(f);
6711 return ret;
6712}
6713
Jens Axboe6c271ce2019-01-10 11:22:30 -07006714static int io_sq_offload_start(struct io_ring_ctx *ctx,
6715 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006716{
6717 int ret;
6718
Jens Axboe6c271ce2019-01-10 11:22:30 -07006719 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006720 mmgrab(current->mm);
6721 ctx->sqo_mm = current->mm;
6722
Jens Axboe6c271ce2019-01-10 11:22:30 -07006723 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006724 ret = -EPERM;
6725 if (!capable(CAP_SYS_ADMIN))
6726 goto err;
6727
Jens Axboe917257d2019-04-13 09:28:55 -06006728 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6729 if (!ctx->sq_thread_idle)
6730 ctx->sq_thread_idle = HZ;
6731
Jens Axboe6c271ce2019-01-10 11:22:30 -07006732 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006733 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006734
Jens Axboe917257d2019-04-13 09:28:55 -06006735 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006736 if (cpu >= nr_cpu_ids)
6737 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006738 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006739 goto err;
6740
Jens Axboe6c271ce2019-01-10 11:22:30 -07006741 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6742 ctx, cpu,
6743 "io_uring-sq");
6744 } else {
6745 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6746 "io_uring-sq");
6747 }
6748 if (IS_ERR(ctx->sqo_thread)) {
6749 ret = PTR_ERR(ctx->sqo_thread);
6750 ctx->sqo_thread = NULL;
6751 goto err;
6752 }
6753 wake_up_process(ctx->sqo_thread);
6754 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6755 /* Can't have SQ_AFF without SQPOLL */
6756 ret = -EINVAL;
6757 goto err;
6758 }
6759
Pavel Begunkov24369c22020-01-28 03:15:48 +03006760 ret = io_init_wq_offload(ctx, p);
6761 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006762 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006763
6764 return 0;
6765err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006766 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006767 mmdrop(ctx->sqo_mm);
6768 ctx->sqo_mm = NULL;
6769 return ret;
6770}
6771
6772static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6773{
6774 atomic_long_sub(nr_pages, &user->locked_vm);
6775}
6776
6777static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6778{
6779 unsigned long page_limit, cur_pages, new_pages;
6780
6781 /* Don't allow more pages than we can safely lock */
6782 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6783
6784 do {
6785 cur_pages = atomic_long_read(&user->locked_vm);
6786 new_pages = cur_pages + nr_pages;
6787 if (new_pages > page_limit)
6788 return -ENOMEM;
6789 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6790 new_pages) != cur_pages);
6791
6792 return 0;
6793}
6794
6795static void io_mem_free(void *ptr)
6796{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006797 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006798
Mark Rutland52e04ef2019-04-30 17:30:21 +01006799 if (!ptr)
6800 return;
6801
6802 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006803 if (put_page_testzero(page))
6804 free_compound_page(page);
6805}
6806
6807static void *io_mem_alloc(size_t size)
6808{
6809 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6810 __GFP_NORETRY;
6811
6812 return (void *) __get_free_pages(gfp_flags, get_order(size));
6813}
6814
Hristo Venev75b28af2019-08-26 17:23:46 +00006815static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6816 size_t *sq_offset)
6817{
6818 struct io_rings *rings;
6819 size_t off, sq_array_size;
6820
6821 off = struct_size(rings, cqes, cq_entries);
6822 if (off == SIZE_MAX)
6823 return SIZE_MAX;
6824
6825#ifdef CONFIG_SMP
6826 off = ALIGN(off, SMP_CACHE_BYTES);
6827 if (off == 0)
6828 return SIZE_MAX;
6829#endif
6830
6831 sq_array_size = array_size(sizeof(u32), sq_entries);
6832 if (sq_array_size == SIZE_MAX)
6833 return SIZE_MAX;
6834
6835 if (check_add_overflow(off, sq_array_size, &off))
6836 return SIZE_MAX;
6837
6838 if (sq_offset)
6839 *sq_offset = off;
6840
6841 return off;
6842}
6843
Jens Axboe2b188cc2019-01-07 10:46:33 -07006844static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6845{
Hristo Venev75b28af2019-08-26 17:23:46 +00006846 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006847
Hristo Venev75b28af2019-08-26 17:23:46 +00006848 pages = (size_t)1 << get_order(
6849 rings_size(sq_entries, cq_entries, NULL));
6850 pages += (size_t)1 << get_order(
6851 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006852
Hristo Venev75b28af2019-08-26 17:23:46 +00006853 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006854}
6855
Jens Axboeedafcce2019-01-09 09:16:05 -07006856static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6857{
6858 int i, j;
6859
6860 if (!ctx->user_bufs)
6861 return -ENXIO;
6862
6863 for (i = 0; i < ctx->nr_user_bufs; i++) {
6864 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6865
6866 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006867 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006868
6869 if (ctx->account_mem)
6870 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006871 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006872 imu->nr_bvecs = 0;
6873 }
6874
6875 kfree(ctx->user_bufs);
6876 ctx->user_bufs = NULL;
6877 ctx->nr_user_bufs = 0;
6878 return 0;
6879}
6880
6881static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6882 void __user *arg, unsigned index)
6883{
6884 struct iovec __user *src;
6885
6886#ifdef CONFIG_COMPAT
6887 if (ctx->compat) {
6888 struct compat_iovec __user *ciovs;
6889 struct compat_iovec ciov;
6890
6891 ciovs = (struct compat_iovec __user *) arg;
6892 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6893 return -EFAULT;
6894
Jens Axboed55e5f52019-12-11 16:12:15 -07006895 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006896 dst->iov_len = ciov.iov_len;
6897 return 0;
6898 }
6899#endif
6900 src = (struct iovec __user *) arg;
6901 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6902 return -EFAULT;
6903 return 0;
6904}
6905
6906static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6907 unsigned nr_args)
6908{
6909 struct vm_area_struct **vmas = NULL;
6910 struct page **pages = NULL;
6911 int i, j, got_pages = 0;
6912 int ret = -EINVAL;
6913
6914 if (ctx->user_bufs)
6915 return -EBUSY;
6916 if (!nr_args || nr_args > UIO_MAXIOV)
6917 return -EINVAL;
6918
6919 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6920 GFP_KERNEL);
6921 if (!ctx->user_bufs)
6922 return -ENOMEM;
6923
6924 for (i = 0; i < nr_args; i++) {
6925 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6926 unsigned long off, start, end, ubuf;
6927 int pret, nr_pages;
6928 struct iovec iov;
6929 size_t size;
6930
6931 ret = io_copy_iov(ctx, &iov, arg, i);
6932 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006933 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006934
6935 /*
6936 * Don't impose further limits on the size and buffer
6937 * constraints here, we'll -EINVAL later when IO is
6938 * submitted if they are wrong.
6939 */
6940 ret = -EFAULT;
6941 if (!iov.iov_base || !iov.iov_len)
6942 goto err;
6943
6944 /* arbitrary limit, but we need something */
6945 if (iov.iov_len > SZ_1G)
6946 goto err;
6947
6948 ubuf = (unsigned long) iov.iov_base;
6949 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6950 start = ubuf >> PAGE_SHIFT;
6951 nr_pages = end - start;
6952
6953 if (ctx->account_mem) {
6954 ret = io_account_mem(ctx->user, nr_pages);
6955 if (ret)
6956 goto err;
6957 }
6958
6959 ret = 0;
6960 if (!pages || nr_pages > got_pages) {
6961 kfree(vmas);
6962 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006963 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006964 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006965 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006966 sizeof(struct vm_area_struct *),
6967 GFP_KERNEL);
6968 if (!pages || !vmas) {
6969 ret = -ENOMEM;
6970 if (ctx->account_mem)
6971 io_unaccount_mem(ctx->user, nr_pages);
6972 goto err;
6973 }
6974 got_pages = nr_pages;
6975 }
6976
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006977 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006978 GFP_KERNEL);
6979 ret = -ENOMEM;
6980 if (!imu->bvec) {
6981 if (ctx->account_mem)
6982 io_unaccount_mem(ctx->user, nr_pages);
6983 goto err;
6984 }
6985
6986 ret = 0;
6987 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006988 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006989 FOLL_WRITE | FOLL_LONGTERM,
6990 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006991 if (pret == nr_pages) {
6992 /* don't support file backed memory */
6993 for (j = 0; j < nr_pages; j++) {
6994 struct vm_area_struct *vma = vmas[j];
6995
6996 if (vma->vm_file &&
6997 !is_file_hugepages(vma->vm_file)) {
6998 ret = -EOPNOTSUPP;
6999 break;
7000 }
7001 }
7002 } else {
7003 ret = pret < 0 ? pret : -EFAULT;
7004 }
7005 up_read(&current->mm->mmap_sem);
7006 if (ret) {
7007 /*
7008 * if we did partial map, or found file backed vmas,
7009 * release any pages we did get
7010 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007011 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007012 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007013 if (ctx->account_mem)
7014 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007015 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007016 goto err;
7017 }
7018
7019 off = ubuf & ~PAGE_MASK;
7020 size = iov.iov_len;
7021 for (j = 0; j < nr_pages; j++) {
7022 size_t vec_len;
7023
7024 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7025 imu->bvec[j].bv_page = pages[j];
7026 imu->bvec[j].bv_len = vec_len;
7027 imu->bvec[j].bv_offset = off;
7028 off = 0;
7029 size -= vec_len;
7030 }
7031 /* store original address for later verification */
7032 imu->ubuf = ubuf;
7033 imu->len = iov.iov_len;
7034 imu->nr_bvecs = nr_pages;
7035
7036 ctx->nr_user_bufs++;
7037 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007038 kvfree(pages);
7039 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007040 return 0;
7041err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007042 kvfree(pages);
7043 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007044 io_sqe_buffer_unregister(ctx);
7045 return ret;
7046}
7047
Jens Axboe9b402842019-04-11 11:45:41 -06007048static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7049{
7050 __s32 __user *fds = arg;
7051 int fd;
7052
7053 if (ctx->cq_ev_fd)
7054 return -EBUSY;
7055
7056 if (copy_from_user(&fd, fds, sizeof(*fds)))
7057 return -EFAULT;
7058
7059 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7060 if (IS_ERR(ctx->cq_ev_fd)) {
7061 int ret = PTR_ERR(ctx->cq_ev_fd);
7062 ctx->cq_ev_fd = NULL;
7063 return ret;
7064 }
7065
7066 return 0;
7067}
7068
7069static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7070{
7071 if (ctx->cq_ev_fd) {
7072 eventfd_ctx_put(ctx->cq_ev_fd);
7073 ctx->cq_ev_fd = NULL;
7074 return 0;
7075 }
7076
7077 return -ENXIO;
7078}
7079
Jens Axboe5a2e7452020-02-23 16:23:11 -07007080static int __io_destroy_buffers(int id, void *p, void *data)
7081{
7082 struct io_ring_ctx *ctx = data;
7083 struct io_buffer *buf = p;
7084
Jens Axboe067524e2020-03-02 16:32:28 -07007085 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007086 return 0;
7087}
7088
7089static void io_destroy_buffers(struct io_ring_ctx *ctx)
7090{
7091 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7092 idr_destroy(&ctx->io_buffer_idr);
7093}
7094
Jens Axboe2b188cc2019-01-07 10:46:33 -07007095static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7096{
Jens Axboe6b063142019-01-10 22:13:58 -07007097 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007098 if (ctx->sqo_mm)
7099 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007100
7101 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007102 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007103 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007104 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007105 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007106 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007107
Jens Axboe2b188cc2019-01-07 10:46:33 -07007108#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007109 if (ctx->ring_sock) {
7110 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007111 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007112 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007113#endif
7114
Hristo Venev75b28af2019-08-26 17:23:46 +00007115 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007116 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007117
7118 percpu_ref_exit(&ctx->refs);
7119 if (ctx->account_mem)
7120 io_unaccount_mem(ctx->user,
7121 ring_pages(ctx->sq_entries, ctx->cq_entries));
7122 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007123 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07007124 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07007125 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007126 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007127 kfree(ctx);
7128}
7129
7130static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7131{
7132 struct io_ring_ctx *ctx = file->private_data;
7133 __poll_t mask = 0;
7134
7135 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007136 /*
7137 * synchronizes with barrier from wq_has_sleeper call in
7138 * io_commit_cqring
7139 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007140 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007141 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7142 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007143 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007144 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007145 mask |= EPOLLIN | EPOLLRDNORM;
7146
7147 return mask;
7148}
7149
7150static int io_uring_fasync(int fd, struct file *file, int on)
7151{
7152 struct io_ring_ctx *ctx = file->private_data;
7153
7154 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7155}
7156
Jens Axboe071698e2020-01-28 10:04:42 -07007157static int io_remove_personalities(int id, void *p, void *data)
7158{
7159 struct io_ring_ctx *ctx = data;
7160 const struct cred *cred;
7161
7162 cred = idr_remove(&ctx->personality_idr, id);
7163 if (cred)
7164 put_cred(cred);
7165 return 0;
7166}
7167
Jens Axboe2b188cc2019-01-07 10:46:33 -07007168static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7169{
7170 mutex_lock(&ctx->uring_lock);
7171 percpu_ref_kill(&ctx->refs);
7172 mutex_unlock(&ctx->uring_lock);
7173
Jens Axboedf069d82020-02-04 16:48:34 -07007174 /*
7175 * Wait for sq thread to idle, if we have one. It won't spin on new
7176 * work after we've killed the ctx ref above. This is important to do
7177 * before we cancel existing commands, as the thread could otherwise
7178 * be queueing new work post that. If that's work we need to cancel,
7179 * it could cause shutdown to hang.
7180 */
7181 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
7182 cpu_relax();
7183
Jens Axboe5262f562019-09-17 12:26:57 -06007184 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007185 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007186
7187 if (ctx->io_wq)
7188 io_wq_cancel_all(ctx->io_wq);
7189
Jens Axboedef596e2019-01-09 08:59:42 -07007190 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007191 /* if we failed setting up the ctx, we might not have any rings */
7192 if (ctx->rings)
7193 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007194 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07007195 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007196 io_ring_ctx_free(ctx);
7197}
7198
7199static int io_uring_release(struct inode *inode, struct file *file)
7200{
7201 struct io_ring_ctx *ctx = file->private_data;
7202
7203 file->private_data = NULL;
7204 io_ring_ctx_wait_and_kill(ctx);
7205 return 0;
7206}
7207
Jens Axboefcb323c2019-10-24 12:39:47 -06007208static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7209 struct files_struct *files)
7210{
7211 struct io_kiocb *req;
7212 DEFINE_WAIT(wait);
7213
7214 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07007215 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06007216
7217 spin_lock_irq(&ctx->inflight_lock);
7218 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007219 if (req->work.files != files)
7220 continue;
7221 /* req is being completed, ignore */
7222 if (!refcount_inc_not_zero(&req->refs))
7223 continue;
7224 cancel_req = req;
7225 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007226 }
Jens Axboe768134d2019-11-10 20:30:53 -07007227 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007228 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007229 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007230 spin_unlock_irq(&ctx->inflight_lock);
7231
Jens Axboe768134d2019-11-10 20:30:53 -07007232 /* We need to keep going until we don't find a matching req */
7233 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007234 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007235
Jens Axboe2ca10252020-02-13 17:17:35 -07007236 if (cancel_req->flags & REQ_F_OVERFLOW) {
7237 spin_lock_irq(&ctx->completion_lock);
7238 list_del(&cancel_req->list);
7239 cancel_req->flags &= ~REQ_F_OVERFLOW;
7240 if (list_empty(&ctx->cq_overflow_list)) {
7241 clear_bit(0, &ctx->sq_check_overflow);
7242 clear_bit(0, &ctx->cq_check_overflow);
7243 }
7244 spin_unlock_irq(&ctx->completion_lock);
7245
7246 WRITE_ONCE(ctx->rings->cq_overflow,
7247 atomic_inc_return(&ctx->cached_cq_overflow));
7248
7249 /*
7250 * Put inflight ref and overflow ref. If that's
7251 * all we had, then we're done with this request.
7252 */
7253 if (refcount_sub_and_test(2, &cancel_req->refs)) {
7254 io_put_req(cancel_req);
7255 continue;
7256 }
7257 }
7258
Bob Liu2f6d9b92019-11-13 18:06:24 +08007259 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7260 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06007261 schedule();
7262 }
Jens Axboe768134d2019-11-10 20:30:53 -07007263 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007264}
7265
7266static int io_uring_flush(struct file *file, void *data)
7267{
7268 struct io_ring_ctx *ctx = file->private_data;
7269
7270 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007271
7272 /*
7273 * If the task is going away, cancel work it may have pending
7274 */
7275 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7276 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7277
Jens Axboefcb323c2019-10-24 12:39:47 -06007278 return 0;
7279}
7280
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007281static void *io_uring_validate_mmap_request(struct file *file,
7282 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007283{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007284 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007285 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007286 struct page *page;
7287 void *ptr;
7288
7289 switch (offset) {
7290 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007291 case IORING_OFF_CQ_RING:
7292 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007293 break;
7294 case IORING_OFF_SQES:
7295 ptr = ctx->sq_sqes;
7296 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007297 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007298 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007299 }
7300
7301 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007302 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007303 return ERR_PTR(-EINVAL);
7304
7305 return ptr;
7306}
7307
7308#ifdef CONFIG_MMU
7309
7310static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7311{
7312 size_t sz = vma->vm_end - vma->vm_start;
7313 unsigned long pfn;
7314 void *ptr;
7315
7316 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7317 if (IS_ERR(ptr))
7318 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007319
7320 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7321 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7322}
7323
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007324#else /* !CONFIG_MMU */
7325
7326static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7327{
7328 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7329}
7330
7331static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7332{
7333 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7334}
7335
7336static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7337 unsigned long addr, unsigned long len,
7338 unsigned long pgoff, unsigned long flags)
7339{
7340 void *ptr;
7341
7342 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7343 if (IS_ERR(ptr))
7344 return PTR_ERR(ptr);
7345
7346 return (unsigned long) ptr;
7347}
7348
7349#endif /* !CONFIG_MMU */
7350
Jens Axboe2b188cc2019-01-07 10:46:33 -07007351SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7352 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7353 size_t, sigsz)
7354{
7355 struct io_ring_ctx *ctx;
7356 long ret = -EBADF;
7357 int submitted = 0;
7358 struct fd f;
7359
Jens Axboeb41e9852020-02-17 09:52:41 -07007360 if (current->task_works)
7361 task_work_run();
7362
Jens Axboe6c271ce2019-01-10 11:22:30 -07007363 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007364 return -EINVAL;
7365
7366 f = fdget(fd);
7367 if (!f.file)
7368 return -EBADF;
7369
7370 ret = -EOPNOTSUPP;
7371 if (f.file->f_op != &io_uring_fops)
7372 goto out_fput;
7373
7374 ret = -ENXIO;
7375 ctx = f.file->private_data;
7376 if (!percpu_ref_tryget(&ctx->refs))
7377 goto out_fput;
7378
Jens Axboe6c271ce2019-01-10 11:22:30 -07007379 /*
7380 * For SQ polling, the thread will do all submissions and completions.
7381 * Just return the requested submit count, and wake the thread if
7382 * we were asked to.
7383 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007384 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007385 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007386 if (!list_empty_careful(&ctx->cq_overflow_list))
7387 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007388 if (flags & IORING_ENTER_SQ_WAKEUP)
7389 wake_up(&ctx->sqo_wait);
7390 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007391 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007392 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007393
7394 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007395 /* already have mm, so io_submit_sqes() won't try to grab it */
7396 cur_mm = ctx->sqo_mm;
7397 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
7398 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007399 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007400
7401 if (submitted != to_submit)
7402 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007403 }
7404 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007405 unsigned nr_events = 0;
7406
Jens Axboe2b188cc2019-01-07 10:46:33 -07007407 min_complete = min(min_complete, ctx->cq_entries);
7408
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007409 /*
7410 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7411 * space applications don't need to do io completion events
7412 * polling again, they can rely on io_sq_thread to do polling
7413 * work, which can reduce cpu usage and uring_lock contention.
7414 */
7415 if (ctx->flags & IORING_SETUP_IOPOLL &&
7416 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007417 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007418 } else {
7419 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7420 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007421 }
7422
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007423out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007424 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007425out_fput:
7426 fdput(f);
7427 return submitted ? submitted : ret;
7428}
7429
Tobias Klauserbebdb652020-02-26 18:38:32 +01007430#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007431static int io_uring_show_cred(int id, void *p, void *data)
7432{
7433 const struct cred *cred = p;
7434 struct seq_file *m = data;
7435 struct user_namespace *uns = seq_user_ns(m);
7436 struct group_info *gi;
7437 kernel_cap_t cap;
7438 unsigned __capi;
7439 int g;
7440
7441 seq_printf(m, "%5d\n", id);
7442 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7443 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7444 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7445 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7446 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7447 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7448 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7449 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7450 seq_puts(m, "\n\tGroups:\t");
7451 gi = cred->group_info;
7452 for (g = 0; g < gi->ngroups; g++) {
7453 seq_put_decimal_ull(m, g ? " " : "",
7454 from_kgid_munged(uns, gi->gid[g]));
7455 }
7456 seq_puts(m, "\n\tCapEff:\t");
7457 cap = cred->cap_effective;
7458 CAP_FOR_EACH_U32(__capi)
7459 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7460 seq_putc(m, '\n');
7461 return 0;
7462}
7463
7464static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7465{
7466 int i;
7467
7468 mutex_lock(&ctx->uring_lock);
7469 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7470 for (i = 0; i < ctx->nr_user_files; i++) {
7471 struct fixed_file_table *table;
7472 struct file *f;
7473
7474 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7475 f = table->files[i & IORING_FILE_TABLE_MASK];
7476 if (f)
7477 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7478 else
7479 seq_printf(m, "%5u: <none>\n", i);
7480 }
7481 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7482 for (i = 0; i < ctx->nr_user_bufs; i++) {
7483 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7484
7485 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7486 (unsigned int) buf->len);
7487 }
7488 if (!idr_is_empty(&ctx->personality_idr)) {
7489 seq_printf(m, "Personalities:\n");
7490 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7491 }
Jens Axboed7718a92020-02-14 22:23:12 -07007492 seq_printf(m, "PollList:\n");
7493 spin_lock_irq(&ctx->completion_lock);
7494 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7495 struct hlist_head *list = &ctx->cancel_hash[i];
7496 struct io_kiocb *req;
7497
7498 hlist_for_each_entry(req, list, hash_node)
7499 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7500 req->task->task_works != NULL);
7501 }
7502 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007503 mutex_unlock(&ctx->uring_lock);
7504}
7505
7506static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7507{
7508 struct io_ring_ctx *ctx = f->private_data;
7509
7510 if (percpu_ref_tryget(&ctx->refs)) {
7511 __io_uring_show_fdinfo(ctx, m);
7512 percpu_ref_put(&ctx->refs);
7513 }
7514}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007515#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007516
Jens Axboe2b188cc2019-01-07 10:46:33 -07007517static const struct file_operations io_uring_fops = {
7518 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007519 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007520 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007521#ifndef CONFIG_MMU
7522 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7523 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7524#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007525 .poll = io_uring_poll,
7526 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007527#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007528 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007529#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007530};
7531
7532static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7533 struct io_uring_params *p)
7534{
Hristo Venev75b28af2019-08-26 17:23:46 +00007535 struct io_rings *rings;
7536 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007537
Hristo Venev75b28af2019-08-26 17:23:46 +00007538 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7539 if (size == SIZE_MAX)
7540 return -EOVERFLOW;
7541
7542 rings = io_mem_alloc(size);
7543 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007544 return -ENOMEM;
7545
Hristo Venev75b28af2019-08-26 17:23:46 +00007546 ctx->rings = rings;
7547 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7548 rings->sq_ring_mask = p->sq_entries - 1;
7549 rings->cq_ring_mask = p->cq_entries - 1;
7550 rings->sq_ring_entries = p->sq_entries;
7551 rings->cq_ring_entries = p->cq_entries;
7552 ctx->sq_mask = rings->sq_ring_mask;
7553 ctx->cq_mask = rings->cq_ring_mask;
7554 ctx->sq_entries = rings->sq_ring_entries;
7555 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007556
7557 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007558 if (size == SIZE_MAX) {
7559 io_mem_free(ctx->rings);
7560 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007561 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007562 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007563
7564 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007565 if (!ctx->sq_sqes) {
7566 io_mem_free(ctx->rings);
7567 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007568 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007569 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007570
Jens Axboe2b188cc2019-01-07 10:46:33 -07007571 return 0;
7572}
7573
7574/*
7575 * Allocate an anonymous fd, this is what constitutes the application
7576 * visible backing of an io_uring instance. The application mmaps this
7577 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7578 * we have to tie this fd to a socket for file garbage collection purposes.
7579 */
7580static int io_uring_get_fd(struct io_ring_ctx *ctx)
7581{
7582 struct file *file;
7583 int ret;
7584
7585#if defined(CONFIG_UNIX)
7586 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7587 &ctx->ring_sock);
7588 if (ret)
7589 return ret;
7590#endif
7591
7592 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7593 if (ret < 0)
7594 goto err;
7595
7596 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7597 O_RDWR | O_CLOEXEC);
7598 if (IS_ERR(file)) {
7599 put_unused_fd(ret);
7600 ret = PTR_ERR(file);
7601 goto err;
7602 }
7603
7604#if defined(CONFIG_UNIX)
7605 ctx->ring_sock->file = file;
7606#endif
7607 fd_install(ret, file);
7608 return ret;
7609err:
7610#if defined(CONFIG_UNIX)
7611 sock_release(ctx->ring_sock);
7612 ctx->ring_sock = NULL;
7613#endif
7614 return ret;
7615}
7616
7617static int io_uring_create(unsigned entries, struct io_uring_params *p)
7618{
7619 struct user_struct *user = NULL;
7620 struct io_ring_ctx *ctx;
7621 bool account_mem;
7622 int ret;
7623
Jens Axboe8110c1a2019-12-28 15:39:54 -07007624 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007625 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007626 if (entries > IORING_MAX_ENTRIES) {
7627 if (!(p->flags & IORING_SETUP_CLAMP))
7628 return -EINVAL;
7629 entries = IORING_MAX_ENTRIES;
7630 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007631
7632 /*
7633 * Use twice as many entries for the CQ ring. It's possible for the
7634 * application to drive a higher depth than the size of the SQ ring,
7635 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007636 * some flexibility in overcommitting a bit. If the application has
7637 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7638 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007639 */
7640 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007641 if (p->flags & IORING_SETUP_CQSIZE) {
7642 /*
7643 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7644 * to a power-of-two, if it isn't already. We do NOT impose
7645 * any cq vs sq ring sizing.
7646 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007647 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007648 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007649 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7650 if (!(p->flags & IORING_SETUP_CLAMP))
7651 return -EINVAL;
7652 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7653 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007654 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7655 } else {
7656 p->cq_entries = 2 * p->sq_entries;
7657 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007658
7659 user = get_uid(current_user());
7660 account_mem = !capable(CAP_IPC_LOCK);
7661
7662 if (account_mem) {
7663 ret = io_account_mem(user,
7664 ring_pages(p->sq_entries, p->cq_entries));
7665 if (ret) {
7666 free_uid(user);
7667 return ret;
7668 }
7669 }
7670
7671 ctx = io_ring_ctx_alloc(p);
7672 if (!ctx) {
7673 if (account_mem)
7674 io_unaccount_mem(user, ring_pages(p->sq_entries,
7675 p->cq_entries));
7676 free_uid(user);
7677 return -ENOMEM;
7678 }
7679 ctx->compat = in_compat_syscall();
7680 ctx->account_mem = account_mem;
7681 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007682 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007683
7684 ret = io_allocate_scq_urings(ctx, p);
7685 if (ret)
7686 goto err;
7687
Jens Axboe6c271ce2019-01-10 11:22:30 -07007688 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007689 if (ret)
7690 goto err;
7691
Jens Axboe2b188cc2019-01-07 10:46:33 -07007692 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007693 p->sq_off.head = offsetof(struct io_rings, sq.head);
7694 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7695 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7696 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7697 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7698 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7699 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007700
7701 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007702 p->cq_off.head = offsetof(struct io_rings, cq.head);
7703 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7704 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7705 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7706 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7707 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007708
Jens Axboe044c1ab2019-10-28 09:15:33 -06007709 /*
7710 * Install ring fd as the very last thing, so we don't risk someone
7711 * having closed it before we finish setup
7712 */
7713 ret = io_uring_get_fd(ctx);
7714 if (ret < 0)
7715 goto err;
7716
Jens Axboeda8c9692019-12-02 18:51:26 -07007717 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007718 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jens Axboed7718a92020-02-14 22:23:12 -07007719 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007720 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007721 return ret;
7722err:
7723 io_ring_ctx_wait_and_kill(ctx);
7724 return ret;
7725}
7726
7727/*
7728 * Sets up an aio uring context, and returns the fd. Applications asks for a
7729 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7730 * params structure passed in.
7731 */
7732static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7733{
7734 struct io_uring_params p;
7735 long ret;
7736 int i;
7737
7738 if (copy_from_user(&p, params, sizeof(p)))
7739 return -EFAULT;
7740 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7741 if (p.resv[i])
7742 return -EINVAL;
7743 }
7744
Jens Axboe6c271ce2019-01-10 11:22:30 -07007745 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007746 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007747 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007748 return -EINVAL;
7749
7750 ret = io_uring_create(entries, &p);
7751 if (ret < 0)
7752 return ret;
7753
7754 if (copy_to_user(params, &p, sizeof(p)))
7755 return -EFAULT;
7756
7757 return ret;
7758}
7759
7760SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7761 struct io_uring_params __user *, params)
7762{
7763 return io_uring_setup(entries, params);
7764}
7765
Jens Axboe66f4af92020-01-16 15:36:52 -07007766static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7767{
7768 struct io_uring_probe *p;
7769 size_t size;
7770 int i, ret;
7771
7772 size = struct_size(p, ops, nr_args);
7773 if (size == SIZE_MAX)
7774 return -EOVERFLOW;
7775 p = kzalloc(size, GFP_KERNEL);
7776 if (!p)
7777 return -ENOMEM;
7778
7779 ret = -EFAULT;
7780 if (copy_from_user(p, arg, size))
7781 goto out;
7782 ret = -EINVAL;
7783 if (memchr_inv(p, 0, size))
7784 goto out;
7785
7786 p->last_op = IORING_OP_LAST - 1;
7787 if (nr_args > IORING_OP_LAST)
7788 nr_args = IORING_OP_LAST;
7789
7790 for (i = 0; i < nr_args; i++) {
7791 p->ops[i].op = i;
7792 if (!io_op_defs[i].not_supported)
7793 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7794 }
7795 p->ops_len = i;
7796
7797 ret = 0;
7798 if (copy_to_user(arg, p, size))
7799 ret = -EFAULT;
7800out:
7801 kfree(p);
7802 return ret;
7803}
7804
Jens Axboe071698e2020-01-28 10:04:42 -07007805static int io_register_personality(struct io_ring_ctx *ctx)
7806{
7807 const struct cred *creds = get_current_cred();
7808 int id;
7809
7810 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7811 USHRT_MAX, GFP_KERNEL);
7812 if (id < 0)
7813 put_cred(creds);
7814 return id;
7815}
7816
7817static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7818{
7819 const struct cred *old_creds;
7820
7821 old_creds = idr_remove(&ctx->personality_idr, id);
7822 if (old_creds) {
7823 put_cred(old_creds);
7824 return 0;
7825 }
7826
7827 return -EINVAL;
7828}
7829
7830static bool io_register_op_must_quiesce(int op)
7831{
7832 switch (op) {
7833 case IORING_UNREGISTER_FILES:
7834 case IORING_REGISTER_FILES_UPDATE:
7835 case IORING_REGISTER_PROBE:
7836 case IORING_REGISTER_PERSONALITY:
7837 case IORING_UNREGISTER_PERSONALITY:
7838 return false;
7839 default:
7840 return true;
7841 }
7842}
7843
Jens Axboeedafcce2019-01-09 09:16:05 -07007844static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7845 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007846 __releases(ctx->uring_lock)
7847 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007848{
7849 int ret;
7850
Jens Axboe35fa71a2019-04-22 10:23:23 -06007851 /*
7852 * We're inside the ring mutex, if the ref is already dying, then
7853 * someone else killed the ctx or is already going through
7854 * io_uring_register().
7855 */
7856 if (percpu_ref_is_dying(&ctx->refs))
7857 return -ENXIO;
7858
Jens Axboe071698e2020-01-28 10:04:42 -07007859 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007860 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007861
Jens Axboe05f3fb32019-12-09 11:22:50 -07007862 /*
7863 * Drop uring mutex before waiting for references to exit. If
7864 * another thread is currently inside io_uring_enter() it might
7865 * need to grab the uring_lock to make progress. If we hold it
7866 * here across the drain wait, then we can deadlock. It's safe
7867 * to drop the mutex here, since no new references will come in
7868 * after we've killed the percpu ref.
7869 */
7870 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007871 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007872 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007873 if (ret) {
7874 percpu_ref_resurrect(&ctx->refs);
7875 ret = -EINTR;
7876 goto out;
7877 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007878 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007879
7880 switch (opcode) {
7881 case IORING_REGISTER_BUFFERS:
7882 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7883 break;
7884 case IORING_UNREGISTER_BUFFERS:
7885 ret = -EINVAL;
7886 if (arg || nr_args)
7887 break;
7888 ret = io_sqe_buffer_unregister(ctx);
7889 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007890 case IORING_REGISTER_FILES:
7891 ret = io_sqe_files_register(ctx, arg, nr_args);
7892 break;
7893 case IORING_UNREGISTER_FILES:
7894 ret = -EINVAL;
7895 if (arg || nr_args)
7896 break;
7897 ret = io_sqe_files_unregister(ctx);
7898 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007899 case IORING_REGISTER_FILES_UPDATE:
7900 ret = io_sqe_files_update(ctx, arg, nr_args);
7901 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007902 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007903 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007904 ret = -EINVAL;
7905 if (nr_args != 1)
7906 break;
7907 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007908 if (ret)
7909 break;
7910 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7911 ctx->eventfd_async = 1;
7912 else
7913 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007914 break;
7915 case IORING_UNREGISTER_EVENTFD:
7916 ret = -EINVAL;
7917 if (arg || nr_args)
7918 break;
7919 ret = io_eventfd_unregister(ctx);
7920 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007921 case IORING_REGISTER_PROBE:
7922 ret = -EINVAL;
7923 if (!arg || nr_args > 256)
7924 break;
7925 ret = io_probe(ctx, arg, nr_args);
7926 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007927 case IORING_REGISTER_PERSONALITY:
7928 ret = -EINVAL;
7929 if (arg || nr_args)
7930 break;
7931 ret = io_register_personality(ctx);
7932 break;
7933 case IORING_UNREGISTER_PERSONALITY:
7934 ret = -EINVAL;
7935 if (arg)
7936 break;
7937 ret = io_unregister_personality(ctx, nr_args);
7938 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007939 default:
7940 ret = -EINVAL;
7941 break;
7942 }
7943
Jens Axboe071698e2020-01-28 10:04:42 -07007944 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007945 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007946 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007947out:
7948 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007949 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007950 return ret;
7951}
7952
7953SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7954 void __user *, arg, unsigned int, nr_args)
7955{
7956 struct io_ring_ctx *ctx;
7957 long ret = -EBADF;
7958 struct fd f;
7959
7960 f = fdget(fd);
7961 if (!f.file)
7962 return -EBADF;
7963
7964 ret = -EOPNOTSUPP;
7965 if (f.file->f_op != &io_uring_fops)
7966 goto out_fput;
7967
7968 ctx = f.file->private_data;
7969
7970 mutex_lock(&ctx->uring_lock);
7971 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7972 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007973 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7974 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007975out_fput:
7976 fdput(f);
7977 return ret;
7978}
7979
Jens Axboe2b188cc2019-01-07 10:46:33 -07007980static int __init io_uring_init(void)
7981{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007982#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7983 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7984 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7985} while (0)
7986
7987#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7988 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7989 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7990 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7991 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7992 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7993 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7994 BUILD_BUG_SQE_ELEM(8, __u64, off);
7995 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7996 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007997 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007998 BUILD_BUG_SQE_ELEM(24, __u32, len);
7999 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8000 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8001 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8002 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8003 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8004 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8005 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8006 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8007 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8008 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8009 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8010 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8011 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008012 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008013 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8014 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8015 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008016 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008017
Jens Axboed3656342019-12-18 09:50:26 -07008018 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008019 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008020 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8021 return 0;
8022};
8023__initcall(io_uring_init);