blob: f3dbf83fabf381fd057b715771cd6a77becd9a4b [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>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020082#define CREATE_TRACE_POINTS
83#include <trace/events/io_uring.h>
84
Jens Axboe2b188cc2019-01-07 10:46:33 -070085#include <uapi/linux/io_uring.h>
86
87#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060088#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070089
Daniel Xu5277dea2019-09-14 14:23:45 -070090#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060091#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060092
93/*
94 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
95 */
96#define IORING_FILE_TABLE_SHIFT 9
97#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
98#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
99#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700100
101struct io_uring {
102 u32 head ____cacheline_aligned_in_smp;
103 u32 tail ____cacheline_aligned_in_smp;
104};
105
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000107 * This data is shared with the application through the mmap at offsets
108 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 *
110 * The offsets to the member fields are published through struct
111 * io_sqring_offsets when calling io_uring_setup.
112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
115 * Head and tail offsets into the ring; the offsets need to be
116 * masked to get valid indices.
117 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 * The kernel controls head of the sq ring and the tail of the cq ring,
119 * and the application controls tail of the sq ring and the head of the
120 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 * ring_entries - 1)
126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127 u32 sq_ring_mask, cq_ring_mask;
128 /* Ring sizes (constant, power of 2) */
129 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200130 /*
131 * Number of invalid entries dropped by the kernel due to
132 * invalid index stored in array
133 *
134 * Written by the kernel, shouldn't be modified by the
135 * application (i.e. get number of "new events" by comparing to
136 * cached value).
137 *
138 * After a new SQ head value was read by the application this
139 * counter includes all submissions that were dropped reaching
140 * the new SQ head (and possibly more).
141 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000142 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200143 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200144 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200145 *
146 * Written by the kernel, shouldn't be modified by the
147 * application.
148 *
149 * The application needs a full memory barrier before checking
150 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
151 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000152 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200154 * Runtime CQ flags
155 *
156 * Written by the application, shouldn't be modified by the
157 * kernel.
158 */
159 u32 cq_flags;
160 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200161 * Number of completion events lost because the queue was full;
162 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800163 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200164 * the completion queue.
165 *
166 * Written by the kernel, shouldn't be modified by the
167 * application (i.e. get number of "new events" by comparing to
168 * cached value).
169 *
170 * As completion events come in out of order this counter is not
171 * ordered with any other data.
172 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000173 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200174 /*
175 * Ring buffer of completion events.
176 *
177 * The kernel writes completion events fresh every time they are
178 * produced, so the application is allowed to modify pending
179 * entries.
180 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000181 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700182};
183
Jens Axboeedafcce2019-01-09 09:16:05 -0700184struct io_mapped_ubuf {
185 u64 ubuf;
186 size_t len;
187 struct bio_vec *bvec;
188 unsigned int nr_bvecs;
189};
190
Jens Axboe65e19f52019-10-26 07:20:21 -0600191struct fixed_file_table {
192 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700193};
194
Xiaoguang Wang05589552020-03-31 14:05:18 +0800195struct fixed_file_ref_node {
196 struct percpu_ref refs;
197 struct list_head node;
198 struct list_head file_list;
199 struct fixed_file_data *file_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600200 struct llist_node llist;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800201};
202
Jens Axboe05f3fb32019-12-09 11:22:50 -0700203struct fixed_file_data {
204 struct fixed_file_table *table;
205 struct io_ring_ctx *ctx;
206
Xiaoguang Wang05589552020-03-31 14:05:18 +0800207 struct percpu_ref *cur_refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700208 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700209 struct completion done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800210 struct list_head ref_list;
211 spinlock_t lock;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700212};
213
Jens Axboe5a2e7452020-02-23 16:23:11 -0700214struct io_buffer {
215 struct list_head list;
216 __u64 addr;
217 __s32 len;
218 __u16 bid;
219};
220
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221struct io_ring_ctx {
222 struct {
223 struct percpu_ref refs;
224 } ____cacheline_aligned_in_smp;
225
226 struct {
227 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800228 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700229 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800230 unsigned int cq_overflow_flushed: 1;
231 unsigned int drain_next: 1;
232 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700233
Hristo Venev75b28af2019-08-26 17:23:46 +0000234 /*
235 * Ring buffer of indices into array of io_uring_sqe, which is
236 * mmapped by the application using the IORING_OFF_SQES offset.
237 *
238 * This indirection could e.g. be used to assign fixed
239 * io_uring_sqe entries to operations and only submit them to
240 * the queue when needed.
241 *
242 * The kernel modifies neither the indices array nor the entries
243 * array.
244 */
245 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246 unsigned cached_sq_head;
247 unsigned sq_entries;
248 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700249 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600250 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700251 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700252 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600253
254 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600255 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700256 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700257
Jens Axboefcb323c2019-10-24 12:39:47 -0600258 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700259 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700260 } ____cacheline_aligned_in_smp;
261
Hristo Venev75b28af2019-08-26 17:23:46 +0000262 struct io_rings *rings;
263
Jens Axboe2b188cc2019-01-07 10:46:33 -0700264 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600265 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700266 struct task_struct *sqo_thread; /* if using sq thread polling */
267 struct mm_struct *sqo_mm;
268 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700269
Jens Axboe6b063142019-01-10 22:13:58 -0700270 /*
271 * If used, fixed file set. Writers must ensure that ->refs is dead,
272 * readers must ensure that ->refs is alive as long as the file* is
273 * used. Only updated through io_uring_register(2).
274 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700275 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700276 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300277 int ring_fd;
278 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700279
Jens Axboeedafcce2019-01-09 09:16:05 -0700280 /* if used, fixed mapped user buffers */
281 unsigned nr_user_bufs;
282 struct io_mapped_ubuf *user_bufs;
283
Jens Axboe2b188cc2019-01-07 10:46:33 -0700284 struct user_struct *user;
285
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700286 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700287
Jens Axboe0f158b42020-05-14 17:18:39 -0600288 struct completion ref_comp;
289 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700290
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700291 /* if all else fails... */
292 struct io_kiocb *fallback_req;
293
Jens Axboe206aefd2019-11-07 18:27:42 -0700294#if defined(CONFIG_UNIX)
295 struct socket *ring_sock;
296#endif
297
Jens Axboe5a2e7452020-02-23 16:23:11 -0700298 struct idr io_buffer_idr;
299
Jens Axboe071698e2020-01-28 10:04:42 -0700300 struct idr personality_idr;
301
Jens Axboe206aefd2019-11-07 18:27:42 -0700302 struct {
303 unsigned cached_cq_tail;
304 unsigned cq_entries;
305 unsigned cq_mask;
306 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700307 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700308 struct wait_queue_head cq_wait;
309 struct fasync_struct *cq_fasync;
310 struct eventfd_ctx *cq_ev_fd;
311 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312
313 struct {
314 struct mutex uring_lock;
315 wait_queue_head_t wait;
316 } ____cacheline_aligned_in_smp;
317
318 struct {
319 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700320
Jens Axboedef596e2019-01-09 08:59:42 -0700321 /*
322 * ->poll_list is protected by the ctx->uring_lock for
323 * io_uring instances that don't use IORING_SETUP_SQPOLL.
324 * For SQPOLL, only the single threaded io_sq_thread() will
325 * manipulate the list, hence no extra locking is needed there.
326 */
327 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700328 struct hlist_head *cancel_hash;
329 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700330 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600331
332 spinlock_t inflight_lock;
333 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700334 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600335
Jens Axboe4a38aed22020-05-14 17:21:15 -0600336 struct delayed_work file_put_work;
337 struct llist_head file_put_llist;
338
Jens Axboe85faa7b2020-04-09 18:14:00 -0600339 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700340};
341
Jens Axboe09bb8392019-03-13 12:39:28 -0600342/*
343 * First field must be the file pointer in all the
344 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
345 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700346struct io_poll_iocb {
347 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700348 union {
349 struct wait_queue_head *head;
350 u64 addr;
351 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700352 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600353 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700354 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700355 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700356};
357
Jens Axboeb5dba592019-12-11 14:02:38 -0700358struct io_close {
359 struct file *file;
360 struct file *put_file;
361 int fd;
362};
363
Jens Axboead8a48a2019-11-15 08:49:11 -0700364struct io_timeout_data {
365 struct io_kiocb *req;
366 struct hrtimer timer;
367 struct timespec64 ts;
368 enum hrtimer_mode mode;
369};
370
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700371struct io_accept {
372 struct file *file;
373 struct sockaddr __user *addr;
374 int __user *addr_len;
375 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600376 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700377};
378
379struct io_sync {
380 struct file *file;
381 loff_t len;
382 loff_t off;
383 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700384 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700385};
386
Jens Axboefbf23842019-12-17 18:45:56 -0700387struct io_cancel {
388 struct file *file;
389 u64 addr;
390};
391
Jens Axboeb29472e2019-12-17 18:50:29 -0700392struct io_timeout {
393 struct file *file;
394 u64 addr;
395 int flags;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300396 u32 off;
397 u32 target_seq;
Jens Axboeb29472e2019-12-17 18:50:29 -0700398};
399
Jens Axboe9adbd452019-12-20 08:45:55 -0700400struct io_rw {
401 /* NOTE: kiocb has the file as the first member, so don't do it here */
402 struct kiocb kiocb;
403 u64 addr;
404 u64 len;
405};
406
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700407struct io_connect {
408 struct file *file;
409 struct sockaddr __user *addr;
410 int addr_len;
411};
412
Jens Axboee47293f2019-12-20 08:58:21 -0700413struct io_sr_msg {
414 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700415 union {
416 struct user_msghdr __user *msg;
417 void __user *buf;
418 };
Jens Axboee47293f2019-12-20 08:58:21 -0700419 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700420 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700421 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700422 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700423};
424
Jens Axboe15b71ab2019-12-11 11:20:36 -0700425struct io_open {
426 struct file *file;
427 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700428 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700429 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600430 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700431};
432
Jens Axboe05f3fb32019-12-09 11:22:50 -0700433struct io_files_update {
434 struct file *file;
435 u64 arg;
436 u32 nr_args;
437 u32 offset;
438};
439
Jens Axboe4840e412019-12-25 22:03:45 -0700440struct io_fadvise {
441 struct file *file;
442 u64 offset;
443 u32 len;
444 u32 advice;
445};
446
Jens Axboec1ca7572019-12-25 22:18:28 -0700447struct io_madvise {
448 struct file *file;
449 u64 addr;
450 u32 len;
451 u32 advice;
452};
453
Jens Axboe3e4827b2020-01-08 15:18:09 -0700454struct io_epoll {
455 struct file *file;
456 int epfd;
457 int op;
458 int fd;
459 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700460};
461
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300462struct io_splice {
463 struct file *file_out;
464 struct file *file_in;
465 loff_t off_out;
466 loff_t off_in;
467 u64 len;
468 unsigned int flags;
469};
470
Jens Axboeddf0322d2020-02-23 16:41:33 -0700471struct io_provide_buf {
472 struct file *file;
473 __u64 addr;
474 __s32 len;
475 __u32 bgid;
476 __u16 nbufs;
477 __u16 bid;
478};
479
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700480struct io_statx {
481 struct file *file;
482 int dfd;
483 unsigned int mask;
484 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700485 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700486 struct statx __user *buffer;
487};
488
Jens Axboef499a022019-12-02 16:28:46 -0700489struct io_async_connect {
490 struct sockaddr_storage address;
491};
492
Jens Axboe03b12302019-12-02 18:50:25 -0700493struct io_async_msghdr {
494 struct iovec fast_iov[UIO_FASTIOV];
495 struct iovec *iov;
496 struct sockaddr __user *uaddr;
497 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700498 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700499};
500
Jens Axboef67676d2019-12-02 11:03:47 -0700501struct io_async_rw {
502 struct iovec fast_iov[UIO_FASTIOV];
503 struct iovec *iov;
504 ssize_t nr_segs;
505 ssize_t size;
506};
507
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700508struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700509 union {
510 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700511 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700512 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700513 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700514 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700515};
516
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300517enum {
518 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
519 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
520 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
521 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
522 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700523 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300524
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300525 REQ_F_LINK_HEAD_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300526 REQ_F_LINK_NEXT_BIT,
527 REQ_F_FAIL_LINK_BIT,
528 REQ_F_INFLIGHT_BIT,
529 REQ_F_CUR_POS_BIT,
530 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300531 REQ_F_LINK_TIMEOUT_BIT,
532 REQ_F_TIMEOUT_BIT,
533 REQ_F_ISREG_BIT,
534 REQ_F_MUST_PUNT_BIT,
535 REQ_F_TIMEOUT_NOSEQ_BIT,
536 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300537 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700538 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700539 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700540 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600541 REQ_F_NO_FILE_TABLE_BIT,
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300542 REQ_F_QUEUE_TIMEOUT_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800543 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300544 REQ_F_TASK_PINNED_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700545
546 /* not a real bit, just to check we're not overflowing the space */
547 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300548};
549
550enum {
551 /* ctx owns file */
552 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
553 /* drain existing IO first */
554 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
555 /* linked sqes */
556 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
557 /* doesn't sever on completion < 0 */
558 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
559 /* IOSQE_ASYNC */
560 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700561 /* IOSQE_BUFFER_SELECT */
562 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300563
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300564 /* head of a link */
565 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300566 /* already grabbed next link */
567 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
568 /* fail rest of links */
569 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
570 /* on inflight list */
571 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
572 /* read/write uses file position */
573 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
574 /* must not punt to workers */
575 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300576 /* has linked timeout */
577 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
578 /* timeout request */
579 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
580 /* regular file */
581 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
582 /* must be punted even for NONBLOCK */
583 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
584 /* no timeout sequence */
585 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
586 /* completion under lock */
587 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300588 /* needs cleanup */
589 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700590 /* in overflow list */
591 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700592 /* already went through poll handler */
593 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700594 /* buffer already selected */
595 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600596 /* doesn't need file table for this request */
597 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Pavel Begunkovd4c81f32020-06-08 21:08:19 +0300598 /* needs to queue linked timeout */
599 REQ_F_QUEUE_TIMEOUT = BIT(REQ_F_QUEUE_TIMEOUT_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800600 /* io_wq_work is initialized */
601 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300602 /* req->task is refcounted */
603 REQ_F_TASK_PINNED = BIT(REQ_F_TASK_PINNED_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700604};
605
606struct async_poll {
607 struct io_poll_iocb poll;
608 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300609};
610
Jens Axboe09bb8392019-03-13 12:39:28 -0600611/*
612 * NOTE! Each of the iocb union members has the file pointer
613 * as the first entry in their struct definition. So you can
614 * access the file pointer through any of the sub-structs,
615 * or directly as just 'ki_filp' in this struct.
616 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700617struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700618 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600619 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700620 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700621 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700622 struct io_accept accept;
623 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700624 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700625 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700626 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700627 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700628 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700629 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700630 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700631 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700632 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700633 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300634 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700635 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700636 struct io_statx statx;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700637 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700639 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300640 int cflags;
Jens Axboed625c6e2019-12-17 19:53:05 -0700641 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800642 /* polled IO has completed */
643 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700645 u16 buf_index;
646
Jens Axboe2b188cc2019-01-07 10:46:33 -0700647 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700648 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700650 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600651 struct task_struct *task;
652 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700653 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600654 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600655 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700656
Jens Axboed7718a92020-02-14 22:23:12 -0700657 struct list_head link_list;
658
Jens Axboefcb323c2019-10-24 12:39:47 -0600659 struct list_head inflight_entry;
660
Xiaoguang Wang05589552020-03-31 14:05:18 +0800661 struct percpu_ref *fixed_file_refs;
662
Jens Axboeb41e9852020-02-17 09:52:41 -0700663 union {
664 /*
665 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700666 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
667 * async armed poll handlers for regular commands. The latter
668 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700669 */
670 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700671 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700672 struct hlist_node hash_node;
673 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700674 };
675 struct io_wq_work work;
676 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700677};
678
Jens Axboedef596e2019-01-09 08:59:42 -0700679#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700680
Jens Axboe9a56a232019-01-09 09:06:50 -0700681struct io_submit_state {
682 struct blk_plug plug;
683
684 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700685 * io_kiocb alloc cache
686 */
687 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300688 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700689
690 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700691 * File reference cache
692 */
693 struct file *file;
694 unsigned int fd;
695 unsigned int has_refs;
696 unsigned int used_refs;
697 unsigned int ios_left;
698};
699
Jens Axboed3656342019-12-18 09:50:26 -0700700struct io_op_def {
701 /* needs req->io allocated for deferral/async */
702 unsigned async_ctx : 1;
703 /* needs current->mm setup, does mm access */
704 unsigned needs_mm : 1;
705 /* needs req->file assigned */
706 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600707 /* don't fail if file grab fails */
708 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700709 /* hash wq insertion if file is a regular file */
710 unsigned hash_reg_file : 1;
711 /* unbound wq insertion if file is a non-regular file */
712 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700713 /* opcode is not supported by this kernel */
714 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700715 /* needs file table */
716 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700717 /* needs ->fs */
718 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700719 /* set if opcode supports polled "wait" */
720 unsigned pollin : 1;
721 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700722 /* op supports buffer selection */
723 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700724};
725
726static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300727 [IORING_OP_NOP] = {},
728 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700729 .async_ctx = 1,
730 .needs_mm = 1,
731 .needs_file = 1,
732 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700733 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700734 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700735 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300736 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700737 .async_ctx = 1,
738 .needs_mm = 1,
739 .needs_file = 1,
740 .hash_reg_file = 1,
741 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700742 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700743 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300744 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700745 .needs_file = 1,
746 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300747 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700748 .needs_file = 1,
749 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700750 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700751 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300752 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700753 .needs_file = 1,
754 .hash_reg_file = 1,
755 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700756 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700757 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300758 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700759 .needs_file = 1,
760 .unbound_nonreg_file = 1,
761 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300762 [IORING_OP_POLL_REMOVE] = {},
763 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700764 .needs_file = 1,
765 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300766 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700767 .async_ctx = 1,
768 .needs_mm = 1,
769 .needs_file = 1,
770 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700771 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700772 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .async_ctx = 1,
776 .needs_mm = 1,
777 .needs_file = 1,
778 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700779 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700780 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700781 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700782 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300783 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700784 .async_ctx = 1,
785 .needs_mm = 1,
786 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300787 [IORING_OP_TIMEOUT_REMOVE] = {},
788 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700789 .needs_mm = 1,
790 .needs_file = 1,
791 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700792 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700793 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700794 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300795 [IORING_OP_ASYNC_CANCEL] = {},
796 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700797 .async_ctx = 1,
798 .needs_mm = 1,
799 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300800 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700801 .async_ctx = 1,
802 .needs_mm = 1,
803 .needs_file = 1,
804 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700805 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700806 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300807 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700808 .needs_file = 1,
809 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300810 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700811 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700812 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700813 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300814 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600815 .needs_file = 1,
816 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700817 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700818 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300819 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700820 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700821 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700822 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300823 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700824 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700825 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600826 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700827 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300828 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700829 .needs_mm = 1,
830 .needs_file = 1,
831 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700832 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700833 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700834 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300835 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700836 .needs_mm = 1,
837 .needs_file = 1,
838 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700839 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700840 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300841 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700842 .needs_file = 1,
843 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700845 .needs_mm = 1,
846 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300847 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700848 .needs_mm = 1,
849 .needs_file = 1,
850 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700851 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700852 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300853 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700854 .needs_mm = 1,
855 .needs_file = 1,
856 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700857 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700858 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700859 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300860 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700861 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700862 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700863 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700864 [IORING_OP_EPOLL_CTL] = {
865 .unbound_nonreg_file = 1,
866 .file_table = 1,
867 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300868 [IORING_OP_SPLICE] = {
869 .needs_file = 1,
870 .hash_reg_file = 1,
871 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700872 },
873 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700874 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300875 [IORING_OP_TEE] = {
876 .needs_file = 1,
877 .hash_reg_file = 1,
878 .unbound_nonreg_file = 1,
879 },
Jens Axboed3656342019-12-18 09:50:26 -0700880};
881
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -0700882enum io_mem_account {
883 ACCT_LOCKED,
884 ACCT_PINNED,
885};
886
Jens Axboe561fb042019-10-24 07:25:42 -0600887static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700888static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800889static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700890static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700891static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
892static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700893static int __io_sqe_files_update(struct io_ring_ctx *ctx,
894 struct io_uring_files_update *ip,
895 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700896static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300897static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700898static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
899 int fd, struct file **out_file, bool fixed);
900static void __io_queue_sqe(struct io_kiocb *req,
901 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600902
Jens Axboeb63534c2020-06-04 11:28:00 -0600903static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
904 struct iovec **iovec, struct iov_iter *iter,
905 bool needs_lock);
906static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
907 struct iovec *iovec, struct iovec *fast_iov,
908 struct iov_iter *iter);
909
Jens Axboe2b188cc2019-01-07 10:46:33 -0700910static struct kmem_cache *req_cachep;
911
912static const struct file_operations io_uring_fops;
913
914struct sock *io_uring_get_socket(struct file *file)
915{
916#if defined(CONFIG_UNIX)
917 if (file->f_op == &io_uring_fops) {
918 struct io_ring_ctx *ctx = file->private_data;
919
920 return ctx->ring_sock->sk;
921 }
922#endif
923 return NULL;
924}
925EXPORT_SYMBOL(io_uring_get_socket);
926
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300927static void io_get_req_task(struct io_kiocb *req)
928{
929 if (req->flags & REQ_F_TASK_PINNED)
930 return;
931 get_task_struct(req->task);
932 req->flags |= REQ_F_TASK_PINNED;
933}
934
935/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
936static void __io_put_req_task(struct io_kiocb *req)
937{
938 if (req->flags & REQ_F_TASK_PINNED)
939 put_task_struct(req->task);
940}
941
Jens Axboe4a38aed22020-05-14 17:21:15 -0600942static void io_file_put_work(struct work_struct *work);
943
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800944/*
945 * Note: must call io_req_init_async() for the first time you
946 * touch any members of io_wq_work.
947 */
948static inline void io_req_init_async(struct io_kiocb *req)
949{
950 if (req->flags & REQ_F_WORK_INITIALIZED)
951 return;
952
953 memset(&req->work, 0, sizeof(req->work));
954 req->flags |= REQ_F_WORK_INITIALIZED;
955}
956
Pavel Begunkov0cdaf762020-05-17 14:13:40 +0300957static inline bool io_async_submit(struct io_ring_ctx *ctx)
958{
959 return ctx->flags & IORING_SETUP_SQPOLL;
960}
961
Jens Axboe2b188cc2019-01-07 10:46:33 -0700962static void io_ring_ctx_ref_free(struct percpu_ref *ref)
963{
964 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
965
Jens Axboe0f158b42020-05-14 17:18:39 -0600966 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700967}
968
969static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
970{
971 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700972 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700973
974 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
975 if (!ctx)
976 return NULL;
977
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700978 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
979 if (!ctx->fallback_req)
980 goto err;
981
Jens Axboe78076bb2019-12-04 19:56:40 -0700982 /*
983 * Use 5 bits less than the max cq entries, that should give us around
984 * 32 entries per hash list if totally full and uniformly spread.
985 */
986 hash_bits = ilog2(p->cq_entries);
987 hash_bits -= 5;
988 if (hash_bits <= 0)
989 hash_bits = 1;
990 ctx->cancel_hash_bits = hash_bits;
991 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
992 GFP_KERNEL);
993 if (!ctx->cancel_hash)
994 goto err;
995 __hash_init(ctx->cancel_hash, 1U << hash_bits);
996
Roman Gushchin21482892019-05-07 10:01:48 -0700997 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700998 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
999 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001000
1001 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -06001002 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001003 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001004 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001005 init_completion(&ctx->ref_comp);
1006 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001007 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001008 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009 mutex_init(&ctx->uring_lock);
1010 init_waitqueue_head(&ctx->wait);
1011 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001012 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001013 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001014 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001015 init_waitqueue_head(&ctx->inflight_wait);
1016 spin_lock_init(&ctx->inflight_lock);
1017 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001018 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1019 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001020 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001021err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001022 if (ctx->fallback_req)
1023 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001024 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001025 kfree(ctx);
1026 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001027}
1028
Bob Liu9d858b22019-11-13 18:06:25 +08001029static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06001030{
Jackie Liua197f662019-11-08 08:09:12 -07001031 struct io_ring_ctx *ctx = req->ctx;
1032
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001033 return req->sequence != ctx->cached_cq_tail
1034 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -06001035}
1036
Bob Liu9d858b22019-11-13 18:06:25 +08001037static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001038{
Pavel Begunkov87987892020-01-18 01:22:30 +03001039 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +08001040 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001041
Bob Liu9d858b22019-11-13 18:06:25 +08001042 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001043}
1044
Jens Axboede0617e2019-04-06 21:51:27 -06001045static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046{
Hristo Venev75b28af2019-08-26 17:23:46 +00001047 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001048
Pavel Begunkov07910152020-01-17 03:52:46 +03001049 /* order cqe stores with ring update */
1050 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001051
Pavel Begunkov07910152020-01-17 03:52:46 +03001052 if (wq_has_sleeper(&ctx->cq_wait)) {
1053 wake_up_interruptible(&ctx->cq_wait);
1054 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001055 }
1056}
1057
Jens Axboecccf0ee2020-01-27 16:34:48 -07001058static inline void io_req_work_grab_env(struct io_kiocb *req,
1059 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001060{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001061 if (!req->work.mm && def->needs_mm) {
1062 mmgrab(current->mm);
1063 req->work.mm = current->mm;
1064 }
1065 if (!req->work.creds)
1066 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001067 if (!req->work.fs && def->needs_fs) {
1068 spin_lock(&current->fs->lock);
1069 if (!current->fs->in_exec) {
1070 req->work.fs = current->fs;
1071 req->work.fs->users++;
1072 } else {
1073 req->work.flags |= IO_WQ_WORK_CANCEL;
1074 }
1075 spin_unlock(&current->fs->lock);
1076 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001077}
1078
1079static inline void io_req_work_drop_env(struct io_kiocb *req)
1080{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001081 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1082 return;
1083
Jens Axboecccf0ee2020-01-27 16:34:48 -07001084 if (req->work.mm) {
1085 mmdrop(req->work.mm);
1086 req->work.mm = NULL;
1087 }
1088 if (req->work.creds) {
1089 put_cred(req->work.creds);
1090 req->work.creds = NULL;
1091 }
Jens Axboeff002b32020-02-07 16:05:21 -07001092 if (req->work.fs) {
1093 struct fs_struct *fs = req->work.fs;
1094
1095 spin_lock(&req->work.fs->lock);
1096 if (--fs->users)
1097 fs = NULL;
1098 spin_unlock(&req->work.fs->lock);
1099 if (fs)
1100 free_fs_struct(fs);
1101 }
Jens Axboe561fb042019-10-24 07:25:42 -06001102}
1103
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001104static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001105 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001106{
Jens Axboed3656342019-12-18 09:50:26 -07001107 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001108
Jens Axboed3656342019-12-18 09:50:26 -07001109 if (req->flags & REQ_F_ISREG) {
1110 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001111 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001112 } else {
1113 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001114 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001115 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001116
Pavel Begunkov59960b92020-06-15 16:36:30 +03001117 io_req_init_async(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001118 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001119
Jens Axboe94ae5e72019-11-14 19:39:52 -07001120 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001121}
1122
Jackie Liua197f662019-11-08 08:09:12 -07001123static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001124{
Jackie Liua197f662019-11-08 08:09:12 -07001125 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001126 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001127
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001128 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001129
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001130 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1131 &req->work, req->flags);
1132 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001133
1134 if (link)
1135 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001136}
1137
Jens Axboe5262f562019-09-17 12:26:57 -06001138static void io_kill_timeout(struct io_kiocb *req)
1139{
1140 int ret;
1141
Jens Axboe2d283902019-12-04 11:08:05 -07001142 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001143 if (ret != -1) {
1144 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001145 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001146 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001147 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001148 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001149 }
1150}
1151
1152static void io_kill_timeouts(struct io_ring_ctx *ctx)
1153{
1154 struct io_kiocb *req, *tmp;
1155
1156 spin_lock_irq(&ctx->completion_lock);
1157 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1158 io_kill_timeout(req);
1159 spin_unlock_irq(&ctx->completion_lock);
1160}
1161
Pavel Begunkov04518942020-05-26 20:34:05 +03001162static void __io_queue_deferred(struct io_ring_ctx *ctx)
1163{
1164 do {
1165 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1166 struct io_kiocb, list);
1167
1168 if (req_need_defer(req))
1169 break;
1170 list_del_init(&req->list);
1171 io_queue_async_work(req);
1172 } while (!list_empty(&ctx->defer_list));
1173}
1174
Pavel Begunkov360428f2020-05-30 14:54:17 +03001175static void io_flush_timeouts(struct io_ring_ctx *ctx)
1176{
1177 while (!list_empty(&ctx->timeout_list)) {
1178 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1179 struct io_kiocb, list);
1180
1181 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
1182 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001183 if (req->timeout.target_seq != ctx->cached_cq_tail
1184 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001185 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001186
Pavel Begunkov360428f2020-05-30 14:54:17 +03001187 list_del_init(&req->list);
1188 io_kill_timeout(req);
1189 }
1190}
1191
Jens Axboede0617e2019-04-06 21:51:27 -06001192static void io_commit_cqring(struct io_ring_ctx *ctx)
1193{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001194 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001195 __io_commit_cqring(ctx);
1196
Pavel Begunkov04518942020-05-26 20:34:05 +03001197 if (unlikely(!list_empty(&ctx->defer_list)))
1198 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001199}
1200
Jens Axboe2b188cc2019-01-07 10:46:33 -07001201static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1202{
Hristo Venev75b28af2019-08-26 17:23:46 +00001203 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001204 unsigned tail;
1205
1206 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001207 /*
1208 * writes to the cq entry need to come after reading head; the
1209 * control dependency is enough as we're using WRITE_ONCE to
1210 * fill the cq entry
1211 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001212 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001213 return NULL;
1214
1215 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001216 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001217}
1218
Jens Axboef2842ab2020-01-08 11:04:00 -07001219static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1220{
Jens Axboef0b493e2020-02-01 21:30:11 -07001221 if (!ctx->cq_ev_fd)
1222 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001223 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1224 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001225 if (!ctx->eventfd_async)
1226 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001227 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001228}
1229
Jens Axboeb41e9852020-02-17 09:52:41 -07001230static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001231{
1232 if (waitqueue_active(&ctx->wait))
1233 wake_up(&ctx->wait);
1234 if (waitqueue_active(&ctx->sqo_wait))
1235 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001236 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001237 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001238}
1239
Jens Axboec4a2ed72019-11-21 21:01:26 -07001240/* Returns true if there are no backlogged entries after the flush */
1241static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001243 struct io_rings *rings = ctx->rings;
1244 struct io_uring_cqe *cqe;
1245 struct io_kiocb *req;
1246 unsigned long flags;
1247 LIST_HEAD(list);
1248
1249 if (!force) {
1250 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001251 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001252 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1253 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001254 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001255 }
1256
1257 spin_lock_irqsave(&ctx->completion_lock, flags);
1258
1259 /* if force is set, the ring is going away. always drop after that */
1260 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001261 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001262
Jens Axboec4a2ed72019-11-21 21:01:26 -07001263 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001264 while (!list_empty(&ctx->cq_overflow_list)) {
1265 cqe = io_get_cqring(ctx);
1266 if (!cqe && !force)
1267 break;
1268
1269 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1270 list);
1271 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001272 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001273 if (cqe) {
1274 WRITE_ONCE(cqe->user_data, req->user_data);
1275 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001276 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001277 } else {
1278 WRITE_ONCE(ctx->rings->cq_overflow,
1279 atomic_inc_return(&ctx->cached_cq_overflow));
1280 }
1281 }
1282
1283 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001284 if (cqe) {
1285 clear_bit(0, &ctx->sq_check_overflow);
1286 clear_bit(0, &ctx->cq_check_overflow);
1287 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001288 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1289 io_cqring_ev_posted(ctx);
1290
1291 while (!list_empty(&list)) {
1292 req = list_first_entry(&list, struct io_kiocb, list);
1293 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001294 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001295 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001296
1297 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001298}
1299
Jens Axboebcda7ba2020-02-23 16:42:51 -07001300static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001301{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001302 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001303 struct io_uring_cqe *cqe;
1304
Jens Axboe78e19bb2019-11-06 15:21:34 -07001305 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001306
Jens Axboe2b188cc2019-01-07 10:46:33 -07001307 /*
1308 * If we can't get a cq entry, userspace overflowed the
1309 * submission (by quite a lot). Increment the overflow count in
1310 * the ring.
1311 */
1312 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001313 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001314 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001315 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001316 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001317 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318 WRITE_ONCE(ctx->rings->cq_overflow,
1319 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001320 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001321 if (list_empty(&ctx->cq_overflow_list)) {
1322 set_bit(0, &ctx->sq_check_overflow);
1323 set_bit(0, &ctx->cq_check_overflow);
1324 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001325 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001326 refcount_inc(&req->refs);
1327 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001328 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001329 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330 }
1331}
1332
Jens Axboebcda7ba2020-02-23 16:42:51 -07001333static void io_cqring_fill_event(struct io_kiocb *req, long res)
1334{
1335 __io_cqring_fill_event(req, res, 0);
1336}
1337
1338static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001340 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001341 unsigned long flags;
1342
1343 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001344 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001345 io_commit_cqring(ctx);
1346 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1347
Jens Axboe8c838782019-03-12 15:48:16 -06001348 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001349}
1350
Jens Axboebcda7ba2020-02-23 16:42:51 -07001351static void io_cqring_add_event(struct io_kiocb *req, long res)
1352{
1353 __io_cqring_add_event(req, res, 0);
1354}
1355
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001356static inline bool io_is_fallback_req(struct io_kiocb *req)
1357{
1358 return req == (struct io_kiocb *)
1359 ((unsigned long) req->ctx->fallback_req & ~1UL);
1360}
1361
1362static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1363{
1364 struct io_kiocb *req;
1365
1366 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001367 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001368 return req;
1369
1370 return NULL;
1371}
1372
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001373static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1374 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001375{
Jens Axboefd6fab22019-03-14 16:30:06 -06001376 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001377 struct io_kiocb *req;
1378
Jens Axboe2579f912019-01-09 09:10:43 -07001379 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001380 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001381 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001382 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001383 } else if (!state->free_reqs) {
1384 size_t sz;
1385 int ret;
1386
1387 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001388 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1389
1390 /*
1391 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1392 * retry single alloc to be on the safe side.
1393 */
1394 if (unlikely(ret <= 0)) {
1395 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1396 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001397 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001398 ret = 1;
1399 }
Jens Axboe2579f912019-01-09 09:10:43 -07001400 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001401 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001402 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001403 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001404 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001405 }
1406
Jens Axboe2579f912019-01-09 09:10:43 -07001407 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001408fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001409 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001410}
1411
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001412static inline void io_put_file(struct io_kiocb *req, struct file *file,
1413 bool fixed)
1414{
1415 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001416 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001417 else
1418 fput(file);
1419}
1420
Jens Axboec6ca97b302019-12-28 12:11:08 -07001421static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001423 if (req->flags & REQ_F_NEED_CLEANUP)
1424 io_cleanup_req(req);
1425
YueHaibing96fd84d2020-01-07 22:22:44 +08001426 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001427 if (req->file)
1428 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov4dd28242020-06-15 10:33:13 +03001429 __io_put_req_task(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001430 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001431}
1432
1433static void __io_free_req(struct io_kiocb *req)
1434{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001435 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001436
Jens Axboefcb323c2019-10-24 12:39:47 -06001437 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001438 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001439 unsigned long flags;
1440
1441 spin_lock_irqsave(&ctx->inflight_lock, flags);
1442 list_del(&req->inflight_entry);
1443 if (waitqueue_active(&ctx->inflight_wait))
1444 wake_up(&ctx->inflight_wait);
1445 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1446 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001447
1448 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001449 if (likely(!io_is_fallback_req(req)))
1450 kmem_cache_free(req_cachep, req);
1451 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001452 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001453}
1454
Jens Axboec6ca97b302019-12-28 12:11:08 -07001455struct req_batch {
1456 void *reqs[IO_IOPOLL_BATCH];
1457 int to_free;
1458 int need_iter;
1459};
1460
1461static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1462{
1463 if (!rb->to_free)
1464 return;
1465 if (rb->need_iter) {
1466 int i, inflight = 0;
1467 unsigned long flags;
1468
1469 for (i = 0; i < rb->to_free; i++) {
1470 struct io_kiocb *req = rb->reqs[i];
1471
Jens Axboec6ca97b302019-12-28 12:11:08 -07001472 if (req->flags & REQ_F_INFLIGHT)
1473 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001474 __io_req_aux_free(req);
1475 }
1476 if (!inflight)
1477 goto do_free;
1478
1479 spin_lock_irqsave(&ctx->inflight_lock, flags);
1480 for (i = 0; i < rb->to_free; i++) {
1481 struct io_kiocb *req = rb->reqs[i];
1482
Jens Axboe10fef4b2020-01-09 07:52:28 -07001483 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001484 list_del(&req->inflight_entry);
1485 if (!--inflight)
1486 break;
1487 }
1488 }
1489 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1490
1491 if (waitqueue_active(&ctx->inflight_wait))
1492 wake_up(&ctx->inflight_wait);
1493 }
1494do_free:
1495 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1496 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001497 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001498}
1499
Jackie Liua197f662019-11-08 08:09:12 -07001500static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001501{
Jackie Liua197f662019-11-08 08:09:12 -07001502 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001503 int ret;
1504
Jens Axboe2d283902019-12-04 11:08:05 -07001505 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001506 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001507 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001508 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001509 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001510 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001511 return true;
1512 }
1513
1514 return false;
1515}
1516
Jens Axboeba816ad2019-09-28 11:36:45 -06001517static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001518{
Jens Axboe2665abf2019-11-05 12:40:47 -07001519 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001520 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001521
Jens Axboe4d7dd462019-11-20 13:03:52 -07001522 /* Already got next link */
1523 if (req->flags & REQ_F_LINK_NEXT)
1524 return;
1525
Jens Axboe9e645e112019-05-10 16:07:28 -06001526 /*
1527 * The list should never be empty when we are called here. But could
1528 * potentially happen if the chain is messed up, check to be on the
1529 * safe side.
1530 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001531 while (!list_empty(&req->link_list)) {
1532 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1533 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001534
Pavel Begunkov44932332019-12-05 16:16:35 +03001535 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1536 (nxt->flags & REQ_F_TIMEOUT))) {
1537 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001538 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001539 req->flags &= ~REQ_F_LINK_TIMEOUT;
1540 continue;
1541 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001542
Pavel Begunkov44932332019-12-05 16:16:35 +03001543 list_del_init(&req->link_list);
1544 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001545 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001546 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001547 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001548 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001549
Jens Axboe4d7dd462019-11-20 13:03:52 -07001550 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001551 if (wake_ev)
1552 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001553}
1554
1555/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001556 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001557 */
1558static void io_fail_links(struct io_kiocb *req)
1559{
Jens Axboe2665abf2019-11-05 12:40:47 -07001560 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001561 unsigned long flags;
1562
1563 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001564
1565 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001566 struct io_kiocb *link = list_first_entry(&req->link_list,
1567 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001568
Pavel Begunkov44932332019-12-05 16:16:35 +03001569 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001570 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001571
1572 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001573 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001574 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001575 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001576 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001577 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001578 }
Jens Axboe5d960722019-11-19 15:31:28 -07001579 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001580 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001581
1582 io_commit_cqring(ctx);
1583 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1584 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001585}
1586
Jens Axboe4d7dd462019-11-20 13:03:52 -07001587static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001588{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001589 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001590 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001591
Jens Axboe9e645e112019-05-10 16:07:28 -06001592 /*
1593 * If LINK is set, we have dependent requests in this chain. If we
1594 * didn't fail this request, queue the first one up, moving any other
1595 * dependencies to the next request. In case of failure, fail the rest
1596 * of the chain.
1597 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001598 if (req->flags & REQ_F_FAIL_LINK) {
1599 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001600 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1601 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001602 struct io_ring_ctx *ctx = req->ctx;
1603 unsigned long flags;
1604
1605 /*
1606 * If this is a timeout link, we could be racing with the
1607 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001608 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001609 */
1610 spin_lock_irqsave(&ctx->completion_lock, flags);
1611 io_req_link_next(req, nxt);
1612 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1613 } else {
1614 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001615 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001616}
Jens Axboe9e645e112019-05-10 16:07:28 -06001617
Jackie Liuc69f8db2019-11-09 11:00:08 +08001618static void io_free_req(struct io_kiocb *req)
1619{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001620 struct io_kiocb *nxt = NULL;
1621
1622 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001623 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001624
1625 if (nxt)
1626 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001627}
1628
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001629static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1630{
1631 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001632 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1633
1634 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1635 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001636
1637 *workptr = &nxt->work;
1638 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001639 if (link)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03001640 nxt->flags |= REQ_F_QUEUE_TIMEOUT;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001641}
1642
Jens Axboeba816ad2019-09-28 11:36:45 -06001643/*
1644 * Drop reference to request, return next in chain (if there is one) if this
1645 * was the last reference to this request.
1646 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001647__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001648static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001649{
Jens Axboe2a44f462020-02-25 13:25:41 -07001650 if (refcount_dec_and_test(&req->refs)) {
1651 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001652 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001653 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001654}
1655
Jens Axboe2b188cc2019-01-07 10:46:33 -07001656static void io_put_req(struct io_kiocb *req)
1657{
Jens Axboedef596e2019-01-09 08:59:42 -07001658 if (refcount_dec_and_test(&req->refs))
1659 io_free_req(req);
1660}
1661
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001662static void io_steal_work(struct io_kiocb *req,
1663 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001664{
1665 /*
1666 * It's in an io-wq worker, so there always should be at least
1667 * one reference, which will be dropped in io_put_work() just
1668 * after the current handler returns.
1669 *
1670 * It also means, that if the counter dropped to 1, then there is
1671 * no asynchronous users left, so it's safe to steal the next work.
1672 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001673 if (refcount_read(&req->refs) == 1) {
1674 struct io_kiocb *nxt = NULL;
1675
1676 io_req_find_next(req, &nxt);
1677 if (nxt)
1678 io_wq_assign_next(workptr, nxt);
1679 }
1680}
1681
Jens Axboe978db572019-11-14 22:39:04 -07001682/*
1683 * Must only be used if we don't need to care about links, usually from
1684 * within the completion handling itself.
1685 */
1686static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001687{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001688 /* drop both submit and complete references */
1689 if (refcount_sub_and_test(2, &req->refs))
1690 __io_free_req(req);
1691}
1692
Jens Axboe978db572019-11-14 22:39:04 -07001693static void io_double_put_req(struct io_kiocb *req)
1694{
1695 /* drop both submit and complete references */
1696 if (refcount_sub_and_test(2, &req->refs))
1697 io_free_req(req);
1698}
1699
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001700static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001701{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001702 struct io_rings *rings = ctx->rings;
1703
Jens Axboead3eb2c2019-12-18 17:12:20 -07001704 if (test_bit(0, &ctx->cq_check_overflow)) {
1705 /*
1706 * noflush == true is from the waitqueue handler, just ensure
1707 * we wake up the task, and the next invocation will flush the
1708 * entries. We cannot safely to it from here.
1709 */
1710 if (noflush && !list_empty(&ctx->cq_overflow_list))
1711 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001712
Jens Axboead3eb2c2019-12-18 17:12:20 -07001713 io_cqring_overflow_flush(ctx, false);
1714 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001715
Jens Axboea3a0e432019-08-20 11:03:11 -06001716 /* See comment at the top of this file */
1717 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001718 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001719}
1720
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001721static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1722{
1723 struct io_rings *rings = ctx->rings;
1724
1725 /* make sure SQ entry isn't read before tail */
1726 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1727}
1728
Jens Axboe8237e042019-12-28 10:48:22 -07001729static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001730{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001731 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001732 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001733
Jens Axboe9d9e88a2020-05-13 12:53:19 -06001734 if (req->file || req->io)
Jens Axboec6ca97b302019-12-28 12:11:08 -07001735 rb->need_iter++;
1736
1737 rb->reqs[rb->to_free++] = req;
1738 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1739 io_free_req_many(req->ctx, rb);
1740 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001741}
1742
Jens Axboebcda7ba2020-02-23 16:42:51 -07001743static int io_put_kbuf(struct io_kiocb *req)
1744{
Jens Axboe4d954c22020-02-27 07:31:19 -07001745 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001746 int cflags;
1747
Jens Axboe4d954c22020-02-27 07:31:19 -07001748 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001749 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1750 cflags |= IORING_CQE_F_BUFFER;
1751 req->rw.addr = 0;
1752 kfree(kbuf);
1753 return cflags;
1754}
1755
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001756static void io_iopoll_queue(struct list_head *again)
1757{
1758 struct io_kiocb *req;
1759
1760 do {
1761 req = list_first_entry(again, struct io_kiocb, list);
1762 list_del(&req->list);
1763 refcount_inc(&req->refs);
1764 io_queue_async_work(req);
1765 } while (!list_empty(again));
1766}
1767
Jens Axboedef596e2019-01-09 08:59:42 -07001768/*
1769 * Find and free completed poll iocbs
1770 */
1771static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1772 struct list_head *done)
1773{
Jens Axboe8237e042019-12-28 10:48:22 -07001774 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001775 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001776 LIST_HEAD(again);
1777
1778 /* order with ->result store in io_complete_rw_iopoll() */
1779 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07001780
Jens Axboec6ca97b302019-12-28 12:11:08 -07001781 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001782 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001783 int cflags = 0;
1784
Jens Axboedef596e2019-01-09 08:59:42 -07001785 req = list_first_entry(done, struct io_kiocb, list);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001786 if (READ_ONCE(req->result) == -EAGAIN) {
1787 req->iopoll_completed = 0;
1788 list_move_tail(&req->list, &again);
1789 continue;
1790 }
Jens Axboedef596e2019-01-09 08:59:42 -07001791 list_del(&req->list);
1792
Jens Axboebcda7ba2020-02-23 16:42:51 -07001793 if (req->flags & REQ_F_BUFFER_SELECTED)
1794 cflags = io_put_kbuf(req);
1795
1796 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001797 (*nr_events)++;
1798
Jens Axboe8237e042019-12-28 10:48:22 -07001799 if (refcount_dec_and_test(&req->refs) &&
1800 !io_req_multi_free(&rb, req))
1801 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001802 }
Jens Axboedef596e2019-01-09 08:59:42 -07001803
Jens Axboe09bb8392019-03-13 12:39:28 -06001804 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001805 if (ctx->flags & IORING_SETUP_SQPOLL)
1806 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001807 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001808
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001809 if (!list_empty(&again))
1810 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001811}
1812
Jens Axboedef596e2019-01-09 08:59:42 -07001813static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1814 long min)
1815{
1816 struct io_kiocb *req, *tmp;
1817 LIST_HEAD(done);
1818 bool spin;
1819 int ret;
1820
1821 /*
1822 * Only spin for completions if we don't have multiple devices hanging
1823 * off our complete list, and we're under the requested amount.
1824 */
1825 spin = !ctx->poll_multi_file && *nr_events < min;
1826
1827 ret = 0;
1828 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001829 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001830
1831 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001832 * Move completed and retryable entries to our local lists.
1833 * If we find a request that requires polling, break out
1834 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001835 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08001836 if (READ_ONCE(req->iopoll_completed)) {
Jens Axboedef596e2019-01-09 08:59:42 -07001837 list_move_tail(&req->list, &done);
1838 continue;
1839 }
1840 if (!list_empty(&done))
1841 break;
1842
1843 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1844 if (ret < 0)
1845 break;
1846
1847 if (ret && spin)
1848 spin = false;
1849 ret = 0;
1850 }
1851
1852 if (!list_empty(&done))
1853 io_iopoll_complete(ctx, nr_events, &done);
1854
1855 return ret;
1856}
1857
1858/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001859 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001860 * non-spinning poll check - we'll still enter the driver poll loop, but only
1861 * as a non-spinning completion check.
1862 */
1863static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1864 long min)
1865{
Jens Axboe08f54392019-08-21 22:19:11 -06001866 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001867 int ret;
1868
1869 ret = io_do_iopoll(ctx, nr_events, min);
1870 if (ret < 0)
1871 return ret;
1872 if (!min || *nr_events >= min)
1873 return 0;
1874 }
1875
1876 return 1;
1877}
1878
1879/*
1880 * We can't just wait for polled events to come to us, we have to actively
1881 * find and complete them.
1882 */
1883static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1884{
1885 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1886 return;
1887
1888 mutex_lock(&ctx->uring_lock);
1889 while (!list_empty(&ctx->poll_list)) {
1890 unsigned int nr_events = 0;
1891
1892 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001893
1894 /*
1895 * Ensure we allow local-to-the-cpu processing to take place,
1896 * in this case we need to ensure that we reap all events.
1897 */
1898 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001899 }
1900 mutex_unlock(&ctx->uring_lock);
1901}
1902
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001903static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1904 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001905{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001906 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001907
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001908 /*
1909 * We disallow the app entering submit/complete with polling, but we
1910 * still need to lock the ring to prevent racing with polled issue
1911 * that got punted to a workqueue.
1912 */
1913 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001914 do {
1915 int tmin = 0;
1916
Jens Axboe500f9fb2019-08-19 12:15:59 -06001917 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001918 * Don't enter poll loop if we already have events pending.
1919 * If we do, we can potentially be spinning for commands that
1920 * already triggered a CQE (eg in error).
1921 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001922 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001923 break;
1924
1925 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001926 * If a submit got punted to a workqueue, we can have the
1927 * application entering polling for a command before it gets
1928 * issued. That app will hold the uring_lock for the duration
1929 * of the poll right here, so we need to take a breather every
1930 * now and then to ensure that the issue has a chance to add
1931 * the poll to the issued list. Otherwise we can spin here
1932 * forever, while the workqueue is stuck trying to acquire the
1933 * very same mutex.
1934 */
1935 if (!(++iters & 7)) {
1936 mutex_unlock(&ctx->uring_lock);
1937 mutex_lock(&ctx->uring_lock);
1938 }
1939
Jens Axboedef596e2019-01-09 08:59:42 -07001940 if (*nr_events < min)
1941 tmin = min - *nr_events;
1942
1943 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1944 if (ret <= 0)
1945 break;
1946 ret = 0;
1947 } while (min && !*nr_events && !need_resched());
1948
Jens Axboe500f9fb2019-08-19 12:15:59 -06001949 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001950 return ret;
1951}
1952
Jens Axboe491381ce2019-10-17 09:20:46 -06001953static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001954{
Jens Axboe491381ce2019-10-17 09:20:46 -06001955 /*
1956 * Tell lockdep we inherited freeze protection from submission
1957 * thread.
1958 */
1959 if (req->flags & REQ_F_ISREG) {
1960 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001961
Jens Axboe491381ce2019-10-17 09:20:46 -06001962 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001963 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001964 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001965}
1966
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001967static inline void req_set_fail_links(struct io_kiocb *req)
1968{
1969 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1970 req->flags |= REQ_F_FAIL_LINK;
1971}
1972
Jens Axboeba816ad2019-09-28 11:36:45 -06001973static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001974{
Jens Axboe9adbd452019-12-20 08:45:55 -07001975 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001976 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001977
Jens Axboe491381ce2019-10-17 09:20:46 -06001978 if (kiocb->ki_flags & IOCB_WRITE)
1979 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001980
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001981 if (res != req->result)
1982 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001983 if (req->flags & REQ_F_BUFFER_SELECTED)
1984 cflags = io_put_kbuf(req);
1985 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001986}
1987
Jens Axboeb63534c2020-06-04 11:28:00 -06001988static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
1989{
1990 struct mm_struct *mm = current->mm;
1991
1992 if (mm) {
1993 kthread_unuse_mm(mm);
1994 mmput(mm);
1995 }
1996}
1997
1998static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
1999 struct io_kiocb *req)
2000{
2001 if (io_op_defs[req->opcode].needs_mm && !current->mm) {
2002 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
2003 return -EFAULT;
2004 kthread_use_mm(ctx->sqo_mm);
2005 }
2006
2007 return 0;
2008}
2009
2010#ifdef CONFIG_BLOCK
2011static bool io_resubmit_prep(struct io_kiocb *req, int error)
2012{
2013 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2014 ssize_t ret = -ECANCELED;
2015 struct iov_iter iter;
2016 int rw;
2017
2018 if (error) {
2019 ret = error;
2020 goto end_req;
2021 }
2022
2023 switch (req->opcode) {
2024 case IORING_OP_READV:
2025 case IORING_OP_READ_FIXED:
2026 case IORING_OP_READ:
2027 rw = READ;
2028 break;
2029 case IORING_OP_WRITEV:
2030 case IORING_OP_WRITE_FIXED:
2031 case IORING_OP_WRITE:
2032 rw = WRITE;
2033 break;
2034 default:
2035 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2036 req->opcode);
2037 goto end_req;
2038 }
2039
2040 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2041 if (ret < 0)
2042 goto end_req;
2043 ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2044 if (!ret)
2045 return true;
2046 kfree(iovec);
2047end_req:
2048 io_cqring_add_event(req, ret);
2049 req_set_fail_links(req);
2050 io_put_req(req);
2051 return false;
2052}
2053
2054static void io_rw_resubmit(struct callback_head *cb)
2055{
2056 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2057 struct io_ring_ctx *ctx = req->ctx;
2058 int err;
2059
2060 __set_current_state(TASK_RUNNING);
2061
2062 err = io_sq_thread_acquire_mm(ctx, req);
2063
2064 if (io_resubmit_prep(req, err)) {
2065 refcount_inc(&req->refs);
2066 io_queue_async_work(req);
2067 }
2068}
2069#endif
2070
2071static bool io_rw_reissue(struct io_kiocb *req, long res)
2072{
2073#ifdef CONFIG_BLOCK
2074 struct task_struct *tsk;
2075 int ret;
2076
2077 if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2078 return false;
2079
2080 tsk = req->task;
2081 init_task_work(&req->task_work, io_rw_resubmit);
2082 ret = task_work_add(tsk, &req->task_work, true);
2083 if (!ret)
2084 return true;
2085#endif
2086 return false;
2087}
2088
Jens Axboeba816ad2019-09-28 11:36:45 -06002089static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2090{
Jens Axboe9adbd452019-12-20 08:45:55 -07002091 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002092
Jens Axboeb63534c2020-06-04 11:28:00 -06002093 if (!io_rw_reissue(req, res)) {
2094 io_complete_rw_common(kiocb, res);
2095 io_put_req(req);
2096 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002097}
2098
Jens Axboedef596e2019-01-09 08:59:42 -07002099static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2100{
Jens Axboe9adbd452019-12-20 08:45:55 -07002101 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002102
Jens Axboe491381ce2019-10-17 09:20:46 -06002103 if (kiocb->ki_flags & IOCB_WRITE)
2104 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002105
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002106 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002107 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002108
2109 WRITE_ONCE(req->result, res);
2110 /* order with io_poll_complete() checking ->result */
2111 if (res != -EAGAIN) {
2112 smp_wmb();
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002113 WRITE_ONCE(req->iopoll_completed, 1);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002114 }
Jens Axboedef596e2019-01-09 08:59:42 -07002115}
2116
2117/*
2118 * After the iocb has been issued, it's safe to be found on the poll list.
2119 * Adding the kiocb to the list AFTER submission ensures that we don't
2120 * find it from a io_iopoll_getevents() thread before the issuer is done
2121 * accessing the kiocb cookie.
2122 */
2123static void io_iopoll_req_issued(struct io_kiocb *req)
2124{
2125 struct io_ring_ctx *ctx = req->ctx;
2126
2127 /*
2128 * Track whether we have multiple files in our lists. This will impact
2129 * how we do polling eventually, not spinning if we're on potentially
2130 * different devices.
2131 */
2132 if (list_empty(&ctx->poll_list)) {
2133 ctx->poll_multi_file = false;
2134 } else if (!ctx->poll_multi_file) {
2135 struct io_kiocb *list_req;
2136
2137 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
2138 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002139 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002140 ctx->poll_multi_file = true;
2141 }
2142
2143 /*
2144 * For fast devices, IO may have already completed. If it has, add
2145 * it to the front so we find it first.
2146 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002147 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002148 list_add(&req->list, &ctx->poll_list);
2149 else
2150 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002151
2152 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2153 wq_has_sleeper(&ctx->sqo_wait))
2154 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002155}
2156
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002157static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002158{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002159 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002160
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002161 if (diff)
2162 fput_many(state->file, diff);
2163 state->file = NULL;
2164}
2165
2166static inline void io_state_file_put(struct io_submit_state *state)
2167{
2168 if (state->file)
2169 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002170}
2171
2172/*
2173 * Get as many references to a file as we have IOs left in this submission,
2174 * assuming most submissions are for one file, or at least that each file
2175 * has more than one submission.
2176 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002177static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002178{
2179 if (!state)
2180 return fget(fd);
2181
2182 if (state->file) {
2183 if (state->fd == fd) {
2184 state->used_refs++;
2185 state->ios_left--;
2186 return state->file;
2187 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002188 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002189 }
2190 state->file = fget_many(fd, state->ios_left);
2191 if (!state->file)
2192 return NULL;
2193
2194 state->fd = fd;
2195 state->has_refs = state->ios_left;
2196 state->used_refs = 1;
2197 state->ios_left--;
2198 return state->file;
2199}
2200
Jens Axboe4503b762020-06-01 10:00:27 -06002201static bool io_bdev_nowait(struct block_device *bdev)
2202{
2203#ifdef CONFIG_BLOCK
2204 return !bdev || queue_is_mq(bdev_get_queue(bdev));
2205#else
2206 return true;
2207#endif
2208}
2209
Jens Axboe2b188cc2019-01-07 10:46:33 -07002210/*
2211 * If we tracked the file through the SCM inflight mechanism, we could support
2212 * any file. For now, just ensure that anything potentially problematic is done
2213 * inline.
2214 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002215static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002216{
2217 umode_t mode = file_inode(file)->i_mode;
2218
Jens Axboe4503b762020-06-01 10:00:27 -06002219 if (S_ISBLK(mode)) {
2220 if (io_bdev_nowait(file->f_inode->i_bdev))
2221 return true;
2222 return false;
2223 }
2224 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002225 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002226 if (S_ISREG(mode)) {
2227 if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2228 file->f_op != &io_uring_fops)
2229 return true;
2230 return false;
2231 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002232
Jens Axboec5b85622020-06-09 19:23:05 -06002233 /* any ->read/write should understand O_NONBLOCK */
2234 if (file->f_flags & O_NONBLOCK)
2235 return true;
2236
Jens Axboeaf197f52020-04-28 13:15:06 -06002237 if (!(file->f_mode & FMODE_NOWAIT))
2238 return false;
2239
2240 if (rw == READ)
2241 return file->f_op->read_iter != NULL;
2242
2243 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002244}
2245
Jens Axboe3529d8c2019-12-19 18:24:38 -07002246static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2247 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002248{
Jens Axboedef596e2019-01-09 08:59:42 -07002249 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002250 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002251 unsigned ioprio;
2252 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002253
Jens Axboe491381ce2019-10-17 09:20:46 -06002254 if (S_ISREG(file_inode(req->file)->i_mode))
2255 req->flags |= REQ_F_ISREG;
2256
Jens Axboe2b188cc2019-01-07 10:46:33 -07002257 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002258 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2259 req->flags |= REQ_F_CUR_POS;
2260 kiocb->ki_pos = req->file->f_pos;
2261 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002262 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002263 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2264 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2265 if (unlikely(ret))
2266 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002267
2268 ioprio = READ_ONCE(sqe->ioprio);
2269 if (ioprio) {
2270 ret = ioprio_check_cap(ioprio);
2271 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002272 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002273
2274 kiocb->ki_ioprio = ioprio;
2275 } else
2276 kiocb->ki_ioprio = get_current_ioprio();
2277
Stefan Bühler8449eed2019-04-27 20:34:19 +02002278 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002279 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002280 req->flags |= REQ_F_NOWAIT;
2281
Jens Axboeb63534c2020-06-04 11:28:00 -06002282 if (kiocb->ki_flags & IOCB_DIRECT)
2283 io_get_req_task(req);
2284
Stefan Bühler8449eed2019-04-27 20:34:19 +02002285 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002286 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002287
Jens Axboedef596e2019-01-09 08:59:42 -07002288 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002289 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2290 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002291 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002292
Jens Axboedef596e2019-01-09 08:59:42 -07002293 kiocb->ki_flags |= IOCB_HIPRI;
2294 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002295 req->result = 0;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002296 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002297 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002298 if (kiocb->ki_flags & IOCB_HIPRI)
2299 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002300 kiocb->ki_complete = io_complete_rw;
2301 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002302
Jens Axboe3529d8c2019-12-19 18:24:38 -07002303 req->rw.addr = READ_ONCE(sqe->addr);
2304 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002305 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002306 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307}
2308
2309static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2310{
2311 switch (ret) {
2312 case -EIOCBQUEUED:
2313 break;
2314 case -ERESTARTSYS:
2315 case -ERESTARTNOINTR:
2316 case -ERESTARTNOHAND:
2317 case -ERESTART_RESTARTBLOCK:
2318 /*
2319 * We can't just restart the syscall, since previously
2320 * submitted sqes may already be in progress. Just fail this
2321 * IO with EINTR.
2322 */
2323 ret = -EINTR;
2324 /* fall through */
2325 default:
2326 kiocb->ki_complete(kiocb, ret, 0);
2327 }
2328}
2329
Pavel Begunkov014db002020-03-03 21:33:12 +03002330static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002331{
Jens Axboeba042912019-12-25 16:33:42 -07002332 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2333
2334 if (req->flags & REQ_F_CUR_POS)
2335 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002336 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002337 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002338 else
2339 io_rw_done(kiocb, ret);
2340}
2341
Jens Axboe9adbd452019-12-20 08:45:55 -07002342static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002343 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002344{
Jens Axboe9adbd452019-12-20 08:45:55 -07002345 struct io_ring_ctx *ctx = req->ctx;
2346 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002347 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002348 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002349 size_t offset;
2350 u64 buf_addr;
2351
2352 /* attempt to use fixed buffers without having provided iovecs */
2353 if (unlikely(!ctx->user_bufs))
2354 return -EFAULT;
2355
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002356 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002357 if (unlikely(buf_index >= ctx->nr_user_bufs))
2358 return -EFAULT;
2359
2360 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2361 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002362 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002363
2364 /* overflow */
2365 if (buf_addr + len < buf_addr)
2366 return -EFAULT;
2367 /* not inside the mapped region */
2368 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2369 return -EFAULT;
2370
2371 /*
2372 * May not be a start of buffer, set size appropriately
2373 * and advance us to the beginning.
2374 */
2375 offset = buf_addr - imu->ubuf;
2376 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002377
2378 if (offset) {
2379 /*
2380 * Don't use iov_iter_advance() here, as it's really slow for
2381 * using the latter parts of a big fixed buffer - it iterates
2382 * over each segment manually. We can cheat a bit here, because
2383 * we know that:
2384 *
2385 * 1) it's a BVEC iter, we set it up
2386 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2387 * first and last bvec
2388 *
2389 * So just find our index, and adjust the iterator afterwards.
2390 * If the offset is within the first bvec (or the whole first
2391 * bvec, just use iov_iter_advance(). This makes it easier
2392 * since we can just skip the first segment, which may not
2393 * be PAGE_SIZE aligned.
2394 */
2395 const struct bio_vec *bvec = imu->bvec;
2396
2397 if (offset <= bvec->bv_len) {
2398 iov_iter_advance(iter, offset);
2399 } else {
2400 unsigned long seg_skip;
2401
2402 /* skip first vec */
2403 offset -= bvec->bv_len;
2404 seg_skip = 1 + (offset >> PAGE_SHIFT);
2405
2406 iter->bvec = bvec + seg_skip;
2407 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002408 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002409 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002410 }
2411 }
2412
Jens Axboe5e559562019-11-13 16:12:46 -07002413 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002414}
2415
Jens Axboebcda7ba2020-02-23 16:42:51 -07002416static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2417{
2418 if (needs_lock)
2419 mutex_unlock(&ctx->uring_lock);
2420}
2421
2422static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2423{
2424 /*
2425 * "Normal" inline submissions always hold the uring_lock, since we
2426 * grab it from the system call. Same is true for the SQPOLL offload.
2427 * The only exception is when we've detached the request and issue it
2428 * from an async worker thread, grab the lock for that case.
2429 */
2430 if (needs_lock)
2431 mutex_lock(&ctx->uring_lock);
2432}
2433
2434static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2435 int bgid, struct io_buffer *kbuf,
2436 bool needs_lock)
2437{
2438 struct io_buffer *head;
2439
2440 if (req->flags & REQ_F_BUFFER_SELECTED)
2441 return kbuf;
2442
2443 io_ring_submit_lock(req->ctx, needs_lock);
2444
2445 lockdep_assert_held(&req->ctx->uring_lock);
2446
2447 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2448 if (head) {
2449 if (!list_empty(&head->list)) {
2450 kbuf = list_last_entry(&head->list, struct io_buffer,
2451 list);
2452 list_del(&kbuf->list);
2453 } else {
2454 kbuf = head;
2455 idr_remove(&req->ctx->io_buffer_idr, bgid);
2456 }
2457 if (*len > kbuf->len)
2458 *len = kbuf->len;
2459 } else {
2460 kbuf = ERR_PTR(-ENOBUFS);
2461 }
2462
2463 io_ring_submit_unlock(req->ctx, needs_lock);
2464
2465 return kbuf;
2466}
2467
Jens Axboe4d954c22020-02-27 07:31:19 -07002468static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2469 bool needs_lock)
2470{
2471 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002472 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002473
2474 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002475 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002476 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2477 if (IS_ERR(kbuf))
2478 return kbuf;
2479 req->rw.addr = (u64) (unsigned long) kbuf;
2480 req->flags |= REQ_F_BUFFER_SELECTED;
2481 return u64_to_user_ptr(kbuf->addr);
2482}
2483
2484#ifdef CONFIG_COMPAT
2485static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2486 bool needs_lock)
2487{
2488 struct compat_iovec __user *uiov;
2489 compat_ssize_t clen;
2490 void __user *buf;
2491 ssize_t len;
2492
2493 uiov = u64_to_user_ptr(req->rw.addr);
2494 if (!access_ok(uiov, sizeof(*uiov)))
2495 return -EFAULT;
2496 if (__get_user(clen, &uiov->iov_len))
2497 return -EFAULT;
2498 if (clen < 0)
2499 return -EINVAL;
2500
2501 len = clen;
2502 buf = io_rw_buffer_select(req, &len, needs_lock);
2503 if (IS_ERR(buf))
2504 return PTR_ERR(buf);
2505 iov[0].iov_base = buf;
2506 iov[0].iov_len = (compat_size_t) len;
2507 return 0;
2508}
2509#endif
2510
2511static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2512 bool needs_lock)
2513{
2514 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2515 void __user *buf;
2516 ssize_t len;
2517
2518 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2519 return -EFAULT;
2520
2521 len = iov[0].iov_len;
2522 if (len < 0)
2523 return -EINVAL;
2524 buf = io_rw_buffer_select(req, &len, needs_lock);
2525 if (IS_ERR(buf))
2526 return PTR_ERR(buf);
2527 iov[0].iov_base = buf;
2528 iov[0].iov_len = len;
2529 return 0;
2530}
2531
2532static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2533 bool needs_lock)
2534{
Jens Axboedddb3e22020-06-04 11:27:01 -06002535 if (req->flags & REQ_F_BUFFER_SELECTED) {
2536 struct io_buffer *kbuf;
2537
2538 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2539 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2540 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002541 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002542 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002543 if (!req->rw.len)
2544 return 0;
2545 else if (req->rw.len > 1)
2546 return -EINVAL;
2547
2548#ifdef CONFIG_COMPAT
2549 if (req->ctx->compat)
2550 return io_compat_import(req, iov, needs_lock);
2551#endif
2552
2553 return __io_iov_buffer_select(req, iov, needs_lock);
2554}
2555
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002556static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002557 struct iovec **iovec, struct iov_iter *iter,
2558 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002559{
Jens Axboe9adbd452019-12-20 08:45:55 -07002560 void __user *buf = u64_to_user_ptr(req->rw.addr);
2561 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002562 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002563 u8 opcode;
2564
Jens Axboed625c6e2019-12-17 19:53:05 -07002565 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002566 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002567 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002568 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002569 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002570
Jens Axboebcda7ba2020-02-23 16:42:51 -07002571 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002572 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002573 return -EINVAL;
2574
Jens Axboe3a6820f2019-12-22 15:19:35 -07002575 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002576 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002577 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2578 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002579 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002580 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002581 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002582 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002583 }
2584
Jens Axboe3a6820f2019-12-22 15:19:35 -07002585 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2586 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002587 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002588 }
2589
Jens Axboef67676d2019-12-02 11:03:47 -07002590 if (req->io) {
2591 struct io_async_rw *iorw = &req->io->rw;
2592
2593 *iovec = iorw->iov;
2594 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2595 if (iorw->iov == iorw->fast_iov)
2596 *iovec = NULL;
2597 return iorw->size;
2598 }
2599
Jens Axboe4d954c22020-02-27 07:31:19 -07002600 if (req->flags & REQ_F_BUFFER_SELECT) {
2601 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002602 if (!ret) {
2603 ret = (*iovec)->iov_len;
2604 iov_iter_init(iter, rw, *iovec, 1, ret);
2605 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002606 *iovec = NULL;
2607 return ret;
2608 }
2609
Jens Axboe2b188cc2019-01-07 10:46:33 -07002610#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002611 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002612 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2613 iovec, iter);
2614#endif
2615
2616 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2617}
2618
Jens Axboe32960612019-09-23 11:05:34 -06002619/*
2620 * For files that don't have ->read_iter() and ->write_iter(), handle them
2621 * by looping over ->read() or ->write() manually.
2622 */
2623static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2624 struct iov_iter *iter)
2625{
2626 ssize_t ret = 0;
2627
2628 /*
2629 * Don't support polled IO through this interface, and we can't
2630 * support non-blocking either. For the latter, this just causes
2631 * the kiocb to be handled from an async context.
2632 */
2633 if (kiocb->ki_flags & IOCB_HIPRI)
2634 return -EOPNOTSUPP;
2635 if (kiocb->ki_flags & IOCB_NOWAIT)
2636 return -EAGAIN;
2637
2638 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002639 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002640 ssize_t nr;
2641
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002642 if (!iov_iter_is_bvec(iter)) {
2643 iovec = iov_iter_iovec(iter);
2644 } else {
2645 /* fixed buffers import bvec */
2646 iovec.iov_base = kmap(iter->bvec->bv_page)
2647 + iter->iov_offset;
2648 iovec.iov_len = min(iter->count,
2649 iter->bvec->bv_len - iter->iov_offset);
2650 }
2651
Jens Axboe32960612019-09-23 11:05:34 -06002652 if (rw == READ) {
2653 nr = file->f_op->read(file, iovec.iov_base,
2654 iovec.iov_len, &kiocb->ki_pos);
2655 } else {
2656 nr = file->f_op->write(file, iovec.iov_base,
2657 iovec.iov_len, &kiocb->ki_pos);
2658 }
2659
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002660 if (iov_iter_is_bvec(iter))
2661 kunmap(iter->bvec->bv_page);
2662
Jens Axboe32960612019-09-23 11:05:34 -06002663 if (nr < 0) {
2664 if (!ret)
2665 ret = nr;
2666 break;
2667 }
2668 ret += nr;
2669 if (nr != iovec.iov_len)
2670 break;
2671 iov_iter_advance(iter, nr);
2672 }
2673
2674 return ret;
2675}
2676
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002677static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002678 struct iovec *iovec, struct iovec *fast_iov,
2679 struct iov_iter *iter)
2680{
2681 req->io->rw.nr_segs = iter->nr_segs;
2682 req->io->rw.size = io_size;
2683 req->io->rw.iov = iovec;
2684 if (!req->io->rw.iov) {
2685 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002686 if (req->io->rw.iov != fast_iov)
2687 memcpy(req->io->rw.iov, fast_iov,
2688 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002689 } else {
2690 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002691 }
2692}
2693
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002694static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2695{
2696 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2697 return req->io == NULL;
2698}
2699
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002700static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002701{
Jens Axboed3656342019-12-18 09:50:26 -07002702 if (!io_op_defs[req->opcode].async_ctx)
2703 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002704
2705 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002706}
2707
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002708static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2709 struct iovec *iovec, struct iovec *fast_iov,
2710 struct iov_iter *iter)
2711{
Jens Axboe980ad262020-01-24 23:08:54 -07002712 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002713 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002714 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002715 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002716 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002717
Jens Axboe5d204bc2020-01-31 12:06:52 -07002718 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2719 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002720 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002721}
2722
Jens Axboe3529d8c2019-12-19 18:24:38 -07002723static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2724 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002725{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002726 struct io_async_ctx *io;
2727 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002728 ssize_t ret;
2729
Jens Axboe3529d8c2019-12-19 18:24:38 -07002730 ret = io_prep_rw(req, sqe, force_nonblock);
2731 if (ret)
2732 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002733
Jens Axboe3529d8c2019-12-19 18:24:38 -07002734 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2735 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002736
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002737 /* either don't need iovec imported or already have it */
2738 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002739 return 0;
2740
2741 io = req->io;
2742 io->rw.iov = io->rw.fast_iov;
2743 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002744 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002745 req->io = io;
2746 if (ret < 0)
2747 return ret;
2748
2749 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2750 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002751}
2752
Pavel Begunkov014db002020-03-03 21:33:12 +03002753static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002754{
2755 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002756 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002757 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002758 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002759 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002760
Jens Axboebcda7ba2020-02-23 16:42:51 -07002761 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002762 if (ret < 0)
2763 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002764
Jens Axboefd6c2e42019-12-18 12:19:41 -07002765 /* Ensure we clear previously set non-block flag */
2766 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002767 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002768
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002769 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002770 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002771 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002772 req->result = io_size;
2773
2774 /*
2775 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2776 * we know to async punt it even if it was opened O_NONBLOCK
2777 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002778 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002779 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002780
Jens Axboe31b51512019-01-18 22:56:34 -07002781 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002782 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002783 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06002784 unsigned long nr_segs = iter.nr_segs;
Jens Axboe4503b762020-06-01 10:00:27 -06002785 ssize_t ret2 = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002786
Jens Axboe9adbd452019-12-20 08:45:55 -07002787 if (req->file->f_op->read_iter)
2788 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002789 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002790 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002791
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002792 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe4503b762020-06-01 10:00:27 -06002793 if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002794 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002795 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002796 iter.count = iov_count;
2797 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07002798copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002799 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002800 inline_vecs, &iter);
2801 if (ret)
2802 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002803 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002804 if (!(req->flags & REQ_F_NOWAIT) &&
2805 !file_can_poll(req->file))
Jens Axboe29de5f62020-02-20 09:56:08 -07002806 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002807 return -EAGAIN;
2808 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002809 }
Jens Axboef67676d2019-12-02 11:03:47 -07002810out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08002811 if (!(req->flags & REQ_F_NEED_CLEANUP))
2812 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002813 return ret;
2814}
2815
Jens Axboe3529d8c2019-12-19 18:24:38 -07002816static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2817 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002818{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002819 struct io_async_ctx *io;
2820 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002821 ssize_t ret;
2822
Jens Axboe3529d8c2019-12-19 18:24:38 -07002823 ret = io_prep_rw(req, sqe, force_nonblock);
2824 if (ret)
2825 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002826
Jens Axboe3529d8c2019-12-19 18:24:38 -07002827 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2828 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002829
Jens Axboe4ed734b2020-03-20 11:23:41 -06002830 req->fsize = rlimit(RLIMIT_FSIZE);
2831
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002832 /* either don't need iovec imported or already have it */
2833 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002834 return 0;
2835
2836 io = req->io;
2837 io->rw.iov = io->rw.fast_iov;
2838 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002839 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002840 req->io = io;
2841 if (ret < 0)
2842 return ret;
2843
2844 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2845 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002846}
2847
Pavel Begunkov014db002020-03-03 21:33:12 +03002848static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002849{
2850 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002851 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002852 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002853 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002854 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002855
Jens Axboebcda7ba2020-02-23 16:42:51 -07002856 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002857 if (ret < 0)
2858 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002859
Jens Axboefd6c2e42019-12-18 12:19:41 -07002860 /* Ensure we clear previously set non-block flag */
2861 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002862 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002863
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002864 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002865 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002866 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002867 req->result = io_size;
2868
2869 /*
2870 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2871 * we know to async punt it even if it was opened O_NONBLOCK
2872 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002873 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07002874 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002875
Jens Axboe10d59342019-12-09 20:16:22 -07002876 /* file path doesn't support NOWAIT for non-direct_IO */
2877 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2878 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002879 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002880
Jens Axboe31b51512019-01-18 22:56:34 -07002881 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002882 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002883 if (!ret) {
Jens Axboeb63534c2020-06-04 11:28:00 -06002884 unsigned long nr_segs = iter.nr_segs;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002885 ssize_t ret2;
2886
Jens Axboe2b188cc2019-01-07 10:46:33 -07002887 /*
2888 * Open-code file_start_write here to grab freeze protection,
2889 * which will be released by another thread in
2890 * io_complete_rw(). Fool lockdep by telling it the lock got
2891 * released so that it doesn't complain about the held lock when
2892 * we return to userspace.
2893 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002894 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002895 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002896 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002897 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002898 SB_FREEZE_WRITE);
2899 }
2900 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002901
Jens Axboe4ed734b2020-03-20 11:23:41 -06002902 if (!force_nonblock)
2903 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2904
Jens Axboe9adbd452019-12-20 08:45:55 -07002905 if (req->file->f_op->write_iter)
2906 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002907 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002908 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002909
2910 if (!force_nonblock)
2911 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2912
Jens Axboefaac9962020-02-07 15:45:22 -07002913 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002914 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002915 * retry them without IOCB_NOWAIT.
2916 */
2917 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2918 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002919 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002920 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002921 } else {
Jens Axboeb63534c2020-06-04 11:28:00 -06002922 iter.count = iov_count;
2923 iter.nr_segs = nr_segs;
Jens Axboef67676d2019-12-02 11:03:47 -07002924copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002925 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002926 inline_vecs, &iter);
2927 if (ret)
2928 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002929 /* any defer here is final, must blocking retry */
Jens Axboec5b85622020-06-09 19:23:05 -06002930 if (!(req->flags & REQ_F_NOWAIT) &&
2931 !file_can_poll(req->file))
Jens Axboe490e8962020-04-28 13:16:53 -06002932 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002933 return -EAGAIN;
2934 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002935 }
Jens Axboe31b51512019-01-18 22:56:34 -07002936out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08002937 if (!(req->flags & REQ_F_NEED_CLEANUP))
2938 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939 return ret;
2940}
2941
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03002942static int __io_splice_prep(struct io_kiocb *req,
2943 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002944{
2945 struct io_splice* sp = &req->splice;
2946 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2947 int ret;
2948
2949 if (req->flags & REQ_F_NEED_CLEANUP)
2950 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03002951 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2952 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002953
2954 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002955 sp->len = READ_ONCE(sqe->len);
2956 sp->flags = READ_ONCE(sqe->splice_flags);
2957
2958 if (unlikely(sp->flags & ~valid_flags))
2959 return -EINVAL;
2960
2961 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2962 (sp->flags & SPLICE_F_FD_IN_FIXED));
2963 if (ret)
2964 return ret;
2965 req->flags |= REQ_F_NEED_CLEANUP;
2966
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08002967 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
2968 /*
2969 * Splice operation will be punted aync, and here need to
2970 * modify io_wq_work.flags, so initialize io_wq_work firstly.
2971 */
2972 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002973 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08002974 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002975
2976 return 0;
2977}
2978
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03002979static int io_tee_prep(struct io_kiocb *req,
2980 const struct io_uring_sqe *sqe)
2981{
2982 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
2983 return -EINVAL;
2984 return __io_splice_prep(req, sqe);
2985}
2986
2987static int io_tee(struct io_kiocb *req, bool force_nonblock)
2988{
2989 struct io_splice *sp = &req->splice;
2990 struct file *in = sp->file_in;
2991 struct file *out = sp->file_out;
2992 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2993 long ret = 0;
2994
2995 if (force_nonblock)
2996 return -EAGAIN;
2997 if (sp->len)
2998 ret = do_tee(in, out, sp->len, flags);
2999
3000 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3001 req->flags &= ~REQ_F_NEED_CLEANUP;
3002
3003 io_cqring_add_event(req, ret);
3004 if (ret != sp->len)
3005 req_set_fail_links(req);
3006 io_put_req(req);
3007 return 0;
3008}
3009
3010static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3011{
3012 struct io_splice* sp = &req->splice;
3013
3014 sp->off_in = READ_ONCE(sqe->splice_off_in);
3015 sp->off_out = READ_ONCE(sqe->off);
3016 return __io_splice_prep(req, sqe);
3017}
3018
Pavel Begunkov014db002020-03-03 21:33:12 +03003019static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003020{
3021 struct io_splice *sp = &req->splice;
3022 struct file *in = sp->file_in;
3023 struct file *out = sp->file_out;
3024 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3025 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003026 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003027
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003028 if (force_nonblock)
3029 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003030
3031 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3032 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003033
Jens Axboe948a7742020-05-17 14:21:38 -06003034 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003035 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003036
3037 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3038 req->flags &= ~REQ_F_NEED_CLEANUP;
3039
3040 io_cqring_add_event(req, ret);
3041 if (ret != sp->len)
3042 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003043 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003044 return 0;
3045}
3046
Jens Axboe2b188cc2019-01-07 10:46:33 -07003047/*
3048 * IORING_OP_NOP just posts a completion event, nothing else.
3049 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07003050static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003051{
3052 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003053
Jens Axboedef596e2019-01-09 08:59:42 -07003054 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3055 return -EINVAL;
3056
Jens Axboe78e19bb2019-11-06 15:21:34 -07003057 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06003058 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003059 return 0;
3060}
3061
Jens Axboe3529d8c2019-12-19 18:24:38 -07003062static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003063{
Jens Axboe6b063142019-01-10 22:13:58 -07003064 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003065
Jens Axboe09bb8392019-03-13 12:39:28 -06003066 if (!req->file)
3067 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003068
Jens Axboe6b063142019-01-10 22:13:58 -07003069 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003070 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003071 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003072 return -EINVAL;
3073
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003074 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3075 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3076 return -EINVAL;
3077
3078 req->sync.off = READ_ONCE(sqe->off);
3079 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003080 return 0;
3081}
3082
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003083static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07003084{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003085 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003086 int ret;
3087
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003088 /* fsync always requires a blocking context */
3089 if (force_nonblock)
3090 return -EAGAIN;
3091
Jens Axboe9adbd452019-12-20 08:45:55 -07003092 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003093 end > 0 ? end : LLONG_MAX,
3094 req->sync.flags & IORING_FSYNC_DATASYNC);
3095 if (ret < 0)
3096 req_set_fail_links(req);
3097 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003098 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003099 return 0;
3100}
3101
Jens Axboed63d1b52019-12-10 10:38:56 -07003102static int io_fallocate_prep(struct io_kiocb *req,
3103 const struct io_uring_sqe *sqe)
3104{
3105 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3106 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003107 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3108 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003109
3110 req->sync.off = READ_ONCE(sqe->off);
3111 req->sync.len = READ_ONCE(sqe->addr);
3112 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06003113 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07003114 return 0;
3115}
3116
Pavel Begunkov014db002020-03-03 21:33:12 +03003117static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07003118{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003119 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003120
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003121 /* fallocate always requiring blocking context */
3122 if (force_nonblock)
3123 return -EAGAIN;
3124
3125 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3126 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3127 req->sync.len);
3128 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3129 if (ret < 0)
3130 req_set_fail_links(req);
3131 io_cqring_add_event(req, ret);
3132 io_put_req(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07003133 return 0;
3134}
3135
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003136static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003137{
Jens Axboef8748882020-01-08 17:47:02 -07003138 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003139 int ret;
3140
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003141 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003142 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003143 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003144 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003145 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003146 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003147
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003148 /* open.how should be already initialised */
3149 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003150 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003151
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003152 req->open.dfd = READ_ONCE(sqe->fd);
3153 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003154 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003155 if (IS_ERR(req->open.filename)) {
3156 ret = PTR_ERR(req->open.filename);
3157 req->open.filename = NULL;
3158 return ret;
3159 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003160 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003161 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003162 return 0;
3163}
3164
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003165static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3166{
3167 u64 flags, mode;
3168
3169 if (req->flags & REQ_F_NEED_CLEANUP)
3170 return 0;
3171 mode = READ_ONCE(sqe->len);
3172 flags = READ_ONCE(sqe->open_flags);
3173 req->open.how = build_open_how(flags, mode);
3174 return __io_openat_prep(req, sqe);
3175}
3176
Jens Axboecebdb982020-01-08 17:59:24 -07003177static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3178{
3179 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003180 size_t len;
3181 int ret;
3182
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003183 if (req->flags & REQ_F_NEED_CLEANUP)
3184 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003185 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3186 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003187 if (len < OPEN_HOW_SIZE_VER0)
3188 return -EINVAL;
3189
3190 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3191 len);
3192 if (ret)
3193 return ret;
3194
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003195 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003196}
3197
Pavel Begunkov014db002020-03-03 21:33:12 +03003198static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003199{
3200 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003201 struct file *file;
3202 int ret;
3203
Jens Axboef86cd202020-01-29 13:46:44 -07003204 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003205 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003206
Jens Axboecebdb982020-01-08 17:59:24 -07003207 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003208 if (ret)
3209 goto err;
3210
Jens Axboe4022e7a2020-03-19 19:23:18 -06003211 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003212 if (ret < 0)
3213 goto err;
3214
3215 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3216 if (IS_ERR(file)) {
3217 put_unused_fd(ret);
3218 ret = PTR_ERR(file);
3219 } else {
3220 fsnotify_open(file);
3221 fd_install(ret, file);
3222 }
3223err:
3224 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003225 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003226 if (ret < 0)
3227 req_set_fail_links(req);
3228 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003229 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003230 return 0;
3231}
3232
Pavel Begunkov014db002020-03-03 21:33:12 +03003233static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003234{
Pavel Begunkov014db002020-03-03 21:33:12 +03003235 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003236}
3237
Jens Axboe067524e2020-03-02 16:32:28 -07003238static int io_remove_buffers_prep(struct io_kiocb *req,
3239 const struct io_uring_sqe *sqe)
3240{
3241 struct io_provide_buf *p = &req->pbuf;
3242 u64 tmp;
3243
3244 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3245 return -EINVAL;
3246
3247 tmp = READ_ONCE(sqe->fd);
3248 if (!tmp || tmp > USHRT_MAX)
3249 return -EINVAL;
3250
3251 memset(p, 0, sizeof(*p));
3252 p->nbufs = tmp;
3253 p->bgid = READ_ONCE(sqe->buf_group);
3254 return 0;
3255}
3256
3257static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3258 int bgid, unsigned nbufs)
3259{
3260 unsigned i = 0;
3261
3262 /* shouldn't happen */
3263 if (!nbufs)
3264 return 0;
3265
3266 /* the head kbuf is the list itself */
3267 while (!list_empty(&buf->list)) {
3268 struct io_buffer *nxt;
3269
3270 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3271 list_del(&nxt->list);
3272 kfree(nxt);
3273 if (++i == nbufs)
3274 return i;
3275 }
3276 i++;
3277 kfree(buf);
3278 idr_remove(&ctx->io_buffer_idr, bgid);
3279
3280 return i;
3281}
3282
3283static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3284{
3285 struct io_provide_buf *p = &req->pbuf;
3286 struct io_ring_ctx *ctx = req->ctx;
3287 struct io_buffer *head;
3288 int ret = 0;
3289
3290 io_ring_submit_lock(ctx, !force_nonblock);
3291
3292 lockdep_assert_held(&ctx->uring_lock);
3293
3294 ret = -ENOENT;
3295 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3296 if (head)
3297 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3298
3299 io_ring_submit_lock(ctx, !force_nonblock);
3300 if (ret < 0)
3301 req_set_fail_links(req);
3302 io_cqring_add_event(req, ret);
3303 io_put_req(req);
3304 return 0;
3305}
3306
Jens Axboeddf0322d2020-02-23 16:41:33 -07003307static int io_provide_buffers_prep(struct io_kiocb *req,
3308 const struct io_uring_sqe *sqe)
3309{
3310 struct io_provide_buf *p = &req->pbuf;
3311 u64 tmp;
3312
3313 if (sqe->ioprio || sqe->rw_flags)
3314 return -EINVAL;
3315
3316 tmp = READ_ONCE(sqe->fd);
3317 if (!tmp || tmp > USHRT_MAX)
3318 return -E2BIG;
3319 p->nbufs = tmp;
3320 p->addr = READ_ONCE(sqe->addr);
3321 p->len = READ_ONCE(sqe->len);
3322
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003323 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003324 return -EFAULT;
3325
3326 p->bgid = READ_ONCE(sqe->buf_group);
3327 tmp = READ_ONCE(sqe->off);
3328 if (tmp > USHRT_MAX)
3329 return -E2BIG;
3330 p->bid = tmp;
3331 return 0;
3332}
3333
3334static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3335{
3336 struct io_buffer *buf;
3337 u64 addr = pbuf->addr;
3338 int i, bid = pbuf->bid;
3339
3340 for (i = 0; i < pbuf->nbufs; i++) {
3341 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3342 if (!buf)
3343 break;
3344
3345 buf->addr = addr;
3346 buf->len = pbuf->len;
3347 buf->bid = bid;
3348 addr += pbuf->len;
3349 bid++;
3350 if (!*head) {
3351 INIT_LIST_HEAD(&buf->list);
3352 *head = buf;
3353 } else {
3354 list_add_tail(&buf->list, &(*head)->list);
3355 }
3356 }
3357
3358 return i ? i : -ENOMEM;
3359}
3360
Jens Axboeddf0322d2020-02-23 16:41:33 -07003361static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3362{
3363 struct io_provide_buf *p = &req->pbuf;
3364 struct io_ring_ctx *ctx = req->ctx;
3365 struct io_buffer *head, *list;
3366 int ret = 0;
3367
3368 io_ring_submit_lock(ctx, !force_nonblock);
3369
3370 lockdep_assert_held(&ctx->uring_lock);
3371
3372 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3373
3374 ret = io_add_buffers(p, &head);
3375 if (ret < 0)
3376 goto out;
3377
3378 if (!list) {
3379 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3380 GFP_KERNEL);
3381 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003382 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003383 goto out;
3384 }
3385 }
3386out:
3387 io_ring_submit_unlock(ctx, !force_nonblock);
3388 if (ret < 0)
3389 req_set_fail_links(req);
3390 io_cqring_add_event(req, ret);
3391 io_put_req(req);
3392 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003393}
3394
Jens Axboe3e4827b2020-01-08 15:18:09 -07003395static int io_epoll_ctl_prep(struct io_kiocb *req,
3396 const struct io_uring_sqe *sqe)
3397{
3398#if defined(CONFIG_EPOLL)
3399 if (sqe->ioprio || sqe->buf_index)
3400 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003401 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3402 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003403
3404 req->epoll.epfd = READ_ONCE(sqe->fd);
3405 req->epoll.op = READ_ONCE(sqe->len);
3406 req->epoll.fd = READ_ONCE(sqe->off);
3407
3408 if (ep_op_has_event(req->epoll.op)) {
3409 struct epoll_event __user *ev;
3410
3411 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3412 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3413 return -EFAULT;
3414 }
3415
3416 return 0;
3417#else
3418 return -EOPNOTSUPP;
3419#endif
3420}
3421
Pavel Begunkov014db002020-03-03 21:33:12 +03003422static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003423{
3424#if defined(CONFIG_EPOLL)
3425 struct io_epoll *ie = &req->epoll;
3426 int ret;
3427
3428 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3429 if (force_nonblock && ret == -EAGAIN)
3430 return -EAGAIN;
3431
3432 if (ret < 0)
3433 req_set_fail_links(req);
3434 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003435 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003436 return 0;
3437#else
3438 return -EOPNOTSUPP;
3439#endif
3440}
3441
Jens Axboec1ca7572019-12-25 22:18:28 -07003442static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3443{
3444#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3445 if (sqe->ioprio || sqe->buf_index || sqe->off)
3446 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003447 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3448 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003449
3450 req->madvise.addr = READ_ONCE(sqe->addr);
3451 req->madvise.len = READ_ONCE(sqe->len);
3452 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3453 return 0;
3454#else
3455 return -EOPNOTSUPP;
3456#endif
3457}
3458
Pavel Begunkov014db002020-03-03 21:33:12 +03003459static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003460{
3461#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3462 struct io_madvise *ma = &req->madvise;
3463 int ret;
3464
3465 if (force_nonblock)
3466 return -EAGAIN;
3467
3468 ret = do_madvise(ma->addr, ma->len, ma->advice);
3469 if (ret < 0)
3470 req_set_fail_links(req);
3471 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003472 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003473 return 0;
3474#else
3475 return -EOPNOTSUPP;
3476#endif
3477}
3478
Jens Axboe4840e412019-12-25 22:03:45 -07003479static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3480{
3481 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3482 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003483 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3484 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003485
3486 req->fadvise.offset = READ_ONCE(sqe->off);
3487 req->fadvise.len = READ_ONCE(sqe->len);
3488 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3489 return 0;
3490}
3491
Pavel Begunkov014db002020-03-03 21:33:12 +03003492static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003493{
3494 struct io_fadvise *fa = &req->fadvise;
3495 int ret;
3496
Jens Axboe3e694262020-02-01 09:22:49 -07003497 if (force_nonblock) {
3498 switch (fa->advice) {
3499 case POSIX_FADV_NORMAL:
3500 case POSIX_FADV_RANDOM:
3501 case POSIX_FADV_SEQUENTIAL:
3502 break;
3503 default:
3504 return -EAGAIN;
3505 }
3506 }
Jens Axboe4840e412019-12-25 22:03:45 -07003507
3508 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3509 if (ret < 0)
3510 req_set_fail_links(req);
3511 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003512 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003513 return 0;
3514}
3515
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003516static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3517{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003518 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3519 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003520 if (sqe->ioprio || sqe->buf_index)
3521 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003522 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003523 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003524
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003525 req->statx.dfd = READ_ONCE(sqe->fd);
3526 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003527 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003528 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3529 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003530
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003531 return 0;
3532}
3533
Pavel Begunkov014db002020-03-03 21:33:12 +03003534static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003535{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003536 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003537 int ret;
3538
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003539 if (force_nonblock) {
3540 /* only need file table for an actual valid fd */
3541 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3542 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003543 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003544 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003545
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003546 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3547 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003548
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003549 if (ret < 0)
3550 req_set_fail_links(req);
3551 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003552 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003553 return 0;
3554}
3555
Jens Axboeb5dba592019-12-11 14:02:38 -07003556static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3557{
3558 /*
3559 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003560 * leave the 'file' in an undeterminate state, and here need to modify
3561 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003562 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003563 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003564 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3565
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003566 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3567 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003568 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3569 sqe->rw_flags || sqe->buf_index)
3570 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003571 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003572 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003573
3574 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003575 if ((req->file && req->file->f_op == &io_uring_fops) ||
3576 req->close.fd == req->ctx->ring_fd)
3577 return -EBADF;
3578
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003579 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003580 return 0;
3581}
3582
Pavel Begunkov014db002020-03-03 21:33:12 +03003583static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003584{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003585 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003586 int ret;
3587
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003588 /* might be already done during nonblock submission */
3589 if (!close->put_file) {
3590 ret = __close_fd_get_file(close->fd, &close->put_file);
3591 if (ret < 0)
3592 return (ret == -ENOENT) ? -EBADF : ret;
3593 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003594
3595 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003596 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003597 /* avoid grabbing files - we don't need the files */
3598 req->flags |= REQ_F_NO_FILE_TABLE | REQ_F_MUST_PUNT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003599 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003600 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003601
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003602 /* No ->flush() or already async, safely close from here */
3603 ret = filp_close(close->put_file, req->work.files);
3604 if (ret < 0)
3605 req_set_fail_links(req);
3606 io_cqring_add_event(req, ret);
3607 fput(close->put_file);
3608 close->put_file = NULL;
3609 io_put_req(req);
Jens Axboe1a417f42020-01-31 17:16:48 -07003610 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003611}
3612
Jens Axboe3529d8c2019-12-19 18:24:38 -07003613static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003614{
3615 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003616
3617 if (!req->file)
3618 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003619
3620 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3621 return -EINVAL;
3622 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3623 return -EINVAL;
3624
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003625 req->sync.off = READ_ONCE(sqe->off);
3626 req->sync.len = READ_ONCE(sqe->len);
3627 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003628 return 0;
3629}
3630
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003631static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003632{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003633 int ret;
3634
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003635 /* sync_file_range always requires a blocking context */
3636 if (force_nonblock)
3637 return -EAGAIN;
3638
Jens Axboe9adbd452019-12-20 08:45:55 -07003639 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003640 req->sync.flags);
3641 if (ret < 0)
3642 req_set_fail_links(req);
3643 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003644 io_put_req(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003645 return 0;
3646}
3647
YueHaibing469956e2020-03-04 15:53:52 +08003648#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003649static int io_setup_async_msg(struct io_kiocb *req,
3650 struct io_async_msghdr *kmsg)
3651{
3652 if (req->io)
3653 return -EAGAIN;
3654 if (io_alloc_async_ctx(req)) {
3655 if (kmsg->iov != kmsg->fast_iov)
3656 kfree(kmsg->iov);
3657 return -ENOMEM;
3658 }
3659 req->flags |= REQ_F_NEED_CLEANUP;
3660 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3661 return -EAGAIN;
3662}
3663
Jens Axboe3529d8c2019-12-19 18:24:38 -07003664static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003665{
Jens Axboee47293f2019-12-20 08:58:21 -07003666 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003667 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003668 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003669
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003670 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3671 return -EINVAL;
3672
Jens Axboee47293f2019-12-20 08:58:21 -07003673 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3674 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003675 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003676
Jens Axboed8768362020-02-27 14:17:49 -07003677#ifdef CONFIG_COMPAT
3678 if (req->ctx->compat)
3679 sr->msg_flags |= MSG_CMSG_COMPAT;
3680#endif
3681
Jens Axboefddafac2020-01-04 20:19:44 -07003682 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003683 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003684 /* iovec is already imported */
3685 if (req->flags & REQ_F_NEED_CLEANUP)
3686 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003687
Jens Axboed9688562019-12-09 19:35:20 -07003688 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003689 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003690 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003691 if (!ret)
3692 req->flags |= REQ_F_NEED_CLEANUP;
3693 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003694}
3695
Pavel Begunkov014db002020-03-03 21:33:12 +03003696static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003697{
Jens Axboe0b416c32019-12-15 10:57:46 -07003698 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003699 struct socket *sock;
3700 int ret;
3701
Jens Axboe03b12302019-12-02 18:50:25 -07003702 sock = sock_from_file(req->file, &ret);
3703 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003704 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003705 unsigned flags;
3706
Jens Axboe03b12302019-12-02 18:50:25 -07003707 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003708 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003709 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003710 /* if iov is set, it's allocated already */
3711 if (!kmsg->iov)
3712 kmsg->iov = kmsg->fast_iov;
3713 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003714 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003715 struct io_sr_msg *sr = &req->sr_msg;
3716
Jens Axboe0b416c32019-12-15 10:57:46 -07003717 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003718 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003719
3720 io.msg.iov = io.msg.fast_iov;
3721 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3722 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003723 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003724 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003725 }
3726
Jens Axboee47293f2019-12-20 08:58:21 -07003727 flags = req->sr_msg.msg_flags;
3728 if (flags & MSG_DONTWAIT)
3729 req->flags |= REQ_F_NOWAIT;
3730 else if (force_nonblock)
3731 flags |= MSG_DONTWAIT;
3732
Jens Axboe0b416c32019-12-15 10:57:46 -07003733 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003734 if (force_nonblock && ret == -EAGAIN)
3735 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003736 if (ret == -ERESTARTSYS)
3737 ret = -EINTR;
3738 }
3739
Pavel Begunkov1e950812020-02-06 19:51:16 +03003740 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003741 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003742 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003743 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003744 if (ret < 0)
3745 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003746 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003747 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003748}
3749
Pavel Begunkov014db002020-03-03 21:33:12 +03003750static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003751{
Jens Axboefddafac2020-01-04 20:19:44 -07003752 struct socket *sock;
3753 int ret;
3754
Jens Axboefddafac2020-01-04 20:19:44 -07003755 sock = sock_from_file(req->file, &ret);
3756 if (sock) {
3757 struct io_sr_msg *sr = &req->sr_msg;
3758 struct msghdr msg;
3759 struct iovec iov;
3760 unsigned flags;
3761
3762 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3763 &msg.msg_iter);
3764 if (ret)
3765 return ret;
3766
3767 msg.msg_name = NULL;
3768 msg.msg_control = NULL;
3769 msg.msg_controllen = 0;
3770 msg.msg_namelen = 0;
3771
3772 flags = req->sr_msg.msg_flags;
3773 if (flags & MSG_DONTWAIT)
3774 req->flags |= REQ_F_NOWAIT;
3775 else if (force_nonblock)
3776 flags |= MSG_DONTWAIT;
3777
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003778 msg.msg_flags = flags;
3779 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003780 if (force_nonblock && ret == -EAGAIN)
3781 return -EAGAIN;
3782 if (ret == -ERESTARTSYS)
3783 ret = -EINTR;
3784 }
3785
3786 io_cqring_add_event(req, ret);
3787 if (ret < 0)
3788 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003789 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003790 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003791}
3792
Jens Axboe52de1fe2020-02-27 10:15:42 -07003793static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3794{
3795 struct io_sr_msg *sr = &req->sr_msg;
3796 struct iovec __user *uiov;
3797 size_t iov_len;
3798 int ret;
3799
3800 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3801 &uiov, &iov_len);
3802 if (ret)
3803 return ret;
3804
3805 if (req->flags & REQ_F_BUFFER_SELECT) {
3806 if (iov_len > 1)
3807 return -EINVAL;
3808 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3809 return -EFAULT;
3810 sr->len = io->msg.iov[0].iov_len;
3811 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3812 sr->len);
3813 io->msg.iov = NULL;
3814 } else {
3815 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3816 &io->msg.iov, &io->msg.msg.msg_iter);
3817 if (ret > 0)
3818 ret = 0;
3819 }
3820
3821 return ret;
3822}
3823
3824#ifdef CONFIG_COMPAT
3825static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3826 struct io_async_ctx *io)
3827{
3828 struct compat_msghdr __user *msg_compat;
3829 struct io_sr_msg *sr = &req->sr_msg;
3830 struct compat_iovec __user *uiov;
3831 compat_uptr_t ptr;
3832 compat_size_t len;
3833 int ret;
3834
3835 msg_compat = (struct compat_msghdr __user *) sr->msg;
3836 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3837 &ptr, &len);
3838 if (ret)
3839 return ret;
3840
3841 uiov = compat_ptr(ptr);
3842 if (req->flags & REQ_F_BUFFER_SELECT) {
3843 compat_ssize_t clen;
3844
3845 if (len > 1)
3846 return -EINVAL;
3847 if (!access_ok(uiov, sizeof(*uiov)))
3848 return -EFAULT;
3849 if (__get_user(clen, &uiov->iov_len))
3850 return -EFAULT;
3851 if (clen < 0)
3852 return -EINVAL;
3853 sr->len = io->msg.iov[0].iov_len;
3854 io->msg.iov = NULL;
3855 } else {
3856 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3857 &io->msg.iov,
3858 &io->msg.msg.msg_iter);
3859 if (ret < 0)
3860 return ret;
3861 }
3862
3863 return 0;
3864}
Jens Axboe03b12302019-12-02 18:50:25 -07003865#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003866
3867static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3868{
3869 io->msg.iov = io->msg.fast_iov;
3870
3871#ifdef CONFIG_COMPAT
3872 if (req->ctx->compat)
3873 return __io_compat_recvmsg_copy_hdr(req, io);
3874#endif
3875
3876 return __io_recvmsg_copy_hdr(req, io);
3877}
3878
Jens Axboebcda7ba2020-02-23 16:42:51 -07003879static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3880 int *cflags, bool needs_lock)
3881{
3882 struct io_sr_msg *sr = &req->sr_msg;
3883 struct io_buffer *kbuf;
3884
3885 if (!(req->flags & REQ_F_BUFFER_SELECT))
3886 return NULL;
3887
3888 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3889 if (IS_ERR(kbuf))
3890 return kbuf;
3891
3892 sr->kbuf = kbuf;
3893 req->flags |= REQ_F_BUFFER_SELECTED;
3894
3895 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3896 *cflags |= IORING_CQE_F_BUFFER;
3897 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07003898}
3899
Jens Axboe3529d8c2019-12-19 18:24:38 -07003900static int io_recvmsg_prep(struct io_kiocb *req,
3901 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003902{
Jens Axboee47293f2019-12-20 08:58:21 -07003903 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003904 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003905 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003906
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003907 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3908 return -EINVAL;
3909
Jens Axboe3529d8c2019-12-19 18:24:38 -07003910 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3911 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003912 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003913 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003914
Jens Axboed8768362020-02-27 14:17:49 -07003915#ifdef CONFIG_COMPAT
3916 if (req->ctx->compat)
3917 sr->msg_flags |= MSG_CMSG_COMPAT;
3918#endif
3919
Jens Axboefddafac2020-01-04 20:19:44 -07003920 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003921 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003922 /* iovec is already imported */
3923 if (req->flags & REQ_F_NEED_CLEANUP)
3924 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003925
Jens Axboe52de1fe2020-02-27 10:15:42 -07003926 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003927 if (!ret)
3928 req->flags |= REQ_F_NEED_CLEANUP;
3929 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003930}
3931
Pavel Begunkov014db002020-03-03 21:33:12 +03003932static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003933{
Jens Axboe0b416c32019-12-15 10:57:46 -07003934 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003935 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003936 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003937
Jens Axboe0fa03c62019-04-19 13:34:07 -06003938 sock = sock_from_file(req->file, &ret);
3939 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003940 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003941 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003942 unsigned flags;
3943
Jens Axboe03b12302019-12-02 18:50:25 -07003944 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003945 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003946 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003947 /* if iov is set, it's allocated already */
3948 if (!kmsg->iov)
3949 kmsg->iov = kmsg->fast_iov;
3950 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003951 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003952 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003953 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003954
Jens Axboe52de1fe2020-02-27 10:15:42 -07003955 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003956 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003957 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003958 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003959
Jens Axboe52de1fe2020-02-27 10:15:42 -07003960 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3961 if (IS_ERR(kbuf)) {
3962 return PTR_ERR(kbuf);
3963 } else if (kbuf) {
3964 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3965 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3966 1, req->sr_msg.len);
3967 }
3968
Jens Axboee47293f2019-12-20 08:58:21 -07003969 flags = req->sr_msg.msg_flags;
3970 if (flags & MSG_DONTWAIT)
3971 req->flags |= REQ_F_NOWAIT;
3972 else if (force_nonblock)
3973 flags |= MSG_DONTWAIT;
3974
3975 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3976 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003977 if (force_nonblock && ret == -EAGAIN)
3978 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003979 if (ret == -ERESTARTSYS)
3980 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003981 }
3982
Pavel Begunkov1e950812020-02-06 19:51:16 +03003983 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003984 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003985 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003986 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003987 if (ret < 0)
3988 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003989 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003990 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003991}
3992
Pavel Begunkov014db002020-03-03 21:33:12 +03003993static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003994{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003995 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003996 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003997 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003998
Jens Axboefddafac2020-01-04 20:19:44 -07003999 sock = sock_from_file(req->file, &ret);
4000 if (sock) {
4001 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004002 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004003 struct msghdr msg;
4004 struct iovec iov;
4005 unsigned flags;
4006
Jens Axboebcda7ba2020-02-23 16:42:51 -07004007 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4008 if (IS_ERR(kbuf))
4009 return PTR_ERR(kbuf);
4010 else if (kbuf)
4011 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004012
Jens Axboebcda7ba2020-02-23 16:42:51 -07004013 ret = import_single_range(READ, buf, sr->len, &iov,
4014 &msg.msg_iter);
4015 if (ret) {
4016 kfree(kbuf);
4017 return ret;
4018 }
4019
4020 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004021 msg.msg_name = NULL;
4022 msg.msg_control = NULL;
4023 msg.msg_controllen = 0;
4024 msg.msg_namelen = 0;
4025 msg.msg_iocb = NULL;
4026 msg.msg_flags = 0;
4027
4028 flags = req->sr_msg.msg_flags;
4029 if (flags & MSG_DONTWAIT)
4030 req->flags |= REQ_F_NOWAIT;
4031 else if (force_nonblock)
4032 flags |= MSG_DONTWAIT;
4033
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004034 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07004035 if (force_nonblock && ret == -EAGAIN)
4036 return -EAGAIN;
4037 if (ret == -ERESTARTSYS)
4038 ret = -EINTR;
4039 }
4040
Jens Axboebcda7ba2020-02-23 16:42:51 -07004041 kfree(kbuf);
4042 req->flags &= ~REQ_F_NEED_CLEANUP;
4043 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004044 if (ret < 0)
4045 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004046 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004047 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004048}
4049
Jens Axboe3529d8c2019-12-19 18:24:38 -07004050static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004051{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004052 struct io_accept *accept = &req->accept;
4053
Jens Axboe17f2fe32019-10-17 14:42:58 -06004054 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4055 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004056 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004057 return -EINVAL;
4058
Jens Axboed55e5f52019-12-11 16:12:15 -07004059 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4060 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004061 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004062 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004063 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004064}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004065
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004066static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004067{
4068 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004069 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004070 int ret;
4071
Jiufei Xuee697dee2020-06-10 13:41:59 +08004072 if (req->file->f_flags & O_NONBLOCK)
4073 req->flags |= REQ_F_NOWAIT;
4074
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004075 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004076 accept->addr_len, accept->flags,
4077 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004078 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004079 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004080 if (ret < 0) {
4081 if (ret == -ERESTARTSYS)
4082 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004083 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004084 }
Jens Axboe78e19bb2019-11-06 15:21:34 -07004085 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004086 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004087 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004088}
4089
Jens Axboe3529d8c2019-12-19 18:24:38 -07004090static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004091{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004092 struct io_connect *conn = &req->connect;
4093 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07004094
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004095 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
4096 return -EINVAL;
4097 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4098 return -EINVAL;
4099
Jens Axboe3529d8c2019-12-19 18:24:38 -07004100 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4101 conn->addr_len = READ_ONCE(sqe->addr2);
4102
4103 if (!io)
4104 return 0;
4105
4106 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004107 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004108}
4109
Pavel Begunkov014db002020-03-03 21:33:12 +03004110static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004111{
Jens Axboef499a022019-12-02 16:28:46 -07004112 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004113 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004114 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004115
Jens Axboef499a022019-12-02 16:28:46 -07004116 if (req->io) {
4117 io = req->io;
4118 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004119 ret = move_addr_to_kernel(req->connect.addr,
4120 req->connect.addr_len,
4121 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004122 if (ret)
4123 goto out;
4124 io = &__io;
4125 }
4126
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004127 file_flags = force_nonblock ? O_NONBLOCK : 0;
4128
4129 ret = __sys_connect_file(req->file, &io->connect.address,
4130 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004131 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004132 if (req->io)
4133 return -EAGAIN;
4134 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004135 ret = -ENOMEM;
4136 goto out;
4137 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004138 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004139 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004140 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004141 if (ret == -ERESTARTSYS)
4142 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004143out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004144 if (ret < 0)
4145 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004146 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004147 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004148 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004149}
YueHaibing469956e2020-03-04 15:53:52 +08004150#else /* !CONFIG_NET */
4151static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4152{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004153 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004154}
4155
YueHaibing469956e2020-03-04 15:53:52 +08004156static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004157{
YueHaibing469956e2020-03-04 15:53:52 +08004158 return -EOPNOTSUPP;
4159}
4160
4161static int io_send(struct io_kiocb *req, bool force_nonblock)
4162{
4163 return -EOPNOTSUPP;
4164}
4165
4166static int io_recvmsg_prep(struct io_kiocb *req,
4167 const struct io_uring_sqe *sqe)
4168{
4169 return -EOPNOTSUPP;
4170}
4171
4172static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4173{
4174 return -EOPNOTSUPP;
4175}
4176
4177static int io_recv(struct io_kiocb *req, bool force_nonblock)
4178{
4179 return -EOPNOTSUPP;
4180}
4181
4182static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4183{
4184 return -EOPNOTSUPP;
4185}
4186
4187static int io_accept(struct io_kiocb *req, bool force_nonblock)
4188{
4189 return -EOPNOTSUPP;
4190}
4191
4192static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4193{
4194 return -EOPNOTSUPP;
4195}
4196
4197static int io_connect(struct io_kiocb *req, bool force_nonblock)
4198{
4199 return -EOPNOTSUPP;
4200}
4201#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004202
Jens Axboed7718a92020-02-14 22:23:12 -07004203struct io_poll_table {
4204 struct poll_table_struct pt;
4205 struct io_kiocb *req;
4206 int error;
4207};
4208
Jens Axboed7718a92020-02-14 22:23:12 -07004209static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4210 __poll_t mask, task_work_func_t func)
4211{
4212 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004213 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004214
4215 /* for instances that support it check for an event match first: */
4216 if (mask && !(mask & poll->events))
4217 return 0;
4218
4219 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4220
4221 list_del_init(&poll->wait.entry);
4222
4223 tsk = req->task;
4224 req->result = mask;
4225 init_task_work(&req->task_work, func);
4226 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004227 * If this fails, then the task is exiting. When a task exits, the
4228 * work gets canceled, so just cancel this request as well instead
4229 * of executing it. We can't safely execute it anyway, as we may not
4230 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004231 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004232 ret = task_work_add(tsk, &req->task_work, true);
4233 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004234 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004235 tsk = io_wq_get_task(req->ctx->io_wq);
4236 task_work_add(tsk, &req->task_work, true);
4237 }
Jens Axboed7718a92020-02-14 22:23:12 -07004238 wake_up_process(tsk);
4239 return 1;
4240}
4241
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004242static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4243 __acquires(&req->ctx->completion_lock)
4244{
4245 struct io_ring_ctx *ctx = req->ctx;
4246
4247 if (!req->result && !READ_ONCE(poll->canceled)) {
4248 struct poll_table_struct pt = { ._key = poll->events };
4249
4250 req->result = vfs_poll(req->file, &pt) & poll->events;
4251 }
4252
4253 spin_lock_irq(&ctx->completion_lock);
4254 if (!req->result && !READ_ONCE(poll->canceled)) {
4255 add_wait_queue(poll->head, &poll->wait);
4256 return true;
4257 }
4258
4259 return false;
4260}
4261
Jens Axboe18bceab2020-05-15 11:56:54 -06004262static void io_poll_remove_double(struct io_kiocb *req)
4263{
4264 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4265
4266 lockdep_assert_held(&req->ctx->completion_lock);
4267
4268 if (poll && poll->head) {
4269 struct wait_queue_head *head = poll->head;
4270
4271 spin_lock(&head->lock);
4272 list_del_init(&poll->wait.entry);
4273 if (poll->wait.private)
4274 refcount_dec(&req->refs);
4275 poll->head = NULL;
4276 spin_unlock(&head->lock);
4277 }
4278}
4279
4280static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4281{
4282 struct io_ring_ctx *ctx = req->ctx;
4283
4284 io_poll_remove_double(req);
4285 req->poll.done = true;
4286 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4287 io_commit_cqring(ctx);
4288}
4289
4290static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4291{
4292 struct io_ring_ctx *ctx = req->ctx;
4293
4294 if (io_poll_rewait(req, &req->poll)) {
4295 spin_unlock_irq(&ctx->completion_lock);
4296 return;
4297 }
4298
4299 hash_del(&req->hash_node);
4300 io_poll_complete(req, req->result, 0);
4301 req->flags |= REQ_F_COMP_LOCKED;
4302 io_put_req_find_next(req, nxt);
4303 spin_unlock_irq(&ctx->completion_lock);
4304
4305 io_cqring_ev_posted(ctx);
4306}
4307
4308static void io_poll_task_func(struct callback_head *cb)
4309{
4310 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4311 struct io_kiocb *nxt = NULL;
4312
4313 io_poll_task_handler(req, &nxt);
4314 if (nxt) {
4315 struct io_ring_ctx *ctx = nxt->ctx;
4316
4317 mutex_lock(&ctx->uring_lock);
4318 __io_queue_sqe(nxt, NULL);
4319 mutex_unlock(&ctx->uring_lock);
4320 }
4321}
4322
4323static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4324 int sync, void *key)
4325{
4326 struct io_kiocb *req = wait->private;
4327 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4328 __poll_t mask = key_to_poll(key);
4329
4330 /* for instances that support it check for an event match first: */
4331 if (mask && !(mask & poll->events))
4332 return 0;
4333
4334 if (req->poll.head) {
4335 bool done;
4336
4337 spin_lock(&req->poll.head->lock);
4338 done = list_empty(&req->poll.wait.entry);
4339 if (!done)
4340 list_del_init(&req->poll.wait.entry);
4341 spin_unlock(&req->poll.head->lock);
4342 if (!done)
4343 __io_async_wake(req, poll, mask, io_poll_task_func);
4344 }
4345 refcount_dec(&req->refs);
4346 return 1;
4347}
4348
4349static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4350 wait_queue_func_t wake_func)
4351{
4352 poll->head = NULL;
4353 poll->done = false;
4354 poll->canceled = false;
4355 poll->events = events;
4356 INIT_LIST_HEAD(&poll->wait.entry);
4357 init_waitqueue_func_entry(&poll->wait, wake_func);
4358}
4359
4360static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4361 struct wait_queue_head *head)
4362{
4363 struct io_kiocb *req = pt->req;
4364
4365 /*
4366 * If poll->head is already set, it's because the file being polled
4367 * uses multiple waitqueues for poll handling (eg one for read, one
4368 * for write). Setup a separate io_poll_iocb if this happens.
4369 */
4370 if (unlikely(poll->head)) {
4371 /* already have a 2nd entry, fail a third attempt */
4372 if (req->io) {
4373 pt->error = -EINVAL;
4374 return;
4375 }
4376 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4377 if (!poll) {
4378 pt->error = -ENOMEM;
4379 return;
4380 }
4381 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4382 refcount_inc(&req->refs);
4383 poll->wait.private = req;
4384 req->io = (void *) poll;
4385 }
4386
4387 pt->error = 0;
4388 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004389
4390 if (poll->events & EPOLLEXCLUSIVE)
4391 add_wait_queue_exclusive(head, &poll->wait);
4392 else
4393 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06004394}
4395
4396static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4397 struct poll_table_struct *p)
4398{
4399 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4400
4401 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4402}
4403
Jens Axboed7718a92020-02-14 22:23:12 -07004404static void io_async_task_func(struct callback_head *cb)
4405{
4406 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4407 struct async_poll *apoll = req->apoll;
4408 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31067252020-05-17 17:43:31 -06004409 bool canceled = false;
Jens Axboed7718a92020-02-14 22:23:12 -07004410
4411 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4412
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004413 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004414 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004415 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004416 }
4417
Jens Axboe31067252020-05-17 17:43:31 -06004418 /* If req is still hashed, it cannot have been canceled. Don't check. */
4419 if (hash_hashed(&req->hash_node)) {
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004420 hash_del(&req->hash_node);
Jens Axboe31067252020-05-17 17:43:31 -06004421 } else {
4422 canceled = READ_ONCE(apoll->poll.canceled);
4423 if (canceled) {
4424 io_cqring_fill_event(req, -ECANCELED);
4425 io_commit_cqring(ctx);
4426 }
Jens Axboe2bae0472020-04-13 11:16:34 -06004427 }
4428
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004429 spin_unlock_irq(&ctx->completion_lock);
4430
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004431 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004432 if (req->flags & REQ_F_WORK_INITIALIZED)
4433 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe31067252020-05-17 17:43:31 -06004434 kfree(apoll);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004435
Jens Axboe31067252020-05-17 17:43:31 -06004436 if (!canceled) {
4437 __set_current_state(TASK_RUNNING);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004438 if (io_sq_thread_acquire_mm(ctx, req)) {
4439 io_cqring_add_event(req, -EFAULT);
4440 goto end_req;
4441 }
Jens Axboe31067252020-05-17 17:43:31 -06004442 mutex_lock(&ctx->uring_lock);
4443 __io_queue_sqe(req, NULL);
4444 mutex_unlock(&ctx->uring_lock);
4445 } else {
Jens Axboe2bae0472020-04-13 11:16:34 -06004446 io_cqring_ev_posted(ctx);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004447end_req:
Jens Axboe2bae0472020-04-13 11:16:34 -06004448 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004449 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004450 }
Jens Axboed7718a92020-02-14 22:23:12 -07004451}
4452
4453static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4454 void *key)
4455{
4456 struct io_kiocb *req = wait->private;
4457 struct io_poll_iocb *poll = &req->apoll->poll;
4458
4459 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4460 key_to_poll(key));
4461
4462 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4463}
4464
4465static void io_poll_req_insert(struct io_kiocb *req)
4466{
4467 struct io_ring_ctx *ctx = req->ctx;
4468 struct hlist_head *list;
4469
4470 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4471 hlist_add_head(&req->hash_node, list);
4472}
4473
4474static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4475 struct io_poll_iocb *poll,
4476 struct io_poll_table *ipt, __poll_t mask,
4477 wait_queue_func_t wake_func)
4478 __acquires(&ctx->completion_lock)
4479{
4480 struct io_ring_ctx *ctx = req->ctx;
4481 bool cancel = false;
4482
4483 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004484 io_init_poll_iocb(poll, mask, wake_func);
4485 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004486
4487 ipt->pt._key = mask;
4488 ipt->req = req;
4489 ipt->error = -EINVAL;
4490
Jens Axboed7718a92020-02-14 22:23:12 -07004491 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4492
4493 spin_lock_irq(&ctx->completion_lock);
4494 if (likely(poll->head)) {
4495 spin_lock(&poll->head->lock);
4496 if (unlikely(list_empty(&poll->wait.entry))) {
4497 if (ipt->error)
4498 cancel = true;
4499 ipt->error = 0;
4500 mask = 0;
4501 }
4502 if (mask || ipt->error)
4503 list_del_init(&poll->wait.entry);
4504 else if (cancel)
4505 WRITE_ONCE(poll->canceled, true);
4506 else if (!poll->done) /* actually waiting for an event */
4507 io_poll_req_insert(req);
4508 spin_unlock(&poll->head->lock);
4509 }
4510
4511 return mask;
4512}
4513
4514static bool io_arm_poll_handler(struct io_kiocb *req)
4515{
4516 const struct io_op_def *def = &io_op_defs[req->opcode];
4517 struct io_ring_ctx *ctx = req->ctx;
4518 struct async_poll *apoll;
4519 struct io_poll_table ipt;
4520 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004521 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004522
4523 if (!req->file || !file_can_poll(req->file))
4524 return false;
4525 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4526 return false;
4527 if (!def->pollin && !def->pollout)
4528 return false;
4529
4530 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4531 if (unlikely(!apoll))
4532 return false;
4533
4534 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004535 if (req->flags & REQ_F_WORK_INITIALIZED)
4536 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004537 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004538
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004539 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004540 req->apoll = apoll;
4541 INIT_HLIST_NODE(&req->hash_node);
4542
Nathan Chancellor8755d972020-03-02 16:01:19 -07004543 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004544 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004545 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004546 if (def->pollout)
4547 mask |= POLLOUT | POLLWRNORM;
4548 mask |= POLLERR | POLLPRI;
4549
4550 ipt.pt._qproc = io_async_queue_proc;
4551
4552 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4553 io_async_wake);
4554 if (ret) {
4555 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004556 /* only remove double add if we did it here */
4557 if (!had_io)
4558 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004559 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004560 if (req->flags & REQ_F_WORK_INITIALIZED)
4561 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004562 kfree(apoll);
4563 return false;
4564 }
4565 spin_unlock_irq(&ctx->completion_lock);
4566 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4567 apoll->poll.events);
4568 return true;
4569}
4570
4571static bool __io_poll_remove_one(struct io_kiocb *req,
4572 struct io_poll_iocb *poll)
4573{
Jens Axboeb41e9852020-02-17 09:52:41 -07004574 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004575
4576 spin_lock(&poll->head->lock);
4577 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004578 if (!list_empty(&poll->wait.entry)) {
4579 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004580 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004581 }
4582 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004583 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004584 return do_complete;
4585}
4586
4587static bool io_poll_remove_one(struct io_kiocb *req)
4588{
4589 bool do_complete;
4590
4591 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004592 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004593 do_complete = __io_poll_remove_one(req, &req->poll);
4594 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004595 struct async_poll *apoll = req->apoll;
4596
Jens Axboed7718a92020-02-14 22:23:12 -07004597 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004598 do_complete = __io_poll_remove_one(req, &apoll->poll);
4599 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004600 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004601 /*
4602 * restore ->work because we will call
4603 * io_req_work_drop_env below when dropping the
4604 * final reference.
4605 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004606 if (req->flags & REQ_F_WORK_INITIALIZED)
4607 memcpy(&req->work, &apoll->work,
4608 sizeof(req->work));
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004609 kfree(apoll);
4610 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004611 }
4612
Jens Axboeb41e9852020-02-17 09:52:41 -07004613 if (do_complete) {
4614 io_cqring_fill_event(req, -ECANCELED);
4615 io_commit_cqring(req->ctx);
4616 req->flags |= REQ_F_COMP_LOCKED;
4617 io_put_req(req);
4618 }
4619
4620 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004621}
4622
4623static void io_poll_remove_all(struct io_ring_ctx *ctx)
4624{
Jens Axboe78076bb2019-12-04 19:56:40 -07004625 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004626 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004627 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004628
4629 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004630 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4631 struct hlist_head *list;
4632
4633 list = &ctx->cancel_hash[i];
4634 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004635 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004636 }
4637 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004638
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004639 if (posted)
4640 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004641}
4642
Jens Axboe47f46762019-11-09 17:43:02 -07004643static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4644{
Jens Axboe78076bb2019-12-04 19:56:40 -07004645 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004646 struct io_kiocb *req;
4647
Jens Axboe78076bb2019-12-04 19:56:40 -07004648 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4649 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004650 if (sqe_addr != req->user_data)
4651 continue;
4652 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004653 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004654 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004655 }
4656
4657 return -ENOENT;
4658}
4659
Jens Axboe3529d8c2019-12-19 18:24:38 -07004660static int io_poll_remove_prep(struct io_kiocb *req,
4661 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004662{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004663 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4664 return -EINVAL;
4665 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4666 sqe->poll_events)
4667 return -EINVAL;
4668
Jens Axboe0969e782019-12-17 18:40:57 -07004669 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004670 return 0;
4671}
4672
4673/*
4674 * Find a running poll command that matches one specified in sqe->addr,
4675 * and remove it if found.
4676 */
4677static int io_poll_remove(struct io_kiocb *req)
4678{
4679 struct io_ring_ctx *ctx = req->ctx;
4680 u64 addr;
4681 int ret;
4682
Jens Axboe0969e782019-12-17 18:40:57 -07004683 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004684 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004685 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004686 spin_unlock_irq(&ctx->completion_lock);
4687
Jens Axboe78e19bb2019-11-06 15:21:34 -07004688 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004689 if (ret < 0)
4690 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004691 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004692 return 0;
4693}
4694
Jens Axboe221c5eb2019-01-17 09:41:58 -07004695static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4696 void *key)
4697{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004698 struct io_kiocb *req = wait->private;
4699 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004700
Jens Axboed7718a92020-02-14 22:23:12 -07004701 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004702}
4703
Jens Axboe221c5eb2019-01-17 09:41:58 -07004704static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4705 struct poll_table_struct *p)
4706{
4707 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4708
Jens Axboed7718a92020-02-14 22:23:12 -07004709 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004710}
4711
Jens Axboe3529d8c2019-12-19 18:24:38 -07004712static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004713{
4714 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08004715 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004716
4717 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4718 return -EINVAL;
4719 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4720 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004721 if (!poll->file)
4722 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004723
Jiufei Xue5769a352020-06-17 17:53:55 +08004724 events = READ_ONCE(sqe->poll32_events);
4725#ifdef __BIG_ENDIAN
4726 events = swahw32(events);
4727#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08004728 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4729 (events & EPOLLEXCLUSIVE);
Jens Axboeb41e9852020-02-17 09:52:41 -07004730
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004731 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004732 return 0;
4733}
4734
Pavel Begunkov014db002020-03-03 21:33:12 +03004735static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004736{
4737 struct io_poll_iocb *poll = &req->poll;
4738 struct io_ring_ctx *ctx = req->ctx;
4739 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004740 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004741
Jens Axboe78076bb2019-12-04 19:56:40 -07004742 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004743 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004744 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004745
Jens Axboed7718a92020-02-14 22:23:12 -07004746 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4747 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004748
Jens Axboe8c838782019-03-12 15:48:16 -06004749 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004750 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004751 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004752 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004753 spin_unlock_irq(&ctx->completion_lock);
4754
Jens Axboe8c838782019-03-12 15:48:16 -06004755 if (mask) {
4756 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004757 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004758 }
Jens Axboe8c838782019-03-12 15:48:16 -06004759 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004760}
4761
Jens Axboe5262f562019-09-17 12:26:57 -06004762static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4763{
Jens Axboead8a48a2019-11-15 08:49:11 -07004764 struct io_timeout_data *data = container_of(timer,
4765 struct io_timeout_data, timer);
4766 struct io_kiocb *req = data->req;
4767 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004768 unsigned long flags;
4769
Jens Axboe5262f562019-09-17 12:26:57 -06004770 atomic_inc(&ctx->cq_timeouts);
4771
4772 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004773 /*
Jens Axboe11365042019-10-16 09:08:32 -06004774 * We could be racing with timeout deletion. If the list is empty,
4775 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004776 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004777 if (!list_empty(&req->list))
Jens Axboe11365042019-10-16 09:08:32 -06004778 list_del_init(&req->list);
Jens Axboe842f9612019-10-29 12:34:10 -06004779
Jens Axboe78e19bb2019-11-06 15:21:34 -07004780 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004781 io_commit_cqring(ctx);
4782 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4783
4784 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004785 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004786 io_put_req(req);
4787 return HRTIMER_NORESTART;
4788}
4789
Jens Axboe47f46762019-11-09 17:43:02 -07004790static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4791{
4792 struct io_kiocb *req;
4793 int ret = -ENOENT;
4794
4795 list_for_each_entry(req, &ctx->timeout_list, list) {
4796 if (user_data == req->user_data) {
4797 list_del_init(&req->list);
4798 ret = 0;
4799 break;
4800 }
4801 }
4802
4803 if (ret == -ENOENT)
4804 return ret;
4805
Jens Axboe2d283902019-12-04 11:08:05 -07004806 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004807 if (ret == -1)
4808 return -EALREADY;
4809
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004810 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004811 io_cqring_fill_event(req, -ECANCELED);
4812 io_put_req(req);
4813 return 0;
4814}
4815
Jens Axboe3529d8c2019-12-19 18:24:38 -07004816static int io_timeout_remove_prep(struct io_kiocb *req,
4817 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004818{
Jens Axboeb29472e2019-12-17 18:50:29 -07004819 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4820 return -EINVAL;
4821 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4822 return -EINVAL;
4823
4824 req->timeout.addr = READ_ONCE(sqe->addr);
4825 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4826 if (req->timeout.flags)
4827 return -EINVAL;
4828
Jens Axboeb29472e2019-12-17 18:50:29 -07004829 return 0;
4830}
4831
Jens Axboe11365042019-10-16 09:08:32 -06004832/*
4833 * Remove or update an existing timeout command
4834 */
Jens Axboefc4df992019-12-10 14:38:45 -07004835static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004836{
4837 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004838 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004839
Jens Axboe11365042019-10-16 09:08:32 -06004840 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004841 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004842
Jens Axboe47f46762019-11-09 17:43:02 -07004843 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004844 io_commit_cqring(ctx);
4845 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004846 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004847 if (ret < 0)
4848 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004849 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004850 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004851}
4852
Jens Axboe3529d8c2019-12-19 18:24:38 -07004853static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004854 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004855{
Jens Axboead8a48a2019-11-15 08:49:11 -07004856 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004857 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03004858 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06004859
Jens Axboead8a48a2019-11-15 08:49:11 -07004860 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004861 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004862 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004863 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03004864 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07004865 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004866 flags = READ_ONCE(sqe->timeout_flags);
4867 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004868 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004869
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004870 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07004871
Jens Axboe3529d8c2019-12-19 18:24:38 -07004872 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004873 return -ENOMEM;
4874
4875 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004876 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004877 req->flags |= REQ_F_TIMEOUT;
4878
4879 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004880 return -EFAULT;
4881
Jens Axboe11365042019-10-16 09:08:32 -06004882 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004883 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004884 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004885 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004886
Jens Axboead8a48a2019-11-15 08:49:11 -07004887 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4888 return 0;
4889}
4890
Jens Axboefc4df992019-12-10 14:38:45 -07004891static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004892{
Jens Axboead8a48a2019-11-15 08:49:11 -07004893 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004894 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004895 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004896 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07004897
Pavel Begunkov733f5c92020-05-26 20:34:03 +03004898 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07004899
Jens Axboe5262f562019-09-17 12:26:57 -06004900 /*
4901 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004902 * timeout event to be satisfied. If it isn't set, then this is
4903 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004904 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004905 if (!off) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07004906 req->flags |= REQ_F_TIMEOUT_NOSEQ;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004907 entry = ctx->timeout_list.prev;
4908 goto add;
4909 }
Jens Axboe5262f562019-09-17 12:26:57 -06004910
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004911 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
4912 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06004913
4914 /*
4915 * Insertion sort, ensuring the first entry in the list is always
4916 * the one we need first.
4917 */
Jens Axboe5262f562019-09-17 12:26:57 -06004918 list_for_each_prev(entry, &ctx->timeout_list) {
4919 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Jens Axboe5262f562019-09-17 12:26:57 -06004920
Jens Axboe93bd25b2019-11-11 23:34:31 -07004921 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4922 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004923 /* nxt.seq is behind @tail, otherwise would've been completed */
4924 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06004925 break;
4926 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07004927add:
Jens Axboe5262f562019-09-17 12:26:57 -06004928 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004929 data->timer.function = io_timeout_fn;
4930 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004931 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004932 return 0;
4933}
4934
Jens Axboe62755e32019-10-28 21:49:21 -06004935static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004936{
Jens Axboe62755e32019-10-28 21:49:21 -06004937 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004938
Jens Axboe62755e32019-10-28 21:49:21 -06004939 return req->user_data == (unsigned long) data;
4940}
4941
Jens Axboee977d6d2019-11-05 12:39:45 -07004942static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004943{
Jens Axboe62755e32019-10-28 21:49:21 -06004944 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004945 int ret = 0;
4946
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03004947 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06004948 switch (cancel_ret) {
4949 case IO_WQ_CANCEL_OK:
4950 ret = 0;
4951 break;
4952 case IO_WQ_CANCEL_RUNNING:
4953 ret = -EALREADY;
4954 break;
4955 case IO_WQ_CANCEL_NOTFOUND:
4956 ret = -ENOENT;
4957 break;
4958 }
4959
Jens Axboee977d6d2019-11-05 12:39:45 -07004960 return ret;
4961}
4962
Jens Axboe47f46762019-11-09 17:43:02 -07004963static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4964 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004965 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004966{
4967 unsigned long flags;
4968 int ret;
4969
4970 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4971 if (ret != -ENOENT) {
4972 spin_lock_irqsave(&ctx->completion_lock, flags);
4973 goto done;
4974 }
4975
4976 spin_lock_irqsave(&ctx->completion_lock, flags);
4977 ret = io_timeout_cancel(ctx, sqe_addr);
4978 if (ret != -ENOENT)
4979 goto done;
4980 ret = io_poll_cancel(ctx, sqe_addr);
4981done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004982 if (!ret)
4983 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004984 io_cqring_fill_event(req, ret);
4985 io_commit_cqring(ctx);
4986 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4987 io_cqring_ev_posted(ctx);
4988
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004989 if (ret < 0)
4990 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004991 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004992}
4993
Jens Axboe3529d8c2019-12-19 18:24:38 -07004994static int io_async_cancel_prep(struct io_kiocb *req,
4995 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004996{
Jens Axboefbf23842019-12-17 18:45:56 -07004997 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004998 return -EINVAL;
4999 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
5000 sqe->cancel_flags)
5001 return -EINVAL;
5002
Jens Axboefbf23842019-12-17 18:45:56 -07005003 req->cancel.addr = READ_ONCE(sqe->addr);
5004 return 0;
5005}
5006
Pavel Begunkov014db002020-03-03 21:33:12 +03005007static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07005008{
5009 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005010
Pavel Begunkov014db002020-03-03 21:33:12 +03005011 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005012 return 0;
5013}
5014
Jens Axboe05f3fb32019-12-09 11:22:50 -07005015static int io_files_update_prep(struct io_kiocb *req,
5016 const struct io_uring_sqe *sqe)
5017{
5018 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
5019 return -EINVAL;
5020
5021 req->files_update.offset = READ_ONCE(sqe->off);
5022 req->files_update.nr_args = READ_ONCE(sqe->len);
5023 if (!req->files_update.nr_args)
5024 return -EINVAL;
5025 req->files_update.arg = READ_ONCE(sqe->addr);
5026 return 0;
5027}
5028
5029static int io_files_update(struct io_kiocb *req, bool force_nonblock)
5030{
5031 struct io_ring_ctx *ctx = req->ctx;
5032 struct io_uring_files_update up;
5033 int ret;
5034
Jens Axboef86cd202020-01-29 13:46:44 -07005035 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005036 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005037
5038 up.offset = req->files_update.offset;
5039 up.fds = req->files_update.arg;
5040
5041 mutex_lock(&ctx->uring_lock);
5042 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
5043 mutex_unlock(&ctx->uring_lock);
5044
5045 if (ret < 0)
5046 req_set_fail_links(req);
5047 io_cqring_add_event(req, ret);
5048 io_put_req(req);
5049 return 0;
5050}
5051
Jens Axboe3529d8c2019-12-19 18:24:38 -07005052static int io_req_defer_prep(struct io_kiocb *req,
5053 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005054{
Jens Axboee7815732019-12-17 19:45:06 -07005055 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005056
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005057 if (!sqe)
5058 return 0;
5059
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005060 io_req_init_async(req);
5061
Jens Axboef86cd202020-01-29 13:46:44 -07005062 if (io_op_defs[req->opcode].file_table) {
5063 ret = io_grab_files(req);
5064 if (unlikely(ret))
5065 return ret;
5066 }
5067
Jens Axboecccf0ee2020-01-27 16:34:48 -07005068 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
5069
Jens Axboed625c6e2019-12-17 19:53:05 -07005070 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005071 case IORING_OP_NOP:
5072 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005073 case IORING_OP_READV:
5074 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005075 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005076 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005077 break;
5078 case IORING_OP_WRITEV:
5079 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005080 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005081 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07005082 break;
Jens Axboe0969e782019-12-17 18:40:57 -07005083 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005084 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005085 break;
5086 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005087 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005088 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005089 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005090 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005091 break;
5092 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005093 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005094 break;
Jens Axboe03b12302019-12-02 18:50:25 -07005095 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005096 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005097 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005098 break;
5099 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005100 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005101 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005102 break;
Jens Axboef499a022019-12-02 16:28:46 -07005103 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005104 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005105 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005106 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005107 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005108 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07005109 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005110 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07005111 break;
Jens Axboefbf23842019-12-17 18:45:56 -07005112 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005113 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005114 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005115 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005116 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005117 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005118 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005119 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005120 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005121 case IORING_OP_FALLOCATE:
5122 ret = io_fallocate_prep(req, sqe);
5123 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005124 case IORING_OP_OPENAT:
5125 ret = io_openat_prep(req, sqe);
5126 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005127 case IORING_OP_CLOSE:
5128 ret = io_close_prep(req, sqe);
5129 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005130 case IORING_OP_FILES_UPDATE:
5131 ret = io_files_update_prep(req, sqe);
5132 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005133 case IORING_OP_STATX:
5134 ret = io_statx_prep(req, sqe);
5135 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005136 case IORING_OP_FADVISE:
5137 ret = io_fadvise_prep(req, sqe);
5138 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005139 case IORING_OP_MADVISE:
5140 ret = io_madvise_prep(req, sqe);
5141 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005142 case IORING_OP_OPENAT2:
5143 ret = io_openat2_prep(req, sqe);
5144 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005145 case IORING_OP_EPOLL_CTL:
5146 ret = io_epoll_ctl_prep(req, sqe);
5147 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005148 case IORING_OP_SPLICE:
5149 ret = io_splice_prep(req, sqe);
5150 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005151 case IORING_OP_PROVIDE_BUFFERS:
5152 ret = io_provide_buffers_prep(req, sqe);
5153 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005154 case IORING_OP_REMOVE_BUFFERS:
5155 ret = io_remove_buffers_prep(req, sqe);
5156 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005157 case IORING_OP_TEE:
5158 ret = io_tee_prep(req, sqe);
5159 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005160 default:
Jens Axboee7815732019-12-17 19:45:06 -07005161 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5162 req->opcode);
5163 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005164 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005165 }
5166
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005167 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005168}
5169
Jens Axboe3529d8c2019-12-19 18:24:38 -07005170static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005171{
Jackie Liua197f662019-11-08 08:09:12 -07005172 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005173 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005174
Bob Liu9d858b22019-11-13 18:06:25 +08005175 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005176 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005177 return 0;
5178
Pavel Begunkov650b5482020-05-17 14:02:11 +03005179 if (!req->io) {
5180 if (io_alloc_async_ctx(req))
5181 return -EAGAIN;
5182 ret = io_req_defer_prep(req, sqe);
5183 if (ret < 0)
5184 return ret;
5185 }
Jens Axboe2d283902019-12-04 11:08:05 -07005186
Jens Axboede0617e2019-04-06 21:51:27 -06005187 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005188 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005189 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005190 return 0;
5191 }
5192
Jens Axboe915967f2019-11-21 09:01:20 -07005193 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005194 list_add_tail(&req->list, &ctx->defer_list);
5195 spin_unlock_irq(&ctx->completion_lock);
5196 return -EIOCBQUEUED;
5197}
5198
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005199static void io_cleanup_req(struct io_kiocb *req)
5200{
5201 struct io_async_ctx *io = req->io;
5202
5203 switch (req->opcode) {
5204 case IORING_OP_READV:
5205 case IORING_OP_READ_FIXED:
5206 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005207 if (req->flags & REQ_F_BUFFER_SELECTED)
5208 kfree((void *)(unsigned long)req->rw.addr);
5209 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005210 case IORING_OP_WRITEV:
5211 case IORING_OP_WRITE_FIXED:
5212 case IORING_OP_WRITE:
5213 if (io->rw.iov != io->rw.fast_iov)
5214 kfree(io->rw.iov);
5215 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005216 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005217 if (req->flags & REQ_F_BUFFER_SELECTED)
5218 kfree(req->sr_msg.kbuf);
5219 /* fallthrough */
5220 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005221 if (io->msg.iov != io->msg.fast_iov)
5222 kfree(io->msg.iov);
5223 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005224 case IORING_OP_RECV:
5225 if (req->flags & REQ_F_BUFFER_SELECTED)
5226 kfree(req->sr_msg.kbuf);
5227 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005228 case IORING_OP_OPENAT:
5229 case IORING_OP_OPENAT2:
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005230 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005231 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005232 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005233 io_put_file(req, req->splice.file_in,
5234 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5235 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005236 }
5237
5238 req->flags &= ~REQ_F_NEED_CLEANUP;
5239}
5240
Jens Axboe3529d8c2019-12-19 18:24:38 -07005241static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005242 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005243{
Jackie Liua197f662019-11-08 08:09:12 -07005244 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005245 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005246
Jens Axboed625c6e2019-12-17 19:53:05 -07005247 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005248 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005249 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005250 break;
5251 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005252 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005253 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005254 if (sqe) {
5255 ret = io_read_prep(req, sqe, force_nonblock);
5256 if (ret < 0)
5257 break;
5258 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005259 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005260 break;
5261 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005262 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005263 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005264 if (sqe) {
5265 ret = io_write_prep(req, sqe, force_nonblock);
5266 if (ret < 0)
5267 break;
5268 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005269 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005270 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005271 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005272 if (sqe) {
5273 ret = io_prep_fsync(req, sqe);
5274 if (ret < 0)
5275 break;
5276 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005277 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005278 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005279 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005280 if (sqe) {
5281 ret = io_poll_add_prep(req, sqe);
5282 if (ret)
5283 break;
5284 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005285 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005286 break;
5287 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005288 if (sqe) {
5289 ret = io_poll_remove_prep(req, sqe);
5290 if (ret < 0)
5291 break;
5292 }
Jens Axboefc4df992019-12-10 14:38:45 -07005293 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005294 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005295 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005296 if (sqe) {
5297 ret = io_prep_sfr(req, sqe);
5298 if (ret < 0)
5299 break;
5300 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005301 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005302 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005303 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005304 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005305 if (sqe) {
5306 ret = io_sendmsg_prep(req, sqe);
5307 if (ret < 0)
5308 break;
5309 }
Jens Axboefddafac2020-01-04 20:19:44 -07005310 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005311 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005312 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005313 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005314 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005315 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005316 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005317 if (sqe) {
5318 ret = io_recvmsg_prep(req, sqe);
5319 if (ret)
5320 break;
5321 }
Jens Axboefddafac2020-01-04 20:19:44 -07005322 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005323 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005324 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005325 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005326 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005327 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005328 if (sqe) {
5329 ret = io_timeout_prep(req, sqe, false);
5330 if (ret)
5331 break;
5332 }
Jens Axboefc4df992019-12-10 14:38:45 -07005333 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005334 break;
Jens Axboe11365042019-10-16 09:08:32 -06005335 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005336 if (sqe) {
5337 ret = io_timeout_remove_prep(req, sqe);
5338 if (ret)
5339 break;
5340 }
Jens Axboefc4df992019-12-10 14:38:45 -07005341 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005342 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005343 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005344 if (sqe) {
5345 ret = io_accept_prep(req, sqe);
5346 if (ret)
5347 break;
5348 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005349 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005350 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005351 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005352 if (sqe) {
5353 ret = io_connect_prep(req, sqe);
5354 if (ret)
5355 break;
5356 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005357 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005358 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005359 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005360 if (sqe) {
5361 ret = io_async_cancel_prep(req, sqe);
5362 if (ret)
5363 break;
5364 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005365 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005366 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005367 case IORING_OP_FALLOCATE:
5368 if (sqe) {
5369 ret = io_fallocate_prep(req, sqe);
5370 if (ret)
5371 break;
5372 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005373 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005374 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005375 case IORING_OP_OPENAT:
5376 if (sqe) {
5377 ret = io_openat_prep(req, sqe);
5378 if (ret)
5379 break;
5380 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005381 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005382 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005383 case IORING_OP_CLOSE:
5384 if (sqe) {
5385 ret = io_close_prep(req, sqe);
5386 if (ret)
5387 break;
5388 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005389 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005390 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005391 case IORING_OP_FILES_UPDATE:
5392 if (sqe) {
5393 ret = io_files_update_prep(req, sqe);
5394 if (ret)
5395 break;
5396 }
5397 ret = io_files_update(req, force_nonblock);
5398 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005399 case IORING_OP_STATX:
5400 if (sqe) {
5401 ret = io_statx_prep(req, sqe);
5402 if (ret)
5403 break;
5404 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005405 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005406 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005407 case IORING_OP_FADVISE:
5408 if (sqe) {
5409 ret = io_fadvise_prep(req, sqe);
5410 if (ret)
5411 break;
5412 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005413 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005414 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005415 case IORING_OP_MADVISE:
5416 if (sqe) {
5417 ret = io_madvise_prep(req, sqe);
5418 if (ret)
5419 break;
5420 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005421 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005422 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005423 case IORING_OP_OPENAT2:
5424 if (sqe) {
5425 ret = io_openat2_prep(req, sqe);
5426 if (ret)
5427 break;
5428 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005429 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005430 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005431 case IORING_OP_EPOLL_CTL:
5432 if (sqe) {
5433 ret = io_epoll_ctl_prep(req, sqe);
5434 if (ret)
5435 break;
5436 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005437 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005438 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005439 case IORING_OP_SPLICE:
5440 if (sqe) {
5441 ret = io_splice_prep(req, sqe);
5442 if (ret < 0)
5443 break;
5444 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005445 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005446 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005447 case IORING_OP_PROVIDE_BUFFERS:
5448 if (sqe) {
5449 ret = io_provide_buffers_prep(req, sqe);
5450 if (ret)
5451 break;
5452 }
5453 ret = io_provide_buffers(req, force_nonblock);
5454 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005455 case IORING_OP_REMOVE_BUFFERS:
5456 if (sqe) {
5457 ret = io_remove_buffers_prep(req, sqe);
5458 if (ret)
5459 break;
5460 }
5461 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005462 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005463 case IORING_OP_TEE:
5464 if (sqe) {
5465 ret = io_tee_prep(req, sqe);
5466 if (ret < 0)
5467 break;
5468 }
5469 ret = io_tee(req, force_nonblock);
5470 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005471 default:
5472 ret = -EINVAL;
5473 break;
5474 }
5475
5476 if (ret)
5477 return ret;
5478
Jens Axboeb5325762020-05-19 21:20:27 -06005479 /* If the op doesn't have a file, we're not polling for it */
5480 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005481 const bool in_async = io_wq_current_is_worker();
5482
Jens Axboe9e645e112019-05-10 16:07:28 -06005483 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005484 return -EAGAIN;
5485
Jens Axboe11ba8202020-01-15 21:51:17 -07005486 /* workqueue context doesn't hold uring_lock, grab it now */
5487 if (in_async)
5488 mutex_lock(&ctx->uring_lock);
5489
Jens Axboe2b188cc2019-01-07 10:46:33 -07005490 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005491
5492 if (in_async)
5493 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005494 }
5495
5496 return 0;
5497}
5498
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005499static void io_arm_async_linked_timeout(struct io_kiocb *req)
5500{
5501 struct io_kiocb *link;
5502
5503 /* link head's timeout is queued in io_queue_async_work() */
5504 if (!(req->flags & REQ_F_QUEUE_TIMEOUT))
5505 return;
5506
5507 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
5508 io_queue_linked_timeout(link);
5509}
5510
Jens Axboe561fb042019-10-24 07:25:42 -06005511static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboedef596e2019-01-09 08:59:42 -07005512{
Jens Axboe561fb042019-10-24 07:25:42 -06005513 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005514 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005515 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005516
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005517 io_arm_async_linked_timeout(req);
5518
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005519 /* if NO_CANCEL is set, we must still run the work */
5520 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5521 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005522 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005523 }
Jens Axboe31b51512019-01-18 22:56:34 -07005524
Jens Axboe561fb042019-10-24 07:25:42 -06005525 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005526 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005527 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005528 /*
5529 * We can get EAGAIN for polled IO even though we're
5530 * forcing a sync submission from here, since we can't
5531 * wait for request slots on the block side.
5532 */
5533 if (ret != -EAGAIN)
5534 break;
5535 cond_resched();
5536 } while (1);
5537 }
Jens Axboe31b51512019-01-18 22:56:34 -07005538
Jens Axboe561fb042019-10-24 07:25:42 -06005539 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005540 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005541 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005542 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005543 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005544
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005545 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005546}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005547
Jens Axboe65e19f52019-10-26 07:20:21 -06005548static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5549 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005550{
Jens Axboe65e19f52019-10-26 07:20:21 -06005551 struct fixed_file_table *table;
5552
Jens Axboe05f3fb32019-12-09 11:22:50 -07005553 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005554 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005555}
5556
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005557static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5558 int fd, struct file **out_file, bool fixed)
5559{
5560 struct io_ring_ctx *ctx = req->ctx;
5561 struct file *file;
5562
5563 if (fixed) {
5564 if (unlikely(!ctx->file_data ||
5565 (unsigned) fd >= ctx->nr_user_files))
5566 return -EBADF;
5567 fd = array_index_nospec(fd, ctx->nr_user_files);
5568 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005569 if (file) {
5570 req->fixed_file_refs = ctx->file_data->cur_refs;
5571 percpu_ref_get(req->fixed_file_refs);
5572 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005573 } else {
5574 trace_io_uring_file_get(ctx, fd);
5575 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005576 }
5577
Jens Axboefd2206e2020-06-02 16:40:47 -06005578 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5579 *out_file = file;
5580 return 0;
5581 }
5582 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005583}
5584
Jens Axboe3529d8c2019-12-19 18:24:38 -07005585static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005586 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005587{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005588 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005589
Jens Axboe63ff8222020-05-07 14:56:15 -06005590 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005591 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005592 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005593
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005594 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005595}
5596
Jackie Liua197f662019-11-08 08:09:12 -07005597static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005598{
Jens Axboefcb323c2019-10-24 12:39:47 -06005599 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005600 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005601
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005602 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005603 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005604 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005605 return -EBADF;
5606
Jens Axboefcb323c2019-10-24 12:39:47 -06005607 rcu_read_lock();
5608 spin_lock_irq(&ctx->inflight_lock);
5609 /*
5610 * We use the f_ops->flush() handler to ensure that we can flush
5611 * out work accessing these files if the fd is closed. Check if
5612 * the fd has changed since we started down this path, and disallow
5613 * this operation if it has.
5614 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005615 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005616 list_add(&req->inflight_entry, &ctx->inflight_list);
5617 req->flags |= REQ_F_INFLIGHT;
5618 req->work.files = current->files;
5619 ret = 0;
5620 }
5621 spin_unlock_irq(&ctx->inflight_lock);
5622 rcu_read_unlock();
5623
5624 return ret;
5625}
5626
Jens Axboe2665abf2019-11-05 12:40:47 -07005627static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5628{
Jens Axboead8a48a2019-11-15 08:49:11 -07005629 struct io_timeout_data *data = container_of(timer,
5630 struct io_timeout_data, timer);
5631 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005632 struct io_ring_ctx *ctx = req->ctx;
5633 struct io_kiocb *prev = NULL;
5634 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005635
5636 spin_lock_irqsave(&ctx->completion_lock, flags);
5637
5638 /*
5639 * We don't expect the list to be empty, that will only happen if we
5640 * race with the completion of the linked work.
5641 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005642 if (!list_empty(&req->link_list)) {
5643 prev = list_entry(req->link_list.prev, struct io_kiocb,
5644 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005645 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005646 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005647 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5648 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005649 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005650 }
5651
5652 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5653
5654 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005655 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005656 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005657 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005658 } else {
5659 io_cqring_add_event(req, -ETIME);
5660 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005661 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005662 return HRTIMER_NORESTART;
5663}
5664
Jens Axboead8a48a2019-11-15 08:49:11 -07005665static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005666{
Jens Axboe76a46e02019-11-10 23:34:16 -07005667 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005668
Jens Axboe76a46e02019-11-10 23:34:16 -07005669 /*
5670 * If the list is now empty, then our linked request finished before
5671 * we got a chance to setup the timer
5672 */
5673 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005674 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005675 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005676
Jens Axboead8a48a2019-11-15 08:49:11 -07005677 data->timer.function = io_link_timeout_fn;
5678 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5679 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005680 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005681 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005682
Jens Axboe2665abf2019-11-05 12:40:47 -07005683 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005684 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005685}
5686
Jens Axboead8a48a2019-11-15 08:49:11 -07005687static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005688{
5689 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005690
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005691 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005692 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005693 /* for polled retry, if flag is set, we already went through here */
5694 if (req->flags & REQ_F_POLLED)
5695 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005696
Pavel Begunkov44932332019-12-05 16:16:35 +03005697 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5698 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005699 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005700 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005701
Jens Axboe76a46e02019-11-10 23:34:16 -07005702 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005703 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005704}
5705
Jens Axboe3529d8c2019-12-19 18:24:38 -07005706static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005707{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005708 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005709 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005710 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005711 int ret;
5712
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005713again:
5714 linked_timeout = io_prep_linked_timeout(req);
5715
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005716 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5717 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005718 if (old_creds)
5719 revert_creds(old_creds);
5720 if (old_creds == req->work.creds)
5721 old_creds = NULL; /* restored original creds */
5722 else
5723 old_creds = override_creds(req->work.creds);
5724 }
5725
Pavel Begunkov014db002020-03-03 21:33:12 +03005726 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005727
5728 /*
5729 * We async punt it if the file wasn't marked NOWAIT, or if the file
5730 * doesn't support non-blocking read/write attempts
5731 */
5732 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5733 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005734 if (io_arm_poll_handler(req)) {
5735 if (linked_timeout)
5736 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005737 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005738 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005739punt:
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005740 io_req_init_async(req);
5741
Jens Axboef86cd202020-01-29 13:46:44 -07005742 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005743 ret = io_grab_files(req);
5744 if (ret)
5745 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005746 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005747
5748 /*
5749 * Queued up for async execution, worker will release
5750 * submit reference when the iocb is actually submitted.
5751 */
5752 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005753 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005754 }
Jens Axboee65ef562019-03-12 10:16:44 -06005755
Jens Axboefcb323c2019-10-24 12:39:47 -06005756err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005757 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005758 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005759 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005760
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005761 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005762 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005763 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005764 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005765 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005766 }
5767
Jens Axboee65ef562019-03-12 10:16:44 -06005768 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005769 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005770 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005771 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005772 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005773 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005774 if (nxt) {
5775 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005776
5777 if (req->flags & REQ_F_FORCE_ASYNC)
5778 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005779 goto again;
5780 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005781exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005782 if (old_creds)
5783 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005784}
5785
Jens Axboe3529d8c2019-12-19 18:24:38 -07005786static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005787{
5788 int ret;
5789
Jens Axboe3529d8c2019-12-19 18:24:38 -07005790 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005791 if (ret) {
5792 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005793fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005794 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005795 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005796 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005797 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005798 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005799 if (!req->io) {
5800 ret = -EAGAIN;
5801 if (io_alloc_async_ctx(req))
5802 goto fail_req;
5803 ret = io_req_defer_prep(req, sqe);
5804 if (unlikely(ret < 0))
5805 goto fail_req;
5806 }
5807
Jens Axboece35a472019-12-17 08:04:44 -07005808 /*
5809 * Never try inline submit of IOSQE_ASYNC is set, go straight
5810 * to async execution.
5811 */
5812 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5813 io_queue_async_work(req);
5814 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005815 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005816 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005817}
5818
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005819static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005820{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005821 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005822 io_cqring_add_event(req, -ECANCELED);
5823 io_double_put_req(req);
5824 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005825 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005826}
5827
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005828static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005829 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005830{
Jackie Liua197f662019-11-08 08:09:12 -07005831 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005832 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06005833
Jens Axboe9e645e112019-05-10 16:07:28 -06005834 /*
5835 * If we already have a head request, queue this one for async
5836 * submittal once the head completes. If we don't have a head but
5837 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5838 * submitted sync once the chain is complete. If none of those
5839 * conditions are true (normal request), then just queue it.
5840 */
5841 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005842 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005843
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005844 /*
5845 * Taking sequential execution of a link, draining both sides
5846 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5847 * requests in the link. So, it drains the head and the
5848 * next after the link request. The last one is done via
5849 * drain_next flag to persist the effect across calls.
5850 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005851 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03005852 head->flags |= REQ_F_IO_DRAIN;
5853 ctx->drain_next = 1;
5854 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005855 if (io_alloc_async_ctx(req))
5856 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06005857
Jens Axboe3529d8c2019-12-19 18:24:38 -07005858 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005859 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005860 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005861 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005862 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005863 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005864 trace_io_uring_link(ctx, req, head);
5865 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005866
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005867 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005868 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005869 io_queue_link_head(head);
5870 *link = NULL;
5871 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005872 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005873 if (unlikely(ctx->drain_next)) {
5874 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005875 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03005876 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005877 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005878 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03005879 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005880
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005881 if (io_alloc_async_ctx(req))
5882 return -EAGAIN;
5883
Pavel Begunkov711be032020-01-17 03:57:59 +03005884 ret = io_req_defer_prep(req, sqe);
5885 if (ret)
5886 req->flags |= REQ_F_FAIL_LINK;
5887 *link = req;
5888 } else {
5889 io_queue_sqe(req, sqe);
5890 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005891 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005892
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005893 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06005894}
5895
Jens Axboe9a56a232019-01-09 09:06:50 -07005896/*
5897 * Batched submission is done, ensure local IO is flushed out.
5898 */
5899static void io_submit_state_end(struct io_submit_state *state)
5900{
5901 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03005902 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005903 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005904 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005905}
5906
5907/*
5908 * Start submission side cache.
5909 */
5910static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005911 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005912{
5913 blk_start_plug(&state->plug);
Jens Axboeb63534c2020-06-04 11:28:00 -06005914#ifdef CONFIG_BLOCK
5915 state->plug.nowait = true;
5916#endif
Jens Axboe2579f912019-01-09 09:10:43 -07005917 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005918 state->file = NULL;
5919 state->ios_left = max_ios;
5920}
5921
Jens Axboe2b188cc2019-01-07 10:46:33 -07005922static void io_commit_sqring(struct io_ring_ctx *ctx)
5923{
Hristo Venev75b28af2019-08-26 17:23:46 +00005924 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005925
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005926 /*
5927 * Ensure any loads from the SQEs are done at this point,
5928 * since once we write the new head, the application could
5929 * write new data to them.
5930 */
5931 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005932}
5933
5934/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005935 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005936 * that is mapped by userspace. This means that care needs to be taken to
5937 * ensure that reads are stable, as we cannot rely on userspace always
5938 * being a good citizen. If members of the sqe are validated and then later
5939 * used, it's important that those reads are done through READ_ONCE() to
5940 * prevent a re-load down the line.
5941 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03005942static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005943{
Hristo Venev75b28af2019-08-26 17:23:46 +00005944 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005945 unsigned head;
5946
5947 /*
5948 * The cached sq head (or cq tail) serves two purposes:
5949 *
5950 * 1) allows us to batch the cost of updating the user visible
5951 * head updates.
5952 * 2) allows the kernel side to track the head on its own, even
5953 * though the application is the one updating it.
5954 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005955 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005956 if (likely(head < ctx->sq_entries))
5957 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07005958
5959 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06005960 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005961 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005962 return NULL;
5963}
5964
5965static inline void io_consume_sqe(struct io_ring_ctx *ctx)
5966{
5967 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005968}
5969
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005970#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5971 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5972 IOSQE_BUFFER_SELECT)
5973
5974static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
5975 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005976 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005977{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005978 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06005979 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005980
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005981 /*
5982 * All io need record the previous position, if LINK vs DARIN,
5983 * it can be used to mark the position of the first IO in the
5984 * link list.
5985 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03005986 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005987 req->opcode = READ_ONCE(sqe->opcode);
5988 req->user_data = READ_ONCE(sqe->user_data);
5989 req->io = NULL;
5990 req->file = NULL;
5991 req->ctx = ctx;
5992 req->flags = 0;
5993 /* one is dropped after submission, the other at completion */
5994 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03005995 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005996 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005997
5998 if (unlikely(req->opcode >= IORING_OP_LAST))
5999 return -EINVAL;
6000
Jens Axboe9d8426a2020-06-16 18:42:49 -06006001 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
6002 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006003
6004 sqe_flags = READ_ONCE(sqe->flags);
6005 /* enforce forwards compatibility on users */
6006 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
6007 return -EINVAL;
6008
6009 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6010 !io_op_defs[req->opcode].buffer_select)
6011 return -EOPNOTSUPP;
6012
6013 id = READ_ONCE(sqe->personality);
6014 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08006015 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006016 req->work.creds = idr_find(&ctx->personality_idr, id);
6017 if (unlikely(!req->work.creds))
6018 return -EINVAL;
6019 get_cred(req->work.creds);
6020 }
6021
6022 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03006023 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006024
Jens Axboe63ff8222020-05-07 14:56:15 -06006025 if (!io_op_defs[req->opcode].needs_file)
6026 return 0;
6027
6028 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006029}
6030
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006031static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006032 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006033{
Jens Axboeac8691c2020-06-01 08:30:41 -06006034 struct io_submit_state state;
Jens Axboe9e645e112019-05-10 16:07:28 -06006035 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06006036 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006037
Jens Axboec4a2ed72019-11-21 21:01:26 -07006038 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006039 if (test_bit(0, &ctx->sq_check_overflow)) {
6040 if (!list_empty(&ctx->cq_overflow_list) &&
6041 !io_cqring_overflow_flush(ctx, false))
6042 return -EBUSY;
6043 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006044
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006045 /* make sure SQ entry isn't read before tail */
6046 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006047
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006048 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6049 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006050
Jens Axboeac8691c2020-06-01 08:30:41 -06006051 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006052
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006053 ctx->ring_fd = ring_fd;
6054 ctx->ring_file = ring_file;
6055
Jens Axboe6c271ce2019-01-10 11:22:30 -07006056 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006057 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006058 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006059 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006060
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03006061 sqe = io_get_sqe(ctx);
6062 if (unlikely(!sqe)) {
6063 io_consume_sqe(ctx);
6064 break;
6065 }
Jens Axboeac8691c2020-06-01 08:30:41 -06006066 req = io_alloc_req(ctx, &state);
Pavel Begunkov196be952019-11-07 01:41:06 +03006067 if (unlikely(!req)) {
6068 if (!submitted)
6069 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006070 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006071 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006072
Jens Axboeac8691c2020-06-01 08:30:41 -06006073 err = io_init_req(ctx, req, sqe, &state);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006074 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07006075 /* will complete beyond this point, count as submitted */
6076 submitted++;
6077
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006078 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03006079fail_req:
6080 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07006081 io_double_put_req(req);
6082 break;
6083 }
6084
Jens Axboe354420f2020-01-08 18:55:15 -07006085 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03006086 true, io_async_submit(ctx));
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08006087 err = io_submit_sqe(req, sqe, &link);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006088 if (err)
6089 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006090 }
6091
Pavel Begunkov9466f432020-01-25 22:34:01 +03006092 if (unlikely(submitted != nr)) {
6093 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
6094
6095 percpu_ref_put_many(&ctx->refs, nr - ref_used);
6096 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006097 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006098 io_queue_link_head(link);
Jens Axboeac8691c2020-06-01 08:30:41 -06006099 io_submit_state_end(&state);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006100
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006101 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6102 io_commit_sqring(ctx);
6103
Jens Axboe6c271ce2019-01-10 11:22:30 -07006104 return submitted;
6105}
6106
6107static int io_sq_thread(void *data)
6108{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006109 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07006110 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006111 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006112 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006113 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006114
Jens Axboe0f158b42020-05-14 17:18:39 -06006115 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08006116
Jens Axboe181e4482019-11-25 08:52:30 -07006117 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006118
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006119 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006120 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006121 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006122
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006123 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006124 unsigned nr_events = 0;
6125
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006126 mutex_lock(&ctx->uring_lock);
6127 if (!list_empty(&ctx->poll_list))
6128 io_iopoll_getevents(ctx, &nr_events, 0);
6129 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006130 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006131 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006132 }
6133
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006134 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006135
6136 /*
6137 * If submit got -EBUSY, flag us as needing the application
6138 * to enter the kernel to reap and flush events.
6139 */
6140 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006141 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006142 * Drop cur_mm before scheduling, we can't hold it for
6143 * long periods (or over schedule()). Do this before
6144 * adding ourselves to the waitqueue, as the unuse/drop
6145 * may sleep.
6146 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006147 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006148
6149 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006150 * We're polling. If we're within the defined idle
6151 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006152 * to sleep. The exception is if we got EBUSY doing
6153 * more IO, we should wait for the application to
6154 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006155 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006156 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07006157 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6158 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006159 if (current->task_works)
6160 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006161 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006162 continue;
6163 }
6164
Jens Axboe6c271ce2019-01-10 11:22:30 -07006165 prepare_to_wait(&ctx->sqo_wait, &wait,
6166 TASK_INTERRUPTIBLE);
6167
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006168 /*
6169 * While doing polled IO, before going to sleep, we need
6170 * to check if there are new reqs added to poll_list, it
6171 * is because reqs may have been punted to io worker and
6172 * will be added to poll_list later, hence check the
6173 * poll_list again.
6174 */
6175 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6176 !list_empty_careful(&ctx->poll_list)) {
6177 finish_wait(&ctx->sqo_wait, &wait);
6178 continue;
6179 }
6180
Jens Axboe6c271ce2019-01-10 11:22:30 -07006181 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006182 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006183 /* make sure to read SQ tail after writing flags */
6184 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006185
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006186 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006187 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006188 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006189 finish_wait(&ctx->sqo_wait, &wait);
6190 break;
6191 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006192 if (current->task_works) {
6193 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006194 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006195 continue;
6196 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006197 if (signal_pending(current))
6198 flush_signals(current);
6199 schedule();
6200 finish_wait(&ctx->sqo_wait, &wait);
6201
Hristo Venev75b28af2019-08-26 17:23:46 +00006202 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006203 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006204 continue;
6205 }
6206 finish_wait(&ctx->sqo_wait, &wait);
6207
Hristo Venev75b28af2019-08-26 17:23:46 +00006208 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006209 }
6210
Jens Axboe8a4955f2019-12-09 14:52:35 -07006211 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006212 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6213 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006214 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006215 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006216 }
6217
Jens Axboeb41e9852020-02-17 09:52:41 -07006218 if (current->task_works)
6219 task_work_run();
6220
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006221 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006222 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006223
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006224 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006225
Jens Axboe6c271ce2019-01-10 11:22:30 -07006226 return 0;
6227}
6228
Jens Axboebda52162019-09-24 13:47:15 -06006229struct io_wait_queue {
6230 struct wait_queue_entry wq;
6231 struct io_ring_ctx *ctx;
6232 unsigned to_wait;
6233 unsigned nr_timeouts;
6234};
6235
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006236static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006237{
6238 struct io_ring_ctx *ctx = iowq->ctx;
6239
6240 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006241 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006242 * started waiting. For timeouts, we always want to return to userspace,
6243 * regardless of event count.
6244 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006245 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006246 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6247}
6248
6249static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6250 int wake_flags, void *key)
6251{
6252 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6253 wq);
6254
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006255 /* use noflush == true, as we can't safely rely on locking context */
6256 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006257 return -1;
6258
6259 return autoremove_wake_function(curr, mode, wake_flags, key);
6260}
6261
Jens Axboe2b188cc2019-01-07 10:46:33 -07006262/*
6263 * Wait until events become available, if we don't already have some. The
6264 * application must reap them itself, as they reside on the shared cq ring.
6265 */
6266static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6267 const sigset_t __user *sig, size_t sigsz)
6268{
Jens Axboebda52162019-09-24 13:47:15 -06006269 struct io_wait_queue iowq = {
6270 .wq = {
6271 .private = current,
6272 .func = io_wake_function,
6273 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6274 },
6275 .ctx = ctx,
6276 .to_wait = min_events,
6277 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006278 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006279 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006280
Jens Axboeb41e9852020-02-17 09:52:41 -07006281 do {
6282 if (io_cqring_events(ctx, false) >= min_events)
6283 return 0;
6284 if (!current->task_works)
6285 break;
6286 task_work_run();
6287 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006288
6289 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006290#ifdef CONFIG_COMPAT
6291 if (in_compat_syscall())
6292 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006293 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006294 else
6295#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006296 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006297
Jens Axboe2b188cc2019-01-07 10:46:33 -07006298 if (ret)
6299 return ret;
6300 }
6301
Jens Axboebda52162019-09-24 13:47:15 -06006302 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006303 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006304 do {
6305 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6306 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006307 if (current->task_works)
6308 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006309 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006310 break;
6311 schedule();
6312 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006313 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006314 break;
6315 }
6316 } while (1);
6317 finish_wait(&ctx->wait, &iowq.wq);
6318
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006319 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006320
Hristo Venev75b28af2019-08-26 17:23:46 +00006321 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006322}
6323
Jens Axboe6b063142019-01-10 22:13:58 -07006324static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6325{
6326#if defined(CONFIG_UNIX)
6327 if (ctx->ring_sock) {
6328 struct sock *sock = ctx->ring_sock->sk;
6329 struct sk_buff *skb;
6330
6331 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6332 kfree_skb(skb);
6333 }
6334#else
6335 int i;
6336
Jens Axboe65e19f52019-10-26 07:20:21 -06006337 for (i = 0; i < ctx->nr_user_files; i++) {
6338 struct file *file;
6339
6340 file = io_file_from_index(ctx, i);
6341 if (file)
6342 fput(file);
6343 }
Jens Axboe6b063142019-01-10 22:13:58 -07006344#endif
6345}
6346
Jens Axboe05f3fb32019-12-09 11:22:50 -07006347static void io_file_ref_kill(struct percpu_ref *ref)
6348{
6349 struct fixed_file_data *data;
6350
6351 data = container_of(ref, struct fixed_file_data, refs);
6352 complete(&data->done);
6353}
6354
Jens Axboe6b063142019-01-10 22:13:58 -07006355static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6356{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006357 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006358 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006359 unsigned nr_tables, i;
6360
Jens Axboe05f3fb32019-12-09 11:22:50 -07006361 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006362 return -ENXIO;
6363
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006364 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006365 if (!list_empty(&data->ref_list))
6366 ref_node = list_first_entry(&data->ref_list,
6367 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006368 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006369 if (ref_node)
6370 percpu_ref_kill(&ref_node->refs);
6371
6372 percpu_ref_kill(&data->refs);
6373
6374 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006375 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006376 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006377
Jens Axboe6b063142019-01-10 22:13:58 -07006378 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006379 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6380 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006381 kfree(data->table[i].files);
6382 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006383 percpu_ref_exit(&data->refs);
6384 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006385 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006386 ctx->nr_user_files = 0;
6387 return 0;
6388}
6389
Jens Axboe6c271ce2019-01-10 11:22:30 -07006390static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6391{
6392 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006393 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006394 /*
6395 * The park is a bit of a work-around, without it we get
6396 * warning spews on shutdown with SQPOLL set and affinity
6397 * set to a single CPU.
6398 */
Jens Axboe06058632019-04-13 09:26:03 -06006399 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006400 kthread_stop(ctx->sqo_thread);
6401 ctx->sqo_thread = NULL;
6402 }
6403}
6404
Jens Axboe6b063142019-01-10 22:13:58 -07006405static void io_finish_async(struct io_ring_ctx *ctx)
6406{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006407 io_sq_thread_stop(ctx);
6408
Jens Axboe561fb042019-10-24 07:25:42 -06006409 if (ctx->io_wq) {
6410 io_wq_destroy(ctx->io_wq);
6411 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006412 }
6413}
6414
6415#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006416/*
6417 * Ensure the UNIX gc is aware of our file set, so we are certain that
6418 * the io_uring can be safely unregistered on process exit, even if we have
6419 * loops in the file referencing.
6420 */
6421static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6422{
6423 struct sock *sk = ctx->ring_sock->sk;
6424 struct scm_fp_list *fpl;
6425 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006426 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006427
Jens Axboe6b063142019-01-10 22:13:58 -07006428 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6429 if (!fpl)
6430 return -ENOMEM;
6431
6432 skb = alloc_skb(0, GFP_KERNEL);
6433 if (!skb) {
6434 kfree(fpl);
6435 return -ENOMEM;
6436 }
6437
6438 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006439
Jens Axboe08a45172019-10-03 08:11:03 -06006440 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006441 fpl->user = get_uid(ctx->user);
6442 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006443 struct file *file = io_file_from_index(ctx, i + offset);
6444
6445 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006446 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006447 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006448 unix_inflight(fpl->user, fpl->fp[nr_files]);
6449 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006450 }
6451
Jens Axboe08a45172019-10-03 08:11:03 -06006452 if (nr_files) {
6453 fpl->max = SCM_MAX_FD;
6454 fpl->count = nr_files;
6455 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006456 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006457 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6458 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006459
Jens Axboe08a45172019-10-03 08:11:03 -06006460 for (i = 0; i < nr_files; i++)
6461 fput(fpl->fp[i]);
6462 } else {
6463 kfree_skb(skb);
6464 kfree(fpl);
6465 }
Jens Axboe6b063142019-01-10 22:13:58 -07006466
6467 return 0;
6468}
6469
6470/*
6471 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6472 * causes regular reference counting to break down. We rely on the UNIX
6473 * garbage collection to take care of this problem for us.
6474 */
6475static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6476{
6477 unsigned left, total;
6478 int ret = 0;
6479
6480 total = 0;
6481 left = ctx->nr_user_files;
6482 while (left) {
6483 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006484
6485 ret = __io_sqe_files_scm(ctx, this_files, total);
6486 if (ret)
6487 break;
6488 left -= this_files;
6489 total += this_files;
6490 }
6491
6492 if (!ret)
6493 return 0;
6494
6495 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006496 struct file *file = io_file_from_index(ctx, total);
6497
6498 if (file)
6499 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006500 total++;
6501 }
6502
6503 return ret;
6504}
6505#else
6506static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6507{
6508 return 0;
6509}
6510#endif
6511
Jens Axboe65e19f52019-10-26 07:20:21 -06006512static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6513 unsigned nr_files)
6514{
6515 int i;
6516
6517 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006518 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006519 unsigned this_files;
6520
6521 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6522 table->files = kcalloc(this_files, sizeof(struct file *),
6523 GFP_KERNEL);
6524 if (!table->files)
6525 break;
6526 nr_files -= this_files;
6527 }
6528
6529 if (i == nr_tables)
6530 return 0;
6531
6532 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006533 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006534 kfree(table->files);
6535 }
6536 return 1;
6537}
6538
Jens Axboe05f3fb32019-12-09 11:22:50 -07006539static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006540{
6541#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006542 struct sock *sock = ctx->ring_sock->sk;
6543 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6544 struct sk_buff *skb;
6545 int i;
6546
6547 __skb_queue_head_init(&list);
6548
6549 /*
6550 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6551 * remove this entry and rearrange the file array.
6552 */
6553 skb = skb_dequeue(head);
6554 while (skb) {
6555 struct scm_fp_list *fp;
6556
6557 fp = UNIXCB(skb).fp;
6558 for (i = 0; i < fp->count; i++) {
6559 int left;
6560
6561 if (fp->fp[i] != file)
6562 continue;
6563
6564 unix_notinflight(fp->user, fp->fp[i]);
6565 left = fp->count - 1 - i;
6566 if (left) {
6567 memmove(&fp->fp[i], &fp->fp[i + 1],
6568 left * sizeof(struct file *));
6569 }
6570 fp->count--;
6571 if (!fp->count) {
6572 kfree_skb(skb);
6573 skb = NULL;
6574 } else {
6575 __skb_queue_tail(&list, skb);
6576 }
6577 fput(file);
6578 file = NULL;
6579 break;
6580 }
6581
6582 if (!file)
6583 break;
6584
6585 __skb_queue_tail(&list, skb);
6586
6587 skb = skb_dequeue(head);
6588 }
6589
6590 if (skb_peek(&list)) {
6591 spin_lock_irq(&head->lock);
6592 while ((skb = __skb_dequeue(&list)) != NULL)
6593 __skb_queue_tail(head, skb);
6594 spin_unlock_irq(&head->lock);
6595 }
6596#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006597 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006598#endif
6599}
6600
Jens Axboe05f3fb32019-12-09 11:22:50 -07006601struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006602 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006603 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006604};
6605
Jens Axboe4a38aed22020-05-14 17:21:15 -06006606static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006607{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006608 struct fixed_file_data *file_data = ref_node->file_data;
6609 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006610 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006611
6612 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006613 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006614 io_ring_file_put(ctx, pfile->file);
6615 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006616 }
6617
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006618 spin_lock(&file_data->lock);
6619 list_del(&ref_node->node);
6620 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006621
Xiaoguang Wang05589552020-03-31 14:05:18 +08006622 percpu_ref_exit(&ref_node->refs);
6623 kfree(ref_node);
6624 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006625}
6626
Jens Axboe4a38aed22020-05-14 17:21:15 -06006627static void io_file_put_work(struct work_struct *work)
6628{
6629 struct io_ring_ctx *ctx;
6630 struct llist_node *node;
6631
6632 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6633 node = llist_del_all(&ctx->file_put_llist);
6634
6635 while (node) {
6636 struct fixed_file_ref_node *ref_node;
6637 struct llist_node *next = node->next;
6638
6639 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6640 __io_file_put_work(ref_node);
6641 node = next;
6642 }
6643}
6644
Jens Axboe05f3fb32019-12-09 11:22:50 -07006645static void io_file_data_ref_zero(struct percpu_ref *ref)
6646{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006647 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006648 struct io_ring_ctx *ctx;
6649 bool first_add;
6650 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006651
Xiaoguang Wang05589552020-03-31 14:05:18 +08006652 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006653 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006654
Jens Axboe4a38aed22020-05-14 17:21:15 -06006655 if (percpu_ref_is_dying(&ctx->file_data->refs))
6656 delay = 0;
6657
6658 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6659 if (!delay)
6660 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6661 else if (first_add)
6662 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006663}
6664
6665static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6666 struct io_ring_ctx *ctx)
6667{
6668 struct fixed_file_ref_node *ref_node;
6669
6670 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6671 if (!ref_node)
6672 return ERR_PTR(-ENOMEM);
6673
6674 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6675 0, GFP_KERNEL)) {
6676 kfree(ref_node);
6677 return ERR_PTR(-ENOMEM);
6678 }
6679 INIT_LIST_HEAD(&ref_node->node);
6680 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006681 ref_node->file_data = ctx->file_data;
6682 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006683}
6684
6685static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6686{
6687 percpu_ref_exit(&ref_node->refs);
6688 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006689}
6690
6691static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6692 unsigned nr_args)
6693{
6694 __s32 __user *fds = (__s32 __user *) arg;
6695 unsigned nr_tables;
6696 struct file *file;
6697 int fd, ret = 0;
6698 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006699 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006700
6701 if (ctx->file_data)
6702 return -EBUSY;
6703 if (!nr_args)
6704 return -EINVAL;
6705 if (nr_args > IORING_MAX_FIXED_FILES)
6706 return -EMFILE;
6707
6708 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6709 if (!ctx->file_data)
6710 return -ENOMEM;
6711 ctx->file_data->ctx = ctx;
6712 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006713 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006714 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006715
6716 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6717 ctx->file_data->table = kcalloc(nr_tables,
6718 sizeof(struct fixed_file_table),
6719 GFP_KERNEL);
6720 if (!ctx->file_data->table) {
6721 kfree(ctx->file_data);
6722 ctx->file_data = NULL;
6723 return -ENOMEM;
6724 }
6725
Xiaoguang Wang05589552020-03-31 14:05:18 +08006726 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006727 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6728 kfree(ctx->file_data->table);
6729 kfree(ctx->file_data);
6730 ctx->file_data = NULL;
6731 return -ENOMEM;
6732 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006733
6734 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6735 percpu_ref_exit(&ctx->file_data->refs);
6736 kfree(ctx->file_data->table);
6737 kfree(ctx->file_data);
6738 ctx->file_data = NULL;
6739 return -ENOMEM;
6740 }
6741
6742 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6743 struct fixed_file_table *table;
6744 unsigned index;
6745
6746 ret = -EFAULT;
6747 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6748 break;
6749 /* allow sparse sets */
6750 if (fd == -1) {
6751 ret = 0;
6752 continue;
6753 }
6754
6755 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6756 index = i & IORING_FILE_TABLE_MASK;
6757 file = fget(fd);
6758
6759 ret = -EBADF;
6760 if (!file)
6761 break;
6762
6763 /*
6764 * Don't allow io_uring instances to be registered. If UNIX
6765 * isn't enabled, then this causes a reference cycle and this
6766 * instance can never get freed. If UNIX is enabled we'll
6767 * handle it just fine, but there's still no point in allowing
6768 * a ring fd as it doesn't support regular read/write anyway.
6769 */
6770 if (file->f_op == &io_uring_fops) {
6771 fput(file);
6772 break;
6773 }
6774 ret = 0;
6775 table->files[index] = file;
6776 }
6777
6778 if (ret) {
6779 for (i = 0; i < ctx->nr_user_files; i++) {
6780 file = io_file_from_index(ctx, i);
6781 if (file)
6782 fput(file);
6783 }
6784 for (i = 0; i < nr_tables; i++)
6785 kfree(ctx->file_data->table[i].files);
6786
6787 kfree(ctx->file_data->table);
6788 kfree(ctx->file_data);
6789 ctx->file_data = NULL;
6790 ctx->nr_user_files = 0;
6791 return ret;
6792 }
6793
6794 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006795 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006796 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006797 return ret;
6798 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006799
Xiaoguang Wang05589552020-03-31 14:05:18 +08006800 ref_node = alloc_fixed_file_ref_node(ctx);
6801 if (IS_ERR(ref_node)) {
6802 io_sqe_files_unregister(ctx);
6803 return PTR_ERR(ref_node);
6804 }
6805
6806 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006807 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006808 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006809 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006810 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006811 return ret;
6812}
6813
Jens Axboec3a31e62019-10-03 13:59:56 -06006814static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6815 int index)
6816{
6817#if defined(CONFIG_UNIX)
6818 struct sock *sock = ctx->ring_sock->sk;
6819 struct sk_buff_head *head = &sock->sk_receive_queue;
6820 struct sk_buff *skb;
6821
6822 /*
6823 * See if we can merge this file into an existing skb SCM_RIGHTS
6824 * file set. If there's no room, fall back to allocating a new skb
6825 * and filling it in.
6826 */
6827 spin_lock_irq(&head->lock);
6828 skb = skb_peek(head);
6829 if (skb) {
6830 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6831
6832 if (fpl->count < SCM_MAX_FD) {
6833 __skb_unlink(skb, head);
6834 spin_unlock_irq(&head->lock);
6835 fpl->fp[fpl->count] = get_file(file);
6836 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6837 fpl->count++;
6838 spin_lock_irq(&head->lock);
6839 __skb_queue_head(head, skb);
6840 } else {
6841 skb = NULL;
6842 }
6843 }
6844 spin_unlock_irq(&head->lock);
6845
6846 if (skb) {
6847 fput(file);
6848 return 0;
6849 }
6850
6851 return __io_sqe_files_scm(ctx, 1, index);
6852#else
6853 return 0;
6854#endif
6855}
6856
Hillf Dantona5318d32020-03-23 17:47:15 +08006857static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08006858 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006859{
Hillf Dantona5318d32020-03-23 17:47:15 +08006860 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006861 struct percpu_ref *refs = data->cur_refs;
6862 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006863
Jens Axboe05f3fb32019-12-09 11:22:50 -07006864 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006865 if (!pfile)
6866 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006867
Xiaoguang Wang05589552020-03-31 14:05:18 +08006868 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006869 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006870 list_add(&pfile->list, &ref_node->file_list);
6871
Hillf Dantona5318d32020-03-23 17:47:15 +08006872 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006873}
6874
6875static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6876 struct io_uring_files_update *up,
6877 unsigned nr_args)
6878{
6879 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006880 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006881 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006882 __s32 __user *fds;
6883 int fd, i, err;
6884 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006885 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06006886
Jens Axboe05f3fb32019-12-09 11:22:50 -07006887 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006888 return -EOVERFLOW;
6889 if (done > ctx->nr_user_files)
6890 return -EINVAL;
6891
Xiaoguang Wang05589552020-03-31 14:05:18 +08006892 ref_node = alloc_fixed_file_ref_node(ctx);
6893 if (IS_ERR(ref_node))
6894 return PTR_ERR(ref_node);
6895
Jens Axboec3a31e62019-10-03 13:59:56 -06006896 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006897 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006898 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006899 struct fixed_file_table *table;
6900 unsigned index;
6901
Jens Axboec3a31e62019-10-03 13:59:56 -06006902 err = 0;
6903 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6904 err = -EFAULT;
6905 break;
6906 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006907 i = array_index_nospec(up->offset, ctx->nr_user_files);
6908 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006909 index = i & IORING_FILE_TABLE_MASK;
6910 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006911 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006912 err = io_queue_file_removal(data, file);
6913 if (err)
6914 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006915 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006916 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006917 }
6918 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006919 file = fget(fd);
6920 if (!file) {
6921 err = -EBADF;
6922 break;
6923 }
6924 /*
6925 * Don't allow io_uring instances to be registered. If
6926 * UNIX isn't enabled, then this causes a reference
6927 * cycle and this instance can never get freed. If UNIX
6928 * is enabled we'll handle it just fine, but there's
6929 * still no point in allowing a ring fd as it doesn't
6930 * support regular read/write anyway.
6931 */
6932 if (file->f_op == &io_uring_fops) {
6933 fput(file);
6934 err = -EBADF;
6935 break;
6936 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006937 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006938 err = io_sqe_file_register(ctx, file, i);
6939 if (err)
6940 break;
6941 }
6942 nr_args--;
6943 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006944 up->offset++;
6945 }
6946
Xiaoguang Wang05589552020-03-31 14:05:18 +08006947 if (needs_switch) {
6948 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006949 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006950 list_add(&ref_node->node, &data->ref_list);
6951 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006952 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006953 percpu_ref_get(&ctx->file_data->refs);
6954 } else
6955 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06006956
6957 return done ? done : err;
6958}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006959
Jens Axboe05f3fb32019-12-09 11:22:50 -07006960static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6961 unsigned nr_args)
6962{
6963 struct io_uring_files_update up;
6964
6965 if (!ctx->file_data)
6966 return -ENXIO;
6967 if (!nr_args)
6968 return -EINVAL;
6969 if (copy_from_user(&up, arg, sizeof(up)))
6970 return -EFAULT;
6971 if (up.resv)
6972 return -EINVAL;
6973
6974 return __io_sqe_files_update(ctx, &up, nr_args);
6975}
Jens Axboec3a31e62019-10-03 13:59:56 -06006976
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006977static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006978{
6979 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6980
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006981 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006982 io_put_req(req);
6983}
6984
Pavel Begunkov24369c22020-01-28 03:15:48 +03006985static int io_init_wq_offload(struct io_ring_ctx *ctx,
6986 struct io_uring_params *p)
6987{
6988 struct io_wq_data data;
6989 struct fd f;
6990 struct io_ring_ctx *ctx_attach;
6991 unsigned int concurrency;
6992 int ret = 0;
6993
6994 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006995 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03006996 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006997
6998 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6999 /* Do QD, or 4 * CPUS, whatever is smallest */
7000 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
7001
7002 ctx->io_wq = io_wq_create(concurrency, &data);
7003 if (IS_ERR(ctx->io_wq)) {
7004 ret = PTR_ERR(ctx->io_wq);
7005 ctx->io_wq = NULL;
7006 }
7007 return ret;
7008 }
7009
7010 f = fdget(p->wq_fd);
7011 if (!f.file)
7012 return -EBADF;
7013
7014 if (f.file->f_op != &io_uring_fops) {
7015 ret = -EINVAL;
7016 goto out_fput;
7017 }
7018
7019 ctx_attach = f.file->private_data;
7020 /* @io_wq is protected by holding the fd */
7021 if (!io_wq_get(ctx_attach->io_wq, &data)) {
7022 ret = -EINVAL;
7023 goto out_fput;
7024 }
7025
7026 ctx->io_wq = ctx_attach->io_wq;
7027out_fput:
7028 fdput(f);
7029 return ret;
7030}
7031
Jens Axboe6c271ce2019-01-10 11:22:30 -07007032static int io_sq_offload_start(struct io_ring_ctx *ctx,
7033 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007034{
7035 int ret;
7036
7037 mmgrab(current->mm);
7038 ctx->sqo_mm = current->mm;
7039
Jens Axboe6c271ce2019-01-10 11:22:30 -07007040 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06007041 ret = -EPERM;
7042 if (!capable(CAP_SYS_ADMIN))
7043 goto err;
7044
Jens Axboe917257d2019-04-13 09:28:55 -06007045 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7046 if (!ctx->sq_thread_idle)
7047 ctx->sq_thread_idle = HZ;
7048
Jens Axboe6c271ce2019-01-10 11:22:30 -07007049 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007050 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007051
Jens Axboe917257d2019-04-13 09:28:55 -06007052 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007053 if (cpu >= nr_cpu_ids)
7054 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007055 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007056 goto err;
7057
Jens Axboe6c271ce2019-01-10 11:22:30 -07007058 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
7059 ctx, cpu,
7060 "io_uring-sq");
7061 } else {
7062 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
7063 "io_uring-sq");
7064 }
7065 if (IS_ERR(ctx->sqo_thread)) {
7066 ret = PTR_ERR(ctx->sqo_thread);
7067 ctx->sqo_thread = NULL;
7068 goto err;
7069 }
7070 wake_up_process(ctx->sqo_thread);
7071 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7072 /* Can't have SQ_AFF without SQPOLL */
7073 ret = -EINVAL;
7074 goto err;
7075 }
7076
Pavel Begunkov24369c22020-01-28 03:15:48 +03007077 ret = io_init_wq_offload(ctx, p);
7078 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007079 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007080
7081 return 0;
7082err:
Jens Axboe54a91f32019-09-10 09:15:04 -06007083 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007084 mmdrop(ctx->sqo_mm);
7085 ctx->sqo_mm = NULL;
7086 return ret;
7087}
7088
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007089static inline void __io_unaccount_mem(struct user_struct *user,
7090 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007091{
7092 atomic_long_sub(nr_pages, &user->locked_vm);
7093}
7094
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007095static inline int __io_account_mem(struct user_struct *user,
7096 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007097{
7098 unsigned long page_limit, cur_pages, new_pages;
7099
7100 /* Don't allow more pages than we can safely lock */
7101 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
7102
7103 do {
7104 cur_pages = atomic_long_read(&user->locked_vm);
7105 new_pages = cur_pages + nr_pages;
7106 if (new_pages > page_limit)
7107 return -ENOMEM;
7108 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
7109 new_pages) != cur_pages);
7110
7111 return 0;
7112}
7113
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007114static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7115 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007116{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007117 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007118 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007119
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007120 if (ctx->sqo_mm) {
7121 if (acct == ACCT_LOCKED)
7122 ctx->sqo_mm->locked_vm -= nr_pages;
7123 else if (acct == ACCT_PINNED)
7124 atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7125 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007126}
7127
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007128static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7129 enum io_mem_account acct)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007130{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007131 int ret;
7132
7133 if (ctx->limit_mem) {
7134 ret = __io_account_mem(ctx->user, nr_pages);
7135 if (ret)
7136 return ret;
7137 }
7138
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007139 if (ctx->sqo_mm) {
7140 if (acct == ACCT_LOCKED)
7141 ctx->sqo_mm->locked_vm += nr_pages;
7142 else if (acct == ACCT_PINNED)
7143 atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7144 }
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007145
7146 return 0;
7147}
7148
Jens Axboe2b188cc2019-01-07 10:46:33 -07007149static void io_mem_free(void *ptr)
7150{
Mark Rutland52e04ef2019-04-30 17:30:21 +01007151 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007152
Mark Rutland52e04ef2019-04-30 17:30:21 +01007153 if (!ptr)
7154 return;
7155
7156 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007157 if (put_page_testzero(page))
7158 free_compound_page(page);
7159}
7160
7161static void *io_mem_alloc(size_t size)
7162{
7163 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7164 __GFP_NORETRY;
7165
7166 return (void *) __get_free_pages(gfp_flags, get_order(size));
7167}
7168
Hristo Venev75b28af2019-08-26 17:23:46 +00007169static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7170 size_t *sq_offset)
7171{
7172 struct io_rings *rings;
7173 size_t off, sq_array_size;
7174
7175 off = struct_size(rings, cqes, cq_entries);
7176 if (off == SIZE_MAX)
7177 return SIZE_MAX;
7178
7179#ifdef CONFIG_SMP
7180 off = ALIGN(off, SMP_CACHE_BYTES);
7181 if (off == 0)
7182 return SIZE_MAX;
7183#endif
7184
7185 sq_array_size = array_size(sizeof(u32), sq_entries);
7186 if (sq_array_size == SIZE_MAX)
7187 return SIZE_MAX;
7188
7189 if (check_add_overflow(off, sq_array_size, &off))
7190 return SIZE_MAX;
7191
7192 if (sq_offset)
7193 *sq_offset = off;
7194
7195 return off;
7196}
7197
Jens Axboe2b188cc2019-01-07 10:46:33 -07007198static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7199{
Hristo Venev75b28af2019-08-26 17:23:46 +00007200 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007201
Hristo Venev75b28af2019-08-26 17:23:46 +00007202 pages = (size_t)1 << get_order(
7203 rings_size(sq_entries, cq_entries, NULL));
7204 pages += (size_t)1 << get_order(
7205 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007206
Hristo Venev75b28af2019-08-26 17:23:46 +00007207 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007208}
7209
Jens Axboeedafcce2019-01-09 09:16:05 -07007210static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7211{
7212 int i, j;
7213
7214 if (!ctx->user_bufs)
7215 return -ENXIO;
7216
7217 for (i = 0; i < ctx->nr_user_bufs; i++) {
7218 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7219
7220 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007221 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007222
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007223 io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007224 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007225 imu->nr_bvecs = 0;
7226 }
7227
7228 kfree(ctx->user_bufs);
7229 ctx->user_bufs = NULL;
7230 ctx->nr_user_bufs = 0;
7231 return 0;
7232}
7233
7234static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7235 void __user *arg, unsigned index)
7236{
7237 struct iovec __user *src;
7238
7239#ifdef CONFIG_COMPAT
7240 if (ctx->compat) {
7241 struct compat_iovec __user *ciovs;
7242 struct compat_iovec ciov;
7243
7244 ciovs = (struct compat_iovec __user *) arg;
7245 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7246 return -EFAULT;
7247
Jens Axboed55e5f52019-12-11 16:12:15 -07007248 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007249 dst->iov_len = ciov.iov_len;
7250 return 0;
7251 }
7252#endif
7253 src = (struct iovec __user *) arg;
7254 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7255 return -EFAULT;
7256 return 0;
7257}
7258
7259static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7260 unsigned nr_args)
7261{
7262 struct vm_area_struct **vmas = NULL;
7263 struct page **pages = NULL;
7264 int i, j, got_pages = 0;
7265 int ret = -EINVAL;
7266
7267 if (ctx->user_bufs)
7268 return -EBUSY;
7269 if (!nr_args || nr_args > UIO_MAXIOV)
7270 return -EINVAL;
7271
7272 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7273 GFP_KERNEL);
7274 if (!ctx->user_bufs)
7275 return -ENOMEM;
7276
7277 for (i = 0; i < nr_args; i++) {
7278 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7279 unsigned long off, start, end, ubuf;
7280 int pret, nr_pages;
7281 struct iovec iov;
7282 size_t size;
7283
7284 ret = io_copy_iov(ctx, &iov, arg, i);
7285 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007286 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007287
7288 /*
7289 * Don't impose further limits on the size and buffer
7290 * constraints here, we'll -EINVAL later when IO is
7291 * submitted if they are wrong.
7292 */
7293 ret = -EFAULT;
7294 if (!iov.iov_base || !iov.iov_len)
7295 goto err;
7296
7297 /* arbitrary limit, but we need something */
7298 if (iov.iov_len > SZ_1G)
7299 goto err;
7300
7301 ubuf = (unsigned long) iov.iov_base;
7302 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7303 start = ubuf >> PAGE_SHIFT;
7304 nr_pages = end - start;
7305
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007306 ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07007307 if (ret)
7308 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007309
7310 ret = 0;
7311 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007312 kvfree(vmas);
7313 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007314 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007315 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007316 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007317 sizeof(struct vm_area_struct *),
7318 GFP_KERNEL);
7319 if (!pages || !vmas) {
7320 ret = -ENOMEM;
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007321 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007322 goto err;
7323 }
7324 got_pages = nr_pages;
7325 }
7326
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007327 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007328 GFP_KERNEL);
7329 ret = -ENOMEM;
7330 if (!imu->bvec) {
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007331 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Jens Axboeedafcce2019-01-09 09:16:05 -07007332 goto err;
7333 }
7334
7335 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007336 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007337 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007338 FOLL_WRITE | FOLL_LONGTERM,
7339 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007340 if (pret == nr_pages) {
7341 /* don't support file backed memory */
7342 for (j = 0; j < nr_pages; j++) {
7343 struct vm_area_struct *vma = vmas[j];
7344
7345 if (vma->vm_file &&
7346 !is_file_hugepages(vma->vm_file)) {
7347 ret = -EOPNOTSUPP;
7348 break;
7349 }
7350 }
7351 } else {
7352 ret = pret < 0 ? pret : -EFAULT;
7353 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007354 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007355 if (ret) {
7356 /*
7357 * if we did partial map, or found file backed vmas,
7358 * release any pages we did get
7359 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007360 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007361 unpin_user_pages(pages, pret);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007362 io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007363 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007364 goto err;
7365 }
7366
7367 off = ubuf & ~PAGE_MASK;
7368 size = iov.iov_len;
7369 for (j = 0; j < nr_pages; j++) {
7370 size_t vec_len;
7371
7372 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7373 imu->bvec[j].bv_page = pages[j];
7374 imu->bvec[j].bv_len = vec_len;
7375 imu->bvec[j].bv_offset = off;
7376 off = 0;
7377 size -= vec_len;
7378 }
7379 /* store original address for later verification */
7380 imu->ubuf = ubuf;
7381 imu->len = iov.iov_len;
7382 imu->nr_bvecs = nr_pages;
7383
7384 ctx->nr_user_bufs++;
7385 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007386 kvfree(pages);
7387 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007388 return 0;
7389err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007390 kvfree(pages);
7391 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007392 io_sqe_buffer_unregister(ctx);
7393 return ret;
7394}
7395
Jens Axboe9b402842019-04-11 11:45:41 -06007396static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7397{
7398 __s32 __user *fds = arg;
7399 int fd;
7400
7401 if (ctx->cq_ev_fd)
7402 return -EBUSY;
7403
7404 if (copy_from_user(&fd, fds, sizeof(*fds)))
7405 return -EFAULT;
7406
7407 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7408 if (IS_ERR(ctx->cq_ev_fd)) {
7409 int ret = PTR_ERR(ctx->cq_ev_fd);
7410 ctx->cq_ev_fd = NULL;
7411 return ret;
7412 }
7413
7414 return 0;
7415}
7416
7417static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7418{
7419 if (ctx->cq_ev_fd) {
7420 eventfd_ctx_put(ctx->cq_ev_fd);
7421 ctx->cq_ev_fd = NULL;
7422 return 0;
7423 }
7424
7425 return -ENXIO;
7426}
7427
Jens Axboe5a2e7452020-02-23 16:23:11 -07007428static int __io_destroy_buffers(int id, void *p, void *data)
7429{
7430 struct io_ring_ctx *ctx = data;
7431 struct io_buffer *buf = p;
7432
Jens Axboe067524e2020-03-02 16:32:28 -07007433 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007434 return 0;
7435}
7436
7437static void io_destroy_buffers(struct io_ring_ctx *ctx)
7438{
7439 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7440 idr_destroy(&ctx->io_buffer_idr);
7441}
7442
Jens Axboe2b188cc2019-01-07 10:46:33 -07007443static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7444{
Jens Axboe6b063142019-01-10 22:13:58 -07007445 io_finish_async(ctx);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007446 if (ctx->sqo_mm) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007447 mmdrop(ctx->sqo_mm);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07007448 ctx->sqo_mm = NULL;
7449 }
Jens Axboedef596e2019-01-09 08:59:42 -07007450
7451 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007452 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007453 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007454 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007455 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007456 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007457
Jens Axboe2b188cc2019-01-07 10:46:33 -07007458#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007459 if (ctx->ring_sock) {
7460 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007461 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007462 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007463#endif
7464
Hristo Venev75b28af2019-08-26 17:23:46 +00007465 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007466 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007467
7468 percpu_ref_exit(&ctx->refs);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07007469 io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7470 ACCT_LOCKED);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007471 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007472 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007473 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007474 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007475 kfree(ctx);
7476}
7477
7478static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7479{
7480 struct io_ring_ctx *ctx = file->private_data;
7481 __poll_t mask = 0;
7482
7483 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007484 /*
7485 * synchronizes with barrier from wq_has_sleeper call in
7486 * io_commit_cqring
7487 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007488 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007489 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7490 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007491 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007492 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007493 mask |= EPOLLIN | EPOLLRDNORM;
7494
7495 return mask;
7496}
7497
7498static int io_uring_fasync(int fd, struct file *file, int on)
7499{
7500 struct io_ring_ctx *ctx = file->private_data;
7501
7502 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7503}
7504
Jens Axboe071698e2020-01-28 10:04:42 -07007505static int io_remove_personalities(int id, void *p, void *data)
7506{
7507 struct io_ring_ctx *ctx = data;
7508 const struct cred *cred;
7509
7510 cred = idr_remove(&ctx->personality_idr, id);
7511 if (cred)
7512 put_cred(cred);
7513 return 0;
7514}
7515
Jens Axboe85faa7b2020-04-09 18:14:00 -06007516static void io_ring_exit_work(struct work_struct *work)
7517{
7518 struct io_ring_ctx *ctx;
7519
7520 ctx = container_of(work, struct io_ring_ctx, exit_work);
7521 if (ctx->rings)
7522 io_cqring_overflow_flush(ctx, true);
7523
Jens Axboe56952e92020-06-17 15:00:04 -06007524 /*
7525 * If we're doing polled IO and end up having requests being
7526 * submitted async (out-of-line), then completions can come in while
7527 * we're waiting for refs to drop. We need to reap these manually,
7528 * as nobody else will be looking for them.
7529 */
7530 while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) {
7531 io_iopoll_reap_events(ctx);
7532 if (ctx->rings)
7533 io_cqring_overflow_flush(ctx, true);
7534 }
Jens Axboe85faa7b2020-04-09 18:14:00 -06007535 io_ring_ctx_free(ctx);
7536}
7537
Jens Axboe2b188cc2019-01-07 10:46:33 -07007538static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7539{
7540 mutex_lock(&ctx->uring_lock);
7541 percpu_ref_kill(&ctx->refs);
7542 mutex_unlock(&ctx->uring_lock);
7543
Jens Axboe5262f562019-09-17 12:26:57 -06007544 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007545 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007546
7547 if (ctx->io_wq)
7548 io_wq_cancel_all(ctx->io_wq);
7549
Jens Axboedef596e2019-01-09 08:59:42 -07007550 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007551 /* if we failed setting up the ctx, we might not have any rings */
7552 if (ctx->rings)
7553 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007554 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007555 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7556 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007557}
7558
7559static int io_uring_release(struct inode *inode, struct file *file)
7560{
7561 struct io_ring_ctx *ctx = file->private_data;
7562
7563 file->private_data = NULL;
7564 io_ring_ctx_wait_and_kill(ctx);
7565 return 0;
7566}
7567
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007568static bool io_wq_files_match(struct io_wq_work *work, void *data)
7569{
7570 struct files_struct *files = data;
7571
7572 return work->files == files;
7573}
7574
Jens Axboefcb323c2019-10-24 12:39:47 -06007575static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7576 struct files_struct *files)
7577{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007578 if (list_empty_careful(&ctx->inflight_list))
7579 return;
7580
7581 /* cancel all at once, should be faster than doing it one by one*/
7582 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7583
Jens Axboefcb323c2019-10-24 12:39:47 -06007584 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007585 struct io_kiocb *cancel_req = NULL, *req;
7586 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007587
7588 spin_lock_irq(&ctx->inflight_lock);
7589 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007590 if (req->work.files != files)
7591 continue;
7592 /* req is being completed, ignore */
7593 if (!refcount_inc_not_zero(&req->refs))
7594 continue;
7595 cancel_req = req;
7596 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007597 }
Jens Axboe768134d2019-11-10 20:30:53 -07007598 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007599 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007600 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007601 spin_unlock_irq(&ctx->inflight_lock);
7602
Jens Axboe768134d2019-11-10 20:30:53 -07007603 /* We need to keep going until we don't find a matching req */
7604 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007605 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007606
Jens Axboe2ca10252020-02-13 17:17:35 -07007607 if (cancel_req->flags & REQ_F_OVERFLOW) {
7608 spin_lock_irq(&ctx->completion_lock);
7609 list_del(&cancel_req->list);
7610 cancel_req->flags &= ~REQ_F_OVERFLOW;
7611 if (list_empty(&ctx->cq_overflow_list)) {
7612 clear_bit(0, &ctx->sq_check_overflow);
7613 clear_bit(0, &ctx->cq_check_overflow);
7614 }
7615 spin_unlock_irq(&ctx->completion_lock);
7616
7617 WRITE_ONCE(ctx->rings->cq_overflow,
7618 atomic_inc_return(&ctx->cached_cq_overflow));
7619
7620 /*
7621 * Put inflight ref and overflow ref. If that's
7622 * all we had, then we're done with this request.
7623 */
7624 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007625 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007626 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007627 continue;
7628 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007629 } else {
7630 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7631 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007632 }
7633
Jens Axboefcb323c2019-10-24 12:39:47 -06007634 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007635 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007636 }
7637}
7638
Pavel Begunkov801dd572020-06-15 10:33:14 +03007639static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007640{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007641 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7642 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007643
Pavel Begunkov801dd572020-06-15 10:33:14 +03007644 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007645}
7646
Jens Axboefcb323c2019-10-24 12:39:47 -06007647static int io_uring_flush(struct file *file, void *data)
7648{
7649 struct io_ring_ctx *ctx = file->private_data;
7650
7651 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007652
7653 /*
7654 * If the task is going away, cancel work it may have pending
7655 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007656 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7657 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007658
Jens Axboefcb323c2019-10-24 12:39:47 -06007659 return 0;
7660}
7661
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007662static void *io_uring_validate_mmap_request(struct file *file,
7663 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007664{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007665 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007666 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007667 struct page *page;
7668 void *ptr;
7669
7670 switch (offset) {
7671 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007672 case IORING_OFF_CQ_RING:
7673 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007674 break;
7675 case IORING_OFF_SQES:
7676 ptr = ctx->sq_sqes;
7677 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007678 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007679 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007680 }
7681
7682 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007683 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007684 return ERR_PTR(-EINVAL);
7685
7686 return ptr;
7687}
7688
7689#ifdef CONFIG_MMU
7690
7691static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7692{
7693 size_t sz = vma->vm_end - vma->vm_start;
7694 unsigned long pfn;
7695 void *ptr;
7696
7697 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7698 if (IS_ERR(ptr))
7699 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007700
7701 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7702 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7703}
7704
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007705#else /* !CONFIG_MMU */
7706
7707static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7708{
7709 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7710}
7711
7712static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7713{
7714 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7715}
7716
7717static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7718 unsigned long addr, unsigned long len,
7719 unsigned long pgoff, unsigned long flags)
7720{
7721 void *ptr;
7722
7723 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7724 if (IS_ERR(ptr))
7725 return PTR_ERR(ptr);
7726
7727 return (unsigned long) ptr;
7728}
7729
7730#endif /* !CONFIG_MMU */
7731
Jens Axboe2b188cc2019-01-07 10:46:33 -07007732SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7733 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7734 size_t, sigsz)
7735{
7736 struct io_ring_ctx *ctx;
7737 long ret = -EBADF;
7738 int submitted = 0;
7739 struct fd f;
7740
Jens Axboeb41e9852020-02-17 09:52:41 -07007741 if (current->task_works)
7742 task_work_run();
7743
Jens Axboe6c271ce2019-01-10 11:22:30 -07007744 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007745 return -EINVAL;
7746
7747 f = fdget(fd);
7748 if (!f.file)
7749 return -EBADF;
7750
7751 ret = -EOPNOTSUPP;
7752 if (f.file->f_op != &io_uring_fops)
7753 goto out_fput;
7754
7755 ret = -ENXIO;
7756 ctx = f.file->private_data;
7757 if (!percpu_ref_tryget(&ctx->refs))
7758 goto out_fput;
7759
Jens Axboe6c271ce2019-01-10 11:22:30 -07007760 /*
7761 * For SQ polling, the thread will do all submissions and completions.
7762 * Just return the requested submit count, and wake the thread if
7763 * we were asked to.
7764 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007765 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007766 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007767 if (!list_empty_careful(&ctx->cq_overflow_list))
7768 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007769 if (flags & IORING_ENTER_SQ_WAKEUP)
7770 wake_up(&ctx->sqo_wait);
7771 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007772 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007773 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03007774 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007775 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007776
7777 if (submitted != to_submit)
7778 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007779 }
7780 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007781 unsigned nr_events = 0;
7782
Jens Axboe2b188cc2019-01-07 10:46:33 -07007783 min_complete = min(min_complete, ctx->cq_entries);
7784
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007785 /*
7786 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7787 * space applications don't need to do io completion events
7788 * polling again, they can rely on io_sq_thread to do polling
7789 * work, which can reduce cpu usage and uring_lock contention.
7790 */
7791 if (ctx->flags & IORING_SETUP_IOPOLL &&
7792 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007793 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007794 } else {
7795 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7796 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007797 }
7798
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007799out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007800 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007801out_fput:
7802 fdput(f);
7803 return submitted ? submitted : ret;
7804}
7805
Tobias Klauserbebdb652020-02-26 18:38:32 +01007806#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007807static int io_uring_show_cred(int id, void *p, void *data)
7808{
7809 const struct cred *cred = p;
7810 struct seq_file *m = data;
7811 struct user_namespace *uns = seq_user_ns(m);
7812 struct group_info *gi;
7813 kernel_cap_t cap;
7814 unsigned __capi;
7815 int g;
7816
7817 seq_printf(m, "%5d\n", id);
7818 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7819 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7820 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7821 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7822 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7823 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7824 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7825 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7826 seq_puts(m, "\n\tGroups:\t");
7827 gi = cred->group_info;
7828 for (g = 0; g < gi->ngroups; g++) {
7829 seq_put_decimal_ull(m, g ? " " : "",
7830 from_kgid_munged(uns, gi->gid[g]));
7831 }
7832 seq_puts(m, "\n\tCapEff:\t");
7833 cap = cred->cap_effective;
7834 CAP_FOR_EACH_U32(__capi)
7835 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7836 seq_putc(m, '\n');
7837 return 0;
7838}
7839
7840static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7841{
7842 int i;
7843
7844 mutex_lock(&ctx->uring_lock);
7845 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7846 for (i = 0; i < ctx->nr_user_files; i++) {
7847 struct fixed_file_table *table;
7848 struct file *f;
7849
7850 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7851 f = table->files[i & IORING_FILE_TABLE_MASK];
7852 if (f)
7853 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7854 else
7855 seq_printf(m, "%5u: <none>\n", i);
7856 }
7857 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7858 for (i = 0; i < ctx->nr_user_bufs; i++) {
7859 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7860
7861 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7862 (unsigned int) buf->len);
7863 }
7864 if (!idr_is_empty(&ctx->personality_idr)) {
7865 seq_printf(m, "Personalities:\n");
7866 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7867 }
Jens Axboed7718a92020-02-14 22:23:12 -07007868 seq_printf(m, "PollList:\n");
7869 spin_lock_irq(&ctx->completion_lock);
7870 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7871 struct hlist_head *list = &ctx->cancel_hash[i];
7872 struct io_kiocb *req;
7873
7874 hlist_for_each_entry(req, list, hash_node)
7875 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7876 req->task->task_works != NULL);
7877 }
7878 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007879 mutex_unlock(&ctx->uring_lock);
7880}
7881
7882static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7883{
7884 struct io_ring_ctx *ctx = f->private_data;
7885
7886 if (percpu_ref_tryget(&ctx->refs)) {
7887 __io_uring_show_fdinfo(ctx, m);
7888 percpu_ref_put(&ctx->refs);
7889 }
7890}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007891#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007892
Jens Axboe2b188cc2019-01-07 10:46:33 -07007893static const struct file_operations io_uring_fops = {
7894 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007895 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007896 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007897#ifndef CONFIG_MMU
7898 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7899 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7900#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007901 .poll = io_uring_poll,
7902 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007903#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007904 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007905#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007906};
7907
7908static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7909 struct io_uring_params *p)
7910{
Hristo Venev75b28af2019-08-26 17:23:46 +00007911 struct io_rings *rings;
7912 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007913
Hristo Venev75b28af2019-08-26 17:23:46 +00007914 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7915 if (size == SIZE_MAX)
7916 return -EOVERFLOW;
7917
7918 rings = io_mem_alloc(size);
7919 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007920 return -ENOMEM;
7921
Hristo Venev75b28af2019-08-26 17:23:46 +00007922 ctx->rings = rings;
7923 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7924 rings->sq_ring_mask = p->sq_entries - 1;
7925 rings->cq_ring_mask = p->cq_entries - 1;
7926 rings->sq_ring_entries = p->sq_entries;
7927 rings->cq_ring_entries = p->cq_entries;
7928 ctx->sq_mask = rings->sq_ring_mask;
7929 ctx->cq_mask = rings->cq_ring_mask;
7930 ctx->sq_entries = rings->sq_ring_entries;
7931 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007932
7933 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007934 if (size == SIZE_MAX) {
7935 io_mem_free(ctx->rings);
7936 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007937 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007938 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007939
7940 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007941 if (!ctx->sq_sqes) {
7942 io_mem_free(ctx->rings);
7943 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007944 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007945 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007946
Jens Axboe2b188cc2019-01-07 10:46:33 -07007947 return 0;
7948}
7949
7950/*
7951 * Allocate an anonymous fd, this is what constitutes the application
7952 * visible backing of an io_uring instance. The application mmaps this
7953 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7954 * we have to tie this fd to a socket for file garbage collection purposes.
7955 */
7956static int io_uring_get_fd(struct io_ring_ctx *ctx)
7957{
7958 struct file *file;
7959 int ret;
7960
7961#if defined(CONFIG_UNIX)
7962 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7963 &ctx->ring_sock);
7964 if (ret)
7965 return ret;
7966#endif
7967
7968 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7969 if (ret < 0)
7970 goto err;
7971
7972 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7973 O_RDWR | O_CLOEXEC);
7974 if (IS_ERR(file)) {
7975 put_unused_fd(ret);
7976 ret = PTR_ERR(file);
7977 goto err;
7978 }
7979
7980#if defined(CONFIG_UNIX)
7981 ctx->ring_sock->file = file;
7982#endif
7983 fd_install(ret, file);
7984 return ret;
7985err:
7986#if defined(CONFIG_UNIX)
7987 sock_release(ctx->ring_sock);
7988 ctx->ring_sock = NULL;
7989#endif
7990 return ret;
7991}
7992
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007993static int io_uring_create(unsigned entries, struct io_uring_params *p,
7994 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007995{
7996 struct user_struct *user = NULL;
7997 struct io_ring_ctx *ctx;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07007998 bool limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007999 int ret;
8000
Jens Axboe8110c1a2019-12-28 15:39:54 -07008001 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008002 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008003 if (entries > IORING_MAX_ENTRIES) {
8004 if (!(p->flags & IORING_SETUP_CLAMP))
8005 return -EINVAL;
8006 entries = IORING_MAX_ENTRIES;
8007 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008008
8009 /*
8010 * Use twice as many entries for the CQ ring. It's possible for the
8011 * application to drive a higher depth than the size of the SQ ring,
8012 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06008013 * some flexibility in overcommitting a bit. If the application has
8014 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
8015 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07008016 */
8017 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06008018 if (p->flags & IORING_SETUP_CQSIZE) {
8019 /*
8020 * If IORING_SETUP_CQSIZE is set, we do the same roundup
8021 * to a power-of-two, if it isn't already. We do NOT impose
8022 * any cq vs sq ring sizing.
8023 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07008024 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06008025 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07008026 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
8027 if (!(p->flags & IORING_SETUP_CLAMP))
8028 return -EINVAL;
8029 p->cq_entries = IORING_MAX_CQ_ENTRIES;
8030 }
Jens Axboe33a107f2019-10-04 12:10:03 -06008031 p->cq_entries = roundup_pow_of_two(p->cq_entries);
8032 } else {
8033 p->cq_entries = 2 * p->sq_entries;
8034 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008035
8036 user = get_uid(current_user());
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008037 limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008038
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008039 if (limit_mem) {
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008040 ret = __io_account_mem(user,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008041 ring_pages(p->sq_entries, p->cq_entries));
8042 if (ret) {
8043 free_uid(user);
8044 return ret;
8045 }
8046 }
8047
8048 ctx = io_ring_ctx_alloc(p);
8049 if (!ctx) {
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008050 if (limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008051 __io_unaccount_mem(user, ring_pages(p->sq_entries,
Jens Axboe2b188cc2019-01-07 10:46:33 -07008052 p->cq_entries));
8053 free_uid(user);
8054 return -ENOMEM;
8055 }
8056 ctx->compat = in_compat_syscall();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008057 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07008058 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07008059
8060 ret = io_allocate_scq_urings(ctx, p);
8061 if (ret)
8062 goto err;
8063
Jens Axboe6c271ce2019-01-10 11:22:30 -07008064 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008065 if (ret)
8066 goto err;
8067
Jens Axboe2b188cc2019-01-07 10:46:33 -07008068 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008069 p->sq_off.head = offsetof(struct io_rings, sq.head);
8070 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
8071 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
8072 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
8073 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
8074 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
8075 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008076
8077 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00008078 p->cq_off.head = offsetof(struct io_rings, cq.head);
8079 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
8080 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
8081 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
8082 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
8083 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02008084 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06008085
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008086 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
8087 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08008088 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8089 IORING_FEAT_POLL_32BITS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008090
8091 if (copy_to_user(params, p, sizeof(*p))) {
8092 ret = -EFAULT;
8093 goto err;
8094 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06008095 /*
8096 * Install ring fd as the very last thing, so we don't risk someone
8097 * having closed it before we finish setup
8098 */
8099 ret = io_uring_get_fd(ctx);
8100 if (ret < 0)
8101 goto err;
8102
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008103 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Bijan Mottahedeh2e0464d2020-06-16 16:36:10 -07008104 io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8105 ACCT_LOCKED);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008106 ctx->limit_mem = limit_mem;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008107 return ret;
8108err:
8109 io_ring_ctx_wait_and_kill(ctx);
8110 return ret;
8111}
8112
8113/*
8114 * Sets up an aio uring context, and returns the fd. Applications asks for a
8115 * ring size, we return the actual sq/cq ring sizes (among other things) in the
8116 * params structure passed in.
8117 */
8118static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
8119{
8120 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008121 int i;
8122
8123 if (copy_from_user(&p, params, sizeof(p)))
8124 return -EFAULT;
8125 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
8126 if (p.resv[i])
8127 return -EINVAL;
8128 }
8129
Jens Axboe6c271ce2019-01-10 11:22:30 -07008130 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07008131 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03008132 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008133 return -EINVAL;
8134
Xiaoguang Wang7f136572020-05-05 16:28:53 +08008135 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008136}
8137
8138SYSCALL_DEFINE2(io_uring_setup, u32, entries,
8139 struct io_uring_params __user *, params)
8140{
8141 return io_uring_setup(entries, params);
8142}
8143
Jens Axboe66f4af92020-01-16 15:36:52 -07008144static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
8145{
8146 struct io_uring_probe *p;
8147 size_t size;
8148 int i, ret;
8149
8150 size = struct_size(p, ops, nr_args);
8151 if (size == SIZE_MAX)
8152 return -EOVERFLOW;
8153 p = kzalloc(size, GFP_KERNEL);
8154 if (!p)
8155 return -ENOMEM;
8156
8157 ret = -EFAULT;
8158 if (copy_from_user(p, arg, size))
8159 goto out;
8160 ret = -EINVAL;
8161 if (memchr_inv(p, 0, size))
8162 goto out;
8163
8164 p->last_op = IORING_OP_LAST - 1;
8165 if (nr_args > IORING_OP_LAST)
8166 nr_args = IORING_OP_LAST;
8167
8168 for (i = 0; i < nr_args; i++) {
8169 p->ops[i].op = i;
8170 if (!io_op_defs[i].not_supported)
8171 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8172 }
8173 p->ops_len = i;
8174
8175 ret = 0;
8176 if (copy_to_user(arg, p, size))
8177 ret = -EFAULT;
8178out:
8179 kfree(p);
8180 return ret;
8181}
8182
Jens Axboe071698e2020-01-28 10:04:42 -07008183static int io_register_personality(struct io_ring_ctx *ctx)
8184{
8185 const struct cred *creds = get_current_cred();
8186 int id;
8187
8188 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8189 USHRT_MAX, GFP_KERNEL);
8190 if (id < 0)
8191 put_cred(creds);
8192 return id;
8193}
8194
8195static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8196{
8197 const struct cred *old_creds;
8198
8199 old_creds = idr_remove(&ctx->personality_idr, id);
8200 if (old_creds) {
8201 put_cred(old_creds);
8202 return 0;
8203 }
8204
8205 return -EINVAL;
8206}
8207
8208static bool io_register_op_must_quiesce(int op)
8209{
8210 switch (op) {
8211 case IORING_UNREGISTER_FILES:
8212 case IORING_REGISTER_FILES_UPDATE:
8213 case IORING_REGISTER_PROBE:
8214 case IORING_REGISTER_PERSONALITY:
8215 case IORING_UNREGISTER_PERSONALITY:
8216 return false;
8217 default:
8218 return true;
8219 }
8220}
8221
Jens Axboeedafcce2019-01-09 09:16:05 -07008222static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8223 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008224 __releases(ctx->uring_lock)
8225 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008226{
8227 int ret;
8228
Jens Axboe35fa71a2019-04-22 10:23:23 -06008229 /*
8230 * We're inside the ring mutex, if the ref is already dying, then
8231 * someone else killed the ctx or is already going through
8232 * io_uring_register().
8233 */
8234 if (percpu_ref_is_dying(&ctx->refs))
8235 return -ENXIO;
8236
Jens Axboe071698e2020-01-28 10:04:42 -07008237 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008238 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008239
Jens Axboe05f3fb32019-12-09 11:22:50 -07008240 /*
8241 * Drop uring mutex before waiting for references to exit. If
8242 * another thread is currently inside io_uring_enter() it might
8243 * need to grab the uring_lock to make progress. If we hold it
8244 * here across the drain wait, then we can deadlock. It's safe
8245 * to drop the mutex here, since no new references will come in
8246 * after we've killed the percpu ref.
8247 */
8248 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008249 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008250 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008251 if (ret) {
8252 percpu_ref_resurrect(&ctx->refs);
8253 ret = -EINTR;
8254 goto out;
8255 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008256 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008257
8258 switch (opcode) {
8259 case IORING_REGISTER_BUFFERS:
8260 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8261 break;
8262 case IORING_UNREGISTER_BUFFERS:
8263 ret = -EINVAL;
8264 if (arg || nr_args)
8265 break;
8266 ret = io_sqe_buffer_unregister(ctx);
8267 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008268 case IORING_REGISTER_FILES:
8269 ret = io_sqe_files_register(ctx, arg, nr_args);
8270 break;
8271 case IORING_UNREGISTER_FILES:
8272 ret = -EINVAL;
8273 if (arg || nr_args)
8274 break;
8275 ret = io_sqe_files_unregister(ctx);
8276 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008277 case IORING_REGISTER_FILES_UPDATE:
8278 ret = io_sqe_files_update(ctx, arg, nr_args);
8279 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008280 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008281 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008282 ret = -EINVAL;
8283 if (nr_args != 1)
8284 break;
8285 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008286 if (ret)
8287 break;
8288 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8289 ctx->eventfd_async = 1;
8290 else
8291 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008292 break;
8293 case IORING_UNREGISTER_EVENTFD:
8294 ret = -EINVAL;
8295 if (arg || nr_args)
8296 break;
8297 ret = io_eventfd_unregister(ctx);
8298 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008299 case IORING_REGISTER_PROBE:
8300 ret = -EINVAL;
8301 if (!arg || nr_args > 256)
8302 break;
8303 ret = io_probe(ctx, arg, nr_args);
8304 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008305 case IORING_REGISTER_PERSONALITY:
8306 ret = -EINVAL;
8307 if (arg || nr_args)
8308 break;
8309 ret = io_register_personality(ctx);
8310 break;
8311 case IORING_UNREGISTER_PERSONALITY:
8312 ret = -EINVAL;
8313 if (arg)
8314 break;
8315 ret = io_unregister_personality(ctx, nr_args);
8316 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008317 default:
8318 ret = -EINVAL;
8319 break;
8320 }
8321
Jens Axboe071698e2020-01-28 10:04:42 -07008322 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008323 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008324 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008325out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008326 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008327 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008328 return ret;
8329}
8330
8331SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8332 void __user *, arg, unsigned int, nr_args)
8333{
8334 struct io_ring_ctx *ctx;
8335 long ret = -EBADF;
8336 struct fd f;
8337
8338 f = fdget(fd);
8339 if (!f.file)
8340 return -EBADF;
8341
8342 ret = -EOPNOTSUPP;
8343 if (f.file->f_op != &io_uring_fops)
8344 goto out_fput;
8345
8346 ctx = f.file->private_data;
8347
8348 mutex_lock(&ctx->uring_lock);
8349 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8350 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008351 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8352 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008353out_fput:
8354 fdput(f);
8355 return ret;
8356}
8357
Jens Axboe2b188cc2019-01-07 10:46:33 -07008358static int __init io_uring_init(void)
8359{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008360#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8361 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8362 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8363} while (0)
8364
8365#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8366 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8367 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8368 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8369 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8370 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8371 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8372 BUILD_BUG_SQE_ELEM(8, __u64, off);
8373 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8374 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008375 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008376 BUILD_BUG_SQE_ELEM(24, __u32, len);
8377 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8378 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8379 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8380 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08008381 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8382 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008383 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8384 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8385 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8386 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8387 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8388 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8389 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8390 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008391 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008392 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8393 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8394 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008395 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008396
Jens Axboed3656342019-12-18 09:50:26 -07008397 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008398 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008399 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8400 return 0;
8401};
8402__initcall(io_uring_init);