blob: 9842443dde203acaee8bec528dbfce78313e0e4a [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,
531 REQ_F_IOPOLL_COMPLETED_BIT,
532 REQ_F_LINK_TIMEOUT_BIT,
533 REQ_F_TIMEOUT_BIT,
534 REQ_F_ISREG_BIT,
535 REQ_F_MUST_PUNT_BIT,
536 REQ_F_TIMEOUT_NOSEQ_BIT,
537 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300538 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700539 REQ_F_OVERFLOW_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700540 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700541 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600542 REQ_F_NO_FILE_TABLE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700543
544 /* not a real bit, just to check we're not overflowing the space */
545 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300546};
547
548enum {
549 /* ctx owns file */
550 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
551 /* drain existing IO first */
552 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
553 /* linked sqes */
554 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
555 /* doesn't sever on completion < 0 */
556 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
557 /* IOSQE_ASYNC */
558 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700559 /* IOSQE_BUFFER_SELECT */
560 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300561
Pavel Begunkovdea3b492020-04-12 02:05:04 +0300562 /* head of a link */
563 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300564 /* already grabbed next link */
565 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
566 /* fail rest of links */
567 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
568 /* on inflight list */
569 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
570 /* read/write uses file position */
571 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
572 /* must not punt to workers */
573 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
574 /* polled IO has completed */
575 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
576 /* 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),
Jens Axboed7718a92020-02-14 22:23:12 -0700598};
599
600struct async_poll {
601 struct io_poll_iocb poll;
602 struct io_wq_work work;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300603};
604
Jens Axboe09bb8392019-03-13 12:39:28 -0600605/*
606 * NOTE! Each of the iocb union members has the file pointer
607 * as the first entry in their struct definition. So you can
608 * access the file pointer through any of the sub-structs,
609 * or directly as just 'ki_filp' in this struct.
610 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700611struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700612 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600613 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700614 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700615 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700616 struct io_accept accept;
617 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700618 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700619 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700620 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700621 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700622 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700623 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700624 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700625 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700626 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700627 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300628 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700629 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700630 struct io_statx statx;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700631 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700633 struct io_async_ctx *io;
Pavel Begunkovc398ecb2020-04-09 08:17:59 +0300634 int cflags;
Jens Axboed625c6e2019-12-17 19:53:05 -0700635 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700636
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700637 u16 buf_index;
638
Jens Axboe2b188cc2019-01-07 10:46:33 -0700639 struct io_ring_ctx *ctx;
Jens Axboed7718a92020-02-14 22:23:12 -0700640 struct list_head list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700642 refcount_t refs;
Jens Axboe3537b6a2020-04-03 11:19:06 -0600643 struct task_struct *task;
644 unsigned long fsize;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600646 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600647 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700648
Jens Axboed7718a92020-02-14 22:23:12 -0700649 struct list_head link_list;
650
Jens Axboefcb323c2019-10-24 12:39:47 -0600651 struct list_head inflight_entry;
652
Xiaoguang Wang05589552020-03-31 14:05:18 +0800653 struct percpu_ref *fixed_file_refs;
654
Jens Axboeb41e9852020-02-17 09:52:41 -0700655 union {
656 /*
657 * Only commands that never go async can use the below fields,
Jens Axboed7718a92020-02-14 22:23:12 -0700658 * obviously. Right now only IORING_OP_POLL_ADD uses them, and
659 * async armed poll handlers for regular commands. The latter
660 * restore the work, if needed.
Jens Axboeb41e9852020-02-17 09:52:41 -0700661 */
662 struct {
Jens Axboeb41e9852020-02-17 09:52:41 -0700663 struct callback_head task_work;
Jens Axboed7718a92020-02-14 22:23:12 -0700664 struct hlist_node hash_node;
665 struct async_poll *apoll;
Jens Axboeb41e9852020-02-17 09:52:41 -0700666 };
667 struct io_wq_work work;
668 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700669};
670
671#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700672#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700673
Jens Axboe9a56a232019-01-09 09:06:50 -0700674struct io_submit_state {
675 struct blk_plug plug;
676
677 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700678 * io_kiocb alloc cache
679 */
680 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300681 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700682
683 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700684 * File reference cache
685 */
686 struct file *file;
687 unsigned int fd;
688 unsigned int has_refs;
689 unsigned int used_refs;
690 unsigned int ios_left;
691};
692
Jens Axboed3656342019-12-18 09:50:26 -0700693struct io_op_def {
694 /* needs req->io allocated for deferral/async */
695 unsigned async_ctx : 1;
696 /* needs current->mm setup, does mm access */
697 unsigned needs_mm : 1;
698 /* needs req->file assigned */
699 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700700 /* hash wq insertion if file is a regular file */
701 unsigned hash_reg_file : 1;
702 /* unbound wq insertion if file is a non-regular file */
703 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700704 /* opcode is not supported by this kernel */
705 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700706 /* needs file table */
707 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700708 /* needs ->fs */
709 unsigned needs_fs : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700710 /* set if opcode supports polled "wait" */
711 unsigned pollin : 1;
712 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700713 /* op supports buffer selection */
714 unsigned buffer_select : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700715};
716
717static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300718 [IORING_OP_NOP] = {},
719 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700720 .async_ctx = 1,
721 .needs_mm = 1,
722 .needs_file = 1,
723 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700724 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700725 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700726 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300727 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700728 .async_ctx = 1,
729 .needs_mm = 1,
730 .needs_file = 1,
731 .hash_reg_file = 1,
732 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700733 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700736 .needs_file = 1,
737 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700739 .needs_file = 1,
740 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700741 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700742 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300743 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700744 .needs_file = 1,
745 .hash_reg_file = 1,
746 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700747 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700748 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300749 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700750 .needs_file = 1,
751 .unbound_nonreg_file = 1,
752 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300753 [IORING_OP_POLL_REMOVE] = {},
754 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700755 .needs_file = 1,
756 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300757 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700758 .async_ctx = 1,
759 .needs_mm = 1,
760 .needs_file = 1,
761 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700762 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700763 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700764 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300765 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700766 .async_ctx = 1,
767 .needs_mm = 1,
768 .needs_file = 1,
769 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700770 .needs_fs = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700771 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700772 .buffer_select = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700773 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300774 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700775 .async_ctx = 1,
776 .needs_mm = 1,
777 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300778 [IORING_OP_TIMEOUT_REMOVE] = {},
779 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700780 .needs_mm = 1,
781 .needs_file = 1,
782 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700783 .file_table = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700784 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700785 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300786 [IORING_OP_ASYNC_CANCEL] = {},
787 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700788 .async_ctx = 1,
789 .needs_mm = 1,
790 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300791 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700792 .async_ctx = 1,
793 .needs_mm = 1,
794 .needs_file = 1,
795 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700796 .pollout = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700797 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300798 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700799 .needs_file = 1,
800 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300801 [IORING_OP_OPENAT] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700802 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700803 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700804 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300805 [IORING_OP_CLOSE] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700806 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700807 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300808 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700809 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700810 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700811 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300812 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700813 .needs_mm = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700814 .needs_fs = 1,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600815 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700816 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300817 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700818 .needs_mm = 1,
819 .needs_file = 1,
820 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700821 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700822 .buffer_select = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700823 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300824 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700825 .needs_mm = 1,
826 .needs_file = 1,
827 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700828 .pollout = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700829 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300830 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700831 .needs_file = 1,
832 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300833 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700834 .needs_mm = 1,
835 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300836 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700837 .needs_mm = 1,
838 .needs_file = 1,
839 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700840 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700841 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300842 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700843 .needs_mm = 1,
844 .needs_file = 1,
845 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700846 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700847 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700848 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300849 [IORING_OP_OPENAT2] = {
Jens Axboef86cd202020-01-29 13:46:44 -0700850 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700851 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700852 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700853 [IORING_OP_EPOLL_CTL] = {
854 .unbound_nonreg_file = 1,
855 .file_table = 1,
856 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300857 [IORING_OP_SPLICE] = {
858 .needs_file = 1,
859 .hash_reg_file = 1,
860 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700861 },
862 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700863 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300864 [IORING_OP_TEE] = {
865 .needs_file = 1,
866 .hash_reg_file = 1,
867 .unbound_nonreg_file = 1,
868 },
Jens Axboed3656342019-12-18 09:50:26 -0700869};
870
Jens Axboe561fb042019-10-24 07:25:42 -0600871static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700872static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800873static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700874static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700875static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
876static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700877static int __io_sqe_files_update(struct io_ring_ctx *ctx,
878 struct io_uring_files_update *ip,
879 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700880static int io_grab_files(struct io_kiocb *req);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300881static void io_cleanup_req(struct io_kiocb *req);
Jens Axboeb41e9852020-02-17 09:52:41 -0700882static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
883 int fd, struct file **out_file, bool fixed);
884static void __io_queue_sqe(struct io_kiocb *req,
885 const struct io_uring_sqe *sqe);
Jens Axboede0617e2019-04-06 21:51:27 -0600886
Jens Axboe2b188cc2019-01-07 10:46:33 -0700887static struct kmem_cache *req_cachep;
888
889static const struct file_operations io_uring_fops;
890
891struct sock *io_uring_get_socket(struct file *file)
892{
893#if defined(CONFIG_UNIX)
894 if (file->f_op == &io_uring_fops) {
895 struct io_ring_ctx *ctx = file->private_data;
896
897 return ctx->ring_sock->sk;
898 }
899#endif
900 return NULL;
901}
902EXPORT_SYMBOL(io_uring_get_socket);
903
Jens Axboe4a38aed22020-05-14 17:21:15 -0600904static void io_file_put_work(struct work_struct *work);
905
Pavel Begunkov0cdaf762020-05-17 14:13:40 +0300906static inline bool io_async_submit(struct io_ring_ctx *ctx)
907{
908 return ctx->flags & IORING_SETUP_SQPOLL;
909}
910
Jens Axboe2b188cc2019-01-07 10:46:33 -0700911static void io_ring_ctx_ref_free(struct percpu_ref *ref)
912{
913 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
914
Jens Axboe0f158b42020-05-14 17:18:39 -0600915 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700916}
917
918static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
919{
920 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700921 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700922
923 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
924 if (!ctx)
925 return NULL;
926
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700927 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
928 if (!ctx->fallback_req)
929 goto err;
930
Jens Axboe78076bb2019-12-04 19:56:40 -0700931 /*
932 * Use 5 bits less than the max cq entries, that should give us around
933 * 32 entries per hash list if totally full and uniformly spread.
934 */
935 hash_bits = ilog2(p->cq_entries);
936 hash_bits -= 5;
937 if (hash_bits <= 0)
938 hash_bits = 1;
939 ctx->cancel_hash_bits = hash_bits;
940 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
941 GFP_KERNEL);
942 if (!ctx->cancel_hash)
943 goto err;
944 __hash_init(ctx->cancel_hash, 1U << hash_bits);
945
Roman Gushchin21482892019-05-07 10:01:48 -0700946 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700947 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
948 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700949
950 ctx->flags = p->flags;
Jens Axboe583863e2020-05-17 09:20:00 -0600951 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700952 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700953 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -0600954 init_completion(&ctx->ref_comp);
955 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -0700956 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -0700957 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700958 mutex_init(&ctx->uring_lock);
959 init_waitqueue_head(&ctx->wait);
960 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700961 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600962 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600963 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600964 init_waitqueue_head(&ctx->inflight_wait);
965 spin_lock_init(&ctx->inflight_lock);
966 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600967 INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work);
968 init_llist_head(&ctx->file_put_llist);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700969 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700970err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700971 if (ctx->fallback_req)
972 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe78076bb2019-12-04 19:56:40 -0700973 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700974 kfree(ctx);
975 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700976}
977
Bob Liu9d858b22019-11-13 18:06:25 +0800978static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600979{
Jackie Liua197f662019-11-08 08:09:12 -0700980 struct io_ring_ctx *ctx = req->ctx;
981
Pavel Begunkov31af27c2020-04-15 00:39:50 +0300982 return req->sequence != ctx->cached_cq_tail
983 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600984}
985
Bob Liu9d858b22019-11-13 18:06:25 +0800986static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600987{
Pavel Begunkov87987892020-01-18 01:22:30 +0300988 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800989 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600990
Bob Liu9d858b22019-11-13 18:06:25 +0800991 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600992}
993
Jens Axboede0617e2019-04-06 21:51:27 -0600994static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700995{
Hristo Venev75b28af2019-08-26 17:23:46 +0000996 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700997
Pavel Begunkov07910152020-01-17 03:52:46 +0300998 /* order cqe stores with ring update */
999 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001000
Pavel Begunkov07910152020-01-17 03:52:46 +03001001 if (wq_has_sleeper(&ctx->cq_wait)) {
1002 wake_up_interruptible(&ctx->cq_wait);
1003 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001004 }
1005}
1006
Jens Axboecccf0ee2020-01-27 16:34:48 -07001007static inline void io_req_work_grab_env(struct io_kiocb *req,
1008 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -06001009{
Jens Axboecccf0ee2020-01-27 16:34:48 -07001010 if (!req->work.mm && def->needs_mm) {
1011 mmgrab(current->mm);
1012 req->work.mm = current->mm;
1013 }
1014 if (!req->work.creds)
1015 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -07001016 if (!req->work.fs && def->needs_fs) {
1017 spin_lock(&current->fs->lock);
1018 if (!current->fs->in_exec) {
1019 req->work.fs = current->fs;
1020 req->work.fs->users++;
1021 } else {
1022 req->work.flags |= IO_WQ_WORK_CANCEL;
1023 }
1024 spin_unlock(&current->fs->lock);
1025 }
Jens Axboe6ab23142020-02-08 20:23:59 -07001026 if (!req->work.task_pid)
1027 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001028}
1029
1030static inline void io_req_work_drop_env(struct io_kiocb *req)
1031{
1032 if (req->work.mm) {
1033 mmdrop(req->work.mm);
1034 req->work.mm = NULL;
1035 }
1036 if (req->work.creds) {
1037 put_cred(req->work.creds);
1038 req->work.creds = NULL;
1039 }
Jens Axboeff002b32020-02-07 16:05:21 -07001040 if (req->work.fs) {
1041 struct fs_struct *fs = req->work.fs;
1042
1043 spin_lock(&req->work.fs->lock);
1044 if (--fs->users)
1045 fs = NULL;
1046 spin_unlock(&req->work.fs->lock);
1047 if (fs)
1048 free_fs_struct(fs);
1049 }
Jens Axboe561fb042019-10-24 07:25:42 -06001050}
1051
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001052static inline void io_prep_async_work(struct io_kiocb *req,
Jens Axboe94ae5e72019-11-14 19:39:52 -07001053 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -06001054{
Jens Axboed3656342019-12-18 09:50:26 -07001055 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe54a91f32019-09-10 09:15:04 -06001056
Jens Axboed3656342019-12-18 09:50:26 -07001057 if (req->flags & REQ_F_ISREG) {
1058 if (def->hash_reg_file)
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001059 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001060 } else {
1061 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001062 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001063 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001064
1065 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -06001066
Jens Axboe94ae5e72019-11-14 19:39:52 -07001067 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001068}
1069
Jackie Liua197f662019-11-08 08:09:12 -07001070static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001071{
Jackie Liua197f662019-11-08 08:09:12 -07001072 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001073 struct io_kiocb *link;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001074
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001075 io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001076
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001077 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1078 &req->work, req->flags);
1079 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001080
1081 if (link)
1082 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001083}
1084
Jens Axboe5262f562019-09-17 12:26:57 -06001085static void io_kill_timeout(struct io_kiocb *req)
1086{
1087 int ret;
1088
Jens Axboe2d283902019-12-04 11:08:05 -07001089 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001090 if (ret != -1) {
1091 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001092 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001093 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001094 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001095 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001096 }
1097}
1098
1099static void io_kill_timeouts(struct io_ring_ctx *ctx)
1100{
1101 struct io_kiocb *req, *tmp;
1102
1103 spin_lock_irq(&ctx->completion_lock);
1104 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1105 io_kill_timeout(req);
1106 spin_unlock_irq(&ctx->completion_lock);
1107}
1108
Pavel Begunkov04518942020-05-26 20:34:05 +03001109static void __io_queue_deferred(struct io_ring_ctx *ctx)
1110{
1111 do {
1112 struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1113 struct io_kiocb, list);
1114
1115 if (req_need_defer(req))
1116 break;
1117 list_del_init(&req->list);
1118 io_queue_async_work(req);
1119 } while (!list_empty(&ctx->defer_list));
1120}
1121
Pavel Begunkov360428f2020-05-30 14:54:17 +03001122static void io_flush_timeouts(struct io_ring_ctx *ctx)
1123{
1124 while (!list_empty(&ctx->timeout_list)) {
1125 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1126 struct io_kiocb, list);
1127
1128 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
1129 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001130 if (req->timeout.target_seq != ctx->cached_cq_tail
1131 - atomic_read(&ctx->cq_timeouts))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001132 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001133
Pavel Begunkov360428f2020-05-30 14:54:17 +03001134 list_del_init(&req->list);
1135 io_kill_timeout(req);
1136 }
1137}
1138
Jens Axboede0617e2019-04-06 21:51:27 -06001139static void io_commit_cqring(struct io_ring_ctx *ctx)
1140{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001141 io_flush_timeouts(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001142 __io_commit_cqring(ctx);
1143
Pavel Begunkov04518942020-05-26 20:34:05 +03001144 if (unlikely(!list_empty(&ctx->defer_list)))
1145 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001146}
1147
Jens Axboe2b188cc2019-01-07 10:46:33 -07001148static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1149{
Hristo Venev75b28af2019-08-26 17:23:46 +00001150 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001151 unsigned tail;
1152
1153 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001154 /*
1155 * writes to the cq entry need to come after reading head; the
1156 * control dependency is enough as we're using WRITE_ONCE to
1157 * fill the cq entry
1158 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001159 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001160 return NULL;
1161
1162 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001163 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001164}
1165
Jens Axboef2842ab2020-01-08 11:04:00 -07001166static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1167{
Jens Axboef0b493e2020-02-01 21:30:11 -07001168 if (!ctx->cq_ev_fd)
1169 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001170 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1171 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001172 if (!ctx->eventfd_async)
1173 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001174 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001175}
1176
Jens Axboeb41e9852020-02-17 09:52:41 -07001177static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001178{
1179 if (waitqueue_active(&ctx->wait))
1180 wake_up(&ctx->wait);
1181 if (waitqueue_active(&ctx->sqo_wait))
1182 wake_up(&ctx->sqo_wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001183 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001184 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001185}
1186
Jens Axboec4a2ed72019-11-21 21:01:26 -07001187/* Returns true if there are no backlogged entries after the flush */
1188static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001189{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001190 struct io_rings *rings = ctx->rings;
1191 struct io_uring_cqe *cqe;
1192 struct io_kiocb *req;
1193 unsigned long flags;
1194 LIST_HEAD(list);
1195
1196 if (!force) {
1197 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001198 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001199 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1200 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001201 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001202 }
1203
1204 spin_lock_irqsave(&ctx->completion_lock, flags);
1205
1206 /* if force is set, the ring is going away. always drop after that */
1207 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001208 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001209
Jens Axboec4a2ed72019-11-21 21:01:26 -07001210 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001211 while (!list_empty(&ctx->cq_overflow_list)) {
1212 cqe = io_get_cqring(ctx);
1213 if (!cqe && !force)
1214 break;
1215
1216 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1217 list);
1218 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001219 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001220 if (cqe) {
1221 WRITE_ONCE(cqe->user_data, req->user_data);
1222 WRITE_ONCE(cqe->res, req->result);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001223 WRITE_ONCE(cqe->flags, req->cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001224 } else {
1225 WRITE_ONCE(ctx->rings->cq_overflow,
1226 atomic_inc_return(&ctx->cached_cq_overflow));
1227 }
1228 }
1229
1230 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001231 if (cqe) {
1232 clear_bit(0, &ctx->sq_check_overflow);
1233 clear_bit(0, &ctx->cq_check_overflow);
1234 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001235 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1236 io_cqring_ev_posted(ctx);
1237
1238 while (!list_empty(&list)) {
1239 req = list_first_entry(&list, struct io_kiocb, list);
1240 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001241 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001242 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001243
1244 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001245}
1246
Jens Axboebcda7ba2020-02-23 16:42:51 -07001247static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001249 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250 struct io_uring_cqe *cqe;
1251
Jens Axboe78e19bb2019-11-06 15:21:34 -07001252 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001253
Jens Axboe2b188cc2019-01-07 10:46:33 -07001254 /*
1255 * If we can't get a cq entry, userspace overflowed the
1256 * submission (by quite a lot). Increment the overflow count in
1257 * the ring.
1258 */
1259 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001260 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001261 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001263 WRITE_ONCE(cqe->flags, cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001264 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265 WRITE_ONCE(ctx->rings->cq_overflow,
1266 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001267 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001268 if (list_empty(&ctx->cq_overflow_list)) {
1269 set_bit(0, &ctx->sq_check_overflow);
1270 set_bit(0, &ctx->cq_check_overflow);
1271 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001272 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001273 refcount_inc(&req->refs);
1274 req->result = res;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001275 req->cflags = cflags;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001276 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001277 }
1278}
1279
Jens Axboebcda7ba2020-02-23 16:42:51 -07001280static void io_cqring_fill_event(struct io_kiocb *req, long res)
1281{
1282 __io_cqring_fill_event(req, res, 0);
1283}
1284
1285static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001286{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001287 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288 unsigned long flags;
1289
1290 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001291 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001292 io_commit_cqring(ctx);
1293 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1294
Jens Axboe8c838782019-03-12 15:48:16 -06001295 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001296}
1297
Jens Axboebcda7ba2020-02-23 16:42:51 -07001298static void io_cqring_add_event(struct io_kiocb *req, long res)
1299{
1300 __io_cqring_add_event(req, res, 0);
1301}
1302
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001303static inline bool io_is_fallback_req(struct io_kiocb *req)
1304{
1305 return req == (struct io_kiocb *)
1306 ((unsigned long) req->ctx->fallback_req & ~1UL);
1307}
1308
1309static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1310{
1311 struct io_kiocb *req;
1312
1313 req = ctx->fallback_req;
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001314 if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001315 return req;
1316
1317 return NULL;
1318}
1319
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001320static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
1321 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001322{
Jens Axboefd6fab22019-03-14 16:30:06 -06001323 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001324 struct io_kiocb *req;
1325
Jens Axboe2579f912019-01-09 09:10:43 -07001326 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001327 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001328 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001329 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001330 } else if (!state->free_reqs) {
1331 size_t sz;
1332 int ret;
1333
1334 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001335 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1336
1337 /*
1338 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1339 * retry single alloc to be on the safe side.
1340 */
1341 if (unlikely(ret <= 0)) {
1342 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1343 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001344 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001345 ret = 1;
1346 }
Jens Axboe2579f912019-01-09 09:10:43 -07001347 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001348 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001349 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001350 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001351 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001352 }
1353
Jens Axboe2579f912019-01-09 09:10:43 -07001354 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001355fallback:
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03001356 return io_get_fallback_req(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001357}
1358
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001359static inline void io_put_file(struct io_kiocb *req, struct file *file,
1360 bool fixed)
1361{
1362 if (fixed)
Xiaoguang Wang05589552020-03-31 14:05:18 +08001363 percpu_ref_put(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001364 else
1365 fput(file);
1366}
1367
Jens Axboec6ca97b302019-12-28 12:11:08 -07001368static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001369{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001370 if (req->flags & REQ_F_NEED_CLEANUP)
1371 io_cleanup_req(req);
1372
YueHaibing96fd84d2020-01-07 22:22:44 +08001373 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001374 if (req->file)
1375 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboe3537b6a2020-04-03 11:19:06 -06001376 if (req->task)
1377 put_task_struct(req->task);
Jens Axboecccf0ee2020-01-27 16:34:48 -07001378
1379 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001380}
1381
1382static void __io_free_req(struct io_kiocb *req)
1383{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001384 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001385
Jens Axboefcb323c2019-10-24 12:39:47 -06001386 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001387 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001388 unsigned long flags;
1389
1390 spin_lock_irqsave(&ctx->inflight_lock, flags);
1391 list_del(&req->inflight_entry);
1392 if (waitqueue_active(&ctx->inflight_wait))
1393 wake_up(&ctx->inflight_wait);
1394 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1395 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001396
1397 percpu_ref_put(&req->ctx->refs);
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03001398 if (likely(!io_is_fallback_req(req)))
1399 kmem_cache_free(req_cachep, req);
1400 else
Bijan Mottahedehdd461af2020-04-29 17:47:50 -07001401 clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001402}
1403
Jens Axboec6ca97b302019-12-28 12:11:08 -07001404struct req_batch {
1405 void *reqs[IO_IOPOLL_BATCH];
1406 int to_free;
1407 int need_iter;
1408};
1409
1410static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1411{
1412 if (!rb->to_free)
1413 return;
1414 if (rb->need_iter) {
1415 int i, inflight = 0;
1416 unsigned long flags;
1417
1418 for (i = 0; i < rb->to_free; i++) {
1419 struct io_kiocb *req = rb->reqs[i];
1420
Jens Axboec6ca97b302019-12-28 12:11:08 -07001421 if (req->flags & REQ_F_INFLIGHT)
1422 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001423 __io_req_aux_free(req);
1424 }
1425 if (!inflight)
1426 goto do_free;
1427
1428 spin_lock_irqsave(&ctx->inflight_lock, flags);
1429 for (i = 0; i < rb->to_free; i++) {
1430 struct io_kiocb *req = rb->reqs[i];
1431
Jens Axboe10fef4b2020-01-09 07:52:28 -07001432 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001433 list_del(&req->inflight_entry);
1434 if (!--inflight)
1435 break;
1436 }
1437 }
1438 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1439
1440 if (waitqueue_active(&ctx->inflight_wait))
1441 wake_up(&ctx->inflight_wait);
1442 }
1443do_free:
1444 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1445 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001446 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001447}
1448
Jackie Liua197f662019-11-08 08:09:12 -07001449static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001450{
Jackie Liua197f662019-11-08 08:09:12 -07001451 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001452 int ret;
1453
Jens Axboe2d283902019-12-04 11:08:05 -07001454 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001455 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001456 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001457 io_commit_cqring(ctx);
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001458 req->flags &= ~REQ_F_LINK_HEAD;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001459 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001460 return true;
1461 }
1462
1463 return false;
1464}
1465
Jens Axboeba816ad2019-09-28 11:36:45 -06001466static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001467{
Jens Axboe2665abf2019-11-05 12:40:47 -07001468 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001469 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001470
Jens Axboe4d7dd462019-11-20 13:03:52 -07001471 /* Already got next link */
1472 if (req->flags & REQ_F_LINK_NEXT)
1473 return;
1474
Jens Axboe9e645e112019-05-10 16:07:28 -06001475 /*
1476 * The list should never be empty when we are called here. But could
1477 * potentially happen if the chain is messed up, check to be on the
1478 * safe side.
1479 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001480 while (!list_empty(&req->link_list)) {
1481 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1482 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001483
Pavel Begunkov44932332019-12-05 16:16:35 +03001484 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1485 (nxt->flags & REQ_F_TIMEOUT))) {
1486 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001487 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001488 req->flags &= ~REQ_F_LINK_TIMEOUT;
1489 continue;
1490 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001491
Pavel Begunkov44932332019-12-05 16:16:35 +03001492 list_del_init(&req->link_list);
1493 if (!list_empty(&nxt->link_list))
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001494 nxt->flags |= REQ_F_LINK_HEAD;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001495 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001496 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001497 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001498
Jens Axboe4d7dd462019-11-20 13:03:52 -07001499 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001500 if (wake_ev)
1501 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001502}
1503
1504/*
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001505 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
Jens Axboe9e645e112019-05-10 16:07:28 -06001506 */
1507static void io_fail_links(struct io_kiocb *req)
1508{
Jens Axboe2665abf2019-11-05 12:40:47 -07001509 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001510 unsigned long flags;
1511
1512 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001513
1514 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001515 struct io_kiocb *link = list_first_entry(&req->link_list,
1516 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001517
Pavel Begunkov44932332019-12-05 16:16:35 +03001518 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001519 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001520
1521 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001522 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001523 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001524 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001525 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001526 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001527 }
Jens Axboe5d960722019-11-19 15:31:28 -07001528 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001529 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001530
1531 io_commit_cqring(ctx);
1532 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1533 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001534}
1535
Jens Axboe4d7dd462019-11-20 13:03:52 -07001536static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001537{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001538 if (likely(!(req->flags & REQ_F_LINK_HEAD)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001539 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001540
Jens Axboe9e645e112019-05-10 16:07:28 -06001541 /*
1542 * If LINK is set, we have dependent requests in this chain. If we
1543 * didn't fail this request, queue the first one up, moving any other
1544 * dependencies to the next request. In case of failure, fail the rest
1545 * of the chain.
1546 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001547 if (req->flags & REQ_F_FAIL_LINK) {
1548 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001549 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1550 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001551 struct io_ring_ctx *ctx = req->ctx;
1552 unsigned long flags;
1553
1554 /*
1555 * If this is a timeout link, we could be racing with the
1556 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001557 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001558 */
1559 spin_lock_irqsave(&ctx->completion_lock, flags);
1560 io_req_link_next(req, nxt);
1561 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1562 } else {
1563 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001564 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001565}
Jens Axboe9e645e112019-05-10 16:07:28 -06001566
Jackie Liuc69f8db2019-11-09 11:00:08 +08001567static void io_free_req(struct io_kiocb *req)
1568{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001569 struct io_kiocb *nxt = NULL;
1570
1571 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001572 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001573
1574 if (nxt)
1575 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001576}
1577
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001578static void io_link_work_cb(struct io_wq_work **workptr)
1579{
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001580 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1581 struct io_kiocb *link;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001582
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001583 link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001584 io_queue_linked_timeout(link);
1585 io_wq_submit_work(workptr);
1586}
1587
1588static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1589{
1590 struct io_kiocb *link;
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001591 const struct io_op_def *def = &io_op_defs[nxt->opcode];
1592
1593 if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1594 io_wq_hash_work(&nxt->work, file_inode(nxt->file));
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001595
1596 *workptr = &nxt->work;
1597 link = io_prep_linked_timeout(nxt);
Pavel Begunkov18a542f2020-03-23 00:23:29 +03001598 if (link)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001599 nxt->work.func = io_link_work_cb;
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001600}
1601
Jens Axboeba816ad2019-09-28 11:36:45 -06001602/*
1603 * Drop reference to request, return next in chain (if there is one) if this
1604 * was the last reference to this request.
1605 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001606__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001607static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001608{
Jens Axboe2a44f462020-02-25 13:25:41 -07001609 if (refcount_dec_and_test(&req->refs)) {
1610 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001611 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001612 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001613}
1614
Jens Axboe2b188cc2019-01-07 10:46:33 -07001615static void io_put_req(struct io_kiocb *req)
1616{
Jens Axboedef596e2019-01-09 08:59:42 -07001617 if (refcount_dec_and_test(&req->refs))
1618 io_free_req(req);
1619}
1620
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001621static void io_steal_work(struct io_kiocb *req,
1622 struct io_wq_work **workptr)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001623{
1624 /*
1625 * It's in an io-wq worker, so there always should be at least
1626 * one reference, which will be dropped in io_put_work() just
1627 * after the current handler returns.
1628 *
1629 * It also means, that if the counter dropped to 1, then there is
1630 * no asynchronous users left, so it's safe to steal the next work.
1631 */
Pavel Begunkov7a743e22020-03-03 21:33:13 +03001632 if (refcount_read(&req->refs) == 1) {
1633 struct io_kiocb *nxt = NULL;
1634
1635 io_req_find_next(req, &nxt);
1636 if (nxt)
1637 io_wq_assign_next(workptr, nxt);
1638 }
1639}
1640
Jens Axboe978db572019-11-14 22:39:04 -07001641/*
1642 * Must only be used if we don't need to care about links, usually from
1643 * within the completion handling itself.
1644 */
1645static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001646{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001647 /* drop both submit and complete references */
1648 if (refcount_sub_and_test(2, &req->refs))
1649 __io_free_req(req);
1650}
1651
Jens Axboe978db572019-11-14 22:39:04 -07001652static void io_double_put_req(struct io_kiocb *req)
1653{
1654 /* drop both submit and complete references */
1655 if (refcount_sub_and_test(2, &req->refs))
1656 io_free_req(req);
1657}
1658
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001659static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001660{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001661 struct io_rings *rings = ctx->rings;
1662
Jens Axboead3eb2c2019-12-18 17:12:20 -07001663 if (test_bit(0, &ctx->cq_check_overflow)) {
1664 /*
1665 * noflush == true is from the waitqueue handler, just ensure
1666 * we wake up the task, and the next invocation will flush the
1667 * entries. We cannot safely to it from here.
1668 */
1669 if (noflush && !list_empty(&ctx->cq_overflow_list))
1670 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001671
Jens Axboead3eb2c2019-12-18 17:12:20 -07001672 io_cqring_overflow_flush(ctx, false);
1673 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001674
Jens Axboea3a0e432019-08-20 11:03:11 -06001675 /* See comment at the top of this file */
1676 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001677 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001678}
1679
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001680static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1681{
1682 struct io_rings *rings = ctx->rings;
1683
1684 /* make sure SQ entry isn't read before tail */
1685 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1686}
1687
Jens Axboe8237e042019-12-28 10:48:22 -07001688static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001689{
Pavel Begunkovdea3b492020-04-12 02:05:04 +03001690 if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
Jens Axboec6ca97b302019-12-28 12:11:08 -07001691 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001692
Jens Axboe9d9e88a2020-05-13 12:53:19 -06001693 if (req->file || req->io)
Jens Axboec6ca97b302019-12-28 12:11:08 -07001694 rb->need_iter++;
1695
1696 rb->reqs[rb->to_free++] = req;
1697 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1698 io_free_req_many(req->ctx, rb);
1699 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001700}
1701
Jens Axboebcda7ba2020-02-23 16:42:51 -07001702static int io_put_kbuf(struct io_kiocb *req)
1703{
Jens Axboe4d954c22020-02-27 07:31:19 -07001704 struct io_buffer *kbuf;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001705 int cflags;
1706
Jens Axboe4d954c22020-02-27 07:31:19 -07001707 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Jens Axboebcda7ba2020-02-23 16:42:51 -07001708 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1709 cflags |= IORING_CQE_F_BUFFER;
1710 req->rw.addr = 0;
1711 kfree(kbuf);
1712 return cflags;
1713}
1714
Jens Axboedef596e2019-01-09 08:59:42 -07001715/*
1716 * Find and free completed poll iocbs
1717 */
1718static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1719 struct list_head *done)
1720{
Jens Axboe8237e042019-12-28 10:48:22 -07001721 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001722 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001723
Jens Axboec6ca97b302019-12-28 12:11:08 -07001724 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001725 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07001726 int cflags = 0;
1727
Jens Axboedef596e2019-01-09 08:59:42 -07001728 req = list_first_entry(done, struct io_kiocb, list);
1729 list_del(&req->list);
1730
Jens Axboebcda7ba2020-02-23 16:42:51 -07001731 if (req->flags & REQ_F_BUFFER_SELECTED)
1732 cflags = io_put_kbuf(req);
1733
1734 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07001735 (*nr_events)++;
1736
Jens Axboe8237e042019-12-28 10:48:22 -07001737 if (refcount_dec_and_test(&req->refs) &&
1738 !io_req_multi_free(&rb, req))
1739 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001740 }
Jens Axboedef596e2019-01-09 08:59:42 -07001741
Jens Axboe09bb8392019-03-13 12:39:28 -06001742 io_commit_cqring(ctx);
Xiaoguang Wang32b22442020-03-11 09:26:09 +08001743 if (ctx->flags & IORING_SETUP_SQPOLL)
1744 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001745 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001746}
1747
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001748static void io_iopoll_queue(struct list_head *again)
1749{
1750 struct io_kiocb *req;
1751
1752 do {
1753 req = list_first_entry(again, struct io_kiocb, list);
1754 list_del(&req->list);
1755 refcount_inc(&req->refs);
1756 io_queue_async_work(req);
1757 } while (!list_empty(again));
1758}
1759
Jens Axboedef596e2019-01-09 08:59:42 -07001760static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1761 long min)
1762{
1763 struct io_kiocb *req, *tmp;
1764 LIST_HEAD(done);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001765 LIST_HEAD(again);
Jens Axboedef596e2019-01-09 08:59:42 -07001766 bool spin;
1767 int ret;
1768
1769 /*
1770 * Only spin for completions if we don't have multiple devices hanging
1771 * off our complete list, and we're under the requested amount.
1772 */
1773 spin = !ctx->poll_multi_file && *nr_events < min;
1774
1775 ret = 0;
1776 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001777 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001778
1779 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001780 * Move completed and retryable entries to our local lists.
1781 * If we find a request that requires polling, break out
1782 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07001783 */
1784 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1785 list_move_tail(&req->list, &done);
1786 continue;
1787 }
1788 if (!list_empty(&done))
1789 break;
1790
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001791 if (req->result == -EAGAIN) {
1792 list_move_tail(&req->list, &again);
1793 continue;
1794 }
1795 if (!list_empty(&again))
1796 break;
1797
Jens Axboedef596e2019-01-09 08:59:42 -07001798 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1799 if (ret < 0)
1800 break;
1801
1802 if (ret && spin)
1803 spin = false;
1804 ret = 0;
1805 }
1806
1807 if (!list_empty(&done))
1808 io_iopoll_complete(ctx, nr_events, &done);
1809
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07001810 if (!list_empty(&again))
1811 io_iopoll_queue(&again);
1812
Jens Axboedef596e2019-01-09 08:59:42 -07001813 return ret;
1814}
1815
1816/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001817 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001818 * non-spinning poll check - we'll still enter the driver poll loop, but only
1819 * as a non-spinning completion check.
1820 */
1821static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1822 long min)
1823{
Jens Axboe08f54392019-08-21 22:19:11 -06001824 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001825 int ret;
1826
1827 ret = io_do_iopoll(ctx, nr_events, min);
1828 if (ret < 0)
1829 return ret;
1830 if (!min || *nr_events >= min)
1831 return 0;
1832 }
1833
1834 return 1;
1835}
1836
1837/*
1838 * We can't just wait for polled events to come to us, we have to actively
1839 * find and complete them.
1840 */
1841static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1842{
1843 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1844 return;
1845
1846 mutex_lock(&ctx->uring_lock);
1847 while (!list_empty(&ctx->poll_list)) {
1848 unsigned int nr_events = 0;
1849
1850 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001851
1852 /*
1853 * Ensure we allow local-to-the-cpu processing to take place,
1854 * in this case we need to ensure that we reap all events.
1855 */
1856 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001857 }
1858 mutex_unlock(&ctx->uring_lock);
1859}
1860
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001861static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1862 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001863{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001864 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001865
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001866 /*
1867 * We disallow the app entering submit/complete with polling, but we
1868 * still need to lock the ring to prevent racing with polled issue
1869 * that got punted to a workqueue.
1870 */
1871 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001872 do {
1873 int tmin = 0;
1874
Jens Axboe500f9fb2019-08-19 12:15:59 -06001875 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001876 * Don't enter poll loop if we already have events pending.
1877 * If we do, we can potentially be spinning for commands that
1878 * already triggered a CQE (eg in error).
1879 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001880 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001881 break;
1882
1883 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001884 * If a submit got punted to a workqueue, we can have the
1885 * application entering polling for a command before it gets
1886 * issued. That app will hold the uring_lock for the duration
1887 * of the poll right here, so we need to take a breather every
1888 * now and then to ensure that the issue has a chance to add
1889 * the poll to the issued list. Otherwise we can spin here
1890 * forever, while the workqueue is stuck trying to acquire the
1891 * very same mutex.
1892 */
1893 if (!(++iters & 7)) {
1894 mutex_unlock(&ctx->uring_lock);
1895 mutex_lock(&ctx->uring_lock);
1896 }
1897
Jens Axboedef596e2019-01-09 08:59:42 -07001898 if (*nr_events < min)
1899 tmin = min - *nr_events;
1900
1901 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1902 if (ret <= 0)
1903 break;
1904 ret = 0;
1905 } while (min && !*nr_events && !need_resched());
1906
Jens Axboe500f9fb2019-08-19 12:15:59 -06001907 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001908 return ret;
1909}
1910
Jens Axboe491381ce2019-10-17 09:20:46 -06001911static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001912{
Jens Axboe491381ce2019-10-17 09:20:46 -06001913 /*
1914 * Tell lockdep we inherited freeze protection from submission
1915 * thread.
1916 */
1917 if (req->flags & REQ_F_ISREG) {
1918 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001919
Jens Axboe491381ce2019-10-17 09:20:46 -06001920 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001921 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001922 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001923}
1924
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001925static inline void req_set_fail_links(struct io_kiocb *req)
1926{
1927 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1928 req->flags |= REQ_F_FAIL_LINK;
1929}
1930
Jens Axboeba816ad2019-09-28 11:36:45 -06001931static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001932{
Jens Axboe9adbd452019-12-20 08:45:55 -07001933 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001934 int cflags = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001935
Jens Axboe491381ce2019-10-17 09:20:46 -06001936 if (kiocb->ki_flags & IOCB_WRITE)
1937 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001938
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001939 if (res != req->result)
1940 req_set_fail_links(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001941 if (req->flags & REQ_F_BUFFER_SELECTED)
1942 cflags = io_put_kbuf(req);
1943 __io_cqring_add_event(req, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06001944}
1945
1946static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1947{
Jens Axboe9adbd452019-12-20 08:45:55 -07001948 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001949
1950 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001951 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001952}
1953
Jens Axboedef596e2019-01-09 08:59:42 -07001954static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1955{
Jens Axboe9adbd452019-12-20 08:45:55 -07001956 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001957
Jens Axboe491381ce2019-10-17 09:20:46 -06001958 if (kiocb->ki_flags & IOCB_WRITE)
1959 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001960
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001961 if (res != req->result)
1962 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001963 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001964 if (res != -EAGAIN)
1965 req->flags |= REQ_F_IOPOLL_COMPLETED;
1966}
1967
1968/*
1969 * After the iocb has been issued, it's safe to be found on the poll list.
1970 * Adding the kiocb to the list AFTER submission ensures that we don't
1971 * find it from a io_iopoll_getevents() thread before the issuer is done
1972 * accessing the kiocb cookie.
1973 */
1974static void io_iopoll_req_issued(struct io_kiocb *req)
1975{
1976 struct io_ring_ctx *ctx = req->ctx;
1977
1978 /*
1979 * Track whether we have multiple files in our lists. This will impact
1980 * how we do polling eventually, not spinning if we're on potentially
1981 * different devices.
1982 */
1983 if (list_empty(&ctx->poll_list)) {
1984 ctx->poll_multi_file = false;
1985 } else if (!ctx->poll_multi_file) {
1986 struct io_kiocb *list_req;
1987
1988 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1989 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001990 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001991 ctx->poll_multi_file = true;
1992 }
1993
1994 /*
1995 * For fast devices, IO may have already completed. If it has, add
1996 * it to the front so we find it first.
1997 */
1998 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1999 list_add(&req->list, &ctx->poll_list);
2000 else
2001 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002002
2003 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2004 wq_has_sleeper(&ctx->sqo_wait))
2005 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002006}
2007
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002008static void __io_state_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07002009{
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002010 int diff = state->has_refs - state->used_refs;
Jens Axboe9a56a232019-01-09 09:06:50 -07002011
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002012 if (diff)
2013 fput_many(state->file, diff);
2014 state->file = NULL;
2015}
2016
2017static inline void io_state_file_put(struct io_submit_state *state)
2018{
2019 if (state->file)
2020 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002021}
2022
2023/*
2024 * Get as many references to a file as we have IOs left in this submission,
2025 * assuming most submissions are for one file, or at least that each file
2026 * has more than one submission.
2027 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002028static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002029{
2030 if (!state)
2031 return fget(fd);
2032
2033 if (state->file) {
2034 if (state->fd == fd) {
2035 state->used_refs++;
2036 state->ios_left--;
2037 return state->file;
2038 }
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002039 __io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002040 }
2041 state->file = fget_many(fd, state->ios_left);
2042 if (!state->file)
2043 return NULL;
2044
2045 state->fd = fd;
2046 state->has_refs = state->ios_left;
2047 state->used_refs = 1;
2048 state->ios_left--;
2049 return state->file;
2050}
2051
Jens Axboe2b188cc2019-01-07 10:46:33 -07002052/*
2053 * If we tracked the file through the SCM inflight mechanism, we could support
2054 * any file. For now, just ensure that anything potentially problematic is done
2055 * inline.
2056 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002057static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002058{
2059 umode_t mode = file_inode(file)->i_mode;
2060
Jens Axboe10d59342019-12-09 20:16:22 -07002061 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002062 return true;
2063 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2064 return true;
2065
Jens Axboeaf197f52020-04-28 13:15:06 -06002066 if (!(file->f_mode & FMODE_NOWAIT))
2067 return false;
2068
2069 if (rw == READ)
2070 return file->f_op->read_iter != NULL;
2071
2072 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002073}
2074
Jens Axboe3529d8c2019-12-19 18:24:38 -07002075static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2076 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002077{
Jens Axboedef596e2019-01-09 08:59:42 -07002078 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002079 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06002080 unsigned ioprio;
2081 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002082
Jens Axboe491381ce2019-10-17 09:20:46 -06002083 if (S_ISREG(file_inode(req->file)->i_mode))
2084 req->flags |= REQ_F_ISREG;
2085
Jens Axboe2b188cc2019-01-07 10:46:33 -07002086 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07002087 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
2088 req->flags |= REQ_F_CUR_POS;
2089 kiocb->ki_pos = req->file->f_pos;
2090 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002091 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002092 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2093 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2094 if (unlikely(ret))
2095 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002096
2097 ioprio = READ_ONCE(sqe->ioprio);
2098 if (ioprio) {
2099 ret = ioprio_check_cap(ioprio);
2100 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002101 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002102
2103 kiocb->ki_ioprio = ioprio;
2104 } else
2105 kiocb->ki_ioprio = get_current_ioprio();
2106
Stefan Bühler8449eed2019-04-27 20:34:19 +02002107 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06002108 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
2109 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02002110 req->flags |= REQ_F_NOWAIT;
2111
2112 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002113 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02002114
Jens Axboedef596e2019-01-09 08:59:42 -07002115 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002116 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2117 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002118 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002119
Jens Axboedef596e2019-01-09 08:59:42 -07002120 kiocb->ki_flags |= IOCB_HIPRI;
2121 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06002122 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002123 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002124 if (kiocb->ki_flags & IOCB_HIPRI)
2125 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002126 kiocb->ki_complete = io_complete_rw;
2127 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002128
Jens Axboe3529d8c2019-12-19 18:24:38 -07002129 req->rw.addr = READ_ONCE(sqe->addr);
2130 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002131 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002132 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002133}
2134
2135static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2136{
2137 switch (ret) {
2138 case -EIOCBQUEUED:
2139 break;
2140 case -ERESTARTSYS:
2141 case -ERESTARTNOINTR:
2142 case -ERESTARTNOHAND:
2143 case -ERESTART_RESTARTBLOCK:
2144 /*
2145 * We can't just restart the syscall, since previously
2146 * submitted sqes may already be in progress. Just fail this
2147 * IO with EINTR.
2148 */
2149 ret = -EINTR;
2150 /* fall through */
2151 default:
2152 kiocb->ki_complete(kiocb, ret, 0);
2153 }
2154}
2155
Pavel Begunkov014db002020-03-03 21:33:12 +03002156static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
Jens Axboeba816ad2019-09-28 11:36:45 -06002157{
Jens Axboeba042912019-12-25 16:33:42 -07002158 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2159
2160 if (req->flags & REQ_F_CUR_POS)
2161 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002162 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov014db002020-03-03 21:33:12 +03002163 io_complete_rw(kiocb, ret, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06002164 else
2165 io_rw_done(kiocb, ret);
2166}
2167
Jens Axboe9adbd452019-12-20 08:45:55 -07002168static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002169 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002170{
Jens Axboe9adbd452019-12-20 08:45:55 -07002171 struct io_ring_ctx *ctx = req->ctx;
2172 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002173 struct io_mapped_ubuf *imu;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002174 u16 index, buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002175 size_t offset;
2176 u64 buf_addr;
2177
2178 /* attempt to use fixed buffers without having provided iovecs */
2179 if (unlikely(!ctx->user_bufs))
2180 return -EFAULT;
2181
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002182 buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002183 if (unlikely(buf_index >= ctx->nr_user_bufs))
2184 return -EFAULT;
2185
2186 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2187 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002188 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002189
2190 /* overflow */
2191 if (buf_addr + len < buf_addr)
2192 return -EFAULT;
2193 /* not inside the mapped region */
2194 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2195 return -EFAULT;
2196
2197 /*
2198 * May not be a start of buffer, set size appropriately
2199 * and advance us to the beginning.
2200 */
2201 offset = buf_addr - imu->ubuf;
2202 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002203
2204 if (offset) {
2205 /*
2206 * Don't use iov_iter_advance() here, as it's really slow for
2207 * using the latter parts of a big fixed buffer - it iterates
2208 * over each segment manually. We can cheat a bit here, because
2209 * we know that:
2210 *
2211 * 1) it's a BVEC iter, we set it up
2212 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2213 * first and last bvec
2214 *
2215 * So just find our index, and adjust the iterator afterwards.
2216 * If the offset is within the first bvec (or the whole first
2217 * bvec, just use iov_iter_advance(). This makes it easier
2218 * since we can just skip the first segment, which may not
2219 * be PAGE_SIZE aligned.
2220 */
2221 const struct bio_vec *bvec = imu->bvec;
2222
2223 if (offset <= bvec->bv_len) {
2224 iov_iter_advance(iter, offset);
2225 } else {
2226 unsigned long seg_skip;
2227
2228 /* skip first vec */
2229 offset -= bvec->bv_len;
2230 seg_skip = 1 + (offset >> PAGE_SHIFT);
2231
2232 iter->bvec = bvec + seg_skip;
2233 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002234 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002235 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002236 }
2237 }
2238
Jens Axboe5e559562019-11-13 16:12:46 -07002239 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002240}
2241
Jens Axboebcda7ba2020-02-23 16:42:51 -07002242static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2243{
2244 if (needs_lock)
2245 mutex_unlock(&ctx->uring_lock);
2246}
2247
2248static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2249{
2250 /*
2251 * "Normal" inline submissions always hold the uring_lock, since we
2252 * grab it from the system call. Same is true for the SQPOLL offload.
2253 * The only exception is when we've detached the request and issue it
2254 * from an async worker thread, grab the lock for that case.
2255 */
2256 if (needs_lock)
2257 mutex_lock(&ctx->uring_lock);
2258}
2259
2260static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2261 int bgid, struct io_buffer *kbuf,
2262 bool needs_lock)
2263{
2264 struct io_buffer *head;
2265
2266 if (req->flags & REQ_F_BUFFER_SELECTED)
2267 return kbuf;
2268
2269 io_ring_submit_lock(req->ctx, needs_lock);
2270
2271 lockdep_assert_held(&req->ctx->uring_lock);
2272
2273 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2274 if (head) {
2275 if (!list_empty(&head->list)) {
2276 kbuf = list_last_entry(&head->list, struct io_buffer,
2277 list);
2278 list_del(&kbuf->list);
2279 } else {
2280 kbuf = head;
2281 idr_remove(&req->ctx->io_buffer_idr, bgid);
2282 }
2283 if (*len > kbuf->len)
2284 *len = kbuf->len;
2285 } else {
2286 kbuf = ERR_PTR(-ENOBUFS);
2287 }
2288
2289 io_ring_submit_unlock(req->ctx, needs_lock);
2290
2291 return kbuf;
2292}
2293
Jens Axboe4d954c22020-02-27 07:31:19 -07002294static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2295 bool needs_lock)
2296{
2297 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002298 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002299
2300 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002301 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002302 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2303 if (IS_ERR(kbuf))
2304 return kbuf;
2305 req->rw.addr = (u64) (unsigned long) kbuf;
2306 req->flags |= REQ_F_BUFFER_SELECTED;
2307 return u64_to_user_ptr(kbuf->addr);
2308}
2309
2310#ifdef CONFIG_COMPAT
2311static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2312 bool needs_lock)
2313{
2314 struct compat_iovec __user *uiov;
2315 compat_ssize_t clen;
2316 void __user *buf;
2317 ssize_t len;
2318
2319 uiov = u64_to_user_ptr(req->rw.addr);
2320 if (!access_ok(uiov, sizeof(*uiov)))
2321 return -EFAULT;
2322 if (__get_user(clen, &uiov->iov_len))
2323 return -EFAULT;
2324 if (clen < 0)
2325 return -EINVAL;
2326
2327 len = clen;
2328 buf = io_rw_buffer_select(req, &len, needs_lock);
2329 if (IS_ERR(buf))
2330 return PTR_ERR(buf);
2331 iov[0].iov_base = buf;
2332 iov[0].iov_len = (compat_size_t) len;
2333 return 0;
2334}
2335#endif
2336
2337static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2338 bool needs_lock)
2339{
2340 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2341 void __user *buf;
2342 ssize_t len;
2343
2344 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2345 return -EFAULT;
2346
2347 len = iov[0].iov_len;
2348 if (len < 0)
2349 return -EINVAL;
2350 buf = io_rw_buffer_select(req, &len, needs_lock);
2351 if (IS_ERR(buf))
2352 return PTR_ERR(buf);
2353 iov[0].iov_base = buf;
2354 iov[0].iov_len = len;
2355 return 0;
2356}
2357
2358static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2359 bool needs_lock)
2360{
2361 if (req->flags & REQ_F_BUFFER_SELECTED)
2362 return 0;
2363 if (!req->rw.len)
2364 return 0;
2365 else if (req->rw.len > 1)
2366 return -EINVAL;
2367
2368#ifdef CONFIG_COMPAT
2369 if (req->ctx->compat)
2370 return io_compat_import(req, iov, needs_lock);
2371#endif
2372
2373 return __io_iov_buffer_select(req, iov, needs_lock);
2374}
2375
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002376static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
Jens Axboebcda7ba2020-02-23 16:42:51 -07002377 struct iovec **iovec, struct iov_iter *iter,
2378 bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002379{
Jens Axboe9adbd452019-12-20 08:45:55 -07002380 void __user *buf = u64_to_user_ptr(req->rw.addr);
2381 size_t sqe_len = req->rw.len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002382 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002383 u8 opcode;
2384
Jens Axboed625c6e2019-12-17 19:53:05 -07002385 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002386 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002387 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002388 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002389 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002390
Jens Axboebcda7ba2020-02-23 16:42:51 -07002391 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002392 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07002393 return -EINVAL;
2394
Jens Axboe3a6820f2019-12-22 15:19:35 -07002395 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002396 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07002397 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
2398 if (IS_ERR(buf)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002399 *iovec = NULL;
Jens Axboe4d954c22020-02-27 07:31:19 -07002400 return PTR_ERR(buf);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002401 }
Jens Axboe3f9d6442020-03-11 12:27:04 -06002402 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002403 }
2404
Jens Axboe3a6820f2019-12-22 15:19:35 -07002405 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2406 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002407 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002408 }
2409
Jens Axboef67676d2019-12-02 11:03:47 -07002410 if (req->io) {
2411 struct io_async_rw *iorw = &req->io->rw;
2412
2413 *iovec = iorw->iov;
2414 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2415 if (iorw->iov == iorw->fast_iov)
2416 *iovec = NULL;
2417 return iorw->size;
2418 }
2419
Jens Axboe4d954c22020-02-27 07:31:19 -07002420 if (req->flags & REQ_F_BUFFER_SELECT) {
2421 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Jens Axboe3f9d6442020-03-11 12:27:04 -06002422 if (!ret) {
2423 ret = (*iovec)->iov_len;
2424 iov_iter_init(iter, rw, *iovec, 1, ret);
2425 }
Jens Axboe4d954c22020-02-27 07:31:19 -07002426 *iovec = NULL;
2427 return ret;
2428 }
2429
Jens Axboe2b188cc2019-01-07 10:46:33 -07002430#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002431 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002432 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2433 iovec, iter);
2434#endif
2435
2436 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2437}
2438
Jens Axboe32960612019-09-23 11:05:34 -06002439/*
2440 * For files that don't have ->read_iter() and ->write_iter(), handle them
2441 * by looping over ->read() or ->write() manually.
2442 */
2443static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2444 struct iov_iter *iter)
2445{
2446 ssize_t ret = 0;
2447
2448 /*
2449 * Don't support polled IO through this interface, and we can't
2450 * support non-blocking either. For the latter, this just causes
2451 * the kiocb to be handled from an async context.
2452 */
2453 if (kiocb->ki_flags & IOCB_HIPRI)
2454 return -EOPNOTSUPP;
2455 if (kiocb->ki_flags & IOCB_NOWAIT)
2456 return -EAGAIN;
2457
2458 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002459 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002460 ssize_t nr;
2461
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002462 if (!iov_iter_is_bvec(iter)) {
2463 iovec = iov_iter_iovec(iter);
2464 } else {
2465 /* fixed buffers import bvec */
2466 iovec.iov_base = kmap(iter->bvec->bv_page)
2467 + iter->iov_offset;
2468 iovec.iov_len = min(iter->count,
2469 iter->bvec->bv_len - iter->iov_offset);
2470 }
2471
Jens Axboe32960612019-09-23 11:05:34 -06002472 if (rw == READ) {
2473 nr = file->f_op->read(file, iovec.iov_base,
2474 iovec.iov_len, &kiocb->ki_pos);
2475 } else {
2476 nr = file->f_op->write(file, iovec.iov_base,
2477 iovec.iov_len, &kiocb->ki_pos);
2478 }
2479
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002480 if (iov_iter_is_bvec(iter))
2481 kunmap(iter->bvec->bv_page);
2482
Jens Axboe32960612019-09-23 11:05:34 -06002483 if (nr < 0) {
2484 if (!ret)
2485 ret = nr;
2486 break;
2487 }
2488 ret += nr;
2489 if (nr != iovec.iov_len)
2490 break;
2491 iov_iter_advance(iter, nr);
2492 }
2493
2494 return ret;
2495}
2496
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002497static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002498 struct iovec *iovec, struct iovec *fast_iov,
2499 struct iov_iter *iter)
2500{
2501 req->io->rw.nr_segs = iter->nr_segs;
2502 req->io->rw.size = io_size;
2503 req->io->rw.iov = iovec;
2504 if (!req->io->rw.iov) {
2505 req->io->rw.iov = req->io->rw.fast_iov;
Xiaoguang Wang45097da2020-04-08 22:29:58 +08002506 if (req->io->rw.iov != fast_iov)
2507 memcpy(req->io->rw.iov, fast_iov,
2508 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002509 } else {
2510 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002511 }
2512}
2513
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002514static inline int __io_alloc_async_ctx(struct io_kiocb *req)
2515{
2516 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
2517 return req->io == NULL;
2518}
2519
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002520static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002521{
Jens Axboed3656342019-12-18 09:50:26 -07002522 if (!io_op_defs[req->opcode].async_ctx)
2523 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002524
2525 return __io_alloc_async_ctx(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002526}
2527
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002528static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2529 struct iovec *iovec, struct iovec *fast_iov,
2530 struct iov_iter *iter)
2531{
Jens Axboe980ad262020-01-24 23:08:54 -07002532 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002533 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002534 if (!req->io) {
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08002535 if (__io_alloc_async_ctx(req))
Jens Axboe5d204bc2020-01-31 12:06:52 -07002536 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002537
Jens Axboe5d204bc2020-01-31 12:06:52 -07002538 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2539 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002540 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002541}
2542
Jens Axboe3529d8c2019-12-19 18:24:38 -07002543static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2544 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002545{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002546 struct io_async_ctx *io;
2547 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002548 ssize_t ret;
2549
Jens Axboe3529d8c2019-12-19 18:24:38 -07002550 ret = io_prep_rw(req, sqe, force_nonblock);
2551 if (ret)
2552 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002553
Jens Axboe3529d8c2019-12-19 18:24:38 -07002554 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2555 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002556
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002557 /* either don't need iovec imported or already have it */
2558 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002559 return 0;
2560
2561 io = req->io;
2562 io->rw.iov = io->rw.fast_iov;
2563 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002564 ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002565 req->io = io;
2566 if (ret < 0)
2567 return ret;
2568
2569 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2570 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002571}
2572
Pavel Begunkov014db002020-03-03 21:33:12 +03002573static int io_read(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002574{
2575 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002576 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002577 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002578 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002579 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002580
Jens Axboebcda7ba2020-02-23 16:42:51 -07002581 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002582 if (ret < 0)
2583 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002584
Jens Axboefd6c2e42019-12-18 12:19:41 -07002585 /* Ensure we clear previously set non-block flag */
2586 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002587 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002588
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002589 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002590 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002591 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002592 req->result = io_size;
2593
2594 /*
2595 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2596 * we know to async punt it even if it was opened O_NONBLOCK
2597 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002598 if (force_nonblock && !io_file_supports_async(req->file, READ))
Jens Axboef67676d2019-12-02 11:03:47 -07002599 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002600
Jens Axboe31b51512019-01-18 22:56:34 -07002601 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002602 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002603 if (!ret) {
2604 ssize_t ret2;
2605
Jens Axboe9adbd452019-12-20 08:45:55 -07002606 if (req->file->f_op->read_iter)
2607 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002608 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002609 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002610
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002611 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002612 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002613 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002614 } else {
2615copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002616 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002617 inline_vecs, &iter);
2618 if (ret)
2619 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002620 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002621 if (!(req->flags & REQ_F_NOWAIT) &&
2622 !file_can_poll(req->file))
Jens Axboe29de5f62020-02-20 09:56:08 -07002623 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002624 return -EAGAIN;
2625 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002626 }
Jens Axboef67676d2019-12-02 11:03:47 -07002627out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002628 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002629 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002630 return ret;
2631}
2632
Jens Axboe3529d8c2019-12-19 18:24:38 -07002633static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2634 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002635{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002636 struct io_async_ctx *io;
2637 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002638 ssize_t ret;
2639
Jens Axboe3529d8c2019-12-19 18:24:38 -07002640 ret = io_prep_rw(req, sqe, force_nonblock);
2641 if (ret)
2642 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002643
Jens Axboe3529d8c2019-12-19 18:24:38 -07002644 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2645 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002646
Jens Axboe4ed734b2020-03-20 11:23:41 -06002647 req->fsize = rlimit(RLIMIT_FSIZE);
2648
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002649 /* either don't need iovec imported or already have it */
2650 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002651 return 0;
2652
2653 io = req->io;
2654 io->rw.iov = io->rw.fast_iov;
2655 req->io = NULL;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002656 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002657 req->io = io;
2658 if (ret < 0)
2659 return ret;
2660
2661 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2662 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002663}
2664
Pavel Begunkov014db002020-03-03 21:33:12 +03002665static int io_write(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002666{
2667 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002668 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002669 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002670 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002671 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002672
Jens Axboebcda7ba2020-02-23 16:42:51 -07002673 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
Jens Axboe06b76d42019-12-19 14:44:26 -07002674 if (ret < 0)
2675 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002676
Jens Axboefd6c2e42019-12-18 12:19:41 -07002677 /* Ensure we clear previously set non-block flag */
2678 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002679 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002680
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002681 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002682 io_size = ret;
Pavel Begunkovdea3b492020-04-12 02:05:04 +03002683 if (req->flags & REQ_F_LINK_HEAD)
Jens Axboef67676d2019-12-02 11:03:47 -07002684 req->result = io_size;
2685
2686 /*
2687 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2688 * we know to async punt it even if it was opened O_NONBLOCK
2689 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002690 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07002691 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002692
Jens Axboe10d59342019-12-09 20:16:22 -07002693 /* file path doesn't support NOWAIT for non-direct_IO */
2694 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2695 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002696 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002697
Jens Axboe31b51512019-01-18 22:56:34 -07002698 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002699 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002700 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002701 ssize_t ret2;
2702
Jens Axboe2b188cc2019-01-07 10:46:33 -07002703 /*
2704 * Open-code file_start_write here to grab freeze protection,
2705 * which will be released by another thread in
2706 * io_complete_rw(). Fool lockdep by telling it the lock got
2707 * released so that it doesn't complain about the held lock when
2708 * we return to userspace.
2709 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002710 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002711 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002712 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002713 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002714 SB_FREEZE_WRITE);
2715 }
2716 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002717
Jens Axboe4ed734b2020-03-20 11:23:41 -06002718 if (!force_nonblock)
2719 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
2720
Jens Axboe9adbd452019-12-20 08:45:55 -07002721 if (req->file->f_op->write_iter)
2722 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002723 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002724 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002725
2726 if (!force_nonblock)
2727 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
2728
Jens Axboefaac9962020-02-07 15:45:22 -07002729 /*
Chucheng Luobff60352020-03-25 11:31:38 +08002730 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
Jens Axboefaac9962020-02-07 15:45:22 -07002731 * retry them without IOCB_NOWAIT.
2732 */
2733 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2734 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002735 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkov014db002020-03-03 21:33:12 +03002736 kiocb_done(kiocb, ret2);
Jens Axboef67676d2019-12-02 11:03:47 -07002737 } else {
2738copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002739 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002740 inline_vecs, &iter);
2741 if (ret)
2742 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002743 /* any defer here is final, must blocking retry */
Jens Axboe490e8962020-04-28 13:16:53 -06002744 if (!file_can_poll(req->file))
2745 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002746 return -EAGAIN;
2747 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002748 }
Jens Axboe31b51512019-01-18 22:56:34 -07002749out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002750 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002751 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002752 return ret;
2753}
2754
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03002755static int __io_splice_prep(struct io_kiocb *req,
2756 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002757{
2758 struct io_splice* sp = &req->splice;
2759 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2760 int ret;
2761
2762 if (req->flags & REQ_F_NEED_CLEANUP)
2763 return 0;
2764
2765 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002766 sp->len = READ_ONCE(sqe->len);
2767 sp->flags = READ_ONCE(sqe->splice_flags);
2768
2769 if (unlikely(sp->flags & ~valid_flags))
2770 return -EINVAL;
2771
2772 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2773 (sp->flags & SPLICE_F_FD_IN_FIXED));
2774 if (ret)
2775 return ret;
2776 req->flags |= REQ_F_NEED_CLEANUP;
2777
2778 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2779 req->work.flags |= IO_WQ_WORK_UNBOUND;
2780
2781 return 0;
2782}
2783
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03002784static int io_tee_prep(struct io_kiocb *req,
2785 const struct io_uring_sqe *sqe)
2786{
2787 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
2788 return -EINVAL;
2789 return __io_splice_prep(req, sqe);
2790}
2791
2792static int io_tee(struct io_kiocb *req, bool force_nonblock)
2793{
2794 struct io_splice *sp = &req->splice;
2795 struct file *in = sp->file_in;
2796 struct file *out = sp->file_out;
2797 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2798 long ret = 0;
2799
2800 if (force_nonblock)
2801 return -EAGAIN;
2802 if (sp->len)
2803 ret = do_tee(in, out, sp->len, flags);
2804
2805 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2806 req->flags &= ~REQ_F_NEED_CLEANUP;
2807
2808 io_cqring_add_event(req, ret);
2809 if (ret != sp->len)
2810 req_set_fail_links(req);
2811 io_put_req(req);
2812 return 0;
2813}
2814
2815static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2816{
2817 struct io_splice* sp = &req->splice;
2818
2819 sp->off_in = READ_ONCE(sqe->splice_off_in);
2820 sp->off_out = READ_ONCE(sqe->off);
2821 return __io_splice_prep(req, sqe);
2822}
2823
Pavel Begunkov014db002020-03-03 21:33:12 +03002824static int io_splice(struct io_kiocb *req, bool force_nonblock)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002825{
2826 struct io_splice *sp = &req->splice;
2827 struct file *in = sp->file_in;
2828 struct file *out = sp->file_out;
2829 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2830 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03002831 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002832
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03002833 if (force_nonblock)
2834 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002835
2836 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2837 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03002838
Jens Axboe948a7742020-05-17 14:21:38 -06002839 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03002840 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002841
2842 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2843 req->flags &= ~REQ_F_NEED_CLEANUP;
2844
2845 io_cqring_add_event(req, ret);
2846 if (ret != sp->len)
2847 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03002848 io_put_req(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002849 return 0;
2850}
2851
Jens Axboe2b188cc2019-01-07 10:46:33 -07002852/*
2853 * IORING_OP_NOP just posts a completion event, nothing else.
2854 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002855static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002856{
2857 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002858
Jens Axboedef596e2019-01-09 08:59:42 -07002859 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2860 return -EINVAL;
2861
Jens Axboe78e19bb2019-11-06 15:21:34 -07002862 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002863 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002864 return 0;
2865}
2866
Jens Axboe3529d8c2019-12-19 18:24:38 -07002867static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002868{
Jens Axboe6b063142019-01-10 22:13:58 -07002869 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002870
Jens Axboe09bb8392019-03-13 12:39:28 -06002871 if (!req->file)
2872 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002873
Jens Axboe6b063142019-01-10 22:13:58 -07002874 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002875 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002876 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002877 return -EINVAL;
2878
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002879 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2880 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2881 return -EINVAL;
2882
2883 req->sync.off = READ_ONCE(sqe->off);
2884 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002885 return 0;
2886}
2887
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002888static bool io_req_cancelled(struct io_kiocb *req)
2889{
2890 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2891 req_set_fail_links(req);
2892 io_cqring_add_event(req, -ECANCELED);
2893 io_put_req(req);
2894 return true;
2895 }
2896
2897 return false;
2898}
2899
Pavel Begunkov014db002020-03-03 21:33:12 +03002900static void __io_fsync(struct io_kiocb *req)
Jens Axboe78912932020-01-14 22:09:06 -07002901{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002902 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002903 int ret;
2904
Jens Axboe9adbd452019-12-20 08:45:55 -07002905 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002906 end > 0 ? end : LLONG_MAX,
2907 req->sync.flags & IORING_FSYNC_DATASYNC);
2908 if (ret < 0)
2909 req_set_fail_links(req);
2910 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002911 io_put_req(req);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002912}
2913
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002914static void io_fsync_finish(struct io_wq_work **workptr)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002915{
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002916 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002917
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002918 if (io_req_cancelled(req))
2919 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002920 __io_fsync(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002921 io_steal_work(req, workptr);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002922}
2923
Pavel Begunkov014db002020-03-03 21:33:12 +03002924static int io_fsync(struct io_kiocb *req, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002925{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002926 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002927 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002928 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002929 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002930 }
Pavel Begunkov014db002020-03-03 21:33:12 +03002931 __io_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002932 return 0;
2933}
2934
Pavel Begunkov014db002020-03-03 21:33:12 +03002935static void __io_fallocate(struct io_kiocb *req)
Jens Axboed63d1b52019-12-10 10:38:56 -07002936{
Jens Axboed63d1b52019-12-10 10:38:56 -07002937 int ret;
2938
Jens Axboe4ed734b2020-03-20 11:23:41 -06002939 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
Jens Axboed63d1b52019-12-10 10:38:56 -07002940 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2941 req->sync.len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002942 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Jens Axboed63d1b52019-12-10 10:38:56 -07002943 if (ret < 0)
2944 req_set_fail_links(req);
2945 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03002946 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002947}
2948
Jens Axboe2b188cc2019-01-07 10:46:33 -07002949static void io_fallocate_finish(struct io_wq_work **workptr)
2950{
2951 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboed63d1b52019-12-10 10:38:56 -07002952
2953 if (io_req_cancelled(req))
2954 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03002955 __io_fallocate(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03002956 io_steal_work(req, workptr);
Jens Axboed63d1b52019-12-10 10:38:56 -07002957}
2958
2959static 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;
2964
2965 req->sync.off = READ_ONCE(sqe->off);
2966 req->sync.len = READ_ONCE(sqe->addr);
2967 req->sync.mode = READ_ONCE(sqe->len);
Jens Axboe4ed734b2020-03-20 11:23:41 -06002968 req->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboed63d1b52019-12-10 10:38:56 -07002969 return 0;
2970}
2971
Pavel Begunkov014db002020-03-03 21:33:12 +03002972static int io_fallocate(struct io_kiocb *req, bool force_nonblock)
Jens Axboed63d1b52019-12-10 10:38:56 -07002973{
Jens Axboed63d1b52019-12-10 10:38:56 -07002974 /* fallocate always requiring blocking context */
2975 if (force_nonblock) {
Jens Axboed63d1b52019-12-10 10:38:56 -07002976 req->work.func = io_fallocate_finish;
2977 return -EAGAIN;
2978 }
2979
Pavel Begunkov014db002020-03-03 21:33:12 +03002980 __io_fallocate(req);
Jens Axboed63d1b52019-12-10 10:38:56 -07002981 return 0;
2982}
2983
Jens Axboe15b71ab2019-12-11 11:20:36 -07002984static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2985{
Jens Axboef8748882020-01-08 17:47:02 -07002986 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002987 int ret;
2988
2989 if (sqe->ioprio || sqe->buf_index)
2990 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03002991 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002992 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002993 if (req->flags & REQ_F_NEED_CLEANUP)
2994 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002995
2996 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002997 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002998 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002999 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003000 if (force_o_largefile())
3001 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003002
Jens Axboef8748882020-01-08 17:47:02 -07003003 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003004 if (IS_ERR(req->open.filename)) {
3005 ret = PTR_ERR(req->open.filename);
3006 req->open.filename = NULL;
3007 return ret;
3008 }
3009
Jens Axboe4022e7a2020-03-19 19:23:18 -06003010 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003011 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003012 return 0;
3013}
3014
Jens Axboecebdb982020-01-08 17:59:24 -07003015static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3016{
3017 struct open_how __user *how;
3018 const char __user *fname;
3019 size_t len;
3020 int ret;
3021
3022 if (sqe->ioprio || sqe->buf_index)
3023 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003024 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003025 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03003026 if (req->flags & REQ_F_NEED_CLEANUP)
3027 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07003028
3029 req->open.dfd = READ_ONCE(sqe->fd);
3030 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3031 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3032 len = READ_ONCE(sqe->len);
3033
3034 if (len < OPEN_HOW_SIZE_VER0)
3035 return -EINVAL;
3036
3037 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3038 len);
3039 if (ret)
3040 return ret;
3041
3042 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
3043 req->open.how.flags |= O_LARGEFILE;
3044
3045 req->open.filename = getname(fname);
3046 if (IS_ERR(req->open.filename)) {
3047 ret = PTR_ERR(req->open.filename);
3048 req->open.filename = NULL;
3049 return ret;
3050 }
3051
Jens Axboe4022e7a2020-03-19 19:23:18 -06003052 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003053 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07003054 return 0;
3055}
3056
Pavel Begunkov014db002020-03-03 21:33:12 +03003057static int io_openat2(struct io_kiocb *req, bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003058{
3059 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003060 struct file *file;
3061 int ret;
3062
Jens Axboef86cd202020-01-29 13:46:44 -07003063 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003064 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003065
Jens Axboecebdb982020-01-08 17:59:24 -07003066 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003067 if (ret)
3068 goto err;
3069
Jens Axboe4022e7a2020-03-19 19:23:18 -06003070 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003071 if (ret < 0)
3072 goto err;
3073
3074 file = do_filp_open(req->open.dfd, req->open.filename, &op);
3075 if (IS_ERR(file)) {
3076 put_unused_fd(ret);
3077 ret = PTR_ERR(file);
3078 } else {
3079 fsnotify_open(file);
3080 fd_install(ret, file);
3081 }
3082err:
3083 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003084 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003085 if (ret < 0)
3086 req_set_fail_links(req);
3087 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003088 io_put_req(req);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003089 return 0;
3090}
3091
Pavel Begunkov014db002020-03-03 21:33:12 +03003092static int io_openat(struct io_kiocb *req, bool force_nonblock)
Jens Axboecebdb982020-01-08 17:59:24 -07003093{
3094 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
Pavel Begunkov014db002020-03-03 21:33:12 +03003095 return io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07003096}
3097
Jens Axboe067524e2020-03-02 16:32:28 -07003098static int io_remove_buffers_prep(struct io_kiocb *req,
3099 const struct io_uring_sqe *sqe)
3100{
3101 struct io_provide_buf *p = &req->pbuf;
3102 u64 tmp;
3103
3104 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3105 return -EINVAL;
3106
3107 tmp = READ_ONCE(sqe->fd);
3108 if (!tmp || tmp > USHRT_MAX)
3109 return -EINVAL;
3110
3111 memset(p, 0, sizeof(*p));
3112 p->nbufs = tmp;
3113 p->bgid = READ_ONCE(sqe->buf_group);
3114 return 0;
3115}
3116
3117static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3118 int bgid, unsigned nbufs)
3119{
3120 unsigned i = 0;
3121
3122 /* shouldn't happen */
3123 if (!nbufs)
3124 return 0;
3125
3126 /* the head kbuf is the list itself */
3127 while (!list_empty(&buf->list)) {
3128 struct io_buffer *nxt;
3129
3130 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3131 list_del(&nxt->list);
3132 kfree(nxt);
3133 if (++i == nbufs)
3134 return i;
3135 }
3136 i++;
3137 kfree(buf);
3138 idr_remove(&ctx->io_buffer_idr, bgid);
3139
3140 return i;
3141}
3142
3143static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3144{
3145 struct io_provide_buf *p = &req->pbuf;
3146 struct io_ring_ctx *ctx = req->ctx;
3147 struct io_buffer *head;
3148 int ret = 0;
3149
3150 io_ring_submit_lock(ctx, !force_nonblock);
3151
3152 lockdep_assert_held(&ctx->uring_lock);
3153
3154 ret = -ENOENT;
3155 head = idr_find(&ctx->io_buffer_idr, p->bgid);
3156 if (head)
3157 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
3158
3159 io_ring_submit_lock(ctx, !force_nonblock);
3160 if (ret < 0)
3161 req_set_fail_links(req);
3162 io_cqring_add_event(req, ret);
3163 io_put_req(req);
3164 return 0;
3165}
3166
Jens Axboeddf0322d2020-02-23 16:41:33 -07003167static int io_provide_buffers_prep(struct io_kiocb *req,
3168 const struct io_uring_sqe *sqe)
3169{
3170 struct io_provide_buf *p = &req->pbuf;
3171 u64 tmp;
3172
3173 if (sqe->ioprio || sqe->rw_flags)
3174 return -EINVAL;
3175
3176 tmp = READ_ONCE(sqe->fd);
3177 if (!tmp || tmp > USHRT_MAX)
3178 return -E2BIG;
3179 p->nbufs = tmp;
3180 p->addr = READ_ONCE(sqe->addr);
3181 p->len = READ_ONCE(sqe->len);
3182
3183 if (!access_ok(u64_to_user_ptr(p->addr), p->len))
3184 return -EFAULT;
3185
3186 p->bgid = READ_ONCE(sqe->buf_group);
3187 tmp = READ_ONCE(sqe->off);
3188 if (tmp > USHRT_MAX)
3189 return -E2BIG;
3190 p->bid = tmp;
3191 return 0;
3192}
3193
3194static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3195{
3196 struct io_buffer *buf;
3197 u64 addr = pbuf->addr;
3198 int i, bid = pbuf->bid;
3199
3200 for (i = 0; i < pbuf->nbufs; i++) {
3201 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3202 if (!buf)
3203 break;
3204
3205 buf->addr = addr;
3206 buf->len = pbuf->len;
3207 buf->bid = bid;
3208 addr += pbuf->len;
3209 bid++;
3210 if (!*head) {
3211 INIT_LIST_HEAD(&buf->list);
3212 *head = buf;
3213 } else {
3214 list_add_tail(&buf->list, &(*head)->list);
3215 }
3216 }
3217
3218 return i ? i : -ENOMEM;
3219}
3220
Jens Axboeddf0322d2020-02-23 16:41:33 -07003221static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3222{
3223 struct io_provide_buf *p = &req->pbuf;
3224 struct io_ring_ctx *ctx = req->ctx;
3225 struct io_buffer *head, *list;
3226 int ret = 0;
3227
3228 io_ring_submit_lock(ctx, !force_nonblock);
3229
3230 lockdep_assert_held(&ctx->uring_lock);
3231
3232 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
3233
3234 ret = io_add_buffers(p, &head);
3235 if (ret < 0)
3236 goto out;
3237
3238 if (!list) {
3239 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
3240 GFP_KERNEL);
3241 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07003242 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003243 goto out;
3244 }
3245 }
3246out:
3247 io_ring_submit_unlock(ctx, !force_nonblock);
3248 if (ret < 0)
3249 req_set_fail_links(req);
3250 io_cqring_add_event(req, ret);
3251 io_put_req(req);
3252 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003253}
3254
Jens Axboe3e4827b2020-01-08 15:18:09 -07003255static int io_epoll_ctl_prep(struct io_kiocb *req,
3256 const struct io_uring_sqe *sqe)
3257{
3258#if defined(CONFIG_EPOLL)
3259 if (sqe->ioprio || sqe->buf_index)
3260 return -EINVAL;
3261
3262 req->epoll.epfd = READ_ONCE(sqe->fd);
3263 req->epoll.op = READ_ONCE(sqe->len);
3264 req->epoll.fd = READ_ONCE(sqe->off);
3265
3266 if (ep_op_has_event(req->epoll.op)) {
3267 struct epoll_event __user *ev;
3268
3269 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
3270 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
3271 return -EFAULT;
3272 }
3273
3274 return 0;
3275#else
3276 return -EOPNOTSUPP;
3277#endif
3278}
3279
Pavel Begunkov014db002020-03-03 21:33:12 +03003280static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
Jens Axboe3e4827b2020-01-08 15:18:09 -07003281{
3282#if defined(CONFIG_EPOLL)
3283 struct io_epoll *ie = &req->epoll;
3284 int ret;
3285
3286 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
3287 if (force_nonblock && ret == -EAGAIN)
3288 return -EAGAIN;
3289
3290 if (ret < 0)
3291 req_set_fail_links(req);
3292 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003293 io_put_req(req);
Jens Axboe3e4827b2020-01-08 15:18:09 -07003294 return 0;
3295#else
3296 return -EOPNOTSUPP;
3297#endif
3298}
3299
Jens Axboec1ca7572019-12-25 22:18:28 -07003300static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3301{
3302#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3303 if (sqe->ioprio || sqe->buf_index || sqe->off)
3304 return -EINVAL;
3305
3306 req->madvise.addr = READ_ONCE(sqe->addr);
3307 req->madvise.len = READ_ONCE(sqe->len);
3308 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
3309 return 0;
3310#else
3311 return -EOPNOTSUPP;
3312#endif
3313}
3314
Pavel Begunkov014db002020-03-03 21:33:12 +03003315static int io_madvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboec1ca7572019-12-25 22:18:28 -07003316{
3317#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
3318 struct io_madvise *ma = &req->madvise;
3319 int ret;
3320
3321 if (force_nonblock)
3322 return -EAGAIN;
3323
3324 ret = do_madvise(ma->addr, ma->len, ma->advice);
3325 if (ret < 0)
3326 req_set_fail_links(req);
3327 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003328 io_put_req(req);
Jens Axboec1ca7572019-12-25 22:18:28 -07003329 return 0;
3330#else
3331 return -EOPNOTSUPP;
3332#endif
3333}
3334
Jens Axboe4840e412019-12-25 22:03:45 -07003335static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3336{
3337 if (sqe->ioprio || sqe->buf_index || sqe->addr)
3338 return -EINVAL;
3339
3340 req->fadvise.offset = READ_ONCE(sqe->off);
3341 req->fadvise.len = READ_ONCE(sqe->len);
3342 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
3343 return 0;
3344}
3345
Pavel Begunkov014db002020-03-03 21:33:12 +03003346static int io_fadvise(struct io_kiocb *req, bool force_nonblock)
Jens Axboe4840e412019-12-25 22:03:45 -07003347{
3348 struct io_fadvise *fa = &req->fadvise;
3349 int ret;
3350
Jens Axboe3e694262020-02-01 09:22:49 -07003351 if (force_nonblock) {
3352 switch (fa->advice) {
3353 case POSIX_FADV_NORMAL:
3354 case POSIX_FADV_RANDOM:
3355 case POSIX_FADV_SEQUENTIAL:
3356 break;
3357 default:
3358 return -EAGAIN;
3359 }
3360 }
Jens Axboe4840e412019-12-25 22:03:45 -07003361
3362 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
3363 if (ret < 0)
3364 req_set_fail_links(req);
3365 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003366 io_put_req(req);
Jens Axboe4840e412019-12-25 22:03:45 -07003367 return 0;
3368}
3369
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003370static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3371{
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003372 if (sqe->ioprio || sqe->buf_index)
3373 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003374 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003375 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003376
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003377 req->statx.dfd = READ_ONCE(sqe->fd);
3378 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003379 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003380 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3381 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003382
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003383 return 0;
3384}
3385
Pavel Begunkov014db002020-03-03 21:33:12 +03003386static int io_statx(struct io_kiocb *req, bool force_nonblock)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003387{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07003388 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003389 int ret;
3390
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003391 if (force_nonblock) {
3392 /* only need file table for an actual valid fd */
3393 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
3394 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003395 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06003396 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003397
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07003398 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
3399 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003400
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003401 if (ret < 0)
3402 req_set_fail_links(req);
3403 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003404 io_put_req(req);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003405 return 0;
3406}
3407
Jens Axboeb5dba592019-12-11 14:02:38 -07003408static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3409{
3410 /*
3411 * If we queue this for async, it must not be cancellable. That would
3412 * leave the 'file' in an undeterminate state.
3413 */
3414 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
3415
3416 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
3417 sqe->rw_flags || sqe->buf_index)
3418 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03003419 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07003420 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07003421
3422 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07003423 return 0;
3424}
3425
Pavel Begunkova93b3332020-02-08 14:04:34 +03003426/* only called when __close_fd_get_file() is done */
Pavel Begunkov014db002020-03-03 21:33:12 +03003427static void __io_close_finish(struct io_kiocb *req)
Pavel Begunkova93b3332020-02-08 14:04:34 +03003428{
3429 int ret;
3430
3431 ret = filp_close(req->close.put_file, req->work.files);
3432 if (ret < 0)
3433 req_set_fail_links(req);
3434 io_cqring_add_event(req, ret);
3435 fput(req->close.put_file);
Pavel Begunkov014db002020-03-03 21:33:12 +03003436 io_put_req(req);
Pavel Begunkova93b3332020-02-08 14:04:34 +03003437}
3438
Jens Axboeb5dba592019-12-11 14:02:38 -07003439static void io_close_finish(struct io_wq_work **workptr)
3440{
3441 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboeb5dba592019-12-11 14:02:38 -07003442
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003443 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkov014db002020-03-03 21:33:12 +03003444 __io_close_finish(req);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003445 io_steal_work(req, workptr);
Jens Axboeb5dba592019-12-11 14:02:38 -07003446}
3447
Pavel Begunkov014db002020-03-03 21:33:12 +03003448static int io_close(struct io_kiocb *req, bool force_nonblock)
Jens Axboeb5dba592019-12-11 14:02:38 -07003449{
3450 int ret;
3451
3452 req->close.put_file = NULL;
3453 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3454 if (ret < 0)
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003455 return (ret == -ENOENT) ? -EBADF : ret;
Jens Axboeb5dba592019-12-11 14:02:38 -07003456
3457 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkova2100672020-03-02 23:45:16 +03003458 if (req->close.put_file->f_op->flush && force_nonblock) {
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003459 /* avoid grabbing files - we don't need the files */
3460 req->flags |= REQ_F_NO_FILE_TABLE | REQ_F_MUST_PUNT;
Pavel Begunkova2100672020-03-02 23:45:16 +03003461 req->work.func = io_close_finish;
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03003462 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03003463 }
Jens Axboeb5dba592019-12-11 14:02:38 -07003464
3465 /*
3466 * No ->flush(), safely close from here and just punt the
3467 * fput() to async context.
3468 */
Pavel Begunkov014db002020-03-03 21:33:12 +03003469 __io_close_finish(req);
Jens Axboe1a417f42020-01-31 17:16:48 -07003470 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003471}
3472
Jens Axboe3529d8c2019-12-19 18:24:38 -07003473static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003474{
3475 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003476
3477 if (!req->file)
3478 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003479
3480 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3481 return -EINVAL;
3482 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3483 return -EINVAL;
3484
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003485 req->sync.off = READ_ONCE(sqe->off);
3486 req->sync.len = READ_ONCE(sqe->len);
3487 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003488 return 0;
3489}
3490
Pavel Begunkov014db002020-03-03 21:33:12 +03003491static void __io_sync_file_range(struct io_kiocb *req)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003492{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003493 int ret;
3494
Jens Axboe9adbd452019-12-20 08:45:55 -07003495 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003496 req->sync.flags);
3497 if (ret < 0)
3498 req_set_fail_links(req);
3499 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003500 io_put_req(req);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003501}
3502
3503
3504static void io_sync_file_range_finish(struct io_wq_work **workptr)
3505{
3506 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003507
3508 if (io_req_cancelled(req))
3509 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003510 __io_sync_file_range(req);
Pavel Begunkov7759a0b2020-05-01 17:09:36 +03003511 io_steal_work(req, workptr);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003512}
3513
Pavel Begunkov014db002020-03-03 21:33:12 +03003514static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003515{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003516 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003517 if (force_nonblock) {
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003518 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003519 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003520 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003521
Pavel Begunkov014db002020-03-03 21:33:12 +03003522 __io_sync_file_range(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003523 return 0;
3524}
3525
YueHaibing469956e2020-03-04 15:53:52 +08003526#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003527static int io_setup_async_msg(struct io_kiocb *req,
3528 struct io_async_msghdr *kmsg)
3529{
3530 if (req->io)
3531 return -EAGAIN;
3532 if (io_alloc_async_ctx(req)) {
3533 if (kmsg->iov != kmsg->fast_iov)
3534 kfree(kmsg->iov);
3535 return -ENOMEM;
3536 }
3537 req->flags |= REQ_F_NEED_CLEANUP;
3538 memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
3539 return -EAGAIN;
3540}
3541
Jens Axboe3529d8c2019-12-19 18:24:38 -07003542static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003543{
Jens Axboee47293f2019-12-20 08:58:21 -07003544 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003545 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003546 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003547
Jens Axboee47293f2019-12-20 08:58:21 -07003548 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3549 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003550 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003551
Jens Axboed8768362020-02-27 14:17:49 -07003552#ifdef CONFIG_COMPAT
3553 if (req->ctx->compat)
3554 sr->msg_flags |= MSG_CMSG_COMPAT;
3555#endif
3556
Jens Axboefddafac2020-01-04 20:19:44 -07003557 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003558 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003559 /* iovec is already imported */
3560 if (req->flags & REQ_F_NEED_CLEANUP)
3561 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003562
Jens Axboed9688562019-12-09 19:35:20 -07003563 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003564 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003565 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003566 if (!ret)
3567 req->flags |= REQ_F_NEED_CLEANUP;
3568 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003569}
3570
Pavel Begunkov014db002020-03-03 21:33:12 +03003571static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003572{
Jens Axboe0b416c32019-12-15 10:57:46 -07003573 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003574 struct socket *sock;
3575 int ret;
3576
3577 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3578 return -EINVAL;
3579
3580 sock = sock_from_file(req->file, &ret);
3581 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003582 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003583 unsigned flags;
3584
Jens Axboe03b12302019-12-02 18:50:25 -07003585 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003586 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003587 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003588 /* if iov is set, it's allocated already */
3589 if (!kmsg->iov)
3590 kmsg->iov = kmsg->fast_iov;
3591 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003592 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003593 struct io_sr_msg *sr = &req->sr_msg;
3594
Jens Axboe0b416c32019-12-15 10:57:46 -07003595 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003596 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003597
3598 io.msg.iov = io.msg.fast_iov;
3599 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3600 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003601 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003602 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003603 }
3604
Jens Axboee47293f2019-12-20 08:58:21 -07003605 flags = req->sr_msg.msg_flags;
3606 if (flags & MSG_DONTWAIT)
3607 req->flags |= REQ_F_NOWAIT;
3608 else if (force_nonblock)
3609 flags |= MSG_DONTWAIT;
3610
Jens Axboe0b416c32019-12-15 10:57:46 -07003611 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003612 if (force_nonblock && ret == -EAGAIN)
3613 return io_setup_async_msg(req, kmsg);
Jens Axboe03b12302019-12-02 18:50:25 -07003614 if (ret == -ERESTARTSYS)
3615 ret = -EINTR;
3616 }
3617
Pavel Begunkov1e950812020-02-06 19:51:16 +03003618 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003619 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003620 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003621 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003622 if (ret < 0)
3623 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003624 io_put_req(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003625 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003626}
3627
Pavel Begunkov014db002020-03-03 21:33:12 +03003628static int io_send(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003629{
Jens Axboefddafac2020-01-04 20:19:44 -07003630 struct socket *sock;
3631 int ret;
3632
3633 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3634 return -EINVAL;
3635
3636 sock = sock_from_file(req->file, &ret);
3637 if (sock) {
3638 struct io_sr_msg *sr = &req->sr_msg;
3639 struct msghdr msg;
3640 struct iovec iov;
3641 unsigned flags;
3642
3643 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3644 &msg.msg_iter);
3645 if (ret)
3646 return ret;
3647
3648 msg.msg_name = NULL;
3649 msg.msg_control = NULL;
3650 msg.msg_controllen = 0;
3651 msg.msg_namelen = 0;
3652
3653 flags = req->sr_msg.msg_flags;
3654 if (flags & MSG_DONTWAIT)
3655 req->flags |= REQ_F_NOWAIT;
3656 else if (force_nonblock)
3657 flags |= MSG_DONTWAIT;
3658
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003659 msg.msg_flags = flags;
3660 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003661 if (force_nonblock && ret == -EAGAIN)
3662 return -EAGAIN;
3663 if (ret == -ERESTARTSYS)
3664 ret = -EINTR;
3665 }
3666
3667 io_cqring_add_event(req, ret);
3668 if (ret < 0)
3669 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003670 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003671 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003672}
3673
Jens Axboe52de1fe2020-02-27 10:15:42 -07003674static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3675{
3676 struct io_sr_msg *sr = &req->sr_msg;
3677 struct iovec __user *uiov;
3678 size_t iov_len;
3679 int ret;
3680
3681 ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3682 &uiov, &iov_len);
3683 if (ret)
3684 return ret;
3685
3686 if (req->flags & REQ_F_BUFFER_SELECT) {
3687 if (iov_len > 1)
3688 return -EINVAL;
3689 if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
3690 return -EFAULT;
3691 sr->len = io->msg.iov[0].iov_len;
3692 iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
3693 sr->len);
3694 io->msg.iov = NULL;
3695 } else {
3696 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3697 &io->msg.iov, &io->msg.msg.msg_iter);
3698 if (ret > 0)
3699 ret = 0;
3700 }
3701
3702 return ret;
3703}
3704
3705#ifdef CONFIG_COMPAT
3706static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3707 struct io_async_ctx *io)
3708{
3709 struct compat_msghdr __user *msg_compat;
3710 struct io_sr_msg *sr = &req->sr_msg;
3711 struct compat_iovec __user *uiov;
3712 compat_uptr_t ptr;
3713 compat_size_t len;
3714 int ret;
3715
3716 msg_compat = (struct compat_msghdr __user *) sr->msg;
3717 ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
3718 &ptr, &len);
3719 if (ret)
3720 return ret;
3721
3722 uiov = compat_ptr(ptr);
3723 if (req->flags & REQ_F_BUFFER_SELECT) {
3724 compat_ssize_t clen;
3725
3726 if (len > 1)
3727 return -EINVAL;
3728 if (!access_ok(uiov, sizeof(*uiov)))
3729 return -EFAULT;
3730 if (__get_user(clen, &uiov->iov_len))
3731 return -EFAULT;
3732 if (clen < 0)
3733 return -EINVAL;
3734 sr->len = io->msg.iov[0].iov_len;
3735 io->msg.iov = NULL;
3736 } else {
3737 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3738 &io->msg.iov,
3739 &io->msg.msg.msg_iter);
3740 if (ret < 0)
3741 return ret;
3742 }
3743
3744 return 0;
3745}
Jens Axboe03b12302019-12-02 18:50:25 -07003746#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07003747
3748static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
3749{
3750 io->msg.iov = io->msg.fast_iov;
3751
3752#ifdef CONFIG_COMPAT
3753 if (req->ctx->compat)
3754 return __io_compat_recvmsg_copy_hdr(req, io);
3755#endif
3756
3757 return __io_recvmsg_copy_hdr(req, io);
3758}
3759
Jens Axboebcda7ba2020-02-23 16:42:51 -07003760static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3761 int *cflags, bool needs_lock)
3762{
3763 struct io_sr_msg *sr = &req->sr_msg;
3764 struct io_buffer *kbuf;
3765
3766 if (!(req->flags & REQ_F_BUFFER_SELECT))
3767 return NULL;
3768
3769 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
3770 if (IS_ERR(kbuf))
3771 return kbuf;
3772
3773 sr->kbuf = kbuf;
3774 req->flags |= REQ_F_BUFFER_SELECTED;
3775
3776 *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3777 *cflags |= IORING_CQE_F_BUFFER;
3778 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07003779}
3780
Jens Axboe3529d8c2019-12-19 18:24:38 -07003781static int io_recvmsg_prep(struct io_kiocb *req,
3782 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003783{
Jens Axboee47293f2019-12-20 08:58:21 -07003784 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003785 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003786 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003787
Jens Axboe3529d8c2019-12-19 18:24:38 -07003788 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3789 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003790 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003791 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003792
Jens Axboed8768362020-02-27 14:17:49 -07003793#ifdef CONFIG_COMPAT
3794 if (req->ctx->compat)
3795 sr->msg_flags |= MSG_CMSG_COMPAT;
3796#endif
3797
Jens Axboefddafac2020-01-04 20:19:44 -07003798 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003799 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003800 /* iovec is already imported */
3801 if (req->flags & REQ_F_NEED_CLEANUP)
3802 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003803
Jens Axboe52de1fe2020-02-27 10:15:42 -07003804 ret = io_recvmsg_copy_hdr(req, io);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003805 if (!ret)
3806 req->flags |= REQ_F_NEED_CLEANUP;
3807 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003808}
3809
Pavel Begunkov014db002020-03-03 21:33:12 +03003810static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003811{
Jens Axboe0b416c32019-12-15 10:57:46 -07003812 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003813 struct socket *sock;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003814 int ret, cflags = 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003815
3816 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3817 return -EINVAL;
3818
3819 sock = sock_from_file(req->file, &ret);
3820 if (sock) {
Jens Axboe52de1fe2020-02-27 10:15:42 -07003821 struct io_buffer *kbuf;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003822 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003823 unsigned flags;
3824
Jens Axboe03b12302019-12-02 18:50:25 -07003825 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003826 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003827 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003828 /* if iov is set, it's allocated already */
3829 if (!kmsg->iov)
3830 kmsg->iov = kmsg->fast_iov;
3831 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003832 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07003833 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003834 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003835
Jens Axboe52de1fe2020-02-27 10:15:42 -07003836 ret = io_recvmsg_copy_hdr(req, &io);
Jens Axboe03b12302019-12-02 18:50:25 -07003837 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003838 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003839 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003840
Jens Axboe52de1fe2020-02-27 10:15:42 -07003841 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3842 if (IS_ERR(kbuf)) {
3843 return PTR_ERR(kbuf);
3844 } else if (kbuf) {
3845 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3846 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3847 1, req->sr_msg.len);
3848 }
3849
Jens Axboee47293f2019-12-20 08:58:21 -07003850 flags = req->sr_msg.msg_flags;
3851 if (flags & MSG_DONTWAIT)
3852 req->flags |= REQ_F_NOWAIT;
3853 else if (force_nonblock)
3854 flags |= MSG_DONTWAIT;
3855
3856 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3857 kmsg->uaddr, flags);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03003858 if (force_nonblock && ret == -EAGAIN)
3859 return io_setup_async_msg(req, kmsg);
Jens Axboe441cdbd2019-12-02 18:49:10 -07003860 if (ret == -ERESTARTSYS)
3861 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003862 }
3863
Pavel Begunkov1e950812020-02-06 19:51:16 +03003864 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003865 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003866 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe52de1fe2020-02-27 10:15:42 -07003867 __io_cqring_add_event(req, ret, cflags);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003868 if (ret < 0)
3869 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003870 io_put_req(req);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003871 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003872}
3873
Pavel Begunkov014db002020-03-03 21:33:12 +03003874static int io_recv(struct io_kiocb *req, bool force_nonblock)
Jens Axboefddafac2020-01-04 20:19:44 -07003875{
Jens Axboebcda7ba2020-02-23 16:42:51 -07003876 struct io_buffer *kbuf = NULL;
Jens Axboefddafac2020-01-04 20:19:44 -07003877 struct socket *sock;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003878 int ret, cflags = 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003879
3880 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3881 return -EINVAL;
3882
3883 sock = sock_from_file(req->file, &ret);
3884 if (sock) {
3885 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003886 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07003887 struct msghdr msg;
3888 struct iovec iov;
3889 unsigned flags;
3890
Jens Axboebcda7ba2020-02-23 16:42:51 -07003891 kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3892 if (IS_ERR(kbuf))
3893 return PTR_ERR(kbuf);
3894 else if (kbuf)
3895 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07003896
Jens Axboebcda7ba2020-02-23 16:42:51 -07003897 ret = import_single_range(READ, buf, sr->len, &iov,
3898 &msg.msg_iter);
3899 if (ret) {
3900 kfree(kbuf);
3901 return ret;
3902 }
3903
3904 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07003905 msg.msg_name = NULL;
3906 msg.msg_control = NULL;
3907 msg.msg_controllen = 0;
3908 msg.msg_namelen = 0;
3909 msg.msg_iocb = NULL;
3910 msg.msg_flags = 0;
3911
3912 flags = req->sr_msg.msg_flags;
3913 if (flags & MSG_DONTWAIT)
3914 req->flags |= REQ_F_NOWAIT;
3915 else if (force_nonblock)
3916 flags |= MSG_DONTWAIT;
3917
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003918 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003919 if (force_nonblock && ret == -EAGAIN)
3920 return -EAGAIN;
3921 if (ret == -ERESTARTSYS)
3922 ret = -EINTR;
3923 }
3924
Jens Axboebcda7ba2020-02-23 16:42:51 -07003925 kfree(kbuf);
3926 req->flags &= ~REQ_F_NEED_CLEANUP;
3927 __io_cqring_add_event(req, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07003928 if (ret < 0)
3929 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03003930 io_put_req(req);
Jens Axboefddafac2020-01-04 20:19:44 -07003931 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07003932}
3933
Jens Axboe3529d8c2019-12-19 18:24:38 -07003934static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003935{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003936 struct io_accept *accept = &req->accept;
3937
Jens Axboe17f2fe32019-10-17 14:42:58 -06003938 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3939 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003940 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003941 return -EINVAL;
3942
Jens Axboed55e5f52019-12-11 16:12:15 -07003943 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3944 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003945 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06003946 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003947 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003948}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003949
Pavel Begunkov014db002020-03-03 21:33:12 +03003950static int __io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003951{
3952 struct io_accept *accept = &req->accept;
3953 unsigned file_flags;
3954 int ret;
3955
3956 file_flags = force_nonblock ? O_NONBLOCK : 0;
3957 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06003958 accept->addr_len, accept->flags,
3959 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003960 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003961 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003962 if (ret == -ERESTARTSYS)
3963 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003964 if (ret < 0)
3965 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003966 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03003967 io_put_req(req);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003968 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003969}
3970
3971static void io_accept_finish(struct io_wq_work **workptr)
3972{
3973 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003974
3975 if (io_req_cancelled(req))
3976 return;
Pavel Begunkov014db002020-03-03 21:33:12 +03003977 __io_accept(req, false);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03003978 io_steal_work(req, workptr);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003979}
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003980
Pavel Begunkov014db002020-03-03 21:33:12 +03003981static int io_accept(struct io_kiocb *req, bool force_nonblock)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003982{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003983 int ret;
3984
Pavel Begunkov014db002020-03-03 21:33:12 +03003985 ret = __io_accept(req, force_nonblock);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003986 if (ret == -EAGAIN && force_nonblock) {
3987 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003988 return -EAGAIN;
3989 }
3990 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003991}
3992
Jens Axboe3529d8c2019-12-19 18:24:38 -07003993static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003994{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003995 struct io_connect *conn = &req->connect;
3996 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003997
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003998 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3999 return -EINVAL;
4000 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4001 return -EINVAL;
4002
Jens Axboe3529d8c2019-12-19 18:24:38 -07004003 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4004 conn->addr_len = READ_ONCE(sqe->addr2);
4005
4006 if (!io)
4007 return 0;
4008
4009 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004010 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004011}
4012
Pavel Begunkov014db002020-03-03 21:33:12 +03004013static int io_connect(struct io_kiocb *req, bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004014{
Jens Axboef499a022019-12-02 16:28:46 -07004015 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004016 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004017 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004018
Jens Axboef499a022019-12-02 16:28:46 -07004019 if (req->io) {
4020 io = req->io;
4021 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004022 ret = move_addr_to_kernel(req->connect.addr,
4023 req->connect.addr_len,
4024 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07004025 if (ret)
4026 goto out;
4027 io = &__io;
4028 }
4029
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004030 file_flags = force_nonblock ? O_NONBLOCK : 0;
4031
4032 ret = __sys_connect_file(req->file, &io->connect.address,
4033 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004034 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004035 if (req->io)
4036 return -EAGAIN;
4037 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004038 ret = -ENOMEM;
4039 goto out;
4040 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004041 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004042 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004043 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004044 if (ret == -ERESTARTSYS)
4045 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004046out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004047 if (ret < 0)
4048 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004049 io_cqring_add_event(req, ret);
Pavel Begunkov014db002020-03-03 21:33:12 +03004050 io_put_req(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004051 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004052}
YueHaibing469956e2020-03-04 15:53:52 +08004053#else /* !CONFIG_NET */
4054static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4055{
Jens Axboef8e85cf2019-11-23 14:24:24 -07004056 return -EOPNOTSUPP;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004057}
4058
YueHaibing469956e2020-03-04 15:53:52 +08004059static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004060{
YueHaibing469956e2020-03-04 15:53:52 +08004061 return -EOPNOTSUPP;
4062}
4063
4064static int io_send(struct io_kiocb *req, bool force_nonblock)
4065{
4066 return -EOPNOTSUPP;
4067}
4068
4069static int io_recvmsg_prep(struct io_kiocb *req,
4070 const struct io_uring_sqe *sqe)
4071{
4072 return -EOPNOTSUPP;
4073}
4074
4075static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4076{
4077 return -EOPNOTSUPP;
4078}
4079
4080static int io_recv(struct io_kiocb *req, bool force_nonblock)
4081{
4082 return -EOPNOTSUPP;
4083}
4084
4085static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4086{
4087 return -EOPNOTSUPP;
4088}
4089
4090static int io_accept(struct io_kiocb *req, bool force_nonblock)
4091{
4092 return -EOPNOTSUPP;
4093}
4094
4095static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4096{
4097 return -EOPNOTSUPP;
4098}
4099
4100static int io_connect(struct io_kiocb *req, bool force_nonblock)
4101{
4102 return -EOPNOTSUPP;
4103}
4104#endif /* CONFIG_NET */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004105
Jens Axboed7718a92020-02-14 22:23:12 -07004106struct io_poll_table {
4107 struct poll_table_struct pt;
4108 struct io_kiocb *req;
4109 int error;
4110};
4111
Jens Axboed7718a92020-02-14 22:23:12 -07004112static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4113 __poll_t mask, task_work_func_t func)
4114{
4115 struct task_struct *tsk;
Jens Axboeaa96bf82020-04-03 11:26:26 -06004116 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004117
4118 /* for instances that support it check for an event match first: */
4119 if (mask && !(mask & poll->events))
4120 return 0;
4121
4122 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4123
4124 list_del_init(&poll->wait.entry);
4125
4126 tsk = req->task;
4127 req->result = mask;
4128 init_task_work(&req->task_work, func);
4129 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004130 * If this fails, then the task is exiting. When a task exits, the
4131 * work gets canceled, so just cancel this request as well instead
4132 * of executing it. We can't safely execute it anyway, as we may not
4133 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004134 */
Jens Axboeaa96bf82020-04-03 11:26:26 -06004135 ret = task_work_add(tsk, &req->task_work, true);
4136 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004137 WRITE_ONCE(poll->canceled, true);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004138 tsk = io_wq_get_task(req->ctx->io_wq);
4139 task_work_add(tsk, &req->task_work, true);
4140 }
Jens Axboed7718a92020-02-14 22:23:12 -07004141 wake_up_process(tsk);
4142 return 1;
4143}
4144
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004145static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4146 __acquires(&req->ctx->completion_lock)
4147{
4148 struct io_ring_ctx *ctx = req->ctx;
4149
4150 if (!req->result && !READ_ONCE(poll->canceled)) {
4151 struct poll_table_struct pt = { ._key = poll->events };
4152
4153 req->result = vfs_poll(req->file, &pt) & poll->events;
4154 }
4155
4156 spin_lock_irq(&ctx->completion_lock);
4157 if (!req->result && !READ_ONCE(poll->canceled)) {
4158 add_wait_queue(poll->head, &poll->wait);
4159 return true;
4160 }
4161
4162 return false;
4163}
4164
Jens Axboe18bceab2020-05-15 11:56:54 -06004165static void io_poll_remove_double(struct io_kiocb *req)
4166{
4167 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4168
4169 lockdep_assert_held(&req->ctx->completion_lock);
4170
4171 if (poll && poll->head) {
4172 struct wait_queue_head *head = poll->head;
4173
4174 spin_lock(&head->lock);
4175 list_del_init(&poll->wait.entry);
4176 if (poll->wait.private)
4177 refcount_dec(&req->refs);
4178 poll->head = NULL;
4179 spin_unlock(&head->lock);
4180 }
4181}
4182
4183static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
4184{
4185 struct io_ring_ctx *ctx = req->ctx;
4186
4187 io_poll_remove_double(req);
4188 req->poll.done = true;
4189 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
4190 io_commit_cqring(ctx);
4191}
4192
4193static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt)
4194{
4195 struct io_ring_ctx *ctx = req->ctx;
4196
4197 if (io_poll_rewait(req, &req->poll)) {
4198 spin_unlock_irq(&ctx->completion_lock);
4199 return;
4200 }
4201
4202 hash_del(&req->hash_node);
4203 io_poll_complete(req, req->result, 0);
4204 req->flags |= REQ_F_COMP_LOCKED;
4205 io_put_req_find_next(req, nxt);
4206 spin_unlock_irq(&ctx->completion_lock);
4207
4208 io_cqring_ev_posted(ctx);
4209}
4210
4211static void io_poll_task_func(struct callback_head *cb)
4212{
4213 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4214 struct io_kiocb *nxt = NULL;
4215
4216 io_poll_task_handler(req, &nxt);
4217 if (nxt) {
4218 struct io_ring_ctx *ctx = nxt->ctx;
4219
4220 mutex_lock(&ctx->uring_lock);
4221 __io_queue_sqe(nxt, NULL);
4222 mutex_unlock(&ctx->uring_lock);
4223 }
4224}
4225
4226static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4227 int sync, void *key)
4228{
4229 struct io_kiocb *req = wait->private;
4230 struct io_poll_iocb *poll = (struct io_poll_iocb *) req->io;
4231 __poll_t mask = key_to_poll(key);
4232
4233 /* for instances that support it check for an event match first: */
4234 if (mask && !(mask & poll->events))
4235 return 0;
4236
4237 if (req->poll.head) {
4238 bool done;
4239
4240 spin_lock(&req->poll.head->lock);
4241 done = list_empty(&req->poll.wait.entry);
4242 if (!done)
4243 list_del_init(&req->poll.wait.entry);
4244 spin_unlock(&req->poll.head->lock);
4245 if (!done)
4246 __io_async_wake(req, poll, mask, io_poll_task_func);
4247 }
4248 refcount_dec(&req->refs);
4249 return 1;
4250}
4251
4252static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4253 wait_queue_func_t wake_func)
4254{
4255 poll->head = NULL;
4256 poll->done = false;
4257 poll->canceled = false;
4258 poll->events = events;
4259 INIT_LIST_HEAD(&poll->wait.entry);
4260 init_waitqueue_func_entry(&poll->wait, wake_func);
4261}
4262
4263static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
4264 struct wait_queue_head *head)
4265{
4266 struct io_kiocb *req = pt->req;
4267
4268 /*
4269 * If poll->head is already set, it's because the file being polled
4270 * uses multiple waitqueues for poll handling (eg one for read, one
4271 * for write). Setup a separate io_poll_iocb if this happens.
4272 */
4273 if (unlikely(poll->head)) {
4274 /* already have a 2nd entry, fail a third attempt */
4275 if (req->io) {
4276 pt->error = -EINVAL;
4277 return;
4278 }
4279 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
4280 if (!poll) {
4281 pt->error = -ENOMEM;
4282 return;
4283 }
4284 io_init_poll_iocb(poll, req->poll.events, io_poll_double_wake);
4285 refcount_inc(&req->refs);
4286 poll->wait.private = req;
4287 req->io = (void *) poll;
4288 }
4289
4290 pt->error = 0;
4291 poll->head = head;
4292 add_wait_queue(head, &poll->wait);
4293}
4294
4295static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
4296 struct poll_table_struct *p)
4297{
4298 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4299
4300 __io_queue_proc(&pt->req->apoll->poll, pt, head);
4301}
4302
Jens Axboed7718a92020-02-14 22:23:12 -07004303static void io_async_task_func(struct callback_head *cb)
4304{
4305 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
4306 struct async_poll *apoll = req->apoll;
4307 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31067252020-05-17 17:43:31 -06004308 bool canceled = false;
Jens Axboed7718a92020-02-14 22:23:12 -07004309
4310 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
4311
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004312 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07004313 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004314 return;
Jens Axboed7718a92020-02-14 22:23:12 -07004315 }
4316
Jens Axboe31067252020-05-17 17:43:31 -06004317 /* If req is still hashed, it cannot have been canceled. Don't check. */
4318 if (hash_hashed(&req->hash_node)) {
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004319 hash_del(&req->hash_node);
Jens Axboe31067252020-05-17 17:43:31 -06004320 } else {
4321 canceled = READ_ONCE(apoll->poll.canceled);
4322 if (canceled) {
4323 io_cqring_fill_event(req, -ECANCELED);
4324 io_commit_cqring(ctx);
4325 }
Jens Axboe2bae0472020-04-13 11:16:34 -06004326 }
4327
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004328 spin_unlock_irq(&ctx->completion_lock);
4329
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004330 /* restore ->work in case we need to retry again */
4331 memcpy(&req->work, &apoll->work, sizeof(req->work));
Jens Axboe31067252020-05-17 17:43:31 -06004332 kfree(apoll);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004333
Jens Axboe31067252020-05-17 17:43:31 -06004334 if (!canceled) {
4335 __set_current_state(TASK_RUNNING);
4336 mutex_lock(&ctx->uring_lock);
4337 __io_queue_sqe(req, NULL);
4338 mutex_unlock(&ctx->uring_lock);
4339 } else {
Jens Axboe2bae0472020-04-13 11:16:34 -06004340 io_cqring_ev_posted(ctx);
4341 req_set_fail_links(req);
Xiaoguang Wang44575a62020-04-19 10:06:55 +08004342 io_double_put_req(req);
Jens Axboe2bae0472020-04-13 11:16:34 -06004343 }
Jens Axboed7718a92020-02-14 22:23:12 -07004344}
4345
4346static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4347 void *key)
4348{
4349 struct io_kiocb *req = wait->private;
4350 struct io_poll_iocb *poll = &req->apoll->poll;
4351
4352 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
4353 key_to_poll(key));
4354
4355 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
4356}
4357
4358static void io_poll_req_insert(struct io_kiocb *req)
4359{
4360 struct io_ring_ctx *ctx = req->ctx;
4361 struct hlist_head *list;
4362
4363 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
4364 hlist_add_head(&req->hash_node, list);
4365}
4366
4367static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
4368 struct io_poll_iocb *poll,
4369 struct io_poll_table *ipt, __poll_t mask,
4370 wait_queue_func_t wake_func)
4371 __acquires(&ctx->completion_lock)
4372{
4373 struct io_ring_ctx *ctx = req->ctx;
4374 bool cancel = false;
4375
4376 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06004377 io_init_poll_iocb(poll, mask, wake_func);
4378 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07004379
4380 ipt->pt._key = mask;
4381 ipt->req = req;
4382 ipt->error = -EINVAL;
4383
Jens Axboed7718a92020-02-14 22:23:12 -07004384 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
4385
4386 spin_lock_irq(&ctx->completion_lock);
4387 if (likely(poll->head)) {
4388 spin_lock(&poll->head->lock);
4389 if (unlikely(list_empty(&poll->wait.entry))) {
4390 if (ipt->error)
4391 cancel = true;
4392 ipt->error = 0;
4393 mask = 0;
4394 }
4395 if (mask || ipt->error)
4396 list_del_init(&poll->wait.entry);
4397 else if (cancel)
4398 WRITE_ONCE(poll->canceled, true);
4399 else if (!poll->done) /* actually waiting for an event */
4400 io_poll_req_insert(req);
4401 spin_unlock(&poll->head->lock);
4402 }
4403
4404 return mask;
4405}
4406
4407static bool io_arm_poll_handler(struct io_kiocb *req)
4408{
4409 const struct io_op_def *def = &io_op_defs[req->opcode];
4410 struct io_ring_ctx *ctx = req->ctx;
4411 struct async_poll *apoll;
4412 struct io_poll_table ipt;
4413 __poll_t mask, ret;
Jens Axboe18bceab2020-05-15 11:56:54 -06004414 bool had_io;
Jens Axboed7718a92020-02-14 22:23:12 -07004415
4416 if (!req->file || !file_can_poll(req->file))
4417 return false;
4418 if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4419 return false;
4420 if (!def->pollin && !def->pollout)
4421 return false;
4422
4423 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
4424 if (unlikely(!apoll))
4425 return false;
4426
4427 req->flags |= REQ_F_POLLED;
4428 memcpy(&apoll->work, &req->work, sizeof(req->work));
Jens Axboe18bceab2020-05-15 11:56:54 -06004429 had_io = req->io != NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07004430
Jens Axboe3537b6a2020-04-03 11:19:06 -06004431 get_task_struct(current);
Jens Axboed7718a92020-02-14 22:23:12 -07004432 req->task = current;
4433 req->apoll = apoll;
4434 INIT_HLIST_NODE(&req->hash_node);
4435
Nathan Chancellor8755d972020-03-02 16:01:19 -07004436 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07004437 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07004438 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07004439 if (def->pollout)
4440 mask |= POLLOUT | POLLWRNORM;
4441 mask |= POLLERR | POLLPRI;
4442
4443 ipt.pt._qproc = io_async_queue_proc;
4444
4445 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
4446 io_async_wake);
4447 if (ret) {
4448 ipt.error = 0;
Jens Axboe18bceab2020-05-15 11:56:54 -06004449 /* only remove double add if we did it here */
4450 if (!had_io)
4451 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004452 spin_unlock_irq(&ctx->completion_lock);
4453 memcpy(&req->work, &apoll->work, sizeof(req->work));
4454 kfree(apoll);
4455 return false;
4456 }
4457 spin_unlock_irq(&ctx->completion_lock);
4458 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
4459 apoll->poll.events);
4460 return true;
4461}
4462
4463static bool __io_poll_remove_one(struct io_kiocb *req,
4464 struct io_poll_iocb *poll)
4465{
Jens Axboeb41e9852020-02-17 09:52:41 -07004466 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004467
4468 spin_lock(&poll->head->lock);
4469 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07004470 if (!list_empty(&poll->wait.entry)) {
4471 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07004472 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004473 }
4474 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004475 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07004476 return do_complete;
4477}
4478
4479static bool io_poll_remove_one(struct io_kiocb *req)
4480{
4481 bool do_complete;
4482
4483 if (req->opcode == IORING_OP_POLL_ADD) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004484 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004485 do_complete = __io_poll_remove_one(req, &req->poll);
4486 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004487 struct async_poll *apoll = req->apoll;
4488
Jens Axboed7718a92020-02-14 22:23:12 -07004489 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004490 do_complete = __io_poll_remove_one(req, &apoll->poll);
4491 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07004492 io_put_req(req);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06004493 /*
4494 * restore ->work because we will call
4495 * io_req_work_drop_env below when dropping the
4496 * final reference.
4497 */
4498 memcpy(&req->work, &apoll->work, sizeof(req->work));
4499 kfree(apoll);
4500 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08004501 }
4502
Jens Axboeb41e9852020-02-17 09:52:41 -07004503 if (do_complete) {
4504 io_cqring_fill_event(req, -ECANCELED);
4505 io_commit_cqring(req->ctx);
4506 req->flags |= REQ_F_COMP_LOCKED;
4507 io_put_req(req);
4508 }
4509
4510 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004511}
4512
4513static void io_poll_remove_all(struct io_ring_ctx *ctx)
4514{
Jens Axboe78076bb2019-12-04 19:56:40 -07004515 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004516 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004517 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004518
4519 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07004520 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
4521 struct hlist_head *list;
4522
4523 list = &ctx->cancel_hash[i];
4524 hlist_for_each_entry_safe(req, tmp, list, hash_node)
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004525 posted += io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004526 }
4527 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07004528
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06004529 if (posted)
4530 io_cqring_ev_posted(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004531}
4532
Jens Axboe47f46762019-11-09 17:43:02 -07004533static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
4534{
Jens Axboe78076bb2019-12-04 19:56:40 -07004535 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07004536 struct io_kiocb *req;
4537
Jens Axboe78076bb2019-12-04 19:56:40 -07004538 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
4539 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07004540 if (sqe_addr != req->user_data)
4541 continue;
4542 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07004543 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07004544 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07004545 }
4546
4547 return -ENOENT;
4548}
4549
Jens Axboe3529d8c2019-12-19 18:24:38 -07004550static int io_poll_remove_prep(struct io_kiocb *req,
4551 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004552{
Jens Axboe221c5eb2019-01-17 09:41:58 -07004553 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4554 return -EINVAL;
4555 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
4556 sqe->poll_events)
4557 return -EINVAL;
4558
Jens Axboe0969e782019-12-17 18:40:57 -07004559 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07004560 return 0;
4561}
4562
4563/*
4564 * Find a running poll command that matches one specified in sqe->addr,
4565 * and remove it if found.
4566 */
4567static int io_poll_remove(struct io_kiocb *req)
4568{
4569 struct io_ring_ctx *ctx = req->ctx;
4570 u64 addr;
4571 int ret;
4572
Jens Axboe0969e782019-12-17 18:40:57 -07004573 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004574 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07004575 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004576 spin_unlock_irq(&ctx->completion_lock);
4577
Jens Axboe78e19bb2019-11-06 15:21:34 -07004578 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004579 if (ret < 0)
4580 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004581 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004582 return 0;
4583}
4584
Jens Axboe221c5eb2019-01-17 09:41:58 -07004585static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
4586 void *key)
4587{
Jens Axboec2f2eb72020-02-10 09:07:05 -07004588 struct io_kiocb *req = wait->private;
4589 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004590
Jens Axboed7718a92020-02-14 22:23:12 -07004591 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004592}
4593
Jens Axboe221c5eb2019-01-17 09:41:58 -07004594static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
4595 struct poll_table_struct *p)
4596{
4597 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
4598
Jens Axboed7718a92020-02-14 22:23:12 -07004599 __io_queue_proc(&pt->req->poll, pt, head);
Jens Axboeeac406c2019-11-14 12:09:58 -07004600}
4601
Jens Axboe3529d8c2019-12-19 18:24:38 -07004602static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07004603{
4604 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004605 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004606
4607 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4608 return -EINVAL;
4609 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
4610 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06004611 if (!poll->file)
4612 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004613
Jens Axboe221c5eb2019-01-17 09:41:58 -07004614 events = READ_ONCE(sqe->poll_events);
4615 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeb41e9852020-02-17 09:52:41 -07004616
Jens Axboe3537b6a2020-04-03 11:19:06 -06004617 get_task_struct(current);
Jens Axboeb41e9852020-02-17 09:52:41 -07004618 req->task = current;
Jens Axboe0969e782019-12-17 18:40:57 -07004619 return 0;
4620}
4621
Pavel Begunkov014db002020-03-03 21:33:12 +03004622static int io_poll_add(struct io_kiocb *req)
Jens Axboe0969e782019-12-17 18:40:57 -07004623{
4624 struct io_poll_iocb *poll = &req->poll;
4625 struct io_ring_ctx *ctx = req->ctx;
4626 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07004627 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07004628
Jens Axboe78076bb2019-12-04 19:56:40 -07004629 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe36703242019-07-25 10:20:18 -06004630 INIT_LIST_HEAD(&req->list);
Jens Axboed7718a92020-02-14 22:23:12 -07004631 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06004632
Jens Axboed7718a92020-02-14 22:23:12 -07004633 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
4634 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004635
Jens Axboe8c838782019-03-12 15:48:16 -06004636 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06004637 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004638 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06004639 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07004640 spin_unlock_irq(&ctx->completion_lock);
4641
Jens Axboe8c838782019-03-12 15:48:16 -06004642 if (mask) {
4643 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03004644 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004645 }
Jens Axboe8c838782019-03-12 15:48:16 -06004646 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004647}
4648
Jens Axboe5262f562019-09-17 12:26:57 -06004649static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
4650{
Jens Axboead8a48a2019-11-15 08:49:11 -07004651 struct io_timeout_data *data = container_of(timer,
4652 struct io_timeout_data, timer);
4653 struct io_kiocb *req = data->req;
4654 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06004655 unsigned long flags;
4656
Jens Axboe5262f562019-09-17 12:26:57 -06004657 atomic_inc(&ctx->cq_timeouts);
4658
4659 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08004660 /*
Jens Axboe11365042019-10-16 09:08:32 -06004661 * We could be racing with timeout deletion. If the list is empty,
4662 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08004663 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004664 if (!list_empty(&req->list))
Jens Axboe11365042019-10-16 09:08:32 -06004665 list_del_init(&req->list);
Jens Axboe842f9612019-10-29 12:34:10 -06004666
Jens Axboe78e19bb2019-11-06 15:21:34 -07004667 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06004668 io_commit_cqring(ctx);
4669 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4670
4671 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004672 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004673 io_put_req(req);
4674 return HRTIMER_NORESTART;
4675}
4676
Jens Axboe47f46762019-11-09 17:43:02 -07004677static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
4678{
4679 struct io_kiocb *req;
4680 int ret = -ENOENT;
4681
4682 list_for_each_entry(req, &ctx->timeout_list, list) {
4683 if (user_data == req->user_data) {
4684 list_del_init(&req->list);
4685 ret = 0;
4686 break;
4687 }
4688 }
4689
4690 if (ret == -ENOENT)
4691 return ret;
4692
Jens Axboe2d283902019-12-04 11:08:05 -07004693 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07004694 if (ret == -1)
4695 return -EALREADY;
4696
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004697 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004698 io_cqring_fill_event(req, -ECANCELED);
4699 io_put_req(req);
4700 return 0;
4701}
4702
Jens Axboe3529d8c2019-12-19 18:24:38 -07004703static int io_timeout_remove_prep(struct io_kiocb *req,
4704 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07004705{
Jens Axboeb29472e2019-12-17 18:50:29 -07004706 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4707 return -EINVAL;
4708 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
4709 return -EINVAL;
4710
4711 req->timeout.addr = READ_ONCE(sqe->addr);
4712 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
4713 if (req->timeout.flags)
4714 return -EINVAL;
4715
Jens Axboeb29472e2019-12-17 18:50:29 -07004716 return 0;
4717}
4718
Jens Axboe11365042019-10-16 09:08:32 -06004719/*
4720 * Remove or update an existing timeout command
4721 */
Jens Axboefc4df992019-12-10 14:38:45 -07004722static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06004723{
4724 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07004725 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06004726
Jens Axboe11365042019-10-16 09:08:32 -06004727 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07004728 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06004729
Jens Axboe47f46762019-11-09 17:43:02 -07004730 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06004731 io_commit_cqring(ctx);
4732 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004733 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004734 if (ret < 0)
4735 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004736 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004737 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004738}
4739
Jens Axboe3529d8c2019-12-19 18:24:38 -07004740static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004741 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004742{
Jens Axboead8a48a2019-11-15 08:49:11 -07004743 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004744 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03004745 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06004746
Jens Axboead8a48a2019-11-15 08:49:11 -07004747 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004748 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004749 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004750 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03004751 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07004752 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004753 flags = READ_ONCE(sqe->timeout_flags);
4754 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004755 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004756
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004757 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07004758
Jens Axboe3529d8c2019-12-19 18:24:38 -07004759 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004760 return -ENOMEM;
4761
4762 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004763 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004764 req->flags |= REQ_F_TIMEOUT;
4765
4766 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004767 return -EFAULT;
4768
Jens Axboe11365042019-10-16 09:08:32 -06004769 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004770 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004771 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004772 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004773
Jens Axboead8a48a2019-11-15 08:49:11 -07004774 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4775 return 0;
4776}
4777
Jens Axboefc4df992019-12-10 14:38:45 -07004778static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004779{
Jens Axboead8a48a2019-11-15 08:49:11 -07004780 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004781 struct io_timeout_data *data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004782 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004783 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07004784
Pavel Begunkov733f5c92020-05-26 20:34:03 +03004785 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07004786
Jens Axboe5262f562019-09-17 12:26:57 -06004787 /*
4788 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004789 * timeout event to be satisfied. If it isn't set, then this is
4790 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004791 */
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004792 if (!off) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07004793 req->flags |= REQ_F_TIMEOUT_NOSEQ;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004794 entry = ctx->timeout_list.prev;
4795 goto add;
4796 }
Jens Axboe5262f562019-09-17 12:26:57 -06004797
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004798 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
4799 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06004800
4801 /*
4802 * Insertion sort, ensuring the first entry in the list is always
4803 * the one we need first.
4804 */
Jens Axboe5262f562019-09-17 12:26:57 -06004805 list_for_each_prev(entry, &ctx->timeout_list) {
4806 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
Jens Axboe5262f562019-09-17 12:26:57 -06004807
Jens Axboe93bd25b2019-11-11 23:34:31 -07004808 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4809 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03004810 /* nxt.seq is behind @tail, otherwise would've been completed */
4811 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06004812 break;
4813 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07004814add:
Jens Axboe5262f562019-09-17 12:26:57 -06004815 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004816 data->timer.function = io_timeout_fn;
4817 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004818 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004819 return 0;
4820}
4821
Jens Axboe62755e32019-10-28 21:49:21 -06004822static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004823{
Jens Axboe62755e32019-10-28 21:49:21 -06004824 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004825
Jens Axboe62755e32019-10-28 21:49:21 -06004826 return req->user_data == (unsigned long) data;
4827}
4828
Jens Axboee977d6d2019-11-05 12:39:45 -07004829static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004830{
Jens Axboe62755e32019-10-28 21:49:21 -06004831 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004832 int ret = 0;
4833
Jens Axboe62755e32019-10-28 21:49:21 -06004834 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4835 switch (cancel_ret) {
4836 case IO_WQ_CANCEL_OK:
4837 ret = 0;
4838 break;
4839 case IO_WQ_CANCEL_RUNNING:
4840 ret = -EALREADY;
4841 break;
4842 case IO_WQ_CANCEL_NOTFOUND:
4843 ret = -ENOENT;
4844 break;
4845 }
4846
Jens Axboee977d6d2019-11-05 12:39:45 -07004847 return ret;
4848}
4849
Jens Axboe47f46762019-11-09 17:43:02 -07004850static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4851 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03004852 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004853{
4854 unsigned long flags;
4855 int ret;
4856
4857 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4858 if (ret != -ENOENT) {
4859 spin_lock_irqsave(&ctx->completion_lock, flags);
4860 goto done;
4861 }
4862
4863 spin_lock_irqsave(&ctx->completion_lock, flags);
4864 ret = io_timeout_cancel(ctx, sqe_addr);
4865 if (ret != -ENOENT)
4866 goto done;
4867 ret = io_poll_cancel(ctx, sqe_addr);
4868done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004869 if (!ret)
4870 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004871 io_cqring_fill_event(req, ret);
4872 io_commit_cqring(ctx);
4873 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4874 io_cqring_ev_posted(ctx);
4875
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004876 if (ret < 0)
4877 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03004878 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004879}
4880
Jens Axboe3529d8c2019-12-19 18:24:38 -07004881static int io_async_cancel_prep(struct io_kiocb *req,
4882 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004883{
Jens Axboefbf23842019-12-17 18:45:56 -07004884 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004885 return -EINVAL;
4886 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4887 sqe->cancel_flags)
4888 return -EINVAL;
4889
Jens Axboefbf23842019-12-17 18:45:56 -07004890 req->cancel.addr = READ_ONCE(sqe->addr);
4891 return 0;
4892}
4893
Pavel Begunkov014db002020-03-03 21:33:12 +03004894static int io_async_cancel(struct io_kiocb *req)
Jens Axboefbf23842019-12-17 18:45:56 -07004895{
4896 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004897
Pavel Begunkov014db002020-03-03 21:33:12 +03004898 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004899 return 0;
4900}
4901
Jens Axboe05f3fb32019-12-09 11:22:50 -07004902static int io_files_update_prep(struct io_kiocb *req,
4903 const struct io_uring_sqe *sqe)
4904{
4905 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4906 return -EINVAL;
4907
4908 req->files_update.offset = READ_ONCE(sqe->off);
4909 req->files_update.nr_args = READ_ONCE(sqe->len);
4910 if (!req->files_update.nr_args)
4911 return -EINVAL;
4912 req->files_update.arg = READ_ONCE(sqe->addr);
4913 return 0;
4914}
4915
4916static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4917{
4918 struct io_ring_ctx *ctx = req->ctx;
4919 struct io_uring_files_update up;
4920 int ret;
4921
Jens Axboef86cd202020-01-29 13:46:44 -07004922 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004923 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004924
4925 up.offset = req->files_update.offset;
4926 up.fds = req->files_update.arg;
4927
4928 mutex_lock(&ctx->uring_lock);
4929 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4930 mutex_unlock(&ctx->uring_lock);
4931
4932 if (ret < 0)
4933 req_set_fail_links(req);
4934 io_cqring_add_event(req, ret);
4935 io_put_req(req);
4936 return 0;
4937}
4938
Jens Axboe3529d8c2019-12-19 18:24:38 -07004939static int io_req_defer_prep(struct io_kiocb *req,
4940 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004941{
Jens Axboee7815732019-12-17 19:45:06 -07004942 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004943
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004944 if (!sqe)
4945 return 0;
4946
Jens Axboef86cd202020-01-29 13:46:44 -07004947 if (io_op_defs[req->opcode].file_table) {
4948 ret = io_grab_files(req);
4949 if (unlikely(ret))
4950 return ret;
4951 }
4952
Jens Axboecccf0ee2020-01-27 16:34:48 -07004953 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4954
Jens Axboed625c6e2019-12-17 19:53:05 -07004955 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004956 case IORING_OP_NOP:
4957 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004958 case IORING_OP_READV:
4959 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004960 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004961 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004962 break;
4963 case IORING_OP_WRITEV:
4964 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004965 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004966 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004967 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004968 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004969 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004970 break;
4971 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004972 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004973 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004974 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004975 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004976 break;
4977 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004978 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004979 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004980 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004981 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004982 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004983 break;
4984 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004985 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004986 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004987 break;
Jens Axboef499a022019-12-02 16:28:46 -07004988 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004989 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004990 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004991 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004992 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004993 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004994 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004995 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004996 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004997 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004998 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004999 break;
Jens Axboe2d283902019-12-04 11:08:05 -07005000 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005001 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005002 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005003 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005004 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005005 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005006 case IORING_OP_FALLOCATE:
5007 ret = io_fallocate_prep(req, sqe);
5008 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005009 case IORING_OP_OPENAT:
5010 ret = io_openat_prep(req, sqe);
5011 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005012 case IORING_OP_CLOSE:
5013 ret = io_close_prep(req, sqe);
5014 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005015 case IORING_OP_FILES_UPDATE:
5016 ret = io_files_update_prep(req, sqe);
5017 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005018 case IORING_OP_STATX:
5019 ret = io_statx_prep(req, sqe);
5020 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005021 case IORING_OP_FADVISE:
5022 ret = io_fadvise_prep(req, sqe);
5023 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005024 case IORING_OP_MADVISE:
5025 ret = io_madvise_prep(req, sqe);
5026 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005027 case IORING_OP_OPENAT2:
5028 ret = io_openat2_prep(req, sqe);
5029 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005030 case IORING_OP_EPOLL_CTL:
5031 ret = io_epoll_ctl_prep(req, sqe);
5032 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005033 case IORING_OP_SPLICE:
5034 ret = io_splice_prep(req, sqe);
5035 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005036 case IORING_OP_PROVIDE_BUFFERS:
5037 ret = io_provide_buffers_prep(req, sqe);
5038 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005039 case IORING_OP_REMOVE_BUFFERS:
5040 ret = io_remove_buffers_prep(req, sqe);
5041 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005042 case IORING_OP_TEE:
5043 ret = io_tee_prep(req, sqe);
5044 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005045 default:
Jens Axboee7815732019-12-17 19:45:06 -07005046 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5047 req->opcode);
5048 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005049 break;
Jens Axboef67676d2019-12-02 11:03:47 -07005050 }
5051
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005052 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07005053}
5054
Jens Axboe3529d8c2019-12-19 18:24:38 -07005055static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06005056{
Jackie Liua197f662019-11-08 08:09:12 -07005057 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07005058 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06005059
Bob Liu9d858b22019-11-13 18:06:25 +08005060 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov4ee36312020-05-01 17:09:37 +03005061 if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06005062 return 0;
5063
Pavel Begunkov650b5482020-05-17 14:02:11 +03005064 if (!req->io) {
5065 if (io_alloc_async_ctx(req))
5066 return -EAGAIN;
5067 ret = io_req_defer_prep(req, sqe);
5068 if (ret < 0)
5069 return ret;
5070 }
Jens Axboe2d283902019-12-04 11:08:05 -07005071
Jens Axboede0617e2019-04-06 21:51:27 -06005072 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08005073 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06005074 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06005075 return 0;
5076 }
5077
Jens Axboe915967f2019-11-21 09:01:20 -07005078 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06005079 list_add_tail(&req->list, &ctx->defer_list);
5080 spin_unlock_irq(&ctx->completion_lock);
5081 return -EIOCBQUEUED;
5082}
5083
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005084static void io_cleanup_req(struct io_kiocb *req)
5085{
5086 struct io_async_ctx *io = req->io;
5087
5088 switch (req->opcode) {
5089 case IORING_OP_READV:
5090 case IORING_OP_READ_FIXED:
5091 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07005092 if (req->flags & REQ_F_BUFFER_SELECTED)
5093 kfree((void *)(unsigned long)req->rw.addr);
5094 /* fallthrough */
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005095 case IORING_OP_WRITEV:
5096 case IORING_OP_WRITE_FIXED:
5097 case IORING_OP_WRITE:
5098 if (io->rw.iov != io->rw.fast_iov)
5099 kfree(io->rw.iov);
5100 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005101 case IORING_OP_RECVMSG:
Jens Axboe52de1fe2020-02-27 10:15:42 -07005102 if (req->flags & REQ_F_BUFFER_SELECTED)
5103 kfree(req->sr_msg.kbuf);
5104 /* fallthrough */
5105 case IORING_OP_SENDMSG:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005106 if (io->msg.iov != io->msg.fast_iov)
5107 kfree(io->msg.iov);
5108 break;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005109 case IORING_OP_RECV:
5110 if (req->flags & REQ_F_BUFFER_SELECTED)
5111 kfree(req->sr_msg.kbuf);
5112 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005113 case IORING_OP_OPENAT:
5114 case IORING_OP_OPENAT2:
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03005115 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005116 case IORING_OP_SPLICE:
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005117 case IORING_OP_TEE:
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005118 io_put_file(req, req->splice.file_in,
5119 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5120 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005121 }
5122
5123 req->flags &= ~REQ_F_NEED_CLEANUP;
5124}
5125
Jens Axboe3529d8c2019-12-19 18:24:38 -07005126static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Pavel Begunkov014db002020-03-03 21:33:12 +03005127 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005128{
Jackie Liua197f662019-11-08 08:09:12 -07005129 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07005130 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005131
Jens Axboed625c6e2019-12-17 19:53:05 -07005132 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07005133 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005134 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005135 break;
5136 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005137 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005138 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005139 if (sqe) {
5140 ret = io_read_prep(req, sqe, force_nonblock);
5141 if (ret < 0)
5142 break;
5143 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005144 ret = io_read(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005145 break;
5146 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07005147 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005148 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005149 if (sqe) {
5150 ret = io_write_prep(req, sqe, force_nonblock);
5151 if (ret < 0)
5152 break;
5153 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005154 ret = io_write(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005155 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005156 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005157 if (sqe) {
5158 ret = io_prep_fsync(req, sqe);
5159 if (ret < 0)
5160 break;
5161 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005162 ret = io_fsync(req, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07005163 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005164 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005165 if (sqe) {
5166 ret = io_poll_add_prep(req, sqe);
5167 if (ret)
5168 break;
5169 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005170 ret = io_poll_add(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005171 break;
5172 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005173 if (sqe) {
5174 ret = io_poll_remove_prep(req, sqe);
5175 if (ret < 0)
5176 break;
5177 }
Jens Axboefc4df992019-12-10 14:38:45 -07005178 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005179 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005180 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005181 if (sqe) {
5182 ret = io_prep_sfr(req, sqe);
5183 if (ret < 0)
5184 break;
5185 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005186 ret = io_sync_file_range(req, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06005187 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005188 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005189 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005190 if (sqe) {
5191 ret = io_sendmsg_prep(req, sqe);
5192 if (ret < 0)
5193 break;
5194 }
Jens Axboefddafac2020-01-04 20:19:44 -07005195 if (req->opcode == IORING_OP_SENDMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005196 ret = io_sendmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005197 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005198 ret = io_send(req, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005199 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06005200 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005201 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005202 if (sqe) {
5203 ret = io_recvmsg_prep(req, sqe);
5204 if (ret)
5205 break;
5206 }
Jens Axboefddafac2020-01-04 20:19:44 -07005207 if (req->opcode == IORING_OP_RECVMSG)
Pavel Begunkov014db002020-03-03 21:33:12 +03005208 ret = io_recvmsg(req, force_nonblock);
Jens Axboefddafac2020-01-04 20:19:44 -07005209 else
Pavel Begunkov014db002020-03-03 21:33:12 +03005210 ret = io_recv(req, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06005211 break;
Jens Axboe5262f562019-09-17 12:26:57 -06005212 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005213 if (sqe) {
5214 ret = io_timeout_prep(req, sqe, false);
5215 if (ret)
5216 break;
5217 }
Jens Axboefc4df992019-12-10 14:38:45 -07005218 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005219 break;
Jens Axboe11365042019-10-16 09:08:32 -06005220 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005221 if (sqe) {
5222 ret = io_timeout_remove_prep(req, sqe);
5223 if (ret)
5224 break;
5225 }
Jens Axboefc4df992019-12-10 14:38:45 -07005226 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06005227 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06005228 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005229 if (sqe) {
5230 ret = io_accept_prep(req, sqe);
5231 if (ret)
5232 break;
5233 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005234 ret = io_accept(req, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005235 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005236 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005237 if (sqe) {
5238 ret = io_connect_prep(req, sqe);
5239 if (ret)
5240 break;
5241 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005242 ret = io_connect(req, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005243 break;
Jens Axboe62755e32019-10-28 21:49:21 -06005244 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07005245 if (sqe) {
5246 ret = io_async_cancel_prep(req, sqe);
5247 if (ret)
5248 break;
5249 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005250 ret = io_async_cancel(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005251 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07005252 case IORING_OP_FALLOCATE:
5253 if (sqe) {
5254 ret = io_fallocate_prep(req, sqe);
5255 if (ret)
5256 break;
5257 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005258 ret = io_fallocate(req, force_nonblock);
Jens Axboed63d1b52019-12-10 10:38:56 -07005259 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07005260 case IORING_OP_OPENAT:
5261 if (sqe) {
5262 ret = io_openat_prep(req, sqe);
5263 if (ret)
5264 break;
5265 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005266 ret = io_openat(req, force_nonblock);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005267 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07005268 case IORING_OP_CLOSE:
5269 if (sqe) {
5270 ret = io_close_prep(req, sqe);
5271 if (ret)
5272 break;
5273 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005274 ret = io_close(req, force_nonblock);
Jens Axboeb5dba592019-12-11 14:02:38 -07005275 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005276 case IORING_OP_FILES_UPDATE:
5277 if (sqe) {
5278 ret = io_files_update_prep(req, sqe);
5279 if (ret)
5280 break;
5281 }
5282 ret = io_files_update(req, force_nonblock);
5283 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005284 case IORING_OP_STATX:
5285 if (sqe) {
5286 ret = io_statx_prep(req, sqe);
5287 if (ret)
5288 break;
5289 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005290 ret = io_statx(req, force_nonblock);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005291 break;
Jens Axboe4840e412019-12-25 22:03:45 -07005292 case IORING_OP_FADVISE:
5293 if (sqe) {
5294 ret = io_fadvise_prep(req, sqe);
5295 if (ret)
5296 break;
5297 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005298 ret = io_fadvise(req, force_nonblock);
Jens Axboe4840e412019-12-25 22:03:45 -07005299 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07005300 case IORING_OP_MADVISE:
5301 if (sqe) {
5302 ret = io_madvise_prep(req, sqe);
5303 if (ret)
5304 break;
5305 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005306 ret = io_madvise(req, force_nonblock);
Jens Axboec1ca7572019-12-25 22:18:28 -07005307 break;
Jens Axboecebdb982020-01-08 17:59:24 -07005308 case IORING_OP_OPENAT2:
5309 if (sqe) {
5310 ret = io_openat2_prep(req, sqe);
5311 if (ret)
5312 break;
5313 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005314 ret = io_openat2(req, force_nonblock);
Jens Axboecebdb982020-01-08 17:59:24 -07005315 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07005316 case IORING_OP_EPOLL_CTL:
5317 if (sqe) {
5318 ret = io_epoll_ctl_prep(req, sqe);
5319 if (ret)
5320 break;
5321 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005322 ret = io_epoll_ctl(req, force_nonblock);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005323 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005324 case IORING_OP_SPLICE:
5325 if (sqe) {
5326 ret = io_splice_prep(req, sqe);
5327 if (ret < 0)
5328 break;
5329 }
Pavel Begunkov014db002020-03-03 21:33:12 +03005330 ret = io_splice(req, force_nonblock);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005331 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07005332 case IORING_OP_PROVIDE_BUFFERS:
5333 if (sqe) {
5334 ret = io_provide_buffers_prep(req, sqe);
5335 if (ret)
5336 break;
5337 }
5338 ret = io_provide_buffers(req, force_nonblock);
5339 break;
Jens Axboe067524e2020-03-02 16:32:28 -07005340 case IORING_OP_REMOVE_BUFFERS:
5341 if (sqe) {
5342 ret = io_remove_buffers_prep(req, sqe);
5343 if (ret)
5344 break;
5345 }
5346 ret = io_remove_buffers(req, force_nonblock);
Jens Axboe31b51512019-01-18 22:56:34 -07005347 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005348 case IORING_OP_TEE:
5349 if (sqe) {
5350 ret = io_tee_prep(req, sqe);
5351 if (ret < 0)
5352 break;
5353 }
5354 ret = io_tee(req, force_nonblock);
5355 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005356 default:
5357 ret = -EINVAL;
5358 break;
5359 }
5360
5361 if (ret)
5362 return ret;
5363
Jens Axboeb5325762020-05-19 21:20:27 -06005364 /* If the op doesn't have a file, we're not polling for it */
5365 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07005366 const bool in_async = io_wq_current_is_worker();
5367
Jens Axboe9e645e112019-05-10 16:07:28 -06005368 if (req->result == -EAGAIN)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005369 return -EAGAIN;
5370
Jens Axboe11ba8202020-01-15 21:51:17 -07005371 /* workqueue context doesn't hold uring_lock, grab it now */
5372 if (in_async)
5373 mutex_lock(&ctx->uring_lock);
5374
Jens Axboe2b188cc2019-01-07 10:46:33 -07005375 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07005376
5377 if (in_async)
5378 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07005379 }
5380
5381 return 0;
5382}
5383
Jens Axboe561fb042019-10-24 07:25:42 -06005384static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboedef596e2019-01-09 08:59:42 -07005385{
Jens Axboe561fb042019-10-24 07:25:42 -06005386 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005387 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06005388 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005389
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005390 /* if NO_CANCEL is set, we must still run the work */
5391 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
5392 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06005393 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07005394 }
Jens Axboe31b51512019-01-18 22:56:34 -07005395
Jens Axboe561fb042019-10-24 07:25:42 -06005396 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06005397 do {
Pavel Begunkov014db002020-03-03 21:33:12 +03005398 ret = io_issue_sqe(req, NULL, false);
Jens Axboe561fb042019-10-24 07:25:42 -06005399 /*
5400 * We can get EAGAIN for polled IO even though we're
5401 * forcing a sync submission from here, since we can't
5402 * wait for request slots on the block side.
5403 */
5404 if (ret != -EAGAIN)
5405 break;
5406 cond_resched();
5407 } while (1);
5408 }
Jens Axboe31b51512019-01-18 22:56:34 -07005409
Jens Axboe561fb042019-10-24 07:25:42 -06005410 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005411 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005412 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06005413 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07005414 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005415
Pavel Begunkove9fd9392020-03-04 16:14:12 +03005416 io_steal_work(req, workptr);
Jens Axboe31b51512019-01-18 22:56:34 -07005417}
Jens Axboe2b188cc2019-01-07 10:46:33 -07005418
Jens Axboe65e19f52019-10-26 07:20:21 -06005419static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
5420 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06005421{
Jens Axboe65e19f52019-10-26 07:20:21 -06005422 struct fixed_file_table *table;
5423
Jens Axboe05f3fb32019-12-09 11:22:50 -07005424 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08005425 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06005426}
5427
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005428static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
5429 int fd, struct file **out_file, bool fixed)
5430{
5431 struct io_ring_ctx *ctx = req->ctx;
5432 struct file *file;
5433
5434 if (fixed) {
5435 if (unlikely(!ctx->file_data ||
5436 (unsigned) fd >= ctx->nr_user_files))
5437 return -EBADF;
5438 fd = array_index_nospec(fd, ctx->nr_user_files);
5439 file = io_file_from_index(ctx, fd);
5440 if (!file)
5441 return -EBADF;
Xiaoguang Wang05589552020-03-31 14:05:18 +08005442 req->fixed_file_refs = ctx->file_data->cur_refs;
5443 percpu_ref_get(req->fixed_file_refs);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005444 } else {
5445 trace_io_uring_file_get(ctx, fd);
5446 file = __io_file_get(state, fd);
5447 if (unlikely(!file))
5448 return -EBADF;
5449 }
5450
5451 *out_file = file;
5452 return 0;
5453}
5454
Jens Axboe3529d8c2019-12-19 18:24:38 -07005455static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
Jens Axboe63ff8222020-05-07 14:56:15 -06005456 int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06005457{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005458 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06005459
Jens Axboe63ff8222020-05-07 14:56:15 -06005460 fixed = (req->flags & REQ_F_FIXED_FILE) != 0;
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005461 if (unlikely(!fixed && io_async_submit(req->ctx)))
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005462 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06005463
Pavel Begunkov8da11c12020-02-24 11:32:44 +03005464 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06005465}
5466
Jackie Liua197f662019-11-08 08:09:12 -07005467static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005468{
Jens Axboefcb323c2019-10-24 12:39:47 -06005469 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07005470 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06005471
Jens Axboe5b0bbee2020-04-27 10:41:22 -06005472 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
Jens Axboef86cd202020-01-29 13:46:44 -07005473 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005474 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07005475 return -EBADF;
5476
Jens Axboefcb323c2019-10-24 12:39:47 -06005477 rcu_read_lock();
5478 spin_lock_irq(&ctx->inflight_lock);
5479 /*
5480 * We use the f_ops->flush() handler to ensure that we can flush
5481 * out work accessing these files if the fd is closed. Check if
5482 * the fd has changed since we started down this path, and disallow
5483 * this operation if it has.
5484 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005485 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06005486 list_add(&req->inflight_entry, &ctx->inflight_list);
5487 req->flags |= REQ_F_INFLIGHT;
5488 req->work.files = current->files;
5489 ret = 0;
5490 }
5491 spin_unlock_irq(&ctx->inflight_lock);
5492 rcu_read_unlock();
5493
5494 return ret;
5495}
5496
Jens Axboe2665abf2019-11-05 12:40:47 -07005497static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
5498{
Jens Axboead8a48a2019-11-15 08:49:11 -07005499 struct io_timeout_data *data = container_of(timer,
5500 struct io_timeout_data, timer);
5501 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07005502 struct io_ring_ctx *ctx = req->ctx;
5503 struct io_kiocb *prev = NULL;
5504 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07005505
5506 spin_lock_irqsave(&ctx->completion_lock, flags);
5507
5508 /*
5509 * We don't expect the list to be empty, that will only happen if we
5510 * race with the completion of the linked work.
5511 */
Pavel Begunkov44932332019-12-05 16:16:35 +03005512 if (!list_empty(&req->link_list)) {
5513 prev = list_entry(req->link_list.prev, struct io_kiocb,
5514 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005515 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03005516 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07005517 prev->flags &= ~REQ_F_LINK_TIMEOUT;
5518 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07005519 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005520 }
5521
5522 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5523
5524 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005525 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03005526 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07005527 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07005528 } else {
5529 io_cqring_add_event(req, -ETIME);
5530 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005531 }
Jens Axboe2665abf2019-11-05 12:40:47 -07005532 return HRTIMER_NORESTART;
5533}
5534
Jens Axboead8a48a2019-11-15 08:49:11 -07005535static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005536{
Jens Axboe76a46e02019-11-10 23:34:16 -07005537 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07005538
Jens Axboe76a46e02019-11-10 23:34:16 -07005539 /*
5540 * If the list is now empty, then our linked request finished before
5541 * we got a chance to setup the timer
5542 */
5543 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03005544 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07005545 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07005546
Jens Axboead8a48a2019-11-15 08:49:11 -07005547 data->timer.function = io_link_timeout_fn;
5548 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
5549 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07005550 }
Jens Axboe76a46e02019-11-10 23:34:16 -07005551 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07005552
Jens Axboe2665abf2019-11-05 12:40:47 -07005553 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07005554 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07005555}
5556
Jens Axboead8a48a2019-11-15 08:49:11 -07005557static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07005558{
5559 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005560
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005561 if (!(req->flags & REQ_F_LINK_HEAD))
Jens Axboe2665abf2019-11-05 12:40:47 -07005562 return NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005563 /* for polled retry, if flag is set, we already went through here */
5564 if (req->flags & REQ_F_POLLED)
5565 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005566
Pavel Begunkov44932332019-12-05 16:16:35 +03005567 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
5568 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07005569 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07005570 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07005571
Jens Axboe76a46e02019-11-10 23:34:16 -07005572 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07005573 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07005574}
5575
Jens Axboe3529d8c2019-12-19 18:24:38 -07005576static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005577{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005578 struct io_kiocb *linked_timeout;
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005579 struct io_kiocb *nxt;
Jens Axboe193155c2020-02-22 23:22:19 -07005580 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005581 int ret;
5582
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005583again:
5584 linked_timeout = io_prep_linked_timeout(req);
5585
Jens Axboe193155c2020-02-22 23:22:19 -07005586 if (req->work.creds && req->work.creds != current_cred()) {
5587 if (old_creds)
5588 revert_creds(old_creds);
5589 if (old_creds == req->work.creds)
5590 old_creds = NULL; /* restored original creds */
5591 else
5592 old_creds = override_creds(req->work.creds);
5593 }
5594
Pavel Begunkov014db002020-03-03 21:33:12 +03005595 ret = io_issue_sqe(req, sqe, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06005596
5597 /*
5598 * We async punt it if the file wasn't marked NOWAIT, or if the file
5599 * doesn't support non-blocking read/write attempts
5600 */
5601 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5602 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboed7718a92020-02-14 22:23:12 -07005603 if (io_arm_poll_handler(req)) {
5604 if (linked_timeout)
5605 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005606 goto exit;
Jens Axboed7718a92020-02-14 22:23:12 -07005607 }
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005608punt:
Jens Axboef86cd202020-01-29 13:46:44 -07005609 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005610 ret = io_grab_files(req);
5611 if (ret)
5612 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005613 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03005614
5615 /*
5616 * Queued up for async execution, worker will release
5617 * submit reference when the iocb is actually submitted.
5618 */
5619 io_queue_async_work(req);
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005620 goto exit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005621 }
Jens Axboee65ef562019-03-12 10:16:44 -06005622
Jens Axboefcb323c2019-10-24 12:39:47 -06005623err:
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005624 nxt = NULL;
Jens Axboee65ef562019-03-12 10:16:44 -06005625 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07005626 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06005627
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005628 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07005629 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005630 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005631 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03005632 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07005633 }
5634
Jens Axboee65ef562019-03-12 10:16:44 -06005635 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06005636 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07005637 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005638 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06005639 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06005640 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005641 if (nxt) {
5642 req = nxt;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03005643
5644 if (req->flags & REQ_F_FORCE_ASYNC)
5645 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07005646 goto again;
5647 }
Pavel Begunkov4bc44942020-02-29 22:48:24 +03005648exit:
Jens Axboe193155c2020-02-22 23:22:19 -07005649 if (old_creds)
5650 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005651}
5652
Jens Axboe3529d8c2019-12-19 18:24:38 -07005653static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005654{
5655 int ret;
5656
Jens Axboe3529d8c2019-12-19 18:24:38 -07005657 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005658 if (ret) {
5659 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03005660fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07005661 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005662 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07005663 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005664 }
Pavel Begunkov25508782019-12-30 21:24:47 +03005665 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbd2ab182020-05-17 14:02:12 +03005666 if (!req->io) {
5667 ret = -EAGAIN;
5668 if (io_alloc_async_ctx(req))
5669 goto fail_req;
5670 ret = io_req_defer_prep(req, sqe);
5671 if (unlikely(ret < 0))
5672 goto fail_req;
5673 }
5674
Jens Axboece35a472019-12-17 08:04:44 -07005675 /*
5676 * Never try inline submit of IOSQE_ASYNC is set, go straight
5677 * to async execution.
5678 */
5679 req->work.flags |= IO_WQ_WORK_CONCURRENT;
5680 io_queue_async_work(req);
5681 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005682 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07005683 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08005684}
5685
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005686static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08005687{
Jens Axboe94ae5e72019-11-14 19:39:52 -07005688 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005689 io_cqring_add_event(req, -ECANCELED);
5690 io_double_put_req(req);
5691 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07005692 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08005693}
5694
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005695static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005696 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06005697{
Jackie Liua197f662019-11-08 08:09:12 -07005698 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005699 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06005700
Jens Axboe9e645e112019-05-10 16:07:28 -06005701 /*
5702 * If we already have a head request, queue this one for async
5703 * submittal once the head completes. If we don't have a head but
5704 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
5705 * submitted sync once the chain is complete. If none of those
5706 * conditions are true (normal request), then just queue it.
5707 */
5708 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03005709 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005710
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005711 /*
5712 * Taking sequential execution of a link, draining both sides
5713 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5714 * requests in the link. So, it drains the head and the
5715 * next after the link request. The last one is done via
5716 * drain_next flag to persist the effect across calls.
5717 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005718 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03005719 head->flags |= REQ_F_IO_DRAIN;
5720 ctx->drain_next = 1;
5721 }
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005722 if (io_alloc_async_ctx(req))
5723 return -EAGAIN;
Jens Axboe9e645e112019-05-10 16:07:28 -06005724
Jens Axboe3529d8c2019-12-19 18:24:38 -07005725 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005726 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005727 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005728 head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005729 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07005730 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005731 trace_io_uring_link(ctx, req, head);
5732 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005733
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005734 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005735 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005736 io_queue_link_head(head);
5737 *link = NULL;
5738 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005739 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005740 if (unlikely(ctx->drain_next)) {
5741 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005742 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03005743 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005744 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkovdea3b492020-04-12 02:05:04 +03005745 req->flags |= REQ_F_LINK_HEAD;
Pavel Begunkov711be032020-01-17 03:57:59 +03005746 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03005747
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005748 if (io_alloc_async_ctx(req))
5749 return -EAGAIN;
5750
Pavel Begunkov711be032020-01-17 03:57:59 +03005751 ret = io_req_defer_prep(req, sqe);
5752 if (ret)
5753 req->flags |= REQ_F_FAIL_LINK;
5754 *link = req;
5755 } else {
5756 io_queue_sqe(req, sqe);
5757 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005758 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005759
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005760 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06005761}
5762
Jens Axboe9a56a232019-01-09 09:06:50 -07005763/*
5764 * Batched submission is done, ensure local IO is flushed out.
5765 */
5766static void io_submit_state_end(struct io_submit_state *state)
5767{
5768 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03005769 io_state_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005770 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005771 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005772}
5773
5774/*
5775 * Start submission side cache.
5776 */
5777static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005778 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005779{
5780 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005781 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005782 state->file = NULL;
5783 state->ios_left = max_ios;
5784}
5785
Jens Axboe2b188cc2019-01-07 10:46:33 -07005786static void io_commit_sqring(struct io_ring_ctx *ctx)
5787{
Hristo Venev75b28af2019-08-26 17:23:46 +00005788 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005789
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005790 /*
5791 * Ensure any loads from the SQEs are done at this point,
5792 * since once we write the new head, the application could
5793 * write new data to them.
5794 */
5795 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005796}
5797
5798/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005799 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005800 * that is mapped by userspace. This means that care needs to be taken to
5801 * ensure that reads are stable, as we cannot rely on userspace always
5802 * being a good citizen. If members of the sqe are validated and then later
5803 * used, it's important that those reads are done through READ_ONCE() to
5804 * prevent a re-load down the line.
5805 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03005806static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005807{
Hristo Venev75b28af2019-08-26 17:23:46 +00005808 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005809 unsigned head;
5810
5811 /*
5812 * The cached sq head (or cq tail) serves two purposes:
5813 *
5814 * 1) allows us to batch the cost of updating the user visible
5815 * head updates.
5816 * 2) allows the kernel side to track the head on its own, even
5817 * though the application is the one updating it.
5818 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005819 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005820 if (likely(head < ctx->sq_entries))
5821 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07005822
5823 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06005824 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005825 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005826 return NULL;
5827}
5828
5829static inline void io_consume_sqe(struct io_ring_ctx *ctx)
5830{
5831 ctx->cached_sq_head++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005832}
5833
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005834#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
5835 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
5836 IOSQE_BUFFER_SELECT)
5837
5838static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
5839 const struct io_uring_sqe *sqe,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005840 struct io_submit_state *state)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005841{
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005842 unsigned int sqe_flags;
Jens Axboe63ff8222020-05-07 14:56:15 -06005843 int id;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005844
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005845 /*
5846 * All io need record the previous position, if LINK vs DARIN,
5847 * it can be used to mark the position of the first IO in the
5848 * link list.
5849 */
Pavel Begunkov31af27c2020-04-15 00:39:50 +03005850 req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005851 req->opcode = READ_ONCE(sqe->opcode);
5852 req->user_data = READ_ONCE(sqe->user_data);
5853 req->io = NULL;
5854 req->file = NULL;
5855 req->ctx = ctx;
5856 req->flags = 0;
5857 /* one is dropped after submission, the other at completion */
5858 refcount_set(&req->refs, 2);
5859 req->task = NULL;
5860 req->result = 0;
5861 INIT_IO_WORK(&req->work, io_wq_submit_work);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005862
5863 if (unlikely(req->opcode >= IORING_OP_LAST))
5864 return -EINVAL;
5865
5866 if (io_op_defs[req->opcode].needs_mm && !current->mm) {
5867 if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
5868 return -EFAULT;
5869 use_mm(ctx->sqo_mm);
5870 }
5871
5872 sqe_flags = READ_ONCE(sqe->flags);
5873 /* enforce forwards compatibility on users */
5874 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
5875 return -EINVAL;
5876
5877 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
5878 !io_op_defs[req->opcode].buffer_select)
5879 return -EOPNOTSUPP;
5880
5881 id = READ_ONCE(sqe->personality);
5882 if (id) {
5883 req->work.creds = idr_find(&ctx->personality_idr, id);
5884 if (unlikely(!req->work.creds))
5885 return -EINVAL;
5886 get_cred(req->work.creds);
5887 }
5888
5889 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkovc11368a52020-05-17 14:13:42 +03005890 req->flags |= sqe_flags;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005891
Jens Axboe63ff8222020-05-07 14:56:15 -06005892 if (!io_op_defs[req->opcode].needs_file)
5893 return 0;
5894
5895 return io_req_set_file(state, req, READ_ONCE(sqe->fd));
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005896}
5897
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005898static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005899 struct file *ring_file, int ring_fd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005900{
5901 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005902 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005903 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005904
Jens Axboec4a2ed72019-11-21 21:01:26 -07005905 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005906 if (test_bit(0, &ctx->sq_check_overflow)) {
5907 if (!list_empty(&ctx->cq_overflow_list) &&
5908 !io_cqring_overflow_flush(ctx, false))
5909 return -EBUSY;
5910 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005911
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005912 /* make sure SQ entry isn't read before tail */
5913 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005914
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005915 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5916 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005917
5918 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005919 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005920 statep = &state;
5921 }
5922
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005923 ctx->ring_fd = ring_fd;
5924 ctx->ring_file = ring_file;
5925
Jens Axboe6c271ce2019-01-10 11:22:30 -07005926 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005927 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005928 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005929 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005930
Pavel Begunkovb1e50e52020-04-08 08:58:44 +03005931 sqe = io_get_sqe(ctx);
5932 if (unlikely(!sqe)) {
5933 io_consume_sqe(ctx);
5934 break;
5935 }
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03005936 req = io_alloc_req(ctx, statep);
Pavel Begunkov196be952019-11-07 01:41:06 +03005937 if (unlikely(!req)) {
5938 if (!submitted)
5939 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005940 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005941 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005942
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005943 err = io_init_req(ctx, req, sqe, statep);
Pavel Begunkov709b3022020-04-08 08:58:43 +03005944 io_consume_sqe(ctx);
Jens Axboed3656342019-12-18 09:50:26 -07005945 /* will complete beyond this point, count as submitted */
5946 submitted++;
5947
Pavel Begunkovef4ff582020-04-12 02:05:05 +03005948 if (unlikely(err)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005949fail_req:
5950 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005951 io_double_put_req(req);
5952 break;
5953 }
5954
Jens Axboe354420f2020-01-08 18:55:15 -07005955 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03005956 true, io_async_submit(ctx));
Xiaoguang Wang7d01bd72020-05-08 21:19:30 +08005957 err = io_submit_sqe(req, sqe, &link);
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03005958 if (err)
5959 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005960 }
5961
Pavel Begunkov9466f432020-01-25 22:34:01 +03005962 if (unlikely(submitted != nr)) {
5963 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5964
5965 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5966 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005967 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005968 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005969 if (statep)
5970 io_submit_state_end(&state);
5971
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005972 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5973 io_commit_sqring(ctx);
5974
Jens Axboe6c271ce2019-01-10 11:22:30 -07005975 return submitted;
5976}
5977
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03005978static inline void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
5979{
5980 struct mm_struct *mm = current->mm;
5981
5982 if (mm) {
5983 unuse_mm(mm);
5984 mmput(mm);
5985 }
5986}
5987
Jens Axboe6c271ce2019-01-10 11:22:30 -07005988static int io_sq_thread(void *data)
5989{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005990 struct io_ring_ctx *ctx = data;
Jens Axboe181e4482019-11-25 08:52:30 -07005991 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005992 mm_segment_t old_fs;
5993 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005994 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005995 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005996
Jens Axboe0f158b42020-05-14 17:18:39 -06005997 complete(&ctx->sq_thread_comp);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005998
Jens Axboe6c271ce2019-01-10 11:22:30 -07005999 old_fs = get_fs();
6000 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07006001 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006002
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006003 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006004 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006005 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006006
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006007 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006008 unsigned nr_events = 0;
6009
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006010 mutex_lock(&ctx->uring_lock);
6011 if (!list_empty(&ctx->poll_list))
6012 io_iopoll_getevents(ctx, &nr_events, 0);
6013 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07006014 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006015 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006016 }
6017
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006018 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006019
6020 /*
6021 * If submit got -EBUSY, flag us as needing the application
6022 * to enter the kernel to reap and flush events.
6023 */
6024 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006025 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006026 * Drop cur_mm before scheduling, we can't hold it for
6027 * long periods (or over schedule()). Do this before
6028 * adding ourselves to the waitqueue, as the unuse/drop
6029 * may sleep.
6030 */
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006031 io_sq_thread_drop_mm(ctx);
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01006032
6033 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07006034 * We're polling. If we're within the defined idle
6035 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07006036 * to sleep. The exception is if we got EBUSY doing
6037 * more IO, we should wait for the application to
6038 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07006039 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006040 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07006041 (!time_after(jiffies, timeout) && ret != -EBUSY &&
6042 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboeb41e9852020-02-17 09:52:41 -07006043 if (current->task_works)
6044 task_work_run();
Jens Axboe9831a902019-09-19 09:48:55 -06006045 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006046 continue;
6047 }
6048
Jens Axboe6c271ce2019-01-10 11:22:30 -07006049 prepare_to_wait(&ctx->sqo_wait, &wait,
6050 TASK_INTERRUPTIBLE);
6051
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006052 /*
6053 * While doing polled IO, before going to sleep, we need
6054 * to check if there are new reqs added to poll_list, it
6055 * is because reqs may have been punted to io worker and
6056 * will be added to poll_list later, hence check the
6057 * poll_list again.
6058 */
6059 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6060 !list_empty_careful(&ctx->poll_list)) {
6061 finish_wait(&ctx->sqo_wait, &wait);
6062 continue;
6063 }
6064
Jens Axboe6c271ce2019-01-10 11:22:30 -07006065 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00006066 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02006067 /* make sure to read SQ tail after writing flags */
6068 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006069
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006070 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07006071 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006072 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07006073 finish_wait(&ctx->sqo_wait, &wait);
6074 break;
6075 }
Jens Axboeb41e9852020-02-17 09:52:41 -07006076 if (current->task_works) {
6077 task_work_run();
Hillf Danton10bea962020-04-01 17:19:33 +08006078 finish_wait(&ctx->sqo_wait, &wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07006079 continue;
6080 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006081 if (signal_pending(current))
6082 flush_signals(current);
6083 schedule();
6084 finish_wait(&ctx->sqo_wait, &wait);
6085
Hristo Venev75b28af2019-08-26 17:23:46 +00006086 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Xiaoguang Wangd4ae2712020-05-20 21:24:35 +08006087 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006088 continue;
6089 }
6090 finish_wait(&ctx->sqo_wait, &wait);
6091
Hristo Venev75b28af2019-08-26 17:23:46 +00006092 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006093 }
6094
Jens Axboe8a4955f2019-12-09 14:52:35 -07006095 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang6b668c92020-05-20 15:35:03 +08006096 if (likely(!percpu_ref_is_dying(&ctx->refs)))
6097 ret = io_submit_sqes(ctx, to_submit, NULL, -1);
Jens Axboe8a4955f2019-12-09 14:52:35 -07006098 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006099 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006100 }
6101
Jens Axboeb41e9852020-02-17 09:52:41 -07006102 if (current->task_works)
6103 task_work_run();
6104
Jens Axboe6c271ce2019-01-10 11:22:30 -07006105 set_fs(old_fs);
Pavel Begunkovbf9c2f12020-04-12 02:05:02 +03006106 io_sq_thread_drop_mm(ctx);
Jens Axboe181e4482019-11-25 08:52:30 -07006107 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006108
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006109 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006110
Jens Axboe6c271ce2019-01-10 11:22:30 -07006111 return 0;
6112}
6113
Jens Axboebda52162019-09-24 13:47:15 -06006114struct io_wait_queue {
6115 struct wait_queue_entry wq;
6116 struct io_ring_ctx *ctx;
6117 unsigned to_wait;
6118 unsigned nr_timeouts;
6119};
6120
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006121static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06006122{
6123 struct io_ring_ctx *ctx = iowq->ctx;
6124
6125 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006126 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006127 * started waiting. For timeouts, we always want to return to userspace,
6128 * regardless of event count.
6129 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006130 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006131 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6132}
6133
6134static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6135 int wake_flags, void *key)
6136{
6137 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6138 wq);
6139
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006140 /* use noflush == true, as we can't safely rely on locking context */
6141 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06006142 return -1;
6143
6144 return autoremove_wake_function(curr, mode, wake_flags, key);
6145}
6146
Jens Axboe2b188cc2019-01-07 10:46:33 -07006147/*
6148 * Wait until events become available, if we don't already have some. The
6149 * application must reap them itself, as they reside on the shared cq ring.
6150 */
6151static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
6152 const sigset_t __user *sig, size_t sigsz)
6153{
Jens Axboebda52162019-09-24 13:47:15 -06006154 struct io_wait_queue iowq = {
6155 .wq = {
6156 .private = current,
6157 .func = io_wake_function,
6158 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6159 },
6160 .ctx = ctx,
6161 .to_wait = min_events,
6162 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006163 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006164 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006165
Jens Axboeb41e9852020-02-17 09:52:41 -07006166 do {
6167 if (io_cqring_events(ctx, false) >= min_events)
6168 return 0;
6169 if (!current->task_works)
6170 break;
6171 task_work_run();
6172 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006173
6174 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006175#ifdef CONFIG_COMPAT
6176 if (in_compat_syscall())
6177 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006178 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006179 else
6180#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006181 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006182
Jens Axboe2b188cc2019-01-07 10:46:33 -07006183 if (ret)
6184 return ret;
6185 }
6186
Jens Axboebda52162019-09-24 13:47:15 -06006187 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006188 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06006189 do {
6190 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
6191 TASK_INTERRUPTIBLE);
Jens Axboeb41e9852020-02-17 09:52:41 -07006192 if (current->task_works)
6193 task_work_run();
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006194 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06006195 break;
6196 schedule();
6197 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006198 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06006199 break;
6200 }
6201 } while (1);
6202 finish_wait(&ctx->wait, &iowq.wq);
6203
Jackie Liue9ffa5c2019-10-29 11:16:42 +08006204 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006205
Hristo Venev75b28af2019-08-26 17:23:46 +00006206 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006207}
6208
Jens Axboe6b063142019-01-10 22:13:58 -07006209static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
6210{
6211#if defined(CONFIG_UNIX)
6212 if (ctx->ring_sock) {
6213 struct sock *sock = ctx->ring_sock->sk;
6214 struct sk_buff *skb;
6215
6216 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
6217 kfree_skb(skb);
6218 }
6219#else
6220 int i;
6221
Jens Axboe65e19f52019-10-26 07:20:21 -06006222 for (i = 0; i < ctx->nr_user_files; i++) {
6223 struct file *file;
6224
6225 file = io_file_from_index(ctx, i);
6226 if (file)
6227 fput(file);
6228 }
Jens Axboe6b063142019-01-10 22:13:58 -07006229#endif
6230}
6231
Jens Axboe05f3fb32019-12-09 11:22:50 -07006232static void io_file_ref_kill(struct percpu_ref *ref)
6233{
6234 struct fixed_file_data *data;
6235
6236 data = container_of(ref, struct fixed_file_data, refs);
6237 complete(&data->done);
6238}
6239
Jens Axboe6b063142019-01-10 22:13:58 -07006240static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
6241{
Jens Axboe05f3fb32019-12-09 11:22:50 -07006242 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006243 struct fixed_file_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06006244 unsigned nr_tables, i;
6245
Jens Axboe05f3fb32019-12-09 11:22:50 -07006246 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07006247 return -ENXIO;
6248
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006249 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006250 if (!list_empty(&data->ref_list))
6251 ref_node = list_first_entry(&data->ref_list,
6252 struct fixed_file_ref_node, node);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006253 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006254 if (ref_node)
6255 percpu_ref_kill(&ref_node->refs);
6256
6257 percpu_ref_kill(&data->refs);
6258
6259 /* wait for all refs nodes to complete */
Jens Axboe4a38aed22020-05-14 17:21:15 -06006260 flush_delayed_work(&ctx->file_put_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07006261 wait_for_completion(&data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006262
Jens Axboe6b063142019-01-10 22:13:58 -07006263 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06006264 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
6265 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006266 kfree(data->table[i].files);
6267 kfree(data->table);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006268 percpu_ref_exit(&data->refs);
6269 kfree(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006270 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006271 ctx->nr_user_files = 0;
6272 return 0;
6273}
6274
Jens Axboe6c271ce2019-01-10 11:22:30 -07006275static void io_sq_thread_stop(struct io_ring_ctx *ctx)
6276{
6277 if (ctx->sqo_thread) {
Jens Axboe0f158b42020-05-14 17:18:39 -06006278 wait_for_completion(&ctx->sq_thread_comp);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006279 /*
6280 * The park is a bit of a work-around, without it we get
6281 * warning spews on shutdown with SQPOLL set and affinity
6282 * set to a single CPU.
6283 */
Jens Axboe06058632019-04-13 09:26:03 -06006284 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006285 kthread_stop(ctx->sqo_thread);
6286 ctx->sqo_thread = NULL;
6287 }
6288}
6289
Jens Axboe6b063142019-01-10 22:13:58 -07006290static void io_finish_async(struct io_ring_ctx *ctx)
6291{
Jens Axboe6c271ce2019-01-10 11:22:30 -07006292 io_sq_thread_stop(ctx);
6293
Jens Axboe561fb042019-10-24 07:25:42 -06006294 if (ctx->io_wq) {
6295 io_wq_destroy(ctx->io_wq);
6296 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07006297 }
6298}
6299
6300#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07006301/*
6302 * Ensure the UNIX gc is aware of our file set, so we are certain that
6303 * the io_uring can be safely unregistered on process exit, even if we have
6304 * loops in the file referencing.
6305 */
6306static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
6307{
6308 struct sock *sk = ctx->ring_sock->sk;
6309 struct scm_fp_list *fpl;
6310 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06006311 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07006312
Jens Axboe6b063142019-01-10 22:13:58 -07006313 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
6314 if (!fpl)
6315 return -ENOMEM;
6316
6317 skb = alloc_skb(0, GFP_KERNEL);
6318 if (!skb) {
6319 kfree(fpl);
6320 return -ENOMEM;
6321 }
6322
6323 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07006324
Jens Axboe08a45172019-10-03 08:11:03 -06006325 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07006326 fpl->user = get_uid(ctx->user);
6327 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006328 struct file *file = io_file_from_index(ctx, i + offset);
6329
6330 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06006331 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06006332 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06006333 unix_inflight(fpl->user, fpl->fp[nr_files]);
6334 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07006335 }
6336
Jens Axboe08a45172019-10-03 08:11:03 -06006337 if (nr_files) {
6338 fpl->max = SCM_MAX_FD;
6339 fpl->count = nr_files;
6340 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006341 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06006342 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
6343 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07006344
Jens Axboe08a45172019-10-03 08:11:03 -06006345 for (i = 0; i < nr_files; i++)
6346 fput(fpl->fp[i]);
6347 } else {
6348 kfree_skb(skb);
6349 kfree(fpl);
6350 }
Jens Axboe6b063142019-01-10 22:13:58 -07006351
6352 return 0;
6353}
6354
6355/*
6356 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
6357 * causes regular reference counting to break down. We rely on the UNIX
6358 * garbage collection to take care of this problem for us.
6359 */
6360static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6361{
6362 unsigned left, total;
6363 int ret = 0;
6364
6365 total = 0;
6366 left = ctx->nr_user_files;
6367 while (left) {
6368 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07006369
6370 ret = __io_sqe_files_scm(ctx, this_files, total);
6371 if (ret)
6372 break;
6373 left -= this_files;
6374 total += this_files;
6375 }
6376
6377 if (!ret)
6378 return 0;
6379
6380 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006381 struct file *file = io_file_from_index(ctx, total);
6382
6383 if (file)
6384 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07006385 total++;
6386 }
6387
6388 return ret;
6389}
6390#else
6391static int io_sqe_files_scm(struct io_ring_ctx *ctx)
6392{
6393 return 0;
6394}
6395#endif
6396
Jens Axboe65e19f52019-10-26 07:20:21 -06006397static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
6398 unsigned nr_files)
6399{
6400 int i;
6401
6402 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006403 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006404 unsigned this_files;
6405
6406 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
6407 table->files = kcalloc(this_files, sizeof(struct file *),
6408 GFP_KERNEL);
6409 if (!table->files)
6410 break;
6411 nr_files -= this_files;
6412 }
6413
6414 if (i == nr_tables)
6415 return 0;
6416
6417 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006418 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06006419 kfree(table->files);
6420 }
6421 return 1;
6422}
6423
Jens Axboe05f3fb32019-12-09 11:22:50 -07006424static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06006425{
6426#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06006427 struct sock *sock = ctx->ring_sock->sk;
6428 struct sk_buff_head list, *head = &sock->sk_receive_queue;
6429 struct sk_buff *skb;
6430 int i;
6431
6432 __skb_queue_head_init(&list);
6433
6434 /*
6435 * Find the skb that holds this file in its SCM_RIGHTS. When found,
6436 * remove this entry and rearrange the file array.
6437 */
6438 skb = skb_dequeue(head);
6439 while (skb) {
6440 struct scm_fp_list *fp;
6441
6442 fp = UNIXCB(skb).fp;
6443 for (i = 0; i < fp->count; i++) {
6444 int left;
6445
6446 if (fp->fp[i] != file)
6447 continue;
6448
6449 unix_notinflight(fp->user, fp->fp[i]);
6450 left = fp->count - 1 - i;
6451 if (left) {
6452 memmove(&fp->fp[i], &fp->fp[i + 1],
6453 left * sizeof(struct file *));
6454 }
6455 fp->count--;
6456 if (!fp->count) {
6457 kfree_skb(skb);
6458 skb = NULL;
6459 } else {
6460 __skb_queue_tail(&list, skb);
6461 }
6462 fput(file);
6463 file = NULL;
6464 break;
6465 }
6466
6467 if (!file)
6468 break;
6469
6470 __skb_queue_tail(&list, skb);
6471
6472 skb = skb_dequeue(head);
6473 }
6474
6475 if (skb_peek(&list)) {
6476 spin_lock_irq(&head->lock);
6477 while ((skb = __skb_dequeue(&list)) != NULL)
6478 __skb_queue_tail(head, skb);
6479 spin_unlock_irq(&head->lock);
6480 }
6481#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07006482 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06006483#endif
6484}
6485
Jens Axboe05f3fb32019-12-09 11:22:50 -07006486struct io_file_put {
Xiaoguang Wang05589552020-03-31 14:05:18 +08006487 struct list_head list;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006488 struct file *file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006489};
6490
Jens Axboe4a38aed22020-05-14 17:21:15 -06006491static void __io_file_put_work(struct fixed_file_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006492{
Jens Axboe4a38aed22020-05-14 17:21:15 -06006493 struct fixed_file_data *file_data = ref_node->file_data;
6494 struct io_ring_ctx *ctx = file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006495 struct io_file_put *pfile, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006496
6497 list_for_each_entry_safe(pfile, tmp, &ref_node->file_list, list) {
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006498 list_del(&pfile->list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006499 io_ring_file_put(ctx, pfile->file);
6500 kfree(pfile);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006501 }
6502
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006503 spin_lock(&file_data->lock);
6504 list_del(&ref_node->node);
6505 spin_unlock(&file_data->lock);
Jens Axboe2faf8522020-02-04 19:54:55 -07006506
Xiaoguang Wang05589552020-03-31 14:05:18 +08006507 percpu_ref_exit(&ref_node->refs);
6508 kfree(ref_node);
6509 percpu_ref_put(&file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006510}
6511
Jens Axboe4a38aed22020-05-14 17:21:15 -06006512static void io_file_put_work(struct work_struct *work)
6513{
6514 struct io_ring_ctx *ctx;
6515 struct llist_node *node;
6516
6517 ctx = container_of(work, struct io_ring_ctx, file_put_work.work);
6518 node = llist_del_all(&ctx->file_put_llist);
6519
6520 while (node) {
6521 struct fixed_file_ref_node *ref_node;
6522 struct llist_node *next = node->next;
6523
6524 ref_node = llist_entry(node, struct fixed_file_ref_node, llist);
6525 __io_file_put_work(ref_node);
6526 node = next;
6527 }
6528}
6529
Jens Axboe05f3fb32019-12-09 11:22:50 -07006530static void io_file_data_ref_zero(struct percpu_ref *ref)
6531{
Xiaoguang Wang05589552020-03-31 14:05:18 +08006532 struct fixed_file_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06006533 struct io_ring_ctx *ctx;
6534 bool first_add;
6535 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006536
Xiaoguang Wang05589552020-03-31 14:05:18 +08006537 ref_node = container_of(ref, struct fixed_file_ref_node, refs);
Jens Axboe4a38aed22020-05-14 17:21:15 -06006538 ctx = ref_node->file_data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006539
Jens Axboe4a38aed22020-05-14 17:21:15 -06006540 if (percpu_ref_is_dying(&ctx->file_data->refs))
6541 delay = 0;
6542
6543 first_add = llist_add(&ref_node->llist, &ctx->file_put_llist);
6544 if (!delay)
6545 mod_delayed_work(system_wq, &ctx->file_put_work, 0);
6546 else if (first_add)
6547 queue_delayed_work(system_wq, &ctx->file_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006548}
6549
6550static struct fixed_file_ref_node *alloc_fixed_file_ref_node(
6551 struct io_ring_ctx *ctx)
6552{
6553 struct fixed_file_ref_node *ref_node;
6554
6555 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
6556 if (!ref_node)
6557 return ERR_PTR(-ENOMEM);
6558
6559 if (percpu_ref_init(&ref_node->refs, io_file_data_ref_zero,
6560 0, GFP_KERNEL)) {
6561 kfree(ref_node);
6562 return ERR_PTR(-ENOMEM);
6563 }
6564 INIT_LIST_HEAD(&ref_node->node);
6565 INIT_LIST_HEAD(&ref_node->file_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006566 ref_node->file_data = ctx->file_data;
6567 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006568}
6569
6570static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node)
6571{
6572 percpu_ref_exit(&ref_node->refs);
6573 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006574}
6575
6576static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
6577 unsigned nr_args)
6578{
6579 __s32 __user *fds = (__s32 __user *) arg;
6580 unsigned nr_tables;
6581 struct file *file;
6582 int fd, ret = 0;
6583 unsigned i;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006584 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006585
6586 if (ctx->file_data)
6587 return -EBUSY;
6588 if (!nr_args)
6589 return -EINVAL;
6590 if (nr_args > IORING_MAX_FIXED_FILES)
6591 return -EMFILE;
6592
6593 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
6594 if (!ctx->file_data)
6595 return -ENOMEM;
6596 ctx->file_data->ctx = ctx;
6597 init_completion(&ctx->file_data->done);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006598 INIT_LIST_HEAD(&ctx->file_data->ref_list);
Xiaoguang Wangf7fe9342020-04-07 20:02:31 +08006599 spin_lock_init(&ctx->file_data->lock);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006600
6601 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
6602 ctx->file_data->table = kcalloc(nr_tables,
6603 sizeof(struct fixed_file_table),
6604 GFP_KERNEL);
6605 if (!ctx->file_data->table) {
6606 kfree(ctx->file_data);
6607 ctx->file_data = NULL;
6608 return -ENOMEM;
6609 }
6610
Xiaoguang Wang05589552020-03-31 14:05:18 +08006611 if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006612 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
6613 kfree(ctx->file_data->table);
6614 kfree(ctx->file_data);
6615 ctx->file_data = NULL;
6616 return -ENOMEM;
6617 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006618
6619 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
6620 percpu_ref_exit(&ctx->file_data->refs);
6621 kfree(ctx->file_data->table);
6622 kfree(ctx->file_data);
6623 ctx->file_data = NULL;
6624 return -ENOMEM;
6625 }
6626
6627 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
6628 struct fixed_file_table *table;
6629 unsigned index;
6630
6631 ret = -EFAULT;
6632 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
6633 break;
6634 /* allow sparse sets */
6635 if (fd == -1) {
6636 ret = 0;
6637 continue;
6638 }
6639
6640 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6641 index = i & IORING_FILE_TABLE_MASK;
6642 file = fget(fd);
6643
6644 ret = -EBADF;
6645 if (!file)
6646 break;
6647
6648 /*
6649 * Don't allow io_uring instances to be registered. If UNIX
6650 * isn't enabled, then this causes a reference cycle and this
6651 * instance can never get freed. If UNIX is enabled we'll
6652 * handle it just fine, but there's still no point in allowing
6653 * a ring fd as it doesn't support regular read/write anyway.
6654 */
6655 if (file->f_op == &io_uring_fops) {
6656 fput(file);
6657 break;
6658 }
6659 ret = 0;
6660 table->files[index] = file;
6661 }
6662
6663 if (ret) {
6664 for (i = 0; i < ctx->nr_user_files; i++) {
6665 file = io_file_from_index(ctx, i);
6666 if (file)
6667 fput(file);
6668 }
6669 for (i = 0; i < nr_tables; i++)
6670 kfree(ctx->file_data->table[i].files);
6671
6672 kfree(ctx->file_data->table);
6673 kfree(ctx->file_data);
6674 ctx->file_data = NULL;
6675 ctx->nr_user_files = 0;
6676 return ret;
6677 }
6678
6679 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006680 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006681 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006682 return ret;
6683 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006684
Xiaoguang Wang05589552020-03-31 14:05:18 +08006685 ref_node = alloc_fixed_file_ref_node(ctx);
6686 if (IS_ERR(ref_node)) {
6687 io_sqe_files_unregister(ctx);
6688 return PTR_ERR(ref_node);
6689 }
6690
6691 ctx->file_data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006692 spin_lock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006693 list_add(&ref_node->node, &ctx->file_data->ref_list);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006694 spin_unlock(&ctx->file_data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006695 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006696 return ret;
6697}
6698
Jens Axboec3a31e62019-10-03 13:59:56 -06006699static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
6700 int index)
6701{
6702#if defined(CONFIG_UNIX)
6703 struct sock *sock = ctx->ring_sock->sk;
6704 struct sk_buff_head *head = &sock->sk_receive_queue;
6705 struct sk_buff *skb;
6706
6707 /*
6708 * See if we can merge this file into an existing skb SCM_RIGHTS
6709 * file set. If there's no room, fall back to allocating a new skb
6710 * and filling it in.
6711 */
6712 spin_lock_irq(&head->lock);
6713 skb = skb_peek(head);
6714 if (skb) {
6715 struct scm_fp_list *fpl = UNIXCB(skb).fp;
6716
6717 if (fpl->count < SCM_MAX_FD) {
6718 __skb_unlink(skb, head);
6719 spin_unlock_irq(&head->lock);
6720 fpl->fp[fpl->count] = get_file(file);
6721 unix_inflight(fpl->user, fpl->fp[fpl->count]);
6722 fpl->count++;
6723 spin_lock_irq(&head->lock);
6724 __skb_queue_head(head, skb);
6725 } else {
6726 skb = NULL;
6727 }
6728 }
6729 spin_unlock_irq(&head->lock);
6730
6731 if (skb) {
6732 fput(file);
6733 return 0;
6734 }
6735
6736 return __io_sqe_files_scm(ctx, 1, index);
6737#else
6738 return 0;
6739#endif
6740}
6741
Hillf Dantona5318d32020-03-23 17:47:15 +08006742static int io_queue_file_removal(struct fixed_file_data *data,
Xiaoguang Wang05589552020-03-31 14:05:18 +08006743 struct file *file)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006744{
Hillf Dantona5318d32020-03-23 17:47:15 +08006745 struct io_file_put *pfile;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006746 struct percpu_ref *refs = data->cur_refs;
6747 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006748
Jens Axboe05f3fb32019-12-09 11:22:50 -07006749 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
Hillf Dantona5318d32020-03-23 17:47:15 +08006750 if (!pfile)
6751 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006752
Xiaoguang Wang05589552020-03-31 14:05:18 +08006753 ref_node = container_of(refs, struct fixed_file_ref_node, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006754 pfile->file = file;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006755 list_add(&pfile->list, &ref_node->file_list);
6756
Hillf Dantona5318d32020-03-23 17:47:15 +08006757 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006758}
6759
6760static int __io_sqe_files_update(struct io_ring_ctx *ctx,
6761 struct io_uring_files_update *up,
6762 unsigned nr_args)
6763{
6764 struct fixed_file_data *data = ctx->file_data;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006765 struct fixed_file_ref_node *ref_node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006766 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006767 __s32 __user *fds;
6768 int fd, i, err;
6769 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006770 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06006771
Jens Axboe05f3fb32019-12-09 11:22:50 -07006772 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06006773 return -EOVERFLOW;
6774 if (done > ctx->nr_user_files)
6775 return -EINVAL;
6776
Xiaoguang Wang05589552020-03-31 14:05:18 +08006777 ref_node = alloc_fixed_file_ref_node(ctx);
6778 if (IS_ERR(ref_node))
6779 return PTR_ERR(ref_node);
6780
Jens Axboec3a31e62019-10-03 13:59:56 -06006781 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006782 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06006783 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06006784 struct fixed_file_table *table;
6785 unsigned index;
6786
Jens Axboec3a31e62019-10-03 13:59:56 -06006787 err = 0;
6788 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
6789 err = -EFAULT;
6790 break;
6791 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006792 i = array_index_nospec(up->offset, ctx->nr_user_files);
6793 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06006794 index = i & IORING_FILE_TABLE_MASK;
6795 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006796 file = io_file_from_index(ctx, index);
Hillf Dantona5318d32020-03-23 17:47:15 +08006797 err = io_queue_file_removal(data, file);
6798 if (err)
6799 break;
Jens Axboe65e19f52019-10-26 07:20:21 -06006800 table->files[index] = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08006801 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06006802 }
6803 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06006804 file = fget(fd);
6805 if (!file) {
6806 err = -EBADF;
6807 break;
6808 }
6809 /*
6810 * Don't allow io_uring instances to be registered. If
6811 * UNIX isn't enabled, then this causes a reference
6812 * cycle and this instance can never get freed. If UNIX
6813 * is enabled we'll handle it just fine, but there's
6814 * still no point in allowing a ring fd as it doesn't
6815 * support regular read/write anyway.
6816 */
6817 if (file->f_op == &io_uring_fops) {
6818 fput(file);
6819 err = -EBADF;
6820 break;
6821 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006822 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006823 err = io_sqe_file_register(ctx, file, i);
6824 if (err)
6825 break;
6826 }
6827 nr_args--;
6828 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006829 up->offset++;
6830 }
6831
Xiaoguang Wang05589552020-03-31 14:05:18 +08006832 if (needs_switch) {
6833 percpu_ref_kill(data->cur_refs);
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006834 spin_lock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006835 list_add(&ref_node->node, &data->ref_list);
6836 data->cur_refs = &ref_node->refs;
Jens Axboe6a4d07c2020-05-15 14:30:38 -06006837 spin_unlock(&data->lock);
Xiaoguang Wang05589552020-03-31 14:05:18 +08006838 percpu_ref_get(&ctx->file_data->refs);
6839 } else
6840 destroy_fixed_file_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06006841
6842 return done ? done : err;
6843}
Xiaoguang Wang05589552020-03-31 14:05:18 +08006844
Jens Axboe05f3fb32019-12-09 11:22:50 -07006845static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6846 unsigned nr_args)
6847{
6848 struct io_uring_files_update up;
6849
6850 if (!ctx->file_data)
6851 return -ENXIO;
6852 if (!nr_args)
6853 return -EINVAL;
6854 if (copy_from_user(&up, arg, sizeof(up)))
6855 return -EFAULT;
6856 if (up.resv)
6857 return -EINVAL;
6858
6859 return __io_sqe_files_update(ctx, &up, nr_args);
6860}
Jens Axboec3a31e62019-10-03 13:59:56 -06006861
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006862static void io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07006863{
6864 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6865
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006866 /* Consider that io_steal_work() relies on this ref */
Jens Axboe7d723062019-11-12 22:31:31 -07006867 io_put_req(req);
6868}
6869
Pavel Begunkov24369c22020-01-28 03:15:48 +03006870static int io_init_wq_offload(struct io_ring_ctx *ctx,
6871 struct io_uring_params *p)
6872{
6873 struct io_wq_data data;
6874 struct fd f;
6875 struct io_ring_ctx *ctx_attach;
6876 unsigned int concurrency;
6877 int ret = 0;
6878
6879 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03006880 data.free_work = io_free_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03006881
6882 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6883 /* Do QD, or 4 * CPUS, whatever is smallest */
6884 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6885
6886 ctx->io_wq = io_wq_create(concurrency, &data);
6887 if (IS_ERR(ctx->io_wq)) {
6888 ret = PTR_ERR(ctx->io_wq);
6889 ctx->io_wq = NULL;
6890 }
6891 return ret;
6892 }
6893
6894 f = fdget(p->wq_fd);
6895 if (!f.file)
6896 return -EBADF;
6897
6898 if (f.file->f_op != &io_uring_fops) {
6899 ret = -EINVAL;
6900 goto out_fput;
6901 }
6902
6903 ctx_attach = f.file->private_data;
6904 /* @io_wq is protected by holding the fd */
6905 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6906 ret = -EINVAL;
6907 goto out_fput;
6908 }
6909
6910 ctx->io_wq = ctx_attach->io_wq;
6911out_fput:
6912 fdput(f);
6913 return ret;
6914}
6915
Jens Axboe6c271ce2019-01-10 11:22:30 -07006916static int io_sq_offload_start(struct io_ring_ctx *ctx,
6917 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006918{
6919 int ret;
6920
6921 mmgrab(current->mm);
6922 ctx->sqo_mm = current->mm;
6923
Jens Axboe6c271ce2019-01-10 11:22:30 -07006924 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006925 ret = -EPERM;
6926 if (!capable(CAP_SYS_ADMIN))
6927 goto err;
6928
Jens Axboe917257d2019-04-13 09:28:55 -06006929 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6930 if (!ctx->sq_thread_idle)
6931 ctx->sq_thread_idle = HZ;
6932
Jens Axboe6c271ce2019-01-10 11:22:30 -07006933 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006934 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006935
Jens Axboe917257d2019-04-13 09:28:55 -06006936 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006937 if (cpu >= nr_cpu_ids)
6938 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006939 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006940 goto err;
6941
Jens Axboe6c271ce2019-01-10 11:22:30 -07006942 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6943 ctx, cpu,
6944 "io_uring-sq");
6945 } else {
6946 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6947 "io_uring-sq");
6948 }
6949 if (IS_ERR(ctx->sqo_thread)) {
6950 ret = PTR_ERR(ctx->sqo_thread);
6951 ctx->sqo_thread = NULL;
6952 goto err;
6953 }
6954 wake_up_process(ctx->sqo_thread);
6955 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6956 /* Can't have SQ_AFF without SQPOLL */
6957 ret = -EINVAL;
6958 goto err;
6959 }
6960
Pavel Begunkov24369c22020-01-28 03:15:48 +03006961 ret = io_init_wq_offload(ctx, p);
6962 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006963 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006964
6965 return 0;
6966err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006967 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006968 mmdrop(ctx->sqo_mm);
6969 ctx->sqo_mm = NULL;
6970 return ret;
6971}
6972
6973static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6974{
6975 atomic_long_sub(nr_pages, &user->locked_vm);
6976}
6977
6978static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6979{
6980 unsigned long page_limit, cur_pages, new_pages;
6981
6982 /* Don't allow more pages than we can safely lock */
6983 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6984
6985 do {
6986 cur_pages = atomic_long_read(&user->locked_vm);
6987 new_pages = cur_pages + nr_pages;
6988 if (new_pages > page_limit)
6989 return -ENOMEM;
6990 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6991 new_pages) != cur_pages);
6992
6993 return 0;
6994}
6995
6996static void io_mem_free(void *ptr)
6997{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006998 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006999
Mark Rutland52e04ef2019-04-30 17:30:21 +01007000 if (!ptr)
7001 return;
7002
7003 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007004 if (put_page_testzero(page))
7005 free_compound_page(page);
7006}
7007
7008static void *io_mem_alloc(size_t size)
7009{
7010 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
7011 __GFP_NORETRY;
7012
7013 return (void *) __get_free_pages(gfp_flags, get_order(size));
7014}
7015
Hristo Venev75b28af2019-08-26 17:23:46 +00007016static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
7017 size_t *sq_offset)
7018{
7019 struct io_rings *rings;
7020 size_t off, sq_array_size;
7021
7022 off = struct_size(rings, cqes, cq_entries);
7023 if (off == SIZE_MAX)
7024 return SIZE_MAX;
7025
7026#ifdef CONFIG_SMP
7027 off = ALIGN(off, SMP_CACHE_BYTES);
7028 if (off == 0)
7029 return SIZE_MAX;
7030#endif
7031
7032 sq_array_size = array_size(sizeof(u32), sq_entries);
7033 if (sq_array_size == SIZE_MAX)
7034 return SIZE_MAX;
7035
7036 if (check_add_overflow(off, sq_array_size, &off))
7037 return SIZE_MAX;
7038
7039 if (sq_offset)
7040 *sq_offset = off;
7041
7042 return off;
7043}
7044
Jens Axboe2b188cc2019-01-07 10:46:33 -07007045static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
7046{
Hristo Venev75b28af2019-08-26 17:23:46 +00007047 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007048
Hristo Venev75b28af2019-08-26 17:23:46 +00007049 pages = (size_t)1 << get_order(
7050 rings_size(sq_entries, cq_entries, NULL));
7051 pages += (size_t)1 << get_order(
7052 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07007053
Hristo Venev75b28af2019-08-26 17:23:46 +00007054 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007055}
7056
Jens Axboeedafcce2019-01-09 09:16:05 -07007057static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
7058{
7059 int i, j;
7060
7061 if (!ctx->user_bufs)
7062 return -ENXIO;
7063
7064 for (i = 0; i < ctx->nr_user_bufs; i++) {
7065 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7066
7067 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007068 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07007069
7070 if (ctx->account_mem)
7071 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007072 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007073 imu->nr_bvecs = 0;
7074 }
7075
7076 kfree(ctx->user_bufs);
7077 ctx->user_bufs = NULL;
7078 ctx->nr_user_bufs = 0;
7079 return 0;
7080}
7081
7082static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
7083 void __user *arg, unsigned index)
7084{
7085 struct iovec __user *src;
7086
7087#ifdef CONFIG_COMPAT
7088 if (ctx->compat) {
7089 struct compat_iovec __user *ciovs;
7090 struct compat_iovec ciov;
7091
7092 ciovs = (struct compat_iovec __user *) arg;
7093 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
7094 return -EFAULT;
7095
Jens Axboed55e5f52019-12-11 16:12:15 -07007096 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07007097 dst->iov_len = ciov.iov_len;
7098 return 0;
7099 }
7100#endif
7101 src = (struct iovec __user *) arg;
7102 if (copy_from_user(dst, &src[index], sizeof(*dst)))
7103 return -EFAULT;
7104 return 0;
7105}
7106
7107static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
7108 unsigned nr_args)
7109{
7110 struct vm_area_struct **vmas = NULL;
7111 struct page **pages = NULL;
7112 int i, j, got_pages = 0;
7113 int ret = -EINVAL;
7114
7115 if (ctx->user_bufs)
7116 return -EBUSY;
7117 if (!nr_args || nr_args > UIO_MAXIOV)
7118 return -EINVAL;
7119
7120 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
7121 GFP_KERNEL);
7122 if (!ctx->user_bufs)
7123 return -ENOMEM;
7124
7125 for (i = 0; i < nr_args; i++) {
7126 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
7127 unsigned long off, start, end, ubuf;
7128 int pret, nr_pages;
7129 struct iovec iov;
7130 size_t size;
7131
7132 ret = io_copy_iov(ctx, &iov, arg, i);
7133 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03007134 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07007135
7136 /*
7137 * Don't impose further limits on the size and buffer
7138 * constraints here, we'll -EINVAL later when IO is
7139 * submitted if they are wrong.
7140 */
7141 ret = -EFAULT;
7142 if (!iov.iov_base || !iov.iov_len)
7143 goto err;
7144
7145 /* arbitrary limit, but we need something */
7146 if (iov.iov_len > SZ_1G)
7147 goto err;
7148
7149 ubuf = (unsigned long) iov.iov_base;
7150 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
7151 start = ubuf >> PAGE_SHIFT;
7152 nr_pages = end - start;
7153
7154 if (ctx->account_mem) {
7155 ret = io_account_mem(ctx->user, nr_pages);
7156 if (ret)
7157 goto err;
7158 }
7159
7160 ret = 0;
7161 if (!pages || nr_pages > got_pages) {
7162 kfree(vmas);
7163 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007164 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07007165 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007166 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07007167 sizeof(struct vm_area_struct *),
7168 GFP_KERNEL);
7169 if (!pages || !vmas) {
7170 ret = -ENOMEM;
7171 if (ctx->account_mem)
7172 io_unaccount_mem(ctx->user, nr_pages);
7173 goto err;
7174 }
7175 got_pages = nr_pages;
7176 }
7177
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007178 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07007179 GFP_KERNEL);
7180 ret = -ENOMEM;
7181 if (!imu->bvec) {
7182 if (ctx->account_mem)
7183 io_unaccount_mem(ctx->user, nr_pages);
7184 goto err;
7185 }
7186
7187 ret = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007188 mmap_read_lock(current->mm);
John Hubbard2113b052020-01-30 22:13:13 -08007189 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07007190 FOLL_WRITE | FOLL_LONGTERM,
7191 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007192 if (pret == nr_pages) {
7193 /* don't support file backed memory */
7194 for (j = 0; j < nr_pages; j++) {
7195 struct vm_area_struct *vma = vmas[j];
7196
7197 if (vma->vm_file &&
7198 !is_file_hugepages(vma->vm_file)) {
7199 ret = -EOPNOTSUPP;
7200 break;
7201 }
7202 }
7203 } else {
7204 ret = pret < 0 ? pret : -EFAULT;
7205 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07007206 mmap_read_unlock(current->mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07007207 if (ret) {
7208 /*
7209 * if we did partial map, or found file backed vmas,
7210 * release any pages we did get
7211 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07007212 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08007213 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007214 if (ctx->account_mem)
7215 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007216 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07007217 goto err;
7218 }
7219
7220 off = ubuf & ~PAGE_MASK;
7221 size = iov.iov_len;
7222 for (j = 0; j < nr_pages; j++) {
7223 size_t vec_len;
7224
7225 vec_len = min_t(size_t, size, PAGE_SIZE - off);
7226 imu->bvec[j].bv_page = pages[j];
7227 imu->bvec[j].bv_len = vec_len;
7228 imu->bvec[j].bv_offset = off;
7229 off = 0;
7230 size -= vec_len;
7231 }
7232 /* store original address for later verification */
7233 imu->ubuf = ubuf;
7234 imu->len = iov.iov_len;
7235 imu->nr_bvecs = nr_pages;
7236
7237 ctx->nr_user_bufs++;
7238 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007239 kvfree(pages);
7240 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007241 return 0;
7242err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01007243 kvfree(pages);
7244 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07007245 io_sqe_buffer_unregister(ctx);
7246 return ret;
7247}
7248
Jens Axboe9b402842019-04-11 11:45:41 -06007249static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
7250{
7251 __s32 __user *fds = arg;
7252 int fd;
7253
7254 if (ctx->cq_ev_fd)
7255 return -EBUSY;
7256
7257 if (copy_from_user(&fd, fds, sizeof(*fds)))
7258 return -EFAULT;
7259
7260 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
7261 if (IS_ERR(ctx->cq_ev_fd)) {
7262 int ret = PTR_ERR(ctx->cq_ev_fd);
7263 ctx->cq_ev_fd = NULL;
7264 return ret;
7265 }
7266
7267 return 0;
7268}
7269
7270static int io_eventfd_unregister(struct io_ring_ctx *ctx)
7271{
7272 if (ctx->cq_ev_fd) {
7273 eventfd_ctx_put(ctx->cq_ev_fd);
7274 ctx->cq_ev_fd = NULL;
7275 return 0;
7276 }
7277
7278 return -ENXIO;
7279}
7280
Jens Axboe5a2e7452020-02-23 16:23:11 -07007281static int __io_destroy_buffers(int id, void *p, void *data)
7282{
7283 struct io_ring_ctx *ctx = data;
7284 struct io_buffer *buf = p;
7285
Jens Axboe067524e2020-03-02 16:32:28 -07007286 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007287 return 0;
7288}
7289
7290static void io_destroy_buffers(struct io_ring_ctx *ctx)
7291{
7292 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
7293 idr_destroy(&ctx->io_buffer_idr);
7294}
7295
Jens Axboe2b188cc2019-01-07 10:46:33 -07007296static void io_ring_ctx_free(struct io_ring_ctx *ctx)
7297{
Jens Axboe6b063142019-01-10 22:13:58 -07007298 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007299 if (ctx->sqo_mm)
7300 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07007301
7302 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07007303 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07007304 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06007305 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07007306 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07007307 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07007308
Jens Axboe2b188cc2019-01-07 10:46:33 -07007309#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07007310 if (ctx->ring_sock) {
7311 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007312 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07007313 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007314#endif
7315
Hristo Venev75b28af2019-08-26 17:23:46 +00007316 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007317 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007318
7319 percpu_ref_exit(&ctx->refs);
7320 if (ctx->account_mem)
7321 io_unaccount_mem(ctx->user,
7322 ring_pages(ctx->sq_entries, ctx->cq_entries));
7323 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07007324 put_cred(ctx->creds);
Jens Axboe78076bb2019-12-04 19:56:40 -07007325 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07007326 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007327 kfree(ctx);
7328}
7329
7330static __poll_t io_uring_poll(struct file *file, poll_table *wait)
7331{
7332 struct io_ring_ctx *ctx = file->private_data;
7333 __poll_t mask = 0;
7334
7335 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02007336 /*
7337 * synchronizes with barrier from wq_has_sleeper call in
7338 * io_commit_cqring
7339 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07007340 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00007341 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
7342 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007343 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01007344 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007345 mask |= EPOLLIN | EPOLLRDNORM;
7346
7347 return mask;
7348}
7349
7350static int io_uring_fasync(int fd, struct file *file, int on)
7351{
7352 struct io_ring_ctx *ctx = file->private_data;
7353
7354 return fasync_helper(fd, file, on, &ctx->cq_fasync);
7355}
7356
Jens Axboe071698e2020-01-28 10:04:42 -07007357static int io_remove_personalities(int id, void *p, void *data)
7358{
7359 struct io_ring_ctx *ctx = data;
7360 const struct cred *cred;
7361
7362 cred = idr_remove(&ctx->personality_idr, id);
7363 if (cred)
7364 put_cred(cred);
7365 return 0;
7366}
7367
Jens Axboe85faa7b2020-04-09 18:14:00 -06007368static void io_ring_exit_work(struct work_struct *work)
7369{
7370 struct io_ring_ctx *ctx;
7371
7372 ctx = container_of(work, struct io_ring_ctx, exit_work);
7373 if (ctx->rings)
7374 io_cqring_overflow_flush(ctx, true);
7375
Jens Axboe0f158b42020-05-14 17:18:39 -06007376 wait_for_completion(&ctx->ref_comp);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007377 io_ring_ctx_free(ctx);
7378}
7379
Jens Axboe2b188cc2019-01-07 10:46:33 -07007380static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
7381{
7382 mutex_lock(&ctx->uring_lock);
7383 percpu_ref_kill(&ctx->refs);
7384 mutex_unlock(&ctx->uring_lock);
7385
Jens Axboe5262f562019-09-17 12:26:57 -06007386 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07007387 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06007388
7389 if (ctx->io_wq)
7390 io_wq_cancel_all(ctx->io_wq);
7391
Jens Axboedef596e2019-01-09 08:59:42 -07007392 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07007393 /* if we failed setting up the ctx, we might not have any rings */
7394 if (ctx->rings)
7395 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07007396 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe85faa7b2020-04-09 18:14:00 -06007397 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
7398 queue_work(system_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007399}
7400
7401static int io_uring_release(struct inode *inode, struct file *file)
7402{
7403 struct io_ring_ctx *ctx = file->private_data;
7404
7405 file->private_data = NULL;
7406 io_ring_ctx_wait_and_kill(ctx);
7407 return 0;
7408}
7409
Jens Axboefcb323c2019-10-24 12:39:47 -06007410static void io_uring_cancel_files(struct io_ring_ctx *ctx,
7411 struct files_struct *files)
7412{
Jens Axboefcb323c2019-10-24 12:39:47 -06007413 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007414 struct io_kiocb *cancel_req = NULL, *req;
7415 DEFINE_WAIT(wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007416
7417 spin_lock_irq(&ctx->inflight_lock);
7418 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07007419 if (req->work.files != files)
7420 continue;
7421 /* req is being completed, ignore */
7422 if (!refcount_inc_not_zero(&req->refs))
7423 continue;
7424 cancel_req = req;
7425 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06007426 }
Jens Axboe768134d2019-11-10 20:30:53 -07007427 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007428 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07007429 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06007430 spin_unlock_irq(&ctx->inflight_lock);
7431
Jens Axboe768134d2019-11-10 20:30:53 -07007432 /* We need to keep going until we don't find a matching req */
7433 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06007434 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08007435
Jens Axboe2ca10252020-02-13 17:17:35 -07007436 if (cancel_req->flags & REQ_F_OVERFLOW) {
7437 spin_lock_irq(&ctx->completion_lock);
7438 list_del(&cancel_req->list);
7439 cancel_req->flags &= ~REQ_F_OVERFLOW;
7440 if (list_empty(&ctx->cq_overflow_list)) {
7441 clear_bit(0, &ctx->sq_check_overflow);
7442 clear_bit(0, &ctx->cq_check_overflow);
7443 }
7444 spin_unlock_irq(&ctx->completion_lock);
7445
7446 WRITE_ONCE(ctx->rings->cq_overflow,
7447 atomic_inc_return(&ctx->cached_cq_overflow));
7448
7449 /*
7450 * Put inflight ref and overflow ref. If that's
7451 * all we had, then we're done with this request.
7452 */
7453 if (refcount_sub_and_test(2, &cancel_req->refs)) {
Pavel Begunkov4518a3c2020-05-26 20:34:02 +03007454 io_free_req(cancel_req);
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007455 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboe2ca10252020-02-13 17:17:35 -07007456 continue;
7457 }
Pavel Begunkov7b53d592020-05-30 14:19:15 +03007458 } else {
7459 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
7460 io_put_req(cancel_req);
Jens Axboe2ca10252020-02-13 17:17:35 -07007461 }
7462
Jens Axboefcb323c2019-10-24 12:39:47 -06007463 schedule();
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08007464 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06007465 }
7466}
7467
7468static int io_uring_flush(struct file *file, void *data)
7469{
7470 struct io_ring_ctx *ctx = file->private_data;
7471
7472 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07007473
7474 /*
7475 * If the task is going away, cancel work it may have pending
7476 */
7477 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
7478 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
7479
Jens Axboefcb323c2019-10-24 12:39:47 -06007480 return 0;
7481}
7482
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007483static void *io_uring_validate_mmap_request(struct file *file,
7484 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007485{
Jens Axboe2b188cc2019-01-07 10:46:33 -07007486 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007487 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007488 struct page *page;
7489 void *ptr;
7490
7491 switch (offset) {
7492 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00007493 case IORING_OFF_CQ_RING:
7494 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007495 break;
7496 case IORING_OFF_SQES:
7497 ptr = ctx->sq_sqes;
7498 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007499 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007500 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007501 }
7502
7503 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07007504 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007505 return ERR_PTR(-EINVAL);
7506
7507 return ptr;
7508}
7509
7510#ifdef CONFIG_MMU
7511
7512static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7513{
7514 size_t sz = vma->vm_end - vma->vm_start;
7515 unsigned long pfn;
7516 void *ptr;
7517
7518 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
7519 if (IS_ERR(ptr))
7520 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007521
7522 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
7523 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
7524}
7525
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007526#else /* !CONFIG_MMU */
7527
7528static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
7529{
7530 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
7531}
7532
7533static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
7534{
7535 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
7536}
7537
7538static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
7539 unsigned long addr, unsigned long len,
7540 unsigned long pgoff, unsigned long flags)
7541{
7542 void *ptr;
7543
7544 ptr = io_uring_validate_mmap_request(file, pgoff, len);
7545 if (IS_ERR(ptr))
7546 return PTR_ERR(ptr);
7547
7548 return (unsigned long) ptr;
7549}
7550
7551#endif /* !CONFIG_MMU */
7552
Jens Axboe2b188cc2019-01-07 10:46:33 -07007553SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
7554 u32, min_complete, u32, flags, const sigset_t __user *, sig,
7555 size_t, sigsz)
7556{
7557 struct io_ring_ctx *ctx;
7558 long ret = -EBADF;
7559 int submitted = 0;
7560 struct fd f;
7561
Jens Axboeb41e9852020-02-17 09:52:41 -07007562 if (current->task_works)
7563 task_work_run();
7564
Jens Axboe6c271ce2019-01-10 11:22:30 -07007565 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007566 return -EINVAL;
7567
7568 f = fdget(fd);
7569 if (!f.file)
7570 return -EBADF;
7571
7572 ret = -EOPNOTSUPP;
7573 if (f.file->f_op != &io_uring_fops)
7574 goto out_fput;
7575
7576 ret = -ENXIO;
7577 ctx = f.file->private_data;
7578 if (!percpu_ref_tryget(&ctx->refs))
7579 goto out_fput;
7580
Jens Axboe6c271ce2019-01-10 11:22:30 -07007581 /*
7582 * For SQ polling, the thread will do all submissions and completions.
7583 * Just return the requested submit count, and wake the thread if
7584 * we were asked to.
7585 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007586 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007587 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07007588 if (!list_empty_careful(&ctx->cq_overflow_list))
7589 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007590 if (flags & IORING_ENTER_SQ_WAKEUP)
7591 wake_up(&ctx->sqo_wait);
7592 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06007593 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007594 mutex_lock(&ctx->uring_lock);
Pavel Begunkov0cdaf762020-05-17 14:13:40 +03007595 submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007596 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007597
7598 if (submitted != to_submit)
7599 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007600 }
7601 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07007602 unsigned nr_events = 0;
7603
Jens Axboe2b188cc2019-01-07 10:46:33 -07007604 min_complete = min(min_complete, ctx->cq_entries);
7605
Xiaoguang Wang32b22442020-03-11 09:26:09 +08007606 /*
7607 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
7608 * space applications don't need to do io completion events
7609 * polling again, they can rely on io_sq_thread to do polling
7610 * work, which can reduce cpu usage and uring_lock contention.
7611 */
7612 if (ctx->flags & IORING_SETUP_IOPOLL &&
7613 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Jens Axboedef596e2019-01-09 08:59:42 -07007614 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07007615 } else {
7616 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
7617 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007618 }
7619
Pavel Begunkov7c504e652019-12-18 19:53:45 +03007620out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03007621 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007622out_fput:
7623 fdput(f);
7624 return submitted ? submitted : ret;
7625}
7626
Tobias Klauserbebdb652020-02-26 18:38:32 +01007627#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007628static int io_uring_show_cred(int id, void *p, void *data)
7629{
7630 const struct cred *cred = p;
7631 struct seq_file *m = data;
7632 struct user_namespace *uns = seq_user_ns(m);
7633 struct group_info *gi;
7634 kernel_cap_t cap;
7635 unsigned __capi;
7636 int g;
7637
7638 seq_printf(m, "%5d\n", id);
7639 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
7640 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
7641 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
7642 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
7643 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
7644 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
7645 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
7646 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
7647 seq_puts(m, "\n\tGroups:\t");
7648 gi = cred->group_info;
7649 for (g = 0; g < gi->ngroups; g++) {
7650 seq_put_decimal_ull(m, g ? " " : "",
7651 from_kgid_munged(uns, gi->gid[g]));
7652 }
7653 seq_puts(m, "\n\tCapEff:\t");
7654 cap = cred->cap_effective;
7655 CAP_FOR_EACH_U32(__capi)
7656 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
7657 seq_putc(m, '\n');
7658 return 0;
7659}
7660
7661static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
7662{
7663 int i;
7664
7665 mutex_lock(&ctx->uring_lock);
7666 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
7667 for (i = 0; i < ctx->nr_user_files; i++) {
7668 struct fixed_file_table *table;
7669 struct file *f;
7670
7671 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7672 f = table->files[i & IORING_FILE_TABLE_MASK];
7673 if (f)
7674 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
7675 else
7676 seq_printf(m, "%5u: <none>\n", i);
7677 }
7678 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
7679 for (i = 0; i < ctx->nr_user_bufs; i++) {
7680 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
7681
7682 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
7683 (unsigned int) buf->len);
7684 }
7685 if (!idr_is_empty(&ctx->personality_idr)) {
7686 seq_printf(m, "Personalities:\n");
7687 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
7688 }
Jens Axboed7718a92020-02-14 22:23:12 -07007689 seq_printf(m, "PollList:\n");
7690 spin_lock_irq(&ctx->completion_lock);
7691 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
7692 struct hlist_head *list = &ctx->cancel_hash[i];
7693 struct io_kiocb *req;
7694
7695 hlist_for_each_entry(req, list, hash_node)
7696 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
7697 req->task->task_works != NULL);
7698 }
7699 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07007700 mutex_unlock(&ctx->uring_lock);
7701}
7702
7703static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
7704{
7705 struct io_ring_ctx *ctx = f->private_data;
7706
7707 if (percpu_ref_tryget(&ctx->refs)) {
7708 __io_uring_show_fdinfo(ctx, m);
7709 percpu_ref_put(&ctx->refs);
7710 }
7711}
Tobias Klauserbebdb652020-02-26 18:38:32 +01007712#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07007713
Jens Axboe2b188cc2019-01-07 10:46:33 -07007714static const struct file_operations io_uring_fops = {
7715 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06007716 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07007717 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01007718#ifndef CONFIG_MMU
7719 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
7720 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
7721#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007722 .poll = io_uring_poll,
7723 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007724#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07007725 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01007726#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07007727};
7728
7729static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
7730 struct io_uring_params *p)
7731{
Hristo Venev75b28af2019-08-26 17:23:46 +00007732 struct io_rings *rings;
7733 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007734
Hristo Venev75b28af2019-08-26 17:23:46 +00007735 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
7736 if (size == SIZE_MAX)
7737 return -EOVERFLOW;
7738
7739 rings = io_mem_alloc(size);
7740 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007741 return -ENOMEM;
7742
Hristo Venev75b28af2019-08-26 17:23:46 +00007743 ctx->rings = rings;
7744 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
7745 rings->sq_ring_mask = p->sq_entries - 1;
7746 rings->cq_ring_mask = p->cq_entries - 1;
7747 rings->sq_ring_entries = p->sq_entries;
7748 rings->cq_ring_entries = p->cq_entries;
7749 ctx->sq_mask = rings->sq_ring_mask;
7750 ctx->cq_mask = rings->cq_ring_mask;
7751 ctx->sq_entries = rings->sq_ring_entries;
7752 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007753
7754 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07007755 if (size == SIZE_MAX) {
7756 io_mem_free(ctx->rings);
7757 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007758 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07007759 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007760
7761 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07007762 if (!ctx->sq_sqes) {
7763 io_mem_free(ctx->rings);
7764 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007765 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07007766 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007767
Jens Axboe2b188cc2019-01-07 10:46:33 -07007768 return 0;
7769}
7770
7771/*
7772 * Allocate an anonymous fd, this is what constitutes the application
7773 * visible backing of an io_uring instance. The application mmaps this
7774 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
7775 * we have to tie this fd to a socket for file garbage collection purposes.
7776 */
7777static int io_uring_get_fd(struct io_ring_ctx *ctx)
7778{
7779 struct file *file;
7780 int ret;
7781
7782#if defined(CONFIG_UNIX)
7783 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
7784 &ctx->ring_sock);
7785 if (ret)
7786 return ret;
7787#endif
7788
7789 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
7790 if (ret < 0)
7791 goto err;
7792
7793 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
7794 O_RDWR | O_CLOEXEC);
7795 if (IS_ERR(file)) {
7796 put_unused_fd(ret);
7797 ret = PTR_ERR(file);
7798 goto err;
7799 }
7800
7801#if defined(CONFIG_UNIX)
7802 ctx->ring_sock->file = file;
7803#endif
7804 fd_install(ret, file);
7805 return ret;
7806err:
7807#if defined(CONFIG_UNIX)
7808 sock_release(ctx->ring_sock);
7809 ctx->ring_sock = NULL;
7810#endif
7811 return ret;
7812}
7813
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007814static int io_uring_create(unsigned entries, struct io_uring_params *p,
7815 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007816{
7817 struct user_struct *user = NULL;
7818 struct io_ring_ctx *ctx;
7819 bool account_mem;
7820 int ret;
7821
Jens Axboe8110c1a2019-12-28 15:39:54 -07007822 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007823 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007824 if (entries > IORING_MAX_ENTRIES) {
7825 if (!(p->flags & IORING_SETUP_CLAMP))
7826 return -EINVAL;
7827 entries = IORING_MAX_ENTRIES;
7828 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007829
7830 /*
7831 * Use twice as many entries for the CQ ring. It's possible for the
7832 * application to drive a higher depth than the size of the SQ ring,
7833 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06007834 * some flexibility in overcommitting a bit. If the application has
7835 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
7836 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07007837 */
7838 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06007839 if (p->flags & IORING_SETUP_CQSIZE) {
7840 /*
7841 * If IORING_SETUP_CQSIZE is set, we do the same roundup
7842 * to a power-of-two, if it isn't already. We do NOT impose
7843 * any cq vs sq ring sizing.
7844 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07007845 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06007846 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07007847 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
7848 if (!(p->flags & IORING_SETUP_CLAMP))
7849 return -EINVAL;
7850 p->cq_entries = IORING_MAX_CQ_ENTRIES;
7851 }
Jens Axboe33a107f2019-10-04 12:10:03 -06007852 p->cq_entries = roundup_pow_of_two(p->cq_entries);
7853 } else {
7854 p->cq_entries = 2 * p->sq_entries;
7855 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007856
7857 user = get_uid(current_user());
7858 account_mem = !capable(CAP_IPC_LOCK);
7859
7860 if (account_mem) {
7861 ret = io_account_mem(user,
7862 ring_pages(p->sq_entries, p->cq_entries));
7863 if (ret) {
7864 free_uid(user);
7865 return ret;
7866 }
7867 }
7868
7869 ctx = io_ring_ctx_alloc(p);
7870 if (!ctx) {
7871 if (account_mem)
7872 io_unaccount_mem(user, ring_pages(p->sq_entries,
7873 p->cq_entries));
7874 free_uid(user);
7875 return -ENOMEM;
7876 }
7877 ctx->compat = in_compat_syscall();
7878 ctx->account_mem = account_mem;
7879 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007880 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007881
7882 ret = io_allocate_scq_urings(ctx, p);
7883 if (ret)
7884 goto err;
7885
Jens Axboe6c271ce2019-01-10 11:22:30 -07007886 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007887 if (ret)
7888 goto err;
7889
Jens Axboe2b188cc2019-01-07 10:46:33 -07007890 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007891 p->sq_off.head = offsetof(struct io_rings, sq.head);
7892 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7893 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7894 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7895 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7896 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7897 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007898
7899 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007900 p->cq_off.head = offsetof(struct io_rings, cq.head);
7901 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7902 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7903 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7904 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7905 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02007906 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06007907
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007908 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
7909 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7910 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
7911
7912 if (copy_to_user(params, p, sizeof(*p))) {
7913 ret = -EFAULT;
7914 goto err;
7915 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06007916 /*
7917 * Install ring fd as the very last thing, so we don't risk someone
7918 * having closed it before we finish setup
7919 */
7920 ret = io_uring_get_fd(ctx);
7921 if (ret < 0)
7922 goto err;
7923
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007924 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007925 return ret;
7926err:
7927 io_ring_ctx_wait_and_kill(ctx);
7928 return ret;
7929}
7930
7931/*
7932 * Sets up an aio uring context, and returns the fd. Applications asks for a
7933 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7934 * params structure passed in.
7935 */
7936static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7937{
7938 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007939 int i;
7940
7941 if (copy_from_user(&p, params, sizeof(p)))
7942 return -EFAULT;
7943 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7944 if (p.resv[i])
7945 return -EINVAL;
7946 }
7947
Jens Axboe6c271ce2019-01-10 11:22:30 -07007948 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007949 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007950 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007951 return -EINVAL;
7952
Xiaoguang Wang7f136572020-05-05 16:28:53 +08007953 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007954}
7955
7956SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7957 struct io_uring_params __user *, params)
7958{
7959 return io_uring_setup(entries, params);
7960}
7961
Jens Axboe66f4af92020-01-16 15:36:52 -07007962static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7963{
7964 struct io_uring_probe *p;
7965 size_t size;
7966 int i, ret;
7967
7968 size = struct_size(p, ops, nr_args);
7969 if (size == SIZE_MAX)
7970 return -EOVERFLOW;
7971 p = kzalloc(size, GFP_KERNEL);
7972 if (!p)
7973 return -ENOMEM;
7974
7975 ret = -EFAULT;
7976 if (copy_from_user(p, arg, size))
7977 goto out;
7978 ret = -EINVAL;
7979 if (memchr_inv(p, 0, size))
7980 goto out;
7981
7982 p->last_op = IORING_OP_LAST - 1;
7983 if (nr_args > IORING_OP_LAST)
7984 nr_args = IORING_OP_LAST;
7985
7986 for (i = 0; i < nr_args; i++) {
7987 p->ops[i].op = i;
7988 if (!io_op_defs[i].not_supported)
7989 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7990 }
7991 p->ops_len = i;
7992
7993 ret = 0;
7994 if (copy_to_user(arg, p, size))
7995 ret = -EFAULT;
7996out:
7997 kfree(p);
7998 return ret;
7999}
8000
Jens Axboe071698e2020-01-28 10:04:42 -07008001static int io_register_personality(struct io_ring_ctx *ctx)
8002{
8003 const struct cred *creds = get_current_cred();
8004 int id;
8005
8006 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
8007 USHRT_MAX, GFP_KERNEL);
8008 if (id < 0)
8009 put_cred(creds);
8010 return id;
8011}
8012
8013static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
8014{
8015 const struct cred *old_creds;
8016
8017 old_creds = idr_remove(&ctx->personality_idr, id);
8018 if (old_creds) {
8019 put_cred(old_creds);
8020 return 0;
8021 }
8022
8023 return -EINVAL;
8024}
8025
8026static bool io_register_op_must_quiesce(int op)
8027{
8028 switch (op) {
8029 case IORING_UNREGISTER_FILES:
8030 case IORING_REGISTER_FILES_UPDATE:
8031 case IORING_REGISTER_PROBE:
8032 case IORING_REGISTER_PERSONALITY:
8033 case IORING_UNREGISTER_PERSONALITY:
8034 return false;
8035 default:
8036 return true;
8037 }
8038}
8039
Jens Axboeedafcce2019-01-09 09:16:05 -07008040static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
8041 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06008042 __releases(ctx->uring_lock)
8043 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07008044{
8045 int ret;
8046
Jens Axboe35fa71a2019-04-22 10:23:23 -06008047 /*
8048 * We're inside the ring mutex, if the ref is already dying, then
8049 * someone else killed the ctx or is already going through
8050 * io_uring_register().
8051 */
8052 if (percpu_ref_is_dying(&ctx->refs))
8053 return -ENXIO;
8054
Jens Axboe071698e2020-01-28 10:04:42 -07008055 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008056 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06008057
Jens Axboe05f3fb32019-12-09 11:22:50 -07008058 /*
8059 * Drop uring mutex before waiting for references to exit. If
8060 * another thread is currently inside io_uring_enter() it might
8061 * need to grab the uring_lock to make progress. If we hold it
8062 * here across the drain wait, then we can deadlock. It's safe
8063 * to drop the mutex here, since no new references will come in
8064 * after we've killed the percpu ref.
8065 */
8066 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f158b42020-05-14 17:18:39 -06008067 ret = wait_for_completion_interruptible(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008068 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07008069 if (ret) {
8070 percpu_ref_resurrect(&ctx->refs);
8071 ret = -EINTR;
8072 goto out;
8073 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008074 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008075
8076 switch (opcode) {
8077 case IORING_REGISTER_BUFFERS:
8078 ret = io_sqe_buffer_register(ctx, arg, nr_args);
8079 break;
8080 case IORING_UNREGISTER_BUFFERS:
8081 ret = -EINVAL;
8082 if (arg || nr_args)
8083 break;
8084 ret = io_sqe_buffer_unregister(ctx);
8085 break;
Jens Axboe6b063142019-01-10 22:13:58 -07008086 case IORING_REGISTER_FILES:
8087 ret = io_sqe_files_register(ctx, arg, nr_args);
8088 break;
8089 case IORING_UNREGISTER_FILES:
8090 ret = -EINVAL;
8091 if (arg || nr_args)
8092 break;
8093 ret = io_sqe_files_unregister(ctx);
8094 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06008095 case IORING_REGISTER_FILES_UPDATE:
8096 ret = io_sqe_files_update(ctx, arg, nr_args);
8097 break;
Jens Axboe9b402842019-04-11 11:45:41 -06008098 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07008099 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06008100 ret = -EINVAL;
8101 if (nr_args != 1)
8102 break;
8103 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07008104 if (ret)
8105 break;
8106 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
8107 ctx->eventfd_async = 1;
8108 else
8109 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06008110 break;
8111 case IORING_UNREGISTER_EVENTFD:
8112 ret = -EINVAL;
8113 if (arg || nr_args)
8114 break;
8115 ret = io_eventfd_unregister(ctx);
8116 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07008117 case IORING_REGISTER_PROBE:
8118 ret = -EINVAL;
8119 if (!arg || nr_args > 256)
8120 break;
8121 ret = io_probe(ctx, arg, nr_args);
8122 break;
Jens Axboe071698e2020-01-28 10:04:42 -07008123 case IORING_REGISTER_PERSONALITY:
8124 ret = -EINVAL;
8125 if (arg || nr_args)
8126 break;
8127 ret = io_register_personality(ctx);
8128 break;
8129 case IORING_UNREGISTER_PERSONALITY:
8130 ret = -EINVAL;
8131 if (arg)
8132 break;
8133 ret = io_unregister_personality(ctx, nr_args);
8134 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008135 default:
8136 ret = -EINVAL;
8137 break;
8138 }
8139
Jens Axboe071698e2020-01-28 10:04:42 -07008140 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07008141 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07008142 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07008143out:
Jens Axboe0f158b42020-05-14 17:18:39 -06008144 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008145 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008146 return ret;
8147}
8148
8149SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
8150 void __user *, arg, unsigned int, nr_args)
8151{
8152 struct io_ring_ctx *ctx;
8153 long ret = -EBADF;
8154 struct fd f;
8155
8156 f = fdget(fd);
8157 if (!f.file)
8158 return -EBADF;
8159
8160 ret = -EOPNOTSUPP;
8161 if (f.file->f_op != &io_uring_fops)
8162 goto out_fput;
8163
8164 ctx = f.file->private_data;
8165
8166 mutex_lock(&ctx->uring_lock);
8167 ret = __io_uring_register(ctx, opcode, arg, nr_args);
8168 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02008169 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
8170 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07008171out_fput:
8172 fdput(f);
8173 return ret;
8174}
8175
Jens Axboe2b188cc2019-01-07 10:46:33 -07008176static int __init io_uring_init(void)
8177{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008178#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
8179 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
8180 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
8181} while (0)
8182
8183#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
8184 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
8185 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
8186 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
8187 BUILD_BUG_SQE_ELEM(1, __u8, flags);
8188 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
8189 BUILD_BUG_SQE_ELEM(4, __s32, fd);
8190 BUILD_BUG_SQE_ELEM(8, __u64, off);
8191 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
8192 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008193 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008194 BUILD_BUG_SQE_ELEM(24, __u32, len);
8195 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
8196 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
8197 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
8198 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8199 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8200 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
8201 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
8202 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
8203 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
8204 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
8205 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
8206 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
8207 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008208 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008209 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
8210 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
8211 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03008212 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01008213
Jens Axboed3656342019-12-18 09:50:26 -07008214 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -07008215 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008216 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
8217 return 0;
8218};
8219__initcall(io_uring_init);