blob: c3e5c1346cfe9e67be6cb3ed96900d1a2da867f3 [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;
229 unsigned int account_mem: 1;
230 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
679#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700680#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700681
Jens Axboe9a56a232019-01-09 09:06:50 -0700682struct io_submit_state {
683 struct blk_plug plug;
684
685 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700686 * io_kiocb alloc cache
687 */
688 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300689 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700690
691 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700692 * File reference cache
693 */
694 struct file *file;
695 unsigned int fd;
696 unsigned int has_refs;
697 unsigned int used_refs;
698 unsigned int ios_left;
699};
700
Jens Axboed3656342019-12-18 09:50:26 -0700701struct io_op_def {
702 /* needs req->io allocated for deferral/async */
703 unsigned async_ctx : 1;
704 /* needs current->mm setup, does mm access */
705 unsigned needs_mm : 1;
706 /* needs req->file assigned */
707 unsigned needs_file : 1;
Jens Axboefd2206e2020-06-02 16:40:47 -0600708 /* don't fail if file grab fails */
709 unsigned needs_file_no_error : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700710 /* hash wq insertion if file is a regular file */
711 unsigned hash_reg_file : 1;
712 /* unbound wq insertion if file is a non-regular file */
713 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700714 /* opcode is not supported by this kernel */
715 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700716 /* needs file table */
717 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700718 /* needs ->fs */
719 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700720 /* set if opcode supports polled "wait" */
721 unsigned pollin : 1;
722 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700723 /* op supports buffer selection */
724 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700725};
726
727static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300728 [IORING_OP_NOP] = {},
729 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700730 .async_ctx = 1,
731 .needs_mm = 1,
732 .needs_file = 1,
733 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700734 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700735 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700736 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300737 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700738 .async_ctx = 1,
739 .needs_mm = 1,
740 .needs_file = 1,
741 .hash_reg_file = 1,
742 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700743 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700744 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300745 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700746 .needs_file = 1,
747 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300748 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700749 .needs_file = 1,
750 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700751 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700752 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300753 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700754 .needs_file = 1,
755 .hash_reg_file = 1,
756 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700757 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700758 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300759 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700760 .needs_file = 1,
761 .unbound_nonreg_file = 1,
762 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300763 [IORING_OP_POLL_REMOVE] = {},
764 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700765 .needs_file = 1,
766 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300767 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700768 .async_ctx = 1,
769 .needs_mm = 1,
770 .needs_file = 1,
771 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700772 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700773 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700774 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300775 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700776 .async_ctx = 1,
777 .needs_mm = 1,
778 .needs_file = 1,
779 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700780 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700781 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700782 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700783 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300784 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700785 .async_ctx = 1,
786 .needs_mm = 1,
787 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300788 [IORING_OP_TIMEOUT_REMOVE] = {},
789 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700790 .needs_mm = 1,
791 .needs_file = 1,
792 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700793 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700794 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700795 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300796 [IORING_OP_ASYNC_CANCEL] = {},
797 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700798 .async_ctx = 1,
799 .needs_mm = 1,
800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700802 .async_ctx = 1,
803 .needs_mm = 1,
804 .needs_file = 1,
805 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700806 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700807 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300808 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700809 .needs_file = 1,
810 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300811 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700812 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700813 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700814 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300815 [IORING_OP_CLOSE] = {
Jens Axboefd2206e2020-06-02 16:40:47 -0600816 .needs_file = 1,
817 .needs_file_no_error = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700818 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700819 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300820 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700821 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700822 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700823 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300824 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700825 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700826 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600827 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700828 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300829 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700830 .needs_mm = 1,
831 .needs_file = 1,
832 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700833 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700834 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700835 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300836 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700837 .needs_mm = 1,
838 .needs_file = 1,
839 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700840 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700841 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300842 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700843 .needs_file = 1,
844 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700846 .needs_mm = 1,
847 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300848 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700849 .needs_mm = 1,
850 .needs_file = 1,
851 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700852 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700855 .needs_mm = 1,
856 .needs_file = 1,
857 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700858 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700859 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700860 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300861 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700862 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700863 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700864 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700865 [IORING_OP_EPOLL_CTL] = {
866 .unbound_nonreg_file = 1,
867 .file_table = 1,
868 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300869 [IORING_OP_SPLICE] = {
870 .needs_file = 1,
871 .hash_reg_file = 1,
872 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700873 },
874 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700875 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300876 [IORING_OP_TEE] = {
877 .needs_file = 1,
878 .hash_reg_file = 1,
879 .unbound_nonreg_file = 1,
880 },
Jens Axboed3656342019-12-18 09:50:26 -0700881};
882
Jens Axboe561fb042019-10-24 07:25:42 -0600883static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700884static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800885static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700886static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700887static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
888static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700889static int __io_sqe_files_update(struct io_ring_ctx *ctx,
890 struct io_uring_files_update *ip,
891 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700892static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300893static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700894static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
895 int fd, struct file **out_file, bool fixed);
896static void __io_queue_sqe(struct io_kiocb *req,
897 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600898
Jens Axboe2b188cc2019-01-07 10:46:33 -0700899static struct kmem_cache *req_cachep;
900
901static const struct file_operations io_uring_fops;
902
903struct sock *io_uring_get_socket(struct file *file)
904{
905#if defined(CONFIG_UNIX)
906 if (file->f_op == &io_uring_fops) {
907 struct io_ring_ctx *ctx = file->private_data;
908
909 return ctx->ring_sock->sk;
910 }
911#endif
912 return NULL;
913}
914EXPORT_SYMBOL(io_uring_get_socket);
915
Pavel Begunkov4dd28242020-06-15 10:33:13 +0300916static void io_get_req_task(struct io_kiocb *req)
917{
918 if (req->flags & REQ_F_TASK_PINNED)
919 return;
920 get_task_struct(req->task);
921 req->flags |= REQ_F_TASK_PINNED;
922}
923
924/* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
925static void __io_put_req_task(struct io_kiocb *req)
926{
927 if (req->flags & REQ_F_TASK_PINNED)
928 put_task_struct(req->task);
929}
930
Jens Axboe4a38aed22020-05-14 17:21:15 -0600931static void io_file_put_work(struct work_struct *work);
932
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800933/*
934 * Note: must call io_req_init_async() for the first time you
935 * touch any members of io_wq_work.
936 */
937static inline void io_req_init_async(struct io_kiocb *req)
938{
939 if (req->flags & REQ_F_WORK_INITIALIZED)
940 return;
941
942 memset(&req->work, 0, sizeof(req->work));
943 req->flags |= REQ_F_WORK_INITIALIZED;
944}
945
Pavel Begunkov0cdaf762020-05-17 14:13:40 +0300946static inline bool io_async_submit(struct io_ring_ctx *ctx)
947{
948 return ctx->flags & IORING_SETUP_SQPOLL;
949}
950
Jens Axboe2b188cc2019-01-07 10:46:33 -0700951static void io_ring_ctx_ref_free(struct percpu_ref *ref)
952{
953 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
954
Jens Axboe0f158b42020-05-14 17:18:39 -0600955 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700956}
957
958static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
959{
960 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700961 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700962
963 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
964 if (!ctx)
965 return NULL;
966
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700967 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
968 if (!ctx->fallback_req)
969 goto err;
970
Jens Axboe78076bb2019-12-04 19:56:40 -0700971 /*
972 * Use 5 bits less than the max cq entries, that should give us around
973 * 32 entries per hash list if totally full and uniformly spread.
974 */
975 hash_bits = ilog2(p->cq_entries);
976 hash_bits -= 5;
977 if (hash_bits <= 0)
978 hash_bits = 1;
979 ctx->cancel_hash_bits = hash_bits;
980 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
981 GFP_KERNEL);
982 if (!ctx->cancel_hash)
983 goto err;
984 __hash_init(ctx->cancel_hash, 1U << hash_bits);
985
Roman Gushchin21482892019-05-07 10:01:48 -0700986 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700987 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
988 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989
990 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -0600991 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700992 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700993 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -0600994 init_completion(&ctx->ref_comp);
995 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700996 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700997 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700998 mutex_init(&ctx->uring_lock);
999 init_waitqueue_head(&ctx->wait);
1000 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001001 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001002 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001003 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001004 init_waitqueue_head(&ctx->inflight_wait);
1005 spin_lock_init(&ctx->inflight_lock);
1006 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -06001007 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
1008 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001010err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001011 if (ctx->fallback_req)
1012 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -07001013 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001014 kfree(ctx);
1015 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001016}
1017
Bob Liu9d858b22019-11-13 18:06:25 +08001018static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06001019{
Jackie Liua197f662019-11-08 08:09:12 -07001020 struct io_ring_ctx *ctx = req->ctx;
1021
Pavel Begunkov31af27c2020-04-15 00:39:50 +03001022 return req->sequence != ctx->cached_cq_tail
1023 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -06001024}
1025
Bob Liu9d858b22019-11-13 18:06:25 +08001026static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001027{
Pavel Begunkov87987892020-01-18 01:22:30 +03001028 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +08001029 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001030
Bob Liu9d858b22019-11-13 18:06:25 +08001031 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001032}
1033
Jens Axboede0617e2019-04-06 21:51:27 -06001034static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001035{
Hristo Venev75b28af2019-08-26 17:23:46 +00001036 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001037
Pavel Begunkov07910152020-01-17 03:52:46 +03001038 /* order cqe stores with ring update */
1039 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001040
Pavel Begunkov07910152020-01-17 03:52:46 +03001041 if (wq_has_sleeper(&ctx->cq_wait)) {
1042 wake_up_interruptible(&ctx->cq_wait);
1043 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001044 }
1045}
1046
Jens Axboecccf0ee2020-01-27 16:34:48 -07001047static inline void io_req_work_grab_env(struct io_kiocb *req,
1048 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001049{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001050 if (!req->work.mm && def->needs_mm) {
1051 mmgrab(current->mm);
1052 req->work.mm = current->mm;
1053 }
1054 if (!req->work.creds)
1055 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001056 if (!req->work.fs && def->needs_fs) {
1057 spin_lock(&current->fs->lock);
1058 if (!current->fs->in_exec) {
1059 req->work.fs = current->fs;
1060 req->work.fs->users++;
1061 } else {
1062 req->work.flags |= IO_WQ_WORK_CANCEL;
1063 }
1064 spin_unlock(&current->fs->lock);
1065 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001066}
1067
1068static inline void io_req_work_drop_env(struct io_kiocb *req)
1069{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001070 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1071 return;
1072
Jens Axboecccf0ee2020-01-27 16:34:48 -07001073 if (req->work.mm) {
1074 mmdrop(req->work.mm);
1075 req->work.mm = NULL;
1076 }
1077 if (req->work.creds) {
1078 put_cred(req->work.creds);
1079 req->work.creds = NULL;
1080 }
Jens Axboeff002b32020-02-07 16:05:21 -07001081 if (req->work.fs) {
1082 struct fs_struct *fs = req->work.fs;
1083
1084 spin_lock(&req->work.fs->lock);
1085 if (--fs->users)
1086 fs = NULL;
1087 spin_unlock(&req->work.fs->lock);
1088 if (fs)
1089 free_fs_struct(fs);
1090 }
Jens Axboe561fb042019-10-24 07:25:42 -06001091}
1092
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001093static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001094 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001095{
Jens Axboed3656342019-12-18 09:50:26 -07001096 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001097
Jens Axboed3656342019-12-18 09:50:26 -07001098 if (req->flags & REQ_F_ISREG) {
1099 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001100 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001101 } else {
1102 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001103 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001104 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001105
Pavel Begunkov59960b92020-06-15 16:36:30 +03001106 io_req_init_async(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001107 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001108
Jens Axboe94ae5e72019-11-14 19:39:52 -07001109 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001110}
1111
Jackie Liua197f662019-11-08 08:09:12 -07001112static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001113{
Jackie Liua197f662019-11-08 08:09:12 -07001114 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001115 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001116
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001117 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001118
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001119 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1120 &req->work, req->flags);
1121 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001122
1123 if (link)
1124 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001125}
1126
Jens Axboe5262f562019-09-17 12:26:57 -06001127static void io_kill_timeout(struct io_kiocb *req)
1128{
1129 int ret;
1130
Jens Axboe2d283902019-12-04 11:08:05 -07001131 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001132 if (ret != -1) {
1133 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001134 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001135 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001136 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001137 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001138 }
1139}
1140
1141static void io_kill_timeouts(struct io_ring_ctx *ctx)
1142{
1143 struct io_kiocb *req, *tmp;
1144
1145 spin_lock_irq(&ctx->completion_lock);
1146 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1147 io_kill_timeout(req);
1148 spin_unlock_irq(&ctx->completion_lock);
1149}
1150
Pavel Begunkov04518942020-05-26 20:34:05 +03001151static void __io_queue_deferred(struct io_ring_ctx *ctx)
1152{
1153 do {
1154 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1155 struct io_kiocb, list);
1156
1157 if (req_need_defer(req))
1158 break;
1159 list_del_init(&req->list);
1160 io_queue_async_work(req);
1161 } while (!list_empty(&ctx->defer_list));
1162}
1163
Pavel Begunkov360428f2020-05-30 14:54:17 +03001164static void io_flush_timeouts(struct io_ring_ctx *ctx)
1165{
1166 while (!list_empty(&ctx->timeout_list)) {
1167 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1168 struct io_kiocb, list);
1169
1170 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
1171 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001172 if (req->timeout.target_seq != ctx->cached_cq_tail
1173 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001174 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001175
Pavel Begunkov360428f2020-05-30 14:54:17 +03001176 list_del_init(&req->list);
1177 io_kill_timeout(req);
1178 }
1179}
1180
Jens Axboede0617e2019-04-06 21:51:27 -06001181static void io_commit_cqring(struct io_ring_ctx *ctx)
1182{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001183 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001184 __io_commit_cqring(ctx);
1185
Pavel Begunkov04518942020-05-26 20:34:05 +03001186 if (unlikely(!list_empty(&ctx->defer_list)))
1187 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001188}
1189
Jens Axboe2b188cc2019-01-07 10:46:33 -07001190static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1191{
Hristo Venev75b28af2019-08-26 17:23:46 +00001192 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001193 unsigned tail;
1194
1195 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001196 /*
1197 * writes to the cq entry need to come after reading head; the
1198 * control dependency is enough as we're using WRITE_ONCE to
1199 * fill the cq entry
1200 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001201 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001202 return NULL;
1203
1204 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001205 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001206}
1207
Jens Axboef2842ab2020-01-08 11:04:00 -07001208static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1209{
Jens Axboef0b493e2020-02-01 21:30:11 -07001210 if (!ctx->cq_ev_fd)
1211 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001212 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1213 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001214 if (!ctx->eventfd_async)
1215 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001216 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001217}
1218
Jens Axboeb41e9852020-02-17 09:52:41 -07001219static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001220{
1221 if (waitqueue_active(&ctx->wait))
1222 wake_up(&ctx->wait);
1223 if (waitqueue_active(&ctx->sqo_wait))
1224 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001225 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001226 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001227}
1228
Jens Axboec4a2ed72019-11-21 21:01:26 -07001229/* Returns true if there are no backlogged entries after the flush */
1230static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001231{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001232 struct io_rings *rings = ctx->rings;
1233 struct io_uring_cqe *cqe;
1234 struct io_kiocb *req;
1235 unsigned long flags;
1236 LIST_HEAD(list);
1237
1238 if (!force) {
1239 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001240 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001241 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1242 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001243 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001244 }
1245
1246 spin_lock_irqsave(&ctx->completion_lock, flags);
1247
1248 /* if force is set, the ring is going away. always drop after that */
1249 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001250 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001251
Jens Axboec4a2ed72019-11-21 21:01:26 -07001252 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001253 while (!list_empty(&ctx->cq_overflow_list)) {
1254 cqe = io_get_cqring(ctx);
1255 if (!cqe && !force)
1256 break;
1257
1258 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1259 list);
1260 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001261 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001262 if (cqe) {
1263 WRITE_ONCE(cqe->user_data, req->user_data);
1264 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001265 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001266 } else {
1267 WRITE_ONCE(ctx->rings->cq_overflow,
1268 atomic_inc_return(&ctx->cached_cq_overflow));
1269 }
1270 }
1271
1272 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001273 if (cqe) {
1274 clear_bit(0, &ctx->sq_check_overflow);
1275 clear_bit(0, &ctx->cq_check_overflow);
1276 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001277 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1278 io_cqring_ev_posted(ctx);
1279
1280 while (!list_empty(&list)) {
1281 req = list_first_entry(&list, struct io_kiocb, list);
1282 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001283 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001284 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001285
1286 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001287}
1288
Jens Axboebcda7ba2020-02-23 16:42:51 -07001289static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001290{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001291 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001292 struct io_uring_cqe *cqe;
1293
Jens Axboe78e19bb2019-11-06 15:21:34 -07001294 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001295
Jens Axboe2b188cc2019-01-07 10:46:33 -07001296 /*
1297 * If we can't get a cq entry, userspace overflowed the
1298 * submission (by quite a lot). Increment the overflow count in
1299 * the ring.
1300 */
1301 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001302 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001303 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001304 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001305 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001306 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001307 WRITE_ONCE(ctx->rings->cq_overflow,
1308 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001309 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001310 if (list_empty(&ctx->cq_overflow_list)) {
1311 set_bit(0, &ctx->sq_check_overflow);
1312 set_bit(0, &ctx->cq_check_overflow);
1313 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001314 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001315 refcount_inc(&req->refs);
1316 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001317 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001318 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001319 }
1320}
1321
Jens Axboebcda7ba2020-02-23 16:42:51 -07001322static void io_cqring_fill_event(struct io_kiocb *req, long res)
1323{
1324 __io_cqring_fill_event(req, res, 0);
1325}
1326
1327static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001328{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001329 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330 unsigned long flags;
1331
1332 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001333 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001334 io_commit_cqring(ctx);
1335 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1336
Jens Axboe8c838782019-03-12 15:48:16 -06001337 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001338}
1339
Jens Axboebcda7ba2020-02-23 16:42:51 -07001340static void io_cqring_add_event(struct io_kiocb *req, long res)
1341{
1342 __io_cqring_add_event(req, res, 0);
1343}
1344
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001345static inline bool io_is_fallback_req(struct io_kiocb *req)
1346{
1347 return req == (struct io_kiocb *)
1348 ((unsigned long) req->ctx->fallback_req & ~1UL);
1349}
1350
1351static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1352{
1353 struct io_kiocb *req;
1354
1355 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001356 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001357 return req;
1358
1359 return NULL;
1360}
1361
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001362static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1363 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364{
Jens Axboefd6fab22019-03-14 16:30:06 -06001365 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001366 struct io_kiocb *req;
1367
Jens Axboe2579f912019-01-09 09:10:43 -07001368 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001369 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001370 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001371 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001372 } else if (!state->free_reqs) {
1373 size_t sz;
1374 int ret;
1375
1376 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001377 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1378
1379 /*
1380 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1381 * retry single alloc to be on the safe side.
1382 */
1383 if (unlikely(ret <= 0)) {
1384 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1385 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001386 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001387 ret = 1;
1388 }
Jens Axboe2579f912019-01-09 09:10:43 -07001389 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001390 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001391 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001392 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001393 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001394 }
1395
Jens Axboe2579f912019-01-09 09:10:43 -07001396 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001397fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001398 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001399}
1400
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001401static inline void io_put_file(struct io_kiocb *req, struct file *file,
1402 bool fixed)
1403{
1404 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001405 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001406 else
1407 fput(file);
1408}
1409
Jens Axboec6ca97b302019-12-28 12:11:08 -07001410static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001411{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001412 if (req->flags & REQ_F_NEED_CLEANUP)
1413 io_cleanup_req(req);
1414
YueHaibing96fd84d2020-01-07 22:22:44 +08001415 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001416 if (req->file)
1417 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Pavel Begunkov4dd28242020-06-15 10:33:13 +03001418 __io_put_req_task(req);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001419 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420}
1421
1422static void __io_free_req(struct io_kiocb *req)
1423{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001424 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001425
Jens Axboefcb323c2019-10-24 12:39:47 -06001426 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001427 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001428 unsigned long flags;
1429
1430 spin_lock_irqsave(&ctx->inflight_lock, flags);
1431 list_del(&req->inflight_entry);
1432 if (waitqueue_active(&ctx->inflight_wait))
1433 wake_up(&ctx->inflight_wait);
1434 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1435 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001436
1437 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001438 if (likely(!io_is_fallback_req(req)))
1439 kmem_cache_free(req_cachep, req);
1440 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001441 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001442}
1443
Jens Axboec6ca97b302019-12-28 12:11:08 -07001444struct req_batch {
1445 void *reqs[IO_IOPOLL_BATCH];
1446 int to_free;
1447 int need_iter;
1448};
1449
1450static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1451{
1452 if (!rb->to_free)
1453 return;
1454 if (rb->need_iter) {
1455 int i, inflight = 0;
1456 unsigned long flags;
1457
1458 for (i = 0; i < rb->to_free; i++) {
1459 struct io_kiocb *req = rb->reqs[i];
1460
Jens Axboec6ca97b302019-12-28 12:11:08 -07001461 if (req->flags & REQ_F_INFLIGHT)
1462 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001463 __io_req_aux_free(req);
1464 }
1465 if (!inflight)
1466 goto do_free;
1467
1468 spin_lock_irqsave(&ctx->inflight_lock, flags);
1469 for (i = 0; i < rb->to_free; i++) {
1470 struct io_kiocb *req = rb->reqs[i];
1471
Jens Axboe10fef4b2020-01-09 07:52:28 -07001472 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001473 list_del(&req->inflight_entry);
1474 if (!--inflight)
1475 break;
1476 }
1477 }
1478 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1479
1480 if (waitqueue_active(&ctx->inflight_wait))
1481 wake_up(&ctx->inflight_wait);
1482 }
1483do_free:
1484 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1485 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001486 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001487}
1488
Jackie Liua197f662019-11-08 08:09:12 -07001489static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001490{
Jackie Liua197f662019-11-08 08:09:12 -07001491 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001492 int ret;
1493
Jens Axboe2d283902019-12-04 11:08:05 -07001494 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001495 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001496 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001497 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001498 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001499 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001500 return true;
1501 }
1502
1503 return false;
1504}
1505
Jens Axboeba816ad2019-09-28 11:36:45 -06001506static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001507{
Jens Axboe2665abf2019-11-05 12:40:47 -07001508 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001509 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001510
Jens Axboe4d7dd462019-11-20 13:03:52 -07001511 /* Already got next link */
1512 if (req->flags & REQ_F_LINK_NEXT)
1513 return;
1514
Jens Axboe9e645e112019-05-10 16:07:28 -06001515 /*
1516 * The list should never be empty when we are called here. But could
1517 * potentially happen if the chain is messed up, check to be on the
1518 * safe side.
1519 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001520 while (!list_empty(&req->link_list)) {
1521 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1522 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001523
Pavel Begunkov44932332019-12-05 16:16:35 +03001524 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1525 (nxt->flags & REQ_F_TIMEOUT))) {
1526 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001527 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001528 req->flags &= ~REQ_F_LINK_TIMEOUT;
1529 continue;
1530 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001531
Pavel Begunkov44932332019-12-05 16:16:35 +03001532 list_del_init(&req->link_list);
1533 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001534 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001535 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001536 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001537 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001538
Jens Axboe4d7dd462019-11-20 13:03:52 -07001539 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001540 if (wake_ev)
1541 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001542}
1543
1544/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001545 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001546 */
1547static void io_fail_links(struct io_kiocb *req)
1548{
Jens Axboe2665abf2019-11-05 12:40:47 -07001549 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001550 unsigned long flags;
1551
1552 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001553
1554 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001555 struct io_kiocb *link = list_first_entry(&req->link_list,
1556 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001557
Pavel Begunkov44932332019-12-05 16:16:35 +03001558 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001559 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001560
1561 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001562 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001563 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001564 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001565 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001566 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001567 }
Jens Axboe5d960722019-11-19 15:31:28 -07001568 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001569 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001570
1571 io_commit_cqring(ctx);
1572 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1573 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001574}
1575
Jens Axboe4d7dd462019-11-20 13:03:52 -07001576static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001577{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001578 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001579 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001580
Jens Axboe9e645e112019-05-10 16:07:28 -06001581 /*
1582 * If LINK is set, we have dependent requests in this chain. If we
1583 * didn't fail this request, queue the first one up, moving any other
1584 * dependencies to the next request. In case of failure, fail the rest
1585 * of the chain.
1586 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001587 if (req->flags & REQ_F_FAIL_LINK) {
1588 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001589 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1590 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001591 struct io_ring_ctx *ctx = req->ctx;
1592 unsigned long flags;
1593
1594 /*
1595 * If this is a timeout link, we could be racing with the
1596 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001597 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001598 */
1599 spin_lock_irqsave(&ctx->completion_lock, flags);
1600 io_req_link_next(req, nxt);
1601 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1602 } else {
1603 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001604 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001605}
Jens Axboe9e645e112019-05-10 16:07:28 -06001606
Jackie Liuc69f8db2019-11-09 11:00:08 +08001607static void io_free_req(struct io_kiocb *req)
1608{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001609 struct io_kiocb *nxt = NULL;
1610
1611 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001612 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001613
1614 if (nxt)
1615 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001616}
1617
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001618static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1619{
1620 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001621 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1622
1623 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1624 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001625
1626 *workptr = &nxt->work;
1627 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001628 if (link)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03001629 nxt->flags |= REQ_F_QUEUE_TIMEOUT;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001630}
1631
Jens Axboeba816ad2019-09-28 11:36:45 -06001632/*
1633 * Drop reference to request, return next in chain (if there is one) if this
1634 * was the last reference to this request.
1635 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001636__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001637static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001638{
Jens Axboe2a44f462020-02-25 13:25:41 -07001639 if (refcount_dec_and_test(&req->refs)) {
1640 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001641 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001642 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001643}
1644
Jens Axboe2b188cc2019-01-07 10:46:33 -07001645static void io_put_req(struct io_kiocb *req)
1646{
Jens Axboedef596e2019-01-09 08:59:42 -07001647 if (refcount_dec_and_test(&req->refs))
1648 io_free_req(req);
1649}
1650
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001651static void io_steal_work(struct io_kiocb *req,
1652 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001653{
1654 /*
1655 * It's in an io-wq worker, so there always should be at least
1656 * one reference, which will be dropped in io_put_work() just
1657 * after the current handler returns.
1658 *
1659 * It also means, that if the counter dropped to 1, then there is
1660 * no asynchronous users left, so it's safe to steal the next work.
1661 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001662 if (refcount_read(&req->refs) == 1) {
1663 struct io_kiocb *nxt = NULL;
1664
1665 io_req_find_next(req, &nxt);
1666 if (nxt)
1667 io_wq_assign_next(workptr, nxt);
1668 }
1669}
1670
Jens Axboe978db572019-11-14 22:39:04 -07001671/*
1672 * Must only be used if we don't need to care about links, usually from
1673 * within the completion handling itself.
1674 */
1675static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001676{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001677 /* drop both submit and complete references */
1678 if (refcount_sub_and_test(2, &req->refs))
1679 __io_free_req(req);
1680}
1681
Jens Axboe978db572019-11-14 22:39:04 -07001682static void io_double_put_req(struct io_kiocb *req)
1683{
1684 /* drop both submit and complete references */
1685 if (refcount_sub_and_test(2, &req->refs))
1686 io_free_req(req);
1687}
1688
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001689static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001690{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001691 struct io_rings *rings = ctx->rings;
1692
Jens Axboead3eb2c2019-12-18 17:12:20 -07001693 if (test_bit(0, &ctx->cq_check_overflow)) {
1694 /*
1695 * noflush == true is from the waitqueue handler, just ensure
1696 * we wake up the task, and the next invocation will flush the
1697 * entries. We cannot safely to it from here.
1698 */
1699 if (noflush && !list_empty(&ctx->cq_overflow_list))
1700 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001701
Jens Axboead3eb2c2019-12-18 17:12:20 -07001702 io_cqring_overflow_flush(ctx, false);
1703 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001704
Jens Axboea3a0e432019-08-20 11:03:11 -06001705 /* See comment at the top of this file */
1706 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001707 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001708}
1709
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001710static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1711{
1712 struct io_rings *rings = ctx->rings;
1713
1714 /* make sure SQ entry isn't read before tail */
1715 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1716}
1717
Jens Axboe8237e042019-12-28 10:48:22 -07001718static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001719{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001720 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001721 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001722
Jens Axboe9d9e88a2020-05-13 12:53:19 -06001723 if (req->file || req->io)
Jens Axboec6ca97b302019-12-28 12:11:08 -07001724 rb->need_iter++;
1725
1726 rb->reqs[rb->to_free++] = req;
1727 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1728 io_free_req_many(req->ctx, rb);
1729 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001730}
1731
Jens Axboebcda7ba2020-02-23 16:42:51 -07001732static int io_put_kbuf(struct io_kiocb *req)
1733{
Jens Axboe4d954c22020-02-27 07:31:19 -07001734 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001735 int cflags;
1736
Jens Axboe4d954c22020-02-27 07:31:19 -07001737 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001738 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1739 cflags |= IORING_CQE_F_BUFFER;
1740 req->rw.addr = 0;
1741 kfree(kbuf);
1742 return cflags;
1743}
1744
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001745static void io_iopoll_queue(struct list_head *again)
1746{
1747 struct io_kiocb *req;
1748
1749 do {
1750 req = list_first_entry(again, struct io_kiocb, list);
1751 list_del(&req->list);
1752 refcount_inc(&req->refs);
1753 io_queue_async_work(req);
1754 } while (!list_empty(again));
1755}
1756
Jens Axboedef596e2019-01-09 08:59:42 -07001757/*
1758 * Find and free completed poll iocbs
1759 */
1760static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1761 struct list_head *done)
1762{
Jens Axboe8237e042019-12-28 10:48:22 -07001763 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001764 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001765 LIST_HEAD(again);
1766
1767 /* order with ->result store in io_complete_rw_iopoll() */
1768 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07001769
Jens Axboec6ca97b302019-12-28 12:11:08 -07001770 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001771 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001772 int cflags = 0;
1773
Jens Axboedef596e2019-01-09 08:59:42 -07001774 req = list_first_entry(done, struct io_kiocb, list);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001775 if (READ_ONCE(req->result) == -EAGAIN) {
1776 req->iopoll_completed = 0;
1777 list_move_tail(&req->list, &again);
1778 continue;
1779 }
Jens Axboedef596e2019-01-09 08:59:42 -07001780 list_del(&req->list);
1781
Jens Axboebcda7ba2020-02-23 16:42:51 -07001782 if (req->flags & REQ_F_BUFFER_SELECTED)
1783 cflags = io_put_kbuf(req);
1784
1785 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001786 (*nr_events)++;
1787
Jens Axboe8237e042019-12-28 10:48:22 -07001788 if (refcount_dec_and_test(&req->refs) &&
1789 !io_req_multi_free(&rb, req))
1790 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001791 }
Jens Axboedef596e2019-01-09 08:59:42 -07001792
Jens Axboe09bb8392019-03-13 12:39:28 -06001793 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001794 if (ctx->flags & IORING_SETUP_SQPOLL)
1795 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001796 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001797
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001798 if (!list_empty(&again))
1799 io_iopoll_queue(&again);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001800}
1801
Jens Axboedef596e2019-01-09 08:59:42 -07001802static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1803 long min)
1804{
1805 struct io_kiocb *req, *tmp;
1806 LIST_HEAD(done);
1807 bool spin;
1808 int ret;
1809
1810 /*
1811 * Only spin for completions if we don't have multiple devices hanging
1812 * off our complete list, and we're under the requested amount.
1813 */
1814 spin = !ctx->poll_multi_file && *nr_events < min;
1815
1816 ret = 0;
1817 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001818 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001819
1820 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001821 * Move completed and retryable entries to our local lists.
1822 * If we find a request that requires polling, break out
1823 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001824 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08001825 if (READ_ONCE(req->iopoll_completed)) {
Jens Axboedef596e2019-01-09 08:59:42 -07001826 list_move_tail(&req->list, &done);
1827 continue;
1828 }
1829 if (!list_empty(&done))
1830 break;
1831
1832 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1833 if (ret < 0)
1834 break;
1835
1836 if (ret && spin)
1837 spin = false;
1838 ret = 0;
1839 }
1840
1841 if (!list_empty(&done))
1842 io_iopoll_complete(ctx, nr_events, &done);
1843
1844 return ret;
1845}
1846
1847/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001848 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001849 * non-spinning poll check - we'll still enter the driver poll loop, but only
1850 * as a non-spinning completion check.
1851 */
1852static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1853 long min)
1854{
Jens Axboe08f54392019-08-21 22:19:11 -06001855 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001856 int ret;
1857
1858 ret = io_do_iopoll(ctx, nr_events, min);
1859 if (ret < 0)
1860 return ret;
1861 if (!min || *nr_events >= min)
1862 return 0;
1863 }
1864
1865 return 1;
1866}
1867
1868/*
1869 * We can't just wait for polled events to come to us, we have to actively
1870 * find and complete them.
1871 */
1872static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1873{
1874 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1875 return;
1876
1877 mutex_lock(&ctx->uring_lock);
1878 while (!list_empty(&ctx->poll_list)) {
1879 unsigned int nr_events = 0;
1880
1881 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001882
1883 /*
1884 * Ensure we allow local-to-the-cpu processing to take place,
1885 * in this case we need to ensure that we reap all events.
1886 */
1887 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001888 }
1889 mutex_unlock(&ctx->uring_lock);
1890}
1891
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001892static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1893 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001894{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001895 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001896
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001897 /*
1898 * We disallow the app entering submit/complete with polling, but we
1899 * still need to lock the ring to prevent racing with polled issue
1900 * that got punted to a workqueue.
1901 */
1902 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001903 do {
1904 int tmin = 0;
1905
Jens Axboe500f9fb2019-08-19 12:15:59 -06001906 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001907 * Don't enter poll loop if we already have events pending.
1908 * If we do, we can potentially be spinning for commands that
1909 * already triggered a CQE (eg in error).
1910 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001911 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001912 break;
1913
1914 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001915 * If a submit got punted to a workqueue, we can have the
1916 * application entering polling for a command before it gets
1917 * issued. That app will hold the uring_lock for the duration
1918 * of the poll right here, so we need to take a breather every
1919 * now and then to ensure that the issue has a chance to add
1920 * the poll to the issued list. Otherwise we can spin here
1921 * forever, while the workqueue is stuck trying to acquire the
1922 * very same mutex.
1923 */
1924 if (!(++iters & 7)) {
1925 mutex_unlock(&ctx->uring_lock);
1926 mutex_lock(&ctx->uring_lock);
1927 }
1928
Jens Axboedef596e2019-01-09 08:59:42 -07001929 if (*nr_events < min)
1930 tmin = min - *nr_events;
1931
1932 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1933 if (ret <= 0)
1934 break;
1935 ret = 0;
1936 } while (min && !*nr_events && !need_resched());
1937
Jens Axboe500f9fb2019-08-19 12:15:59 -06001938 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001939 return ret;
1940}
1941
Jens Axboe491381ce2019-10-17 09:20:46 -06001942static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001943{
Jens Axboe491381ce2019-10-17 09:20:46 -06001944 /*
1945 * Tell lockdep we inherited freeze protection from submission
1946 * thread.
1947 */
1948 if (req->flags & REQ_F_ISREG) {
1949 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001950
Jens Axboe491381ce2019-10-17 09:20:46 -06001951 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001952 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001953 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001954}
1955
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001956static inline void req_set_fail_links(struct io_kiocb *req)
1957{
1958 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1959 req->flags |= REQ_F_FAIL_LINK;
1960}
1961
Jens Axboeba816ad2019-09-28 11:36:45 -06001962static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001963{
Jens Axboe9adbd452019-12-20 08:45:55 -07001964 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001965 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001966
Jens Axboe491381ce2019-10-17 09:20:46 -06001967 if (kiocb->ki_flags & IOCB_WRITE)
1968 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001969
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001970 if (res != req->result)
1971 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001972 if (req->flags & REQ_F_BUFFER_SELECTED)
1973 cflags = io_put_kbuf(req);
1974 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001975}
1976
1977static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1978{
Jens Axboe9adbd452019-12-20 08:45:55 -07001979 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001980
1981 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001982 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001983}
1984
Jens Axboedef596e2019-01-09 08:59:42 -07001985static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1986{
Jens Axboe9adbd452019-12-20 08:45:55 -07001987 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001988
Jens Axboe491381ce2019-10-17 09:20:46 -06001989 if (kiocb->ki_flags & IOCB_WRITE)
1990 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001991
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08001992 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001993 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08001994
1995 WRITE_ONCE(req->result, res);
1996 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03001997 smp_wmb();
1998 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07001999}
2000
2001/*
2002 * After the iocb has been issued, it's safe to be found on the poll list.
2003 * Adding the kiocb to the list AFTER submission ensures that we don't
2004 * find it from a io_iopoll_getevents() thread before the issuer is done
2005 * accessing the kiocb cookie.
2006 */
2007static void io_iopoll_req_issued(struct io_kiocb *req)
2008{
2009 struct io_ring_ctx *ctx = req->ctx;
2010
2011 /*
2012 * Track whether we have multiple files in our lists. This will impact
2013 * how we do polling eventually, not spinning if we're on potentially
2014 * different devices.
2015 */
2016 if (list_empty(&ctx->poll_list)) {
2017 ctx->poll_multi_file = false;
2018 } else if (!ctx->poll_multi_file) {
2019 struct io_kiocb *list_req;
2020
2021 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
2022 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07002023 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002024 ctx->poll_multi_file = true;
2025 }
2026
2027 /*
2028 * For fast devices, IO may have already completed. If it has, add
2029 * it to the front so we find it first.
2030 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002031 if (READ_ONCE(req->iopoll_completed))
Jens Axboedef596e2019-01-09 08:59:42 -07002032 list_add(&req->list, &ctx->poll_list);
2033 else
2034 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002035
2036 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2037 wq_has_sleeper(&ctx->sqo_wait))
2038 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002039}
2040
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002041static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002042{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002043 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002044
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002045 if (diff)
2046 fput_many(state->file, diff);
2047 state->file = NULL;
2048}
2049
2050static inline void io_state_file_put(struct io_submit_state *state)
2051{
2052 if (state->file)
2053 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002054}
2055
2056/*
2057 * Get as many references to a file as we have IOs left in this submission,
2058 * assuming most submissions are for one file, or at least that each file
2059 * has more than one submission.
2060 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002061static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002062{
2063 if (!state)
2064 return fget(fd);
2065
2066 if (state->file) {
2067 if (state->fd == fd) {
2068 state->used_refs++;
2069 state->ios_left--;
2070 return state->file;
2071 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002072 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002073 }
2074 state->file = fget_many(fd, state->ios_left);
2075 if (!state->file)
2076 return NULL;
2077
2078 state->fd = fd;
2079 state->has_refs = state->ios_left;
2080 state->used_refs = 1;
2081 state->ios_left--;
2082 return state->file;
2083}
2084
Jens Axboe2b188cc2019-01-07 10:46:33 -07002085/*
2086 * If we tracked the file through the SCM inflight mechanism, we could support
2087 * any file. For now, just ensure that anything potentially problematic is done
2088 * inline.
2089 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002090static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002091{
2092 umode_t mode = file_inode(file)->i_mode;
2093
Jens Axboe10d59342019-12-09 20:16:22 -07002094 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002095 return true;
2096 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2097 return true;
2098
Jens Axboec5b85622020-06-09 19:23:05 -06002099 /* any ->read/write should understand O_NONBLOCK */
2100 if (file->f_flags & O_NONBLOCK)
2101 return true;
2102
Jens Axboeaf197f52020-04-28 13:15:06 -06002103 if (!(file->f_mode & FMODE_NOWAIT))
2104 return false;
2105
2106 if (rw == READ)
2107 return file->f_op->read_iter != NULL;
2108
2109 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002110}
2111
Jens Axboe3529d8c2019-12-19 18:24:38 -07002112static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2113 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002114{
Jens Axboedef596e2019-01-09 08:59:42 -07002115 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002116 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002117 unsigned ioprio;
2118 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002119
Jens Axboe491381ce2019-10-17 09:20:46 -06002120 if (S_ISREG(file_inode(req->file)->i_mode))
2121 req->flags |= REQ_F_ISREG;
2122
Jens Axboe2b188cc2019-01-07 10:46:33 -07002123 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002124 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2125 req->flags |= REQ_F_CUR_POS;
2126 kiocb->ki_pos = req->file->f_pos;
2127 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002128 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002129 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2130 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2131 if (unlikely(ret))
2132 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002133
2134 ioprio = READ_ONCE(sqe->ioprio);
2135 if (ioprio) {
2136 ret = ioprio_check_cap(ioprio);
2137 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002138 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002139
2140 kiocb->ki_ioprio = ioprio;
2141 } else
2142 kiocb->ki_ioprio = get_current_ioprio();
2143
Stefan Bühler8449eed2019-04-27 20:34:19 +02002144 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboec5b85622020-06-09 19:23:05 -06002145 if (kiocb->ki_flags & IOCB_NOWAIT)
Stefan Bühler8449eed2019-04-27 20:34:19 +02002146 req->flags |= REQ_F_NOWAIT;
2147
2148 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002149 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002150
Jens Axboedef596e2019-01-09 08:59:42 -07002151 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002152 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2153 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002154 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002155
Jens Axboedef596e2019-01-09 08:59:42 -07002156 kiocb->ki_flags |= IOCB_HIPRI;
2157 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002158 req->result = 0;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002159 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002160 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002161 if (kiocb->ki_flags & IOCB_HIPRI)
2162 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002163 kiocb->ki_complete = io_complete_rw;
2164 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002165
Jens Axboe3529d8c2019-12-19 18:24:38 -07002166 req->rw.addr = READ_ONCE(sqe->addr);
2167 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002168 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002169 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002170}
2171
2172static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2173{
2174 switch (ret) {
2175 case -EIOCBQUEUED:
2176 break;
2177 case -ERESTARTSYS:
2178 case -ERESTARTNOINTR:
2179 case -ERESTARTNOHAND:
2180 case -ERESTART_RESTARTBLOCK:
2181 /*
2182 * We can't just restart the syscall, since previously
2183 * submitted sqes may already be in progress. Just fail this
2184 * IO with EINTR.
2185 */
2186 ret = -EINTR;
2187 /* fall through */
2188 default:
2189 kiocb->ki_complete(kiocb, ret, 0);
2190 }
2191}
2192
Pavel Begunkov014db002020-03-03 21:33:12 +03002193static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002194{
Jens Axboeba042912019-12-25 16:33:42 -07002195 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2196
2197 if (req->flags & REQ_F_CUR_POS)
2198 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002199 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002200 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002201 else
2202 io_rw_done(kiocb, ret);
2203}
2204
Jens Axboe9adbd452019-12-20 08:45:55 -07002205static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002206 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002207{
Jens Axboe9adbd452019-12-20 08:45:55 -07002208 struct io_ring_ctx *ctx = req->ctx;
2209 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002210 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002211 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002212 size_t offset;
2213 u64 buf_addr;
2214
2215 /* attempt to use fixed buffers without having provided iovecs */
2216 if (unlikely(!ctx->user_bufs))
2217 return -EFAULT;
2218
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002219 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002220 if (unlikely(buf_index >= ctx->nr_user_bufs))
2221 return -EFAULT;
2222
2223 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2224 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002225 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002226
2227 /* overflow */
2228 if (buf_addr + len < buf_addr)
2229 return -EFAULT;
2230 /* not inside the mapped region */
2231 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2232 return -EFAULT;
2233
2234 /*
2235 * May not be a start of buffer, set size appropriately
2236 * and advance us to the beginning.
2237 */
2238 offset = buf_addr - imu->ubuf;
2239 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002240
2241 if (offset) {
2242 /*
2243 * Don't use iov_iter_advance() here, as it's really slow for
2244 * using the latter parts of a big fixed buffer - it iterates
2245 * over each segment manually. We can cheat a bit here, because
2246 * we know that:
2247 *
2248 * 1) it's a BVEC iter, we set it up
2249 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2250 * first and last bvec
2251 *
2252 * So just find our index, and adjust the iterator afterwards.
2253 * If the offset is within the first bvec (or the whole first
2254 * bvec, just use iov_iter_advance(). This makes it easier
2255 * since we can just skip the first segment, which may not
2256 * be PAGE_SIZE aligned.
2257 */
2258 const struct bio_vec *bvec = imu->bvec;
2259
2260 if (offset <= bvec->bv_len) {
2261 iov_iter_advance(iter, offset);
2262 } else {
2263 unsigned long seg_skip;
2264
2265 /* skip first vec */
2266 offset -= bvec->bv_len;
2267 seg_skip = 1 + (offset >> PAGE_SHIFT);
2268
2269 iter->bvec = bvec + seg_skip;
2270 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002271 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002272 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002273 }
2274 }
2275
Jens Axboe5e559562019-11-13 16:12:46 -07002276 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002277}
2278
Jens Axboebcda7ba2020-02-23 16:42:51 -07002279static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2280{
2281 if (needs_lock)
2282 mutex_unlock(&ctx->uring_lock);
2283}
2284
2285static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2286{
2287 /*
2288 * "Normal" inline submissions always hold the uring_lock, since we
2289 * grab it from the system call. Same is true for the SQPOLL offload.
2290 * The only exception is when we've detached the request and issue it
2291 * from an async worker thread, grab the lock for that case.
2292 */
2293 if (needs_lock)
2294 mutex_lock(&ctx->uring_lock);
2295}
2296
2297static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2298 int bgid, struct io_buffer *kbuf,
2299 bool needs_lock)
2300{
2301 struct io_buffer *head;
2302
2303 if (req->flags & REQ_F_BUFFER_SELECTED)
2304 return kbuf;
2305
2306 io_ring_submit_lock(req->ctx, needs_lock);
2307
2308 lockdep_assert_held(&req->ctx->uring_lock);
2309
2310 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2311 if (head) {
2312 if (!list_empty(&head->list)) {
2313 kbuf = list_last_entry(&head->list, struct io_buffer,
2314 list);
2315 list_del(&kbuf->list);
2316 } else {
2317 kbuf = head;
2318 idr_remove(&req->ctx->io_buffer_idr, bgid);
2319 }
2320 if (*len > kbuf->len)
2321 *len = kbuf->len;
2322 } else {
2323 kbuf = ERR_PTR(-ENOBUFS);
2324 }
2325
2326 io_ring_submit_unlock(req->ctx, needs_lock);
2327
2328 return kbuf;
2329}
2330
Jens Axboe4d954c22020-02-27 07:31:19 -07002331static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2332 bool needs_lock)
2333{
2334 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002335 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002336
2337 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002338 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002339 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2340 if (IS_ERR(kbuf))
2341 return kbuf;
2342 req->rw.addr = (u64) (unsigned long) kbuf;
2343 req->flags |= REQ_F_BUFFER_SELECTED;
2344 return u64_to_user_ptr(kbuf->addr);
2345}
2346
2347#ifdef CONFIG_COMPAT
2348static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2349 bool needs_lock)
2350{
2351 struct compat_iovec __user *uiov;
2352 compat_ssize_t clen;
2353 void __user *buf;
2354 ssize_t len;
2355
2356 uiov = u64_to_user_ptr(req->rw.addr);
2357 if (!access_ok(uiov, sizeof(*uiov)))
2358 return -EFAULT;
2359 if (__get_user(clen, &uiov->iov_len))
2360 return -EFAULT;
2361 if (clen < 0)
2362 return -EINVAL;
2363
2364 len = clen;
2365 buf = io_rw_buffer_select(req, &len, needs_lock);
2366 if (IS_ERR(buf))
2367 return PTR_ERR(buf);
2368 iov[0].iov_base = buf;
2369 iov[0].iov_len = (compat_size_t) len;
2370 return 0;
2371}
2372#endif
2373
2374static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2375 bool needs_lock)
2376{
2377 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2378 void __user *buf;
2379 ssize_t len;
2380
2381 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2382 return -EFAULT;
2383
2384 len = iov[0].iov_len;
2385 if (len < 0)
2386 return -EINVAL;
2387 buf = io_rw_buffer_select(req, &len, needs_lock);
2388 if (IS_ERR(buf))
2389 return PTR_ERR(buf);
2390 iov[0].iov_base = buf;
2391 iov[0].iov_len = len;
2392 return 0;
2393}
2394
2395static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2396 bool needs_lock)
2397{
Jens Axboedddb3e22020-06-04 11:27:01 -06002398 if (req->flags & REQ_F_BUFFER_SELECTED) {
2399 struct io_buffer *kbuf;
2400
2401 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2402 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2403 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002404 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002405 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002406 if (!req->rw.len)
2407 return 0;
2408 else if (req->rw.len > 1)
2409 return -EINVAL;
2410
2411#ifdef CONFIG_COMPAT
2412 if (req->ctx->compat)
2413 return io_compat_import(req, iov, needs_lock);
2414#endif
2415
2416 return __io_iov_buffer_select(req, iov, needs_lock);
2417}
2418
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002419static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002420 struct iovec **iovec, struct iov_iter *iter,
2421 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002422{
Jens Axboe9adbd452019-12-20 08:45:55 -07002423 void __user *buf = u64_to_user_ptr(req->rw.addr);
2424 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002425 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002426 u8 opcode;
2427
Jens Axboed625c6e2019-12-17 19:53:05 -07002428 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002429 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002430 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002431 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002432 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002433
Jens Axboebcda7ba2020-02-23 16:42:51 -07002434 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002435 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002436 return -EINVAL;
2437
Jens Axboe3a6820f2019-12-22 15:19:35 -07002438 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002439 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002440 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2441 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002442 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002443 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002444 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002445 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002446 }
2447
Jens Axboe3a6820f2019-12-22 15:19:35 -07002448 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2449 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002450 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002451 }
2452
Jens Axboef67676d2019-12-02 11:03:47 -07002453 if (req->io) {
2454 struct io_async_rw *iorw = &req->io->rw;
2455
2456 *iovec = iorw->iov;
2457 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2458 if (iorw->iov == iorw->fast_iov)
2459 *iovec = NULL;
2460 return iorw->size;
2461 }
2462
Jens Axboe4d954c22020-02-27 07:31:19 -07002463 if (req->flags & REQ_F_BUFFER_SELECT) {
2464 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002465 if (!ret) {
2466 ret = (*iovec)->iov_len;
2467 iov_iter_init(iter, rw, *iovec, 1, ret);
2468 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002469 *iovec = NULL;
2470 return ret;
2471 }
2472
Jens Axboe2b188cc2019-01-07 10:46:33 -07002473#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002474 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002475 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2476 iovec, iter);
2477#endif
2478
2479 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2480}
2481
Jens Axboe32960612019-09-23 11:05:34 -06002482/*
2483 * For files that don't have ->read_iter() and ->write_iter(), handle them
2484 * by looping over ->read() or ->write() manually.
2485 */
2486static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2487 struct iov_iter *iter)
2488{
2489 ssize_t ret = 0;
2490
2491 /*
2492 * Don't support polled IO through this interface, and we can't
2493 * support non-blocking either. For the latter, this just causes
2494 * the kiocb to be handled from an async context.
2495 */
2496 if (kiocb->ki_flags & IOCB_HIPRI)
2497 return -EOPNOTSUPP;
2498 if (kiocb->ki_flags & IOCB_NOWAIT)
2499 return -EAGAIN;
2500
2501 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002502 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002503 ssize_t nr;
2504
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002505 if (!iov_iter_is_bvec(iter)) {
2506 iovec = iov_iter_iovec(iter);
2507 } else {
2508 /* fixed buffers import bvec */
2509 iovec.iov_base = kmap(iter->bvec->bv_page)
2510 + iter->iov_offset;
2511 iovec.iov_len = min(iter->count,
2512 iter->bvec->bv_len - iter->iov_offset);
2513 }
2514
Jens Axboe32960612019-09-23 11:05:34 -06002515 if (rw == READ) {
2516 nr = file->f_op->read(file, iovec.iov_base,
2517 iovec.iov_len, &kiocb->ki_pos);
2518 } else {
2519 nr = file->f_op->write(file, iovec.iov_base,
2520 iovec.iov_len, &kiocb->ki_pos);
2521 }
2522
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002523 if (iov_iter_is_bvec(iter))
2524 kunmap(iter->bvec->bv_page);
2525
Jens Axboe32960612019-09-23 11:05:34 -06002526 if (nr < 0) {
2527 if (!ret)
2528 ret = nr;
2529 break;
2530 }
2531 ret += nr;
2532 if (nr != iovec.iov_len)
2533 break;
2534 iov_iter_advance(iter, nr);
2535 }
2536
2537 return ret;
2538}
2539
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002540static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002541 struct iovec *iovec, struct iovec *fast_iov,
2542 struct iov_iter *iter)
2543{
2544 req->io->rw.nr_segs = iter->nr_segs;
2545 req->io->rw.size = io_size;
2546 req->io->rw.iov = iovec;
2547 if (!req->io->rw.iov) {
2548 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002549 if (req->io->rw.iov != fast_iov)
2550 memcpy(req->io->rw.iov, fast_iov,
2551 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002552 } else {
2553 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002554 }
2555}
2556
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002557static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2558{
2559 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2560 return req->io == NULL;
2561}
2562
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002563static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002564{
Jens Axboed3656342019-12-18 09:50:26 -07002565 if (!io_op_defs[req->opcode].async_ctx)
2566 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002567
2568 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002569}
2570
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002571static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2572 struct iovec *iovec, struct iovec *fast_iov,
2573 struct iov_iter *iter)
2574{
Jens Axboe980ad262020-01-24 23:08:54 -07002575 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002576 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002577 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002578 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002579 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002580
Jens Axboe5d204bc2020-01-31 12:06:52 -07002581 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2582 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002583 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002584}
2585
Jens Axboe3529d8c2019-12-19 18:24:38 -07002586static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2587 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002588{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002589 struct io_async_ctx *io;
2590 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002591 ssize_t ret;
2592
Jens Axboe3529d8c2019-12-19 18:24:38 -07002593 ret = io_prep_rw(req, sqe, force_nonblock);
2594 if (ret)
2595 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002596
Jens Axboe3529d8c2019-12-19 18:24:38 -07002597 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2598 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002599
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002600 /* either don't need iovec imported or already have it */
2601 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002602 return 0;
2603
2604 io = req->io;
2605 io->rw.iov = io->rw.fast_iov;
2606 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002607 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002608 req->io = io;
2609 if (ret < 0)
2610 return ret;
2611
2612 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2613 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002614}
2615
Pavel Begunkov014db002020-03-03 21:33:12 +03002616static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002617{
2618 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002619 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002620 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002621 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002622 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002623
Jens Axboebcda7ba2020-02-23 16:42:51 -07002624 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002625 if (ret < 0)
2626 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002627
Jens Axboefd6c2e42019-12-18 12:19:41 -07002628 /* Ensure we clear previously set non-block flag */
2629 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002630 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002631
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002632 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002633 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002634 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002635 req->result = io_size;
2636
2637 /*
2638 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2639 * we know to async punt it even if it was opened O_NONBLOCK
2640 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002641 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002642 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002643
Jens Axboe31b51512019-01-18 22:56:34 -07002644 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002645 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002646 if (!ret) {
2647 ssize_t ret2;
2648
Jens Axboe9adbd452019-12-20 08:45:55 -07002649 if (req->file->f_op->read_iter)
2650 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002651 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002652 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002653
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002654 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002655 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002656 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002657 } else {
2658copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002659 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002660 inline_vecs, &iter);
2661 if (ret)
2662 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002663 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002664 if (!(req->flags & REQ_F_NOWAIT) &&
2665 !file_can_poll(req->file))
Jens Axboe29de5f62020-02-20 09:56:08 -07002666 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002667 return -EAGAIN;
2668 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002669 }
Jens Axboef67676d2019-12-02 11:03:47 -07002670out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08002671 if (!(req->flags & REQ_F_NEED_CLEANUP))
2672 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002673 return ret;
2674}
2675
Jens Axboe3529d8c2019-12-19 18:24:38 -07002676static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2677 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002678{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002679 struct io_async_ctx *io;
2680 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002681 ssize_t ret;
2682
Jens Axboe3529d8c2019-12-19 18:24:38 -07002683 ret = io_prep_rw(req, sqe, force_nonblock);
2684 if (ret)
2685 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002686
Jens Axboe3529d8c2019-12-19 18:24:38 -07002687 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2688 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002689
Jens Axboe4ed734b2020-03-20 11:23:41 -06002690 req->fsize = rlimit(RLIMIT_FSIZE);
2691
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002692 /* either don't need iovec imported or already have it */
2693 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002694 return 0;
2695
2696 io = req->io;
2697 io->rw.iov = io->rw.fast_iov;
2698 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002699 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002700 req->io = io;
2701 if (ret < 0)
2702 return ret;
2703
2704 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2705 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002706}
2707
Pavel Begunkov014db002020-03-03 21:33:12 +03002708static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002709{
2710 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002711 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002712 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002713 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002714 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002715
Jens Axboebcda7ba2020-02-23 16:42:51 -07002716 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002717 if (ret < 0)
2718 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002719
Jens Axboefd6c2e42019-12-18 12:19:41 -07002720 /* Ensure we clear previously set non-block flag */
2721 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002722 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002723
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002724 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002725 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002726 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002727 req->result = io_size;
2728
2729 /*
2730 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2731 * we know to async punt it even if it was opened O_NONBLOCK
2732 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002733 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07002734 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002735
Jens Axboe10d59342019-12-09 20:16:22 -07002736 /* file path doesn't support NOWAIT for non-direct_IO */
2737 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2738 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002739 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002740
Jens Axboe31b51512019-01-18 22:56:34 -07002741 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002742 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002743 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002744 ssize_t ret2;
2745
Jens Axboe2b188cc2019-01-07 10:46:33 -07002746 /*
2747 * Open-code file_start_write here to grab freeze protection,
2748 * which will be released by another thread in
2749 * io_complete_rw(). Fool lockdep by telling it the lock got
2750 * released so that it doesn't complain about the held lock when
2751 * we return to userspace.
2752 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002753 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002754 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002755 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002756 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002757 SB_FREEZE_WRITE);
2758 }
2759 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002760
Jens Axboe4ed734b2020-03-20 11:23:41 -06002761 if (!force_nonblock)
2762 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2763
Jens Axboe9adbd452019-12-20 08:45:55 -07002764 if (req->file->f_op->write_iter)
2765 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002766 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002767 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002768
2769 if (!force_nonblock)
2770 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2771
Jens Axboefaac9962020-02-07 15:45:22 -07002772 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002773 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002774 * retry them without IOCB_NOWAIT.
2775 */
2776 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2777 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002778 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002779 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002780 } else {
2781copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002782 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002783 inline_vecs, &iter);
2784 if (ret)
2785 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002786 /* any defer here is final, must blocking retry */
Jens Axboec5b85622020-06-09 19:23:05 -06002787 if (!(req->flags & REQ_F_NOWAIT) &&
2788 !file_can_poll(req->file))
Jens Axboe490e8962020-04-28 13:16:53 -06002789 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002790 return -EAGAIN;
2791 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002792 }
Jens Axboe31b51512019-01-18 22:56:34 -07002793out_free:
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08002794 if (!(req->flags & REQ_F_NEED_CLEANUP))
2795 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002796 return ret;
2797}
2798
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03002799static int __io_splice_prep(struct io_kiocb *req,
2800 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002801{
2802 struct io_splice* sp = &req->splice;
2803 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2804 int ret;
2805
2806 if (req->flags & REQ_F_NEED_CLEANUP)
2807 return 0;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03002808 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2809 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002810
2811 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002812 sp->len = READ_ONCE(sqe->len);
2813 sp->flags = READ_ONCE(sqe->splice_flags);
2814
2815 if (unlikely(sp->flags & ~valid_flags))
2816 return -EINVAL;
2817
2818 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2819 (sp->flags & SPLICE_F_FD_IN_FIXED));
2820 if (ret)
2821 return ret;
2822 req->flags |= REQ_F_NEED_CLEANUP;
2823
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08002824 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
2825 /*
2826 * Splice operation will be punted aync, and here need to
2827 * modify io_wq_work.flags, so initialize io_wq_work firstly.
2828 */
2829 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002830 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08002831 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002832
2833 return 0;
2834}
2835
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03002836static int io_tee_prep(struct io_kiocb *req,
2837 const struct io_uring_sqe *sqe)
2838{
2839 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
2840 return -EINVAL;
2841 return __io_splice_prep(req, sqe);
2842}
2843
2844static int io_tee(struct io_kiocb *req, bool force_nonblock)
2845{
2846 struct io_splice *sp = &req->splice;
2847 struct file *in = sp->file_in;
2848 struct file *out = sp->file_out;
2849 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2850 long ret = 0;
2851
2852 if (force_nonblock)
2853 return -EAGAIN;
2854 if (sp->len)
2855 ret = do_tee(in, out, sp->len, flags);
2856
2857 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2858 req->flags &= ~REQ_F_NEED_CLEANUP;
2859
2860 io_cqring_add_event(req, ret);
2861 if (ret != sp->len)
2862 req_set_fail_links(req);
2863 io_put_req(req);
2864 return 0;
2865}
2866
2867static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2868{
2869 struct io_splice* sp = &req->splice;
2870
2871 sp->off_in = READ_ONCE(sqe->splice_off_in);
2872 sp->off_out = READ_ONCE(sqe->off);
2873 return __io_splice_prep(req, sqe);
2874}
2875
Pavel Begunkov014db002020-03-03 21:33:12 +03002876static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002877{
2878 struct io_splice *sp = &req->splice;
2879 struct file *in = sp->file_in;
2880 struct file *out = sp->file_out;
2881 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2882 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03002883 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002884
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03002885 if (force_nonblock)
2886 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002887
2888 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2889 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03002890
Jens Axboe948a7742020-05-17 14:21:38 -06002891 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03002892 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002893
2894 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2895 req->flags &= ~REQ_F_NEED_CLEANUP;
2896
2897 io_cqring_add_event(req, ret);
2898 if (ret != sp->len)
2899 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002900 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002901 return 0;
2902}
2903
Jens Axboe2b188cc2019-01-07 10:46:33 -07002904/*
2905 * IORING_OP_NOP just posts a completion event, nothing else.
2906 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002907static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002908{
2909 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002910
Jens Axboedef596e2019-01-09 08:59:42 -07002911 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2912 return -EINVAL;
2913
Jens Axboe78e19bb2019-11-06 15:21:34 -07002914 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002915 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002916 return 0;
2917}
2918
Jens Axboe3529d8c2019-12-19 18:24:38 -07002919static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002920{
Jens Axboe6b063142019-01-10 22:13:58 -07002921 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002922
Jens Axboe09bb8392019-03-13 12:39:28 -06002923 if (!req->file)
2924 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002925
Jens Axboe6b063142019-01-10 22:13:58 -07002926 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002927 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002928 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002929 return -EINVAL;
2930
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002931 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2932 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2933 return -EINVAL;
2934
2935 req->sync.off = READ_ONCE(sqe->off);
2936 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002937 return 0;
2938}
2939
Pavel Begunkovac45abc2020-06-08 21:08:18 +03002940static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Jens Axboe78912932020-01-14 22:09:06 -07002941{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002942 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002943 int ret;
2944
Pavel Begunkovac45abc2020-06-08 21:08:18 +03002945 /* fsync always requires a blocking context */
2946 if (force_nonblock)
2947 return -EAGAIN;
2948
Jens Axboe9adbd452019-12-20 08:45:55 -07002949 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002950 end > 0 ? end : LLONG_MAX,
2951 req->sync.flags & IORING_FSYNC_DATASYNC);
2952 if (ret < 0)
2953 req_set_fail_links(req);
2954 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002955 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002956 return 0;
2957}
2958
Jens Axboed63d1b52019-12-10 10:38:56 -07002959static int io_fallocate_prep(struct io_kiocb *req,
2960 const struct io_uring_sqe *sqe)
2961{
2962 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2963 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03002964 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2965 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07002966
2967 req->sync.off = READ_ONCE(sqe->off);
2968 req->sync.len = READ_ONCE(sqe->addr);
2969 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002970 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07002971 return 0;
2972}
2973
Pavel Begunkov014db002020-03-03 21:33:12 +03002974static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002975{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03002976 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07002977
Pavel Begunkovac45abc2020-06-08 21:08:18 +03002978 /* fallocate always requiring blocking context */
2979 if (force_nonblock)
2980 return -EAGAIN;
2981
2982 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2983 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2984 req->sync.len);
2985 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2986 if (ret < 0)
2987 req_set_fail_links(req);
2988 io_cqring_add_event(req, ret);
2989 io_put_req(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002990 return 0;
2991}
2992
Pavel Begunkovec65fea2020-06-03 18:03:24 +03002993static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002994{
Jens Axboef8748882020-01-08 17:47:02 -07002995 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002996 int ret;
2997
Pavel Begunkov3232dd02020-06-03 18:03:22 +03002998 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
Jens Axboe15b71ab2019-12-11 11:20:36 -07002999 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003000 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003001 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003002 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003003 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003004
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003005 /* open.how should be already initialised */
3006 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003007 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003008
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003009 req->open.dfd = READ_ONCE(sqe->fd);
3010 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003011 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003012 if (IS_ERR(req->open.filename)) {
3013 ret = PTR_ERR(req->open.filename);
3014 req->open.filename = NULL;
3015 return ret;
3016 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003017 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003018 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003019 return 0;
3020}
3021
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003022static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3023{
3024 u64 flags, mode;
3025
3026 if (req->flags & REQ_F_NEED_CLEANUP)
3027 return 0;
3028 mode = READ_ONCE(sqe->len);
3029 flags = READ_ONCE(sqe->open_flags);
3030 req->open.how = build_open_how(flags, mode);
3031 return __io_openat_prep(req, sqe);
3032}
3033
Jens Axboecebdb982020-01-08 17:59:24 -07003034static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3035{
3036 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003037 size_t len;
3038 int ret;
3039
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003040 if (req->flags & REQ_F_NEED_CLEANUP)
3041 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003042 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3043 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003044 if (len < OPEN_HOW_SIZE_VER0)
3045 return -EINVAL;
3046
3047 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3048 len);
3049 if (ret)
3050 return ret;
3051
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003052 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003053}
3054
Pavel Begunkov014db002020-03-03 21:33:12 +03003055static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003056{
3057 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003058 struct file *file;
3059 int ret;
3060
Jens Axboef86cd202020-01-29 13:46:44 -07003061 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003062 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003063
Jens Axboecebdb982020-01-08 17:59:24 -07003064 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003065 if (ret)
3066 goto err;
3067
Jens Axboe4022e7a2020-03-19 19:23:18 -06003068 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003069 if (ret < 0)
3070 goto err;
3071
3072 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3073 if (IS_ERR(file)) {
3074 put_unused_fd(ret);
3075 ret = PTR_ERR(file);
3076 } else {
3077 fsnotify_open(file);
3078 fd_install(ret, file);
3079 }
3080err:
3081 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003082 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003083 if (ret < 0)
3084 req_set_fail_links(req);
3085 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003086 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003087 return 0;
3088}
3089
Pavel Begunkov014db002020-03-03 21:33:12 +03003090static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003091{
Pavel Begunkov014db002020-03-03 21:33:12 +03003092 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003093}
3094
Jens Axboe067524e2020-03-02 16:32:28 -07003095static int io_remove_buffers_prep(struct io_kiocb *req,
3096 const struct io_uring_sqe *sqe)
3097{
3098 struct io_provide_buf *p = &req->pbuf;
3099 u64 tmp;
3100
3101 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3102 return -EINVAL;
3103
3104 tmp = READ_ONCE(sqe->fd);
3105 if (!tmp || tmp > USHRT_MAX)
3106 return -EINVAL;
3107
3108 memset(p, 0, sizeof(*p));
3109 p->nbufs = tmp;
3110 p->bgid = READ_ONCE(sqe->buf_group);
3111 return 0;
3112}
3113
3114static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3115 int bgid, unsigned nbufs)
3116{
3117 unsigned i = 0;
3118
3119 /* shouldn't happen */
3120 if (!nbufs)
3121 return 0;
3122
3123 /* the head kbuf is the list itself */
3124 while (!list_empty(&buf->list)) {
3125 struct io_buffer *nxt;
3126
3127 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3128 list_del(&nxt->list);
3129 kfree(nxt);
3130 if (++i == nbufs)
3131 return i;
3132 }
3133 i++;
3134 kfree(buf);
3135 idr_remove(&ctx->io_buffer_idr, bgid);
3136
3137 return i;
3138}
3139
3140static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3141{
3142 struct io_provide_buf *p = &req->pbuf;
3143 struct io_ring_ctx *ctx = req->ctx;
3144 struct io_buffer *head;
3145 int ret = 0;
3146
3147 io_ring_submit_lock(ctx, !force_nonblock);
3148
3149 lockdep_assert_held(&ctx->uring_lock);
3150
3151 ret = -ENOENT;
3152 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3153 if (head)
3154 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3155
3156 io_ring_submit_lock(ctx, !force_nonblock);
3157 if (ret < 0)
3158 req_set_fail_links(req);
3159 io_cqring_add_event(req, ret);
3160 io_put_req(req);
3161 return 0;
3162}
3163
Jens Axboeddf0322d2020-02-23 16:41:33 -07003164static int io_provide_buffers_prep(struct io_kiocb *req,
3165 const struct io_uring_sqe *sqe)
3166{
3167 struct io_provide_buf *p = &req->pbuf;
3168 u64 tmp;
3169
3170 if (sqe->ioprio || sqe->rw_flags)
3171 return -EINVAL;
3172
3173 tmp = READ_ONCE(sqe->fd);
3174 if (!tmp || tmp > USHRT_MAX)
3175 return -E2BIG;
3176 p->nbufs = tmp;
3177 p->addr = READ_ONCE(sqe->addr);
3178 p->len = READ_ONCE(sqe->len);
3179
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07003180 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003181 return -EFAULT;
3182
3183 p->bgid = READ_ONCE(sqe->buf_group);
3184 tmp = READ_ONCE(sqe->off);
3185 if (tmp > USHRT_MAX)
3186 return -E2BIG;
3187 p->bid = tmp;
3188 return 0;
3189}
3190
3191static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3192{
3193 struct io_buffer *buf;
3194 u64 addr = pbuf->addr;
3195 int i, bid = pbuf->bid;
3196
3197 for (i = 0; i < pbuf->nbufs; i++) {
3198 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3199 if (!buf)
3200 break;
3201
3202 buf->addr = addr;
3203 buf->len = pbuf->len;
3204 buf->bid = bid;
3205 addr += pbuf->len;
3206 bid++;
3207 if (!*head) {
3208 INIT_LIST_HEAD(&buf->list);
3209 *head = buf;
3210 } else {
3211 list_add_tail(&buf->list, &(*head)->list);
3212 }
3213 }
3214
3215 return i ? i : -ENOMEM;
3216}
3217
Jens Axboeddf0322d2020-02-23 16:41:33 -07003218static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3219{
3220 struct io_provide_buf *p = &req->pbuf;
3221 struct io_ring_ctx *ctx = req->ctx;
3222 struct io_buffer *head, *list;
3223 int ret = 0;
3224
3225 io_ring_submit_lock(ctx, !force_nonblock);
3226
3227 lockdep_assert_held(&ctx->uring_lock);
3228
3229 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3230
3231 ret = io_add_buffers(p, &head);
3232 if (ret < 0)
3233 goto out;
3234
3235 if (!list) {
3236 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3237 GFP_KERNEL);
3238 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003239 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003240 goto out;
3241 }
3242 }
3243out:
3244 io_ring_submit_unlock(ctx, !force_nonblock);
3245 if (ret < 0)
3246 req_set_fail_links(req);
3247 io_cqring_add_event(req, ret);
3248 io_put_req(req);
3249 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003250}
3251
Jens Axboe3e4827b2020-01-08 15:18:09 -07003252static int io_epoll_ctl_prep(struct io_kiocb *req,
3253 const struct io_uring_sqe *sqe)
3254{
3255#if defined(CONFIG_EPOLL)
3256 if (sqe->ioprio || sqe->buf_index)
3257 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003258 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3259 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07003260
3261 req->epoll.epfd = READ_ONCE(sqe->fd);
3262 req->epoll.op = READ_ONCE(sqe->len);
3263 req->epoll.fd = READ_ONCE(sqe->off);
3264
3265 if (ep_op_has_event(req->epoll.op)) {
3266 struct epoll_event __user *ev;
3267
3268 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3269 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3270 return -EFAULT;
3271 }
3272
3273 return 0;
3274#else
3275 return -EOPNOTSUPP;
3276#endif
3277}
3278
Pavel Begunkov014db002020-03-03 21:33:12 +03003279static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003280{
3281#if defined(CONFIG_EPOLL)
3282 struct io_epoll *ie = &req->epoll;
3283 int ret;
3284
3285 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3286 if (force_nonblock && ret == -EAGAIN)
3287 return -EAGAIN;
3288
3289 if (ret < 0)
3290 req_set_fail_links(req);
3291 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003292 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003293 return 0;
3294#else
3295 return -EOPNOTSUPP;
3296#endif
3297}
3298
Jens Axboec1ca7572019-12-25 22:18:28 -07003299static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3300{
3301#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3302 if (sqe->ioprio || sqe->buf_index || sqe->off)
3303 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003304 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3305 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07003306
3307 req->madvise.addr = READ_ONCE(sqe->addr);
3308 req->madvise.len = READ_ONCE(sqe->len);
3309 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3310 return 0;
3311#else
3312 return -EOPNOTSUPP;
3313#endif
3314}
3315
Pavel Begunkov014db002020-03-03 21:33:12 +03003316static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003317{
3318#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3319 struct io_madvise *ma = &req->madvise;
3320 int ret;
3321
3322 if (force_nonblock)
3323 return -EAGAIN;
3324
3325 ret = do_madvise(ma->addr, ma->len, ma->advice);
3326 if (ret < 0)
3327 req_set_fail_links(req);
3328 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003329 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003330 return 0;
3331#else
3332 return -EOPNOTSUPP;
3333#endif
3334}
3335
Jens Axboe4840e412019-12-25 22:03:45 -07003336static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3337{
3338 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3339 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003340 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3341 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07003342
3343 req->fadvise.offset = READ_ONCE(sqe->off);
3344 req->fadvise.len = READ_ONCE(sqe->len);
3345 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3346 return 0;
3347}
3348
Pavel Begunkov014db002020-03-03 21:33:12 +03003349static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003350{
3351 struct io_fadvise *fa = &req->fadvise;
3352 int ret;
3353
Jens Axboe3e694262020-02-01 09:22:49 -07003354 if (force_nonblock) {
3355 switch (fa->advice) {
3356 case POSIX_FADV_NORMAL:
3357 case POSIX_FADV_RANDOM:
3358 case POSIX_FADV_SEQUENTIAL:
3359 break;
3360 default:
3361 return -EAGAIN;
3362 }
3363 }
Jens Axboe4840e412019-12-25 22:03:45 -07003364
3365 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3366 if (ret < 0)
3367 req_set_fail_links(req);
3368 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003369 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003370 return 0;
3371}
3372
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003373static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3374{
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003375 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3376 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003377 if (sqe->ioprio || sqe->buf_index)
3378 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003379 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003380 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003381
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003382 req->statx.dfd = READ_ONCE(sqe->fd);
3383 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003384 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003385 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3386 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003387
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003388 return 0;
3389}
3390
Pavel Begunkov014db002020-03-03 21:33:12 +03003391static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003392{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003393 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003394 int ret;
3395
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003396 if (force_nonblock) {
3397 /* only need file table for an actual valid fd */
3398 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3399 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003400 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003401 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003402
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003403 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3404 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003405
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003406 if (ret < 0)
3407 req_set_fail_links(req);
3408 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003409 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003410 return 0;
3411}
3412
Jens Axboeb5dba592019-12-11 14:02:38 -07003413static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3414{
3415 /*
3416 * If we queue this for async, it must not be cancellable. That would
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003417 * leave the 'file' in an undeterminate state, and here need to modify
3418 * io_wq_work.flags, so initialize io_wq_work firstly.
Jens Axboeb5dba592019-12-11 14:02:38 -07003419 */
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003420 io_req_init_async(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07003421 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3422
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003423 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3424 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003425 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3426 sqe->rw_flags || sqe->buf_index)
3427 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003428 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003429 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003430
3431 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06003432 if ((req->file && req->file->f_op == &io_uring_fops) ||
3433 req->close.fd == req->ctx->ring_fd)
3434 return -EBADF;
3435
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003436 req->close.put_file = NULL;
Jens Axboeb5dba592019-12-11 14:02:38 -07003437 return 0;
3438}
3439
Pavel Begunkov014db002020-03-03 21:33:12 +03003440static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003441{
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003442 struct io_close *close = &req->close;
Jens Axboeb5dba592019-12-11 14:02:38 -07003443 int ret;
3444
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003445 /* might be already done during nonblock submission */
3446 if (!close->put_file) {
3447 ret = __close_fd_get_file(close->fd, &close->put_file);
3448 if (ret < 0)
3449 return (ret == -ENOENT) ? -EBADF : ret;
3450 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003451
3452 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003453 if (close->put_file->f_op->flush && force_nonblock) {
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003454 /* avoid grabbing files - we don't need the files */
3455 req->flags |= REQ_F_NO_FILE_TABLE | REQ_F_MUST_PUNT;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003456 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003457 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003458
Pavel Begunkov3af73b22020-06-08 21:08:17 +03003459 /* No ->flush() or already async, safely close from here */
3460 ret = filp_close(close->put_file, req->work.files);
3461 if (ret < 0)
3462 req_set_fail_links(req);
3463 io_cqring_add_event(req, ret);
3464 fput(close->put_file);
3465 close->put_file = NULL;
3466 io_put_req(req);
Jens Axboe1a417f42020-01-31 17:16:48 -07003467 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003468}
3469
Jens Axboe3529d8c2019-12-19 18:24:38 -07003470static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003471{
3472 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003473
3474 if (!req->file)
3475 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003476
3477 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3478 return -EINVAL;
3479 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3480 return -EINVAL;
3481
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003482 req->sync.off = READ_ONCE(sqe->off);
3483 req->sync.len = READ_ONCE(sqe->len);
3484 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003485 return 0;
3486}
3487
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003488static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003489{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003490 int ret;
3491
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003492 /* sync_file_range always requires a blocking context */
3493 if (force_nonblock)
3494 return -EAGAIN;
3495
Jens Axboe9adbd452019-12-20 08:45:55 -07003496 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003497 req->sync.flags);
3498 if (ret < 0)
3499 req_set_fail_links(req);
3500 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003501 io_put_req(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003502 return 0;
3503}
3504
YueHaibing469956e2020-03-04 15:53:52 +08003505#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003506static int io_setup_async_msg(struct io_kiocb *req,
3507 struct io_async_msghdr *kmsg)
3508{
3509 if (req->io)
3510 return -EAGAIN;
3511 if (io_alloc_async_ctx(req)) {
3512 if (kmsg->iov != kmsg->fast_iov)
3513 kfree(kmsg->iov);
3514 return -ENOMEM;
3515 }
3516 req->flags |= REQ_F_NEED_CLEANUP;
3517 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3518 return -EAGAIN;
3519}
3520
Jens Axboe3529d8c2019-12-19 18:24:38 -07003521static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003522{
Jens Axboee47293f2019-12-20 08:58:21 -07003523 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003524 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003525 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003526
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003527 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3528 return -EINVAL;
3529
Jens Axboee47293f2019-12-20 08:58:21 -07003530 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3531 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003532 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003533
Jens Axboed8768362020-02-27 14:17:49 -07003534#ifdef CONFIG_COMPAT
3535 if (req->ctx->compat)
3536 sr->msg_flags |= MSG_CMSG_COMPAT;
3537#endif
3538
Jens Axboefddafac2020-01-04 20:19:44 -07003539 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003540 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003541 /* iovec is already imported */
3542 if (req->flags & REQ_F_NEED_CLEANUP)
3543 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003544
Jens Axboed9688562019-12-09 19:35:20 -07003545 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003546 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003547 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003548 if (!ret)
3549 req->flags |= REQ_F_NEED_CLEANUP;
3550 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003551}
3552
Pavel Begunkov014db002020-03-03 21:33:12 +03003553static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003554{
Jens Axboe0b416c32019-12-15 10:57:46 -07003555 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003556 struct socket *sock;
3557 int ret;
3558
Jens Axboe03b12302019-12-02 18:50:25 -07003559 sock = sock_from_file(req->file, &ret);
3560 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003561 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003562 unsigned flags;
3563
Jens Axboe03b12302019-12-02 18:50:25 -07003564 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003565 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003566 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003567 /* if iov is set, it's allocated already */
3568 if (!kmsg->iov)
3569 kmsg->iov = kmsg->fast_iov;
3570 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003571 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003572 struct io_sr_msg *sr = &req->sr_msg;
3573
Jens Axboe0b416c32019-12-15 10:57:46 -07003574 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003575 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003576
3577 io.msg.iov = io.msg.fast_iov;
3578 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3579 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003580 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003581 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003582 }
3583
Jens Axboee47293f2019-12-20 08:58:21 -07003584 flags = req->sr_msg.msg_flags;
3585 if (flags & MSG_DONTWAIT)
3586 req->flags |= REQ_F_NOWAIT;
3587 else if (force_nonblock)
3588 flags |= MSG_DONTWAIT;
3589
Jens Axboe0b416c32019-12-15 10:57:46 -07003590 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003591 if (force_nonblock && ret == -EAGAIN)
3592 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003593 if (ret == -ERESTARTSYS)
3594 ret = -EINTR;
3595 }
3596
Pavel Begunkov1e950812020-02-06 19:51:16 +03003597 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003598 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003599 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003600 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003601 if (ret < 0)
3602 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003603 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003604 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003605}
3606
Pavel Begunkov014db002020-03-03 21:33:12 +03003607static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003608{
Jens Axboefddafac2020-01-04 20:19:44 -07003609 struct socket *sock;
3610 int ret;
3611
Jens Axboefddafac2020-01-04 20:19:44 -07003612 sock = sock_from_file(req->file, &ret);
3613 if (sock) {
3614 struct io_sr_msg *sr = &req->sr_msg;
3615 struct msghdr msg;
3616 struct iovec iov;
3617 unsigned flags;
3618
3619 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3620 &msg.msg_iter);
3621 if (ret)
3622 return ret;
3623
3624 msg.msg_name = NULL;
3625 msg.msg_control = NULL;
3626 msg.msg_controllen = 0;
3627 msg.msg_namelen = 0;
3628
3629 flags = req->sr_msg.msg_flags;
3630 if (flags & MSG_DONTWAIT)
3631 req->flags |= REQ_F_NOWAIT;
3632 else if (force_nonblock)
3633 flags |= MSG_DONTWAIT;
3634
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003635 msg.msg_flags = flags;
3636 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003637 if (force_nonblock && ret == -EAGAIN)
3638 return -EAGAIN;
3639 if (ret == -ERESTARTSYS)
3640 ret = -EINTR;
3641 }
3642
3643 io_cqring_add_event(req, ret);
3644 if (ret < 0)
3645 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003646 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003647 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003648}
3649
Jens Axboe52de1fe2020-02-27 10:15:42 -07003650static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3651{
3652 struct io_sr_msg *sr = &req->sr_msg;
3653 struct iovec __user *uiov;
3654 size_t iov_len;
3655 int ret;
3656
3657 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3658 &uiov, &iov_len);
3659 if (ret)
3660 return ret;
3661
3662 if (req->flags & REQ_F_BUFFER_SELECT) {
3663 if (iov_len > 1)
3664 return -EINVAL;
3665 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3666 return -EFAULT;
3667 sr->len = io->msg.iov[0].iov_len;
3668 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3669 sr->len);
3670 io->msg.iov = NULL;
3671 } else {
3672 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3673 &io->msg.iov, &io->msg.msg.msg_iter);
3674 if (ret > 0)
3675 ret = 0;
3676 }
3677
3678 return ret;
3679}
3680
3681#ifdef CONFIG_COMPAT
3682static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3683 struct io_async_ctx *io)
3684{
3685 struct compat_msghdr __user *msg_compat;
3686 struct io_sr_msg *sr = &req->sr_msg;
3687 struct compat_iovec __user *uiov;
3688 compat_uptr_t ptr;
3689 compat_size_t len;
3690 int ret;
3691
3692 msg_compat = (struct compat_msghdr __user *) sr->msg;
3693 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3694 &ptr, &len);
3695 if (ret)
3696 return ret;
3697
3698 uiov = compat_ptr(ptr);
3699 if (req->flags & REQ_F_BUFFER_SELECT) {
3700 compat_ssize_t clen;
3701
3702 if (len > 1)
3703 return -EINVAL;
3704 if (!access_ok(uiov, sizeof(*uiov)))
3705 return -EFAULT;
3706 if (__get_user(clen, &uiov->iov_len))
3707 return -EFAULT;
3708 if (clen < 0)
3709 return -EINVAL;
3710 sr->len = io->msg.iov[0].iov_len;
3711 io->msg.iov = NULL;
3712 } else {
3713 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3714 &io->msg.iov,
3715 &io->msg.msg.msg_iter);
3716 if (ret < 0)
3717 return ret;
3718 }
3719
3720 return 0;
3721}
Jens Axboe03b12302019-12-02 18:50:25 -07003722#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003723
3724static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3725{
3726 io->msg.iov = io->msg.fast_iov;
3727
3728#ifdef CONFIG_COMPAT
3729 if (req->ctx->compat)
3730 return __io_compat_recvmsg_copy_hdr(req, io);
3731#endif
3732
3733 return __io_recvmsg_copy_hdr(req, io);
3734}
3735
Jens Axboebcda7ba2020-02-23 16:42:51 -07003736static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3737 int *cflags, bool needs_lock)
3738{
3739 struct io_sr_msg *sr = &req->sr_msg;
3740 struct io_buffer *kbuf;
3741
3742 if (!(req->flags & REQ_F_BUFFER_SELECT))
3743 return NULL;
3744
3745 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3746 if (IS_ERR(kbuf))
3747 return kbuf;
3748
3749 sr->kbuf = kbuf;
3750 req->flags |= REQ_F_BUFFER_SELECTED;
3751
3752 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3753 *cflags |= IORING_CQE_F_BUFFER;
3754 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07003755}
3756
Jens Axboe3529d8c2019-12-19 18:24:38 -07003757static int io_recvmsg_prep(struct io_kiocb *req,
3758 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003759{
Jens Axboee47293f2019-12-20 08:58:21 -07003760 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003761 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003762 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003763
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03003764 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3765 return -EINVAL;
3766
Jens Axboe3529d8c2019-12-19 18:24:38 -07003767 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3768 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003769 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003770 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003771
Jens Axboed8768362020-02-27 14:17:49 -07003772#ifdef CONFIG_COMPAT
3773 if (req->ctx->compat)
3774 sr->msg_flags |= MSG_CMSG_COMPAT;
3775#endif
3776
Jens Axboefddafac2020-01-04 20:19:44 -07003777 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003778 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003779 /* iovec is already imported */
3780 if (req->flags & REQ_F_NEED_CLEANUP)
3781 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003782
Jens Axboe52de1fe2020-02-27 10:15:42 -07003783 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003784 if (!ret)
3785 req->flags |= REQ_F_NEED_CLEANUP;
3786 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003787}
3788
Pavel Begunkov014db002020-03-03 21:33:12 +03003789static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003790{
Jens Axboe0b416c32019-12-15 10:57:46 -07003791 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003792 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003793 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003794
Jens Axboe0fa03c62019-04-19 13:34:07 -06003795 sock = sock_from_file(req->file, &ret);
3796 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003797 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003798 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003799 unsigned flags;
3800
Jens Axboe03b12302019-12-02 18:50:25 -07003801 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003802 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003803 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003804 /* if iov is set, it's allocated already */
3805 if (!kmsg->iov)
3806 kmsg->iov = kmsg->fast_iov;
3807 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003808 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003809 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003810 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003811
Jens Axboe52de1fe2020-02-27 10:15:42 -07003812 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003813 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003814 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003815 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003816
Jens Axboe52de1fe2020-02-27 10:15:42 -07003817 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3818 if (IS_ERR(kbuf)) {
3819 return PTR_ERR(kbuf);
3820 } else if (kbuf) {
3821 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3822 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3823 1, req->sr_msg.len);
3824 }
3825
Jens Axboee47293f2019-12-20 08:58:21 -07003826 flags = req->sr_msg.msg_flags;
3827 if (flags & MSG_DONTWAIT)
3828 req->flags |= REQ_F_NOWAIT;
3829 else if (force_nonblock)
3830 flags |= MSG_DONTWAIT;
3831
3832 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3833 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003834 if (force_nonblock && ret == -EAGAIN)
3835 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003836 if (ret == -ERESTARTSYS)
3837 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003838 }
3839
Pavel Begunkov1e950812020-02-06 19:51:16 +03003840 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003841 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003842 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003843 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003844 if (ret < 0)
3845 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003846 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003847 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003848}
3849
Pavel Begunkov014db002020-03-03 21:33:12 +03003850static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003851{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003852 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003853 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003854 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003855
Jens Axboefddafac2020-01-04 20:19:44 -07003856 sock = sock_from_file(req->file, &ret);
3857 if (sock) {
3858 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003859 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003860 struct msghdr msg;
3861 struct iovec iov;
3862 unsigned flags;
3863
Jens Axboebcda7ba2020-02-23 16:42:51 -07003864 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3865 if (IS_ERR(kbuf))
3866 return PTR_ERR(kbuf);
3867 else if (kbuf)
3868 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003869
Jens Axboebcda7ba2020-02-23 16:42:51 -07003870 ret = import_single_range(READ, buf, sr->len, &iov,
3871 &msg.msg_iter);
3872 if (ret) {
3873 kfree(kbuf);
3874 return ret;
3875 }
3876
3877 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003878 msg.msg_name = NULL;
3879 msg.msg_control = NULL;
3880 msg.msg_controllen = 0;
3881 msg.msg_namelen = 0;
3882 msg.msg_iocb = NULL;
3883 msg.msg_flags = 0;
3884
3885 flags = req->sr_msg.msg_flags;
3886 if (flags & MSG_DONTWAIT)
3887 req->flags |= REQ_F_NOWAIT;
3888 else if (force_nonblock)
3889 flags |= MSG_DONTWAIT;
3890
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003891 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003892 if (force_nonblock && ret == -EAGAIN)
3893 return -EAGAIN;
3894 if (ret == -ERESTARTSYS)
3895 ret = -EINTR;
3896 }
3897
Jens Axboebcda7ba2020-02-23 16:42:51 -07003898 kfree(kbuf);
3899 req->flags &= ~REQ_F_NEED_CLEANUP;
3900 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003901 if (ret < 0)
3902 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003903 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003904 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003905}
3906
Jens Axboe3529d8c2019-12-19 18:24:38 -07003907static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003908{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003909 struct io_accept *accept = &req->accept;
3910
Jens Axboe17f2fe32019-10-17 14:42:58 -06003911 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3912 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003913 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003914 return -EINVAL;
3915
Jens Axboed55e5f52019-12-11 16:12:15 -07003916 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3917 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003918 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06003919 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003920 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003921}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003922
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003923static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003924{
3925 struct io_accept *accept = &req->accept;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003926 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003927 int ret;
3928
Jiufei Xuee697dee2020-06-10 13:41:59 +08003929 if (req->file->f_flags & O_NONBLOCK)
3930 req->flags |= REQ_F_NOWAIT;
3931
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003932 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06003933 accept->addr_len, accept->flags,
3934 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003935 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003936 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003937 if (ret < 0) {
3938 if (ret == -ERESTARTSYS)
3939 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003940 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003941 }
Jens Axboe78e19bb2019-11-06 15:21:34 -07003942 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003943 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003944 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003945}
3946
Jens Axboe3529d8c2019-12-19 18:24:38 -07003947static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003948{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003949 struct io_connect *conn = &req->connect;
3950 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003951
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003952 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3953 return -EINVAL;
3954 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3955 return -EINVAL;
3956
Jens Axboe3529d8c2019-12-19 18:24:38 -07003957 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3958 conn->addr_len = READ_ONCE(sqe->addr2);
3959
3960 if (!io)
3961 return 0;
3962
3963 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003964 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003965}
3966
Pavel Begunkov014db002020-03-03 21:33:12 +03003967static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003968{
Jens Axboef499a022019-12-02 16:28:46 -07003969 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003970 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003971 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003972
Jens Axboef499a022019-12-02 16:28:46 -07003973 if (req->io) {
3974 io = req->io;
3975 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003976 ret = move_addr_to_kernel(req->connect.addr,
3977 req->connect.addr_len,
3978 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003979 if (ret)
3980 goto out;
3981 io = &__io;
3982 }
3983
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003984 file_flags = force_nonblock ? O_NONBLOCK : 0;
3985
3986 ret = __sys_connect_file(req->file, &io->connect.address,
3987 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003988 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003989 if (req->io)
3990 return -EAGAIN;
3991 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003992 ret = -ENOMEM;
3993 goto out;
3994 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003995 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003996 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003997 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003998 if (ret == -ERESTARTSYS)
3999 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004000out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004001 if (ret < 0)
4002 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004003 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004004 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004005 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004006}
YueHaibing469956e2020-03-04 15:53:52 +08004007#else /* !CONFIG_NET */
4008static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4009{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004010 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004011}
4012
YueHaibing469956e2020-03-04 15:53:52 +08004013static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004014{
YueHaibing469956e2020-03-04 15:53:52 +08004015 return -EOPNOTSUPP;
4016}
4017
4018static int io_send(struct io_kiocb *req, bool force_nonblock)
4019{
4020 return -EOPNOTSUPP;
4021}
4022
4023static int io_recvmsg_prep(struct io_kiocb *req,
4024 const struct io_uring_sqe *sqe)
4025{
4026 return -EOPNOTSUPP;
4027}
4028
4029static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4030{
4031 return -EOPNOTSUPP;
4032}
4033
4034static int io_recv(struct io_kiocb *req, bool force_nonblock)
4035{
4036 return -EOPNOTSUPP;
4037}
4038
4039static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4040{
4041 return -EOPNOTSUPP;
4042}
4043
4044static int io_accept(struct io_kiocb *req, bool force_nonblock)
4045{
4046 return -EOPNOTSUPP;
4047}
4048
4049static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4050{
4051 return -EOPNOTSUPP;
4052}
4053
4054static int io_connect(struct io_kiocb *req, bool force_nonblock)
4055{
4056 return -EOPNOTSUPP;
4057}
4058#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004059
Jens Axboed7718a92020-02-14 22:23:12 -07004060struct io_poll_table {
4061 struct poll_table_struct pt;
4062 struct io_kiocb *req;
4063 int error;
4064};
4065
Jens Axboed7718a92020-02-14 22:23:12 -07004066static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4067 __poll_t mask, task_work_func_t func)
4068{
4069 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004070 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004071
4072 /* for instances that support it check for an event match first: */
4073 if (mask && !(mask & poll->events))
4074 return 0;
4075
4076 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4077
4078 list_del_init(&poll->wait.entry);
4079
4080 tsk = req->task;
4081 req->result = mask;
4082 init_task_work(&req->task_work, func);
4083 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004084 * If this fails, then the task is exiting. When a task exits, the
4085 * work gets canceled, so just cancel this request as well instead
4086 * of executing it. We can't safely execute it anyway, as we may not
4087 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004088 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004089 ret = task_work_add(tsk, &req->task_work, true);
4090 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004091 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004092 tsk = io_wq_get_task(req->ctx->io_wq);
4093 task_work_add(tsk, &req->task_work, true);
4094 }
Jens Axboed7718a92020-02-14 22:23:12 -07004095 wake_up_process(tsk);
4096 return 1;
4097}
4098
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004099static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4100 __acquires(&req->ctx->completion_lock)
4101{
4102 struct io_ring_ctx *ctx = req->ctx;
4103
4104 if (!req->result && !READ_ONCE(poll->canceled)) {
4105 struct poll_table_struct pt = { ._key = poll->events };
4106
4107 req->result = vfs_poll(req->file, &pt) & poll->events;
4108 }
4109
4110 spin_lock_irq(&ctx->completion_lock);
4111 if (!req->result && !READ_ONCE(poll->canceled)) {
4112 add_wait_queue(poll->head, &poll->wait);
4113 return true;
4114 }
4115
4116 return false;
4117}
4118
Jens Axboe18bceab2020-05-15 11:56:54 -06004119static void io_poll_remove_double(struct io_kiocb *req)
4120{
4121 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4122
4123 lockdep_assert_held(&req->ctx->completion_lock);
4124
4125 if (poll && poll->head) {
4126 struct wait_queue_head *head = poll->head;
4127
4128 spin_lock(&head->lock);
4129 list_del_init(&poll->wait.entry);
4130 if (poll->wait.private)
4131 refcount_dec(&req->refs);
4132 poll->head = NULL;
4133 spin_unlock(&head->lock);
4134 }
4135}
4136
4137static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4138{
4139 struct io_ring_ctx *ctx = req->ctx;
4140
4141 io_poll_remove_double(req);
4142 req->poll.done = true;
4143 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4144 io_commit_cqring(ctx);
4145}
4146
4147static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4148{
4149 struct io_ring_ctx *ctx = req->ctx;
4150
4151 if (io_poll_rewait(req, &req->poll)) {
4152 spin_unlock_irq(&ctx->completion_lock);
4153 return;
4154 }
4155
4156 hash_del(&req->hash_node);
4157 io_poll_complete(req, req->result, 0);
4158 req->flags |= REQ_F_COMP_LOCKED;
4159 io_put_req_find_next(req, nxt);
4160 spin_unlock_irq(&ctx->completion_lock);
4161
4162 io_cqring_ev_posted(ctx);
4163}
4164
4165static void io_poll_task_func(struct callback_head *cb)
4166{
4167 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4168 struct io_kiocb *nxt = NULL;
4169
4170 io_poll_task_handler(req, &nxt);
4171 if (nxt) {
4172 struct io_ring_ctx *ctx = nxt->ctx;
4173
4174 mutex_lock(&ctx->uring_lock);
4175 __io_queue_sqe(nxt, NULL);
4176 mutex_unlock(&ctx->uring_lock);
4177 }
4178}
4179
4180static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4181 int sync, void *key)
4182{
4183 struct io_kiocb *req = wait->private;
4184 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4185 __poll_t mask = key_to_poll(key);
4186
4187 /* for instances that support it check for an event match first: */
4188 if (mask && !(mask & poll->events))
4189 return 0;
4190
4191 if (req->poll.head) {
4192 bool done;
4193
4194 spin_lock(&req->poll.head->lock);
4195 done = list_empty(&req->poll.wait.entry);
4196 if (!done)
4197 list_del_init(&req->poll.wait.entry);
4198 spin_unlock(&req->poll.head->lock);
4199 if (!done)
4200 __io_async_wake(req, poll, mask, io_poll_task_func);
4201 }
4202 refcount_dec(&req->refs);
4203 return 1;
4204}
4205
4206static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4207 wait_queue_func_t wake_func)
4208{
4209 poll->head = NULL;
4210 poll->done = false;
4211 poll->canceled = false;
4212 poll->events = events;
4213 INIT_LIST_HEAD(&poll->wait.entry);
4214 init_waitqueue_func_entry(&poll->wait, wake_func);
4215}
4216
4217static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4218 struct wait_queue_head *head)
4219{
4220 struct io_kiocb *req = pt->req;
4221
4222 /*
4223 * If poll->head is already set, it's because the file being polled
4224 * uses multiple waitqueues for poll handling (eg one for read, one
4225 * for write). Setup a separate io_poll_iocb if this happens.
4226 */
4227 if (unlikely(poll->head)) {
4228 /* already have a 2nd entry, fail a third attempt */
4229 if (req->io) {
4230 pt->error = -EINVAL;
4231 return;
4232 }
4233 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4234 if (!poll) {
4235 pt->error = -ENOMEM;
4236 return;
4237 }
4238 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4239 refcount_inc(&req->refs);
4240 poll->wait.private = req;
4241 req->io = (void *) poll;
4242 }
4243
4244 pt->error = 0;
4245 poll->head = head;
4246 add_wait_queue(head, &poll->wait);
4247}
4248
4249static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4250 struct poll_table_struct *p)
4251{
4252 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4253
4254 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4255}
4256
Jens Axboe9d8426a2020-06-16 18:42:49 -06004257static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
4258{
4259 struct mm_struct *mm = current->mm;
4260
4261 if (mm) {
4262 kthread_unuse_mm(mm);
4263 mmput(mm);
4264 }
4265}
4266
4267static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
4268 struct io_kiocb *req)
4269{
4270 if (io_op_defs[req->opcode].needs_mm && !current->mm) {
4271 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
4272 return -EFAULT;
4273 kthread_use_mm(ctx->sqo_mm);
4274 }
4275
4276 return 0;
4277}
4278
Jens Axboed7718a92020-02-14 22:23:12 -07004279static void io_async_task_func(struct callback_head *cb)
4280{
4281 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4282 struct async_poll *apoll = req->apoll;
4283 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31067252020-05-17 17:43:31 -06004284 bool canceled = false;
Jens Axboed7718a92020-02-14 22:23:12 -07004285
4286 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4287
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004288 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004289 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004290 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004291 }
4292
Jens Axboe31067252020-05-17 17:43:31 -06004293 /* If req is still hashed, it cannot have been canceled. Don't check. */
4294 if (hash_hashed(&req->hash_node)) {
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004295 hash_del(&req->hash_node);
Jens Axboe31067252020-05-17 17:43:31 -06004296 } else {
4297 canceled = READ_ONCE(apoll->poll.canceled);
4298 if (canceled) {
4299 io_cqring_fill_event(req, -ECANCELED);
4300 io_commit_cqring(ctx);
4301 }
Jens Axboe2bae0472020-04-13 11:16:34 -06004302 }
4303
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004304 spin_unlock_irq(&ctx->completion_lock);
4305
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004306 /* restore ->work in case we need to retry again */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004307 if (req->flags & REQ_F_WORK_INITIALIZED)
4308 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe31067252020-05-17 17:43:31 -06004309 kfree(apoll);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004310
Jens Axboe31067252020-05-17 17:43:31 -06004311 if (!canceled) {
4312 __set_current_state(TASK_RUNNING);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004313 if (io_sq_thread_acquire_mm(ctx, req)) {
4314 io_cqring_add_event(req, -EFAULT);
4315 goto end_req;
4316 }
Jens Axboe31067252020-05-17 17:43:31 -06004317 mutex_lock(&ctx->uring_lock);
4318 __io_queue_sqe(req, NULL);
4319 mutex_unlock(&ctx->uring_lock);
4320 } else {
Jens Axboe2bae0472020-04-13 11:16:34 -06004321 io_cqring_ev_posted(ctx);
Jens Axboe9d8426a2020-06-16 18:42:49 -06004322end_req:
Jens Axboe2bae0472020-04-13 11:16:34 -06004323 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004324 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004325 }
Jens Axboed7718a92020-02-14 22:23:12 -07004326}
4327
4328static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4329 void *key)
4330{
4331 struct io_kiocb *req = wait->private;
4332 struct io_poll_iocb *poll = &req->apoll->poll;
4333
4334 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4335 key_to_poll(key));
4336
4337 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4338}
4339
4340static void io_poll_req_insert(struct io_kiocb *req)
4341{
4342 struct io_ring_ctx *ctx = req->ctx;
4343 struct hlist_head *list;
4344
4345 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4346 hlist_add_head(&req->hash_node, list);
4347}
4348
4349static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4350 struct io_poll_iocb *poll,
4351 struct io_poll_table *ipt, __poll_t mask,
4352 wait_queue_func_t wake_func)
4353 __acquires(&ctx->completion_lock)
4354{
4355 struct io_ring_ctx *ctx = req->ctx;
4356 bool cancel = false;
4357
4358 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004359 io_init_poll_iocb(poll, mask, wake_func);
4360 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004361
4362 ipt->pt._key = mask;
4363 ipt->req = req;
4364 ipt->error = -EINVAL;
4365
Jens Axboed7718a92020-02-14 22:23:12 -07004366 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4367
4368 spin_lock_irq(&ctx->completion_lock);
4369 if (likely(poll->head)) {
4370 spin_lock(&poll->head->lock);
4371 if (unlikely(list_empty(&poll->wait.entry))) {
4372 if (ipt->error)
4373 cancel = true;
4374 ipt->error = 0;
4375 mask = 0;
4376 }
4377 if (mask || ipt->error)
4378 list_del_init(&poll->wait.entry);
4379 else if (cancel)
4380 WRITE_ONCE(poll->canceled, true);
4381 else if (!poll->done) /* actually waiting for an event */
4382 io_poll_req_insert(req);
4383 spin_unlock(&poll->head->lock);
4384 }
4385
4386 return mask;
4387}
4388
4389static bool io_arm_poll_handler(struct io_kiocb *req)
4390{
4391 const struct io_op_def *def = &io_op_defs[req->opcode];
4392 struct io_ring_ctx *ctx = req->ctx;
4393 struct async_poll *apoll;
4394 struct io_poll_table ipt;
4395 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004396 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004397
4398 if (!req->file || !file_can_poll(req->file))
4399 return false;
4400 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4401 return false;
4402 if (!def->pollin && !def->pollout)
4403 return false;
4404
4405 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4406 if (unlikely(!apoll))
4407 return false;
4408
4409 req->flags |= REQ_F_POLLED;
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004410 if (req->flags & REQ_F_WORK_INITIALIZED)
4411 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004412 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004413
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004414 io_get_req_task(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004415 req->apoll = apoll;
4416 INIT_HLIST_NODE(&req->hash_node);
4417
Nathan Chancellor8755d972020-03-02 16:01:19 -07004418 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004419 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004420 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004421 if (def->pollout)
4422 mask |= POLLOUT | POLLWRNORM;
4423 mask |= POLLERR | POLLPRI;
4424
4425 ipt.pt._qproc = io_async_queue_proc;
4426
4427 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4428 io_async_wake);
4429 if (ret) {
4430 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004431 /* only remove double add if we did it here */
4432 if (!had_io)
4433 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004434 spin_unlock_irq(&ctx->completion_lock);
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004435 if (req->flags & REQ_F_WORK_INITIALIZED)
4436 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboed7718a92020-02-14 22:23:12 -07004437 kfree(apoll);
4438 return false;
4439 }
4440 spin_unlock_irq(&ctx->completion_lock);
4441 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4442 apoll->poll.events);
4443 return true;
4444}
4445
4446static bool __io_poll_remove_one(struct io_kiocb *req,
4447 struct io_poll_iocb *poll)
4448{
Jens Axboeb41e9852020-02-17 09:52:41 -07004449 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004450
4451 spin_lock(&poll->head->lock);
4452 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004453 if (!list_empty(&poll->wait.entry)) {
4454 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004455 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004456 }
4457 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004458 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004459 return do_complete;
4460}
4461
4462static bool io_poll_remove_one(struct io_kiocb *req)
4463{
4464 bool do_complete;
4465
4466 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004467 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004468 do_complete = __io_poll_remove_one(req, &req->poll);
4469 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004470 struct async_poll *apoll = req->apoll;
4471
Jens Axboed7718a92020-02-14 22:23:12 -07004472 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004473 do_complete = __io_poll_remove_one(req, &apoll->poll);
4474 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004475 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004476 /*
4477 * restore ->work because we will call
4478 * io_req_work_drop_env below when dropping the
4479 * final reference.
4480 */
Xiaoguang Wang405a5d22020-06-10 19:41:20 +08004481 if (req->flags & REQ_F_WORK_INITIALIZED)
4482 memcpy(&req->work, &apoll->work,
4483 sizeof(req->work));
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004484 kfree(apoll);
4485 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004486 }
4487
Jens Axboeb41e9852020-02-17 09:52:41 -07004488 if (do_complete) {
4489 io_cqring_fill_event(req, -ECANCELED);
4490 io_commit_cqring(req->ctx);
4491 req->flags |= REQ_F_COMP_LOCKED;
4492 io_put_req(req);
4493 }
4494
4495 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004496}
4497
4498static void io_poll_remove_all(struct io_ring_ctx *ctx)
4499{
Jens Axboe78076bb2019-12-04 19:56:40 -07004500 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004501 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004502 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004503
4504 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004505 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4506 struct hlist_head *list;
4507
4508 list = &ctx->cancel_hash[i];
4509 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004510 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004511 }
4512 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004513
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004514 if (posted)
4515 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004516}
4517
Jens Axboe47f46762019-11-09 17:43:02 -07004518static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4519{
Jens Axboe78076bb2019-12-04 19:56:40 -07004520 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004521 struct io_kiocb *req;
4522
Jens Axboe78076bb2019-12-04 19:56:40 -07004523 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4524 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004525 if (sqe_addr != req->user_data)
4526 continue;
4527 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004528 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004529 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004530 }
4531
4532 return -ENOENT;
4533}
4534
Jens Axboe3529d8c2019-12-19 18:24:38 -07004535static int io_poll_remove_prep(struct io_kiocb *req,
4536 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004537{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004538 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4539 return -EINVAL;
4540 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4541 sqe->poll_events)
4542 return -EINVAL;
4543
Jens Axboe0969e782019-12-17 18:40:57 -07004544 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004545 return 0;
4546}
4547
4548/*
4549 * Find a running poll command that matches one specified in sqe->addr,
4550 * and remove it if found.
4551 */
4552static int io_poll_remove(struct io_kiocb *req)
4553{
4554 struct io_ring_ctx *ctx = req->ctx;
4555 u64 addr;
4556 int ret;
4557
Jens Axboe0969e782019-12-17 18:40:57 -07004558 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004559 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004560 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004561 spin_unlock_irq(&ctx->completion_lock);
4562
Jens Axboe78e19bb2019-11-06 15:21:34 -07004563 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004564 if (ret < 0)
4565 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004566 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004567 return 0;
4568}
4569
Jens Axboe221c5eb2019-01-17 09:41:58 -07004570static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4571 void *key)
4572{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004573 struct io_kiocb *req = wait->private;
4574 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004575
Jens Axboed7718a92020-02-14 22:23:12 -07004576 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004577}
4578
Jens Axboe221c5eb2019-01-17 09:41:58 -07004579static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4580 struct poll_table_struct *p)
4581{
4582 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4583
Jens Axboed7718a92020-02-14 22:23:12 -07004584 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004585}
4586
Jens Axboe3529d8c2019-12-19 18:24:38 -07004587static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004588{
4589 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004590 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004591
4592 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4593 return -EINVAL;
4594 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4595 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004596 if (!poll->file)
4597 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004598
Jens Axboe221c5eb2019-01-17 09:41:58 -07004599 events = READ_ONCE(sqe->poll_events);
4600 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004601
Pavel Begunkov4dd28242020-06-15 10:33:13 +03004602 io_get_req_task(req);
Jens Axboe0969e782019-12-17 18:40:57 -07004603 return 0;
4604}
4605
Pavel Begunkov014db002020-03-03 21:33:12 +03004606static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004607{
4608 struct io_poll_iocb *poll = &req->poll;
4609 struct io_ring_ctx *ctx = req->ctx;
4610 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004611 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004612
Jens Axboe78076bb2019-12-04 19:56:40 -07004613 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004614 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004615 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004616
Jens Axboed7718a92020-02-14 22:23:12 -07004617 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4618 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004619
Jens Axboe8c838782019-03-12 15:48:16 -06004620 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004621 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004622 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004623 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004624 spin_unlock_irq(&ctx->completion_lock);
4625
Jens Axboe8c838782019-03-12 15:48:16 -06004626 if (mask) {
4627 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004628 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004629 }
Jens Axboe8c838782019-03-12 15:48:16 -06004630 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004631}
4632
Jens Axboe5262f562019-09-17 12:26:57 -06004633static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4634{
Jens Axboead8a48a2019-11-15 08:49:11 -07004635 struct io_timeout_data *data = container_of(timer,
4636 struct io_timeout_data, timer);
4637 struct io_kiocb *req = data->req;
4638 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004639 unsigned long flags;
4640
Jens Axboe5262f562019-09-17 12:26:57 -06004641 atomic_inc(&ctx->cq_timeouts);
4642
4643 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004644 /*
Jens Axboe11365042019-10-16 09:08:32 -06004645 * We could be racing with timeout deletion. If the list is empty,
4646 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004647 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004648 if (!list_empty(&req->list))
Jens Axboe11365042019-10-16 09:08:32 -06004649 list_del_init(&req->list);
Jens Axboe842f9612019-10-29 12:34:10 -06004650
Jens Axboe78e19bb2019-11-06 15:21:34 -07004651 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004652 io_commit_cqring(ctx);
4653 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4654
4655 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004656 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004657 io_put_req(req);
4658 return HRTIMER_NORESTART;
4659}
4660
Jens Axboe47f46762019-11-09 17:43:02 -07004661static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4662{
4663 struct io_kiocb *req;
4664 int ret = -ENOENT;
4665
4666 list_for_each_entry(req, &ctx->timeout_list, list) {
4667 if (user_data == req->user_data) {
4668 list_del_init(&req->list);
4669 ret = 0;
4670 break;
4671 }
4672 }
4673
4674 if (ret == -ENOENT)
4675 return ret;
4676
Jens Axboe2d283902019-12-04 11:08:05 -07004677 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004678 if (ret == -1)
4679 return -EALREADY;
4680
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004681 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004682 io_cqring_fill_event(req, -ECANCELED);
4683 io_put_req(req);
4684 return 0;
4685}
4686
Jens Axboe3529d8c2019-12-19 18:24:38 -07004687static int io_timeout_remove_prep(struct io_kiocb *req,
4688 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004689{
Jens Axboeb29472e2019-12-17 18:50:29 -07004690 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4691 return -EINVAL;
4692 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4693 return -EINVAL;
4694
4695 req->timeout.addr = READ_ONCE(sqe->addr);
4696 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4697 if (req->timeout.flags)
4698 return -EINVAL;
4699
Jens Axboeb29472e2019-12-17 18:50:29 -07004700 return 0;
4701}
4702
Jens Axboe11365042019-10-16 09:08:32 -06004703/*
4704 * Remove or update an existing timeout command
4705 */
Jens Axboefc4df992019-12-10 14:38:45 -07004706static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004707{
4708 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004709 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004710
Jens Axboe11365042019-10-16 09:08:32 -06004711 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004712 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004713
Jens Axboe47f46762019-11-09 17:43:02 -07004714 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004715 io_commit_cqring(ctx);
4716 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004717 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004718 if (ret < 0)
4719 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004720 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004721 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004722}
4723
Jens Axboe3529d8c2019-12-19 18:24:38 -07004724static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004725 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004726{
Jens Axboead8a48a2019-11-15 08:49:11 -07004727 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004728 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03004729 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06004730
Jens Axboead8a48a2019-11-15 08:49:11 -07004731 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004732 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004733 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004734 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03004735 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07004736 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004737 flags = READ_ONCE(sqe->timeout_flags);
4738 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004739 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004740
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004741 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07004742
Jens Axboe3529d8c2019-12-19 18:24:38 -07004743 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004744 return -ENOMEM;
4745
4746 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004747 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004748 req->flags |= REQ_F_TIMEOUT;
4749
4750 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004751 return -EFAULT;
4752
Jens Axboe11365042019-10-16 09:08:32 -06004753 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004754 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004755 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004756 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004757
Jens Axboead8a48a2019-11-15 08:49:11 -07004758 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4759 return 0;
4760}
4761
Jens Axboefc4df992019-12-10 14:38:45 -07004762static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004763{
Jens Axboead8a48a2019-11-15 08:49:11 -07004764 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004765 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004766 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004767 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07004768
Pavel Begunkov733f5c92020-05-26 20:34:03 +03004769 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07004770
Jens Axboe5262f562019-09-17 12:26:57 -06004771 /*
4772 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004773 * timeout event to be satisfied. If it isn't set, then this is
4774 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004775 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004776 if (!off) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07004777 req->flags |= REQ_F_TIMEOUT_NOSEQ;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004778 entry = ctx->timeout_list.prev;
4779 goto add;
4780 }
Jens Axboe5262f562019-09-17 12:26:57 -06004781
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004782 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
4783 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06004784
4785 /*
4786 * Insertion sort, ensuring the first entry in the list is always
4787 * the one we need first.
4788 */
Jens Axboe5262f562019-09-17 12:26:57 -06004789 list_for_each_prev(entry, &ctx->timeout_list) {
4790 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Jens Axboe5262f562019-09-17 12:26:57 -06004791
Jens Axboe93bd25b2019-11-11 23:34:31 -07004792 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4793 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004794 /* nxt.seq is behind @tail, otherwise would've been completed */
4795 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06004796 break;
4797 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07004798add:
Jens Axboe5262f562019-09-17 12:26:57 -06004799 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004800 data->timer.function = io_timeout_fn;
4801 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004802 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004803 return 0;
4804}
4805
Jens Axboe62755e32019-10-28 21:49:21 -06004806static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004807{
Jens Axboe62755e32019-10-28 21:49:21 -06004808 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004809
Jens Axboe62755e32019-10-28 21:49:21 -06004810 return req->user_data == (unsigned long) data;
4811}
4812
Jens Axboee977d6d2019-11-05 12:39:45 -07004813static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004814{
Jens Axboe62755e32019-10-28 21:49:21 -06004815 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004816 int ret = 0;
4817
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03004818 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06004819 switch (cancel_ret) {
4820 case IO_WQ_CANCEL_OK:
4821 ret = 0;
4822 break;
4823 case IO_WQ_CANCEL_RUNNING:
4824 ret = -EALREADY;
4825 break;
4826 case IO_WQ_CANCEL_NOTFOUND:
4827 ret = -ENOENT;
4828 break;
4829 }
4830
Jens Axboee977d6d2019-11-05 12:39:45 -07004831 return ret;
4832}
4833
Jens Axboe47f46762019-11-09 17:43:02 -07004834static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4835 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004836 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004837{
4838 unsigned long flags;
4839 int ret;
4840
4841 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4842 if (ret != -ENOENT) {
4843 spin_lock_irqsave(&ctx->completion_lock, flags);
4844 goto done;
4845 }
4846
4847 spin_lock_irqsave(&ctx->completion_lock, flags);
4848 ret = io_timeout_cancel(ctx, sqe_addr);
4849 if (ret != -ENOENT)
4850 goto done;
4851 ret = io_poll_cancel(ctx, sqe_addr);
4852done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004853 if (!ret)
4854 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004855 io_cqring_fill_event(req, ret);
4856 io_commit_cqring(ctx);
4857 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4858 io_cqring_ev_posted(ctx);
4859
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004860 if (ret < 0)
4861 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004862 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004863}
4864
Jens Axboe3529d8c2019-12-19 18:24:38 -07004865static int io_async_cancel_prep(struct io_kiocb *req,
4866 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004867{
Jens Axboefbf23842019-12-17 18:45:56 -07004868 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004869 return -EINVAL;
4870 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4871 sqe->cancel_flags)
4872 return -EINVAL;
4873
Jens Axboefbf23842019-12-17 18:45:56 -07004874 req->cancel.addr = READ_ONCE(sqe->addr);
4875 return 0;
4876}
4877
Pavel Begunkov014db002020-03-03 21:33:12 +03004878static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004879{
4880 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004881
Pavel Begunkov014db002020-03-03 21:33:12 +03004882 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004883 return 0;
4884}
4885
Jens Axboe05f3fb32019-12-09 11:22:50 -07004886static int io_files_update_prep(struct io_kiocb *req,
4887 const struct io_uring_sqe *sqe)
4888{
4889 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4890 return -EINVAL;
4891
4892 req->files_update.offset = READ_ONCE(sqe->off);
4893 req->files_update.nr_args = READ_ONCE(sqe->len);
4894 if (!req->files_update.nr_args)
4895 return -EINVAL;
4896 req->files_update.arg = READ_ONCE(sqe->addr);
4897 return 0;
4898}
4899
4900static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4901{
4902 struct io_ring_ctx *ctx = req->ctx;
4903 struct io_uring_files_update up;
4904 int ret;
4905
Jens Axboef86cd202020-01-29 13:46:44 -07004906 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004907 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004908
4909 up.offset = req->files_update.offset;
4910 up.fds = req->files_update.arg;
4911
4912 mutex_lock(&ctx->uring_lock);
4913 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4914 mutex_unlock(&ctx->uring_lock);
4915
4916 if (ret < 0)
4917 req_set_fail_links(req);
4918 io_cqring_add_event(req, ret);
4919 io_put_req(req);
4920 return 0;
4921}
4922
Jens Axboe3529d8c2019-12-19 18:24:38 -07004923static int io_req_defer_prep(struct io_kiocb *req,
4924 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004925{
Jens Axboee7815732019-12-17 19:45:06 -07004926 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004927
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004928 if (!sqe)
4929 return 0;
4930
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08004931 io_req_init_async(req);
4932
Jens Axboef86cd202020-01-29 13:46:44 -07004933 if (io_op_defs[req->opcode].file_table) {
4934 ret = io_grab_files(req);
4935 if (unlikely(ret))
4936 return ret;
4937 }
4938
Jens Axboecccf0ee2020-01-27 16:34:48 -07004939 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4940
Jens Axboed625c6e2019-12-17 19:53:05 -07004941 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004942 case IORING_OP_NOP:
4943 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004944 case IORING_OP_READV:
4945 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004946 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004947 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004948 break;
4949 case IORING_OP_WRITEV:
4950 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004951 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004952 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004953 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004954 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004955 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004956 break;
4957 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004958 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004959 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004960 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004961 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004962 break;
4963 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004964 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004965 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004966 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004967 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004968 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004969 break;
4970 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004971 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004972 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004973 break;
Jens Axboef499a022019-12-02 16:28:46 -07004974 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004975 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004976 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004977 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004978 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004979 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004980 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004981 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004982 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004983 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004984 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004985 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004986 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004987 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004988 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004989 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004990 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004991 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004992 case IORING_OP_FALLOCATE:
4993 ret = io_fallocate_prep(req, sqe);
4994 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004995 case IORING_OP_OPENAT:
4996 ret = io_openat_prep(req, sqe);
4997 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004998 case IORING_OP_CLOSE:
4999 ret = io_close_prep(req, sqe);
5000 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005001 case IORING_OP_FILES_UPDATE:
5002 ret = io_files_update_prep(req, sqe);
5003 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005004 case IORING_OP_STATX:
5005 ret = io_statx_prep(req, sqe);
5006 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005007 case IORING_OP_FADVISE:
5008 ret = io_fadvise_prep(req, sqe);
5009 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005010 case IORING_OP_MADVISE:
5011 ret = io_madvise_prep(req, sqe);
5012 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005013 case IORING_OP_OPENAT2:
5014 ret = io_openat2_prep(req, sqe);
5015 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005016 case IORING_OP_EPOLL_CTL:
5017 ret = io_epoll_ctl_prep(req, sqe);
5018 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005019 case IORING_OP_SPLICE:
5020 ret = io_splice_prep(req, sqe);
5021 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005022 case IORING_OP_PROVIDE_BUFFERS:
5023 ret = io_provide_buffers_prep(req, sqe);
5024 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005025 case IORING_OP_REMOVE_BUFFERS:
5026 ret = io_remove_buffers_prep(req, sqe);
5027 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005028 case IORING_OP_TEE:
5029 ret = io_tee_prep(req, sqe);
5030 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005031 default:
Jens Axboee7815732019-12-17 19:45:06 -07005032 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5033 req->opcode);
5034 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005035 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005036 }
5037
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005038 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005039}
5040
Jens Axboe3529d8c2019-12-19 18:24:38 -07005041static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005042{
Jackie Liua197f662019-11-08 08:09:12 -07005043 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005044 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005045
Bob Liu9d858b22019-11-13 18:06:25 +08005046 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005047 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005048 return 0;
5049
Pavel Begunkov650b5482020-05-17 14:02:11 +03005050 if (!req->io) {
5051 if (io_alloc_async_ctx(req))
5052 return -EAGAIN;
5053 ret = io_req_defer_prep(req, sqe);
5054 if (ret < 0)
5055 return ret;
5056 }
Jens Axboe2d283902019-12-04 11:08:05 -07005057
Jens Axboede0617e2019-04-06 21:51:27 -06005058 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005059 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005060 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005061 return 0;
5062 }
5063
Jens Axboe915967f2019-11-21 09:01:20 -07005064 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005065 list_add_tail(&req->list, &ctx->defer_list);
5066 spin_unlock_irq(&ctx->completion_lock);
5067 return -EIOCBQUEUED;
5068}
5069
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005070static void io_cleanup_req(struct io_kiocb *req)
5071{
5072 struct io_async_ctx *io = req->io;
5073
5074 switch (req->opcode) {
5075 case IORING_OP_READV:
5076 case IORING_OP_READ_FIXED:
5077 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005078 if (req->flags & REQ_F_BUFFER_SELECTED)
5079 kfree((void *)(unsigned long)req->rw.addr);
5080 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005081 case IORING_OP_WRITEV:
5082 case IORING_OP_WRITE_FIXED:
5083 case IORING_OP_WRITE:
5084 if (io->rw.iov != io->rw.fast_iov)
5085 kfree(io->rw.iov);
5086 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005087 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005088 if (req->flags & REQ_F_BUFFER_SELECTED)
5089 kfree(req->sr_msg.kbuf);
5090 /* fallthrough */
5091 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005092 if (io->msg.iov != io->msg.fast_iov)
5093 kfree(io->msg.iov);
5094 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005095 case IORING_OP_RECV:
5096 if (req->flags & REQ_F_BUFFER_SELECTED)
5097 kfree(req->sr_msg.kbuf);
5098 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005099 case IORING_OP_OPENAT:
5100 case IORING_OP_OPENAT2:
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005101 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005102 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005103 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005104 io_put_file(req, req->splice.file_in,
5105 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5106 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005107 }
5108
5109 req->flags &= ~REQ_F_NEED_CLEANUP;
5110}
5111
Jens Axboe3529d8c2019-12-19 18:24:38 -07005112static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005113 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005114{
Jackie Liua197f662019-11-08 08:09:12 -07005115 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005116 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005117
Jens Axboed625c6e2019-12-17 19:53:05 -07005118 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005119 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005120 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005121 break;
5122 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005123 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005124 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005125 if (sqe) {
5126 ret = io_read_prep(req, sqe, force_nonblock);
5127 if (ret < 0)
5128 break;
5129 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005130 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005131 break;
5132 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005133 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005134 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005135 if (sqe) {
5136 ret = io_write_prep(req, sqe, force_nonblock);
5137 if (ret < 0)
5138 break;
5139 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005140 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005141 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005142 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005143 if (sqe) {
5144 ret = io_prep_fsync(req, sqe);
5145 if (ret < 0)
5146 break;
5147 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005148 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005149 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005150 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005151 if (sqe) {
5152 ret = io_poll_add_prep(req, sqe);
5153 if (ret)
5154 break;
5155 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005156 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005157 break;
5158 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005159 if (sqe) {
5160 ret = io_poll_remove_prep(req, sqe);
5161 if (ret < 0)
5162 break;
5163 }
Jens Axboefc4df992019-12-10 14:38:45 -07005164 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005165 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005166 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005167 if (sqe) {
5168 ret = io_prep_sfr(req, sqe);
5169 if (ret < 0)
5170 break;
5171 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005172 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005173 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005174 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005175 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005176 if (sqe) {
5177 ret = io_sendmsg_prep(req, sqe);
5178 if (ret < 0)
5179 break;
5180 }
Jens Axboefddafac2020-01-04 20:19:44 -07005181 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005182 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005183 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005184 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005185 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005186 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005187 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005188 if (sqe) {
5189 ret = io_recvmsg_prep(req, sqe);
5190 if (ret)
5191 break;
5192 }
Jens Axboefddafac2020-01-04 20:19:44 -07005193 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005194 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005195 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005196 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005197 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005198 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005199 if (sqe) {
5200 ret = io_timeout_prep(req, sqe, false);
5201 if (ret)
5202 break;
5203 }
Jens Axboefc4df992019-12-10 14:38:45 -07005204 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005205 break;
Jens Axboe11365042019-10-16 09:08:32 -06005206 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005207 if (sqe) {
5208 ret = io_timeout_remove_prep(req, sqe);
5209 if (ret)
5210 break;
5211 }
Jens Axboefc4df992019-12-10 14:38:45 -07005212 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005213 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005214 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005215 if (sqe) {
5216 ret = io_accept_prep(req, sqe);
5217 if (ret)
5218 break;
5219 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005220 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005221 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005222 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005223 if (sqe) {
5224 ret = io_connect_prep(req, sqe);
5225 if (ret)
5226 break;
5227 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005228 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005229 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005230 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005231 if (sqe) {
5232 ret = io_async_cancel_prep(req, sqe);
5233 if (ret)
5234 break;
5235 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005236 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005237 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005238 case IORING_OP_FALLOCATE:
5239 if (sqe) {
5240 ret = io_fallocate_prep(req, sqe);
5241 if (ret)
5242 break;
5243 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005244 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005245 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005246 case IORING_OP_OPENAT:
5247 if (sqe) {
5248 ret = io_openat_prep(req, sqe);
5249 if (ret)
5250 break;
5251 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005252 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005253 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005254 case IORING_OP_CLOSE:
5255 if (sqe) {
5256 ret = io_close_prep(req, sqe);
5257 if (ret)
5258 break;
5259 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005260 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005261 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005262 case IORING_OP_FILES_UPDATE:
5263 if (sqe) {
5264 ret = io_files_update_prep(req, sqe);
5265 if (ret)
5266 break;
5267 }
5268 ret = io_files_update(req, force_nonblock);
5269 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005270 case IORING_OP_STATX:
5271 if (sqe) {
5272 ret = io_statx_prep(req, sqe);
5273 if (ret)
5274 break;
5275 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005276 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005277 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005278 case IORING_OP_FADVISE:
5279 if (sqe) {
5280 ret = io_fadvise_prep(req, sqe);
5281 if (ret)
5282 break;
5283 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005284 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005285 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005286 case IORING_OP_MADVISE:
5287 if (sqe) {
5288 ret = io_madvise_prep(req, sqe);
5289 if (ret)
5290 break;
5291 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005292 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005293 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005294 case IORING_OP_OPENAT2:
5295 if (sqe) {
5296 ret = io_openat2_prep(req, sqe);
5297 if (ret)
5298 break;
5299 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005300 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005301 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005302 case IORING_OP_EPOLL_CTL:
5303 if (sqe) {
5304 ret = io_epoll_ctl_prep(req, sqe);
5305 if (ret)
5306 break;
5307 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005308 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005309 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005310 case IORING_OP_SPLICE:
5311 if (sqe) {
5312 ret = io_splice_prep(req, sqe);
5313 if (ret < 0)
5314 break;
5315 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005316 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005317 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005318 case IORING_OP_PROVIDE_BUFFERS:
5319 if (sqe) {
5320 ret = io_provide_buffers_prep(req, sqe);
5321 if (ret)
5322 break;
5323 }
5324 ret = io_provide_buffers(req, force_nonblock);
5325 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005326 case IORING_OP_REMOVE_BUFFERS:
5327 if (sqe) {
5328 ret = io_remove_buffers_prep(req, sqe);
5329 if (ret)
5330 break;
5331 }
5332 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005333 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005334 case IORING_OP_TEE:
5335 if (sqe) {
5336 ret = io_tee_prep(req, sqe);
5337 if (ret < 0)
5338 break;
5339 }
5340 ret = io_tee(req, force_nonblock);
5341 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005342 default:
5343 ret = -EINVAL;
5344 break;
5345 }
5346
5347 if (ret)
5348 return ret;
5349
Jens Axboeb5325762020-05-19 21:20:27 -06005350 /* If the op doesn't have a file, we're not polling for it */
5351 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005352 const bool in_async = io_wq_current_is_worker();
5353
Jens Axboe11ba8202020-01-15 21:51:17 -07005354 /* workqueue context doesn't hold uring_lock, grab it now */
5355 if (in_async)
5356 mutex_lock(&ctx->uring_lock);
5357
Jens Axboe2b188cc2019-01-07 10:46:33 -07005358 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005359
5360 if (in_async)
5361 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005362 }
5363
5364 return 0;
5365}
5366
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005367static void io_arm_async_linked_timeout(struct io_kiocb *req)
5368{
5369 struct io_kiocb *link;
5370
5371 /* link head's timeout is queued in io_queue_async_work() */
5372 if (!(req->flags & REQ_F_QUEUE_TIMEOUT))
5373 return;
5374
5375 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
5376 io_queue_linked_timeout(link);
5377}
5378
Jens Axboe561fb042019-10-24 07:25:42 -06005379static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboedef596e2019-01-09 08:59:42 -07005380{
Jens Axboe561fb042019-10-24 07:25:42 -06005381 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005382 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005383 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005384
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03005385 io_arm_async_linked_timeout(req);
5386
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005387 /* if NO_CANCEL is set, we must still run the work */
5388 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5389 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005390 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005391 }
Jens Axboe31b51512019-01-18 22:56:34 -07005392
Jens Axboe561fb042019-10-24 07:25:42 -06005393 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005394 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005395 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005396 /*
5397 * We can get EAGAIN for polled IO even though we're
5398 * forcing a sync submission from here, since we can't
5399 * wait for request slots on the block side.
5400 */
5401 if (ret != -EAGAIN)
5402 break;
5403 cond_resched();
5404 } while (1);
5405 }
Jens Axboe31b51512019-01-18 22:56:34 -07005406
Jens Axboe561fb042019-10-24 07:25:42 -06005407 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005408 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005409 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005410 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005411 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005412
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005413 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005414}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005415
Jens Axboe65e19f52019-10-26 07:20:21 -06005416static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5417 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005418{
Jens Axboe65e19f52019-10-26 07:20:21 -06005419 struct fixed_file_table *table;
5420
Jens Axboe05f3fb32019-12-09 11:22:50 -07005421 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005422 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005423}
5424
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005425static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5426 int fd, struct file **out_file, bool fixed)
5427{
5428 struct io_ring_ctx *ctx = req->ctx;
5429 struct file *file;
5430
5431 if (fixed) {
5432 if (unlikely(!ctx->file_data ||
5433 (unsigned) fd >= ctx->nr_user_files))
5434 return -EBADF;
5435 fd = array_index_nospec(fd, ctx->nr_user_files);
5436 file = io_file_from_index(ctx, fd);
Jens Axboefd2206e2020-06-02 16:40:47 -06005437 if (file) {
5438 req->fixed_file_refs = ctx->file_data->cur_refs;
5439 percpu_ref_get(req->fixed_file_refs);
5440 }
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005441 } else {
5442 trace_io_uring_file_get(ctx, fd);
5443 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005444 }
5445
Jens Axboefd2206e2020-06-02 16:40:47 -06005446 if (file || io_op_defs[req->opcode].needs_file_no_error) {
5447 *out_file = file;
5448 return 0;
5449 }
5450 return -EBADF;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005451}
5452
Jens Axboe3529d8c2019-12-19 18:24:38 -07005453static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005454 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005455{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005456 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005457
Jens Axboe63ff8222020-05-07 14:56:15 -06005458 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005459 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005460 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005461
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005462 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005463}
5464
Jackie Liua197f662019-11-08 08:09:12 -07005465static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005466{
Jens Axboefcb323c2019-10-24 12:39:47 -06005467 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005468 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005469
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005470 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005471 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005472 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005473 return -EBADF;
5474
Jens Axboefcb323c2019-10-24 12:39:47 -06005475 rcu_read_lock();
5476 spin_lock_irq(&ctx->inflight_lock);
5477 /*
5478 * We use the f_ops->flush() handler to ensure that we can flush
5479 * out work accessing these files if the fd is closed. Check if
5480 * the fd has changed since we started down this path, and disallow
5481 * this operation if it has.
5482 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005483 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005484 list_add(&req->inflight_entry, &ctx->inflight_list);
5485 req->flags |= REQ_F_INFLIGHT;
5486 req->work.files = current->files;
5487 ret = 0;
5488 }
5489 spin_unlock_irq(&ctx->inflight_lock);
5490 rcu_read_unlock();
5491
5492 return ret;
5493}
5494
Jens Axboe2665abf2019-11-05 12:40:47 -07005495static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5496{
Jens Axboead8a48a2019-11-15 08:49:11 -07005497 struct io_timeout_data *data = container_of(timer,
5498 struct io_timeout_data, timer);
5499 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005500 struct io_ring_ctx *ctx = req->ctx;
5501 struct io_kiocb *prev = NULL;
5502 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005503
5504 spin_lock_irqsave(&ctx->completion_lock, flags);
5505
5506 /*
5507 * We don't expect the list to be empty, that will only happen if we
5508 * race with the completion of the linked work.
5509 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005510 if (!list_empty(&req->link_list)) {
5511 prev = list_entry(req->link_list.prev, struct io_kiocb,
5512 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005513 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005514 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005515 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5516 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005517 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005518 }
5519
5520 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5521
5522 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005523 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005524 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005525 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005526 } else {
5527 io_cqring_add_event(req, -ETIME);
5528 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005529 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005530 return HRTIMER_NORESTART;
5531}
5532
Jens Axboead8a48a2019-11-15 08:49:11 -07005533static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005534{
Jens Axboe76a46e02019-11-10 23:34:16 -07005535 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005536
Jens Axboe76a46e02019-11-10 23:34:16 -07005537 /*
5538 * If the list is now empty, then our linked request finished before
5539 * we got a chance to setup the timer
5540 */
5541 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005542 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005543 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005544
Jens Axboead8a48a2019-11-15 08:49:11 -07005545 data->timer.function = io_link_timeout_fn;
5546 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5547 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005548 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005549 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005550
Jens Axboe2665abf2019-11-05 12:40:47 -07005551 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005552 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005553}
5554
Jens Axboead8a48a2019-11-15 08:49:11 -07005555static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005556{
5557 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005558
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005559 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005560 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005561 /* for polled retry, if flag is set, we already went through here */
5562 if (req->flags & REQ_F_POLLED)
5563 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005564
Pavel Begunkov44932332019-12-05 16:16:35 +03005565 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5566 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005567 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005568 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005569
Jens Axboe76a46e02019-11-10 23:34:16 -07005570 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005571 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005572}
5573
Jens Axboe3529d8c2019-12-19 18:24:38 -07005574static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005575{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005576 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005577 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005578 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005579 int ret;
5580
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005581again:
5582 linked_timeout = io_prep_linked_timeout(req);
5583
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005584 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
5585 req->work.creds != current_cred()) {
Jens Axboe193155c2020-02-22 23:22:19 -07005586 if (old_creds)
5587 revert_creds(old_creds);
5588 if (old_creds == req->work.creds)
5589 old_creds = NULL; /* restored original creds */
5590 else
5591 old_creds = override_creds(req->work.creds);
5592 }
5593
Pavel Begunkov014db002020-03-03 21:33:12 +03005594 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005595
5596 /*
5597 * We async punt it if the file wasn't marked NOWAIT, or if the file
5598 * doesn't support non-blocking read/write attempts
5599 */
5600 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5601 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005602 if (io_arm_poll_handler(req)) {
5603 if (linked_timeout)
5604 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005605 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005606 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005607punt:
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005608 io_req_init_async(req);
5609
Jens Axboef86cd202020-01-29 13:46:44 -07005610 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005611 ret = io_grab_files(req);
5612 if (ret)
5613 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005614 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005615
5616 /*
5617 * Queued up for async execution, worker will release
5618 * submit reference when the iocb is actually submitted.
5619 */
5620 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005621 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005622 }
Jens Axboee65ef562019-03-12 10:16:44 -06005623
Jens Axboefcb323c2019-10-24 12:39:47 -06005624err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005625 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005626 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005627 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005628
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005629 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005630 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005631 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005632 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005633 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005634 }
5635
Jens Axboee65ef562019-03-12 10:16:44 -06005636 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005637 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005638 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005639 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005640 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005641 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005642 if (nxt) {
5643 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005644
5645 if (req->flags & REQ_F_FORCE_ASYNC)
5646 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005647 goto again;
5648 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005649exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005650 if (old_creds)
5651 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005652}
5653
Jens Axboe3529d8c2019-12-19 18:24:38 -07005654static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005655{
5656 int ret;
5657
Jens Axboe3529d8c2019-12-19 18:24:38 -07005658 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005659 if (ret) {
5660 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005661fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005662 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005663 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005664 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005665 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005666 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005667 if (!req->io) {
5668 ret = -EAGAIN;
5669 if (io_alloc_async_ctx(req))
5670 goto fail_req;
5671 ret = io_req_defer_prep(req, sqe);
5672 if (unlikely(ret < 0))
5673 goto fail_req;
5674 }
5675
Jens Axboece35a472019-12-17 08:04:44 -07005676 /*
5677 * Never try inline submit of IOSQE_ASYNC is set, go straight
5678 * to async execution.
5679 */
5680 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5681 io_queue_async_work(req);
5682 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005683 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005684 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005685}
5686
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005687static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005688{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005689 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005690 io_cqring_add_event(req, -ECANCELED);
5691 io_double_put_req(req);
5692 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005693 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005694}
5695
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005696static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005697 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005698{
Jackie Liua197f662019-11-08 08:09:12 -07005699 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005700 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06005701
Jens Axboe9e645e112019-05-10 16:07:28 -06005702 /*
5703 * If we already have a head request, queue this one for async
5704 * submittal once the head completes. If we don't have a head but
5705 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5706 * submitted sync once the chain is complete. If none of those
5707 * conditions are true (normal request), then just queue it.
5708 */
5709 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005710 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005711
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005712 /*
5713 * Taking sequential execution of a link, draining both sides
5714 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5715 * requests in the link. So, it drains the head and the
5716 * next after the link request. The last one is done via
5717 * drain_next flag to persist the effect across calls.
5718 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005719 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03005720 head->flags |= REQ_F_IO_DRAIN;
5721 ctx->drain_next = 1;
5722 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005723 if (io_alloc_async_ctx(req))
5724 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06005725
Jens Axboe3529d8c2019-12-19 18:24:38 -07005726 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005727 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005728 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005729 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005730 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005731 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005732 trace_io_uring_link(ctx, req, head);
5733 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005734
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005735 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005736 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005737 io_queue_link_head(head);
5738 *link = NULL;
5739 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005740 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005741 if (unlikely(ctx->drain_next)) {
5742 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005743 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03005744 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005745 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005746 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03005747 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005748
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005749 if (io_alloc_async_ctx(req))
5750 return -EAGAIN;
5751
Pavel Begunkov711be032020-01-17 03:57:59 +03005752 ret = io_req_defer_prep(req, sqe);
5753 if (ret)
5754 req->flags |= REQ_F_FAIL_LINK;
5755 *link = req;
5756 } else {
5757 io_queue_sqe(req, sqe);
5758 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005759 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005760
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005761 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06005762}
5763
Jens Axboe9a56a232019-01-09 09:06:50 -07005764/*
5765 * Batched submission is done, ensure local IO is flushed out.
5766 */
5767static void io_submit_state_end(struct io_submit_state *state)
5768{
5769 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03005770 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005771 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005772 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005773}
5774
5775/*
5776 * Start submission side cache.
5777 */
5778static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005779 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005780{
5781 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005782 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005783 state->file = NULL;
5784 state->ios_left = max_ios;
5785}
5786
Jens Axboe2b188cc2019-01-07 10:46:33 -07005787static void io_commit_sqring(struct io_ring_ctx *ctx)
5788{
Hristo Venev75b28af2019-08-26 17:23:46 +00005789 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005790
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005791 /*
5792 * Ensure any loads from the SQEs are done at this point,
5793 * since once we write the new head, the application could
5794 * write new data to them.
5795 */
5796 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005797}
5798
5799/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005800 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005801 * that is mapped by userspace. This means that care needs to be taken to
5802 * ensure that reads are stable, as we cannot rely on userspace always
5803 * being a good citizen. If members of the sqe are validated and then later
5804 * used, it's important that those reads are done through READ_ONCE() to
5805 * prevent a re-load down the line.
5806 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03005807static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005808{
Hristo Venev75b28af2019-08-26 17:23:46 +00005809 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005810 unsigned head;
5811
5812 /*
5813 * The cached sq head (or cq tail) serves two purposes:
5814 *
5815 * 1) allows us to batch the cost of updating the user visible
5816 * head updates.
5817 * 2) allows the kernel side to track the head on its own, even
5818 * though the application is the one updating it.
5819 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005820 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005821 if (likely(head < ctx->sq_entries))
5822 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07005823
5824 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06005825 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005826 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005827 return NULL;
5828}
5829
5830static inline void io_consume_sqe(struct io_ring_ctx *ctx)
5831{
5832 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005833}
5834
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005835#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5836 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5837 IOSQE_BUFFER_SELECT)
5838
5839static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
5840 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005841 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005842{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005843 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06005844 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005845
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005846 /*
5847 * All io need record the previous position, if LINK vs DARIN,
5848 * it can be used to mark the position of the first IO in the
5849 * link list.
5850 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03005851 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005852 req->opcode = READ_ONCE(sqe->opcode);
5853 req->user_data = READ_ONCE(sqe->user_data);
5854 req->io = NULL;
5855 req->file = NULL;
5856 req->ctx = ctx;
5857 req->flags = 0;
5858 /* one is dropped after submission, the other at completion */
5859 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03005860 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005861 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005862
5863 if (unlikely(req->opcode >= IORING_OP_LAST))
5864 return -EINVAL;
5865
Jens Axboe9d8426a2020-06-16 18:42:49 -06005866 if (unlikely(io_sq_thread_acquire_mm(ctx, req)))
5867 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005868
5869 sqe_flags = READ_ONCE(sqe->flags);
5870 /* enforce forwards compatibility on users */
5871 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
5872 return -EINVAL;
5873
5874 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5875 !io_op_defs[req->opcode].buffer_select)
5876 return -EOPNOTSUPP;
5877
5878 id = READ_ONCE(sqe->personality);
5879 if (id) {
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08005880 io_req_init_async(req);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005881 req->work.creds = idr_find(&ctx->personality_idr, id);
5882 if (unlikely(!req->work.creds))
5883 return -EINVAL;
5884 get_cred(req->work.creds);
5885 }
5886
5887 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03005888 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005889
Jens Axboe63ff8222020-05-07 14:56:15 -06005890 if (!io_op_defs[req->opcode].needs_file)
5891 return 0;
5892
5893 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005894}
5895
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005896static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005897 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005898{
5899 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005900 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005901 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005902
Jens Axboec4a2ed72019-11-21 21:01:26 -07005903 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005904 if (test_bit(0, &ctx->sq_check_overflow)) {
5905 if (!list_empty(&ctx->cq_overflow_list) &&
5906 !io_cqring_overflow_flush(ctx, false))
5907 return -EBUSY;
5908 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005909
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005910 /* make sure SQ entry isn't read before tail */
5911 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005912
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005913 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5914 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005915
5916 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005917 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005918 statep = &state;
5919 }
5920
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005921 ctx->ring_fd = ring_fd;
5922 ctx->ring_file = ring_file;
5923
Jens Axboe6c271ce2019-01-10 11:22:30 -07005924 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005925 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005926 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005927 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005928
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03005929 sqe = io_get_sqe(ctx);
5930 if (unlikely(!sqe)) {
5931 io_consume_sqe(ctx);
5932 break;
5933 }
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005934 req = io_alloc_req(ctx, statep);
Pavel Begunkov196be952019-11-07 01:41:06 +03005935 if (unlikely(!req)) {
5936 if (!submitted)
5937 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005938 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005939 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005940
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005941 err = io_init_req(ctx, req, sqe, statep);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005942 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07005943 /* will complete beyond this point, count as submitted */
5944 submitted++;
5945
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005946 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005947fail_req:
5948 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005949 io_double_put_req(req);
5950 break;
5951 }
5952
Jens Axboe354420f2020-01-08 18:55:15 -07005953 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005954 true, io_async_submit(ctx));
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005955 err = io_submit_sqe(req, sqe, &link);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005956 if (err)
5957 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005958 }
5959
Pavel Begunkov9466f432020-01-25 22:34:01 +03005960 if (unlikely(submitted != nr)) {
5961 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5962
5963 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5964 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005965 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005966 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005967 if (statep)
5968 io_submit_state_end(&state);
5969
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005970 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5971 io_commit_sqring(ctx);
5972
Jens Axboe6c271ce2019-01-10 11:22:30 -07005973 return submitted;
5974}
5975
5976static int io_sq_thread(void *data)
5977{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005978 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07005979 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005980 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005981 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005982 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005983
Jens Axboe0f158b42020-05-14 17:18:39 -06005984 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005985
Jens Axboe181e4482019-11-25 08:52:30 -07005986 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005987
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005988 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005989 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005990 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005991
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005992 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005993 unsigned nr_events = 0;
5994
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005995 mutex_lock(&ctx->uring_lock);
5996 if (!list_empty(&ctx->poll_list))
5997 io_iopoll_getevents(ctx, &nr_events, 0);
5998 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005999 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006000 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006001 }
6002
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006003 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006004
6005 /*
6006 * If submit got -EBUSY, flag us as needing the application
6007 * to enter the kernel to reap and flush events.
6008 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006009 if (!to_submit || ret == -EBUSY || need_resched()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006010 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006011 * Drop cur_mm before scheduling, we can't hold it for
6012 * long periods (or over schedule()). Do this before
6013 * adding ourselves to the waitqueue, as the unuse/drop
6014 * may sleep.
6015 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006016 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006017
6018 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006019 * We're polling. If we're within the defined idle
6020 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006021 * to sleep. The exception is if we got EBUSY doing
6022 * more IO, we should wait for the application to
6023 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006024 */
Xuan Zhuob772f072020-06-23 19:34:06 +08006025 if (!list_empty(&ctx->poll_list) || need_resched() ||
Jens Axboedf069d82020-02-04 16:48:34 -07006026 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6027 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006028 if (current->task_works)
6029 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006030 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006031 continue;
6032 }
6033
Jens Axboe6c271ce2019-01-10 11:22:30 -07006034 prepare_to_wait(&ctx->sqo_wait, &wait,
6035 TASK_INTERRUPTIBLE);
6036
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006037 /*
6038 * While doing polled IO, before going to sleep, we need
6039 * to check if there are new reqs added to poll_list, it
6040 * is because reqs may have been punted to io worker and
6041 * will be added to poll_list later, hence check the
6042 * poll_list again.
6043 */
6044 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6045 !list_empty_careful(&ctx->poll_list)) {
6046 finish_wait(&ctx->sqo_wait, &wait);
6047 continue;
6048 }
6049
Jens Axboe6c271ce2019-01-10 11:22:30 -07006050 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006051 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006052 /* make sure to read SQ tail after writing flags */
6053 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006054
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006055 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006056 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006057 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006058 finish_wait(&ctx->sqo_wait, &wait);
6059 break;
6060 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006061 if (current->task_works) {
6062 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006063 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006064 continue;
6065 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006066 if (signal_pending(current))
6067 flush_signals(current);
6068 schedule();
6069 finish_wait(&ctx->sqo_wait, &wait);
6070
Hristo Venev75b28af2019-08-26 17:23:46 +00006071 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006072 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006073 continue;
6074 }
6075 finish_wait(&ctx->sqo_wait, &wait);
6076
Hristo Venev75b28af2019-08-26 17:23:46 +00006077 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006078 }
6079
Jens Axboe8a4955f2019-12-09 14:52:35 -07006080 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006081 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6082 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006083 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006084 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006085 }
6086
Jens Axboeb41e9852020-02-17 09:52:41 -07006087 if (current->task_works)
6088 task_work_run();
6089
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006090 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006091 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006092
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006093 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006094
Jens Axboe6c271ce2019-01-10 11:22:30 -07006095 return 0;
6096}
6097
Jens Axboebda52162019-09-24 13:47:15 -06006098struct io_wait_queue {
6099 struct wait_queue_entry wq;
6100 struct io_ring_ctx *ctx;
6101 unsigned to_wait;
6102 unsigned nr_timeouts;
6103};
6104
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006105static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006106{
6107 struct io_ring_ctx *ctx = iowq->ctx;
6108
6109 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006110 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006111 * started waiting. For timeouts, we always want to return to userspace,
6112 * regardless of event count.
6113 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006114 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006115 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6116}
6117
6118static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6119 int wake_flags, void *key)
6120{
6121 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6122 wq);
6123
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006124 /* use noflush == true, as we can't safely rely on locking context */
6125 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006126 return -1;
6127
6128 return autoremove_wake_function(curr, mode, wake_flags, key);
6129}
6130
Jens Axboe2b188cc2019-01-07 10:46:33 -07006131/*
6132 * Wait until events become available, if we don't already have some. The
6133 * application must reap them itself, as they reside on the shared cq ring.
6134 */
6135static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6136 const sigset_t __user *sig, size_t sigsz)
6137{
Jens Axboebda52162019-09-24 13:47:15 -06006138 struct io_wait_queue iowq = {
6139 .wq = {
6140 .private = current,
6141 .func = io_wake_function,
6142 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6143 },
6144 .ctx = ctx,
6145 .to_wait = min_events,
6146 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006147 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006148 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006149
Jens Axboeb41e9852020-02-17 09:52:41 -07006150 do {
6151 if (io_cqring_events(ctx, false) >= min_events)
6152 return 0;
6153 if (!current->task_works)
6154 break;
6155 task_work_run();
6156 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006157
6158 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006159#ifdef CONFIG_COMPAT
6160 if (in_compat_syscall())
6161 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006162 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006163 else
6164#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006165 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006166
Jens Axboe2b188cc2019-01-07 10:46:33 -07006167 if (ret)
6168 return ret;
6169 }
6170
Jens Axboebda52162019-09-24 13:47:15 -06006171 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006172 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006173 do {
6174 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6175 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006176 if (current->task_works)
6177 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006178 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006179 break;
6180 schedule();
6181 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006182 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006183 break;
6184 }
6185 } while (1);
6186 finish_wait(&ctx->wait, &iowq.wq);
6187
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006188 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006189
Hristo Venev75b28af2019-08-26 17:23:46 +00006190 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006191}
6192
Jens Axboe6b063142019-01-10 22:13:58 -07006193static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6194{
6195#if defined(CONFIG_UNIX)
6196 if (ctx->ring_sock) {
6197 struct sock *sock = ctx->ring_sock->sk;
6198 struct sk_buff *skb;
6199
6200 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6201 kfree_skb(skb);
6202 }
6203#else
6204 int i;
6205
Jens Axboe65e19f52019-10-26 07:20:21 -06006206 for (i = 0; i < ctx->nr_user_files; i++) {
6207 struct file *file;
6208
6209 file = io_file_from_index(ctx, i);
6210 if (file)
6211 fput(file);
6212 }
Jens Axboe6b063142019-01-10 22:13:58 -07006213#endif
6214}
6215
Jens Axboe05f3fb32019-12-09 11:22:50 -07006216static void io_file_ref_kill(struct percpu_ref *ref)
6217{
6218 struct fixed_file_data *data;
6219
6220 data = container_of(ref, struct fixed_file_data, refs);
6221 complete(&data->done);
6222}
6223
Jens Axboe6b063142019-01-10 22:13:58 -07006224static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6225{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006226 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006227 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006228 unsigned nr_tables, i;
6229
Jens Axboe05f3fb32019-12-09 11:22:50 -07006230 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006231 return -ENXIO;
6232
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006233 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006234 if (!list_empty(&data->ref_list))
6235 ref_node = list_first_entry(&data->ref_list,
6236 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006237 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006238 if (ref_node)
6239 percpu_ref_kill(&ref_node->refs);
6240
6241 percpu_ref_kill(&data->refs);
6242
6243 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006244 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006245 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006246
Jens Axboe6b063142019-01-10 22:13:58 -07006247 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006248 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6249 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006250 kfree(data->table[i].files);
6251 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006252 percpu_ref_exit(&data->refs);
6253 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006254 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006255 ctx->nr_user_files = 0;
6256 return 0;
6257}
6258
Jens Axboe6c271ce2019-01-10 11:22:30 -07006259static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6260{
6261 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006262 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006263 /*
6264 * The park is a bit of a work-around, without it we get
6265 * warning spews on shutdown with SQPOLL set and affinity
6266 * set to a single CPU.
6267 */
Jens Axboe06058632019-04-13 09:26:03 -06006268 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006269 kthread_stop(ctx->sqo_thread);
6270 ctx->sqo_thread = NULL;
6271 }
6272}
6273
Jens Axboe6b063142019-01-10 22:13:58 -07006274static void io_finish_async(struct io_ring_ctx *ctx)
6275{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006276 io_sq_thread_stop(ctx);
6277
Jens Axboe561fb042019-10-24 07:25:42 -06006278 if (ctx->io_wq) {
6279 io_wq_destroy(ctx->io_wq);
6280 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006281 }
6282}
6283
6284#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006285/*
6286 * Ensure the UNIX gc is aware of our file set, so we are certain that
6287 * the io_uring can be safely unregistered on process exit, even if we have
6288 * loops in the file referencing.
6289 */
6290static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6291{
6292 struct sock *sk = ctx->ring_sock->sk;
6293 struct scm_fp_list *fpl;
6294 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006295 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006296
Jens Axboe6b063142019-01-10 22:13:58 -07006297 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6298 if (!fpl)
6299 return -ENOMEM;
6300
6301 skb = alloc_skb(0, GFP_KERNEL);
6302 if (!skb) {
6303 kfree(fpl);
6304 return -ENOMEM;
6305 }
6306
6307 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006308
Jens Axboe08a45172019-10-03 08:11:03 -06006309 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006310 fpl->user = get_uid(ctx->user);
6311 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006312 struct file *file = io_file_from_index(ctx, i + offset);
6313
6314 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006315 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006316 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006317 unix_inflight(fpl->user, fpl->fp[nr_files]);
6318 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006319 }
6320
Jens Axboe08a45172019-10-03 08:11:03 -06006321 if (nr_files) {
6322 fpl->max = SCM_MAX_FD;
6323 fpl->count = nr_files;
6324 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006325 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006326 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6327 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006328
Jens Axboe08a45172019-10-03 08:11:03 -06006329 for (i = 0; i < nr_files; i++)
6330 fput(fpl->fp[i]);
6331 } else {
6332 kfree_skb(skb);
6333 kfree(fpl);
6334 }
Jens Axboe6b063142019-01-10 22:13:58 -07006335
6336 return 0;
6337}
6338
6339/*
6340 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6341 * causes regular reference counting to break down. We rely on the UNIX
6342 * garbage collection to take care of this problem for us.
6343 */
6344static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6345{
6346 unsigned left, total;
6347 int ret = 0;
6348
6349 total = 0;
6350 left = ctx->nr_user_files;
6351 while (left) {
6352 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006353
6354 ret = __io_sqe_files_scm(ctx, this_files, total);
6355 if (ret)
6356 break;
6357 left -= this_files;
6358 total += this_files;
6359 }
6360
6361 if (!ret)
6362 return 0;
6363
6364 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006365 struct file *file = io_file_from_index(ctx, total);
6366
6367 if (file)
6368 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006369 total++;
6370 }
6371
6372 return ret;
6373}
6374#else
6375static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6376{
6377 return 0;
6378}
6379#endif
6380
Jens Axboe65e19f52019-10-26 07:20:21 -06006381static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6382 unsigned nr_files)
6383{
6384 int i;
6385
6386 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006387 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006388 unsigned this_files;
6389
6390 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6391 table->files = kcalloc(this_files, sizeof(struct file *),
6392 GFP_KERNEL);
6393 if (!table->files)
6394 break;
6395 nr_files -= this_files;
6396 }
6397
6398 if (i == nr_tables)
6399 return 0;
6400
6401 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006402 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006403 kfree(table->files);
6404 }
6405 return 1;
6406}
6407
Jens Axboe05f3fb32019-12-09 11:22:50 -07006408static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006409{
6410#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006411 struct sock *sock = ctx->ring_sock->sk;
6412 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6413 struct sk_buff *skb;
6414 int i;
6415
6416 __skb_queue_head_init(&list);
6417
6418 /*
6419 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6420 * remove this entry and rearrange the file array.
6421 */
6422 skb = skb_dequeue(head);
6423 while (skb) {
6424 struct scm_fp_list *fp;
6425
6426 fp = UNIXCB(skb).fp;
6427 for (i = 0; i < fp->count; i++) {
6428 int left;
6429
6430 if (fp->fp[i] != file)
6431 continue;
6432
6433 unix_notinflight(fp->user, fp->fp[i]);
6434 left = fp->count - 1 - i;
6435 if (left) {
6436 memmove(&fp->fp[i], &fp->fp[i + 1],
6437 left * sizeof(struct file *));
6438 }
6439 fp->count--;
6440 if (!fp->count) {
6441 kfree_skb(skb);
6442 skb = NULL;
6443 } else {
6444 __skb_queue_tail(&list, skb);
6445 }
6446 fput(file);
6447 file = NULL;
6448 break;
6449 }
6450
6451 if (!file)
6452 break;
6453
6454 __skb_queue_tail(&list, skb);
6455
6456 skb = skb_dequeue(head);
6457 }
6458
6459 if (skb_peek(&list)) {
6460 spin_lock_irq(&head->lock);
6461 while ((skb = __skb_dequeue(&list)) != NULL)
6462 __skb_queue_tail(head, skb);
6463 spin_unlock_irq(&head->lock);
6464 }
6465#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006466 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006467#endif
6468}
6469
Jens Axboe05f3fb32019-12-09 11:22:50 -07006470struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006471 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006472 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006473};
6474
Jens Axboe4a38aed22020-05-14 17:21:15 -06006475static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006476{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006477 struct fixed_file_data *file_data = ref_node->file_data;
6478 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006479 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006480
6481 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006482 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006483 io_ring_file_put(ctx, pfile->file);
6484 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006485 }
6486
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006487 spin_lock(&file_data->lock);
6488 list_del(&ref_node->node);
6489 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006490
Xiaoguang Wang05589552020-03-31 14:05:18 +08006491 percpu_ref_exit(&ref_node->refs);
6492 kfree(ref_node);
6493 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006494}
6495
Jens Axboe4a38aed22020-05-14 17:21:15 -06006496static void io_file_put_work(struct work_struct *work)
6497{
6498 struct io_ring_ctx *ctx;
6499 struct llist_node *node;
6500
6501 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6502 node = llist_del_all(&ctx->file_put_llist);
6503
6504 while (node) {
6505 struct fixed_file_ref_node *ref_node;
6506 struct llist_node *next = node->next;
6507
6508 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6509 __io_file_put_work(ref_node);
6510 node = next;
6511 }
6512}
6513
Jens Axboe05f3fb32019-12-09 11:22:50 -07006514static void io_file_data_ref_zero(struct percpu_ref *ref)
6515{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006516 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006517 struct io_ring_ctx *ctx;
6518 bool first_add;
6519 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006520
Xiaoguang Wang05589552020-03-31 14:05:18 +08006521 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006522 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006523
Jens Axboe4a38aed22020-05-14 17:21:15 -06006524 if (percpu_ref_is_dying(&ctx->file_data->refs))
6525 delay = 0;
6526
6527 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6528 if (!delay)
6529 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6530 else if (first_add)
6531 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006532}
6533
6534static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6535 struct io_ring_ctx *ctx)
6536{
6537 struct fixed_file_ref_node *ref_node;
6538
6539 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6540 if (!ref_node)
6541 return ERR_PTR(-ENOMEM);
6542
6543 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6544 0, GFP_KERNEL)) {
6545 kfree(ref_node);
6546 return ERR_PTR(-ENOMEM);
6547 }
6548 INIT_LIST_HEAD(&ref_node->node);
6549 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006550 ref_node->file_data = ctx->file_data;
6551 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006552}
6553
6554static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6555{
6556 percpu_ref_exit(&ref_node->refs);
6557 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006558}
6559
6560static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6561 unsigned nr_args)
6562{
6563 __s32 __user *fds = (__s32 __user *) arg;
6564 unsigned nr_tables;
6565 struct file *file;
6566 int fd, ret = 0;
6567 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006568 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006569
6570 if (ctx->file_data)
6571 return -EBUSY;
6572 if (!nr_args)
6573 return -EINVAL;
6574 if (nr_args > IORING_MAX_FIXED_FILES)
6575 return -EMFILE;
6576
6577 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6578 if (!ctx->file_data)
6579 return -ENOMEM;
6580 ctx->file_data->ctx = ctx;
6581 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006582 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006583 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006584
6585 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6586 ctx->file_data->table = kcalloc(nr_tables,
6587 sizeof(struct fixed_file_table),
6588 GFP_KERNEL);
6589 if (!ctx->file_data->table) {
6590 kfree(ctx->file_data);
6591 ctx->file_data = NULL;
6592 return -ENOMEM;
6593 }
6594
Xiaoguang Wang05589552020-03-31 14:05:18 +08006595 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006596 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6597 kfree(ctx->file_data->table);
6598 kfree(ctx->file_data);
6599 ctx->file_data = NULL;
6600 return -ENOMEM;
6601 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006602
6603 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6604 percpu_ref_exit(&ctx->file_data->refs);
6605 kfree(ctx->file_data->table);
6606 kfree(ctx->file_data);
6607 ctx->file_data = NULL;
6608 return -ENOMEM;
6609 }
6610
6611 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6612 struct fixed_file_table *table;
6613 unsigned index;
6614
6615 ret = -EFAULT;
6616 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6617 break;
6618 /* allow sparse sets */
6619 if (fd == -1) {
6620 ret = 0;
6621 continue;
6622 }
6623
6624 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6625 index = i & IORING_FILE_TABLE_MASK;
6626 file = fget(fd);
6627
6628 ret = -EBADF;
6629 if (!file)
6630 break;
6631
6632 /*
6633 * Don't allow io_uring instances to be registered. If UNIX
6634 * isn't enabled, then this causes a reference cycle and this
6635 * instance can never get freed. If UNIX is enabled we'll
6636 * handle it just fine, but there's still no point in allowing
6637 * a ring fd as it doesn't support regular read/write anyway.
6638 */
6639 if (file->f_op == &io_uring_fops) {
6640 fput(file);
6641 break;
6642 }
6643 ret = 0;
6644 table->files[index] = file;
6645 }
6646
6647 if (ret) {
6648 for (i = 0; i < ctx->nr_user_files; i++) {
6649 file = io_file_from_index(ctx, i);
6650 if (file)
6651 fput(file);
6652 }
6653 for (i = 0; i < nr_tables; i++)
6654 kfree(ctx->file_data->table[i].files);
6655
6656 kfree(ctx->file_data->table);
6657 kfree(ctx->file_data);
6658 ctx->file_data = NULL;
6659 ctx->nr_user_files = 0;
6660 return ret;
6661 }
6662
6663 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006664 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006665 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006666 return ret;
6667 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006668
Xiaoguang Wang05589552020-03-31 14:05:18 +08006669 ref_node = alloc_fixed_file_ref_node(ctx);
6670 if (IS_ERR(ref_node)) {
6671 io_sqe_files_unregister(ctx);
6672 return PTR_ERR(ref_node);
6673 }
6674
6675 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006676 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006677 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006678 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006679 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006680 return ret;
6681}
6682
Jens Axboec3a31e62019-10-03 13:59:56 -06006683static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6684 int index)
6685{
6686#if defined(CONFIG_UNIX)
6687 struct sock *sock = ctx->ring_sock->sk;
6688 struct sk_buff_head *head = &sock->sk_receive_queue;
6689 struct sk_buff *skb;
6690
6691 /*
6692 * See if we can merge this file into an existing skb SCM_RIGHTS
6693 * file set. If there's no room, fall back to allocating a new skb
6694 * and filling it in.
6695 */
6696 spin_lock_irq(&head->lock);
6697 skb = skb_peek(head);
6698 if (skb) {
6699 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6700
6701 if (fpl->count < SCM_MAX_FD) {
6702 __skb_unlink(skb, head);
6703 spin_unlock_irq(&head->lock);
6704 fpl->fp[fpl->count] = get_file(file);
6705 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6706 fpl->count++;
6707 spin_lock_irq(&head->lock);
6708 __skb_queue_head(head, skb);
6709 } else {
6710 skb = NULL;
6711 }
6712 }
6713 spin_unlock_irq(&head->lock);
6714
6715 if (skb) {
6716 fput(file);
6717 return 0;
6718 }
6719
6720 return __io_sqe_files_scm(ctx, 1, index);
6721#else
6722 return 0;
6723#endif
6724}
6725
Hillf Dantona5318d32020-03-23 17:47:15 +08006726static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08006727 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006728{
Hillf Dantona5318d32020-03-23 17:47:15 +08006729 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006730 struct percpu_ref *refs = data->cur_refs;
6731 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006732
Jens Axboe05f3fb32019-12-09 11:22:50 -07006733 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006734 if (!pfile)
6735 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006736
Xiaoguang Wang05589552020-03-31 14:05:18 +08006737 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006738 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006739 list_add(&pfile->list, &ref_node->file_list);
6740
Hillf Dantona5318d32020-03-23 17:47:15 +08006741 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006742}
6743
6744static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6745 struct io_uring_files_update *up,
6746 unsigned nr_args)
6747{
6748 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006749 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006750 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006751 __s32 __user *fds;
6752 int fd, i, err;
6753 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006754 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06006755
Jens Axboe05f3fb32019-12-09 11:22:50 -07006756 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006757 return -EOVERFLOW;
6758 if (done > ctx->nr_user_files)
6759 return -EINVAL;
6760
Xiaoguang Wang05589552020-03-31 14:05:18 +08006761 ref_node = alloc_fixed_file_ref_node(ctx);
6762 if (IS_ERR(ref_node))
6763 return PTR_ERR(ref_node);
6764
Jens Axboec3a31e62019-10-03 13:59:56 -06006765 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006766 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006767 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006768 struct fixed_file_table *table;
6769 unsigned index;
6770
Jens Axboec3a31e62019-10-03 13:59:56 -06006771 err = 0;
6772 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6773 err = -EFAULT;
6774 break;
6775 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006776 i = array_index_nospec(up->offset, ctx->nr_user_files);
6777 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006778 index = i & IORING_FILE_TABLE_MASK;
6779 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006780 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006781 err = io_queue_file_removal(data, file);
6782 if (err)
6783 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006784 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006785 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006786 }
6787 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006788 file = fget(fd);
6789 if (!file) {
6790 err = -EBADF;
6791 break;
6792 }
6793 /*
6794 * Don't allow io_uring instances to be registered. If
6795 * UNIX isn't enabled, then this causes a reference
6796 * cycle and this instance can never get freed. If UNIX
6797 * is enabled we'll handle it just fine, but there's
6798 * still no point in allowing a ring fd as it doesn't
6799 * support regular read/write anyway.
6800 */
6801 if (file->f_op == &io_uring_fops) {
6802 fput(file);
6803 err = -EBADF;
6804 break;
6805 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006806 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006807 err = io_sqe_file_register(ctx, file, i);
6808 if (err)
6809 break;
6810 }
6811 nr_args--;
6812 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006813 up->offset++;
6814 }
6815
Xiaoguang Wang05589552020-03-31 14:05:18 +08006816 if (needs_switch) {
6817 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006818 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006819 list_add(&ref_node->node, &data->ref_list);
6820 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006821 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006822 percpu_ref_get(&ctx->file_data->refs);
6823 } else
6824 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06006825
6826 return done ? done : err;
6827}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006828
Jens Axboe05f3fb32019-12-09 11:22:50 -07006829static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6830 unsigned nr_args)
6831{
6832 struct io_uring_files_update up;
6833
6834 if (!ctx->file_data)
6835 return -ENXIO;
6836 if (!nr_args)
6837 return -EINVAL;
6838 if (copy_from_user(&up, arg, sizeof(up)))
6839 return -EFAULT;
6840 if (up.resv)
6841 return -EINVAL;
6842
6843 return __io_sqe_files_update(ctx, &up, nr_args);
6844}
Jens Axboec3a31e62019-10-03 13:59:56 -06006845
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006846static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006847{
6848 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6849
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006850 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006851 io_put_req(req);
6852}
6853
Pavel Begunkov24369c22020-01-28 03:15:48 +03006854static int io_init_wq_offload(struct io_ring_ctx *ctx,
6855 struct io_uring_params *p)
6856{
6857 struct io_wq_data data;
6858 struct fd f;
6859 struct io_ring_ctx *ctx_attach;
6860 unsigned int concurrency;
6861 int ret = 0;
6862
6863 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006864 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03006865 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006866
6867 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6868 /* Do QD, or 4 * CPUS, whatever is smallest */
6869 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6870
6871 ctx->io_wq = io_wq_create(concurrency, &data);
6872 if (IS_ERR(ctx->io_wq)) {
6873 ret = PTR_ERR(ctx->io_wq);
6874 ctx->io_wq = NULL;
6875 }
6876 return ret;
6877 }
6878
6879 f = fdget(p->wq_fd);
6880 if (!f.file)
6881 return -EBADF;
6882
6883 if (f.file->f_op != &io_uring_fops) {
6884 ret = -EINVAL;
6885 goto out_fput;
6886 }
6887
6888 ctx_attach = f.file->private_data;
6889 /* @io_wq is protected by holding the fd */
6890 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6891 ret = -EINVAL;
6892 goto out_fput;
6893 }
6894
6895 ctx->io_wq = ctx_attach->io_wq;
6896out_fput:
6897 fdput(f);
6898 return ret;
6899}
6900
Jens Axboe6c271ce2019-01-10 11:22:30 -07006901static int io_sq_offload_start(struct io_ring_ctx *ctx,
6902 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006903{
6904 int ret;
6905
6906 mmgrab(current->mm);
6907 ctx->sqo_mm = current->mm;
6908
Jens Axboe6c271ce2019-01-10 11:22:30 -07006909 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006910 ret = -EPERM;
6911 if (!capable(CAP_SYS_ADMIN))
6912 goto err;
6913
Jens Axboe917257d2019-04-13 09:28:55 -06006914 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6915 if (!ctx->sq_thread_idle)
6916 ctx->sq_thread_idle = HZ;
6917
Jens Axboe6c271ce2019-01-10 11:22:30 -07006918 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006919 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006920
Jens Axboe917257d2019-04-13 09:28:55 -06006921 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006922 if (cpu >= nr_cpu_ids)
6923 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006924 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006925 goto err;
6926
Jens Axboe6c271ce2019-01-10 11:22:30 -07006927 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6928 ctx, cpu,
6929 "io_uring-sq");
6930 } else {
6931 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6932 "io_uring-sq");
6933 }
6934 if (IS_ERR(ctx->sqo_thread)) {
6935 ret = PTR_ERR(ctx->sqo_thread);
6936 ctx->sqo_thread = NULL;
6937 goto err;
6938 }
6939 wake_up_process(ctx->sqo_thread);
6940 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6941 /* Can't have SQ_AFF without SQPOLL */
6942 ret = -EINVAL;
6943 goto err;
6944 }
6945
Pavel Begunkov24369c22020-01-28 03:15:48 +03006946 ret = io_init_wq_offload(ctx, p);
6947 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006948 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006949
6950 return 0;
6951err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006952 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006953 mmdrop(ctx->sqo_mm);
6954 ctx->sqo_mm = NULL;
6955 return ret;
6956}
6957
6958static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6959{
6960 atomic_long_sub(nr_pages, &user->locked_vm);
6961}
6962
6963static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6964{
6965 unsigned long page_limit, cur_pages, new_pages;
6966
6967 /* Don't allow more pages than we can safely lock */
6968 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6969
6970 do {
6971 cur_pages = atomic_long_read(&user->locked_vm);
6972 new_pages = cur_pages + nr_pages;
6973 if (new_pages > page_limit)
6974 return -ENOMEM;
6975 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6976 new_pages) != cur_pages);
6977
6978 return 0;
6979}
6980
6981static void io_mem_free(void *ptr)
6982{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006983 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006984
Mark Rutland52e04ef2019-04-30 17:30:21 +01006985 if (!ptr)
6986 return;
6987
6988 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006989 if (put_page_testzero(page))
6990 free_compound_page(page);
6991}
6992
6993static void *io_mem_alloc(size_t size)
6994{
6995 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6996 __GFP_NORETRY;
6997
6998 return (void *) __get_free_pages(gfp_flags, get_order(size));
6999}
7000
Hristo Venev75b28af2019-08-26 17:23:46 +00007001static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7002 size_t *sq_offset)
7003{
7004 struct io_rings *rings;
7005 size_t off, sq_array_size;
7006
7007 off = struct_size(rings, cqes, cq_entries);
7008 if (off == SIZE_MAX)
7009 return SIZE_MAX;
7010
7011#ifdef CONFIG_SMP
7012 off = ALIGN(off, SMP_CACHE_BYTES);
7013 if (off == 0)
7014 return SIZE_MAX;
7015#endif
7016
7017 sq_array_size = array_size(sizeof(u32), sq_entries);
7018 if (sq_array_size == SIZE_MAX)
7019 return SIZE_MAX;
7020
7021 if (check_add_overflow(off, sq_array_size, &off))
7022 return SIZE_MAX;
7023
7024 if (sq_offset)
7025 *sq_offset = off;
7026
7027 return off;
7028}
7029
Jens Axboe2b188cc2019-01-07 10:46:33 -07007030static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7031{
Hristo Venev75b28af2019-08-26 17:23:46 +00007032 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007033
Hristo Venev75b28af2019-08-26 17:23:46 +00007034 pages = (size_t)1 << get_order(
7035 rings_size(sq_entries, cq_entries, NULL));
7036 pages += (size_t)1 << get_order(
7037 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007038
Hristo Venev75b28af2019-08-26 17:23:46 +00007039 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007040}
7041
Jens Axboeedafcce2019-01-09 09:16:05 -07007042static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7043{
7044 int i, j;
7045
7046 if (!ctx->user_bufs)
7047 return -ENXIO;
7048
7049 for (i = 0; i < ctx->nr_user_bufs; i++) {
7050 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7051
7052 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007053 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007054
7055 if (ctx->account_mem)
7056 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007057 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007058 imu->nr_bvecs = 0;
7059 }
7060
7061 kfree(ctx->user_bufs);
7062 ctx->user_bufs = NULL;
7063 ctx->nr_user_bufs = 0;
7064 return 0;
7065}
7066
7067static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7068 void __user *arg, unsigned index)
7069{
7070 struct iovec __user *src;
7071
7072#ifdef CONFIG_COMPAT
7073 if (ctx->compat) {
7074 struct compat_iovec __user *ciovs;
7075 struct compat_iovec ciov;
7076
7077 ciovs = (struct compat_iovec __user *) arg;
7078 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7079 return -EFAULT;
7080
Jens Axboed55e5f52019-12-11 16:12:15 -07007081 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007082 dst->iov_len = ciov.iov_len;
7083 return 0;
7084 }
7085#endif
7086 src = (struct iovec __user *) arg;
7087 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7088 return -EFAULT;
7089 return 0;
7090}
7091
7092static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7093 unsigned nr_args)
7094{
7095 struct vm_area_struct **vmas = NULL;
7096 struct page **pages = NULL;
7097 int i, j, got_pages = 0;
7098 int ret = -EINVAL;
7099
7100 if (ctx->user_bufs)
7101 return -EBUSY;
7102 if (!nr_args || nr_args > UIO_MAXIOV)
7103 return -EINVAL;
7104
7105 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7106 GFP_KERNEL);
7107 if (!ctx->user_bufs)
7108 return -ENOMEM;
7109
7110 for (i = 0; i < nr_args; i++) {
7111 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7112 unsigned long off, start, end, ubuf;
7113 int pret, nr_pages;
7114 struct iovec iov;
7115 size_t size;
7116
7117 ret = io_copy_iov(ctx, &iov, arg, i);
7118 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007119 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007120
7121 /*
7122 * Don't impose further limits on the size and buffer
7123 * constraints here, we'll -EINVAL later when IO is
7124 * submitted if they are wrong.
7125 */
7126 ret = -EFAULT;
7127 if (!iov.iov_base || !iov.iov_len)
7128 goto err;
7129
7130 /* arbitrary limit, but we need something */
7131 if (iov.iov_len > SZ_1G)
7132 goto err;
7133
7134 ubuf = (unsigned long) iov.iov_base;
7135 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7136 start = ubuf >> PAGE_SHIFT;
7137 nr_pages = end - start;
7138
7139 if (ctx->account_mem) {
7140 ret = io_account_mem(ctx->user, nr_pages);
7141 if (ret)
7142 goto err;
7143 }
7144
7145 ret = 0;
7146 if (!pages || nr_pages > got_pages) {
Denis Efremova8c73c12020-06-05 12:32:03 +03007147 kvfree(vmas);
7148 kvfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007149 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007150 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007151 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007152 sizeof(struct vm_area_struct *),
7153 GFP_KERNEL);
7154 if (!pages || !vmas) {
7155 ret = -ENOMEM;
7156 if (ctx->account_mem)
7157 io_unaccount_mem(ctx->user, nr_pages);
7158 goto err;
7159 }
7160 got_pages = nr_pages;
7161 }
7162
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007163 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007164 GFP_KERNEL);
7165 ret = -ENOMEM;
7166 if (!imu->bvec) {
7167 if (ctx->account_mem)
7168 io_unaccount_mem(ctx->user, nr_pages);
7169 goto err;
7170 }
7171
7172 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007173 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007174 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007175 FOLL_WRITE | FOLL_LONGTERM,
7176 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007177 if (pret == nr_pages) {
7178 /* don't support file backed memory */
7179 for (j = 0; j < nr_pages; j++) {
7180 struct vm_area_struct *vma = vmas[j];
7181
7182 if (vma->vm_file &&
7183 !is_file_hugepages(vma->vm_file)) {
7184 ret = -EOPNOTSUPP;
7185 break;
7186 }
7187 }
7188 } else {
7189 ret = pret < 0 ? pret : -EFAULT;
7190 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007191 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007192 if (ret) {
7193 /*
7194 * if we did partial map, or found file backed vmas,
7195 * release any pages we did get
7196 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007197 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007198 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007199 if (ctx->account_mem)
7200 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007201 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007202 goto err;
7203 }
7204
7205 off = ubuf & ~PAGE_MASK;
7206 size = iov.iov_len;
7207 for (j = 0; j < nr_pages; j++) {
7208 size_t vec_len;
7209
7210 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7211 imu->bvec[j].bv_page = pages[j];
7212 imu->bvec[j].bv_len = vec_len;
7213 imu->bvec[j].bv_offset = off;
7214 off = 0;
7215 size -= vec_len;
7216 }
7217 /* store original address for later verification */
7218 imu->ubuf = ubuf;
7219 imu->len = iov.iov_len;
7220 imu->nr_bvecs = nr_pages;
7221
7222 ctx->nr_user_bufs++;
7223 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007224 kvfree(pages);
7225 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007226 return 0;
7227err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007228 kvfree(pages);
7229 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007230 io_sqe_buffer_unregister(ctx);
7231 return ret;
7232}
7233
Jens Axboe9b402842019-04-11 11:45:41 -06007234static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7235{
7236 __s32 __user *fds = arg;
7237 int fd;
7238
7239 if (ctx->cq_ev_fd)
7240 return -EBUSY;
7241
7242 if (copy_from_user(&fd, fds, sizeof(*fds)))
7243 return -EFAULT;
7244
7245 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7246 if (IS_ERR(ctx->cq_ev_fd)) {
7247 int ret = PTR_ERR(ctx->cq_ev_fd);
7248 ctx->cq_ev_fd = NULL;
7249 return ret;
7250 }
7251
7252 return 0;
7253}
7254
7255static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7256{
7257 if (ctx->cq_ev_fd) {
7258 eventfd_ctx_put(ctx->cq_ev_fd);
7259 ctx->cq_ev_fd = NULL;
7260 return 0;
7261 }
7262
7263 return -ENXIO;
7264}
7265
Jens Axboe5a2e7452020-02-23 16:23:11 -07007266static int __io_destroy_buffers(int id, void *p, void *data)
7267{
7268 struct io_ring_ctx *ctx = data;
7269 struct io_buffer *buf = p;
7270
Jens Axboe067524e2020-03-02 16:32:28 -07007271 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007272 return 0;
7273}
7274
7275static void io_destroy_buffers(struct io_ring_ctx *ctx)
7276{
7277 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7278 idr_destroy(&ctx->io_buffer_idr);
7279}
7280
Jens Axboe2b188cc2019-01-07 10:46:33 -07007281static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7282{
Jens Axboe6b063142019-01-10 22:13:58 -07007283 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007284 if (ctx->sqo_mm)
7285 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007286
7287 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007288 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007289 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007290 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007291 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007292 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007293
Jens Axboe2b188cc2019-01-07 10:46:33 -07007294#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007295 if (ctx->ring_sock) {
7296 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007297 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007298 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007299#endif
7300
Hristo Venev75b28af2019-08-26 17:23:46 +00007301 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007302 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007303
7304 percpu_ref_exit(&ctx->refs);
7305 if (ctx->account_mem)
7306 io_unaccount_mem(ctx->user,
7307 ring_pages(ctx->sq_entries, ctx->cq_entries));
7308 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007309 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007310 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007311 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007312 kfree(ctx);
7313}
7314
7315static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7316{
7317 struct io_ring_ctx *ctx = file->private_data;
7318 __poll_t mask = 0;
7319
7320 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007321 /*
7322 * synchronizes with barrier from wq_has_sleeper call in
7323 * io_commit_cqring
7324 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007325 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007326 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7327 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007328 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007329 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007330 mask |= EPOLLIN | EPOLLRDNORM;
7331
7332 return mask;
7333}
7334
7335static int io_uring_fasync(int fd, struct file *file, int on)
7336{
7337 struct io_ring_ctx *ctx = file->private_data;
7338
7339 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7340}
7341
Jens Axboe071698e2020-01-28 10:04:42 -07007342static int io_remove_personalities(int id, void *p, void *data)
7343{
7344 struct io_ring_ctx *ctx = data;
7345 const struct cred *cred;
7346
7347 cred = idr_remove(&ctx->personality_idr, id);
7348 if (cred)
7349 put_cred(cred);
7350 return 0;
7351}
7352
Jens Axboe85faa7b2020-04-09 18:14:00 -06007353static void io_ring_exit_work(struct work_struct *work)
7354{
7355 struct io_ring_ctx *ctx;
7356
7357 ctx = container_of(work, struct io_ring_ctx, exit_work);
7358 if (ctx->rings)
7359 io_cqring_overflow_flush(ctx, true);
7360
Jens Axboe56952e92020-06-17 15:00:04 -06007361 /*
7362 * If we're doing polled IO and end up having requests being
7363 * submitted async (out-of-line), then completions can come in while
7364 * we're waiting for refs to drop. We need to reap these manually,
7365 * as nobody else will be looking for them.
7366 */
7367 while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) {
7368 io_iopoll_reap_events(ctx);
7369 if (ctx->rings)
7370 io_cqring_overflow_flush(ctx, true);
7371 }
Jens Axboe85faa7b2020-04-09 18:14:00 -06007372 io_ring_ctx_free(ctx);
7373}
7374
Jens Axboe2b188cc2019-01-07 10:46:33 -07007375static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7376{
7377 mutex_lock(&ctx->uring_lock);
7378 percpu_ref_kill(&ctx->refs);
7379 mutex_unlock(&ctx->uring_lock);
7380
Jens Axboe5262f562019-09-17 12:26:57 -06007381 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007382 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007383
7384 if (ctx->io_wq)
7385 io_wq_cancel_all(ctx->io_wq);
7386
Jens Axboedef596e2019-01-09 08:59:42 -07007387 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007388 /* if we failed setting up the ctx, we might not have any rings */
7389 if (ctx->rings)
7390 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007391 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007392 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7393 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007394}
7395
7396static int io_uring_release(struct inode *inode, struct file *file)
7397{
7398 struct io_ring_ctx *ctx = file->private_data;
7399
7400 file->private_data = NULL;
7401 io_ring_ctx_wait_and_kill(ctx);
7402 return 0;
7403}
7404
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007405static bool io_wq_files_match(struct io_wq_work *work, void *data)
7406{
7407 struct files_struct *files = data;
7408
7409 return work->files == files;
7410}
7411
Jens Axboefcb323c2019-10-24 12:39:47 -06007412static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7413 struct files_struct *files)
7414{
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03007415 if (list_empty_careful(&ctx->inflight_list))
7416 return;
7417
7418 /* cancel all at once, should be faster than doing it one by one*/
7419 io_wq_cancel_cb(ctx->io_wq, io_wq_files_match, files, true);
7420
Jens Axboefcb323c2019-10-24 12:39:47 -06007421 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007422 struct io_kiocb *cancel_req = NULL, *req;
7423 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007424
7425 spin_lock_irq(&ctx->inflight_lock);
7426 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007427 if (req->work.files != files)
7428 continue;
7429 /* req is being completed, ignore */
7430 if (!refcount_inc_not_zero(&req->refs))
7431 continue;
7432 cancel_req = req;
7433 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007434 }
Jens Axboe768134d2019-11-10 20:30:53 -07007435 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007436 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007437 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007438 spin_unlock_irq(&ctx->inflight_lock);
7439
Jens Axboe768134d2019-11-10 20:30:53 -07007440 /* We need to keep going until we don't find a matching req */
7441 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007442 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007443
Jens Axboe2ca10252020-02-13 17:17:35 -07007444 if (cancel_req->flags & REQ_F_OVERFLOW) {
7445 spin_lock_irq(&ctx->completion_lock);
7446 list_del(&cancel_req->list);
7447 cancel_req->flags &= ~REQ_F_OVERFLOW;
7448 if (list_empty(&ctx->cq_overflow_list)) {
7449 clear_bit(0, &ctx->sq_check_overflow);
7450 clear_bit(0, &ctx->cq_check_overflow);
7451 }
7452 spin_unlock_irq(&ctx->completion_lock);
7453
7454 WRITE_ONCE(ctx->rings->cq_overflow,
7455 atomic_inc_return(&ctx->cached_cq_overflow));
7456
7457 /*
7458 * Put inflight ref and overflow ref. If that's
7459 * all we had, then we're done with this request.
7460 */
7461 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007462 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007463 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007464 continue;
7465 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007466 } else {
7467 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7468 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007469 }
7470
Jens Axboefcb323c2019-10-24 12:39:47 -06007471 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007472 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007473 }
7474}
7475
Pavel Begunkov801dd572020-06-15 10:33:14 +03007476static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007477{
Pavel Begunkov801dd572020-06-15 10:33:14 +03007478 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7479 struct task_struct *task = data;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007480
Pavel Begunkov801dd572020-06-15 10:33:14 +03007481 return req->task == task;
Pavel Begunkov44e728b2020-06-15 10:24:04 +03007482}
7483
Jens Axboefcb323c2019-10-24 12:39:47 -06007484static int io_uring_flush(struct file *file, void *data)
7485{
7486 struct io_ring_ctx *ctx = file->private_data;
7487
7488 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007489
7490 /*
7491 * If the task is going away, cancel work it may have pending
7492 */
Pavel Begunkov801dd572020-06-15 10:33:14 +03007493 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7494 io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
Jens Axboe6ab23142020-02-08 20:23:59 -07007495
Jens Axboefcb323c2019-10-24 12:39:47 -06007496 return 0;
7497}
7498
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007499static void *io_uring_validate_mmap_request(struct file *file,
7500 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007501{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007502 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007503 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007504 struct page *page;
7505 void *ptr;
7506
7507 switch (offset) {
7508 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007509 case IORING_OFF_CQ_RING:
7510 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007511 break;
7512 case IORING_OFF_SQES:
7513 ptr = ctx->sq_sqes;
7514 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007515 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007516 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007517 }
7518
7519 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007520 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007521 return ERR_PTR(-EINVAL);
7522
7523 return ptr;
7524}
7525
7526#ifdef CONFIG_MMU
7527
7528static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7529{
7530 size_t sz = vma->vm_end - vma->vm_start;
7531 unsigned long pfn;
7532 void *ptr;
7533
7534 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7535 if (IS_ERR(ptr))
7536 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007537
7538 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7539 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7540}
7541
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007542#else /* !CONFIG_MMU */
7543
7544static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7545{
7546 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7547}
7548
7549static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7550{
7551 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7552}
7553
7554static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7555 unsigned long addr, unsigned long len,
7556 unsigned long pgoff, unsigned long flags)
7557{
7558 void *ptr;
7559
7560 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7561 if (IS_ERR(ptr))
7562 return PTR_ERR(ptr);
7563
7564 return (unsigned long) ptr;
7565}
7566
7567#endif /* !CONFIG_MMU */
7568
Jens Axboe2b188cc2019-01-07 10:46:33 -07007569SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7570 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7571 size_t, sigsz)
7572{
7573 struct io_ring_ctx *ctx;
7574 long ret = -EBADF;
7575 int submitted = 0;
7576 struct fd f;
7577
Jens Axboeb41e9852020-02-17 09:52:41 -07007578 if (current->task_works)
7579 task_work_run();
7580
Jens Axboe6c271ce2019-01-10 11:22:30 -07007581 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007582 return -EINVAL;
7583
7584 f = fdget(fd);
7585 if (!f.file)
7586 return -EBADF;
7587
7588 ret = -EOPNOTSUPP;
7589 if (f.file->f_op != &io_uring_fops)
7590 goto out_fput;
7591
7592 ret = -ENXIO;
7593 ctx = f.file->private_data;
7594 if (!percpu_ref_tryget(&ctx->refs))
7595 goto out_fput;
7596
Jens Axboe6c271ce2019-01-10 11:22:30 -07007597 /*
7598 * For SQ polling, the thread will do all submissions and completions.
7599 * Just return the requested submit count, and wake the thread if
7600 * we were asked to.
7601 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007602 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007603 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007604 if (!list_empty_careful(&ctx->cq_overflow_list))
7605 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007606 if (flags & IORING_ENTER_SQ_WAKEUP)
7607 wake_up(&ctx->sqo_wait);
7608 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007609 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007610 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03007611 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007612 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007613
7614 if (submitted != to_submit)
7615 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007616 }
7617 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007618 unsigned nr_events = 0;
7619
Jens Axboe2b188cc2019-01-07 10:46:33 -07007620 min_complete = min(min_complete, ctx->cq_entries);
7621
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007622 /*
7623 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7624 * space applications don't need to do io completion events
7625 * polling again, they can rely on io_sq_thread to do polling
7626 * work, which can reduce cpu usage and uring_lock contention.
7627 */
7628 if (ctx->flags & IORING_SETUP_IOPOLL &&
7629 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007630 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007631 } else {
7632 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7633 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007634 }
7635
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007636out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007637 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007638out_fput:
7639 fdput(f);
7640 return submitted ? submitted : ret;
7641}
7642
Tobias Klauserbebdb652020-02-26 18:38:32 +01007643#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007644static int io_uring_show_cred(int id, void *p, void *data)
7645{
7646 const struct cred *cred = p;
7647 struct seq_file *m = data;
7648 struct user_namespace *uns = seq_user_ns(m);
7649 struct group_info *gi;
7650 kernel_cap_t cap;
7651 unsigned __capi;
7652 int g;
7653
7654 seq_printf(m, "%5d\n", id);
7655 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7656 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7657 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7658 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7659 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7660 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7661 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7662 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7663 seq_puts(m, "\n\tGroups:\t");
7664 gi = cred->group_info;
7665 for (g = 0; g < gi->ngroups; g++) {
7666 seq_put_decimal_ull(m, g ? " " : "",
7667 from_kgid_munged(uns, gi->gid[g]));
7668 }
7669 seq_puts(m, "\n\tCapEff:\t");
7670 cap = cred->cap_effective;
7671 CAP_FOR_EACH_U32(__capi)
7672 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7673 seq_putc(m, '\n');
7674 return 0;
7675}
7676
7677static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7678{
7679 int i;
7680
7681 mutex_lock(&ctx->uring_lock);
7682 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7683 for (i = 0; i < ctx->nr_user_files; i++) {
7684 struct fixed_file_table *table;
7685 struct file *f;
7686
7687 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7688 f = table->files[i & IORING_FILE_TABLE_MASK];
7689 if (f)
7690 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7691 else
7692 seq_printf(m, "%5u: <none>\n", i);
7693 }
7694 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7695 for (i = 0; i < ctx->nr_user_bufs; i++) {
7696 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7697
7698 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7699 (unsigned int) buf->len);
7700 }
7701 if (!idr_is_empty(&ctx->personality_idr)) {
7702 seq_printf(m, "Personalities:\n");
7703 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7704 }
Jens Axboed7718a92020-02-14 22:23:12 -07007705 seq_printf(m, "PollList:\n");
7706 spin_lock_irq(&ctx->completion_lock);
7707 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7708 struct hlist_head *list = &ctx->cancel_hash[i];
7709 struct io_kiocb *req;
7710
7711 hlist_for_each_entry(req, list, hash_node)
7712 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7713 req->task->task_works != NULL);
7714 }
7715 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007716 mutex_unlock(&ctx->uring_lock);
7717}
7718
7719static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7720{
7721 struct io_ring_ctx *ctx = f->private_data;
7722
7723 if (percpu_ref_tryget(&ctx->refs)) {
7724 __io_uring_show_fdinfo(ctx, m);
7725 percpu_ref_put(&ctx->refs);
7726 }
7727}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007728#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007729
Jens Axboe2b188cc2019-01-07 10:46:33 -07007730static const struct file_operations io_uring_fops = {
7731 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007732 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007733 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007734#ifndef CONFIG_MMU
7735 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7736 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7737#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007738 .poll = io_uring_poll,
7739 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007740#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007741 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007742#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007743};
7744
7745static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7746 struct io_uring_params *p)
7747{
Hristo Venev75b28af2019-08-26 17:23:46 +00007748 struct io_rings *rings;
7749 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007750
Hristo Venev75b28af2019-08-26 17:23:46 +00007751 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7752 if (size == SIZE_MAX)
7753 return -EOVERFLOW;
7754
7755 rings = io_mem_alloc(size);
7756 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007757 return -ENOMEM;
7758
Hristo Venev75b28af2019-08-26 17:23:46 +00007759 ctx->rings = rings;
7760 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7761 rings->sq_ring_mask = p->sq_entries - 1;
7762 rings->cq_ring_mask = p->cq_entries - 1;
7763 rings->sq_ring_entries = p->sq_entries;
7764 rings->cq_ring_entries = p->cq_entries;
7765 ctx->sq_mask = rings->sq_ring_mask;
7766 ctx->cq_mask = rings->cq_ring_mask;
7767 ctx->sq_entries = rings->sq_ring_entries;
7768 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007769
7770 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007771 if (size == SIZE_MAX) {
7772 io_mem_free(ctx->rings);
7773 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007774 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007775 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007776
7777 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007778 if (!ctx->sq_sqes) {
7779 io_mem_free(ctx->rings);
7780 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007781 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007782 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007783
Jens Axboe2b188cc2019-01-07 10:46:33 -07007784 return 0;
7785}
7786
7787/*
7788 * Allocate an anonymous fd, this is what constitutes the application
7789 * visible backing of an io_uring instance. The application mmaps this
7790 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7791 * we have to tie this fd to a socket for file garbage collection purposes.
7792 */
7793static int io_uring_get_fd(struct io_ring_ctx *ctx)
7794{
7795 struct file *file;
7796 int ret;
7797
7798#if defined(CONFIG_UNIX)
7799 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7800 &ctx->ring_sock);
7801 if (ret)
7802 return ret;
7803#endif
7804
7805 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7806 if (ret < 0)
7807 goto err;
7808
7809 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7810 O_RDWR | O_CLOEXEC);
7811 if (IS_ERR(file)) {
7812 put_unused_fd(ret);
7813 ret = PTR_ERR(file);
7814 goto err;
7815 }
7816
7817#if defined(CONFIG_UNIX)
7818 ctx->ring_sock->file = file;
7819#endif
7820 fd_install(ret, file);
7821 return ret;
7822err:
7823#if defined(CONFIG_UNIX)
7824 sock_release(ctx->ring_sock);
7825 ctx->ring_sock = NULL;
7826#endif
7827 return ret;
7828}
7829
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007830static int io_uring_create(unsigned entries, struct io_uring_params *p,
7831 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007832{
7833 struct user_struct *user = NULL;
7834 struct io_ring_ctx *ctx;
7835 bool account_mem;
7836 int ret;
7837
Jens Axboe8110c1a2019-12-28 15:39:54 -07007838 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007839 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007840 if (entries > IORING_MAX_ENTRIES) {
7841 if (!(p->flags & IORING_SETUP_CLAMP))
7842 return -EINVAL;
7843 entries = IORING_MAX_ENTRIES;
7844 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007845
7846 /*
7847 * Use twice as many entries for the CQ ring. It's possible for the
7848 * application to drive a higher depth than the size of the SQ ring,
7849 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007850 * some flexibility in overcommitting a bit. If the application has
7851 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7852 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007853 */
7854 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007855 if (p->flags & IORING_SETUP_CQSIZE) {
7856 /*
7857 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7858 * to a power-of-two, if it isn't already. We do NOT impose
7859 * any cq vs sq ring sizing.
7860 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007861 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007862 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007863 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7864 if (!(p->flags & IORING_SETUP_CLAMP))
7865 return -EINVAL;
7866 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7867 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007868 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7869 } else {
7870 p->cq_entries = 2 * p->sq_entries;
7871 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007872
7873 user = get_uid(current_user());
7874 account_mem = !capable(CAP_IPC_LOCK);
7875
7876 if (account_mem) {
7877 ret = io_account_mem(user,
7878 ring_pages(p->sq_entries, p->cq_entries));
7879 if (ret) {
7880 free_uid(user);
7881 return ret;
7882 }
7883 }
7884
7885 ctx = io_ring_ctx_alloc(p);
7886 if (!ctx) {
7887 if (account_mem)
7888 io_unaccount_mem(user, ring_pages(p->sq_entries,
7889 p->cq_entries));
7890 free_uid(user);
7891 return -ENOMEM;
7892 }
7893 ctx->compat = in_compat_syscall();
7894 ctx->account_mem = account_mem;
7895 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007896 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007897
7898 ret = io_allocate_scq_urings(ctx, p);
7899 if (ret)
7900 goto err;
7901
Jens Axboe6c271ce2019-01-10 11:22:30 -07007902 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007903 if (ret)
7904 goto err;
7905
Jens Axboe2b188cc2019-01-07 10:46:33 -07007906 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007907 p->sq_off.head = offsetof(struct io_rings, sq.head);
7908 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7909 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7910 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7911 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7912 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7913 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007914
7915 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007916 p->cq_off.head = offsetof(struct io_rings, cq.head);
7917 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7918 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7919 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7920 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7921 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02007922 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06007923
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007924 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
7925 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7926 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
7927
7928 if (copy_to_user(params, p, sizeof(*p))) {
7929 ret = -EFAULT;
7930 goto err;
7931 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06007932 /*
7933 * Install ring fd as the very last thing, so we don't risk someone
7934 * having closed it before we finish setup
7935 */
7936 ret = io_uring_get_fd(ctx);
7937 if (ret < 0)
7938 goto err;
7939
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007940 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007941 return ret;
7942err:
7943 io_ring_ctx_wait_and_kill(ctx);
7944 return ret;
7945}
7946
7947/*
7948 * Sets up an aio uring context, and returns the fd. Applications asks for a
7949 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7950 * params structure passed in.
7951 */
7952static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7953{
7954 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007955 int i;
7956
7957 if (copy_from_user(&p, params, sizeof(p)))
7958 return -EFAULT;
7959 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7960 if (p.resv[i])
7961 return -EINVAL;
7962 }
7963
Jens Axboe6c271ce2019-01-10 11:22:30 -07007964 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007965 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007966 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007967 return -EINVAL;
7968
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007969 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007970}
7971
7972SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7973 struct io_uring_params __user *, params)
7974{
7975 return io_uring_setup(entries, params);
7976}
7977
Jens Axboe66f4af92020-01-16 15:36:52 -07007978static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7979{
7980 struct io_uring_probe *p;
7981 size_t size;
7982 int i, ret;
7983
7984 size = struct_size(p, ops, nr_args);
7985 if (size == SIZE_MAX)
7986 return -EOVERFLOW;
7987 p = kzalloc(size, GFP_KERNEL);
7988 if (!p)
7989 return -ENOMEM;
7990
7991 ret = -EFAULT;
7992 if (copy_from_user(p, arg, size))
7993 goto out;
7994 ret = -EINVAL;
7995 if (memchr_inv(p, 0, size))
7996 goto out;
7997
7998 p->last_op = IORING_OP_LAST - 1;
7999 if (nr_args > IORING_OP_LAST)
8000 nr_args = IORING_OP_LAST;
8001
8002 for (i = 0; i < nr_args; i++) {
8003 p->ops[i].op = i;
8004 if (!io_op_defs[i].not_supported)
8005 p->ops[i].flags = IO_URING_OP_SUPPORTED;
8006 }
8007 p->ops_len = i;
8008
8009 ret = 0;
8010 if (copy_to_user(arg, p, size))
8011 ret = -EFAULT;
8012out:
8013 kfree(p);
8014 return ret;
8015}
8016
Jens Axboe071698e2020-01-28 10:04:42 -07008017static int io_register_personality(struct io_ring_ctx *ctx)
8018{
8019 const struct cred *creds = get_current_cred();
8020 int id;
8021
8022 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8023 USHRT_MAX, GFP_KERNEL);
8024 if (id < 0)
8025 put_cred(creds);
8026 return id;
8027}
8028
8029static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8030{
8031 const struct cred *old_creds;
8032
8033 old_creds = idr_remove(&ctx->personality_idr, id);
8034 if (old_creds) {
8035 put_cred(old_creds);
8036 return 0;
8037 }
8038
8039 return -EINVAL;
8040}
8041
8042static bool io_register_op_must_quiesce(int op)
8043{
8044 switch (op) {
8045 case IORING_UNREGISTER_FILES:
8046 case IORING_REGISTER_FILES_UPDATE:
8047 case IORING_REGISTER_PROBE:
8048 case IORING_REGISTER_PERSONALITY:
8049 case IORING_UNREGISTER_PERSONALITY:
8050 return false;
8051 default:
8052 return true;
8053 }
8054}
8055
Jens Axboeedafcce2019-01-09 09:16:05 -07008056static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8057 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008058 __releases(ctx->uring_lock)
8059 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008060{
8061 int ret;
8062
Jens Axboe35fa71a2019-04-22 10:23:23 -06008063 /*
8064 * We're inside the ring mutex, if the ref is already dying, then
8065 * someone else killed the ctx or is already going through
8066 * io_uring_register().
8067 */
8068 if (percpu_ref_is_dying(&ctx->refs))
8069 return -ENXIO;
8070
Jens Axboe071698e2020-01-28 10:04:42 -07008071 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008072 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008073
Jens Axboe05f3fb32019-12-09 11:22:50 -07008074 /*
8075 * Drop uring mutex before waiting for references to exit. If
8076 * another thread is currently inside io_uring_enter() it might
8077 * need to grab the uring_lock to make progress. If we hold it
8078 * here across the drain wait, then we can deadlock. It's safe
8079 * to drop the mutex here, since no new references will come in
8080 * after we've killed the percpu ref.
8081 */
8082 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008083 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008084 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008085 if (ret) {
8086 percpu_ref_resurrect(&ctx->refs);
8087 ret = -EINTR;
8088 goto out;
8089 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008090 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008091
8092 switch (opcode) {
8093 case IORING_REGISTER_BUFFERS:
8094 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8095 break;
8096 case IORING_UNREGISTER_BUFFERS:
8097 ret = -EINVAL;
8098 if (arg || nr_args)
8099 break;
8100 ret = io_sqe_buffer_unregister(ctx);
8101 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008102 case IORING_REGISTER_FILES:
8103 ret = io_sqe_files_register(ctx, arg, nr_args);
8104 break;
8105 case IORING_UNREGISTER_FILES:
8106 ret = -EINVAL;
8107 if (arg || nr_args)
8108 break;
8109 ret = io_sqe_files_unregister(ctx);
8110 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008111 case IORING_REGISTER_FILES_UPDATE:
8112 ret = io_sqe_files_update(ctx, arg, nr_args);
8113 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008114 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008115 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008116 ret = -EINVAL;
8117 if (nr_args != 1)
8118 break;
8119 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008120 if (ret)
8121 break;
8122 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8123 ctx->eventfd_async = 1;
8124 else
8125 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008126 break;
8127 case IORING_UNREGISTER_EVENTFD:
8128 ret = -EINVAL;
8129 if (arg || nr_args)
8130 break;
8131 ret = io_eventfd_unregister(ctx);
8132 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008133 case IORING_REGISTER_PROBE:
8134 ret = -EINVAL;
8135 if (!arg || nr_args > 256)
8136 break;
8137 ret = io_probe(ctx, arg, nr_args);
8138 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008139 case IORING_REGISTER_PERSONALITY:
8140 ret = -EINVAL;
8141 if (arg || nr_args)
8142 break;
8143 ret = io_register_personality(ctx);
8144 break;
8145 case IORING_UNREGISTER_PERSONALITY:
8146 ret = -EINVAL;
8147 if (arg)
8148 break;
8149 ret = io_unregister_personality(ctx, nr_args);
8150 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008151 default:
8152 ret = -EINVAL;
8153 break;
8154 }
8155
Jens Axboe071698e2020-01-28 10:04:42 -07008156 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008157 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008158 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008159out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008160 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008161 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008162 return ret;
8163}
8164
8165SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8166 void __user *, arg, unsigned int, nr_args)
8167{
8168 struct io_ring_ctx *ctx;
8169 long ret = -EBADF;
8170 struct fd f;
8171
8172 f = fdget(fd);
8173 if (!f.file)
8174 return -EBADF;
8175
8176 ret = -EOPNOTSUPP;
8177 if (f.file->f_op != &io_uring_fops)
8178 goto out_fput;
8179
8180 ctx = f.file->private_data;
8181
8182 mutex_lock(&ctx->uring_lock);
8183 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8184 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008185 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8186 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008187out_fput:
8188 fdput(f);
8189 return ret;
8190}
8191
Jens Axboe2b188cc2019-01-07 10:46:33 -07008192static int __init io_uring_init(void)
8193{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008194#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8195 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8196 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8197} while (0)
8198
8199#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8200 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8201 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8202 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8203 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8204 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8205 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8206 BUILD_BUG_SQE_ELEM(8, __u64, off);
8207 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8208 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008209 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008210 BUILD_BUG_SQE_ELEM(24, __u32, len);
8211 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8212 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8213 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8214 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8215 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8216 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8217 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8218 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8219 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8220 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8221 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8222 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8223 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008224 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008225 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8226 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8227 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008228 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008229
Jens Axboed3656342019-12-18 09:50:26 -07008230 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008231 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008232 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8233 return 0;
8234};
8235__initcall(io_uring_init);